3 # decodebin.py - Audio autopluging example using 'decodebin' element
4 # Copyright (C) 2006 Jason Gerard DeRose <jderose@jasonderose.org>
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Library General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Library General Public License for more details.
16 # You should have received a copy of the GNU Library General Public
17 # License along with this library; if not, write to the
18 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 # Boston, MA 02110-1301, USA.
24 gobject.threads_init()
32 def __init__(self, location):
34 self.pipeline = gst.Pipeline()
36 # Create bus and connect several handlers
37 self.bus = self.pipeline.get_bus()
38 self.bus.add_signal_watch()
39 self.bus.connect('message::eos', self.on_eos)
40 self.bus.connect('message::tag', self.on_tag)
41 self.bus.connect('message::error', self.on_error)
44 self.src = gst.element_factory_make('filesrc')
45 self.dec = gst.element_factory_make('decodebin')
46 self.conv = gst.element_factory_make('audioconvert')
47 self.rsmpl = gst.element_factory_make('audioresample')
48 self.sink = gst.element_factory_make('alsasink')
50 # Set 'location' property on filesrc
51 self.src.set_property('location', location)
53 # Connect handler for 'new-decoded-pad' signal
54 self.dec.connect('new-decoded-pad', self.on_new_decoded_pad)
56 # Add elements to pipeline
57 self.pipeline.add(self.src, self.dec, self.conv, self.rsmpl, self.sink)
59 # Link *some* elements
60 # This is completed in self.on_new_decoded_pad()
61 self.src.link(self.dec)
62 gst.element_link_many(self.conv, self.rsmpl, self.sink)
64 # Reference used in self.on_new_decoded_pad()
65 self.apad = self.conv.get_pad('sink')
68 self.mainloop = gobject.MainLoop()
71 self.pipeline.set_state(gst.STATE_PLAYING)
75 def on_new_decoded_pad(self, element, pad, last):
77 name = caps[0].get_name()
78 print 'on_new_decoded_pad:', name
79 if name == 'audio/x-raw-float' or name == 'audio/x-raw-int':
80 if not self.apad.is_linked(): # Only link once
84 def on_eos(self, bus, msg):
89 def on_tag(self, bus, msg):
90 taglist = msg.parse_tag()
92 for key in taglist.keys():
93 print '\t%s = %s' % (key, taglist[key])
96 def on_error(self, bus, msg):
97 error = msg.parse_error()
98 print 'on_error:', error[1]
105 if __name__ == '__main__':
106 if len(sys.argv) == 2:
107 Decodebin(sys.argv[1])
109 print 'Usage: %s /path/to/media/file' % sys.argv[0]