webrtc: Make ssrc map into separate data structures 11/262811/2 accepted/tizen/unified/20210826.014219 submit/tizen/20210824.072619
authorOlivier CrĂȘte <olivier.crete@collabora.com>
Fri, 20 Nov 2020 22:32:44 +0000 (17:32 -0500)
committerSangchul Lee <sc11.lee@samsung.com>
Mon, 23 Aug 2021 10:46:56 +0000 (10:46 +0000)
They now contain a weak reference and that could be freed later
causing strange crashes as GWeakRef are not movable.

Change-Id: I3a5f4ee907bbaac48b25747f76f5d3abb7e3a1f6
Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1766>

ext/webrtc/gstwebrtcbin.c
ext/webrtc/gstwebrtcstats.c
ext/webrtc/transportstream.c
ext/webrtc/transportstream.h

index 4ec503a..a3b0871 100644 (file)
@@ -4886,16 +4886,7 @@ _set_description_task (GstWebRTCBin * webrtc, struct set_description *sd)
 
           if (split[0] && sscanf (split[0], "%u", &ssrc) && split[1]
               && g_str_has_prefix (split[1], "cname:")) {
-            SsrcMapItem ssrc_item;
-
-            ssrc_item.media_idx = i;
-            ssrc_item.ssrc = ssrc;
-            g_array_append_val (item->remote_ssrcmap, ssrc_item);
-
-            /* Must be done aftrer the value has been appended because
-             * a weak ref cannot be moved. */
-            g_weak_ref_init (&g_array_index (item->remote_ssrcmap, SsrcMapItem,
-                    item->remote_ssrcmap->len - 1).rtpjitterbuffer, NULL);
+            g_ptr_array_add (item->remote_ssrcmap, ssrcmap_item_new (ssrc, i));
           }
           g_strfreev (split);
         }
@@ -5527,8 +5518,7 @@ on_rtpbin_pad_added (GstElement * rtpbin, GstPad * new_pad,
     media_idx = session_id;
 
     for (i = 0; i < stream->remote_ssrcmap->len; i++) {
-      SsrcMapItem *item =
-          &g_array_index (stream->remote_ssrcmap, SsrcMapItem, i);
+      SsrcMapItem *item = g_ptr_array_index (stream->remote_ssrcmap, i);
       if (item->ssrc == ssrc) {
         media_idx = item->media_idx;
         found_ssrc = TRUE;
@@ -5987,8 +5977,7 @@ on_rtpbin_new_jitterbuffer (GstElement * rtpbin, GstElement * jitterbuffer,
         WEBRTC_TRANSCEIVER (trans)->do_nack, NULL);
 
     for (i = 0; i < trans->stream->remote_ssrcmap->len; i++) {
-      SsrcMapItem *item =
-          &g_array_index (trans->stream->remote_ssrcmap, SsrcMapItem, i);
+      SsrcMapItem *item = g_ptr_array_index (trans->stream->remote_ssrcmap, i);
 
       if (item->ssrc == ssrc) {
         g_weak_ref_set (&item->rtpjitterbuffer, jitterbuffer);
index a901be2..e42ded4 100644 (file)
@@ -318,8 +318,7 @@ _get_stats_from_rtp_source_stats (GstWebRTCBin * webrtc,
     gst_structure_get (source_stats, "have-sr", G_TYPE_BOOLEAN, &have_sr, NULL);
 
     for (i = 0; i < stream->remote_ssrcmap->len; i++) {
-      SsrcMapItem *item =
-          &g_array_index (stream->remote_ssrcmap, SsrcMapItem, i);
+      SsrcMapItem *item = g_ptr_array_index (stream->remote_ssrcmap, i);
 
       if (item->ssrc == ssrc) {
         GObject *jb = g_weak_ref_get (&item->rtpjitterbuffer);
index 935275a..c08bfc0 100644 (file)
@@ -190,7 +190,7 @@ transport_stream_finalize (GObject * object)
   TransportStream *stream = TRANSPORT_STREAM (object);
 
   g_array_free (stream->ptmap, TRUE);
-  g_array_free (stream->remote_ssrcmap, TRUE);
+  g_ptr_array_free (stream->remote_ssrcmap, TRUE);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -276,10 +276,24 @@ clear_ptmap_item (PtMapItem * item)
   if (item->caps)
     gst_caps_unref (item->caps);
 }
+
+SsrcMapItem *
+ssrcmap_item_new (guint32 ssrc, guint media_idx)
+{
+  SsrcMapItem *ssrc_item = g_slice_new (SsrcMapItem);
+
+  ssrc_item->media_idx = media_idx;
+  ssrc_item->ssrc = ssrc;
+  g_weak_ref_init (&ssrc_item->rtpjitterbuffer, NULL);
+
+  return ssrc_item;
+}
+
 static void
-clear_ssrcmap_item (SsrcMapItem * item)
+ssrcmap_item_free (SsrcMapItem * item)
 {
   g_weak_ref_clear (&item->rtpjitterbuffer);
+  g_slice_free (SsrcMapItem, item);
 }
 
 static void
@@ -287,9 +301,8 @@ transport_stream_init (TransportStream * stream)
 {
   stream->ptmap = g_array_new (FALSE, TRUE, sizeof (PtMapItem));
   g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
-  stream->remote_ssrcmap = g_array_new (FALSE, TRUE, sizeof (SsrcMapItem));
-  g_array_set_clear_func (stream->remote_ssrcmap,
-      (GDestroyNotify) clear_ssrcmap_item);
+  stream->remote_ssrcmap = g_ptr_array_new_with_free_func (
+      (GDestroyNotify) ssrcmap_item_free);
 }
 
 TransportStream *
index d309437..4642c9a 100644 (file)
@@ -44,6 +44,9 @@ typedef struct
   GWeakRef rtpjitterbuffer; /* for stats */
 } SsrcMapItem;
 
+SsrcMapItem *           ssrcmap_item_new            (guint32 ssrc,
+                                                     guint media_idx);
+
 struct _TransportStream
 {
   GstObject                 parent;
@@ -58,7 +61,7 @@ struct _TransportStream
   GstWebRTCDTLSTransport   *transport;
 
   GArray                   *ptmap;                  /* array of PtMapItem's */
-  GArray                   *remote_ssrcmap;         /* array of SsrcMapItem's */
+  GPtrArray                *remote_ssrcmap;         /* array of SsrcMapItem's */
   gboolean                  output_connected;       /* whether receive bin is connected to rtpbin */
 
   GstElement               *rtxsend;