cliamp music player integration with browser audio streaming
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
pcm.!default {
|
||||
type pulse
|
||||
fallback "sysdefault"
|
||||
}
|
||||
|
||||
ctl.!default {
|
||||
type pulse
|
||||
fallback "sysdefault"
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import type { ServerWebSocket } from 'bun';
|
||||
import { spawn, type Subprocess } from 'bun';
|
||||
|
||||
type WSData = {
|
||||
userId: number;
|
||||
email: string;
|
||||
username: string;
|
||||
role: string;
|
||||
};
|
||||
|
||||
type AudioSession = {
|
||||
proc: Subprocess;
|
||||
closed: boolean;
|
||||
};
|
||||
|
||||
const sessions = new Map<ServerWebSocket<WSData>, AudioSession>();
|
||||
|
||||
export const cliampAudioWebsocket = {
|
||||
async open(ws: ServerWebSocket<WSData>) {
|
||||
const parecPath = Bun.which('parec');
|
||||
if (!parecPath) {
|
||||
ws.close(4000, 'parec not found on host');
|
||||
return;
|
||||
}
|
||||
|
||||
let proc: Subprocess<'ignore', 'pipe', 'ignore'>;
|
||||
try {
|
||||
proc = spawn({
|
||||
cmd: [parecPath, '--format=s16le', '--rate=44100', '--channels=2', '-d', 'virtual_out.monitor'],
|
||||
stdout: 'pipe',
|
||||
stderr: 'ignore',
|
||||
stdin: 'ignore',
|
||||
});
|
||||
} catch {
|
||||
ws.close(4000, 'Failed to start audio capture');
|
||||
return;
|
||||
}
|
||||
|
||||
const session: AudioSession = { proc, closed: false };
|
||||
sessions.set(ws, session);
|
||||
console.log('[cliamp-audio] parec started, streaming to WS');
|
||||
|
||||
// Stream stdout chunks as binary WS frames
|
||||
const reader = proc.stdout.getReader();
|
||||
let totalBytes = 0;
|
||||
const pump = async () => {
|
||||
try {
|
||||
while (!session.closed) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (value && !session.closed) {
|
||||
totalBytes += value.byteLength;
|
||||
if (totalBytes <= value.byteLength) {
|
||||
console.log(`[cliamp-audio] first chunk: ${value.byteLength} bytes`);
|
||||
}
|
||||
try {
|
||||
ws.sendBinary(value);
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// stream ended or error
|
||||
} finally {
|
||||
if (!session.closed) {
|
||||
session.closed = true;
|
||||
try { ws.close(); } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pump();
|
||||
},
|
||||
|
||||
message() {
|
||||
// No client-to-server messages expected
|
||||
},
|
||||
|
||||
close(ws: ServerWebSocket<WSData>) {
|
||||
console.log('[cliamp-audio] WS closed');
|
||||
const session = sessions.get(ws);
|
||||
if (session) {
|
||||
session.closed = true;
|
||||
try { session.proc.kill(); } catch { /* ignore */ }
|
||||
sessions.delete(ws);
|
||||
}
|
||||
},
|
||||
|
||||
drain() {},
|
||||
};
|
||||
@@ -0,0 +1,190 @@
|
||||
import type { ServerWebSocket } from 'bun';
|
||||
import { spawn, type Subprocess } from 'bun';
|
||||
import { resolve, normalize, dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { getHomeDir } from '@@/data-path';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const ASOUNDRC_PATH = join(__dirname, 'asoundrc');
|
||||
|
||||
type WSData = {
|
||||
userId: number;
|
||||
email: string;
|
||||
username: string;
|
||||
role: string;
|
||||
files: string;
|
||||
};
|
||||
|
||||
type CliampSession = {
|
||||
proc: Subprocess<'pipe', 'pipe', 'pipe'>;
|
||||
closed: boolean;
|
||||
};
|
||||
|
||||
const sessions = new Map<ServerWebSocket<WSData>, CliampSession>();
|
||||
|
||||
const sendOutput = (ws: ServerWebSocket<WSData>, data: string) => {
|
||||
try {
|
||||
ws.send(JSON.stringify({ type: 'output', data }));
|
||||
} catch {
|
||||
// ws already closed
|
||||
}
|
||||
};
|
||||
|
||||
const sendExit = (ws: ServerWebSocket<WSData>) => {
|
||||
try {
|
||||
ws.send(JSON.stringify({ type: 'exit' }));
|
||||
} catch {
|
||||
// ws already closed
|
||||
}
|
||||
};
|
||||
|
||||
const validateFilePaths = (files: string[], homeDir: string): string[] | null => {
|
||||
const resolved = files.map((f) => normalize(resolve(homeDir, f)));
|
||||
for (const p of resolved) {
|
||||
if (!p.startsWith(homeDir)) return null;
|
||||
}
|
||||
return resolved;
|
||||
};
|
||||
|
||||
const findCliamp = (): string | null => {
|
||||
const which = Bun.which('cliamp');
|
||||
if (which) return which;
|
||||
const goPath = process.env.GOPATH ?? `${process.env.HOME}/go`;
|
||||
const goBin = `${goPath}/bin/cliamp`;
|
||||
try {
|
||||
const stat = Bun.spawnSync({ cmd: ['test', '-x', goBin], stdout: 'ignore', stderr: 'ignore' });
|
||||
if (stat.exitCode === 0) return goBin;
|
||||
} catch { /* ignore */ }
|
||||
return null;
|
||||
};
|
||||
|
||||
const shellEscape = (s: string) => `'${s.replace(/'/g, "'\\''")}'`;
|
||||
|
||||
export const cliampWebsocket = {
|
||||
async open(ws: ServerWebSocket<WSData>) {
|
||||
const { email, files: filesParam } = ws.data;
|
||||
|
||||
if (!filesParam) {
|
||||
sendOutput(ws, '\r\n[Error] No files specified.\r\n');
|
||||
return;
|
||||
}
|
||||
|
||||
const cliampPath = findCliamp();
|
||||
if (!cliampPath) {
|
||||
sendOutput(ws, '\r\n[Error] cliamp not found on host.\r\n');
|
||||
return;
|
||||
}
|
||||
|
||||
const homeDir = getHomeDir(email);
|
||||
const rawFiles = [filesParam];
|
||||
|
||||
// Resolve paths relative to user home dir
|
||||
const absoluteFiles = rawFiles.map((f) => {
|
||||
if (f.startsWith('/')) return `${homeDir}${f}`;
|
||||
return `${homeDir}/${f}`;
|
||||
});
|
||||
|
||||
const validated = validateFilePaths(absoluteFiles, homeDir);
|
||||
if (!validated) {
|
||||
sendOutput(ws, '\r\n[Error] Invalid file path.\r\n');
|
||||
return;
|
||||
}
|
||||
|
||||
// Use `script` to allocate a PTY for cliamp (avoids node-pty dependency)
|
||||
// script -qfc '<command> <args...>' /dev/null
|
||||
const cliampCmd = [shellEscape(cliampPath), ...validated.map(shellEscape)].join(' ');
|
||||
console.log(`[cliamp] spawning: ${cliampCmd}`);
|
||||
let proc: Subprocess<'pipe', 'pipe', 'pipe'>;
|
||||
try {
|
||||
proc = spawn({
|
||||
cmd: ['script', '-qfc', cliampCmd, '/dev/null'],
|
||||
stdin: 'pipe',
|
||||
stdout: 'pipe',
|
||||
stderr: 'pipe',
|
||||
cwd: homeDir,
|
||||
env: { ...process.env, TERM: 'xterm-256color', PULSE_SINK: 'virtual_out', ALSA_CONFIG_PATH: ASOUNDRC_PATH },
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Failed to start cliamp';
|
||||
sendOutput(ws, `\r\n[Error] ${message}\r\n`);
|
||||
return;
|
||||
}
|
||||
|
||||
const session: CliampSession = { proc, closed: false };
|
||||
sessions.set(ws, session);
|
||||
|
||||
// Pump stdout → WS
|
||||
const reader = proc.stdout.getReader();
|
||||
const pump = async () => {
|
||||
try {
|
||||
while (!session.closed) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (value && !session.closed) {
|
||||
sendOutput(ws, new TextDecoder().decode(value));
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// stream ended
|
||||
} finally {
|
||||
if (!session.closed) {
|
||||
session.closed = true;
|
||||
sendExit(ws);
|
||||
}
|
||||
}
|
||||
};
|
||||
pump();
|
||||
|
||||
// Also read stderr (cliamp may write there)
|
||||
const stderrReader = proc.stderr.getReader();
|
||||
const pumpStderr = async () => {
|
||||
try {
|
||||
while (!session.closed) {
|
||||
const { done, value } = await stderrReader.read();
|
||||
if (done) break;
|
||||
if (value && !session.closed) {
|
||||
sendOutput(ws, new TextDecoder().decode(value));
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// stream ended
|
||||
}
|
||||
};
|
||||
pumpStderr();
|
||||
|
||||
// On process exit → notify client
|
||||
proc.exited.then((code) => {
|
||||
console.log(`[cliamp] process exited code=${code}`);
|
||||
if (!session.closed) {
|
||||
session.closed = true;
|
||||
sendExit(ws);
|
||||
}
|
||||
sessions.delete(ws);
|
||||
});
|
||||
},
|
||||
|
||||
message(ws: ServerWebSocket<WSData>, raw: string | Buffer) {
|
||||
const session = sessions.get(ws);
|
||||
if (!session || session.closed) return;
|
||||
|
||||
try {
|
||||
const msg = JSON.parse(typeof raw === 'string' ? raw : raw.toString());
|
||||
if (msg.type === 'input' && msg.data) {
|
||||
session.proc.stdin.write(msg.data);
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
},
|
||||
|
||||
close(ws: ServerWebSocket<WSData>) {
|
||||
const session = sessions.get(ws);
|
||||
if (session) {
|
||||
session.closed = true;
|
||||
try { session.proc.kill(); } catch { /* ignore */ }
|
||||
sessions.delete(ws);
|
||||
}
|
||||
},
|
||||
|
||||
drain() {},
|
||||
};
|
||||
Reference in New Issue
Block a user