@@ -44,6 +44,9 @@ const SPEAKER_LEVEL_TICK_MS: u64 = 50;
4444/// Per-peer arrival window for the TTS interrupt frame counter.
4545const FRAME_WINDOW : std:: time:: Duration = std:: time:: Duration :: from_millis ( 500 ) ;
4646const REMOTE_RELEASE_DEBOUNCE : std:: time:: Duration = std:: time:: Duration :: from_millis ( 500 ) ;
47+ /// Match Mobile's speaking treatment: an open microphone can emit continuous
48+ /// non-DTX Opus for room tone, so packet type alone is not evidence of speech.
49+ const REMOTE_SPEECH_LEVEL_DBOV : i8 = -55 ;
4750/// Playout clock: NetEq emits 10 ms frames, so we tick at 10 ms.
4851const PLAYOUT_TICK_MS : u64 = 10 ;
4952
@@ -86,19 +89,30 @@ fn normalized_speaker_level(level_dbov: i8) -> f32 {
8689 ( ( f32:: from ( level_dbov) + 60.0 ) / 48.0 ) . clamp ( 0.0 , 1.0 )
8790}
8891
92+ fn is_remote_speech_frame ( is_dtx : bool , level_dbov : i8 ) -> bool {
93+ !is_dtx && level_dbov >= REMOTE_SPEECH_LEVEL_DBOV
94+ }
95+
8996fn update_remote_release_deadline (
9097 peer : u8 ,
91- is_dtx : bool ,
98+ is_speech : bool ,
9299 remote_floor_owners : & std:: collections:: HashSet < u8 > ,
93100 deadlines : & mut std:: collections:: HashMap < u8 , tokio:: time:: Instant > ,
94101 now : tokio:: time:: Instant ,
95102) {
96- if !is_dtx {
97- deadlines. remove ( & peer) ;
98- } else if remote_floor_owners. contains ( & peer) {
99- deadlines
100- . entry ( peer)
101- . or_insert ( now + REMOTE_RELEASE_DEBOUNCE ) ;
103+ if remote_floor_owners. contains ( & peer) {
104+ if is_speech {
105+ // Refresh from audible speech itself. Some mobile capture paths
106+ // stop producing packets once speech ends, so waiting for a DTX
107+ // or quiet packet can otherwise hold the human floor forever.
108+ deadlines. insert ( peer, now + REMOTE_RELEASE_DEBOUNCE ) ;
109+ } else {
110+ // Preserve the deadline from the last audible frame. Continuous
111+ // room-tone packets must not keep extending the human floor.
112+ deadlines
113+ . entry ( peer)
114+ . or_insert ( now + REMOTE_RELEASE_DEBOUNCE ) ;
115+ }
102116 }
103117}
104118
@@ -437,19 +451,20 @@ pub(crate) async fn run_playout_recv_loop(
437451 continue ;
438452 }
439453 let is_dtx = ( header. flags & FLAG_DTX ) != 0 ;
440- // Only count non-DTX arrivals toward the UI's
441- // active-speaker set. DTX/comfort packets are emitted
442- // by an idle peer to keep the codec alive — they
443- // don't mean the peer is speaking, and shouldn't
444- // make their tile flash for the 500 ms speaker tick.
454+ let is_remote_speech =
455+ is_remote_speech_frame( is_dtx, header. level_dbov) ;
456+ // Only count audible arrivals toward the UI's
457+ // active-speaker set. An open mobile microphone can
458+ // continuously emit non-DTX room tone, so require an
459+ // audible level before treating a packet as speech.
445460 update_remote_release_deadline(
446461 peer_idx,
447- is_dtx ,
462+ is_remote_speech ,
448463 & remote_floor_owners,
449464 & mut remote_release_deadlines,
450465 tokio:: time:: Instant :: now( ) ,
451466 ) ;
452- if !is_dtx {
467+ if is_remote_speech {
453468 active_indices. insert( peer_idx) ;
454469 let level = normalized_speaker_level( header. level_dbov) ;
455470 speaker_levels
@@ -497,7 +512,7 @@ pub(crate) async fn run_playout_recv_loop(
497512 // Count only remote-human speech toward floor onset.
498513 // Agent audio still plays, but it must not acquire the
499514 // human floor or suppress another agent's response.
500- if !is_dtx && remote_human {
515+ if is_remote_speech && remote_human {
501516 if last_frame_reset. elapsed( ) >= FRAME_WINDOW {
502517 frame_counts. clear( ) ;
503518 last_frame_reset = tokio:: time:: Instant :: now( ) ;
@@ -507,6 +522,14 @@ pub(crate) async fn run_playout_recv_loop(
507522 if * count >= REMOTE_SPEECH_THRESHOLD {
508523 human_floor. enter_remote( peer_idx) ;
509524 remote_floor_owners. insert( peer_idx) ;
525+ // The threshold-crossing frame is processed
526+ // before this peer becomes an owner. Arm its
527+ // release here so silence need not arrive in a
528+ // later packet to let queued TTS continue.
529+ remote_release_deadlines. insert(
530+ peer_idx,
531+ tokio:: time:: Instant :: now( ) + REMOTE_RELEASE_DEBOUNCE ,
532+ ) ;
510533 if tts_active. load( Ordering :: Acquire ) {
511534 tts_cancel. store( true , Ordering :: Release ) ;
512535 }
@@ -647,18 +670,18 @@ mod tests {
647670 use super :: * ;
648671
649672 #[ test]
650- fn continuous_dtx_does_not_extend_remote_floor_deadline ( ) {
673+ fn continuous_silence_does_not_extend_remote_floor_deadline ( ) {
651674 let peer = 7 ;
652675 let started = tokio:: time:: Instant :: now ( ) ;
653676 let owners = std:: collections:: HashSet :: from ( [ peer] ) ;
654677 let mut deadlines = std:: collections:: HashMap :: new ( ) ;
655678
656- update_remote_release_deadline ( peer, true , & owners, & mut deadlines, started) ;
679+ update_remote_release_deadline ( peer, false , & owners, & mut deadlines, started) ;
657680 let armed = deadlines[ & peer] ;
658681 for elapsed_ms in [ 100 , 200 , 300 , 400 ] {
659682 update_remote_release_deadline (
660683 peer,
661- true ,
684+ false ,
662685 & owners,
663686 & mut deadlines,
664687 started + std:: time:: Duration :: from_millis ( elapsed_ms) ,
@@ -678,18 +701,48 @@ mod tests {
678701 }
679702
680703 #[ test]
681- fn dtx_from_non_owner_does_not_arm_remote_floor_deadline ( ) {
704+ fn last_speech_frame_arms_remote_floor_release_without_follow_up_audio ( ) {
705+ let peer = 7 ;
706+ let started = tokio:: time:: Instant :: now ( ) ;
707+ let owners = std:: collections:: HashSet :: from ( [ peer] ) ;
708+ let mut deadlines = std:: collections:: HashMap :: new ( ) ;
709+
710+ update_remote_release_deadline ( peer, true , & owners, & mut deadlines, started) ;
711+ let armed = started + REMOTE_RELEASE_DEBOUNCE ;
712+ assert_eq ! ( deadlines[ & peer] , armed) ;
713+
714+ let human_floor = HumanFloor :: new ( ) ;
715+ human_floor. enter_remote ( peer) ;
716+ let mut owners = owners;
717+ release_expired_remote_floors ( armed, & mut owners, & mut deadlines, & human_floor) ;
718+
719+ assert ! ( !human_floor. is_blocked( ) ) ;
720+ assert ! ( owners. is_empty( ) ) ;
721+ assert ! ( deadlines. is_empty( ) ) ;
722+ }
723+
724+ #[ test]
725+ fn silence_from_non_owner_does_not_arm_remote_floor_deadline ( ) {
682726 let mut deadlines = std:: collections:: HashMap :: new ( ) ;
683727 update_remote_release_deadline (
684728 7 ,
685- true ,
729+ false ,
686730 & std:: collections:: HashSet :: new ( ) ,
687731 & mut deadlines,
688732 tokio:: time:: Instant :: now ( ) ,
689733 ) ;
690734 assert ! ( deadlines. is_empty( ) ) ;
691735 }
692736
737+ #[ test]
738+ fn remote_speech_requires_non_dtx_audio_above_the_activity_floor ( ) {
739+ assert ! ( !is_remote_speech_frame( true , 0 ) ) ;
740+ assert ! ( !is_remote_speech_frame( false , -127 ) ) ;
741+ assert ! ( !is_remote_speech_frame( false , -56 ) ) ;
742+ assert ! ( is_remote_speech_frame( false , -55 ) ) ;
743+ assert ! ( is_remote_speech_frame( false , -12 ) ) ;
744+ }
745+
693746 #[ test]
694747 fn speaker_level_maps_conversational_range ( ) {
695748 assert_eq ! ( normalized_speaker_level( -127 ) , 0.0 ) ;
0 commit comments