gstreamer_video/
video_time_code_interval.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{cmp, fmt, mem, str};
4
5use crate::ffi;
6use glib::translate::*;
7
8glib::wrapper! {
9    /// A representation of a difference between two [`VideoTimeCode`][crate::VideoTimeCode] instances.
10    /// Will not necessarily correspond to a real timecode (e.g. 00:00:10;00)
11    #[doc(alias = "GstVideoTimeCodeInterval")]
12    pub struct VideoTimeCodeInterval(BoxedInline<ffi::GstVideoTimeCodeInterval>);
13
14    match fn {
15        type_ => || ffi::gst_video_time_code_interval_get_type(),
16    }
17}
18
19impl VideoTimeCodeInterval {
20    /// ## `hours`
21    /// the hours field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
22    /// ## `minutes`
23    /// the minutes field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
24    /// ## `seconds`
25    /// the seconds field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
26    /// ## `frames`
27    /// the frames field of [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval]
28    ///
29    /// # Returns
30    ///
31    /// a new [`VideoTimeCodeInterval`][crate::VideoTimeCodeInterval] with the given values.
32    pub fn new(hours: u32, minutes: u32, seconds: u32, frames: u32) -> Self {
33        assert_initialized_main_thread!();
34        unsafe {
35            let mut v = mem::MaybeUninit::uninit();
36            ffi::gst_video_time_code_interval_init(v.as_mut_ptr(), hours, minutes, seconds, frames);
37            Self {
38                inner: v.assume_init(),
39            }
40        }
41    }
42
43    #[doc(alias = "get_hours")]
44    pub fn hours(&self) -> u32 {
45        self.inner.hours
46    }
47
48    pub fn set_hours(&mut self, hours: u32) {
49        self.inner.hours = hours
50    }
51
52    #[doc(alias = "get_minutes")]
53    pub fn minutes(&self) -> u32 {
54        self.inner.minutes
55    }
56
57    pub fn set_minutes(&mut self, minutes: u32) {
58        assert!(minutes < 60);
59        self.inner.minutes = minutes
60    }
61
62    #[doc(alias = "get_seconds")]
63    pub fn seconds(&self) -> u32 {
64        self.inner.seconds
65    }
66
67    pub fn set_seconds(&mut self, seconds: u32) {
68        assert!(seconds < 60);
69        self.inner.seconds = seconds
70    }
71
72    #[doc(alias = "get_frames")]
73    pub fn frames(&self) -> u32 {
74        self.inner.frames
75    }
76
77    pub fn set_frames(&mut self, frames: u32) {
78        self.inner.frames = frames
79    }
80}
81
82unsafe impl Send for VideoTimeCodeInterval {}
83unsafe impl Sync for VideoTimeCodeInterval {}
84
85impl PartialEq for VideoTimeCodeInterval {
86    fn eq(&self, other: &Self) -> bool {
87        self.inner.hours == other.inner.hours
88            && self.inner.minutes == other.inner.minutes
89            && self.inner.seconds == other.inner.seconds
90            && self.inner.frames == other.inner.frames
91    }
92}
93
94impl Eq for VideoTimeCodeInterval {}
95
96impl PartialOrd for VideoTimeCodeInterval {
97    #[inline]
98    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
99        Some(self.cmp(other))
100    }
101}
102
103impl Ord for VideoTimeCodeInterval {
104    #[inline]
105    fn cmp(&self, other: &Self) -> cmp::Ordering {
106        self.inner
107            .hours
108            .cmp(&other.inner.hours)
109            .then_with(|| self.inner.minutes.cmp(&other.inner.minutes))
110            .then_with(|| self.inner.seconds.cmp(&other.inner.seconds))
111            .then_with(|| self.inner.frames.cmp(&other.inner.frames))
112    }
113}
114
115impl fmt::Debug for VideoTimeCodeInterval {
116    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117        f.debug_struct("VideoTimeCodeInterval")
118            .field("hours", &self.inner.hours)
119            .field("minutes", &self.inner.minutes)
120            .field("seconds", &self.inner.seconds)
121            .field("frames", &self.inner.frames)
122            .finish()
123    }
124}
125
126impl fmt::Display for VideoTimeCodeInterval {
127    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128        write!(
129            f,
130            "{:02}:{:02}:{:02}:{:02}",
131            self.inner.hours, self.inner.minutes, self.inner.seconds, self.inner.frames
132        )
133    }
134}
135
136impl str::FromStr for VideoTimeCodeInterval {
137    type Err = glib::error::BoolError;
138
139    #[doc(alias = "gst_video_time_code_interval_new_from_string")]
140    fn from_str(s: &str) -> Result<Self, Self::Err> {
141        assert_initialized_main_thread!();
142        unsafe {
143            Option::<Self>::from_glib_full(ffi::gst_video_time_code_interval_new_from_string(
144                s.to_glib_none().0,
145            ))
146            .ok_or_else(|| glib::bool_error!("Failed to create VideoTimeCodeInterval from string"))
147        }
148    }
149}