RTMP protocol (4): RTMP playback basic process

What happens between starting to play an RTMP stream and closing the stream?

Basic process of RTMP playback

1. Basic process of RTMP playback

1.1 Step 1: TCP three-way handshake --- Repair the highway

RTMP is an application layer protocol based on TCP. Through the TCP three-way handshake, the RTMP client can establish a reliable network connection with the designated port of the RTMP server (the default port is 1935). The network connection here is the real physical connection. After completing the three-way handshake, the client and server can start transmitting data.

Schematic diagram of TCP three-way handshake

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

The first step of RTMP playback: TCP three-way handshake

SYN

SYN, ACK

ACK

After three handshakes, the client and the server establish a TCP Connection on port 1935 .

After three handshakes

1.2 Step 2: RTMP handshake -- security check

Instead of calling it an RTMP handshake, it actually plays the role of verification. The basic process of RTMP handshake:

RTMP handshake

RTMP handshakes are mainly divided into: simple handshake and complex handshake . The Adobe protocol describes a simple handshake, but the Flash Media Server provided by Adobe uses a complex handshake.

RTMP Handshake Classification

1.3 Simple handshake

A simple handshake is as follows:

simple handshake

In the simple handshake, C1 and S1 are random numbers starting from the ninth byte. S2 is a copy of C1. C2 is a copy of S1. For details, please refer to: RTMP Handshake (handshake protocol)

[Learning address]: FFmpeg/WebRTC/RTMP/NDK/Android audio and video streaming media advanced development

[Article Benefits]: Receive more audio and video learning packages, Dachang interview questions, technical videos and learning roadmaps for free. The materials include (C/C++, Linux, FFmpeg webRTC rtmp hls rtsp ffplay srs, etc.) Click 1079654574 to join the group to receive it~

1.4 Complex handshake

Compared with the simple handshake, the complex handshake mainly adds stricter verification. Mainly divide the 1528Bytes random number in the simple handshake into two parts, one part of 764Bytes stores the public key (public key), and the other part of 764Bytes stores the digest (ciphertext, 32 bytes). In addition, another obvious feature of the complex handshake is that the Version part is not 0, and the server can judge whether it is a simple handshake or a complex handshake based on this .

RTMP complex handshake mode

For details, please refer to: C1 in Handshake (crtmpserver) Handshake (handshake operation) in crtmpserver -- ValidateClientScheme (validate client mode)

1.5 Step 3: connect

This is also called a connection, what is the connection? Here you must understand a very important concept in RTMP: Application Instance.

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

Different Application Instancecan be distinguished according to functions, for example, live broadcast can be represented by live, and playback on demand can be represented by vod.

Example:

connect example

Example of connecting to Application Instance

1.5 Step 4: createStream (create stream) --- Create a logical channel

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.

The createStream command is used to create a logical channel , which is used to transmit video, audio, and metadata. The Stream ID will be returned in the server's response message , which is used to uniquely identify the Stream .

The server's response message format to the createStream command

Example of the server's response message to the createStream command

It can be seen that the returned Stream ID is 1. The Stream ID of subsequent video or audio is 1.

Stream ID in the subsequent video Chunk (little endian storage)

Stream ID in the subsequent audio Chunk (little endian storage)

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.

The client sends the play command to play the specified stream. Start transmitting audio and video data. If you want to play immediately after sending the play command, you need to clear other streams in the play queue and set reset to true.

1.7 Step 6: deleteStream (delete stream)

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

deleteStream command format

Delete the stream with the specified Stream ID. The server does not need to send a response message to this command.

2、References:

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

Original text link: RTMP protocol 04 RTMP playback basic process - short book

Guess you like

Origin blog.csdn.net/irainsa/article/details/130069309