Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / wearable / tests / check / gst / gstsystemclock.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstsystemclock.c: Unit test for GstSystemClock
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 static GMutex *af_lock;
25 static GCond *af_cond;
26
27 /* see if the defines make sense */
28 GST_START_TEST (test_range)
29 {
30   GstClockTime time, time2;
31
32   time = GST_SECOND;
33   fail_unless (time == G_GUINT64_CONSTANT (1000000000));
34
35   time2 = time / 1000;
36   fail_unless (time2 == 1000000);
37   fail_unless (time2 == GST_MSECOND);
38   fail_unless (time2 == GST_TIME_AS_USECONDS (time));
39
40   time2 = time / 1000000;
41   fail_unless (time2 == 1000);
42   fail_unless (time2 == GST_USECOND);
43   fail_unless (time2 == GST_TIME_AS_MSECONDS (time));
44 }
45
46 GST_END_TEST;
47
48 GST_START_TEST (test_signedness)
49 {
50   GstClockTime time[] = { 0, 1, G_MAXUINT64 / GST_SECOND };
51   GstClockTimeDiff diff[] =
52       { 0, 1, -1, G_MAXINT64 / GST_SECOND, G_MININT64 / GST_SECOND };
53   guint i;
54
55   for (i = 0; i < G_N_ELEMENTS (time); i++) {
56     fail_if (time[i] != (time[i] * GST_SECOND / GST_SECOND));
57   }
58   for (i = 0; i < G_N_ELEMENTS (diff); i++) {
59     fail_if (diff[i] != (diff[i] * GST_SECOND / GST_SECOND));
60   }
61 }
62
63 GST_END_TEST;
64
65 #define TIME_UNIT (GST_SECOND / 5)
66 static void
67 gst_clock_debug (GstClock * clock)
68 {
69   GstClockTime time;
70
71   time = gst_clock_get_time (clock);
72   GST_DEBUG ("Clock info: time %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
73 }
74
75 static gboolean
76 ok_callback (GstClock * clock, GstClockTime time,
77     GstClockID id, gpointer user_data)
78 {
79   GST_LOG ("unlocked async id %p", id);
80   return FALSE;
81 }
82
83 static gboolean
84 error_callback (GstClock * clock, GstClockTime time,
85     GstClockID id, gpointer user_data)
86 {
87   GST_WARNING ("unlocked unscheduled async id %p, this is wrong", id);
88   fail_if (TRUE);
89
90   return FALSE;
91 }
92
93 GMutex *store_lock;
94
95 static gboolean
96 store_callback (GstClock * clock, GstClockTime time,
97     GstClockID id, gpointer user_data)
98 {
99   GList **list = user_data;
100
101   GST_DEBUG ("unlocked async id %p", id);
102   g_mutex_lock (store_lock);
103   *list = g_list_append (*list, id);
104   g_mutex_unlock (store_lock);
105   return FALSE;
106 }
107
108 static gboolean
109 notify_callback (GstClock * clock, GstClockTime time,
110     GstClockID id, gpointer user_data)
111 {
112   gboolean *ret = (gboolean *) user_data;
113
114   if (ret != NULL)
115     *ret = TRUE;
116
117   return FALSE;
118 }
119
120 GST_START_TEST (test_single_shot)
121 {
122   GstClock *clock;
123   GstClockID id, id2;
124   GstClockTime base;
125   GstClockReturn result;
126
127   clock = gst_system_clock_obtain ();
128   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
129
130   gst_clock_debug (clock);
131   base = gst_clock_get_time (clock);
132
133   id = gst_clock_new_single_shot_id (clock, base + TIME_UNIT);
134   fail_unless (id != NULL, "Could not create single shot id");
135
136   GST_DEBUG ("waiting one time unit");
137   result = gst_clock_id_wait (id, NULL);
138   gst_clock_debug (clock);
139   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK (result=%d)",
140       result);
141   fail_unless (gst_clock_get_time (clock) > (base + TIME_UNIT),
142       "target time has not been reached");
143
144   GST_DEBUG ("waiting in the past");
145   result = gst_clock_id_wait (id, NULL);
146   gst_clock_debug (clock);
147   fail_unless (result == GST_CLOCK_EARLY,
148       "Waiting did not return EARLY(result=%d)", result);
149   gst_clock_id_unref (id);
150
151   id = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
152   GST_DEBUG ("waiting one second async id %p", id);
153   result = gst_clock_id_wait_async (id, ok_callback, NULL);
154   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
155   g_usleep (TIME_UNIT / (2 * 1000));
156   gst_clock_id_unschedule (id);
157   gst_clock_id_unref (id);
158
159   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
160   GST_DEBUG ("waiting one second async, with cancel on id %p", id);
161   result = gst_clock_id_wait_async (id, error_callback, NULL);
162   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
163   g_usleep (TIME_UNIT / (2 * 1000));
164   GST_DEBUG ("cancel id %p after half a time unit", id);
165   gst_clock_id_unschedule (id);
166   gst_clock_id_unref (id);
167   GST_DEBUG ("canceled id %p", id);
168
169   GST_DEBUG ("waiting multiple one second async, with cancel");
170   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
171   id2 = gst_clock_new_single_shot_id (clock, base + 6 * TIME_UNIT);
172   GST_DEBUG ("waiting id %p", id);
173   result = gst_clock_id_wait_async (id, ok_callback, NULL);
174   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
175
176   GST_DEBUG ("waiting id %p", id2);
177   result = gst_clock_id_wait_async (id2, error_callback, NULL);
178   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
179   g_usleep (TIME_UNIT / (2 * 1000));
180   GST_DEBUG ("cancel id %p after half a time unit", id2);
181   gst_clock_id_unschedule (id2);
182   GST_DEBUG ("canceled id %p", id2);
183   gst_clock_id_unref (id2);
184
185   /* wait for the entry to time out */
186   g_usleep (TIME_UNIT / 1000 * 5);
187   fail_unless (((GstClockEntry *) id)->status == GST_CLOCK_OK,
188       "Waiting did not finish");
189   gst_clock_id_unref (id);
190
191   gst_object_unref (clock);
192 }
193
194 GST_END_TEST;
195
196 GST_START_TEST (test_periodic_shot)
197 {
198   GstClock *clock;
199   GstClockID id, id2;
200   GstClockTime base;
201   GstClockReturn result;
202
203   clock = gst_system_clock_obtain ();
204   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
205
206   gst_clock_debug (clock);
207   base = gst_clock_get_time (clock);
208
209   /* signal every half a time unit */
210   id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
211   fail_unless (id != NULL, "Could not create periodic id");
212
213   GST_DEBUG ("waiting one time unit");
214   result = gst_clock_id_wait (id, NULL);
215   gst_clock_debug (clock);
216   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
217
218   GST_DEBUG ("waiting for the next");
219   result = gst_clock_id_wait (id, NULL);
220   gst_clock_debug (clock);
221   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
222
223   GST_DEBUG ("waiting for the next async %p", id);
224   result = gst_clock_id_wait_async (id, ok_callback, NULL);
225   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
226   g_usleep (TIME_UNIT / (2 * 1000));
227
228   GST_DEBUG ("waiting some more for the next async %p", id);
229   result = gst_clock_id_wait_async (id, ok_callback, NULL);
230   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
231   g_usleep (TIME_UNIT / (2 * 1000));
232
233   id2 = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
234   fail_unless (id2 != NULL, "Could not create second periodic id");
235
236   GST_DEBUG ("waiting some more for another async %p", id2);
237   result = gst_clock_id_wait_async (id2, ok_callback, NULL);
238   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
239   g_usleep (TIME_UNIT / (2 * 1000));
240
241   GST_DEBUG ("unschedule %p", id);
242   gst_clock_id_unschedule (id);
243
244   /* entry cannot be used again */
245   result = gst_clock_id_wait_async (id, error_callback, NULL);
246   fail_unless (result == GST_CLOCK_UNSCHEDULED,
247       "Waiting did not return UNSCHEDULED");
248   result = gst_clock_id_wait (id, NULL);
249   fail_unless (result == GST_CLOCK_UNSCHEDULED,
250       "Waiting did not return UNSCHEDULED");
251   g_usleep (TIME_UNIT / (2 * 1000));
252
253   /* clean up */
254   gst_clock_id_unref (id);
255   gst_clock_id_unschedule (id2);
256   gst_clock_id_unref (id2);
257
258   gst_object_unref (clock);
259 }
260
261 GST_END_TEST;
262
263 GST_START_TEST (test_async_order)
264 {
265   GstClock *clock;
266   GstClockID id1, id2;
267   GList *cb_list = NULL, *next;
268   GstClockTime base;
269   GstClockReturn result;
270
271   store_lock = g_mutex_new ();
272
273   clock = gst_system_clock_obtain ();
274   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
275
276   gst_clock_debug (clock);
277   base = gst_clock_get_time (clock);
278
279   id1 = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
280   id2 = gst_clock_new_single_shot_id (clock, base + 1 * TIME_UNIT);
281   result = gst_clock_id_wait_async (id1, store_callback, &cb_list);
282   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
283   g_usleep (TIME_UNIT / (2 * 1000));
284   result = gst_clock_id_wait_async (id2, store_callback, &cb_list);
285   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
286   g_usleep (TIME_UNIT / 1000);
287   /* at this point at least one of the timers should have timed out */
288   g_mutex_lock (store_lock);
289   fail_unless (cb_list != NULL, "expected notification");
290   fail_unless (cb_list->data == id2,
291       "Expected notification for id2 to come first");
292   g_mutex_unlock (store_lock);
293   g_usleep (TIME_UNIT / 1000);
294   g_mutex_lock (store_lock);
295   /* now both should have timed out */
296   next = g_list_next (cb_list);
297   fail_unless (next != NULL, "expected second notification");
298   fail_unless (next->data == id1, "Missing notification for id1");
299   g_mutex_unlock (store_lock);
300
301   gst_clock_id_unref (id1);
302   gst_clock_id_unref (id2);
303   g_list_free (cb_list);
304
305   gst_object_unref (clock);
306   g_mutex_free (store_lock);
307 }
308
309 GST_END_TEST;
310
311 struct test_async_sync_interaction_data
312 {
313   GMutex *lock;
314
315   GstClockID sync_id;
316   GstClockID sync_id2;
317
318   GstClockID async_id;
319   GstClockID async_id2;
320   GstClockID async_id3;
321 };
322
323 static gboolean
324 test_async_sync_interaction_cb (GstClock * clock, GstClockTime time,
325     GstClockID id, gpointer user_data)
326 {
327   struct test_async_sync_interaction_data *td =
328       (struct test_async_sync_interaction_data *) (user_data);
329
330   g_mutex_lock (td->lock);
331   /* The first async callback is ignored */
332   if (id == td->async_id)
333     goto out;
334
335   if (id != td->async_id2 && id != td->async_id3)
336     goto out;
337
338   /* Unschedule the sync callback */
339   if (id == td->async_id3) {
340     gst_clock_id_unschedule (td->sync_id);
341     gst_clock_id_unschedule (td->async_id2);
342   }
343 out:
344   g_mutex_unlock (td->lock);
345   return FALSE;
346 }
347
348 GST_START_TEST (test_async_sync_interaction)
349 {
350   /* This test schedules an async callback, then before it completes, schedules
351    * an earlier async callback, and quickly unschedules the first, and inserts
352    * a THIRD even earlier async callback. It then attempts to wait on a
353    * sync clock ID. While that's sleeping, the 3rd async callback should fire
354    * and unschedule it. This tests for problems with unscheduling async and
355    * sync callbacks on the system clock. */
356   GstClock *clock;
357   GstClockReturn result;
358   GstClockTime base;
359   GstClockTimeDiff jitter;
360   struct test_async_sync_interaction_data td;
361   int i;
362
363   clock = gst_system_clock_obtain ();
364   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
365
366   td.lock = g_mutex_new ();
367
368   for (i = 0; i < 50; i++) {
369     gst_clock_debug (clock);
370     base = gst_clock_get_time (clock);
371     g_mutex_lock (td.lock);
372     td.async_id = gst_clock_new_single_shot_id (clock, base + 40 * GST_MSECOND);
373     td.async_id2 =
374         gst_clock_new_single_shot_id (clock, base + 30 * GST_MSECOND);
375     td.async_id3 =
376         gst_clock_new_single_shot_id (clock, base + 20 * GST_MSECOND);
377     td.sync_id2 = gst_clock_new_single_shot_id (clock, base + 10 * GST_MSECOND);
378     td.sync_id = gst_clock_new_single_shot_id (clock, base + 50 * GST_MSECOND);
379     g_mutex_unlock (td.lock);
380
381     result = gst_clock_id_wait_async (td.async_id,
382         test_async_sync_interaction_cb, &td);
383     fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
384
385     /* Wait 10ms, then unschedule async_id and schedule async_id2 */
386     result = gst_clock_id_wait (td.sync_id2, &jitter);
387     fail_unless (result == GST_CLOCK_OK || result == GST_CLOCK_EARLY,
388         "Waiting did not return OK or EARLY");
389     /* async_id2 is earlier than async_id - should become head of the queue */
390     result = gst_clock_id_wait_async (td.async_id2,
391         test_async_sync_interaction_cb, &td);
392     fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
393     gst_clock_id_unschedule (td.async_id);
394
395     /* async_id3 is earlier than async_id2 - should become head of the queue */
396     result = gst_clock_id_wait_async (td.async_id3,
397         test_async_sync_interaction_cb, &td);
398     fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
399
400     /* While this is sleeping, the async3 id should fire and unschedule it */
401     result = gst_clock_id_wait (td.sync_id, &jitter);
402     fail_unless (result == GST_CLOCK_UNSCHEDULED || result == GST_CLOCK_EARLY,
403         "Waiting did not return UNSCHEDULED (was %d)", result);
404
405     gst_clock_id_unschedule (td.async_id3);
406     g_mutex_lock (td.lock);
407
408     gst_clock_id_unref (td.sync_id);
409     gst_clock_id_unref (td.sync_id2);
410     gst_clock_id_unref (td.async_id);
411     gst_clock_id_unref (td.async_id2);
412     gst_clock_id_unref (td.async_id3);
413     g_mutex_unlock (td.lock);
414   }
415
416   g_mutex_free (td.lock);
417   gst_object_unref (clock);
418 }
419
420 GST_END_TEST;
421
422 GST_START_TEST (test_periodic_multi)
423 {
424   GstClock *clock;
425   GstClockID clock_id;
426   GstClockID clock_id_async;
427   GstClockTime base;
428   GstClockReturn result;
429   gboolean got_callback = FALSE;
430
431   clock = gst_system_clock_obtain ();
432   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
433
434   gst_clock_debug (clock);
435   base = gst_clock_get_time (clock);
436
437   clock_id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT);
438   gst_clock_id_wait (clock_id, NULL);
439   fail_unless (gst_clock_get_time (clock) >= base + TIME_UNIT);
440   fail_unless (gst_clock_get_time (clock) < base + 2 * TIME_UNIT);
441
442   /* now perform a concurrent wait and wait_async */
443
444   clock_id_async =
445       gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT);
446   result =
447       gst_clock_id_wait_async (clock_id_async, notify_callback, &got_callback);
448   fail_unless (result == GST_CLOCK_OK, "Async waiting did not return OK");
449
450   result = gst_clock_id_wait (clock_id, NULL);
451   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
452   fail_unless (gst_clock_get_time (clock) >= base + 2 * TIME_UNIT);
453   /* give the async thread some time to call our callback: */
454   g_usleep (TIME_UNIT / (10 * 1000));
455   fail_unless (got_callback == TRUE, "got no async callback (1)");
456   fail_unless (gst_clock_get_time (clock) < base + 3 * TIME_UNIT);
457   got_callback = FALSE;
458
459   result = gst_clock_id_wait (clock_id, NULL);
460   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
461   fail_unless (gst_clock_get_time (clock) >= base + 3 * TIME_UNIT);
462   /* give the async thread some time to call our callback: */
463   g_usleep (TIME_UNIT / (10 * 1000));
464   fail_unless (got_callback == TRUE, "got no async callback (2)");
465   fail_unless (gst_clock_get_time (clock) < base + 4 * TIME_UNIT);
466
467   /* clean up */
468   gst_clock_id_unref (clock_id);
469   gst_clock_id_unschedule (clock_id_async);
470   gst_clock_id_unref (clock_id_async);
471   gst_object_unref (clock);
472 }
473
474 GST_END_TEST;
475
476 GST_START_TEST (test_diff)
477 {
478   GstClockTime time1[] = { 0, (GstClockTime) - 1, 0, 1, 2 * GST_SECOND,
479     (GstClockTime) - GST_SECOND, (GstClockTime) - GST_SECOND
480   };
481   GstClockTime time2[] =
482       { 0, 1, 1, 0, 1 * GST_SECOND, (GstClockTime) - GST_SECOND, GST_SECOND };
483   GstClockTimeDiff d[] = { 0, 2, 1, -1, -GST_SECOND, 0, 2 * GST_SECOND };
484   guint i;
485
486   for (i = 0; i < G_N_ELEMENTS (d); i++) {
487     fail_if (d[i] != GST_CLOCK_DIFF (time1[i], time2[i]));
488   }
489 }
490
491 GST_END_TEST;
492
493 /* test if a blocking wait, unblocked by an async entry continues to be
494  * scheduled */
495 typedef struct
496 {
497   GstClock *clock;
498   GstClockID id;
499   GstClockTimeDiff jitter;
500   GstClockReturn ret;
501 } MixedInfo;
502
503 static gpointer
504 mixed_thread (MixedInfo * info)
505 {
506   info->ret = gst_clock_id_wait (info->id, &info->jitter);
507   return NULL;
508 }
509
510 static gboolean
511 mixed_async_cb (GstClock * clock, GstClockTime time,
512     GstClockID id, gpointer user_data)
513 {
514   return TRUE;
515 }
516
517 GST_START_TEST (test_mixed)
518 {
519   GThread *thread;
520   GError *error = NULL;
521   MixedInfo info;
522   GstClockTime base;
523   GstClockID id;
524
525   info.clock = gst_system_clock_obtain ();
526   fail_unless (info.clock != NULL,
527       "Could not create instance of GstSystemClock");
528
529   /* get current time of the clock as base time */
530   base = gst_clock_get_time (info.clock);
531
532   /* create entry to wait for 1 second */
533   info.id = gst_clock_new_single_shot_id (info.clock, base + GST_SECOND);
534
535   /* make and start an entry that is scheduled every 10ms */
536   id = gst_clock_new_periodic_id (info.clock, base, 10 * GST_MSECOND);
537
538   /* start waiting for the entry */
539   thread = g_thread_create ((GThreadFunc) mixed_thread, &info, TRUE, &error);
540   fail_unless (error == NULL, "error creating thread");
541   fail_unless (thread != NULL, "Could not create thread");
542
543   /* wait half a second so we are sure to be in the thread */
544   g_usleep (G_USEC_PER_SEC / 2);
545
546   /* start scheduling the entry */
547   gst_clock_id_wait_async (id, mixed_async_cb, NULL);
548
549   /* wait for thread to finish */
550   g_thread_join (thread);
551   /* entry must have timed out correctly */
552   fail_unless (info.ret == GST_CLOCK_OK, "clock return was %d", info.ret);
553
554   gst_clock_id_unschedule (id);
555   gst_clock_id_unref (id);
556   gst_clock_id_unref (info.id);
557   gst_object_unref (info.clock);
558 }
559
560 GST_END_TEST;
561
562 static gboolean
563 test_async_full_slave_callback (GstClock * master, GstClockTime time,
564     GstClockID id, GstClock * clock)
565 {
566   GstClockTime stime, mtime;
567   gdouble r_squared;
568
569   /* notify the test case that we started */
570   GST_INFO ("callback started");
571   g_mutex_lock (af_lock);
572   g_cond_signal (af_cond);
573
574   /* wait for the test case to unref "clock" and signal */
575   GST_INFO ("waiting for test case to signal");
576   g_cond_wait (af_cond, af_lock);
577
578   stime = gst_clock_get_internal_time (clock);
579   mtime = gst_clock_get_time (master);
580
581   gst_clock_add_observation (clock, stime, mtime, &r_squared);
582
583   g_cond_signal (af_cond);
584   g_mutex_unlock (af_lock);
585   GST_INFO ("callback finished");
586
587   return TRUE;
588 }
589
590 GST_START_TEST (test_async_full)
591 {
592   GstClock *master, *slave;
593   GstClockID *clockid;
594
595   af_lock = g_mutex_new ();
596   af_cond = g_cond_new ();
597
598   /* create master and slave */
599   master =
600       g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "TestClockMaster", NULL);
601   slave = g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "TestClockMaster", NULL);
602   GST_OBJECT_FLAG_SET (slave, GST_CLOCK_FLAG_CAN_SET_MASTER);
603   g_object_set (slave, "timeout", 50 * GST_MSECOND, NULL);
604
605   fail_unless (GST_OBJECT_REFCOUNT (master) == 1);
606   fail_unless (GST_OBJECT_REFCOUNT (slave) == 1);
607
608   /* register a periodic shot on the master to calibrate the slave */
609   g_mutex_lock (af_lock);
610   clockid = gst_clock_new_periodic_id (master,
611       gst_clock_get_time (master), slave->timeout);
612   gst_clock_id_wait_async_full (clockid,
613       (GstClockCallback) test_async_full_slave_callback,
614       gst_object_ref (slave), (GDestroyNotify) gst_object_unref);
615
616   /* wait for the shot to be fired and test_async_full_slave_callback to be
617    * called */
618   GST_INFO ("waiting for the slave callback to start");
619   g_cond_wait (af_cond, af_lock);
620   GST_INFO ("slave callback running, unreffing slave");
621
622   /* unref the slave clock while the slave_callback is running. This should be
623    * safe since the master clock now stores a ref to the slave */
624   gst_object_unref (slave);
625
626   /* unref the clock entry. This should be safe as well since the clock thread
627    * refs the entry before executing it */
628   gst_clock_id_unschedule (clockid);
629   gst_clock_id_unref (clockid);
630
631   /* signal and wait for the callback to complete */
632   g_cond_signal (af_cond);
633
634   GST_INFO ("waiting for callback to finish");
635   g_cond_wait (af_cond, af_lock);
636   GST_INFO ("callback finished");
637   g_mutex_unlock (af_lock);
638
639   gst_object_unref (master);
640
641   g_mutex_free (af_lock);
642   g_cond_free (af_cond);
643 }
644
645 GST_END_TEST;
646
647 static Suite *
648 gst_systemclock_suite (void)
649 {
650   Suite *s = suite_create ("GstSystemClock");
651   TCase *tc_chain = tcase_create ("waiting");
652
653   suite_add_tcase (s, tc_chain);
654   tcase_add_test (tc_chain, test_range);
655   tcase_add_test (tc_chain, test_signedness);
656   tcase_add_test (tc_chain, test_single_shot);
657   tcase_add_test (tc_chain, test_periodic_shot);
658   tcase_add_test (tc_chain, test_periodic_multi);
659   tcase_add_test (tc_chain, test_async_order);
660   tcase_add_test (tc_chain, test_async_sync_interaction);
661   tcase_add_test (tc_chain, test_diff);
662   tcase_add_test (tc_chain, test_mixed);
663   tcase_add_test (tc_chain, test_async_full);
664
665   return s;
666 }
667
668 GST_CHECK_MAIN (gst_systemclock);