multirtspserver_web_logo

Multiple RTSP servers application template

v1.0.2

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.

Overview

The MultiRtspServer application is example of streaming video with using of multiple RtspServer objects. Application supports Windows and Linux with Intel GPU. The repository includes the source code of the application itself and source code of libraries: RtspServer (provides RTSP server), VCodecOneVpl (video codec for Intel GPU), 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 start the application reads configuration file MultiRtspServer.json (if no configuration file the application will create new one with default parameters, it uses ConfigReader library), after initializes video source (VSourceOpenCv) object (if user set parameters videoSource->source = “file dialog” the application will show file dialog to chose video file), after initializes three RTSP servers with individual parameters, after initialization the application runs main processing loop. In main processing loop the application captures video from video file or stream (output pixel format BGR24) and puts video frames to each RTSP server. RTSP server performs color format conversion, video scaling, video overlay (using custom implementation of VOverlay interface), video encoding (using VCodecOneVpl library for Intel GPU, the codec object is passed to the server during initialization) and provides RTSP stream for clients. Thus the application provides video streaming from video source (file or stream) using three RTSP servers with individual parameters. Application source code:

#include "VCodecOneVpl.h"
#include "RtspServer.h"
#include "VSourceOpenCv.h"
#include "SimpleFileDialog.h"
#include "CustomOverlay.h"

/// Number of RTSP servers.
#define NUM_RTSP_SERVERS 3

/// Applications params class.
class Params
{
public:
    /// 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)
        {
            params.rtspServer[i].port = 7000 + i;
            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";
            params.rtspServer[i].ip = "0.0.0.0"; // Connection from any IP.
        }

        // 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 RTSP servers.
    cr::rtsp::RtspServer 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],
            new cr::video::VCodecOneVpl(), 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.
3rdparty ----------------- Folder with third-party libraries.
    CMakeLists.txt ------- CMake file to include third-party libraries.
    RtspServer ----------- Folder with RtspServer library source code.
    SimpleFileDialog ----- Folder with SimpleFileDialog library source code.
    VCodecOneVpl --------- Folder with VCodecOneVpl library source code.
    VSourceOpenCv -------- Folder with VSourceOpenCv library source code.
src ---------------------- Folder with source code of the application.
    CMakeLists.txt ------- CMake file of the application.
    CustomOverlay.h ------ Custom implementation ov video overlay.
    CustomOverlay.c++ ---- C++ implementation file.
    main.cpp ------------- Source code file of the application.

Build application

The MultiRtspServer is complete repository in form of CMake project. The application and all included libraries depends on OpenCV and oneVPL API. Before compiling user has to install OpenCV library and OneVPL (see instructions in VCodecOneVpl library) into your OS. To install OpenCV in Linux (Ubuntu) use 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 application on Linux run command:

./MultiRtspServer

Note: on Windows you may need copy OpenCV dll files to application’s executable file folder. After start the application will create configuration file (if it doesn’t exist) MultiRtspServer.json with default parameters. Default contents of the configuration file:

{
    "Params": {
        "rtspServer": [
            {
                "bitrateKbps": 3000,
                "bitrateMode": 0,
                "codec": "H264",
                "custom1": 0.0,
                "custom2": 0.0,
                "custom3": 0.0,
                "enable": true,
                "fitMode": 0,
                "fps": 30.0,
                "gop": 30,
                "h264Profile": 0,
                "height": 480,
                "ip": "0.0.0.0",
                "jpegQuality": 80,
                "maxBitrateKbps": 5000,
                "minBitrateKbps": 1000,
                "multicastIp": "224.1.0.1",
                "multicastPort": 18000,
                "overlayMode": true,
                "password": "",
                "port": 7000,
                "suffix": "live",
                "type": 0,
                "user": "",
                "width": 640
            },
            {
                "bitrateKbps": 3000,
                "bitrateMode": 0,
                "codec": "H264",
                "custom1": 0.0,
                "custom2": 0.0,
                "custom3": 0.0,
                "enable": true,
                "fitMode": 0,
                "fps": 30.0,
                "gop": 30,
                "h264Profile": 0,
                "height": 720,
                "ip": "0.0.0.0",
                "jpegQuality": 80,
                "maxBitrateKbps": 5000,
                "minBitrateKbps": 1000,
                "multicastIp": "224.1.0.1",
                "multicastPort": 18000,
                "overlayMode": true,
                "password": "",
                "port": 7001,
                "suffix": "live",
                "type": 0,
                "user": "",
                "width": 1280
            },
            {
                "bitrateKbps": 3000,
                "bitrateMode": 0,
                "codec": "H264",
                "custom1": 0.0,
                "custom2": 0.0,
                "custom3": 0.0,
                "enable": true,
                "fitMode": 0,
                "fps": 30.0,
                "gop": 30,
                "h264Profile": 0,
                "height": 240,
                "ip": "0.0.0.0",
                "jpegQuality": 80,
                "maxBitrateKbps": 5000,
                "minBitrateKbps": 1000,
                "multicastIp": "224.1.0.1",
                "multicastPort": 18000,
                "overlayMode": true,
                "password": "",
                "port": 7002,
                "suffix": "live",
                "type": 0,
                "user": "",
                "width": 320
            }
        ],
        "videoSource": {
            "custom1": 0.0,
            "custom2": 0.0,
            "custom3": 0.0,
            "exposureMode": 1,
            "focusMode": 1,
            "fourcc": "YUYV",
            "fps": 30.0,
            "gainMode": 1,
            "height": 1080,
            "logLevel": 0,
            "roiHeight": 0,
            "roiWidth": 0,
            "roiX": 0,
            "roiY": 0,
            "source": "file dialog",
            "width": 1920
        }
    }
}

The application parameters include video source parameters (see description of VSourceOpenCv library) and parameters for three RTSP servers (see description of RtspServer library). After start user will see source video. Image bellow shows application main window and example of receiving video from three RTSP server with VLC player.

multirtspserver_user_interface