Clock

class Clock(**properties: Any)

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 master of the master clock and the time slave of the slave clock are added to the list of observations. If enough observations are available, a linear regression algorithm is run on the observations and clock is recalibrated.

If this functions returns True, r_squared will 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 internal clock time to the external time, adjusting for the rate and reference time set with set_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_target clock time to the external time, using the passed calibration parameters. This function performs the same calculation as adjust_unlocked() when called using the current calibration parameters, but doesn’t ensure a monotonically increasing result as adjust_unlocked() does.

Note: The clock parameter is unused and can be NULL

Added 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

get_calibration() tuple[int, int, int, int]

Gets the internal rate and reference time of clock. See set_calibration() for more information.

internal, external, rate_num, and rate_denom can be left None if 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 clock is slaved to or None when 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.

get_timeout() int

Gets the amount of time that master and slave clocks are sampled.

id_compare_func(id1: None, id2: None) int

Compares the two ClockID instances. This function can be used as a GCompareFunc when sorting ids.

Parameters:
  • id1 – A ClockID

  • id2 – A ClockID to compare with

id_get_clock(id: None) Clock | None

This function returns the underlying clock.

Added in version 1.16.

Parameters:

id – a ClockID

id_get_time(id: None) int

Gets the time of the clock ID

Parameters:

id – The ClockID to query

id_unschedule(id: None) None

Cancels an outstanding request with id. This can either be an outstanding async notification or a pending sync notification. After this call, id cannot be used anymore to receive sync or async notifications, you need to create a new ClockID.

Parameters:

id – The id to unschedule

id_uses_clock(id: None, clock: Clock) bool

This function returns whether id uses clock as the underlying clock. clock can be NULL, in which case the return value indicates whether the underlying clock has been freed. If this is the case, the id is no longer usable and should be freed.

Added in version 1.16.

Parameters:
  • id – a ClockID to check

  • clock – a Clock to compare against

id_wait(id: None) tuple[ClockReturn, int]

Performs a blocking wait on id. id should have been created with new_single_shot_id() or new_periodic_id() and should not have been unscheduled with a call to id_unschedule().

If the jitter argument is not None and this function returns GST_CLOCK_OK or GST_CLOCK_EARLY, it will contain the difference against the clock and the time of id when this method was called. Positive values indicate how late id was relative to the clock (in which case this function will return GST_CLOCK_EARLY). Negative values indicate how much time was spent waiting on the clock before this function returned.

Parameters:

id – The ClockID to wait on

id_wait_async(id: None, func: Callable[[...], bool], *user_data: Any) ClockReturn

Registers a callback on the given ClockID id with the given function and user_data. When passing a ClockID with an invalid time to this function, the callback will be called immediately with a time set to CLOCK_TIME_NONE. The callback will be called when the time of id has been reached.

The callback func can be invoked from any thread, either provided by the core or from a streaming thread. The application should be prepared for this.

Parameters:
  • id – a ClockID to wait on

  • 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_SYNC is set.

Added in version 1.6.

new_periodic_id(start_time: int, interval: int) None

Gets an ID from clock to trigger a periodic notification. The periodic notifications will start at time start_time and will then be fired with the given interval.

Parameters:
  • start_time – the requested start time

  • interval – the requested interval

new_single_shot_id(time: int) None

Gets a ClockID from clock to trigger a single shot notification at the requested time.

Parameters:

time – the requested time

periodic_id_reinit(id: None, start_time: int, interval: int) bool

Reinitializes the provided periodic id to the provided start time and interval. Does not modify the reference count.

Parameters:
  • id – a ClockID

  • start_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.

internal and external are calibration parameters that arrange that get_time() should have been external at internal time internal. This internal time should not be in the future; that is, it should be less than the value of get_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 master as the master clock for clock. clock will be automatically calibrated so that get_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().

master can be None in which case clock will 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_RESOLUTION flag set.

Parameters:

resolution – The resolution to set

set_synced(synced: bool) None

Sets clock to synced and emits the Clock::synced signal, and wakes up any thread waiting in wait_for_sync().

This function must only be called if NEEDS_STARTUP_SYNC is 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: None, time: int) bool

Reinitializes the provided single shot id to the provided time. Does not modify the reference count.

Parameters:
  • id – a ClockID

  • time – The requested time.

unadjust_unlocked(external: int) int

Converts the given external clock time to the internal time of clock, using the rate and reference time set with set_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_target clock time to the internal time, using the passed calibration parameters. This function performs the same calculation as unadjust_unlocked() when called using the current calibration parameters.

Note: The clock parameter is unused and can be NULL

Added 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 clock is synced for reporting the current time. If timeout is CLOCK_TIME_NONE it will wait forever, otherwise it will time out after timeout nanoseconds.

For asynchronous waiting, the Clock::synced signal can be used.

This returns immediately with True if NEEDS_STARTUP_SYNC is 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

class Clock
props.timeout: int
props.window_size: int
props.window_threshold: int

Signals

class Clock.signals
synced(synced: bool) None

Signaled on clocks with NEEDS_STARTUP_SYNC set once the clock is synchronized, or when it completely lost synchronization. This signal will not be emitted on clocks without the flag.

This signal will be emitted from an arbitrary thread, most likely not the application’s main thread.

Added in version 1.6.

Parameters:

synced – if the clock is synced now

Virtual Methods

class Clock
do_change_resolution(old_resolution: int, new_resolution: int) int

Change the resolution of the clock. Not all values might be acceptable.

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

Unblock a blocking or async wait operation.

Parameters:

entry – the entry to unschedule

do_wait(entry: ClockEntry) tuple[ClockReturn, int]

Perform a blocking wait on the given ClockEntry and return the jitter.

Parameters:

entry – the entry to wait on

do_wait_async(entry: ClockEntry) ClockReturn

Perform an asynchronous wait on the given ClockEntry.

Parameters:

entry – the entry to wait on

Fields

class Clock
object

The parent structure

priv