WebRTC RTCP 分析

0x00 前言

下面是 WebRTC 的 SDP 描述中的 m-line 的一个示例

m=audio 58779 UDP/TLS/RTP/SAVPF 111 103 104 9 0 8 106 105 13 126
m=video 60372 UDP/TLS/RTP/SAVPF 100 101 116 117 96

可以看出 audio 和 video 的传输都是基于 RTP 已经定义的几种 Profile (AVP/SAVP/AVPF/SAVPF) 中最复杂的 SAVPF,这些机制里面最复杂的部分要数 Feedback,而 RTCP 是整个 Feedback 机制实现的重要组成部分(还有一部分可以通过 RTP Header Extention 实现)。本文主要介绍和分析 WebRTC 中与 RTCP 相关的内容。
对于基于 UDP 的 RTP 并不能为按顺序传送数据包提供可靠的机制,也不提供流量控制或拥塞控制,所以需要依靠 RTCP 提供这些服务。通常 RTP 算法并不作为一个独立的网络层来实现,在 RTP 会话期间,通常会配备一些 发现/安全/反馈 机制来保证基于 RTP 的应用完整实现。
在语音通信中 RTCP 主要用于传递状态和发送统计数据。而视频通信中的 RTCP 被赋予了控制功能,比如关键帧请求,重传请求,码率控制等。

0x01 RTCP 介绍

WebRTC 中的 RTCP 常见类型有:

SequenceRepresentSubtype
200SR (Sender Report)-
201RR (Receiver Report)-
202SDES (Source Description)-
203BYE-
204APP-
205RTPFB (Generic RTP Feedback)NACK/TransportFeedback
206PSFB (Payload-Specific Feedback)PLI/FIR/REMB
207XR (Extended Reports)LRLE/DRLE/PRT/RRT/DLRR/SS/VM

更多类型定义可以参考 https://www.ietf.org/assignments/rtp-parameters/rtp-parameters.xml#rtp-parameters-4

RTCP 主要涉及到 3 个 RFC 文档

和下面几个补充文档

  • RFC3551 - RTP Profile for Audio and Video Conferences with Minimal Control
  • RFC5104 - Codec Control Messages in the RTP Audio-Visual Profile with Feedback (AVPF)
  • RFC5124 - Extended Secure RTP Profile for Real-time Transport Control Protocol (RTCP)-Based Feedback (RTP/SAVPF)
  • RFC5450 - Transmission Time Offsets in RTP Streams

0x02 RTCP 类型分析

① SR/RR/SDES/BYE/APP

SR

https://tools.ietf.org/html/rfc3550#section-6.4.1

        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P|    RC   |   PT=SR=200   |             length            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         SSRC of sender                        |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
sender |              NTP timestamp, most significant word             |
info   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |             NTP timestamp, least significant word             |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         RTP timestamp                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                     sender's packet count                     |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      sender's octet count                     |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_1 (SSRC of first source)                 |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1    | fraction lost |       cumulative number of packets lost       |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |           extended highest sequence number received           |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      interarrival jitter                      |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         last SR (LSR)                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                   delay since last SR (DLSR)                  |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_2 (SSRC of second source)                |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2    :                               ...                             :
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
       |                  profile-specific extensions                  |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
RR

https://tools.ietf.org/html/rfc3550#section-6.4.2

        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P|    RC   |   PT=RR=201   |             length            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                     SSRC of packet sender                     |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_1 (SSRC of first source)                 |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1    | fraction lost |       cumulative number of packets lost       |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |           extended highest sequence number received           |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      interarrival jitter                      |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         last SR (LSR)                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                   delay since last SR (DLSR)                  |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_2 (SSRC of second source)                |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2    :                               ...                             :
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
       |                  profile-specific extensions                  |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

SR 和 RR 的唯一区别是 SR 多了 20 字节的 Sender Info,用于描述发送者的信息。

SDES

https://tools.ietf.org/html/rfc3550#section-6.5

        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P|    SC   |  PT=SDES=202  |             length            |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
chunk  |                          SSRC/CSRC_1                          |
  1    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                           SDES items                          |
       |                              ...                              |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
chunk  |                          SSRC/CSRC_2                          |
  2    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                           SDES items                          |
       |                              ...                              |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
BYE

https://tools.ietf.org/html/rfc3550#section-6.6

   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |V=2|P|    SC   |   PT=BYE=203  |             length            |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                           SSRC/CSRC                           |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      :                              ...                              :
      +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
(opt) |     length    |               reason for leaving            ...
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
APP

https://datatracker.ietf.org/doc/html/rfc3550#section-6.7

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |V=2|P| subtype |   PT=APP=204  |             length            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                           SSRC/CSRC                           |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                          name (ASCII)                         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                   application-dependent data                ...
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

② RTPFB/PSFB

常规的 SR/RR 是一种定时报告机制,对于因为事件引起的反馈并不是很友好,例如关键帧丢失。RTPFB/PSFB 提供了这样的保证机制,其中 RTPFB 是在 RTP Stream 层面的 Feedback,PSFB 是 RTP 所携带的负载信息的反馈机制。这种 Feedback 机制主要目的为:关键帧请求、重传请求、码率控制等。
参考 https://www.ietf.org/assignments/rtp-parameters/rtp-parameters.xml,常见 RTPFB/PSFB 类型如下表。

RTPFB

ValueNameLong NameReference
1NACKGeneric negative acknowledgementRFC4585
2ReservedRFC5104
3TMMBRTemporary Maximum Media Stream Bit Rate RequestRFC5104
4TMMBNTemporary Maximum Media Stream Bit Rate NotificationRFC5104
5SR_REQRTCP Rapid Resynchronisation RequestRFC6051
6RAMSRapid Acquisition of Multicast SessionsRFC6285
7TLLEITransport-Layer Third-Party Loss Early IndicationRFC6642
8ECNRTCP ECN FeedbackRFC6679
9PSMedia Pause/ResumeRFC7728
10DBI DelayBudget Information (DBI)[3GPP TS 26.114 v16.3.0][Ozgur_Oyman]
15TCCTransport Wide Congestion Controlhttps://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01
31EXTReserved for future extensionsRFC4585

PSFB

ValueNameLong NameReference
1PLIPicture Loss IndicationRFC4585
2SLISlice Loss IndicationRFC4585
3RPSIReference Picture Selection IndicationRFC4585
4FIRFull Intra Request CommandRFC5104
5TSTRTemporal-Spatial Trade-off RequestRFC5104
6TSTNTemporal-Spatial Trade-off NotificationRFC5104
7VBCMVideo Back Channel MessageRFC5104
8PSLEIPayload-Specific Third-Party Loss Early IndicationRFC6642
9ROIVideo region-of-interest (ROI)[3GPP TS 26.114 v16.3.0][Ozgur_Oyman]
10LRRLayer Refresh Request CommandRFC-ietf-avtext-lrr-07
15AFB(REMB)Application Layer Feedback (Receiver Estimated Maximum Bitrate)https://tools.ietf.org/html/draft-alvestrand-rmcat-remb-03
31EXTReserved for future extensionsRFC4585

另外,除了关键帧请求和重传,Webrtc还支持RED/FEC等冗余编码和前向纠错手段来保证视频质量。

③ XR

RTCP XR 的目的是补充传递 RTCP SR 和 RTCP RR 包含的六个统计信息。

共定义了 7 种类型的 BT (Block Type),见下表。

ValueNameLong NameReference
1LRLELoss RLE (Run-Length Encoding)rfc3611#section-4.1
2DRLEDuplicate RLE (Run-Length Encoding)rfc3611#section-4.2
3PRTPacket Receipt Timesrfc3611#section-4.3
4RRTReceiver Reference Timerfc3611#section-4.4
5DLRRDelay since the Last Receiver Referencerfc3611#section-4.5
6SSStatistics Summaryrfc3611#section-4.6
7VMVoIP Metricsrfc3611#section-4.7

定义的报表块类型分为三类。

  • 第一类包括收到或丢失RTP Packet 的逐包报告。
    • LRLE
    • DRLE
    • PRT
  • 第二类报告在 RTP 参与者之间传递参考时间信息。
    • RRT
    • DLRR
  • 第三类报告传递与包接收相关的度量,这些度量在本质上是摘要,但比现有 RTCP 包中传递的度量更详细,或者具有不同的类型。
    • SS
    • VM

XR 类型 RTCP 的头部信息为:

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |V=2|P|reserved |   PT=XR=207   |             length            |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                              SSRC                             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   :                         report blocks                         :
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

report blocks 格式为:

  0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |      BT       | type-specific |         block length          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   :             type-specific block contents                      :
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

在 WebRTC 中比较常用的为 RRT 和 DLRR,接收⽅发送 RTCP-XR RRT 后,发送⽅在下次的 RTCP 中携带 RTCP-XR DLRR,可以使接收方估算出网络的 RTT 参数。


参考文档

  1. https://www.ietf.org/assignments/rtp-parameters/rtp-parameters.xml
  2. 超越RFC3550 - RTP/RTCP协议族分析
  3. WebRTC之RTCP
  4. WebRTC之RTT计算
  5. 网络抖动(jitter)的计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值