2005-03-09 16:10:59 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
|
|
|
|
*
|
|
|
|
* gstsystemclock.c: Unit test for GstSystemClock
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:44:48 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2005-03-09 16:10:59 +00:00
|
|
|
*/
|
|
|
|
|
2005-08-20 12:14:28 +00:00
|
|
|
#include <gst/check/gstcheck.h>
|
2005-03-09 16:10:59 +00:00
|
|
|
|
2012-09-12 09:52:25 +00:00
|
|
|
static GMutex af_lock;
|
|
|
|
static GCond af_cond;
|
2010-07-05 11:10:09 +00:00
|
|
|
|
2007-11-28 10:58:39 +00:00
|
|
|
/* see if the defines make sense */
|
|
|
|
GST_START_TEST (test_range)
|
|
|
|
{
|
|
|
|
GstClockTime time, time2;
|
|
|
|
|
|
|
|
time = GST_SECOND;
|
|
|
|
fail_unless (time == G_GUINT64_CONSTANT (1000000000));
|
|
|
|
|
|
|
|
time2 = time / 1000;
|
|
|
|
fail_unless (time2 == 1000000);
|
|
|
|
fail_unless (time2 == GST_MSECOND);
|
|
|
|
fail_unless (time2 == GST_TIME_AS_USECONDS (time));
|
|
|
|
|
|
|
|
time2 = time / 1000000;
|
|
|
|
fail_unless (time2 == 1000);
|
|
|
|
fail_unless (time2 == GST_USECOND);
|
|
|
|
fail_unless (time2 == GST_TIME_AS_MSECONDS (time));
|
|
|
|
}
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
2005-07-09 16:36:18 +00:00
|
|
|
GST_START_TEST (test_signedness)
|
2005-03-09 16:10:59 +00:00
|
|
|
{
|
|
|
|
GstClockTime time[] = { 0, 1, G_MAXUINT64 / GST_SECOND };
|
|
|
|
GstClockTimeDiff diff[] =
|
|
|
|
{ 0, 1, -1, G_MAXINT64 / GST_SECOND, G_MININT64 / GST_SECOND };
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (time); i++) {
|
|
|
|
fail_if (time[i] != (time[i] * GST_SECOND / GST_SECOND));
|
|
|
|
}
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (diff); i++) {
|
|
|
|
fail_if (diff[i] != (diff[i] * GST_SECOND / GST_SECOND));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
2005-08-20 12:39:05 +00:00
|
|
|
#define TIME_UNIT (GST_SECOND / 5)
|
2008-02-29 13:59:24 +00:00
|
|
|
static void
|
2005-03-09 16:10:59 +00:00
|
|
|
gst_clock_debug (GstClock * clock)
|
|
|
|
{
|
|
|
|
GstClockTime time;
|
|
|
|
|
|
|
|
time = gst_clock_get_time (clock);
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("Clock info: time %" GST_TIME_FORMAT, GST_TIME_ARGS (time));
|
2005-03-09 16:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
ok_callback (GstClock * clock, GstClockTime time,
|
|
|
|
GstClockID id, gpointer user_data)
|
|
|
|
{
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_LOG ("unlocked async id %p", id);
|
2005-03-09 16:10:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
error_callback (GstClock * clock, GstClockTime time,
|
|
|
|
GstClockID id, gpointer user_data)
|
|
|
|
{
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_WARNING ("unlocked unscheduled async id %p, this is wrong", id);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_if (TRUE);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-12 09:52:25 +00:00
|
|
|
GMutex store_lock;
|
2009-03-25 23:03:38 +00:00
|
|
|
|
2006-12-07 10:51:36 +00:00
|
|
|
static gboolean
|
|
|
|
store_callback (GstClock * clock, GstClockTime time,
|
|
|
|
GstClockID id, gpointer user_data)
|
|
|
|
{
|
2007-05-23 13:56:25 +00:00
|
|
|
GList **list = user_data;
|
2006-12-07 10:51:36 +00:00
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("unlocked async id %p", id);
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&store_lock);
|
2007-05-23 13:56:25 +00:00
|
|
|
*list = g_list_append (*list, id);
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_unlock (&store_lock);
|
2006-12-07 10:51:36 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
notify_callback (GstClock * clock, GstClockTime time,
|
|
|
|
GstClockID id, gpointer user_data)
|
|
|
|
{
|
|
|
|
gboolean *ret = (gboolean *) user_data;
|
|
|
|
|
|
|
|
if (ret != NULL)
|
|
|
|
*ret = TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2005-07-09 16:36:18 +00:00
|
|
|
GST_START_TEST (test_single_shot)
|
2005-03-09 16:10:59 +00:00
|
|
|
{
|
|
|
|
GstClock *clock;
|
|
|
|
GstClockID id, id2;
|
|
|
|
GstClockTime base;
|
|
|
|
GstClockReturn result;
|
|
|
|
|
|
|
|
clock = gst_system_clock_obtain ();
|
|
|
|
fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
|
|
|
|
|
|
|
|
gst_clock_debug (clock);
|
|
|
|
base = gst_clock_get_time (clock);
|
|
|
|
|
|
|
|
id = gst_clock_new_single_shot_id (clock, base + TIME_UNIT);
|
|
|
|
fail_unless (id != NULL, "Could not create single shot id");
|
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting one time unit");
|
2005-03-09 16:10:59 +00:00
|
|
|
result = gst_clock_id_wait (id, NULL);
|
|
|
|
gst_clock_debug (clock);
|
2007-09-11 13:43:53 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK (result=%d)",
|
|
|
|
result);
|
|
|
|
fail_unless (gst_clock_get_time (clock) > (base + TIME_UNIT),
|
|
|
|
"target time has not been reached");
|
2005-03-09 16:10:59 +00:00
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting in the past");
|
2005-03-09 16:10:59 +00:00
|
|
|
result = gst_clock_id_wait (id, NULL);
|
|
|
|
gst_clock_debug (clock);
|
2007-09-11 13:43:53 +00:00
|
|
|
fail_unless (result == GST_CLOCK_EARLY,
|
|
|
|
"Waiting did not return EARLY(result=%d)", result);
|
2005-03-09 16:10:59 +00:00
|
|
|
gst_clock_id_unref (id);
|
|
|
|
|
|
|
|
id = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting one second async id %p", id);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
2005-08-20 12:39:05 +00:00
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
2009-03-25 23:03:38 +00:00
|
|
|
gst_clock_id_unschedule (id);
|
|
|
|
gst_clock_id_unref (id);
|
2005-03-09 16:10:59 +00:00
|
|
|
|
|
|
|
id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting one second async, with cancel on id %p", id);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id, error_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
2005-08-20 12:39:05 +00:00
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("cancel id %p after half a time unit", id);
|
2005-03-09 16:10:59 +00:00
|
|
|
gst_clock_id_unschedule (id);
|
|
|
|
gst_clock_id_unref (id);
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("canceled id %p", id);
|
2005-03-09 16:10:59 +00:00
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting multiple one second async, with cancel");
|
2005-03-09 16:10:59 +00:00
|
|
|
id = gst_clock_new_single_shot_id (clock, base + 5 * TIME_UNIT);
|
|
|
|
id2 = gst_clock_new_single_shot_id (clock, base + 6 * TIME_UNIT);
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting id %p", id);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
2009-03-26 14:55:02 +00:00
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting id %p", id2);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id2, error_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
2005-08-20 12:39:05 +00:00
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("cancel id %p after half a time unit", id2);
|
2005-03-09 16:10:59 +00:00
|
|
|
gst_clock_id_unschedule (id2);
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("canceled id %p", id2);
|
2005-03-09 16:10:59 +00:00
|
|
|
gst_clock_id_unref (id2);
|
2009-03-25 23:03:38 +00:00
|
|
|
|
2009-03-26 14:55:02 +00:00
|
|
|
/* wait for the entry to time out */
|
|
|
|
g_usleep (TIME_UNIT / 1000 * 5);
|
|
|
|
fail_unless (((GstClockEntry *) id)->status == GST_CLOCK_OK,
|
|
|
|
"Waiting did not finish");
|
2009-03-25 23:03:38 +00:00
|
|
|
gst_clock_id_unref (id);
|
|
|
|
|
|
|
|
gst_object_unref (clock);
|
2005-03-09 16:10:59 +00:00
|
|
|
}
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
2005-07-09 16:36:18 +00:00
|
|
|
GST_START_TEST (test_periodic_shot)
|
2005-03-09 16:10:59 +00:00
|
|
|
{
|
|
|
|
GstClock *clock;
|
|
|
|
GstClockID id, id2;
|
|
|
|
GstClockTime base;
|
|
|
|
GstClockReturn result;
|
|
|
|
|
|
|
|
clock = gst_system_clock_obtain ();
|
|
|
|
fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
|
|
|
|
|
|
|
|
gst_clock_debug (clock);
|
|
|
|
base = gst_clock_get_time (clock);
|
|
|
|
|
2005-08-20 12:39:05 +00:00
|
|
|
/* signal every half a time unit */
|
2005-03-09 16:10:59 +00:00
|
|
|
id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
|
|
|
|
fail_unless (id != NULL, "Could not create periodic id");
|
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting one time unit");
|
2005-03-09 16:10:59 +00:00
|
|
|
result = gst_clock_id_wait (id, NULL);
|
|
|
|
gst_clock_debug (clock);
|
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting for the next");
|
2005-03-09 16:10:59 +00:00
|
|
|
result = gst_clock_id_wait (id, NULL);
|
|
|
|
gst_clock_debug (clock);
|
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting for the next async %p", id);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
2005-08-20 12:39:05 +00:00
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
2005-03-09 16:10:59 +00:00
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting some more for the next async %p", id);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id, ok_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
2005-08-20 12:39:05 +00:00
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
2005-03-09 16:10:59 +00:00
|
|
|
|
|
|
|
id2 = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT / 2);
|
|
|
|
fail_unless (id2 != NULL, "Could not create second periodic id");
|
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("waiting some more for another async %p", id2);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id2, ok_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
2005-08-20 12:39:05 +00:00
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
2005-03-09 16:10:59 +00:00
|
|
|
|
2007-09-11 13:43:53 +00:00
|
|
|
GST_DEBUG ("unschedule %p", id);
|
2005-03-09 16:10:59 +00:00
|
|
|
gst_clock_id_unschedule (id);
|
|
|
|
|
|
|
|
/* entry cannot be used again */
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id, error_callback, NULL, NULL);
|
2005-03-09 16:10:59 +00:00
|
|
|
fail_unless (result == GST_CLOCK_UNSCHEDULED,
|
|
|
|
"Waiting did not return UNSCHEDULED");
|
|
|
|
result = gst_clock_id_wait (id, NULL);
|
|
|
|
fail_unless (result == GST_CLOCK_UNSCHEDULED,
|
|
|
|
"Waiting did not return UNSCHEDULED");
|
2005-08-20 12:39:05 +00:00
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
gst_clock_id_unref (id);
|
2009-03-25 23:03:38 +00:00
|
|
|
gst_clock_id_unschedule (id2);
|
|
|
|
gst_clock_id_unref (id2);
|
2010-04-07 07:31:39 +00:00
|
|
|
|
|
|
|
gst_object_unref (clock);
|
2005-03-09 16:10:59 +00:00
|
|
|
}
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
2006-12-07 10:51:36 +00:00
|
|
|
GST_START_TEST (test_async_order)
|
|
|
|
{
|
|
|
|
GstClock *clock;
|
2007-05-23 13:56:25 +00:00
|
|
|
GstClockID id1, id2;
|
|
|
|
GList *cb_list = NULL, *next;
|
2006-12-07 10:51:36 +00:00
|
|
|
GstClockTime base;
|
|
|
|
GstClockReturn result;
|
|
|
|
|
|
|
|
clock = gst_system_clock_obtain ();
|
|
|
|
fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
|
|
|
|
|
|
|
|
gst_clock_debug (clock);
|
|
|
|
base = gst_clock_get_time (clock);
|
|
|
|
|
|
|
|
id1 = gst_clock_new_single_shot_id (clock, base + 2 * TIME_UNIT);
|
|
|
|
id2 = gst_clock_new_single_shot_id (clock, base + 1 * TIME_UNIT);
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id1, store_callback, &cb_list, NULL);
|
2006-12-07 10:51:36 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
g_usleep (TIME_UNIT / (2 * 1000));
|
2012-06-20 07:58:14 +00:00
|
|
|
result = gst_clock_id_wait_async (id2, store_callback, &cb_list, NULL);
|
2006-12-07 10:51:36 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
g_usleep (TIME_UNIT / 1000);
|
2007-05-23 13:56:25 +00:00
|
|
|
/* at this point at least one of the timers should have timed out */
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&store_lock);
|
2007-05-23 13:56:25 +00:00
|
|
|
fail_unless (cb_list != NULL, "expected notification");
|
|
|
|
fail_unless (cb_list->data == id2,
|
|
|
|
"Expected notification for id2 to come first");
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_unlock (&store_lock);
|
2006-12-07 10:51:36 +00:00
|
|
|
g_usleep (TIME_UNIT / 1000);
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&store_lock);
|
2007-05-23 13:56:25 +00:00
|
|
|
/* now both should have timed out */
|
|
|
|
next = g_list_next (cb_list);
|
|
|
|
fail_unless (next != NULL, "expected second notification");
|
|
|
|
fail_unless (next->data == id1, "Missing notification for id1");
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_unlock (&store_lock);
|
2009-03-25 23:03:38 +00:00
|
|
|
|
2006-12-07 10:51:36 +00:00
|
|
|
gst_clock_id_unref (id1);
|
|
|
|
gst_clock_id_unref (id2);
|
2007-05-23 13:56:25 +00:00
|
|
|
g_list_free (cb_list);
|
2009-03-25 23:03:38 +00:00
|
|
|
|
|
|
|
gst_object_unref (clock);
|
2006-12-07 10:51:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
2009-03-25 21:37:38 +00:00
|
|
|
struct test_async_sync_interaction_data
|
|
|
|
{
|
2012-09-12 09:52:25 +00:00
|
|
|
GMutex lock;
|
2009-03-25 21:37:38 +00:00
|
|
|
|
|
|
|
GstClockID sync_id;
|
|
|
|
GstClockID sync_id2;
|
|
|
|
|
|
|
|
GstClockID async_id;
|
|
|
|
GstClockID async_id2;
|
|
|
|
GstClockID async_id3;
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
test_async_sync_interaction_cb (GstClock * clock, GstClockTime time,
|
|
|
|
GstClockID id, gpointer user_data)
|
|
|
|
{
|
|
|
|
struct test_async_sync_interaction_data *td =
|
|
|
|
(struct test_async_sync_interaction_data *) (user_data);
|
|
|
|
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&td->lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
/* The first async callback is ignored */
|
|
|
|
if (id == td->async_id)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (id != td->async_id2 && id != td->async_id3)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Unschedule the sync callback */
|
|
|
|
if (id == td->async_id3) {
|
|
|
|
gst_clock_id_unschedule (td->sync_id);
|
|
|
|
gst_clock_id_unschedule (td->async_id2);
|
|
|
|
}
|
|
|
|
out:
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_unlock (&td->lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_START_TEST (test_async_sync_interaction)
|
|
|
|
{
|
|
|
|
/* This test schedules an async callback, then before it completes, schedules
|
|
|
|
* an earlier async callback, and quickly unschedules the first, and inserts
|
|
|
|
* a THIRD even earlier async callback. It then attempts to wait on a
|
|
|
|
* sync clock ID. While that's sleeping, the 3rd async callback should fire
|
|
|
|
* and unschedule it. This tests for problems with unscheduling async and
|
|
|
|
* sync callbacks on the system clock. */
|
|
|
|
GstClock *clock;
|
|
|
|
GstClockReturn result;
|
|
|
|
GstClockTime base;
|
|
|
|
GstClockTimeDiff jitter;
|
|
|
|
struct test_async_sync_interaction_data td;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
clock = gst_system_clock_obtain ();
|
|
|
|
fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
|
|
|
|
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_init (&td.lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 50; i++) {
|
|
|
|
gst_clock_debug (clock);
|
|
|
|
base = gst_clock_get_time (clock);
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&td.lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
td.async_id = gst_clock_new_single_shot_id (clock, base + 40 * GST_MSECOND);
|
|
|
|
td.async_id2 =
|
|
|
|
gst_clock_new_single_shot_id (clock, base + 30 * GST_MSECOND);
|
|
|
|
td.async_id3 =
|
|
|
|
gst_clock_new_single_shot_id (clock, base + 20 * GST_MSECOND);
|
|
|
|
td.sync_id2 = gst_clock_new_single_shot_id (clock, base + 10 * GST_MSECOND);
|
|
|
|
td.sync_id = gst_clock_new_single_shot_id (clock, base + 50 * GST_MSECOND);
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_unlock (&td.lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
|
|
|
|
result = gst_clock_id_wait_async (td.async_id,
|
2012-06-20 07:58:14 +00:00
|
|
|
test_async_sync_interaction_cb, &td, NULL);
|
2009-03-25 21:37:38 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
|
|
|
|
/* Wait 10ms, then unschedule async_id and schedule async_id2 */
|
|
|
|
result = gst_clock_id_wait (td.sync_id2, &jitter);
|
|
|
|
fail_unless (result == GST_CLOCK_OK || result == GST_CLOCK_EARLY,
|
|
|
|
"Waiting did not return OK or EARLY");
|
|
|
|
/* async_id2 is earlier than async_id - should become head of the queue */
|
|
|
|
result = gst_clock_id_wait_async (td.async_id2,
|
2012-06-20 07:58:14 +00:00
|
|
|
test_async_sync_interaction_cb, &td, NULL);
|
2009-03-25 21:37:38 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
gst_clock_id_unschedule (td.async_id);
|
|
|
|
|
|
|
|
/* async_id3 is earlier than async_id2 - should become head of the queue */
|
|
|
|
result = gst_clock_id_wait_async (td.async_id3,
|
2012-06-20 07:58:14 +00:00
|
|
|
test_async_sync_interaction_cb, &td, NULL);
|
2009-03-25 21:37:38 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
|
|
|
|
/* While this is sleeping, the async3 id should fire and unschedule it */
|
|
|
|
result = gst_clock_id_wait (td.sync_id, &jitter);
|
|
|
|
fail_unless (result == GST_CLOCK_UNSCHEDULED || result == GST_CLOCK_EARLY,
|
2009-11-14 22:35:07 +00:00
|
|
|
"Waiting did not return UNSCHEDULED (was %d)", result);
|
2009-03-25 21:37:38 +00:00
|
|
|
|
|
|
|
gst_clock_id_unschedule (td.async_id3);
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&td.lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
|
|
|
|
gst_clock_id_unref (td.sync_id);
|
|
|
|
gst_clock_id_unref (td.sync_id2);
|
|
|
|
gst_clock_id_unref (td.async_id);
|
|
|
|
gst_clock_id_unref (td.async_id2);
|
|
|
|
gst_clock_id_unref (td.async_id3);
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_unlock (&td.lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_clear (&td.lock);
|
2009-03-25 21:37:38 +00:00
|
|
|
gst_object_unref (clock);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2006-12-07 10:51:36 +00:00
|
|
|
GST_START_TEST (test_periodic_multi)
|
|
|
|
{
|
|
|
|
GstClock *clock;
|
|
|
|
GstClockID clock_id;
|
2009-03-25 23:03:38 +00:00
|
|
|
GstClockID clock_id_async;
|
2006-12-07 10:51:36 +00:00
|
|
|
GstClockTime base;
|
|
|
|
GstClockReturn result;
|
|
|
|
gboolean got_callback = FALSE;
|
|
|
|
|
|
|
|
clock = gst_system_clock_obtain ();
|
|
|
|
fail_unless (clock != NULL, "Could not create instance of GstSystemClock");
|
|
|
|
|
|
|
|
gst_clock_debug (clock);
|
|
|
|
base = gst_clock_get_time (clock);
|
|
|
|
|
|
|
|
clock_id = gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT);
|
|
|
|
gst_clock_id_wait (clock_id, NULL);
|
|
|
|
fail_unless (gst_clock_get_time (clock) >= base + TIME_UNIT);
|
|
|
|
fail_unless (gst_clock_get_time (clock) < base + 2 * TIME_UNIT);
|
|
|
|
|
|
|
|
/* now perform a concurrent wait and wait_async */
|
|
|
|
|
2009-03-25 23:03:38 +00:00
|
|
|
clock_id_async =
|
|
|
|
gst_clock_new_periodic_id (clock, base + TIME_UNIT, TIME_UNIT);
|
|
|
|
result =
|
2012-06-20 07:58:14 +00:00
|
|
|
gst_clock_id_wait_async (clock_id_async, notify_callback, &got_callback,
|
|
|
|
NULL);
|
2006-12-07 10:51:36 +00:00
|
|
|
fail_unless (result == GST_CLOCK_OK, "Async waiting did not return OK");
|
2009-03-25 23:03:38 +00:00
|
|
|
|
2006-12-07 10:51:36 +00:00
|
|
|
result = gst_clock_id_wait (clock_id, NULL);
|
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
fail_unless (gst_clock_get_time (clock) >= base + 2 * TIME_UNIT);
|
|
|
|
/* give the async thread some time to call our callback: */
|
|
|
|
g_usleep (TIME_UNIT / (10 * 1000));
|
|
|
|
fail_unless (got_callback == TRUE, "got no async callback (1)");
|
|
|
|
fail_unless (gst_clock_get_time (clock) < base + 3 * TIME_UNIT);
|
|
|
|
got_callback = FALSE;
|
|
|
|
|
|
|
|
result = gst_clock_id_wait (clock_id, NULL);
|
|
|
|
fail_unless (result == GST_CLOCK_OK, "Waiting did not return OK");
|
|
|
|
fail_unless (gst_clock_get_time (clock) >= base + 3 * TIME_UNIT);
|
|
|
|
/* give the async thread some time to call our callback: */
|
|
|
|
g_usleep (TIME_UNIT / (10 * 1000));
|
|
|
|
fail_unless (got_callback == TRUE, "got no async callback (2)");
|
|
|
|
fail_unless (gst_clock_get_time (clock) < base + 4 * TIME_UNIT);
|
2009-03-25 23:03:38 +00:00
|
|
|
|
|
|
|
/* clean up */
|
|
|
|
gst_clock_id_unref (clock_id);
|
|
|
|
gst_clock_id_unschedule (clock_id_async);
|
|
|
|
gst_clock_id_unref (clock_id_async);
|
|
|
|
gst_object_unref (clock);
|
2006-12-07 10:51:36 +00:00
|
|
|
}
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
gst/gstclock.*: Review docs.
Original commit message from CVS:
* gst/gstclock.c: (gst_clock_entry_new),
(gst_clock_id_compare_func), (gst_clock_id_wait),
(gst_clock_id_wait_async), (gst_clock_id_unschedule),
(gst_clock_init), (gst_clock_get_internal_time),
(gst_clock_set_master), (do_linear_regression),
(gst_clock_add_observation), (gst_clock_set_property):
* gst/gstclock.h:
Review docs.
Small cleanups.
Fix a possible segfault when the window-size is made smaller.
Calculate jitter before performing the clock wait. Ideally
the clock implementation should calculate jitter but we need
API breakage for that.
* gst/gstsystemclock.c: (gst_system_clock_init):
Docs review.
* libs/gst/base/gstbasesink.c: (gst_base_sink_do_sync):
Remove leftover else
* tests/check/gst/gstsystemclock.c: (GST_START_TEST),
(gst_systemclock_suite):
Added check to test GST_CLOCK_DIFF.
2006-03-09 11:45:14 +00:00
|
|
|
GST_START_TEST (test_diff)
|
|
|
|
{
|
2008-01-12 20:22:30 +00:00
|
|
|
GstClockTime time1[] = { 0, (GstClockTime) - 1, 0, 1, 2 * GST_SECOND,
|
|
|
|
(GstClockTime) - GST_SECOND, (GstClockTime) - GST_SECOND
|
|
|
|
};
|
gst/gstclock.*: Review docs.
Original commit message from CVS:
* gst/gstclock.c: (gst_clock_entry_new),
(gst_clock_id_compare_func), (gst_clock_id_wait),
(gst_clock_id_wait_async), (gst_clock_id_unschedule),
(gst_clock_init), (gst_clock_get_internal_time),
(gst_clock_set_master), (do_linear_regression),
(gst_clock_add_observation), (gst_clock_set_property):
* gst/gstclock.h:
Review docs.
Small cleanups.
Fix a possible segfault when the window-size is made smaller.
Calculate jitter before performing the clock wait. Ideally
the clock implementation should calculate jitter but we need
API breakage for that.
* gst/gstsystemclock.c: (gst_system_clock_init):
Docs review.
* libs/gst/base/gstbasesink.c: (gst_base_sink_do_sync):
Remove leftover else
* tests/check/gst/gstsystemclock.c: (GST_START_TEST),
(gst_systemclock_suite):
Added check to test GST_CLOCK_DIFF.
2006-03-09 11:45:14 +00:00
|
|
|
GstClockTime time2[] =
|
2008-01-12 20:22:30 +00:00
|
|
|
{ 0, 1, 1, 0, 1 * GST_SECOND, (GstClockTime) - GST_SECOND, GST_SECOND };
|
gst/gstclock.*: Review docs.
Original commit message from CVS:
* gst/gstclock.c: (gst_clock_entry_new),
(gst_clock_id_compare_func), (gst_clock_id_wait),
(gst_clock_id_wait_async), (gst_clock_id_unschedule),
(gst_clock_init), (gst_clock_get_internal_time),
(gst_clock_set_master), (do_linear_regression),
(gst_clock_add_observation), (gst_clock_set_property):
* gst/gstclock.h:
Review docs.
Small cleanups.
Fix a possible segfault when the window-size is made smaller.
Calculate jitter before performing the clock wait. Ideally
the clock implementation should calculate jitter but we need
API breakage for that.
* gst/gstsystemclock.c: (gst_system_clock_init):
Docs review.
* libs/gst/base/gstbasesink.c: (gst_base_sink_do_sync):
Remove leftover else
* tests/check/gst/gstsystemclock.c: (GST_START_TEST),
(gst_systemclock_suite):
Added check to test GST_CLOCK_DIFF.
2006-03-09 11:45:14 +00:00
|
|
|
GstClockTimeDiff d[] = { 0, 2, 1, -1, -GST_SECOND, 0, 2 * GST_SECOND };
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS (d); i++) {
|
|
|
|
fail_if (d[i] != GST_CLOCK_DIFF (time1[i], time2[i]));
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 11:58:08 +00:00
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
2007-03-22 11:58:08 +00:00
|
|
|
/* test if a blocking wait, unblocked by an async entry continues to be
|
|
|
|
* scheduled */
|
2008-02-29 13:59:24 +00:00
|
|
|
typedef struct
|
2007-03-22 11:58:08 +00:00
|
|
|
{
|
|
|
|
GstClock *clock;
|
|
|
|
GstClockID id;
|
|
|
|
GstClockTimeDiff jitter;
|
|
|
|
GstClockReturn ret;
|
|
|
|
} MixedInfo;
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
mixed_thread (MixedInfo * info)
|
|
|
|
{
|
|
|
|
info->ret = gst_clock_id_wait (info->id, &info->jitter);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
mixed_async_cb (GstClock * clock, GstClockTime time,
|
|
|
|
GstClockID id, gpointer user_data)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_START_TEST (test_mixed)
|
|
|
|
{
|
|
|
|
GThread *thread;
|
|
|
|
GError *error = NULL;
|
|
|
|
MixedInfo info;
|
|
|
|
GstClockTime base;
|
|
|
|
GstClockID id;
|
|
|
|
|
|
|
|
info.clock = gst_system_clock_obtain ();
|
|
|
|
fail_unless (info.clock != NULL,
|
|
|
|
"Could not create instance of GstSystemClock");
|
|
|
|
|
|
|
|
/* get current time of the clock as base time */
|
|
|
|
base = gst_clock_get_time (info.clock);
|
|
|
|
|
|
|
|
/* create entry to wait for 1 second */
|
|
|
|
info.id = gst_clock_new_single_shot_id (info.clock, base + GST_SECOND);
|
|
|
|
|
|
|
|
/* make and start an entry that is scheduled every 10ms */
|
|
|
|
id = gst_clock_new_periodic_id (info.clock, base, 10 * GST_MSECOND);
|
|
|
|
|
|
|
|
/* start waiting for the entry */
|
2012-09-12 09:52:25 +00:00
|
|
|
thread =
|
|
|
|
g_thread_try_new ("gst-check", (GThreadFunc) mixed_thread, &info, &error);
|
2007-03-22 11:58:08 +00:00
|
|
|
fail_unless (error == NULL, "error creating thread");
|
|
|
|
fail_unless (thread != NULL, "Could not create thread");
|
|
|
|
|
|
|
|
/* wait half a second so we are sure to be in the thread */
|
|
|
|
g_usleep (G_USEC_PER_SEC / 2);
|
|
|
|
|
|
|
|
/* start scheduling the entry */
|
2012-06-20 07:58:14 +00:00
|
|
|
gst_clock_id_wait_async (id, mixed_async_cb, NULL, NULL);
|
2007-03-22 11:58:08 +00:00
|
|
|
|
|
|
|
/* wait for thread to finish */
|
|
|
|
g_thread_join (thread);
|
|
|
|
/* entry must have timed out correctly */
|
|
|
|
fail_unless (info.ret == GST_CLOCK_OK, "clock return was %d", info.ret);
|
|
|
|
|
|
|
|
gst_clock_id_unschedule (id);
|
2007-03-22 12:31:54 +00:00
|
|
|
gst_clock_id_unref (id);
|
2007-03-22 11:58:08 +00:00
|
|
|
gst_clock_id_unref (info.id);
|
|
|
|
gst_object_unref (info.clock);
|
|
|
|
}
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
GST_END_TEST;
|
|
|
|
|
2010-07-05 11:10:09 +00:00
|
|
|
static gboolean
|
|
|
|
test_async_full_slave_callback (GstClock * master, GstClockTime time,
|
|
|
|
GstClockID id, GstClock * clock)
|
|
|
|
{
|
|
|
|
GstClockTime stime, mtime;
|
|
|
|
gdouble r_squared;
|
|
|
|
|
|
|
|
/* notify the test case that we started */
|
|
|
|
GST_INFO ("callback started");
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&af_lock);
|
|
|
|
g_cond_signal (&af_cond);
|
2010-07-05 11:10:09 +00:00
|
|
|
|
|
|
|
/* wait for the test case to unref "clock" and signal */
|
|
|
|
GST_INFO ("waiting for test case to signal");
|
2012-09-12 09:52:25 +00:00
|
|
|
g_cond_wait (&af_cond, &af_lock);
|
2010-07-05 11:10:09 +00:00
|
|
|
|
|
|
|
stime = gst_clock_get_internal_time (clock);
|
|
|
|
mtime = gst_clock_get_time (master);
|
|
|
|
|
|
|
|
gst_clock_add_observation (clock, stime, mtime, &r_squared);
|
|
|
|
|
2012-09-12 09:52:25 +00:00
|
|
|
g_cond_signal (&af_cond);
|
|
|
|
g_mutex_unlock (&af_lock);
|
2010-07-05 11:10:09 +00:00
|
|
|
GST_INFO ("callback finished");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_START_TEST (test_async_full)
|
|
|
|
{
|
|
|
|
GstClock *master, *slave;
|
|
|
|
GstClockID *clockid;
|
|
|
|
|
|
|
|
/* create master and slave */
|
|
|
|
master =
|
|
|
|
g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "TestClockMaster", NULL);
|
|
|
|
slave = g_object_new (GST_TYPE_SYSTEM_CLOCK, "name", "TestClockMaster", NULL);
|
|
|
|
GST_OBJECT_FLAG_SET (slave, GST_CLOCK_FLAG_CAN_SET_MASTER);
|
|
|
|
g_object_set (slave, "timeout", 50 * GST_MSECOND, NULL);
|
|
|
|
|
|
|
|
fail_unless (GST_OBJECT_REFCOUNT (master) == 1);
|
|
|
|
fail_unless (GST_OBJECT_REFCOUNT (slave) == 1);
|
|
|
|
|
|
|
|
/* register a periodic shot on the master to calibrate the slave */
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_lock (&af_lock);
|
2010-07-05 11:10:09 +00:00
|
|
|
clockid = gst_clock_new_periodic_id (master,
|
2012-02-27 08:02:07 +00:00
|
|
|
gst_clock_get_time (master), gst_clock_get_timeout (slave));
|
2012-06-20 07:58:14 +00:00
|
|
|
gst_clock_id_wait_async (clockid,
|
2010-07-05 11:10:09 +00:00
|
|
|
(GstClockCallback) test_async_full_slave_callback,
|
|
|
|
gst_object_ref (slave), (GDestroyNotify) gst_object_unref);
|
|
|
|
|
|
|
|
/* wait for the shot to be fired and test_async_full_slave_callback to be
|
|
|
|
* called */
|
|
|
|
GST_INFO ("waiting for the slave callback to start");
|
2012-09-12 09:52:25 +00:00
|
|
|
g_cond_wait (&af_cond, &af_lock);
|
2010-07-05 11:10:09 +00:00
|
|
|
GST_INFO ("slave callback running, unreffing slave");
|
|
|
|
|
|
|
|
/* unref the slave clock while the slave_callback is running. This should be
|
|
|
|
* safe since the master clock now stores a ref to the slave */
|
|
|
|
gst_object_unref (slave);
|
|
|
|
|
|
|
|
/* unref the clock entry. This should be safe as well since the clock thread
|
|
|
|
* refs the entry before executing it */
|
|
|
|
gst_clock_id_unschedule (clockid);
|
|
|
|
gst_clock_id_unref (clockid);
|
|
|
|
|
|
|
|
/* signal and wait for the callback to complete */
|
2012-09-12 09:52:25 +00:00
|
|
|
g_cond_signal (&af_cond);
|
2010-07-05 11:10:09 +00:00
|
|
|
|
|
|
|
GST_INFO ("waiting for callback to finish");
|
2012-09-12 09:52:25 +00:00
|
|
|
g_cond_wait (&af_cond, &af_lock);
|
2010-07-05 11:10:09 +00:00
|
|
|
GST_INFO ("callback finished");
|
2012-09-12 09:52:25 +00:00
|
|
|
g_mutex_unlock (&af_lock);
|
2010-07-05 11:10:09 +00:00
|
|
|
|
|
|
|
gst_object_unref (master);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_END_TEST;
|
|
|
|
|
2008-02-29 13:59:24 +00:00
|
|
|
static Suite *
|
2007-05-23 13:56:25 +00:00
|
|
|
gst_systemclock_suite (void)
|
2005-03-09 16:10:59 +00:00
|
|
|
{
|
|
|
|
Suite *s = suite_create ("GstSystemClock");
|
|
|
|
TCase *tc_chain = tcase_create ("waiting");
|
|
|
|
|
|
|
|
suite_add_tcase (s, tc_chain);
|
2007-11-28 10:58:39 +00:00
|
|
|
tcase_add_test (tc_chain, test_range);
|
2005-03-09 16:10:59 +00:00
|
|
|
tcase_add_test (tc_chain, test_signedness);
|
|
|
|
tcase_add_test (tc_chain, test_single_shot);
|
|
|
|
tcase_add_test (tc_chain, test_periodic_shot);
|
2006-12-07 10:51:36 +00:00
|
|
|
tcase_add_test (tc_chain, test_periodic_multi);
|
|
|
|
tcase_add_test (tc_chain, test_async_order);
|
2009-03-25 21:37:38 +00:00
|
|
|
tcase_add_test (tc_chain, test_async_sync_interaction);
|
gst/gstclock.*: Review docs.
Original commit message from CVS:
* gst/gstclock.c: (gst_clock_entry_new),
(gst_clock_id_compare_func), (gst_clock_id_wait),
(gst_clock_id_wait_async), (gst_clock_id_unschedule),
(gst_clock_init), (gst_clock_get_internal_time),
(gst_clock_set_master), (do_linear_regression),
(gst_clock_add_observation), (gst_clock_set_property):
* gst/gstclock.h:
Review docs.
Small cleanups.
Fix a possible segfault when the window-size is made smaller.
Calculate jitter before performing the clock wait. Ideally
the clock implementation should calculate jitter but we need
API breakage for that.
* gst/gstsystemclock.c: (gst_system_clock_init):
Docs review.
* libs/gst/base/gstbasesink.c: (gst_base_sink_do_sync):
Remove leftover else
* tests/check/gst/gstsystemclock.c: (GST_START_TEST),
(gst_systemclock_suite):
Added check to test GST_CLOCK_DIFF.
2006-03-09 11:45:14 +00:00
|
|
|
tcase_add_test (tc_chain, test_diff);
|
2007-03-22 11:58:08 +00:00
|
|
|
tcase_add_test (tc_chain, test_mixed);
|
2010-07-05 11:10:09 +00:00
|
|
|
tcase_add_test (tc_chain, test_async_full);
|
2005-03-09 16:10:59 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2006-07-01 20:56:56 +00:00
|
|
|
GST_CHECK_MAIN (gst_systemclock);
|