bus: Add a function to remove a bus watch
authorLinus Svensson <linusp.svensson@gmail.com>
Tue, 19 Aug 2014 21:28:52 +0000 (23:28 +0200)
committerSebastian Dröge <sebastian@centricular.com>
Tue, 14 Oct 2014 08:31:18 +0000 (10:31 +0200)
If a bus watch is added to the non default main context it's not
possible to remove it using g_source_remove().

Fixes: https://bugzilla.gnome.org/show_bug.cgi?id=735195

gst/gstbus.c
gst/gstbus.h
tests/check/gst/gstbus.c
win32/common/libgstreamer.def

index 9b64a7f..b9ce67b 100644 (file)
@@ -897,8 +897,9 @@ gst_bus_add_watch_full_unlocked (GstBus * bus, gint priority,
  * When @func is called, the message belongs to the caller; if you want to
  * keep a copy of it, call gst_message_ref() before leaving @func.
  *
- * The watch can be removed using g_source_remove() or by returning %FALSE
- * from @func.
+ * The watch can be removed using gst_bus_remove_watch() or by returning %FALSE
+ * from @func. If the watch was added to the default main context it is also
+ * possible to remove the watch using g_source_remove().
  *
  * MT safe.
  *
@@ -936,8 +937,9 @@ gst_bus_add_watch_full (GstBus * bus, gint priority,
  * There can only be a single bus watch per bus, you must remove it before you
  * can set a new one.
  *
- * The watch can be removed using g_source_remove() or by returning %FALSE
- * from @func.
+ * The watch can be removed using gst_bus_remove_watch() or by returning %FALSE
+ * from @func. If the watch was added to the default main context it is also
+ * possible to remove the watch using g_source_remove().
  *
  * Returns: The event source id or 0 if @bus already got an event source.
  *
@@ -950,6 +952,45 @@ gst_bus_add_watch (GstBus * bus, GstBusFunc func, gpointer user_data)
       user_data, NULL);
 }
 
+/**
+ * gst_bus_remove_watch:
+ * @bus: a #GstBus to remove the watch from.
+ *
+ * Removes an installed bus watch from @bus.
+ *
+ * Returns: %TRUE on success or %FALSE if @bus has no event source.
+ *
+ * Since: 1.6
+ *
+ */
+gboolean
+gst_bus_remove_watch (GstBus * bus)
+{
+  GSource *watch_id;
+
+  g_return_val_if_fail (GST_IS_BUS (bus), FALSE);
+
+  GST_OBJECT_LOCK (bus);
+
+  if (bus->priv->signal_watch == NULL) {
+    GST_ERROR_OBJECT (bus, "no bus watch was present");
+    goto no_watch;
+  }
+
+  watch_id = bus->priv->signal_watch;
+
+  GST_OBJECT_UNLOCK (bus);
+
+  g_source_destroy (watch_id);
+
+  return TRUE;
+
+no_watch:
+  GST_OBJECT_UNLOCK (bus);
+
+  return FALSE;
+}
+
 typedef struct
 {
   GMainLoop *loop;
index 92d2160..d369682 100644 (file)
@@ -159,6 +159,7 @@ guint                   gst_bus_add_watch_full          (GstBus * bus,
 guint                   gst_bus_add_watch               (GstBus * bus,
                                                          GstBusFunc func,
                                                          gpointer user_data);
+gboolean                gst_bus_remove_watch            (GstBus * bus);
 
 /* polling the bus */
 GstMessage*             gst_bus_poll                    (GstBus *bus, GstMessageType events,
index 5eaa7f1..f7bed46 100644 (file)
@@ -188,7 +188,7 @@ GST_START_TEST (test_watch)
   fail_unless_equals_int (num_eos, 10);
   fail_unless_equals_int (num_app, 10);
 
-  g_source_remove (id);
+  fail_unless (gst_bus_remove_watch (test_bus));
   g_main_loop_unref (main_loop);
 
   gst_object_unref ((GstObject *) test_bus);
index b83293b..99a1568 100644 (file)
@@ -206,6 +206,7 @@ EXPORTS
        gst_bus_pop_filtered
        gst_bus_post
        gst_bus_remove_signal_watch
+       gst_bus_remove_watch
        gst_bus_set_flushing
        gst_bus_set_sync_handler
        gst_bus_sync_reply_get_type