hex/src/linux/pipewire.rs
Dax 8d62e7ec22
fix(linux): pass pw-cat --raw only where pipewire knows it (#1803)
* fix(meetings): pass pw-cat's --raw only where PipeWire knows it

`--raw` only exists from PipeWire 1.3.81 on. 1.2 and older reject it as an
unknown option and exit before doing anything, so meetings recorded the
microphone alone and the Hex Microphone source never started. Since 1.4
`-` also means a WAV file unless `--raw` is passed, so the flag cannot just
be dropped: probe `pw-cat --help` once per process and pass it only where
the usage lists it, keeping it when the probe cannot run.

Also stop resetting a meeting source's backoff on every successful open: a
source that dies right after each spawn now keeps backing off instead of
respawning and logging twice a second forever.

* fix(linux): test pw-cat's real help and trust only a successful probe

The fixtures are now the exact `pw-cat --help` output of 1.0.5 and 1.4.0
(show_usage with its format args filled in, from a build without
-Dpw-cat-ffmpeg as Arch and Ubuntu build it), not an excerpt, so the
option the probe looks for is checked against the text it really prints.

The probe also asks for the untranslated usage with LC_ALL=C, and keeps
--raw when the help process starts but exits non-success, as its comment
already claimed.
2026-11-13 16:30:28 -05:00

139 lines
6.4 KiB
Rust

//! The one `pw-cat` flag whose meaning changed under Hex's two Linux audio
//! paths: `--raw`.
//!
//! Up to PipeWire 1.2, `pw-cat` (and its `pw-record` symlink) treats `-` as
//! raw pipe I/O and has no `--raw` option, so passing it is an unknown
//! option and the tool exits before recording or playing anything. From
//! 1.3.81 on, `-` goes through libsndfile as a WAV file and `--raw` brings
//! back the raw pipe. The flag is therefore passed only when the installed
//! `pw-cat`'s own help lists it.
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::OnceLock;
/// The arguments that make `executable` speak raw `f32` on `-`: `--raw`
/// where PipeWire knows it, nothing where `-` is already raw.
///
/// One answer per process: every caller runs the same `pw-cat` binary,
/// whichever name it was found under.
pub(crate) fn raw_args(executable: impl AsRef<Path>) -> &'static [&'static str] {
static RAW: OnceLock<bool> = OnceLock::new();
if *RAW.get_or_init(|| offers_raw(executable.as_ref())) {
&["--raw"]
} else {
&[]
}
}
/// Whether `pw-cat --help` lists `--raw`.
fn offers_raw(executable: &Path) -> bool {
let help = Command::new(executable)
// The usage text is translated, so ask for the untranslated option
// names the probe looks for.
.env("LC_ALL", "C")
.arg("--help")
.stdin(Stdio::null())
.output();
let Ok(help) = help else {
// A `pw-cat` that cannot even be run keeps the flag: a newer
// PipeWire is the common case, and its output needs the flag to stay
// raw instead of becoming a WAV file.
return true;
};
if !help.status.success() {
// A help that failed says nothing trustworthy about the flags.
return true;
}
let stdout = String::from_utf8_lossy(&help.stdout);
let stderr = String::from_utf8_lossy(&help.stderr);
lists_raw(&stdout) || lists_raw(&stderr)
}
/// Whether a usage text lists `--raw`, as PipeWire 1.3.81's does.
fn lists_raw(help: &str) -> bool {
help.contains("--raw")
}
#[cfg(test)]
mod tests {
use super::*;
/// The exact `pw-cat --help` output of PipeWire 1.0.5's `show_usage`
/// (`src/tools/pw-cat.c`) with its defaults filled in, from a build
/// without `-Dpw-cat-ffmpeg` (Arch's and Ubuntu's), so no
/// `-o, --encoded` line: `-` is raw pipe I/O and `--raw` does not exist.
const USAGE_1_0_5: &str = r#"pw-cat [options] [<file>|-]
-h, --help Show this help
--version Show version
-v, --verbose Enable verbose operations
-R, --remote Remote daemon name
--media-type Set media type (default Audio)
--media-category Set media category (default Playback)
--media-role Set media role (default Music)
--target Set node target serial or name (default auto)
0 means don't link
--latency Set node latency (default 100ms)
Xunit (unit = s, ms, us, ns)
or direct samples (256)
the rate is the one of the source file
-P --properties Set node properties
--rate Sample rate (req. for rec) (default 48000)
--channels Number of channels (req. for rec) (default 2)
--channel-map Channel map
one of: "stereo", "surround-51",... or
comma separated list of channel names: eg. "FL,FR"
--format Sample format (ulaw|alaw|u8|s8|s16|s32|f32|f64) (req. for rec) (default s16)
--volume Stream volume 0-1.0 (default 1.000)
-q --quality Resampler quality (0 - 15) (default 4)
-p, --playback Playback mode
-r, --record Recording mode
-m, --midi Midi mode
-d, --dsd DSD mode
"#;
/// The same from PipeWire 1.4.0, whose `show_usage` adds `--raw`.
const USAGE_1_4_0: &str = r#"pw-cat [options] [<file>|-]
-h, --help Show this help
--version Show version
-v, --verbose Enable verbose operations
-R, --remote Remote daemon name
--media-type Set media type (default Audio)
--media-category Set media category (default Playback)
--media-role Set media role (default Music)
--target Set node target serial or name (default auto)
0 means don't link
--latency Set node latency (default 100ms)
Xunit (unit = s, ms, us, ns)
or direct samples (256)
the rate is the one of the source file
-P --properties Set node properties
--rate Sample rate (req. for rec) (default 48000)
--channels Number of channels (req. for rec) (default 2)
--channel-map Channel map
one of: "stereo", "surround-51",... or
comma separated list of channel names: eg. "FL,FR"
--format Sample format (ulaw|alaw|u8|s8|s16|s32|f32|f64) (req. for rec) (default s16)
--volume Stream volume 0-1.0 (default 1.000)
-q --quality Resampler quality (0 - 15) (default 4)
-a, --raw RAW mode
-p, --playback Playback mode
-r, --record Recording mode
-m, --midi Midi mode
-d, --dsd DSD mode
"#;
#[test]
fn raw_is_passed_only_to_pipewire_that_lists_it() {
assert!(!lists_raw(USAGE_1_0_5));
assert!(lists_raw(USAGE_1_4_0));
}
}