BlueGit

ict rusty-teams

Files

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

echo_call.rs

echo_call.rs 3.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 for second in &report.seconds {
20 println!(
21 "# t+{:>2}s packets={:>5} level={:.3} rms={:.3} tone440={:.2}",
22 second.second, second.packets, second.audio_level, second.decoded_rms, second.tone_ratio
23 );
24 }
25 match &report.end {
26 Some(end) => println!("# call end: code={} subCode={} phrase={}", end.code, end.sub_code, end.phrase),
27 None => println!("# call end: no call/end callback"),
28 }
29 println!("# left cleanly: {}, decoded samples: {}", report.left_cleanly, report.recorded_samples);
30}
31
32#[tokio::main]
33async fn main() {
34 let wav_path = std::env::args().skip_while(|argument| argument != "--wav").nth(1).map(PathBuf::from);
35 let endpoint = std::env::var("CDP_ENDPOINT").unwrap_or_else(|_| DEFAULT_ENDPOINT.to_owned());
36 let session = Session::connect(&endpoint).await.expect("browser");
37 let poll_session = Session::connect(&endpoint).await.expect("browser");
38 let audio = AudioMode::parse(Some(&std::env::var(AUDIO_MODE_ENV).unwrap_or_else(|_| "tone".to_owned())));
39 let options = CallOptions {
40 audio,
41 hold_after_connected: Some(HOLD_AFTER_CONNECTED),
42 hard_limit: Some(HARD_LIMIT),
43 record_remote: true,
44 wav_path: wav_path.clone(),
45 sdp_dump_dir: std::env::var("CALLING_SDP_DUMP").ok().map(PathBuf::from),
46 trace: true,
47 ..CallOptions::default()
48 };
49 println!(
50 "# Test call, audio {:?}, hang up after {:?} connected (hard limit {:?})",
51 options.audio, HOLD_AFTER_CONNECTED, HARD_LIMIT
52 );
53 let (mut handle, control) = call_channel();
54 let drain = tokio::spawn(async move { while handle.updates.recv().await.is_some() {} });
55 let outcome = run_test_call(&session, &poll_session, options, control).await;
56 let _ = drain.await;
57 match outcome {
58 Ok(report) => {
59 print_report(&report);
60 if let Some(path) = wav_path {
61 println!("# wav written to {}", path.display());
62 }
63 }
64 Err(error) => {
65 eprintln!("# test call failed: {error}");
66 std::process::exit(1);
67 }
68 }
69}