4.3. Sound, Cameras, and Removable Devices

Audio, video capture, and removable storage are often described together as "desktop hardware," but the kernel exposes each through a different subsystem. Sound cards appear as ALSA devices, cameras as Video4Linux nodes, and removable disks as block devices with partitions and filesystems. User space adds a session-level service on top: PipeWire for audio and camera streams, and UDisks with polkit for mounting removable media.

4.3.1. PipeWire, Audio Routing, and Device Selection

PipeWire is a graph-based media server. It connects clients that produce or consume audio to devices and filters, and it can move streams between sinks and sources without restarting the applications. A Debian desktop normally runs three cooperating pieces, each a user service started with the graphical session:

  • pipewire provides the graph, devices, and streams.
  • pipewire-pulse provides the PulseAudio protocol, so applications and tools written for PulseAudio continue to work.
  • wireplumber is the session and policy manager. It selects devices, restores routes and volumes, and decides which stream belongs to which sink or source.

Check that all three are running; is-active prints one status per unit:

$ systemctl --user is-active pipewire pipewire-pulse wireplumber
active
active
active

ALSA remains the kernel interface to the sound card, and PipeWire normally owns the ALSA devices and mixes multiple clients before handing samples to the hardware.

PipeWire audio paths

Figure 4.3. Conceptual audio graph. Applications reach PipeWire through the PulseAudio-compatible layer or the native interface; WirePlumber chooses devices and routes, and auto_null is a fallback sink when no card exists.

The PulseAudio-compatible view reports the running server and the current routing. The head -12 stage of the pipeline keeps the report to its first twelve lines:

$ pactl info | head -12
Server String: /run/user/1001/pulse/native
Library Protocol Version: 35
Server Protocol Version: 35
Is Local: yes
Client Index: 48
Tile Size: 65472
User Name: john
Host Name: trixie
Server Name: PulseAudio (on PipeWire 1.4.2)
Server Version: 15.0.0
Default Sample Specification: float32le 2ch 48000Hz
Default Channel Map: front-left,front-right

Server Name: PulseAudio (on PipeWire 1.4.2) is the compatibility layer identifying its real server; it is not a sign that PulseAudio and PipeWire are both running. The server socket lives under john's runtime directory, /run/user/1001, matching the account's UID. The short listings show the graph through the same API:

$ pactl list short sinks
35  auto_null   PipeWire    float32le 2ch 48000Hz   SUSPENDED

A sink consumes audio and sends it to an output device; a source produces audio from an input device. auto_null is a virtual sink created by PipeWire's module-always-sink because this guest has no sound card, and it exists only while no other sink does. The index and state columns change when the graph is rebuilt, so use names in commands that should remain stable.

To exercise routing without hardware, create a second null sink. pactl load-module returns the module index, which is the handle needed to remove it later:

$ pactl load-module module-null-sink sink_name=ch04test sink_properties=device.description=CH04-Test
536870916

The number is a module index, not a process ID. The new sink appears at its own index - and auto_null has disappeared, because module-always-sink keeps its fallback only while no other sink exists:

$ pactl list short sinks
52  ch04test    PipeWire    float32le 2ch 48000Hz   SUSPENDED

Select the test sink as the default for new streams. The command is silent; the selection is visible through a separate query:

$ pactl set-default-sink ch04test
$ pactl get-default-sink
ch04test

Both the PulseAudio-compatible player and the native PipeWire player can target the sink by name. paplay takes a PulseAudio device name through --device, and pw-play takes a native target through --target; /usr/share/sounds/alsa/Front_Center.wav is a short test sound installed with alsa-utils. Successful playback is silent, so the absence of output is not a failure:

$ paplay --device=ch04test /usr/share/sounds/alsa/Front_Center.wav
$ pw-play --target=ch04test /usr/share/sounds/alsa/Front_Center.wav

Both commands returned status 0. They exercised the client-to-graph path and named-sink selection; they did not produce audible sound, because the null sink discards the samples. wpctl presents the native view, grouped into devices, sinks, sources, filters, and streams; head -20 keeps the listing to its first twenty lines:

$ wpctl status | head -20
PipeWire 'pipewire-0' [1.4.2, john@trixie, cookie:1090147151]
 └─ Clients:
        33. WirePlumber                         [1.4.2, john@trixie, pid:425634]
        34. pipewire                            [1.4.2, john@trixie, pid:425636]
        45. wpctl                               [1.4.2, john@trixie, pid:427258]
        47. WirePlumber [export]                [1.4.2, john@trixie, pid:425634]
        49. pipewire                            [1.4.2, john@trixie, pid:425636]

Audio
 ├─ Devices:
 │
 ├─ Sinks:
 │  *   50. CH04-Test                           [vol: 1.00]
 │
 ├─ Sources:
 │
 ├─ Filters:
 │
 └─ Streams:

The * marks the default sink. CH04-Test is the description supplied to the module, while ch04test is the node name used by pactl and pw-play; the numbered entries are PipeWire object IDs, which are not stable across restarts.

Removing the test sink has one trap worth seeing before it bites. Trying to restore the previous default first fails, because auto_null is not in the graph while the test sink exists:

$ pactl set-default-sink auto_null
Failure: No such entity

The error names a sink that is absent, not a broken server. Unload the module by its index instead; the command is silent, and module-always-sink then recreates the fallback sink, which becomes the default again on its own:

$ pactl unload-module 536870916
$ pactl list short sinks
100 auto_null   PipeWire    float32le 2ch 48000Hz   SUSPENDED
$ pactl get-default-sink
auto_null

The recreated sink has a new index, which is one more reason to refer to sinks by name. If the module index from load-module has been lost, pactl list short modules lists the loaded modules with their indices so the right one can be unloaded.

pactl command Description
info Show server name, default devices, and sample format.
list short sinks / list short sources List devices in a compact table.
list short modules List loaded modules with their indices.
load-module MODULE [arguments] Load a module and print its index.
unload-module INDEX Unload a module by index.
set-default-sink NAME Select the default output for new streams.
set-default-source NAME Select the default input for new streams.
move-sink-input ID NAME Move an existing playback stream to another sink.

On real hardware the same operations select internal speakers, headphones, an HDMI output, or a USB headset, and wpctl status shows which of those the kernel and PipeWire detected. When an expected device is missing, check the kernel before touching any configuration:

$ aplay -l
aplay: device_list:279: no soundcards found...

aplay -l lists ALSA playback devices from the kernel's view, and arecord -l does the same for capture devices; on this guest both report that no card exists, so no PipeWire configuration can create one. On a physical machine a missing card can be a device disabled in the BIOS or UEFI setup, a missing kernel module, or a permissions problem; check lspci -k for the audio controller, lsmod for the codec driver, and the kernel log for ALSA errors before reinstalling the desktop.

PipeWire's system-wide defaults live under /usr/share/pipewire/ and are overridden in /etc/pipewire/; WirePlumber's policy and device rules live under /usr/share/wireplumber/ and /etc/wireplumber/, with per-user configuration under ~/.config/pipewire/ and ~/.config/wireplumber/. After changing those files, restart the user services with systemctl --user restart pipewire pipewire-pulse wireplumber; the restart rebuilds the graph, so applications may need to reopen their audio streams. The null-sink exercise above needed no restart because the module was loaded and unloaded at runtime.

4.3.2. Webcams, Microphones, and Application Permissions

A USB webcam normally appears as a Video4Linux device node such as /dev/video0: the kernel's uvcvideo driver exposes capture formats and controls, and applications either open the node directly or receive a stream through a portal. The example guest has no camera node:

$ ls -l /dev/video*
ls: cannot access '/dev/video*': No such file or directory

The command exited with status 2 because the shell found no matching name. The only USB device is the bus's root hub, so there is no camera attached anywhere:

$ lsusb
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

v4l2-ctl --list-devices is the Video4Linux counterpart of lspci for capture devices. On this guest it prints nothing and exits 0, which means the enumeration ran and found no devices:

$ v4l2-ctl --list-devices
v4l2-ctl option Description
--list-devices List capture devices and their /dev/video* nodes.
--list-formats-ext Show pixel formats, resolutions, and frame rates for a device.
--all Print all controls and current values.
--device /dev/videoN Select a device when more than one exists.

On hardware, the same command names each camera and its node. A laptop's built-in camera may connect through USB or a platform-specific bus, but it still appears as a Video4Linux node. When v4l2-ctl shows a device that applications cannot open, check the node's ownership and mode, the user's groups, and whether a portal is mediating access: logind grants the user on the active seat an access-control entry on the device, and a session that is not attached to the seat - such as a bare SSH login - may not receive the same access.

Microphones follow the audio graph rather than Video4Linux: a capture device appears as an ALSA capture device and as a PipeWire source. This guest has no capture hardware either:

$ arecord -l
arecord: device_list:279: no soundcards found...

pactl list short sources on this machine shows only auto_null.monitor, which is not a microphone: a monitor source records the output of a sink, so using it as an input captures what the machine is playing, not what a person says. On hardware, pactl list short sources lists an analog or digital input, and the desktop's sound settings can unmute and select it; WirePlumber's routing policy then makes it the default source for new applications.

Modern desktops increasingly route camera and screen capture through xdg-desktop-portal. A sandboxed application asks the portal for a camera stream, the portal asks the user for permission, and PipeWire carries the stream. This is why a Flatpak application may not see /dev/video0 directly even when a non-sandboxed program can open it. The portal implementation depends on the desktop - GNOME and KDE ship their own backends, and a minimal session may have no camera portal at all - so check that xdg-desktop-portal and the desktop's backend are installed, and adjust the application's sandbox permissions rather than loosening the device node's permissions for every user.

Camera privacy is partly a hardware property. A built-in camera may have a physical shutter or a firmware-controlled indicator, and closing one application does not stop a stream if a portal or another process still holds the device. fuser -v /dev/video0 shows processes with the node open, and the desktop's camera indicator is a useful confirmation. These checks apply to real hardware; the camera-less guest could not exercise them.

4.3.3. Mounting and Safely Removing External Media

A removable disk is a block device with a partition table, partitions, and filesystems. The desktop does not mount the raw disk by magic: UDisks receives a udev event, creates a D-Bus object for the device, and a file manager or the user runs a mount operation; polkit decides whether the user is allowed to. Once mounted, the filesystem appears under a path such as /media/user/LABEL.

Removable media and safe removal

Figure 4.4. Conceptual removable-media path. UDisks and polkit authorize a mount; applications then use ordinary filesystem paths. A device must be flushed and unmounted before it is detached.

Start by seeing what block devices the kernel already knows:

$ lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
NAME    SIZE TYPE FSTYPE MOUNTPOINTS
vda     100G disk
└─vda1  100G part ext4   /

One virtual disk with one ext4 partition holding the root filesystem - no removable device is present. The exercise below uses a loop-backed file to exercise the block-device path without a USB stick; every step works the same way against a real device such as /dev/sdb, with the device name substituted. Create a 64 MiB sparse image in the workspace; truncate creates the file at full size without writing data blocks, and it is silent:

$ truncate -s 64M /home/john/debian-handbook/desktop-baseline/removable.img

Attach the image to a free loop device with partition scanning enabled:

losetup option Description
-f Find the first free loop device.
-P Scan the backing file for partitions and create partition devices.
--show Print the device name that was assigned.
-d DEVICE Detach a loop device.
-j FILE Show the loop device associated with a backing file.
$ sudo losetup -fP /home/john/debian-handbook/desktop-baseline/removable.img

The command is silent; ask which device was assigned:

$ sudo losetup -j /home/john/debian-handbook/desktop-baseline/removable.img
/dev/loop0: [65025]:655574 (/home/john/debian-handbook/desktop-baseline/removable.img)

The loop number can differ; the commands below use /dev/loop0 as it was assigned here. Create a DOS partition table and one FAT32 partition covering the image. parted -s runs without interactive prompts, mklabel msdos writes a new partition table - a destructive act on the target device, acceptable here because the image is disposable - and mkpart primary fat32 1MiB 100% creates the partition, starting at 1 MiB for alignment:

$ sudo parted -s /dev/loop0 mklabel msdos mkpart primary fat32 1MiB 100%

Ask the kernel to reread the partition table, then wait for udev to create the partition device; both commands are silent:

$ sudo partprobe /dev/loop0
$ sudo udevadm settle

Format the partition; this is destructive for that partition's contents:

$ sudo mkfs.vfat /dev/loop0p1
mkfs.fat 4.2 (2021-01-31)

A desktop file manager would mount the partition through UDisks after the user selects it. From a shell without a polkit authentication agent, the same operation is refused:

$ udisksctl mount -b /dev/loop0p1
Error mounting /dev/loop0p1: GDBus.Error:org.freedesktop.UDisks2.Error.NotAuthorized: Not authorized to perform operation (polkit authority not available and caller is not uid 0)

This refusal is not evidence that UDisks is broken. A graphical session starts a polkit agent that can ask the user to authenticate; a bare shell has no agent, so polkit cannot complete the authorization. Elevating the same operation is authorized without an agent:

$ sudo udisksctl mount -b /dev/loop0p1
Mounted /dev/loop0p1 at /media/root/15A7-49A6

The mount point is named after the filesystem UUID and sits under /media/root because root performed the mount; a desktop user's mount appears under /media/john with the user as owner. Confirm the mount with the live mount table:

$ findmnt /dev/loop0p1
TARGET                SOURCE       FSTYPE OPTIONS
/media/root/15A7-49A6 /dev/loop0p1 vfat   rw,nosuid,nodev,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,showexec,utf8,flush,errors=remount-ro

Write a file to the mounted filesystem. The mount point is owned by root, so the pipeline sends the text through sudo tee, which writes the file and echoes its input to standard output; the echo is redirected to /dev/null because the verification below reads the file instead. The command is otherwise silent:

$ printf 'removable media test\n' | sudo tee /media/root/15A7-49A6/note.txt >/dev/null
$ sudo cat /media/root/15A7-49A6/note.txt
removable media test

On a desktop mount owned by the user, an editor or file manager writes the file without sudo. Either way, writes can still be in kernel cache after the application closes the file, so the data is not safely on the device until the filesystem has been flushed.

Warning: Unmounting ends access to the filesystem, and detaching the device while it is still mounted can lose buffered writes and leave the filesystem inconsistent. Close applications that hold files open on the device, unmount, and confirm the unmount before detaching.

$ sudo udisksctl unmount -b /dev/loop0p1
Unmounted /dev/loop0p1.

A repeat of the findmnt query prints nothing and exits with status 1, which is how findmnt reports that nothing is mounted on that source:

$ findmnt /dev/loop0p1

Detach the loop device, which is silent, and remove the image from the workspace:

$ sudo losetup -d /dev/loop0
$ rm /home/john/debian-handbook/desktop-baseline/removable.img

For a real USB disk, the equivalent final steps are udisksctl unmount -b for each mounted partition followed by udisksctl power-off -b /dev/sdX, which tells the device it may be removed; it is not a substitute for unmounting. The file manager's eject action performs the same sequence.

udisksctl command Description
mount -b DEVICE Mount a filesystem through UDisks.
unmount -b DEVICE Unmount a mounted filesystem.
power-off -b DEVICE Power down a removable drive after its filesystems are unmounted.
info -b DEVICE Show UDisks properties for a block device.
status Show whether UDisks is running.

If a filesystem is not recognized, check lsblk -f and blkid before formatting anything. Debian's kernel supports many filesystems, but the tools for creating or repairing them are in separate packages: FAT32 needs dosfstools, exFAT normally needs exfatprogs, and NTFS can use the kernel's ntfs3 driver or the older ntfs-3g package. Installing a filesystem driver does not mount a device automatically, and the file manager's offer to format an unrecognized device destroys its contents, so read that confirmation carefully. Chapter 11 covers block devices, partitioning, and mount policy in depth.