@@ -33,7 +33,7 @@ use tokio_util::sync::CancellationToken;
3333use super :: human_floor:: HumanFloor ;
3434use super :: jitter:: { PeerJitterBuffer , SAMPLE_RATE_HZ } ;
3535use super :: relay_api:: { WsStream , REMOTE_SPEECH_THRESHOLD } ;
36- use super :: wire:: { FrameHeader , FLAG_DTX , V2_HEADER_LEN } ;
36+ use super :: wire:: { parse_relay_frame , FLAG_DTX } ;
3737
3838/// Speaker-tick window for emitting `huddle-active-speakers`. Active set is
3939/// cleared each tick — peers that didn't send a frame in the last window are
@@ -149,18 +149,11 @@ fn is_agent_peer(
149149 } )
150150}
151151
152- /// Whether `peer_idx` is currently occupied at exactly `epoch`, per the
153- /// authoritative roster. A frame is deliverable only when both match: an index
154- /// absent from the roster is stale, and a slot reused by a later occupant has
155- /// advanced its epoch, so a departed occupant's in-flight frame is fenced
156- /// rather than mis-attributed to the new occupant. A legacy relay omits the
157- /// epoch, which degrades to `0` on both sides, making the fence a no-op.
158- fn is_current_occupant (
159- peer_idx : u8 ,
160- epoch : u8 ,
161- index_to_epoch : & std:: collections:: HashMap < u8 , u8 > ,
162- ) -> bool {
163- index_to_epoch. get ( & peer_idx) == Some ( & epoch)
152+ /// Whether `peer_idx` is currently occupied per the authoritative roster.
153+ /// Protocol v2 media carries only the peer index, so roster presence is the
154+ /// strongest routing boundary available until the relay supports v3 epochs.
155+ fn is_current_occupant ( peer_idx : u8 , index_to_epoch : & std:: collections:: HashMap < u8 , u8 > ) -> bool {
156+ index_to_epoch. contains_key ( & peer_idx)
164157}
165158
166159fn same_occupancy (
@@ -196,7 +189,7 @@ fn f32_samples_to_le_bytes(samples: &[f32]) -> Vec<u8> {
196189/// One remote peer's slot: jitter buffer + dedicated rodio Player.
197190///
198191/// Per-frame seq/timestamp come from the v2 wire header (sender-authored).
199- /// The relay forwards `peer_index | epoch | header | opus_bytes` opaquely; we
192+ /// The relay forwards `peer_index | header | opus_bytes` opaquely; we
200193/// parse the header here and pass the sender's own monotonic seq + 48 kHz media
201194/// timestamp into NetEq.
202195struct PeerSlot {
@@ -422,22 +415,19 @@ pub(crate) async fn run_playout_recv_loop(
422415 msg = ws_rx. next( ) => {
423416 match msg {
424417 Some ( Ok ( WsMsg :: Binary ( data) ) ) => {
425- // Wire shape (v2): [peer_index: u8][epoch: u8][header: 8 bytes][opus payload...]
426- // The minimum size is 2 (peer_index + epoch) + 8 (header) + ≥1 Opus byte.
427- if data. len( ) <= 2 + V2_HEADER_LEN {
418+ // Wire shape (v2): [peer_index: u8][header: 8 bytes][opus payload...]
419+ // The minimum size is 1 (peer index) + 8 (header) + ≥1 Opus byte.
420+ let Some ( ( peer_idx, header, opus_bytes) ) = parse_relay_frame( & data) else {
421+ eprintln!(
422+ "buzz-desktop: dropping malformed v2 audio relay frame ({} bytes)" ,
423+ data. len( ) ,
424+ ) ;
428425 continue ;
429- }
430- let peer_idx = data[ 0 ] ;
431- let epoch = data[ 1 ] ;
432- // Fence the peer-index reuse race: a frame authored by a
433- // departed occupant that arrives after its index is
434- // reassigned carries the old epoch. Drop it rather than
435- // mis-attribute stale audio (and the new occupant's
436- // human/agent STT policy) to whoever grabbed the index.
437- // An index absent from the roster is also stale. A slot
438- // with no known epoch (legacy relay) degrades to 0 on
439- // both sides, so the fence is a no-op there.
440- if !is_current_occupant( peer_idx, epoch, & index_to_epoch) {
426+ } ;
427+ // Protocol v2 has no media epoch. Drop frames for slots
428+ // absent from the control roster; delayed frames after
429+ // an index is reassigned cannot be fenced until v3.
430+ if !is_current_occupant( peer_idx, & index_to_epoch) {
441431 continue ;
442432 }
443433 // Suppress only an agent stream synthesized and
@@ -446,21 +436,6 @@ pub(crate) async fn run_playout_recv_loop(
446436 if is_locally_synthesized_peer( peer_idx, & local_tts_publishers) {
447437 continue ;
448438 }
449- let after_idx = & data[ 2 ..] ;
450- let Some ( ( header, opus_bytes) ) = FrameHeader :: parse( after_idx)
451- else {
452- // Malformed v2 frame: header parse only fails when
453- // the slice is too short, which `if data.len() <= ...`
454- // already guards. Defensive log + drop.
455- eprintln!(
456- "buzz-desktop: dropping malformed audio frame from peer {peer_idx} ({} bytes)" ,
457- data. len( ) ,
458- ) ;
459- continue ;
460- } ;
461- if opus_bytes. is_empty( ) {
462- continue ;
463- }
464439 let is_dtx = ( header. flags & FLAG_DTX ) != 0 ;
465440 // Only count non-DTX arrivals toward the UI's
466441 // active-speaker set. DTX/comfort packets are emitted
@@ -771,34 +746,16 @@ mod tests {
771746 ) ;
772747 }
773748
774- /// Causal regression for the peer-index reuse race (Jude's blocking
775- /// finding): a frame authored by a departed occupant that arrives after
776- /// its slot is reassigned to a new occupant carries the stale epoch and
777- /// must be fenced, never mis-attributed to the new occupant.
778749 #[ test]
779- fn stale_epoch_frame_is_fenced_after_its_index_is_reused ( ) {
750+ fn v2_media_is_routed_only_for_current_roster_indices ( ) {
780751 let mut index_to_epoch = std:: collections:: HashMap :: new ( ) ;
781- // Slot 3 first occupied at epoch 0.
782752 index_to_epoch. insert ( 3_u8 , 0_u8 ) ;
783753 assert ! (
784- is_current_occupant( 3 , 0 , & index_to_epoch) ,
754+ is_current_occupant( 3 , & index_to_epoch) ,
785755 "current occupant's frame is delivered"
786756 ) ;
787-
788- // The occupant departs and a new peer reuses slot 3 at epoch 1.
789- index_to_epoch. insert ( 3 , 1 ) ;
790- assert ! (
791- !is_current_occupant( 3 , 0 , & index_to_epoch) ,
792- "in-flight frame from the departed occupant (epoch 0) is fenced"
793- ) ;
794- assert ! (
795- is_current_occupant( 3 , 1 , & index_to_epoch) ,
796- "the new occupant's frame (epoch 1) is delivered"
797- ) ;
798-
799- // A frame for an index absent from the roster is stale.
800757 assert ! (
801- !is_current_occupant( 9 , 0 , & index_to_epoch) ,
758+ !is_current_occupant( 9 , & index_to_epoch) ,
802759 "frame for an unoccupied index is dropped"
803760 ) ;
804761 }
0 commit comments