Class Clock
- All Implemented Interfaces:
Proxy
- Direct Known Subclasses:
Clock.ClockImpl
,SystemClock
GstSystemClock
.
The GstClock
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 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 GstPipeline
.
A clock implementation can support periodic and single shot clock notifications both synchronous and asynchronous.
One first needs to create a GstClockID
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 GstClockID
use
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 repeatedly called automatically until they are unscheduled. To schedule a sync periodic callback, gst_clock_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 GstClockID
that has been unscheduled cannot be used again for any wait
operation, a new GstClockID
should be created and the old unscheduled one
should be destroyed with gst_clock_id_unref().
It is possible to perform a blocking wait on the same GstClockID
from
multiple threads. However, registering the same GstClockID
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 GstClockID
, 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 GstClockID
has to
keep a handle to the GstClockID
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 GstClock
with gst_clock_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 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 GstPipeline
. 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 GstClock
:timeout,
GstClock
:window-size and GstClock
:window-threshold properties.
The GstClock
:timeout property defines the interval to sample the master
clock and run the calibration functions. GstClock
:window-size defines the
number of samples to use when calibrating and GstClock
:window-threshold
defines the minimum number of samples before the calibration is performed.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Clock.Builder<B extends Clock.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
GStreamer clock class.static class
The ClockImpl type represents a native instance of the abstract Clock class.static interface
Functional interface declaration of theSyncedCallback
callback.Nested classes/interfaces inherited from class org.freedesktop.gstreamer.gst.GstObject
GstObject.DeepNotifyCallback, GstObject.ObjectClass, GstObject.ObjectImpl
Nested classes/interfaces inherited from class org.gnome.gobject.InitiallyUnowned
InitiallyUnowned.InitiallyUnownedClass
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback
-
Constructor Summary
ConstructorDescriptionClock
(MemorySegment address) Create a Clock proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionboolean
addObservation
(ClockTime slave, ClockTime master, Out<Double> rSquared) The timemaster
of the master clock and the timeslave
of the slave clock are added to the list of observations.boolean
addObservationUnapplied
(ClockTime slave, ClockTime master, Out<Double> rSquared, @Nullable ClockTime internal, @Nullable ClockTime external, @Nullable ClockTime rateNum, @Nullable ClockTime rateDenom) Add a clock observation to the internal slaving algorithm the same as gst_clock_add_observation(), and return the result of the master clock estimation, without updating the internal calibration.adjustUnlocked
(ClockTime internal) Converts the giveninternal
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.adjustWithCalibration
(ClockTime internalTarget, ClockTime cinternal, ClockTime cexternal, ClockTime cnum, ClockTime cdenom) Converts the giveninternalTarget
clock time to the external time, using the passed calibration parameters.protected Clock
asParent()
Returns this instance as if it were its parent type.static Clock.Builder
<? extends Clock.Builder> builder()
AClock.Builder
object constructs aClock
with the specified properties.protected ClockTime
changeResolution
(ClockTime oldResolution, ClockTime newResolution) Change the resolution of the clock.void
emitSynced
(boolean synced) Emits the "synced" signal.void
getCalibration
(@Nullable ClockTime internal, @Nullable ClockTime external, @Nullable ClockTime rateNum, @Nullable ClockTime rateDenom) Gets the internal rate and reference time of this Clock.Gets the current internal time of the given clock.Gets the master clock that this Clock is slaved to ornull
when the clock is not slaved to any master clock.static MemoryLayout
The memory layout of the native struct.Gets the accuracy of the clock.getTime()
Gets the current time of the given clock.Gets the amount of time that master and slave clocks are sampled.static Type
getType()
Get the GType of the Clock classstatic int
idCompareFunc
(@Nullable MemorySegment id1, @Nullable MemorySegment id2) Compares the twoGstClockID
instances.static Clock
idGetClock
(ClockID id) This function returns the underlying clock.static ClockTime
Gets the time of the clock IDstatic ClockID
Increases the refcount of givenid
.static void
Unrefs givenid
.static void
idUnschedule
(ClockID id) Cancels an outstanding request withid
.static boolean
idUsesClock
(ClockID id, Clock clock) This function returns whetherid
usesclock
as the underlying clock.static ClockReturn
idWait
(ClockID id, @Nullable ClockTimeDiff jitter) Performs a blocking wait onid
.static ClockReturn
idWaitAsync
(ClockID id, ClockCallback func) Registers a callback on the givenGstClockID
id
with the given function and user_data.boolean
isSynced()
Checks if the clock is currently synced, by looking at whetherClockFlags.NEEDS_STARTUP_SYNC
is set.newPeriodicId
(ClockTime startTime, ClockTime interval) Gets an ID from this Clock to trigger a periodic notification.newSingleShotId
(ClockTime time) Gets aGstClockID
from this Clock to trigger a single shot notification at the requested time.onSynced
(Clock.SyncedCallback handler) Signaled on clocks withClockFlags.NEEDS_STARTUP_SYNC
set once the clock is synchronized, or when it completely lost synchronization.boolean
periodicIdReinit
(ClockID id, ClockTime startTime, ClockTime interval) Reinitializes the provided periodicid
to the provided start time and interval.void
setCalibration
(ClockTime internal, ClockTime external, ClockTime rateNum, ClockTime rateDenom) Adjusts the rate and time of this Clock.boolean
Setsmaster
as the master clock for this Clock.setResolution
(ClockTime resolution) Sets the accuracy of the clock.void
setSynced
(boolean synced) Sets this Clock to synced and emits theGstClock
::synced signal, and wakes up any thread waiting in gst_clock_wait_for_sync().void
setTimeout
(ClockTime timeout) Sets the amount of time, in nanoseconds, to sample master and slave clocksboolean
singleShotIdReinit
(ClockID id, ClockTime time) Reinitializes the provided single shotid
to the provided time.unadjustUnlocked
(ClockTime external) Converts the givenexternal
clock time to the internal time of this Clock, using the rate and reference time set with gst_clock_set_calibration().unadjustWithCalibration
(ClockTime externalTarget, ClockTime cinternal, ClockTime cexternal, ClockTime cnum, ClockTime cdenom) Converts the givenexternalTarget
clock time to the internal time, using the passed calibration parameters.protected void
unschedule
(ClockEntry entry) Unblock a blocking or async wait operation.protected ClockReturn
wait_
(ClockEntry entry, @Nullable ClockTimeDiff jitter) Perform a blocking wait on the givenGstClockEntry
and return the jitter.protected ClockReturn
waitAsync
(ClockEntry entry) Perform an asynchronous wait on the givenGstClockEntry
.boolean
waitForSync
(ClockTime timeout) Waits until this Clock is synced for reporting the current time.Methods inherited from class org.freedesktop.gstreamer.gst.GstObject
addControlBinding, checkUniqueness, deepNotify, defaultDeepNotify, defaultError, emitDeepNotify, getControlBinding, getControlRate, getGValueArray, getName, getParent, getPathString, getValue, getValueArray, hasActiveControlBindings, hasAncestor, hasAsAncestor, hasAsParent, onDeepNotify, ref, refSink, removeControlBinding, replace, setControlBindingDisabled, setControlBindingsDisabled, setControlRate, setName, setParent, suggestNextSync, syncValues, unparent, unref
Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Clock
Create a Clock proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
idCompareFunc
public static int idCompareFunc(@Nullable @Nullable MemorySegment id1, @Nullable @Nullable MemorySegment id2) Compares the twoGstClockID
instances. This function can be used as a GCompareFunc when sorting ids.- Parameters:
id1
- AGstClockID
id2
- AGstClockID
to compare with- Returns:
- negative value if
a < b
; zero ifa = b
; positive value ifa > b
-
idGetClock
-
idGetTime
-
idRef
-
idUnref
Unrefs givenid
. When the refcount reaches 0 theGstClockID
will be freed.- Parameters:
id
- TheGstClockID
to unref
-
idUnschedule
Cancels an outstanding request withid
. 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 newGstClockID
.- Parameters:
id
- The id to unschedule
-
idUsesClock
This function returns whetherid
usesclock
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, theid
is no longer usable and should be freed.- Parameters:
id
- aGstClockID
to checkclock
- aGstClock
to compare against- Returns:
- whether the clock
id
uses the same underlyingGstClock
clock
.
-
idWait
Performs a blocking wait onid
.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 notnull
and this function returnsGST_CLOCK_OK
orGST_CLOCK_EARLY
, it will contain the difference against the clock and the time ofid
when this method was called. Positive values indicate how lateid
was 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.- Parameters:
id
- TheGstClockID
to wait onjitter
- a pointer that will contain the jitter, can benull
.- Returns:
- the result of the blocking wait.
GST_CLOCK_EARLY
will be returned if the current clock time is past the time ofid
,GST_CLOCK_OK
ifid
was scheduled in time.GST_CLOCK_UNSCHEDULED
ifid
was unscheduled with gst_clock_id_unschedule().
-
idWaitAsync
Registers a callback on the givenGstClockID
id
with the given function and user_data. When passing aGstClockID
with an invalid time to this function, the callback will be called immediately with a time set toGST_CLOCK_TIME_NONE
. The callback will be called when the time ofid
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
- aGstClockID
to wait onfunc
- The callback function- Returns:
- the result of the non blocking wait.
-
addObservation
The timemaster
of the master clock and the timeslave
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 this Clock is recalibrated.If this functions returns
true
,rSquared
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 slavemaster
- a time on the masterrSquared
- a pointer to hold the result- Returns:
true
if enough observations were added to run the regression algorithm.
-
addObservationUnapplied
public boolean addObservationUnapplied(ClockTime slave, ClockTime master, Out<Double> rSquared, @Nullable @Nullable ClockTime internal, @Nullable @Nullable ClockTime external, @Nullable @Nullable ClockTime rateNum, @Nullable @Nullable ClockTime rateDenom) Add a clock observation to the internal slaving algorithm the same as gst_clock_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 gst_clock_set_calibration() with the values, or some modified version of them.
- Parameters:
slave
- a time on the slavemaster
- a time on the masterrSquared
- a pointer to hold the resultinternal
- a location to store the internal timeexternal
- a location to store the external timerateNum
- a location to store the rate numeratorrateDenom
- a location to store the rate denominator- Returns:
true
if enough observations were added to run the regression algorithm.
-
adjustUnlocked
Converts the giveninternal
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 the reverse of gst_clock_unadjust_unlocked().
- Parameters:
internal
- a clock time- Returns:
- the converted time of the clock.
-
adjustWithCalibration
public ClockTime adjustWithCalibration(ClockTime internalTarget, ClockTime cinternal, ClockTime cexternal, ClockTime cnum, ClockTime cdenom) Converts the giveninternalTarget
clock time to the external time, using the passed calibration parameters. This function performs the same calculation as gst_clock_adjust_unlocked() when called using the current calibration parameters, but doesn't ensure a monotonically increasing result as gst_clock_adjust_unlocked() does.Note: The this Clock parameter is unused and can be NULL
- Parameters:
internalTarget
- a clock timecinternal
- a reference internal timecexternal
- a reference external timecnum
- the numerator of the rate of the clock relative to its internal timecdenom
- the denominator of the rate of the clock- Returns:
- the converted time of the clock.
-
getCalibration
public void getCalibration(@Nullable @Nullable ClockTime internal, @Nullable @Nullable ClockTime external, @Nullable @Nullable ClockTime rateNum, @Nullable @Nullable ClockTime rateDenom) Gets the internal rate and reference time of this Clock. See gst_clock_set_calibration() for more information.internal
,external
,rateNum
, andrateDenom
can be leftnull
if the caller is not interested in the values.- Parameters:
internal
- a location to store the internal timeexternal
- a location to store the external timerateNum
- a location to store the rate numeratorrateDenom
- a location to store the rate denominator
-
getInternalTime
Gets the current internal time of the given clock. The time is returned unadjusted for the offset and the rate.- Returns:
- the internal time of the clock. Or
GST_CLOCK_TIME_NONE
when given invalid input.
-
getMaster
Gets the master clock that this Clock is slaved to ornull
when the clock is not slaved to any master clock.- Returns:
- a master
GstClock
ornull
when this clock is not slaved to a master clock.
-
getResolution
Gets the accuracy of the clock. The accuracy of the clock is the granularity of the values returned by gst_clock_get_time().- Returns:
- the resolution of the clock in units of
GstClockTime
.
-
getTime
Gets the current time of the given clock. The time is always monotonically increasing and adjusted according to the current offset and rate.- Returns:
- the time of the clock. Or
GST_CLOCK_TIME_NONE
when given invalid input.
-
getTimeout
Gets the amount of time that master and slave clocks are sampled.- Returns:
- the interval between samples.
-
isSynced
public boolean isSynced()Checks if the clock is currently synced, by looking at whetherClockFlags.NEEDS_STARTUP_SYNC
is set.- Returns:
true
if the clock is currently synced
-
newPeriodicId
Gets an ID from this Clock to trigger a periodic notification. The periodic notifications will start at timestartTime
and will then be fired with the giveninterval
.- Parameters:
startTime
- the requested start timeinterval
- the requested interval- Returns:
- a
GstClockID
that can be used to request the time notification.
-
newSingleShotId
-
periodicIdReinit
Reinitializes the provided periodicid
to the provided start time and interval. Does not modify the reference count.- Parameters:
id
- aGstClockID
startTime
- the requested start timeinterval
- the requested interval- Returns:
true
if the GstClockID could be reinitialized to the providedtime
, elsefalse
.
-
setCalibration
public void setCalibration(ClockTime internal, ClockTime external, ClockTime rateNum, ClockTime rateDenom) Adjusts the rate and time of this Clock. A rate of 1/1 is the normal speed of the clock. Values bigger than 1/1 make the clock go faster.internal
andexternal
are calibration parameters that arrange that gst_clock_get_time() should have beenexternal
at internal timeinternal
. 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.
- Parameters:
internal
- a reference internal timeexternal
- a reference external timerateNum
- the numerator of the rate of the clock relative to its internal timerateDenom
- the denominator of the rate of the clock
-
setMaster
Setsmaster
as the master clock for this Clock. this 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 benull
in which case this 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 masterGstClock
- Returns:
true
if the clock is capable of being slaved to a master clock. Trying to set a master on a clock without theGST_CLOCK_FLAG_CAN_SET_MASTER
flag will make this function returnfalse
.
-
setResolution
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 theGST_CLOCK_FLAG_CAN_SET_RESOLUTION
flag set.- Parameters:
resolution
- The resolution to set- Returns:
- the new resolution of the clock.
-
setSynced
public void setSynced(boolean synced) Sets this Clock to synced and emits theGstClock
::synced signal, and wakes up any thread waiting in gst_clock_wait_for_sync().This function must only be called if
ClockFlags.NEEDS_STARTUP_SYNC
is set on the clock, and is intended to be called by subclasses only.- Parameters:
synced
- if the clock is synced
-
setTimeout
Sets the amount of time, in nanoseconds, to sample master and slave clocks- Parameters:
timeout
- a timeout
-
singleShotIdReinit
Reinitializes the provided single shotid
to the provided time. Does not modify the reference count.- Parameters:
id
- aGstClockID
time
- The requested time.- Returns:
true
if the GstClockID could be reinitialized to the providedtime
, elsefalse
.
-
unadjustUnlocked
Converts the givenexternal
clock time to the internal time of this Clock, using the rate and reference time set with gst_clock_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 gst_clock_adjust_unlocked().
- Parameters:
external
- an external clock time- Returns:
- the internal time of the clock corresponding to
external
.
-
unadjustWithCalibration
public ClockTime unadjustWithCalibration(ClockTime externalTarget, ClockTime cinternal, ClockTime cexternal, ClockTime cnum, ClockTime cdenom) Converts the givenexternalTarget
clock time to the internal time, using the passed calibration parameters. This function performs the same calculation as gst_clock_unadjust_unlocked() when called using the current calibration parameters.Note: The this Clock parameter is unused and can be NULL
- Parameters:
externalTarget
- a clock timecinternal
- a reference internal timecexternal
- a reference external timecnum
- the numerator of the rate of the clock relative to its internal timecdenom
- the denominator of the rate of the clock- Returns:
- the converted time of the clock.
-
waitForSync
Waits until this Clock is synced for reporting the current time. Iftimeout
isGST_CLOCK_TIME_NONE
it will wait forever, otherwise it will time out aftertimeout
nanoseconds.For asynchronous waiting, the
GstClock
::synced signal can be used.This returns immediately with
true
ifClockFlags.NEEDS_STARTUP_SYNC
is not set on the clock, or if the clock is already synced.- Parameters:
timeout
- timeout for waiting orGST_CLOCK_TIME_NONE
- Returns:
true
if waiting was successful, orfalse
on timeout
-
changeResolution
-
unschedule
Unblock a blocking or async wait operation.- Parameters:
entry
- the entry to unschedule
-
wait_
Perform a blocking wait on the givenGstClockEntry
and return the jitter.- Parameters:
entry
- the entry to wait onjitter
- a pointer that will contain the jitter- Returns:
- the result of the blocking wait.
GST_CLOCK_EARLY
will be returned if the current clock time is past the time ofid
,GST_CLOCK_OK
ifid
was scheduled in time.GST_CLOCK_UNSCHEDULED
ifid
was unscheduled with gst_clock_id_unschedule().
-
waitAsync
Perform an asynchronous wait on the givenGstClockEntry
.- Parameters:
entry
- the entry to wait on- Returns:
- the result of the non blocking wait.
-
onSynced
Signaled on clocks withClockFlags.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.
- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitSynced
public void emitSynced(boolean synced) Emits the "synced" signal. SeeonSynced(org.freedesktop.gstreamer.gst.Clock.SyncedCallback)
. -
builder
AClock.Builder
object constructs aClock
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withClock.Builder.build()
.
-