- /* Wrap the TS header and adaption field in a GstMemory */
- mem = gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
- packet, TSMUX_PACKET_LENGTH, 0, offset, packet, g_free);
-
- 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;
- } else {
- extra_alloc_bytes = 0;
- }
- packet_buffer = gst_buffer_copy_region (section_buffer, GST_BUFFER_COPY_ALL,
- 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);
- }