
Multiple RTSP servers application template
v1.2.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. |
Overview
The MultiRtspServer application is an example of streaming video using multiple RtspServer objects. The application supports Windows and Linux. The repository depends on libraries: RtspServer (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 with individual parameters, and after initialization the application runs the main processing loop. 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 servers 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)
{
params.rtspServer[i].rtspPort = 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 video codec.
cr::video::VCodec* codec = new cr::video::VCodecLibav();
codec->setParam(cr::video::VCodecParam::TYPE, 1); // Software codec.
// 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], 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 RtspServer 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.
