rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpmp4vpay.c
index 8a94595..2980339 100644 (file)
@@ -13,8 +13,8 @@
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #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)
@@ -35,7 +37,7 @@ static GstStaticPadTemplate gst_rtp_mp4v_pay_sink_template =
     GST_PAD_SINK,
     GST_PAD_ALWAYS,
     GST_STATIC_CAPS ("video/mpeg,"
-        "mpegversion=(int) 4," "systemstream=(boolean)false;" "video/x-xvid")
+        "mpegversion=(int) 4, systemstream=(boolean)false;" "video/x-divx")
     );
 
 static GstStaticPadTemplate gst_rtp_mp4v_pay_src_template =
@@ -54,16 +56,12 @@ GST_STATIC_PAD_TEMPLATE ("src",
     )
     );
 
-#define DEFAULT_SEND_CONFIG     FALSE
-#define DEFAULT_BUFFER_LIST     FALSE
 #define DEFAULT_CONFIG_INTERVAL 0
 
 enum
 {
-  ARG_0,
-  ARG_SEND_CONFIG,
-  ARG_BUFFER_LIST,
-  ARG_CONFIG_INTERVAL
+  PROP_0,
+  PROP_CONFIG_INTERVAL
 };
 
 
@@ -78,13 +76,14 @@ static gboolean gst_rtp_mp4v_pay_setcaps (GstRTPBasePayload * payload,
     GstCaps * caps);
 static GstFlowReturn gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload *
     payload, GstBuffer * buffer);
-static gboolean gst_rtp_mp4v_pay_handle_event (GstRTPBasePayload * pay,
+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;
@@ -97,32 +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_details_simple (gstelement_class,
+  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_SEND_CONFIG,
-      g_param_spec_boolean ("send-config", "Send Config",
-          "Send the config parameters in RTP packets as well(deprecated "
-          "see config-interval)",
-          DEFAULT_SEND_CONFIG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-
-  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BUFFER_LIST,
-      g_param_spec_boolean ("buffer-list", "Buffer Array",
-          "Use Buffer Arrays",
-          DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
-
-  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)
       );
 
@@ -130,7 +119,7 @@ G_DEFINE_TYPE (GstRtpMP4VPay, gst_rtp_mp4v_pay, GST_TYPE_RTP_BASE_PAYLOAD)
 
   gstrtpbasepayload_class->set_caps = gst_rtp_mp4v_pay_setcaps;
   gstrtpbasepayload_class->handle_buffer = gst_rtp_mp4v_pay_handle_buffer;
-  gstrtpbasepayload_class->handle_event = gst_rtp_mp4v_pay_handle_event;
+  gstrtpbasepayload_class->sink_event = gst_rtp_mp4v_pay_sink_event;
 
   GST_DEBUG_CATEGORY_INIT (rtpmp4vpay_debug, "rtpmp4vpay", 0,
       "MP4 video RTP Payloader");
@@ -142,8 +131,6 @@ gst_rtp_mp4v_pay_init (GstRtpMP4VPay * rtpmp4vpay)
   rtpmp4vpay->adapter = gst_adapter_new ();
   rtpmp4vpay->rate = 90000;
   rtpmp4vpay->profile = 1;
-  rtpmp4vpay->buffer_list = DEFAULT_BUFFER_LIST;
-  rtpmp4vpay->send_config = DEFAULT_SEND_CONFIG;
   rtpmp4vpay->need_config = TRUE;
   rtpmp4vpay->config_interval = DEFAULT_CONFIG_INTERVAL;
   rtpmp4vpay->last_config = -1;
@@ -240,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;
@@ -265,26 +254,24 @@ gst_rtp_mp4v_pay_flush (GstRtpMP4VPay * rtpmp4vpay)
   if (!avail)
     return GST_FLOW_OK;
 
-  ret = GST_FLOW_OK;
+  mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp4vpay);
 
-  if (rtpmp4vpay->buffer_list) {
-    /* 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 ();
-  }
+  /* 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_sized ((avail / (mtu - RTP_HEADER_LEN)) + 1);
 
   while (avail > 0) {
     guint towrite;
     guint payload_len;
     guint packet_len;
-    GstRTPBuffer rtp;
+    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);
@@ -294,33 +281,26 @@ 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);
 
-    outbuf = gst_buffer_join (outbuf, outbuf_data);
-
-    GST_BUFFER_TIMESTAMP (outbuf) = rtpmp4vpay->first_timestamp;
+    GST_BUFFER_PTS (outbuf) = rtpmp4vpay->first_timestamp;
 
-    if (rtpmp4vpay->buffer_list) {
-      /* add to list */
-      gst_buffer_list_insert (list, -1, outbuf);
-    } else {
-      ret =
-          gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp4vpay), outbuf);
-    }
+    /* add to list */
+    gst_buffer_list_insert (list, -1, outbuf);
   }
 
-  if (rtpmp4vpay->buffer_list) {
-    /* push the whole buffer list at once */
-    ret =
-        gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmp4vpay),
-        list);
-  }
+  /* push the whole buffer list at once */
+  ret =
+      gst_rtp_base_payload_push_list (GST_RTP_BASE_PAYLOAD (rtpmp4vpay), list);
 
   return ret;
 }
@@ -387,14 +367,12 @@ gst_rtp_mp4v_pay_depay_data (GstRtpMP4VPay * enc, guint8 * data, guint size,
       }
       /* if config string changed or new profile, make new caps */
       if (!equal || newprofile) {
-        guint8 *bdata;
-
         if (enc->config)
           gst_buffer_unref (enc->config);
         enc->config = gst_buffer_new_and_alloc (i);
-        bdata = gst_buffer_map (enc->config, NULL, NULL, GST_MAP_WRITE);
-        memcpy (bdata, data, i);
-        gst_buffer_unmap (enc->config, bdata, -1);
+
+        gst_buffer_fill (enc->config, 0, data, i);
+
         gst_rtp_mp4v_pay_new_caps (enc);
       }
       *strip = i;
@@ -435,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,
@@ -444,22 +422,24 @@ gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
   GstRtpMP4VPay *rtpmp4vpay;
   GstFlowReturn ret;
   guint avail;
-  gsize size;
   guint packet_len;
-  guint8 *data;
+  GstMapInfo map;
+  gsize size;
   gboolean flush;
   gint strip;
   GstClockTime timestamp, duration;
   gboolean vopi;
   gboolean send_config;
+  GstClockTime running_time = GST_CLOCK_TIME_NONE;
 
   ret = GST_FLOW_OK;
   send_config = FALSE;
 
   rtpmp4vpay = GST_RTP_MP4V_PAY (basepayload);
 
-  data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
-  timestamp = GST_BUFFER_TIMESTAMP (buffer);
+  gst_buffer_map (buffer, &map, GST_MAP_READ);
+  size = map.size;
+  timestamp = GST_BUFFER_PTS (buffer);
   duration = GST_BUFFER_DURATION (buffer);
   avail = gst_adapter_available (rtpmp4vpay->adapter);
 
@@ -472,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, data, size, &strip, &vopi);
-  gst_buffer_unmap (buffer, data, -1);
-  data = NULL;
+  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;
       }
@@ -520,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;
@@ -530,25 +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 */
-      GstBuffer *superbuf;
-
-      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 */
-      superbuf = gst_buffer_merge (rtpmp4vpay->config, buffer);
+  if (send_config) {
+    /* we need to send config now first */
+    GST_LOG_OBJECT (rtpmp4vpay, "inserting config in stream");
 
-      GST_BUFFER_TIMESTAMP (superbuf) = timestamp;
-      gst_buffer_unref (buffer);
-      buffer = superbuf;
+    /* insert header */
+    buffer = gst_buffer_append (gst_buffer_ref (rtpmp4vpay->config), buffer);
 
-      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;
     }
   }
 
@@ -579,7 +570,7 @@ gst_rtp_mp4v_pay_handle_buffer (GstRTPBasePayload * basepayload,
 }
 
 static gboolean
-gst_rtp_mp4v_pay_handle_event (GstRTPBasePayload * pay, GstEvent * event)
+gst_rtp_mp4v_pay_sink_event (GstRTPBasePayload * pay, GstEvent * event)
 {
   GstRtpMP4VPay *rtpmp4vpay;
 
@@ -602,7 +593,7 @@ gst_rtp_mp4v_pay_handle_event (GstRTPBasePayload * pay, GstEvent * event)
   }
 
   /* let parent handle event too */
-  return FALSE;
+  return GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (pay, event);
 }
 
 static void
@@ -614,18 +605,8 @@ gst_rtp_mp4v_pay_set_property (GObject * object, guint prop_id,
   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
 
   switch (prop_id) {
-    case ARG_SEND_CONFIG:
-      rtpmp4vpay->send_config = g_value_get_boolean (value);
-      /* send the configuration once every minute */
-      if (rtpmp4vpay->send_config && !(rtpmp4vpay->config_interval > 0)) {
-        rtpmp4vpay->config_interval = 60;
-      }
-      break;
-    case ARG_BUFFER_LIST:
-      rtpmp4vpay->buffer_list = g_value_get_boolean (value);
-      break;
-    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;
@@ -641,14 +622,8 @@ gst_rtp_mp4v_pay_get_property (GObject * object, guint prop_id,
   rtpmp4vpay = GST_RTP_MP4V_PAY (object);
 
   switch (prop_id) {
-    case ARG_SEND_CONFIG:
-      g_value_set_boolean (value, rtpmp4vpay->send_config);
-      break;
-    case ARG_BUFFER_LIST:
-      g_value_set_boolean (value, rtpmp4vpay->buffer_list);
-      break;
-    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;
@@ -658,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);
 }