Package flumotion :: Package component :: Package effects :: Package audiorate :: Module audiorate
[hide private]

Source Code for Module flumotion.component.effects.audiorate.audiorate

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  import sys 
 23   
 24  import gobject 
 25  import gst 
 26   
 27  from flumotion.common.i18n import gettexter 
 28  from flumotion.component import feedcomponent 
 29  from flumotion.common import gstreamer 
 30   
 31  __version__ = "$Rev$" 
 32  T_ = gettexter() 
 33   
 34  DEFAULT_TOLERANCE = 20000000 # 20ms 
 35   
 36   
37 -class AudiorateBin(gst.Bin):
38 """ 39 I am a GStreamer bin that can change the samplerate of an audio stream. 40 """ 41 logCategory = "audiorate" 42 RATE_CAPS = ', rate=%d' 43 CAPS_TEMPLATE = ("audio/x-raw-int %(extra_caps)s ;" 44 "audio/x-raw-float %(extra_caps)s") 45 46 __gproperties__ = { 47 'samplerate': (gobject.TYPE_UINT, 'samplerate', 48 'Audio samplerate', 1, 200000, 44100, 49 gobject.PARAM_READWRITE), 50 'tolerance': (gobject.TYPE_UINT, 'tolerance', 51 'Correct imperfect timestamps when it exeeds the ' 52 'tolerance', 0, sys.maxint, DEFAULT_TOLERANCE, 53 gobject.PARAM_READWRITE)} 54
55 - def __init__(self, samplerate=None, tolerance=DEFAULT_TOLERANCE):
56 gst.Bin.__init__(self) 57 self._samplerate = samplerate 58 self._samplerate_caps = '' 59 60 self._audiorate = gst.element_factory_make("audiorate") 61 self._audioconv = gst.element_factory_make("audioconvert") 62 self._audioresample = gst.element_factory_make("legacyresample") 63 self._capsfilter = gst.element_factory_make("capsfilter") 64 self._identity = gst.parse_launch("identity silent=true") 65 self.add(self._audiorate) 66 self.add(self._audioconv) 67 self.add(self._audioresample) 68 self.add(self._capsfilter) 69 self.add(self._identity) 70 71 self._audiorate.link(self._audioconv) 72 self._audioconv.link(self._audioresample) 73 self._audioresample.link(self._capsfilter) 74 self._capsfilter.link(self._identity) 75 76 # Create source and sink pads 77 self._sinkPad = gst.GhostPad('sink', self._audiorate.get_pad('sink')) 78 self._srcPad = gst.GhostPad('src', self._identity.get_pad('src')) 79 self.add_pad(self._sinkPad) 80 self.add_pad(self._srcPad) 81 82 self._setSamplerate(samplerate) 83 self._setTolerance(tolerance)
84
85 - def _getCapsString(self):
86 # Use this for when we will have more stuff to add to the caps 87 extra_caps = ' '.join([self._samplerate_caps, ]) 88 return self.CAPS_TEMPLATE % dict(extra_caps=extra_caps)
89
90 - def _setSamplerate(self, samplerate):
91 self._samplerate = samplerate 92 self._samplerate_caps = '' 93 if self._samplerate is not None: 94 self._samplerate_caps = self.RATE_CAPS % samplerate 95 self._capsfilter.set_property('caps', gst.Caps(self._getCapsString()))
96
97 - def _setTolerance(self, tolerance):
98 self._tolerance = tolerance 99 if gstreamer.element_has_property(self._audiorate, 'tolerance'): 100 self._audiorate.set_property('tolerance', self._tolerance) 101 else: 102 self.warning("The 'tolerance' property could not be set in the " 103 "audiorate element.")
104
105 - def do_set_property(self, property, value):
106 if property.name == 'samplerate': 107 self._setSamplerate(value) 108 if property.name == 'tolerance': 109 self._setTolerance(value) 110 else: 111 raise AttributeError('unknown property %s' % property.name)
112
113 - def do_get_property(self, property):
114 if property.name == 'samplerate': 115 return self._samplerate 116 if property.name == 'tolerance': 117 return self._tolerance 118 else: 119 raise AttributeError('unknown property %s' % property.name)
120 121
122 -class Audiorate(feedcomponent.PostProcEffect):
123 """ 124 I am an effect that can be added to any component that changes the 125 samplerate of the audio output. 126 """ 127 logCategory = "audiorate-effect" 128
129 - def __init__(self, name, sourcePad, pipeline, samplerate=None, 130 tolerance=DEFAULT_TOLERANCE):
131 """ 132 @param element: the video source element on which the post 133 processing effect will be added 134 @param sourcePad: source pad used for linking the effect 135 @param pipeline: the pipeline of the element 136 @param samplerate: output samplerate 137 @param tolerance: tolerance to correct imperfect timestamps 138 """ 139 feedcomponent.PostProcEffect.__init__(self, name, sourcePad, 140 AudiorateBin(samplerate, tolerance), pipeline)
141
142 - def effect_setTolerance(self, tolerance):
143 self.effectBin.set_property("tolerance", tolerance) 144 return tolerance
145
146 - def effect_getTolerance(self):
147 return self.effectBin.get_property('tolerance')
148
149 - def effect_setSamplerate(self, samplerate):
150 self.effectBin.set_property("samplerate", samplerate) 151 return samplerate
152
153 - def effect_getSamplerate(self):
154 return self.effectBin.get_property('samplerate')
155