RTMP流播放的流程首先要进行握手操作,才能进行后续的connect和createStream等操作, 本文简单阐述握手基础知识.
Handshake Diagram
wireshark分析握手基本流程
用wireshark对rtmp的包进行抓包分析:
可以看到前三个报文是握手协议:
从以上可看出握手的实际流程为三个步骤:
- Client -> Server
C0+C1 - Server -> Client
S0+S1+S2 - Client -> Server
C2
可以看出握手的起点是Client(RMTP播放器)发送C0+C1
, 结束是Server收到Client的C2
.
其中C0 C1 C2 S0 S1 S2
的大小都是固定的.
C2 S2
属于用于确认(Ack
)的包
Step 1: C0 + C1
C0 + C1
一起发送.
其中C0
一个字节, 固定为03
.
C1
为1536个字节.
数据包总长度为:
1 + 1536 = 1537
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.
0-2 are deprecated values used by earlier proprietary products;
4-31 are reserved for future implementations;
32-255 are not allowed (to allow distinguishing RTMP from text-based protocols, which always start with a printable character).
Step 2: S0+S1+S2
S0+S1+S2
一起发送.
S0
为1个字节,固定为03
.
S1
和S2
都为1536
个字节.
整个数据包总长度为:
1 + 1536 + 1536 = 3073
Step 3: C2
C2为1536
个字节.
RTMP Server
接收到C2
意味着握手成功结束.
References:
rtmp_specification_1.0.pdf