Next: , Previous: GstChildProxy, Up: Top


7 GstClock

Abstract class for global clocks

7.1 Overview

GStreamer uses a global clock to synchronize the plugins in a pipeline. Different clock implementations are possible by implementing this abstract base class.

The <gst-clock> returns a monotonically increasing time with the method gst-clock-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 stream time. Usually all renderers synchronize to the global clock using the buffer timestamps, the newsegment events and the element's base time, see <gst-pipeline>.

A clock implementation can support periodic and single shot clock notifications both synchronous and asynchronous.

One first needs to create a <gst-clock-id> for the periodic or single shot notification using gst-clock-new-single-shot-id or gst-clock-new-periodic-id.

To perform a blocking wait for the specific time of the <gst-clock-id> use the gst-clock-id-wait. To receive a callback when the specific time is reached in the clock use gst-clock-id-wait-async. Both these calls can be interrupted with the gst-clock-id-unschedule call. If the blocking wait is unscheduled a return value of GST_CLOCK_UNSCHEDULED is returned.

Periodic callbacks scheduled async will be repeadedly called automatically until it is unscheduled. To schedule a sync periodic callback, gst-clock-id-wait should be called repeadedly.

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 <gst-clock-id> that has been unscheduled cannot be used again for any wait operation, a new <gst-clock-id> should be created and the old unscheduled one should be destroyed wirth gst-clock-id-unref.

It is possible to perform a blocking wait on the same <gst-clock-id> from multiple threads. However, registering the same <gst-clock-id> 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 <gst-clock-id>, 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 <gst-clock-id> has to keep a handle to the <gst-clock-id> 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 stream 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 <gst-clock> with the gst-clock-set-master. The clock will then automatically be synchronized to this master clock by repeadedly sampling the master clock and the slave clock and recalibrating the slave clock with gst-clock-set-calibration. This feature is mostly useful for plugins that have an internal clock but must operate with another clock selected by the <gst-pipeline>. They can track the offset and rate difference of their internal clock relative to the master clock by using the gst-clock-get-calibration function.

The master/slave synchronisation can be tuned with the "timeout", "window-size" and "window-threshold" properties. The "timeout" property defines the interval to sample the master clock and run the calibration functions. "window-size" defines the number of samples to use when calibrating and "window-threshold" defines the minimum number of samples before the calibration is performed.

Last reviewed on 2006-08-11 (0.10.10)

7.2 Usage

— Class: <gst-clock>

This <gobject> class defines the following properties:

stats
Enable clock stats (unimplemented)
window-size
The size of the window used to calculate rate and offset
window-threshold
The threshold to start calculating rate and offset
timeout
The amount of time, in nanoseconds, to sample master and slave clocks
— Function: gst-clock-add-observation (self <gst-clock>) (slave unsigned-long-long) (master unsigned-long-long)   (ret bool) (r_squared double)
— Method: add-observation

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 ‘#t’, r-squared will contain the correlation coefficient of the interpollation. 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.

clock
a <gst-clock>
slave
a time on the slave
master
a time on the master
r-squared
a pointer to hold the result
ret
TRUE if enough observations were added to run the regression algorithm. MT safe.
— Function: gst-clock-set-master (self <gst-clock>) (master <gst-clock>)   (ret bool)
— Method: set-master

Set master as the master clock for clock. clock will be automatically calibrated so that gst-clock-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 gst-clock-get-calibration.

master can be NULL 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.

clock
a <gst-clock>
master
a master <gst-clock>
ret
TRUE if the clock is capable of being slaved to a master clock. Trying to set a master on a clock without the GST_CLOCK_FLAG_CAN_SET_MASTER flag will make this function return FALSE. MT safe.
— Function: gst-clock-get-master (self <gst-clock>)   (ret <gst-clock>)
— Method: get-master

Get the master clock that clock is slaved to or NULL when the clock is not slaved to any master clock.

clock
a <gst-clock>
ret
a master <gst-clock> or NULL when this clock is not slaved to a master clock. Unref after usage. MT safe.
— Function: gst-clock-set-resolution (self <gst-clock>) (resolution unsigned-long-long)   (ret unsigned-long-long)
— Method: set-resolution

Set 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.

clock
a <gst-clock>
resolution
The resolution to set
ret
the new resolution of the clock.
— Function: gst-clock-get-resolution (self <gst-clock>)   (ret unsigned-long-long)
— Method: get-resolution

Get the accuracy of the clock. The accuracy of the clock is the granularity of the values returned by gst-clock-get-time.

clock
a <gst-clock>
ret
the resolution of the clock in units of <gst-clock-time>. MT safe.
— Function: gst-clock-get-time (self <gst-clock>)   (ret unsigned-long-long)
— Method: get-time

Gets the current time of the given clock. The time is always monotonically increasing and adjusted according to the current offset and rate.

clock
a <gst-clock> to query
ret
the time of the clock. Or GST_CLOCK_TIME_NONE when giving wrong input. MT safe.
— Function: gst-clock-new-single-shot-id (self <gst-clock>) (time unsigned-long-long)   (ret <gst-clock-id>)
— Method: new-single-shot-id

Get a <gst-clock-id> from clock to trigger a single shot notification at the requested time. The single shot id should be unreffed after usage.

clock
The <gst-clock-id> to get a single shot notification from
time
the requested time
ret
A <gst-clock-id> that can be used to request the time notification. MT safe.
— Function: gst-clock-new-periodic-id (self <gst-clock>) (start_time unsigned-long-long) (interval unsigned-long-long)   (ret <gst-clock-id>)
— Method: new-periodic-id

Get an ID from clock to trigger a periodic notification. The periodeic notifications will be start at time start_time and will then be fired with the given interval. id should be unreffed after usage.

clock
The <gst-clock-id> to get a periodic notification id from
start-time
the requested start time
interval
the requested interval
ret
A <gst-clock-id> that can be used to request the time notification. MT safe.
— Function: gst-clock-get-internal-time (self <gst-clock>)   (ret unsigned-long-long)
— Method: get-internal-time

Gets the current internal time of the given clock. The time is returned unadjusted for the offset and the rate.

clock
a <gst-clock> to query
ret
the internal time of the clock. Or GST_CLOCK_TIME_NONE when giving wrong input. MT safe.
— Function: gst-clock-adjust-unlocked (self <gst-clock>) (internal unsigned-long-long)   (ret unsigned-long-long)
— Method: adjust-unlocked

Converts the given internal clock time to the external time, adjusting for the rate and reference time set with gst-clock-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 te reverse of gst-clock-unadjust-unlocked.

clock
a <gst-clock> to use
internal
a clock time
ret
the converted time of the clock.
— Function: gst-clock-get-calibration (self <gst-clock>) (internal <gst-clock-time*>) (external <gst-clock-time*>) (rate_num <gst-clock-time*>) (rate_denom <gst-clock-time*>)
— Method: get-calibration

Gets the internal rate and reference time of clock. See gst-clock-set-calibration for more information.

internal, external, rate-num, and rate-denom can be left NULL if the caller is not interested in the values.

MT safe.

clock
a <gst-clock>
internal
a location to store the internal time
external
a location to store the external time
rate-num
a location to store the rate numerator
rate-denom
a location to store the rate denominator
— Function: gst-clock-set-calibration (self <gst-clock>) (internal unsigned-long-long) (external unsigned-long-long) (rate_num unsigned-long-long) (rate_denom unsigned-long-long)
— Method: set-calibration

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 gst-clock-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 gst-clock-get-internal-time when this function is called.

Subsequent calls to gst-clock-get-time will return clock times computed as follows:

          
            time = (internal_time - @internal) * @rate_num / @rate_denom + @external

This formula is implemented in gst-clock-adjust-unlocked. Of course, it tries to do the integer arithmetic as precisely as possible.

Note that gst-clock-get-time always returns increasing values so when you move the clock backwards, gst-clock-get-time will report the previous value until the clock catches up.

MT safe.

clock
a <gst-clock> to calibrate
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
— Function: gst-clock-id-get-time (id <gst-clock-id>)   (ret unsigned-long-long)

Get the time of the clock ID

id
The <gst-clock-id> to query
ret
the time of the given clock id. MT safe.
— Function: gst-clock-id-wait (id <gst-clock-id>) (jitter <gst-clock-time-diff*>)   (ret <gst-clock-return>)

Perform a blocking wait on id. id should have been created with gst-clock-new-single-shot-id or gst-clock-new-periodic-id and should not have been unscheduled with a call to gst-clock-id-unschedule.

If the jitter argument is not NULL 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.

id
The <gst-clock-id> to wait on
jitter
A pointer that will contain the jitter, can be NULL.
ret
the result of the blocking wait. <gst-clock-early> will be returned if the current clock time is past the time of id, <gst-clock-ok> if id was scheduled in time. <gst-clock-unscheduled> if id was unscheduled with gst-clock-id-unschedule. MT safe.
— Function: gst-clock-id-wait-async (id <gst-clock-id>) (callback scm)   (ret <gst-clock-return>)

Register a callback on the given <gst-clock-id>id with the given function and user_data. When passing a <gst-clock-id> with an invalid time to this function, the callback will be called immediatly with a time set to GST_CLOCK_TIME_NONE. The callback will be called when the time of id has been reached.

id
a <gst-clock-id> to wait on
func
The callback function
user-data
User data passed in the calback
ret
the result of the non blocking wait. MT safe.
— Function: gst-clock-id-unschedule (id <gst-clock-id>)

Cancel 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 <gst-clock-id>.

MT safe.

id
The id to unschedule
— Function: gst-clock-id-compare-func (id1 <gconstpointer>) (id2 <gconstpointer>)   (ret int)

Compares the two <gst-clock-id> instances. This function can be used as a GCompareFunc when sorting ids.

id1
A <gst-clock-id>
id2
A <gst-clock-id> to compare with
ret
negative value if a < b; zero if a = b; positive value if a > b MT safe.