rtpsrc: re-use the same src pad for streams that have the same payload type
authorGeorge Kiagiadakis <george.kiagiadakis@collabora.com>
Fri, 4 Sep 2020 11:18:13 +0000 (14:18 +0300)
committerGStreamer Merge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Fri, 16 Oct 2020 17:23:46 +0000 (17:23 +0000)
Also use payload type when naming pads, this will make it easier to identify
pads and simplify the code.

Fixes #1395

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1575>

gst/rtp/gstrtpsrc.c

index f8c29a7..3e576fa 100644 (file)
@@ -426,7 +426,9 @@ gst_rtp_src_rtpbin_pad_added_cb (GstElement * element, GstPad * pad,
 {
   GstRtpSrc *self = GST_RTP_SRC (data);
   GstCaps *caps = gst_pad_query_caps (pad, NULL);
-  GstPad *upad;
+  const GstStructure *s;
+  GstPad *upad = NULL;
+  gint pt = -1;
   gchar name[48];
 
   /* Expose RTP data pad only */
@@ -458,15 +460,26 @@ gst_rtp_src_rtpbin_pad_added_cb (GstElement * element, GstPad * pad,
 
     return;
   }
+
+  s = gst_caps_get_structure (caps, 0);
+  gst_structure_get_int (s, "payload", &pt);
   gst_caps_unref (caps);
 
   GST_RTP_SRC_LOCK (self);
-  g_snprintf (name, 48, "src_%u", GST_ELEMENT (self)->numpads);
-  upad = gst_ghost_pad_new (name, pad);
+  g_snprintf (name, 48, "src_%u", pt);
+  upad = gst_element_get_static_pad (GST_ELEMENT (self), name);
 
-  gst_pad_set_active (upad, TRUE);
-  gst_element_add_pad (GST_ELEMENT (self), upad);
+  if (!upad) {
+    GST_DEBUG_OBJECT (self, "Adding new pad: %s", name);
 
+    upad = gst_ghost_pad_new (name, pad);
+    gst_pad_set_active (upad, TRUE);
+    gst_element_add_pad (GST_ELEMENT (self), upad);
+  } else {
+    GST_DEBUG_OBJECT (self, "Re-using existing pad: %s", GST_PAD_NAME (upad));
+    gst_ghost_pad_set_target (GST_GHOST_PAD (upad), pad);
+    gst_object_unref (upad);
+  }
   GST_RTP_SRC_UNLOCK (self);
 }