gstlal 1.13.0
Loading...
Searching...
No Matches
pipetools.py
1"""Utilities for constructing pipeline elements
2
3"""
4import functools
5import inspect
6from typing import Union, Optional
7
8import gi
9import ligo.segments
10from lal import LIGOTimeGPS
11
12gi.require_version('Gst', '1.0')
13from gi.repository import GObject
14from gi.repository import Gst
15
16from gstlal import gstpipetools
17
18GObject.threads_init()
19Gst.init(None)
20
21PROPERTY_BUILTIN_NAMES = ('async')
22
23# The below are used for type annotations
24Pipeline = Gst.Pipeline
25Element = Gst.Element
26Caps = Gst.Caps
27GValueArray = Gst.ValueArray # Verify this type
28ValueArray = GObject.ValueArray
29Segment = ligo.segments.segment
30TimeGPS = LIGOTimeGPS
31
32
33def clean_property_name(name: str):
34 """Utility function for cleaning a property name, handles two cases:
35
36 1. Underscores to hyphens: property_name -> property-name
37 2. Removes trailing underscores originally used to avoid collisions with builtin: math_ -> math.
38 The specific list of builtin collisions detected is defined in PROPERTY_BUILTIN_NAMES
39
40 Args:
41 name:
42 str, the python property name
43
44 Returns:
45 str, the formatted gstreamer compatible element property name
46 """
47 if name.endswith('_') and name[:-1] in PROPERTY_BUILTIN_NAMES:
48 return name[:-1]
49 return name.replace('_', '-')
50
51
52def make_element_with_src(pipeline: Gst.Pipeline, src: Union[Gst.Element, Gst.Pad], elem_type_name: str, **properties):
53 """Create a new element of the type defined by the given element factory. If name is None, then the
54 element will receive a guaranteed unique name, consisting of the element factory name and a number.
55 If name is given, it will be given the name supplied.
56
57 Args:
58 pipeline:
59 Pipeline, the existing pipeline to which the new element will be added
60 src:
61 Element or Pad, the element or pad to use as a source for the new element
62 elem_type_name:
63 str or None, name of new element, or None to automatically create a unique name
64 **properties:
65 dict, keyword arguments to be set as properties on the new element
66
67 References:
68 [1] https://lazka.github.io/pgi-docs/Gst-1.0/classes/ElementFactory.html#Gst.ElementFactory.make
69
70 Raises:
71 RuntimeError:
72 If any problem occurs creating the element.
73
74 Returns:
75 Element, the new element
76 """
77 # Determine element name
78 name = properties.pop('name') if 'name' in properties else None
79
80 properties = {clean_property_name(key): value for key, value in properties.items()}
81
82 # Make the element
83 elem = gstpipetools.make_element(elem_type_name, name, **properties)
84
85 # Add element to pipeline
86 pipeline.add(elem)
87
88 # Link src argument as input to element
89 if gstpipetools.is_pad(src):
90 src.get_parent_element().link_pads(src, elem, None)
91 elif src is not None:
92 src.link(elem)
93
94 return elem
make_element_with_src(Gst.Pipeline pipeline, Union[Gst.Element, Gst.Pad] src, str elem_type_name, **properties)
Definition pipetools.py:52