2017-08-13 22:40:43 +00:00
|
|
|
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use glib;
|
2019-04-23 15:47:13 +00:00
|
|
|
#[cfg(any(feature = "v1_16", feature = "dox"))]
|
|
|
|
use glib::prelude::*;
|
2018-04-01 08:30:03 +00:00
|
|
|
use glib::translate::*;
|
2018-04-25 08:10:06 +00:00
|
|
|
use glib::IsA;
|
2019-03-19 07:58:20 +00:00
|
|
|
use glib_sys::{gboolean, gpointer};
|
|
|
|
use gst_sys;
|
2017-08-13 22:40:43 +00:00
|
|
|
use libc::c_void;
|
2018-04-01 08:30:03 +00:00
|
|
|
use std::cmp;
|
|
|
|
use std::ptr;
|
2018-04-25 08:10:06 +00:00
|
|
|
use Clock;
|
2019-09-25 14:17:39 +00:00
|
|
|
use ClockEntryType;
|
2019-01-08 16:13:37 +00:00
|
|
|
use ClockError;
|
2019-09-25 13:46:46 +00:00
|
|
|
use ClockFlags;
|
2018-04-25 08:10:06 +00:00
|
|
|
use ClockReturn;
|
2019-01-08 16:13:37 +00:00
|
|
|
use ClockSuccess;
|
2018-04-25 08:10:06 +00:00
|
|
|
use ClockTime;
|
|
|
|
use ClockTimeDiff;
|
2017-08-13 22:40:43 +00:00
|
|
|
|
2019-09-25 14:17:39 +00:00
|
|
|
use std::sync::atomic;
|
|
|
|
use std::sync::atomic::AtomicI32;
|
|
|
|
|
2017-08-13 22:40:43 +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 {
|
2019-03-19 07:58:20 +00:00
|
|
|
ref => |ptr| gst_sys::gst_clock_id_ref(ptr),
|
|
|
|
unref => |ptr| gst_sys::gst_clock_id_unref(ptr),
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
unsafe extern "C" fn trampoline_wait_async<F: Fn(&Clock, ClockTime, &ClockId) + Send + 'static>(
|
2019-03-19 07:58:20 +00:00
|
|
|
clock: *mut gst_sys::GstClock,
|
|
|
|
time: gst_sys::GstClockTime,
|
2017-08-13 22:40:43 +00:00
|
|
|
id: gpointer,
|
|
|
|
func: gpointer,
|
|
|
|
) -> gboolean {
|
2019-02-21 17:30:36 +00:00
|
|
|
let f: &F = &*(func as *const F);
|
2017-11-11 10:21:55 +00:00
|
|
|
f(
|
|
|
|
&from_glib_borrow(clock),
|
|
|
|
from_glib(time),
|
|
|
|
&from_glib_borrow(id),
|
2019-01-17 22:10:58 +00:00
|
|
|
);
|
2019-03-19 07:58:20 +00:00
|
|
|
glib_sys::GTRUE
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 13:02:03 +00:00
|
|
|
unsafe extern "C" fn destroy_closure_wait_async<
|
|
|
|
F: Fn(&Clock, ClockTime, &ClockId) + Send + 'static,
|
|
|
|
>(
|
|
|
|
ptr: gpointer,
|
|
|
|
) {
|
|
|
|
Box::<F>::from_raw(ptr as *mut _);
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 22:10:58 +00:00
|
|
|
fn into_raw_wait_async<F: Fn(&Clock, ClockTime, &ClockId) + Send + 'static>(func: F) -> gpointer {
|
2019-02-28 08:32:13 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-01-30 13:02:03 +00:00
|
|
|
let func: Box<F> = Box::new(func);
|
2017-08-13 22:40:43 +00:00
|
|
|
Box::into_raw(func) as gpointer
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ClockId {
|
|
|
|
pub fn get_time(&self) -> ClockTime {
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { from_glib(gst_sys::gst_clock_id_get_time(self.to_glib_none().0)) }
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unschedule(&self) {
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { gst_sys::gst_clock_id_unschedule(self.to_glib_none().0) }
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2019-03-19 07:58:20 +00:00
|
|
|
let res: ClockReturn = from_glib(gst_sys::gst_clock_id_wait(
|
|
|
|
self.to_glib_none().0,
|
|
|
|
&mut jitter,
|
|
|
|
));
|
2019-01-08 16:13:37 +00:00
|
|
|
(res.into_result(), jitter)
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2019-01-17 22:10:58 +00:00
|
|
|
F: Fn(&Clock, ClockTime, &ClockId) + Send + 'static,
|
2017-08-13 22:40:43 +00:00
|
|
|
{
|
2019-01-08 16:13:37 +00:00
|
|
|
let ret: ClockReturn = unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(gst_sys::gst_clock_id_wait_async(
|
2017-08-13 22:40:43 +00:00
|
|
|
self.to_glib_none().0,
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(trampoline_wait_async::<F>),
|
2017-08-13 22:40:43 +00:00
|
|
|
into_raw_wait_async(func),
|
2019-01-30 13:02:03 +00:00
|
|
|
Some(destroy_closure_wait_async::<F>),
|
2017-08-13 22:40:43 +00:00
|
|
|
))
|
2019-01-08 16:13:37 +00:00
|
|
|
};
|
|
|
|
ret.into_result()
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:48:57 +00:00
|
|
|
pub fn compare_by_time(&self, other: &Self) -> cmp::Ordering {
|
2017-08-13 22:40:43 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
let res =
|
|
|
|
gst_sys::gst_clock_id_compare_func(self.to_glib_none().0, other.to_glib_none().0);
|
2017-08-13 22:40:43 +00:00
|
|
|
if res < 0 {
|
|
|
|
cmp::Ordering::Less
|
|
|
|
} else if res > 0 {
|
|
|
|
cmp::Ordering::Greater
|
|
|
|
} else {
|
|
|
|
cmp::Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 15:47:13 +00:00
|
|
|
|
|
|
|
#[cfg(any(feature = "v1_16", feature = "dox"))]
|
|
|
|
pub fn get_clock(&self) -> Option<Clock> {
|
|
|
|
unsafe { from_glib_full(gst_sys::gst_clock_id_get_clock(self.to_glib_none().0)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(feature = "v1_16", feature = "dox"))]
|
2019-09-25 14:17:39 +00:00
|
|
|
pub fn uses_clock<P: IsA<Clock>>(&self, clock: &P) -> bool {
|
2019-04-23 15:47:13 +00:00
|
|
|
unsafe {
|
|
|
|
from_glib(gst_sys::gst_clock_id_uses_clock(
|
|
|
|
self.to_glib_none().0,
|
|
|
|
clock.as_ref().as_ptr(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
2019-09-25 14:17:39 +00:00
|
|
|
|
|
|
|
pub fn get_type(&self) -> ClockEntryType {
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstClockEntry = self.to_glib_none().0 as *mut _;
|
|
|
|
from_glib((*ptr).type_)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_interval(&self) -> Option<ClockTime> {
|
|
|
|
if self.get_type() != ClockEntryType::Periodic {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstClockEntry = self.to_glib_none().0 as *mut _;
|
|
|
|
Some(from_glib((*ptr).interval))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_status(&self) -> &AtomicClockReturn {
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstClockEntry = self.to_glib_none().0 as *mut _;
|
|
|
|
&*((&(*ptr).status) as *const i32 as *const AtomicClockReturn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct AtomicClockReturn(AtomicI32);
|
|
|
|
|
|
|
|
impl AtomicClockReturn {
|
|
|
|
pub fn load(&self) -> ClockReturn {
|
|
|
|
from_glib(self.0.load(atomic::Ordering::SeqCst))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn store(&self, val: ClockReturn) {
|
|
|
|
self.0.store(val.to_glib(), atomic::Ordering::SeqCst)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn swap(&self, val: ClockReturn) -> ClockReturn {
|
|
|
|
from_glib(self.0.swap(val.to_glib(), atomic::Ordering::SeqCst))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compare_and_swap(&self, current: ClockReturn, new: ClockReturn) -> ClockReturn {
|
|
|
|
from_glib(self.0.compare_and_swap(
|
|
|
|
current.to_glib(),
|
|
|
|
new.to_glib(),
|
|
|
|
atomic::Ordering::SeqCst,
|
|
|
|
))
|
|
|
|
}
|
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 {
|
|
|
|
pub fn adjust_with_calibration(
|
|
|
|
internal_target: ClockTime,
|
|
|
|
cinternal: ClockTime,
|
|
|
|
cexternal: ClockTime,
|
|
|
|
cnum: ClockTime,
|
|
|
|
cdenom: ClockTime,
|
|
|
|
) -> ClockTime {
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(gst_sys::gst_clock_adjust_with_calibration(
|
2017-12-18 07:39:37 +00:00
|
|
|
ptr::null_mut(),
|
|
|
|
internal_target.to_glib(),
|
|
|
|
cinternal.to_glib(),
|
|
|
|
cexternal.to_glib(),
|
|
|
|
cnum.to_glib(),
|
|
|
|
cdenom.to_glib(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unadjust_with_calibration(
|
|
|
|
external_target: ClockTime,
|
|
|
|
cinternal: ClockTime,
|
|
|
|
cexternal: ClockTime,
|
|
|
|
cnum: ClockTime,
|
|
|
|
cdenom: ClockTime,
|
|
|
|
) -> ClockTime {
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(gst_sys::gst_clock_unadjust_with_calibration(
|
2017-12-18 07:39:37 +00:00
|
|
|
ptr::null_mut(),
|
|
|
|
external_target.to_glib(),
|
|
|
|
cinternal.to_glib(),
|
|
|
|
cexternal.to_glib(),
|
|
|
|
cnum.to_glib(),
|
|
|
|
cdenom.to_glib(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-08 09:22:42 +00:00
|
|
|
pub trait ClockExtManual: 'static {
|
2017-08-13 22:40:43 +00:00
|
|
|
fn new_periodic_id(&self, start_time: ClockTime, interval: ClockTime) -> Option<ClockId>;
|
|
|
|
|
|
|
|
fn periodic_id_reinit(
|
|
|
|
&self,
|
|
|
|
id: &ClockId,
|
|
|
|
start_time: ClockTime,
|
|
|
|
interval: ClockTime,
|
|
|
|
) -> Result<(), glib::BoolError>;
|
|
|
|
|
|
|
|
fn new_single_shot_id(&self, time: ClockTime) -> Option<ClockId>;
|
|
|
|
|
|
|
|
fn single_shot_id_reinit(&self, id: &ClockId, 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);
|
|
|
|
|
|
|
|
fn get_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 {
|
2017-08-13 22:40:43 +00:00
|
|
|
fn new_periodic_id(&self, start_time: ClockTime, interval: ClockTime) -> Option<ClockId> {
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib_full(gst_sys::gst_clock_new_periodic_id(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2017-11-11 10:21:55 +00:00
|
|
|
start_time.to_glib(),
|
|
|
|
interval.to_glib(),
|
2017-08-13 22:40:43 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn periodic_id_reinit(
|
|
|
|
&self,
|
|
|
|
id: &ClockId,
|
|
|
|
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 {
|
2019-03-19 07:58:20 +00:00
|
|
|
let res: bool = from_glib(gst_sys::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,
|
2017-11-11 10:21:55 +00:00
|
|
|
start_time.to_glib(),
|
|
|
|
interval.to_glib(),
|
2017-08-13 22:40:43 +00:00
|
|
|
));
|
|
|
|
if res {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2019-01-04 12:15:58 +00:00
|
|
|
Err(glib_bool_error!("Failed to reinit periodic clock id"))
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_single_shot_id(&self, time: ClockTime) -> Option<ClockId> {
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib_full(gst_sys::gst_clock_new_single_shot_id(
|
2019-01-16 11:32:58 +00:00
|
|
|
self.as_ref().to_glib_none().0,
|
2017-11-11 10:21:55 +00:00
|
|
|
time.to_glib(),
|
2017-08-13 22:40:43 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn single_shot_id_reinit(&self, id: &ClockId, time: ClockTime) -> Result<(), glib::BoolError> {
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
let res: bool = from_glib(gst_sys::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,
|
2017-11-11 10:21:55 +00:00
|
|
|
time.to_glib(),
|
2017-08-13 22:40:43 +00:00
|
|
|
));
|
|
|
|
if res {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2019-01-04 12:15:58 +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 {
|
|
|
|
let ptr: *mut gst_sys::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = ::utils::MutexGuard::lock(&(*ptr).lock);
|
|
|
|
(*ptr).flags |= flags.to_glib();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unset_clock_flags(&self, flags: ClockFlags) {
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = ::utils::MutexGuard::lock(&(*ptr).lock);
|
|
|
|
(*ptr).flags &= !flags.to_glib();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_clock_flags(&self) -> ClockFlags {
|
|
|
|
unsafe {
|
|
|
|
let ptr: *mut gst_sys::GstObject = self.as_ptr() as *mut _;
|
|
|
|
let _guard = ::utils::MutexGuard::lock(&(*ptr).lock);
|
|
|
|
from_glib((*ptr).flags)
|
|
|
|
}
|
|
|
|
}
|
2017-08-13 22:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::super::*;
|
2018-04-01 08:30:03 +00:00
|
|
|
use super::*;
|
2017-08-13 22:40:43 +00:00
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wait() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let clock = SystemClock::obtain();
|
|
|
|
let now = clock.get_time();
|
2017-11-11 10:21:55 +00:00
|
|
|
let id = clock.new_single_shot_id(now + 20 * ::MSECOND).unwrap();
|
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() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let (sender, receiver) = channel();
|
|
|
|
|
|
|
|
let clock = SystemClock::obtain();
|
|
|
|
let now = clock.get_time();
|
2017-11-11 10:21:55 +00:00
|
|
|
let id = clock.new_single_shot_id(now + 20 * ::MSECOND).unwrap();
|
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(()));
|
|
|
|
}
|
|
|
|
}
|