-
Notifications
You must be signed in to change notification settings - Fork 0
/
gst-boilerplate.py
84 lines (59 loc) · 1.97 KB
/
gst-boilerplate.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
#! /usr/bin/python3
from ctypes import *
import sys
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtp', '1.0')
gi.require_version('GObject', '2.0')
from gi.repository import Gst, GObject, GLib, GstRtp
def on_bus_message(bus: Gst.Bus, message: Gst.Message, loop: GLib.MainLoop):
mtype = message.type
if mtype == Gst.MessageType.EOS:
print("End of stream")
loop.quit()
elif mtype == Gst.MessageType.ERROR:
err, debug = message.parse_error()
print(err, debug)
loop.quit()
elif mtype == Gst.MessageType.WARNING:
err, debug = message.parse_warning()
print(err, debug)
return True
def probe_on_data(pad, info, u_data):
gst_buffer = info.get_buffer()
assert(gst_buffer), "Unable to get GstBuffer"
# Do the shit
return Gst.PadProbeReturn.OK
if __name__ == "__main__":
Gst.init(sys.argv[1:])
loop = GLib.MainLoop()
pipe_desc = f"""
videotestsrc
! videoconvert ! video/x-raw,width=480,height=640
! xvimagesink
videotestsrc
! videoconvert ! video/x-raw,width=640,height=480
! identity name=probe_me
! xvimagesink
"""
# Parsing and setting stuff up
pipeline = Gst.parse_launch(pipe_desc)
# Listening to the bus for events and errors
bus = pipeline.get_bus()
bus.add_signal_watch()
bus.connect("message", on_bus_message, loop)
# Probing a pad for custom applications
element = pipeline.get_by_name("probe_me")
pad = element.get_static_pad('src')
pad.add_probe(Gst.PadProbeType.BUFFER, probe_on_data, None)
# Start the pipeline
pipeline.set_state(Gst.State.PLAYING)
try:
# Blocking run call
loop.run()
except Exception as e:
print(e)
loop.quit()
# Python has a garbage collector, but normally we'd clean up here
pipeline.set_state(Gst.State.NULL)
del pipeline