minutes icon indicating copy to clipboard operation
minutes copied to clipboard

Linux: capture audio from PipeWire monitor sources

Open silverstein opened this issue 8 months ago • 3 comments

Context

Follow-up to #62 (PipeWire auto-detection). Once monitor sources are detected via pactl, we still need to actually capture audio from them. cpal's ALSA backend can't open PulseAudio source names directly.

Problem

detect_loopback_device() will return something like alsa_output.pci-0000_00_1b.0.analog-stereo.monitor, but passing that to cpal as a device name will fail because ALSA and PulseAudio are different device namespaces.

Options

A. PULSE_SOURCE env var + pulse ALSA device

Set PULSE_SOURCE=<monitor_name> before opening the pulse ALSA virtual device via cpal. PipeWire's PulseAudio compat layer should honor this. Minimal code change, but depends on the pulse ALSA plugin being configured.

B. parec subprocess

Shell out to parec --format=s16le --rate=16000 --channels=1 --device=<monitor> and pipe stdout into our processing pipeline. Works with both PulseAudio and PipeWire. Adds a subprocess dependency but is the most portable option.

C. libpulse crate

Use libpulse-binding as an alternative audio backend on Linux. Most correct solution but largest code change and adds a native dependency.

Recommendation

Start with option A (simplest). Fall back to B if the pulse ALSA device isn't reliably available. Option C is overkill unless A and B both fail.

Relevant code

  • crates/core/src/capture.rs - select_input_device() and record_to_wav()
  • The device name returned by detect_loopback_device() needs to be translated into something cpal can open

Depends on

  • #62 (detection must land first)

silverstein avatar Apr 03 '26 16:04 silverstein

Update: #75 merged, giving cpal a native PipeWire backend. This does not solve monitor capture.

@Dieterbe confirmed in #43 and PR #75 that PipeWire monitors are ports on sink nodes, not separate devices. cpal cannot open them regardless of backend. The original problem statement ("cpal's ALSA backend can't open PulseAudio source names") is now superseded by a more fundamental constraint: there is no monitor "device" to open.

The three options listed in the issue body need revision. With the native PipeWire backend active, the most promising paths are:

  • pw-record / pw-cat subprocess to capture from a specific monitor port
  • PipeWire's native API to programmatically connect a monitor port to a capture stream
  • PULSE_SOURCE env var (Option A) may still work through PipeWire's PulseAudio compatibility layer

This needs someone with a PipeWire system to validate which approach actually works. Dieter noted he hasn't dug into the port-level side yet.

silverstein avatar Apr 04 '26 21:04 silverstein

Update: original diagnosis looks outdated for cpal's PipeWire host

After #75 brought cpal's PipeWire host into our build and #85 taught the categorizer to surface sinks as SystemAudio, the ALSA-versus-Pulse namespace mismatch this issue describes does not appear to be the right diagnosis on PipeWire systems. For the PipeWire-host path, the original workarounds (PULSE_SOURCE env, parec subprocess, libpulse crate) do not appear necessary just to open a capture-capable device.

The remaining open question is the same one tracked in #62: how Minutes should actually route Linux call capture once those devices can be opened directly.

Pure-ALSA Linux setups (no Pulse/PipeWire compat layer) are a separate question. If this issue stays open, a title focused on Linux call-capture routing would better match the remaining question than the current *.monitor framing.

Related to #62, but not the same scope: #62 is detection and categorization, #69 was about post-detection capture.

Reconciling with the Apr 4 update above

Same correction as in #62: my Apr 4 statement that "cpal cannot open [PipeWire monitors] regardless of backend" was wrong. cpal exposes Audio/Sink nodes as Duplex devices via its native PipeWire host, and opening an input stream on one captures audio playing to the sink (STREAM_CAPTURE_SINK is set in pw_properties for sink-role streams). This was true in 63ea9ff and is still true in 8b152c3.

So the framing of this issue was right in spirit. For the native PipeWire-host path, the original capture-via-Pulse options don't appear necessary just to open a capture-capable device. The reason isn't "cpal can't open them," it's that cpal opens them transparently via the sink-as-Duplex path. The remaining open question (routing detected SystemAudio devices through minutes record --call) is unchanged, and we don't yet have a post-#85 end-to-end capture test on real PipeWire hardware.

silverstein avatar Apr 07 '26 15:04 silverstein

Closing as superseded. Original diagnosis ("cpal's ALSA backend can't open PulseAudio source names") no longer matches reality on the cpal PipeWire host path. After #75 brought in the native PipeWire backend and #85 + #87 taught the categorizer to treat Audio/Sink nodes as SystemAudio, there is no separate monitor-source capture to implement. The remaining piece (wiring [recording.sources].call = "auto" into the runtime path so minutes record --call actually routes to the detected sink) is tracked in #62.

silverstein avatar Apr 16 '26 17:04 silverstein