live555简介
live555是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议(如RTP/RTCP、RTSP、SIP等)的支持。同时,live555实现了对多种音视频编码格式(包括MPEG、H.263+、DV、JPEG等)的流化、接收和处理等的支持。目前,live555已经被用于多款播放器的流媒体播放功能的实现,如VLC、MPlayer、MediaPortal、ivrworx等。
live555库文件的编译生成
live555开源项目网址是:http://www.live555.com/liveMedia/,点击如下图红色区域内的文字即可进入代码下载页面:
点击“here”之后,出现了如下页面,点击“live555-latest.tar.gz”下载最新代码:
将下载之后的代码拷贝到虚拟机上,并按照如下图所示的方法对文件进行编译:
具体使用的命令如下:
第一步:./genMakefiles linux-64bit (linux的位数要根据自己实际所使用的系统位数来确定)
第二步:make (这一步生成各个静态库和测试代码的可执行文件)
第三步:make install (此时将静态库文件安装到了/usr/local/lib目录下,将可执行文件安装到了/usr/local/bin目录下)
生成静态库文件和可执行文件之后,大家便可以进入/usr/local/bin目录运行各个可执行文件。
live555库文件简介
live555包含四个基本的库,分别是:libBasicUsageEnvironment.a,libgroupsock.a,libliveMedia.a和libUsageEnvironment.a。其中,libBasicUsageEnvironment.a库包含的是用于整个系统的基础功能类;libUsageEnvironment.a库用于事件的调度,实现异步读取事件的句柄的设置以及错误信息的输出;libgroupsock.a库是对网络接口的封装,用于收发数据包,它主要是面向多播数据的收发,同时也支持单播数据的收发;libliveMedia.a库中有一系列类,这些类针对不同的流媒体类型和编码。
有关live555的更多详细介绍请参考:http://blog.csdn.net/leixiaohua1020/article/details/12235615。
live555视频文件生成流程介绍
视频文件中保存的是RTSP的视频流,一个简单的RTSP消息交互过程如下(C 表示RTSP客户端,S 表示RTSP服务端):
1.C->S:OPTION request //询问S 有哪些方法可用
1.S->C:OPTION response //S 回应信息中包括提供的所有可用方法
2.C->S:DESCRIBE request //要求得到S 提供的媒体初始化描述信息
2.S->C:DESCRIBE response //S 回应媒体初始化描述信息,主要是sdp
3.C->S:SETUP request //设置会话的属性,以及传输模式,提醒S 建立会
话
3.S->C:SETUP response //S 建立会话,返回会话标识符,以及会话相关信息
4.C->S:PLAY request //C 请求播放
4.S->C:PLAY response //S 回应该请求的信息
S->C:发送流媒体数据
5.C->S:TEARDOWN request //C 请求关闭会话
5.S->C:TEARDOWN response //S 回应该请求
笔者编写的上位机程序要利用RTSP协议从海康威视摄像机中获取视频流并保存到本地文件(mp4格式)中,其消息总体流程如下图所示:
注意,在建立了TCP连接之后,我们要让RTSP报文通过HTTP端口(80)传输,因为RTSP的标准端口是554,但是由于各种不同的防火墙等安全策略配置的原因,客户端在访问554端口时可能存在限制,从而无法正常传输RTSP报文,但是HTTP端口(80端口)是普遍开放的,于是就有了让RTSP报文通过80端口透传的想法,即RTSP-Over-HTTP。
live555视频文件生成程序所产生的日志样式如下所示:
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)...
命令样式
如果利用live555提供的测试程序来生成视频文件,那么就要大家就可以参考live目录下的testProgs目录下的openRTSP.cpp和playCommon.cpp文件,它们生成的可执行文件名为openRTSP,可用此命令来在当前目录下生成mp4文件:./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”,其中,-T 80表示通过HTTP的80端口来传输RTSP报文,-b 200000表示将file-sink-buffer-size设置为200000,-B 2250000表示输入socket的大小(input-socket-buffer-size)设置为2250000,-f 25表示视频流每秒的帧数为25,-w 720 -h 480表示视频的宽度为720、高度为480,-r为接收并保存视频流,-4表示生成mp4格式的视频文件,-P 300表示每个视频文件的长度为300秒,-v表示只生成视频文件,“rtsp://admin:admin123@192.168.4.60:554/h264/ch01/main/av_stream”是获取摄像机RTSP视频流的URL。
在64位Ubuntu系统中,通过对openRTSP.cpp和playCommon.cpp文件进行修改,可利用C代码调用C++函数,可配置文件生成目录及生成文件名,并在文件生成好之后搬移到特定的目录中。
总结
live555是一个用C++程序编写的开源项目,支持TCP、HTTP、RTSP等协议,非常适合用于对视频数据的采集和播放,已经被成功应用到很多的视频播放器中。
在将live555应用到上位机程序之前,我们需要了解RTSP消息的交互流程、RTSP 消息格式等。通过笔者前期的测试验证,已实现通过live555从海康威视摄像机中采集视频流并保存到mp4文件中,同时可设置每个文件的录制时长、帧数、存放目录等。