RTMP协议之三次握手

RTMP协议之三次握手

  • 简介(Introduction)
  • 握手(Handshake)
    • 握手顺序(Handshake Sequence)
    • C0和S0格式(C0 and S0 Format)
    • C1和S1格式(C1 and S1 Format)
    • C2和S2格式(C2 and S2 Format)
    • 握手过程图(Handshake Diagram)
  • Just do it

注:本文引用的原文内容均摘自 http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/rtmp/pdf/rtmp_specification_1.0.pdf

简介(Introduction)

【原文】

Adobe’s Real Time Messaging Protocol (RTMP) provides a bidirectional message multiplex service over a reliable stream transport, such as TCP [RFC0793], intended to carry parallel streams of video, audio, and data messages, with associated timing information, between a pair of communicating peers. Implementations typically assign different priorities to different classes of messages, which can effect the order in which messages are enqueued to the underlying stream transport when transport capacity is constrained.

This memo describes the syntax and operation of the Real Time Messaging Protocol.

【译文】
Adobe 的 Real Time Messaging Protocol(Real Time Messaging Protocol, RTMP)通过可靠的流传输(如TCP [RFC0793])提供双向消息多路复用服务,用于在一对通信对等点之间传输具有相关计时信息的并行视频流、音频和数据消息。实现通常为不同的消息类分配不同的优先级,从而在传输带宽受限时可以影响消息在底层流传输通道的入队顺序。

本文描述了实时消息传递协议的语法和操作。

握手(Handshake)

【原文】

An RTMP connection begins with a handshake. The handshake is unlike the rest of the protocol; it consists of three static-sized chunks rather than consisting of variable-sized chunks with headers. The client (the endpoint that has initiated the connection) and the server each send the same three chunks. For exposition, these chunks will be designated C0, C1, and C2 when sent by the client; S0, S1, and S2 when sent by the server.

【译文】
RTMP连接从握手开始。握手不同于协议的其他部分;它由三个固定大小的块组成,而不是由带有头的可变大小的块组成。客户端(发起连接的一端)和服务器都发送相同的三个块。为了便于说明,当客户端发送时,这些块将被指定为C0、C1和C2;当服务器发送时,这些块被指定为S0、S1和S2。

握手顺序(Handshake Sequence)

【原文】

The handshake begins with the client sending the C0 and C1 chunks.

The client MUST wait until S1 has been received before sending C2.
The client MUST wait until S2 has been received before sending any other data.

The server MUST wait until C0 has been received before sending S0 and S1, and MAY wait until after C1 as well.
The server MUST wait until C1 has been received before sending S2.
The server MUST wait until C2 has been received before sending any other data.

【译文】
握手从客户端发送C0和C1块开始。

客户端必须收到S1后才能发送C2。
客户端必须收到S2后才能发送任何其他数据。

服务器必须收到C0C1后才能发送S0和S1。
服务器必须收到C1后才能发送S2。
服务器必须收到C2之后才能发送任何其他数据。

C0和S0格式(C0 and S0 Format)

【原文】

The C0 and S0 packets are a single octet, treated as a single 8-bit integer field:
RTMP协议之三次握手_第1张图片
Following are the fields in the C0/S0 packets:

Version (8 bits): In C0, this field identifies the RTMP version requested by the client. In S0, this field identifies the RTMP version selected by the server. The version defined by this specification is 3. Values 0-2 are deprecated values used by earlier proprietary products; 4-31 are reserved for future implementations; and 32-255 are not allowed (to allow distinguishing RTMP from text-based protocols, which always start with a printable character). A server that does not recognize the client’s requested version SHOULD respond with 3. The client MAY choose to degrade to version 3, or to abandon the handshake.

【译文】
C0和S0都是单个8位字节,可以看成一个8位整形字段:
RTMP协议之三次握手_第2张图片
以下是C0/S0数据包中的字段:
Version(8位):在C0中,该字段标识客户端请求的RTMP版本。在S0中,该字段标识由服务器选择的RTMP版本。本规范定义的版本是3。0-2是早期专有产品使用,现已弃用;4-31预留用于未来使用;32-255不允许使用(以区分RTMP和文本协议,因为它总是以一个可打印字符为开始)。当识别不出客户端请求的版本时,服务器应该使用3来响应。客户端可以选择降级到版本3,或者放弃握手。

C1和S1格式(C1 and S1 Format)

【原文】

The C1 and S1 packets are 1536 octets long, consisting of the following fields:
RTMP协议之三次握手_第3张图片
Time (4 bytes): This field contains a timestamp, which SHOULD be used as the epoch for all future chunks sent from this endpoint. This may be 0, or some arbitrary value. To synchronize multiple chunkstreams, the endpoint may wish to send the current value of the other chunkstream’s timestamp.

Zero (4 bytes): This field MUST be all 0s.

Random data (1528 bytes): This field can contain any arbitrary values. Since each endpoint has to distinguish between the response to the handshake it has initiated and the handshake initiated by its peer,this data SHOULD send something sufficiently random. But there is no need for cryptographically-secure randomness, or even dynamic values.

【译文】
C1和S1数据包长度为1536字节,包括以下字段:
RTMP协议之三次握手_第4张图片
Time(4个字节):这个字段包含一个时间戳,它应该作为未来从这个端点发出的所有块的时间基准。该字段值可以是0,也可以是任意值。要同步多个chunkstream,此端点可能要发送其他chunkstream的时间戳的当前值。

Zero(4字节):该字段必须全部为0。

Random data(1528字节):该字段可以包含任意值。由于每个端点都必须区分自己发起的握手的应答和它的对等点发起的握手的应答,因此该数据应该发送足够随机的内容。但是没有必要用密码保护随机数,甚至不需要动态值。

C2和S2格式(C2 and S2 Format)

【原文】

The C2 and S2 packets are 1536 octets long, and nearly an echo of S1 and C1 (respectively), consisting of the following fields:
RTMP协议之三次握手_第5张图片
Time (4 bytes): This field MUST contain the timestamp sent by the peer in S1 (for C2) or C1 (for S2).

Time2 (4 bytes): This field MUST contain the timestamp at which the previous packet(s1 or c1) sent by the peer was read.

Random echo (1528 bytes): This field MUST contain the random data field sent by the peer in S1 (for C2) or S2 (for C1). Either peer can use the time and time2 fields together with the current timestamp as a quick estimate of the bandwidth and/or latency of the connection, but this is unlikely to be useful.

【译文】
C2和S2数据包长度均为1536个八位的字节,几乎分别类似于S1和C1的原样返回,包括以下字段:
RTMP协议之三次握手_第6张图片
Time(4字节):该字段必须包含由对端发送的S1(对应C2)或C1(对应S2)中的发送时间戳。

Time2(4字节):该字段必须包含读取对端发送的前一个包(S1或C1)时的时间戳。

Random echo(1528字节):该字段必须包含对端发送的S1(对应C2)或S2(对应C1)中的随机数据字段。任何一端都可以使用time和time2字段以及当前时间戳来快速估计连接的带宽和/或延迟,但这不大可能有用。

握手过程图(Handshake Diagram)

【原文】

RTMP协议之三次握手_第7张图片
The following describes the states mentioned in the handshake diagram:

Uninitialized: The protocol version is sent during this stage. Boththe client and server are uninitialized. The The client sends the protocol version in packet C0. If the server supports the version, it sends S0 and S1 in response. If not, the server responds by taking the appropriate action. In RTMP, this action is terminating the connection.

Version Sent: Both client and server are in the Version Sent state after the Uninitialized state. The client is waiting for the packet S1 and the server is waiting for the packet C1. On receiving the awaited packets, the client sends the packet C2 and the server sends the packet S2. The state then becomes Ack Sent.

Ack Sent: The client and the server wait for S2 and C2 respectively.

Handshake Done: The client and the server exchange messages.

【译文】
RTMP协议之三次握手_第8张图片
下面描述握手过程图中提到的状态:

Uninitialized:在此阶段发送协议版本。客户端和服务器都没有初始化。客户端以数据包C0的形式发送协议版本。如果服务器支持该版本,它将发送S0和S1作为响应。否则,服务器将通过采取适当的操作进行响应。在RTMP中,此操作正在终止连接。

Version Sent:在Uninitialized状态之后,客户端和服务器都处于Version Sent状态。客户端在等待包S1,服务器在等待包C1。当接收到等待的包时,客户端发送包C2,服务器发送包S2。然后状态变为Ack Sent。

Ack Sent:客户端和服务器分别等待S2和C2。

Handshake Done:客户机和服务器处于可以交换消息的状态。

Just do it

与上述握手过程不同,握手的实际流程分三个步骤:

  1. Client -> Server,C0 + C1
  2. Server -> Client,S0 + S1 + S2
  3. Client -> Server,C2

Handshake Diagram

你可能感兴趣的:(RTMP协议之三次握手)