How to use live555 open source library to capture video data from camera and save files?

Posted by Lord Brar on Tue, 21 May 2019 23:34:30 +0200

** Brief Introduction to live555**
live555 is a cross-platform C++ open source project that provides solutions for streaming media. It implements support for standard streaming media transmission protocols (such as RTP/RTCP, RTSP, SIP, etc.). At the same time, live555 supports streaming, receiving and processing of various audio and video coding formats (including MPEG, H.263+, DV, JPEG, etc.). At present, live555 has been used to implement streaming media playback functions of many players, such as VLC, MPlayer, MediaPortal, ivrworx and so on.

Compilation and Generation of live555 Library Files
live555 Open Source Project website is: http://www.live555.com/liveMedia/, click on the text in the red area of the following figure to access the code download page:

After clicking "here", the following page appears. Click "live555-latest.tar.gz" to download the latest code:

Copy the downloaded code to the virtual machine and compile the file as shown in the following figure:

The specific commands used are as follows:

Step 1:. / genMakefiles linux-64bit (the number of Linux bits is determined by the number of system bits you actually use)

Step 2: make (this step generates executable files for each static library and test code)

Step 3: make install (at this point, install the static library files into the / usr/local/lib directory and the executable files into the / usr/local/bin directory)

After generating static library files and executable files, you can run the executable files in the / usr/local/bin directory.

** Introduction to live555 Library Files**
live555 contains four basic libraries: libBasic Usage Environment.a, libgroupsock.a, libliveMedia.a and libUsage Environment.a. Among them, libBasicUsageEnvironment.a library contains basic functional classes for the whole system; libUsageEnvironment.a library is used for event scheduling, to achieve asynchronous access to event handle settings and error information output; libgroupsock.a library is the encapsulation of network interface for sending and receiving data packets, which is mainly for multicast data, but also supports unicast data. There are a series of classes in the libliveMedia.a library, which are based on different streaming media types and codes.

For more details on live 555, please refer to: http://blog.csdn.net/leixiaohua1020/article/details/12235615.

** live555 Video File Generation Process Introduction**
The video file saves the video stream of RTSP. A simple interactive process of RTSP message is as follows (C means RTSP client, S means RTSP server):

1.C - > S: OPTION request // Ask S what methods are available
1.S - > C: OPTION response //S response information includes all available methods provided
2.C-> S: DESCRIBE request // Request for media initialization description information provided by S
2.S-> C: DESCRIBE response //S response media initialization description information, mainly sdp
3.C - > S: SETUP request // Set session attributes and transport mode to remind S to set up
word
3.S - > C: SETUP response //S establishes a session, returns a session identifier, and session-related information
4.C-> S: PLAY request//C request playback
4.S - > C: PLAY response //S information in response to the request
S-> C: Sending Streaming Media Data
5.C-> S: TEARDOWN request //C request to close the session
5.S - > C: TEARDOWN response //S responds to the request

The host computer program written by the author uses RTSP protocol to capture video stream from Haikangwei video camera and save it to local file (mp4 format). The overall flow of the message is as follows:

Note that after the establishment of TCP connection, we need to transfer RTSP messages through HTTP port (80), because the standard port of RTSP is 554, but due to various security policy configurations such as firewalls, the client may have limitations in accessing port 554, so it can not normally transmit RTSP messages, but HTTP port (80 port) is generally open, so RTS is allowed. The idea of passing P messages through port 80 is RTSP-Over-HTTP.

The log style generated by the live 555 video file generator is as follows:

Created new TCP socket 3 for connection
Connecting to 192.168.4.60, port 80 on socket 3...
...remote connection opened
Requesting RTSP-over-HTTP tunneling (on port 80)

Sending request: GET /h264/ch01/main/av_stream HTTP/1.1
CSeq: 1
User-Agent: openRTSP (LIVE555 Streaming Media v2017.09.12)
Host: 192.168.4.60
x-sessioncookie: 26bd2785b22af60ca36f09d
Accept: application/x-rtsp-tunnelled
Pragma: no-cache
Cache-Control: no-cache


Received 63 new bytes of response data.
Received a complete GET response:
HTTP/1.0 200 OK
Content-Type: application/x-rtsp-tunnelled


Connecting to 192.168.4.60, port 80 on socket 4...
...remote connection opened
Sending request: POST /h264/ch01/main/av_stream HTTP/1.1
CSeq: 1
User-Agent: openRTSP (LIVE555 Streaming Media v2017.09.12)
Host: 192.168.4.60
x-sessioncookie: 26bd2785b22af60ca36f09d
Content-Type: application/x-rtsp-tunnelled
Pragma: no-cache
Cache-Control: no-cache
Content-Length: 32767
Expires: Sun, 9 Jan 1972 00:00:00 GMT


Sending request: OPTIONS rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream RTSP/1.0
CSeq: 2
User-Agent: openRTSP (LIVE555 Streaming Media v2017.09.12)


    The request was base-64 encoded to: T1BUSU9OUyBydHNwOi8vYWRtaW46YWRtaW4xMjNAMTkyLjE2OC40LjYwOjU1NC9oMjY0L2NoMDEvbWFpbi9hdl9zdHJlYW0gUlRTUC8xLjANCkNTZXE6IDINClVzZXItQWdlbnQ6IG9wZW5SVFNQIChMSVZFNTU1IFN0cmVhbWluZyBNZWRpYSB2MjAxNy4wOS4xMikNCg0K

Received 153 new bytes of response data.
Received a complete OPTIONS response:
RTSP/1.0 200 OK
CSeq: 2
Public: OPTIONS, DESCRIBE, PLAY, PAUSE, SETUP, TEARDOWN, SET_PARAMETER, GET_PARAMETER
Date:  Tue, Sep 26 2017 13:37:03 GMT


Sending request: DESCRIBE rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream RTSP/1.0
CSeq: 3
User-Agent: openRTSP (LIVE555 Streaming Media v2017.09.12)
Accept: application/sdp


    The request was base-64 encoded to: REVTQ1JJQkUgcnRzcDovL2FkbWluOmFkbWluMTIzQDE5Mi4xNjguNC42MDo1NTQvaDI2NC9jaDAxL21haW4vYXZfc3RyZWFtIFJUU1AvMS4wDQpDU2VxOiAzDQpVc2VyLUFnZW50OiBvcGVuUlRTUCAoTElWRTU1NSBTdHJlYW1pbmcgTWVkaWEgdjIwMTcuMDkuMTIpDQpBY2NlcHQ6IGFwcGxpY2F0aW9uL3NkcA0KDQo=

Received 733 new bytes of response data.
Received a complete DESCRIBE response:
RTSP/1.0 200 OK
CSeq: 3
Content-Type: application/sdp
Content-Base: rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/
Content-Length: 573

v=0
o=- 1506433023437648 1506433023437648 IN IP4 192.168.4.60
s=Media Presentation
e=NONE
b=AS:5050
t=0 0
a=control:rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/
m=video 0 RTP/AVP 96
b=AS:5000
a=control:rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/trackID=1
a=rtpmap:96 H264/90000
a=fmtp:96 profile-level-id=420029; packetization-mode=1; sprop-parameter-sets=Z0KAMouVAEABg0IAAA4QAAK/IQg=,aN44gA==
a=Media_header:MEDIAINFO=494D4B48010100000400000100000000000000000000000000000000000000000000000000000000;
a=appversion:1.0

Opened URL "rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream", returning a SDP description:
v=0
o=- 1506433023437648 1506433023437648 IN IP4 192.168.4.60
s=Media Presentation
e=NONE
b=AS:5050
t=0 0
a=control:rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/
m=video 0 RTP/AVP 96
b=AS:5000
a=control:rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/trackID=1
a=rtpmap:96 H264/90000
a=fmtp:96 profile-level-id=420029; packetization-mode=1; sprop-parameter-sets=Z0KAMouVAEABg0IAAA4QAAK/IQg=,aN44gA==
a=Media_header:MEDIAINFO=494D4B48010100000400000100000000000000000000000000000000000000000000000000000000;
a=appversion:1.0

Created receiver for "video/H264" subsession (client ports 39434-39435)
Changed socket receive buffer size for the "video/H264" subsession from 212992 to 425984 bytes
Sending request: SETUP rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/trackID=1 RTSP/1.0
CSeq: 4
User-Agent: openRTSP (LIVE555 Streaming Media v2017.09.12)
Transport: RTP/AVP/TCP;unicast;interleaved=0-1


    The request was base-64 encoded to: U0VUVVAgcnRzcDovL2FkbWluOmFkbWluMTIzQDE5Mi4xNjguNC42MDo1NTQvaDI2NC9jaDAxL21haW4vYXZfc3RyZWFtL3RyYWNrSUQ9MSBSVFNQLzEuMA0KQ1NlcTogNA0KVXNlci1BZ2VudDogb3BlblJUU1AgKExJVkU1NTUgU3RyZWFtaW5nIE1lZGlhIHYyMDE3LjA5LjEyKQ0KVHJhbnNwb3J0OiBSVFAvQVZQL1RDUDt1bmljYXN0O2ludGVybGVhdmVkPTAtMQ0KDQo=

Received 178 new bytes of response data.
Received a complete SETUP response:
RTSP/1.0 200 OK
CSeq: 4
Session:       2103693098;timeout=60
Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=4fdd3575;mode="play"
Date:  Tue, Sep 26 2017 13:37:03 GMT


Setup "video/H264" subsession (client ports 39434-39435)
Outputting to the file: "/usr/local/DaTang/Video/Video20170926133712.mp4"
Sending request: PLAY rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/ RTSP/1.0
CSeq: 5
User-Agent: openRTSP (LIVE555 Streaming Media v2017.09.12)
Session: 2103693098
Range: npt=0.000-


    The request was base-64 encoded to: UExBWSBydHNwOi8vYWRtaW46YWRtaW4xMjNAMTkyLjE2OC40LjYwOjU1NC9oMjY0L2NoMDEvbWFpbi9hdl9zdHJlYW0vIFJUU1AvMS4wDQpDU2VxOiA1DQpVc2VyLUFnZW50OiBvcGVuUlRTUCAoTElWRTU1NSBTdHJlYW1pbmcgTWVkaWEgdjIwMTcuMDkuMTIpDQpTZXNzaW9uOiAyMTAzNjkzMDk4DQpSYW5nZTogbnB0PTAuMDAwLQ0KDQo=

Received a complete PLAY response:
RTSP/1.0 200 OK
CSeq: 5
Session:       2103693098
RTP-Info: url=rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream/trackID=1;seq=43500;rtptime=280383108
Date:  Tue, Sep 26 2017 13:37:03 GMT


Started playing session
Receiving streamed data (signal with "kill -HUP 3652" or "kill -USR1 3652" to terminate)...

** Command Style**
If you use the test program provided by live555 to generate video files, you can refer to the openRTSP.cpp and playCommon.cpp files in the testProgs directory of live directory. The executable files they generate are called openRTSP, which can be used to generate mp4 files in the current directory:. / openRTSP-T 80-b 200000-B 2250000-f 25-w 720-h 480-r-4-P 300-v. "Rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream", where -T 80 represents the transmission of RTSP messages over 80 ports of HTTP, -b 200000 means the setting of file-sink-buffer-size to 20000, -B 2250000 means the input-socket-buffer-size to 225000, -f 25 means the number of frames per second for video streams, and-w 720-h 480 means the width of video streams." Degree 720, height 480, - R for receiving and saving video streams, - 4 for generating video files in mp4 format, - P 300 for each video file length of 300 seconds, - V for generating only video files,'rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream'is the URL for acquiring video streams from camera RTSP.

In 64-bit Ubuntu system, by modifying the openRTSP.cpp and playCommon.cpp files, C++ functions can be invoked by C code. The directory and file name can be generated by the configuration file, and then moved to a specific directory after the file is generated.

summary
live555 is an open source project written in C++ program. It supports TCP, HTTP, RTSP and other protocols. It is very suitable for video data acquisition and playback. It has been successfully applied to many video players.

Before applying live555 to PC programs, we need to understand the interaction process of RTSP messages and the format of RTSP messages. Through the author's previous test and verification, it has been realized to capture video stream from Haikangwei Video Camera through live555 and save it to mp4 file. At the same time, the recording time, frame number and storage directory of each file can be set.

Topics: Session socket Linux network