rtph263pay: Stop using an adapter and directly use the buffer
authorSebastian Dröge <sebastian@centricular.com>
Thu, 2 Jul 2015 07:17:59 +0000 (09:17 +0200)
committerSebastian Dröge <sebastian@centricular.com>
Thu, 2 Jul 2015 07:26:27 +0000 (09:26 +0200)
We always pushed one buffer into the adapter, then handled exactly that one
buffer and flushed it from the adapter. Now also don't memcpy() the actual
payload but just attach the input buffer's data to the output buffer.

This code still needs some serious refactoring/rewriting.

gst/rtp/gstrtph263pay.c
gst/rtp/gstrtph263pay.h

index 152093a..c39aa70 100644 (file)
@@ -440,8 +440,6 @@ gst_rtp_h263_pay_class_init (GstRtpH263PayClass * klass)
 static void
 gst_rtp_h263_pay_init (GstRtpH263Pay * rtph263pay)
 {
-  rtph263pay->adapter = gst_adapter_new ();
-
   rtph263pay->prop_payload_mode = DEFAULT_MODE_A;
 }
 
@@ -452,8 +450,7 @@ gst_rtp_h263_pay_finalize (GObject * object)
 
   rtph263pay = GST_RTP_H263_PAY (object);
 
-  g_object_unref (rtph263pay->adapter);
-  rtph263pay->adapter = NULL;
+  gst_buffer_replace (&rtph263pay->current_buffer, NULL);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -1268,14 +1265,12 @@ gst_rtp_h263_pay_push (GstRtpH263Pay * rtph263pay,
    * Splat the payload header values
    */
   guint8 *header;
-  guint8 *payload;
   GstFlowReturn ret;
   GstRTPBuffer rtp = { NULL };
 
   gst_rtp_buffer_map (package->outbuf, GST_MAP_WRITE, &rtp);
 
   header = gst_rtp_buffer_get_payload (&rtp);
-  payload = header + package->mode;
 
   switch (package->mode) {
     case GST_RTP_H263_PAYLOAD_HEADER_MODE_A:
@@ -1293,13 +1288,6 @@ gst_rtp_h263_pay_push (GstRtpH263Pay * rtph263pay,
       return GST_FLOW_ERROR;
   }
 
-
-  /*
-   * Copy the payload data in the buffer
-   */
-  GST_DEBUG ("Copying memory");
-  memcpy (payload, (guint8 *) package->payload_start, package->payload_len);
-
   /*
    * timestamp the buffer
    */
@@ -1311,6 +1299,14 @@ gst_rtp_h263_pay_push (GstRtpH263Pay * rtph263pay,
 
   gst_rtp_buffer_unmap (&rtp);
 
+  /*
+   * Copy the payload data in the buffer
+   */
+  GST_DEBUG ("Copying memory");
+  gst_buffer_copy_into (package->outbuf, rtph263pay->current_buffer,
+      GST_BUFFER_COPY_MEMORY, package->payload_start - rtph263pay->map.data,
+      package->payload_len);
+
   ret =
       gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtph263pay),
       package->outbuf);
@@ -1343,8 +1339,7 @@ gst_rtp_h263_pay_A_fragment_push (GstRtpH263Pay * rtph263pay,
 
   pack->gobn = context->gobs[first]->gobn;
   pack->mode = GST_RTP_H263_PAYLOAD_HEADER_MODE_A;
-  pack->outbuf =
-      gst_rtp_buffer_new_allocate (pack->payload_len + pack->mode, 0, 0);
+  pack->outbuf = gst_rtp_buffer_new_allocate (pack->mode, 0, 0);
 
   GST_DEBUG ("Sending len:%d data to push function", pack->payload_len);
 
@@ -1400,8 +1395,7 @@ gst_rtp_h263_pay_B_fragment_push (GstRtpH263Pay * rtph263pay,
   }
 
   pack->payload_len = pack->payload_end - pack->payload_start + 1;
-  pack->outbuf =
-      gst_rtp_buffer_new_allocate (pack->payload_len + pack->mode, 0, 0);
+  pack->outbuf = gst_rtp_buffer_new_allocate (pack->mode, 0, 0);
 
   return gst_rtp_h263_pay_push (rtph263pay, context, pack);
 }
@@ -1441,7 +1435,6 @@ gst_rtp_h263_pay_mode_B_fragment (GstRtpH263Pay * rtph263pay,
           &boundry.end, &gob->end) != 0) {
     GST_ERROR
         ("The rest of the bits should be 0, exiting, because something bad happend");
-    gst_adapter_flush (rtph263pay->adapter, rtph263pay->available_data);
     goto decode_error;
   }
   //The first GOB of a frame "has no" actual header - PICTURE header is his header
@@ -1613,8 +1606,7 @@ gst_rtp_h263_send_entire_frame (GstRtpH263Pay * rtph263pay,
   GST_DEBUG ("Available data: %d", rtph263pay->available_data);
 
   pack->outbuf =
-      gst_rtp_buffer_new_allocate (pack->payload_len +
-      GST_RTP_H263_PAYLOAD_HEADER_MODE_A, 0, 0);
+      gst_rtp_buffer_new_allocate (GST_RTP_H263_PAYLOAD_HEADER_MODE_A, 0, 0);
 
   return gst_rtp_h263_pay_push (rtph263pay, context, pack);
 }
@@ -1639,16 +1631,15 @@ gst_rtp_h263_pay_flush (GstRtpH263Pay * rtph263pay)
       GST_RTP_H263_PAYLOAD_HEADER_MODE_C);
 
   GST_DEBUG ("MTU: %d", context->mtu);
-  rtph263pay->available_data = gst_adapter_available (rtph263pay->adapter);
+  rtph263pay->available_data = gst_buffer_get_size (rtph263pay->current_buffer);
   if (rtph263pay->available_data == 0) {
     ret = GST_FLOW_OK;
     goto end;
   }
 
   /* Get a pointer to all the data for the frame */
-  rtph263pay->data =
-      (guint8 *) gst_adapter_map (rtph263pay->adapter,
-      rtph263pay->available_data);
+  gst_buffer_map (rtph263pay->current_buffer, &rtph263pay->map, GST_MAP_READ);
+  rtph263pay->data = (guint8 *) rtph263pay->map.data;
 
   /* Picture header */
   context->piclayer = (GstRtpH263PayPic *) rtph263pay->data;
@@ -1804,8 +1795,8 @@ gst_rtp_h263_pay_flush (GstRtpH263Pay * rtph263pay)
 end:
   gst_rtp_h263_pay_context_destroy (context,
       context->piclayer->ptype_srcformat);
-  gst_adapter_unmap (rtph263pay->adapter);
-  gst_adapter_flush (rtph263pay->adapter, rtph263pay->available_data);
+  gst_buffer_unmap (rtph263pay->current_buffer, &rtph263pay->map);
+  gst_buffer_replace (&rtph263pay->current_buffer, NULL);
 
   return ret;
 }
@@ -1822,8 +1813,10 @@ gst_rtp_h263_pay_handle_buffer (GstRTPBasePayload * payload, GstBuffer * buffer)
 
   rtph263pay->first_ts = GST_BUFFER_PTS (buffer);
 
+  gst_buffer_replace (&rtph263pay->current_buffer, buffer);
+  gst_buffer_unref (buffer);
+
   /* we always encode and flush a full picture */
-  gst_adapter_push (rtph263pay->adapter, buffer);
   ret = gst_rtp_h263_pay_flush (rtph263pay);
   GST_DEBUG ("-------------------- END FRAME ---------------");
 
index d41c3ed..2abfc87 100644 (file)
@@ -24,7 +24,6 @@
 
 #include <gst/gst.h>
 #include <gst/rtp/gstrtpbasepayload.h>
-#include <gst/base/gstadapter.h>
 
 G_BEGIN_DECLS
 #define GST_TYPE_RTP_H263_PAY \
@@ -63,7 +62,9 @@ struct _GstRtpH263Pay
 {
   GstRTPBasePayload payload;
 
-  GstAdapter *adapter;
+  GstBuffer *current_buffer;
+  GstMapInfo map;
+
   GstClockTime first_ts;
   gboolean prop_payload_mode;
   guint8 *data;