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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use glib;
use glib::translate::*;
use gst_sys;
use std::time::Duration;
use std::{cmp, convert, fmt};
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug, Default)]
pub struct ClockTime(pub Option<u64>);
impl ClockTime {
pub fn hours(&self) -> Option<u64> {
(*self / ::SECOND / 60 / 60).0
}
pub fn minutes(&self) -> Option<u64> {
(*self / ::SECOND / 60).0
}
pub fn seconds(&self) -> Option<u64> {
(*self / ::SECOND).0
}
pub fn mseconds(&self) -> Option<u64> {
(*self / ::MSECOND).0
}
pub fn useconds(&self) -> Option<u64> {
(*self / ::USECOND).0
}
pub fn nseconds(&self) -> Option<u64> {
(*self / ::NSECOND).0
}
pub fn nanoseconds(&self) -> Option<u64> {
self.0
}
pub fn from_seconds(seconds: u64) -> ClockTime {
skip_assert_initialized!();
seconds * ::SECOND
}
pub fn from_mseconds(mseconds: u64) -> ClockTime {
skip_assert_initialized!();
mseconds * ::MSECOND
}
pub fn from_useconds(useconds: u64) -> ClockTime {
skip_assert_initialized!();
useconds * ::USECOND
}
pub fn from_nseconds(nseconds: u64) -> ClockTime {
skip_assert_initialized!();
nseconds * ::NSECOND
}
pub fn none() -> ClockTime {
ClockTime(None)
}
}
impl fmt::Display for ClockTime {
#[allow(clippy::many_single_char_names)]
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let precision = f.precision().unwrap_or(9);
let (h, m, s, ns) = match self.0 {
Some(v) => {
let mut s = v / 1_000_000_000;
let mut m = s / 60;
let h = m / 60;
s %= 60;
m %= 60;
let ns = v % 1_000_000_000;
(h, m, s, ns)
}
None => (99, 99, 99, 999_999_999),
};
if precision == 0 {
f.write_fmt(format_args!("{:02}:{:02}:{:02}", h, m, s))
} else {
let mut divisor = 1;
let precision = cmp::min(precision, 9);
for _ in 0..(9 - precision) {
divisor *= 10;
}
f.write_fmt(format_args!(
"{:02}:{:02}:{:02}.{:0width$}",
h,
m,
s,
ns / divisor,
width = precision
))
}
}
}
#[doc(hidden)]
impl ToGlib for ClockTime {
type GlibType = gst_sys::GstClockTime;
fn to_glib(&self) -> gst_sys::GstClockTime {
match self.0 {
None => gst_sys::GST_CLOCK_TIME_NONE,
Some(v) => v,
}
}
}
#[doc(hidden)]
impl FromGlib<gst_sys::GstClockTime> for ClockTime {
fn from_glib(value: gst_sys::GstClockTime) -> Self {
skip_assert_initialized!();
match value {
gst_sys::GST_CLOCK_TIME_NONE => ClockTime(None),
value => ClockTime(Some(value)),
}
}
}
#[doc(hidden)]
impl<'a> glib::value::FromValueOptional<'a> for ClockTime {
unsafe fn from_value_optional(value: &'a glib::Value) -> Option<Self> {
<u64 as glib::value::FromValueOptional>::from_value_optional(value)
.map(ClockTime::from_glib)
}
}
#[doc(hidden)]
impl<'a> glib::value::FromValue<'a> for ClockTime {
unsafe fn from_value(value: &'a glib::Value) -> Self {
ClockTime::from_glib(<u64 as glib::value::FromValue>::from_value(value))
}
}
#[doc(hidden)]
impl glib::value::SetValue for ClockTime {
unsafe fn set_value(value: &mut glib::Value, this: &Self) {
<u64 as glib::value::SetValue>::set_value(value, &this.to_glib());
}
}
#[doc(hidden)]
impl glib::StaticType for ClockTime {
fn static_type() -> glib::Type {
<u64 as glib::StaticType>::static_type()
}
}
impl From<Duration> for ClockTime {
fn from(d: Duration) -> Self {
skip_assert_initialized!();
let nanos = d.as_nanos();
if nanos > std::u64::MAX as u128 {
::CLOCK_TIME_NONE
} else {
ClockTime::from_nseconds(nanos as u64)
}
}
}
impl convert::TryFrom<ClockTime> for Duration {
type Error = glib::BoolError;
fn try_from(t: ClockTime) -> Result<Self, Self::Error> {
skip_assert_initialized!();
t.nanoseconds()
.map(Duration::from_nanos)
.ok_or_else(|| glib_bool_error!("Can't convert ClockTime::NONE to Duration"))
}
}