rtpmp2tpay: respect mtu and packet boundaries

See #659915.
This commit is contained in:
Mark Nauwelaerts 2012-05-18 12:53:44 +02:00
parent f96d18e959
commit 182596b3ab

View file

@ -117,35 +117,56 @@ gst_rtp_mp2t_pay_setcaps (GstRTPBasePayload * payload, GstCaps * caps)
static GstFlowReturn static GstFlowReturn
gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay) gst_rtp_mp2t_pay_flush (GstRTPMP2TPay * rtpmp2tpay)
{ {
guint avail; guint avail, mtu;
guint8 *payload; GstFlowReturn ret = GST_FLOW_OK;
GstFlowReturn ret;
GstBuffer *outbuf; GstBuffer *outbuf;
GstRTPBuffer rtp = { NULL };
avail = gst_adapter_available (rtpmp2tpay->adapter); avail = gst_adapter_available (rtpmp2tpay->adapter);
if (avail == 0)
return GST_FLOW_OK; mtu = GST_RTP_BASE_PAYLOAD_MTU (rtpmp2tpay);
outbuf = gst_rtp_buffer_new_allocate (avail, 0, 0);
while (avail > 0 && (ret == GST_FLOW_OK)) {
guint towrite;
guint8 *payload;
guint payload_len;
guint packet_len;
GstRTPBuffer rtp = { NULL };
/* 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, mtu);
/* this is the payload length */
payload_len = gst_rtp_buffer_calc_payload_len (towrite, 0, 0);
payload_len -= payload_len % 188;
/* need whole packets */
if (!payload_len)
break;
/* create buffer to hold the payload */
outbuf = gst_rtp_buffer_new_allocate (payload_len, 0, 0);
/* get payload */ /* get payload */
gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp); gst_rtp_buffer_map (outbuf, GST_MAP_WRITE, &rtp);
payload = gst_rtp_buffer_get_payload (&rtp); payload = gst_rtp_buffer_get_payload (&rtp);
/* copy stuff from adapter to payload */ /* copy stuff from adapter to payload */
gst_adapter_copy (rtpmp2tpay->adapter, payload, 0, avail); gst_adapter_copy (rtpmp2tpay->adapter, payload, 0, payload_len);
gst_rtp_buffer_unmap (&rtp); gst_rtp_buffer_unmap (&rtp);
gst_adapter_flush (rtpmp2tpay->adapter, payload_len);
avail -= payload_len;
GST_BUFFER_TIMESTAMP (outbuf) = rtpmp2tpay->first_ts; GST_BUFFER_TIMESTAMP (outbuf) = rtpmp2tpay->first_ts;
GST_BUFFER_DURATION (outbuf) = rtpmp2tpay->duration; GST_BUFFER_DURATION (outbuf) = rtpmp2tpay->duration;
GST_DEBUG_OBJECT (rtpmp2tpay, "pushing buffer of size %" G_GSIZE_FORMAT, GST_DEBUG_OBJECT (rtpmp2tpay, "pushing buffer of size %d",
gst_buffer_get_size (outbuf)); gst_buffer_get_size (outbuf));
ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp2tpay), outbuf); ret = gst_rtp_base_payload_push (GST_RTP_BASE_PAYLOAD (rtpmp2tpay), outbuf);
}
/* flush the adapter content */
gst_adapter_flush (rtpmp2tpay->adapter, avail);
return ret; return ret;
} }
@ -165,6 +186,7 @@ gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload * basepayload,
timestamp = GST_BUFFER_TIMESTAMP (buffer); timestamp = GST_BUFFER_TIMESTAMP (buffer);
duration = GST_BUFFER_DURATION (buffer); duration = GST_BUFFER_DURATION (buffer);
again:
ret = GST_FLOW_OK; ret = GST_FLOW_OK;
avail = gst_adapter_available (rtpmp2tpay->adapter); avail = gst_adapter_available (rtpmp2tpay->adapter);
@ -174,13 +196,12 @@ gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload * basepayload,
rtpmp2tpay->duration = duration; rtpmp2tpay->duration = duration;
} }
/* get packet length of previous data and this new data, /* 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 (avail + size, 0, 0);
packet_len = gst_rtp_buffer_calc_packet_len (4 + avail + size, 0, 0);
/* if this buffer is going to overflow the packet, flush what we /* if this buffer is going to overflow the packet, flush what we have,
* have. */ * or if upstream is handing us several packets, to keep latency low */
if (gst_rtp_base_payload_is_filled (basepayload, if (!size || gst_rtp_base_payload_is_filled (basepayload,
packet_len, rtpmp2tpay->duration + duration)) { packet_len, rtpmp2tpay->duration + duration)) {
ret = gst_rtp_mp2t_pay_flush (rtpmp2tpay); ret = gst_rtp_mp2t_pay_flush (rtpmp2tpay);
rtpmp2tpay->first_ts = timestamp; rtpmp2tpay->first_ts = timestamp;
@ -193,7 +214,15 @@ gst_rtp_mp2t_pay_handle_buffer (GstRTPBasePayload * basepayload,
} }
/* copy buffer to adapter */ /* copy buffer to adapter */
if (buffer) {
gst_adapter_push (rtpmp2tpay->adapter, buffer); gst_adapter_push (rtpmp2tpay->adapter, buffer);
buffer = NULL;
}
if (size >= (188 * 2)) {
size = 0;
goto again;
}
return ret; return ret;