Rtsp协议包含两块,第一部分是信令交互,这一部分建立在tcp协议的基础之上,另一部分是流媒体数据,rtp传输,既可以建立在tcp协议基础之上,也可以建立在udp协议的基础之上。
本章主要讲解一下rtsp信令这一部分的交互流程。
第一步OPTIONS,查看服务支持哪些命令
请求:
OPTIONS rtsp://192.168.150.129:8554/H264 RTSP/1.0
CSeq: 2
User-Agent: LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)
应答:
RTSP/1.0 200 OK
Server: VLC/3.0.3
Content-Length: 0
Cseq: 2
Public: DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE,GET_PARAMETER
第二步DESCRIBE,查看服务端sdp支持情况
请求:
DESCRIBE rtsp://192.168.150.129:8554/H264 RTSP/1.0
CSeq: 3
User-Agent: LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
应答
RTSP/1.0 200 OK
Server: VLC/3.0.3
Date: Sat, 01 Sep 2018 04:38:33 GMT
Content-Type: application/sdp
Content-Base: rtsp://192.168.150.129:8554/H264
Content-Length: 578
Cache-Control: no-cache
Cseq: 3
v=0
o=- 16083645413441119698 16083645413441119698 IN IP4 WIN-NJUKDS19FLI
s=Unnamed
i=N/A
c=IN IP4 0.0.0.0
t=0 0
a=tool:vlc 3.0.3
a=recvonly
a=type:broadcast
a=charset:UTF-8
a=control:rtsp://192.168.150.129:8554/H264
m=audio 0 RTP/AVP 14
b=AS:128
b=RR:0
a=rtpmap:14 MPA/90000/2
a=control:rtsp://192.168.150.129:8554/H264/trackID=0
m=video 0 RTP/AVP 96
b=RR:0
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6zZQFAFuwEQAAADABAAAAMDKPGDGWA=,aOvjyyLA;
a=control:rtsp://192.168.150.129:8554/H264/trackID=1
这里的重点:
m=audio 0 RTP/AVP 14
b=AS:128
b=RR:0
a=rtpmap:14 MPA/90000/2
a=control:rtsp://192.168.150.129:8554/H264/trackID=0
说明服务端支持音频方式,类型MPA 采样率90000 通道数2 对于的rtp的pt 填写值为14 RTP/AVP说支持rtp协议,UDP方式。
最后的a=control:rtsp://192.168.150.129:8554/H264/trackID=0 是在setup命令中需要使用的。
m=video 0 RTP/AVP 96
b=RR:0
a=rtpmap:96 H264/90000
a=fmtp:96packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6zZQFAFuwEQAAADABAAAAMDKPGDGWA=,aOvjyyLA;
a=control:rtsp://192.168.150.129:8554/H264/trackID=1
说明服务端支持视频方式,类型为H264,采样率90000 ,pt值为96,最后的a页视setup命令中需要使用的。
第三步SETUP,
启动媒体请求 UDP方式
请求音频数据
SETUP rtsp://192.168.150.129:8554/H264/trackID=0 RTSP/1.0
CSeq: 5
User-Agent: LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP;unicast;client_port=55220-55221
transport里面的RTP/AVP说明是UDP方式发送媒体流,client_port=55220-55221,表示流媒体接收端口55220,55221为rtcp接受端口,
setup 后面的url为前面sdp应答的音频的的url
答音频请求
RTSP/1.0 200 OK
Server: VLC/3.0.3
Date: Sat, 01 Sep 2018 04:38:34 GMT
Transport: RTP/AVP/UDP;unicast;client_port=55220-55221;server_port=54235-54236;ssrc=68D48D51;mode=play
Session: 81d2286eeee8dc56;timeout=60
Content-Length: 0
Cache-Control: no-cache
Cseq: 5
transport里面的server_port=54235-54236 标识发送媒体流的端口为54235 发送rtcp端口为54236,
session为本次会话的标识,后面的play 和setup 都需要带有这字段,ssrc为rtp包里面ssrc的值
视频流程一样
请求视频数据
SETUP rtsp://192.168.150.129:8554/H264/trackID=1 RTSP/1.0
CSeq: 7
User-Agent: LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP;unicast;client_port=55222-55223
Session: 81d2286eeee8dc56
应答视频请求:
RTSP/1.0 200 OK
Server: VLC/3.0.3
Date: Sat, 01 Sep 2018 04:38:34 GMT
Transport: RTP/AVP/UDP;unicast;client_port=55222-55223;server_port=54236-54237;ssrc=C470E2F0;mode=play
Session: 81d2286eeee8dc56;timeout=60
Content-Length: 0
Cache-Control: no-cache
Cseq: 7
启动媒体请求 TCP方式:
请求视频:
SETUP rtsp://10.10.14.168:554/user=admin_password=tlJwpbo6_channel=1_stream=0.sdp/trackID=2?real_stream RTSP/1.0
CSeq: 7
Authorization: Digest username="admin", realm="IP Camera(C3459)", nonce="7c4e0298a2d0f792ed11e284ee0e2300", uri="rtsp://10.10.14.168:554/user=admin_password=tlJwpbo6_channel=1_stream=0.sdp/", response="5a987d357fb858e636f765e37dd6b0a3"
User-Agent: LibVLC/2.1.0 (LIVE555 Streaming Media v2011.12.23)
Transport: RTP/AVP/TCP;unicast;interleaved=0-1
RTSP/1.0 200 OK
CSeq: 7
Session: 1931661604;timeout=60
Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=1855e319;mode="play"
Date: Tue, Sep 04 2018 14:45:00 GMT
请求音频:
SETUP rtsp://10.10.14.168:554/Streaming/Channels/101/trackID=2 RTSP/1.0
CSeq: 8
Authorization: Digest username="admin", realm="IP Camera(C3459)", nonce="9a7fb020dcf4be47cc43bf1f0ecf0cc0", uri="rtsp://10.10.14.168:554/Streaming/Channels/101/", response="34e71f4a77e64a15b65e5ef5304cc759"
User-Agent: LibVLC/2.2.1 (LIVE555 Streaming Media v2014.07.25)
Transport: RTP/AVP/TCP;unicast;interleaved=2-3
Session: 1641687743
RTSP/1.0 200 OK
CSeq: 8
Session: 1641687743;timeout=60
Transport: RTP/AVP/TCP;unicast;interleaved=2-3;ssrc=27ebd3e5;mode="play"
Date: Tue, Sep 04 2018 14:50:59 GMT
第四步PLAY,让后台开始发送媒体流
请求:
PLAY rtsp://192.168.150.129:8554/H264 RTSP/1.0
CSeq: 8
User-Agent: LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)
Session: 81d2286eeee8dc56
Range: npt=0.000-
应答
RTSP/1.0 200 OK
Server: VLC/3.0.3
Date: Sat, 01 Sep 2018 04:38:34 GMT
RTP-Info: url=rtsp://192.168.150.129:8554/H264/trackID=0;seq=44225;rtptime=26995590, url=rtsp://192.168.150.129:8554/H264/trackID=1;seq=48797;rtptime=26995590
Range: npt=123.437000-
Session: 81d2286eeee8dc56;timeout=60
Content-Length: 0
Cache-Control: no-cache
Cseq: 8
************大华的应答居然是这样子的*************************
PLAY rtsp://10.10.14.162:554/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif RTSP/1.0
Session: 234447372275
CSeq: 5
RTSP/1.0 200 OK
Server: Rtsp Server/2.0
CSeq: 5
Session: 234447372275
RTP-Info: url=trackID=0;seq=59454;rtptime=1291528915;ssrc=48b42fb1,url=trackID=1;seq=6089;rtptime=165528883;ssrc=1ca16dac
Range: npt=0.000000-
注意看RTP-Info的url没有写请求的url,而是直接写了一个trackId,日了,这里的解析需要再优化一下,不能拿Url去匹配。
第五步,TEARDOWN 结束回话
请求:
TEARDOWN rtsp://192.168.150.129:8554/H264 RTSP/1.0
CSeq: 9
User-Agent: LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)
Session: 81d2286eeee8dc56
应答:
RTSP/1.0 200 OK
Server: VLC/3.0.3
Date: Sat, 01 Sep 2018 04:38:41 GMT
Session: 81d2286eeee8dc56;timeout=60
Content-Length: 0
Cache-Control: no-cache
Cseq: 9