RTMP协议 (四): RTMP播放基本流程

从开始播放一个RTMP流到关闭该流,中间发生了什么?

RTMP播放基本流程

1、RTMP播放基本流程

1.1 Step 1: TCP三次握手 --- 修高速公路

RTMP是基于TCP的应用层协议。 通过TCP三次握手,可实现RTMP客户端与RTMP服务器的指定端口(默认端口为1935)建立一个可靠的网络连接。 这里的网络连接才是真正的物理连接。 完成了三次握手,客户端和服务器端就可以开始传送数据。

TCP三次握手 示意图

http://www.jellythink.com/archives/705

RTMP播放的第一步: TCP三次握手

SYN

SYN, ACK

ACK

经过三次握手,客户端与服务器端1935端口建立了TCP Connection。

经过三次握手之后

1.2 Step 2: RTMP握手 -- 安检

与其叫RTMP握手,其实实质上起到的是验证的作用。 RTMP握手的基本流程:

RTMP握手

RTMP握手主要分为: 简单握手和复杂握手。 Adobe协议中描述的是简单握手,但Adobe提供的Flash Media Server采用的却是复杂握手。

RTMP握手 分类

1.3 简单握手

简单握手如下:

简单握手

简单握手中C1和S1从第9个字节开始都是随机数。 S2是C1的复制。 C2是S1的复制。 详情可参见: RTMP Handshake(握手协议)

【学习地址】:FFmpeg/WebRTC/RTMP/NDK/Android音视频流媒体高级开发

【文章福利】:免费领取更多音视频学习资料包、大厂面试题、技术视频和学习路线图,资料包括(C/C++,Linux,FFmpeg webRTC rtmp hls rtsp ffplay srs 等等)有需要的可以点击1079654574加群领取哦~

​​​​​​1.4 复杂握手

相对于简单握手,复杂握手主要是增加了更严格的验证。 主要是将简单握手中1528Bytes随机数的部分平均分成两部分,一部分764Bytes存储public key(公共密钥),另一部分764Bytes存储digest(密文,32字节)。 另外, 复杂握手还有一个明显的特征就是: Version部分不为0,服务器端可根据这个来判断是否简单握手或复杂握手

RTMP复杂握手的模式

详情可参见: C1 in Handshake (crtmpserver) crtmpserver 中的Handshake(握手操作) -- ValidateClientScheme(验证客户端模式)

1.5 Step 3: connect(连接)

这里也叫连接,连接的是什么呢? 这里必须明白RTMP中一个很重要的概念: Application Instance

Application Instance: The instance of the application at the server with which the clients connect by sending the connect request.

不同的 Application Instance可根据功能等进行区分,比如直播可以用live来表示,点播回放可以用vod来表示。

举例:

connect举例

连接Application Instance举例

1.5 Step 4: createStream(创建流) --- 创建逻辑通道

The client sends this command to the server to create a logical channel for message communication. The publishing of audio, video, and metadata is carried out over stream channel created using the createStream command. NetConnection is the default communication channel, which has a stream ID 0. Protocol and a few command messages, including createStream, use the default communication channel.

createStream命令用于创建逻辑通道,该通道用于传输视频、音频、metadata。 在服务器的响应报文中会返回Stream ID,用于唯一的标示该Stream

服务器对createStream命令的响应报文格式

服务器对createStream命令的响应报文举例

可看出返回的Stream ID为1。 后续的视频或音频的Stream ID就是1。

后续的视频Chunk中的Stream ID (小端存储)

后续的音频Chunk中的Stream ID (小端存储)

1.6 Step 5: play(播放)

The client sends this command to the server to play a stream. A playlist can also be created using this command multiple times.

If you want to create a dynamic playlist that switches among different live or recorded streams, call play more than once and pass false for reset each time. Conversely, if you want to play the specified stream immediately, clearing any other streams that are queued for play, pass true for reset.

客户端发送play命令来播放指定流。开始传输音视频数据。 如果发送play命令后想要立即播放,需要清空play队列中的其它流,并将reset置为true。

1.7 Step 6: deleteStream(删除流)

NetStream sends the deleteStream command when the NetStream object is getting destroyed.

deleteStream命令格式

删除指定Stream ID的流。 服务器不用对这条命令发送响应报文。

2、References:

http://www.jellythink.com/archives/705 rtmp_specification_1.0.pdf

原文链接:RTMP协议 04 RTMP播放基本流程 - 简书

猜你喜欢

转载自blog.csdn.net/irainsa/article/details/130069309