Use Qt+Live555 to build RTSP server

1. Project background

With the continuous development of Internet of Things technology, video surveillance systems are increasingly used in various fields. Among them, RTSP (Real Time Streaming Protocol) is a commonly used streaming media transmission protocol that can realize the transmission and playback of real-time audio and video data. In order to realize the networking and intelligence of the video surveillance system, it is necessary to develop a video streaming server based on the RTSP protocol, which can receive the video stream from the front-end device and provide services of the RTSP protocol to facilitate the client’s real-time video browsing, playback and other operations. .

During the development process, in order to improve development efficiency, reduce development difficulty and cost, and have good scalability and maintainability, I chose to use Qt and Live555 libraries to build the RTSP server. Qt is a cross-platform C++ application development framework with complete GUI interface design tools and rich functional modules, which can greatly simplify the development process; Live555 is a cross-platform streaming media development library that supports a variety of streaming media Protocols, including RTSP, SIP, RTP, etc., can help us quickly realize the transmission and processing of video streams.

This project will mainly implement the following functions:

  1. Realize the construction of RTSP server in Qt + Live555 environment, supporting the transmission and playback of multiple video streams.
  2. The GUI interface design based on Qt makes it convenient for users to configure and manage.
  3. Realize the adaptability of video encoding formats and support common video encoding formats such as H.264 and H.265.
  4. Enables encryption and decryption of video streams and supports RTSP over HTTPS secure communication protocol.
  5. Implement basic user rights management and logging functions.

2. Introduction to RTSP

The RTSP server is a server that provides streaming media services. It uses the RTSP protocol to communicate with the client and supports the transmission and control of audio and video data. RTSP (Real-Time Streaming Protocol) real-time streaming protocol is an application layer protocol that transmits data through TCP or UDP and is used to realize real-time transmission of multimedia data.

RTSP servers are mainly used in application scenarios such as live streaming, on-demand, and video recording, allowing users to watch videos, listen to audio, etc. in real time over the network. RTSP servers generally have the following functions:

  1. Realize the transmission and control of streaming media data, including establishing connections, transmitting media data, pausing playback, fast forwarding and rewinding, etc.;
  2. Supports multiple codec formats and media container formats, such as H.264, MPEG-4, AAC, MP3, etc.;
  3. Supports multiple network transmission protocols, such as UDP, TCP, HTTP, HTTPS, etc.;
  4. Supports multiple security authentication methods, such as username and password authentication, digital certificate authentication, etc.;
  5. Supports extended functions such as real-time transcoding, load balancing, cluster deployment, etc.

Common RTSP server software includes Live555, Wowza Media Server, Darwin Streaming Server, etc. Using RTSP servers, network-based streaming media services can be easily implemented to meet the needs of application scenarios such as live broadcast, video conferencing, and remote monitoring.

3. Introduction to Live555 library

The Live555 library is an open source multimedia streaming service framework that provides a series of C++ classes and library functions for developing streaming applications based on standard network protocols. This library is mainly used to implement standard protocols such as RTP/RTCP, RTSP, SIP and SDP, and can easily implement functions such as network transmission, playback, recording and transcoding of video/audio.

The Live555 library has the following features:

  1. Applicable to various platforms and operating systems, including Windows, Linux, Mac OS X, etc.;
  2. Support common video and audio formats, such as H.264, MPEG-4, MP3, AAC, etc.;
  3. Supports multiple network transmission protocols such as UDP, TCP, HTTP, HTTPS;
  4. Support IPv4 and IPv6 dual-stack network environment;
  5. Functionality can be extended through the plug-in mechanism.

Using the Live555 library for development, you can quickly build network-based streaming media applications. For example, you can use this library to implement RTSP servers or clients to implement application scenarios such as live video broadcast, remote monitoring, and video conferencing. At the same time, the Live555 library can also be used as a basic component of other streaming media server software, such as: Wowza Media Server, Darwin Streaming Server, etc.

4. Implementation process

To build an RTSP server in Qt, you can use the open source Live555 library. Live555 is a cross-platform, multimedia development library written in C++ language. It supports common protocols such as RTSP and SIP, and has complete server and client implementations.

The following are the steps to implement a simple RTSP server based on the Live555 library in Qt:

【1】Download and install the Live555 library. Download the latest version of the library from the official website (www.live555.com/liveMedia/#…).

and install it according to the documentation instructions. Add the path to the Live555 library to Qt Creator’s project configuration file.

【2】Create a Qt console application. Create an empty console application in Qt Creator and add the link option for the Live555 library in the project’s .pro file, for example:

 LIBS + = -path to LLive555 library -lliveMedia -lgroupsock -lUsageEnvironment -lBasicUsageEnvironment

【3】Write RTSP server code. Create a class that inherits from the RTSPServer class in the live555 library and implements the corresponding virtual functions, createNewSession() and deleteStream().

【4】Start RTSP server. Create an RTSP server object in the main() function and call the start() function to start the server, as shown below:

int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
     
     RTSPServer* server = new MyRTSPServer();
     server->start();
     
     return a.exec();
 }

【5】Test. Use an RTSP client tool (such as VLC player) to connect to the local RTSP server and play the video stream.

5. Implementation code

The following is a core code example of using Qt + Live555 to build an RTSP server:

#include <liveMedia.hh>
 #include <BasicUsageEnvironment.hh>
 ?
 class VideoStreamSource : public FramedSource {
 public:
   static VideoStreamSource* createNew(UsageEnvironment & amp; env) {
     return new VideoStreamSource(env);
   }
   virtual void doGetNextFrame() {
     //Copy the video data frame to fTo and set fFrameSize and fNumTruncatedBytes, then call the afterGetting() function to notify that the video frame is available.
     if (condition1 & amp; & amp; condition2) {
         memcpy(fTo, fVideoFrame, fVideoFrameSize);
         afterGetting(this);
     } else {
         handleClosure(this);
     }
   }
 protected:
   VideoStreamSource(UsageEnvironment & amp; env) : FramedSource(env) {
     //Initialize some variables
   }
   virtual ~VideoStreamSource() {}
 private:
   // Some member variables
   char* fVideoFrame;
   unsigned fVideoFrameSize;
 };
 ?
 class MyRTSPServer : public RTSPServer {
 public:
   static MyRTSPServer* createNew(UsageEnvironment & amp; env, Port ourPort) {
     return new MyRTSPServer(env, ourPort);
   }
 protected:
   MyRTSPServer(UsageEnvironment & amp; env, Port ourPort)
     : RTSPServer(env, ourPort, NULL) {}
   virtual ~MyRTSPServer() {}
   virtual ServerMediaSession* lookupSession(char const* streamName, Boolean isFirstLookup) {
     ServerMediaSession* session = RTSPServer::lookupSession(streamName, isFirstLookup);
     if (session == NULL) {
       // Create a new session to support the video stream requested by the RTSP client
       session = ServerMediaSession::createNew(envir(), streamName);
       //Add video frames to the session
       VideoStreamSource* videoSource = VideoStreamSource::createNew(envir());
       session->addSubsession(MPEG4VideoStreamDiscreteFramer::createNew(envir(), videoSource, false));
       addServerMediaSession(session);
     }
     return session;
   }
 };
 ?
 int main(int argc, char *argv[]) {
   //Create a QT application instance
   QCoreApplication app(argc, argv);
 ?
   //Create an RTSP server instance and listen to port 9090
   MyRTSPServer* rtspServer = MyRTSPServer::createNew(*(app.instance()), 9090);
   if (rtspServer == NULL) {
     qDebug() << "Failed to create RTSP server: " << env.getResultMsg() << endl;
     exit(1);
   }
 ?
   //Start the Qt event loop
   return app.exec();
 }

The above code implements the following functions:

  • Created a VideoStreamSource class to obtain video data frames and encapsulate them into FramedSource objects.
  • Created a MyRTSPServer class that inherits from RTSPServer and overridden the lookupSession() function to create and add new video streaming sessions to RTSP in the server.
  • In the main() function, a Qt application instance and an RTSP server instance are created, and the Qt event loop is started.

Original text Using Qt + Live555 to build RTSP server – Nuggets

On the business card at the end of the article, you can get free audio and video development learning materials, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmap, etc.

See below! ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓