The simplest librtmp-based example: receive (RTMP is saved as FLV), librtmprtmp
This document records a libRTMP-based program for receiving streaming media: Simplest libRTMP Receive. This program can save the RTMP stream to FLV files at a cost. In fact, the program recorded in this article is a "streamlined" RTMPDump. RTMPDump has many functions, so the code is complicated, so many beginners do not know where to start. The program recorded in this article only retains the core functions in RTMPDump, making it easier for beginners to learn libRTMP.
The flowchart shows the execution flowchart of the function that uses librtmp to receive the RTMP stream.
The functions of key functions in the flowchart are listed as follows:
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_Connect (): establishes an RTMP connection and creates a NetConnection in the rtmp protocol specification.
RTMP_ConnectStream (): Creates a NetStream in the rtmp protocol specification.
RTMP_Read (): reads data from the server.
RTMP_Close (): Close the RTMP connection.
RTMP_Free (): Release the struct "RTMP ".
CleanupSockets (): Disable Socket.
NetStream and NetConnection are two logical structures in the RTMP protocol specification. NetStream is created on top of NetConnection. A NetConnection can contain multiple netstreams. Shows the relationship between them.
Source code
/*** Simplest Librtmp Receive, zhang Hui * leixiaohua1020@126.com * zhanghuicuc@gmail.com * China Media University/Digital television Technology * Communication University of China/Digital TV Technology * http://blog.csdn.net/leixiaohua1020 * this program is used to receive RTMP streaming media and save it locally as FLV file. * This program can receive rtmp live stream and save it as local flv file. */# include <stdio. h> # include "librtmp/rtmp_sys.h" # include "librtmp/log. h "int InitSockets () {WORD version; WSADATA wsaData; version = MAKEWORD (1, 1); return (WSAStartup (version, & wsaData) = 0 );} void CleanupSockets () {WSACleanup ();} int main (int argc, char * argv []) {InitSockets (); double duration =-1; int nRead; // is live s Tream? Bool bLiveStream = true; int bufsize = 1024*1024*10; char * buf = (char *) malloc (bufsize); memset (buf, 0, bufsize ); long countbufsize = 0; FILE * fp = fopen ("receive. flv "," wb "); if (! Fp) {RTMP_LogPrintf ("Open File Error. \ n "); CleanupSockets (); return-1;}/* set log level * // RTMP_LogLevel loglvl = RTMP_LOGDEBUG; // RTMP_LogSetLevel (loglvl ); RTMP * rtmp = RTMP_Alloc (); RTMP_Init (rtmp); // set connection timeout, default 30 s rtmp-> Link. timeout = 10; // HKS's live URL if (! RTMP_SetupURL (rtmp, "rtmp: // logs") {RTMP_Log (RTMP_LOGERROR, "SetupURL Err \ n"); RTMP_Free (rtmp); CleanupSockets (); return-1 ;} if (bLiveStream) {rtmp-> Link. lFlags | = RTMP_LF_LIVE;} // 1 hour RTMP_SetBufferMS (rtmp, 3600*1000); if (! RTMP_Connect (rtmp, NULL) {RTMP_Log (RTMP_LOGERROR, "Connect Err \ n"); RTMP_Free (rtmp); CleanupSockets (); return-1;} if (! RTMP_ConnectStream (rtmp, 0) {RTMP_Log (RTMP_LOGERROR, "ConnectStream Err \ n"); RTMP_Free (rtmp); RTMP_Close (rtmp); CleanupSockets (); return-1 ;} while (nRead = RTMP_Read (rtmp, buf, bufsize) {fwrite (buf, 1, nRead, fp); countbufsize + = nRead; RTMP_LogPrintf ("Receive: % 5 dByte, total: % 5.2fkB \ n ", nRead, countbufsize * 1.0/1024);} if (fp) fclose (fp); if (buf) {free (buf );} if (rtmp) {RTMP_Close (rtmp); RTMP_Free (rtmp); CleanupSockets (); rtmp = NULL;} return 0 ;}
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.