Class Clock

All Implemented Interfaces:
Proxy
Direct Known Subclasses:
Clock.Clock$Impl, SystemClock

@Generated("org.javagi.JavaGI") public abstract class Clock extends GstObject

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

  • Constructor Details

    • Clock

      public Clock(MemorySegment address)
      Create a Clock instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Clock

      public Clock()
      Create a new Clock.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Clock class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Clock asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GstObject
      Returns:
      the instance as if it were its parent type
    • idCompareFunc

      public static int idCompareFunc(@Nullable MemorySegment id1, @Nullable MemorySegment id2)
      Compares the two GstClockID instances. This function can be used as a GCompareFunc when sorting ids.
      Parameters:
      id1 - A GstClockID
      id2 - A GstClockID to compare with
      Returns:
      negative value if a < b; zero if a = b; positive value if a > b
    • idGetClock

      public static @Nullable Clock idGetClock(@Nullable ClockID id)
      This function returns the underlying clock.
      Parameters:
      id - a GstClockID
      Returns:
      a GstClock or null when the underlying clock has been freed.
      Since:
      1.16
    • idGetTime

      public static ClockTime idGetTime(@Nullable ClockID id)
      Gets the time of the clock ID
      Parameters:
      id - The GstClockID to query
      Returns:
      the time of the given clock id.
    • idRef

      public static @Nullable ClockID idRef(@Nullable ClockID id)
      Increases the refcount of given id.
      Parameters:
      id - The GstClockID to ref
      Returns:
      The same GstClockID with increased refcount.
    • idUnref

      public static void idUnref(@Nullable ClockID id)
      Unrefs given id. When the refcount reaches 0 the GstClockID will be freed.
      Parameters:
      id - The GstClockID to unref
    • idUnschedule

      public static void idUnschedule(@Nullable ClockID id)
      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 GstClockID.
      Parameters:
      id - The id to unschedule
    • idUsesClock

      public static boolean idUsesClock(@Nullable ClockID id, Clock clock)
      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.
      Parameters:
      id - a GstClockID to check
      clock - a GstClock to compare against
      Returns:
      whether the clock id uses the same underlying GstClock clock.
      Since:
      1.16
    • idWait

      public static ClockReturn idWait(@Nullable ClockID id, @Nullable ClockTimeDiff jitter)

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

      Parameters:
      id - The GstClockID to wait on
      jitter - a pointer that will contain the jitter, can be null.
      Returns:
      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().
    • idWaitAsync

      public static ClockReturn idWaitAsync(@Nullable ClockID id, @Nullable ClockCallback func)

      Registers a callback on the given GstClockID id with the given function and user_data. When passing a GstClockID with an invalid time to this function, the callback will be called immediately with a time set to GST_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 GstClockID to wait on
      func - The callback function
      Returns:
      the result of the non blocking wait.
    • addObservation

      public boolean addObservation(ClockTime observationInternal, ClockTime observationExternal, Out<Double> rSquared)

      The time observationExternal of the external or master clock and the time observationInternal of the internal or 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:
      observationInternal - a time on the internal clock
      observationExternal - a time on the external clock
      rSquared - a pointer to hold the result
      Returns:
      true if enough observations were added to run the regression algorithm.
    • addObservationUnapplied

      public boolean addObservationUnapplied(ClockTime observationInternal, ClockTime observationExternal, 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 external or 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:
      observationInternal - a time on the internal clock
      observationExternal - a time on the external clock
      rSquared - a pointer to hold the result
      internal - a location to store the internal time
      external - a location to store the external time
      rateNum - a location to store the rate numerator
      rateDenom - a location to store the rate denominator
      Returns:
      true if enough observations were added to run the regression algorithm.
      Since:
      1.6
    • adjustUnlocked

      public ClockTime adjustUnlocked(ClockTime internal)

      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 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 given internalTarget 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 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
      Returns:
      the converted time of the clock.
      Since:
      1.6
    • getCalibration

      public void getCalibration(@Nullable ClockTime internal, @Nullable ClockTime external, @Nullable ClockTime rateNum, @Nullable ClockTime rateDenom)

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

      internal, external, rateNum, and rateDenom can be left null if the caller is not interested in the values.

      Parameters:
      internal - a location to store the internal time
      external - a location to store the external time
      rateNum - a location to store the rate numerator
      rateDenom - a location to store the rate denominator
    • getInternalTime

      public ClockTime 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

      public @Nullable Clock getMaster()
      Gets the master clock that this Clock is slaved to or null when the clock is not slaved to any master clock.
      Returns:
      a master GstClock or null when this clock is not slaved to a master clock.
    • getResolution

      public ClockTime 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

      public ClockTime 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

      public ClockTime 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 whether ClockFlags.NEEDS_STARTUP_SYNC is set.
      Returns:
      true if the clock is currently synced
      Since:
      1.6
    • newPeriodicId

      public @Nullable ClockID newPeriodicId(ClockTime startTime, ClockTime interval)
      Gets an ID from this Clock to trigger a periodic notification. The periodic notifications will start at time startTime and will then be fired with the given interval.
      Parameters:
      startTime - the requested start time
      interval - the requested interval
      Returns:
      a GstClockID that can be used to request the time notification.
    • newSingleShotId

      public @Nullable ClockID newSingleShotId(ClockTime time)
      Gets a GstClockID from this Clock to trigger a single shot notification at the requested time.
      Parameters:
      time - the requested time
      Returns:
      a GstClockID that can be used to request the time notification.
    • periodicIdReinit

      public boolean periodicIdReinit(@Nullable ClockID id, ClockTime startTime, ClockTime interval)
      Reinitializes the provided periodic id to the provided start time and interval. Does not modify the reference count.
      Parameters:
      id - a GstClockID
      startTime - the requested start time
      interval - the requested interval
      Returns:
      true if the GstClockID could be reinitialized to the provided time, else false.
    • setCalibration

      public void setCalibration(ClockTime internal, ClockTime external, ClockTime rateNum, ClockTime rateDenom)

      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.

      Parameters:
      internal - a reference internal time
      external - a reference external time
      rateNum - the numerator of the rate of the clock relative to its internal time
      rateDenom - the denominator of the rate of the clock
    • setMaster

      public boolean setMaster(@Nullable Clock master)

      Sets master as the master clock for 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 be null 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 master GstClock
      Returns:
      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.
    • setResolution

      public ClockTime setResolution(ClockTime resolution)
      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
      Returns:
      the new resolution of the clock.
    • setSynced

      public void setSynced(boolean synced)

      Sets this Clock to synced and emits the GstClock::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
      Since:
      1.6
    • setTimeout

      public void setTimeout(ClockTime timeout)
      Sets the amount of time, in nanoseconds, to sample master and slave clocks
      Parameters:
      timeout - a timeout
    • singleShotIdReinit

      public boolean singleShotIdReinit(@Nullable ClockID id, ClockTime time)
      Reinitializes the provided single shot id to the provided time. Does not modify the reference count.
      Parameters:
      id - a GstClockID
      time - The requested time.
      Returns:
      true if the GstClockID could be reinitialized to the provided time, else false.
    • unadjustUnlocked

      public ClockTime unadjustUnlocked(ClockTime external)

      Converts the given external clock time to the internal time of 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 given externalTarget 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 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
      Returns:
      the converted time of the clock.
      Since:
      1.8
    • waitForSync

      public boolean waitForSync(ClockTime timeout)

      Waits until this Clock is synced for reporting the current time. If timeout is GST_CLOCK_TIME_NONE it will wait forever, otherwise it will time out after timeout nanoseconds.

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

      This returns immediately with true if ClockFlags.NEEDS_STARTUP_SYNC is not set on the clock, or if the clock is already synced.

      Parameters:
      timeout - timeout for waiting or GST_CLOCK_TIME_NONE
      Returns:
      true if waiting was successful, or false on timeout
      Since:
      1.6
    • changeResolution

      protected ClockTime changeResolution(ClockTime oldResolution, ClockTime newResolution)
      Change the resolution of the clock. Not all values might be acceptable.
      Parameters:
      oldResolution - the previous resolution
      newResolution - the new resolution
      Returns:
      the new resolution
    • unschedule

      protected void unschedule(ClockEntry entry)
      Unblock a blocking or async wait operation.
      Parameters:
      entry - the entry to unschedule
    • wait_

      protected ClockReturn wait_(ClockEntry entry, @Nullable ClockTimeDiff jitter)
      Perform a blocking wait on the given GstClockEntry and return the jitter.
      Parameters:
      entry - the entry to wait on
      jitter - 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 of id, GST_CLOCK_OK if id was scheduled in time. GST_CLOCK_UNSCHEDULED if id was unscheduled with gst_clock_id_unschedule().
    • waitAsync

      protected ClockReturn waitAsync(ClockEntry entry)
      Perform an asynchronous wait on the given GstClockEntry.
      Parameters:
      entry - the entry to wait on
      Returns:
      the result of the non blocking wait.
    • onSynced

      Signaled on clocks with ClockFlags.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
      Since:
      1.6
      See Also:
    • emitSynced

      public void emitSynced(boolean synced)
      Emits the "synced" signal. See onSynced(Clock.SyncedCallback).