[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# External USB cameras\n\n\\\u003c!-- Copyright 2018 The Android Open Source Project\n\n\u003cbr /\u003e\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at \n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n--\\\u003e\n\nThe Android platform supports the use of plug-and-play USB cameras (that is,\nwebcams) using the standard\n[Android Camera2 API](https://developer.android.com/reference/android/hardware/camera2/package-summary.html)\nand the camera\n[HAL](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/camera/provider/aidl/android/hardware/camera/provider/ICameraProvider.aidl)\ninterface. Webcams generally support\n[USB video class (UVC)](https://en.wikipedia.org/wiki/USB_video_device_class)\ndrivers and on Linux, the standard\n[Video4Linux (V4L)](https://en.wikipedia.org/wiki/Video4Linux)\ndriver is used to control UVC cameras.\n\nWith support for webcams, devices can be used in lightweight use cases such as\nvideo chatting and photo kiosks. This feature doesn't replace typical internal\ncamera HALs on Android phones and isn't designed to\nsupport performance-intensive, complex tasks involving high-resolution and\nhigh-speed streaming, AR, and manual ISP/sensor/lens control.\n\nThe USB camera HAL process is part of the external camera provider that\nlistens to USB device availability and enumerates external camera devices\naccordingly. The process has permissions and an SE policy similar to the\nbuilt-in camera HAL process. Third-party webcam apps that communicate\ndirectly with USB devices require the same camera permissions to access UVC\ndevices as with any regular camera app.\n\nExamples and sources\n--------------------\n\nFor more information on how to implement USB cameras, see the external camera\nprovider reference implementation at\n[`ExternalCameraProvider`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/camera/provider/default/ExternalCameraProvider.cpp).\nThe external camera device and session implementations are included in\n[`ExternalCameraDevice`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/camera/device/default/ExternalCameraDevice.cpp)\nand\n[`ExternalCameraDeviceSession`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/camera/device/default/ExternalCameraDeviceSession.cpp).\nStarting in API level 28, the Java client API includes the\n[`EXTERNAL`](https://developer.android.com/reference/android/hardware/camera2/CameraMetadata?authuser=3#INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL)\nhardware level.\n\nImplementation\n--------------\n\nThe implementation must support the\n[`android.hardware.usb.host`](https://developer.android.com/guide/topics/connectivity/usb/host)\nsystem feature.\n\nKernel support for UVC devices must also be enabled. You can enable this by\nadding the following to the respective kernel `defconfig` files. \n\n +CONFIG_USB_VIDEO_CLASS=y\n +CONFIG_MEDIA_USB_SUPPORT=y\n\n| **Note:** Make sure you also have this [patch](https://patchwork.kernel.org/patch/6874491/) for UVC video.\n\nTo enable the external camera provider in the respective device build, which\nadds the necessary SELinux permissions, external camera configuration, and\nexternal camera provider dependency, complete the following steps:\n\n- Add the external camera config file and external camera library to\n `device.mk`.\n\n +PRODUCT_PACKAGES += android.hardware.camera.provider-V1-external-service\n\n +PRODUCT_COPY_FILES += \\\n +device/manufacturerX/productY/external_camera_config.xml:$(TARGET_COPY_OUT_VENDOR)/etc/external_camera_config.xml\n\n- Add the external camera provider name to the device Treble HAL manifest.\n\n \u003chal format=\"aidl\"\u003e\n \u003cname\u003eandroid.hardware.camera.provider\u003c/name\u003e\n \u003cversion\u003e1\u003c/version\u003e\n \u003cinterface\u003e\n \u003cname\u003eICameraProvider\u003c/name\u003e\n \u003cinstance\u003einternal/0\u003c/instance\u003e\n + \u003cinstance\u003eexternal/0\u003c/instance\u003e\n \u003c/interface\u003e\n \u003c/hal\u003e\n\n- (Optional) If the device runs in Treble passthrough mode, update `sepolicy`\n so `cameraserver` can access the UVC camera.\n\n +# for external camera\n +allow cameraserver device:dir r_dir_perms;\n +allow cameraserver video_device:dir r_dir_perms;\n +allow cameraserver video_device:chr_file rw_file_perms;\n\nHere's an example of `external_camera_config.xml` (copyright lines omitted). \n\n```gdscript\n\u003cExternalCamera\u003e\n \u003cProvider\u003e\n \u003cignore\u003e \u003c!-- Internal video devices to be ignored by external camera HAL --\u003e\n \u003cid\u003e0\u003c/id\u003e \u003c!-- No leading/trailing spaces --\u003e\n \u003cid\u003e1\u003c/id\u003e\n \u003c/ignore\u003e\n \u003c/Provider\u003e\n \u003c!-- See ExternalCameraUtils.cpp for default values of Device configurations below --\u003e\n \u003cDevice\u003e\n \u003c!-- Max JPEG buffer size in bytes--\u003e\n \u003cMaxJpegBufferSize bytes=\"3145728\"/\u003e \u003c!-- 3MB (~= 1080p YUV420) --\u003e\n \u003c!-- Size of v4l2 buffer queue when streaming \u003e= 30fps --\u003e\n \u003c!-- Larger value: more request can be cached pipeline (less janky) --\u003e\n \u003c!-- Smaller value: use less memory --\u003e\n \u003cNumVideoBuffers count=\"4\"/\u003e\n \u003c!-- Size of v4l2 buffer queue when streaming \u003c 30fps --\u003e\n \u003cNumStillBuffers count=\"2\"/\u003e\n\n \u003c!-- List of maximum fps for various output sizes --\u003e\n \u003c!-- Any image size smaller than the size listed in Limit row will report\n fps (as minimum frame duration) up to the fpsBound value. --\u003e\n \u003cFpsList\u003e\n \u003c!-- width/height must be increasing, fpsBound must be decreasing--\u003e\n \u003cLimit width=\"640\" height=\"480\" fpsBound=\"30.0\"/\u003e\n \u003cLimit width=\"1280\" height=\"720\" fpsBound=\"15.0\"/\u003e\n \u003cLimit width=\"1920\" height=\"1080\" fpsBound=\"10.0\"/\u003e\n \u003c!-- image size larger than the last entry will not be supported--\u003e\n \u003c/FpsList\u003e\n \u003c/Device\u003e\n\u003c/ExternalCamera\u003e\n```\n\nCustomization\n-------------\n\nYou can enhance the Android camera either through general customization options\nor device-specific optimizations.\n\n### General customizations\n\nYou can customize the external camera provider by modifying the\n`external_camera_config.xml` file. Specifically, clients can customize the\nfollowing parameters:\n\n- Excluding video nodes of internal cameras\n- Supported image size and frame rate upper bound\n- Number of inflight buffers (jank vs memory tradeoff)\n\nIn addition to these parameters, you can add your own parameters or develop your\nown configurations.\n\n### Device-specific optimizations\n\nYou can also improve performance by adding device-specific optimizations.\n\n#### Buffer copy/scaling and JPEG decode/encode\n\nGeneric implementations use CPU (libyuv/libjpeg) but you can replace this with\ndevice-specific optimizations.\n\n#### HAL output format\n\nGeneric implementations use the following output formats:\n\n- `YUV_420_888` for video `IMPLEMENTATION_DEFINED` buffers\n- `YUV12` for all other `IMPLEMENTATION_DEFINED` buffers\n\nTo improve performance, you can replace output formats with device-specific\nefficient formats. You can also support additional formats in a customized\nimplementation\n\nValidation\n----------\n\nDevices with external camera support must pass\n[camera CTS](/docs/compatibility/cts/camera-hal#cts_tests). The external USB\nwebcam must remain plugged in the specific device during the entire test run,\notherwise some test cases will fail.\n| **Note:** `media_profiles` entries aren't available for external USB webcams, so [camcorder profiles](https://developer.android.com/reference/android/media/CamcorderProfile) are absent."]]