1use core::time::Duration;
12
13pub const MIN_CHUNK_STREAM_ID: u32 = 2;
16
17pub const MAX_CHUNK_STREAM_ID: u32 = 65_599;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[repr(transparent)]
26pub struct ChunkStreamId(u32);
27
28impl ChunkStreamId {
29 #[must_use]
35 pub const fn new(raw: u32) -> Self {
36 Self(raw)
37 }
38
39 #[must_use]
41 pub const fn get(self) -> u32 {
42 self.0
43 }
44
45 #[must_use]
47 pub const fn basic_header_encodable(self) -> bool {
48 self.0 >= MIN_CHUNK_STREAM_ID && self.0 <= MAX_CHUNK_STREAM_ID
49 }
50}
51
52impl From<u32> for ChunkStreamId {
53 fn from(raw: u32) -> Self {
54 Self(raw)
55 }
56}
57
58impl From<ChunkStreamId> for u32 {
59 fn from(id: ChunkStreamId) -> Self {
60 id.0
61 }
62}
63
64impl core::fmt::Display for ChunkStreamId {
65 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
66 write!(f, "{}", self.0)
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
74#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
75#[repr(transparent)]
76pub struct MessageStreamId(u32);
77
78impl MessageStreamId {
79 #[must_use]
81 pub const fn new(raw: u32) -> Self {
82 Self(raw)
83 }
84
85 #[must_use]
87 pub const fn get(self) -> u32 {
88 self.0
89 }
90}
91
92impl From<u32> for MessageStreamId {
93 fn from(raw: u32) -> Self {
94 Self(raw)
95 }
96}
97
98impl From<MessageStreamId> for u32 {
99 fn from(id: MessageStreamId) -> Self {
100 id.0
101 }
102}
103
104impl core::fmt::Display for MessageStreamId {
105 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
106 write!(f, "{}", self.0)
107 }
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
113#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
114#[repr(transparent)]
115pub struct SequenceNumber(u32);
116
117impl SequenceNumber {
118 #[must_use]
120 pub const fn new(raw: u32) -> Self {
121 Self(raw)
122 }
123
124 #[must_use]
126 pub const fn get(self) -> u32 {
127 self.0
128 }
129}
130
131impl From<u32> for SequenceNumber {
132 fn from(raw: u32) -> Self {
133 Self(raw)
134 }
135}
136
137impl From<SequenceNumber> for u32 {
138 fn from(seq: SequenceNumber) -> Self {
139 seq.0
140 }
141}
142
143impl core::fmt::Display for SequenceNumber {
144 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
145 write!(f, "{}", self.0)
146 }
147}
148
149#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
158#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
159pub struct Timestamp(Duration);
160
161impl Timestamp {
162 pub const ZERO: Self = Self(Duration::ZERO);
164
165 pub fn try_new(span: Duration) -> Result<Self, TimestampOutOfRange> {
171 u32::try_from(span.as_millis())
172 .map(|_| Self(span))
173 .map_err(|_| TimestampOutOfRange)
174 }
175
176 #[must_use]
178 pub const fn span(self) -> Duration {
179 self.0
180 }
181
182 #[must_use]
184 pub fn millis(self) -> u32 {
185 u32::try_from(self.0.as_millis()).unwrap_or(u32::MAX)
186 }
187
188 #[must_use]
190 pub const fn from_millis(millis: u32) -> Self {
191 Self(Duration::from_millis(millis as u64))
192 }
193
194 #[must_use]
196 pub fn checked_add(self, delta: Duration) -> Option<Self> {
197 self.0
198 .checked_add(delta)
199 .and_then(|s| Self::try_new(s).ok())
200 }
201}
202
203impl From<Duration> for Timestamp {
204 fn from(span: Duration) -> Self {
205 Self(span)
206 }
207}
208
209impl From<Timestamp> for Duration {
210 fn from(ts: Timestamp) -> Self {
211 ts.0
212 }
213}
214
215impl core::fmt::Display for Timestamp {
216 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
217 write!(f, "{} ms", self.millis())
218 }
219}
220
221#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
224#[error("RTMP timestamp does not fit the 32-bit wire field (max 2^32-1 ms)")]
225pub struct TimestampOutOfRange;
226
227#[cfg(test)]
228mod tests {
229 use super::*;
230
231 #[test]
232 fn timestamp_out_of_range_displays_the_wire_limit() {
233 extern crate alloc;
234 use alloc::string::ToString;
235 assert_eq!(
236 TimestampOutOfRange.to_string(),
237 "RTMP timestamp does not fit the 32-bit wire field (max 2^32-1 ms)"
238 );
239 }
240
241 #[test]
242 fn chunk_stream_ids_mark_which_are_encodable() {
243 assert!(!ChunkStreamId::new(1).basic_header_encodable());
244 assert!(ChunkStreamId::new(MIN_CHUNK_STREAM_ID).basic_header_encodable());
245 assert!(ChunkStreamId::new(MAX_CHUNK_STREAM_ID).basic_header_encodable());
246 assert!(!ChunkStreamId::new(MAX_CHUNK_STREAM_ID + 1).basic_header_encodable());
247 }
248
249 #[test]
250 fn timestamps_round_trip_through_milliseconds() {
251 let ts = Timestamp::try_new(Duration::from_millis(3000)).unwrap();
252 assert_eq!(ts.millis(), 3000);
253 assert_eq!(ts.span(), Duration::from_millis(3000));
254 assert_eq!(Timestamp::from_millis(3000), ts);
255 assert_eq!(Duration::from(ts), Duration::from_millis(3000));
256 }
257
258 #[test]
259 fn a_timestamp_past_the_wire_width_is_rejected() {
260 assert_eq!(
261 Timestamp::try_new(Duration::from_millis(u64::from(u32::MAX) + 1)),
262 Err(TimestampOutOfRange)
263 );
264 assert!(Timestamp::try_new(Duration::from_millis(u64::from(u32::MAX))).is_ok());
265 }
266
267 #[test]
268 fn adding_a_delta_past_the_wire_width_is_none() {
269 let near_end = Timestamp::from_millis(u32::MAX);
270 assert_eq!(near_end.checked_add(Duration::from_millis(1)), None);
271 assert_eq!(
272 Timestamp::from_millis(1).checked_add(Duration::from_millis(1)),
273 Some(Timestamp::from_millis(2))
274 );
275 }
276
277 #[test]
278 fn ids_carry_their_raw_value() {
279 assert_eq!(ChunkStreamId::from(4).get(), 4);
280 assert_eq!(u32::from(MessageStreamId::new(0)), 0);
281 assert_eq!(ChunkStreamId::new(2).to_string(), "2");
282 }
283}