RTSP RTP RTCP协议解析

一.RTP 会话类型

225049_5dgp_258930.jpg

二.软件架构

  • 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.整体架构图见下图:

    193959_Fy8H_258930.png

三、协议概述

RTSP

Real Time Streaming Protocol或者RTSP(实时流媒体协议),是由Real network 和
Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议。RTSP提供一
种可扩展的框架,使能够提供能控制的,按需传输实时数据,比如音频和视频文件。rtsp对流媒体提供了诸如暂停,快进等控制,而它本身并不传输数据,rtsp作用相当于流媒体服务器的远程控制。传输数据可以通过传输层的tcp,udp协议。 


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 


回应消息:
RTSP版本 状态码 解释 CR LF
消息头 CR LF CR LF
消息体 CR LF
其中RTSP版本一般都是RTSP/1.0,状态码是一个数值,200表示成功,解释是与状态码对应
的文本解释.

四、rtsp交互过程抓包分析
C表示rtsp客户端,S表示rtsp服务端

194356_NKtK_258930.png

1. C->S:OPTION request //询问S有哪些方法可用
       S->C:OPTION response //S回应信息中包括提供的所有可用方法

 2. C->S:DESCRIBE request //要求得到S提供的媒体初始化描述信息
        S->C:DESCRIBE response //S回应媒体初始化描述信息,主要是sdp

 

194558_OeaN_258930.png

194558_QYds_258930.png

 

 

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请求来完成。第五步,可以根
据系统需求的设计来决定是否需要。

 五、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).


六、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)

 

 


 


 

转载于:https://my.oschina.net/u/258930/blog/603852

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值