cliamp playback was implemented entirely in officer: it located the cliamp binary, validated the requested path against the owner's home, faked a PTY with `script`, injected PULSE_SINK and an ALSA config shipped inside the API tree, spawned parec to capture the sink, and set up the pulseaudio daemon and the virtual_out null sink at every boot — about 356 lines of audio-pipeline knowledge in a process that is meant to be a proxy, and none of it owned by the sidecar whose whole job is music. all of it now lives in sidecar/music: cliamp-ws.ts serves both sockets (/cliamp/ws for the player, /cliamp/audio/ws for the PCM capture) on the loopback server it already runs, pulse-audio.ts does the daemon + sink setup at sidecar startup instead of at officer's, and the asoundrc moved next to the code that passes it. officer keeps the part that is actually its job — authenticating the browser — and relays frames both ways without reading them (api/cliamp/relay.ts, same dumb-pipe shape as the vault notifications relay). the browser's frame contract is unchanged, so the frontend is not touched. two things fixed on the way: the traversal check now requires a separator after the home path, so a sibling directory whose name merely starts with it can no longer pass; and the music proxy no longer special-cases /reindex and /reindex/stream by name to extend the idle timeout — it extends the whole prefix, because a proxy should not know which of the sidecar's routes are slow. the music-specific `files` query param is out of the shared WS envelope too: upgradeWs now carries the raw query string, which any relayed provider can use. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
// Host audio plumbing for local playback: a PulseAudio daemon and a null sink named `virtual_out`.
|
|
// cliamp plays *into* that sink (PULSE_SINK) and the capture side reads `virtual_out.monitor`, so the
|
|
// sink has to exist before either of them starts — which is why this runs at sidecar startup rather
|
|
// than on first play. Both steps are idempotent and both failures are non-fatal: a host without
|
|
// pulseaudio simply has no browser playback, and everything else the music sidecar does still works.
|
|
|
|
export const VIRTUAL_SINK = 'virtual_out';
|
|
|
|
export function ensurePulseAudio(): void {
|
|
const pulseaudio = Bun.which('pulseaudio');
|
|
const pactl = Bun.which('pactl');
|
|
if (!pulseaudio || !pactl) {
|
|
console.log('[music] pulseaudio not installed, skipping audio setup');
|
|
return;
|
|
}
|
|
|
|
const check = Bun.spawnSync({ cmd: [pulseaudio, '--check'], stdout: 'ignore', stderr: 'ignore' });
|
|
if (check.exitCode !== 0) {
|
|
const start = Bun.spawnSync({ cmd: [pulseaudio, '--start', '-D'], stdout: 'ignore', stderr: 'ignore' });
|
|
if (start.exitCode !== 0) {
|
|
console.error('[music] failed to start pulseaudio');
|
|
return;
|
|
}
|
|
console.log('[music] pulseaudio started');
|
|
} else {
|
|
console.log('[music] pulseaudio already running');
|
|
}
|
|
|
|
const sinks = Bun.spawnSync({ cmd: [pactl, 'list', 'short', 'sinks'], stdout: 'pipe', stderr: 'ignore' });
|
|
if (sinks.stdout.toString().includes(VIRTUAL_SINK)) {
|
|
console.log(`[music] ${VIRTUAL_SINK} sink already exists`);
|
|
return;
|
|
}
|
|
|
|
const load = Bun.spawnSync({
|
|
cmd: [
|
|
pactl,
|
|
'load-module',
|
|
'module-null-sink',
|
|
`sink_name=${VIRTUAL_SINK}`,
|
|
'sink_properties=device.description=Virtual_Output',
|
|
],
|
|
stdout: 'pipe',
|
|
stderr: 'pipe',
|
|
});
|
|
if (load.exitCode !== 0) console.error('[music] failed to load null sink:', load.stderr.toString().trim());
|
|
else console.log(`[music] ${VIRTUAL_SINK} null sink loaded`);
|
|
}
|