Path: blob/master/libraries/AP_Camera/examples/gst_rtsp_server.py
9645 views
"""1GST RTSP Server Example23Usage4-----561. Start the server:78python ./gst_rtsp_server.py9102. Display the RTSP stream1112gst-launch-1.0 rtspsrc location=rtsp://localhost:8554/camera latency=50 ! decodebin ! autovideosink131415Acknowledgments16---------------1718GStreamer RTSP server example adapted from code by Jerome Carretero (Tamaggo)1920https://github.com/tamaggo/gstreamer-examples21https://github.com/tamaggo/gstreamer-examples/blob/master/test_gst_rtsp_server.py22"""2324# Copyright (c) 2015 Tamaggo Inc.25#26# Permission is hereby granted, free of charge, to any person obtaining a copy27# of this software and associated documentation files (the "Software"), to deal28# in the Software without restriction, including without limitation the rights29# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell30# copies of the Software, and to permit persons to whom the Software is31# furnished to do so, subject to the following conditions:32#33# The above copyright notice and this permission notice shall be included in all34# copies or substantial portions of the Software.35#36# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR37# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,38# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE39# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER40# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,41# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE42# SOFTWARE.4344# flake8: noqa4546import sys47import gi4849gi.require_version("Gst", "1.0")50gi.require_version("GstRtspServer", "1.0")51from gi.repository import Gst52from gi.repository import GstRtspServer53from gi.repository import GLib545556class VideoTestMediaFactory(GstRtspServer.RTSPMediaFactory):57"""58Create a GStreamer pipeline for a videotestsrc source.5960The default videotestsrc uses the smpte test pattern. Other test patterns61may be specified when initialising the class.62"""63def __init__(self, pattern="smpte"):64GstRtspServer.RTSPMediaFactory.__init__(self)65self.pattern = pattern6667def do_create_element(self, url):68s_src = f"videotestsrc pattern={self.pattern} ! video/x-raw,rate=30,width=640,height=480,format=I420"69s_h264 = "x264enc tune=zerolatency"70pipeline_str = f"( {s_src} ! queue max-size-buffers=1 name=q_enc ! {s_h264} ! rtph264pay name=pay0 pt=96 )"71if len(sys.argv) > 1:72pipeline_str = " ".join(sys.argv[1:])73print(pipeline_str)74return Gst.parse_launch(pipeline_str)757677class GstServer:78"""79A GStreamer RTSP server streaming three different test patterns:8081rtsp://127.0.0.1:8554/camera82rtsp://127.0.0.1:8554/ball83rtsp://127.0.0.1:8554/snow84"""85def __init__(self):86self.server = GstRtspServer.RTSPServer()87self.server.set_address("127.0.0.1")88self.server.set_service("8554")8990media_factory1 = VideoTestMediaFactory()91media_factory1.set_shared(True)9293media_factory2 = VideoTestMediaFactory("ball")94media_factory2.set_shared(True)9596media_factory3 = VideoTestMediaFactory("snow")97media_factory3.set_shared(True)9899mount_points = self.server.get_mount_points()100mount_points.add_factory("/camera", media_factory1)101mount_points.add_factory("/ball", media_factory2)102mount_points.add_factory("/snow", media_factory3)103104self.server.attach(None)105106107def main():108Gst.init(None)109server = GstServer()110loop = GLib.MainLoop()111loop.run()112113114if __name__ == "__main__":115main()116117118