webrtc sendonly: Exit on bus errors
authorOlivier Crête <olivier.crete@collabora.com>
Thu, 9 Jul 2020 20:30:41 +0000 (16:30 -0400)
committerOlivier Crête <olivier.crete@collabora.com>
Thu, 8 Oct 2020 20:16:16 +0000 (16:16 -0400)
Catch bus errors and cleanly error out

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-examples/-/merge_requests/18>

webrtc/sendonly/webrtc-recvonly-h264.c
webrtc/sendonly/webrtc-unidirectional-h264.c

index 1fc9a1d..8df62f4 100644 (file)
@@ -270,6 +270,38 @@ on_incoming_stream (GstElement * webrtc, GstPad * pad,
   gst_object_unref (sinkpad);
 }
 
+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;
+}
 
 ReceiverEntry *
 create_receiver_entry (SoupWebsocketConnection * connection)
@@ -278,6 +310,7 @@ create_receiver_entry (SoupWebsocketConnection * connection)
   ReceiverEntry *receiver_entry;
   GstCaps *video_caps;
   GstWebRTCRTPTransceiver *trans = NULL;
+  GstBus *bus;
 
   receiver_entry = g_slice_alloc0 (sizeof (ReceiverEntry));
   receiver_entry->connection = connection;
@@ -331,7 +364,13 @@ create_receiver_entry (SoupWebsocketConnection * connection)
   g_signal_connect (receiver_entry->webrtcbin, "on-ice-candidate",
       G_CALLBACK (on_ice_candidate_cb), (gpointer) receiver_entry);
 
-  gst_element_set_state (receiver_entry->pipeline, GST_STATE_PLAYING);
+  bus = gst_pipeline_get_bus (GST_PIPELINE (receiver_entry->pipeline));
+  gst_bus_add_watch (bus, bus_watch_cb, NULL);
+  gst_object_unref (bus);
+
+  if (gst_element_set_state (receiver_entry->pipeline, GST_STATE_PLAYING) ==
+      GST_STATE_CHANGE_FAILURE)
+    g_error ("Error starting pipeline");
 
   return receiver_entry;
 
index 4dba828..4f919c5 100644 (file)
@@ -158,6 +158,39 @@ const gchar *html_source = " \n \
 </html> \n \
 ";
 
+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;
+}
+
 ReceiverEntry *
 create_receiver_entry (SoupWebsocketConnection * connection)
 {
@@ -165,6 +198,7 @@ create_receiver_entry (SoupWebsocketConnection * connection)
   ReceiverEntry *receiver_entry;
   GstWebRTCRTPTransceiver *trans;
   GArray *transceivers;
+  GstBus *bus;
 
   receiver_entry = g_slice_alloc0 (sizeof (ReceiverEntry));
   receiver_entry->connection = connection;
@@ -205,7 +239,13 @@ create_receiver_entry (SoupWebsocketConnection * connection)
   g_signal_connect (receiver_entry->webrtcbin, "on-ice-candidate",
       G_CALLBACK (on_ice_candidate_cb), (gpointer) receiver_entry);
 
-  gst_element_set_state (receiver_entry->pipeline, GST_STATE_PLAYING);
+  bus = gst_pipeline_get_bus (GST_PIPELINE (receiver_entry->pipeline));
+  gst_bus_add_watch (bus, bus_watch_cb, NULL);
+  gst_object_unref (bus);
+
+  if (gst_element_set_state (receiver_entry->pipeline, GST_STATE_PLAYING) ==
+      GST_STATE_CHANGE_FAILURE)
+    g_error ("Could not start pipeline");
 
   return receiver_entry;