rtpmanager: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtpmanager / gstrtpmux.c
index 7d24ef4..4d16d5d 100644 (file)
 
 /**
  * SECTION:element-rtpmux
+ * @title: rtpmux
  * @see_also: rtpdtmfmux
  *
  * The rtp muxer takes multiple RTP streams having the same clock-rate and
  * muxes into a single stream with a single SSRC.
  *
- * <refsect2>
- * <title>Example pipelines</title>
+ * ## Example pipelines
  * |[
  * gst-launch-1.0 rtpmux name=mux ! udpsink host=127.0.0.1 port=8888        \
  *              alsasrc ! alawenc ! rtppcmapay !                        \
@@ -45,7 +45,7 @@
  * In this example, an audio stream is captured from ALSA and another is
  * generated, both are encoded into different payload types and muxed together
  * so they can be sent on the same port.
- * </refsect2>
+ *
  */
 
 #ifdef HAVE_CONFIG_H
@@ -124,6 +124,7 @@ gst_rtp_mux_class_init (GstRTPMuxClass * klass)
   gobject_class = (GObjectClass *) klass;
   gstelement_class = (GstElementClass *) klass;
 
+
   gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
   gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
 
@@ -137,25 +138,21 @@ gst_rtp_mux_class_init (GstRTPMuxClass * klass)
 
   klass->src_event = gst_rtp_mux_src_event_real;
 
-  g_object_class_install_property (gobject_class, PROP_TIMESTAMP_OFFSET,
-      g_param_spec_int64 ("timestamp-offset", "Timestamp Offset",
-          "Offset to add to all outgoing timestamps (-1 = random)",
-          -1, G_MAXUINT32, DEFAULT_TIMESTAMP_OFFSET,
+  g_object_class_install_property (G_OBJECT_CLASS (klass),
+      PROP_TIMESTAMP_OFFSET, g_param_spec_int ("timestamp-offset",
+          "Timestamp Offset",
+          "Offset to add to all outgoing timestamps (-1 = random)", -1,
+          G_MAXINT, DEFAULT_TIMESTAMP_OFFSET,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-
-  g_object_class_install_property (gobject_class, PROP_SEQNUM_OFFSET,
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM_OFFSET,
       g_param_spec_int ("seqnum-offset", "Sequence number Offset",
-          "Offset to add to all outgoing seqnum (-1 = random)",
-          -1, G_MAXUINT16, DEFAULT_SEQNUM_OFFSET,
-          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-
-  g_object_class_install_property (gobject_class, PROP_SEQNUM,
+          "Offset to add to all outgoing seqnum (-1 = random)", -1, G_MAXINT,
+          DEFAULT_SEQNUM_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SEQNUM,
       g_param_spec_uint ("seqnum", "Sequence number",
           "The RTP sequence number of the last processed packet",
-          0, G_MAXUINT, 0,
-          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
-
-  g_object_class_install_property (gobject_class, PROP_SSRC,
+          0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SSRC,
       g_param_spec_uint ("ssrc", "SSRC",
           "The SSRC of the packets (default == random)",
           0, G_MAXUINT, DEFAULT_SSRC,
@@ -216,7 +213,7 @@ gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
         if (!gst_structure_get_uint (s, "ssrc", &ssrc))
           ssrc = -1;
 
-        GST_DEBUG_OBJECT (rtp_mux, "collided ssrc: %" G_GUINT32_FORMAT, ssrc);
+        GST_DEBUG_OBJECT (rtp_mux, "collided ssrc: %x", ssrc);
 
         /* choose another ssrc for our stream */
         GST_OBJECT_LOCK (rtp_mux);
@@ -232,6 +229,8 @@ gst_rtp_mux_src_event_real (GstRTPMux * rtp_mux, GstEvent * event)
             rtp_mux->current_ssrc = g_random_int ();
 
           new_ssrc = rtp_mux->current_ssrc;
+          GST_INFO_OBJECT (rtp_mux, "New ssrc after collision %x (was: %x)",
+              new_ssrc, ssrc);
           GST_OBJECT_UNLOCK (rtp_mux);
 
           caps = gst_pad_get_current_caps (rtp_mux->srcpad);
@@ -268,7 +267,6 @@ gst_rtp_mux_init (GstRTPMux * rtp_mux)
 
   rtp_mux->ssrc = DEFAULT_SSRC;
   rtp_mux->current_ssrc = DEFAULT_SSRC;
-  rtp_mux->ssrc_random = TRUE;
   rtp_mux->ts_offset = DEFAULT_TIMESTAMP_OFFSET;
   rtp_mux->seqnum_offset = DEFAULT_SEQNUM_OFFSET;
 
@@ -354,10 +352,8 @@ gst_rtp_mux_readjust_rtp_timestamp_locked (GstRTPMux * rtp_mux,
 
   ts = gst_rtp_buffer_get_timestamp (rtpbuffer) - sink_ts_base +
       rtp_mux->ts_base;
-  GST_LOG_OBJECT (rtp_mux,
-      "Re-adjusting RTP ts %u to %u (sink_ts_base = %u, rtp_mux->ts_base=%u)",
-      gst_rtp_buffer_get_timestamp (rtpbuffer),
-      ts, sink_ts_base, rtp_mux->ts_base);
+  GST_LOG_OBJECT (rtp_mux, "Re-adjusting RTP ts %u to %u",
+      gst_rtp_buffer_get_timestamp (rtpbuffer), ts);
   gst_rtp_buffer_set_timestamp (rtpbuffer, ts);
 }
 
@@ -377,15 +373,19 @@ process_buffer_locked (GstRTPMux * rtp_mux, GstRTPMuxPadPrivate * padpriv,
   gst_rtp_buffer_set_ssrc (rtpbuffer, rtp_mux->current_ssrc);
   gst_rtp_mux_readjust_rtp_timestamp_locked (rtp_mux, padpriv, rtpbuffer);
   GST_LOG_OBJECT (rtp_mux,
-      "Pushing packet size %" G_GSIZE_FORMAT ", seq=%d, ts=%u",
+      "Pushing packet size %" G_GSIZE_FORMAT ", seq=%d, ts=%u, ssrc=%x",
       rtpbuffer->map[0].size, rtp_mux->seqnum,
-      gst_rtp_buffer_get_timestamp (rtpbuffer));
+      gst_rtp_buffer_get_timestamp (rtpbuffer), rtp_mux->current_ssrc);
 
   if (padpriv) {
-    if (padpriv->segment.format == GST_FORMAT_TIME)
+    if (padpriv->segment.format == GST_FORMAT_TIME) {
       GST_BUFFER_PTS (rtpbuffer->buffer) =
           gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
           GST_BUFFER_PTS (rtpbuffer->buffer));
+      GST_BUFFER_DTS (rtpbuffer->buffer) =
+          gst_segment_to_running_time (&padpriv->segment, GST_FORMAT_TIME,
+          GST_BUFFER_DTS (rtpbuffer->buffer));
+    }
   }
 
   return TRUE;
@@ -444,7 +444,11 @@ gst_rtp_mux_chain_list (GstPad * pad, GstObject * parent,
     GstCaps *current_caps = gst_pad_get_current_caps (pad);
 
     if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
-      ret = GST_FLOW_NOT_NEGOTIATED;
+      gst_pad_mark_reconfigure (rtp_mux->srcpad);
+      if (GST_PAD_IS_FLUSHING (rtp_mux->srcpad))
+        ret = GST_FLOW_FLUSHING;
+      else
+        ret = GST_FLOW_NOT_NEGOTIATED;
       gst_buffer_list_unref (bufferlist);
       goto out;
     }
@@ -501,6 +505,10 @@ resend_events (GstPad * pad, GstEvent ** event, gpointer user_data)
 
     gst_event_parse_caps (*event, &caps);
     gst_rtp_mux_setcaps (pad, rtp_mux, caps);
+  } else if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
+    GstSegment new_segment;
+    gst_segment_init (&new_segment, GST_FORMAT_TIME);
+    gst_pad_push_event (rtp_mux->srcpad, gst_event_new_segment (&new_segment));
   } else {
     gst_pad_push_event (rtp_mux->srcpad, gst_event_ref (*event));
   }
@@ -524,7 +532,11 @@ gst_rtp_mux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
     GstCaps *current_caps = gst_pad_get_current_caps (pad);
 
     if (!gst_rtp_mux_setcaps (pad, rtp_mux, current_caps)) {
-      ret = GST_FLOW_NOT_NEGOTIATED;
+      gst_pad_mark_reconfigure (rtp_mux->srcpad);
+      if (GST_PAD_IS_FLUSHING (rtp_mux->srcpad))
+        ret = GST_FLOW_FLUSHING;
+      else
+        ret = GST_FLOW_NOT_NEGOTIATED;
       gst_buffer_unref (buffer);
       goto out;
     }
@@ -592,6 +604,9 @@ gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
   GstRTPMuxPadPrivate *padpriv;
   GstCaps *peercaps;
 
+  if (caps == NULL)
+    return FALSE;
+
   if (!gst_caps_is_fixed (caps))
     return FALSE;
 
@@ -606,10 +621,14 @@ gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
       structure = gst_caps_get_structure (othercaps, 0);
       GST_OBJECT_LOCK (rtp_mux);
       if (gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc)) {
-        GST_DEBUG_OBJECT (pad, "Use downstream ssrc: %x",
-            rtp_mux->current_ssrc);
+        GST_INFO_OBJECT (pad, "Use downstream ssrc: %x", rtp_mux->current_ssrc);
         rtp_mux->have_ssrc = TRUE;
       }
+      if (gst_structure_get_uint (structure,
+              "timestamp-offset", &rtp_mux->ts_base)) {
+        GST_INFO_OBJECT (pad, "Use downstream timestamp-offset: %u",
+            rtp_mux->ts_base);
+      }
       GST_OBJECT_UNLOCK (rtp_mux);
     }
 
@@ -636,12 +655,16 @@ gst_rtp_mux_setcaps (GstPad * pad, GstRTPMux * rtp_mux, GstCaps * caps)
 
   /* if we don't have a specified ssrc, first try to take one from the caps,
      and if that fails, generate one */
-  if (!rtp_mux->have_ssrc) {
-    if (rtp_mux->ssrc_random) {
-      if (!gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc))
+  if (rtp_mux->ssrc == DEFAULT_SSRC) {
+    if (rtp_mux->current_ssrc == DEFAULT_SSRC) {
+      if (!gst_structure_get_uint (structure, "ssrc", &rtp_mux->current_ssrc)) {
         rtp_mux->current_ssrc = g_random_int ();
-      rtp_mux->have_ssrc = TRUE;
+        GST_INFO_OBJECT (rtp_mux, "Set random ssrc %x", rtp_mux->current_ssrc);
+      }
     }
+  } else {
+    rtp_mux->current_ssrc = rtp_mux->ssrc;
+    GST_INFO_OBJECT (rtp_mux, "Set ssrc %x", rtp_mux->current_ssrc);
   }
 
   gst_caps_set_simple (caps,
@@ -727,6 +750,7 @@ gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
   GstCaps *peercaps;
   GstCaps *othercaps;
   GstCaps *tcaps;
+  const GstStructure *structure;
 
   peercaps = gst_pad_peer_query_caps (mux->srcpad, NULL);
 
@@ -748,6 +772,12 @@ gst_rtp_mux_getcaps (GstPad * pad, GstRTPMux * mux, GstCaps * filter)
   GST_LOG_OBJECT (pad, "Intersected srcpad-peercaps and template caps: %"
       GST_PTR_FORMAT, othercaps);
 
+  structure = gst_caps_get_structure (othercaps, 0);
+  if (mux->ssrc == DEFAULT_SSRC) {
+    if (gst_structure_get_uint (structure, "ssrc", &mux->current_ssrc))
+      GST_DEBUG_OBJECT (pad, "Use downstream ssrc: %x", mux->current_ssrc);
+  }
+
   clear_caps (othercaps, TRUE);
 
   g_value_init (&v, GST_TYPE_CAPS);
@@ -815,7 +845,7 @@ gst_rtp_mux_get_property (GObject * object,
   GST_OBJECT_LOCK (rtp_mux);
   switch (prop_id) {
     case PROP_TIMESTAMP_OFFSET:
-      g_value_set_int64 (value, rtp_mux->ts_offset);
+      g_value_set_int (value, rtp_mux->ts_offset);
       break;
     case PROP_SEQNUM_OFFSET:
       g_value_set_int (value, rtp_mux->seqnum_offset);
@@ -843,7 +873,7 @@ gst_rtp_mux_set_property (GObject * object,
 
   switch (prop_id) {
     case PROP_TIMESTAMP_OFFSET:
-      rtp_mux->ts_offset = g_value_get_int64 (value);
+      rtp_mux->ts_offset = g_value_get_int (value);
       break;
     case PROP_SEQNUM_OFFSET:
       rtp_mux->seqnum_offset = g_value_get_int (value);
@@ -853,7 +883,7 @@ gst_rtp_mux_set_property (GObject * object,
       rtp_mux->ssrc = g_value_get_uint (value);
       rtp_mux->current_ssrc = rtp_mux->ssrc;
       rtp_mux->have_ssrc = TRUE;
-      rtp_mux->ssrc_random = FALSE;
+      GST_DEBUG_OBJECT (rtp_mux, "ssrc prop set to %x", rtp_mux->ssrc);
       GST_OBJECT_UNLOCK (rtp_mux);
       break;
     default:
@@ -869,6 +899,10 @@ gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
   gboolean is_pad;
   gboolean ret;
 
+  GST_OBJECT_LOCK (mux);
+  is_pad = (pad == mux->last_pad);
+  GST_OBJECT_UNLOCK (mux);
+
   switch (GST_EVENT_TYPE (event)) {
     case GST_EVENT_CAPS:
     {
@@ -899,16 +933,19 @@ gst_rtp_mux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
         gst_event_copy_segment (event, &padpriv->segment);
       }
       GST_OBJECT_UNLOCK (mux);
+
+      if (is_pad) {
+        GstSegment new_segment;
+        gst_segment_init (&new_segment, GST_FORMAT_TIME);
+        gst_event_unref (event);
+        event = gst_event_new_segment (&new_segment);
+      }
       break;
     }
     default:
       break;
   }
 
-  GST_OBJECT_LOCK (mux);
-  is_pad = (pad == mux->last_pad);
-  GST_OBJECT_UNLOCK (mux);
-
   if (is_pad) {
     return gst_pad_push_event (mux->srcpad, event);
   } else {
@@ -939,12 +976,8 @@ gst_rtp_mux_ready_to_paused (GstRTPMux * rtp_mux)
 
   rtp_mux->last_stop = GST_CLOCK_TIME_NONE;
 
-  if (rtp_mux->ssrc_random) {
-    rtp_mux->have_ssrc = FALSE;
-  } else {
+  if (rtp_mux->have_ssrc)
     rtp_mux->current_ssrc = rtp_mux->ssrc;
-    rtp_mux->have_ssrc = TRUE;
-  }
 
   GST_DEBUG_OBJECT (rtp_mux, "set timestamp-offset to %u", rtp_mux->ts_base);