use crate::metrics::{Counter, Gauge, Histogram, RateCalculator};
use std::sync::Mutex;
pub struct StreamingCollector {
frames_received: Counter,
frames_encoded: Counter,
frames_sent: Counter,
frames_dropped: Counter,
encoder_queue_depth: Gauge,
bytes_sent: Counter,
fps_calculator: RateCalculator,
bandwidth_calculator: RateCalculator,
encoding_histogram: Mutex<Option<Histogram>>,
slow_frames: Counter,
}
impl StreamingCollector {
pub fn new(_source: impl Into<String>) -> Self {
Self {
frames_received: Counter::new(),
frames_encoded: Counter::new(),
frames_sent: Counter::new(),
frames_dropped: Counter::new(),
encoder_queue_depth: Gauge::new(),
bytes_sent: Counter::new(),
fps_calculator: RateCalculator::new(),
bandwidth_calculator: RateCalculator::new(),
encoding_histogram: Mutex::new(Histogram::for_duration().ok()),
slow_frames: Counter::new(),
}
}
#[inline]
pub fn record_frame_received(&self) {
self.frames_received.inc();
}
#[inline]
pub fn record_frame_encoded(&self, encode_time_us: u64) {
self.frames_encoded.inc();
if let Ok(mut hist) = self.encoding_histogram.try_lock() {
if let Some(h) = hist.as_mut() {
h.record(encode_time_us);
}
}
if encode_time_us > 50_000 {
self.slow_frames.inc();
}
}
#[inline]
pub fn record_frame_sent(&self, frame_size_bytes: u64) {
self.frames_sent.inc();
self.bytes_sent.add(frame_size_bytes);
}
#[inline]
pub fn record_frame_dropped(&self) {
self.frames_dropped.inc();
}
pub fn reset(&self) {
self.frames_received.reset();
self.frames_encoded.reset();
self.frames_sent.reset();
self.frames_dropped.reset();
self.bytes_sent.reset();
self.slow_frames.reset();
self.encoder_queue_depth.set(0);
self.fps_calculator.reset();
self.bandwidth_calculator.reset();
if let Ok(mut hist) = self.encoding_histogram.lock() {
if let Some(h) = hist.as_mut() {
h.reset();
}
}
}
}