home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / gst-0.10 / gst / extend / sources.py < prev    next >
Encoding:
Python Source  |  2009-02-21  |  5.0 KB  |  166 lines

  1. #!/usr/bin/env python
  2. # -*- Mode: Python -*-
  3. # vi:si:et:sw=4:sts=4:ts=4
  4. #
  5. # GStreamer python bindings
  6. # Copyright (C) 2005 Edward Hervey <edward at fluendo dot com>
  7.  
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. # Lesser General Public License for more details.
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  19.  
  20. import os
  21. import sys
  22.  
  23. import gobject
  24. import pygst
  25. pygst.require('0.10')
  26. import gst
  27.  
  28. from pygobject import gsignal
  29.  
  30. EOS = 'EOS'
  31. ERROR = 'ERROR'
  32. WRONG_TYPE = 'WRONG_TYPE'
  33. UNKNOWN_TYPE = 'UNKNOWN_TYPE'
  34.  
  35. class AudioSource(gst.Bin):
  36.     """A bin for audio sources with proper audio converters"""
  37.  
  38.     gsignal('done', str)
  39.     gsignal('prerolled')
  40.  
  41.     def __init__(self, filename, caps="audio/x-raw-int,channels=2,rate=44100"):
  42.         # with pygtk 2.4 this call is needed for the gsignal to work
  43.         gst.Bin.__init__(self)
  44.  
  45.         self.filename = filename
  46.         self.outcaps = caps
  47.  
  48.         self.filesrc = gst.element_factory_make("filesrc")
  49.         self.filesrc.set_property("location", self.filename)
  50.         self.dbin = gst.element_factory_make("decodebin")
  51.         self.audioconvert = gst.element_factory_make("audioconvert")
  52.         self.audioresample = gst.element_factory_make("audioresample")
  53.         self.volume = gst.element_factory_make("volume")
  54.         
  55.         self.add(self.filesrc, self.dbin,
  56.                  self.audioconvert, self.audioresample, self.volume)
  57.         self.filesrc.link(self.dbin)
  58.         self.audioconvert.link(self.audioresample)
  59.         self.audioresample.link(self.volume, caps)
  60.         
  61.         self.dbin.connect("new-decoded-pad", self._new_decoded_pad_cb)
  62.         self.dbin.connect("unknown-type", self._unknown_type_cb)
  63.  
  64.         self._srcpad = None
  65.  
  66.     def __repr__(self):
  67.         return "<AudioSource for %s>" % self.filename
  68.         
  69.     def set_volume(self, volume):
  70.         gst.debug("setting volume to %f" % volume)
  71.         self.volume.set_property("volume", volume)
  72.  
  73.     def _new_decoded_pad_cb(self, dbin, pad, is_last):
  74.         gst.debug("new decoded pad: pad %r [%s]" % (pad, pad.get_caps().to_string()))
  75.         if not "audio" in pad.get_caps().to_string() or self._srcpad:
  76.             return
  77.  
  78.         gst.debug("linking pad %r to audioconvert" % pad)
  79.         pad.link(self.audioconvert.get_pad("sink"))
  80.  
  81.         self._srcpad = gst.GhostPad("src", self.volume.get_pad("src"))
  82.         self._srcpad.set_active(True)
  83.         self.add_pad(self._srcpad)
  84.  
  85.     def _unknown_type_cb(self, pad, caps):
  86.         self.emit('done', UNKNOWN_TYPE)
  87.  
  88.     def stop(self):
  89.         self.set_state(gst.STATE_NULL)
  90.  
  91.     def clean(self):
  92.         self.stop()
  93.         self.remove(self.filesrc)
  94.         self.remove(self.dbin)
  95.         self.remove(self.audioconvert)
  96.         self.remove(self.audioresample)
  97.         self.remove(self.volume)
  98.         self.filesrc = None
  99.         self.dbin = None
  100.         self.audioconvert = None
  101.         self.volume = None
  102.         
  103. gobject.type_register(AudioSource)
  104.  
  105.        
  106. # run us to test
  107. if __name__ == "__main__":
  108.     main = gobject.MainLoop()
  109.  
  110.     def _done_cb(source, reason):
  111.         print "Done"
  112.         sys.stdout.flush()
  113.         if reason != EOS:
  114.             print "Some error happened: %s" % reason
  115.         main.quit()
  116.  
  117.     def _error_cb(source, element, gerror, message):
  118.         print "Error: %s" % gerror
  119.         main.quit()
  120.         
  121.     try:
  122.         source = AudioSource(sys.argv[1])
  123.     except IndexError:
  124.         sys.stderr.write("Please give a filename to play\n")
  125.         sys.exit(1)
  126.  
  127.     pipeline = gst.Pipeline("playing")
  128.     # connecting on the source also catches eos emit when
  129.     # no audio pad
  130.     source.connect('done', _done_cb)
  131.     pipeline.connect('error', _error_cb)
  132.  
  133.     p = "osssink"
  134.     if len(sys.argv) > 2:
  135.         p = " ".join(sys.argv[2:])
  136.     
  137.     pipeline.add(source)
  138.     sink = gst.parse_launch(p)
  139.     pipeline.add(sink)
  140.     source.link(sink)
  141.  
  142.     # we schedule this as a timeout so that we are definately in the main
  143.     # loop when it goes to PLAYING, and errors make main.quit() work correctly
  144.     def _start(pipeline):
  145.         print "setting pipeline to PLAYING"
  146.         pipeline.set_state(gst.STATE_PLAYING)
  147.         print "set pipeline to PLAYING"
  148.  
  149.     gobject.timeout_add(0, _start, pipeline)
  150.     gobject.idle_add(pipeline.iterate)
  151.  
  152.     print "Going into main loop"
  153.     main.run()
  154.     print "Left main loop"
  155.  
  156.     pipeline.set_state(gst.STATE_NULL)
  157.     pipeline.remove(source)
  158.     pipeline.remove(sink)
  159.     utils.gc_collect('cleaned out pipeline')
  160.     source.clean()
  161.     utils.gc_collect('cleaned up source')
  162.     source = None
  163.     utils.gc_collect('set source to None')
  164.