参考
RTP(A Transport Protocol for Real-Time Applications–实时传输协议,rfc3550)
RTP Payload Format for H.264 Video(rfc6184)
https://en.wikipedia.org/wiki/Real-time_Transport_Protocol
RTP包组成:
![image.png](https://img-blog.csdnimg.cn/img_convert/5e64f923f3e5963de96215252c6de55c.png#averageHue=#ed7c79&clientId=ucfdf849f-a63f-4&from=paste&height=51&id=uec45ff36&name=image.png&originHeight=51&originWidth=301&originalType=binary&ratio=1&rotation=0&showTitle=false&size=2848&status=done&style=none&taskId=ub67feac2-25e1-464d-869a-85b50707055&title=&width=301)
头部
RTP头部最小12字节(没有CSRC没有扩展头部)
![image.png](https://img-blog.csdnimg.cn/img_convert/fbe0fcddc011474aecbdcd6307d2bc47.png#averageHue=#eceed1&clientId=ud530656f-ecd5-4&from=paste&height=323&id=udfec0e41&name=image.png&originHeight=323&originWidth=1119&originalType=binary&ratio=1&rotation=0&showTitle=false&size=25433&status=done&style=none&taskId=u9a3a47a3-93e3-42f0-8a5e-5eb3e0c45db&title=&width=1119)
- V (Version): (2位) RTP协议版本号,rfc3550规定是2。
- P (Padding): (1位) 如果设置了padding位,则数据包的末尾包含一个或多个额外的填充字节(八位),它们不是有效负载的一部分。填充的最后一个八位字节包含应忽略的字节数,包括其本身。某些具有固定块大小的加密算法或在较低层协议数据单元中承载多个RTP数据包可能需要填充。
- X (Extension): (1位) 如果设置了扩展位,则RTP头部后面必须紧跟一个扩展头部,格式见第5.3.1节。
- CC (CSRC count): (4位) 表示SSRC后面的CSRC数量。
- M (Marker): (1位) 标记一帧的结束,由负载类型决定含义。当多个RTP包携带1帧数据时,前面的RTP包的marker标志设置为0,最后一个RTP包marker标志设置为1。
- PT (Payload type): (7位) 表示RTP负载类型。可以被动态赋值,例如在RTSP的Describe响应的SDP媒体描述中包含
a=rtpmap:96 H264/90000
行,表明RTP负载类型96对应的是H264。 - Sequence number: (16位) RTP包的序列号,每发送一个RTP包,序列号加1。接收器可使用序列号检测数据包丢失并恢复数据包序列。序列号的初始值应该是随机的(不可预测的),以使对加密的已知明文攻击更加困难。RTSP PLAY响应包括序列号和时间戳的初始值。
- Timestamp: (32位) 时间戳反映RTP数据包中第一个八位字节的采样瞬间。时间戳的初始值应该是随机的。RTP时间戳在RTP同步中是非常重要的,它确保接收方能够以正确的速率和顺序播放媒体数据,并保持与发送方的同步。
- SSRC: (32位) 同步源标识符。应随机选择该标识符,确保同一个RTP会话的两个同步源(音频流和视频流)不会有相同的SSRC标识符。
- CSRC: (32位) 可选的,当CC大于0存在该字段。用于标识当前RTP数据包中参与数据生成和发送的所有同步源(标识参与当前媒体数据采集、编码或发送的所有同步源)。它可以帮助接收方进行同步和混音,并提高RTP流的安全性和可靠性。CSRC的数量由CC字段表示。在一个RTP会话中,每个RTP数据包可以包含多个CSRC值,最多可以包含15个。
- Header extension: 可选的,当设置了扩展标志位,存在该扩展头部。第一个32位包括配置文件特定的标识符(16位)和长度标识符。
![image.png](https://img-blog.csdnimg.cn/img_convert/6dfe79c2c3ca2d5c33c4096b842abcc4.png#averageHue=#f2f2f2&clientId=udd8b2815-8e04-4&from=paste&height=354&id=u3cdf4309&name=image.png&originHeight=354&originWidth=802&originalType=binary&ratio=1&rotation=0&showTitle=false&size=46272&status=done&style=none&taskId=u69d62bb5-0894-449f-86e6-11fa19c5ed1&title=&width=802)
H.264的RTP负载格式
对于单个NALU和非交错分组模式,序列号用于确定NALU的解码顺序。
RTP时间戳设置为内容的采样时间戳。必须使用90 kHz的时钟频率。
负载结构有三种,根据RTP负载的第一个字节的后5位来判断
- Single NAL Unit Packet(单个NAL单元数据包)
- Aggregation Packet(聚合数据包)
- Fragmentation Unit(分片单元)
Single NAL Unit Packet即1个RTP包承载1个NALU
Aggregation Packet即1个RTP包承载多个NALU
Fragmentation Unit即多个RTP包承载1个NALU(例如1个IDR帧很大,需要分成多个RTP包)
![image.png](https://img-blog.csdnimg.cn/img_convert/0b113481e4d646726fd3fbfa66b31212.png#averageHue=#fefcfa&clientId=u9a5d8e14-7480-4&from=paste&height=113&id=cCKj3&name=image.png&originHeight=113&originWidth=158&originalType=binary&ratio=1&rotation=0&showTitle=false&size=2237&status=done&style=none&taskId=u7d727d4e-6e81-4a6d-abed-cdf796827ad&title=&width=158)
RTP包负载的第一个字节
forbidden_zero_bit
占1位,禁止位,H.264规定为0。rfc6184规定为1时表示NALU有比特错误或者语法错误。
nal_ref_idc
占2位,表示该NALU的重要程度。SPS,PPS,IDR不应该为0,值越大越重要。为0表示该NALU不用来重建参考图像,可能会被某些解码器丢弃。
nal_unit_type
占5位,指定了NALU单元的数据类型。1-23是Single NAL Unit Packet,24-27是Aggregation Packet,28-29是Fragmentation Unit。
![image.png](https://img-blog.csdnimg.cn/img_convert/ae043257b02893f4b08b19f8a073ef93.png#averageHue=#fefdfc&clientId=u9a5d8e14-7480-4&from=paste&height=395&id=ud5a623a0&name=image.png&originHeight=395&originWidth=794&originalType=binary&ratio=1&rotation=0&showTitle=false&size=35396&status=done&style=none&taskId=ufabf6c11-c57f-4490-8067-2f10374dcd1&title=&width=794)
其中NAL unit和FU-A最常见。
Single NAL Unit Packet(单NAL单元数据包)
![image.png](https://img-blog.csdnimg.cn/img_convert/127a9f10fb8c0cfcc2e6f83d73e79128.png#averageHue=#fefdfd&clientId=u9a5d8e14-7480-4&from=paste&height=400&id=u2aee7872&name=image.png&originHeight=400&originWidth=694&originalType=binary&ratio=1&rotation=0&showTitle=false&size=18879&status=done&style=none&taskId=ue5077f32-a627-4234-87c0-da43ac0bdaf&title=&width=694)
FU-A分片
![image.png](https://img-blog.csdnimg.cn/img_convert/1fcf3dbc645d18bdcb9e3300bfc4938e.png#averageHue=#fefefd&clientId=ua40d88a6-b0af-4&from=paste&height=392&id=uda07c885&name=image.png&originHeight=392&originWidth=697&originalType=binary&ratio=1&rotation=0&showTitle=false&size=16269&status=done&style=none&taskId=u96813d6b-540d-491e-beb1-23aacf70f9c&title=&width=697)
FU indicator
FU indicator字节的格式如下:
![image.png](https://img-blog.csdnimg.cn/img_convert/1a0a5410c1072a02baebf06c48d058e0.png#averageHue=#f0eeed&clientId=ua40d88a6-b0af-4&from=paste&height=152&id=u486af3e2&name=image.png&originHeight=152&originWidth=306&originalType=binary&ratio=1&rotation=0&showTitle=false&size=19449&status=done&style=none&taskId=uba7330f1-0f66-43b4-950c-d3958d13dfb&title=&width=306)
Type字段中等于28和29的值分别表示FU-A和FU-B。
FU header
FU header具有以下格式:
![image.png](https://img-blog.csdnimg.cn/img_convert/7c1a95e521fa478ed380f5e6913f0b52.png#averageHue=#fefdfb&clientId=ua40d88a6-b0af-4&from=paste&height=132&id=u446ae6f7&name=image.png&originHeight=132&originWidth=188&originalType=binary&ratio=1&rotation=0&showTitle=false&size=2602&status=done&style=none&taskId=ub7e4afde-41f3-4873-9fa7-5a596e0abf9&title=&width=188)
S:1位,起始位。设置为1时表示分片NAL单元的开始。
E:1位,结束位。设置为1时表示分片NAL单元的结束。
R:1位,保留位。必须等于0,并且必须被接收者忽略。
Type:5位,NAL单元负载类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)