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_udp_to_rtsp.py
Views: 1799
1
"""
2
Capture a UDP video stream and pipe to an GStreamer RTSP server
3
4
The Gazebo GStreamerPlugin with <use_basic_pipeline>1</use_basic_pipeline>
5
has elements:
6
7
source: appsrc
8
caps='video/x-raw, format=I420, width=640, height=480, framerate=50'
9
is-live=1
10
do-timestamp=1
11
stream-type=0
12
queue: queue
13
converter: videoconvert
14
encoder: x264enc bitrate=800 speed-preset=6 tune=4 key-int-max=10
15
payloader: rtph264pay
16
sink: udpsink host=127.0.0.1 port=5600
17
18
19
A udpsink example streaming a test pattern using the same settings as the basic
20
pipeline in the Gazebo GstCameraPlugin.
21
22
1. udpsink
23
24
gst-launch-1.0 -v videotestsrc ! 'video/x-raw,format=I420,width=640,height=480,framerate=50/1' ! queue ! videoconvert ! x264enc bitrate=800 speed-preset=6 tune=4 key-int-max=10 ! rtph264pay ! udpsink host=127.0.0.1 port=5600
25
26
2a. Display the udpsink directly
27
28
udpsrc
29
30
gst-launch-1.0 -v udpsrc address=127.0.0.1 port=5600 caps='application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264' ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink sync=false
31
32
2b. Or run the udpsink, this script to convert to RTSP, and view the RTSP stream:
33
34
python ./gst_udp_to_rtsp.py
35
36
3. rtspsrc
37
38
gst-launch-1.0 rtspsrc location=rtsp://localhost:8554/camera latency=50 ! decodebin ! autovideosink
39
40
41
Acknowledgments
42
---------------
43
44
GStreamer RTSP server example adapted from code by Jerome Carretero (Tamaggo)
45
46
https://github.com/tamaggo/gstreamer-examples
47
https://github.com/tamaggo/gstreamer-examples/blob/master/test_gst_rtsp_server.py
48
"""
49
50
# Copyright (c) 2015 Tamaggo Inc.
51
#
52
# Permission is hereby granted, free of charge, to any person obtaining a copy
53
# of this software and associated documentation files (the "Software"), to deal
54
# in the Software without restriction, including without limitation the rights
55
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
56
# copies of the Software, and to permit persons to whom the Software is
57
# furnished to do so, subject to the following conditions:
58
#
59
# The above copyright notice and this permission notice shall be included in all
60
# copies or substantial portions of the Software.
61
#
62
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
63
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
64
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
65
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
66
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
67
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
68
# SOFTWARE.
69
#
70
71
import gi
72
73
gi.require_version("Gst", "1.0")
74
gi.require_version("GstRtspServer", "1.0")
75
from gi.repository import Gst
76
from gi.repository import GstRtspServer
77
from gi.repository import GLib
78
79
80
class GstUdpMediaFactory(GstRtspServer.RTSPMediaFactory):
81
"""
82
Create a GStreamer pipeline for a udpsrc source.
83
"""
84
85
def __init__(self, address="127.0.0.1", port="5600"):
86
GstRtspServer.RTSPMediaFactory.__init__(self)
87
88
# udpsrc options
89
self.address = address
90
self.port = port
91
92
def do_create_element(self, url):
93
source = f"udpsrc address={self.address} port={self.port}"
94
95
codec = "application/x-rtp, payload=96 ! rtph264depay ! h264parse ! avdec_h264"
96
97
s_h264 = "x264enc tune=zerolatency"
98
pipeline_str = f"( {source} ! {codec} ! queue max-size-buffers=1 name=q_enc ! {s_h264} ! rtph264pay name=pay0 pt=96 )"
99
print(pipeline_str)
100
return Gst.parse_launch(pipeline_str)
101
102
103
class GstServer:
104
"""
105
A GStreamer RTSP server streaming a udpsrc to:
106
107
rtsp://127.0.0.1:8554/camera
108
"""
109
110
def __init__(self):
111
self.server = GstRtspServer.RTSPServer()
112
self.server.set_address("127.0.0.1")
113
self.server.set_service("8554")
114
115
media_factory = GstUdpMediaFactory(address="127.0.0.1", port="5600")
116
media_factory.set_shared(True)
117
118
mount_points = self.server.get_mount_points()
119
mount_points.add_factory("/camera", media_factory)
120
121
self.server.attach(None)
122
123
124
def main():
125
Gst.init(None)
126
server = GstServer()
127
loop = GLib.MainLoop()
128
loop.run()
129
130
131
if __name__ == "__main__":
132
main()
133
134