use std::time::Duration;
use crate::{
BuildStreamError, Data, DefaultStreamConfigError, DeviceDescription, DeviceId, DeviceIdError,
DeviceNameError, DevicesError, InputCallbackInfo, InputDevices, OutputCallbackInfo,
OutputDevices, PauseStreamError, PlayStreamError, SampleFormat, SizedSample, StreamConfig,
StreamError, SupportedStreamConfig, SupportedStreamConfigRange, SupportedStreamConfigsError,
};
pub trait HostTrait {
type Devices: Iterator<Item = Self::Device>;
type Device: DeviceTrait;
fn is_available() -> bool;
fn devices(&self) -> Result<Self::Devices, DevicesError>;
fn device_by_id(&self, id: &DeviceId) -> Option<Self::Device> {
self.devices()
.ok()?
.find(|device| device.id().ok().as_ref() == Some(id))
}
fn default_input_device(&self) -> Option<Self::Device>;
fn default_output_device(&self) -> Option<Self::Device>;
fn input_devices(&self) -> Result<InputDevices<Self::Devices>, DevicesError> {
Ok(self.devices()?.filter(DeviceTrait::supports_input))
}
fn output_devices(&self) -> Result<OutputDevices<Self::Devices>, DevicesError> {
Ok(self.devices()?.filter(DeviceTrait::supports_output))
}
}
pub trait DeviceTrait {
type SupportedInputConfigs: Iterator<Item = SupportedStreamConfigRange>;
type SupportedOutputConfigs: Iterator<Item = SupportedStreamConfigRange>;
type Stream: StreamTrait;
#[deprecated(
since = "0.17.0",
note = "Use `description()` for comprehensive device information including name, \
manufacturer, and device type. Use `id()` for a unique, stable device identifier \
that persists across reboots and reconnections."
)]
fn name(&self) -> Result<String, DeviceNameError> {
self.description().map(|desc| desc.name().to_string())
}
fn description(&self) -> Result<DeviceDescription, DeviceNameError>;
fn id(&self) -> Result<DeviceId, DeviceIdError>;
fn supports_input(&self) -> bool {
self.supported_input_configs()
.is_ok_and(|mut iter| iter.next().is_some())
}
fn supports_output(&self) -> bool {
self.supported_output_configs()
.is_ok_and(|mut iter| iter.next().is_some())
}
fn supported_input_configs(
&self,
) -> Result<Self::SupportedInputConfigs, SupportedStreamConfigsError>;
fn supported_output_configs(
&self,
) -> Result<Self::SupportedOutputConfigs, SupportedStreamConfigsError>;
fn default_input_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError>;
fn default_output_config(&self) -> Result<SupportedStreamConfig, DefaultStreamConfigError>;
fn build_input_stream<T, D, E>(
&self,
config: &StreamConfig,
mut data_callback: D,
error_callback: E,
timeout: Option<Duration>,
) -> Result<Self::Stream, BuildStreamError>
where
T: SizedSample,
D: FnMut(&[T], &InputCallbackInfo) + Send + 'static,
E: FnMut(StreamError) + Send + 'static,
{
self.build_input_stream_raw(
config,
T::FORMAT,
move |data, info| {
data_callback(
data.as_slice()
.expect("host supplied incorrect sample type"),
info,
)
},
error_callback,
timeout,
)
}
fn build_output_stream<T, D, E>(
&self,
config: &StreamConfig,
mut data_callback: D,
error_callback: E,
timeout: Option<Duration>,
) -> Result<Self::Stream, BuildStreamError>
where
T: SizedSample,
D: FnMut(&mut [T], &OutputCallbackInfo) + Send + 'static,
E: FnMut(StreamError) + Send + 'static,
{
self.build_output_stream_raw(
config,
T::FORMAT,
move |data, info| {
data_callback(
data.as_slice_mut()
.expect("host supplied incorrect sample type"),
info,
)
},
error_callback,
timeout,
)
}
fn build_input_stream_raw<D, E>(
&self,
config: &StreamConfig,
sample_format: SampleFormat,
data_callback: D,
error_callback: E,
timeout: Option<Duration>,
) -> Result<Self::Stream, BuildStreamError>
where
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
E: FnMut(StreamError) + Send + 'static;
fn build_output_stream_raw<D, E>(
&self,
config: &StreamConfig,
sample_format: SampleFormat,
data_callback: D,
error_callback: E,
timeout: Option<Duration>,
) -> Result<Self::Stream, BuildStreamError>
where
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
E: FnMut(StreamError) + Send + 'static;
}
pub trait StreamTrait {
fn play(&self) -> Result<(), PlayStreamError>;
fn pause(&self) -> Result<(), PauseStreamError>;
}
#[macro_export]
macro_rules! assert_stream_send {
($t:ty) => {
const fn _assert_stream_send<T: Send>() {}
const _: () = _assert_stream_send::<$t>();
};
}
#[macro_export]
macro_rules! assert_stream_sync {
($t:ty) => {
const fn _assert_stream_sync<T: Sync>() {}
const _: () = _assert_stream_sync::<$t>();
};
}