gdp-if / gstreamer-log-plugin / gst-launch-w.py @ master
History | View | Annotate | Download (3.5 KB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
|
3 |
import gi |
4 |
gi.require_version('Gst', '1.0') |
5 |
gi.require_version('GstBase', '1.0') |
6 |
|
7 |
from gi.repository import GObject, Gst, GstBase |
8 |
|
9 |
import os |
10 |
import gdp |
11 |
import sys |
12 |
import cPickle |
13 |
|
14 |
import gdpsrc, gdpsink |
15 |
|
16 |
if __name__ == '__main__': |
17 |
|
18 |
sys.stderr.write("THIS IS ONLY A PROOF-OF-CONCEPT, USE AT YOUR OWN RISK!\n")
|
19 |
|
20 |
GObject.threads_init() |
21 |
Gst.init(None)
|
22 |
|
23 |
# register the elements again, just in case
|
24 |
Gst.Element.register(None, "gdpsrc", Gst.Rank.NONE, gdpsrc.GDPSrc) |
25 |
Gst.Element.register(None, "gdpsink", Gst.Rank.NONE, gdpsink.GDPSink) |
26 |
|
27 |
if len(sys.argv)<2: |
28 |
sys.stderr.write("Usage: %s <a valid pipeline>\n" % sys.argv[0]) |
29 |
sys.stderr.write("Example: audiotestsrc ! gdpsink logname=xyz\n")
|
30 |
sys.stderr.write("Example: gdpsrc logname=xyz ! autoaudiosink\n")
|
31 |
sys.stderr.write("Valid properties are 'logname' and 'name'.\n")
|
32 |
sys.exit(-1)
|
33 |
|
34 |
pipeline_desc = " ".join(sys.argv[1:]) |
35 |
|
36 |
# Not sure if escaping is allowed. If so, this doesn't work well.
|
37 |
orig_elems = pipeline_desc.split("!")
|
38 |
assert len(orig_elems)>=2 |
39 |
|
40 |
# Because of a weird bug somewhere (most likely in Python Gstreamer),
|
41 |
# setting properties for python plugins (other than 'name') does not
|
42 |
# work from the pipeline description passed to Gst.parse_launch. It
|
43 |
# does, however, work if we get a handle to the element and call
|
44 |
# 'set_property' on it explicitly.
|
45 |
# To get around this bug, we must parse the pipeline description, find
|
46 |
# our particular elements, generate a pipeline without the property
|
47 |
# assignments, create the pipeline by calling Gst.parse_launch, then
|
48 |
# get all our elements, and individually set properties on them.
|
49 |
|
50 |
# Original properties specified in the pipeline description
|
51 |
# A list of dicts, each item in the list represents our elements
|
52 |
gdp_elem_list = [] |
53 |
|
54 |
# A list of strings representing elements in the modified pipeline desc
|
55 |
new_elems = [] |
56 |
|
57 |
for elem in orig_elems: |
58 |
# elem is simply a string representing an element and its property
|
59 |
# assignment
|
60 |
elem = elem.strip() |
61 |
_elem = elem.split(" ")[0] |
62 |
|
63 |
if _elem == "gdpsrc" or _elem == "gdpsink": |
64 |
# parse the properties.
|
65 |
props = {} |
66 |
__prop_strs = elem.split(" ")[1:] |
67 |
for __prop_str in __prop_strs: |
68 |
__prop_str = __prop_str.strip() |
69 |
_tmp = __prop_str.split("=")
|
70 |
props[_tmp[0]] = _tmp[1] |
71 |
|
72 |
# ensure that we have the 'name' set
|
73 |
props["name"] = props.get("name", |
74 |
"%s%d" % (_elem, len(gdp_elem_list))) |
75 |
|
76 |
# parsed properties, now recreate this element
|
77 |
elem = "%s name=%s" % (_elem, props["name"]) |
78 |
gdp_elem_list.append(props) |
79 |
|
80 |
new_elems.append(elem) |
81 |
|
82 |
new_pipeline_desc = " ! ".join(new_elems)
|
83 |
|
84 |
sys.stderr.write("Modified original pipeline\n")
|
85 |
sys.stderr.write("launching pipeline %s\n" % new_pipeline_desc)
|
86 |
pipeline = Gst.parse_launch(new_pipeline_desc) |
87 |
|
88 |
# Now is the time to set properties to each of the GDP elements
|
89 |
for props in gdp_elem_list: |
90 |
elem_name = props["name"]
|
91 |
elem = pipeline.get_by_name(elem_name) |
92 |
|
93 |
for key in props: |
94 |
if key == "name": |
95 |
continue
|
96 |
elem.set_property(key, props[key]) |
97 |
|
98 |
# All set, ready to go now
|
99 |
pipeline.set_state(Gst.State.PLAYING) |
100 |
|
101 |
loop = GObject.MainLoop() |
102 |
try:
|
103 |
loop.run() |
104 |
except:
|
105 |
pass
|
106 |
|
107 |
pipeline.set_state(Gst.State.NULL) |