rustpbx 0.4.8

A SIP PBX implementation in Rust
Documentation
/// Tests for unified PC architecture with dynamic audio source switching
use super::*;
use audio_source::*;

#[tokio::test]
async fn test_audio_source_manager_file_switching() {
    let manager = AudioSourceManager::new(8000);

    // Start with silence
    manager.switch_to_silence();
    assert!(manager.has_active_source());

    // Read some samples
    let mut buffer = vec![0i16; 160];
    let samples_read = manager.read_samples(&mut buffer);
    assert_eq!(samples_read, 160);
    assert!(buffer.iter().all(|&s| s == 0)); // All silence
}

#[test]
fn test_resampling_audio_source_8k_to_16k() {
    let silence = SilenceSource::new(8000);
    let mut resampling = ResamplingAudioSource::new(Box::new(silence), 16000);

    assert_eq!(resampling.sample_rate(), 16000);
    assert_eq!(resampling.channels(), 1);

    let mut buffer = vec![0i16; 320]; // 20ms @ 16kHz
    let samples_read = resampling.read_samples(&mut buffer);
    assert!(samples_read > 0);
}

#[tokio::test]
async fn test_unified_pc_no_reinvite_needed() {
    // This test demonstrates the unified PC architecture:
    // 1. Create FileTrack (with PC)
    let track =
        FileTrack::new("unified-pc-test".to_string()).with_path("/tmp/hold.wav".to_string());

    // 2. Get initial SDP
    let sdp1 = track.local_description().await;
    assert!(sdp1.is_ok());

    // 3. In the new architecture, switching audio source doesn't require
    // recreating the PC or getting new SDP. The same PC is used with
    // different audio sources flowing through it.

    // The key point: same PC, same SDP structure, different audio source
    // This is what enables seamless switching without client re-negotiation

    // Verify track ID is consistent
    assert_eq!(track.id(), "unified-pc-test");
}

#[test]
fn test_audio_source_has_data() {
    let mut silence = SilenceSource::new(8000);
    assert!(silence.has_data());
    assert_eq!(silence.sample_rate(), 8000);
    assert_eq!(silence.channels(), 1);

    let mut buffer = vec![0i16; 160];
    let samples_read = silence.read_samples(&mut buffer);
    assert_eq!(samples_read, 160);
}