Raspberry Pi_Eng_22.1.2 Processing API for Raspberry Pi Camera


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


22.1.2  Processing API for Raspberry Pi Camera

 

In the Raspberry Pi system, MMAL method and V4L2 method can be used as processing API for the Raspberry Pi camera function.

 

22.1.2.1    MMAL (Multi-Media Abstraction Layer)

 

The Raspberry Pi A+/B+ system uses a BCM2835 system board made by the Broadcom, where the graphics processing GPU is VideoCore .

 

MMAL (Multi-Media Abstraction Layer) is a Broadcom-specific API that can be used only in systems equipped with Videocore IV. The MMAL API is an API that is based on OpenMAX, which is easier to handle system rather than using OpenMAX. The MMAL API is a low-level API as compared to other APIs, enabling to process more detailed graphics and utilizing higher functionality in terms of performance.

 

In Raspberry Pi, you can use the following as the camera tool that uses MMAL API:

    Motion                  -- Detect and record motion in video.

    G-Streaming            -- Transfer video to another computer on the network

    VLC                       -- Broadcast video on the Web

 

For more detailed information on MMAL, please refer to the followings:

    http://www.jvcref.com/files/PI/documentation/html/index.html#features  


 

22.1.2.2    V4L2 (Video For Linux2)

 

   V4L (Video For Linux) and V4L2(Video For Linux 2)

 

V4L (Video For Linux) is a standard API for Linux that processes functions for various video devices such as TV tuners and USB web cameras. The first version of V4 (Video For Linux) interface was developed Linux 2.2 by Alan Cox. The second version, V4L2 (Video For Linux 2) interface began development in 1999, fixed bugs found in V4L, and supported more devices.

 

The V4L2 driver runs like a normal kernel module. It can be loaded by an administrator or automatically loaded when opening a device file. Drivers use the default "videodev" kernel module. This kernel module matches the kernel device for working with video devices.

 

   UV4L (Userspace Video For Linux) for Raspberry Pi

 

In the early days of Raspberry Pi, the V4L function was not available. So the unofficial V4L (Video4Linux) created by a particular developer group to use V4L on Raspberry Pi is the UV4L (Userspace Video4Linux) driver. This driver is not included in the firmware of the system but can be installed separately, so it is called "Userspace". With this driver, many standard Linux applications developed for USB webcams on Linux can be used in Raspberry Pi.

 

 

   V4L2 (Video For Linux 2) for Raspberry Pi

 

As Raspberry Pi became popular, Raspberry Pi Foundataion developed a kernel-space V4L2 driver for Raspberry pi in cooperation with Broadcom because it needed to include the V4L2 (Video4Linux2) function in the system's firmware.

 

The recently released Raspberry Pi includes the V4L2 driver in the kernel, and the Raspbian image includes V4L2 utilities such as V4L2-ctl as default. Since this driver is included and operated in the firmware, it does not need to be installed separately and it shows better performance than UV4L. With this driver, many standard Linux applications developed for USB webcams on Linux can be used in Raspberry Pi.

 

The V4L2 driver can interface through devices file like "/dev/video0" ~ "/dev/videon", which are not visible when the system is first installed. If you check as shown below, there is no such device.

 

pi@raspberrypi ~ $ ls /dev/video*

ls: cannot access /dev/video*: No such file or directory

 

This driver is provided as part of the firmware, but it is in the disabled status, so if you want to use that function, you have to activate the driver. Activate with "modprobe" command as follows. A detailed description of how to use "modprobe" command is beyond the scope of this article, so if you need more information, please refer to additional documents.

 

sudo modprobe bcm2835-v4l2

 

If you check the device again, you can see that the device is activated as shown below.

 

pi@raspberrypi ~ $ sudo ls /dev/video*

/dev/video0

 

Now let's see if the "video0" device is working properly with "dd" command. Here, we read some data from the "video0" device and save it in another file as follows. You can see that "test_snap2.jpg" file is created.

 

pi@raspberrypi ~/Image_Camera $ dd if=/dev/video0 of=test_snap2.jpg bs=64M count=1

0+1 records in

0+1 records out

152064 bytes (152 kB) copied, 0.581062 s, 262 kB/s

pi@raspberrypi ~/Image_Camera $ ls -l

-rw-r--r-- 1 pi pi 2799282 May 13 14:06 test_snap1.jpg

-rw-r--r-- 1 pi pi  152064 May 16 23:07 test_snap2.jpg

 

However, the activation of the V4L2 driver is valid only while the system is in operation and will not be maintained in the next booting, so it must be re-enabled each time the system boots. If you want the driver to be automatically activated every time the system boots, you can set and use the particular contents in the "/etc/rc.local" file. See the description of [20.3.3 Creating Init Script] for information on how to set up using the init scrip.

 

In Raspberry Pi, you can use the followings as the camera tool that uses V4L2 API:

    Motion                  -- Detect and record motion in video

    Open CV

    VLC --RTSP method -- Broadcast video on the web

    Mjpg-streamming     -- Broadcast video on the web

    luvcview                 -- Broadcast video on the web