rtsp

This module contains RTSPSkyLine and RTSPServer. These two classes allow serving a sequence of OpenCV images as video streams using the RTSP protocol.

To use this class you MUST have the following dependencies correctly configured on your system:

  • GStreamer 1.0 installed with standard plugins pack, libav, tools and development libraries

  • OpenCV 4.2.0, compiled with GStreamer support and Python bindings

  • gst-rtsp-server with development libraries (libgstrtspserver-1.0-dev)

These dependencies can not be easily specified by a requirements.txt or a Conda environment. See the example Dockerfile on how to install these dependencies on your system.

class backpack.rtsp.RTSPServer(port='8554', parent_logger=None)

Bases: object

The RTSPServer instance wraps a GStreamer RTSP server that serves video streams to clients using the RTSP protocol.

You typically want to have a single instance of RTSPServer for your application. You can register any number of video streams that will be served by a single instance of RTSP server. The port number of the server should be unique among all applications on the device.

For an example usage of RTSPServer, see the documentation of RTSPSkyLine class.

Parameters:
  • port (str) – The port to listen on. You can not modify the port after the initialization of the RTSPServer instance. Defaults to 8554.

  • parent_logger (Optional[Logger]) – If you want to connect the logger of RTSPServer to a parent, specify it here.

add_stream(mount_point, pipeline)

Registers a new video stream to the server.

Parameters:
  • mount_point (str) – The path that will be used to access the stream. For example, if you specify /my_stream, the stream will be accessible for clients using the rtsp://127.0.0.1:8854/mystream url (change the IP address and the port number accordingly).

  • pipeline (str) – The GStreamer pipeline to use for the stream. This will be typically a pipeline picking up the raw UDP packets from a local port and wrapping it to a H.264 envelope, for example: udpsrc port=5000 ! application/x-rtp,media=video,encoding-name=H264 ! rtph264depay ! rtph264pay name=pay0

Return type:

None

remove_stream(mount_point)

It removes a registered stream from the server.

Parameters:

mount_point (str) – The registered path of the stream.

Return type:

None

start()

Starts the RTSP server asynchronously.

After calling this method, the RTSP requests will be served.

stop()

Stops the RTSP server.

After calling this method, no RTSP requests will be served. The server can be restarted later on calling the start() method.

property port: str

The port where this server listens to incoming connections.

urls()

Returns the list of URLs where the server serves RTSP streams.

Return type:

List[str]

class backpack.rtsp.RTSPSkyLine(server, path, *args, **kwargs)

Bases: SkyLine

Together with RTSPServer, RTSPSkyLine a sequence of OpenCV frames on the RTSP protocol.

A single instance of RTSPServer application can serve streams coming from multiple RTSPSkyLine instances. You should instantiate the RTSPServer instance first. For example, if you want to serve two separate RTSP streams, you could use this code to set up your scenario:

server = RTSPServer(port="8554")
skyline1 = RTSPSkyLine(server, "/stream1")
skyline2 = RTSPSkyLine(server, "/stream2")
skyline1.start_streaming(30, 640, 480)
skyline2.start_streaming(30, 640, 480)
server.start()

while True:
    frame1 = ... # Get frame for the first stream as a numpy array of shape (640, 480, 3)
    frame2 = ... # Get frame for the second stream
    skyline1.put(frame1)
    skyline2.put(frame2)

Using this code, you can access the streams at the following URLs:

  • rtsp://127.0.0.1:8554/stream1

  • rtsp://127.0.0.1:8554/stream2

If the application (or the firewall) is configured to allow incoming connections on the 8554 port, the streams will be accessibly also from the external ip of the device.

Parameters:
  • server (RTSPServer) – The RTSPServer instance that this stream is being served by

  • path (str) – The path to the stream. This is the path that the client will use to connect to the stream

  • args (Any) – Positional arguments to be passed to SkyLine superclass initializer.

  • kwargs (Any) – Keyword arguments to be passed to SkyLine superclass initializer.