写点什么

Rtmp Message 与 Chunk 格式

用户头像
糖米唐爹
关注
发布于: 刚刚
Rtmp Message 与 Chunk格式

一,简言


本文大部分内容摘自RTMP协议规范, 仅供个人学习总结。


二,Message Format


RTMP 协议中基本的数据单元称为消息(Message),消息主要分为三类: 协议控制消息、数据消息、命令消息。


  • 协议控制消息

  • Message Type ID = 1~6,主要用于协议内的控制

  • 数据消息

Message Type ID = 8 9 18

  • 8: Audio 音频数据

    9: Video 视频数据

    18: Metadata 包括音视频编码、视频宽高等信息。

  • 命令消息 Command Message (20, 17)

  • 可理解为远程函数调用,详见 RTMP 协议 7.2 节。

1,Message Header

  • Message Type: One byte field to represent the message type. A range of type IDs (1-6) are reserved for protocol control messages.

  • Length: Three-byte field that represents the size of the payload in bytes. It is set in big-endian format.

  • Timestamp: Four-byte field that contains a timestamp of the message. The 4 bytes are packed in the big-endian order.

  • Message Stream Id: Three-byte field that identifies the stream of the message. These bytes are set in big-endian format.


2,Message Type



三,Chunk Format

rtmp 协议中 Message 会被拆分成多个 chunk 块,然后以 chunk 作为传输单元,目的是防止大的数据块阻塞小的数据块(如音频数据或控制信息)。


Chunk 由 Chunk Header 和 Chunk Data 两部分组成。


  • Basic Header (1 to 3 bytes): This field encodes the chunk stream ID and the chunk type. Chunk type determines the format of the encoded message header. The length depends entirely on the chunk stream ID, which is a variable-length field.

  • Message Header (0, 3, 7, or 11 bytes): This field encodes information about the message being sent (whether in whole or in part). The length can be determined using the chunk type specified in the chunk header.

  • Extended Timestamp (0 or 4 bytes): This field is present in certain circumstances depending on the encoded timestamp or timestamp delta field in the Chunk Message header. See Section 5.3.1.3 for more information.

  • Chunk Data (variable size): The payload of this chunk, up to the configured maximum chunk size.


Basic Header


Basic Header is a variable-length field.  The length depends entirely on the cs id(chunk stream ID)


  • Chunk stream IDs 2-63 can be encoded in the 1-byte version of this field.


  • Chunk stream IDs 64-319 can be encoded in the 2-byte form of the header. ID is computed as (the second byte + 64).

当 cs id 在[64,319] ,2-Byte


  • Chunk stream IDs 64-65599 can be encoded in the 3-byte version of this field. ID is computed as ((the third byte)*256 + (the second byte) + 64).

当 cs id 在[64,319] ,2-Byte


Chunk Message Header


There are four different formats for the chunk message header, selected by the "fmt" field in the chunk basic header.


Type 0

Type 0 chunk headers are 11 bytes long. This type MUST be used at the start of a chunk stream, and whenever the stream timestamp goes backward (e.g., because of a backward seek).



Type 1

Type 1 chunk headers are 7 bytes long. The message stream ID is not included; this chunk takes the same stream ID as the preceding chunk. Streams with variable-sized messages (for example, many video formats) SHOULD use this format for the first chunk of each new message after the first.



Type 2


Type 2 chunk headers are 3 bytes long. Neither the stream ID nor the message length is included; this chunk has the same stream ID and message length as the preceding chunk. Streams with constant-sized messages (for example, some audio and data formats) SHOULD use this format for the first chunk of each message after the first.


Type 3


Type 3 chunks have no message header. The stream ID, message length and timestamp delta fields are not present; chunks of this type take values from the preceding chunk for the same Chunk Stream ID. When a single message is split into chunks, all chunks of a message except the first one SHOULD use this type. Refer to Example 2 (Section 5.3.2.2). A stream consisting of messages of exactly the same size, stream ID and spacing in time SHOULD use this type for all chunks after a chunk of Type 2. Refer to Example 1 (Section 5.3.2.1). If the delta between the first message and the second message is same as the timestamp of the first message, then a chunk of Type 3 could immediately follow the chunk of Type 0 as there is no need for a chunk of Type 2 to register the delta. If a Type 3 chunk follows a Type 0 chunk, then the timestamp delta for this Type 3 chunk is the same as the timestamp of the Type 0 chunk.


Common Header Fields


  • timestamp delta (3 bytes): For a type-1 or type-2 chunk, the difference between the previous chunk’s timestamp and the current chunk’s timestamp is sent here. If the delta is greater than or equal to 16777215 (hexadecimal 0xFFFFFF), this field MUST be 16777215, indicating the presence of the Extended Timestamp field to encode the full 32 bit delta. Otherwise, this field SHOULD be the actual delta.

  • message length (3 bytes): For a type-0 or type-1 chunk, the length of the message is sent here. Note that this is generally not the same as the length of the chunk payload. The chunk payload length is the maximum chunk size for all but the last chunk, and the remainder (which may be the entire length, for small messages) for the last chunk.

  • message type id (1 byte): For a type-0 or type-1 , Message 的类型见:2.2 章节.

  • msg stream id 当前消息所属信道分类。

  • message stream id (cont) : 流对应的流标记,消息内容对应数据流 ID 标志位。


Example 

This example illustrates a message that is too long to fit in a 128 byte chunk and is broken into several chunks.


Sample Message to be broken to chunks Here are the chunks that are produced:



  • message type id 为 9 。

  • msg stream id 为 4。

  • message stream id (cont)为 12345。

四,总结

  1. Message 为 rtmp 协议中基本的数据单元,Chunk 做为传输单元。

  2. Chunk 由 Basic Header 和 Message Header 组成。

  3. Basic Header 是非定长的字段,取决于 cs id 的取值,有 1-byte,2-bytes,3-bytes3 种。

  4. Basic Header fmt 决定了 Message Header 的类型。

  5. 每个 Chunk 中带有 MessageID,接受端根据 MessageID 将 Chunk 进行组装。


五,Reference

RTMP协议规范

理解RTMP协议——chunk格式

流媒体:RTMP 协议完全解析


用户头像

糖米唐爹

关注

还未添加个人签名 2020.08.12 加入

还未添加个人简介

评论

发布
暂无评论
Rtmp Message 与 Chunk格式