Skip to content

Commit 582e93c

Browse files
authored
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
1 parent ca77d84 commit 582e93c

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/host/wasapi/device.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ impl DeviceTrait for Device {
5858
Device::name(self)
5959
}
6060

61+
fn supports_input(&self) -> bool {
62+
self.data_flow() == Audio::eCapture
63+
}
64+
65+
fn supports_output(&self) -> bool {
66+
self.data_flow() == Audio::eRender
67+
}
68+
6169
fn supported_input_configs(
6270
&self,
6371
) -> Result<Self::SupportedInputConfigs, SupportedStreamConfigsError> {

src/platform/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,24 @@ macro_rules! impl_platform_host {
315315
}
316316
}
317317

318+
fn supports_input(&self) -> bool {
319+
match self.0 {
320+
$(
321+
$(#[cfg($feat)])?
322+
DeviceInner::$HostVariant(ref d) => d.supports_input(),
323+
)*
324+
}
325+
}
326+
327+
fn supports_output(&self) -> bool {
328+
match self.0 {
329+
$(
330+
$(#[cfg($feat)])?
331+
DeviceInner::$HostVariant(ref d) => d.supports_output(),
332+
)*
333+
}
334+
}
335+
318336
fn supported_input_configs(&self) -> Result<Self::SupportedInputConfigs, crate::SupportedStreamConfigsError> {
319337
match self.0 {
320338
$(

src/traits.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,15 @@ pub trait HostTrait {
5959
///
6060
/// Can be empty if the system does not support audio input.
6161
fn input_devices(&self) -> Result<InputDevices<Self::Devices>, DevicesError> {
62-
fn supports_input<D: DeviceTrait>(device: &D) -> bool {
63-
device
64-
.supported_input_configs()
65-
.map(|mut iter| iter.next().is_some())
66-
.unwrap_or(false)
67-
}
68-
Ok(self.devices()?.filter(supports_input::<Self::Device>))
62+
Ok(self.devices()?.filter(DeviceTrait::supports_input))
6963
}
7064

7165
/// An iterator yielding all `Device`s currently available to the system that support one or more
7266
/// output stream formats.
7367
///
7468
/// Can be empty if the system does not support audio output.
7569
fn output_devices(&self) -> Result<OutputDevices<Self::Devices>, DevicesError> {
76-
fn supports_output<D: DeviceTrait>(device: &D) -> bool {
77-
device
78-
.supported_output_configs()
79-
.map(|mut iter| iter.next().is_some())
80-
.unwrap_or(false)
81-
}
82-
Ok(self.devices()?.filter(supports_output::<Self::Device>))
70+
Ok(self.devices()?.filter(DeviceTrait::supports_output))
8371
}
8472
}
8573

@@ -101,6 +89,20 @@ pub trait DeviceTrait {
10189
/// The human-readable name of the device.
10290
fn name(&self) -> Result<String, DeviceNameError>;
10391

92+
/// True if the device supports audio input, otherwise false
93+
fn supports_input(&self) -> bool {
94+
self.supported_input_configs()
95+
.map(|mut iter| iter.next().is_some())
96+
.unwrap_or(false)
97+
}
98+
99+
/// True if the device supports audio output, otherwise false
100+
fn supports_output(&self) -> bool {
101+
self.supported_output_configs()
102+
.map(|mut iter| iter.next().is_some())
103+
.unwrap_or(false)
104+
}
105+
104106
/// An iterator yielding formats that are supported by the backend.
105107
///
106108
/// Can return an error if the device is no longer valid (e.g. it has been disconnected).

0 commit comments

Comments
 (0)