2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
4 * gstsystemclock.c: Unit test for GstSystemClock
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.
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.
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.
22 #include <gst/check/gstcheck.h>
24 static GMutex *af_lock;
25 static GCond *af_cond;
27 /* see if the defines make sense */
28 GST_START_TEST (test_range)
30 GstClockTime time, time2;
33 fail_unless (time == G_GUINT64_CONSTANT (1000000000));
36 fail_unless (time2 == 1000000);
37 fail_unless (time2 == GST_MSECOND);
38 fail_unless (time2 == GST_TIME_AS_USECONDS (time));
40 time2 = time / 1000000;
41 fail_unless (time2 == 1000);
42 fail_unless (time2 == GST_USECOND);
43 fail_unless (time2 == GST_TIME_AS_MSECONDS (time));
48 GST_START_TEST (test_signedness)
50 GstClockTime time[] = { 0, 1, G_MAXUINT64 / GST_SECOND };
51 GstClockTimeDiff diff[] =
52 { 0, 1, -1, G_MAXINT64 / GST_SECOND, G_MININT64 / GST_SECOND };
55 for (i = 0; i < G_N_ELEMENTS (time); i++) {
56 fail_if (time[i] != (time[i] * GST_SECOND / GST_SECOND));
58 for (i = 0; i < G_N_ELEMENTS (diff); i++) {
59 fail_if (diff[i] != (diff[i] * GST_SECOND / GST_SECOND));
65 #define TIME_UNIT (GST_SECOND / 5)
67 gst_clock_debug (GstClock * clock)
71 time = gst_clock_get_time (clock);
72 GST_DEBUG ("Clock info: time %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
76 ok_callback (GstClock * clock, GstClockTime time,
77 GstClockID id, gpointer user_data)
79 GST_LOG ("unlocked async id %p", id);
84 error_callback (GstClock * clock, GstClockTime time,
85 GstClockID id, gpointer user_data)
87 GST_WARNING ("unlocked unscheduled async id %p, this is wrong", id);
96 store_callback (GstClock * clock, GstClockTime time,
97 GstClockID id, gpointer user_data)
99 GList **list = user_data;
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);
109 notify_callback (GstClock * clock, GstClockTime time,
110 GstClockID id, gpointer user_data)
112 gboolean *ret = (gboolean *) user_data;
120 GST_START_TEST (test_single_shot)
125 GstClockReturn result;
127 clock = gst_system_clock_obtain ();
128 fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
130 gst_clock_debug (clock);
131 base = gst_clock_get_time (clock);
133 id = gst_clock_new_single_shot_id (clock, base + TIME_UNIT);
134 fail_unless (id != NULL, "Could not create single shot id");
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)",
141 fail_unless (gst_clock_get_time (clock) > (base + TIME_UNIT),
142 "target time has not been reached");
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);
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);
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);
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");
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);
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);
191 gst_object_unref (clock);
196 GST_START_TEST (test_periodic_shot)
201 GstClockReturn result;
203 clock = gst_system_clock_obtain ();
204 fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
206 gst_clock_debug (clock);
207 base = gst_clock_get_time (clock);
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");
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");
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");
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));
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));
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");
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));
241 GST_DEBUG ("unschedule %p", id);
242 gst_clock_id_unschedule (id);
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));
254 gst_clock_id_unref (id);
255 gst_clock_id_unschedule (id2);
256 gst_clock_id_unref (id2);
258 gst_object_unref (clock);
263 GST_START_TEST (test_async_order)
267 GList *cb_list = NULL, *next;
269 GstClockReturn result;
271 store_lock = g_mutex_new ();
273 clock = gst_system_clock_obtain ();
274 fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
276 gst_clock_debug (clock);
277 base = gst_clock_get_time (clock);
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);
301 gst_clock_id_unref (id1);
302 gst_clock_id_unref (id2);
303 g_list_free (cb_list);
305 gst_object_unref (clock);
306 g_mutex_free (store_lock);
311 struct test_async_sync_interaction_data
319 GstClockID async_id2;
320 GstClockID async_id3;
324 test_async_sync_interaction_cb (GstClock * clock, GstClockTime time,
325 GstClockID id, gpointer user_data)
327 struct test_async_sync_interaction_data *td =
328 (struct test_async_sync_interaction_data *) (user_data);
330 g_mutex_lock (td->lock);
331 /* The first async callback is ignored */
332 if (id == td->async_id)
335 if (id != td->async_id2 && id != td->async_id3)
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);
344 g_mutex_unlock (td->lock);
348 GST_START_TEST (test_async_sync_interaction)
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. */
357 GstClockReturn result;
359 GstClockTimeDiff jitter;
360 struct test_async_sync_interaction_data td;
363 clock = gst_system_clock_obtain ();
364 fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
366 td.lock = g_mutex_new ();
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);
374 gst_clock_new_single_shot_id (clock, base + 30 * GST_MSECOND);
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);
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");
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);
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");
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);
405 gst_clock_id_unschedule (td.async_id3);
406 g_mutex_lock (td.lock);
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);
416 g_mutex_free (td.lock);
417 gst_object_unref (clock);
422 GST_START_TEST (test_periodic_multi)
426 GstClockID clock_id_async;
428 GstClockReturn result;
429 gboolean got_callback = FALSE;
431 clock = gst_system_clock_obtain ();
432 fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
434 gst_clock_debug (clock);
435 base = gst_clock_get_time (clock);
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);
442 /* now perform a concurrent wait and wait_async */
445 gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT);
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");
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;
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);
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);
476 GST_START_TEST (test_diff)
478 GstClockTime time1[] = { 0, (GstClockTime) - 1, 0, 1, 2 * GST_SECOND,
479 (GstClockTime) - GST_SECOND, (GstClockTime) - GST_SECOND
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 };
486 for (i = 0; i < G_N_ELEMENTS (d); i++) {
487 fail_if (d[i] != GST_CLOCK_DIFF (time1[i], time2[i]));
493 /* test if a blocking wait, unblocked by an async entry continues to be
499 GstClockTimeDiff jitter;
504 mixed_thread (MixedInfo * info)
506 info->ret = gst_clock_id_wait (info->id, &info->jitter);
511 mixed_async_cb (GstClock * clock, GstClockTime time,
512 GstClockID id, gpointer user_data)
517 GST_START_TEST (test_mixed)
520 GError *error = NULL;
525 info.clock = gst_system_clock_obtain ();
526 fail_unless (info.clock != NULL,
527 "Could not create instance of GstSystemClock");
529 /* get current time of the clock as base time */
530 base = gst_clock_get_time (info.clock);
532 /* create entry to wait for 1 second */
533 info.id = gst_clock_new_single_shot_id (info.clock, base + GST_SECOND);
535 /* make and start an entry that is scheduled every 10ms */
536 id = gst_clock_new_periodic_id (info.clock, base, 10 * GST_MSECOND);
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");
543 /* wait half a second so we are sure to be in the thread */
544 g_usleep (G_USEC_PER_SEC / 2);
546 /* start scheduling the entry */
547 gst_clock_id_wait_async (id, mixed_async_cb, NULL);
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);
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);
563 test_async_full_slave_callback (GstClock * master, GstClockTime time,
564 GstClockID id, GstClock * clock)
566 GstClockTime stime, mtime;
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);
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);
578 stime = gst_clock_get_internal_time (clock);
579 mtime = gst_clock_get_time (master);
581 gst_clock_add_observation (clock, stime, mtime, &r_squared);
583 g_cond_signal (af_cond);
584 g_mutex_unlock (af_lock);
585 GST_INFO ("callback finished");
590 GST_START_TEST (test_async_full)
592 GstClock *master, *slave;
595 af_lock = g_mutex_new ();
596 af_cond = g_cond_new ();
598 /* create master and slave */
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);
605 fail_unless (GST_OBJECT_REFCOUNT (master) == 1);
606 fail_unless (GST_OBJECT_REFCOUNT (slave) == 1);
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);
616 /* wait for the shot to be fired and test_async_full_slave_callback to be
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");
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);
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);
631 /* signal and wait for the callback to complete */
632 g_cond_signal (af_cond);
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);
639 gst_object_unref (master);
641 g_mutex_free (af_lock);
642 g_cond_free (af_cond);
648 gst_systemclock_suite (void)
650 Suite *s = suite_create ("GstSystemClock");
651 TCase *tc_chain = tcase_create ("waiting");
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);
668 GST_CHECK_MAIN (gst_systemclock);