BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / examples / echo_call.rs

echo_call.rs

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