javascript 解析 RTP 包

432 阅读1分钟

1. 图示 RTP 包结构

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|X|  CC   |M|     PT      |       sequence number         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           timestamp                           |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           synchronization source (SSRC) identifier            |
+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
|            contributing source (CSRC) identifiers             |
|                             ....                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

2. 正常解析流程 (RFC 3550 规范)

按照图示,解析

V: version,版本号

P: padding,填充

X: extension,扩展标志

CC: count of csrc, csrc 的数量

M: marker,标志位

PT: payloadType,负载类型

Sequence Number: 序列号

TimeStamp: 时间戳

SSRC: synchronization source identifier,同步资源标识符

3. 特殊处理 CC 和 CSRC (RFC 3350 规范)

CC 代表 SSRC 后面有多少个 CSRC 字段

4. 特殊处理 X (RFC 8285 规范)

X 如果为 1,代表 CSRC(如果有)或者 SSRC 后面紧跟着的扩展数据。

4.1 扩展数据头部图示

0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+
|  ID   |  len  |
+-+-+-+-+-+-+-+-+

4.2 One Byte Header

4.3 Two Byte Header

6. 代码参考

github.com/pion/rtp/bl…

5. 参考

RFC 3350 RTP: A Transport Protocol for Real-Time Application

RFC 8285 A General Mechanism for RTP Header Extensions