active-call 0.3.64

A SIP/WebRTC voice agent
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
use crate::media::{
    AudioFrame, PcmBuf, Sample, Samples,
    recorder::{Recorder, RecorderOption},
};
use anyhow::Result;
use std::{path::Path, sync::Arc};
use tempfile::tempdir;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

#[tokio::test]
async fn test_recorder() -> Result<()> {
    // Setup
    let temp_dir = tempdir()?;
    let file_path = temp_dir.path().join("test_recording.wav");
    let file_path_clone = file_path.clone(); // Clone for the spawned task
    let cancel_token = CancellationToken::new();
    let config = RecorderOption::default();

    let recorder = Arc::new(Recorder::new(
        cancel_token.clone(),
        "test".to_string(),
        config,
    ));

    // Create channels for testing
    let (tx, rx) = mpsc::unbounded_channel();

    // Start recording in the background
    let recorder_clone = recorder.clone();
    let recorder_hanadle = tokio::spawn(async move {
        let r = recorder_clone.process_recording(&file_path_clone, rx).await;
        println!("recorder: {:?}", r);
    });

    // Create test frames
    let left_channel_id = "left".to_string();
    let right_channel_id = "right".to_string();

    // Generate some sample audio data (sine wave)
    let sample_count = 1600; // 100ms of audio at 16kHz

    for i in 0..5 {
        // Send 5 frames (500ms total)
        // Left channel (lower frequency sine wave)
        let left_samples: PcmBuf = (0..sample_count)
            .map(|j| {
                let t = (i * sample_count + j) as f32 / 16000.0;
                ((t * 440.0 * 2.0 * std::f32::consts::PI).sin() * 16384.0) as Sample
            })
            .collect();

        // Right channel (higher frequency sine wave)
        let right_samples: PcmBuf = (0..sample_count)
            .map(|j| {
                let t = (i * sample_count + j) as f32 / 16000.0;
                ((t * 880.0 * 2.0 * std::f32::consts::PI).sin() * 16384.0) as Sample
            })
            .collect();

        let left_frame = AudioFrame {
            track_id: left_channel_id.clone(),
            samples: Samples::PCM {
                samples: left_samples,
            },
            timestamp: (i * 100), // Increment timestamp by 100ms
            sample_rate: 16000,
            channels: 1,
            ..Default::default()
        };

        let right_frame = AudioFrame {
            track_id: right_channel_id.clone(),
            samples: Samples::PCM {
                samples: right_samples,
            },
            timestamp: (i * 100), // Same timestamp for synchronized channels
            sample_rate: 16000,
            channels: 1,
            ..Default::default()
        };

        // Send frames
        tx.send(left_frame)?;
        tx.send(right_frame)?;

        // Wait a bit to simulate real-time recording
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
    }
    recorder.stop_recording()?;
    recorder_hanadle.await?;
    // Verify the file exists
    assert!(file_path.exists());
    println!("file_path: {:?}", file_path.to_str());
    // Verify the file is a valid WAV file with expected content
    verify_wav_file(&file_path)?;

    Ok(())
}

fn verify_wav_file(path: &Path) -> Result<()> {
    // Open the WAV file
    let reader = hound::WavReader::open(path)?;
    let spec = reader.spec();

    // Verify format
    assert_eq!(spec.channels, 2); // Stereo
    assert_eq!(spec.sample_rate, 16000);
    assert_eq!(spec.bits_per_sample, 16);
    assert_eq!(spec.sample_format, hound::SampleFormat::Int);

    // Verify the file has some samples
    let samples_count = reader.len();
    assert!(samples_count > 0, "WAV file has no samples");

    Ok(())
}

#[tokio::test]
async fn test_recorder_intermittent_data() -> Result<()> {
    // Test for intermittent data handling and clipping detection
    let temp_dir = tempdir()?;
    let file_path = temp_dir.path().join("test_intermittent.wav");
    let file_path_clone = file_path.clone();
    let cancel_token = CancellationToken::new();
    let config = RecorderOption::default();

    let recorder = Arc::new(Recorder::new(
        cancel_token.clone(),
        "test".to_string(),
        config,
    ));
    let (tx, rx) = mpsc::unbounded_channel();

    // Start recording
    let recorder_clone = recorder.clone();
    let recorder_handle =
        tokio::spawn(async move { recorder_clone.process_recording(&file_path_clone, rx).await });

    let track_id = "test_track".to_string();

    // Test 1: Send intermittent small frames (less than chunk_size)
    for i in 0..10 {
        let small_samples: PcmBuf = (0..50) // Small frame, much less than chunk_size (320)
            .map(|j| {
                let t = (i * 50 + j) as f32 / 16000.0;
                ((t * 440.0 * 2.0 * std::f32::consts::PI).sin() * 16384.0) as Sample
            })
            .collect();

        let frame = AudioFrame {
            track_id: track_id.clone(),
            samples: Samples::PCM {
                samples: small_samples,
            },
            timestamp: (i * 50) as u64,
            sample_rate: 16000,
            channels: 1,
            ..Default::default()
        };

        tx.send(frame)?;

        // Simulate intermittent arrival with gaps
        if i % 3 == 0 {
            tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
        }
    }

    // Test 2: Send frames with clipping values
    let clipped_samples: PcmBuf = vec![32767, -32768, 32767, -32768, 0, 0, 0, 0]; // Clipped values
    let clipped_frame = AudioFrame {
        track_id: track_id.clone(),
        samples: Samples::PCM {
            samples: clipped_samples,
        },
        timestamp: 1000,
        sample_rate: 16000,
        channels: 1,
        ..Default::default()
    };
    tx.send(clipped_frame)?;

    // Test 3: Send frames with constant values (freeze detection)
    let constant_samples: PcmBuf = vec![1000; 20]; // 20 identical values
    let constant_frame = AudioFrame {
        track_id: track_id.clone(),
        samples: Samples::PCM {
            samples: constant_samples,
        },
        timestamp: 1100,
        sample_rate: 16000,
        channels: 1,
        ..Default::default()
    };
    tx.send(constant_frame)?;

    // Wait for processing
    tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;

    // Stop recording
    recorder.stop_recording()?;
    recorder_handle.await??;

    // Verify file exists and is valid
    assert!(file_path.exists());
    verify_wav_file(&file_path)?;

    println!("Intermittent data test completed successfully");
    Ok(())
}

#[tokio::test]
async fn test_constant_value_detection() -> Result<()> {
    // Test the improved constant value detection logic
    let temp_dir = tempdir()?;
    let file_path = temp_dir.path().join("test_constant_detection.wav");
    let file_path_clone = file_path.clone();
    let cancel_token = CancellationToken::new();
    let config = RecorderOption::default();

    let recorder = Arc::new(Recorder::new(
        cancel_token.clone(),
        "test".to_string(),
        config,
    ));
    let (tx, rx) = mpsc::unbounded_channel();

    // Start recording
    let recorder_clone = recorder.clone();
    let recorder_handle =
        tokio::spawn(async move { recorder_clone.process_recording(&file_path_clone, rx).await });

    let track_id = "test_track".to_string();

    // Test 1: Short silence (should NOT trigger warning)
    let short_silence: PcmBuf = vec![0; 15]; // Less than 20 samples
    let frame1 = AudioFrame {
        track_id: track_id.clone(),
        samples: Samples::PCM {
            samples: short_silence,
        },
        timestamp: 0,
        sample_rate: 16000,
        channels: 1,
        ..Default::default()
    };
    tx.send(frame1)?;

    // Test 2: Medium silence (should NOT trigger warning as it's normal)
    let medium_silence: PcmBuf = vec![0; 40]; // Less than 50 samples
    let frame2 = AudioFrame {
        track_id: track_id.clone(),
        samples: Samples::PCM {
            samples: medium_silence,
        },
        timestamp: 100,
        sample_rate: 16000,
        channels: 1,
        ..Default::default()
    };
    tx.send(frame2)?;

    // Test 3: Large silence buffer (should trigger warning)
    let large_silence: PcmBuf = vec![0; 100]; // More than 50 samples, all zeros
    let frame3 = AudioFrame {
        track_id: track_id.clone(),
        samples: Samples::PCM {
            samples: large_silence,
        },
        timestamp: 200,
        sample_rate: 16000,
        channels: 1,
        ..Default::default()
    };
    tx.send(frame3)?;

    // Test 4: Non-zero constant values (should trigger warning)
    let constant_non_zero: PcmBuf = vec![1000; 50]; // 50 identical non-zero values
    let frame4 = AudioFrame {
        track_id: track_id.clone(),
        samples: Samples::PCM {
            samples: constant_non_zero,
        },
        timestamp: 300,
        sample_rate: 16000,
        channels: 1,
        ..Default::default()
    };
    tx.send(frame4)?;

    // Test 5: Normal audio (should NOT trigger warning)
    let normal_audio: PcmBuf = (0..50)
        .map(|i| ((i as f32 * 0.1).sin() * 1000.0) as Sample)
        .collect();
    let frame5 = AudioFrame {
        track_id: track_id.clone(),
        samples: Samples::PCM {
            samples: normal_audio,
        },
        timestamp: 400,
        sample_rate: 16000,
        channels: 1,
        ..Default::default()
    };
    tx.send(frame5)?;

    // Wait for processing
    tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;

    // Stop recording
    recorder.stop_recording()?;
    recorder_handle.await??;

    // Verify file exists and is valid
    assert!(file_path.exists());
    verify_wav_file(&file_path)?;

    println!("Constant value detection test completed successfully");
    Ok(())
}

#[tokio::test]
async fn test_recorder_200ms_timing() -> Result<()> {
    // Test with the new 200ms timing and improved buffer handling
    let temp_dir = tempdir()?;
    let file_path = temp_dir.path().join("test_200ms_recording.wav");
    let file_path_clone = file_path.clone();
    let cancel_token = CancellationToken::new();
    let config = RecorderOption::default(); // Uses 200ms ptime now

    let recorder = Arc::new(Recorder::new(
        cancel_token.clone(),
        "test".to_string(),
        config,
    ));
    let (tx, rx) = mpsc::unbounded_channel();

    // Start recording
    let recorder_clone = recorder.clone();
    let recorder_handle = tokio::spawn(async move {
        let r = recorder_clone.process_recording(&file_path_clone, rx).await;
        println!("recorder result: {:?}", r);
    });

    let track_id_1 = "track_1".to_string();
    let track_id_2 = "track_2".to_string();

    // Send smaller, irregular frames to test the improved pop mechanism
    for i in 0..10 {
        // Varying frame sizes to test the new buffer handling
        let frame_size = 100 + (i * 50); // Variable frame sizes: 100, 150, 200, ..., 550

        let samples_1: PcmBuf = (0..frame_size)
            .map(|j| {
                let t = (i * frame_size + j) as f32 / 16000.0;
                ((t * 440.0 * 2.0 * std::f32::consts::PI).sin() * 8000.0) as Sample
            })
            .collect();

        let samples_2: PcmBuf = (0..frame_size)
            .map(|j| {
                let t = (i * frame_size + j) as f32 / 16000.0;
                ((t * 880.0 * 2.0 * std::f32::consts::PI).sin() * 8000.0) as Sample
            })
            .collect();

        let frame_1 = AudioFrame {
            track_id: track_id_1.clone(),
            samples: Samples::PCM { samples: samples_1 },
            timestamp: (i * 200) as u64, // 200ms intervals
            sample_rate: 16000,
            channels: 1,
            ..Default::default()
        };

        let frame_2 = AudioFrame {
            track_id: track_id_2.clone(),
            samples: Samples::PCM { samples: samples_2 },
            timestamp: (i * 200) as u64,
            sample_rate: 16000,
            channels: 1,
            ..Default::default()
        };

        tx.send(frame_1)?;
        // Send second frame with slight delay to test buffer handling
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
        tx.send(frame_2)?;

        // Wait for the 200ms interval
        tokio::time::sleep(tokio::time::Duration::from_millis(150)).await;
    }

    // Let it process for a bit longer
    tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

    // Stop recording - this should trigger the buffer flush
    recorder.stop_recording()?;
    recorder_handle.await?;

    // Verify the file exists and is valid
    assert!(file_path.exists());
    verify_wav_file(&file_path)?;

    println!("200ms timing test completed successfully");
    Ok(())
}