active_call/media/
inactivity.rs1use super::processor::Processor;
2use crate::event::{EventSender, SessionEvent};
3use crate::media::{AudioFrame, get_timestamp};
4use anyhow::Result;
5use std::sync::Arc;
6use std::sync::atomic::{AtomicU64, Ordering};
7use std::time::Duration;
8use tokio_util::sync::CancellationToken;
9use tracing::info;
10
11pub struct InactivityProcessor {
12 last_received: Arc<AtomicU64>,
13}
14
15impl InactivityProcessor {
16 pub fn new(
17 track_id: String,
18 timeout: Duration,
19 event_sender: EventSender,
20 cancel_token: CancellationToken,
21 ) -> Self {
22 let last_received = Arc::new(AtomicU64::new(get_timestamp()));
23 let last_received_clone = last_received.clone();
24 let track_id_clone = track_id.clone();
25
26 crate::spawn(async move {
27 let mut interval = tokio::time::interval(Duration::from_secs(1));
28 loop {
29 tokio::select! {
30 _ = cancel_token.cancelled() => break,
31 _ = interval.tick() => {
32 let last = last_received_clone.load(Ordering::SeqCst);
33 let now = get_timestamp();
34 if now > last && now - last > timeout.as_millis() as u64 {
35 info!(track_id = track_id_clone, "Inactivity timeout reached, sending inactivity event");
36 let _ = event_sender.send(SessionEvent::Inactivity {
37 track_id: track_id_clone.clone(),
38 timestamp: now,
39 });
40 break;
41 }
42 }
43 }
44 }
45 });
46
47 Self { last_received }
48 }
49}
50
51impl Processor for InactivityProcessor {
52 fn process_frame(&mut self, _frame: &mut AudioFrame) -> Result<()> {
53 self.last_received.store(get_timestamp(), Ordering::SeqCst);
54 Ok(())
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use crate::event::SessionEvent;
62
63 fn frame(track_id: &str) -> AudioFrame {
64 AudioFrame {
65 track_id: track_id.to_string(),
66 samples: crate::media::Samples::PCM { samples: vec![] },
67 src_packet: None,
68 timestamp: 0,
69 sample_rate: 8000,
70 channels: 1,
71 speech_probability: None,
72 }
73 }
74
75 #[tokio::test]
76 async fn watchdog_fires_without_frames() {
77 let (tx, mut rx) = tokio::sync::broadcast::channel(8);
78 let token = CancellationToken::new();
79 let _p = InactivityProcessor::new("t".to_string(), Duration::from_millis(120), tx, token);
80 let ev = tokio::time::timeout(Duration::from_secs(2), rx.recv())
81 .await
82 .expect("no inactivity event within 2s");
83 assert!(matches!(ev, Ok(SessionEvent::Inactivity { .. })));
84 }
85
86 #[tokio::test]
92 async fn watchdog_disarmed_when_track_token_cancelled() {
93 let (tx, mut rx) = tokio::sync::broadcast::channel(8);
94 let session_token = CancellationToken::new();
95 let track_token = session_token.child_token();
96 let _p = InactivityProcessor::new(
97 "t".to_string(),
98 Duration::from_millis(120),
99 tx,
100 track_token,
101 );
102 session_token.cancel(); tokio::time::sleep(Duration::from_millis(400)).await;
104 assert!(
105 rx.try_recv().is_err(),
106 "zombie watchdog fired after its track was stopped"
107 );
108 }
109
110 #[tokio::test]
111 async fn watchdog_reset_by_frames() {
112 let (tx, mut rx) = tokio::sync::broadcast::channel(8);
113 let token = CancellationToken::new();
114 let mut p = InactivityProcessor::new("t".to_string(), Duration::from_millis(150), tx, token);
115 for _ in 0..8 {
117 tokio::time::sleep(Duration::from_millis(60)).await;
118 p.process_frame(&mut frame("t")).expect("process_frame");
119 }
120 assert!(
121 rx.try_recv().is_err(),
122 "watchdog fired despite continuous frames"
123 );
124 }
125}