Linux system Rtmpdump (LIBRTMP) publishes H264 data via rtmp __linux

Source: Internet
Author: User
Tags goto

About the use of rtmpdump, many of the applications in the Windows platform, Thor has to do a series of analysis, but Raytheon is mainly based on the Windows platform. The main task of this article is to migrate the project from the simplest librtmp example: Release H.264 (H.264 through Rtmp) to the Linux system, while fixing some problems and adding a description.

Before using the project, there should be a rtmp server and a better understanding of the H264 data format. can refer to:

Nginx build rtmp Streaming Media server

H264 grammatical structure and coding principle

The entire software flowchart is as follows:


Careful analysis of the Rtmp264_send function

/** * H.264 encoded video data in memory is sent to the server * * @param read_buffer callback function, when the data is insufficient, the system automatically calls the function to get input data. * 2 Parameters: * uint8_t *buf: External data sent to the address * int buf_size: External Data size * Return value: Successfully read memory size * @ Success returns 1, Failure returns 0 */int R
	Tmp264_send (int (*read_buffer) (unsigned char *buf, int buf_size)) {int ret;
	  
	uint32_t now,last_update;
	memset (&metadata,0,sizeof (rtmpmetadata));
	memset (m_pfilebuf,0,buffer_size);
	if ((Ret=read_buffer (m_pfilebuf,m_nfilebufsize)) <0) {return FALSE;  
	} Naluunit Naluunit;  
	Read SPS frame Readfirstnalufrombuf (Naluunit,read_buffer);  
	Metadata.nspslen = naluunit.size;
	Metadata.sps=null;
	metadata.sps= (unsigned char*) malloc (naluunit.size);

	memcpy (metadata.sps,naluunit.data,naluunit.size);  
	Read PPS frame readonenalufrombuf (naluunit,read_buffer); 
	Metadata.nppslen = naluunit.size;
	Metadata.pps=null;
	metadata.pps= (unsigned char*) malloc (naluunit.size);
	
	memcpy (metadata.pps,naluunit.data,naluunit.size); Decode SPS, get video image wide, high information int width =0,height = 0, fps=0;  
	H264_decode_sps (Metadata.sps,metadata.nspslen,width,height,fps);  
	Metadata.nwidth = width;  
	Metadata.nheight = height; 
	if (fps) metadata.nframerate = fps;

	else Metadata.nframerate = 25;
	Send Pps,sps//ret=sendvideospspps (Metadata.pps,metadata.nppslen,metadata.sps,metadata.nspslen);

	if (ret!=1)//return FALSE;  
	unsigned int tick = 0; 
	unsigned int tick_gap = 1000/metadata.nframerate;
	printf ("nframerate =%d \ n", metadata.nframerate);
	Readonenalufrombuf (Naluunit,read_buffer); int bkeyframe = (Naluunit.type = = 0x05)?
	True:false; while (Sendh264packet (Naluunit.data,naluunit.size,bkeyframe,tick)) {got_sps_pps://if (naluunit.size==8581) PRI
		NTF ("Nalu size:%8d\n", naluunit.size);
		Last_update=rtmp_gettime (); if (!
		Readonenalufrombuf (Naluunit,read_buffer)) goto end;
		if (Naluunit.type = = 0x07 | | naluunit.type = = 0x08) goto Got_sps_pps; Bkeyframe = (Naluunit.type = = 0x05)?
		True:false;
		Tick +=tick_gap; Now=rTmp_gettime ();
		Msleep (tick_gap-now+last_update);  
		printf ("now =%d, last_update =%d, now-last_update =%d \ n", now,last_update,now-last_update);
	Msleep (TICK_GAP);
	} end:free (Metadata.sps);
	Free (METADATA.PPS);  
return TRUE;  
 }
Read the SPS and PPS for the H264 file before you begin looping through the data. The SPS is a set of sequence parameters and the PPS is the set of image parameters. In the SPS sequence parameter set, the information such as width, height and frame rate of the image can be resolved. In the H264 file, the first two frames of data are SPS and PPS, and this h264 file only has one SPS frame and one PPS frame. When making a rtmp transmission, it needs to send an SPS sequence parameter set frame and a PPS image parameter set frame before sending the H264 I frame each time. The way we do this is to extract the SPS and PPS frames First, then save them, and then send the SPS and PPS frames once each time I frame is sent. SPS frames and PPS frames are as follows:


The project documents are as follows:

lcb@ubuntu:~/test/rtmp/rtmp_push_h264$ Tree
.
├──cuc_ieschool.h264
├──include
│├──librtmp
││├──amf.h
││├──bytes.h
││├──DH Groups.h
││├──dh.h
││├──handshake.h
││├──http.h
││├──log.h
││├──r Tmp.h
││└──rtmp_sys.h
│├──librtmp_send264.h
│└──sps_decode.h
├──lib
│├──LIBRT Mp.a
│├──librtmp.so
│└──librtmp.so.0
├──librtmp_send264.cpp
├──makefile
├──simplest_l Ibrtmp_send264.cpp
└──SRC

Complete Project Download Address: Rtmpdump (LIBRTMP) publishes H264 data through rtmp

Related Article

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.