BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / timeline.rs

timeline.rs

timeline.rs 1.6 KiB Download Raw
1use std::sync::{Arc, Mutex};
2use std::time::Instant;
3
4use crate::diagnostics::{SdpSummary, summarize_sdp};
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct TimelineEntry {
8 pub elapsed_ms: u128,
9 pub label: String,
10 pub detail: String,
11}
12
13#[derive(Debug, Clone)]
14pub struct Timeline {
15 started: Instant,
16 entries: Arc<Mutex<Vec<TimelineEntry>>>,
17 sdp: Arc<Mutex<Vec<SdpSummary>>>,
18 echo: bool,
19}
20
21impl Timeline {
22 pub fn new(echo: bool) -> Self {
23 Timeline {
24 started: Instant::now(),
25 entries: Arc::default(),
26 sdp: Arc::default(),
27 echo,
28 }
29 }
30
31 pub fn elapsed_ms(&self) -> u128 {
32 self.started.elapsed().as_millis()
33 }
34
35 pub fn record(&self, label: impl Into<String>, detail: impl Into<String>) {
36 let entry = TimelineEntry {
37 elapsed_ms: self.elapsed_ms(),
38 label: label.into(),
39 detail: detail.into(),
40 };
41 if self.echo {
42 println!("{:>6} ms {:<34} {}", entry.elapsed_ms, entry.label, entry.detail);
43 }
44 self.entries.lock().expect("timeline lock").push(entry);
45 }
46
47 pub fn record_sdp(&self, role: &str, sdp: &str) {
48 if let Some(summary) = summarize_sdp(self.elapsed_ms(), role, sdp) {
49 self.sdp.lock().expect("timeline lock").push(summary);
50 }
51 }
52
53 pub fn sdp_summaries(&self) -> Vec<SdpSummary> {
54 self.sdp.lock().expect("timeline lock").clone()
55 }
56
57 pub fn entries(&self) -> Vec<TimelineEntry> {
58 self.entries.lock().expect("timeline lock").clone()
59 }
60}