视频技术系列(2):基于UDP和TCP的RTSP推流

对于视频直播流传输来讲,RTMP和RTSP是常用的2种上行传输方式(这里暂时不讨论基于HTTP的Living方式),通常熟悉的应用方式是RTMP推流,RTSP拉流。但实际上,RTSP也是可以方便实现推流的,对于IPC而言,使用RTSP推流在不增加设备开发工作量的同时,可以方便快捷的解决NAT穿越问题,因此有必要详细介绍下RTSP推流的方法。


RTSP推流的方法在协议中有清晰的规定,主要是使用到了OPTIONS 、ANNOUNCE、SETUP、RECORD几个方法:

  • 发送options方法
  • 发送announce方法:这个里面包含了sdp
  • 进行setup
  • 进行record:这个是和下行播放不同,下行播放是play
  • 最后就可以发送rtp的流了

下面使用easydarwin作为server,把完整的RTSP推流报文用实例说明一下:


OPTIONS rtsp://xxx.xxx.xxx.xxx:554/udp.sdp RTSP/1.0
CSeq: 1
User-Agent: Lavf56.23.105

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 1
Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, OPTIONS, ANNOUNCE, RECORD


ANNOUNCE rtsp://xxx.xxx.xxx.xxx:554/udp.sdp RTSP/1.0
Content-Type: application/sdp
CSeq: 2
User-Agent: Lavf56.23.105
Content-Length: 511
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 xxx.xxx.xxx.xxx
t=0 0
a=tool:libavformat 56.23.105
m=video 0 RTP/AVP 96
b=AS:200
a=rtpmap:96 MP4V-ES/90000
a=fmtp:96 profile-level-id=1; config=000001B001000001B58913000001000000012000C48D8AEE0528045A1443000001B24C61766335362E32362E313030
a=control:streamid=0
m=audio 0 RTP/AVP 97
b=AS:128
a=rtpmap:97 MPEG4-GENERIC/48000/2
a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=1190
a=control:streamid=1

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 2


SETUP rtsp://xxx.xxx.xxx.xxx:554/udp.sdp/streamid=0 RTSP/1.0
Transport: RTP/AVP/UDP;unicast;client_port=8328-8329;mode=record
CSeq: 3
User-Agent: Lavf56.23.105

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 3
Cache-Control: no-cache
Session: 7960110611306097900
Date: Tue, 28 Jul 2015 10:48:34 GMT
Expires: Tue, 28 Jul 2015 10:48:34 GMT
Transport: RTP/AVP/UDP;unicast;mode=record;source=xxx.xxx.xxx.xxx;client_port=17600-17601;server_port=6976-6977


SETUP rtsp://xxx.xxx.xxx.xxx:554/udp.sdp/streamid=1 RTSP/1.0
Transport: RTP/AVP/UDP;unicast;client_port=8330-8331;mode=record
CSeq: 4
User-Agent: Lavf56.23.105
Session: 7960110611306097900

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 4
Session: 7960110611306097900
Cache-Control: no-cache
Date: Tue, 28 Jul 2015 10:48:34 GMT
Expires: Tue, 28 Jul 2015 10:48:34 GMT
Transport: RTP/AVP/UDP;unicast;mode=record;source=xxx.xxx.xxx.xxx;client_port=55560-55561;server_port=6978-6979


RECORD rtsp://xxx.xxx.xxx.xxx:554/udp.sdp RTSP/1.0
Range: npt=0.000-
CSeq: 5
User-Agent: Lavf56.23.105
Session: 7960110611306097900

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 5
Session: 7960110611306097900
RTP-Info: url=rtsp://xxx.xxx.xxx.xxx:554/udp.sdp/streamid=0,url=rtsp://xxx.xxx.xxx.xxx:554/udp.sdp/streamid=1

在上面,可以看到协商的sdp中,传输使用了“RTP/AVP/UDP”。RTSP也可以基于TCP进行推流,下面给出TCP传输的实例:


OPTIONS rtsp://xxx.xxx.xxx.xxx:554/tcp.sdp RTSP/1.0
CSeq: 1
User-Agent: Lavf56.23.105

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 1
Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, OPTIONS, ANNOUNCE, RECORD


ANNOUNCE rtsp://xxx.xxx.xxx.xxx:554/tcp.sdp RTSP/1.0
Content-Type: application/sdp
CSeq: 2
User-Agent: Lavf56.23.105
Content-Length: 511
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 xxx.xxx.xxx.xxx
t=0 0
a=tool:libavformat 56.23.105
m=video 0 RTP/AVP 96
b=AS:200
a=rtpmap:96 MP4V-ES/90000
a=fmtp:96 profile-level-id=1; config=000001B001000001B58913000001000000012000C48D8AEE0528045A1443000001B24C61766335362E32362E313030
a=control:streamid=0
m=audio 0 RTP/AVP 97
b=AS:128
a=rtpmap:97 MPEG4-GENERIC/48000/2
a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=1190
a=control:streamid=1

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 2


SETUP rtsp://xxx.xxx.xxx.xxx:554/tcp.sdp/streamid=0 RTSP/1.0
Transport: RTP/AVP/TCP;unicast;interleaved=0-1;mode=record
CSeq: 3
User-Agent: Lavf56.23.105

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 3
Cache-Control: no-cache
Session: 962502261437537563
Date: Tue, 28 Jul 2015 10:45:49 GMT
Expires: Tue, 28 Jul 2015 10:45:49 GMT
Transport: RTP/AVP/TCP;unicast;mode=record;interleaved=0-1


SETUP rtsp://xxx.xxx.xxx.xxx:554/tcp.sdp/streamid=1 RTSP/1.0
Transport: RTP/AVP/TCP;unicast;interleaved=2-3;mode=record
CSeq: 4
User-Agent: Lavf56.23.105
Session: 962502261437537563

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 4
Session: 962502261437537563
Cache-Control: no-cache
Date: Tue, 28 Jul 2015 10:45:49 GMT
Expires: Tue, 28 Jul 2015 10:45:49 GMT
Transport: RTP/AVP/TCP;unicast;mode=record;interleaved=2-3


RECORD rtsp://xxx.xxx.xxx.xxx:554/tcp.sdp RTSP/1.0
Range: npt=0.000-
CSeq: 5
User-Agent: Lavf56.23.105
Session: 962502261437537563

RTSP/1.0 200 OK
Server: DSS/7.0.0 (Build/15.0310; Platform/Linux; Release/EasyDarwin; State/Development; )
Cseq: 5
Session: 962502261437537563
RTP-Info: url=rtsp://xxx.xxx.xxx.xxx:554/tcp.sdp/streamid=0,url=rtsp://xxx.xxx.xxx.xxx:554/tcp.sdp/streamid=1

视频技术系列(2):基于UDP和TCP的RTSP推流 by GM,[email protected]
欢迎转载,请注明原文链接:http://blog.csdn.net/gmer1/article/details/53064582

你可能感兴趣的:(视频)