rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpac3pay.c
index 851b92d..1fecced 100644 (file)
  *
  * 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.
+ */
+
+/**
+ * SECTION:element-rtpac3pay
+ * @title: rtpac3pay
+ * @see_also: rtpac3depay
+ *
+ * Payload AC3 audio into RTP packets according to RFC 4184.
+ * For detailed information see: http://www.rfc-editor.org/rfc/rfc4184.txt
+ *
+ * ## Example pipeline
+ * |[
+ * gst-launch-1.0 -v audiotestsrc ! avenc_ac3 ! rtpac3pay ! udpsink
+ * ]| This example pipeline will encode and payload AC3 stream. Refer to
+ * the rtpac3depay example to depayload and decode the RTP stream.
+ *
  */
 
 #ifdef HAVE_CONFIG_H
 #include <string.h>
 
 #include <gst/rtp/gstrtpbuffer.h>
+#include <gst/audio/audio.h>
 
 #include "gstrtpac3pay.h"
+#include "gstrtputils.h"
 
 GST_DEBUG_CATEGORY_STATIC (rtpac3pay_debug);
 #define GST_CAT_DEFAULT (rtpac3pay_debug)
@@ -53,48 +71,48 @@ static void gst_rtp_ac3_pay_finalize (GObject * object);
 static GstStateChangeReturn gst_rtp_ac3_pay_change_state (GstElement * element,
     GstStateChange transition);
 
-static gboolean gst_rtp_ac3_pay_setcaps (GstBaseRTPPayload * payload,
+static gboolean gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload,
     GstCaps * caps);
-static gboolean gst_rtp_ac3_pay_handle_event (GstBaseRTPPayload * payload,
+static gboolean gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload,
     GstEvent * event);
 static GstFlowReturn gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay);
-static GstFlowReturn gst_rtp_ac3_pay_handle_buffer (GstBaseRTPPayload * payload,
+static GstFlowReturn gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * payload,
     GstBuffer * buffer);
 
 #define gst_rtp_ac3_pay_parent_class parent_class
-G_DEFINE_TYPE (GstRtpAC3Pay, gst_rtp_ac3_pay, GST_TYPE_BASE_RTP_PAYLOAD);
+G_DEFINE_TYPE (GstRtpAC3Pay, gst_rtp_ac3_pay, GST_TYPE_RTP_BASE_PAYLOAD);
 
 static void
 gst_rtp_ac3_pay_class_init (GstRtpAC3PayClass * klass)
 {
   GObjectClass *gobject_class;
   GstElementClass *gstelement_class;
-  GstBaseRTPPayloadClass *gstbasertppayload_class;
+  GstRTPBasePayloadClass *gstrtpbasepayload_class;
 
   GST_DEBUG_CATEGORY_INIT (rtpac3pay_debug, "rtpac3pay", 0,
       "AC3 Audio RTP Depayloader");
 
   gobject_class = (GObjectClass *) klass;
   gstelement_class = (GstElementClass *) klass;
-  gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
+  gstrtpbasepayload_class = (GstRTPBasePayloadClass *) klass;
 
   gobject_class->finalize = gst_rtp_ac3_pay_finalize;
 
   gstelement_class->change_state = gst_rtp_ac3_pay_change_state;
 
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&gst_rtp_ac3_pay_src_template));
-  gst_element_class_add_pad_template (gstelement_class,
-      gst_static_pad_template_get (&gst_rtp_ac3_pay_sink_template));
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_ac3_pay_src_template);
+  gst_element_class_add_static_pad_template (gstelement_class,
+      &gst_rtp_ac3_pay_sink_template);
 
-  gst_element_class_set_details_simple (gstelement_class,
+  gst_element_class_set_static_metadata (gstelement_class,
       "RTP AC3 audio payloader", "Codec/Payloader/Network/RTP",
       "Payload AC3 audio as RTP packets (RFC 4184)",
       "Wim Taymans <wim.taymans@gmail.com>");
 
-  gstbasertppayload_class->set_caps = gst_rtp_ac3_pay_setcaps;
-  gstbasertppayload_class->handle_event = gst_rtp_ac3_pay_handle_event;
-  gstbasertppayload_class->handle_buffer = gst_rtp_ac3_pay_handle_buffer;
+  gstrtpbasepayload_class->set_caps = gst_rtp_ac3_pay_setcaps;
+  gstrtpbasepayload_class->sink_event = gst_rtp_ac3_pay_sink_event;
+  gstrtpbasepayload_class->handle_buffer = gst_rtp_ac3_pay_handle_buffer;
 }
 
 static void
@@ -125,7 +143,7 @@ gst_rtp_ac3_pay_reset (GstRtpAC3Pay * pay)
 }
 
 static gboolean
-gst_rtp_ac3_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
+gst_rtp_ac3_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
 {
   gboolean res;
   gint rate;
@@ -136,14 +154,14 @@ gst_rtp_ac3_pay_setcaps (GstBaseRTPPayload * payload, GstCaps * caps)
   if (!gst_structure_get_int (structure, "rate", &rate))
     rate = 90000;               /* default */
 
-  gst_basertppayload_set_options (payload, "audio", TRUE, "AC3", rate);
-  res = gst_basertppayload_set_outcaps (payload, NULL);
+  gst_rtp_base_payload_set_options (payload, "audio", TRUE, "AC3", rate);
+  res = gst_rtp_base_payload_set_outcaps (payload, NULL);
 
   return res;
 }
 
 static gboolean
-gst_rtp_ac3_pay_handle_event (GstBaseRTPPayload * payload, GstEvent * event)
+gst_rtp_ac3_pay_sink_event (GstRTPBasePayload * payload, GstEvent * event)
 {
   gboolean res;
   GstRtpAC3Pay *rtpac3pay;
@@ -162,8 +180,7 @@ gst_rtp_ac3_pay_handle_event (GstBaseRTPPayload * payload, GstEvent * event)
       break;
   }
 
-  res =
-      GST_BASE_RTP_PAYLOAD_CLASS (parent_class)->handle_event (payload, event);
+  res = GST_RTP_BASE_PAYLOAD_CLASS (parent_class)->sink_event (payload, event);
 
   return res;
 }
@@ -235,7 +252,7 @@ gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
   /* number of frames */
   NF = rtpac3pay->NF;
 
-  mtu = GST_BASE_RTP_PAYLOAD_MTU (rtpac3pay);
+  mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpac3pay);
 
   GST_LOG_OBJECT (rtpac3pay, "flushing %u bytes", avail);
 
@@ -245,6 +262,7 @@ gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
     guint payload_len;
     guint packet_len;
     GstRTPBuffer rtp = { NULL, };
+    GstBuffer *payload_buffer;
 
     /* this will be the total length of the packet */
     packet_len = gst_rtp_buffer_calc_packet_len (2 + avail, 0, 0);
@@ -256,7 +274,9 @@ gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
     payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
 
     /* create buffer to hold the payload */
-    outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
+    outbuf =
+        gst_rtp_base_payload_allocate_output_buffer (GST_RTP_BASE_PAYLOAD
+        (rtpac3pay), 2, 0, 0);
 
     if (FT == 0) {
       /* check if it all fits */
@@ -299,49 +319,55 @@ gst_rtp_ac3_pay_flush (GstRtpAC3Pay * rtpac3pay)
     payload[1] = NF;
     payload_len -= 2;
 
-    gst_adapter_copy (rtpac3pay->adapter, &payload[2], 0, payload_len);
-    gst_adapter_flush (rtpac3pay->adapter, payload_len);
-
-    avail -= payload_len;
-    if (avail == 0)
+    if (avail == payload_len)
       gst_rtp_buffer_set_marker (&rtp, TRUE);
     gst_rtp_buffer_unmap (&rtp);
 
-    GST_BUFFER_TIMESTAMP (outbuf) = rtpac3pay->first_ts;
+    payload_buffer =
+        gst_adapter_take_buffer_fast (rtpac3pay->adapter, payload_len);
+
+    gst_rtp_copy_audio_meta (rtpac3pay, outbuf, payload_buffer);
+
+    outbuf = gst_buffer_append (outbuf, payload_buffer);
+
+    avail -= payload_len;
+
+    GST_BUFFER_PTS (outbuf) = rtpac3pay->first_ts;
     GST_BUFFER_DURATION (outbuf) = rtpac3pay->duration;
 
-    ret = gst_basertppayload_push (GST_BASE_RTP_PAYLOAD (rtpac3pay), outbuf);
+    ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpac3pay), outbuf);
   }
 
   return ret;
 }
 
 static GstFlowReturn
-gst_rtp_ac3_pay_handle_buffer (GstBaseRTPPayload * basepayload,
+gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * basepayload,
     GstBuffer * buffer)
 {
   GstRtpAC3Pay *rtpac3pay;
   GstFlowReturn ret;
-  gsize size, avail, left, NF;
-  guint8 *data, *p;
+  gsize avail, left, NF;
+  GstMapInfo map;
+  guint8 *p;
   guint packet_len;
   GstClockTime duration, timestamp;
 
   rtpac3pay = GST_RTP_AC3_PAY (basepayload);
 
-  data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
+  gst_buffer_map (buffer, &map, GST_MAP_READ);
   duration = GST_BUFFER_DURATION (buffer);
-  timestamp = GST_BUFFER_TIMESTAMP (buffer);
+  timestamp = GST_BUFFER_PTS (buffer);
 
   if (GST_BUFFER_IS_DISCONT (buffer)) {
     GST_DEBUG_OBJECT (rtpac3pay, "DISCONT");
     gst_rtp_ac3_pay_reset (rtpac3pay);
   }
 
-  /* count the amount of incomming packets */
+  /* count the amount of incoming packets */
   NF = 0;
-  left = size;
-  p = data;
+  left = map.size;
+  p = map.data;
   while (TRUE) {
     guint bsid, fscod, frmsizecod, frame_size;
 
@@ -368,12 +394,13 @@ gst_rtp_ac3_pay_handle_buffer (GstBaseRTPPayload * basepayload,
       break;
 
     NF++;
-    GST_DEBUG_OBJECT (rtpac3pay, "found frame %u of size %u", NF, frame_size);
+    GST_DEBUG_OBJECT (rtpac3pay, "found frame %" G_GSIZE_FORMAT " of size %u",
+        NF, frame_size);
 
     p += frame_size;
     left -= frame_size;
   }
-  gst_buffer_unmap (buffer, data, size);
+  gst_buffer_unmap (buffer, &map);
   if (NF == 0)
     goto no_frames;
 
@@ -381,11 +408,11 @@ gst_rtp_ac3_pay_handle_buffer (GstBaseRTPPayload * basepayload,
 
   /* get packet length of previous data and this new data,
    * payload length includes a 4 byte header */
-  packet_len = gst_rtp_buffer_calc_packet_len (2 + avail + size, 0, 0);
+  packet_len = gst_rtp_buffer_calc_packet_len (2 + avail + map.size, 0, 0);
 
   /* if this buffer is going to overflow the packet, flush what we
    * have. */
-  if (gst_basertppayload_is_filled (basepayload,
+  if (gst_rtp_base_payload_is_filled (basepayload,
           packet_len, rtpac3pay->duration + duration)) {
     ret = gst_rtp_ac3_pay_flush (rtpac3pay);
     avail = 0;