From 21bc5ebd7bf2fb19795f36ef42eb1dd67c11d80f Mon Sep 17 00:00:00 2001 From: Jan Schmidt Date: Tue, 15 Aug 2023 14:57:09 +1000 Subject: [PATCH] python: update examples Update various examples for brokenness, bindings and pep8 format changes Part-of: --- .../gst-python/examples/dynamic_src.py | 28 +++++++++++-------- subprojects/gst-python/examples/helloworld.py | 27 ++++++++++-------- .../gst-python/examples/record_sound.py | 26 ++++++++++++----- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/subprojects/gst-python/examples/dynamic_src.py b/subprojects/gst-python/examples/dynamic_src.py index 2b2fb5cdb9..44a4b96463 100644 --- a/subprojects/gst-python/examples/dynamic_src.py +++ b/subprojects/gst-python/examples/dynamic_src.py @@ -11,14 +11,15 @@ import random import gi gi.require_version('Gst', '1.0') gi.require_version('GLib', '2.0') -gi.require_version('GObject', '2.0') -from gi.repository import GLib, GObject, Gst +from gi.repository import GLib, Gst + class ProbeData: def __init__(self, pipe, src): self.pipe = pipe self.src = src + def bus_call(bus, message, loop): t = message.type if t == Gst.MessageType.EOS: @@ -30,9 +31,11 @@ def bus_call(bus, message, loop): loop.quit() return True + def dispose_src_cb(src): src.set_state(Gst.State.NULL) + def probe_cb(pad, info, pdata): peer = pad.get_peer() pad.unlink(peer) @@ -42,8 +45,9 @@ def probe_cb(pad, info, pdata): pdata.src = Gst.ElementFactory.make('videotestsrc') pdata.src.props.pattern = random.randint(0, 24) + pdata.src.props.is_live = True pdata.pipe.add(pdata.src) - srcpad = pdata.src.get_static_pad ("src") + srcpad = pdata.src.get_static_pad("src") srcpad.link(peer) pdata.src.sync_state_with_parent() @@ -51,13 +55,14 @@ def probe_cb(pad, info, pdata): return Gst.PadProbeReturn.REMOVE + def timeout_cb(pdata): srcpad = pdata.src.get_static_pad('src') srcpad.add_probe(Gst.PadProbeType.IDLE, probe_cb, pdata) return GLib.SOURCE_REMOVE + def main(args): - GObject.threads_init() Gst.init(None) pipe = Gst.Pipeline.new('dynamic') @@ -68,23 +73,24 @@ def main(args): pdata = ProbeData(pipe, src) - loop = GObject.MainLoop() + loop = GLib.MainLoop() GLib.timeout_add_seconds(1, timeout_cb, pdata) bus = pipe.get_bus() bus.add_signal_watch() - bus.connect ("message", bus_call, loop) - + bus.connect("message", bus_call, loop) + # start play back and listen to events pipe.set_state(Gst.State.PLAYING) try: - loop.run() - except: - pass - + loop.run() + except e: + pass + # cleanup pipe.set_state(Gst.State.NULL) + if __name__ == '__main__': sys.exit(main(sys.argv)) diff --git a/subprojects/gst-python/examples/helloworld.py b/subprojects/gst-python/examples/helloworld.py index a5a270e8cd..717b4ed8c9 100644 --- a/subprojects/gst-python/examples/helloworld.py +++ b/subprojects/gst-python/examples/helloworld.py @@ -4,7 +4,9 @@ import sys import gi gi.require_version('Gst', '1.0') -from gi.repository import GObject, Gst +gi.require_version('GLib', '2.0') +from gi.repository import GLib, Gst + def bus_call(bus, message, loop): t = message.type @@ -17,14 +19,14 @@ def bus_call(bus, message, loop): loop.quit() return True + def main(args): if len(args) != 2: sys.stderr.write("usage: %s \n" % args[0]) sys.exit(1) - GObject.threads_init() Gst.init(None) - + playbin = Gst.ElementFactory.make("playbin", None) if not playbin: sys.stderr.write("'playbin' gstreamer plugin missing\n") @@ -32,27 +34,28 @@ def main(args): # take the commandline argument and ensure that it is a uri if Gst.uri_is_valid(args[1]): - uri = args[1] + uri = args[1] else: - uri = Gst.filename_to_uri(args[1]) + uri = Gst.filename_to_uri(args[1]) playbin.set_property('uri', uri) # create and event loop and feed gstreamer bus mesages to it - loop = GObject.MainLoop() + loop = GLib.MainLoop() bus = playbin.get_bus() bus.add_signal_watch() - bus.connect ("message", bus_call, loop) - + bus.connect("message", bus_call, loop) + # start play back and listed to events playbin.set_state(Gst.State.PLAYING) try: - loop.run() - except: - pass - + loop.run() + except e: + pass + # cleanup playbin.set_state(Gst.State.NULL) + if __name__ == '__main__': sys.exit(main(sys.argv)) diff --git a/subprojects/gst-python/examples/record_sound.py b/subprojects/gst-python/examples/record_sound.py index c3277d44c8..5ef81b574a 100755 --- a/subprojects/gst-python/examples/record_sound.py +++ b/subprojects/gst-python/examples/record_sound.py @@ -12,6 +12,7 @@ gi.require_version('GLib', '2.0') gi.require_version('GObject', '2.0') from gi.repository import GLib, GObject, Gst, GstPbutils + def bus_call(bus, message, loop): t = message.type Gst.debug_bin_to_dot_file_with_ts(pipeline, Gst.DebugGraphDetails.ALL, "test") @@ -24,6 +25,7 @@ def bus_call(bus, message, loop): loop.quit() return True + def stop(loop, pipeline): _, position = pipeline.query_position(Gst.Format.TIME) print("Position: %s\r" % Gst.TIME_ARGS(position)) @@ -35,11 +37,18 @@ def stop(loop, pipeline): return True + +def _link_to_filesink(element, pad, filesink): + if not element.link(filesink): + print(f"Failed to link transcodebin to output.") + sys.exit(1) + + if __name__ == "__main__": Gst.init(sys.argv) if len(sys.argv) != 2: - print("Missing parametter") + print("Missing parameter") sys.exit(1) monitor = Gst.DeviceMonitor.new() @@ -58,7 +67,7 @@ if __name__ == "__main__": if len(default) == 1: device = default[0] else: - print("Avalaible microphones:") + print("Available microphones:") for i, d in enumerate(devices): print("%d - %s" % (i, d.get_display_name())) res = int(input("Select device: ")) @@ -66,14 +75,17 @@ if __name__ == "__main__": pipeline = Gst.ElementFactory.make("pipeline", None) source = device.create_element() + conv = Gst.ElementFactory.make("audioconvert", None) transcodebin = Gst.ElementFactory.make("transcodebin", None) Gst.util_set_object_arg(transcodebin, "profile", "video/ogg:audio/x-opus") filesink = Gst.ElementFactory.make("filesink", None) filesink.props.location = sys.argv[1] - pipeline.add(source, transcodebin, filesink) - source.link(transcodebin) - transcodebin.link(filesink) + pipeline.add(source, conv, transcodebin, filesink) + source.link(conv) + conv.link(transcodebin) + + transcodebin.connect('pad-added', _link_to_filesink, filesink) pipeline.set_state(Gst.State.PLAYING) @@ -82,8 +94,8 @@ if __name__ == "__main__": loop = GLib.MainLoop() GLib.timeout_add_seconds(1, stop, loop, pipeline) - bus.connect ("message", bus_call, loop) + bus.connect("message", bus_call, loop) loop.run() pipeline.set_state(Gst.State.NULL) - pipeline.get_state(Gst.CLOCK_TIME_NONE) \ No newline at end of file + pipeline.get_state(Gst.CLOCK_TIME_NONE)