Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 您可以在以下網址取得授權副本:
http://www.apache.org/licenses/LICENSE-2.0
除非適用法律要求或雙方另有書面協議,否則依本授權條款發布的軟體均依「現況」提供,不附帶任何明示或暗示的擔保或條件。See the License for the specific language governing permissions and limitations under the License. -->
Android 平台支援使用隨插即用的 USB 攝影機 (即網路攝影機),方法是使用標準的 Android Camera2 API 和相機 HAL 介面。網路攝影機通常支援 USB 視訊類別 (UVC) 驅動程式,在 Linux 上,則使用標準 Video4Linux (V4L) 驅動程式控制 UVC 攝影機。
裝置支援網路攝影機,因此可用於視訊通訊和照相亭等輕量用途。這項功能不會取代 Android 手機上的一般內部相機 HAL,也不支援需要大量效能的複雜工作,例如高解析度和高速串流、AR,以及手動 ISP/感應器/鏡頭控制。
USB 攝影機 HAL 程序是外部攝影機供應商的一部分,負責監聽 USB 裝置可用性,並據此列舉外部攝影機裝置。這個程序具有與內建攝影機 HAL 程序類似的權限和 SE 政策。與 USB 裝置直接通訊的第三方網路攝影機應用程式,必須取得與一般相機應用程式相同的相機權限,才能存取 UVC 裝置。
範例和來源
如要進一步瞭解如何導入 USB 攝影機,請參閱 ExternalCameraProvider
的外部攝影機供應商參考實作。外部攝影機裝置和工作階段實作項目包含在 ExternalCameraDevice
和 ExternalCameraDeviceSession
中。從 API 級別 28 開始,Java 用戶端 API 會包含EXTERNAL
硬體層級。
實作
實作項目必須支援 android.hardware.usb.host
系統功能。
此外,也必須啟用核心對 UVC 裝置的支援。如要啟用這項功能,請在對應的 Kernel defconfig
檔案中新增下列內容。
+CONFIG_USB_VIDEO_CLASS=y
+CONFIG_MEDIA_USB_SUPPORT=y
如要在相應的裝置建構版本中啟用外部攝影機供應商,並新增必要的 SELinux 權限、外部攝影機設定和外部攝影機供應商依附元件,請完成下列步驟:
將外部攝影機設定檔和外部攝影機程式庫新增至
device.mk
。+PRODUCT_PACKAGES += android.hardware.camera.provider-V1-external-service +PRODUCT_COPY_FILES += \ +device/manufacturerX/productY/external_camera_config.xml:$(TARGET_COPY_OUT_VENDOR)/etc/external_camera_config.xml
將外部攝影機供應商名稱新增至裝置 Treble HAL 資訊清單。
<hal format="aidl"> <name>android.hardware.camera.provider</name> <version>1</version> <interface> <name>ICameraProvider</name> <instance>internal/0</instance> + <instance>external/0</instance> </interface> </hal>
(選用) 如果裝置以 Treble 直通模式執行,請更新
sepolicy
,讓cameraserver
存取 UVC 攝影機。+# 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;
以下是 external_camera_config.xml
的範例 (省略著作權行)。
<ExternalCamera> <Provider> <ignore> <!-- Internal video devices to be ignored by external camera HAL --> <id>0</id> <!-- No leading/trailing spaces --> <id>1</id> </ignore> </Provider> <!-- See ExternalCameraUtils.cpp for default values of Device configurations below --> <Device> <!-- Max JPEG buffer size in bytes--> <MaxJpegBufferSize bytes="3145728"/> <!-- 3MB (~= 1080p YUV420) --> <!-- Size of v4l2 buffer queue when streaming >= 30fps --> <!-- Larger value: more request can be cached pipeline (less janky) --> <!-- Smaller value: use less memory --> <NumVideoBuffers count="4"/> <!-- Size of v4l2 buffer queue when streaming < 30fps --> <NumStillBuffers count="2"/> <!-- 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> </ExternalCamera>
自訂
您可以透過一般自訂選項或裝置專屬最佳化功能,提升 Android 相機效能。
一般自訂項目
您可以修改 external_camera_config.xml
檔案,自訂外部攝影機供應商。具體來說,客戶可以自訂下列參數:
- 排除內部攝影機的影片節點
- 支援的圖片大小和畫面更新率上限
- 進行中緩衝區數量 (延遲與記憶體之間的取捨)
除了這些參數,您也可以加入自己的參數或開發自己的設定。
裝置專屬最佳化
您也可以加入裝置專屬的最佳化設定,進一步提升效能。
緩衝區複製/縮放和 JPEG 解碼/編碼
一般實作項目會使用 CPU (libyuv/libjpeg),但您可以改用裝置專屬最佳化項目。
HAL 輸出格式
一般實作會使用下列輸出格式:
YUV_420_888
影片IMPLEMENTATION_DEFINED
緩衝區YUV12
適用於所有其他IMPLEMENTATION_DEFINED
緩衝區
如要提升效能,可以將輸出格式換成裝置專用的高效格式。您也可以在自訂實作中支援其他格式
驗證
支援外接式攝影機的裝置必須通過攝影機 CTS。在整個測試執行期間,外部 USB 網路攝影機必須持續插入特定裝置,否則部分測試案例會失敗。