RtspServerLive555 C++ library
v3.0.1
Table of contents
- Overview
- Versions
- Library files
- RtspServerLive555 class description
- Data structures
- VStreamerParams class description
- Examples
- Dependencies
- Build and connect to your project
Overview
The RtspServerLive555 is a C++ library which implements RTSP server. The library based on popular open source library Live555 (source code not included, just linked). It provides simple interface according to VStreamer interface class and can be included in any C++ project. It allows developer to create a RTSP server without the complexity of the Live555 library. User can set general RTSP server parameters: RTSP server port, IP, stream name, user name and password. Server works frame-by-frame: user puts video frame. RtspServerLive555 accepts both raw pixel formats and compressed formats like H264, HEVC or JPEG. If the frame is in a compressed format (H264, HEVC or JPEG), it bypasses any overlay and resizing processes and is sent directly to clients. However, if the frame is raw, it undergoes a series of processing steps. First, resizing is performed if necessary, followed by the application of any required overlay. Finally, the processed frame is encoded into a compressed format before being transmitted to clients (to use this functions the user must define video codec and video overlay implementation according to VCodec, VOverlay interfaces). The library relies on several dependencies: VCodec (defines video codec interface, source code included, Apache 2.0 license), VOverlay (defines video overlay interface, source code included, Apache 2.0 license), FormatConverterOpenCv (provides methods to convert pixel formats, source code included), ConfigReader (provides methods to work with JSON files, source code included, Apache 2.0 license) and OpenCV (version 4.5 and higher, video scaling functions). It has been specifically tested and verified for functionality on the Linux operating system. it uses C++17 standard. The RTSP server provided by library compatible with all popular RTSP clients: ffmpeg, gstreamer, VLC, Milestone etc.
Versions
Table 1 - Library versions.
Version | Release date | What’s new |
---|---|---|
1.0.0 | 20.11.2023 | - First version which supports H264 and JPEG codecs. |
1.0.1 | 22.11.2023 | - Documentation updated. |
1.1.0 | 04.12.2023 | - Getting number of clients feature added. - Tracing package lost ratio feature added. |
2.0.0 | 13.12.2023 | - Interface is changed. - Streaming Raw frame support added. |
2.0.1 | 04.01.2024 | - Source code typos and styling issues fixed. - Example applications updated. - Documentation updated. |
2.0.2 | 15.01.2024 | - Bugs fixed. |
2.0.3 | 22.01.2024 | - Initialization bugs fixed. - New parameters added. |
3.0.0 | 16.04.2024 | - New VStreamer interface implemented. - Source code reviewed and simplified. |
3.0.1 | 21.05.2024 | - Submodules updated. - Documentation updated. |
3.1.0 | 05.08.2024 | - Moved implementation files. - CMake structure changed. |
Library files
The library is supplied only by source code. The user is given a set of files in the form of a CMake project (repository). The repository structure is shown below:
CMakeLists.txt ------------------------ Main CMake file of the library.
3rdparty ------------------------------ Folder with third-party libraries.
CMakeLists.txt -------------------- CMake file to include third-party libraries.
FormatConverterOpenCv ------------- Folder with FormatConverterOpenCv library source code.
VStreamer ------------------------- Folder with VStreamer interface library source code.
src ----------------------------------- Folder with library source code.
CMakeLists.txt -------------------- CMake file of the library.
RtspServerLive555.cpp ------------- Library main class source file.
RtspServerLive555.h --------------- Library main class header file.
RtspServerLive555Version.h -------- Header file with library version.
RtspServerLive555Version.h.in ----- Service CMake file to generate version header.
impl
RtspServerLive555.cpp --------- Library main class source file.
RtspServerLive555.h ----------- Library main class header file.
MJPEGVideoSource.cpp ---------- Library side class source file.
MJPEGVideoSource.h ------------ Library side class header file.
Source.cpp -------------------- Library side class source file.
Source.h ---------------------- Library side class header file.
UnicastServerMediaSubsession.cpp Library main class source file.
UnicastServerMediaSubsession.h - Library main class header file.
test ---------------------------------- Folder of test application.
3rdparty -------------------------- Folder with third-party libraries for test application.
VSourceFile ------------------- Folder with VSourceFile library source code.
CMakeLists.txt -------------------- CMake file of test application.
main.cpp -------------------------- Source C++ file of test application.
examples ------------------------------ Folder for examples
CompressedFrameStreaming ---------- Folder with example for compressed video frames.
CMakeLists.txt ---------------- CMake file of example.
main.cpp ---------------------- Source code of example.
RawFrameStreaming ----------------- Folder with example for RAW video frames.
CMakeLists.txt ---------------- CMake file of example.
main.cpp ---------------------- Source code of example.
Test application additionally depends on VSourceFile library (source code included) which provides methods to read H264 and HEVC files.
RtspServerLive555 class description
RtspServerLive555 class declaration
RtspServerLive555 class declared in RtspServerLive555.h file. Class declaration:
class RtspServerLive555 : public VStreamer
{
public:
/// Class destructor.
~RtspServerLive555();
/// Get class version string.
static std::string getVersion();
/// Init RTSP server.
bool initVStreamer(VStreamerParams& params,
cr::video::VCodec* codec = nullptr,
cr::video::VOverlay* overlay = nullptr) override;
/// Get init status.
bool isVStreamerInit() override;
/// Set param.
bool setParam(VStreamerParam id, float value) override;
/// Set param.
bool setParam(VStreamerParam id, std::string value) override;
/// Get RTSP server params.
void getParams(VStreamerParams& params) override;
/// Execute command. Method is thread safe.
bool executeCommand(VStreamerCommand id) override;
/// Send frame.
bool sendFrame(cr::video::Frame& frame) override;
/// Close RTSP server.
void closeVStreamer() override;
};
getVersion method
The getVersion() method returns string of current class version. Method declaration:
static std::string getVersion();
Method can be used without RtspServerLive555 class instance:
std::cout << "RtspServerLive555 version: " << RtspServerLive555::getVersion();
Console output:
RtspServerLive555 version: 3.1.0
initVStreamer method
The initVStreamer(…) method initializes RTSP server. If server already initialized the method will reinitialize server. If RTSP server parameters changed user does not need to call initVStreamer(…) method to reinitialize server. Method declaration:
bool initVStreamer(VStreamerParams& params,
cr::video::VCodec* codec = nullptr,
cr::video::VOverlay* overlay = nullptr) override;
Parameter | Value |
---|---|
params | VStreamerParams object which includes required init parameters. |
codec | VCodec object pointer in case raw frame streaming. |
overlay | VOverlay object pointer. |
Important note: The VCodec implementation must be designed to accept input frames in the NV12 format. The RtspServerLive555 library feeds NV12 formatted frames into the codec. If a custom implementation of VCodec does not support NV12 as the input format, the library will not work properly. Similarly, the VOverlay implementation must also be designed to handle input frames, but in the YUV24 format instead of NV12. This requirement is essential for the correct operation of the library.
Returns: TRUE if initialization is done or FALSE if not.
isVStreamerInit method
The isVStreamerInit() method returns video streamer initialization status. Method declaration:
bool isVStreamerInit() override;
Returns: TRUE if the video streamer initialized or FALSE if not.
setParam (string parameter) method
The setParam(…) method to set parameter with string value. Method declaration:
bool setParam(VStreamerParam id, std::string value) override;
Parameter | Value |
---|---|
id | Parameter ID from VStreamerParam enum class. |
value | Parameter value with string type. |
Returns: TRUE if parameter is set or FALSE if not.
setParam (float parameter) method
The setParam(…) method to set parameter with float value. Method declaration:
bool setParam(VStreamerParam id, float value) override;
Parameter | Value |
---|---|
id | Parameter ID from VStreamerParam enum class. |
value | Parameter value with float type. |
Returns: TRUE if parameter is set or FALSE if not.
getParams method
The getParams(…) method to get current parameters that are defined in form of VStreamerParams. Method declaration:
void getParams(VStreamerParams& params) override;
Parameter | Value |
---|---|
params | Reference to VStreamerParams object to store current parameters. |
executeCommand method
The executeCommand(…) method executes command. Method declaration:
bool executeCommand(VStreamerCommand id) override;
Parameter | Value |
---|---|
id | Command id from VStreamerCommand. |
Returns: TRUE if command is executed or FALSE if not.
sendFrame method
The sendFrame(…) method sends Frame to RTSP server. Method declaration:
bool sendFrame(cr::video::Frame& frame) override;
Parameter | Value |
---|---|
frame | Frame object to send RTSP stream. H264, JPEG, HEVC and raw frame formats are supported. In case of raw frame, VCodec object had to be provided to initVStreamer method. Also in case of raw frame format, fitting-scaling will be carried out by server if provided frame has different dimensions than dimensions defined by RtspServerParams. If provided frame is not RAW, Overlay can not apply even if user provides VOverlay instance to initVStreamer method. |
Returns: TRUE if frame is sent or FALSE if not.
closeVStreamer method
The closeVStreamer() method to close RTSP server if it is open. Method declaration:
void closeVStreamer() override;
Data structures
VStreamer.h file defines IDs for parameters (VStreamerParam enum) and IDs for commands (VStreamerCommand enum).
VStreamerCommand enum
Enum declaration:
enum class VStreamerCommand
{
/// Restart.
RESTART = 1,
/// Enable. Equal to MODE param.
ON,
/// Disable. Equal to MODE params.
OFF
};
Table 4 - Video stream commands description.
Command | Description |
---|---|
RESTART | Restarts streamer with last VStreamerParams. |
ON | Enables streamer if it is disabled. |
OFF | Disables streamer if it is enabled. |
VStreamerParam enum
Enum declaration:
enum class VStreamerParam
{
/// Mode: 0 - disabled, 1 - enabled.
MODE = 1,
/// Video stream width from 8 to 8192.
WIDTH,
/// Video stream height from 8 to 8192.
HEIGHT,
/// Streamer IP.
IP,
/// Streamer port.
PORT,
/// Streamer multicast IP.
MULTICAST_IP,
/// Streamer multicast port.
MULTICAST_PORT,
/// Streamer user (for rtsp streaming): "" - no user.
USER,
/// Streamer password (for rtsp streaming): "" - no password.
PASSWORD,
/// Streamer suffix(for rtsp streaming, stream name).
SUFFIX,
/// Minimum bitrate for variable bitrate mode, kbps.
MIN_BITRATE_KBPS,
/// Maximum bitrate for variable bitrate mode, kbps.
MAX_BITRATE_KBPS,
/// Current bitrate, kbps.
BITRATE_KBPS,
/// Bitrate mode: 0 - constant bitrate, 1 - variable bitrate.
BITRATE_MODE,
/// FPS.
FPS,
/// GOP size for H264 and H265 codecs.
GOP,
/// H264 profile: 0 - baseline, 1 - main, 2 - high.
H264_PROFILE,
/// JPEG quality from 1 to 100% for JPEG codec.
JPEG_QUALITY,
/// Codec type: "H264", "HEVC" or "JPEG".
CODEC,
/// Scaling mode: 0 - fit, 1 - cut.
FIT_MODE,
/// Overlay mode: false - off, true - on.
OVERLAY_MODE,
/// TYPE of the streamer
TYPE,
/// Custom parameter 1.
CUSTOM1,
/// Custom parameter 2.
CUSTOM2,
/// Custom parameter 3.
CUSTOM3
};
Table 5 - Video streamer params description.
Parameter | Description |
---|---|
MODE | Enable/disable RTSP server (0 - disabled, 1 - enabled). |
WIDTH | Frame width. |
HEIGHT | Frame height. |
IP | RTSP server’s ip. |
PORT | RTSP server’s port. |
MULTICAST_IP | Streamer’s multicast ip. |
MULTICAST_PORT | Streamer’s multicast port. It is UDP port. |
USER | User name for auth of RTSP stream. |
PASSWORD | Password name for auth of RTSP stream. |
SUFFIX | Stream name for RTSP stream. |
MIN_BITRATE_KBPS | Minimum bitrate for variable bitrate encoding in case RAW input video frames.Not used. |
MAX_BITRATE_KBPS | Maximum bitrate for variable bitrate encoding in case RAW input video frames.Not used. |
BITRATE_KBPS | Bitrate for constant bitrate encoding in case RAW input video frames. |
BITRATE_MODE | Enable/disable variable bitrate. Not used. |
FPS | Streamer’s fps and also encoding fps in case RAW input video frames. Regardless of the input video frame rate, the streamer provides the specified FPS. If the FPS value is 0, the streamer provides FPS equal to the input video frame rate. |
GOP | Codec gop size in case in case RAW input video frames. |
H264_PROFILE | H264 encoding profile in case RAW input video frames. |
JPEG_QUALITY | JPEG encoding quality in case RAW input video frames. |
CODEC | Codec type for encoding RAW frames. |
FIT_MODE | Scaling mode. Values: 0 - fit, 1 - crop. in case RAW input video frames. |
OVERLAY_MODE | Overlay enable/disable in case RAW input video frames. Values : 0 - disable, 1 - enable. |
CYCLE_TIME_MKSEC | Read only. Cycle timeout, microseconds. |
TYPE | Type of streamer. Not used. |
CUSTOM1 | Custom parameter. Not used. |
CUSTOM2 | Custom parameter. Not used. |
CUSTOM3 | Custom parameter. Not used. |
VStreamerParams class description
VStreamerParams class declaration
VStreamerParams class used for video stream initialization (initVStreamer(…) method) or to get all actual params (getParams(…) method). Also VStreamerParams provides structure to write/read params from JSON files (JSON_READABLE macro, see ConfigReader class description) and provide methods to encode and decode params. Class declaration:
class VStreamerParams
{
public:
/// Streamer mode: false - Off, true - On.
bool enable{true};
/// Video stream width from 8 to 8192.
int width{1280};
/// Video stream height from 8 to 8192.
int height{720};
/// Streamer IP.
std::string ip{"127.0.0.1"};
/// Streamer port.
int port{8554};
/// Streamer multicast IP.
std::string multicastIp{"224.1.0.1"};
/// Streamer multicast port.
int multicastPort{18000};
/// Streamer user (for rtsp streaming): "" - no user.
std::string user{""};
/// Streamer password (for rtsp streaming): "" - no password.
std::string password{""};
/// Streamer suffix (for rtsp streaming) (stream name).
std::string suffix{"live"};
/// Minimum bitrate for variable bitrate mode, kbps.
int minBitrateKbps{1000};
/// Maximum bitrate for variable bitrate mode, kbps.
int maxBitrateKbps{5000};
/// Current bitrate, kbps.
int bitrateKbps{3000};
/// Bitrate mode: 0 - constant bitrate, 1 - variable bitrate.
int bitrateMode{0};
/// FPS.
float fps{30.0f};
/// GOP size for H264 and H265 codecs.
int gop{30};
/// H264 profile: 0 - baseline, 1 - main, 2 - high.
int h264Profile{0};
/// JPEG quality from 1 to 100% for JPEG codec.
int jpegQuality{80};
/// Codec type: "H264", "HEVC" or "JPEG".
std::string codec{"H264"};
/// Scaling mode: 0 - fit, 1 - cut.
int fitMode{0};
/// Cycle time, mksec. Calculated by streamer.
int cycleTimeMksec{0};
/// Overlay mode: false - off, true - on.
bool overlayMode{true};
/// type of the streamer
int type{0};
/// Custom parameter 1.
float custom1{0.0f};
/// Custom parameter 2.
float custom2{0.0f};
/// Custom parameter 3.
float custom3{0.0f};
JSON_READABLE(VStreamerParams, enable, width, height, ip, port, user,
password, suffix, minBitrateKbps, maxBitrateKbps, bitrateKbps,
bitrateMode, fps, gop, h264Profile, jpegQuality, codec,
fitMode, overlayMode, type, custom1, custom2, custom3)
/// Assignment operator.
VStreamerParams& operator= (const VStreamerParams& src);
/// Serialize parameters.
bool encode(uint8_t* data, int bufferSize, int& size,
VStreamerParamsMask* mask = nullptr);
/// Deserialize parameters.
bool decode(uint8_t* data, int dataSize);
};
Serialize video streamer params
VStreamerParams class provides method encode(…) to serialize video streamer params (fields of VStreamerParams class). Serialization of video streamer params necessary in case when you need to send video streamer params via communication channels. Method provides options to exclude particular parameters from serialization. To do this method inserts binary mask (4 bytes) where each bit represents particular parameter and decode(…) method recognizes it. Method declaration:
bool encode(uint8_t* data, int bufferSize, int& size, VStreamerParamsMask* mask = nullptr);
Parameter | Value |
---|---|
data | Pointer to data buffer. |
size | Size of encoded data. |
bufferSize | Data buffer size. If buffer size smaller than required, buffer will be filled with fewer parameters. |
mask | Parameters mask - pointer to VStreamerParamsMask structure. VStreamerParamsMask (declared in VStreamer.h file) determines flags for each field (parameter) declared in VStreamerParams class. If the user wants to exclude any parameters from serialization, he can put a pointer to the mask. If the user wants to exclude a particular parameter from serialization, he should set the corresponding flag in the VStreamerParams structure. |
VStreamerParamsMask structure declaration:
struct VStreamerParamsMask
{
bool enable{true};
bool width{true};
bool height{true};
bool ip{true};
bool port{true};
bool multicastIp{true};
bool multicastPort{true};
bool user{true};
bool password{true};
bool suffix{true};
bool minBitrateKbps{true};
bool maxBitrateKbps{true};
bool bitrateKbps{true};
bool bitrateMode{true};
bool fps{true};
bool gop{true};
bool h264Profile{true};
bool jpegQuality{true};
bool codec{true};
bool fitMode{true};
bool cycleTimeMksec{true};
bool overlayMode{true};
bool type{true};
bool custom1{true};
bool custom2{true};
bool custom3{true};
};
Example without parameters mask:
// Prepare random params.
VStreamerParams in;
in.ip = "alsfghljb";
in.port = 0;
// Encode data.
uint8_t data[1024];
int size = 0;
in.encode(data, 1024, size);
cout << "Encoded data size: " << size << " bytes" << endl;
Example without parameters mask:
// Prepare random params.
VStreamerParams in;
in.ip = "alsfghljb";
in.port = 0;
// Prepare params mask.
VStreamerParamsMask mask;
mask.port = false; // Exclude port. Others by default.
// Encode data.
uint8_t data[1024];
int size = 0;
in.encode(data, 1024, size, &mask);
cout << "Encoded data size: " << size << " bytes" << endl;
Deserialize video stream params
VStreamerParams class provides method decode(…) to deserialize video streamer params (fields of VStreamerParams class). Deserialization of video streamer params necessary in case when you need to receive video streamer params via communication channels. Method automatically recognizes which parameters were serialized by encode(…) method. Method declaration:
bool decode(uint8_t* data, int dataSize);
Parameter | Value |
---|---|
data | Pointer to encode data buffer. |
dataSize | Size of data. |
Returns: TRUE if data decoded (deserialized) or FALSE if not.
Example:
// Encode data.
VStreamerParams in;
uint8_t data[1024];
int size = 0;
in.encode(data, 1024, size);
cout << "Encoded data size: " << size << " bytes" << endl;
// Decode data.
VStreamerParams out;
if (!out.decode(data, size))
cout << "Can't decode data" << endl;
Read params from JSON file and write to JSON file
VStreamer library depends on ConfigReader library which provides method to read params from JSON file and to write params to JSON file. Example of writing and reading params to JSON file:
// Write params to file.
VStreamerParams in;
cr::utils::ConfigReader inConfig;
inConfig.set(in, "vStreamerParams");
inConfig.writeToFile("TestVStreamerParams.json");
// Read params from file.
cr::utils::ConfigReader outConfig;
if(!outConfig.readFromFile("TestVStreamerParams.json"))
{
cout << "Can't open config file" << endl;
return false;
}
TestVStreamerParams.json will look like:
{
"vStreamerParams":
{
"bitrateKbps": 207,
"bitrateMode": 206,
"codec": "eydiucnksa",
"custom1": 249.0,
"custom2": 150.0,
"custom3": 252.0,
"enable": false,
"fitMode": 39,
"fps": 35.0,
"gop": 1,
"h264Profile": 61,
"height": 61,
"ip": "afhjaskdm",
"jpegQuality": 80,
"maxBitrateKbps": 89,
"minBitrateKbps": 180,
"multicastIp": "afhjaskdmasd",
"multicastPort": 180,
"overlayMode": true,
"password": "adafsodjf",
"port": 226,
"suffix": "asdasdasd",
"type": 167,
"user": "afhidsjfnm",
"width": 50
}
}
Examples
First application shows how to open video file (compressed video) with VSourceFile library, capture video and put to RTSP server.
#include <iostream>
#include "RtspServerLive555.h"
#include "VSourceFile.h"
using namespace std;
using namespace cr::rtsp;
using namespace cr::video;
int main(void)
{
cout << "RtspServerLive555 v" << RtspServerLive555::getVersion() << endl;
cout << endl;
// Init RTSP server params for encoded frames by default params.
VStreamerParams params;
params.port = 7031;
params.ip = "0.0.0.0";
params.suffix = "live";
params.user = ""; // No user name.
params.password = ""; // No password.
params.fps = 30;
// RTSP server.
RtspServerLive555 server;
if(!server.initVStreamer(params))
{
cout << "Can't init RTSP server" << endl;
return -1;
}
cout << "RTSP init string: rtsp://127.0.0.1:7031/live" << endl;
// Set video source init string.
string initString = "out.264;1280;720;30";
// Init file video source.
VSourceFile fileSource;
if(!fileSource.openVSource(initString))
{
cout << "Can't open source file" << endl;
return -1;
}
// Main thread.
Frame sourceFrame;
while(1)
{
// Get new frame from video file.
if(!fileSource.getFrame(sourceFrame, 1000))
{
cout << "Frame could not get" << endl;
continue;
}
// Send frame to RTSP server.
if(!server.sendFrame(sourceFrame))
{
cout << "Can not send frame to rtsp server" << endl;
}
}
return 1;
}
Second application shows how to work with RAW video frames. This application includes custom implementation of video code and video overlay.
#include <iostream>
#include <opencv2/opencv.hpp>
#include "RtspServerLive555.h"
#include <FormatConverterOpenCv.h>
using namespace cv;
using namespace std;
using namespace cr::rtsp;
using namespace cr::video;
using namespace std::chrono;
/// Custom video codec class based on OpenCV for JPEG encoding.
class VCodecOpenCv : public VCodec
{
public:
/// Encode video frame.
bool transcode(Frame& src, Frame& dst)
{
// Check input data.
if (src.size == 0 || src.width == 0 || src.height == 0)
return false;
// Check output frame initialization.
if (dst.width != src.width || dst.height != src.height)
dst = Frame(src.width, src.height, Fourcc::JPEG);
// VCodec implementations work with NV12 input
// So RtspServerLive555 also follows same logic.
// This means any custom VCodec implementation should accept NV12 input.
Frame bgrFrame;
bgrFrame.fourcc = Fourcc::BGR24;
FormatConverterOpenCv converter;
converter.convert(src, bgrFrame);
// Create cv::Mat from the BGR data
Mat bgrFrameCv(bgrFrame.height, bgrFrame.width, CV_8UC3, bgrFrame.data);
// Set JPEG quality. Here, 70 is an example quality level.
vector<int> compression_params;
compression_params.push_back(IMWRITE_JPEG_QUALITY);
compression_params.push_back(70); // Quality level here
// Encode the image to JPG format
vector<uchar> encodedImage;
imencode(".jpg", bgrFrameCv, encodedImage, compression_params);
// Copy the data from vector to uint8_t array
copy(encodedImage.begin(), encodedImage.end(), dst.data);
dst.size = encodedImage.size();
return true;
}
// Not used.
bool setParam(VCodecParam id, float value) { return false; }
// Not used.
float getParam(VCodecParam id) { return -1.0; }
// Not used.
bool executeCommand(VCodecCommand id) { return false; }
};
/// Custom video overlay implementation. Works with YUV pixel formats.
class VOverlayCustom : public VOverlay
{
public:
/// Overlay the information on the video.
bool overlay(Frame& frame, void* data = nullptr)
{
// Put the text on the frame.
static int counter = 0;
Mat yuvImg(frame.height, frame.width, CV_8UC3, frame.data);
putText(yuvImg, to_string(counter++), Point(60, 60),
FONT_HERSHEY_SIMPLEX, 2.5, Scalar(76, 84, 255), 2);
return true;
}
};
int main(void)
{
cout << "RtspServerLive555 v" << RtspServerLive555::getVersion() << endl;
cout << endl;
// Set initial RTSP server params.
VStreamerParams params;
params.port = 7031;
params.ip = "0.0.0.0"; // For any IP.
params.suffix = "live";
params.user = ""; // No user name.
params.password = ""; // No password.
params.fps = 30;
params.codec = "JPEG";
params.width = 1280;
params.height = 720;
params.fitMode = 0;
params.overlayMode = true;
// Create and init RTSP server.
RtspServerLive555 server;
if(!server.initVStreamer(params, new VCodecOpenCv(), new VOverlayCustom()))
{
cout << "Can't init RTSP server" << endl;
return -1;
}
cout << "RTSP init string: rtsp://127.0.0.1:7031/live" << endl;
// Main loop.
int sourceFrameWidth = 1280;
int sourceFrameHeight = 1024;
Mat sourceFrame(sourceFrameHeight, sourceFrameWidth, CV_8UC3);
int frameId = 0;
while(1)
{
// Create new artificial frame.
memset(sourceFrame.data, 60, sourceFrameWidth * sourceFrameHeight * 3);
putText(sourceFrame, to_string(frameId++), Point(300, 300),
FONT_HERSHEY_SIMPLEX, 6, Scalar(255, 255, 0), 5);
// Prepare Frame object from OpenCV.
Frame bgrFrame;
bgrFrame.width = sourceFrame.size().width;
bgrFrame.height = sourceFrame.size().height;
bgrFrame.size = bgrFrame.width * bgrFrame.height * 3;
bgrFrame.data = sourceFrame.data;
bgrFrame.fourcc = Fourcc::BGR24;
// Send RAW frame to RTSP server.
if(!server.sendFrame(bgrFrame))
{
cout << "Can not send frame to rtsp server" << endl;
continue;
}
// Wait for aprox 30 fps.
this_thread::sleep_for(milliseconds(33));
}
return 1;
}
Dependencies
The RtspServerLive555 is a C++ library uses the Live555 library, which in turn relies on OpenSSL for secure network communication. To effectively utilize the RtspServerLive555 library, it is essential to ensure that both Live555 and OpenSSL are correctly installed and configured on your system. Also library uses OpenCV library (version 4.5 and higher).
How to install OpenSSL
-
Run command :
sudo apt-get install libssl-dev
How to install Live555 by using package manager
-
Run command :
sudo apt-get install liblivemedia-dev
If you get error from package manager, go to next step and install live555 from source code.
Important note : There will be a compilation error if your system has live555 from source code installation and package manager installation at the same time. In order to solve problem you should remove one of them. For example:
sudo apt-get remove liblivemedia-dev
How to install Live555 from source code
-
First you need to download Live555 from official website.
-
Extract .tar.gz file.
tar -xz live.(version).tar.gz
-
Run genMakefiles command with proper OS name. You check supported OS in live folder see files config.OSname .
cd live
./genMakefiles <os-platform>
for example:
./genMakefiles linux-64bit
if you are getting compile error. Add -std=C++17 -DNO_STD_LIB flags to CPLUSPLUS_FLAGS param in config.os-platform for example config.linux-64-bit:
COMPILE_OPTS = $(INCLUDES) -fPIC -I/usr/local/include -I. -O2 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64 C = c C_COMPILER = cc C_FLAGS = $(COMPILE_OPTS) CPP = cpp CPLUSPLUS_COMPILER = c++ CPLUSPLUS_FLAGS = $(COMPILE_OPTS) -Wall -DBSD=1 -Wno-deprecated -std=C++17 -DNO_STD_LIB OBJ = o LINK = c++ -o LINK_OPTS = -L. CONSOLE_LINK_OPTS = $(LINK_OPTS) LIBRARY_LINK = ar cr LIBRARY_LINK_OPTS = LIB_SUFFIX = a LIBS_FOR_CONSOLE_APPLICATION = -lssl -lcrypto LIBS_FOR_GUI_APPLICATION = EXE =
- Run make command
make
- Install to system
sudo make install
How to install OpenCV
-
Run command :
sudo apt-get install libopencv-dev
Build and connect to your project
Typical commands to build RtspServerLive555 library:
cd RtspServerLive555
mkdir build
cd build
cmake ..
make
If you want connect RtspServerLive555 library to your CMake project as source code you can make follow. For example, if your repository has structure:
CMakeLists.txt
src
CMakeList.txt
yourLib.h
yourLib.cpp
Create 3rdparty folder in your repository and copy RtspServerLive555 repository folder there. New structure of your repository:
CMakeLists.txt
src
CMakeList.txt
yourLib.h
yourLib.cpp
3rdparty
RtspServerLive555
Create CMakeLists.txt file in 3rdparty folder. CMakeLists.txt should contain:
cmake_minimum_required(VERSION 3.13)
################################################################################
## 3RD-PARTY
## dependencies for the project
################################################################################
project(3rdparty LANGUAGES CXX)
################################################################################
## SETTINGS
## basic 3rd-party settings before use
################################################################################
# To inherit the top-level architecture when the project is used as a submodule.
SET(PARENT ${PARENT}_YOUR_PROJECT_3RDPARTY)
# Disable self-overwriting of parameters inside included subdirectories.
SET(${PARENT}_SUBMODULE_CACHE_OVERWRITE OFF CACHE BOOL "" FORCE)
################################################################################
## CONFIGURATION
## 3rd-party submodules configuration
################################################################################
SET(${PARENT}_SUBMODULE_RTSP_SERVER_LIVE555 ON CACHE BOOL "" FORCE)
if (${PARENT}_SUBMODULE_RTSP_SERVER_LIVE555)
SET(${PARENT}_RTSP_SERVER_LIVE555 ON CACHE BOOL "" FORCE)
SET(${PARENT}_RTSP_SERVER_LIVE555_TEST OFF CACHE BOOL "" FORCE)
SET(${PARENT}_RTSP_SERVER_LIVE555_EXAMPLES OFF CACHE BOOL "" FORCE)
endif()
################################################################################
## INCLUDING SUBDIRECTORIES
## Adding subdirectories according to the 3rd-party configuration
################################################################################
if (${PARENT}_SUBMODULE_RTSP_SERVER_LIVE555)
add_subdirectory(RtspServerLive555)
endif()
File 3rdparty/CMakeLists.txt adds folder RtspServerLive555 to your project and excludes test application from compiling (by default test application is excluded from compiling if RtspServerLive555 is included as sub-repository). The new structure of your repository:
CMakeLists.txt
src
CMakeList.txt
yourLib.h
yourLib.cpp
3rdparty
CMakeLists.txt
RtspServerLive555
Next you need include folder 3rdparty in main CMakeLists.txt file of your repository. Add string at the end of your main CMakeLists.txt:
add_subdirectory(3rdparty)
Next you need to add following part in main CMakeLists.txt file of your repository in order to include Live555 library properly. This part should be located before add_subdirectory(3rdparty) in main CMakeLists.txt.
if(EXISTS /usr/include/liveMedia)
include_directories(/usr/include/BasicUsageEnvironment)
include_directories(/usr/include/groupsock)
include_directories(/usr/include/UsageEnvironment)
include_directories(/usr/include/liveMedia)
set(live555PackageInstallation 1)
add_definitions(-Dlive555PackageInstallation=${live555PackageInstallation})
else()
include_directories(/usr/local/include/BasicUsageEnvironment)
include_directories(/usr/local/include/groupsock)
include_directories(/usr/local/include/UsageEnvironment)
include_directories(/usr/local/include/liveMedia)
endif()
Next you have to include RtspServerLive555 library in your src/CMakeLists.txt file:
target_link_libraries(${PROJECT_NAME} RtspServerLive555)
Done!