SDP, RTP, RTCP, RTSP, RTMP 名词解释

读维基百科里的词条,记录的一点笔记。

SDP

会话描述协议 Session Description Protocol

严格来说 SDP 不是一种协议,而是一种格式约定,用于描述流媒体的参数。如协商媒体类型、格式及其它相关属性。这些属性和参数通常叫做 session profile

SDP 被广泛用于 RTP、RTSP、SIP 等协议中。

会话描述

一个会话由一系列域值来描述,每个一行,如

<character>=<value>
  • character 是一个字符,小写和大小意义不同。
  • value 是一串文本,格式取决于属性类型。UTF-8 编码。
  • = 等号两边不能有空白符。

SDP 消息内有三个主要部分:会话、时间、媒体。每个消息都有多个时间和媒体描述。

下述中有 =* 表示可选值。顺序如下所示:

会话描述
    v=  (protocol version number, currently only 0)
    o=  (originator and session identifier : username, id, version number, network address)
    s=  (session name : mandatory with at least one UTF-8-encoded character)
    i=* (session title or short information)
    u=* (URI of description)
    e=* (zero or more email address with optional name of contacts)
    p=* (zero or more phone number with optional name of contacts)
    c=* (connection information—not required if included in all media)
    b=* (zero or more bandwidth information lines)
    One or more Time descriptions ("t=" and "r=" lines; see below)
    z=* (time zone adjustments)
    k=* (encryption key)
    a=* (zero or more session attribute lines)
    Zero or more Media descriptions (each one starting by an "m=" line; see below)
时间描述(必须指定)
    t=  (time the session is active)
    r=* (zero or more repeat times)
媒体描述
    m=  (media name and transport address)
    i=* (media title or information field)
    c=* (connection information — optional if included at session level)
    b=* (zero or more bandwidth information lines)
    k=* (encryption key)
    a=* (zero or more media attribute lines — overriding the Session attribute lines)

RTP

Real-time Transport Protocol 实时传输协议。

应用于流媒体传输,如电话、视频电话应用、视频服务等。

  • RTP 常结合 RTCP 一起使用。RTP 负责传输流媒体(如:音、视频),RTCP 负责监控传输质量,以及多个流的同步。
  • RTP 是 VoIP 的基础设施,在 VoIP 领域经常与另一种会话协议搭配工作,如 SIP 协议。
  • RTP 允许使用 IP 组播技术。
  • 实时多媒体流需要时实性,因而容许包的丢失。例如音频应用中一秒中有一小部分数据丢失,并不影响接听感受。
  • RTP 可以使用 TCP,但由于 TCP 的强可靠性却较少被使用。大部分 RTP 都建立在 UDP 之上。
  • 与 RTP 相关的协议有许多,比如 SIP、Jingle, RTSP, H.225, H.245 等会话控制协议,另外如 H.264, MPEG, H.263 等协议常用来编码数据,作为 RTP 的 payload。

位域 大小 bits 说明
Version 2 RTP 协议版本号,当前版本为 2
P 1 Padding, 指示 RTP 包尾是否有填充的字节。填充字节的最后一个字节保存填充字节数(包括它自己)
X 1 Extension,指示在标准头和 payload data 之间是否有扩展头(Extension header)
CC 4 CSRC 计数器,指示 CSRC 标识符个数
M 1 Marker,标记,应用层使用。指示该 payload 是否有特殊意义。如对于视频,M=1 表示一帧的结束; 对于音频,标记会话的开始。
PT 7 Payload type,携带数据的格式。由 RTP profile 指定。便于应用解析。
Sequence number 16 每发送一个 RTP 包,序列号递增 1. 接收方用序列号来检查包丢失并恢复序列号。包丢失了 RTP 不会作任何处理,交由应用层来处理。RFC3550 规定初始序列号为一个随机数。
Timestamp 32 接收方根据时间戳来在合适的间隔处理接收数据。每个流有独立的时间戳,不能用于媒体同步。时间粒度由应用指定。
SSRC 32 Synchronization source identifier,同步源标识用来标识一个流。同一个 RTP 会话有唯一的同步标识。
CSRC 32 bits each 特约信息源,标识包含在该 RTP 报文中的所有特约信源。可以有 0~15 个。
Header extension optional 头 32 位包含 16 位的 profile-spcific 标识符和 16 位的长度,用于指示扩展头的长度。扩展以 32 bit 对齐。

RTCP

RTP Control Protocol

RTP 控制协议,与 RTP 搭配使用。

RTCP 有五种包:

1. sender report (SR)
2. receiver report (RP)
3. source description (SDES)
4. goodbye (BYE)
5. Application-specific message (APP)
6. 

RTSP

实时流传输协议 Real Time Streaming Protocol

RTSP 并不负责流数据传输。大多数 RTSP 服务使用 RTP/RTCP 来进行流数据传输。

与 HTTP 类似。RTSP 使用 TCP 来进行端到端连接,且大部分 RTCP 控制消息由客户端向服务端发起。
但 HTTP 是无状态的,RTSP 有状态。RTSP 有标识符用于跟踪多个会话。

默认的传输端口是 554,默认使用 TCP 协议。

主要命令:

  • 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 包含一个 RTSP URL,以及允许的返回数据类型。返回数据一般是 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 描述怎样传输一个流。在 PLAY 之前必须发送 SETUP 包,其中包含媒体流的 URL 和端口描述。比如本地用于接收 RTP 数据的端口和 RTCP 数据的端口。服务器的回复一般用于确认这些参数,并描述服务器上选择的端口。
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
      Session: 12345678
  • 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 暂停一个或多个流。暂停后可被 PLAY 命令恢复播放。可指定 range 来表示何时暂停。未指定 range 则立即暂停。
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 依据描述信息记录一段媒体数据。时间戳指定起始和结束时间。如果未指定 range,则使用描述信息里的起始和结束时间。如果会话已经开始,则立即开始记录。由服务器决定是否存储记录数据。如果不使用此命令,则应返回 201 。
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 此方法有两个作用:客户端向服务端发送,将请求的媒体数据的 URL 发给服务器。服务器向客户端发送时,立即更新会话。如果一个新的媒体流加入,则应该发送整个描述信息,而不是增加的部分。
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 指定流的参数值。没有实体时可用来测试客户端和服务器的连通,类似 ping 包。
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 设置参数
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 指示客户端必须要去连接另一个服务地址。其中指定客户端要去连接的 URL。如果客户端还是想连接当前 URI,客户端必须先在当前会话发送 TEARDOWN 和 SETUP 请求。
S->C: REDIRECT rtsp://example.com/media.mp4 RTSP/1.0
      CSeq: 11
      Location: rtsp://bigserver.com:8001
      Range: clock=19960213T143205Z-
  • Embedded (Interleaved) Binary Data

RTMP

实时消息协议 Real Time Messaging Protocol

一种流媒体传输协议,在网络上传输音、视频及数据。由 Macromedia 开发,后该公司被 Adobe 收购。Flash 播放器即使用该协议。

RTMP 有多个变种
* 原始 RTMP 工作于 TCP 上,默认使用端口 1935
* RTMPS 即工作于 TLS/SSL 连接上的 RTMP
* RTMPE Adobe 使用私有机制加密的 RTMP。
* RTMPT 使用 HTTP 封装的 RTMP,可用于穿过防火墙,使用 HTTP 的端口 80 或 443.封装的会话可能携带 RTMP、RTMPS 或 RTMPE 等几种包。

posted @ 2015-12-11 01:37  sammei  阅读(1524)  评论(0编辑  收藏  举报