1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use core::time::Duration;
/// RTMP のタイムスタンプを表す構造体
///
/// RTMP では、タイムスタンプはミリ秒単位の符号なし 32 ビット整数として扱われる
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RtmpTimestamp(u32);
impl RtmpTimestamp {
/// ゼロタイムスタンプ(0 ミリ秒)
pub const ZERO: Self = Self(0);
/// ミリ秒単位の値から [`RtmpTimestamp`] を作成する
pub const fn from_millis(t: u32) -> Self {
Self(t)
}
/// タイムスタンプをミリ秒単位の値として取得する
pub const fn as_millis(self) -> u32 {
self.0
}
/// タイムスタンプを [`Duration`] として取得する
pub const fn as_duration(self) -> Duration {
Duration::from_millis(self.0 as u64)
}
/// 別のタイムスタンプを加算する(オーバーフロー時はラップアラウンドする)
pub fn wrapping_add(self, other: Self) -> Self {
Self(self.0.wrapping_add(other.0))
}
/// 別のタイムスタンプを減算する(結果が負になる場合は `None` を返す)
pub fn checked_sub(self, other: Self) -> Option<Self> {
self.0.checked_sub(other.0).map(Self)
}
}
/// RTMP のタイムスタンプの差分を表す構造体
///
/// RTMP では、タイムスタンプの差分はミリ秒単位の符号付き 32 ビット整数として扱われる
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RtmpTimestampDelta(i32);
impl RtmpTimestampDelta {
/// ゼロ差分(0 ミリ秒)
pub const ZERO: Self = Self(0);
/// ミリ秒単位の値から [`RtmpTimestampDelta`] を作成する
pub const fn from_millis(t: i32) -> Self {
Self(t)
}
/// タイムスタンプ差分をミリ秒単位の値として取得する
pub const fn as_millis(self) -> i32 {
self.0
}
}