broadcast_dash/
utc_timing.rs1use broadcast_common::impl_spec_display;
7
8pub const SCHEME_NTP: &str = "urn:mpeg:dash:utc:ntp:2014";
10pub const SCHEME_SNTP: &str = "urn:mpeg:dash:utc:sntp:2014";
12pub const SCHEME_HTTP_HEAD: &str = "urn:mpeg:dash:utc:http-head:2014";
14pub const SCHEME_HTTP_XSDATE: &str = "urn:mpeg:dash:utc:http-xsdate:2014";
16pub const SCHEME_HTTP_ISO: &str = "urn:mpeg:dash:utc:http-iso:2014";
18pub const SCHEME_HTTP_NTP: &str = "urn:mpeg:dash:utc:http-ntp:2014";
20pub const SCHEME_DIRECT: &str = "urn:mpeg:dash:utc:direct:2014";
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
25#[non_exhaustive]
26pub enum UtcScheme {
27 Ntp,
29 Sntp,
31 HttpHead,
33 HttpXsdate,
35 HttpIso,
37 HttpNtp,
39 #[default]
41 Direct,
42 Unknown,
44}
45
46impl UtcScheme {
47 #[must_use]
49 pub fn from_scheme_id_uri(uri: &str) -> Self {
50 match uri {
51 SCHEME_NTP => Self::Ntp,
52 SCHEME_SNTP => Self::Sntp,
53 SCHEME_HTTP_HEAD => Self::HttpHead,
54 SCHEME_HTTP_XSDATE => Self::HttpXsdate,
55 SCHEME_HTTP_ISO => Self::HttpIso,
56 SCHEME_HTTP_NTP => Self::HttpNtp,
57 SCHEME_DIRECT => Self::Direct,
58 _ => Self::Unknown,
59 }
60 }
61
62 #[must_use]
64 pub fn name(&self) -> &'static str {
65 match self {
66 Self::Ntp => "ntp",
67 Self::Sntp => "sntp",
68 Self::HttpHead => "http_head",
69 Self::HttpXsdate => "http_xsdate",
70 Self::HttpIso => "http_iso",
71 Self::HttpNtp => "http_ntp",
72 Self::Direct => "direct",
73 Self::Unknown => "unknown",
74 }
75 }
76
77 #[must_use]
79 pub fn is_actionable(&self) -> bool {
80 !matches!(self, Self::Ntp | Self::Sntp | Self::Unknown)
81 }
82
83 #[must_use]
85 pub fn needs_request(&self) -> bool {
86 matches!(
87 self,
88 Self::HttpHead | Self::HttpXsdate | Self::HttpIso | Self::HttpNtp
89 )
90 }
91}
92impl_spec_display!(UtcScheme);
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn schemes_round_trip_through_their_uri() {
100 for (uri, scheme) in [
101 (SCHEME_NTP, UtcScheme::Ntp),
102 (SCHEME_SNTP, UtcScheme::Sntp),
103 (SCHEME_HTTP_HEAD, UtcScheme::HttpHead),
104 (SCHEME_HTTP_XSDATE, UtcScheme::HttpXsdate),
105 (SCHEME_HTTP_ISO, UtcScheme::HttpIso),
106 (SCHEME_HTTP_NTP, UtcScheme::HttpNtp),
107 (SCHEME_DIRECT, UtcScheme::Direct),
108 ("urn:example:utc", UtcScheme::Unknown),
109 ] {
110 assert_eq!(UtcScheme::from_scheme_id_uri(uri), scheme, "{uri}");
111 }
112 }
113}