Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Camera/examples/gst_udp_to_rtsp.py
9793 views
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
# flake8: noqa
71
72
import gi
73
74
gi.require_version("Gst", "1.0")
75
gi.require_version("GstRtspServer", "1.0")
76
from gi.repository import Gst
77
from gi.repository import GstRtspServer
78
from gi.repository import GLib
79
80
81
class GstUdpMediaFactory(GstRtspServer.RTSPMediaFactory):
82
"""
83
Create a GStreamer pipeline for a udpsrc source.
84
"""
85
86
def __init__(self, address="127.0.0.1", port="5600"):
87
GstRtspServer.RTSPMediaFactory.__init__(self)
88
89
# udpsrc options
90
self.address = address
91
self.port = port
92
93
def do_create_element(self, url):
94
source = f"udpsrc address={self.address} port={self.port}"
95
96
codec = "application/x-rtp, payload=96 ! rtph264depay ! h264parse ! avdec_h264"
97
98
s_h264 = "x264enc tune=zerolatency"
99
pipeline_str = f"( {source} ! {codec} ! queue max-size-buffers=1 name=q_enc ! {s_h264} ! rtph264pay name=pay0 pt=96 )"
100
print(pipeline_str)
101
return Gst.parse_launch(pipeline_str)
102
103
104
class GstServer:
105
"""
106
A GStreamer RTSP server streaming a udpsrc to:
107
108
rtsp://127.0.0.1:8554/camera
109
"""
110
111
def __init__(self):
112
self.server = GstRtspServer.RTSPServer()
113
self.server.set_address("127.0.0.1")
114
self.server.set_service("8554")
115
116
media_factory = GstUdpMediaFactory(address="127.0.0.1", port="5600")
117
media_factory.set_shared(True)
118
119
mount_points = self.server.get_mount_points()
120
mount_points.add_factory("/camera", media_factory)
121
122
self.server.attach(None)
123
124
125
def main():
126
Gst.init(None)
127
server = GstServer()
128
loop = GLib.MainLoop()
129
loop.run()
130
131
132
if __name__ == "__main__":
133
main()
134
135