Skip to content

Capture Targets, Scopes & Endpoint Tracking

CaptureTarget is the single deterministic value type used across discovery, plan inspection (inspect()), single-session AudioStreams, and managed multi-source CaptureManager sessions.


TargetKind Factory Helper What It Captures
DefaultOutput default_output Audio playing on the OS default speakers or headphones (Follow default or Pin).
Device device(id) Loopback output from a specific named render endpoint (device:<id>).
Application find_app(name, ...) / application(...) Audio produced by a top-level desktop application (Spotify, Zoom, Firefox), resolved with parent-tree roll-up.
Process process(pid, ...) Audio produced by an explicit OS process ID ((pid, process_start_time) validated).
DefaultInput microphone Audio from the OS default microphone / capture input (Follow default or Pin).
InputDevice input_device(id) Audio from a specific physical or virtual input endpoint (input_device:<id>, excluding .monitor loopback sinks).

import capture_system_or_process_audio as cspa
# 1. Default speakers & default microphone (Follow OS switches automatically):
speakers = cspa.default_output(default_device_mode=cspa.DefaultDeviceMode.FOLLOW)
mic = cspa.microphone(default_device_mode="follow", gain=1.25)
# 2. Specific output or input device by ID (e.g., from cspa.discover()):
pinned_spk = cspa.device("speakers-endpoint-id")
pinned_mic = cspa.input_device("usb-mic-id", gain=0.8)
# 3. Application or PID target with scope & root-exit policy:
proc = cspa.process(
4242,
scope=cspa.ProcessScope.TREE,
on_root_exit=cspa.RootExitPolicy.WAIT,
wait_timeout_ms=5000,
)

3. Process Scopes: Tree vs Exact Zero Silent Fallback

Section titled “3. Process Scopes: Tree vs Exact Zero Silent Fallback ”

Modern desktop apps (Chromium/Electron apps, Firefox plugin-container, Zoom, games with launcher helpers) often render audio from a child helper process rather than the root UI PID.

  • ProcessScope::Tree (Default): Captures the target process and its descendant audio helper processes:
    • Windows (WASAPI Build 20348+): Uses native kernel include-tree loopback (PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE).
    • Linux (PipeWire / PulseAudio): Dynamically tracks /proc/<pid>/stat ancestry and links all matching streams into a PipeWire support.null-audio-sink (SharedNativeGraphMix) or CspaMixer.
    • macOS (Core Audio 14.2+): Dynamically maintains [parent_obj, child_obj, ...] AudioObjectID sets on the CATapDescription.
  • ProcessScope::Exact: Captures strictly the specified PID or non-tree application bundle without descendant expansion:
    • Supported on Linux (PipeWire & PulseAudio), macOS Core Audio (14.2+), and macOS ScreenCaptureKit (13.0+, Application targets).
    • Explicitly rejected on Windows WASAPI: Because Microsoft’s ActivateAudioInterfaceAsync process loopback API only exposes tree inclusion/exclusion in the kernel, requesting scope="exact" on Windows fails deterministically with CapabilityError::UnsupportedScope (PlanErrorCode.UNSUPPORTED_SCOPE) — never silently broadening to Tree or system loopback.

Both default_output(...) and microphone(...) accept DefaultDeviceMode:

Mode Behavior When User Plugs In USB/Bluetooth Audio or Switches OS Default
Follow (Default) CaptureManager subscribes to native OS default-device notifications off the real-time audio thread (IMMNotificationClient on Windows, PipeWire/PulseAudio default metadata on Linux, AudioObjectAddPropertyListener on macOS). On change, it cleanly drains/closes the old endpoint, opens the new default endpoint, and flags BoundaryKind::DeviceChanged on the transition read.
Pin Resolves the current default endpoint ID once when opening the session (device(current_default_id) / input_device(current_default_id)) and stays locked to that hardware device even if the user later changes the OS default.

5. Root Process Exit & Identity-Safe Relaunch (OnRootExit)

Section titled “5. Root Process Exit & Identity-Safe Relaunch (OnRootExit)”

Every Process and Application target carries an OnRootExit policy plus (pid, process_start_time) generation metadata:

  • Stop (Default): When the target root process exits, CaptureManager drains all already-captured frames and cleanly ends that target’s stream (or degrades gracefully in multi-input mixes when OnInputFailure::Degrade is active).
  • Wait { wait_timeout } ("wait" + wait_timeout_ms): If an application restarts (for example, an auto-updating desktop client or a game transitioning from launcher to engine), CaptureManager waits up to wait_timeout for a unique successor matching the application identity created after the exit timestamp, revalidating (pid, process_start_time) so OS PID recycling never hijacks an unrelated process.

6. Per-Target Backend Pinning & Static Gain

Section titled “6. Per-Target Backend Pinning & Static Gain”
  • Backend Pinning (target.with_backend(...)): On multi-backend platforms (Linux pipewire + pulseaudio; macOS coreaudio + screencapturekit), a target defaults to backend = None (allowing the planner to pick the highest-fidelity backend in request.backends). Calling with_backend(...) pins that specific target to one backend and prefixes its canonical ID (<backend>:<kind>:...), enabling intentional cross-backend mixes such as [app.with_backend("screencapturekit"), mic.with_backend("coreaudio")].
  • Static Gain (target.with_gain(gain)): Applies a finite non-negative linear gain multiplier (0.0 <= gain <= 16.0, default 1.0). Setting non-unity gain (gain != 1.0) automatically routes single-target or multi-target sessions through CspaMixer (f32 accumulation).

Continue to Live Target Discovery & Subscriptions.