Author: Cao Zhongming, huaqing Vision Embedded College lecturer.
Mjpg-streamer is a good open source project to use as a video server, using the V4l2 interface. We talked about the transplant process earlier, but in certain circumstances the project does not meet our needs, so we need to modify the source code, or directly write a video server. Before you modify the source code or write your own server, we analyze the source codes.
There are three parts in this code that we need to master, the first is the V4l2 interface, the second is socket programming, and the third is multithreaded programming.
First, V4L2 interface description
Here's how we get the data out of the camera, the first is to encapsulate a structure that describes the camera's information, such as the width of the image, the format of the image, and so on.
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;
};
The structure is then written to the driver to initialize the camera. This operation is done through the IOCTL, involving commands including Vidioc_querycap, VIDIOC_S_FMT, Vidioc_s_parm, Vidioc_reqbufs,vidioc_querybuf, and the mapping of memory through MMAP.
Finally, we complete the image reading through the IOCT command, which involves the vidioc_qbuf and vidioc_dqbuf. Then writes the obtained data to the file is the picture, transmits through the network the continuous picture is the video.
Second, socket programming
In this program, a TCP socket is used, and each connection request creates a single thread that communicates with the request, including sockets, bind, listen, accept, and write.
Three, multi-threaded programming
To be able 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 functions involved include pthread_create, Pthread_detach, Pthread_cond_init, Pthread_cond_destroy, Pthread_mutex_init, Pthread_ Mutex_destroy.
Iv. Mjpg-streamer Work Flow
Original: Http://sourceforge.net/apps/mediawiki/mjpg-streamer/index.php?title=Main_Page
About the project
"Mjpg-streamer" is used to capture images from the webcam camera and stream them to a browser such as a FIREHOX,CAMBOZOLA,VLC player, Windows Mobile device, or other mobile device that has a browser, in the form of streaming through an IP-based network. She can use some webcams hardware compression capabilities to reduce the overhead of the server CPU. She provides a lightweight and less CPU-intensive solution for embedded devices and some regular servers because she doesn't need to waste a lot of computational efficiency for video frame compression (this is done to the hardware). Examples are: on a 200MHz router (an example is the Cisco Classic Wireless Linkfs WRT54G route, which can run OpenWrt (mini Linux system), the frequency 200mhz,4mram,16flash), Stream encoding 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, because Mpeg-streamer was used in earlier projects, so we decided to stay the same.
What it does
We need a input-plugin to provide the picture. Input-plugin is responsible for generating images and copying them into memory. The corresponding Output-plugin is responsible for taking these images out of memory for subsequent processing. The most common is webserver-output-plugin, which allows images to be transferred to a Web browser. Mjpg-streamer acts as a binder, connecting this single input-plugin with numerous output-plugin, and almost all of the work is given to these plugins.
Plugins
Input-plugins
Like other normal input-plugin, these plug-ins work by copying images in JPEG format to globally accessible memory, and then sending a signal to the waiting process (tell him: I'm coming.) Want to do ha-ha bar).
Input_testpicture.so
The module was compiled with a picture (as its name: test), which means that you can do the testing without the camera (you compiled the right). He also provides you with a template, a template that you want to write your own input-plugin, because he is implemented as easy as possible to understand. Its role is to convert the Jpeg-files file obtained from the Testpictures module into a header file that contains some images that have been compiled into the Testpictures module (as mentioned earlier). When activated, the move is repeated over and over: the change is obtained.
Input_uvc.so
As its name, she grabs pictures from devices that are compatible with LINUX-UVC V4L2 standards. Like Logitech Quickcam Sphere AF et cetera domestic has ZC0303. Her source code is based on the Open source project "Luvcview", of course, made a lot of changes. Unlike "Luvcview", he initializes the camera with Logitech's home with the Pan/tilt/focus command. Without the help of a library, fiddling with XML files or Udev rules (the way the device files appear in the system). This input plug-in is now popular, thanks to its ability to encode photos into 960x720 pixel sizes at a rate greater than 15fps (frame per second) without increasing CPU load. If you have the money, do not consider the CPU load problem, can be 1600x1200 pixel (strong) uncompressed encoding, and then the software to achieve compression and upload to the client.
Input_control.so
This module only implements the control interface of the pan/tilt, that is, he gave the video streaming function to other programs (Linux is highly respected "concise", is a software a function, efficiency first, the function has the software quantity guarantee). I used him to provide a network interface to allow pan/tilt my Logitech Orbit AF over the network, and Skype, of course, added the functionality of audio and video streaming. When we go to a remote conference via Skype, she works very well and the invisible hand is convenient for them to control the camera.
Output-plugins
Output_http.so
This is definitely a full version of the webserver that complies with the HTTP1.0 standard. You can provide file access in a separate folder, or you can execute some commands. As an example, you can access the JPEG files obtained from the input plugin (see above) or encode them according to the M-JPEG standard. The ability to provide services from a single folder means that you can customize your own web pages, such as embedding images and gods and horses. You can refer to the example in the Mjpg-streamer package. What's important is that multiple instances can run concurrently, which means that multiple server-threads processes can handle different passwords (different requests) Ah, the rendered layout, or the command with restrictions.
This module is used to store JPEG images in the specified folder. You can use it to crawl images and just access them, but you can also submit them to the FTP server by executing commands after the storage is complete. You can see more documents on the Output_file page.
Moreproject Status
JavaScript Motion Detection
High resolution webcam with 1600x1200 pictures
Usage