如果想要完全理解rtsp相关的技术, 需要阅读以下文档资料:
rtsp协议 https://tools.ietf.org/html/rfc2326
sdp协议 https://tools.ietf.org/html/rfc2327
rtp/rtcp协议 https://tools.ietf.org/html/rfc3550
rtp H.264数据格式封装 https://tools.ietf.org/html/rfc6184
rtp/sdp中aac/mpeg4的相关协议 https://tools.ietf.org/html/rfc3640
rtp/sdp中G.711相关协议 https://tools.ietf.org/html/rfc5391
RTSP客户端工具vlc: https://www.videolan.org/
RTSP服务端工具: 考虑到搭建rtsp服务器很费事, 可以使用vlc的串流功能
本机的网络抓包工具: https://www.netresec.com/?page=RawCap
客户端发送
OPTIONS rtsp://192.168.2.232:8456/test RTSP/1.0
CSeq: 2
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
服务器回复
RTSP/1.0 200 OK
CSeq: 2
Content-Length: 0
Cache-Control: no-cache
Public: DESCRIBE,SETUP,TEARDOWN,PLAY
VLC发送
DESCRIBE rtsp://192.168.2.232:8456/test RTSP/1.0
CSeq: 3
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
method: describe
服务端回复
RTSP/1.0 200 OK
CSeq: 3
Accept: application/sdp
Cache-Control: no-cache
Content-Length: 522
v=0
o=- 1546226325720 1546226325720 IN IP4 Eques
s=Unnamed
i=N/A
c=IN IP4 0.0.0.0
t=0 0
a=recvonly
a=type:broadcast
a=control:rtsp://192.168.2.232:8456/test/
m=audio 0 RTP/AVP 98
a=rtpmap:98 PCMA/22050/1
a=fmtp:98 complaw=al;
a=control:rtsp://192.168.2.232:8456/test/trackID=0
m=video 0 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=42c014;sprop-parameter-sets=Z0LAFKtAoP0IAAADAAgAAAMBRwIABkAAAMgBaxsDxQqo,KM48gA==;
a=control:rtsp://192.168.2.232:8456/test/trackID=1
因为有音频和视频两路流, 所以需要setup两次
SETUP rtsp://192.168.2.232:8456/test/trackID=0 RTSP/1.0
CSeq: 4
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP/TCP;unicast;interleaved=0-1
method: setup
handle setup request
RTSP/1.0 200 OK
CSeq: 4
Cache-Control: no-cache
Content-Length: 0
Session: uYu2RlouWJBByDn1;timeout=60
Transport: RTP/AVP/TCP;unicast;interleaved=0-1
SETUP rtsp://192.168.2.232:8456/test/trackID=1 RTSP/1.0
CSeq: 5
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP/TCP;unicast;interleaved=2-3
Session: uYu2RlouWJBByDn1
method: setup
handle setup request
RTSP/1.0 200 OK
CSeq: 5
Cache-Control: no-cache
Content-Length: 0
Session: uYu2RlouWJBByDn1;timeout=60
Transport: RTP/AVP/TCP;unicast;interleaved=2-3
PLAY rtsp://192.168.2.232:8456/test/ RTSP/1.0
CSeq: 6
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Session: uYu2RlouWJBByDn1
Range: npt=0.000-
method: play
RTSP/1.0 200 OK
CSeq: 6
Content-Length: 0
Cache-Control: no-cache
Session: uYu2RlouWJBByDn1;timeout=60
RTP-Info: url=rtsp://192.168.2.232:8456/test/trackID=0;seq=0;rtptime=0,url=rtsp://192.168.2.232:8456/test/trackID=1;seq=0;rtptime=0
Range: npt=0.000-
TEARDOWN rtsp://192.168.2.232:8456/test/ RTSP/1.0
CSeq: 7
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Session: uYu2RlouWJBByDn1
文档在 https://tools.ietf.org/html/rfc2326#page-40
音视频数据添加RTP头之后, 还要再添加4个字节头, 它的格式为
0 1 2 3
----------------------------------------------------------------------------------
| $ (美元符号) | channel id | rtp包大小(2字节, 网络字节序)
----------------------------------------------------------------------------------
rtsp的基本流程还是一样的, 只是部分参数的差别
DESCRIBE rtsp://192.168.2.232:8456/test RTSP/1.0
CSeq: 3
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
RTSP/1.0 200 OK
CSeq: 3
Accept: application/sdp
Cache-Control: no-cache
Content-Length: 635
v=0
o=- 1546233621391 1546233621391 IN IP4 Eques
s=Unnamed
i=N/A
c=IN IP4 0.0.0.0
t=0 0
a=recvonly
a=type:broadcast
a=control:rtsp://192.168.2.232:8456/test/
m=audio 0 RTP/AVP 97
a=rtpmap:97 mpeg4-generic/22050/1
a=fmtp:97 mode=AAC-hbr;streamtype=5;IndexDeltaLength=3;profile-level-id=15;config=0b88;SizeLength=13;IndexLength=3;Profile=1;
a=control:rtsp://192.168.2.232:8456/test/trackID=0
m=video 0 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1;profile-level-id=42c014;sprop-parameter-sets=Z0LAFKtAoP0IAAADAAgAAAMBRwIABkAAAMgBaxsDxQqo,KM48gA==;
a=control:rtsp://192.168.2.232:8456/test/trackID=1
SETUP rtsp://192.168.2.232:8456/test/trackID=0 RTSP/1.0
CSeq: 4
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP;unicast;client_port=52122-52123
method: setup
RTSP/1.0 200 OK
CSeq: 4
Cache-Control: no-cache
Content-Length: 0
Session: gixEa5aMd6gd3ZWZ;timeout=60
Transport: RTP/AVP;unicast;client_port=52122-52123;server_port=33697-33698
SETUP rtsp://192.168.2.232:8456/test/trackID=1 RTSP/1.0
CSeq: 5
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP;unicast;client_port=52124-52125
Session: gixEa5aMd6gd3ZWZ
RTSP/1.0 200 OK
CSeq: 5
Cache-Control: no-cache
Content-Length: 0
Session: gixEa5aMd6gd3ZWZ;timeout=60
Transport: RTP/AVP;unicast;client_port=52124-52125;server_port=51830-51831
PLAY rtsp://192.168.2.232:8456/test/ RTSP/1.0
CSeq: 6
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Session: gixEa5aMd6gd3ZWZ
Range: npt=0.000-
RTSP/1.0 200 OK
CSeq: 6
Content-Length: 0
Cache-Control: no-cache
Session: gixEa5aMd6gd3ZWZ;timeout=60
RTP-Info: url=rtsp://192.168.2.232:8456/test/trackID=0;seq=0;rtptime=0,url=rtsp://192.168.2.232:8456/test/trackID=1;seq=0;rtptime=0
Range: npt=0.000-