Android 10 Camera -- External USB Cameras

说明:转载自 Android Camera,不定期添加自己的学习思考。


External USB Cameras

The Android platform supports the use of plug-and-play USB cameras (that is, webcams) using the standard Android Camera2 API and the camera HIDL interface. Webcams generally support USB video class (UVC) drivers and on Linux, the standard Video4Linux (V4L) driver is used to control UVC cameras.

With support for webcams, devices can be used in lightweight use cases such as video chatting and photo kiosks. This feature doesn’t replace typical internal camera HALs on Android phones and isn’t designed to support performance-intensive, complex tasks involving high-resolution and high-speed streaming, AR, and manual ISP/sensor/lens control.

The USB camera HAL process is part of the external camera provider that listens to USB device availability and enumerates external camera devices accordingly. The process has permissions and an SE policy similar to the built-in camera HAL process. Third-party webcam apps that communicate directly with USB devices require the same camera permissions to access UVC devices as with any regular camera app.

Examples and sources
For more information on how to implement USB cameras, see an external camera provider reference implementation at ExternalCameraProvider. The external camera device and session implementations are included in ExternalCameraDevice and ExternalCameraDeviceSession. Starting in API level 28, the Java client API includes the EXTERNAL hardware level.

Implementation
The implementation must support the android.hardware.usb.host system feature.

Kernel support for UVC devices must also be enabled. You can enable this by adding the following to the respective kernel deconfig files.

+CONFIG_USB_VIDEO_CLASS=y
+CONFIG_MEDIA_USB_SUPPORT=y

Note: Make sure you also have this patch for UVC video.
To enable the external camera provider in the respective device build, which adds the necessary SELinux permissions, external camera configuration, and external camera provider dependency, complete the following steps:

Add the external camera config file and external camera library to device.mk.

+PRODUCT_PACKAGES += [email protected]
+PRODUCT_PACKAGES += [email protected]

+PRODUCT_COPY_FILES +=
+device/manufacturerX/productY/external_camera_config.xml:$(TARGET_COPY_OUT_VENDOR)/etc/external_camera_config.xml

Add the external camera provider name to the device Treble HAL manifest.

android.hardware.camera.provider passthrough 2.4 ICameraProvider legacy/0 + external/0

(Optional) If the device runs in Treble passthrough mode, update sepolicy so cameraserver can access the UVC camera.

+# for external camera
+allow cameraserver device:dir r_dir_perms;
+allow cameraserver video_device:dir r_dir_perms;
+allow cameraserver video_device:chr_file rw_file_perms;

Here’s an example of external_camera_config.xml (copyright lines omitted).

0 1
    <!-- List of maximum fps for various output sizes -->
    <!-- Any image size smaller than the size listed in Limit row will report
        fps (as minimum frame duration) up to the fpsBound value. -->
    <FpsList>
        <!-- width/height must be increasing, fpsBound must be decreasing-->
        <Limit width="640" height="480" fpsBound="30.0"/>
        <Limit width="1280" height="720" fpsBound="15.0"/>
        <Limit width="1920" height="1080" fpsBound="10.0"/>
        <!-- image size larger than the last entry will not be supported-->
    </FpsList>
</Device>

Customization
You can enhance the Android camera either through general customization options or device-specific optimizations.

General customizations
You can customize the external camera provider by modifying the external_camera_config.xml file. Specifically, clients can customize the following parameters:

Excluding video nodes of internal cameras
Supported image size and frame rate upper bound
Number of inflight buffers (jank vs memory tradeoff)
In addition to these parameters, you can add your own parameters or develop your own configurations.

Device-specific optimizations
You can also improve performance by adding device-specific optimizations.

Buffer copy/scaling and JPEG decode/encode
Generic implementations use CPU (libyuv/libjpeg) but you can replace this with device-specific optimizations.

HAL output format
Generic implementations use the following output formats:

YUV_420_888 for video IMPLEMENTATION_DEFINED buffers
YUV12 for all other IMPLEMENTATION_DEFINED buffers
To improve performance, you can replace output formats with device-specific efficient formats. You can also support additional formats in a customized implementation

Validation
Devices with external camera support must pass camera CTS. The external USB webcam must remain plugged in the specific device during the entire test run, otherwise some test cases will fail.

Note: media_profiles entries aren’t available for external USB webcams, so camcorder profiles are absent.

发布了81 篇原创文章 · 获赞 31 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/xiaosaerjt/article/details/105587255