multirtspserver_web_logo

Multiple RTSP servers application template

v1.3.0

Table of contents

Application versions

Table 1 - Application versions.

Version Release date What’s new
1.0.0 22.05.2024 First version.
1.0.1 06.08.2024 - Submodules updated.
1.0.2 03.12.2024 - Submodules updated.
1.0.3 03.04.2025 - Multiple submodules update.
1.0.4 27.07.2025 - RtspServer submodules update.
1.1.0 16.08.2025 - CMake structure changed.
1.2.0 28.03.2026 - Codec was changed to software implementation.
1.2.1 20.06.2026 - Fixed RTSP server parameters initialization according to new VStreamer interface.
1.3.0 28.08.2026 - RtspServer replaced by MServer.
- Streamer preprocessing thread count set to 2 (VStreamerParams::custom1).
- Documentation updated.

Overview

The MultiRtspServer application is an example of streaming video using multiple MServer objects. The application supports Windows and Linux. The repository depends on libraries: MServer (provides RTSP server), VCodecLibav (software video codec based on ffmpeg), VSourceOpenCv (provides video capture from video files or streams) and SimpleFileDialog (provides file dialog). The application has a fairly short source code and works as follows: after starting, the application reads the configuration file MultiRtspServer.json (if there is no configuration file, the application will create a new one with default parameters, it uses ConfigReader library), then initializes the video source (VSourceOpenCv) object (if the user sets the parameter videoSource->source = “file dialog”, the application will show a file dialog to choose a video file), then initializes three RTSP servers, and after initialization the application runs the main processing loop. All MServer instances of one process share a single RTSP listener: the first instance opens it and the others join it, so the servers are told apart by the stream suffix and not by the port. The three streams are published as rtsp://IP:7000/live0, rtsp://IP:7000/live1 and rtsp://IP:7000/live2. In the main processing loop, the application captures video from a video file or stream (output pixel format BGR24) and puts video frames to each RTSP server. The RTSP server performs color format conversion, video scaling, video overlay (using custom implementation of VOverlay interface), video encoding (using VCodecLibav library, the codec object is passed to the server during initialization) and provides an RTSP stream for clients. Thus, the application provides video streaming from a video source (file or stream) using three RTSP streams with individual parameters. Application source code:

#include "RapidPixel.h"
#include "CustomOverlay.h"
#include <opencv2/opencv.hpp>

/// Number of RTSP servers.
constexpr int NUM_RTSP_SERVERS = 3;

/// Applications params class.
struct Params
{
    /// Video source params.
    cr::video::VSourceParams videoSource;
    /// RTSP servers params.
    cr::video::VStreamerParams rtspServer[NUM_RTSP_SERVERS];

    JSON_READABLE(Params, videoSource, rtspServer)
};

int main(void)
{
    // Load params from JSON file.
    cr::utils::ConfigReader config;
    Params params;
    bool needNewConfig = false;
    if (!config.readFromFile("MultiRtspServer.json"))
    {
        needNewConfig = true;
    }
    else if (!config.get(params, "Params"))
    {
        needNewConfig = true;
    }

    // Create new config file if needed.
    if (needNewConfig)
    {
        // Generate default params.
        params.videoSource.source = "file dialog";
        params.videoSource.fps = 30.0f;
        for (int i = 0; i < NUM_RTSP_SERVERS; ++i)
        {
            // MServer instances of one process SHARE a single RTSP listener:
            // the first instance opens it, the others join it and their own
            // rtspPort is ignored. Instances are told apart by the stream
            // suffix, not by the port, and a suffix can be claimed only once -
            // giving them all "live" makes every instance after the first fail
            // to initialise. Hence: one port, a unique suffix each.
            params.rtspServer[i].rtspPort = 7000;
            params.rtspServer[i].codec = "H264";
            params.rtspServer[i].width = 640;
            params.rtspServer[i].height = 480;
            params.rtspServer[i].fps = 30;
            params.rtspServer[i].bitrateKbps = 3000;
            params.rtspServer[i].gop = 30;
            params.rtspServer[i].suffix = "live" + std::to_string(i);

            // Codec type MServer applies to the codec object it was given:
            // 0 - hardware, 1 - software. Leaving the default 0 makes the
            // encoder look for a VA-API device and produce nothing on a
            // machine without one, so the stream is never published.
            params.rtspServer[i].type = 1;

            // The example demonstrates plain RTSP servers. MServer also serves
            // WebRTC, HLS, SRT, RTMP, metadata and the direct stream, and those
            // ports are NOT derived from rtspPort: leaving them enabled makes
            // every instance bind the same defaults, so the second server to
            // start fails. They are switched off here; enable them only with
            // per-instance ports.
            params.rtspServer[i].directStreamEnable = false;
            params.rtspServer[i].webRtcEnable = false;
            params.rtspServer[i].hlsEnable = false;
            params.rtspServer[i].srtEnable = false;
            params.rtspServer[i].rtmpEnable = false;
            params.rtspServer[i].metadataEnable = false;

        // Preprocessing threads of the streamer. MServer reads the value
        // from VStreamerParams::custom1 as a plain count: 2 keeps scaling,
        // conversion and overlay off the pipeline thread without taking
        // the whole machine.
            params.rtspServer[i].custom1 = 2.0f;
        }

        // Create new config file.
        config.set(params, "Params");
        if (!config.writeToFile("MultiRtspServer.json"))
        {
            return -1;
        }
    }

    // Init video source.
    cr::video::VSourceOpenCv videoSource;
    if (params.videoSource.source == "file dialog")
    {
        params.videoSource.source = cr::utils::SimpleFileDialog::dialog();
    }
    if (!videoSource.initVSource(params.videoSource))
    {
        return -1;
    }

    // Init video codec.
    cr::video::VCodec* codec = new cr::video::VCodecLibav();
    codec->setParam(cr::video::VCodecParam::TYPE, 1); // Software codec.

    // Init RTSP servers.
    cr::video::MServer rtspServer[NUM_RTSP_SERVERS];
    cr::video::CustomOverlay* overlay = new cr::video::CustomOverlay();
    for (int i = 0; i < NUM_RTSP_SERVERS; ++i)
    {
        if (!rtspServer[i].initVStreamer(params.rtspServer[i], codec, overlay))
        {
            return -1;
        }
    }

    // Main loop.
    cr::video::Frame bgrFrame;
    while (true)
    {
        // Get new frame.
        if (!videoSource.getFrame(bgrFrame, 1000))
        {
            continue;
        }

        // Update data in overlay.
        overlay->addFrameId(bgrFrame.frameId);

        // Send frame to RTSP servers.
        for (int i = 0; i < NUM_RTSP_SERVERS; ++i)
        {
            rtspServer[i].sendFrame(bgrFrame);
        }

        // Show frame.
        cv::Mat img(bgrFrame.height, bgrFrame.width, CV_8UC3, bgrFrame.data);
        cv::imshow("MultiRtspServer", img);
        if (cv::waitKey(1) == 27)
        {
            break;
        }
    }
    
    return 1;
}

Application files

The MultiRtspServer is provided as source code. Users are given a set of files in the form of a CMake project (repository). The repository structure is outlined below:

CMakeLists.txt ----------- Main CMake file of the application.
src ---------------------- Folder with source code of the application.
    CMakeLists.txt ------- CMake file of the application.
    CustomOverlay.h ------ Custom implementation of video overlay.
    CustomOverlay.cpp ---- C++ implementation file.
    main.cpp ------------- Source code file of the application.

Build application

The MultiRtspServer is a complete repository in the form of a CMake project. The application and all included libraries depend on OpenCV. Before compiling, the user has to install the OpenCV library. To install OpenCV in Linux (Ubuntu), use the command:

sudo apt-get install cmake libopencv-dev

Typical commands to build MultiRtspServer application (Release mode):

cd MultiRtspServer
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make

Launch and user interface

After compiling, you will get MultiRtspServer.exe executable file on Windows or MultiRtspServer executable file on Linux. To run the application on Linux, run the command:

./MultiRtspServer

Note: On Windows, you may need to copy OpenCV dll files to the application’s executable file folder. After starting, the application will create a configuration file (if it doesn’t exist). The application parameters include video source parameters (see description of VSourceOpenCv library) and parameters for three RTSP servers (see description of MServer library). After starting, the user will see the source video. The image below shows the application main window and an example of receiving video from three RTSP servers with VLC player.

multirtspserver_user_interface


Table of contents