RTSP协议

350 阅读7分钟

RTSP协议

简介

RTSP协议即实时流协议Real TIme Streaming Protocol,RTSP)是一种网络应用协议用以控制流媒体服务器。流数据本身的传输不是RTSP的任务,大多数RTSP服务器使用实时传输协议(RTP)和实时传输控制协议(RTCP)结合媒体流传输

RTSP由RealNetworks公司,NetScape公司和哥伦比亚大学开发。

RTSP与HTTP

虽然RTSP协议在某些方面与HTTP类似,RTSP定义了控制多媒体播放控制顺序。HTTP是无状态的,但是RTSP是具有状态的,当需要跟踪会话时使用标识符。RTSP和HTTP一样使用TCP来维护端到端的链接,大多数RTSP控制消息由客户端发送到服务器,一些命令从服务器到客户端传播。RTSP协议使用554端口,http使用80端口。

RTSP消息

RTSP的消息有两大类,一是请求消息,一个是回应消息,两种消息的格式不同。

CR:Carriage Return,对应ASCII中转义字符\r,表示回车

LF:Linefeed,对应ASCII中转义字符\n,表示换行

CRLF:Carriage Return & Linefeed,\r\n,表示回车并换行

请求的消息格式如下:

方法 URI RTSP版本 CR LF

消息头 CR LF CR LF

消息体 CR LF

其中方法包括OPTIONS回应中所有的命令

响应的格式消息如下:

RTSP版本 状态码 解释 CR LF

消息头 CR LF CR LF

消息体 CR LF

状态码

其中RTSP版本一般都是RTSP/1.0,状态码200表示成功,对应的状态码说明如下:

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

RTSP中定义的方法

rtsp中定义的方法有:OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE, SCALE, GET_PARAMETER ,SET_PARAMETER

下面是RTSP的基本请求

(C代表客户端,S代表服务端)

OPTIONS请求, OPTIONS请求返回服务器将接受的请求类型

C->S : 
    OPTIONS rtsp://example.com/media.mp4 RTSP/1.0
    CSeq: 1
    Require: implicit-play
    Proxy-Require: gzipped-messages
S->C:
    RTSP/1.0 200 OK
    CSeq: 1
    Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE

DESCRIBE请求,DESCRIBE请求包括RTSPURL (rstp://)以及可以处理的回复数据类型。该回复包括呈现描述,通常以会话描述协议SDP格式。其中,演示文稿描述列出了使用汇总网址控制的媒体流。在典型的情况下,每个音频和视频都有一个媒体流

C->S: 
    DESCRIBE rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 2
​
S->C: 
    RTSP/1.0 200 OK
      CSeq: 2
      Content-Base: rtsp://example.com/media.mp4
      Content-Type: application/sdp
      Content-Length: 460
​
      m=video 0 RTP/AVP 96
      a=control:streamid=0
      a=range:npt=0-7.741000
      a=length:npt=7.741000
      a=rtpmap:96 MP4V-ES/5544
      a=mimetype:string;"video/MP4V-ES"
      a=AvgBitRate:integer;304018
      a=StreamName:string;"hinted video track"
      m=audio 0 RTP/AVP 97
      a=control:streamid=1
      a=range:npt=0-7.712000
      a=length:npt=7.712000
      a=rtpmap:97 mpeg4-generic/32000/2
      a=mimetype:string;"audio/mpeg4-generic"
      a=AvgBitRate:integer;65790
      a=StreamName:string;"hinted audio track"

SETUP请求,SETUP请求制定如何传输媒体流,必须在发送PLAY请求之前完成。请求包含媒体流URL和传输说明符,该说明符通常包括用于接受RTP数据的本地端口,另一个用于RTCP数据,服务器回复通常会确认所选参数,并填写缺少的部分。必须在发送聚合播放请求之前,使用SETUP配置每个媒体流。

C->S: 
      SETUP rtsp://example.com/media.mp4/streamid=0 RTSP/1.0
      CSeq: 3
      Transport: RTP/AVP;unicast;client_port=8000-8001

S->C: 
      RTSP/1.0 200 OK
      CSeq: 3
      Transport: RTP/AVP;unicast;client_port=8000-8001;server_port=9000-9001;ssrc=1234ABCD
      Session: 12345678

PLAY请求:PLAY播放请求,请求播放一个或者所有媒体流,如果媒体流暂停,则在暂停点恢复播放。

C->S: 
      PLAY rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 4
      Range: npt=5-20
      Session: 12345678

S->C: 
      RTSP/1.0 200 OK
      CSeq: 4
      Session: 12345678
      RTP-Info: url=rtsp://example.com/media.mp4/streamid=0;seq=9810092;rtptime=3450012

PAUSE暂停请求,PAUSE暂停请求暂时停止一个或所有媒体流,通过PLAY请求恢复播放。

C->S: 
      PAUSE rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 5
      Session: 12345678

S->C: 
      RTSP/1.0 200 OK
      CSeq: 5
      Session: 12345678

RECORD记录请求,用于记录一系列媒体数据,时间戳反映开始时间和结束时间。

C->S: 
      RECORD rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 6
      Session: 12345678

S->C: 
      RTSP/1.0 200 OK
      CSeq: 6
      Session: 12345678

ANNOUNCE发布请求,发布方法有两个目的:从客户端发送到服务器时,ANNOUNCE将请求URL标识的演示文稿或媒体对象的描述发布到服务器。当从服务器发送到客户端时,ANNOUNCE会实时更新会话描述。

C->S: 
      ANNOUNCE rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 7
      Date: 23 Jan 1997 15:35:06 GMT
      Session: 12345678
      Content-Type: application/sdp
      Content-Length: 332
​
      v=0
      o=mhandley 2890844526 2890845468 IN IP4 126.16.64.4
      s=SDP Seminar
      i=A Seminar on the session description protocol
      u=http://www.cs.ucl.ac.uk/staff/M.Handley/sdp.03.ps
      e=mjh#isi.edu (Mark Handley)
      c=IN IP4 224.2.17.12/127
      t=2873397496 2873404696
      a=recvonly
      m=audio 3456 RTP/AVP 0
      m=video 2232 RTP/AVP 31
​
S->C: 
      RTSP/1.0 200 OK
      CSeq: 7
​

TEARDOWN 停止发布流请求,该请求用于停止所有媒体流。并释放服务器上所有与会话相关的数据

C->S: 
      TEARDOWN rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 8
      Session: 12345678
​
S->C: 
      RTSP/1.0 200 OK
      CSeq: 8

GET_PARAMETER 获取参数请求,请求检索URI中指定的流的参数。

S->C: 
      GET_PARAMETER rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 9
      Content-Type: text/parameters
      Session: 12345678
      Content-Length: 15

      packets_received
      jitter

C->S: 
      RTSP/1.0 200 OK
      CSeq: 9
      Content-Length: 46
      Content-Type: text/parameters

      packets_received: 10
      jitter: 0.3838

SET_PARAMETER 设置参数请求,请求设置URI指定的流的参数值

C->S: 
      SET_PARAMETER rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 10
      Content-length: 20
      Content-type: text/parameters

      barparam: barstuff

S->C: 
      RTSP/1.0 451 Invalid Parameter
      CSeq: 10
      Content-length: 10
      Content-type: text/parameters

      barparam

REDIRECT 重定向请求,该请求通知客户端必须链接到另一个服务器位置上。

S->C: 
      REDIRECT rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 11
      Location: rtsp://bigserver.com:8001
      Range: clock=19960213T143205Z-

RTSP的重要字段参数

  1. Accept: 用于指定客户端可以接受的媒体描述信息类型。比如: Accept: application/rtsl, application/sdp;level=2
  2. Bandwidth: 用于描述客户端可用的带宽值。
  3. CSeq: 指定了RTSP请求回应对的序列号,在每个请求或回应中都必须包括这个头字段。对每个包含一个给定序列号的请求消息,都会有一个相同序列号的回应消息。
  4. Rang: 用于指定一个时间范围,可以使用SMPTE、NTP或clock时间单元。
  5. Session: Session头字段标识了一个RTSP会话。Session ID 是由服务器在SETUP的回应中选择的,客户端一当得到Session ID后,在以后的对Session 的操作请求消息中都要包含Session ID.
  6. Transport: Transport头字段包含客户端可以接受的转输选项列表,包括传输协议,地址端口,TTL等。服务器端也通过这个头字段返回实际选择的具体选项。如: Transport: RTP/AVP;multicast;ttl=127;mode="PLAY", RTP/AVP;unicast;client_port=3456-3457;mode="PLAY"

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 个或多个媒体属性行)