utils: Avoid memory merge in gst_util_dump_buffer()
authorJohan Sternerup <johast@axis.com>
Tue, 20 Aug 2019 15:18:31 +0000 (17:18 +0200)
committerJohan Sternerup <johast@axis.com>
Wed, 21 Aug 2019 06:37:50 +0000 (08:37 +0200)
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

index 4404c61..4d7fcf5 100644 (file)
 #include "glib-compat-private.h"
 #include <math.h>
 
-/**
- * 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);
@@ -87,6 +81,19 @@ gst_util_dump_mem (const guchar * mem, guint size)
 }
 
 /**
+ * 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");
   }
 }