18 Boilerplate code, shorthand, and utility functions for creating GStreamer
19 elements and pipelines.
22 __author__ =
"Leo Singer <leo.singer@ligo.org>"
23 __organization__ = [
"LIGO",
"California Institute of Technology"]
24 __copyright__ =
"Copyright 2010, Leo Singer"
25 __all__ = [
"gobject",
"gst",
"gstlal_element_register",
"mkelem",
"mkelems_in_bin",
"splice"]
32 gobject.threads_init()
38 def gstlal_element_register(clazz):
39 """Class decorator for registering a Python element. Note that decorator
40 syntax was extended from functions to classes in Python 2.6, so until 2.6
41 becomes the norm we have to invoke this as a function instead of by
44 @gstlal_element_register
45 class foo(gst.Element):
48 Until then, you have to do::
50 class foo(gst.Element):
52 gstlal_element_register(foo)
54 from inspect
import getmodule
55 gobject.type_register(clazz)
56 getmodule(clazz).__gstelementfactory__ = (clazz.__name__, gst.RANK_NONE, clazz)
60 def mkelem(elemname, props={}):
61 """Instantiate an element named elemname and optionally set some of its
62 properties from the dictionary props."""
63 elem = gst.element_factory_make(elemname)
64 for (k, v)
in props.iteritems():
65 elem.set_property(k, v)
69 def mkelems_in_bin(bin, *pipedesc):
70 """Create an array of elements from a list of tuples, add them to a bin,
71 link them sequentially, and return the list. Example:
73 mkelem(bin, ('audiotestsrc', {'wave':9}), ('audioresample',))
77 audiotestsrc wave=9 ! audioresample
79 elems = [mkelem(*elemdesc)
for elemdesc
in pipedesc]
83 gst.element_link_many(*elems)
88 def splice(bin, pad, element):
89 """Splice element into an existing bin by teeing off an existing pad.
91 If necessary, a tee is added to the pipeline in order to splice the new element.
93 bin is an instance of gst.Bin or gst.Pipeline. pad is a string that
94 describes any pad inside that bin. The syntax used in gst-launch is
95 understood. For example, the string 'foo.bar.bat' means the pad called 'bat'
96 on the element called 'bar' in the bin called 'foo' inside bin. 'foo.bar.'
97 refers to any pad on the element 'bar'. element_or_pad is either an element
100 FIXME: implicit pad names not yet understood.
103 padpath = pad.split(
'.')
104 padname = padpath.pop()
108 elem = elem.get_by_name(name)
110 raise NameError(
"no such element: '%s'" % name)
112 pad = elem.get_pad(padname)
114 raise NameError(
"no such pad: '%s'" % padname)
116 tee_type = gst.element_factory_find(
'tee').get_element_type()
118 tee = pad.get_parent_element()
119 if tee.__gtype__ != tee_type:
120 peer_pad = pad.get_peer()
122 if hasattr(element,
'get_direction'):
123 elem.get_pad(
'src').link(element)
128 peer_element = peer_pad.get_parent_element()
129 if peer_element.__gtype__ == tee_type:
132 if pad.get_direction() == gst.PAD_SINK:
133 pad, peer_pad = peer_pad, pad
135 tee = gst.element_factory_make(
"tee")
137 pad.link(tee.get_static_pad(
'sink'))
138 tee.get_request_pad(
'src%d').link(peer_pad)
139 if hasattr(element,
'get_direction'):
140 tee.get_request_pad(
'src%d').link(element)