systemclock: Add test for gst_clock_get_resolution
[platform/upstream/gstreamer.git] / 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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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_set_default)
121 {
122   GstClock *clock, *static_clock;
123
124   /* obtain the default system clock, which keeps a static ref and bumps the
125    * refcount before returning */
126   static_clock = gst_system_clock_obtain ();
127   fail_unless (static_clock != NULL, "Could not create default system clock");
128   g_assert_cmpint (GST_OBJECT_REFCOUNT (static_clock), ==, 2);
129
130   /* set a new default clock to a different instance which should replace the
131    * static clock with this one, and unref the static clock */
132   clock = g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "TestClock", NULL);
133   gst_system_clock_set_default (clock);
134   g_assert_cmpint (GST_OBJECT_REFCOUNT (static_clock), ==, 1);
135   g_object_unref (static_clock);
136   static_clock = gst_system_clock_obtain ();
137   fail_unless (static_clock == clock);
138   g_assert_cmpint (GST_OBJECT_REFCOUNT (clock), ==, 3);
139   g_object_unref (static_clock);
140
141   /* Reset the default clock to the static one */
142   gst_system_clock_set_default (NULL);
143   static_clock = gst_system_clock_obtain ();
144   fail_unless (static_clock != clock);
145   g_assert_cmpint (GST_OBJECT_REFCOUNT (clock), ==, 1);
146   g_assert_cmpint (GST_OBJECT_REFCOUNT (static_clock), ==, 2);
147   g_object_unref (clock);
148   g_object_unref (static_clock);
149 }
150
151 GST_END_TEST;
152
153 GST_START_TEST (test_single_shot)
154 {
155   GstClock *clock;
156   GstClockID id, id2;
157   GstClockTime base;
158   GstClockReturn result;
159
160   clock = gst_system_clock_obtain ();
161   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
162
163   gst_clock_debug (clock);
164   base = gst_clock_get_time (clock);
165
166   id = gst_clock_new_single_shot_id (clock, base + TIME_UNIT);
167   fail_unless (id != NULL, "Could not create single shot id");
168
169   GST_DEBUG ("waiting one time unit");
170   result = gst_clock_id_wait (id, NULL);
171   gst_clock_debug (clock);
172   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK (result=%d)",
173       result);
174   fail_unless (gst_clock_get_time (clock) > (base + TIME_UNIT),
175       "target time has not been reached");
176
177   GST_DEBUG ("waiting in the past");
178   result = gst_clock_id_wait (id, NULL);
179   gst_clock_debug (clock);
180   fail_unless (result == GST_CLOCK_EARLY,
181       "Waiting did not return EARLY(result=%d)", result);
182   gst_clock_id_unref (id);
183
184   id = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
185   GST_DEBUG ("waiting one second async id %p", id);
186   result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
187   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
188   g_usleep (TIME_UNIT / (2 * 1000));
189   gst_clock_id_unschedule (id);
190   gst_clock_id_unref (id);
191
192   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
193   GST_DEBUG ("waiting one second async, with cancel on id %p", id);
194   result = gst_clock_id_wait_async (id, error_callback, NULL, NULL);
195   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
196   g_usleep (TIME_UNIT / (2 * 1000));
197   GST_DEBUG ("cancel id %p after half a time unit", id);
198   gst_clock_id_unschedule (id);
199   gst_clock_id_unref (id);
200   GST_DEBUG ("canceled id %p", id);
201
202   GST_DEBUG ("waiting multiple one second async, with cancel");
203   id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
204   id2 = gst_clock_new_single_shot_id (clock, base + 6 * TIME_UNIT);
205   GST_DEBUG ("waiting id %p", id);
206   result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
207   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
208
209   GST_DEBUG ("waiting id %p", id2);
210   result = gst_clock_id_wait_async (id2, error_callback, NULL, NULL);
211   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
212   g_usleep (TIME_UNIT / (2 * 1000));
213   GST_DEBUG ("cancel id %p after half a time unit", id2);
214   gst_clock_id_unschedule (id2);
215   GST_DEBUG ("canceled id %p", id2);
216   gst_clock_id_unref (id2);
217
218   /* wait for the entry to time out */
219   g_usleep (TIME_UNIT / 1000 * 5);
220   fail_unless (((GstClockEntry *) id)->status == GST_CLOCK_OK,
221       "Waiting did not finish");
222   gst_clock_id_unref (id);
223
224   gst_object_unref (clock);
225 }
226
227 GST_END_TEST;
228
229 GST_START_TEST (test_periodic_shot)
230 {
231   GstClock *clock;
232   GstClockID id, id2;
233   GstClockTime base;
234   GstClockReturn result;
235
236   clock = gst_system_clock_obtain ();
237   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
238
239   gst_clock_debug (clock);
240   base = gst_clock_get_time (clock);
241
242   /* signal every half a time unit */
243   id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
244   fail_unless (id != NULL, "Could not create periodic id");
245
246   GST_DEBUG ("waiting one time unit");
247   result = gst_clock_id_wait (id, NULL);
248   gst_clock_debug (clock);
249   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
250
251   GST_DEBUG ("waiting for the next");
252   result = gst_clock_id_wait (id, NULL);
253   gst_clock_debug (clock);
254   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
255
256   GST_DEBUG ("waiting for the next async %p", id);
257   result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
258   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
259   g_usleep (TIME_UNIT / (2 * 1000));
260
261   GST_DEBUG ("waiting some more for the next async %p", id);
262   result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
263   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
264   g_usleep (TIME_UNIT / (2 * 1000));
265
266   id2 = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
267   fail_unless (id2 != NULL, "Could not create second periodic id");
268
269   GST_DEBUG ("waiting some more for another async %p", id2);
270   result = gst_clock_id_wait_async (id2, ok_callback, NULL, NULL);
271   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
272   g_usleep (TIME_UNIT / (2 * 1000));
273
274   GST_DEBUG ("unschedule %p", id);
275   gst_clock_id_unschedule (id);
276
277   /* entry cannot be used again */
278   result = gst_clock_id_wait_async (id, error_callback, NULL, NULL);
279   fail_unless (result == GST_CLOCK_UNSCHEDULED,
280       "Waiting did not return UNSCHEDULED");
281   result = gst_clock_id_wait (id, NULL);
282   fail_unless (result == GST_CLOCK_UNSCHEDULED,
283       "Waiting did not return UNSCHEDULED");
284   g_usleep (TIME_UNIT / (2 * 1000));
285
286   /* clean up */
287   gst_clock_id_unref (id);
288   gst_clock_id_unschedule (id2);
289   gst_clock_id_unref (id2);
290
291   gst_object_unref (clock);
292 }
293
294 GST_END_TEST;
295
296 GST_START_TEST (test_async_order)
297 {
298   GstClock *clock;
299   GstClockID id1, id2;
300   GList *cb_list = NULL, *next;
301   GstClockTime base;
302   GstClockReturn result;
303
304   clock = gst_system_clock_obtain ();
305   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
306
307   gst_clock_debug (clock);
308   base = gst_clock_get_time (clock);
309
310   id1 = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
311   id2 = gst_clock_new_single_shot_id (clock, base + 1 * TIME_UNIT);
312   result = gst_clock_id_wait_async (id1, store_callback, &cb_list, NULL);
313   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
314   g_usleep (TIME_UNIT / (2 * 1000));
315   result = gst_clock_id_wait_async (id2, store_callback, &cb_list, NULL);
316   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
317   g_usleep (TIME_UNIT / 1000);
318   /* at this point at least one of the timers should have timed out */
319   g_mutex_lock (&store_lock);
320   fail_unless (cb_list != NULL, "expected notification");
321   fail_unless (cb_list->data == id2,
322       "Expected notification for id2 to come first");
323   g_mutex_unlock (&store_lock);
324   g_usleep (TIME_UNIT / 1000);
325   g_mutex_lock (&store_lock);
326   /* now both should have timed out */
327   next = g_list_next (cb_list);
328   fail_unless (next != NULL, "expected second notification");
329   fail_unless (next->data == id1, "Missing notification for id1");
330   g_mutex_unlock (&store_lock);
331
332   gst_clock_id_unref (id1);
333   gst_clock_id_unref (id2);
334   g_list_free (cb_list);
335
336   gst_object_unref (clock);
337 }
338
339 GST_END_TEST;
340
341 struct test_async_sync_interaction_data
342 {
343   GMutex lock;
344
345   GstClockID sync_id;
346   GstClockID sync_id2;
347
348   GstClockID async_id;
349   GstClockID async_id2;
350   GstClockID async_id3;
351 };
352
353 static gboolean
354 test_async_sync_interaction_cb (GstClock * clock, GstClockTime time,
355     GstClockID id, gpointer user_data)
356 {
357   struct test_async_sync_interaction_data *td =
358       (struct test_async_sync_interaction_data *) (user_data);
359
360   g_mutex_lock (&td->lock);
361   /* The first async callback is ignored */
362   if (id == td->async_id)
363     goto out;
364
365   if (id != td->async_id2 && id != td->async_id3)
366     goto out;
367
368   /* Unschedule the sync callback */
369   if (id == td->async_id3) {
370     gst_clock_id_unschedule (td->sync_id);
371     gst_clock_id_unschedule (td->async_id2);
372   }
373 out:
374   g_mutex_unlock (&td->lock);
375   return FALSE;
376 }
377
378 GST_START_TEST (test_async_sync_interaction)
379 {
380   /* This test schedules an async callback, then before it completes, schedules
381    * an earlier async callback, and quickly unschedules the first, and inserts
382    * a THIRD even earlier async callback. It then attempts to wait on a
383    * sync clock ID. While that's sleeping, the 3rd async callback should fire
384    * and unschedule it. This tests for problems with unscheduling async and
385    * sync callbacks on the system clock. */
386   GstClock *clock;
387   GstClockReturn result;
388   GstClockTime base;
389   GstClockTimeDiff jitter;
390   struct test_async_sync_interaction_data td;
391   int i;
392
393   clock = gst_system_clock_obtain ();
394   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
395
396   g_mutex_init (&td.lock);
397
398   for (i = 0; i < 50; i++) {
399     gst_clock_debug (clock);
400     base = gst_clock_get_time (clock);
401     g_mutex_lock (&td.lock);
402     td.async_id = gst_clock_new_single_shot_id (clock, base + 40 * GST_MSECOND);
403     td.async_id2 =
404         gst_clock_new_single_shot_id (clock, base + 30 * GST_MSECOND);
405     td.async_id3 =
406         gst_clock_new_single_shot_id (clock, base + 20 * GST_MSECOND);
407     td.sync_id2 = gst_clock_new_single_shot_id (clock, base + 10 * GST_MSECOND);
408     td.sync_id = gst_clock_new_single_shot_id (clock, base + 50 * GST_MSECOND);
409     g_mutex_unlock (&td.lock);
410
411     result = gst_clock_id_wait_async (td.async_id,
412         test_async_sync_interaction_cb, &td, NULL);
413     fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
414
415     /* Wait 10ms, then unschedule async_id and schedule async_id2 */
416     result = gst_clock_id_wait (td.sync_id2, &jitter);
417     fail_unless (result == GST_CLOCK_OK || result == GST_CLOCK_EARLY,
418         "Waiting did not return OK or EARLY");
419     /* async_id2 is earlier than async_id - should become head of the queue */
420     result = gst_clock_id_wait_async (td.async_id2,
421         test_async_sync_interaction_cb, &td, NULL);
422     fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
423     gst_clock_id_unschedule (td.async_id);
424
425     /* async_id3 is earlier than async_id2 - should become head of the queue */
426     result = gst_clock_id_wait_async (td.async_id3,
427         test_async_sync_interaction_cb, &td, NULL);
428     fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
429
430     /* While this is sleeping, the async3 id should fire and unschedule it */
431     result = gst_clock_id_wait (td.sync_id, &jitter);
432     fail_unless (result == GST_CLOCK_UNSCHEDULED || result == GST_CLOCK_EARLY,
433         "Waiting did not return UNSCHEDULED (was %d)", result);
434
435     gst_clock_id_unschedule (td.async_id3);
436     g_mutex_lock (&td.lock);
437
438     gst_clock_id_unref (td.sync_id);
439     gst_clock_id_unref (td.sync_id2);
440     gst_clock_id_unref (td.async_id);
441     gst_clock_id_unref (td.async_id2);
442     gst_clock_id_unref (td.async_id3);
443     g_mutex_unlock (&td.lock);
444   }
445
446   g_mutex_clear (&td.lock);
447   gst_object_unref (clock);
448 }
449
450 GST_END_TEST;
451
452 GST_START_TEST (test_periodic_multi)
453 {
454   GstClock *clock;
455   GstClockID clock_id;
456   GstClockID clock_id_async;
457   GstClockTime base;
458   GstClockReturn result;
459   gboolean got_callback = FALSE;
460
461   clock = gst_system_clock_obtain ();
462   fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
463
464   gst_clock_debug (clock);
465   base = gst_clock_get_time (clock);
466
467   clock_id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT);
468   gst_clock_id_wait (clock_id, NULL);
469   fail_unless (gst_clock_get_time (clock) >= base + TIME_UNIT);
470   fail_unless (gst_clock_get_time (clock) < base + 2 * TIME_UNIT);
471
472   /* now perform a concurrent wait and wait_async */
473
474   clock_id_async =
475       gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT);
476   result =
477       gst_clock_id_wait_async (clock_id_async, notify_callback, &got_callback,
478       NULL);
479   fail_unless (result == GST_CLOCK_OK, "Async waiting did not return OK");
480
481   result = gst_clock_id_wait (clock_id, NULL);
482   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
483   fail_unless (gst_clock_get_time (clock) >= base + 2 * TIME_UNIT);
484   /* give the async thread some time to call our callback: */
485   g_usleep (TIME_UNIT / (10 * 1000));
486   fail_unless (got_callback == TRUE, "got no async callback (1)");
487   fail_unless (gst_clock_get_time (clock) < base + 3 * TIME_UNIT);
488   got_callback = FALSE;
489
490   result = gst_clock_id_wait (clock_id, NULL);
491   fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
492   fail_unless (gst_clock_get_time (clock) >= base + 3 * TIME_UNIT);
493   /* give the async thread some time to call our callback: */
494   g_usleep (TIME_UNIT / (10 * 1000));
495   fail_unless (got_callback == TRUE, "got no async callback (2)");
496   fail_unless (gst_clock_get_time (clock) < base + 4 * TIME_UNIT);
497
498   /* clean up */
499   gst_clock_id_unref (clock_id);
500   gst_clock_id_unschedule (clock_id_async);
501   gst_clock_id_unref (clock_id_async);
502   gst_object_unref (clock);
503 }
504
505 GST_END_TEST;
506
507 GST_START_TEST (test_diff)
508 {
509   GstClockTime time1[] = { 0, (GstClockTime) - 1, 0, 1, 2 * GST_SECOND,
510     (GstClockTime) - GST_SECOND, (GstClockTime) - GST_SECOND
511   };
512   GstClockTime time2[] =
513       { 0, 1, 1, 0, 1 * GST_SECOND, (GstClockTime) - GST_SECOND, GST_SECOND };
514   GstClockTimeDiff d[] = { 0, 2, 1, -1, -GST_SECOND, 0, 2 * GST_SECOND };
515   guint i;
516
517   for (i = 0; i < G_N_ELEMENTS (d); i++) {
518     fail_if (d[i] != GST_CLOCK_DIFF (time1[i], time2[i]));
519   }
520 }
521
522 GST_END_TEST;
523
524 /* test if a blocking wait, unblocked by an async entry continues to be
525  * scheduled */
526 typedef struct
527 {
528   GstClock *clock;
529   GstClockID id;
530   GstClockTimeDiff jitter;
531   GstClockReturn ret;
532 } MixedInfo;
533
534 static gpointer
535 mixed_thread (MixedInfo * info)
536 {
537   info->ret = gst_clock_id_wait (info->id, &info->jitter);
538   return NULL;
539 }
540
541 static gboolean
542 mixed_async_cb (GstClock * clock, GstClockTime time,
543     GstClockID id, gpointer user_data)
544 {
545   return TRUE;
546 }
547
548 GST_START_TEST (test_mixed)
549 {
550   GThread *thread;
551   GError *error = NULL;
552   MixedInfo info;
553   GstClockTime base;
554   GstClockID id;
555
556   info.clock = gst_system_clock_obtain ();
557   fail_unless (info.clock != NULL,
558       "Could not create instance of GstSystemClock");
559
560   /* get current time of the clock as base time */
561   base = gst_clock_get_time (info.clock);
562
563   /* create entry to wait for 1 second */
564   info.id = gst_clock_new_single_shot_id (info.clock, base + GST_SECOND);
565
566   /* make and start an entry that is scheduled every 10ms */
567   id = gst_clock_new_periodic_id (info.clock, base, 10 * GST_MSECOND);
568
569   /* start waiting for the entry */
570   thread =
571       g_thread_try_new ("gst-check", (GThreadFunc) mixed_thread, &info, &error);
572   fail_unless (error == NULL, "error creating thread");
573   fail_unless (thread != NULL, "Could not create thread");
574
575   /* wait half a second so we are sure to be in the thread */
576   g_usleep (G_USEC_PER_SEC / 2);
577
578   /* start scheduling the entry */
579   gst_clock_id_wait_async (id, mixed_async_cb, NULL, NULL);
580
581   /* wait for thread to finish */
582   g_thread_join (thread);
583   /* entry must have timed out correctly */
584   fail_unless (info.ret == GST_CLOCK_OK, "clock return was %d", info.ret);
585
586   gst_clock_id_unschedule (id);
587   gst_clock_id_unref (id);
588   gst_clock_id_unref (info.id);
589   gst_object_unref (info.clock);
590 }
591
592 GST_END_TEST;
593
594 static gboolean
595 test_async_full_slave_callback (GstClock * master, GstClockTime time,
596     GstClockID id, GstClock * clock)
597 {
598   GstClockTime stime, mtime;
599   gdouble r_squared;
600
601   /* notify the test case that we started */
602   GST_INFO ("callback started");
603   g_mutex_lock (&af_lock);
604   g_cond_signal (&af_cond);
605
606   /* wait for the test case to unref "clock" and signal */
607   GST_INFO ("waiting for test case to signal");
608   g_cond_wait (&af_cond, &af_lock);
609
610   stime = gst_clock_get_internal_time (clock);
611   mtime = gst_clock_get_time (master);
612
613   gst_clock_add_observation (clock, stime, mtime, &r_squared);
614
615   g_cond_signal (&af_cond);
616   g_mutex_unlock (&af_lock);
617   GST_INFO ("callback finished");
618
619   return TRUE;
620 }
621
622 GST_START_TEST (test_async_full)
623 {
624   GstClock *master, *slave;
625   GstClockID *clockid;
626
627   /* create master and slave */
628   master =
629       g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "TestClockMaster", NULL);
630   slave = g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "TestClockMaster", NULL);
631   GST_OBJECT_FLAG_SET (slave, GST_CLOCK_FLAG_CAN_SET_MASTER);
632   g_object_set (slave, "timeout", 50 * GST_MSECOND, NULL);
633
634   fail_unless (GST_OBJECT_REFCOUNT (master) == 1);
635   fail_unless (GST_OBJECT_REFCOUNT (slave) == 1);
636
637   /* register a periodic shot on the master to calibrate the slave */
638   g_mutex_lock (&af_lock);
639   clockid = gst_clock_new_periodic_id (master,
640       gst_clock_get_time (master), gst_clock_get_timeout (slave));
641   gst_clock_id_wait_async (clockid,
642       (GstClockCallback) test_async_full_slave_callback,
643       gst_object_ref (slave), (GDestroyNotify) gst_object_unref);
644
645   /* wait for the shot to be fired and test_async_full_slave_callback to be
646    * called */
647   GST_INFO ("waiting for the slave callback to start");
648   g_cond_wait (&af_cond, &af_lock);
649   GST_INFO ("slave callback running, unreffing slave");
650
651   /* unref the slave clock while the slave_callback is running. This should be
652    * safe since the master clock now stores a ref to the slave */
653   gst_object_unref (slave);
654
655   /* unref the clock entry. This should be safe as well since the clock thread
656    * refs the entry before executing it */
657   gst_clock_id_unschedule (clockid);
658   gst_clock_id_unref (clockid);
659
660   /* signal and wait for the callback to complete */
661   g_cond_signal (&af_cond);
662
663   GST_INFO ("waiting for callback to finish");
664   g_cond_wait (&af_cond, &af_lock);
665   GST_INFO ("callback finished");
666   g_mutex_unlock (&af_lock);
667
668   gst_object_unref (master);
669 }
670
671 GST_END_TEST;
672
673 GST_START_TEST (test_resolution)
674 {
675   GstClock *clock;
676   GstClockTime now_t, prev_t, resolution;
677   int i;
678
679   now_t = prev_t = GST_CLOCK_TIME_NONE;
680   clock = gst_system_clock_obtain ();
681   fail_unless (clock != NULL, "Could not create default system clock");
682   resolution = gst_clock_get_resolution (clock);
683   fail_unless (resolution != GST_CLOCK_TIME_NONE);
684
685   for (i = 0; i < 100000; ++i) {
686     now_t = gst_clock_get_internal_time (clock);
687     fail_unless (now_t != GST_CLOCK_TIME_NONE);
688     if (prev_t != GST_CLOCK_TIME_NONE) {
689       GstClockTime diff;
690       fail_unless (now_t >= prev_t);
691       diff = now_t - prev_t;
692       fail_unless (diff == 0 || diff >= resolution);
693     }
694     prev_t = now_t;
695     g_thread_yield ();
696   }
697   g_object_unref (clock);
698   clock = NULL;
699 }
700
701 GST_END_TEST;
702
703 static Suite *
704 gst_systemclock_suite (void)
705 {
706   Suite *s = suite_create ("GstSystemClock");
707   TCase *tc_chain = tcase_create ("waiting");
708
709   suite_add_tcase (s, tc_chain);
710   tcase_add_test (tc_chain, test_range);
711   tcase_add_test (tc_chain, test_signedness);
712   tcase_add_test (tc_chain, test_single_shot);
713   tcase_add_test (tc_chain, test_periodic_shot);
714   tcase_add_test (tc_chain, test_periodic_multi);
715   tcase_add_test (tc_chain, test_async_order);
716   tcase_add_test (tc_chain, test_async_sync_interaction);
717   tcase_add_test (tc_chain, test_diff);
718   tcase_add_test (tc_chain, test_mixed);
719   tcase_add_test (tc_chain, test_async_full);
720   tcase_add_test (tc_chain, test_set_default);
721   tcase_add_test (tc_chain, test_resolution);
722
723   return s;
724 }
725
726 GST_CHECK_MAIN (gst_systemclock);