Use Rtmpdump (librtmp) to live camera data from V4L2

Source: Internet
Author: User
Tags printf usleep

RTMP is real time Messaging Protocol (Realtime Message Transfer Protocol), Rtmpdump encapsulates some of the RTMP protocol interfaces, making it easier for users to use the RTMP protocol. For the use of rtmpdump, you can refer to the blog

Linux system Rtmpdump (LIBRTMP) publishes H264 data via rtmp

Linux system Rtmpdump (LIBRTMP) publishes FLV data via rtmp

In the RTMP experiment, you need to build the rtmp file and a rtmp server, in particular, you can refer to:

Linux compiled installation Trmpdump (LIBRTMP)

Nginx to build rtmp streaming media server

To implement the rtmp live V4L2 camera data, the simplest solution is to use the FFmpeg, which has implemented all the functions, the user can only use a single command. However, for some embedded devices, there is not enough hardware resources to run FFmpeg, in this case, you can only implement all the required interfaces. This article mainly introduces in the Linux system, the user collects the camera data through the V4L2, then carries on the x264 encoding to the camera data, then uses the Rtmpdump library through the RTMP protocol to push the encoded H264 data to the RTMP server, Finally, the client pulls the data down from the RTMP server, decodes it, and finally shows it, and realizes the real-time broadcast. The main work flow chart is as follows:


About V4L2 acquisition camera data and encoded into H264, you can refer to: V4L2 Video collection and coding--Learning directory and summary

The main program code is as follows:

/*============================================================================= * FileName:main.cpp * Des C: * Author:licaibiao * lastchange:2017-05-8 * ======================================================= ======================*/#include "x264_encoder.h" #include "v4l2_device.h" #include "librtmp/log.h" #include <
signal.h> #include <error.h> int runflag=0;
        static void Sig_user (int signo) {if (signo==sigint) {runflag=0;
  printf ("Received sigint\n");
	}} void Rtmp_push_v4l2 () {char url[]= "rtmp://192.168.0.5:1935/live";
	int fps = 30;
	int rate = 333;
	int width = 640;
	int height = 480;

	int outsize = 1024;	
	int index=0;
	unsigned int tick = 0;
	unsigned int tick_gap = 1000/fps;
	uint32_t now=0;

	uint32_t last_update=0;
	int FD;
	int len = 0;
	uint8_t *cam_buf;

	uint32_t PixelFormat;
	Cam_buf = (uint8_t*) malloc (1024*1024*3);
	
	memset (cam_buf, 0, 1024*1024*3);
	
    PixelFormat = V4L2_PIX_FMT_YUYV; if (Signal (Sigint,sig_user) ==sig_err) {perror ("catch SIGINT ERR");
	} FD =open_camera ();
	Init_camera (fd, width, height);
		
	Start_capture (FD);
	Rtmp_createpublish (Url,outsize,1,rtmp_loginfo);
	printf ("Connected \ n");
	Rtmp_initvideoparams (Width,height,fps, rate, pixelformat,false);
	printf ("inited \ n");
	runflag=1;
		
	runflag=3;
			while (Runflag) {if (index!=0) {rtmp_sendscreencapture (Cam_buf,height,tick, PixelFormat, width, height);
		printf ("Send frame index--%d\n", index);

		} last_update=rtmp_gettime ();

		Read_frame (FD, Cam_buf,&len);
		Tick +=tick_gap;
		
		Now=rtmp_gettime ();
		Usleep ((tick_gap-now+last_update) *1000);
		Usleep (1000);
	index++;
	} free (CAM_BUF);
	Stop_capture (FD);
	Close_camera_device (FD);
Rtmp_deletepublish ();
	} int main () {rtmp_push_v4l2 ();
return 0;

 }
I am using a 300,000 megapixel camera, that is, the output image size is 640*480, it can support the output of MJPEG and YUV422 two data formats, because x264 encoding is required, so I set the output YUV422 (YUYV) format here. I built my own rtmp server with the address: rtmp://192.168.0.5:1935/live.

There are a few points to note:

1. When sending data, it is necessary to set the appropriate frame rate, because on some platforms, it may take more time to encode, and can not reach the frame rate of the initialization setting, so that there will be problems when displayed.
2. The client is required to request data from the server before pushing the H264 data to the servers, otherwise there will be very noticeable image delay, about 2-3 seconds.

The project catalog is as follows:

-bash-4.1# Tree
.
├──include
│├──librtmp
││├──amf.h
││├──bytes.h
││├──dhgroups.h
││├──d H.H
││├──handshake.h
││├──http.h
││├──log.h
││├──rtmp.h
││└──rtmp _sys.h
│├──librtmp_send264.h
│├──sps_decode.h
│├──v4l2_device.h
│├──x264_config.h
  
   │├──x264_encoder.h
│└──x264.h
├──lib
│├──librtmp.a
│└──libx264.a
├──librtmp_se Nd264.cpp
├──main.cpp
├──makefile
├──v4l2_device.cpp
└──x264_encoder.cpp
  
The program runs as follows:

-bash-4.1#./test Camera driver name is:uvcvideo camera device name IS:UVC camera (046d:0825) camera bus information : usb-0000:00:1a.0-1.1 Support Device 1.YUV 4:2:2 (YUYV) support device 2.MJPEG N_buffer = 4 connected x264 [WARNING]: VBV maxrate specified, but no bufsize, ignored x264 [INFO]: Using CPU capabilities:mmx2 sse2fast SSSE3 SSE4.2 AVX x264 [i     NFO]: 4:2:2, Level 3.0, 4:2:2 8-bit inited ^creceived SIGINT Select received SIGINT x264 [INFO]: Frame i:1 Avg qp:36.90 size:3106 x264 [INFO]: Frame p:55 Avg qp:25.29 size:1070 x264 [INFO]: MB I I16. 4:25.9% 72.0% 2.1% x264 [INFO]: MB P I16. 4:3.7% 2.5% 0.0% P16.  4:20.0% 4.0% 0.6% 0.0% 0.0% skip:69.1% x264 [INFO]: Final ratefactor:24.32 x264 [INFO]: 8x8 transform intra:47.2% inter:32.7% x264 [INFO]: coded y,uvdc,uvac intra:18.5% 41.6% 7.7% inter:2.0% 10.7% 0.0% x264 [INFO]: i16 v,h,dc,p:27%  51% 12% 11% x264 [INFO]: i8 v,h,dc,ddl,ddr,vr,hd,vl,hu:13% 45% 30% 2% 1% 1% 3% 1%4% x264 [INFO]: I4 v,h,dc,ddl,ddr,vr,hd,vl,hu:20% 54% 12% 2% 2% 2% 4% 1% 4% x264 [INFO]: i8c dc,h,v,p:61% 19% 17% 2% x264 [INFO]: Weighted p-frames:y:0.0% uv:0.0% x264 [INFO]: kb/s:265.47
The client uses VLC player directly, the effect is as follows:




Need a full project download link: Use Rtmpdump (librtmp) to stream camera data from V4L2


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.