UVC app flow chart

Introduction

uvc_app implements the complete functions of uvc device, including configuration, preview, switching, event and command response, etc. It collects camera data and transmits it through MJPG encoding or H264 encoding through ISOC mode.

Instructions

  • Enable uvc app: make menuconfig, select enable uvc app or add BR2_PACKAGE UVC APP=y in the buildroot corresponding product defconfig
  • Confirm uvc_config.sh: Confirm USB device configuration, currently supports uvc and rndis composite.
  • Execute uvc config.sh. If you need to use a composite device such as rndis, execute uvc config.sh rndis
  • Executing uvc_app will transmit camera data through uvc by default.

Source code description

  • Compilation related: /external/uvc app/CMakeLists.txt, /buildroot/package/rockchip/uvc app/Config.in uvc_app.mk

  • Entry: main.c

  • usb script configuration related: uvc_config.sh

  • Process: camera initialization, PU processing, de-initialization and other processing

    • camera_control.cpp: camera thread processing implementation
    • camera pu control.cpp: camera PU processing implementation
  • Hot plug events: uevent.c, uevent.h

  • uvc: uvc processing code

    • Control the opening and closing of uvc, camera, and encoding threads: uvc control.c, uvc control.h
    • UVC encoding transmission processing: uvc encode.cpp, uvc encode.h
    • uvc main process: uvc-gadget.c, uvc-gadget.h
    • UVC multi-node operation, buffer management: uvc video.cpp, uvc video.h
    • MJPG/H264 encoding: mpi enc.c, mpi enc.h
    • YUV format conversion: yuv.c, yuv.h
  • drm memory operations: drm.c, drm.h

Code demo directory: external/uvc_app
#uvc_app code process:
 
Main.c: main() //Initialize the incoming length, width, operating mode flag and other parameters, and apply for buffer
    drm_open/drm_alloc/drm_handle_to_fd
->uvc_control_run (flags) //Run the uvc control interface, the default is LOOP_ONCE, wait in a loop for the uvc driver switching setting to be completed
 
    -> uvc_control.c: uevent_monitor_run //The uvc event listening thread starts and loops to monitor whether video4linux (v4l2) is connected to the event
        ->video_uevent //Analyze whether it is an add video access event.
            ->uvc_control_signal recognizes add video, notifies the uvc control thread, and sets find_video to true;
    ->uvc_control_thread //Create and start the uvc control thread, and loop to determine whether the uvc is connected
        ->check_uvc_video_id // Check whether uvc is connected, read cat /sys/class/video4linux/video%d/name in a loop, and identify whether there is a uvc node with USB or gadget in its name
        ->add_uvc_video //Start uvc connection
 
             -> uvc_video.cpp: uvc_video_id_add // Enter the initialization structure uvc_video for the first time and start the uvc_gadget thread
                ->uvc_gadget_pthread_create //Start the uvc_gadget thread
 
                    -> uvc-gadget.c: uvc_gadget_main //Run main, initialize uvc_device structure, configure uvc parameters: name length and width transmission format, speed, io mode configuration, etc., open uvc device, initialize event
                       ->uvc_open //ioctl controls the driver to start the uvc device 
                        ->uvc_events_init //Initialize uvc device-related event registration according to the incoming fcc configuration, and then process events and data in a loop
                        while loop
                        ->uvc_events_process //v4l2 event processing function
                            ->uvc_events_process_setup //Process the UVC_EVENT_SETUP event requested by the host
                            ->v4l2_stop_capturing
                            ->uvc_events_process_data -> uvc_events_process_control_data// Process UVC_EVENT_DATA events requested by the host -> like exposure, white balance, brightness, contrast, etc.
                            -> uvc_handle_streamon_event// Process the UVC_EVENT_STREAMON event requested by the host , and start the video streaming transmission
 
                                -> uvc_control.c: uvc_control_init //Pass in the obtained width and height information, etc.
                                    ->uvc_encode_init  
                                        -> mpi_enc.c: mpi_enc_cmd_config_mjpg //Configured as MPP_VIDEO_CodingMJPEG encoding type, format MPP_FMT_YUV420SP (  NV12, NV21 )
                                        ->mpi_enc.c:mpi_enc_test_init
                                            ->test_ctx_init
                                            -> mpp_create //mpp interface
                                            -> mpp_init /mpp interface
                                            -> test_mpp_setup// Encoding configuration is related to calculation
                                            
 
                        -> uvc-gadget.c: uvc_video_process//When the uvc video stream is turned on, it mainly responds to the qbuffer and dqbuffer of the v4l2 buffer queue.
 
->while(1) { ...
      uvc_read_camera_buffer //Loop to write the encoded camera raw data information to the uvc buffer queue
    }
    -> uvc_encode.cpp: uvc_encode_process //Encoding processing function according to format
        (-> mpi_enc.c: mpi_enc_test_run // Call this function encoding first in mjpeg format)
        ->uvc_video.cpp:uvc_buffer_write 
            ->_uvc_buffer_write// Data format conversion part
                ->uvc_buffer_push_back(&v->uvc->read, buffer);//Push the converted data to the read queue and subsequently transmit it to the host
 
 
This is the simple process analysis of the code, which is summarized as follows: UVC module side running steps:
    1. The usb uvc and AP need to be connected and configured ready. The code can read cat /sys/class/video4linux/video%d/name through a loop to identify whether there is a uvc node with USB or gadget in its name;
    2. Run uvc_control_run and start the uvc_control thread to initialize uvc-related services and event monitoring and processing interfaces;
    3.uvc_read_camera_buffer, cyclically reads the camera data according to the set format encoding and then passes it into the uvc buffer queue for host processing.
 
The old video demo process compiled by colleagues from the three departments is as follows, which is consistent with the main process of uvc_app:
 
 
                                                                            

Guess you like

Origin blog.csdn.net/zhoudidong/article/details/105813763