From a4d5c95638b0783124e983d5ba063bb38c6f53a3 Mon Sep 17 00:00:00 2001 From: Johan Sternerup Date: Tue, 20 Aug 2019 17:18:31 +0200 Subject: [PATCH] utils: Avoid memory merge in gst_util_dump_buffer() For buffers with multiple memory chunks, gst_buffer_map() has the side effect of merging the memory chunks into one contiguous chunk. Since gst_util_dump_mem() used gst_buffer_map() the internals of the buffer could actually change as a result of printing it. For the case of a buffer containing several memory chunks, gst_memory_map() is now used to obtain the memory address and each memory chunk is dumped separately preceded by a header line. The behaviour for a buffer containing a single memory chunk is left unchanged. --- gst/gstutils.c | 58 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/gst/gstutils.c b/gst/gstutils.c index 4404c61348..4d7fcf50e9 100644 --- a/gst/gstutils.c +++ b/gst/gstutils.c @@ -48,15 +48,9 @@ #include "glib-compat-private.h" #include -/** - * gst_util_dump_mem: - * @mem: (array length=size): a pointer to the memory to dump - * @size: the size of the memory block to dump - * - * Dumps the memory block into a hex representation. Useful for debugging. - */ -void -gst_util_dump_mem (const guchar * mem, guint size) + +static void +gst_util_dump_mem_offset (const guchar * mem, guint size, guint offset) { guint i, j; GString *string = g_string_sized_new (50); @@ -75,7 +69,7 @@ gst_util_dump_mem (const guchar * mem, guint size) i++; if (j == 16 || i == size) { - g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j, mem + i - j, + g_print ("%08x (%p): %-48.48s %-16.16s\n", i - j + offset, mem + i - j, string->str, chars->str); g_string_set_size (string, 0); g_string_set_size (chars, 0); @@ -86,6 +80,19 @@ gst_util_dump_mem (const guchar * mem, guint size) g_string_free (chars, TRUE); } +/** + * gst_util_dump_mem: + * @mem: (array length=size): a pointer to the memory to dump + * @size: the size of the memory block to dump + * + * Dumps the memory block into a hex representation. Useful for debugging. + */ +void +gst_util_dump_mem (const guchar * mem, guint size) +{ + gst_util_dump_mem_offset (mem, size, 0); +} + /** * gst_util_dump_buffer: * @buf: a #GstBuffer whose memory to dump @@ -98,10 +105,35 @@ void gst_util_dump_buffer (GstBuffer * buf) { GstMapInfo map; + GstMemory *mem; + guint n_memory; + guint i; + guint offset; - if (gst_buffer_map (buf, &map, GST_MAP_READ)) { - gst_util_dump_mem (map.data, map.size); - gst_buffer_unmap (buf, &map); + n_memory = gst_buffer_n_memory (buf); + + if (n_memory == 1) { + if (gst_buffer_map (buf, &map, GST_MAP_READ)) { + gst_util_dump_mem (map.data, map.size); + gst_buffer_unmap (buf, &map); + } + } else if (n_memory > 1) { + /* gst_buffer_map() will merge multiple memory segments into one contiguous + * area so we need to use gst_memory_map() in order not to affect the + * contents of buf */ + offset = 0; + for (i = 0; i < n_memory; ++i) { + g_print ("[Memory #%u]\n", i); + mem = gst_buffer_get_memory (buf, i); + if (gst_memory_map (mem, &map, GST_MAP_READ)) { + gst_util_dump_mem_offset (map.data, map.size, offset); + offset += map.size; + gst_memory_unmap (mem, &map); + } + gst_memory_unref (mem); + } + } else { + g_print ("[Empty]\n"); } }