mjpg-streamer framework analysis

The mjpg-streamer process frame diagram is as follows:

After the program runs, the main process opens the corresponding input and output dynamic link library according to the input and output channels set by the incoming parameters, and sequentially calls the following functions

1. Input-warehouse-output (mjpg-streamer.h)

(1) global structure

stop is the stop flag, when set to 1, the program stops running;

db and db_update are respectively mutex lock and condition variable, the two are used for thread synchronization, when the input channel puts the data into the warehouse, the output channel is notified to fetch the data

The warehouse corresponds to the buf pointer, and the size of the warehouse is size.

The output corresponds to the output structure (output plugin)

outcnt indicates that there are several ways in the current output channel

(2) mjpg-streamer-r63 file

       The .so file is a plug-in, which may be an input plug-in (with the word input in front) or an output plug-in (with an output plug in front). 

When running mjpg_streamer, specify the input and output channels through the command line mjpg_streamer -i "input_uvc.so -f 10 -r 320 * 240" -o "output_http.so -w www"

       Since a USB camera is used to collect data, the input_uvc.so input plug-in is selected as the input channel (as shown above, specify which plug-in as the input channel through -i).

      Since we want to use the webpage display, we choose the output_http.so output plugin as the output channel. (As shown in the figure above, specify which plugin is used as the output channel through the -o option)

(3) The main function parses the command line -i "input_uvc.so -f 10 -r 320 * 240 " to open the corresponding input_uvc.so dynamic link library

Open the dynamic link library through the dlopen function

(4) The main function opens the corresponding out_http.so dynamic link library by parsing the command line -o "output_http.so -w www"

2. The work done by the input plugin and output plugin

(1) Input plugin 1 (input_uvc.c file)

input structure

(2) Input plugin 2

init function

Mainly execute the init_videoIn function

Input parameters width width, height height, frame rate fps, format format

The work done by init_videoIn: ① call the init_v4l2 function (through a series of ioctl provided by V4L2 to set the camera resolution, frame rate, and camera output format, and allocate the buffer and then map to the application space videoIn-> mem [i ]); ② Allocate a temporary buffer videoIn-> tmpbuffer for receiving camera data (camera data flow direction is: videoIn-> mem [i] -----> videoIn-> tmpbuffer -----> globals- > buf (we call warehouse) ----------> output channel to take out data and send it out)

run function

Mainly execute the cam_thread function in the creation thread function parameters

Work done: call uvcGrab function (this function copies a frame of data from copy videoIn-> mem [i] to videoIn-> tmpbuffer) to get a frame of data

Determine the format of the obtained data. If the input data format is MJPEG format, copy it directly to the buf of the global structure (that is, put it in the warehouse); if it is YUV format, you must convert the YUV format data into MJPEG format data. Copy to the buf of the global structure (that is, put it in the warehouse globals-> buf)

stop function

Do some cleanup

 

(3) Output plugin 1

output structure

 

(4) Output plugin 2

init function

Socket programming and use socket to simulate http protocol, so do some initialization of socket, such as port number

run function

Remove the data from the warehouse, store it in a buffer, and then send the data out through the write function for the mobile phone or other devices to receive

stop function

Do some cleanup

 

Reference link for this article:

https://blog.csdn.net/qingkongyeyue/article/details/52400036

https://blog.csdn.net/qingkongyeyue/article/details/52401514

https://blog.csdn.net/qingkongyeyue/article/details/52824165

Published 42 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/qq_37659294/article/details/104279995