64 lines
3.3 KiB
JavaScript
64 lines
3.3 KiB
JavaScript
import { MusicError } from '../src/core/controller.mjs';
|
|
// In-process transport prototype; accepts only operator-generated 20 ms PCM fixtures.
|
|
// No Sendspin socket, upstream URL, codec negotiation, network listener or MA connection.
|
|
export class SyntheticAudioGateway {
|
|
#control; #streams = new Map(); #connections = new Set();
|
|
constructor({ control }) { this.#control = control; }
|
|
// Operator-only session bootstrap; rotate on gateway restart. Never a browser route.
|
|
provision({ campaign, session }) {
|
|
if (!this.#control.campaigns.has(campaign) || !session || this.#streams.has(campaign)) throw new MusicError('denied');
|
|
this.#streams.set(campaign, { session, generation: 0, frames: [] });
|
|
}
|
|
// Operator-only synthetic source. Real sources must acquire/check the authority fence.
|
|
publish({ campaign, session, generation, pcm }) {
|
|
const stream = this.#streams.get(campaign);
|
|
if (!stream || stream.session !== session || !Number.isSafeInteger(generation) || generation !== stream.generation + 1) throw new MusicError('stale');
|
|
if (!(pcm instanceof Uint8Array) || pcm.byteLength !== 1920) throw new MusicError('denied');
|
|
stream.generation = generation;
|
|
stream.frames.push({ generation, pcm: Uint8Array.from(pcm) });
|
|
// At most 200 ms of stereo PCM16 at 24 kHz per campaign, no disk cache.
|
|
if (stream.frames.length > 10) stream.frames.shift();
|
|
}
|
|
#check(request) {
|
|
if (!request || Object.keys(request).sort().join(',') !== 'campaign,origin,session,token') throw new MusicError('denied');
|
|
this.#control.authorize(request.token, request.origin, request.campaign, request.session, 'listener');
|
|
const stream = this.#streams.get(request.campaign);
|
|
if (!stream || stream.session !== request.session) throw new MusicError('denied');
|
|
return stream;
|
|
}
|
|
connect(request) {
|
|
const stream = this.#check(request);
|
|
this.sweep();
|
|
if ([...this.#connections].filter(c => c.request.campaign === request.campaign).length >= 16) throw new MusicError('denied');
|
|
const connection = { request: { ...request }, cursor: stream.generation, closed: false };
|
|
this.#connections.add(connection);
|
|
const close = () => { connection.closed = true; this.#connections.delete(connection); };
|
|
return {
|
|
close,
|
|
get closed() { return connection.closed; },
|
|
next: () => {
|
|
try {
|
|
if (connection.closed) throw new MusicError('denied');
|
|
const current = this.#check(connection.request);
|
|
const frame = current.frames.find(f => f.generation > connection.cursor);
|
|
// Fail closed for listeners that fell behind the bounded ring.
|
|
if (current.frames.length && connection.cursor < current.frames[0].generation - 1) throw new MusicError('stale');
|
|
if (!frame) return null;
|
|
connection.cursor = frame.generation;
|
|
return { generation: frame.generation, pcm: Uint8Array.from(frame.pcm), sampleRate: 24000, channels: 2 };
|
|
} catch (error) { close(); throw error; }
|
|
}
|
|
};
|
|
}
|
|
// A future transport must schedule this and close its socket when a handle closes.
|
|
sweep() {
|
|
for (const connection of this.#connections) {
|
|
try { this.#check(connection.request); }
|
|
catch { connection.closed = true; this.#connections.delete(connection); }
|
|
}
|
|
}
|
|
purge(campaign) {
|
|
this.#streams.delete(campaign); this.sweep();
|
|
}
|
|
}
|