Added supports_input/output methods to DeviceTrait (#920)

Time to get list of WASAPI devices on my machine (5 devices in total) before change:
Input devices: 12 ms, output devices: 187 ms

After change:
Input devices: 1 ms, output devices: 1 ms
This commit is contained in:
Jesper Nordenberg 2024-11-03 15:09:27 +01:00 • committed by GitHub
parent ca77d848ad
commit 582e93c41d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 14 deletions

View file

@ -58,6 +58,14 @@ impl DeviceTrait for Device {
Device::name(self)
}
fn supports_input(&self) -> bool {
self.data_flow() == Audio::eCapture
}
fn supports_output(&self) -> bool {
self.data_flow() == Audio::eRender
}
fn supported_input_configs(
&self,
) -> Result<Self::SupportedInputConfigs, SupportedStreamConfigsError> {

View file

@ -315,6 +315,24 @@ macro_rules! impl_platform_host {
}
}
fn supports_input(&self) -> bool {
match self.0 {
$(
$(#[cfg($feat)])?
DeviceInner::$HostVariant(ref d) => d.supports_input(),
)*
}
}
fn supports_output(&self) -> bool {
match self.0 {
$(
$(#[cfg($feat)])?
DeviceInner::$HostVariant(ref d) => d.supports_output(),
)*
}
}
fn supported_input_configs(&self) -> Result<Self::SupportedInputConfigs, crate::SupportedStreamConfigsError> {
match self.0 {
$(

View file

@ -59,13 +59,7 @@ pub trait HostTrait {
///
/// Can be empty if the system does not support audio input.
fn input_devices(&self) -> Result<InputDevices<Self::Devices>, DevicesError> {
fn supports_input<D: DeviceTrait>(device: &D) -> bool {
device
.supported_input_configs()
.map(|mut iter| iter.next().is_some())
.unwrap_or(false)
}
Ok(self.devices()?.filter(supports_input::<Self::Device>))
Ok(self.devices()?.filter(DeviceTrait::supports_input))
}
/// An iterator yielding all `Device`s currently available to the system that support one or more
@ -73,13 +67,7 @@ pub trait HostTrait {
///
/// Can be empty if the system does not support audio output.
fn output_devices(&self) -> Result<OutputDevices<Self::Devices>, DevicesError> {
fn supports_output<D: DeviceTrait>(device: &D) -> bool {
device
.supported_output_configs()
.map(|mut iter| iter.next().is_some())
.unwrap_or(false)
}
Ok(self.devices()?.filter(supports_output::<Self::Device>))
Ok(self.devices()?.filter(DeviceTrait::supports_output))
}
}
@ -101,6 +89,20 @@ pub trait DeviceTrait {
/// The human-readable name of the device.
fn name(&self) -> Result<String, DeviceNameError>;
/// True if the device supports audio input, otherwise false
fn supports_input(&self) -> bool {
self.supported_input_configs()
.map(|mut iter| iter.next().is_some())
.unwrap_or(false)
}
/// True if the device supports audio output, otherwise false
fn supports_output(&self) -> bool {
self.supported_output_configs()
.map(|mut iter| iter.next().is_some())
.unwrap_or(false)
}
/// An iterator yielding formats that are supported by the backend.
///
/// Can return an error if the device is no longer valid (e.g. it has been disconnected).