rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4vpay.c
index 5185d03..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)
@@ -58,8 +60,8 @@ GST_STATIC_PAD_TEMPLATE ("src",
 
 enum
 {
-  ARG_0,
-  ARG_CONFIG_INTERVAL
+  PROP_0,
+  PROP_CONFIG_INTERVAL
 };
 
 
@@ -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,21 +96,22 @@ 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",
       "Payload MPEG-4 video as RTP packets (RFC 3016)",
       "Wim Taymans <wim.taymans@gmail.com>");
 
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_CONFIG_INTERVAL,
-      g_param_spec_uint ("config-interval", "Config Send Interval",
+  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONFIG_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)
       );
 
@@ -223,10 +227,12 @@ gst_rtp_mp4v_pay_empty (GstRtpMP4VPay * rtpmp4vpay)
   gst_adapter_clear (rtpmp4vpay->adapter);
 }
 
+#define RTP_HEADER_LEN 12
+
 static GstFlowReturn
 gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
 {
-  guint avail;
+  guint avail, mtu;
   GstBuffer *outbuf;
   GstBuffer *outbuf_data = NULL;
   GstFlowReturn ret;
@@ -248,12 +254,12 @@ gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
   if (!avail)
     return GST_FLOW_OK;
 
-  ret = GST_FLOW_OK;
+  mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4vpay);
 
   /* Use buffer lists. Each frame will be put into a list
    * of buffers and the whole list will be pushed downstream
    * at once */
-  list = gst_buffer_list_new ();
+  list = gst_buffer_list_new_sized ((avail / (mtu - RTP_HEADER_LEN)) + 1);
 
   while (avail > 0) {
     guint towrite;
@@ -261,11 +267,11 @@ 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 */
-    towrite = MIN (packet_len, GST_RTP_BASE_PAYLOAD_MTU (rtpmp4vpay));
+    towrite = MIN (packet_len, mtu);
 
     /* this is the payload length */
     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
@@ -275,17 +281,18 @@ gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
     outbuf = gst_rtp_buffer_new_allocate (0, 0, 0);
 
     /* Take buffer with the payload from the adapter */
-    outbuf_data = gst_adapter_take_buffer (rtpmp4vpay->adapter, payload_len);
+    outbuf_data = gst_adapter_take_buffer_fast (rtpmp4vpay->adapter,
+        payload_len);
 
     avail -= payload_len;
 
     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);
@@ -406,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,
@@ -423,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;
@@ -431,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);
 
@@ -444,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;
       }
@@ -492,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;
@@ -502,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 (vopi && (rtpmp4vpay->config_interval == -1)) {
+    GST_DEBUG_OBJECT (rtpmp4vpay, "sending config before current IDR frame");
+    /* send config before every IDR frame */
+    send_config = TRUE;
+  }
 
-    if (send_config) {
-      /* we need to send config now first */
-      GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
+  if (send_config) {
+    /* we need to send config now first */
+    GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
 
-      /* insert header */
-      buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
+    /* insert header */
+    buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
 
-      GST_BUFFER_TIMESTAMP (buffer) = timestamp;
-      size = gst_buffer_get_size (buffer);
+    GST_BUFFER_PTS (buffer) = timestamp;
+    size = gst_buffer_get_size (buffer);
 
-      if (timestamp != -1) {
-        rtpmp4vpay->last_config = timestamp;
-      }
+    if (running_time != -1) {
+      rtpmp4vpay->last_config = running_time;
     }
   }
 
@@ -581,8 +605,8 @@ gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
 
   switch (prop_id) {
-    case ARG_CONFIG_INTERVAL:
-      rtpmp4vpay->config_interval = g_value_get_uint (value);
+    case PROP_CONFIG_INTERVAL:
+      rtpmp4vpay->config_interval = g_value_get_int (value);
       break;
     default:
       break;
@@ -598,8 +622,8 @@ gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
 
   switch (prop_id) {
-    case ARG_CONFIG_INTERVAL:
-      g_value_set_uint (value, rtpmp4vpay->config_interval);
+    case PROP_CONFIG_INTERVAL:
+      g_value_set_int (value, rtpmp4vpay->config_interval);
       break;
     default:
       break;
@@ -609,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);
 }