It's faster to set pipeline to READY than to NULL
[python-gstreamer-examples] / example2.py
1 #!/usr/bin/env python
2 # This is a reworked version gstreamer example 2 from Jono Bacon's Python and
3 # Gstreamer primer:
4 # http://www.jonobacon.org/2006/08/28/getting-started-with-gstreamer-with-python
5 #
6 # It uses Gstreamer 1.0, GTK3, and replaces alsa with pulse for audio output
7 # Info on porting python scripts to GStreamer 1.0 can be found here:
8 # https://wiki.ubuntu.com/Novacut/GStreamer1.0
9
10 import gi, signal
11 gi.require_version('Gtk', '3.0')
12 gi.require_version('Gst', '1.0')
13 from gi.repository import Gtk, Gst
14
15 class Main:
16 def __init__(self):
17
18 # Create gui bits and bobs
19
20 self.wTree = Gtk.Builder()
21 self.wTree.add_from_file("example2.glade")
22
23 signals = {
24 "on_play_clicked" : self.OnPlay,
25 "on_stop_clicked" : self.OnStop,
26 "on_quit_clicked" : self.OnQuit,
27 }
28
29 self.wTree.connect_signals(signals)
30
31 # Create GStreamer bits and bobs
32
33 # Initiate the pipeline
34 Gst.init(None)
35 self.pipeline = Gst.Pipeline()
36
37 # Add an audiotestsrc element to the pipeline
38 self.audiotestsrc = Gst.ElementFactory.make("audiotestsrc", "audio")
39 self.audiotestsrc.set_property("freq", 800)
40 self.pipeline.add(self.audiotestsrc)
41
42 # Add a pulsesink element to the pipeline
43 self.pulsesink = Gst.ElementFactory.make("pulsesink", "sink")
44 self.pipeline.add(self.pulsesink)
45
46 # Link the two elements together
47 self.audiotestsrc.link(self.pulsesink)
48
49 # Summon the window and connect the window's close button to Quit
50 self.window = self.wTree.get_object("mainwindow")
51 self.window.connect("delete-event", Gtk.main_quit)
52 self.window.show_all()
53
54
55 def OnPlay(self, widget):
56 print("play")
57 self.pipeline.set_state(Gst.State.PLAYING)
58
59 def OnStop(self, widget):
60 print("stop")
61 self.pipeline.set_state(Gst.State.READY)
62
63 def OnQuit(self, widget):
64 print("quit")
65 Gtk.main_quit()
66
67 # Workaround to get Ctrl+C to terminate from command line
68 # ref: https://bugzilla.gnome.org/show_bug.cgi?id=622084#c12
69 signal.signal(signal.SIGINT, signal.SIG_DFL)
70
71 start = Main()
72 Gtk.main()