I want to build a RTSPMediaFactory with GStreamer that would redirect the client to a video or another based on how many clients arrived before:
First two clients should be redirected to video/stream 0. Next ones should be redirected to video/stream 1.
Here’s what I have come up so far:
<code>import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject
class DynamicRTSPMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self, video_files):
super().__init__()
self.video_files = video_files
self.client_count = 0
self.set_shared(True)
def do_create_element(self, url):
self.client_count += 1
if self.client_count <= 2:
video_file = self.video_files[0]
else:
video_file = self.video_files[1]
pipeline_str = f"filesrc location={video_file} ! decodebin ! x264enc ! rtph264pay name=pay0"
return Gst.parse_launch(pipeline_str)
class RTSPServer:
def __init__(self):
Gst.init(None)
self.server = GstRtspServer.RTSPServer.new()
self.server.props.service = "8554" # set the port
self.mount_points = self.server.get_mount_points()
# Create and set up the dynamic factory
video_files = ["test.mp4", "test2.mp4"]
self.factory = DynamicRTSPMediaFactory(video_files)
# Add the factory to the mount points
self.mount_points.add_factory("/test", self.factory)
# Attach the server to the default main context
self.server.attach(None)
if __name__ == "__main__":
server = RTSPServer()
GObject.MainLoop().run()
</code>
<code>import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject
class DynamicRTSPMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self, video_files):
super().__init__()
self.video_files = video_files
self.client_count = 0
self.set_shared(True)
def do_create_element(self, url):
self.client_count += 1
if self.client_count <= 2:
video_file = self.video_files[0]
else:
video_file = self.video_files[1]
pipeline_str = f"filesrc location={video_file} ! decodebin ! x264enc ! rtph264pay name=pay0"
return Gst.parse_launch(pipeline_str)
class RTSPServer:
def __init__(self):
Gst.init(None)
self.server = GstRtspServer.RTSPServer.new()
self.server.props.service = "8554" # set the port
self.mount_points = self.server.get_mount_points()
# Create and set up the dynamic factory
video_files = ["test.mp4", "test2.mp4"]
self.factory = DynamicRTSPMediaFactory(video_files)
# Add the factory to the mount points
self.mount_points.add_factory("/test", self.factory)
# Attach the server to the default main context
self.server.attach(None)
if __name__ == "__main__":
server = RTSPServer()
GObject.MainLoop().run()
</code>
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject
class DynamicRTSPMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self, video_files):
super().__init__()
self.video_files = video_files
self.client_count = 0
self.set_shared(True)
def do_create_element(self, url):
self.client_count += 1
if self.client_count <= 2:
video_file = self.video_files[0]
else:
video_file = self.video_files[1]
pipeline_str = f"filesrc location={video_file} ! decodebin ! x264enc ! rtph264pay name=pay0"
return Gst.parse_launch(pipeline_str)
class RTSPServer:
def __init__(self):
Gst.init(None)
self.server = GstRtspServer.RTSPServer.new()
self.server.props.service = "8554" # set the port
self.mount_points = self.server.get_mount_points()
# Create and set up the dynamic factory
video_files = ["test.mp4", "test2.mp4"]
self.factory = DynamicRTSPMediaFactory(video_files)
# Add the factory to the mount points
self.mount_points.add_factory("/test", self.factory)
# Attach the server to the default main context
self.server.attach(None)
if __name__ == "__main__":
server = RTSPServer()
GObject.MainLoop().run()
The issue is that all clients receive the video/stream 0. client_count
isn’t incremented at each new connection. Is this the right approach ?
If it is of any use, I use VLC to test the RTSP Server.