Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Camera/examples/gst_udp_to_rtsp.py
Views: 1799
"""1Capture a UDP video stream and pipe to an GStreamer RTSP server23The Gazebo GStreamerPlugin with <use_basic_pipeline>1</use_basic_pipeline>4has elements:56source: appsrc7caps='video/x-raw, format=I420, width=640, height=480, framerate=50'8is-live=19do-timestamp=110stream-type=011queue: queue12converter: videoconvert13encoder: x264enc bitrate=800 speed-preset=6 tune=4 key-int-max=1014payloader: rtph264pay15sink: udpsink host=127.0.0.1 port=5600161718A udpsink example streaming a test pattern using the same settings as the basic19pipeline in the Gazebo GstCameraPlugin.20211. udpsink2223gst-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=560024252a. Display the udpsink directly2627udpsrc2829gst-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=false30312b. Or run the udpsink, this script to convert to RTSP, and view the RTSP stream:3233python ./gst_udp_to_rtsp.py34353. rtspsrc3637gst-launch-1.0 rtspsrc location=rtsp://localhost:8554/camera latency=50 ! decodebin ! autovideosink383940Acknowledgments41---------------4243GStreamer RTSP server example adapted from code by Jerome Carretero (Tamaggo)4445https://github.com/tamaggo/gstreamer-examples46https://github.com/tamaggo/gstreamer-examples/blob/master/test_gst_rtsp_server.py47"""4849# Copyright (c) 2015 Tamaggo Inc.50#51# Permission is hereby granted, free of charge, to any person obtaining a copy52# of this software and associated documentation files (the "Software"), to deal53# in the Software without restriction, including without limitation the rights54# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell55# copies of the Software, and to permit persons to whom the Software is56# furnished to do so, subject to the following conditions:57#58# The above copyright notice and this permission notice shall be included in all59# copies or substantial portions of the Software.60#61# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR62# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,63# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE64# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER65# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,66# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE67# SOFTWARE.68#6970import gi7172gi.require_version("Gst", "1.0")73gi.require_version("GstRtspServer", "1.0")74from gi.repository import Gst75from gi.repository import GstRtspServer76from gi.repository import GLib777879class GstUdpMediaFactory(GstRtspServer.RTSPMediaFactory):80"""81Create a GStreamer pipeline for a udpsrc source.82"""8384def __init__(self, address="127.0.0.1", port="5600"):85GstRtspServer.RTSPMediaFactory.__init__(self)8687# udpsrc options88self.address = address89self.port = port9091def do_create_element(self, url):92source = f"udpsrc address={self.address} port={self.port}"9394codec = "application/x-rtp, payload=96 ! rtph264depay ! h264parse ! avdec_h264"9596s_h264 = "x264enc tune=zerolatency"97pipeline_str = f"( {source} ! {codec} ! queue max-size-buffers=1 name=q_enc ! {s_h264} ! rtph264pay name=pay0 pt=96 )"98print(pipeline_str)99return Gst.parse_launch(pipeline_str)100101102class GstServer:103"""104A GStreamer RTSP server streaming a udpsrc to:105106rtsp://127.0.0.1:8554/camera107"""108109def __init__(self):110self.server = GstRtspServer.RTSPServer()111self.server.set_address("127.0.0.1")112self.server.set_service("8554")113114media_factory = GstUdpMediaFactory(address="127.0.0.1", port="5600")115media_factory.set_shared(True)116117mount_points = self.server.get_mount_points()118mount_points.add_factory("/camera", media_factory)119120self.server.attach(None)121122123def main():124Gst.init(None)125server = GstServer()126loop = GLib.MainLoop()127loop.run()128129130if __name__ == "__main__":131main()132133134