22 Boilerplate code, shorthand, and utility functions for creating GStreamer 23 elements and pipelines. 26 __author__ =
"Leo Singer <leo.singer@ligo.org>" 27 __organization__ = [
"LIGO",
"California Institute of Technology"]
28 __copyright__ =
"Copyright 2010, Leo Singer" 29 __all__ = [
"gstlal_element_register",
"mkelem",
"mkelems_in_bin",
"splice"]
34 gi.require_version(
'Gst',
'1.0')
35 from gi.repository
import GObject
36 from gi.repository
import Gst
37 GObject.threads_init()
41 def gstlal_element_register(clazz):
42 """Class decorator for registering a Python element. Note that decorator 43 syntax was extended from functions to classes in Python 2.6, so until 2.6 44 becomes the norm we have to invoke this as a function instead of by 47 @gstlal_element_register 48 class foo(Gst.Element): 51 Until then, you have to do:: 53 class foo(Gst.Element): 55 gstlal_element_register(foo) 57 from inspect
import getmodule
58 GObject.type_register(clazz)
59 getmodule(clazz).__gstelementfactory__ = (clazz.__name__, Gst.RANK_NONE, clazz)
63 def mkelem(elemname, props={}):
64 """Instantiate an element named elemname and optionally set some of its 65 properties from the dictionary props.""" 66 elem = Gst.ElementFactory.make(elemname,
None)
67 for (k, v)
in props.iteritems():
68 elem.set_property(k, v)
72 def mkelems_in_bin(bin, *pipedesc):
73 """Create an array of elements from a list of tuples, add them to a bin, 74 link them sequentially, and return the list. Example: 76 mkelem(bin, ('audiotestsrc', {'wave':9}), ('audioresample',)) 80 audiotestsrc wave=9 ! audioresample 82 elems = [mkelem(*elemdesc)
for elemdesc
in pipedesc]
86 Gst.element_link_many(*elems)
91 def splice(bin, pad, element):
92 """Splice element into an existing bin by teeing off an existing pad. 94 If necessary, a tee is added to the pipeline in order to splice the new element. 96 bin is an instance of Gst.Bin or Gst.Pipeline. pad is a string that 97 describes any pad inside that bin. The syntax used in gst-launch is 98 understood. For example, the string 'foo.bar.bat' means the pad called 'bat' 99 on the element called 'bar' in the bin called 'foo' inside bin. 'foo.bar.' 100 refers to any pad on the element 'bar'. element_or_pad is either an element 103 FIXME: implicit pad names not yet understood. 106 padpath = pad.split(
'.')
107 padname = padpath.pop()
111 elem = elem.get_by_name(name)
113 raise NameError(
"no such element: '%s'" % name)
115 pad = elem.get_static_pad(padname)
117 raise NameError(
"no such pad: '%s'" % padname)
119 tee_type = Gst.element_factory_find(
'tee').get_element_type()
121 tee = pad.get_parent_element()
122 if tee.__gtype__ != tee_type:
123 peer_pad = pad.get_peer()
125 if hasattr(element,
'get_direction'):
126 elem.get_static_pad(
'src').link(element)
131 peer_element = peer_pad.get_parent_element()
132 if peer_element.__gtype__ == tee_type:
135 if pad.get_direction() == Gst.PadDirection.SINK:
136 pad, peer_pad = peer_pad, pad
138 tee = Gst.ElementFactory.make(
"tee",
None)
140 pad.link(tee.get_static_pad(
'sink'))
141 tee.get_request_pad(
'src%d').link(peer_pad)
142 if hasattr(element,
'get_direction'):
143 tee.get_request_pad(
'src%d').link(element)