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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use druid::Data;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
#[derive(
Copy, Clone, Data, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize,
)]
#[serde(transparent)]
pub struct Time(i64);
#[derive(
Copy, Clone, Data, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize,
)]
#[serde(transparent)]
pub struct Diff(i64);
#[derive(Copy, Clone, Data, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct TimeSpan {
start: Time,
end: Time,
}
pub const ZERO: Time = Time(0);
impl Time {
pub fn from_micros(us: i64) -> Time {
assert!(us >= 0);
Time(us)
}
pub fn as_micros(&self) -> i64 {
self.0
}
pub fn as_gst_clock_time(&self) -> gstreamer::ClockTime {
u64::try_from(self.as_micros()).unwrap() * gstreamer::USECOND
}
pub fn from_video_frame(frame: u32, fps: f64) -> Time {
Time::from_micros((frame as f64 / fps * 1e6) as i64)
}
pub fn as_video_frame(&self, fps: f64) -> u32 {
(self.0 as f64 * fps / 1e6) as u32
}
pub fn as_audio_idx(&self, sample_rate: u32) -> usize {
(self.0 as f64 / 1e6 * sample_rate as f64) as usize
}
}
impl Diff {
pub fn as_audio_idx(&self, sample_rate: u32) -> isize {
(self.0 as f64 / 1e6 * sample_rate as f64) as isize
}
pub fn from_audio_idx(idx: i64, sample_rate: u32) -> Diff {
Diff(((idx as f64) * 1e6 / sample_rate as f64) as i64)
}
pub const fn as_micros(self) -> i64 {
self.0
}
pub const fn from_micros(us: i64) -> Diff {
Diff(us)
}
}
impl std::ops::AddAssign<Diff> for Time {
fn add_assign(&mut self, rhs: Diff) {
*self = *self + rhs;
}
}
impl std::ops::Add<Diff> for Time {
type Output = Time;
fn add(self, rhs: Diff) -> Time {
Time(self.0.saturating_add(rhs.0).max(0))
}
}
impl std::ops::Add<Diff> for Diff {
type Output = Diff;
fn add(self, rhs: Diff) -> Diff {
Diff(self.0.saturating_add(rhs.0))
}
}
impl std::ops::Sub<Diff> for Diff {
type Output = Diff;
fn sub(self, rhs: Diff) -> Diff {
Diff(self.0.saturating_sub(rhs.0))
}
}
impl std::ops::SubAssign<Diff> for Time {
fn sub_assign(&mut self, rhs: Diff) {
*self = *self - rhs;
}
}
impl std::ops::Sub<Diff> for Time {
type Output = Time;
fn sub(self, rhs: Diff) -> Time {
Time(self.0.saturating_sub(rhs.0).max(0))
}
}
impl std::ops::Sub<Time> for Time {
type Output = Diff;
fn sub(self, rhs: Time) -> Diff {
Diff(self.0 - rhs.0)
}
}
impl TimeSpan {
pub fn new(start: Time, end: Time) -> TimeSpan {
assert!(start <= end);
TimeSpan { start, end }
}
pub fn start(&self) -> Time {
self.start
}
pub fn end(&self) -> Time {
self.end
}
pub fn interpolate_to(&self, time: Time, other: TimeSpan) -> Time {
assert!(self.start <= time && time <= self.end);
if self.start == self.end {
return other.end;
}
let ratio = (time - self.start).0 as f64 / (self.end - self.start).0 as f64;
Time(other.start.0 + ((other.end - other.start).0 as f64 * ratio) as i64)
}
}