rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4vpay.c
index 1ed1ac5..2980339 100644 (file)
 #include <string.h>
 
 #include <gst/rtp/gstrtpbuffer.h>
+#include <gst/video/video.h>
 
 #include "gstrtpmp4vpay.h"
+#include "gstrtputils.h"
 
 GST_DEBUG_CATEGORY_STATIC (rtpmp4vpay_debug);
 #define GST_CAT_DEFAULT (rtpmp4vpay_debug)
@@ -78,9 +80,10 @@ static gboolean gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay,
     GstEvent * event);
 
 #define gst_rtp_mp4v_pay_parent_class parent_class
-G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD)
+G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD);
 
-     static void gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
+static void
+gst_rtp_mp4v_pay_class_init (GstRtpMP4VPayClass * klass)
 {
   GObjectClass *gobject_class;
   GstElementClass *gstelement_class;
@@ -93,10 +96,10 @@ G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD)
   gobject_class->set_property = gst_rtp_mp4v_pay_set_property;
   gobject_class->get_property = gst_rtp_mp4v_pay_get_property;
 
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&gst_rtp_mp4v_pay_src_template));
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&gst_rtp_mp4v_pay_sink_template));
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_mp4v_pay_src_template);
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_mp4v_pay_sink_template);
 
   gst_element_class_set_static_metadata (gstelement_class,
       "RTP MPEG4 Video payloader", "Codec/Payloader/Network/RTP",
@@ -104,10 +107,11 @@ G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD)
       "Wim Taymans <wim.taymans@gmail.com>");
 
   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_INTERVAL,
-      g_param_spec_uint ("config-interval", "Config Send Interval",
+      g_param_spec_int ("config-interval", "Config Send Interval",
           "Send Config Insertion Interval in seconds (configuration headers "
-          "will be multiplexed in the data stream when detected.) (0 = disabled)",
-          0, 3600, DEFAULT_CONFIG_INTERVAL,
+          "will be multiplexed in the data stream when detected.) "
+          "(0 = disabled, -1 = send with every IDR frame)",
+          -1, 3600, DEFAULT_CONFIG_INTERVAL,
           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
       );
 
@@ -263,7 +267,7 @@ gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
     guint packet_len;
     GstRTPBuffer rtp = { NULL };
 
-    /* this will be the total lenght of the packet */
+    /* this will be the total length of the packet */
     packet_len = gst_rtp_buffer_calc_packet_len (avail, 0, 0);
 
     /* fill one MTU or all available bytes */
@@ -285,10 +289,10 @@ gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
     gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
     gst_rtp_buffer_set_marker (&rtp, avail == 0);
     gst_rtp_buffer_unmap (&rtp);
-
+    gst_rtp_copy_video_meta (rtpmp4vpay, outbuf, outbuf_data);
     outbuf = gst_buffer_append (outbuf, outbuf_data);
 
-    GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4vpay->first_timestamp;
+    GST_BUFFER_PTS (outbuf) = rtpmp4vpay->first_timestamp;
 
     /* add to list */
     gst_buffer_list_insert (list, -1, outbuf);
@@ -409,7 +413,7 @@ gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
   return result;
 }
 
-/* we expect buffers starting on startcodes. 
+/* we expect buffers starting on startcodes.
  */
 static GstFlowReturn
 gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
@@ -426,6 +430,7 @@ gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
   GstClockTime timestamp, duration;
   gboolean vopi;
   gboolean send_config;
+  GstClockTime running_time = GST_CLOCK_TIME_NONE;
 
   ret = GST_FLOW_OK;
   send_config = FALSE;
@@ -434,7 +439,7 @@ gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
 
   gst_buffer_map (buffer, &map, GST_MAP_READ);
   size = map.size;
-  timestamp = GST_BUFFER_TIMESTAMP (buffer);
+  timestamp = GST_BUFFER_PTS (buffer);
   duration = GST_BUFFER_DURATION (buffer);
   avail = gst_adapter_available (rtpmp4vpay->adapter);
 
@@ -447,46 +452,57 @@ gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
     rtpmp4vpay->duration = 0;
   }
 
-  /* depay incomming data and see if we need to start a new RTP
+  /* depay incoming data and see if we need to start a new RTP
    * packet */
   flush =
       gst_rtp_mp4v_pay_depay_data (rtpmp4vpay, map.data, size, &strip, &vopi);
   gst_buffer_unmap (buffer, &map);
 
   if (strip) {
-    /* strip off config if requested */
-    if (!(rtpmp4vpay->config_interval > 0)) {
+    /* strip off config if requested, do not strip off if the
+     * config_interval is set to -1 */
+    if (!(rtpmp4vpay->config_interval > 0)
+        && !(rtpmp4vpay->config_interval == -1)) {
       GstBuffer *subbuf;
 
       GST_LOG_OBJECT (rtpmp4vpay, "stripping config at %d, size %d", strip,
           (gint) size - strip);
 
       /* strip off header */
-      subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_MEMORY, strip,
+      subbuf = gst_buffer_copy_region (buffer, GST_BUFFER_COPY_ALL, strip,
           size - strip);
-      GST_BUFFER_TIMESTAMP (subbuf) = timestamp;
+      GST_BUFFER_PTS (subbuf) = timestamp;
       gst_buffer_unref (buffer);
       buffer = subbuf;
 
       size = gst_buffer_get_size (buffer);
     } else {
+      running_time =
+          gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+          timestamp);
+
       GST_LOG_OBJECT (rtpmp4vpay, "found config in stream");
-      rtpmp4vpay->last_config = timestamp;
+      rtpmp4vpay->last_config = running_time;
     }
   }
 
   /* there is a config request, see if we need to insert it */
   if (vopi && (rtpmp4vpay->config_interval > 0) && rtpmp4vpay->config) {
+    running_time =
+        gst_segment_to_running_time (&basepayload->segment, GST_FORMAT_TIME,
+        timestamp);
+
     if (rtpmp4vpay->last_config != -1) {
       guint64 diff;
 
       GST_LOG_OBJECT (rtpmp4vpay,
           "now %" GST_TIME_FORMAT ", last VOP-I %" GST_TIME_FORMAT,
-          GST_TIME_ARGS (timestamp), GST_TIME_ARGS (rtpmp4vpay->last_config));
+          GST_TIME_ARGS (running_time),
+          GST_TIME_ARGS (rtpmp4vpay->last_config));
 
       /* calculate diff between last config in milliseconds */
-      if (timestamp > rtpmp4vpay->last_config) {
-        diff = timestamp - rtpmp4vpay->last_config;
+      if (running_time > rtpmp4vpay->last_config) {
+        diff = running_time - rtpmp4vpay->last_config;
       } else {
         diff = 0;
       }
@@ -495,7 +511,6 @@ gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
           "interval since last config %" GST_TIME_FORMAT, GST_TIME_ARGS (diff));
 
       /* bigger than interval, queue config */
-      /* FIXME should convert timestamps to running time */
       if (GST_TIME_AS_SECONDS (diff) >= rtpmp4vpay->config_interval) {
         GST_DEBUG_OBJECT (rtpmp4vpay, "time to send config");
         send_config = TRUE;
@@ -505,20 +520,26 @@ gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
       GST_DEBUG_OBJECT (rtpmp4vpay, "no previous config time, send now");
       send_config = TRUE;
     }
+  }
 
-    if (send_config) {
-      /* we need to send config now first */
-      GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
+  if (vopi && (rtpmp4vpay->config_interval == -1)) {
+    GST_DEBUG_OBJECT (rtpmp4vpay, "sending config before current IDR frame");
+    /* send config before every IDR frame */
+    send_config = TRUE;
+  }
 
-      /* insert header */
-      buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
+  if (send_config) {
+    /* we need to send config now first */
+    GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
 
-      GST_BUFFER_TIMESTAMP (buffer) = timestamp;
-      size = gst_buffer_get_size (buffer);
+    /* insert header */
+    buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
 
-      if (timestamp != -1) {
-        rtpmp4vpay->last_config = timestamp;
-      }
+    GST_BUFFER_PTS (buffer) = timestamp;
+    size = gst_buffer_get_size (buffer);
+
+    if (running_time != -1) {
+      rtpmp4vpay->last_config = running_time;
     }
   }
 
@@ -585,7 +606,7 @@ gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
 
   switch (prop_id) {
     case PROP_CONFIG_INTERVAL:
-      rtpmp4vpay->config_interval = g_value_get_uint (value);
+      rtpmp4vpay->config_interval = g_value_get_int (value);
       break;
     default:
       break;
@@ -602,7 +623,7 @@ gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
 
   switch (prop_id) {
     case PROP_CONFIG_INTERVAL:
-      g_value_set_uint (value, rtpmp4vpay->config_interval);
+      g_value_set_int (value, rtpmp4vpay->config_interval);
       break;
     default:
       break;
@@ -612,6 +633,10 @@ gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
 gboolean
 gst_rtp_mp4v_pay_plugin_init (GstPlugin * plugin)
 {
+  /* Note: This element is marked at a "+1" rank to make sure that
+   * auto-plugging of payloaders for MPEG4 elementary streams don't
+   * end up using the 'rtpmp4gpay' element (generic mpeg4) which isn't
+   * as well supported as this RFC */
   return gst_element_register (plugin, "rtpmp4vpay",
-      GST_RANK_SECONDARY, GST_TYPE_RTP_MP4V_PAY);
+      GST_RANK_SECONDARY + 1, GST_TYPE_RTP_MP4V_PAY);
 }