pythonvideo-streaminggstreamerpygobjectrtsp-server

How can I change serve RTSP stream on custom ip and port?


I`m trying to publish a video file over RTSP with Gstreamer with Python binding. Here is my code

from argparse import Namespace, ArgumentParser

import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')

from gi.repository import Gst, GstRtspServer, GObject, GLib

loop = GLib.MainLoop()
Gst.init(None)


class RtspMediaFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self, filename):
        GstRtspServer.RTSPMediaFactory.__init__(self)
        self.filename = filename

    def do_create_element(self, url):
        # set mp4 file path to filesrc's location property
        src_demux = f"filesrc location={self.filename} ! qtdemux name=demux"
        h264_transcode = "demux.video_0"
        # h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
        pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
        print("Element created: " + pipeline)
        return Gst.parse_launch(pipeline)


def main(args: Namespace) -> None:
    rtsp_server = GstRtspServer.RTSPServer()
    factory = RtspMediaFactory(filename=args.file)
    factory.set_shared(True)
    mount_points = rtsp_server.get_mount_points()
    mount_points.add_factory("/Stream/", factory)
    rtsp_server.attach(None)


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--file', help='Video file path', type=str)
    main(args=parser.parse_args())
    loop.run()

If I run the code, the video will stream over rtsp://127.0.0.1:8554/stream/. Here I want to publish on a custom IP address like 0.0.0.0 with a custom port. Is there a way?
Thanks.


Solution

  • You can not freely choose what IP address the stream is published to. This script will start a RTSP server on your computer. The server will be reachable on the computers IP address. The 127.0.0.1 address is the loopback IP of your computer and the service will not be reachable by any other computer on the network using this address.

    If you can figure out your computers local ip address (typically looking something like 192.168.X.XXX) the service will probably be reachable over rtsp://192.168.X.XXX:8554/stream/. You can also publish the stream out towards the internet, then you need to configure your router to do port forwarding. The service will then be reachable on your home networks public IP.

    To change the port of the stream I think you can do:

    rtsp_server.set_service("<port-number")
    

    According to the documentation.