4 Simple example to demonstrate dynamically adding and removing source elements
12 gi.require_version('Gst', '1.0')
13 gi.require_version('GLib', '2.0')
14 gi.require_version('GObject', '2.0')
15 from gi.repository import GLib, GObject, Gst
18 def __init__(self, pipe, src):
22 def bus_call(bus, message, loop):
24 if t == Gst.MessageType.EOS:
25 sys.stdout.write("End-of-stream\n")
27 elif t == Gst.MessageType.ERROR:
28 err, debug = message.parse_error()
29 sys.stderr.write("Error: %s: %s\n" % (err, debug))
33 def dispose_src_cb(src):
34 src.set_state(Gst.State.NULL)
36 def probe_cb(pad, info, pdata):
39 pdata.pipe.remove(pdata.src)
40 # Can't set the state of the src to NULL from its streaming thread
41 GLib.idle_add(dispose_src_cb, pdata.src)
43 pdata.src = Gst.ElementFactory.make('videotestsrc')
44 pdata.src.props.pattern = random.randint(0, 24)
45 pdata.pipe.add(pdata.src)
46 srcpad = pdata.src.get_static_pad ("src")
48 pdata.src.sync_state_with_parent()
50 GLib.timeout_add_seconds(1, timeout_cb, pdata)
52 return Gst.PadProbeReturn.REMOVE
54 def timeout_cb(pdata):
55 srcpad = pdata.src.get_static_pad('src')
56 srcpad.add_probe(Gst.PadProbeType.IDLE, probe_cb, pdata)
57 return GLib.SOURCE_REMOVE
60 GObject.threads_init()
63 pipe = Gst.Pipeline.new('dynamic')
64 src = Gst.ElementFactory.make('videotestsrc')
65 sink = Gst.ElementFactory.make('autovideosink')
69 pdata = ProbeData(pipe, src)
71 loop = GObject.MainLoop()
73 GLib.timeout_add_seconds(1, timeout_cb, pdata)
76 bus.add_signal_watch()
77 bus.connect ("message", bus_call, loop)
79 # start play back and listen to events
80 pipe.set_state(Gst.State.PLAYING)
87 pipe.set_state(Gst.State.NULL)
89 if __name__ == '__main__':
90 sys.exit(main(sys.argv))