zebra-spaces: tear down SFU worklet path when mesh ontrack fires (no more double-audio)

Fox 2026-06-04: "the cohost is hearing two feeds now."

Regression from role-aware audio routing. Pre-refactor: SFU mic and
mesh mic for the same speaker both wrote into the remoteAudio map
(both used <audio> elements), so the second arrival overwrote the
first — one audible path. Post-refactor: SFU mic for speakers /
cohosts / hosts goes through AudioContext + worklet (stored in
listenerAudioNodes), mesh mic still uses <audio> (in remoteAudio).
Different maps, both play, same voice in two different delays =
echo / phasing.

Fix: at the top of mesh pc.ontrack, call detachListenerStream(uuid)
to remove the SFU worklet path. The existing canSpeak-mesh-skip
in handleRemoteSfuTrack already prevents the OPPOSITE race (SFU mic
arriving after mesh is connected).

Logged so we can see the handoff: "mesh audio attached for XXXX —
SFU worklet path detached".
This commit is contained in:
Russell Ballestrini 2026-06-04 17:51:55 -04:00
parent 8d18216854
commit fd119cf572
No known key found for this signature in database

View file

@ -5077,6 +5077,15 @@ async function connectToPeer(uuid, weOffer){
for (const tr of micStream.getTracks()){ tagTrack(tr); pc.addTrack(tr, micStream); }
setSenderBitrate(pc.getSenders().find(s=>s.track && s.track.kind==='audio'));
pc.ontrack = (ev) => {
/* Tear down the SFU worklet path for this uuid before mesh's
* <audio> takes over. Without this we hear the same voice
* through TWO paths simultaneously: SFU mic → AudioContext
* worklet (~0.5s delay), and mesh mic → <audio> (~50ms).
* Pre-refactor, both paths used <audio> in the remoteAudio map
* so the second overwrote the first. After role-aware routing,
* SFU lives in listenerAudioNodes and mesh in remoteAudio —
* different maps, both play, echo. Fox 2026-06-04. */
detachListenerStream(uuid);
let a = remoteAudio.get(uuid);
if (!a){
a = document.createElement('audio'); a.autoplay = true;
@ -5091,6 +5100,7 @@ async function connectToPeer(uuid, weOffer){
try { ev.receiver.playoutDelayHint = SPEAKER_PLAYOUT_DELAY_SEC; } catch(_){}
try { ev.receiver.jitterBufferTarget = SPEAKER_PLAYOUT_DELAY_SEC * 1000; } catch(_){}
stopMeter(uuid); startMeter(uuid, a.srcObject);
logLine('', 'mesh audio attached for '+uuid.slice(0,4)+' — SFU worklet path detached');
};
pc.onicecandidate = (ev) => { /* using waitForIceGathering pattern, candidates ignored */ };
pc.onconnectionstatechange = () => {
@ -6371,8 +6381,8 @@ logLine('', 'ready — pick a handle, type a rendezvous code, enter the space');
<footer style="margin:2.2rem auto 0;font-size:0.65rem;color:#999;line-height:1.7;word-break:break-all;font-family:monospace">
<span id="pi-seal" style="color:#777;cursor:default;user-select:none" title="">page integrity</span> &nbsp;·&nbsp; built <span class="stamp-date">2026-06-04</span><br>
md5 <span class="stamp-md5">544ed25e7616ced6a24bbc8c29bf13ae</span><br>
sha256 <span class="stamp-sha">1bb6283957a5a00cc6e9e52c427d85791345bd6383d95f24bca1016497205f81</span><br>
md5 <span class="stamp-md5">ffdaafffe887e5213d2152178708bb3b</span><br>
sha256 <span class="stamp-sha">e1f9d34469407f49bc8129eef43cf990c611bd77cd6f8725bd2be8416e5eb644</span><br>
<span style="color:#bbb">hashes are of this page with these two fields zeroed — to verify, blank them and re-hash</span><br>
<span style="color:#bbb">one self-contained file — <strong>save a copy</strong> and verify against these hashes; point at your own servers with ?signal= and ?turncred=, or <a href="host-your-own.html" style="color:#999">host your own community</a></span>
</footer>