CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Camera/examples/gst_rtsp_server.py
Views: 1799
1
"""
2
GST RTSP Server Example
3
4
Usage
5
-----
6
7
1. Start the server:
8
9
python ./gst_rtsp_server.py
10
11
2. Display the RTSP stream
12
13
gst-launch-1.0 rtspsrc location=rtsp://localhost:8554/camera latency=50 ! decodebin ! autovideosink
14
15
16
Acknowledgments
17
---------------
18
19
GStreamer RTSP server example adapted from code by Jerome Carretero (Tamaggo)
20
21
https://github.com/tamaggo/gstreamer-examples
22
https://github.com/tamaggo/gstreamer-examples/blob/master/test_gst_rtsp_server.py
23
"""
24
25
# Copyright (c) 2015 Tamaggo Inc.
26
#
27
# Permission is hereby granted, free of charge, to any person obtaining a copy
28
# of this software and associated documentation files (the "Software"), to deal
29
# in the Software without restriction, including without limitation the rights
30
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31
# copies of the Software, and to permit persons to whom the Software is
32
# furnished to do so, subject to the following conditions:
33
#
34
# The above copyright notice and this permission notice shall be included in all
35
# copies or substantial portions of the Software.
36
#
37
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43
# SOFTWARE.
44
#
45
46
import sys
47
import gi
48
49
gi.require_version("Gst", "1.0")
50
gi.require_version("GstRtspServer", "1.0")
51
from gi.repository import Gst
52
from gi.repository import GstRtspServer
53
from gi.repository import GLib
54
55
56
class VideoTestMediaFactory(GstRtspServer.RTSPMediaFactory):
57
"""
58
Create a GStreamer pipeline for a videotestsrc source.
59
60
The default videotestsrc uses the smpte test pattern. Other test patterns
61
may be specified when initialising the class.
62
"""
63
def __init__(self, pattern="smpte"):
64
GstRtspServer.RTSPMediaFactory.__init__(self)
65
self.pattern = pattern
66
67
def do_create_element(self, url):
68
s_src = f"videotestsrc pattern={self.pattern} ! video/x-raw,rate=30,width=640,height=480,format=I420"
69
s_h264 = "x264enc tune=zerolatency"
70
pipeline_str = f"( {s_src} ! queue max-size-buffers=1 name=q_enc ! {s_h264} ! rtph264pay name=pay0 pt=96 )"
71
if len(sys.argv) > 1:
72
pipeline_str = " ".join(sys.argv[1:])
73
print(pipeline_str)
74
return Gst.parse_launch(pipeline_str)
75
76
77
class GstServer:
78
"""
79
A GStreamer RTSP server streaming three different test patterns:
80
81
rtsp://127.0.0.1:8554/camera
82
rtsp://127.0.0.1:8554/ball
83
rtsp://127.0.0.1:8554/snow
84
"""
85
def __init__(self):
86
self.server = GstRtspServer.RTSPServer()
87
self.server.set_address("127.0.0.1")
88
self.server.set_service("8554")
89
90
media_factory1 = VideoTestMediaFactory()
91
media_factory1.set_shared(True)
92
93
media_factory2 = VideoTestMediaFactory("ball")
94
media_factory2.set_shared(True)
95
96
media_factory3 = VideoTestMediaFactory("snow")
97
media_factory3.set_shared(True)
98
99
mount_points = self.server.get_mount_points()
100
mount_points.add_factory("/camera", media_factory1)
101
mount_points.add_factory("/ball", media_factory2)
102
mount_points.add_factory("/snow", media_factory3)
103
104
self.server.attach(None)
105
106
107
def main():
108
Gst.init(None)
109
server = GstServer()
110
loop = GLib.MainLoop()
111
loop.run()
112
113
114
if __name__ == "__main__":
115
main()
116
117