webrtcbin: fix prevention of webrtcbin deletion due to ref held by probe callback
authorRob Agar <rob@engineeredarts.co.uk>
Thu, 14 Oct 2021 13:07:07 +0000 (14:07 +0100)
committerRob Agar <rob@engineeredarts.co.uk>
Mon, 18 Oct 2021 09:42:12 +0000 (10:42 +0100)
https://gitlab.freedesktop.org/gstreamer/gstreamer/-/issues/810

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

subprojects/gst-plugins-bad/ext/webrtc/gstwebrtcbin.c

index 861b005..4ca2ebe 100644 (file)
@@ -1858,10 +1858,20 @@ gst_webrtc_bin_attach_tos_to_session (GstWebRTCBin * webrtc, guint session_id)
   }
 }
 
+static void
+weak_free (GWeakRef * weak)
+{
+  g_weak_ref_clear (weak);
+  g_free (weak);
+}
+
 static GstPadProbeReturn
 _nicesink_pad_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
 {
-  GstWebRTCBin *webrtc = user_data;
+  GstWebRTCBin *webrtc = g_weak_ref_get ((GWeakRef *) user_data);
+
+  if (!webrtc)
+    return GST_PAD_PROBE_REMOVE;
 
   if (GST_EVENT_TYPE (GST_PAD_PROBE_INFO_EVENT (info))
       == GST_EVENT_CUSTOM_DOWNSTREAM_STICKY) {
@@ -1950,6 +1960,9 @@ _nicesink_pad_probe (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
       }
     }
   }
+
+  gst_object_unref (webrtc);
+
   return GST_PAD_PROBE_OK;
 }
 
@@ -1994,11 +2007,15 @@ gst_webrtc_bin_attach_probe_to_ice_sink (GstWebRTCBin * webrtc,
     GstWebRTCICETransport * transport)
 {
   GstPad *pad;
+  GWeakRef *weak;
 
   pad = gst_element_get_static_pad (transport->sink, "sink");
+
+  weak = g_new0 (GWeakRef, 1);
+  g_weak_ref_init (weak, webrtc);
+
   gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
-      _nicesink_pad_probe, g_object_ref (webrtc),
-      (GDestroyNotify) gst_object_unref);
+      _nicesink_pad_probe, weak, (GDestroyNotify) weak_free);
   gst_object_unref (pad);
 }