对于视频直播流传输来讲,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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
在上面,可以看到协商的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