1 extern crate winapi;
2 
3 pub use self::device::{
4     default_input_device, default_output_device, Device, Devices, SupportedInputConfigs,
5     SupportedOutputConfigs,
6 };
7 pub use self::stream::Stream;
8 use self::winapi::um::winnt::HRESULT;
9 use std::io::Error as IoError;
10 use traits::HostTrait;
11 use BackendSpecificError;
12 use DevicesError;
13 
14 mod com;
15 mod device;
16 mod stream;
17 
18 /// The WASAPI host, the default windows host type.
19 ///
20 /// Note: If you use a WASAPI output device as an input device it will
21 /// transparently enable loopback mode (see
22 /// https://docs.microsoft.com/en-us/windows/win32/coreaudio/loopback-recording).
23 #[derive(Debug)]
24 pub struct Host;
25 
26 impl Host {
new() -> Result<Self, crate::HostUnavailable>27     pub fn new() -> Result<Self, crate::HostUnavailable> {
28         Ok(Host)
29     }
30 }
31 
32 impl HostTrait for Host {
33     type Devices = Devices;
34     type Device = Device;
35 
is_available() -> bool36     fn is_available() -> bool {
37         // Assume WASAPI is always available on Windows.
38         true
39     }
40 
devices(&self) -> Result<Self::Devices, DevicesError>41     fn devices(&self) -> Result<Self::Devices, DevicesError> {
42         Devices::new()
43     }
44 
default_input_device(&self) -> Option<Self::Device>45     fn default_input_device(&self) -> Option<Self::Device> {
46         default_input_device()
47     }
48 
default_output_device(&self) -> Option<Self::Device>49     fn default_output_device(&self) -> Option<Self::Device> {
50         default_output_device()
51     }
52 }
53 
54 #[inline]
check_result(result: HRESULT) -> Result<(), IoError>55 fn check_result(result: HRESULT) -> Result<(), IoError> {
56     if result < 0 {
57         Err(IoError::from_raw_os_error(result))
58     } else {
59         Ok(())
60     }
61 }
62 
check_result_backend_specific(result: HRESULT) -> Result<(), BackendSpecificError>63 fn check_result_backend_specific(result: HRESULT) -> Result<(), BackendSpecificError> {
64     match check_result(result) {
65         Ok(()) => Ok(()),
66         Err(err) => Err(BackendSpecificError {
67             description: format!("{}", err),
68         }),
69     }
70 }
71