| 1 | use std::path::PathBuf; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use calling::audio_io::AUDIO_MODE_ENV; |
| 5 | use calling::{AudioMode, CallCommand, CallOptions, CallReport, CallState, CallUpdate, call_channel, run_test_call}; |
| 6 | use session::{DEFAULT_ENDPOINT, Session}; |
| 7 | |
| 8 | const HOLD_AFTER_CONNECTED: Duration = Duration::from_secs(12); |
| 9 | const HARD_LIMIT: Duration = Duration::from_secs(20); |
| 10 | const HOLD_START: Duration = Duration::from_secs(4); |
| 11 | const HOLD_LENGTH: Duration = Duration::from_secs(10); |
| 12 | const AFTER_RESUME: Duration = Duration::from_secs(20); |
| 13 | |
| 14 | fn print_report(report: &CallReport) { |
| 15 | println!("# ice connected at {:?} ms, peer connected at {:?} ms", report.ice_connected_ms, report.peer_connected_ms); |
| 16 | println!("# selected pair: {}", report.selected_pair.as_deref().unwrap_or("-")); |
| 17 | println!("# dtls {} {} srtp {}", report.tls_version, report.dtls_cipher, report.srtp_cipher); |
| 18 | println!( |
| 19 | "# inbound audio: {} packets, {} bytes, {} s with new packets; outbound {} packets", |
| 20 | report.inbound_packets, report.inbound_bytes, report.seconds_with_inbound_audio, report.outbound_packets |
| 21 | ); |
| 22 | let video = &report.video; |
| 23 | println!( |
| 24 | "# video: inbound {} packets, {} frames decoded ({}x{}), {} frames to the UI hub; outbound {} packets, {} frames encoded ({}x{})", |
| 25 | video.inbound_packets, video.inbound_frames_decoded, video.inbound_width, video.inbound_height, video.frames_published, |
| 26 | video.outbound_packets, video.outbound_frames_encoded, video.outbound_width, video.outbound_height |
| 27 | ); |
| 28 | for second in &report.seconds { |
| 29 | println!( |
| 30 | "# t+{:>2}s packets={:>5} level={:.3} rms={:.3} tone440={:.2}", |
| 31 | second.second, second.packets, second.audio_level, second.decoded_rms, second.tone_ratio |
| 32 | ); |
| 33 | } |
| 34 | match &report.end { |
| 35 | Some(end) => println!("# call end: code={} subCode={} phrase={}", end.code, end.sub_code, end.phrase), |
| 36 | None => println!("# call end: no call/end callback"), |
| 37 | } |
| 38 | println!("# left cleanly: {}, decoded samples: {}", report.left_cleanly, report.recorded_samples); |
| 39 | } |
| 40 | |
| 41 | #[tokio::main] |
| 42 | async fn main() { |
| 43 | let wav_path = std::env::args().skip_while(|argument| argument != "--wav").nth(1).map(PathBuf::from); |
| 44 | let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned()); |
| 45 | let session = Session::connect(&endpoint).await.expect("browser"); |
| 46 | let poll_session = Session::connect(&endpoint).await.expect("browser"); |
| 47 | let audio = AudioMode::parse(Some(&std::env::var(AUDIO_MODE_ENV).unwrap_or_else(|_| "tone".to_owned()))); |
| 48 | let hold = std::env::args().any(|argument| argument == "--hold"); |
| 49 | let rounds: usize = std::env::args().skip_while(|argument| argument != "--rounds").nth(1).and_then(|value| value.parse().ok()).unwrap_or(1); |
| 50 | for round in 1..=rounds { |
| 51 | println!("# round {round} of {rounds}"); |
| 52 | let round_wav = match (&wav_path, rounds) { |
| 53 | (Some(path), 2..) => Some(path.with_extension(format!("{round}.wav"))), |
| 54 | _ => wav_path.clone(), |
| 55 | }; |
| 56 | test_call(&session, &poll_session, audio, round_wav, hold).await; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | async fn test_call(session: &Session, poll_session: &Session, audio: AudioMode, wav_path: Option<PathBuf>, hold: bool) { |
| 61 | let connected_for = if hold { HOLD_START + HOLD_LENGTH + AFTER_RESUME } else { HOLD_AFTER_CONNECTED }; |
| 62 | let options = CallOptions { |
| 63 | audio, |
| 64 | hold_after_connected: Some(connected_for), |
| 65 | hard_limit: Some(connected_for + HARD_LIMIT - HOLD_AFTER_CONNECTED), |
| 66 | record_remote: true, |
| 67 | wav_path: wav_path.clone(), |
| 68 | sdp_dump_dir: std::env::var("CALLING_SDP_DUMP").ok().map(PathBuf::from), |
| 69 | trace: true, |
| 70 | ..CallOptions::default() |
| 71 | }; |
| 72 | println!( |
| 73 | "# Test call, audio {:?}, hang up after {:?} connected (hard limit {:?})", |
| 74 | options.audio, options.hold_after_connected, options.hard_limit |
| 75 | ); |
| 76 | let (mut handle, control) = call_channel(); |
| 77 | let commands = handle.commands.clone(); |
| 78 | let drain = tokio::spawn(async move { |
| 79 | let mut hold_scheduled = !hold; |
| 80 | while let Some(update) = handle.updates.recv().await { |
| 81 | match update { |
| 82 | CallUpdate::State(CallState::Connected { .. }) if !hold_scheduled => { |
| 83 | hold_scheduled = true; |
| 84 | let commands = commands.clone(); |
| 85 | tokio::spawn(async move { |
| 86 | tokio::time::sleep(HOLD_START).await; |
| 87 | println!("# hold"); |
| 88 | let _ = commands.send(CallCommand::Hold(true)); |
| 89 | tokio::time::sleep(HOLD_LENGTH).await; |
| 90 | println!("# resume"); |
| 91 | let _ = commands.send(CallCommand::Hold(false)); |
| 92 | }); |
| 93 | } |
| 94 | CallUpdate::Hold(state) => println!("# hold state {state:?}"), |
| 95 | CallUpdate::Notice(text) => println!("# notice {text}"), |
| 96 | _ => {} |
| 97 | } |
| 98 | } |
| 99 | }); |
| 100 | let outcome = run_test_call(session, poll_session, options, control).await; |
| 101 | drain.abort(); |
| 102 | match outcome { |
| 103 | Ok(report) => { |
| 104 | print_report(&report); |
| 105 | if let Some(path) = wav_path { |
| 106 | println!("# wav written to {}", path.display()); |
| 107 | } |
| 108 | } |
| 109 | Err(error) => { |
| 110 | eprintln!("# test call failed: {error}"); |
| 111 | std::process::exit(1); |
| 112 | } |
| 113 | } |
| 114 | } |