TestClock

GstTestClock is an implementation of #GstClock which has different behaviour compared to #GstSystemClock. Time for #GstSystemClock advances according to the system time, while time for #GstTestClock changes only when gstcheck.test_clock.TestClock.setTime or gstcheck.test_clock.TestClock.advanceTime are called. #GstTestClock provides unit tests with the possibility to precisely advance the time in a deterministic manner, independent of the system time or any other external factors.

Advancing the time of a #GstTestClock

#include <gst/gst.h>
#include <gst/check/gsttestclock.h>

GstClock *clock;
GstTestClock *test_clock;

clock = gst_test_clock_new ();
test_clock = GST_TEST_CLOCK (clock);
GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock)));
gst_test_clock_advance_time ( test_clock, 1 * GST_SECOND);
GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock)));
g_usleep (10 * G_USEC_PER_SEC);
GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock)));
gst_test_clock_set_time (test_clock, 42 * GST_SECOND);
GST_INFO ("Time: %" GST_TIME_FORMAT, GST_TIME_ARGS (gst_clock_get_time (clock)));
...

#GstClock allows for setting up single shot or periodic clock notifications as well as waiting for these notifications synchronously (using gst.clock.Clock.idWait) or asynchronously (using gst.clock.Clock.idWaitAsync or gst.clock.Clock.idWaitAsync). This is used by many GStreamer elements, among them #GstBaseSrc and #GstBaseSink.

#GstTestClock keeps track of these clock notifications. By calling gstcheck.test_clock.TestClock.waitForNextPendingId or gstcheck.test_clock.TestClock.waitForMultiplePendingIds a unit tests may wait for the next one or several clock notifications to be requested. Additionally unit tests may release blocked waits in a controlled fashion by calling gstcheck.test_clock.TestClock.processNextClockId. This way a unit test can control the inaccuracy (jitter) of clock notifications, since the test can decide to release blocked waits when the clock time has advanced exactly to, or past, the requested clock notification time.

There are also interfaces for determining if a notification belongs to a #GstTestClock or not, as well as getting the number of requested clock notifications so far.

N.B.: When a unit test waits for a certain amount of clock notifications to be requested in gstcheck.test_clock.TestClock.waitForNextPendingId or gstcheck.test_clock.TestClock.waitForMultiplePendingIds then these functions may block for a long time. If they block forever then the expected clock notifications were never requested from #GstTestClock, and so the assumptions in the code of the unit test are wrong. The unit test case runner in gstcheck is expected to catch these cases either by the default test case timeout or the one set for the unit test by calling tcase_set_timeout\(\).

The sample code below assumes that the element under test will delay a buffer pushed on the source pad by some latency until it arrives on the sink pad. Moreover it is assumed that the element will at some point call gst.clock.Clock.idWait to synchronously wait for a specific time. The first buffer sent will arrive exactly on time only delayed by the latency. The second buffer will arrive a little late (7ms) due to simulated jitter in the clock notification.

Demonstration of how to work with clock notifications and #GstTestClock

1 #include <gst/gst.h>
2 #include <gst/check/gstcheck.h>
3 #include <gst/check/gsttestclock.h>
4 
5 GstClockTime latency;
6 GstElement *element;
7 GstPad *srcpad;
8 GstClock *clock;
9 GstTestClock *test_clock;
10 GstBuffer buf;
11 GstClockID pending_id;
12 GstClockID processed_id;
13 
14 latency = 42 * GST_MSECOND;
15 element = create_element (latency, ...);
16 srcpad = get_source_pad (element);
17 
18 clock = gst_test_clock_new ();
19 test_clock = GST_TEST_CLOCK (clock);
20 gst_element_set_clock (element, clock);
21 
22 GST_INFO ("Set time, create and push the first buffer\n");
23 gst_test_clock_set_time (test_clock, 0);
24 buf = create_test_buffer (gst_clock_get_time (clock), ...);
25 gst_assert_cmpint (gst_pad_push (srcpad, buf), ==, GST_FLOW_OK);
26 
27 GST_INFO ("Block until element is waiting for a clock notification\n");
28 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
29 GST_INFO ("Advance to the requested time of the clock notification\n");
30 gst_test_clock_advance_time (test_clock, latency);
31 GST_INFO ("Release the next blocking wait and make sure it is the one from element\n");
32 processed_id = gst_test_clock_process_next_clock_id (test_clock);
33 g_assert (processed_id == pending_id);
34 g_assert_cmpint (GST_CLOCK_ENTRY_STATUS (processed_id), ==, GST_CLOCK_OK);
35 gst_clock_id_unref (pending_id);
36 gst_clock_id_unref (processed_id);
37 
38 GST_INFO ("Validate that element produced an output buffer and check its timestamp\n");
39 g_assert_cmpint (get_number_of_output_buffer (...), ==, 1);
40 buf = get_buffer_pushed_by_element (element, ...);
41 g_assert_cmpint (GST_BUFFER_TIMESTAMP (buf), ==, latency);
42 gst_buffer_unref (buf);
43 GST_INFO ("Check that element does not wait for any clock notification\n");
44 g_assert (!gst_test_clock_peek_next_pending_id (test_clock, NULL));
45 
46 GST_INFO ("Set time, create and push the second buffer\n");
47 gst_test_clock_advance_time (test_clock, 10 * GST_SECOND);
48 buf = create_test_buffer (gst_clock_get_time (clock), ...);
49 gst_assert_cmpint (gst_pad_push (srcpad, buf), ==, GST_FLOW_OK);
50 
51 GST_INFO ("Block until element is waiting for a new clock notification\n");
52 (gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
53 GST_INFO ("Advance past 7ms beyond the requested time of the clock notification\n");
54 gst_test_clock_advance_time (test_clock, latency + 7 * GST_MSECOND);
55 GST_INFO ("Release the next blocking wait and make sure it is the one from element\n");
56 processed_id = gst_test_clock_process_next_clock_id (test_clock);
57 g_assert (processed_id == pending_id);
58 g_assert_cmpint (GST_CLOCK_ENTRY_STATUS (processed_id), ==, GST_CLOCK_OK);
59 gst_clock_id_unref (pending_id);
60 gst_clock_id_unref (processed_id);
61 
62 GST_INFO ("Validate that element produced an output buffer and check its timestamp\n");
63 g_assert_cmpint (get_number_of_output_buffer (...), ==, 1);
64 buf = get_buffer_pushed_by_element (element, ...);
65 g_assert_cmpint (GST_BUFFER_TIMESTAMP (buf), ==,
66     10 * GST_SECOND + latency + 7 * GST_MSECOND);
67 gst_buffer_unref (buf);
68 GST_INFO ("Check that element does not wait for any clock notification\n");
69 g_assert (!gst_test_clock_peek_next_pending_id (test_clock, NULL));
70 ...

Since #GstTestClock is only supposed to be used in unit tests it calls g_assert(), g_assert_cmpint() or g_assert_cmpuint() to validate all function arguments. This will highlight any issues with the unit test code itself.

Constructors

this
this()

Creates a new test clock with its time set to zero.

Members

Functions

advanceTime
void advanceTime(gst.types.ClockTimeDiff delta)

Advances the time of the test_clock by the amount given by delta. The time of test_clock is monotonically increasing, therefore providing a delta which is negative or zero is a programming error.

crank
bool crank()

A "crank" consists of three steps: 1: Wait for a #GstClockID to be registered with the #GstTestClock. 2: Advance the #GstTestClock to the time the #GstClockID is waiting, unless the clock time is already passed the clock id (Since: 1.18). 3: Release the #GstClockID wait. A "crank" can be though of as the notion of manually driving the clock forward to its next logical step.

getNextEntryTime
gst.types.ClockTime getNextEntryTime()

Retrieve the requested time for the next pending clock notification.

hasId
bool hasId(gst.types.ClockID id)

Checks whether test_clock was requested to provide the clock notification given by id.

peekIdCount
uint peekIdCount()

Determine the number of pending clock notifications that have been requested from the test_clock.

peekNextPendingId
bool peekNextPendingId(gst.types.ClockID pendingId)

Determines if the pending_id is the next clock notification scheduled to be triggered given the current time of the test_clock.

processId
bool processId(gst.types.ClockID pendingId)

Processes and releases the pending ID.

processIdList
uint processIdList(gst.types.ClockID[] pendingList)

Processes and releases the pending IDs in the list.

processNextClockId
gst.types.ClockID processNextClockId()

MT safe.

setTime
void setTime(gst.types.ClockTime newTime)

Sets the time of test_clock to the time given by new_time. The time of test_clock is monotonically increasing, therefore providing a new_time which is earlier or equal to the time of the clock as given by gst.clock.Clock.getTime is a programming error.

timedWaitForMultiplePendingIds
bool timedWaitForMultiplePendingIds(uint count, uint timeoutMs, gst.types.ClockID[] pendingList)

Blocks until at least count clock notifications have been requested from test_clock, or the timeout expires.

waitForMultiplePendingIds
void waitForMultiplePendingIds(uint count, gst.types.ClockID[] pendingList)

Blocks until at least count clock notifications have been requested from test_clock. There is no timeout for this wait, see the main description of #GstTestClock.

waitForNextPendingId
void waitForNextPendingId(gst.types.ClockID pendingId)

Waits until a clock notification is requested from test_clock. There is no timeout for this wait, see the main description of #GstTestClock. A reference to the pending clock notification is stored in pending_id.

waitForPendingIdCount
void waitForPendingIdCount(uint count)

Blocks until at least count clock notifications have been requested from test_clock. There is no timeout for this wait, see the main description of #GstTestClock.

Static functions

idListGetLatestTime
gst.types.ClockTime idListGetLatestTime(gst.types.ClockID[] pendingList)

Finds the latest time inside the list.

newWithStartTime
gstcheck.test_clock.TestClock newWithStartTime(gst.types.ClockTime startTime)

Creates a new test clock with its time set to the specified time.

Inherited Members

From Clock

idCompareFunc
int idCompareFunc(const(void)* id1, const(void)* id2)

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

idGetClock
gst.clock.Clock idGetClock(gst.types.ClockID id)

This function returns the underlying clock.

idGetTime
gst.types.ClockTime idGetTime(gst.types.ClockID id)

Gets the time of the clock ID

idRef
gst.types.ClockID idRef(gst.types.ClockID id)

Increases the refcount of given id.

idUnref
void idUnref(gst.types.ClockID id)

Unrefs given id. When the refcount reaches 0 the #GstClockID will be freed.

idUnschedule
void idUnschedule(gst.types.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.

idUsesClock
bool idUsesClock(gst.types.ClockID id, gst.clock.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.

idWait
gst.types.ClockReturn idWait(gst.types.ClockID id, gst.types.ClockTimeDiff jitter)

Performs a blocking wait on id. id should have been created with gst.clock.Clock.newSingleShotId or gst.clock.Clock.newPeriodicId and should not have been unscheduled with a call to gst.clock.Clock.idUnschedule.

idWaitAsync
gst.types.ClockReturn idWaitAsync(gst.types.ClockID id, gst.types.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.

addObservation
bool addObservation(gst.types.ClockTime slave, gst.types.ClockTime master, double rSquared)

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.

addObservationUnapplied
bool addObservationUnapplied(gst.types.ClockTime slave, gst.types.ClockTime master, double rSquared, gst.types.ClockTime internal, gst.types.ClockTime external, gst.types.ClockTime rateNum, gst.types.ClockTime rateDenom)

Add a clock observation to the internal slaving algorithm the same as gst.clock.Clock.addObservation, and return the result of the master clock estimation, without updating the internal calibration.

adjustUnlocked
gst.types.ClockTime adjustUnlocked(gst.types.ClockTime internal)

Converts the given internal clock time to the external time, adjusting for the rate and reference time set with gst.clock.Clock.setCalibration 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.

adjustWithCalibration
gst.types.ClockTime adjustWithCalibration(gst.types.ClockTime internalTarget, gst.types.ClockTime cinternal, gst.types.ClockTime cexternal, gst.types.ClockTime cnum, gst.types.ClockTime cdenom)

Converts the given internal_target clock time to the external time, using the passed calibration parameters. This function performs the same calculation as gst.clock.Clock.adjustUnlocked when called using the current calibration parameters, but doesn't ensure a monotonically increasing result as gst.clock.Clock.adjustUnlocked does.

getCalibration
void getCalibration(gst.types.ClockTime internal, gst.types.ClockTime external, gst.types.ClockTime rateNum, gst.types.ClockTime rateDenom)

Gets the internal rate and reference time of clock. See gst.clock.Clock.setCalibration for more information.

getInternalTime
gst.types.ClockTime getInternalTime()

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

getMaster
gst.clock.Clock getMaster()

Gets the master clock that clock is slaved to or null when the clock is not slaved to any master clock.

getResolution
gst.types.ClockTime getResolution()

Gets the accuracy of the clock. The accuracy of the clock is the granularity of the values returned by gst.clock.Clock.getTime.

getTime
gst.types.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.

getTimeout
gst.types.ClockTime getTimeout()

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

isSynced
bool isSynced()

Checks if the clock is currently synced, by looking at whether gst.types.ClockFlags.NeedsStartupSync is set.

newPeriodicId
gst.types.ClockID newPeriodicId(gst.types.ClockTime startTime, gst.types.ClockTime interval)

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.

newSingleShotId
gst.types.ClockID newSingleShotId(gst.types.ClockTime time)

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

periodicIdReinit
bool periodicIdReinit(gst.types.ClockID id, gst.types.ClockTime startTime, gst.types.ClockTime interval)

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

setCalibration
void setCalibration(gst.types.ClockTime internal, gst.types.ClockTime external, gst.types.ClockTime rateNum, gst.types.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.

setMaster
bool setMaster(gst.clock.Clock master)

Sets master as the master clock for clock. clock will be automatically calibrated so that gst.clock.Clock.getTime reports the same time as the master clock.

setResolution
gst.types.ClockTime setResolution(gst.types.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.

setSynced
void setSynced(bool synced)

Sets clock to synced and emits the #GstClock::synced signal, and wakes up any thread waiting in gst.clock.Clock.waitForSync.

setTimeout
void setTimeout(gst.types.ClockTime timeout)

Sets the amount of time, in nanoseconds, to sample master and slave clocks

singleShotIdReinit
bool singleShotIdReinit(gst.types.ClockID id, gst.types.ClockTime time)

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

unadjustUnlocked
gst.types.ClockTime unadjustUnlocked(gst.types.ClockTime external)

Converts the given external clock time to the internal time of clock, using the rate and reference time set with gst.clock.Clock.setCalibration. This function should be called with the clock's OBJECT_LOCK held and is mainly used by clock subclasses.

unadjustWithCalibration
gst.types.ClockTime unadjustWithCalibration(gst.types.ClockTime externalTarget, gst.types.ClockTime cinternal, gst.types.ClockTime cexternal, gst.types.ClockTime cnum, gst.types.ClockTime cdenom)

Converts the given external_target clock time to the internal time, using the passed calibration parameters. This function performs the same calculation as gst.clock.Clock.unadjustUnlocked when called using the current calibration parameters.

waitForSync
bool waitForSync(gst.types.ClockTime timeout)

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

connectSynced
ulong connectSynced(T callback, Flag!"After" after)

Connect to Synced signal.