RTP Fixed Header Fields

本文详细介绍了RTP协议的头部格式及各字段含义,包括版本、填充、扩展、贡献源计数、标记、载荷类型、序列号、时间戳、同步源标识符等关键字段。

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

The first twelve octets are present in every RTP packet, while the list of CSRC identifiers is present only when inserted by a mixer. The fields have the following meaning:

version (V): 2 bits
This field identifies the version of RTP. The version defined by this specification is two (2). (The value 1 is used by the first draft version of RTP and the value 0 is used by the protocol initially implemented in the "vat" audio tool.)

padding (P): 1 bit
If the padding bit is set, the packet contains one or more additional padding octets at the end which are not part of the payload. The last octet of the padding contains a count of how many padding octets should be ignored. Padding may be needed by some encryption algorithms with fixed block sizes or for carrying several RTP packets in a lower-layer protocol data unit.

extension (X): 1 bit
If the extension bit is set, the fixed header is followed by exactly one header extension, with a format defined in Section 5.3.1.

CSRC count (CC): 4 bits
The CSRC count contains the number of CSRC identifiers that follow the fixed header.

marker (M): 1 bit
The interpretation of the marker is defined by a profile. It is intended to allow significant events such as frame boundaries to be marked in the packet stream. A profile may define additional marker bits or specify that there is no marker bit by changing the number of bits in the payload type field (see Section 5.3).

payload type (PT): 7 bits
This field identifies the format of the RTP payload and determines its interpretation by the application. A profile specifies a default static mapping of payload type codes to payload formats. Additional payload type codes may be defined dynamically through non-RTP means (see Section 3). An initial set of default mappings for audio and video is specified in the companion profile Internet-Draft draft-ietf-avt-profile, and may be extended in future editions of the Assigned Numbers RFC [6]. An RTP sender emits a single RTP payload type at any given time; this field is not intended for multiplexing separate media streams (see Section 5.2).

sequence number: 16 bits
The sequence number increments by one for each RTP data packet sent, and may be used by the receiver to detect packet loss and to restore packet sequence. The initial value of the sequence number is random (unpredictable) to make known-plaintext attacks on encryption more difficult, even if the source itself does not encrypt, because the packets may flow through a translator that does. Techniques for choosing unpredictable numbers are discussed in [7].

timestamp: 32 bits
The timestamp reflects the sampling instant of the first octet in the RTP data packet. The sampling instant must be derived from a clock that increments monotonically and linearly in time to allow synchronization and jitter calculations (see Section 6.3.1). The resolution of the clock must be sufficient for the desired synchronization accuracy and for measuring packet arrival jitter (one tick per video frame is typically not sufficient). The clock frequency is dependent on the format of data carried as payload and is specified statically in the profile or payload format specification that defines the format, or may be specified dynamically for payload formats defined through non-RTP means. If RTP packets are generated periodically, the nominal sampling instant as determined from the sampling clock is to be used, not a reading of the system clock. As an example, for fixed-rate audio the timestamp clock would likely increment by one for each sampling period. If an audio application reads blocks covering 160 sampling periods from the input device, the timestamp would be increased by 160 for each such block, regardless of whether the block is transmitted in a packet or dropped as silent.

The initial value of the timestamp is random, as for the sequence number. Several consecutive RTP packets may have equal timestamps if they are (logically) generated at once, e.g., belong to the same video frame. Consecutive RTP packets may contain timestamps that are not monotonic if the data is not transmitted in the order it was sampled, as in the case of MPEG interpolated video frames. (The sequence numbers of the packets as transmitted will still be monotonic.)

SSRC: 32 bits
The SSRC field identifies the synchronization source. This identifier is chosen randomly, with the intent that no two synchronization sources within the same RTP session will have the same SSRC identifier. An example algorithm for generating a random identifier is presented in Appendix A.6. Although the probability of multiple sources choosing the same identifier is low, all RTP implementations must be prepared to detect and resolve collisions. Section 8 describes the probability of collision along with a mechanism for resolving collisions and detecting RTP-level forwarding loops based on the uniqueness of the SSRC identifier. If a source changes its source transport address, it must also choose a new SSRC identifier to avoid being interpreted as a looped source.

CSRC list: 0 to 15 items, 32 bits each
The CSRC list identifies the contributing sources for the payload contained in this packet. The number of identifiers is given by the CC field. If there are more than 15 contributing sources, only 15 may be identified. CSRC identifiers are inserted by mixers, using the SSRC identifiers of contributing sources. For example, for audio packets the SSRC identifiers of all sources that were mixed together to create a packet are listed, allowing correct talker indication at the receiver.
### RTP Header 结构 RTP 报文由 RTP 报头(RTP Header)和 RTP 有效载荷(RTP Payload)组成。RTP 报头的基本结构如下: | 位域 | 长度(位) | 描述 | | --- | --- | --- | | V(版本) | 2 | RTP 协议版本号,当前版本为 2 | | P(填充) | 1 | 若该位置 1,表示 RTP 有效载荷末尾包含填充字节 | | X(扩展) | 1 | 若该位置 1,表示 RTP 报头后包含一个扩展报头 | | CC(CSRC 计数) | 4 | 紧跟在固定报头后的 CSRC 标识符数量 | | M(标记) | 1 | 标记位,具体含义由具体应用定义 | | PT(有效载荷类型) | 7 | 标识 RTP 有效载荷的格式,如音频编码格式、视频编码格式等 | | Sequence Number(序列号) | 16 | 每个 RTP 数据包的序列号,用于检测数据包丢失和重排序 | | Timestamp(时间戳) | 32 | 反映 RTP 数据包中数据的采样时刻,用于同步和抖动缓冲 | | SSRC(同步源标识符) | 32 | 标识 RTP 流的源,用于区分不同的 RTP 流 | | CSRC(贡献源标识符) | 0 - 15 * 32 | 可选字段,最多 15 个 32 位的 CSRC 标识符,用于标识对混合流有贡献的源 | ### RTP Header 解析方法 以下是一个简单的 Python 示例,用于解析 RTP 报头: ```python import struct def parse_rtp_header(data): # 解析固定报头的前 12 字节 v_p_x_cc, m_pt, seq_num, timestamp, ssrc = struct.unpack('!BBHII', data[:12]) # 提取各个字段 version = (v_p_x_cc >> 6) & 0x03 padding = (v_p_x_cc >> 5) & 0x01 extension = (v_p_x_cc >> 4) & 0x01 csrc_count = v_p_x_cc & 0x0F marker = (m_pt >> 7) & 0x01 payload_type = m_pt & 0x7F # 解析 CSRC 标识符 csrcs = [] for i in range(csrc_count): csrc = struct.unpack('!I', data[12 + i * 4: 16 + i * 4])[0] csrcs.append(csrc) # 处理扩展报头 if extension: # 假设扩展报头长度为 4 字节(实际需要根据具体情况解析) ext_len = struct.unpack('!H', data[12 + csrc_count * 4: 14 + csrc_count * 4])[0] ext_data = data[14 + csrc_count * 4: 14 + csrc_count * 4 + ext_len] else: ext_data = None return { 'version': version, 'padding': padding, 'extension': extension, 'csrc_count': csrc_count, 'marker': marker, 'payload_type': payload_type, 'seq_num': seq_num, 'timestamp': timestamp, 'ssrc': ssrc, 'csrcs': csrcs, 'ext_data': ext_data } # 示例数据 rtp_data = b'\x80\x60\x12\x34\x01\x02\x03\x04\x05\x06\x07\x08' header_info = parse_rtp_header(rtp_data) print(header_info) ``` ### 相关注意事项 在 FFmpeg 源码中,若 RTP 报头存在 RTP Header Extension,会跳过 RTP Header Extension 的读取,让指针 `buf` 移动到 RTP Header Extension 之后,以便读取后续数据。具体代码如下: ```c /* RFC 3550 Section 5.3.1 RTP Header Extension handling */ if (ext) { if (len < 4) return -1; /* calculate the header extension length (stored as number * of 32-bit words) */ ext = (AV_RB16(buf + 2) + 1) << 2; if (len < ext) return -1; // skip past RTP header extension len -= ext; buf += ext; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值