RTSP RTP RTCP協議解析

一.RTP 會話類型 html

二.軟件架構android

  • RTP 組播方式同步播放方案基於兩個RTP組件 android端的live555 RTP服務端和SPK端Gstream Rtpmanage RTP client。服務器

  • Live555 RTP 服務端包括RTSP服務器和RTP&RTCP 服務(RTSP 服務器沒有使用RTCP實現QoS,只實現了RTSP client live 檢測功能和SR 包解析和RR SS 的發送組播),RTP client 端實現了 GstRtpSession, GstRtpSsrcDemux, GstRtpJitterBuffer and GstRtpPtDemux.總體架構圖見下圖:網絡

3、協議概述session

RTSP架構

Real Time Streaming Protocol或者RTSP(實時流媒體協議),是由Real network 和
Netscape共同提出的如何有效地在IP網絡上傳輸流媒體數據的應用層協議。RTSP提供一
種可擴展的框架,使可以提供能控制的,按需傳輸實時數據,好比音頻和視頻文件。rtsp對流媒體提供了諸如暫停,快進等控制,而它自己並不傳輸數據,rtsp做用至關於流媒體服務器的遠程控制。傳輸數據能夠經過傳輸層的tcp,udp協議。 app


RTSP消息格式:
RTSP的消息有兩大類,一是請求消息(request),一是迴應消息(response),兩種消息的格式不一樣.框架


請求消息:
方法 URI RTSP版本 CR LF
消息頭 CR LF CR LF 
消息體 CR LF
其中方法包括OPTION迴應中全部的命令,URI是接受方的地址,例如
rtsp://192.168.20.136
RTSP版本通常都是 RTSP/1.0.每行後面的CR LF表示回車換行,須要接受端有相應的解
析,最後一個消息頭須要有兩個CR LF tcp


迴應消息:
RTSP版本 狀態碼 解釋 CR LF
消息頭 CR LF CR LF
消息體 CR LF
其中RTSP版本通常都是RTSP/1.0,狀態碼是一個數值,200表示成功,解釋是與狀態碼對應
的文本解釋.ide

4、rtsp交互過程抓包分析
C表示rtsp客戶端,S表示rtsp服務端

1. C->S:OPTION request //詢問S有哪些方法可用
       S->C:OPTION response //S迴應信息中包括提供的全部可用方法

 2. C->S:DESCRIBE request //要求獲得S提供的媒體初始化描述信息
        S->C:DESCRIBE response //S迴應媒體初始化描述信息,主要是sdp

 

 

 

4.C->S:PLAY request //C請求播放
  S->C:PLAY response //S迴應該請求的信息

S->C:發送流媒體數據
5.C->S:TEARDOWN request //C請求關閉會話
  S->C:TEARDOWN response //S迴應該請求

上述的過程是標準的、友好的rtsp流程,但實際的需求中並不必定循序漸進來。
其中第3和4步是必需的!第一步,只要服務器客戶端約定好,有哪些方法可用,則
option請求能夠不要。第二步,若是咱們有其餘途徑獲得媒體初始化描述信息(好比
http請求等等),則咱們也不須要經過rtsp中的describe請求來完成。第五步,能夠根
據系統需求的設計來決定是否須要。

 5、RTSP States

   RTSP controls a stream which may be sent via a separate protocol,

   independent of the control channel. For example, RTSP control may

   occur on a TCP connection while the data flows via UDP. Thus, data

   delivery continues even if no RTSP requests are received by the media

   server. Also, during its lifetime, a single media stream may be

   controlled by RTSP requests issued sequentially on different TCP

   connections. Therefore, the server needs to maintain "session state"

   to be able to correlate RTSP requests with a stream. The state

   transitions are described in Section A.

 

   Many methods in RTSP do not contribute to state. However, the

   following play a central role in defining the allocation and usage of

   stream resources on the server: SETUP, PLAY, RECORD, PAUSE, and

   TEARDOWN.

 

   SETUP:

          Causes the server to allocate resources for a stream and start

          an RTSP session.

 

   PLAY and RECORD:

          Starts data transmission on a stream allocated via SETUP.

   PAUSE:

          Temporarily halts a stream without freeing server resources.

   TEARDOWN:

          Frees resources associated with the stream. The RTSP session

          ceases to exist on the server.

  RTSP methods that contribute to state use the Session header

  field (Section 12.37) to identify the RTSP session whose state

  is being manipulated. The server generates session identifiers

  in response to SETUP requests (Section 10.4).


6、rtsp中經常使用方法:
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:///
e=admin@
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等等


ps: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 = * (Phone 號碼) 
c = * (鏈接信息) 
b = * (帶寬信息) 
z = * (時間區域調整) 
k = * (加密密鑰) 
a = * (0 個或多個會話屬性行) 
時間描述:
t = (會話活動時間) 
r = * (0或屢次重複次數) 
媒體描述:
m = (媒體名稱和傳輸地址) 
i = * (媒體標題) 
c = * (鏈接信息 — 若是包含在會話層則該字段可選) 
b = * (帶寬信息) 
k = * (加密密鑰) 
a = * (0 個或多個媒體屬性行)

 

Status-Code  =     "100"      ; Continue

                |     "200"      ; OK

                |     "201"      ; Created

                |     "250"      ; Low on Storage Space

                |     "300"      ; Multiple Choices

                |     "301"      ; Moved Permanently

                |     "302"      ; Moved Temporarily

                |     "303"      ; See Other

                |     "304"      ; Not Modified

                |     "305"      ; Use Proxy

                |     "400"      ; Bad Request

                |     "401"      ; Unauthorized

                |     "402"      ; Payment Required

                |     "403"      ; Forbidden

                |     "404"      ; Not Found

                |     "405"      ; Method Not Allowed

                |     "406"      ; Not Acceptable

                |     "407"      ; Proxy Authentication Required

                |     "408"      ; Request Time-out

                |     "410"      ; Gone

                |     "411"      ; Length Required

                |     "412"      ; Precondition Failed

                |     "413"      ; Request Entity Too Large

                |     "414"      ; Request-URI Too Large

                |     "415"      ; Unsupported Media Type

                |     "451"      ; Parameter Not Understood

                |     "452"      ; Conference Not Found

                |     "453"      ; Not Enough Bandwidth

                |     "454"      ; Session Not Found

                |     "455"      ; Method Not Valid in This State

                |     "456"      ; Header Field Not Valid for Resource

                |     "457"      ; Invalid Range

                |     "458"      ; Parameter Is Read-Only

                |     "459"      ; Aggregate operation not allowed

                |     "460"      ; Only aggregate operation allowed

                |     "461"      ; Unsupported transport

                |     "462"      ; Destination unreachable

                |     "500"      ; Internal Server Error

                |     "501"      ; Not Implemented

                |     "502"      ; Bad Gateway

                |     "503"      ; Service Unavailable

                |     "504"      ; Gateway Time-out

                |     "505"      ; RTSP Version not supported

                |     "551"      ; Option not supported

                |     extension-code


參考文章:rfc2326(rtsp);rfc2327(sdp)

相關文章
相關標籤/搜索