| Lift freeq's AV media plane out of sleek 90f8b89 nandi 19d ago | 1 | //! ASIO backend implementation. |
| 2 | //! |
| 3 | //! ASIO is available on Windows with the `asio` feature. |
| 4 | //! See the project README for setup instructions. |
| 5 | |
| 6 | extern crate asio_sys as sys; |
| 7 | |
| 8 | use crate::host::com; |
| 9 | use crate::traits::{DeviceTrait, HostTrait, StreamTrait}; |
| 10 | use crate::{ |
| 11 | BuildStreamError, Data, DefaultStreamConfigError, DeviceDescription, DeviceId, DeviceIdError, |
| 12 | DeviceNameError, DevicesError, InputCallbackInfo, OutputCallbackInfo, PauseStreamError, |
| 13 | PlayStreamError, SampleFormat, StreamConfig, StreamError, SupportedStreamConfig, |
| 14 | SupportedStreamConfigsError, |
| 15 | }; |
| 16 | |
| 17 | pub use self::device::{Device, Devices, SupportedInputConfigs, SupportedOutputConfigs}; |
| 18 | pub use self::stream::Stream; |
| 19 | use std::sync::{Arc, OnceLock}; |
| 20 | use std::time::Duration; |
| 21 | |
| 22 | mod device; |
| 23 | mod stream; |
| 24 | |
| 25 | /// Global ASIO instance shared across all Host instances. |
| 26 | /// |
| 27 | /// ASIO only supports loading a single driver at a time globally, so all Host instances |
| 28 | /// must share the same underlying sys::Asio wrapper to properly coordinate driver access. |
| 29 | static GLOBAL_ASIO: OnceLock<Arc<sys::Asio>> = OnceLock::new(); |
| 30 | |
| 31 | /// The host for ASIO. |
| 32 | #[derive(Debug)] |
| 33 | pub struct Host { |
| 34 | asio: Arc<sys::Asio>, |
| 35 | } |
| 36 | |
| 37 | impl Host { |
| 38 | pub fn new() -> Result<Self, crate::HostUnavailable> { |
| 39 | com::com_initialized(); |
| 40 | let asio = GLOBAL_ASIO |
| 41 | .get_or_init(|| Arc::new(sys::Asio::new())) |
| 42 | .clone(); |
| 43 | let host = Host { asio }; |
| 44 | Ok(host) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl HostTrait for Host { |
| 49 | type Devices = Devices; |
| 50 | type Device = Device; |
| 51 | |
| 52 | fn is_available() -> bool { |
| 53 | true |
| 54 | //unimplemented!("check how to do this using asio-sys") |
| 55 | } |
| 56 | |
| 57 | fn devices(&self) -> Result<Self::Devices, DevicesError> { |
| 58 | Devices::new(self.asio.clone()) |
| 59 | } |
| 60 | |
| 61 | fn default_input_device(&self) -> Option<Self::Device> { |
| 62 | // ASIO has no concept of a default device, so just use the first. |
| 63 | self.input_devices().ok().and_then(|mut ds| ds.next()) |
| 64 | } |
| 65 | |
| 66 | fn default_output_device(&self) -> Option<Self::Device> { |
| 67 | // ASIO has no concept of a default device, so just use the first. |
| 68 | self.output_devices().ok().and_then(|mut ds| ds.next()) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl DeviceTrait for Device { |
| 73 | type SupportedInputConfigs = SupportedInputConfigs; |
| 74 | type SupportedOutputConfigs = SupportedOutputConfigs; |
| 75 | type Stream = Stream; |
| 76 | |
| 77 | fn description(&self) -> Result<DeviceDescription, DeviceNameError> { |
| 78 | Device::description(self) |
| 79 | } |
| 80 | |
| 81 | fn id(&self) -> Result<DeviceId, DeviceIdError> { |
| 82 | Device::id(self) |
| 83 | } |
| 84 | |
| 85 | fn supported_input_configs( |
| 86 | &self, |
| 87 | ) -> Result<Self::SupportedInputConfigs, SupportedStreamConfigsError> { |
| 88 | Device::supported_input_configs(self) |
| 89 | } |
| 90 | |
| 91 | fn supported_output_configs( |
| 92 | &self, |
| 93 | ) -> Result<Self::SupportedOutputConfigs, SupportedStreamConfigsError> { |
| 94 | Device::supported_output_configs(self) |
| 95 | } |
| 96 | |
| 97 | fn default_input_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError> { |
| 98 | Device::default_input_config(self) |
| 99 | } |
| 100 | |
| 101 | fn default_output_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError> { |
| 102 | Device::default_output_config(self) |
| 103 | } |
| 104 | |
| 105 | fn build_input_stream_raw<D, E>( |
| 106 | &self, |
| 107 | config: StreamConfig, |
| 108 | sample_format: SampleFormat, |
| 109 | data_callback: D, |
| 110 | error_callback: E, |
| 111 | timeout: Option<Duration>, |
| 112 | ) -> Result<Self::Stream, BuildStreamError> |
| 113 | where |
| 114 | D: FnMut(&Data, &InputCallbackInfo) + Send + 'static, |
| 115 | E: FnMut(StreamError) + Send + 'static, |
| 116 | { |
| 117 | Device::build_input_stream_raw( |
| 118 | self, |
| 119 | config, |
| 120 | sample_format, |
| 121 | data_callback, |
| 122 | error_callback, |
| 123 | timeout, |
| 124 | ) |
| 125 | } |
| 126 | |
| 127 | fn build_output_stream_raw<D, E>( |
| 128 | &self, |
| 129 | config: StreamConfig, |
| 130 | sample_format: SampleFormat, |
| 131 | data_callback: D, |
| 132 | error_callback: E, |
| 133 | timeout: Option<Duration>, |
| 134 | ) -> Result<Self::Stream, BuildStreamError> |
| 135 | where |
| 136 | D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static, |
| 137 | E: FnMut(StreamError) + Send + 'static, |
| 138 | { |
| 139 | Device::build_output_stream_raw( |
| 140 | self, |
| 141 | config, |
| 142 | sample_format, |
| 143 | data_callback, |
| 144 | error_callback, |
| 145 | timeout, |
| 146 | ) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl StreamTrait for Stream { |
| 151 | fn play(&self) -> Result<(), PlayStreamError> { |
| 152 | Stream::play(self) |
| 153 | } |
| 154 | |
| 155 | fn pause(&self) -> Result<(), PauseStreamError> { |
| 156 | Stream::pause(self) |
| 157 | } |
| 158 | |
| 159 | fn buffer_size(&self) -> Option<crate::FrameCount> { |
| 160 | Stream::buffer_size(self) |
| 161 | } |
| 162 | } |