clock: use the new gst_clock_id_wait_async_full.
authorAlessandro Decina <alessandro.decina@collabora.co.uk>
Mon, 5 Jul 2010 11:10:09 +0000 (13:10 +0200)
committerAlessandro Decina <alessandro.d@gmail.com>
Tue, 6 Jul 2010 08:46:42 +0000 (10:46 +0200)
Use the new gst_clock_id_wait_async_full in gst_clock_set_master.
Also add some tests.

gst/gstclock.c
tests/check/Makefile.am
tests/check/gst/gstclock.c [new file with mode: 0644]
tests/check/gst/gstsystemclock.c

index 1c4765d..e792778 100644 (file)
@@ -1095,8 +1095,9 @@ gst_clock_set_master (GstClock * clock, GstClock * master)
      * clock calibration. */
     clock->clockid = gst_clock_new_periodic_id (master,
         gst_clock_get_time (master), clock->timeout);
-    gst_clock_id_wait_async (clock->clockid,
-        (GstClockCallback) gst_clock_slave_callback, clock);
+    gst_clock_id_wait_async_full (clock->clockid,
+        (GstClockCallback) gst_clock_slave_callback,
+        gst_object_ref (clock), (GDestroyNotify) gst_object_unref);
   }
   GST_CLOCK_SLAVE_UNLOCK (clock);
 
index 5c1f35d..3a0a87c 100644 (file)
@@ -86,6 +86,7 @@ check_PROGRAMS =                              \
        gst/gstpoll                             \
        gst/gstsegment                          \
        gst/gstsystemclock                      \
+       gst/gstclock                            \
        gst/gststructure                        \
        gst/gsttag                              \
        gst/gsttagsetter                        \
diff --git a/tests/check/gst/gstclock.c b/tests/check/gst/gstclock.c
new file mode 100644 (file)
index 0000000..a8f374d
--- /dev/null
@@ -0,0 +1,93 @@
+/* GStreamer
+ * Copyright (C) 2010 Alessandro Decina <alessandro.decina@collabora.co.uk>
+ *
+ * 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
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <gst/check/gstcheck.h>
+
+static void
+weak_notify (gpointer data, GObject * object)
+{
+  *(gboolean *) data = FALSE;
+}
+
+static GstClockReturn
+fake_wait_async (GstClock * clock, GstClockEntry * entry)
+{
+  return GST_CLOCK_OK;
+}
+
+GST_START_TEST (test_set_master_refcount)
+{
+  GstClock *master, *slave;
+  GstClockClass *klass;
+  gboolean master_alive = TRUE;
+
+  /* create master and slave */
+  master = g_object_new (GST_TYPE_CLOCK, "name", "TestClockMaster", NULL);
+  slave = g_object_new (GST_TYPE_CLOCK, "name", "TestClockMaster", NULL);
+  GST_OBJECT_FLAG_SET (slave, GST_CLOCK_FLAG_CAN_SET_MASTER);
+
+  /* look ma! i'm doing monkey patching in C */
+  klass = GST_CLOCK_GET_CLASS (master);
+  klass->wait_async = fake_wait_async;
+
+  fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 1);
+  fail_unless_equals_int (GST_OBJECT_REFCOUNT (slave), 1);
+
+  g_object_weak_ref (G_OBJECT (master), weak_notify, &master_alive);
+  fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 1);
+
+  gst_clock_set_master (slave, master);
+  /* slave stores master in slave->master */
+  fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 2);
+  /* master stores a ref to slave in master->clockid */
+  fail_unless_equals_int (GST_OBJECT_REFCOUNT (slave), 2);
+
+  /* discard our ref */
+  gst_object_unref (master);
+
+  /* master should still be reffed inside slave */
+  fail_unless_equals_int (GST_OBJECT_REFCOUNT (master), 1);
+  fail_unless (master_alive);
+
+  /* drop the last ref to mater */
+  gst_clock_set_master (slave, NULL);
+
+  fail_if (master_alive);
+  fail_unless_equals_int (GST_OBJECT_REFCOUNT (slave), 1);
+
+  gst_object_unref (slave);
+
+  klass->wait_async = NULL;
+}
+
+GST_END_TEST;
+
+static Suite *
+gst_clock_suite (void)
+{
+  Suite *s = suite_create ("GstClock");
+  TCase *tc_chain = tcase_create ("clock");
+
+  suite_add_tcase (s, tc_chain);
+  tcase_add_test (tc_chain, test_set_master_refcount);
+
+  return s;
+}
+
+GST_CHECK_MAIN (gst_clock);
index 40c9947..2207667 100644 (file)
@@ -21,6 +21,9 @@
 
 #include <gst/check/gstcheck.h>
 
+static GMutex *af_lock;
+static GCond *af_cond;
+
 /* see if the defines make sense */
 GST_START_TEST (test_range)
 {
@@ -556,6 +559,91 @@ GST_START_TEST (test_mixed)
 
 GST_END_TEST;
 
+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");
+  g_mutex_lock (af_lock);
+  g_cond_signal (af_cond);
+
+  /* wait for the test case to unref "clock" and signal */
+  GST_INFO ("waiting for test case to signal");
+  g_cond_wait (af_cond, af_lock);
+
+  stime = gst_clock_get_internal_time (clock);
+  mtime = gst_clock_get_time (master);
+
+  gst_clock_add_observation (clock, stime, mtime, &r_squared);
+
+  g_cond_signal (af_cond);
+  g_mutex_unlock (af_lock);
+  GST_INFO ("callback finished");
+
+  return TRUE;
+}
+
+GST_START_TEST (test_async_full)
+{
+  GstClock *master, *slave;
+  GstClockID *clockid;
+
+  af_lock = g_mutex_new ();
+  af_cond = g_cond_new ();
+
+  /* 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 */
+  g_mutex_lock (af_lock);
+  clockid = gst_clock_new_periodic_id (master,
+      gst_clock_get_time (master), slave->timeout);
+  gst_clock_id_wait_async_full (clockid,
+      (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");
+  g_cond_wait (af_cond, af_lock);
+  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 */
+  g_cond_signal (af_cond);
+
+  GST_INFO ("waiting for callback to finish");
+  g_cond_wait (af_cond, af_lock);
+  GST_INFO ("callback finished");
+  g_mutex_unlock (af_lock);
+
+  gst_object_unref (master);
+
+  g_mutex_free (af_lock);
+  g_cond_free (af_cond);
+}
+
+GST_END_TEST;
+
 static Suite *
 gst_systemclock_suite (void)
 {
@@ -572,6 +660,7 @@ gst_systemclock_suite (void)
   tcase_add_test (tc_chain, test_async_sync_interaction);
   tcase_add_test (tc_chain, test_diff);
   tcase_add_test (tc_chain, test_mixed);
+  tcase_add_test (tc_chain, test_async_full);
 
   return s;
 }