Simplest LIBRTMP-based example: Receive (rtmp saved as FLV)

Source: Internet
Author: User
Tags flv file

=====================================================

The simplest example series based on Librtmp is a list of articles:

Simplest LIBRTMP-based example: Receive (rtmp saved as FLV)

Simplest LIBRTMP-based example: Publish (FLV is released via RTMP)

Simplest LIBRTMP-based example: release of H. (H. rtmp Post)

=====================================================

This document records a LIBRTMP-based program for receiving streaming media: simplest librtmp receive. The program can save rtmp streams to the cost of FLV files. In fact, the procedure documented in this paper is a "streamlined" rtmpdump. Rtmpdump function More, so its code is more complex cause many beginners do not know where to start. This document recorded in this program only retains the rtmpdump of the core functions, more convenient for beginners to learn librtmp.   Flowchart The function execution flowchart that uses librtmp to receive RTMP streams is shown in the following figure. The function of the key function in the

flowchart is as follows:
Initsockets (): Initialize socket
Rtmp_alloc (): Allocates memory for the structure "RTMP".
Rtmp_init (): Initializes a member variable in the structure "RTMP".
Rtmp_setupurl (): Sets the URL of the input RTMP connection.
Rtmp_connect (): Establish an RTMP connection to create 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 (): Turn off RTMP connections.
Rtmp_free (): Releases the structure "RTMP".
Cleanupsockets (): Closes the socket.
 
where NetStream and Netconnection are the two logical structures in the RTMP protocol specification. NetStream is built on netconnection. A netconnection can contain multiple netstream. The relationship between them is shown in the following figure.

 
 
source code

/** * Simplest librtmp Receive * * Lei Hua, Zhang Hui * leixiaohua1020@126.com * zhanghuicuc@gmail.com * Communication University/Digital TV Technology * Commun Ication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program is used to receive rtmp streaming media and save locally as an FLV
 Type of 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 stream?				
	
	
	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 30s rtmp->link.timeout=10; HKS ' s Live URL if (!
		Rtmp_setupurl (rtmp, "rtmp://live.hkstv.hk.lxdns.com/live/hks")) {Rtmp_log (Rtmp_logerror, "Setupurl Err\n");
		Rtmp_free (RTMP);
		Cleanupsockets ();
	return-1;
	} if (Blivestream) {rtmp->link.lflags|=rtmp_lf_live;		
	
	}//1hour 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_close (RTMP);
		Rtmp_free (RTMP);
		Cleanupsockets ();
	return-1;

		} while (Nread=rtmp_read (rtmp,buf,bufsize)) {fwrite (BUF,1,NREAD,FP);
		Countbufsize+=nread;
	rtmp_logprintf ("Receive:%5dbyte, 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; }


Run Results

After the program runs, the live stream with the url "rtmp://live.hkstv.hk.lxdns.com/live/hks" (actually Hong Kong TV) is saved locally as "receive.flv". Saved files can be viewed using the player.


Download
simplest librtmp Example

Project Home

sourceforge:https://sourceforge.net/projects/simplestlibrtmpexample/

Github:https://github.com/leixiaohua1020/simplest_librtmp_example

Open source China: http://git.oschina.net/leixiaohua1020/simplest_librtmp_example


CSDN Download: http://download.csdn.net/detail/leixiaohua1020/8291757

This project contains examples of the use of librtmp, including the following sub-projects:
Simplest_librtmp_receive: Receive rtmp streaming and save files in FLV format locally.
SIMPLEST_LIBRTMP_SEND_FLV: The FLV format av file is pushed to the rtmp streaming media server using rtmp.
simplest_librtmp_send264: Pushes the in-memory data to the RTMP streaming media server.

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.