Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added supports_input/output methods to DeviceTrait #920

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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