BlueGit

ict rusty-teams

Files

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

echo_call.rs

echo_call.rs 3.4 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 options = CallOptions {
46 audio,
47 hold_after_connected: Some(HOLD_AFTER_CONNECTED),
48 hard_limit: Some(HARD_LIMIT),
49 record_remote: true,
50 wav_path: wav_path.clone(),
51 sdp_dump_dir: std::env::var("CALLING_SDP_DUMP").ok().map(PathBuf::from),
52 trace: true,
53 ..CallOptions::default()
54 };
55 println!(
56 "# Test call, audio {:?}, hang up after {:?} connected (hard limit {:?})",
57 options.audio, HOLD_AFTER_CONNECTED, HARD_LIMIT
58 );
59 let (mut handle, control) = call_channel();
60 let drain = tokio::spawn(async move { while handle.updates.recv().await.is_some() {} });
61 let outcome = run_test_call(&session, &poll_session, options, control).await;
62 let _ = drain.await;
63 match outcome {
64 Ok(report) => {
65 print_report(&report);
66 if let Some(path) = wav_path {
67 println!("# wav written to {}", path.display());
68 }
69 }
70 Err(error) => {
71 eprintln!("# test call failed: {error}");
72 std::process::exit(1);
73 }
74 }
75}