GStreamer PyGObject Flag and Enum usage

Viewed 261

I am using PyGObject to run Gstreamer pipelines from python. I need to set properties of some of my GStreamerElements, for example, the profiles property of the rtspclientsink element.

I am using the <element>.set_property(name, val) PyGObject equivalent of the GObject set_property function.

However, in this particular case, val is a flag, GstRTSPProfile. I was wondering how I might import and instantiate this flag as a python type such that I can set this property without having to just use an integer.

This is how I am importing Gstreamer:

import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstBase', '1.0')

from gi.repository import GObject, Gst, GstBase

Importing GstRTSPProfile from gi.repository does not work. I'm not able to access GstRTSPProfile from Gst or GstBase either.

2 Answers

You can get the gobject.GEnum by asking the element that uses it (thanks to https://stackoverflow.com/a/62139101/104446). Here's an example using the queue element and leaky property.

enum_class = Gst.ElementFactory.make("queue").find_property("leaky").enum_class

I couldn't figure out how to use this directly in a useful way - the class needs to be instantiated with the integer value. e.g.

>>> enum_class(1)
<enum Leaky on upstream (new buffers) of type __main__.GstQueueLeaky>

But you can create a Python enum from it:

>>> GstQueueLeaky = IntEnum(
...     enum_class.__name__,
...     {v.value_nick.upper(): k for k, v in enum_class.__enum_values__.items()}
... )

>>> GstQueueLeaky.DOWNSTREAM
<GstQueueLeaky.DOWNSTREAM: 2>

>>> GstQueueLeaky.__members__
mappingproxy({
    'NO': <GstQueueLeaky.NO: 0>, 
    'UPSTREAM': <GstQueueLeaky.UPSTREAM: 1>, 
    'DOWNSTREAM': <GstQueueLeaky.DOWNSTREAM: 2>
})
Related