BlueGit

ict rusty-teams

Files

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

echo_call.rs

echo_call.rs 5.6 KiB Download Raw
1use std::path::PathBuf;
2use std::time::Duration;
3
4use calling::audio_io::AUDIO_MODE_ENV;
5use calling::{AudioMode, CallCommand, CallOptions, CallReport, CallState, CallUpdate, 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);
10const HOLD_START: Duration = Duration::from_secs(4);
11const HOLD_LENGTH: Duration = Duration::from_secs(10);
12const AFTER_RESUME: Duration = Duration::from_secs(20);
13
14fn 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]
42async 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
60async fn test_call(session: &Session, poll_session: &Session, audio: AudioMode, wav_path: Option<PathBuf>, hold: bool) {
61 let seconds = std::env::args().skip_while(|argument| argument != "--seconds").nth(1).and_then(|value| value.parse().ok());
62 let connected_for = match (hold, seconds) {
63 (true, _) => HOLD_START + HOLD_LENGTH + AFTER_RESUME,
64 (false, Some(seconds)) => Duration::from_secs(seconds),
65 (false, None) => HOLD_AFTER_CONNECTED,
66 };
67 let options = CallOptions {
68 audio,
69 hold_after_connected: Some(connected_for),
70 hard_limit: Some(connected_for + HARD_LIMIT - HOLD_AFTER_CONNECTED),
71 record_remote: true,
72 wav_path: wav_path.clone(),
73 sdp_dump_dir: std::env::var("CALLING_SDP_DUMP").ok().map(PathBuf::from),
74 trace: true,
75 ..CallOptions::default()
76 };
77 println!(
78 "# Test call, audio {:?}, hang up after {:?} connected (hard limit {:?})",
79 options.audio, options.hold_after_connected, options.hard_limit
80 );
81 let (mut handle, control) = call_channel();
82 let commands = handle.commands.clone();
83 let drain = tokio::spawn(async move {
84 let mut hold_scheduled = !hold;
85 while let Some(update) = handle.updates.recv().await {
86 match update {
87 CallUpdate::State(CallState::Connected { .. }) if !hold_scheduled => {
88 hold_scheduled = true;
89 let commands = commands.clone();
90 tokio::spawn(async move {
91 tokio::time::sleep(HOLD_START).await;
92 println!("# hold");
93 let _ = commands.send(CallCommand::Hold(true));
94 tokio::time::sleep(HOLD_LENGTH).await;
95 println!("# resume");
96 let _ = commands.send(CallCommand::Hold(false));
97 });
98 }
99 CallUpdate::Hold(state) => println!("# hold state {state:?}"),
100 CallUpdate::Notice(text) => println!("# notice {text}"),
101 _ => {}
102 }
103 }
104 });
105 let outcome = run_test_call(session, poll_session, options, control).await;
106 drain.abort();
107 match outcome {
108 Ok(report) => {
109 print_report(&report);
110 if let Some(path) = wav_path {
111 println!("# wav written to {}", path.display());
112 }
113 }
114 Err(error) => {
115 eprintln!("# test call failed: {error}");
116 std::process::exit(1);
117 }
118 }
119}