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 GST_START_TEST (test_signedness)
26 GstClockTime time[] = { 0, 1, G_MAXUINT64 / GST_SECOND };
27 GstClockTimeDiff diff[] =
28 { 0, 1, -1, G_MAXINT64 / GST_SECOND, G_MININT64 / GST_SECOND };
31 for (i = 0; i < G_N_ELEMENTS (time); i++) {
32 fail_if (time[i] != (time[i] * GST_SECOND / GST_SECOND));
34 for (i = 0; i < G_N_ELEMENTS (diff); i++) {
35 fail_if (diff[i] != (diff[i] * GST_SECOND / GST_SECOND));
40 #define TIME_UNIT (GST_SECOND / 5)
42 gst_clock_debug (GstClock * clock)
46 time = gst_clock_get_time (clock);
47 g_message ("Clock info: time %" G_GUINT64_FORMAT "\n", time);
51 ok_callback (GstClock * clock, GstClockTime time,
52 GstClockID id, gpointer user_data)
54 g_message ("unlocked async id %p\n", id);
59 error_callback (GstClock * clock, GstClockTime time,
60 GstClockID id, gpointer user_data)
62 g_message ("unlocked unscheduled async id %p, this is wrong\n", id);
68 GST_START_TEST (test_single_shot)
73 GstClockReturn result;
75 clock = gst_system_clock_obtain ();
76 fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
78 gst_clock_debug (clock);
79 base = gst_clock_get_time (clock);
81 id = gst_clock_new_single_shot_id (clock, base + TIME_UNIT);
82 fail_unless (id != NULL, "Could not create single shot id");
84 g_message ("waiting one second\n");
85 result = gst_clock_id_wait (id, NULL);
86 gst_clock_debug (clock);
87 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
89 g_message ("waiting in the past\n");
90 result = gst_clock_id_wait (id, NULL);
91 gst_clock_debug (clock);
92 fail_unless (result == GST_CLOCK_EARLY, "Waiting did not return EARLY");
93 gst_clock_id_unref (id);
95 id = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
96 g_message ("waiting one second async id %p\n", id);
97 result = gst_clock_id_wait_async (id, ok_callback, NULL);
98 gst_clock_id_unref (id);
99 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
100 g_usleep (TIME_UNIT / (2 * 1000));
102 id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
103 g_message ("waiting one second async, with cancel on id %p\n", id);
104 result = gst_clock_id_wait_async (id, error_callback, NULL);
105 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
106 g_usleep (TIME_UNIT / (2 * 1000));
107 g_message ("cancel id %p after half a time unit\n", id);
108 gst_clock_id_unschedule (id);
109 gst_clock_id_unref (id);
110 g_message ("canceled id %p\n", id);
112 g_message ("waiting multiple one second async, with cancel\n");
113 id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
114 id2 = gst_clock_new_single_shot_id (clock, base + 6 * TIME_UNIT);
115 g_message ("waiting id %p\n", id);
116 result = gst_clock_id_wait_async (id, ok_callback, NULL);
117 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
118 gst_clock_id_unref (id);
119 g_message ("waiting id %p\n", id2);
120 result = gst_clock_id_wait_async (id2, error_callback, NULL);
121 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
122 g_usleep (TIME_UNIT / (2 * 1000));
123 g_message ("cancel id %p after half a time unit\n", id2);
124 gst_clock_id_unschedule (id2);
125 g_message ("canceled id %p\n", id2);
126 gst_clock_id_unref (id2);
127 g_usleep (TIME_UNIT / (2 * 1000));
131 GST_START_TEST (test_periodic_shot)
136 GstClockReturn result;
138 clock = gst_system_clock_obtain ();
139 fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
141 gst_clock_debug (clock);
142 base = gst_clock_get_time (clock);
144 /* signal every half a time unit */
145 id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
146 fail_unless (id != NULL, "Could not create periodic id");
148 g_message ("waiting one time unit\n");
149 result = gst_clock_id_wait (id, NULL);
150 gst_clock_debug (clock);
151 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
153 g_message ("waiting for the next\n");
154 result = gst_clock_id_wait (id, NULL);
155 gst_clock_debug (clock);
156 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
158 g_message ("waiting for the next async %p\n", id);
159 result = gst_clock_id_wait_async (id, ok_callback, NULL);
160 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
161 g_usleep (TIME_UNIT / (2 * 1000));
163 g_message ("waiting some more for the next async %p\n", id);
164 result = gst_clock_id_wait_async (id, ok_callback, NULL);
165 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
166 g_usleep (TIME_UNIT / (2 * 1000));
168 id2 = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
169 fail_unless (id2 != NULL, "Could not create second periodic id");
171 g_message ("waiting some more for another async %p\n", id2);
172 result = gst_clock_id_wait_async (id2, ok_callback, NULL);
173 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
174 g_usleep (TIME_UNIT / (2 * 1000));
176 g_message ("unschedule %p\n", id);
177 gst_clock_id_unschedule (id);
179 /* entry cannot be used again */
180 result = gst_clock_id_wait_async (id, error_callback, NULL);
181 fail_unless (result == GST_CLOCK_UNSCHEDULED,
182 "Waiting did not return UNSCHEDULED");
183 result = gst_clock_id_wait (id, NULL);
184 fail_unless (result == GST_CLOCK_UNSCHEDULED,
185 "Waiting did not return UNSCHEDULED");
186 g_usleep (TIME_UNIT / (2 * 1000));
189 gst_clock_id_unref (id);
193 GST_START_TEST (test_diff)
195 GstClockTime time1[] =
196 { 0, -1, 0, 1, 2 * GST_SECOND, -GST_SECOND, -GST_SECOND };
197 GstClockTime time2[] =
198 { 0, 1, 1, 0, 1 * GST_SECOND, -GST_SECOND, GST_SECOND };
199 GstClockTimeDiff d[] = { 0, 2, 1, -1, -GST_SECOND, 0, 2 * GST_SECOND };
202 for (i = 0; i < G_N_ELEMENTS (d); i++) {
203 fail_if (d[i] != GST_CLOCK_DIFF (time1[i], time2[i]));
207 gst_systemclock_suite (void)
209 Suite *s = suite_create ("GstSystemClock");
210 TCase *tc_chain = tcase_create ("waiting");
212 suite_add_tcase (s, tc_chain);
213 tcase_add_test (tc_chain, test_signedness);
214 tcase_add_test (tc_chain, test_single_shot);
215 tcase_add_test (tc_chain, test_periodic_shot);
216 tcase_add_test (tc_chain, test_diff);
221 GST_CHECK_MAIN (gst_systemclock);