BlueGit

ict rusty-teams

Files

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

echo_call.rs

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