2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2017-08-13 22:40:43 +00:00
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use std::{
|
|
|
|
cmp,
|
|
|
|
marker::Unpin,
|
|
|
|
pin::Pin,
|
|
|
|
ptr,
|
|
|
|
sync::{atomic, atomic::AtomicI32},
|
|
|
|
};
|
2017-08-13 22:40:43 +00:00
|
|
|
|
2020-10-10 09:10:52 +00:00
|
|
|
use futures_core::{Future, Stream};
|
2023-01-03 18:58:25 +00:00
|
|
|
use glib::{
|
|
|
|
ffi::{gboolean, gpointer},
|
|
|
|
prelude::*,
|
|
|
|
translate::*,
|
|
|
|
};
|
|
|
|
use libc::c_void;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
Clock, ClockEntryType, ClockError, ClockFlags, ClockReturn, ClockSuccess, ClockTime,
|
|
|
|
ClockTimeDiff,
|
|
|
|
};
|
2019-09-25 14:17:39 +00:00
|
|
|
|
2020-12-17 22:38:06 +00:00
|
|
|
glib::wrapper! {
|
2019-02-21 16:48:57 +00:00
|
|
|
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
2017-08-13 22:40:43 +00:00
|
|
|
pub struct ClockId(Shared<c_void>);
|
|
|
|
|
|
|
|
match fn {
|
2020-11-21 13:46:48 +00:00
|
|
|
ref => |ptr| ffi::gst_clock_id_ref(ptr),
|
|
|
|
unref => |ptr| ffi::gst_clock_id_unref(ptr),
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClockId {
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_time")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_id_get_time")]
|
2021-12-12 15:52:32 +00:00
|
|
|
#[doc(alias = "GST_CLOCK_ENTRY_TIME")]
|
2021-04-28 22:29:13 +00:00
|
|
|
pub fn time(&self) -> ClockTime {
|
|
|
|
unsafe {
|
|
|
|
try_from_glib(ffi::gst_clock_id_get_time(self.to_glib_none().0))
|
|
|
|
.expect("undefined time")
|
|
|
|
}
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_id_unschedule")]
|
2017-08-13 22:40:43 +00:00
|
|
|
pub fn unschedule(&self) {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { ffi::gst_clock_id_unschedule(self.to_glib_none().0) }
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_id_wait")]
|
2019-01-08 16:13:37 +00:00
|
|
|
pub fn wait(&self) -> (Result<ClockSuccess, ClockError>, ClockTimeDiff) {
|
2017-08-13 22:40:43 +00:00
|
|
|
unsafe {
|
2019-07-11 12:34:28 +00:00
|
|
|
let mut jitter = 0;
|
2021-05-13 12:54:41 +00:00
|
|
|
let res = try_from_glib(ffi::gst_clock_id_wait(self.to_glib_none().0, &mut jitter));
|
2021-04-28 12:34:56 +00:00
|
|
|
(res, jitter)
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 15:52:32 +00:00
|
|
|
#[doc(alias = "gst_clock_id_compare_func")]
|
2020-10-10 09:10:52 +00:00
|
|
|
pub fn compare_by_time(&self, other: &Self) -> cmp::Ordering {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let res = ffi::gst_clock_id_compare_func(self.to_glib_none().0, other.to_glib_none().0);
|
2020-10-10 09:10:52 +00:00
|
|
|
res.cmp(&0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:37:49 +00:00
|
|
|
#[cfg(any(feature = "v1_16", feature = "dox"))]
|
|
|
|
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_clock")]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_id_get_clock")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn clock(&self) -> Option<Clock> {
|
2020-11-21 13:46:48 +00:00
|
|
|
unsafe { from_glib_full(ffi::gst_clock_id_get_clock(self.to_glib_none().0)) }
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 13:37:49 +00:00
|
|
|
#[cfg(any(feature = "v1_16", feature = "dox"))]
|
|
|
|
#[cfg_attr(feature = "dox", doc(cfg(feature = "v1_16")))]
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_id_uses_clock")]
|
2020-10-10 09:10:52 +00:00
|
|
|
pub fn uses_clock<P: IsA<Clock>>(&self, clock: &P) -> bool {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
from_glib(ffi::gst_clock_id_uses_clock(
|
2020-10-10 09:10:52 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
clock.as_ref().as_ptr(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_type")]
|
2021-12-12 15:52:32 +00:00
|
|
|
#[doc(alias = "GST_CLOCK_ENTRY_TYPE")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn type_(&self) -> ClockEntryType {
|
2020-10-10 09:10:52 +00:00
|
|
|
unsafe {
|
2022-10-08 12:35:37 +00:00
|
|
|
let ptr = self.as_ptr() as *mut ffi::GstClockEntry;
|
2020-10-10 09:10:52 +00:00
|
|
|
from_glib((*ptr).type_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_status")]
|
2021-12-12 15:52:32 +00:00
|
|
|
#[doc(alias = "GST_CLOCK_ENTRY_STATUS")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn status(&self) -> &AtomicClockReturn {
|
2020-10-10 09:10:52 +00:00
|
|
|
unsafe {
|
2022-10-08 12:35:37 +00:00
|
|
|
let ptr = self.as_ptr() as *mut ffi::GstClockEntry;
|
2020-10-10 09:10:52 +00:00
|
|
|
&*((&(*ptr).status) as *const i32 as *const AtomicClockReturn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
|
|
|
pub struct SingleShotClockId(ClockId);
|
|
|
|
|
|
|
|
impl std::ops::Deref for SingleShotClockId {
|
|
|
|
type Target = ClockId;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<SingleShotClockId> for ClockId {
|
|
|
|
fn from(id: SingleShotClockId) -> ClockId {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
id.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 16:00:02 +00:00
|
|
|
impl TryFrom<ClockId> for SingleShotClockId {
|
2020-10-10 09:10:52 +00:00
|
|
|
type Error = glib::BoolError;
|
|
|
|
|
|
|
|
fn try_from(id: ClockId) -> Result<SingleShotClockId, glib::BoolError> {
|
|
|
|
skip_assert_initialized!();
|
2021-04-11 19:39:50 +00:00
|
|
|
match id.type_() {
|
2020-10-10 09:10:52 +00:00
|
|
|
ClockEntryType::Single => Ok(SingleShotClockId(id)),
|
2020-12-17 22:38:06 +00:00
|
|
|
_ => Err(glib::bool_error!("Not a single-shot clock id")),
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SingleShotClockId {
|
2021-12-12 15:52:32 +00:00
|
|
|
#[doc(alias = "gst_clock_id_compare_func")]
|
2020-10-10 09:10:52 +00:00
|
|
|
pub fn compare_by_time(&self, other: &Self) -> cmp::Ordering {
|
|
|
|
self.0.compare_by_time(&other.0)
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_id_wait_async")]
|
2019-01-08 16:13:37 +00:00
|
|
|
pub fn wait_async<F>(&self, func: F) -> Result<ClockSuccess, ClockError>
|
2017-08-13 22:40:43 +00:00
|
|
|
where
|
2020-10-27 17:27:16 +00:00
|
|
|
F: FnOnce(&Clock, Option<ClockTime>, &ClockId) + Send + 'static,
|
2017-08-13 22:40:43 +00:00
|
|
|
{
|
2020-10-27 17:27:16 +00:00
|
|
|
unsafe extern "C" fn trampoline<
|
|
|
|
F: FnOnce(&Clock, Option<ClockTime>, &ClockId) + Send + 'static,
|
|
|
|
>(
|
2020-11-21 13:46:48 +00:00
|
|
|
clock: *mut ffi::GstClock,
|
|
|
|
time: ffi::GstClockTime,
|
2020-10-10 09:10:52 +00:00
|
|
|
id: gpointer,
|
|
|
|
func: gpointer,
|
|
|
|
) -> gboolean {
|
|
|
|
let f: &mut Option<F> = &mut *(func as *mut Option<F>);
|
|
|
|
let f = f.take().unwrap();
|
|
|
|
|
|
|
|
f(
|
|
|
|
&from_glib_borrow(clock),
|
|
|
|
from_glib(time),
|
|
|
|
&from_glib_borrow(id),
|
|
|
|
);
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GTRUE
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn destroy_notify<
|
2020-10-27 17:27:16 +00:00
|
|
|
F: FnOnce(&Clock, Option<ClockTime>, &ClockId) + Send + 'static,
|
2020-10-10 09:10:52 +00:00
|
|
|
>(
|
|
|
|
ptr: gpointer,
|
|
|
|
) {
|
2022-08-10 09:27:00 +00:00
|
|
|
let _ = Box::<Option<F>>::from_raw(ptr as *mut _);
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let func: Box<Option<F>> = Box::new(Some(func));
|
|
|
|
|
2021-04-28 12:34:56 +00:00
|
|
|
unsafe {
|
2021-05-13 12:54:41 +00:00
|
|
|
try_from_glib(ffi::gst_clock_id_wait_async(
|
2017-08-13 22:40:43 +00:00
|
|
|
self.to_glib_none().0,
|
2020-10-10 09:10:52 +00:00
|
|
|
Some(trampoline::<F>),
|
|
|
|
Box::into_raw(func) as gpointer,
|
|
|
|
Some(destroy_notify::<F>),
|
2017-08-13 22:40:43 +00:00
|
|
|
))
|
2021-04-28 12:34:56 +00:00
|
|
|
}
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 07:13:52 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2020-10-10 09:10:52 +00:00
|
|
|
pub fn wait_async_future(
|
2020-10-01 07:13:52 +00:00
|
|
|
&self,
|
|
|
|
) -> Result<
|
2020-10-27 17:27:16 +00:00
|
|
|
Pin<
|
|
|
|
Box<
|
|
|
|
dyn Future<Output = Result<(Option<ClockTime>, ClockId), ClockError>>
|
|
|
|
+ Send
|
|
|
|
+ 'static,
|
|
|
|
>,
|
|
|
|
>,
|
2020-10-01 07:13:52 +00:00
|
|
|
ClockError,
|
|
|
|
> {
|
2020-10-10 09:10:52 +00:00
|
|
|
use futures_channel::oneshot;
|
2020-10-01 07:13:52 +00:00
|
|
|
|
2020-10-10 09:10:52 +00:00
|
|
|
let (sender, receiver) = oneshot::channel();
|
2020-10-01 07:13:52 +00:00
|
|
|
|
|
|
|
self.wait_async(move |_clock, jitter, id| {
|
2020-10-10 09:10:52 +00:00
|
|
|
if sender.send((jitter, id.clone())).is_err() {
|
2020-10-01 08:25:40 +00:00
|
|
|
// Unschedule any future calls if the receiver end is disconnected
|
|
|
|
id.unschedule();
|
|
|
|
}
|
2020-10-01 07:13:52 +00:00
|
|
|
})?;
|
|
|
|
|
2021-01-14 13:13:00 +00:00
|
|
|
Ok(Box::pin(async move {
|
|
|
|
receiver.await.map_err(|_| ClockError::Unscheduled)
|
|
|
|
}))
|
2020-10-01 07:13:52 +00:00
|
|
|
}
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
2020-10-01 07:13:52 +00:00
|
|
|
|
2020-10-10 09:10:52 +00:00
|
|
|
#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash)]
|
|
|
|
pub struct PeriodicClockId(ClockId);
|
|
|
|
|
|
|
|
impl std::ops::Deref for PeriodicClockId {
|
|
|
|
type Target = ClockId;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
2019-04-23 15:47:13 +00:00
|
|
|
|
2020-10-10 09:10:52 +00:00
|
|
|
impl From<PeriodicClockId> for ClockId {
|
|
|
|
fn from(id: PeriodicClockId) -> ClockId {
|
|
|
|
skip_assert_initialized!();
|
|
|
|
id.0
|
2019-04-23 15:47:13 +00:00
|
|
|
}
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
2019-04-23 15:47:13 +00:00
|
|
|
|
2021-10-31 16:00:02 +00:00
|
|
|
impl TryFrom<ClockId> for PeriodicClockId {
|
2020-10-10 09:10:52 +00:00
|
|
|
type Error = glib::BoolError;
|
|
|
|
|
|
|
|
fn try_from(id: ClockId) -> Result<PeriodicClockId, glib::BoolError> {
|
|
|
|
skip_assert_initialized!();
|
2021-04-11 19:39:50 +00:00
|
|
|
match id.type_() {
|
2020-10-10 09:10:52 +00:00
|
|
|
ClockEntryType::Periodic => Ok(PeriodicClockId(id)),
|
2020-12-17 22:38:06 +00:00
|
|
|
_ => Err(glib::bool_error!("Not a periodic clock id")),
|
2019-04-23 15:47:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-10 09:10:52 +00:00
|
|
|
}
|
2019-09-25 14:17:39 +00:00
|
|
|
|
2020-10-10 09:10:52 +00:00
|
|
|
impl PeriodicClockId {
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_interval")]
|
2021-12-12 15:52:32 +00:00
|
|
|
#[doc(alias = "GST_CLOCK_ENTRY_INTERVAL")]
|
2021-04-11 19:39:50 +00:00
|
|
|
pub fn interval(&self) -> ClockTime {
|
2019-09-25 14:17:39 +00:00
|
|
|
unsafe {
|
2022-10-08 12:35:37 +00:00
|
|
|
let ptr = self.as_ptr() as *mut ffi::GstClockEntry;
|
2021-04-28 22:29:13 +00:00
|
|
|
try_from_glib((*ptr).interval).expect("undefined interval")
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-12 15:52:32 +00:00
|
|
|
#[doc(alias = "gst_clock_id_compare_func")]
|
2020-10-10 09:10:52 +00:00
|
|
|
pub fn compare_by_time(&self, other: &Self) -> cmp::Ordering {
|
|
|
|
self.0.compare_by_time(&other.0)
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_id_wait_async")]
|
2020-10-10 09:10:52 +00:00
|
|
|
pub fn wait_async<F>(&self, func: F) -> Result<ClockSuccess, ClockError>
|
|
|
|
where
|
2020-10-27 17:27:16 +00:00
|
|
|
F: Fn(&Clock, Option<ClockTime>, &ClockId) + Send + 'static,
|
2020-10-10 09:10:52 +00:00
|
|
|
{
|
2020-10-27 17:27:16 +00:00
|
|
|
unsafe extern "C" fn trampoline<
|
|
|
|
F: Fn(&Clock, Option<ClockTime>, &ClockId) + Send + 'static,
|
|
|
|
>(
|
2020-11-21 13:46:48 +00:00
|
|
|
clock: *mut ffi::GstClock,
|
|
|
|
time: ffi::GstClockTime,
|
2020-10-10 09:10:52 +00:00
|
|
|
id: gpointer,
|
|
|
|
func: gpointer,
|
|
|
|
) -> gboolean {
|
|
|
|
let f: &F = &*(func as *const F);
|
|
|
|
f(
|
|
|
|
&from_glib_borrow(clock),
|
|
|
|
from_glib(time),
|
|
|
|
&from_glib_borrow(id),
|
|
|
|
);
|
2020-11-21 13:46:48 +00:00
|
|
|
glib::ffi::GTRUE
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 17:27:16 +00:00
|
|
|
unsafe extern "C" fn destroy_notify<
|
|
|
|
F: Fn(&Clock, Option<ClockTime>, &ClockId) + Send + 'static,
|
|
|
|
>(
|
2020-10-10 09:10:52 +00:00
|
|
|
ptr: gpointer,
|
|
|
|
) {
|
2022-08-10 09:27:00 +00:00
|
|
|
let _ = Box::<F>::from_raw(ptr as *mut _);
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
2020-10-10 09:10:52 +00:00
|
|
|
|
|
|
|
let func: Box<F> = Box::new(func);
|
2021-04-28 12:34:56 +00:00
|
|
|
unsafe {
|
2021-05-13 12:54:41 +00:00
|
|
|
try_from_glib(ffi::gst_clock_id_wait_async(
|
2020-10-10 09:10:52 +00:00
|
|
|
self.to_glib_none().0,
|
|
|
|
Some(trampoline::<F>),
|
|
|
|
Box::into_raw(func) as gpointer,
|
|
|
|
Some(destroy_notify::<F>),
|
|
|
|
))
|
2021-04-28 12:34:56 +00:00
|
|
|
}
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 09:10:52 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
pub fn wait_async_stream(
|
|
|
|
&self,
|
|
|
|
) -> Result<
|
2020-10-27 17:27:16 +00:00
|
|
|
Pin<Box<dyn Stream<Item = (Option<ClockTime>, ClockId)> + Unpin + Send + 'static>>,
|
2020-10-10 09:10:52 +00:00
|
|
|
ClockError,
|
|
|
|
> {
|
|
|
|
use futures_channel::mpsc;
|
|
|
|
|
|
|
|
let (sender, receiver) = mpsc::unbounded();
|
|
|
|
|
|
|
|
self.wait_async(move |_clock, jitter, id| {
|
|
|
|
if sender.unbounded_send((jitter, id.clone())).is_err() {
|
|
|
|
// Unschedule any future calls if the receiver end is disconnected
|
|
|
|
id.unschedule();
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(Box::pin(receiver))
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:06:59 +00:00
|
|
|
#[repr(transparent)]
|
2019-09-25 14:17:39 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AtomicClockReturn(AtomicI32);
|
|
|
|
|
|
|
|
impl AtomicClockReturn {
|
|
|
|
pub fn load(&self) -> ClockReturn {
|
2020-12-08 14:07:12 +00:00
|
|
|
unsafe { from_glib(self.0.load(atomic::Ordering::SeqCst)) }
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store(&self, val: ClockReturn) {
|
2021-04-27 15:15:46 +00:00
|
|
|
self.0.store(val.into_glib(), atomic::Ordering::SeqCst)
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn swap(&self, val: ClockReturn) -> ClockReturn {
|
2021-04-27 15:15:46 +00:00
|
|
|
unsafe { from_glib(self.0.swap(val.into_glib(), atomic::Ordering::SeqCst)) }
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:13:26 +00:00
|
|
|
pub fn compare_exchange(
|
|
|
|
&self,
|
|
|
|
current: ClockReturn,
|
|
|
|
new: ClockReturn,
|
|
|
|
) -> Result<ClockReturn, ClockReturn> {
|
2020-12-08 14:07:12 +00:00
|
|
|
unsafe {
|
2021-02-09 14:13:26 +00:00
|
|
|
self.0
|
|
|
|
.compare_exchange(
|
2021-04-27 15:15:46 +00:00
|
|
|
current.into_glib(),
|
|
|
|
new.into_glib(),
|
2021-02-09 14:13:26 +00:00
|
|
|
atomic::Ordering::SeqCst,
|
|
|
|
atomic::Ordering::SeqCst,
|
|
|
|
)
|
|
|
|
.map(|v| from_glib(v))
|
|
|
|
.map_err(|v| from_glib(v))
|
2020-12-08 14:07:12 +00:00
|
|
|
}
|
2019-09-25 14:17:39 +00:00
|
|
|
}
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for ClockId {}
|
|
|
|
unsafe impl Sync for ClockId {}
|
|
|
|
|
2017-12-18 07:39:37 +00:00
|
|
|
impl Clock {
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_adjust_with_calibration")]
|
2017-12-18 07:39:37 +00:00
|
|
|
pub fn adjust_with_calibration(
|
|
|
|
internal_target: ClockTime,
|
|
|
|
cinternal: ClockTime,
|
|
|
|
cexternal: ClockTime,
|
|
|
|
cnum: ClockTime,
|
|
|
|
cdenom: ClockTime,
|
2021-04-28 22:29:13 +00:00
|
|
|
) -> ClockTime {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2017-12-18 07:39:37 +00:00
|
|
|
unsafe {
|
2021-04-28 22:29:13 +00:00
|
|
|
try_from_glib(ffi::gst_clock_adjust_with_calibration(
|
2017-12-18 07:39:37 +00:00
|
|
|
ptr::null_mut(),
|
2021-04-27 15:15:46 +00:00
|
|
|
internal_target.into_glib(),
|
|
|
|
cinternal.into_glib(),
|
|
|
|
cexternal.into_glib(),
|
|
|
|
cnum.into_glib(),
|
|
|
|
cdenom.into_glib(),
|
2017-12-18 07:39:37 +00:00
|
|
|
))
|
2021-04-28 22:29:13 +00:00
|
|
|
.expect("undefined ClockTime")
|
2017-12-18 07:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_unadjust_with_calibration")]
|
2017-12-18 07:39:37 +00:00
|
|
|
pub fn unadjust_with_calibration(
|
|
|
|
external_target: ClockTime,
|
|
|
|
cinternal: ClockTime,
|
|
|
|
cexternal: ClockTime,
|
|
|
|
cnum: ClockTime,
|
|
|
|
cdenom: ClockTime,
|
2021-04-28 22:29:13 +00:00
|
|
|
) -> ClockTime {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2017-12-18 07:39:37 +00:00
|
|
|
unsafe {
|
2021-04-28 22:29:13 +00:00
|
|
|
try_from_glib(ffi::gst_clock_unadjust_with_calibration(
|
2017-12-18 07:39:37 +00:00
|
|
|
ptr::null_mut(),
|
2021-04-27 15:15:46 +00:00
|
|
|
external_target.into_glib(),
|
|
|
|
cinternal.into_glib(),
|
|
|
|
cexternal.into_glib(),
|
|
|
|
cnum.into_glib(),
|
|
|
|
cdenom.into_glib(),
|
2017-12-18 07:39:37 +00:00
|
|
|
))
|
2021-04-28 22:29:13 +00:00
|
|
|
.expect("undefined ClockTime")
|
2017-12-18 07:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 09:22:42 +00:00
|
|
|
pub trait ClockExtManual: 'static {
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_new_periodic_id")]
|
2020-10-17 17:25:54 +00:00
|
|
|
fn new_periodic_id(&self, start_time: ClockTime, interval: ClockTime) -> PeriodicClockId;
|
2017-08-13 22:40:43 +00:00
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_periodic_id_reinit")]
|
2017-08-13 22:40:43 +00:00
|
|
|
fn periodic_id_reinit(
|
|
|
|
&self,
|
2020-10-10 09:10:52 +00:00
|
|
|
id: &PeriodicClockId,
|
2017-08-13 22:40:43 +00:00
|
|
|
start_time: ClockTime,
|
|
|
|
interval: ClockTime,
|
|
|
|
) -> Result<(), glib::BoolError>;
|
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_new_single_shot_id")]
|
2020-10-17 17:25:54 +00:00
|
|
|
fn new_single_shot_id(&self, time: ClockTime) -> SingleShotClockId;
|
2017-08-13 22:40:43 +00:00
|
|
|
|
2021-05-19 20:35:47 +00:00
|
|
|
#[doc(alias = "gst_clock_single_shot_id_reinit")]
|
2020-10-10 09:10:52 +00:00
|
|
|
fn single_shot_id_reinit(
|
|
|
|
&self,
|
|
|
|
id: &SingleShotClockId,
|
|
|
|
time: ClockTime,
|
|
|
|
) -> Result<(), glib::BoolError>;
|
2019-09-25 13:46:46 +00:00
|
|
|
|
|
|
|
fn set_clock_flags(&self, flags: ClockFlags);
|
|
|
|
|
|
|
|
fn unset_clock_flags(&self, flags: ClockFlags);
|
|
|
|
|
2021-05-02 09:41:18 +00:00
|
|
|
#[doc(alias = "get_clock_flags")]
|
2021-04-11 19:39:50 +00:00
|
|
|
fn clock_flags(&self) -> ClockFlags;
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 09:22:42 +00:00
|
|
|
impl<O: IsA<Clock>> ClockExtManual for O {
|
2020-10-17 17:25:54 +00:00
|
|
|
fn new_periodic_id(&self, start_time: ClockTime, interval: ClockTime) -> PeriodicClockId {
|
2020-10-27 17:27:16 +00:00
|
|
|
assert_ne!(interval, ClockTime::ZERO);
|
2020-10-17 17:25:54 +00:00
|
|
|
|
2017-08-13 22:40:43 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
PeriodicClockId(from_glib_full(ffi::gst_clock_new_periodic_id(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2021-04-27 15:15:46 +00:00
|
|
|
start_time.into_glib(),
|
|
|
|
interval.into_glib(),
|
2020-10-17 17:25:54 +00:00
|
|
|
)))
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn periodic_id_reinit(
|
|
|
|
&self,
|
2020-10-10 09:10:52 +00:00
|
|
|
id: &PeriodicClockId,
|
2017-08-13 22:40:43 +00:00
|
|
|
start_time: ClockTime,
|
|
|
|
interval: ClockTime,
|
|
|
|
) -> Result<(), glib::BoolError> {
|
2017-08-30 11:39:09 +00:00
|
|
|
skip_assert_initialized!();
|
2017-08-13 22:40:43 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let res: bool = from_glib(ffi::gst_clock_periodic_id_reinit(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2017-08-13 22:40:43 +00:00
|
|
|
id.to_glib_none().0,
|
2021-04-27 15:15:46 +00:00
|
|
|
start_time.into_glib(),
|
|
|
|
interval.into_glib(),
|
2017-08-13 22:40:43 +00:00
|
|
|
));
|
|
|
|
if res {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2020-12-17 22:38:06 +00:00
|
|
|
Err(glib::bool_error!("Failed to reinit periodic clock id"))
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 17:25:54 +00:00
|
|
|
fn new_single_shot_id(&self, time: ClockTime) -> SingleShotClockId {
|
2017-08-13 22:40:43 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
SingleShotClockId(from_glib_full(ffi::gst_clock_new_single_shot_id(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2021-04-27 15:15:46 +00:00
|
|
|
time.into_glib(),
|
2020-10-17 17:25:54 +00:00
|
|
|
)))
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 09:10:52 +00:00
|
|
|
fn single_shot_id_reinit(
|
|
|
|
&self,
|
|
|
|
id: &SingleShotClockId,
|
|
|
|
time: ClockTime,
|
|
|
|
) -> Result<(), glib::BoolError> {
|
2017-08-13 22:40:43 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let res: bool = from_glib(ffi::gst_clock_single_shot_id_reinit(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2017-08-13 22:40:43 +00:00
|
|
|
id.to_glib_none().0,
|
2021-04-27 15:15:46 +00:00
|
|
|
time.into_glib(),
|
2017-08-13 22:40:43 +00:00
|
|
|
));
|
|
|
|
if res {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2020-12-17 22:38:06 +00:00
|
|
|
Err(glib::bool_error!("Failed to reinit single shot clock id"))
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 13:46:46 +00:00
|
|
|
|
|
|
|
fn set_clock_flags(&self, flags: ClockFlags) {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
|
2021-04-27 15:15:46 +00:00
|
|
|
(*ptr).flags |= flags.into_glib();
|
2019-09-25 13:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unset_clock_flags(&self, flags: ClockFlags) {
|
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
|
2021-04-27 15:15:46 +00:00
|
|
|
(*ptr).flags &= !flags.into_glib();
|
2019-09-25 13:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-11 19:39:50 +00:00
|
|
|
fn clock_flags(&self) -> ClockFlags {
|
2019-09-25 13:46:46 +00:00
|
|
|
unsafe {
|
2020-11-21 13:46:48 +00:00
|
|
|
let ptr: *mut ffi::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = crate::utils::MutexGuard::lock(&(*ptr).lock);
|
2019-09-25 13:46:46 +00:00
|
|
|
from_glib((*ptr).flags)
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
|
2023-01-03 18:58:25 +00:00
|
|
|
use super::{
|
|
|
|
super::{prelude::*, *},
|
|
|
|
*,
|
|
|
|
};
|
|
|
|
|
2017-08-13 22:40:43 +00:00
|
|
|
#[test]
|
|
|
|
fn test_wait() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2017-08-13 22:40:43 +00:00
|
|
|
|
|
|
|
let clock = SystemClock::obtain();
|
2020-10-27 17:27:16 +00:00
|
|
|
let now = clock.time().unwrap();
|
|
|
|
let id = clock.new_single_shot_id(now + 20 * ClockTime::MSECOND);
|
2017-08-13 22:40:43 +00:00
|
|
|
let (res, _) = id.wait();
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
assert!(res == Ok(ClockSuccess::Ok) || res == Err(ClockError::Early));
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wait_async() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2017-08-13 22:40:43 +00:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
|
|
|
|
let clock = SystemClock::obtain();
|
2020-10-27 17:27:16 +00:00
|
|
|
let now = clock.time().unwrap();
|
|
|
|
let id = clock.new_single_shot_id(now + 20 * ClockTime::MSECOND);
|
2017-08-13 22:40:43 +00:00
|
|
|
let res = id.wait_async(move |_, _, _| {
|
|
|
|
sender.send(()).unwrap();
|
|
|
|
});
|
|
|
|
|
2019-01-08 16:13:37 +00:00
|
|
|
assert!(res == Ok(ClockSuccess::Ok));
|
2017-08-13 22:40:43 +00:00
|
|
|
|
|
|
|
assert_eq!(receiver.recv(), Ok(()));
|
|
|
|
}
|
2020-10-10 09:10:52 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wait_periodic() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2020-10-10 09:10:52 +00:00
|
|
|
|
|
|
|
let clock = SystemClock::obtain();
|
2020-10-27 17:27:16 +00:00
|
|
|
let now = clock.time().unwrap();
|
|
|
|
let id = clock.new_periodic_id(now + 20 * ClockTime::MSECOND, 20 * ClockTime::MSECOND);
|
2020-10-10 09:10:52 +00:00
|
|
|
|
|
|
|
let (res, _) = id.wait();
|
|
|
|
assert!(res == Ok(ClockSuccess::Ok) || res == Err(ClockError::Early));
|
|
|
|
|
|
|
|
let (res, _) = id.wait();
|
|
|
|
assert!(res == Ok(ClockSuccess::Ok) || res == Err(ClockError::Early));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wait_async_periodic() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2020-10-10 09:10:52 +00:00
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
|
|
|
|
let clock = SystemClock::obtain();
|
2020-10-27 17:27:16 +00:00
|
|
|
let now = clock.time().unwrap();
|
|
|
|
let id = clock.new_periodic_id(now + 20 * ClockTime::MSECOND, 20 * ClockTime::MSECOND);
|
2020-10-10 09:10:52 +00:00
|
|
|
let res = id.wait_async(move |_, _, _| {
|
|
|
|
let _ = sender.send(());
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(res == Ok(ClockSuccess::Ok));
|
|
|
|
|
|
|
|
assert_eq!(receiver.recv(), Ok(()));
|
|
|
|
assert_eq!(receiver.recv(), Ok(()));
|
|
|
|
}
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|