forked from rubenrua/GstreamerCodeSnippets
/
basic-tutorial-6.py
162 lines (132 loc) · 5.43 KB
/
basic-tutorial-6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
"""
Basic tutorial 6: Media formats and Pad Capabilities
https://gstreamer.freedesktop.org/documentation/tutorials/basic/media-formats-and-pad-capabilities.html
"""
import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib
# Functions below print the Capabilities in a human-friendly format
def print_field(field, value, pfx):
str = Gst.value_serialize(value)
print("{0:s} {1:15s}: {2:s}".format(
pfx, GLib.quark_to_string(field), str))
return True
def print_caps(caps, pfx):
if caps is None:
return
if caps.is_any():
print("%sANY" % pfx)
return
if caps.is_empty():
print("%sEMPTY" % pfx)
return
for i in range(caps.get_size()):
structure = caps.get_structure(i)
print("%s%s" % (pfx, structure.get_name()))
structure.foreach(print_field, pfx)
# Prints information about a Pad Template, including its Capabilities
def print_pad_templates_information(factory):
print("Pad Templates for %s:" % (factory.get_name()))
if not factory.get_num_pad_templates():
print(" none")
return
padstemplates = factory.get_static_pad_templates()
for padtemplate in padstemplates:
if padtemplate.direction == Gst.PadDirection.SRC:
print(" SRC template: '%s'" % padtemplate.name_template)
elif padtemplate.direction == Gst.PadDirection.SINK:
print(" SINK template: '%s'"% padtemplate.name_template)
else:
print(" UNKNOWN!!! template: '%s'"% padtemplate.name_template)
if padtemplate.presence == Gst.PadPresence.ALWAYS:
print(" Availability: Always")
elif padtemplate.presence == Gst.PadPresence.SOMETIMES:
print(" Availability: Sometimes")
elif padtemplate.presence == Gst.PadPresence.REQUEST:
print(" Availability: On request")
else:
print(" Availability: UNKNOWN!!!")
if padtemplate.static_caps.string:
print(" Capabilities:")
print_caps(padtemplate.get_caps(), " ")
print("")
# Shows the CURRENT capabilities of the requested pad in the given element
def print_pad_capabilities(element, pad_name):
# Retrieve pad
pad = element.get_static_pad(pad_name)
if not pad:
print("Could not retrieve pad '%s'" % pad_name, file=sys.stderr)
return
# Retrieve negotiated caps (or acceptable caps if negotiation is not finished yet)
caps = pad.get_current_caps()
if not caps:
caps = pad.get_allowed_caps()
# Print
print("Caps for the %s pad:" % pad_name)
print_caps(caps, " ")
def main():
Gst.init(None)
# Create the element factories
source_factory = Gst.ElementFactory.find("audiotestsrc")
sink_factory = Gst.ElementFactory.find("autoaudiosink")
if not source_factory or not sink_factory:
print("Not all element factories could be created.", file=sys.stderr)
exit(-1)
# Print information about the pad templates of these factories
print_pad_templates_information(source_factory)
print_pad_templates_information(sink_factory)
# Ask the factories to instantiate actual elements
source = source_factory.create("source")
sink = sink_factory.create("sink")
# Create the empty pipeline
pipeline = Gst.Pipeline.new("test-pipeline")
if not pipeline or not source or not sink:
print("Not all elements could be created.", file=sys.stderr)
exit(-1)
# Build the pipeline
pipeline.add(source, sink)
if not Gst.Element.link(source, sink):
print("Elements could not be linked.", file=sys.stderr)
exit(-1)
# Print initial negotiated caps (in NULL state)
print("In NULL state:")
print_pad_capabilities(sink, "sink")
# Start playing
ret = pipeline.set_state(Gst.State.PLAYING)
if ret == Gst.StateChangeReturn.FAILURE:
print("Unable to set the pipeline to the playing state "
"(check the bus for error messages).", file=sys.stderr)
# Wait until error, EOS or State Change
bus = pipeline.get_bus()
while True:
message = bus.timed_pop_filtered(
Gst.CLOCK_TIME_NONE,
Gst.MessageType.STATE_CHANGED |
Gst.MessageType.ERROR |
Gst.MessageType.EOS
)
if message.type == Gst.MessageType.ERROR:
err, debug_info = message.parse_error()
print("Error received from element %s: %s" % (
message.src.get_name(), err), file=sys.stderr)
print("Debugging information: %s" % debug_info, file=sys.stderr)
break
elif message.type == Gst.MessageType.EOS:
print("End-Of-Stream reached.")
break
elif message.type == Gst.MessageType.STATE_CHANGED:
if message.src == pipeline:
old_state, new_state, pending_state = message.parse_state_changed()
print("Pipeline state changed from %s to %s." %
(old_state.value_nick, new_state.value_nick))
# Print the current capabilities of the sink element
print_pad_capabilities (sink, "sink")
else:
# We should not reach here because we only asked for ERRORs, EOS and STATE_CHANGED
print("Unexpected message received.", file=sys.stderr)
# Free resources
pipeline.set_state(gst.STATE_NULL)
if __name__ == '__main__':
main()