python: Add Gst.Float wrapper

When the float python type is used inside a GstValueArray, it is
converted to a G_TYPE_DOUBLE GValue. Sometimes it is important to be
able to force G_TYPE_FLOAT GValue, for instance to set the the
"mix-matrix" property on audioconvert.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/8957>
This commit is contained in:
Xavier Claessens 2025-05-09 11:14:03 -04:00 committed by GStreamer Marge Bot
parent cff1d1962b
commit 0172e47a79
2 changed files with 34 additions and 0 deletions

View File

@ -48,6 +48,17 @@ python module to use with Gst 0.10"
warnings.warn(warn_msg, RuntimeWarning) warnings.warn(warn_msg, RuntimeWarning)
class Float(float):
'''
A wrapper to force conversion to G_TYPE_FLOAT instead of G_TYPE_DOUBLE when
used in e.g. Gst.ValueArray.
'''
__gtype__ = GObject.TYPE_FLOAT
__all__.append('Float')
# Ensuring that PyGObject loads the URIHandler interface # Ensuring that PyGObject loads the URIHandler interface
# so we can force our own implementation soon enough (in gstmodule.c) # so we can force our own implementation soon enough (in gstmodule.c)
class URIHandler(Gst.URIHandler): class URIHandler(Gst.URIHandler):

View File

@ -436,3 +436,26 @@ class TestBitmask(TestCase):
self.assertEqual(str(r), '0x20') self.assertEqual(str(r), '0x20')
else: else:
self.assertEqual(str(r), '0x20L') self.assertEqual(str(r), '0x20L')
class TestFloat(TestCase):
def testSetProperty(self):
''' Test we can set the mix-matrix property on audioconvert
It requires the Gst.Float class to force the conversion of python float
to G_TYPE_FLOAT instead of G_TYPE_DOUBLE.
'''
Gst.init(None)
audioconvert = Gst.ElementFactory.make("audioconvert")
if not audioconvert:
self.skipTest("audioconvert not available")
row = Gst.ValueArray([Gst.Float(0.0)])
matrix = Gst.ValueArray([row])
audioconvert.set_property("mix-matrix", matrix)
value = audioconvert.get_property("mix-matrix")
self.assertEqual(len(value), 1)
self.assertEqual(len(value[0]), 1)
self.assertEqual(value[0][0], 0.0)