Last edited 2 weeks ago

How to use a Webcam camera

Applicable for STM32MP13x lines, STM32MP15x lines, STM32MP21x lines, STM32MP23x lines, STM32MP25x lines


1. Article purpose[edit | edit source]

This article aims to present how to handle USB Webcam supported by Linux UVC[1] framework using v4l2-ctl and gst-launch utilities.

2. Linux compatibility[edit | edit source]

After having plugged an USB Webcam on board, first check if it is well recognized by UVC framework, here is an example with Hercule Nexigo 930 full-HD Webcam:

dmesg | grep -i uvc
[   11.754469] usb 1-1.3: Found UVC 1.00 device NexiGo N930P FHD Webcam (1bcf:2283)

If recognized, the camera is exposed through a V4L2 video device node:

v4l2-ctl --list-devices
[...]
NexiGo N930P FHD Webcam: NexiGo (usb-482f0000.usb-1.3):
       /dev/video7
       /dev/video8
       /dev/media3

Two video nodes are exposed but only the first one is a video capture node which output pictures:

v4l2-ctl -d /dev/video7 -D
Driver Info:
       Driver name      : uvcvideo
       Card type        : NexiGo N930P FHD Webcam: NexiGo
       Bus info         : usb-482f0000.usb-1.3
       Driver version   : 6.6.116
       Capabilities     : 0x84a00001
               Video Capture
               Metadata Capture
               Streaming
               Extended Pix Format
               Device Capabilities
       Device Caps      : 0x04200001
               Video Capture
               Streaming
               Extended Pix Format
Media Driver Info:
       Driver name      : uvcvideo
       Model            : NexiGo N930P FHD Webcam: NexiGo
       Serial           : 20200804001
       Bus info         : usb-482f0000.usb-1.3
       Media version    : 6.6.116
       Hardware revision: 0x00000804 (2052)
       Driver version   : 6.6.116
Interface Info:
       ID               : 0x03000002
       Type             : V4L Video
Entity Info:
       ID               : 0x00000001 (1)
       Name             : NexiGo N930P FHD Webcam: NexiGo
       Function         : V4L2 I/O
       Flags            : default
       Pad 0x01000007   : 0: Sink
         Link 0x02000013: from remote pad 0x100000a of entity 'Extension 4' (Video Pixel Formatter): Data, Enabled, Immutable

The second video node only exposes some metadatas:

v4l2-ctl -d /dev/video8 -D
[...]
       Device Caps      : 0x04a00000
               Metadata Capture
               Streaming
               Extended Pix Format
[...]

3. Get the supported format, resolution and framerate[edit | edit source]

USB Webcam usually supports only discrete formats, resolutions and framerate:

v4l2-ctl -d /dev/video7 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
       Type: Video Capture
       [0]: 'MJPG' (Motion-JPEG, compressed)
               Size: Discrete 640x480
                       Interval: Discrete 0.033s (30.000 fps)
                       Interval: Discrete 0.040s (25.000 fps)
                       Interval: Discrete 0.050s (20.000 fps)
                       Interval: Discrete 0.067s (15.000 fps)
                       Interval: Discrete 0.100s (10.000 fps)
                       Interval: Discrete 0.133s (7.500 fps)
                       Interval: Discrete 0.200s (5.000 fps)
               Size: Discrete 1920x1080
                       Interval: Discrete 0.033s (30.000 fps)
                       Interval: Discrete 0.040s (25.000 fps)
                       Interval: Discrete 0.050s (20.000 fps)
                       Interval: Discrete 0.067s (15.000 fps)
                       Interval: Discrete 0.100s (10.000 fps)
                       Interval: Discrete 0.133s (7.500 fps)
                       Interval: Discrete 0.200s (5.000 fps)
               Size: Discrete 320x240
[...]
               Size: Discrete 800x600
[...]
               Size: Discrete 1280x720
[...]
               Size: Discrete 1024x576
[...]
       [1]: 'YUYV' (YUYV 4:2:2)        [1]: 'YUYV' (YUYV 4:2:2)
               Size: Discrete 640x480
                       Interval: Discrete 0.033s (30.000 fps)
[...]
               Size: Discrete 1920x1080
                       Interval: Discrete 0.200s (5.000 fps)
               Size: Discrete 320x240
[...]

We can see that both MJPEG and YUV422 coplanar are supported, MJPEG offers a better resolution/framerate performance (lower bandwidth required to transfer pictures), while YUV preserves the original image quality and is easier to process.

4. Capture raw frames[edit | edit source]

GStreamer v4l2src element is used to capture pictures giving the discrete format, resolution and framerate:

gst-launch-1.0 v4l2src device=/dev/video7 num-buffers=300 ! video/x-raw, format=YUY2, width=640, height=480, framerate=30/1 ! queue ! avimux ! filesink location=webcam_yuyv_vga_30fps.avi -e -v

YUV frames are encapsulated into an avi container to ease afterward playback, result can then easily be played using gst-play:

gst-play-1.0 webcam_yuyv_vga_30fps.avi

5. Preview of raw frames[edit | edit source]

gst-launch-1.0 v4l2src device=/dev/video7 io-mode=2 num-buffers=300 ! video/x-raw, format=YUY2, width=640, height=480, framerate=30/1 ! queue ! waylandsink fullscreen=true -v

6. Capture MJPEG video stream[edit | edit source]

MJPEG enables higher resolution/framerate than YUV, allowing to reach the camera maximum performances:

gst-launch-1.0 v4l2src device=/dev/video7 num-buffers=300 ! image/jpeg, width=1920, height=1080, framerate=30/1 ! queue ! avimux ! filesink location=webcam_mjpeg_hd_30fps.avi -e -v

Result can then be played using gst-play:

gst-play-1.0 webcam_mjpeg_hd_30fps.avi --videosink="waylandsink fullscreen=true"

7. Preview of MJPEG video stream (only STM32MP2 series)[edit | edit source]

STM32MP2 series hardware decoder can be used to display USB Webcam MJPEG video stream, refer to V4L2 video codec article for details.

8. Transcode of MJPEG video stream (only STM32MP2 series)[edit | edit source]

STM32MP2 series hardware decoders and encoders can be used to transcode USB Webcam MJPEG video stream to a better compression ratio format such as H264 or VP8, refer to V4L2 video codec article for details.


9. References[edit | edit source]

  1. Linux UVC Gadget Driver in Linux kernel documentations