summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Penquerc'h <vincent.penquerch@collabora.co.uk>2016-03-18 14:09:37 +0000
committerVincent Penquerc'h <vincent.penquerch@collabora.co.uk>2016-10-17 12:32:58 +0100
commit1630d2a0d7228765a169bbca4a745a46a9484e4a (patch)
treeb8a7dab3a5d013a9b5c3ed414b711def46c23df8
parent7c7e0a6a6dcee8f018a238b655cf269b8ca9e574 (diff)
mpegtsmux: fix buffer size mismatch in M2TS mode
In M2TS mode, we need an extra 4 bytes in the buffer, so need to ensure the buffer can contain these. The allocation site does not know the mode, so this is done in all cases.
-rw-r--r--gst/mpegtsmux/tsmux/tsmux.c26
1 files changed, 25 insertions, 1 deletions
diff --git a/gst/mpegtsmux/tsmux/tsmux.c b/gst/mpegtsmux/tsmux/tsmux.c
index 638de15aa..ee2870498 100644
--- a/gst/mpegtsmux/tsmux/tsmux.c
+++ b/gst/mpegtsmux/tsmux/tsmux.c
@@ -853,6 +853,7 @@ tsmux_section_write_packet (GstMpegtsSectionType * type,
gsize data_size = 0;
gsize payload_written;
guint len = 0, offset = 0, payload_len = 0;
+ guint extra_alloc_bytes = 0;
g_return_val_if_fail (section != NULL, FALSE);
g_return_val_if_fail (mux != NULL, FALSE);
@@ -907,12 +908,35 @@ tsmux_section_write_packet (GstMpegtsSectionType * type,
TS_DEBUG ("Creating packet buffer at offset "
"%" G_GSIZE_FORMAT " with length %u", payload_written, payload_len);
+ /* If in M2TS mode, we will need to resize to 4 bytes after the end
+ of the buffer. For performance reasons, we will now try to include
+ 4 extra bytes from the source buffer, then resize down, to avoid
+ having an extra 4 byte GstMemory appended. If the source buffer
+ does not have enough data for this, a new GstMemory will be used */
+ if (gst_buffer_get_size (section_buffer) - (payload_written +
+ payload_len) >= 4) {
+ /* enough space */
+ extra_alloc_bytes = 4;
+ }
packet_buffer = gst_buffer_copy_region (section_buffer, GST_BUFFER_COPY_ALL,
- payload_written, payload_len);
+ payload_written, payload_len + extra_alloc_bytes);
/* Prepend the header to the section data */
gst_buffer_prepend_memory (packet_buffer, mem);
+ /* add an extra 4 bytes if it could not be reserved already */
+ if (extra_alloc_bytes == 4) {
+ /* we allocated those already, resize */
+ gst_buffer_set_size (packet_buffer,
+ gst_buffer_get_size (packet_buffer) - extra_alloc_bytes);
+ } else {
+ void *ptr = g_malloc (4);
+ GstMemory *extra =
+ gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY, ptr, 4, 0, 0, ptr,
+ g_free);
+ gst_buffer_append_memory (packet_buffer, extra);
+ }
+
TS_DEBUG ("Writing %d bytes to section. %d bytes remaining",
len, section->pi.stream_avail - len);