流媒體之rtsp篇

rtsp協議簡介

老規矩,百科下咯。

RTSP(Real Time Streaming Protocol),RFC2326,實時流傳輸協議,是TCP/IP協議體系中的一個應用層協議,由哥倫比亞大學、網景和RealNetworks公司提交的IETF RFC標準。該協議定義了一對多應用程式如何有效地通過IP網路傳送多媒體資料。RTSP在體系結構上位於RTP和RTCP之上,它使用TCP或UDP完成資料傳輸。

太囉嗦了,RFC2326也看不下去,還是直接抓包分析吧

抓包分析

1、開啟wireshark抓tcp包,根據rtsp過濾;
2、開啟IPC的web頁,開始預覽,停止預覽,可以看到wireshark已經抓到了我們想要的東西;
rtsp
3、在wireshark的RTSP包上右鍵Follow TCP Stream即可得到下面一段rtsp對答

DESCRIBE rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi RTSP/1.0
CSeq: 1
Accept: application/sdp
Session: 1495700359
RTSP/1.0 401 Unauthorized
CSeq: 1
Thu, May 25 2017 08:21:29 GMT
WWW-Authenticate: Digest realm="Keda Streaming Media", nonce="ecb2ddc1ddef1851c055af808ad9f0fb"
DESCRIBE rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi RTSP/1.0
CSeq: 2
Accept: application/sdp
Authorization: Digest username="admin", realm="Keda Streaming Media", nonce="ecb2ddc1ddef1851c055af808ad9f0fb", uri="rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi", response="92edf3ade54179369eb807ef371aff8f"
RTSP/1.0 200 OK
CSeq: 2
Content-Type: application/sdp
Content-Length: 325
v=0
o=NVR 1495700489 1495700489 IN IP4 0.0.0.0
s=3GPP Unicast SDP 
c=IN IP4 0.0.0.0
t=0 0
a=range:npt=0-
a=control:*
m=video 0 RTP/AVP 96
a=control:video_0
a=rtpmap:96 H264/90000
a=fmtp:96 profile-level-id=;sprop-parameter-sets=;packetization-mode=0
m=audio 0 RTP/AVP 8
a=control:audio_0
a=rtpmap:8 PCMA/8000
SETUP rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi/video_0 RTSP/1.0
CSeq: 3
Transport: RTP/AVP;unicast;destination=172.16.192.60;client_port=60000-60001
RTSP/1.0 200 OK
CSeq: 3
Session: 1495700489 ;timeout=60
Transport: RTP/AVP;unicast;mode=play;client_port=60000-60001;server_port=0-0
Cache-Control: no-cache
SETUP rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi/audio_0 RTSP/1.0
CSeq: 4
Session: 1495700489 ;timeout=60
Transport: RTP/AVP;unicast;destination=172.16.192.60;client_port=60002-60003
RTSP/1.0 200 OK
CSeq: 4
Session: 1495700489 ;timeout=60
Transport: RTP/AVP;unicast;mode=play;client_port=60002-60003;server_port=0-0
Cache-Control: no-cache
PLAY rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi RTSP/1.0
CSeq: 5
Range: npt=0.000-
Session: 1495700489 ;timeout=60
RTSP/1.0 200 OK
CSeq: 5
Session: 1495700489
RTP-Info: url=video/id=0;seq=0;rtptime=0,url=audio/id=0;seq=0;rtptime=0
Range: npt=0.000-
Date: Thu, May 25 2017 08:21:29 GMT
GET_PARAMETER rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi RTSP/1.0
CSeq: 6
Content-Length: 0
Content-Type: text/parameters
Session: 1495700489
TitleDisplay: OFF
TitleAttrib: White, 0,0
TitleChar: 
RTSP/1.0 200 OK
CSeq: 6
Session: 1495700489
GET_PARAMETER rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi RTSP/1.0
CSeq: 7
Content-Length: 0
Content-Type: text/parameters
Session: 1495700489
TitleDisplay: OFF
TitleAttrib: White, 0,0
TitleChar: 
RTSP/1.0 200 OK
CSeq: 7
Session: 1495700489
TEARDOWN rtsp://172.16.193.152:554/realtime?chnid=1;vid=0;aid=0;agent=cgi RTSP/1.0
CSeq: 8
Session: 1495700489
RTSP/1.0 200 OK
CSeq: 8
Session: 1495700489

一個基本的對答流程是:

Created with Raphaël 2.1.0CCSSDESCRIBE url RTSP/1.0RTSP/1.0 401 UnauthorizedDESCRIBE url RTSP/1.0RTSP/1.0 200 OKSETUP url/video? RTSP/1.0RTSP/1.0 200 OKSETUP url/audio? RTSP/1.0RTSP/1.0 200 OKPLAY url RTSP/1.0RTSP/1.0 200 OK……TEARDOWN url RTSP/1.0RTSP/1.0 200 OK

協議詳解

1.  OPTION 
目的是得到伺服器提供的可用方法:
OPTIONS rtsp://192.168.20.136:5000/xxx666 RTSP/1.0 
CSeq: 1  //每個訊息都有序號來標記,第一個包通常是option請求訊息
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10) 
伺服器的迴應資訊包括提供的一些方法,例如:
RTSP/1.0 200 OK  
Server: UServer 0.9.7_rc1 
Cseq: 1  //每個迴應訊息的cseq數值和請求訊息的cseq相對應
Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, SCALE,GET_PARAMETER  //伺服器提供的可用的方法
2. DESCRIBE  
C向S發起DESCRIBE請求,為了得到會話描述資訊(SDP):
DESCRIBE rtsp://192.168.20.136:5000/xxx666 RTSP/1.0 
CSeq: 2 
token:  
Accept: application/sdp 
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)  
伺服器迴應一些對此會話的描述資訊(sdp):
RTSP/1.0 200 OK  
Server: UServer 0.9.7_rc1  
Cseq: 2  
x-prev-url: rtsp://192.168.20.136:5000  
x-next-url: rtsp://192.168.20.136:5000  
x-Accept-Retransmit: our-retransmit  
x-Accept-Dynamic-Rate: 1  
Cache-Control: must-revalidate  
Last-Modified: Fri, 10 Nov 2006 12:34:38 GMT  
Date: Fri, 10 Nov 2006 12:34:38 GMT  
Expires: Fri, 10 Nov 2006 12:34:38 GMT  
Content-Base: rtsp://192.168.20.136:5000/xxx666/  
Content-Length: 344  
Content-Type: application/sdp  
v=0  //以下都是sdp資訊
o=OnewaveUServerNG 1451516402 1025358037 IN IP4 192.168.20.136  
s=/xxx666  
u=http:///  
[email protected]  
c=IN IP4 0.0.0.0  
t=0 0  
a=isma-compliance:1,1.0,1  
a=range:npt=0-  
m=video 0 RTP/AVP 96  //m表示媒體描述,下面是對會話中視訊通道的媒體描述
a=rtpmap:96 MP4V-ES/90000  
a=fmtp:96 profile-level-id=245;config=000001B0F5000001B509000001000000012000C888B0E0E0FA62D089028307 a=control:trackID=0  //trackID=0表示視訊流用的是通道0
3.SETUP  
客戶端提醒伺服器建立會話,並確定傳輸模式:
SETUP rtsp://192.168.20.136:5000/xxx666/trackID=0 RTSP/1.0  
CSeq: 3  
Transport: RTP/AVP/TCP;unicast;interleaved=0-1  
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10) 
//uri 中 帶有trackID=0,表示對該通道進行設定。Transport引數設定了傳輸模式,包的結構。接下來的資料包頭部第二個位元組位置就是 interleaved,它的值是每個通道都不同的,trackID=0的interleaved值有兩個0或1,0表示rtp包,1表示rtcp包,接 受端根據interleaved的值來區別是哪種資料包。
伺服器迴應資訊:
RTSP/1.0 200 OK  
Server: UServer 0.9.7_rc1  
Cseq: 3  
Session: 6310936469860791894  //伺服器迴應的會話識別符號
Cache-Control: no-cache  
Transport: RTP/AVP/TCP;unicast;interleaved=0-1;ssrc=6B8B4567 
4.PLAY  
客戶端傳送播放請求:
PLAY rtsp://192.168.20.136:5000/xxx666 RTSP/1.0  
CSeq: 4  
Session: 6310936469860791894  
Range: npt=0.000-  //設定播放時間的範圍
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10) 
伺服器迴應資訊:
RTSP/1.0 200 OK  
Server: UServer 0.9.7_rc1  
Cseq: 4  
Session: 6310936469860791894  
Range: npt=0.000000-  
RTP-Info: url=trackID=0;seq=17040;rtptime=1467265309  
//seq和rtptime都是rtp包中的資訊
5.TEARDOWN  
客戶端發起關閉請求:
TEARDOWN rtsp://192.168.20.136:5000/xxx666 RTSP/1.0  
CSeq: 5  
Session: 6310936469860791894  
User-Agent: VLC media player (LIVE555 Streaming Media v2005.11.10)  
伺服器迴應:
RTSP/1.0 200 OK  
Server: UServer 0.9.7_rc1  
Cseq: 5  
Session: 6310936469860791894  
Connection: Close  
以上方法都是互動過程中最為常用的, 其它還有一些重要的方法如get/set_parameter,pause,redirect等等
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - - - - - - - - -
sdp 的格式: 
v=<version>
o=<username> <session id> <version> <network type> <address type> <address>
s=<session name>
i=<session description>
u=<URI>
e=<email address>
p=<phone number>
c=<network type> <address type> <connection address>
b=<modifier>:<bandwidth-value>
t=<start time> <stop time>
r=<repeat interval> <active duration> <list of offsets from start-time>
z=<adjustment time> <offset> <adjustment time> <offset> ....
k=<method>
k=<method>:<encryption key>
a=<attribute>
a=<attribute>:<value>
m=<media> <port> <transport> <fmt list>
v = (協議版本)
o = (所有者/建立者和會話識別符號)
s = (會話名稱)
i = * (會話資訊)
u = * (URI 描述)
e = * (Email 地址)
p = * (電話號碼)
c = * (連線資訊)
b = * (頻寬資訊)
z = * (時間區域調整)
k = * (加密金鑰)
a = * (0 個或多個會話屬性行)
時間描述:
t = (會話活動時間)
r = * (0或多次重複次數)
媒體描述:
m = (媒體名稱和傳輸地址)
i = * (媒體標題)
c = * (連線資訊 — 如果包含在會話層則該欄位可選)
b = * (頻寬資訊)
k = * (加密金鑰)
a = * (0 個或多個媒體屬性行)