Skip to content

Commit

Permalink
Added supports_input/output methods to DeviceTrait (#920)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jesnor authored Nov 3, 2024
1 parent ca77d84 commit 582e93c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/host/wasapi/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
18 changes: 18 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
$(
Expand Down
30 changes: 16 additions & 14 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,15 @@ 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
/// output stream formats.
///
/// 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))
}
}

Expand All @@ -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).
Expand Down

0 comments on commit 582e93c

Please sign in to comment.