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_rtsp_server.py
Views: 1799
"""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.43#4445import sys46import gi4748gi.require_version("Gst", "1.0")49gi.require_version("GstRtspServer", "1.0")50from gi.repository import Gst51from gi.repository import GstRtspServer52from gi.repository import GLib535455class VideoTestMediaFactory(GstRtspServer.RTSPMediaFactory):56"""57Create a GStreamer pipeline for a videotestsrc source.5859The default videotestsrc uses the smpte test pattern. Other test patterns60may be specified when initialising the class.61"""62def __init__(self, pattern="smpte"):63GstRtspServer.RTSPMediaFactory.__init__(self)64self.pattern = pattern6566def do_create_element(self, url):67s_src = f"videotestsrc pattern={self.pattern} ! video/x-raw,rate=30,width=640,height=480,format=I420"68s_h264 = "x264enc tune=zerolatency"69pipeline_str = f"( {s_src} ! queue max-size-buffers=1 name=q_enc ! {s_h264} ! rtph264pay name=pay0 pt=96 )"70if len(sys.argv) > 1:71pipeline_str = " ".join(sys.argv[1:])72print(pipeline_str)73return Gst.parse_launch(pipeline_str)747576class GstServer:77"""78A GStreamer RTSP server streaming three different test patterns:7980rtsp://127.0.0.1:8554/camera81rtsp://127.0.0.1:8554/ball82rtsp://127.0.0.1:8554/snow83"""84def __init__(self):85self.server = GstRtspServer.RTSPServer()86self.server.set_address("127.0.0.1")87self.server.set_service("8554")8889media_factory1 = VideoTestMediaFactory()90media_factory1.set_shared(True)9192media_factory2 = VideoTestMediaFactory("ball")93media_factory2.set_shared(True)9495media_factory3 = VideoTestMediaFactory("snow")96media_factory3.set_shared(True)9798mount_points = self.server.get_mount_points()99mount_points.add_factory("/camera", media_factory1)100mount_points.add_factory("/ball", media_factory2)101mount_points.add_factory("/snow", media_factory3)102103self.server.attach(None)104105106def main():107Gst.init(None)108server = GstServer()109loop = GLib.MainLoop()110loop.run()111112113if __name__ == "__main__":114main()115116117