Changes
Co-authored-by: Reifonas <211114984+Reifonas@users.noreply.github.com>
This commit is contained in:
@@ -97,10 +97,18 @@ export async function startBroadcast(stream: MediaStream): Promise<BroadcasterHa
|
|||||||
|
|
||||||
channel = joinSignalingChannel(code, handleMessage);
|
channel = joinSignalingChannel(code, handleMessage);
|
||||||
|
|
||||||
|
// Track the active stream so we can swap it on the fly
|
||||||
|
let activeStream: MediaStream = stream;
|
||||||
|
|
||||||
// Stop when the underlying stream ends
|
// Stop when the underlying stream ends
|
||||||
stream.getTracks().forEach((t) => {
|
const attachEndedListeners = (s: MediaStream) => {
|
||||||
t.addEventListener('ended', () => stop());
|
s.getTracks().forEach((t) => {
|
||||||
|
t.addEventListener('ended', () => {
|
||||||
|
if (s === activeStream) stop();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
attachEndedListeners(stream);
|
||||||
|
|
||||||
let stopped = false;
|
let stopped = false;
|
||||||
const stop = async () => {
|
const stop = async () => {
|
||||||
@@ -113,9 +121,27 @@ export async function startBroadcast(stream: MediaStream): Promise<BroadcasterHa
|
|||||||
await supabase.from('share_rooms').update({ is_active: false, viewer_count: 0 }).eq('code', code);
|
await supabase.from('share_rooms').update({ is_active: false, viewer_count: 0 }).eq('code', code);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const replaceVideoTrack = async (newStream: MediaStream) => {
|
||||||
|
const newVideo = newStream.getVideoTracks()[0];
|
||||||
|
if (!newVideo) return;
|
||||||
|
activeStream = newStream;
|
||||||
|
attachEndedListeners(newStream);
|
||||||
|
for (const pc of peers.values()) {
|
||||||
|
const sender = pc.getSenders().find((s) => s.track && s.track.kind === 'video');
|
||||||
|
if (sender) {
|
||||||
|
try {
|
||||||
|
await sender.replaceTrack(newVideo);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('[broadcaster] replaceTrack failed', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
code,
|
code,
|
||||||
stop,
|
stop,
|
||||||
|
replaceVideoTrack,
|
||||||
onViewerCountChange: (cb) => {
|
onViewerCountChange: (cb) => {
|
||||||
listeners.add(cb);
|
listeners.add(cb);
|
||||||
cb(peers.size);
|
cb(peers.size);
|
||||||
|
|||||||
Reference in New Issue
Block a user