Clock#
Superclasses: Object, InitiallyUnowned, Object
Subclasses: SystemClock
GStreamer uses a global clock to synchronize the plugins in a pipeline.
Different clock implementations are possible by implementing this abstract
base class or, more conveniently, by subclassing GstSystemClock.
The Clock returns a monotonically increasing time with the method
get_time(). Its accuracy and base time depend on the specific
clock implementation but time is always expressed in nanoseconds. Since the
baseline of the clock is undefined, the clock time returned is not
meaningful in itself, what matters are the deltas between two clock times.
The time returned by a clock is called the absolute time.
The pipeline uses the clock to calculate the running time. Usually all
renderers synchronize to the global clock using the buffer timestamps, the
GST_EVENT_SEGMENT events and the element’s base time, see Pipeline.
A clock implementation can support periodic and single shot clock notifications both synchronous and asynchronous.
One first needs to create a ClockID for the periodic or single shot
notification using new_single_shot_id() or
new_periodic_id().
To perform a blocking wait for the specific time of the ClockID use
id_wait(). To receive a callback when the specific time is reached
in the clock use id_wait_async(). Both these calls can be
interrupted with the id_unschedule() call. If the blocking wait is
unscheduled a return value of GST_CLOCK_UNSCHEDULED is returned.
Periodic callbacks scheduled async will be repeatedly called automatically
until they are unscheduled. To schedule a sync periodic callback,
id_wait() should be called repeatedly.
The async callbacks can happen from any thread, either provided by the core or from a streaming thread. The application should be prepared for this.
A ClockID that has been unscheduled cannot be used again for any wait
operation, a new ClockID should be created and the old unscheduled one
should be destroyed with id_unref().
It is possible to perform a blocking wait on the same ClockID from
multiple threads. However, registering the same ClockID for multiple
async notifications is not possible, the callback will only be called for
the thread registering the entry last.
None of the wait operations unref the ClockID, the owner is responsible
for unreffing the ids itself. This holds for both periodic and single shot
notifications. The reason being that the owner of the ClockID has to
keep a handle to the ClockID to unblock the wait on FLUSHING events or
state changes and if the entry would be unreffed automatically, the handle
might become invalid without any notification.
These clock operations do not operate on the running time, so the callbacks will also occur when not in PLAYING state as if the clock just keeps on running. Some clocks however do not progress when the element that provided the clock is not PLAYING.
When a clock has the GST_CLOCK_FLAG_CAN_SET_MASTER flag set, it can be
slaved to another Clock with set_master(). The clock will
then automatically be synchronized to this master clock by repeatedly
sampling the master clock and the slave clock and recalibrating the slave
clock with set_calibration(). This feature is mostly useful for
plugins that have an internal clock but must operate with another clock
selected by the Pipeline. They can track the offset and rate difference
of their internal clock relative to the master clock by using the
get_calibration() function.
The master/slave synchronisation can be tuned with the Clock:timeout,
Clock:window-size and Clock:window-threshold properties.
The Clock:timeout property defines the interval to sample the master
clock and run the calibration functions. Clock:window-size defines the
number of samples to use when calibrating and Clock:window-threshold
defines the minimum number of samples before the calibration is performed.
Methods#
- class Clock
- add_observation(slave: int, master: int) tuple[bool, float]#
The time
masterof the master clock and the timeslaveof the slave clock are added to the list of observations. If enough observations are available, a linear regression algorithm is run on the observations andclockis recalibrated.If this functions returns
True,r_squaredwill contain the correlation coefficient of the interpolation. A value of 1.0 means a perfect regression was performed. This value can be used to control the sampling frequency of the master and slave clocks.- Parameters:
slave – a time on the slave
master – a time on the master
- add_observation_unapplied(slave: int, master: int) tuple[bool, float, int, int, int, int]#
Add a clock observation to the internal slaving algorithm the same as
add_observation(), and return the result of the master clock estimation, without updating the internal calibration.The caller can then take the results and call
set_calibration()with the values, or some modified version of them.Added in version 1.6.
- Parameters:
slave – a time on the slave
master – a time on the master
- adjust_unlocked(internal: int) int#
Converts the given
internalclock time to the external time, adjusting for the rate and reference time set withset_calibration()and making sure that the returned time is increasing. This function should be called with the clock’s OBJECT_LOCK held and is mainly used by clock subclasses.This function is the reverse of
unadjust_unlocked().- Parameters:
internal – a clock time
- adjust_with_calibration(internal_target: int, cinternal: int, cexternal: int, cnum: int, cdenom: int) int#
Converts the given
internal_targetclock time to the external time, using the passed calibration parameters. This function performs the same calculation asadjust_unlocked()when called using the current calibration parameters, but doesn’t ensure a monotonically increasing result asadjust_unlocked()does.Note: The
clockparameter is unused and can be NULLAdded in version 1.6.
- Parameters:
internal_target – a clock time
cinternal – a reference internal time
cexternal – a reference external time
cnum – the numerator of the rate of the clock relative to its internal time
cdenom – the denominator of the rate of the clock
- do_change_resolution(self, old_resolution: int, new_resolution: int) int#
- Parameters:
old_resolution
new_resolution
- do_unschedule(self, entry: ClockEntry) None#
- Parameters:
entry
- do_wait(self, entry: ClockEntry) tuple[ClockReturn, int]#
- Parameters:
entry
- do_wait_async(self, entry: ClockEntry) ClockReturn#
- Parameters:
entry
- get_calibration() tuple[int, int, int, int]#
Gets the internal rate and reference time of
clock. Seeset_calibration()for more information.internal,external,rate_num, andrate_denomcan be leftNoneif the caller is not interested in the values.
- get_internal_time() int#
Gets the current internal time of the given clock. The time is returned unadjusted for the offset and the rate.
- get_master() Clock | None#
Gets the master clock that
clockis slaved to orNonewhen the clock is not slaved to any master clock.
- get_resolution() int#
Gets the accuracy of the clock. The accuracy of the clock is the granularity of the values returned by
get_time().
- get_time() int#
Gets the current time of the given clock. The time is always monotonically increasing and adjusted according to the current offset and rate.
- classmethod id_compare_func(id2: Any = None) int#
Compares the two
ClockIDinstances. This function can be used as a GCompareFunc when sorting ids.- Parameters:
id2 – A
ClockIDto compare with
- classmethod id_get_clock() Clock | None#
This function returns the underlying clock.
Added in version 1.16.
- classmethod id_unschedule() None#
Cancels an outstanding request with
id. This can either be an outstanding async notification or a pending sync notification. After this call,idcannot be used anymore to receive sync or async notifications, you need to create a newClockID.
- classmethod id_uses_clock(clock: Clock) bool#
This function returns whether
idusesclockas the underlying clock.clockcan be NULL, in which case the return value indicates whether the underlying clock has been freed. If this is the case, theidis no longer usable and should be freed.Added in version 1.16.
- Parameters:
clock – a
Clockto compare against
- classmethod id_wait() tuple[ClockReturn, int]#
Performs a blocking wait on
id.idshould have been created withnew_single_shot_id()ornew_periodic_id()and should not have been unscheduled with a call toid_unschedule().If the
jitterargument is notNoneand this function returnsGST_CLOCK_OKorGST_CLOCK_EARLY, it will contain the difference against the clock and the time ofidwhen this method was called. Positive values indicate how lateidwas relative to the clock (in which case this function will returnGST_CLOCK_EARLY). Negative values indicate how much time was spent waiting on the clock before this function returned.
- classmethod id_wait_async(func: Callable[[Clock, int, Any, Any], bool], user_data: Any = None) ClockReturn#
Registers a callback on the given
ClockIDidwith the given function and user_data. When passing aClockIDwith an invalid time to this function, the callback will be called immediately with a time set toCLOCK_TIME_NONE. The callback will be called when the time ofidhas been reached.The callback
funccan be invoked from any thread, either provided by the core or from a streaming thread. The application should be prepared for this.- Parameters:
func – The callback function
user_data – User data passed in the callback
- is_synced() bool#
Checks if the clock is currently synced, by looking at whether
NEEDS_STARTUP_SYNCis set.Added in version 1.6.
- new_periodic_id(start_time: int, interval: int) Any#
Gets an ID from
clockto trigger a periodic notification. The periodic notifications will start at timestart_timeand will then be fired with the giveninterval.- Parameters:
start_time – the requested start time
interval – the requested interval
- new_single_shot_id(time: int) Any#
Gets a
ClockIDfromclockto trigger a single shot notification at the requested time.- Parameters:
time – the requested time
- periodic_id_reinit(id: Any, start_time: int, interval: int) bool#
Reinitializes the provided periodic
idto the provided start time and interval. Does not modify the reference count.- Parameters:
id – a
ClockIDstart_time – the requested start time
interval – the requested interval
- set_calibration(internal: int, external: int, rate_num: int, rate_denom: int) None#
Adjusts the rate and time of
clock. A rate of 1/1 is the normal speed of the clock. Values bigger than 1/1 make the clock go faster.internalandexternalare calibration parameters that arrange thatget_time()should have beenexternalat internal timeinternal. This internal time should not be in the future; that is, it should be less than the value ofget_internal_time()when this function is called.Subsequent calls to
get_time()will return clock times computed as follows:time = (internal_time - internal) * rate_num / rate_denom + external
This formula is implemented in
adjust_unlocked(). Of course, it tries to do the integer arithmetic as precisely as possible.Note that
get_time()always returns increasing values so when you move the clock backwards,get_time()will report the previous value until the clock catches up.- Parameters:
internal – a reference internal time
external – a reference external time
rate_num – the numerator of the rate of the clock relative to its internal time
rate_denom – the denominator of the rate of the clock
- set_master(master: Clock | None = None) bool#
Sets
masteras the master clock forclock.clockwill be automatically calibrated so thatget_time()reports the same time as the master clock.A clock provider that slaves its clock to a master can get the current calibration values with
get_calibration().mastercan beNonein which caseclockwill not be slaved anymore. It will however keep reporting its time adjusted with the last configured rate and time offsets.- Parameters:
master – a master
Clock
- set_resolution(resolution: int) int#
Sets the accuracy of the clock. Some clocks have the possibility to operate with different accuracy at the expense of more resource usage. There is normally no need to change the default resolution of a clock. The resolution of a clock can only be changed if the clock has the
GST_CLOCK_FLAG_CAN_SET_RESOLUTIONflag set.- Parameters:
resolution – The resolution to set
- set_synced(synced: bool) None#
Sets
clockto synced and emits theClock::synced signal, and wakes up any thread waiting inwait_for_sync().This function must only be called if
NEEDS_STARTUP_SYNCis set on the clock, and is intended to be called by subclasses only.Added in version 1.6.
- Parameters:
synced – if the clock is synced
- set_timeout(timeout: int) None#
Sets the amount of time, in nanoseconds, to sample master and slave clocks
- Parameters:
timeout – a timeout
- single_shot_id_reinit(id: Any, time: int) bool#
Reinitializes the provided single shot
idto the provided time. Does not modify the reference count.- Parameters:
id – a
ClockIDtime – The requested time.
- unadjust_unlocked(external: int) int#
Converts the given
externalclock time to the internal time ofclock, using the rate and reference time set withset_calibration(). This function should be called with the clock’s OBJECT_LOCK held and is mainly used by clock subclasses.This function is the reverse of
adjust_unlocked().- Parameters:
external – an external clock time
- unadjust_with_calibration(external_target: int, cinternal: int, cexternal: int, cnum: int, cdenom: int) int#
Converts the given
external_targetclock time to the internal time, using the passed calibration parameters. This function performs the same calculation asunadjust_unlocked()when called using the current calibration parameters.Note: The
clockparameter is unused and can be NULLAdded in version 1.8.
- Parameters:
external_target – a clock time
cinternal – a reference internal time
cexternal – a reference external time
cnum – the numerator of the rate of the clock relative to its internal time
cdenom – the denominator of the rate of the clock
- wait_for_sync(timeout: int) bool#
Waits until
clockis synced for reporting the current time. IftimeoutisCLOCK_TIME_NONEit will wait forever, otherwise it will time out aftertimeoutnanoseconds.For asynchronous waiting, the
Clock::synced signal can be used.This returns immediately with
TrueifNEEDS_STARTUP_SYNCis not set on the clock, or if the clock is already synced.Added in version 1.6.
- Parameters:
timeout – timeout for waiting or
CLOCK_TIME_NONE
Properties#
Signals#
Virtual Methods#
- class Clock
- do_change_resolution(old_resolution: int, new_resolution: int) int#
The type of the None singleton.
- Parameters:
old_resolution – the previous resolution
new_resolution – the new resolution
- do_get_internal_time() int#
Gets the current internal time of the given clock. The time is returned unadjusted for the offset and the rate.
- do_get_resolution() int#
Gets the accuracy of the clock. The accuracy of the clock is the granularity of the values returned by
get_time().
- do_unschedule(entry: ClockEntry) None#
The type of the None singleton.
- Parameters:
entry – the entry to unschedule
- do_wait(entry: ClockEntry) tuple[ClockReturn, int]#
The type of the None singleton.
- Parameters:
entry – the entry to wait on
- do_wait_async(entry: ClockEntry) ClockReturn#
The type of the None singleton.
- Parameters:
entry – the entry to wait on