diff --git a/src/components/WebRtcPlayer.astro b/src/components/WebRtcPlayer.astro index a301f68..1f60e78 100644 --- a/src/components/WebRtcPlayer.astro +++ b/src/components/WebRtcPlayer.astro @@ -158,11 +158,30 @@ const id = `${Math.random().toString(36).slice(2)}`; } else { remoteStream.addTrack(ev.track); } + ev.track.onended = () => { + console.warn("🔴 Remote track ended!"); + emit("disconnected", { msg: "Stream ended" }); + }; + + ev.track.onmute = () => + console.warn("Track muted (broadcaster may have stopped)"); + ev.track.onunmute = () => console.log("Track unmuted"); + emit("track", { kind: ev.track.kind }); }); pc.addEventListener("iceconnectionstatechange", () => { - emit("ice", { state: pc.iceConnectionState }); + // emit("ice", { state: pc.iceConnectionState }); + const state = pc.iceConnectionState; + emit("ice", { state }); + console.log("ICE connection state:", state); + + if (["disconnected", "failed", "closed"].includes(state)) { + console.warn( + "🔴 ICE disconnected — broadcaster likely ended stream.", + ); + emit("disconnected", { msg: "Stream disconnected" }); + } }); const localCandidates = []; @@ -278,11 +297,22 @@ const id = `${Math.random().toString(36).slice(2)}`; if (type === "status") showOverlay(payload.msg); if (type === "playing") hideOverlay(); if (type === "error") showOverlay(payload.msg || payload); - if (type === "disconnected") showOverlay(payload.msg); - if (type === "no-media") showOverlay("Stream not started"); + if (type === "disconnected") { + handleStreamEnded(); + showOverlay(payload.msg); + } + if (type === "no-media") { + handleStreamEnded(); + showOverlay("Stream not started"); + } }, }); + function handleStreamEnded() { + videoEl.pause(); + videoEl.srcObject = null; + } + async function startWithRetry() { try { await player.start();