examples: webrtc: mp-sendrecv: add bus handler
authorSam Van Den Berge <sam.van.den.berge@gmail.com>
Tue, 18 Oct 2022 13:28:32 +0000 (15:28 +0200)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Wed, 19 Oct 2022 00:51:44 +0000 (00:51 +0000)
Without this bus handler, messages posted to the bus will keep a ref to
their source elements, preventing them from being disposed.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3219>

subprojects/gst-examples/webrtc/multiparty-sendrecv/gst/mp-webrtc-sendrecv.c

index ff45996..81d56b5 100644 (file)
@@ -119,6 +119,39 @@ get_string_from_json_object (JsonObject * object)
   return text;
 }
 
+static gboolean
+bus_watch_cb (GstBus * bus, GstMessage * message, gpointer user_data)
+{
+  switch (GST_MESSAGE_TYPE (message)) {
+    case GST_MESSAGE_ERROR:
+    {
+      GError *error = NULL;
+      gchar *debug = NULL;
+
+      gst_message_parse_error (message, &error, &debug);
+      g_error ("Error on bus: %s (debug: %s)", error->message, debug);
+      g_error_free (error);
+      g_free (debug);
+      break;
+    }
+    case GST_MESSAGE_WARNING:
+    {
+      GError *error = NULL;
+      gchar *debug = NULL;
+
+      gst_message_parse_warning (message, &error, &debug);
+      g_warning ("Warning on bus: %s (debug: %s)", error->message, debug);
+      g_error_free (error);
+      g_free (debug);
+      break;
+    }
+    default:
+      break;
+  }
+
+  return G_SOURCE_CONTINUE;
+}
+
 static void
 handle_media_stream (GstPad * pad, GstElement * pipe, const char *convert_name,
     const char *sink_name)
@@ -413,6 +446,7 @@ start_pipeline (void)
 {
   GstStateChangeReturn ret;
   GError *error = NULL;
+  GstBus *bus = NULL;
 
   /* NOTE: webrtcbin currently does not support dynamic addition/removal of
    * streams, so we use a separate webrtcbin for each peer, but all of them are
@@ -428,6 +462,10 @@ start_pipeline (void)
     goto err;
   }
 
+  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
+  gst_bus_add_watch (bus, bus_watch_cb, NULL);
+  gst_object_unref (bus);
+
   gst_print ("Starting pipeline, not transmitting yet\n");
   ret = gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
   if (ret == GST_STATE_CHANGE_FAILURE)