This article from: http://blog.163.com/chenhongswing@126/blog/static/1335924432011825104144612/
Mjpg-streamer source code analysis
10:42:57 | category: embedded applications | font size subscription
Author: Cao zhongming, a lecturer at Huaqing vision embedded College.
Mjpg-streamer is a good open-source project used for Video Servers. It uses v4l2 interfaces. We mentioned the porting process above, but in some specific cases this project cannot meet our needs, so we need to modify the source code, or directly write your own video server. Analyze the source code before modifying the source code or writing your own server.
There are three parts in this code that we need to know. The first is the v4l2 interface, the second is socket programming, and the third is multithreading programming.
1. v4l2 Interface Description
This involves how to extract data from the camera. First, we encapsulate a structure to describe the camera information, such as the width and height of the image, and the format of the image.
Struct vdin {
Int FD;
Char * videodevice;
Char * status;
Char * pictname;
Struct v4l2_capability cap;
Struct v4l2_format FMT;
Struct v4l2_buffer Buf;
Struct v4l2_requestbuffers RB;
Void * mem [nb_buffer];
Unsigned char * tmpbuffer;
Unsigned char * framebuffer;
Int isstreaming;
Int grabmethod;
Int width;
Int height;
Int FPS;
Int formatin;
Int formatout;
Int framesizein;
Int signalquit;
Int toggleavi;
Int getpict;
Int rawframecapture;
/* Raw frame capture */
Unsigned int filecounter;
/* Raw frame stream capture */
Unsigned int rfsframeswritten;
Unsigned int rfsbyteswritten;
/* Raw stream capture */
File * capturefile;
Unsigned int frameswritten;
Unsigned int byteswritten;
Int framecount;
Int recordstart;
Int recordtime;
};
Next, write the struct into the driver to initialize the camera. This operation is completed through IOCTL. The commands involved include vidioc_querycap, vidioc_s_fmt, vidioc_s_parm, vidioc_reqbufs, vidioc_querybuf, and MMAP.
Finally, we use the ioct command to read images. The commands involved include vidioc_qbuf and vidioc_dqbuf. Then, write the obtained data into the file as an image, and transmit the data continuously over the network as a video.
Ii. socket programming
A tcp socket is used in this program. Each connection request creates a thread to communicate with the request independently. The functions involved here include socket, bind, listen, accept, and write.
Iii. Multi-Thread Programming
To respond to requests from multiple clients at the same time, multithreading is used here to establish a connection for each request, and each connection is a thread. The involved functions include pthread_create, pthread_detach, pthread_cond_init, pthread_cond_destroy, pthread_mutex_init, and pthread_mutex_destroy.
Iv. mjpg-streamer Workflow
Http://sourceforge.net/apps/mediawiki/mjpg-streamer/index.php? Title = main_page
About the project
"Mjpg-streamer" is used to collect images from webcam cameras and transmit them to browsers such as firehox, cambozola, and vlc through an IP-based network in the form of streams, windows Mobile devices or other mobile devices with browsers. She can use some webcams hardware compression functions to reduce the server CPU overhead. She provides a lightweight and less CPU-consuming solution for embedded devices and some common servers, because she does not need to waste a lot of computing efficiency for video frame compression (this is handed over to hardware ). Example: on a 200 MHz router (for example, the Cisco classic wireless linkfs WRT54G route, which can run openwrt (mini Linux system) with a clock speed of MHz and 4 MRAM, 16 flash), stream code A 960x720 pixel video, she can reduce CPU usage by 10%.
Screenshot of the example webpage |
Testpicture frame #1 |
MJPEG-client written in Lazarus (free Pascal) |
VideoLAN can display the stream |
About the name
Some other suitable names are MJPEG-streamer or M-JPEG-streamer. Since we used MPEG-streamer in earlier projects, we decided to keep it as it is.
What it does
We need an input-plugin to provide images. Input-plugin is responsible for generating images and copying them to the memory. The corresponding output-plugin is responsible for extracting the images in the memory for subsequent processing. Most commonly used is WebServer-output-plugin, which allows you to transmit images to a web browser. Mjpg-streamer acts as an adhesive and connects this Single Input-plugin with many output-plugins, and almost all the work is handed over to these plug-ins.
Plugins
Input-plugins
Like other common input-plugin, these plug-ins copy JPEG images to the globally accessible memory, and then send signals to the waiting process (tell him: I'm coming! ).
Input_testpicture.so
This module has already been added to the image compilation process (just like its name: test), which means you can perform testing without a camera (If your compilation is correct ). He also provides you with a template, a template that you want to write your own input-plugin, because it is implemented as easy as possible. It is used to convert the JPEG-files file obtained by the testpictures module into a header file. This header file contains images compiled into the testpictures module (as mentioned earlier ). When activated, the above action will continue: Get-> change.
Input_uvc.so
As her name is, she captures images from devices compatible with the Linux-Uvc v4l2 standard. Zc0303 is made in China, such as Logitech quickcam sphere AF. Her source code is based on the open-source project "luvcview". Of course, many changes have been made. Unlike "luvcview", he can use the PAN/Tilt/focus command to initialize the camera of Logitech's home. Without the help of libraries, you can play with XML files or udev rules (the method in which device files appear in the system ). This type of input plug-in is very popular, thanks to its ability to encode photos into X pixels at a rate higher than 15 FPS (frame per second) without increasing the CPU load. If you have money, you don't have to worry about the CPU load. You can perform a 16 x pixel (strong) Non-compressed encoding, and then compress it with the software and upload it to the client!
Input_control.so
This module only implements the PAN/Tilt Control Interface, that is to say, he handed over the video stream function to other programs (in Linux, "concise" is a software function, efficiency is the highest, and the number of features is guaranteed ). He once provided a network interface to allow Pan/tilt my Logitech orbit af through the network. Of course, Skype is used to supplement the sound and video stream functions. When we were going to have a remote meeting via Skype, she worked very well and it was easy for them to control the camera when they were invisible.
Output-plugins
Output_http.so
This is definitely a full-version webserver that complies with the http1.0 standard. You can provide file access in a separate folder or execute some commands. For example, you can access the JPEG files obtained from the input plugin (see above) or encode them according to M-JPEG standards. Providing services from a folder means you can customize your own web pages, such as embedding images and Trojans. You can refer to the example in the mjpg-streamer package. It is important that multiple instances can run concurrently, which means that multiple server-threads processes can process different passwords (different requests, display layout or restricted commands.
This module stores JPEG images in a specified folder. You can capture images and only access them. Of course, you can also submit them to the FTP server by executing the command after the storage is complete! You can refer to more documents on the output_file page.
Moreproject status
Javascript motion detection
High Resolution webcam with 1600x1200 pictures
Usage