tests: don't use deprecated thread API
[platform/upstream/gstreamer.git] / tests / check / libs / gsttestclock.c
1 /*
2  * Unit test for a deterministic clock for Gstreamer unit tests
3  *
4  * Copyright (C) 2008 Ole André Vadla Ravnås <ole.andre.ravnas@tandberg.com>
5  * Copyright (C) 2012 Sebastian Rasmussen <sebastian.rasmussen@axis.com>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <gst/check/gstcheck.h>
24 #include <gst/check/gsttestclock.h>
25
26 typedef struct
27 {
28   GstTestClock *test_clock;
29   GstClockID id;
30   GstClockTime reference;
31 } GtuClockWaitContext;
32
33 typedef struct
34 {
35   GstClockID clock_id;
36   GstClockTimeDiff jitter;
37 } SyncClockWaitContext;
38
39 #define assert_pending_id(pending_id, id, type, time) \
40 G_STMT_START { \
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)); \
45 } G_STMT_END
46
47 #define assert_processed_id(processed_id, id, type, time) \
48 G_STMT_START { \
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)); \
53 } G_STMT_END
54
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);
60
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 *
64     wait_ctx);
65 static gboolean
66 gst_test_util_clock_wait_context_has_completed (GtuClockWaitContext * wait_ctx);
67
68 static gpointer
69 test_wait_pending_single_shot_id_sync_worker (gpointer data)
70 {
71   SyncClockWaitContext *ctx = data;
72
73   gst_clock_id_wait (ctx->clock_id, &ctx->jitter);
74
75   return NULL;
76 }
77
78 static gpointer
79 test_wait_pending_single_shot_id_async_worker (gpointer data)
80 {
81   GstClockID clock_id = data;
82
83   g_usleep (G_USEC_PER_SEC / 10);
84   gst_clock_id_wait_async (clock_id, test_async_wait_cb, NULL, NULL);
85
86   return NULL;
87 }
88
89 static gpointer
90 test_wait_pending_periodic_id_waiter_thread (gpointer data)
91 {
92   GstClockID clock_id = data;
93   gst_clock_id_wait (clock_id, NULL);
94   return NULL;
95 }
96
97 static gboolean
98 test_async_wait_cb (GstClock * clock,
99     GstClockTime time, GstClockID id, gpointer user_data)
100 {
101
102   gboolean *wait_complete = user_data;
103
104   if (wait_complete != NULL)
105     *wait_complete = TRUE;
106
107   return TRUE;
108 }
109
110 static GtuClockWaitContext *
111 gst_test_util_wait_for_clock_id_begin (GstTestClock * test_clock, GstClockID id,
112     GstClockTimeDiff * jitter)
113 {
114   GtuClockWaitContext *wait_ctx;
115
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);
120
121   if (jitter) {
122     GstClockEntry *entry = GST_CLOCK_ENTRY (wait_ctx->id);
123     GstClockTime requested = GST_CLOCK_ENTRY_TIME (entry);
124     GstClockTime reference = wait_ctx->reference;
125
126     *jitter = GST_CLOCK_DIFF (requested, reference);
127   }
128
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);
133   }
134
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);
137
138   return wait_ctx;
139 }
140
141 static GstClockReturn
142 gst_test_util_wait_for_clock_id_end (GtuClockWaitContext * wait_ctx)
143 {
144   GstClockReturn status = GST_CLOCK_ERROR;
145   GstClockEntry *entry = GST_CLOCK_ENTRY (wait_ctx->id);
146
147   if (G_UNLIKELY (GST_CLOCK_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED)) {
148     status = GST_CLOCK_UNSCHEDULED;
149   } else {
150     GstClockTime requested = GST_CLOCK_ENTRY_TIME (entry);
151     GstClockTimeDiff diff;
152
153     g_assert (gst_test_clock_has_id (wait_ctx->test_clock, wait_ctx->id));
154
155     diff = GST_CLOCK_DIFF (requested, wait_ctx->reference);
156
157     if (diff > 0) {
158       status = GST_CLOCK_EARLY;
159     } else {
160       status = GST_CLOCK_OK;
161     }
162
163     g_atomic_int_set (&GST_CLOCK_ENTRY_STATUS (entry), status);
164   }
165
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);
169
170     klass->unschedule (clock, wait_ctx->id);
171     g_assert (!gst_test_clock_has_id (wait_ctx->test_clock, wait_ctx->id));
172   } else {
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));
175   }
176
177   gst_clock_id_unref (wait_ctx->id);
178   gst_object_unref (wait_ctx->test_clock);
179   g_slice_free (GtuClockWaitContext, wait_ctx);
180
181   return status;
182 }
183
184 static gboolean
185 gst_test_util_clock_wait_context_has_completed (GtuClockWaitContext * wait_ctx)
186 {
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);
191
192   return requested < now;
193 }
194
195 GST_START_TEST (test_object_flags)
196 {
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);
205 }
206
207 GST_END_TEST;
208
209 GST_START_TEST (test_resolution_query)
210 {
211   GstClock *clock = gst_test_clock_new ();
212   g_assert_cmpuint (gst_clock_get_resolution (clock), ==, 1);
213   gst_object_unref (clock);
214 }
215
216 GST_END_TEST;
217
218 GST_START_TEST (test_start_time)
219 {
220   GstClock *clock;
221   guint64 start_time;
222
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);
228
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);
234 }
235
236 GST_END_TEST;
237
238 GST_START_TEST (test_set_time)
239 {
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);
246 }
247
248 GST_END_TEST;
249
250 GST_START_TEST (test_advance_time)
251 {
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);
259 }
260
261 GST_END_TEST;
262
263 GST_START_TEST (test_wait_synchronous_no_timeout)
264 {
265   GstClock *clock;
266   GstTestClock *test_clock;
267   GstClockID clock_id;
268   GThread *worker_thread;
269   GstClockID pending_id;
270   GstClockID processed_id;
271   SyncClockWaitContext context;
272
273   clock = gst_test_clock_new_with_start_time (GST_SECOND);
274   test_clock = GST_TEST_CLOCK (clock);
275
276   clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND - 1);
277   context.clock_id = gst_clock_id_ref (clock_id);
278   context.jitter = 0;
279   worker_thread =
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,
284       GST_SECOND - 1);
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,
288       GST_CLOCK_EARLY);
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);
294
295   clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND);
296   context.clock_id = gst_clock_id_ref (clock_id);
297   context.jitter = 0;
298   worker_thread =
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,
306       GST_CLOCK_OK);
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);
312
313   clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND + 1);
314   context.clock_id = gst_clock_id_ref (clock_id);
315   context.jitter = 0;
316   worker_thread =
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,
321       GST_SECOND + 1);
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,
328       GST_CLOCK_OK);
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);
334
335   gst_object_unref (clock);
336 }
337
338 GST_END_TEST;
339
340 GST_START_TEST (test_wait_pending_single_shot_id)
341 {
342   GstClock *clock;
343   GstTestClock *test_clock;
344   GstClockID clock_id;
345   GstClockID processed_id;
346   GThread *worker_thread;
347   GstClockID pending_id;
348
349   clock = gst_test_clock_new_with_start_time (GST_SECOND);
350   test_clock = GST_TEST_CLOCK (clock);
351
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,
359       GST_CLOCK_OK);
360   gst_clock_id_unref (processed_id);
361   gst_clock_id_unref (clock_id);
362
363   clock_id = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
364   worker_thread =
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,
369       2 * GST_SECOND);
370   gst_clock_id_unref (pending_id);
371   g_thread_join (worker_thread);
372   gst_clock_id_unref (clock_id);
373
374   clock_id = gst_clock_new_single_shot_id (clock, 3 * GST_SECOND);
375   worker_thread =
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);
381
382   gst_object_unref (clock);
383 }
384
385 GST_END_TEST;
386
387 GST_START_TEST (test_wait_pending_periodic_id)
388 {
389   GstClock *clock;
390   GstTestClock *test_clock;
391   GstClockID clock_id;
392   GstClockID processed_id;
393
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);
397
398   {
399     GThread *waiter_thread;
400
401     waiter_thread =
402         g_thread_new ("waiter_thread",
403         test_wait_pending_periodic_id_waiter_thread, clock_id);
404
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,
409         GST_CLOCK_OK);
410     gst_clock_id_unref (processed_id);
411
412     g_thread_join (waiter_thread);
413   }
414
415   {
416     guint i;
417     GThread *waiter_thread;
418
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);
422     }
423
424     waiter_thread =
425         g_thread_new ("waiter_thread",
426         test_wait_pending_periodic_id_waiter_thread, clock_id);
427
428     gst_test_clock_wait_for_next_pending_id (test_clock, NULL);
429     gst_clock_id_unschedule (clock_id);
430
431     g_thread_join (waiter_thread);
432   }
433
434   gst_clock_id_unref (clock_id);
435   gst_object_unref (clock);
436 }
437
438 GST_END_TEST;
439
440 GST_START_TEST (test_single_shot_sync_past)
441 {
442   GstClock *clock;
443   GstTestClock *test_clock;
444   GstClockID clock_id;
445   GstClockTimeDiff jitter;
446   GtuClockWaitContext *wait_ctx;
447
448   clock = gst_test_clock_new_with_start_time (GST_SECOND);
449   test_clock = GST_TEST_CLOCK (clock);
450
451   clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND - 1);
452   wait_ctx =
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);
457
458   gst_object_unref (clock);
459 }
460
461 GST_END_TEST;
462
463 GST_START_TEST (test_single_shot_sync_present)
464 {
465   GstClock *clock;
466   GstTestClock *test_clock;
467   GstClockID clock_id;
468   GstClockTimeDiff jitter;
469   GtuClockWaitContext *wait_ctx;
470
471   clock = gst_test_clock_new_with_start_time (GST_SECOND);
472   test_clock = GST_TEST_CLOCK (clock);
473
474   clock_id = gst_clock_new_single_shot_id (clock, GST_SECOND);
475   wait_ctx =
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);
480
481   gst_object_unref (clock);
482 }
483
484 GST_END_TEST;
485
486 GST_START_TEST (test_single_shot_sync_future)
487 {
488   GstClock *clock;
489   GstTestClock *test_clock;
490   GstClockID clock_id;
491   GstClockTimeDiff jitter;
492   GtuClockWaitContext *wait_ctx;
493
494   clock = gst_test_clock_new_with_start_time (GST_SECOND);
495   test_clock = GST_TEST_CLOCK (clock);
496
497   clock_id = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
498   wait_ctx =
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);
504
505   gst_object_unref (clock);
506 }
507
508 GST_END_TEST;
509
510 GST_START_TEST (test_single_shot_sync_unschedule)
511 {
512   GstClock *clock;
513   GstTestClock *test_clock;
514   GstClockID clock_id;
515   GtuClockWaitContext *wait_ctx;
516
517   clock = gst_test_clock_new_with_start_time (GST_SECOND);
518   test_clock = GST_TEST_CLOCK (clock);
519
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);
523
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);
530
531   gst_object_unref (clock);
532 }
533
534 GST_END_TEST;
535
536 GST_START_TEST (test_single_shot_sync_ordering)
537 {
538   GstClock *clock;
539   GstTestClock *test_clock;
540   GstClockID clock_id_a, clock_id_b;
541   GtuClockWaitContext *wait_ctx_a, *wait_ctx_b;
542
543   clock = gst_test_clock_new_with_start_time (GST_SECOND);
544   test_clock = GST_TEST_CLOCK (clock);
545
546   clock_id_a = gst_clock_new_single_shot_id (clock, 3 * GST_SECOND);
547   wait_ctx_a =
548       gst_test_util_wait_for_clock_id_begin (test_clock, clock_id_a, NULL);
549
550   gst_test_clock_advance_time (test_clock, GST_SECOND);
551
552   clock_id_b = gst_clock_new_single_shot_id (clock, 2 * GST_SECOND);
553   wait_ctx_b =
554       gst_test_util_wait_for_clock_id_begin (test_clock, clock_id_b, NULL);
555
556   gst_test_clock_advance_time (test_clock, GST_SECOND);
557
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);
560
561   gst_clock_id_unref (clock_id_b);
562   gst_clock_id_unref (clock_id_a);
563
564   gst_object_unref (clock);
565 }
566
567 GST_END_TEST;
568
569 GST_START_TEST (test_single_shot_sync_ordering_parallel)
570 {
571   GstClock *clock;
572   GstTestClock *test_clock;
573   GstClockID clock_id_a, clock_id_b;
574   GtuClockWaitContext *wait_ctx_a, *wait_ctx_b;
575
576   clock = gst_test_clock_new_with_start_time (GST_SECOND);
577   test_clock = GST_TEST_CLOCK (clock);
578
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,
582       NULL);
583   wait_ctx_b = gst_test_util_wait_for_clock_id_begin (test_clock, clock_id_b,
584       NULL);
585
586   g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
587       2 * GST_SECOND);
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);
590
591   g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
592       3 * GST_SECOND);
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);
595
596   gst_clock_id_unref (clock_id_b);
597   gst_clock_id_unref (clock_id_a);
598
599   gst_object_unref (clock);
600 }
601
602 GST_END_TEST;
603
604 GST_START_TEST (test_single_shot_sync_simultaneous_no_timeout)
605 {
606   GstClock *clock;
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;
616
617   clock = gst_test_clock_new_with_start_time (GST_SECOND);
618   test_clock = GST_TEST_CLOCK (clock);
619
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);
622
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;
627
628   gst_test_clock_wait_for_pending_id_count (test_clock, 0);
629
630   worker_thread_b =
631       g_thread_new ("worker_thread_b",
632       test_wait_pending_single_shot_id_sync_worker, &context_b);
633
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,
637       6 * GST_SECOND);
638   gst_clock_id_unref (pending_id);
639
640   worker_thread_a =
641       g_thread_new ("worker_thread_a",
642       test_wait_pending_single_shot_id_sync_worker, &context_a);
643
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,
647       5 * GST_SECOND);
648   gst_clock_id_unref (pending_id);
649
650   g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
651       5 * GST_SECOND);
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,
655       GST_CLOCK_OK);
656   gst_clock_id_unref (processed_id);
657
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,
661       6 * GST_SECOND);
662   gst_clock_id_unref (pending_id);
663
664   g_assert_cmpuint (gst_test_clock_get_next_entry_time (test_clock), ==,
665       6 * GST_SECOND);
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,
669       GST_CLOCK_OK);
670   gst_clock_id_unref (processed_id);
671
672   gst_test_clock_wait_for_pending_id_count (test_clock, 0);
673
674   g_thread_join (worker_thread_a);
675   g_thread_join (worker_thread_b);
676
677   g_assert_cmpuint (context_a.jitter, ==, -4 * GST_SECOND);
678   g_assert_cmpuint (context_b.jitter, ==, -5 * GST_SECOND);
679
680   gst_clock_id_unref (context_a.clock_id);
681   gst_clock_id_unref (context_b.clock_id);
682
683   gst_clock_id_unref (clock_id_a);
684   gst_clock_id_unref (clock_id_b);
685
686   gst_object_unref (clock);
687 }
688
689 GST_END_TEST;
690
691 GST_START_TEST (test_single_shot_async_past)
692 {
693   GstClock *clock;
694   GstClockID clock_id;
695   GstClockID processed_id;
696   gboolean wait_complete = FALSE;
697
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,
706       GST_CLOCK_EARLY);
707   gst_clock_id_unref (processed_id);
708   gst_clock_id_unref (clock_id);
709   gst_object_unref (clock);
710 }
711
712 GST_END_TEST;
713
714 GST_START_TEST (test_single_shot_async_present)
715 {
716   GstClock *clock;
717   GstClockID clock_id;
718   GstClockID processed_id;
719   gboolean wait_complete = FALSE;
720
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,
729       GST_CLOCK_OK);
730   gst_clock_id_unref (processed_id);
731   gst_clock_id_unref (clock_id);
732   gst_object_unref (clock);
733 }
734
735 GST_END_TEST;
736
737 GST_START_TEST (test_single_shot_async_future)
738 {
739   GstClock *clock;
740   GstClockID clock_id;
741   GstClockID processed_id;
742   gboolean wait_complete = FALSE;
743
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))
752       == GST_CLOCK_OK);
753
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))
759       == GST_CLOCK_OK);
760
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,
765       GST_CLOCK_OK);
766   gst_clock_id_unref (processed_id);
767   g_assert (GST_CLOCK_ENTRY_STATUS (GST_CLOCK_ENTRY (clock_id))
768       == GST_CLOCK_OK);
769
770   gst_clock_id_unref (clock_id);
771   gst_object_unref (clock);
772 }
773
774 GST_END_TEST;
775
776 GST_START_TEST (test_single_shot_async_unschedule)
777 {
778   GstClock *clock;
779   GstClockID clock_id;
780   gboolean wait_complete = FALSE;
781
782   clock = gst_test_clock_new_with_start_time (GST_SECOND);
783
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);
787
788   gst_clock_id_unschedule (clock_id);
789
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))
792       == NULL);
793   g_assert (!wait_complete);
794
795   gst_clock_id_unref (clock_id);
796   gst_object_unref (clock);
797 }
798
799 GST_END_TEST;
800
801 GST_START_TEST (test_periodic_sync)
802 {
803   GstClock *clock;
804   GstTestClock *test_clock;
805   GstClockID clock_id;
806   guint i;
807   const GstClockTime interval = 4 * GST_MSECOND;
808
809   clock = gst_test_clock_new ();
810   test_clock = GST_TEST_CLOCK (clock);
811
812   clock_id = gst_clock_new_periodic_id (clock, GST_SECOND, interval);
813
814   for (i = 0; i < 3; i++) {
815     GtuClockWaitContext *wait_ctx;
816     GstClockID pending_id;
817     guint j;
818
819     wait_ctx =
820         gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, NULL);
821
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);
826
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));
830     }
831
832     if (i == 0)
833       gst_test_clock_advance_time (test_clock, GST_SECOND);
834     else
835       gst_test_clock_advance_time (test_clock, interval);
836
837     gst_test_util_wait_for_clock_id_end (wait_ctx);
838   }
839
840   gst_clock_id_unref (clock_id);
841   gst_object_unref (clock);
842 }
843
844 GST_END_TEST;
845
846 GST_START_TEST (test_periodic_async)
847 {
848   GstClock *clock;
849   GstClockID clock_id;
850   GstClockID processed_id;
851   gboolean wait_complete = FALSE;
852   const GstClockTime interval = 4 * GST_MSECOND;
853
854   clock = gst_test_clock_new ();
855   clock_id = gst_clock_new_periodic_id (clock, gst_clock_get_time (clock),
856       interval);
857   g_assert (gst_clock_id_wait_async (clock_id, test_async_wait_cb,
858           &wait_complete, NULL) == GST_CLOCK_OK);
859
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,
862       GST_CLOCK_OK);
863   gst_clock_id_unref (processed_id);
864
865   g_assert (wait_complete);
866   wait_complete = FALSE;
867
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);
872
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,
876       GST_CLOCK_OK);
877   gst_clock_id_unref (processed_id);
878   g_assert (wait_complete);
879   wait_complete = FALSE;
880
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);
885
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,
889       GST_CLOCK_OK);
890   gst_clock_id_unref (processed_id);
891   g_assert (wait_complete);
892   wait_complete = FALSE;
893
894   gst_clock_id_unref (clock_id);
895   gst_object_unref (clock);
896 }
897
898 GST_END_TEST;
899
900 GST_START_TEST (test_periodic_uniqueness)
901 {
902   GstClock *clock;
903   GstTestClock *test_clock;
904   GstClockID clock_id;
905   guint i;
906   const GstClockTime interval = 4 * GST_MSECOND;
907
908   clock = gst_test_clock_new ();
909   test_clock = GST_TEST_CLOCK (clock);
910
911   clock_id = gst_clock_new_periodic_id (clock, 0, interval);
912
913   for (i = 0; i < 3; i++) {
914     GtuClockWaitContext *wait_ctx;
915     guint j;
916
917     wait_ctx =
918         gst_test_util_wait_for_clock_id_begin (test_clock, clock_id, NULL);
919
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);
923     }
924
925     gst_test_clock_advance_time (test_clock, interval);
926     gst_test_util_wait_for_clock_id_end (wait_ctx);
927   }
928
929   gst_clock_id_unref (clock_id);
930   gst_object_unref (clock);
931 }
932
933 GST_END_TEST;
934
935 static Suite *
936 gst_test_clock_suite (void)
937 {
938   Suite *s = suite_create ("GstTestClock");
939   TCase *tc_chain = tcase_create ("testclock");
940
941   suite_add_tcase (s, tc_chain);
942
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);
965
966   return s;
967 }
968
969 GST_CHECK_MAIN (gst_test_clock);