RTMP push flow and protocol learning

Source: Internet
Author: User
Tags linecap openssl prepare stream api

Pre-readiness to understand RTMP definition readiness Rtmpdump in LIBRTMP using the Libssllibcrypto push flow work overall framework diagram in OpenSSL use the LIBTRMP provided API to package streaming into rtmp format Advanced rtmp Client and RTMP server interaction process Rtmpdump Source Analysis handshake handsshake static int handshakertmp r int fp9handshake Establish link netconnnet rtmp_connectr TMP r rtmppacket CP int rtmp_connect0rtmp r struct sockaddr service int rtmp_connect1rtmp r rtmppacket CP build stream NetStream int rtmp_connectstreamrtmp r int Seektime int RTMP_READPACKETRTMP r rtmppacket Packet int readnrtmp r char buffer int n int r Tmpsockbuf_fillrtmp r rtmpsockbuf SB Reference

Pre-Preparation RTMP definition prepare librtmp in rtmpdump use Libssl+libcrypto in OpenSSL to understand the RTMP definition

RTMP is the acronym for Real Time Messaging Protocol (Live Message Transfer Protocol). The protocol is based on TCP and is a protocol family, including various variants such as RTMP Basic protocol and Rtmpt/rtmps/rtmpe. RTMP is a network protocol designed for real-time data communication, which is used primarily for audio and video and data communication between the Flash/air platform and the streaming/interactive server that supports the RTMP protocol. prepare the librtmp in the Rtmpdump Download
On Librtmp's official web site, you can download: http://rtmpdump.mplayerhq.hu, if you can't access it, you can find the code on GitHub. The latest version is: Rtmpdump-2.3.tgz. compiling
To extract the rtmpdump-2.3.tgz, you can view the Readme to get the compilation method:

To compile type ' make ' with sys=, e.g.
Make Sys=posix
For Linux, MacOSX, Unix, etc. or
Make Sys=mingw
For Windows.
You can cross-compile for other platforms using the Cross_compile variable:
Make Cross_compile=arm-none-linux-inc=-i/my/cross/includes

After the compilation is complete, you can find the LIBRTMP.A library file under the Librtmp directory. In the project that needs to use librtmp, this lib is linked to it. using the Libssl+libcrypto in OpenSSL Download
The latest OpenSSL source code is available on the official web site of OpenSSL. https://www.openssl.org/source/. The latest version is: Version 1.1.0e. compiling
Compiling OpenSSL is simple, if it is on Linux, direct./configure + make can be done. If you want to cross-compile, after the configure corresponding platform, you need to modify the makefile cc AR prefix related variables.

./configure android-armv7 Modify makefile cc ar prefix .... (edited)

After compiling, we can see the LIBSSL.A and Libcrypto.a two lib in the Libs directory. The two Libs support is required for rtmp push.Push Flow work Overall frame DiagramCreated with Raphaël 2.1.0 streaming from RTSP Get Audio/video Frame Convert frame RTMP push strong> using the API provided by LIBTRMP

LIBRTMP provides a push-stream API that allows you to view all the APIs in the Rtmp.h file. We only need to use a few common APIs to push streaming to the server.
-Rtmp_init ()//Initialize struct
-Rtmp_free ()
-Rtmp_alloc ()
-Rtmp_setupurl ()//Set RTMP server address
-Rtmp_enablewrite ()//Open writable option, set to push current state
-Rtmp_connect ()//Build Netconnection
-Rtmp_close ()//close connection
-Rtmp_connectstream ()//Build NetStream
-Rtmp_deletestream ()//delete NetStream
-Rtmp_sendpacket ()//Send data

FlowchartCreated with Raphaël 2.1.0 Start rtmp_init () Rtmp_alloc () Rtmp_setup () Rtmp_enablewrite () Rtmp_connect () Rtmp_ ConnectStream () Rtmp_sendpacket () End encapsulate streaming as rtmp format

When you send the first frame of audio and video, you need to encapsulate the audio and video messages as rtmp headers and send them to rtmp server.
The audio header has 4 bytes, including: Head marker 0xaf 0x00, profile, channel, bitrate information.
The video header has 16 bytes, including IFRAME, pframe, AVC identification, and in addition, SPS and PPS need to be placed inside the header.
The RTMP protocol defines the message type, where messages of type ID 8,9 are used to transmit audio and video data, respectively:

#define Rtmp_packet_type_audio 0x08
#define Rtmp_packet_type_video 0x09
Audio format encapsulated source code:
AAC Header packet:
BODY = (unsigned char *) malloc (4 + size);
memset (body, 0, 4);
Body[0] = 0xaf;
BODY[1] = 0x00;

Switch (profile) {case
 0:
    body[2]|= (1<<3);//main break
    ;
 Case 1:
    body[2]|= (1<<4),//LC break
    ;
 Case 2:
    body[2]|= (1<<3);//SSR
    body[2]|= (1<<4);
    break;
 Default:
    ;
}
Switch (this->channel) {case
 1:
    body[3]|= (1<<3);//channel1 break
    ;
 Case 2:
    body[3]|= (1<<4),//channel2 break
    ;
 Default:
    ;
}
Switch (this->rate) {case
 48000:
    body[2]|= (1);
    Body[3]|= (1<<7);
    break;
 Case 44100:
    body[2]|= (1<<1);
    break;
 Case 32000:
    body[2]|= (1<<1);
    Body[3]|= (1<<7);
    break;
 Default:
    ;
}
Sendpacket (Rtmp_packet_type_audio, body, 4, 0);
Free (body);
The source code of the Video format package:
H264 Header packet:
BODY = (unsigned char *) malloc (+ Sps_len + pps_len);
This->videofist = false;

memset (body, 0, + Sps_len + pps_len);
body[i++] = 0x17;   1:iframe, 7:AVC
                    //AVC Sequence Header
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;

Avcdecoderconfigurationrecord
body[i++] = 0x01;
body[i++] = sps[1];
body[i++] = sps[2];
body[i++] = sps[3];
body[i++] = 0xFF;
body[i++] = 0xe1;
body[i++] = (Sps_len >> 8) & 0xFF;
body[i++] = Sps_len & 0xFF;
for (size_t j = 0; J < Sps_len; J + +)
{
    body[i++] = sps[j];
}
body[i++] = 0x01;
body[i++] = (Pps_len >> 8) & 0xFF;
body[i++] = Pps_len & 0xFF;
for (size_t j = 0; J < Pps_len; J + +)
{
    body[i++] = pps[j];
}
Sendpacket (Rtmp_packet_type_video, body, I, ntimestamp);

Free (body);

Only the first audio and first frame video need to send header information. The frame data is then sent directly.
To send audio, simply precede the data frame with 2 byte header information:

Spec_info[0] = 0xAF;
SPEC_INFO[1] = 0x01;

When you send video, you need to identify the information in the header of the I p frame, as well as the length information of the video frame:

BODY = (unsigned char *) malloc (9 + size);
memset (body, 0, 9);
i = 0;
if (biskeyframe== 0) {
    body[i++] = 0x17;   1:iframe, 7:AVC
}
else {
    body[i++] = 0x27;   2:pframe, 7:AVC
}
//Avcvideopacket
body[i++] = 0x01;
body[i++] = 0x00;
body[i++] = 0x00;
body[i++] = 0x00;

Nalus
body[i++] = size >> & 0xff;
body[i++] = size >> & 0xff;
body[i++] = size >> 8 & 0xff;
body[i++] = size & 0xFF;
memcpy (&body[i], data, size);
Advanced rtmp Client interaction process with RTMP server Introduction
Streaming media that plays an rtmp protocol needs to go through: shaking hands, establishing links, setting up streams, playing/sending four steps. After the handshake is successful, you need to establish a "network link" between the client and the server during the link phase. The establish flow stage is used to establish a "network flow" between the client and the server. The playback stage is used to transfer audio and video data. Handshake (Handsshake)
The server and the client need to send a fixed three block of data, the following steps:
The handshake begins when the client sends C0, C1 blocks. The server sends S0 and S1 after receiving C0 or C1. After the client has received S0 and S1, it starts sending the C2. After the server has received C0 and C1, it starts sending S2. When the client and server receive S2 and C2 respectively, the handshake is complete.
build Link (netconnnet)
The client sends a connect to the server in the command message, requesting a connection to a service application instance. After the server receives the connection command message, it sends the confirmation window size (window acknowledgement size) protocol message to the client while connecting to the application that is mentioned in the connection command. The server sends the set bandwidth () protocol message to the client. After the client handles setting the bandwidth protocol message, it sends a confirmation window size (window acknowledgement size) protocol message to the server side. The server sends a stream begin message to the client in the user control message. The server sends a "result" (_result) in the command message informing the status of the client connection.
build Flow (NetStream)
The client sends the "create Stream" (createstream) command in the command message to the server side. When the server side receives the Create flow command, it sends a "result" (_result) in the command message informing the state of the client flow.
Play (play)
The client sends the "play" command in the command message to the server. After receiving the play command, the server sends a set block size (ChunkSize) protocol message. The server sends "Streambegin" in the user control message, informing the client of the flow ID. If the playback command succeeds, the server sends the "response status" NetStream.Play.Start & NetStream.Play.reset in the command message, informing the client that the "play" command was successfully executed. After this, the server sends the audio and video data that the client wants to play.
sending (send)
rtmpdump Source Code Analysis Handshake (Handsshake) static int Handshake (RTMP * r, int fp9handshake);

The handshake function is in:/rtmp/rtmplib/handshack.h.
./rtmp.c:69: #define RTMP_SIG_SIZE 1536

/*client handshake*/
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.