3

In the old (pre-GObject-introspection) GStreamer bindings, it was possible to access gst.Buffer data via the .data attribute or by casting to str. This is no longer possible:

>>> p buf.data
*** AttributeError: 'Buffer' object has no attribute 'data'
>>> str(buf)
'<GstBuffer at 0x7fca2c7c2950>'
daf
  • 5,085
  • 4
  • 31
  • 34

1 Answers1

6

To access the contents of a Gst.Buffer in recent versions, you must first map() the buffer to get a Gst.MapInfo, which has a data attribute of type bytes (str in Python 2).

(result, mapinfo) = buf.map(Gst.MapFlags.READ)
assert result

try:
    # use mapinfo.data here
    pass
finally:
    buf.unmap(mapinfo)

You can also access the buffer's constituent Gst.Memory elements with get_memory(), and map them individually. (AFAICT, calling Buffer.map() is equivalent to calling .get_all_memory() and mapping the resulting Memory.)

Unfortunately, writing to these buffers is not possible since Python represents them with immutable types even when the Gst.MapFlags.WRITE flag is set. Instead, you'd have to do something like create a new Gst.Memory with the modified data, and use Gst.Buffer.replace_all_memory().

daf
  • 5,085
  • 4
  • 31
  • 34
  • If you are targeting CentOS 7, there is no such method in the inadequate gi bindings that ship with it, but there is a workaround as documented here: https://bugzilla.gnome.org/show_bug.cgi?id=678663#c73 – totaam Dec 28 '15 at 11:49