Skip to content
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
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions rs/moq-audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ capture = [
# `capture`: a consumer that only encodes shouldn't pull cpal, or the ALSA build
# dependency on Linux.
playback = ["dep:cpal", "dep:fixed-resample", "dep:tokio", "dep:tracing"]
# Reach devices through the Linux sound server rather than its ALSA plugin, for
# the server's own device names and routing. cpal picks the host at runtime, so
# these only cost a link against the client library.
pipewire = ["cpal/pipewire"]
pulseaudio = ["cpal/pulseaudio"]
Comment thread
kixelated marked this conversation as resolved.
Resolved
# Acoustic echo cancellation: subtract what `playback` is playing from what
# `capture` hears, so a speakerphone doesn't send its own output back. Needs
# both halves, since the canceller taps the output mix as its reference.
Expand Down
107 changes: 82 additions & 25 deletions rs/moq-audio/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,15 @@ pub struct Device {
/// Human-readable name, e.g. "MacBook Pro Microphone".
pub name: String,
/// Whether this is the system default input.
///
/// True for at most one device: the preferred host's default. Another host's
/// default is that host's, not the system's.
pub default: bool,
/// The host API this device is reached through, e.g. "PipeWire" or "ALSA".
///
/// The same hardware is usually reachable through several, so a caller that
/// offers a choice groups by this.
pub host: String,
}

impl Device {
Expand All @@ -482,21 +490,59 @@ pub async fn devices() -> Result<Vec<Device>, Error> {

/// The blocking half of [`devices`].
fn list() -> Result<Vec<Device>, Error> {
let host = cpal::default_host();
let default = host.default_input_device().and_then(|device| device.id().ok());
Ok(host
.input_devices()
.map_err(capture_err)?
// A device being reconfigured can fail `id`/`description` on its own, so
// skip it rather than losing every other input from the listing.
.filter_map(|device| match describe(&device, default.as_ref()) {
Ok(device) => Some(device),
// Every host, not just the preferred one, matching the output side. The same
// hardware appears under each, and which one a caller wants is its decision:
// PipeWire and PulseAudio carry the server's own names and routing, ALSA
// reaches a device directly. `Device::host` is what lets a caller group them.
let preferred = cpal::default_host().id();
let mut devices = Vec::new();
// A sound server reports one id per stream, not per device, so a client with
// several open would otherwise appear once per stream.
let mut seen = std::collections::HashSet::new();

for id in cpal::available_hosts() {
// A host that will not open takes every device on it with it, so say so:
// the symptom is a device missing from the listing with no other trace.
let host = match cpal::host_from_id(id) {
Ok(host) => host,
Err(err) => {
tracing::debug!(error = %err, "skipping an input device that could not be described");
None
tracing::debug!(host = id.name(), error = %err, "skipping an audio host that would not open");
continue;
}
})
.collect())
};
let default = host.default_input_device().and_then(|device| device.id().ok());

let inputs = match host.input_devices() {
Ok(inputs) => inputs,
Err(err) => {
tracing::debug!(host = id.name(), error = %err, "skipping a host that would not list its inputs");
continue;
}
};
for device in inputs {
let device_id = match device.id() {
Ok(device_id) => device_id,
Err(err) => {
tracing::debug!(host = id.name(), error = %err, "skipping an input device with no id");
continue;
}
};
if !seen.insert(device_id.to_string()) {
continue;
}
// Only the preferred host's default is the system default; the others
// are that host's idea of one.
let is_default = id == preferred && Some(&device_id) == default.as_ref();
match describe(&device, &device_id, is_default) {
Ok(device) => devices.push(device),
Err(err) => {
tracing::debug!(error = %err, "skipping an input device that could not be described");
}
}
}
}

Ok(devices)
}

/// Run blocking cpal host I/O off the runtime's worker threads.
Expand All @@ -517,7 +563,7 @@ fn resolve(
) -> Result<(cpal::Device, Device, cpal::SampleFormat, cpal::StreamConfig), Failure> {
let host = cpal::default_host();
let default = host.default_input_device().and_then(|device| device.id().ok());
let device = match selector {
let (device, id) = match selector {
// `Host::device_by_id` searches outputs too, so match against the inputs
// ourselves: an output id must not resolve as a microphone.
Some(selector) => {
Expand All @@ -526,16 +572,27 @@ fn resolve(
"{selector:?} is not an input device id; run `devices` to list them: {err}"
)))
})?;
host.input_devices()
// Ids are host-qualified, so route to the host that issued this one
// rather than searching the preferred host alone: `devices` lists
// every host, and a device it named has to be openable.
let host = cpal::host_from_id(wanted.host())
.map_err(|err| Failure::fatal(Error::Device(format!("{selector:?}: {err}"))))?;
let device = host
.input_devices()
.map_err(Failure::cpal)?
.find(|device| device.id().ok().as_ref() == Some(&wanted))
.ok_or_else(|| Failure::retry(Error::Device(format!("input device {selector:?} not found"))))?
.ok_or_else(|| Failure::retry(Error::Device(format!("input device {selector:?} not found"))))?;
(device, wanted)
}
None => {
let device = host
.default_input_device()
.ok_or_else(|| Failure::retry(Error::Device("no default input device".into())))?;
let id = device.id().map_err(Failure::cpal)?;
(device, id)
}
None => host
.default_input_device()
.ok_or_else(|| Failure::retry(Error::Device("no default input device".into())))?,
};
let current = describe(&device, default.as_ref()).map_err(Failure::cpal)?;
let current = describe(&device, &id, Some(&id) == default.as_ref()).map_err(Failure::cpal)?;

let supported = device.default_input_config().map_err(Failure::cpal)?;
let sample_format = supported.sample_format();
Expand All @@ -549,13 +606,13 @@ fn resolve(
Ok((device, current, sample_format, stream_config))
}

fn describe(device: &cpal::Device, default: Option<&cpal::DeviceId>) -> Result<Device, cpal::Error> {
let id = device.id()?;
let description = device.description()?;
/// Build the listing entry for `device`, whose id the caller has already read.
fn describe(device: &cpal::Device, id: &cpal::DeviceId, default: bool) -> Result<Device, cpal::Error> {
Ok(Device {
default: Some(&id) == default,
default,
name: device.description()?.name().into(),
host: id.host().name().to_string(),
id: id.to_string(),
name: description.name().into(),
})
}

Expand Down
1 change: 1 addition & 0 deletions rs/moq-audio/src/encode/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ mod tests {
id: name.into(),
name: name.into(),
default: false,
host: "test".into(),
}
}

Expand Down
50 changes: 44 additions & 6 deletions rs/moq-audio/src/playback/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ pub struct Device {
/// Human-readable name, e.g. "Built-in Output".
pub name: String,
/// Whether this is the system default output.
///
/// True for at most one device: the preferred host's default. Another host's
/// default is that host's, not the system's.
pub default: bool,
/// The host API this device is reached through, e.g. "PipeWire" or "ALSA".
///
/// The same hardware is usually reachable through several, so a caller that
/// offers a choice groups by this.
pub host: String,
}

/// List the audio outputs, across every host API the platform offers.
Expand All @@ -48,19 +56,49 @@ pub async fn devices() -> Result<Vec<Device>, Error> {
}

fn list() -> Result<Vec<Device>, Error> {
// Every host, not just the preferred one. The same hardware appears under
// each, and which one a caller wants is its decision: PipeWire and PulseAudio
// carry the server's own names and routing, ALSA reaches a device directly.
// `Device::host` is what lets a caller group or filter them.
let preferred = cpal::default_host().id();
let mut devices = Vec::new();
let mut seen = std::collections::HashSet::new();

for id in cpal::available_hosts() {
let Ok(host) = cpal::host_from_id(id) else { continue };
// A host that will not open takes every device on it with it, so say so:
// the symptom is a device missing from the listing with no other trace.
let host = match cpal::host_from_id(id) {
Ok(host) => host,
Err(err) => {
tracing::debug!(host = id.name(), error = %err, "skipping an audio host that would not open");
continue;
}
};
let default = host.default_output_device().and_then(|d| d.id().ok());
let Ok(outputs) = host.output_devices() else { continue };

let outputs = match host.output_devices() {
Ok(outputs) => outputs,
Err(err) => {
tracing::debug!(host = id.name(), error = %err, "skipping a host that would not list its outputs");
continue;
}
};
for device in outputs {
let Ok(id) = device.id() else { continue };
let Ok(device_id) = device.id() else {
tracing::debug!(host = id.name(), "skipping an output device with no id");
continue;
};
// A sound server reports one id per stream, not per device.
if !seen.insert(device_id.to_string()) {
continue;
}
devices.push(Device {
default: Some(&id) == default.as_ref(),
name: describe(&device, &id),
id: id.to_string(),
// Only the preferred host's default is the system default; the
// others are that host's idea of one.
default: id == preferred && Some(&device_id) == default.as_ref(),
name: describe(&device, &device_id),
host: id.name().to_string(),
id: device_id.to_string(),
});
}
}
Expand Down
8 changes: 7 additions & 1 deletion rs/moq-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ vaapi = ["moq-video?/vaapi", "moq-transcode?/vaapi"]
# Screen capture for `capture --display` on Linux (xdg-desktop-portal + PipeWire),
# on by default like the hardware codecs above and trimmable the same way. Only
# bites when `capture` pulls in moq-video; links libpipewire-0.3 at build time.
pipewire = ["moq-video?/pipewire"]
# Also reaches audio, so `devices`, microphone capture and playback go through
# the sound server rather than its ALSA plugin on a machine running one.
pipewire = ["moq-video?/pipewire", "moq-audio?/pipewire"]
# The other Linux sound server. Off by default, unlike `pipewire`: PulseAudio is
# what a machine runs when it is not running PipeWire, so enabling both by
# default would link a client library most builds never reach for.
pulseaudio = ["moq-audio?/pulseaudio"]

[dependencies]
anyhow = { workspace = true, features = ["backtrace"] }
Expand Down
Loading