Skip to content

Commit b96b2af

Browse files
authored
Merge pull request #862 from RustAudio/filter-out-null
Cpal has (correctly) started listing the null device in 0.17. This messes up our Sink's fallback strategy since it can now land on the null device (which happily eats all samples without producing any sound.) This fixes that by filtering it out. Our microphone input has a different API, it does not have fallback. We provide a method to list all input devices which was now including null, a possible footgun if users try to implement fallback themselves. We've taken that out for now.
2 parents 1a659cb + 54a99a8 commit b96b2af

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/microphone.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,19 @@ impl fmt::Display for Input {
166166
}
167167

168168
/// Returns a list of available input devices on the system.
169+
///
170+
/// Note: this hides the 'null' device which generates zeros.
169171
pub fn available_inputs() -> Result<Vec<Input>, ListError> {
170-
let host = cpal::default_host();
171-
let devices = host
172+
Ok(cpal::default_host()
172173
.input_devices()
173174
.map_err(ListError)?
174-
.map(|dev| Input { inner: dev });
175-
Ok(devices.collect())
175+
.filter(|dev| {
176+
dev.description()
177+
.map(|descr| descr.driver().is_none_or(|driver| driver != "null"))
178+
.unwrap_or(false)
179+
})
180+
.map(|dev| Input { inner: dev })
181+
.collect::<Vec<_>>())
176182
}
177183

178184
/// A microphone input stream that can be used as `Source`.

src/stream.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl DeviceSinkBuilder {
234234
Self::from_default_device()
235235
.and_then(|x| x.open_stream())
236236
.or_else(|original_err| {
237-
let mut devices = match cpal::default_host().output_devices() {
237+
let devices = match cpal::default_host().output_devices() {
238238
Ok(devices) => devices,
239239
Err(err) => {
240240
#[cfg(feature = "tracing")]
@@ -245,6 +245,11 @@ impl DeviceSinkBuilder {
245245
}
246246
};
247247
devices
248+
.filter(|dev| {
249+
dev.description()
250+
.map(|desc| desc.driver().is_some_and(|driver| driver != "null"))
251+
.unwrap_or(false)
252+
})
248253
.find_map(|d| {
249254
Self::from_device(d)
250255
.and_then(|x| x.open_sink_or_fallback())

0 commit comments

Comments
 (0)