| 1 | use std::sync::{Arc, Mutex}; |
| 2 | use std::time::Instant; |
| 3 | |
| 4 | use crate::diagnostics::{SdpSummary, summarize_sdp}; |
| 5 | |
| 6 | #[derive(Debug, Clone, PartialEq)] |
| 7 | pub struct TimelineEntry { |
| 8 | pub elapsed_ms: u128, |
| 9 | pub label: String, |
| 10 | pub detail: String, |
| 11 | } |
| 12 | |
| 13 | #[derive(Debug, Clone)] |
| 14 | pub struct Timeline { |
| 15 | started: Instant, |
| 16 | entries: Arc<Mutex<Vec<TimelineEntry>>>, |
| 17 | sdp: Arc<Mutex<Vec<SdpSummary>>>, |
| 18 | echo: bool, |
| 19 | } |
| 20 | |
| 21 | impl 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!( |
| 43 | "{:>6} ms {:<34} {}", |
| 44 | entry.elapsed_ms, entry.label, entry.detail |
| 45 | ); |
| 46 | } |
| 47 | self.entries.lock().expect("timeline lock").push(entry); |
| 48 | } |
| 49 | |
| 50 | pub fn record_sdp(&self, role: &str, sdp: &str) { |
| 51 | if let Some(summary) = summarize_sdp(self.elapsed_ms(), role, sdp) { |
| 52 | self.sdp.lock().expect("timeline lock").push(summary); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pub fn sdp_summaries(&self) -> Vec<SdpSummary> { |
| 57 | self.sdp.lock().expect("timeline lock").clone() |
| 58 | } |
| 59 | |
| 60 | pub fn entries(&self) -> Vec<TimelineEntry> { |
| 61 | self.entries.lock().expect("timeline lock").clone() |
| 62 | } |
| 63 | } |