1 /* |
1 /* |
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. |
2 * Copyright © 2008 Nokia Corporation. |
3 * |
3 * This material, including documentation and any related |
4 * This library is free software; you can redistribute it and/or |
4 * computer programs, is protected by copyright controlled by |
5 * modify it under the terms of the GNU Lesser General Public |
5 * Nokia Corporation. All rights are reserved. Copying, |
6 * License as published by the Free Software Foundation; either |
6 * including reproducing, storing, adapting or translating, any |
7 * version 2 of the License, or (at your option) any later version. |
7 * or all of this material requires the prior written consent of |
8 * |
8 * Nokia Corporation. This material also contains confidential |
9 * This library is distributed in the hope that it will be useful, |
9 * information which may not be disclosed to others without the |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 * prior written consent of Nokia Corporation. |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 * ============================================================================ |
12 * Lesser General Public License for more details. |
12 */ |
13 * |
|
14 * You should have received a copy of the GNU Lesser General Public |
|
15 * License along with this library; if not, write to the |
|
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
17 * Boston, MA 02111-1307, USA. |
|
18 * |
|
19 * Description: |
|
20 * |
|
21 */ |
|
22 |
13 |
23 /* GStreamer |
14 /* GStreamer |
24 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com> |
15 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com> |
25 * |
16 * |
26 * gstsystemclock.c: Unit test for GstSystemClock |
17 * gstsystemclock.c: Unit test for GstSystemClock |
120 fail_if (TRUE); |
120 fail_if (TRUE); |
121 |
121 |
122 return FALSE; |
122 return FALSE; |
123 } |
123 } |
124 |
124 |
|
125 GMutex *store_lock; |
125 static gboolean |
126 static gboolean |
126 store_callback (GstClock * clock, GstClockTime time, |
127 store_callback (GstClock * clock, GstClockTime time, |
127 GstClockID id, gpointer user_data) |
128 GstClockID id, gpointer user_data) |
128 { |
129 { |
129 GList **list = user_data; |
130 GList **list = user_data; |
130 |
131 |
131 GST_DEBUG ("unlocked async id %p", id); |
132 GST_DEBUG ("unlocked async id %p", id); |
|
133 g_mutex_lock (store_lock); |
132 *list = g_list_append (*list, id); |
134 *list = g_list_append (*list, id); |
|
135 g_mutex_unlock (store_lock); |
133 return FALSE; |
136 return FALSE; |
134 } |
137 } |
135 |
138 |
136 static gboolean |
139 static gboolean |
137 notify_callback (GstClock * clock, GstClockTime time, |
140 notify_callback (GstClock * clock, GstClockTime time, |
182 fail_unless (result == GST_CLOCK_EARLY, |
188 fail_unless (result == GST_CLOCK_EARLY, |
183 "Waiting did not return EARLY(result=%d)", result); |
189 "Waiting did not return EARLY(result=%d)", result); |
184 |
190 |
185 gst_clock_id_unref (id); |
191 gst_clock_id_unref (id); |
186 |
192 |
187 id = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT); |
193 id = gst_clock_new_single_shot_id (clock, base + (2 * TIME_UNIT)); |
188 GST_DEBUG ("waiting one second async id %p", id); |
194 GST_DEBUG ("waiting one second async id %p", id); |
189 result = gst_clock_id_wait_async (id, ok_callback, NULL); |
195 result = gst_clock_id_wait_async (id, ok_callback, NULL); |
190 gst_clock_id_unref (id); |
196 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
191 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
197 |
192 |
198 g_usleep (TIME_UNIT / (2 * 1000)); |
193 g_usleep (TIME_UNIT / (2 * 1000)); |
199 gst_clock_id_unschedule (id); |
194 |
200 gst_clock_id_unref (id); |
195 id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT); |
201 |
|
202 id = gst_clock_new_single_shot_id (clock, base + (5 * TIME_UNIT)); |
196 GST_DEBUG ("waiting one second async, with cancel on id %p", id); |
203 GST_DEBUG ("waiting one second async, with cancel on id %p", id); |
197 result = gst_clock_id_wait_async (id, error_callback, NULL); |
204 result = gst_clock_id_wait_async (id, error_callback, NULL); |
198 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
205 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
199 |
206 |
200 g_usleep (TIME_UNIT / (2 * 1000)); |
207 g_usleep (TIME_UNIT / (2 * 1000)); |
202 gst_clock_id_unschedule (id); |
209 gst_clock_id_unschedule (id); |
203 gst_clock_id_unref (id); |
210 gst_clock_id_unref (id); |
204 GST_DEBUG ("canceled id %p", id); |
211 GST_DEBUG ("canceled id %p", id); |
205 |
212 |
206 GST_DEBUG ("waiting multiple one second async, with cancel"); |
213 GST_DEBUG ("waiting multiple one second async, with cancel"); |
207 id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT); |
214 id = gst_clock_new_single_shot_id (clock, base + (5 * TIME_UNIT)); |
208 id2 = gst_clock_new_single_shot_id (clock, base + 6 * TIME_UNIT); |
215 id2 = gst_clock_new_single_shot_id (clock, base + (6 * TIME_UNIT)); |
209 GST_DEBUG ("waiting id %p", id); |
216 GST_DEBUG ("waiting id %p", id); |
210 result = gst_clock_id_wait_async (id, ok_callback, NULL); |
217 result = gst_clock_id_wait_async (id, ok_callback, NULL); |
211 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
218 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
212 |
219 |
213 gst_clock_id_unref (id); |
|
214 GST_DEBUG ("waiting id %p", id2); |
220 GST_DEBUG ("waiting id %p", id2); |
215 result = gst_clock_id_wait_async (id2, error_callback, NULL); |
221 result = gst_clock_id_wait_async (id2, error_callback, NULL); |
216 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
222 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
217 |
223 |
218 g_usleep (TIME_UNIT / (2 * 1000)); |
224 g_usleep (TIME_UNIT / (2 * 1000)); |
219 GST_DEBUG ("cancel id %p after half a time unit", id2); |
225 GST_DEBUG ("cancel id %p after half a time unit", id2); |
220 gst_clock_id_unschedule (id2); |
226 gst_clock_id_unschedule (id2); |
221 GST_DEBUG ("canceled id %p", id2); |
227 GST_DEBUG ("canceled id %p", id2); |
222 gst_clock_id_unref (id2); |
228 gst_clock_id_unref (id2); |
223 g_usleep (TIME_UNIT / (2 * 1000)); |
229 g_usleep (TIME_UNIT / 1000 * 5); |
224 |
230 fail_unless (((GstClockEntry *) id)->status == GST_CLOCK_OK, |
|
231 "Waiting did not finish"); |
|
232 gst_clock_id_unref (id); |
|
233 |
|
234 gst_object_unref (clock); |
|
235 GST_DEBUG ("test_single_shot finished "); |
225 std_log(LOG_FILENAME_LINE, "Test Successful"); |
236 std_log(LOG_FILENAME_LINE, "Test Successful"); |
226 create_xml(0); |
237 create_xml(0); |
227 } |
238 } |
228 |
239 |
229 |
240 |
333 result = gst_clock_id_wait_async (id2, store_callback, &cb_list); |
345 result = gst_clock_id_wait_async (id2, store_callback, &cb_list); |
334 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
346 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
335 |
347 |
336 g_usleep (TIME_UNIT / 1000); |
348 g_usleep (TIME_UNIT / 1000); |
337 // at this point at least one of the timers should have timed out |
349 // at this point at least one of the timers should have timed out |
|
350 g_mutex_lock (store_lock); |
338 fail_unless (cb_list != NULL, "expected notification"); |
351 fail_unless (cb_list != NULL, "expected notification"); |
339 |
352 |
340 fail_unless (cb_list->data == id2, |
353 fail_unless (cb_list->data == id2, |
341 "Expected notification for id2 to come first"); |
354 "Expected notification for id2 to come first"); |
342 |
355 g_mutex_unlock (store_lock); |
343 g_usleep (TIME_UNIT / 1000); |
356 g_usleep (TIME_UNIT / 1000); |
|
357 g_mutex_lock (store_lock); |
344 // now both should have timed out |
358 // now both should have timed out |
345 next = g_list_next (cb_list); |
359 next = g_list_next (cb_list); |
346 fail_unless (next != NULL, "expected second notification"); |
360 fail_unless (next != NULL, "expected second notification"); |
347 |
361 |
348 fail_unless (next->data == id1, "Missing notification for id1"); |
362 fail_unless (next->data == id1, "Missing notification for id1"); |
|
363 g_mutex_unlock (store_lock); |
349 |
364 |
350 gst_clock_id_unref (id1); |
365 gst_clock_id_unref (id1); |
351 gst_clock_id_unref (id2); |
366 gst_clock_id_unref (id2); |
352 g_list_free (cb_list); |
367 g_list_free (cb_list); |
|
368 gst_object_unref (clock); |
|
369 g_mutex_free (store_lock); |
353 |
370 |
354 std_log(LOG_FILENAME_LINE, "Test Successful"); |
371 std_log(LOG_FILENAME_LINE, "Test Successful"); |
355 create_xml(0); |
372 create_xml(0); |
356 } |
373 } |
357 |
374 |
540 std_log(LOG_FILENAME_LINE, "Test Successful"); |
558 std_log(LOG_FILENAME_LINE, "Test Successful"); |
541 create_xml(0); |
559 create_xml(0); |
542 |
560 |
543 } |
561 } |
544 |
562 |
545 |
563 struct test_async_sync_interaction_data |
546 |
564 { |
|
565 GMutex *lock; |
|
566 GstClockID sync_id; |
|
567 GstClockID sync_id2; |
|
568 GstClockID async_id; |
|
569 GstClockID async_id2; |
|
570 GstClockID async_id3; |
|
571 }; |
|
572 static gboolean |
|
573 test_async_sync_interaction_cb (GstClock * clock, GstClockTime time, |
|
574 GstClockID id, gpointer user_data) |
|
575 { |
|
576 struct test_async_sync_interaction_data *td = |
|
577 (struct test_async_sync_interaction_data *) (user_data); |
|
578 g_mutex_lock (td->lock); |
|
579 if (id == td->async_id) |
|
580 goto out; |
|
581 if (id != td->async_id2 && id != td->async_id3) |
|
582 goto out; |
|
583 if (id == td->async_id3) { |
|
584 gst_clock_id_unschedule (td->sync_id); |
|
585 gst_clock_id_unschedule (td->async_id2); |
|
586 } |
|
587 out: |
|
588 g_mutex_unlock (td->lock); |
|
589 return FALSE; |
|
590 } |
|
591 void test_async_sync_interaction() |
|
592 { |
|
593 GstClock *clock; |
|
594 GstClockReturn result; |
|
595 GstClockTime base; |
|
596 GstClockTimeDiff jitter; |
|
597 struct test_async_sync_interaction_data td; |
|
598 int i; |
|
599 xmlfile = "test_async_sync_interaction"; |
|
600 std_log(LOG_FILENAME_LINE, "Test Started test_async_sync_interaction"); |
|
601 clock = gst_system_clock_obtain (); |
|
602 fail_unless (clock != NULL, "Could not create instance of GstSystemClock"); |
|
603 td.lock = g_mutex_new (); |
|
604 for (i = 0; i < 50; i++) { |
|
605 gst_clock_debug (clock); |
|
606 base = gst_clock_get_time (clock); |
|
607 g_mutex_lock (td.lock); |
|
608 td.async_id = gst_clock_new_single_shot_id (clock, base + 40 * GST_MSECOND); |
|
609 td.async_id2 = |
|
610 gst_clock_new_single_shot_id (clock, base + 30 * GST_MSECOND); |
|
611 td.async_id3 = |
|
612 gst_clock_new_single_shot_id (clock, base + 20 * GST_MSECOND); |
|
613 td.sync_id2 = gst_clock_new_single_shot_id (clock, base + 10 * GST_MSECOND); |
|
614 td.sync_id = gst_clock_new_single_shot_id (clock, base + 50 * GST_MSECOND); |
|
615 g_mutex_unlock (td.lock); |
|
616 result = gst_clock_id_wait_async (td.async_id, |
|
617 test_async_sync_interaction_cb, &td); |
|
618 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
|
619 result = gst_clock_id_wait (td.sync_id2, &jitter); |
|
620 fail_unless (result == GST_CLOCK_OK || result == GST_CLOCK_EARLY, |
|
621 "Waiting did not return OK or EARLY"); |
|
622 result = gst_clock_id_wait_async (td.async_id2, |
|
623 test_async_sync_interaction_cb, &td); |
|
624 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
|
625 gst_clock_id_unschedule (td.async_id); |
|
626 result = gst_clock_id_wait_async (td.async_id3, |
|
627 test_async_sync_interaction_cb, &td); |
|
628 fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK"); |
|
629 result = gst_clock_id_wait (td.sync_id, &jitter); |
|
630 fail_unless (result == GST_CLOCK_UNSCHEDULED || result == GST_CLOCK_EARLY, |
|
631 "Waiting did not return UNSCHEDULED"); |
|
632 gst_clock_id_unschedule (td.async_id3); |
|
633 g_mutex_lock (td.lock); |
|
634 gst_clock_id_unref (td.sync_id); |
|
635 gst_clock_id_unref (td.sync_id2); |
|
636 gst_clock_id_unref (td.async_id); |
|
637 gst_clock_id_unref (td.async_id2); |
|
638 gst_clock_id_unref (td.async_id3); |
|
639 g_mutex_unlock (td.lock); |
|
640 } |
|
641 g_mutex_free (td.lock); |
|
642 gst_object_unref (clock); |
|
643 std_log(LOG_FILENAME_LINE, "Test Successful"); |
|
644 create_xml(0); |
|
645 } |
547 /*void main(int argc,char** argv) |
646 /*void main(int argc,char** argv) |
548 { |
647 { |
549 gst_init(&argc,&argv); |
648 gst_init(&argc,&argv); |
550 test_range(); |
649 test_range(); |
551 test_mixed(); |
650 test_mixed(); |