2 * Unit test for a deterministic clock for Gstreamer unit tests
4 * Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
5 * Copyright (C) 2012 Sebastian Rasmussen <sebastian.rasmussen@axis.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include <gst/check/gstcheck.h>
24 #include <gst/check/gsttestclock.h>
28 GstTestClock *test_clock;
30 GstClockTime reference;
31 } GtuClockWaitContext;
36 GstClockTimeDiff jitter;
37 } SyncClockWaitContext;
39 #define assert_pending_id(pending_id, id, type, time) \
41 GstClockEntry *entry = GST_CLOCK_ENTRY (pending_id); \
42 g_assert (entry == (id)); \
43 g_assert (GST_CLOCK_ENTRY_TYPE (entry) == (type)); \
44 g_assert_cmpuint (GST_CLOCK_ENTRY_TIME (entry), ==, (time)); \
47 #define assert_processed_id(processed_id, id, type, time) \
49 GstClockEntry *entry = GST_CLOCK_ENTRY (processed_id); \
50 g_assert (entry == (id)); \
51 g_assert (GST_CLOCK_ENTRY_TYPE (entry) == (type)); \
52 g_assert_cmpuint (GST_CLOCK_ENTRY_STATUS (entry), ==, (time)); \
55 static gpointer test_wait_pending_single_shot_id_sync_worker (gpointer data);
56 static gpointer test_wait_pending_single_shot_id_async_worker (gpointer data);
57 static gpointer test_wait_pending_periodic_id_waiter_thread (gpointer data);
58 static gboolean test_async_wait_cb (GstClock * clock, GstClockTime time,
59 GstClockID id, gpointer user_data);
61 static GtuClockWaitContext *gst_test_util_wait_for_clock_id_begin (GstTestClock
62 * clock, GstClockID id, GstClockTimeDiff * jitter);
63 static GstClockReturn gst_test_util_wait_for_clock_id_end (GtuClockWaitContext *
66 gst_test_util_clock_wait_context_has_completed (GtuClockWaitContext * wait_ctx);
69 test_wait_pending_single_shot_id_sync_worker (gpointer data)
71 SyncClockWaitContext *ctx = data;
73 gst_clock_id_wait (ctx->clock_id, &ctx->jitter);
79 test_wait_pending_single_shot_id_async_worker (gpointer data)
81 GstClockID clock_id = data;
83 g_usleep (G_USEC_PER_SEC / 10);
84 gst_clock_id_wait_async (clock_id, test_async_wait_cb, NULL, NULL);
90 test_wait_pending_periodic_id_waiter_thread (gpointer data)
92 GstClockID clock_id = data;
93 gst_clock_id_wait (clock_id, NULL);
98 test_async_wait_cb (GstClock * clock,
99 GstClockTime time, GstClockID id, gpointer user_data)
102 gboolean *wait_complete = user_data;
104 if (wait_complete != NULL)
105 *wait_complete = TRUE;
110 static GtuClockWaitContext *
111 gst_test_util_wait_for_clock_id_begin (GstTestClock * test_clock, GstClockID id,
112 GstClockTimeDiff * jitter)
114 GtuClockWaitContext *wait_ctx;
116 wait_ctx = g_slice_new (GtuClockWaitContext);
117 wait_ctx->test_clock = gst_object_ref (test_clock);
118 wait_ctx->reference = gst_clock_get_time (GST_CLOCK (wait_ctx->test_clock));
119 wait_ctx->id = gst_clock_id_ref (id);
122 GstClockEntry *entry = GST_CLOCK_ENTRY (wait_ctx->id);
123 GstClockTime requested = GST_CLOCK_ENTRY_TIME (entry);
124 GstClockTime reference = wait_ctx->reference;
126 *jitter = GST_CLOCK_DIFF (requested, reference);
129 if (!gst_test_clock_has_id (wait_ctx->test_clock, wait_ctx->id)) {
130 GstClockClass *klass = GST_CLOCK_GET_CLASS (wait_ctx->test_clock);
131 GstClock *clock = GST_CLOCK (wait_ctx->test_clock);
132 g_assert (klass->wait_async (clock, wait_ctx->id) == GST_CLOCK_OK);
135 g_assert (gst_test_clock_has_id (wait_ctx->test_clock, wait_ctx->id));
136 g_assert_cmpint (gst_test_clock_peek_id_count (wait_ctx->test_clock), >, 0);
141 static GstClockReturn
142 gst_test_util_wait_for_clock_id_end (GtuClockWaitContext * wait_ctx)
144 GstClockReturn status = GST_CLOCK_ERROR;
145 GstClockEntry *entry = GST_CLOCK_ENTRY (wait_ctx->id);
147 if (G_UNLIKELY (GST_CLOCK_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED)) {
148 status = GST_CLOCK_UNSCHEDULED;
150 GstClockTime requested = GST_CLOCK_ENTRY_TIME (entry);
151 GstClockTimeDiff diff;
153 g_assert (gst_test_clock_has_id (wait_ctx->test_clock, wait_ctx->id));
155 diff = GST_CLOCK_DIFF (requested, wait_ctx->reference);
158 status = GST_CLOCK_EARLY;
160 status = GST_CLOCK_OK;
163 g_atomic_int_set (&GST_CLOCK_ENTRY_STATUS (entry), status);
166 if (GST_CLOCK_ENTRY_TYPE (entry) == GST_CLOCK_ENTRY_SINGLE) {
167 GstClockClass *klass = GST_CLOCK_GET_CLASS (wait_ctx->test_clock);
168 GstClock *clock = GST_CLOCK (wait_ctx->test_clock);
170 klass->unschedule (clock, wait_ctx->id);
171 g_assert (!gst_test_clock_has_id (wait_ctx->test_clock, wait_ctx->id));
173 GST_CLOCK_ENTRY_TIME (entry) += GST_CLOCK_ENTRY_INTERVAL (entry);
174 g_assert (gst_test_clock_has_id (wait_ctx->test_clock, wait_ctx->id));
177 gst_clock_id_unref (wait_ctx->id);
178 gst_object_unref (wait_ctx->test_clock);
179 g_slice_free (GtuClockWaitContext, wait_ctx);
185 gst_test_util_clock_wait_context_has_completed (GtuClockWaitContext * wait_ctx)
187 GstClock *clock = GST_CLOCK (wait_ctx->test_clock);
188 GstClockEntry *entry = GST_CLOCK_ENTRY (wait_ctx->id);
189 GstClockTime requested = GST_CLOCK_ENTRY_TIME (entry);
190 GstClockTime now = gst_clock_get_time (clock);
192 return requested < now;
195 GST_START_TEST (test_object_flags)
197 GstClock *clock = gst_test_clock_new ();
198 g_assert (GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC));
199 g_assert (GST_OBJECT_FLAG_IS_SET (clock, GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC));
200 g_assert (GST_OBJECT_FLAG_IS_SET (clock,
201 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC));
202 g_assert (GST_OBJECT_FLAG_IS_SET (clock,
203 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC));
204 gst_object_unref (clock);
209 GST_START_TEST (test_resolution_query)
211 GstClock *clock = gst_test_clock_new ();
212 g_assert_cmpuint (gst_clock_get_resolution (clock), ==, 1);
213 gst_object_unref (clock);
218 GST_START_TEST (test_start_time)
223 clock = gst_test_clock_new ();
224 g_assert_cmpuint (gst_clock_get_time (clock), ==, 0);
225 g_object_get (clock, "start-time", &start_time, NULL);
226 g_assert_cmpuint (start_time, ==, 0);
227 gst_object_unref (clock);
229 clock = gst_test_clock_new_with_start_time (GST_SECOND);
230 g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND);
231 g_object_get (clock, "start-time", &start_time, NULL);
232 g_assert_cmpuint (start_time, ==, GST_SECOND);
233 gst_object_unref (clock);
238 GST_START_TEST (test_set_time)
240 GstClock *clock = gst_test_clock_new_with_start_time (GST_SECOND);
241 gst_test_clock_set_time (GST_TEST_CLOCK (clock), GST_SECOND);
242 g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND);
243 gst_test_clock_set_time (GST_TEST_CLOCK (clock), GST_SECOND + 1);
244 g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND + 1);
245 gst_object_unref (clock);
250 GST_START_TEST (test_advance_time)
252 GstClock *clock = gst_test_clock_new_with_start_time (GST_SECOND);
253 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 0);
254 g_assert_cmpuint (gst_clock_get_time (clock), ==, GST_SECOND);
255 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 42 * GST_MSECOND);
256 g_assert_cmpuint (gst_clock_get_time (clock), ==,
257 GST_SECOND + (42 * GST_MSECOND));
258 gst_object_unref (clock);
263 GST_START_TEST (test_wait_synchronous_no_timeout)
266 GstTestClock *test_clock;
268 GThread *worker_thread;
269 GstClockID pending_id;
270 GstClockID processed_id;
271 SyncClockWaitContext context;
273 clock = gst_test_clock_new_with_start_time (GST_SECOND);
274 test_clock = GST_TEST_CLOCK (clock);
276 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND - 1);
277 context.clock_id = gst_clock_id_ref (clock_id);
280 g_thread_new ("worker_thread",
281 test_wait_pending_single_shot_id_sync_worker, &context);
282 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
283 assert_pending_id (pending_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
285 gst_clock_id_unref (pending_id);
286 processed_id = gst_test_clock_process_next_clock_id (test_clock);
287 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
289 gst_clock_id_unref (processed_id);
290 g_thread_join (worker_thread);
291 g_assert_cmpuint (context.jitter, ==, 1);
292 gst_clock_id_unref (context.clock_id);
293 gst_clock_id_unref (clock_id);
295 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND);
296 context.clock_id = gst_clock_id_ref (clock_id);
299 g_thread_new ("worker_thread",
300 test_wait_pending_single_shot_id_sync_worker, &context);
301 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
302 assert_pending_id (pending_id, clock_id, GST_CLOCK_ENTRY_SINGLE, GST_SECOND);
303 gst_clock_id_unref (pending_id);
304 processed_id = gst_test_clock_process_next_clock_id (test_clock);
305 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
307 gst_clock_id_unref (processed_id);
308 g_thread_join (worker_thread);
309 g_assert_cmpuint (context.jitter, ==, 0);
310 gst_clock_id_unref (context.clock_id);
311 gst_clock_id_unref (clock_id);
313 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND + 1);
314 context.clock_id = gst_clock_id_ref (clock_id);
317 g_thread_new ("worker_thread",
318 test_wait_pending_single_shot_id_sync_worker, &context);
319 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
320 assert_pending_id (pending_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
322 gst_clock_id_unref (pending_id);
323 processed_id = gst_test_clock_process_next_clock_id (test_clock);
324 g_assert (processed_id == NULL);
325 gst_test_clock_advance_time (test_clock, 1);
326 processed_id = gst_test_clock_process_next_clock_id (test_clock);
327 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
329 gst_clock_id_unref (processed_id);
330 g_thread_join (worker_thread);
331 g_assert_cmpuint (context.jitter, ==, -1);
332 gst_clock_id_unref (context.clock_id);
333 gst_clock_id_unref (clock_id);
335 gst_object_unref (clock);
340 GST_START_TEST (test_wait_pending_single_shot_id)
343 GstTestClock *test_clock;
345 GstClockID processed_id;
346 GThread *worker_thread;
347 GstClockID pending_id;
349 clock = gst_test_clock_new_with_start_time (GST_SECOND);
350 test_clock = GST_TEST_CLOCK (clock);
352 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND);
353 gst_clock_id_wait_async (clock_id, test_async_wait_cb, NULL, NULL);
354 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
355 assert_pending_id (pending_id, clock_id, GST_CLOCK_ENTRY_SINGLE, GST_SECOND);
356 gst_clock_id_unref (pending_id);
357 processed_id = gst_test_clock_process_next_clock_id (test_clock);
358 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
360 gst_clock_id_unref (processed_id);
361 gst_clock_id_unref (clock_id);
363 clock_id = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
365 g_thread_new ("worker_thread",
366 test_wait_pending_single_shot_id_async_worker, clock_id);
367 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
368 assert_pending_id (pending_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
370 gst_clock_id_unref (pending_id);
371 g_thread_join (worker_thread);
372 gst_clock_id_unref (clock_id);
374 clock_id = gst_clock_new_single_shot_id (clock, 3 * GST_SECOND);
376 g_thread_new ("worker_thread",
377 test_wait_pending_single_shot_id_async_worker, clock_id);
378 gst_test_clock_wait_for_next_pending_id (test_clock, NULL);
379 g_thread_join (worker_thread);
380 gst_clock_id_unref (clock_id);
382 gst_object_unref (clock);
387 GST_START_TEST (test_wait_pending_periodic_id)
390 GstTestClock *test_clock;
392 GstClockID processed_id;
394 clock = gst_test_clock_new_with_start_time (GST_SECOND);
395 test_clock = GST_TEST_CLOCK (clock);
396 clock_id = gst_clock_new_periodic_id (clock, GST_SECOND, GST_MSECOND);
399 GThread *waiter_thread;
402 g_thread_new ("waiter_thread",
403 test_wait_pending_periodic_id_waiter_thread, clock_id);
405 gst_test_clock_wait_for_next_pending_id (test_clock, NULL);
406 gst_test_clock_set_time (test_clock, GST_SECOND);
407 processed_id = gst_test_clock_process_next_clock_id (test_clock);
408 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_PERIODIC,
410 gst_clock_id_unref (processed_id);
412 g_thread_join (waiter_thread);
417 GThread *waiter_thread;
419 for (i = 0; i < 3; i++) {
420 g_assert (!gst_test_clock_peek_next_pending_id (test_clock, NULL));
421 g_usleep (G_USEC_PER_SEC / 10 / 10);
425 g_thread_new ("waiter_thread",
426 test_wait_pending_periodic_id_waiter_thread, clock_id);
428 gst_test_clock_wait_for_next_pending_id (test_clock, NULL);
429 gst_clock_id_unschedule (clock_id);
431 g_thread_join (waiter_thread);
434 gst_clock_id_unref (clock_id);
435 gst_object_unref (clock);
440 GST_START_TEST (test_single_shot_sync_past)
443 GstTestClock *test_clock;
445 GstClockTimeDiff jitter;
446 GtuClockWaitContext *wait_ctx;
448 clock = gst_test_clock_new_with_start_time (GST_SECOND);
449 test_clock = GST_TEST_CLOCK (clock);
451 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND - 1);
453 gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, &jitter);
454 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx) == GST_CLOCK_EARLY);
455 g_assert_cmpint (jitter, ==, 1);
456 gst_clock_id_unref (clock_id);
458 gst_object_unref (clock);
463 GST_START_TEST (test_single_shot_sync_present)
466 GstTestClock *test_clock;
468 GstClockTimeDiff jitter;
469 GtuClockWaitContext *wait_ctx;
471 clock = gst_test_clock_new_with_start_time (GST_SECOND);
472 test_clock = GST_TEST_CLOCK (clock);
474 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND);
476 gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, &jitter);
477 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx) == GST_CLOCK_OK);
478 g_assert_cmpint (jitter, ==, 0);
479 gst_clock_id_unref (clock_id);
481 gst_object_unref (clock);
486 GST_START_TEST (test_single_shot_sync_future)
489 GstTestClock *test_clock;
491 GstClockTimeDiff jitter;
492 GtuClockWaitContext *wait_ctx;
494 clock = gst_test_clock_new_with_start_time (GST_SECOND);
495 test_clock = GST_TEST_CLOCK (clock);
497 clock_id = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
499 gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, &jitter);
500 gst_test_clock_advance_time (test_clock, GST_SECOND);
501 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx) == GST_CLOCK_OK);
502 g_assert_cmpint (jitter, ==, -GST_SECOND);
503 gst_clock_id_unref (clock_id);
505 gst_object_unref (clock);
510 GST_START_TEST (test_single_shot_sync_unschedule)
513 GstTestClock *test_clock;
515 GtuClockWaitContext *wait_ctx;
517 clock = gst_test_clock_new_with_start_time (GST_SECOND);
518 test_clock = GST_TEST_CLOCK (clock);
520 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND);
521 gst_clock_id_unschedule (clock_id);
522 gst_clock_id_unref (clock_id);
524 clock_id = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
525 wait_ctx = gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, NULL);
526 gst_clock_id_unschedule (clock_id);
527 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx)
528 == GST_CLOCK_UNSCHEDULED);
529 gst_clock_id_unref (clock_id);
531 gst_object_unref (clock);
536 GST_START_TEST (test_single_shot_sync_ordering)
539 GstTestClock *test_clock;
540 GstClockID clock_id_a, clock_id_b;
541 GtuClockWaitContext *wait_ctx_a, *wait_ctx_b;
543 clock = gst_test_clock_new_with_start_time (GST_SECOND);
544 test_clock = GST_TEST_CLOCK (clock);
546 clock_id_a = gst_clock_new_single_shot_id (clock, 3 * GST_SECOND);
548 gst_test_util_wait_for_clock_id_begin (test_clock, clock_id_a, NULL);
550 gst_test_clock_advance_time (test_clock, GST_SECOND);
552 clock_id_b = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
554 gst_test_util_wait_for_clock_id_begin (test_clock, clock_id_b, NULL);
556 gst_test_clock_advance_time (test_clock, GST_SECOND);
558 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx_b) == GST_CLOCK_OK);
559 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx_a) == GST_CLOCK_OK);
561 gst_clock_id_unref (clock_id_b);
562 gst_clock_id_unref (clock_id_a);
564 gst_object_unref (clock);
569 GST_START_TEST (test_single_shot_sync_ordering_parallel)
572 GstTestClock *test_clock;
573 GstClockID clock_id_a, clock_id_b;
574 GtuClockWaitContext *wait_ctx_a, *wait_ctx_b;
576 clock = gst_test_clock_new_with_start_time (GST_SECOND);
577 test_clock = GST_TEST_CLOCK (clock);
579 clock_id_a = gst_clock_new_single_shot_id (clock, 3 * GST_SECOND);
580 clock_id_b = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
581 wait_ctx_a = gst_test_util_wait_for_clock_id_begin (test_clock, clock_id_a,
583 wait_ctx_b = gst_test_util_wait_for_clock_id_begin (test_clock, clock_id_b,
586 g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
588 gst_test_clock_advance_time (test_clock, GST_SECOND);
589 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx_b) == GST_CLOCK_OK);
591 g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
593 gst_test_clock_advance_time (test_clock, GST_SECOND);
594 g_assert (gst_test_util_wait_for_clock_id_end (wait_ctx_a) == GST_CLOCK_OK);
596 gst_clock_id_unref (clock_id_b);
597 gst_clock_id_unref (clock_id_a);
599 gst_object_unref (clock);
604 GST_START_TEST (test_single_shot_sync_simultaneous_no_timeout)
607 GstTestClock *test_clock;
608 GstClockID clock_id_a;
609 GstClockID clock_id_b;
610 SyncClockWaitContext context_a;
611 SyncClockWaitContext context_b;
612 GThread *worker_thread_a;
613 GThread *worker_thread_b;
614 GstClockID processed_id;
615 GstClockID pending_id;
617 clock = gst_test_clock_new_with_start_time (GST_SECOND);
618 test_clock = GST_TEST_CLOCK (clock);
620 clock_id_a = gst_clock_new_single_shot_id (clock, 5 * GST_SECOND);
621 clock_id_b = gst_clock_new_single_shot_id (clock, 6 * GST_SECOND);
623 context_a.clock_id = gst_clock_id_ref (clock_id_a);
624 context_a.jitter = 0;
625 context_b.clock_id = gst_clock_id_ref (clock_id_b);
626 context_b.jitter = 0;
628 gst_test_clock_wait_for_pending_id_count (test_clock, 0);
631 g_thread_new ("worker_thread_b",
632 test_wait_pending_single_shot_id_sync_worker, &context_b);
634 gst_test_clock_wait_for_pending_id_count (test_clock, 1);
635 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
636 assert_pending_id (pending_id, clock_id_b, GST_CLOCK_ENTRY_SINGLE,
638 gst_clock_id_unref (pending_id);
641 g_thread_new ("worker_thread_a",
642 test_wait_pending_single_shot_id_sync_worker, &context_a);
644 gst_test_clock_wait_for_pending_id_count (test_clock, 2);
645 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
646 assert_pending_id (pending_id, clock_id_a, GST_CLOCK_ENTRY_SINGLE,
648 gst_clock_id_unref (pending_id);
650 g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
652 gst_test_clock_advance_time (test_clock, 5 * GST_SECOND);
653 processed_id = gst_test_clock_process_next_clock_id (test_clock);
654 assert_processed_id (processed_id, clock_id_a, GST_CLOCK_ENTRY_SINGLE,
656 gst_clock_id_unref (processed_id);
658 gst_test_clock_wait_for_pending_id_count (test_clock, 1);
659 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
660 assert_pending_id (pending_id, clock_id_b, GST_CLOCK_ENTRY_SINGLE,
662 gst_clock_id_unref (pending_id);
664 g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
666 gst_test_clock_advance_time (test_clock, 6 * GST_SECOND);
667 processed_id = gst_test_clock_process_next_clock_id (test_clock);
668 assert_processed_id (processed_id, clock_id_b, GST_CLOCK_ENTRY_SINGLE,
670 gst_clock_id_unref (processed_id);
672 gst_test_clock_wait_for_pending_id_count (test_clock, 0);
674 g_thread_join (worker_thread_a);
675 g_thread_join (worker_thread_b);
677 g_assert_cmpuint (context_a.jitter, ==, -4 * GST_SECOND);
678 g_assert_cmpuint (context_b.jitter, ==, -5 * GST_SECOND);
680 gst_clock_id_unref (context_a.clock_id);
681 gst_clock_id_unref (context_b.clock_id);
683 gst_clock_id_unref (clock_id_a);
684 gst_clock_id_unref (clock_id_b);
686 gst_object_unref (clock);
691 GST_START_TEST (test_single_shot_async_past)
695 GstClockID processed_id;
696 gboolean wait_complete = FALSE;
698 clock = gst_test_clock_new_with_start_time (GST_SECOND);
699 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND - 1);
700 g_assert (gst_clock_id_wait_async (clock_id, test_async_wait_cb,
701 &wait_complete, NULL) == GST_CLOCK_OK);
702 g_assert (!wait_complete);
703 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
704 g_assert (wait_complete);
705 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
707 gst_clock_id_unref (processed_id);
708 gst_clock_id_unref (clock_id);
709 gst_object_unref (clock);
714 GST_START_TEST (test_single_shot_async_present)
718 GstClockID processed_id;
719 gboolean wait_complete = FALSE;
721 clock = gst_test_clock_new_with_start_time (GST_SECOND);
722 clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND);
723 g_assert (gst_clock_id_wait_async (clock_id, test_async_wait_cb,
724 &wait_complete, NULL) == GST_CLOCK_OK);
725 g_assert (!wait_complete);
726 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
727 g_assert (wait_complete);
728 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
730 gst_clock_id_unref (processed_id);
731 gst_clock_id_unref (clock_id);
732 gst_object_unref (clock);
737 GST_START_TEST (test_single_shot_async_future)
741 GstClockID processed_id;
742 gboolean wait_complete = FALSE;
744 clock = gst_test_clock_new_with_start_time (GST_SECOND);
745 clock_id = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
746 g_assert (gst_clock_id_wait_async (clock_id, test_async_wait_cb,
747 &wait_complete, NULL) == GST_CLOCK_OK);
748 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
749 g_assert (processed_id == NULL);
750 g_assert (!wait_complete);
751 g_assert (GST_CLOCK_ENTRY_STATUS (GST_CLOCK_ENTRY (clock_id))
754 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), GST_SECOND - 1);
755 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
756 g_assert (processed_id == NULL);
757 g_assert (!wait_complete);
758 g_assert (GST_CLOCK_ENTRY_STATUS (GST_CLOCK_ENTRY (clock_id))
761 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 1);
762 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
763 g_assert (wait_complete);
764 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_SINGLE,
766 gst_clock_id_unref (processed_id);
767 g_assert (GST_CLOCK_ENTRY_STATUS (GST_CLOCK_ENTRY (clock_id))
770 gst_clock_id_unref (clock_id);
771 gst_object_unref (clock);
776 GST_START_TEST (test_single_shot_async_unschedule)
780 gboolean wait_complete = FALSE;
782 clock = gst_test_clock_new_with_start_time (GST_SECOND);
784 clock_id = gst_clock_new_single_shot_id (clock, 3 * GST_SECOND);
785 g_assert (gst_clock_id_wait_async (clock_id, test_async_wait_cb,
786 &wait_complete, NULL) == GST_CLOCK_OK);
788 gst_clock_id_unschedule (clock_id);
790 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 2 * GST_SECOND);
791 g_assert (gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock))
793 g_assert (!wait_complete);
795 gst_clock_id_unref (clock_id);
796 gst_object_unref (clock);
801 GST_START_TEST (test_periodic_sync)
804 GstTestClock *test_clock;
807 const GstClockTime interval = 4 * GST_MSECOND;
809 clock = gst_test_clock_new ();
810 test_clock = GST_TEST_CLOCK (clock);
812 clock_id = gst_clock_new_periodic_id (clock, GST_SECOND, interval);
814 for (i = 0; i < 3; i++) {
815 GtuClockWaitContext *wait_ctx;
816 GstClockID pending_id;
820 gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, NULL);
822 gst_test_clock_wait_for_next_pending_id (test_clock, &pending_id);
823 assert_pending_id (pending_id, clock_id, GST_CLOCK_ENTRY_PERIODIC,
824 GST_SECOND + (i * interval));
825 gst_clock_id_unref (pending_id);
827 for (j = 0; j < 10; j++) {
828 g_usleep (G_USEC_PER_SEC / 10 / 10);
829 g_assert (!gst_test_util_clock_wait_context_has_completed (wait_ctx));
833 gst_test_clock_advance_time (test_clock, GST_SECOND);
835 gst_test_clock_advance_time (test_clock, interval);
837 gst_test_util_wait_for_clock_id_end (wait_ctx);
840 gst_clock_id_unref (clock_id);
841 gst_object_unref (clock);
846 GST_START_TEST (test_periodic_async)
850 GstClockID processed_id;
851 gboolean wait_complete = FALSE;
852 const GstClockTime interval = 4 * GST_MSECOND;
854 clock = gst_test_clock_new ();
855 clock_id = gst_clock_new_periodic_id (clock, gst_clock_get_time (clock),
857 g_assert (gst_clock_id_wait_async (clock_id, test_async_wait_cb,
858 &wait_complete, NULL) == GST_CLOCK_OK);
860 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
861 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_PERIODIC,
863 gst_clock_id_unref (processed_id);
865 g_assert (wait_complete);
866 wait_complete = FALSE;
868 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), interval - 1);
869 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
870 g_assert (processed_id == NULL);
871 g_assert (!wait_complete);
873 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 1);
874 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
875 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_PERIODIC,
877 gst_clock_id_unref (processed_id);
878 g_assert (wait_complete);
879 wait_complete = FALSE;
881 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), interval - 1);
882 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
883 g_assert (processed_id == NULL);
884 g_assert (!wait_complete);
886 gst_test_clock_advance_time (GST_TEST_CLOCK (clock), 1);
887 processed_id = gst_test_clock_process_next_clock_id (GST_TEST_CLOCK (clock));
888 assert_processed_id (processed_id, clock_id, GST_CLOCK_ENTRY_PERIODIC,
890 gst_clock_id_unref (processed_id);
891 g_assert (wait_complete);
892 wait_complete = FALSE;
894 gst_clock_id_unref (clock_id);
895 gst_object_unref (clock);
900 GST_START_TEST (test_periodic_uniqueness)
903 GstTestClock *test_clock;
906 const GstClockTime interval = 4 * GST_MSECOND;
908 clock = gst_test_clock_new ();
909 test_clock = GST_TEST_CLOCK (clock);
911 clock_id = gst_clock_new_periodic_id (clock, 0, interval);
913 for (i = 0; i < 3; i++) {
914 GtuClockWaitContext *wait_ctx;
918 gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, NULL);
920 for (j = 0; j < 10; j++) {
921 g_usleep (G_USEC_PER_SEC / 10 / 10);
922 g_assert_cmpuint (gst_test_clock_peek_id_count (test_clock), ==, 1);
925 gst_test_clock_advance_time (test_clock, interval);
926 gst_test_util_wait_for_clock_id_end (wait_ctx);
929 gst_clock_id_unref (clock_id);
930 gst_object_unref (clock);
936 gst_test_clock_suite (void)
938 Suite *s = suite_create ("GstTestClock");
939 TCase *tc_chain = tcase_create ("testclock");
941 suite_add_tcase (s, tc_chain);
943 tcase_add_test (tc_chain, test_object_flags);
944 tcase_add_test (tc_chain, test_resolution_query);
945 tcase_add_test (tc_chain, test_start_time);
946 tcase_add_test (tc_chain, test_set_time);
947 tcase_add_test (tc_chain, test_advance_time);
948 tcase_add_test (tc_chain, test_wait_synchronous_no_timeout);
949 tcase_add_test (tc_chain, test_wait_pending_single_shot_id);
950 tcase_add_test (tc_chain, test_wait_pending_periodic_id);
951 tcase_add_test (tc_chain, test_single_shot_sync_simultaneous_no_timeout);
952 tcase_add_test (tc_chain, test_single_shot_sync_past);
953 tcase_add_test (tc_chain, test_single_shot_sync_present);
954 tcase_add_test (tc_chain, test_single_shot_sync_future);
955 tcase_add_test (tc_chain, test_single_shot_sync_unschedule);
956 tcase_add_test (tc_chain, test_single_shot_sync_ordering);
957 tcase_add_test (tc_chain, test_single_shot_sync_ordering_parallel);
958 tcase_add_test (tc_chain, test_single_shot_async_past);
959 tcase_add_test (tc_chain, test_single_shot_async_present);
960 tcase_add_test (tc_chain, test_single_shot_async_future);
961 tcase_add_test (tc_chain, test_single_shot_async_unschedule);
962 tcase_add_test (tc_chain, test_periodic_sync);
963 tcase_add_test (tc_chain, test_periodic_async);
964 tcase_add_test (tc_chain, test_periodic_uniqueness);
969 GST_CHECK_MAIN (gst_test_clock);