The simplest example based on librtmp: Release H.264 (H.264 via RTMP), librtmprtmp
This article records a program that publishes the H.264 code stream based on libRTMP. This program can publish H.264 data to the RTMP Streaming Media Server. The current example is not very stable, and the next step is to be modified.
This program uses the callback function as the input. Through the custom callback function, you can send local files or data in the memory.
Function call Structure
The function call structure of this program is as follows.
The entire program contains three interface functions:
RTMP264_Connect (): establishes an RTMP connection.
Rtmp1__send (): send data.
RTMP264_Close (): Close the RTMP connection.
You can call the preceding three interface functions in sequence to send the H.264 code stream.
The functions of key functions in the structure diagram are listed as follows.
RTMP264_Connect () contains the following functions:
InitSockets (): Initialize Socket
RTMP_Alloc (): allocates memory for the struct "RTMP.
RTMP_Init (): Initialize the member variable in the struct "RTMP.
RTMP_SetupURL (): Set the input RTMP connection URL.
RTMP_EnableWrite (): it must be used to publish a stream. If this parameter is not used, the system receives the stream.
RTMP_Connect (): establishes an RTMP connection and creates a NetConnection in the rtmp protocol specification.
RTMP_ConnectStream (): Creates a NetStream in the rtmp protocol specification.
Rtmp1__send () contains the following functions:
ReadFirstNaluFromBuf (): Read the first NAL unit from the memory.
ReadOneNaluFromBuf (): Read a NAL unit from the memory.
Hsf-_decode_sps (): decodes SPS to obtain video width, height, and frame rate information.
Sendh1_packet (): sends an NAL unit.
Sendh1_packet () contains the following functions:
SendVideoSpsPps (): If it is a key frame, SPS and PPS are sent before the frame is sent.
SendPacket (): assemble an RTMPPacket and call RTMP_SendPacket () to send it out.
RTMP_SendPacket (): sends an RTMP data RTMPPacket.
RTMP264_Close () contains the following functions:
RTMP_Close (): Close the RTMP connection.
RTMP_Free (): Release the struct "RTMP ".
CleanupSockets (): Disable Socket.
Source code
The three interface functions provided by the program are used as follows. It can be seen that the interface is relatively simple.
/*** Simplest Librtmp Send 264 *** Lei Xiaohua, zhang Hui * leixiaohua1020@126.com * zhanghuicuc@gmail.com * China Communications University/Digital television Technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * this program is used to put the H in memory. 264 data is pushed to the RTMP Streaming Media Server. * This program can send local h264 stream to net server as rtmp live stream. */# include <stdio. h> # include "librtmp_send264.h" FILE * fp_send1; // callback function for reading files // we use this callback function to read data from bufferint read_buffer1 (unsigned char * buf, int buf_size) {if (! Feof (fp_send1) {int true_size = fread (buf, 1, buf_size, fp_send1); return true_size;} else {return-1 ;}} int main (int argc, char * argv []) {fp_send1 = fopen ("cuc_ieschool.h264", "rb"); // initialize and connect to the server rtmp1__connect ("rtmp: // localhost/publishlive/livestream "); // send rtmp1__send (read_buffer1); // disconnect and release related resources rtmp1__close (); return 0 ;}
There are a lot of code in the interface functions, so they are not recorded in detail.
Download
Simplest LibRTMP Example
SourceForge project: https://sourceforge.net/projects/simplestlibrtmpexample/
CSDN download: http://download.csdn.net/detail/leixiaohua1020/8291757
This project contains the usage example of LibRTMP, including the following sub-projects:
Simplest_librtmp_receive: receives RTMP streaming media and saves it as a FLV file locally.
Simplest_librtmp_send_flv: pushes video and audio files in FLV format to the RTMP Streaming Media Server using RTMP.
Simplest_librtmp_send264: pushes H.264 data in the memory to the RTMP Streaming Media Server.