
RTP包解析笔记文章目录RTP包解析笔记RTP包头结构RTP over TCPTCP负载RTP包RTCP包头结构SRSender Report RTCP Packet发送端报告 PT200RR (Receiver Report RTCP Packet) PT201RTP包头结构引用自RFC3550#section-5.1The RTP header has the following format: 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 -------------------------------- |V2|P|X| CC |M| PT | sequence number | -------------------------------- | timestamp | -------------------------------- | synchronization source (SSRC) identifier | | contributing source (CSRC) identifiers | | .... | --------------------------------VVersion2位RTP版本号当前标准版本为2V2。PPadding1位填充标志置1时表示包末尾包含填充字节。XExtension1位扩展标志置1时表示固定包头后紧跟一个扩展包头。CCCSRC Count4位贡献源CSRC数量指示CSRC标识符的个数0~15。MMarker1位标记位置1时通常用于标记帧结束。PTPayload Type7位负载类型指示RTP负载的编码格式。Sequence Number16位序列号每发送一个RTP包递增1用于接收端检测丢包、排序。Timestamp32位时间戳反映负载数据的采样时刻采样率由负载类型决定用于接收端同步。SSRCSynchronization Source32位同步源标识符唯一标识一个RTP流的发送端。CSRCContributing Source32位/个贡献源标识符最多15个用于标识对当前负载有贡献的发送端如混音场景。GB28181的PS流通常没有CSRC, 即 CC 0b0000所以GB28181的RTP包头通常是12个字节逐层 即字节12位V 1位P 1位X 4位CC字节21位M 7位CC字节3-4Seq 序号字节5-8Ts 时间戳字节9-12Ssrc 源标识GB28181流的PayloadType通常是96即PS流RTP包头前两个字节通常为1帧结束RTP包0b10(V) 0b0(P) 0b0(X) 0b0000(CC) 0b1(M) 0b1100000(PT)即0b 1000 0000 1110 0000 0x80e02非帧结束RTP包0b10(V) 0b0(P) 0b0(X) 0b0000(CC) 0b0(M) 0b1100000(PT)即0b 1000 0000 1110 0000 0x8060RTP over TCPTCP负载RTP包引用自RFC4571#section-2Figure 1 defines the framing method. 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 --------------------------------------------------------------- | LENGTH | RTP or RTCP packet ... | ---------------------------------------------------------------LENGTH16位无符号整数网络字节序大端表示后续RTP/RTCP包的总长度单位字节不含自身2字节长度字段。RTP/RTCP packet完整的RTP或RTCP包包含RTP固定包头、扩展包头若有及负载数据遵循RFC 3550标准。RTCP包头结构SRSender Report RTCP Packet发送端报告 PT200发送端报告用于媒体发送端汇报发送数据量、时间戳、同步信息适配主动发送媒体的节点。引用自RFC3550#section-6.4.10 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 |V2|P| RC | PTSR200 | length | -------------------------------- | SSRC of sender | sender | NTP timestamp, most significant word | info -------------------------------- | NTP timestamp, least significant word | -------------------------------- | RTP timestamp | -------------------------------- | senders packet count | -------------------------------- | senders 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 (Receiver Report RTCP Packet) PT201引用自RFC3550#section-6.4.20 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 |V2|P| RC | PTRR201 | 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 | --------------------------------