From c5e7ffcd1bcc8c24f2464e7881326d48131bada7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Kotiuk?= <kotiuk@zohomail.eu>
Date: Wed, 24 Jul 2024 18:39:00 +0200
Subject: [PATCH] example: Add example for streaming camera rith RTSP

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6965>
---
 .../gst-python/examples/stream_camera_rtsp.py | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100755 subprojects/gst-python/examples/stream_camera_rtsp.py

diff --git a/subprojects/gst-python/examples/stream_camera_rtsp.py b/subprojects/gst-python/examples/stream_camera_rtsp.py
new file mode 100755
index 0000000000..7690efaecc
--- /dev/null
+++ b/subprojects/gst-python/examples/stream_camera_rtsp.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+'''
+Simple example to demonstrate using GstRtspServer in Python with video source from camera
+'''
+import argparse
+import gi
+gi.require_version("Gst", "1.0")
+gi.require_version("GstRtspServer", "1.0")
+from gi.repository import GLib, Gst, GstRtspServer
+
+
+def start_rtsp_server(rtsp_port, device, stream_name):
+    server = GstRtspServer.RTSPServer.new()
+    server.props.service = rtsp_port
+    server.attach(None)
+
+    factory = GstRtspServer.RTSPMediaFactory.new()
+    # Define pipeline which will be created during connection
+    launch_str = f"( v4l2src device={device} ! videoconvert ! queue ! video/x-raw,format=I420 ! x264enc ! h264parse ! rtph264pay name=pay0 pt=96 )"
+    factory.set_launch(launch_str)
+    factory.set_shared(True)
+
+    server.get_mount_points().add_factory("/" + stream_name, factory)
+    print(f"Device: {device} \nUnder: rtsp://localhost:{rtsp_port}/{stream_name}")
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "--camera",
+        help="Camera file path",
+        type=str,
+        default="/dev/video0",
+    )
+    parser.add_argument("--rtsp-port", default="8557")
+    parser.add_argument("--stream-name", default="cam")
+    args = parser.parse_args()
+    Gst.init(None)
+    start_rtsp_server(args.rtsp_port, args.camera, args.stream_name)
+    loop = GLib.MainLoop()
+    loop.run()