48 lines
5.5 KiB
JavaScript
48 lines
5.5 KiB
JavaScript
// HEL-878 browser check only: a scripted stand-in for the livekit-client UMD global, served by tests/hel878_mock.mjs in
|
|
// place of the vendored build. Same names the page uses (Room, RoomEvent, Track.Source, ParticipantKind,
|
|
// createLocalAudioTrack); no WebRTC, no signalling. The microphone is a real MediaStreamTrack from the browser's fake
|
|
// device (--use-fake-device-for-media-stream), so stopping it can be observed. window.__lk drives her side.
|
|
(() => {
|
|
const RoomEvent = { TrackSubscribed: 'trackSubscribed', TrackUnsubscribed: 'trackUnsubscribed', ParticipantConnected: 'participantConnected', ParticipantDisconnected: 'participantDisconnected', ParticipantAttributesChanged: 'participantAttributesChanged', DataReceived: 'dataReceived', Disconnected: 'disconnected', AudioPlaybackStatusChanged: 'audioPlaybackChanged' };
|
|
const lk = window.__lk = { rooms: [], connects: [], published: [], mics: [], connectDelay: 200 };
|
|
class Room {
|
|
constructor() { this.handlers = {}; this.remoteParticipants = new Map(); this.canPlaybackAudio = true; this.state = 'new'; const self = this; this.localParticipant = { tracks: [], async publishTrack(t) { if (self.state !== 'connected') throw new Error('not connected'); self.localParticipant.tracks.push(t); lk.published.push(t); } }; lk.rooms.push(this); }
|
|
on(e, cb) { (this.handlers[e] ||= []).push(cb); return this; }
|
|
emit(e, ...a) { (this.handlers[e] || []).forEach(cb => cb(...a)); }
|
|
async connect(url, token) { lk.connects.push({ url, token }); this.state = 'connecting'; await new Promise(r => setTimeout(r, lk.connectDelay)); if (this.state !== 'connecting') throw new Error('aborted'); this.state = 'connected'; }
|
|
async disconnect(stop = true) { if (this.state === 'gone') return; this.state = 'gone'; if (stop) this.localParticipant.tracks.forEach(t => t.stop()); this.emit(RoomEvent.Disconnected); }
|
|
async startAudio() { this.canPlaybackAudio = true; }
|
|
}
|
|
const live = () => lk.rooms.filter(r => r.state === 'connected').at(-1);
|
|
// Her worker joins: an agent participant with a looping tone as her voice track.
|
|
lk.join = () => {
|
|
const room = live(); if (!room) return false;
|
|
const p = { identity: 'agent-AJ_fake', kind: 4, attributes: { 'lk.agent.state': 'listening' } };
|
|
room.remoteParticipants.set(p.identity, p); room.emit(RoomEvent.ParticipantConnected, p);
|
|
const ctx = new AudioContext(), osc = ctx.createOscillator(), dest = ctx.createMediaStreamDestination(); osc.connect(dest); osc.start();
|
|
const mst = dest.stream.getAudioTracks()[0];
|
|
const track = { kind: 'audio', mediaStreamTrack: mst, els: [], attach() { const el = document.createElement('audio'); el.srcObject = new MediaStream([mst]); el.autoplay = true; this.els.push(el); return el; }, detach() { const e = this.els; this.els = []; return e; }, stop() { mst.stop(); } };
|
|
room.emit(RoomEvent.TrackSubscribed, track, {}, p);
|
|
return true;
|
|
};
|
|
lk.state = s => { const room = lk.rooms.at(-1), p = [...room.remoteParticipants.values()][0]; if (!p) return false; p.attributes['lk.agent.state'] = s; room.emit(RoomEvent.ParticipantAttributesChanged, { 'lk.agent.state': s }, p); return true; };
|
|
// Frames without an envelope get the worker's v2 one (next seq of this room); explicit frames are sent as given.
|
|
lk.say = obj => { const room = lk.rooms.at(-1), frame = 'v' in obj ? obj : { v: 2, seq: (room.seq || 0) + 1, ...obj }; if (Number.isSafeInteger(frame.seq)) room.seq = Math.max(room.seq || 0, frame.seq); room.emit(RoomEvent.DataReceived, new TextEncoder().encode(JSON.stringify(frame)), undefined, 0, 'cg.subtitle'); return true; };
|
|
// HEL-898 (f92a86f): someone leaves while the room stays connected, as the SDK reports it (removed from the map,
|
|
// then ParticipantDisconnected(participant, reason)). leave() is her worker; guest()/guestLeave() anyone else.
|
|
// `room` picks an older room (index into lk.rooms) for late events.
|
|
const part = (room, identity) => { const p = room.remoteParticipants.get(identity); if (!p) return false; room.remoteParticipants.delete(identity); room.emit(RoomEvent.ParticipantDisconnected, p, 1); return true; };
|
|
lk.leave = (at = -1) => { const room = lk.rooms.at(at), p = [...room.remoteParticipants.values()].find(x => x.kind === 4); return p ? part(room, p.identity) : false; };
|
|
lk.lateLeave = at => { const room = lk.rooms.at(at); room.emit(RoomEvent.ParticipantDisconnected, { identity: 'agent-AJ_fake', kind: 4, attributes: {} }, 1); return true; };
|
|
lk.guest = () => { const room = live(), p = { identity: 'guest-1', kind: 0, attributes: {} }; room.remoteParticipants.set(p.identity, p); room.emit(RoomEvent.ParticipantConnected, p); return true; };
|
|
lk.guestLeave = () => part(live(), 'guest-1');
|
|
lk.drop = () => { const room = live(); room.state = 'gone'; room.emit(RoomEvent.Disconnected); return true; };
|
|
lk.block = () => { const room = live(); room.canPlaybackAudio = false; room.emit(RoomEvent.AudioPlaybackStatusChanged); return true; };
|
|
async function createLocalAudioTrack(constraints) {
|
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: constraints || true }), mst = stream.getAudioTracks()[0];
|
|
lk.mics.push(mst);
|
|
return { kind: 'audio', mediaStreamTrack: mst, attach() { return document.createElement('audio'); }, detach() { return []; }, stop() { mst.stop(); } };
|
|
}
|
|
window.LivekitClient = { Room, RoomEvent, Track: { Source: { Microphone: 'microphone' } }, ParticipantKind: { AGENT: 4 }, createLocalAudioTrack };
|
|
})();
|