rtp: Update codes based on 1.18.4
[platform/upstream/gst-plugins-good.git] / gst / rtp / gstrtpac3pay.c
index 37c8631..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)
@@ -82,12 +100,12 @@ gst_rtp_ac3_pay_class_init (GstRtpAC3PayClass * klass)
 
   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>");
@@ -244,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);
@@ -255,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 */
@@ -298,15 +319,20 @@ 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_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpac3pay), outbuf);
@@ -321,26 +347,27 @@ gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * basepayload,
 {
   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;
 
@@ -373,7 +400,7 @@ gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * basepayload,
     p += frame_size;
     left -= frame_size;
   }
-  gst_buffer_unmap (buffer, data, size);
+  gst_buffer_unmap (buffer, &map);
   if (NF == 0)
     goto no_frames;
 
@@ -381,7 +408,7 @@ gst_rtp_ac3_pay_handle_buffer (GstRTPBasePayload * 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. */