use core::time::Duration;
use crate::jitter::JitterEstimate;
use crate::time::{Clock27, instant_micros};
use alloc::collections::VecDeque;
pub(crate) const EVENT_QUEUE_CAP: usize = 16;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum State {
Buffering,
Playing,
Starved,
}
impl State {
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::Buffering => "buffering",
Self::Playing => "playing",
Self::Starved => "starved",
}
}
#[must_use]
pub fn code(&self) -> u32 {
match self {
Self::Buffering => 1,
Self::Playing => 2,
Self::Starved => 3,
}
}
}
broadcast_common::impl_spec_display!(State);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum Reason {
Startup,
NoData,
DataArriving,
Discontinuity,
Reprime,
}
impl Reason {
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::Startup => "startup",
Self::NoData => "no data",
Self::DataArriving => "data arriving",
Self::Discontinuity => "discontinuity",
Self::Reprime => "reprime",
}
}
#[must_use]
pub fn code(&self) -> u32 {
match self {
Self::Startup => 1,
Self::NoData => 2,
Self::DataArriving => 3,
Self::Discontinuity => 4,
Self::Reprime => 5,
}
}
}
broadcast_common::impl_spec_display!(Reason);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum Hint {
InputDegraded,
InputRecovered,
SyncLoss,
PcrDiscontinuity,
SourceEnded,
SourceError,
Reconnected,
}
impl Hint {
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::InputDegraded => "input degraded",
Self::InputRecovered => "input recovered",
Self::SyncLoss => "sync loss",
Self::PcrDiscontinuity => "pcr discontinuity",
Self::SourceEnded => "source ended",
Self::SourceError => "source error",
Self::Reconnected => "reconnected",
}
}
#[must_use]
pub fn code(&self) -> u32 {
match self {
Self::InputDegraded => 1,
Self::InputRecovered => 2,
Self::SyncLoss => 3,
Self::PcrDiscontinuity => 4,
Self::SourceEnded => 5,
Self::SourceError => 6,
Self::Reconnected => 7,
}
}
#[must_use]
pub fn from_code(code: u32) -> Option<Self> {
Some(match code {
1 => Self::InputDegraded,
2 => Self::InputRecovered,
3 => Self::SyncLoss,
4 => Self::PcrDiscontinuity,
5 => Self::SourceEnded,
6 => Self::SourceError,
7 => Self::Reconnected,
_ => return None,
})
}
}
broadcast_common::impl_spec_display!(Hint);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum Event {
StartPlaying {
media_27mhz: Clock27,
lead: Duration,
},
Pause {
hold_at_27mhz: Clock27,
reason: Reason,
depth: Duration,
resume_target: Duration,
track: Option<u32>,
},
Resume {
media_27mhz: Clock27,
lead: Duration,
},
TargetChanged {
old: Duration,
new: Duration,
},
OverMax {
need: Duration,
max: Duration,
},
Rebaselined,
EventsDropped {
count: u32,
},
}
pub mod kind {
pub const START_PLAYING: u32 = 1;
pub const PAUSE: u32 = 2;
pub const RESUME: u32 = 3;
pub const TARGET_CHANGED: u32 = 4;
pub const OVER_MAX: u32 = 5;
pub const REBASELINED: u32 = 6;
pub const EVENTS_DROPPED: u32 = 7;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[repr(C)]
#[non_exhaustive]
pub struct EventRecord {
pub kind: u32,
pub reason: u32,
pub media_27mhz: u64,
pub a: u64,
pub b: u64,
pub track: u32,
pub track_valid: u32,
}
impl Event {
#[must_use]
pub fn to_record(&self) -> EventRecord {
let mut r = EventRecord::default();
match *self {
Self::StartPlaying { media_27mhz, lead } => {
r.kind = kind::START_PLAYING;
r.media_27mhz = media_27mhz.ticks();
r.a = instant_micros(lead);
}
Self::Pause {
hold_at_27mhz,
reason,
depth,
resume_target,
track,
} => {
r.kind = kind::PAUSE;
if let Some(t) = track {
r.track = t;
r.track_valid = 1;
}
r.reason = reason.code();
r.media_27mhz = hold_at_27mhz.ticks();
r.a = instant_micros(depth);
r.b = instant_micros(resume_target);
}
Self::Resume { media_27mhz, lead } => {
r.kind = kind::RESUME;
r.media_27mhz = media_27mhz.ticks();
r.a = instant_micros(lead);
}
Self::TargetChanged { old, new } => {
r.kind = kind::TARGET_CHANGED;
r.a = instant_micros(old);
r.b = instant_micros(new);
}
Self::OverMax { need, max } => {
r.kind = kind::OVER_MAX;
r.a = instant_micros(need);
r.b = instant_micros(max);
}
Self::Rebaselined => r.kind = kind::REBASELINED,
Self::EventsDropped { count } => {
r.kind = kind::EVENTS_DROPPED;
r.a = u64::from(count);
}
}
r
}
#[must_use]
pub fn name(&self) -> &'static str {
match self {
Self::StartPlaying { .. } => "start playing",
Self::Pause { .. } => "pause",
Self::Resume { .. } => "resume",
Self::TargetChanged { .. } => "target changed",
Self::OverMax { .. } => "over max",
Self::Rebaselined => "rebaselined",
Self::EventsDropped { .. } => "events dropped",
}
}
fn is_critical(&self) -> bool {
matches!(
self,
Self::StartPlaying { .. } | Self::Pause { .. } | Self::Resume { .. }
)
}
}
broadcast_common::impl_spec_display!(Event);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Stats {
pub state: State,
pub reason: crate::events::Reason,
pub lead: Duration,
pub target: Duration,
pub stalls: u32,
pub reprimes: u32,
pub last_stall_depth: Duration,
pub longest_freeze: Duration,
pub est: JitterEstimate,
pub clock_regressions: u32,
pub discontinuities: u32,
pub hints_seen: u32,
pub events_dropped: u32,
pub input_degraded: bool,
pub window_overflows: u32,
pub limiting_track: Option<u32>,
}
#[derive(Clone, Debug, Default)]
pub(crate) struct EventQueue {
q: VecDeque<Event>,
dropped_pending: u32,
dropped_total: u32,
}
impl EventQueue {
pub(crate) fn new() -> Self {
Self {
q: VecDeque::with_capacity(EVENT_QUEUE_CAP + 1),
dropped_pending: 0,
dropped_total: 0,
}
}
pub(crate) fn push(&mut self, e: Event) {
if self.q.len() >= EVENT_QUEUE_CAP {
let victim = self.q.iter().position(|x| !x.is_critical());
match victim {
Some(i) => {
self.q.remove(i);
}
None => {
self.q.pop_front();
}
}
self.dropped_pending = self.dropped_pending.saturating_add(1);
self.dropped_total = self.dropped_total.saturating_add(1);
}
self.q.push_back(e);
}
pub(crate) fn poll(&mut self) -> Option<Event> {
if self.dropped_pending > 0 {
let count = core::mem::take(&mut self.dropped_pending);
return Some(Event::EventsDropped { count });
}
self.q.pop_front()
}
pub(crate) fn dropped_total(&self) -> u32 {
self.dropped_total
}
pub(crate) fn clear(&mut self) {
self.q.clear();
self.dropped_pending = 0;
}
}