名字
英文原文: rtmp_specification_1.0.pdf
本节定义RTMP chunk stream。它为高层的媒体流协议提供多路复用和包封装的服务。
当RTMP chuck stream被设计成为RTMP协议服务的时候,它能处理任何消息流的协议。每个消息包含时间戳和负载类型。RTMP chunk stream和RTMP在一起是非常适合各种音视频应用做点到点、点到多点的直播,或VOD服务,或会议应用。
当用可靠传输协议来传输,如TCP(RFC0793),RTMP chunk stream对所有流的消息提供可靠时序的点到点传输。RTMP chunk stream不提供任何优先级或类似控制,但可以在上层协议来提供优先级。例如,直播视频服务对网速慢的用户,可以选择丢弃视频消息以此保证音频消息的实时接收,无论基于时间发送还是回复每个消息的时间。
RTMP chunk stream包括它自己的带内协议控制消息,并且也为高层协议提供机制内嵌用户控制消息。
消息格式能被分片成多个chunk,依次在上层协议上支持多路复用。消息格式因为应该包含下面几个字段,其是用来创建chunk的必要条件。
一个RTMP连接开始于握手。握手完全不同于rtmp的其他协议。它有3个同等大小的chunk组成,而不是变长的chunk并带有头部。
客户端和服务器各自发送同样3个chunk。客户端发送C0, C1和C2;服务端发送S0, S1和S2。
一开始客户端发送C0和C1的chunck。
客户端发送C2前需要先收到服务端恢复的S1。客户端必须在收到S2之后,才能发送其他的数据。
服务端必须收到C0后,才能发送S0和S1,也可以等到收到C0后才发送S0和S1。服务端必须等到收到C1才发送S2。服务端必须接收到C2后,才能发送其他的数据。
C0和S0报文只有一个字节,也就是8-bit的字段:
0 1 2 3 4 5 6 7 +-+-+-+-+-+-+-+-+ | version | +-+-+-+-+-+-+-+-+ C0 and S0 bits
C0/S0报文字段:
Version(8bits): 在C0中,这个字段是客户端定义RTMP版本。在S0中,这个字段由服务端选择支持的版本。在本文中,协议定义为3。值0-2是早期的版本好,已经不再使用;4-31是为未来的保留号;32-255不允许。服务端如果不认识客户端发来的版本好,就应该回复版本号3。客户端可以选择遵循协议3,或放弃握手。
C1和S1包是1536字节长,由一下字段组成:
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time (4 bytes) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | zero (4 bytes) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | random bytes | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | random bytes | | (cont) | | .... | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ C1 and S1 bits* Time(4字节):这个字段包含时间戳,这个时间戳应该被后续所有的chuck作为时间开始。其可以是0,或者是其他任何数字。为了同步多个chuck流,服务端/客户端希望发送其他chunk流的时间戳。 * Zero(4字节): 这个字段必须是0。 * Random data (1528 bytes): 这个字段可以保护任何自定义信息。因为客户端和服务端为了区分发起握手的消息和响应握手的消息,这个数据块应该发送随机数值。但也不必一定是密码级别的随机值,或者可以是变量。 ### 5.2.4 C2和S2格式 C2和S2都是1536字节长,是对S1/C1分别的回复,由以下字段组成:
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time (4 bytes) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time2 (4 bytes) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | random echo | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | random echo | | (cont) | | .... | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ C2 and S2 bits* Time (4 bytes): 这个字段是时间戳,S1的对C2,或C1对S2; * Time2 (4 bytes): 这个字段必须包含上一个报文(s1或c1)发过来的时间戳; * Random echo (1528 bytes): 这个字段必须包含随机数据对端发过来的,S1(对C2)或者S2(对C1)。双方都可以用time, time2字段来估计带宽和连接的延时,当然这比一定有用; ### 5.2.5. Handshake图解 
+--------------+----------------+--------------------+--------------+ | Basic Header | Message Header | Extended Timestamp | Chunk Data | +--------------+----------------+--------------------+--------------+ |<------------------- Chunk Header ----------------->|* Basic Header (1 to 3 bytes): 这个字段包括stream id和chunk type。chunk type定义了message header的格式。长度完全由chunk stream id决定,其是可变长度的字段 * Message Header (0, 3, 7, or 11 bytes): 这个字段包含消息要发送的信息(无论是全部还是部分)。该长度通过chunk header中的chunk type来定义 * Extended Timestamp (0 or 4 bytes): 这个字段chunk message header中的时间戳。 5.3.1.3中有详细信息。 * Chunk Data (variable size): chunk的负载数据,数据填充到chunk size的大小。 #### 5.3.1.1. Chunk Basic Header 这个chunk basic header包含chunk stream id 和chunk type(有fmt字段表示)。chunk type定义message header的格式类型。chunk basic header字段可以是1,2或3字节,其有由chunk stream id决定。
0 1 2 3 4 5 6 7 +-+-+-+-+-+-+-+-+ |fmt| cs id | +-+-+-+-+-+-+-+-+ Chunk basic header 1
0 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |fmt| 0 | cs id - 64 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Chunk basic header 2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |fmt| 1 | cs id - 64 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Chunk basic header 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | timestamp |message length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | message length (cont) |message type id| msg stream id | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | message stream id (cont) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Chunk Message Header - Type 0* timestamp (3 bytes): 对于type-0的chunk,消息使用绝对时间戳。如果时间戳大于等于16777215 (16进制0xFFFFFF),这个字段必须是16777215,意味着Extended Timestamp字段32bit的时间戳。此外,这个字段代表完整的时间戳。 ##### 5.3.1.2.2 Type 1 Type 1 chunk headers是7字节长。message stream ID不在其内。这个chunk带有上个chunk相同的chunk stream id。流都是变长的消息(举例,多种视频格式)应该用这个格式作为第二个stream的chunk报文。
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | timestamp delta |message length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | message length (cont) |message type id| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Chunk Message Header - Type 1##### 5.3.1.2.3 Type 2 类型2chunk headers是3字节长。stream ID和message length都不包含;chunk有与前一个chunk相同的stream ID和message length。流是定长的消息(例如,音频和数据格式)应该用这个类型,作为第二个stream的chunk报文。
0 1 2 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | timestamp delta | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Chunk Message Header - Type 2##### 5.3.1.2.4 Type 3 类型3其实没有message header。stream ID,message length和timestamp都不存在。这个chunk类型都继承前一个相同chunk stream ID的chunk所有字段。当单个消息被切分多个chunk,所有的消息除了第一个chunk外都用这个类型。
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |0| chunk size (31 bits) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Payload for the ‘Set Chunk Size’ protocol message* 0: 这个bit必须是0; * chunk size (31 bits): 这个字段包含chunk size的最大值,发送端接下来的chunk都以这个chunk size作为有效尺寸来发送,直到有新的set chunk size消息来到。值的范围是1到2147483647(0x7fffffff);然后,所有值大于16777215(0xffffff)的都被认为是16777215,因为没有哪个chunk大小会比message还有大,因为message都是小于16777215字节的。 ### 5.4.2 Abort Message(2) 协议控制消息2,Abort Message,被用于通知对端如果还在等待chunk包来完成message组播啊,可以丢弃掉已经接收到的chunk报文。对端接收到这个类型协议的报文,其负载是chunk stream ID。一个应用应该在开始关闭连接是发送这个消息,为了对端不用在等待未完成的message。
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | chunk stream id (32 bits) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Payload for the ‘Abort Message’ protocol messagechunk stream ID (32 bits): 这个字段包含chunk stream ID。也就是这个chunk stream ID的消息将被丢弃。 ### 5.4.3 Acknowledgement(3) 客户端或服务器必须在接收到window size消息后,发送Acknowledgement给对端。这个windows size是发送者还没有接收到acknowledgement前发送最大的字节数。消息定义了sequence number,其是目前已经接收到的字节数。
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | sequence number (4 bytes) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Payload for the ‘Acknowledgement’ protocol message
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgement Window size (4 bytes) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Payload for the ‘Window Acknowledgement Size’ protocol message
客户端或服务器发送这个消息以限制对端发送带宽。对端收到这个消息后通过限制发送数量来限制输出带宽,但是不需要针对这个消息的回复。对端接收到这个消息,如果window size与上次这个消息的发送者发送的不一样,就应该返回一个window acknowledgement size massage。
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Acknowledgement Window size | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Limit Type | +-+-+-+-+-+-+-+-+ Payload for the ‘Set Peer Bandwidth’ protocol messageLimit type是如下几种:
+------------------------------+------------------------- | Event Type (16 bits) | Event Data +------------------------------+------------------------- Payload for the ‘User Control’ protocol message
+----------------+---------+---------------------------------------+ | Field Name | Type | Description | +--------------- +---------+---------------------------------------+ | Command Name | String | Name of the command. Set to "connect".| +----------------+---------+---------------------------------------+ | Transaction ID | Number | Always set to 1. | +----------------+---------+---------------------------------------+ | Command Object | Object | Command information object which has | | | | the name-value pairs. | +----------------+---------+---------------------------------------+ | Optional User | Object | Any optional information | | Arguments | | | +----------------+---------+---------------------------------------+
+--------------+----------+----------------------------------------+ | Field Name | Type | Description | +--------------+----------+----------------------------------------+ | Command Name | String | Name of the command. Set to | | | | "createStream". | +--------------+----------+----------------------------------------+ | Transaction | Number | Transaction ID of the command. | | ID | | | +--------------+----------+----------------------------------------+ | Command | Object | If there exists any command info this | | Object | | is set, else this is set to null type. | +--------------+----------+----------------------------------------+
+--------------+----------+----------------------------------------+ | Field Name | Type | Description | +--------------+----------+----------------------------------------+ | Command Name | String | _result or _error; indicates whether | | | | the response is result or error. | +--------------+----------+----------------------------------------+ | Transaction | Number | ID of the command that response belongs| | ID | | to. | +--------------+----------+----------------------------------------+ | Command | Object | If there exists any command info this | | Object | | is set, else this is set to null type. | +--------------+----------+----------------------------------------+ | Stream | Number | The return value is either a stream ID | | ID | | or an error information object. | +--------------+----------+----------------------------------------+
最新评论 我的评论
t-io为本站提供HTTP、WebSocket、Socket、页面渲染与压缩等服务,nginx为本站提供反向代理服务
© 2017-2023 钛特云 版权所有 | 浙ICP备17032976号 | 浙公网安备 33011802002129号