本专栏内容针对 “知识星球”成员免费,欢迎关注公众号:小驰行动派,加入知识星球。
#MTK Camera开发学习系列 #小驰私房菜
Google 官方介绍文档:
https://source.android.google.cn/docs/core/camera/external-usb-cameras?hl=zh-cn
一、MTK平台支持通过标准接口打开USB Camera
1 ) 修改目录 /device/
---
mediatek/mt***/device.mk | 4 ++++
.../hal/camera/external_camera_config.xml | 20 +++++++++++++++++++
mediatek/mt***/init.mt***.rc | 5 +++++
mediatek/mt***/manifest.xml | 1 +
mediatek/mt***/ueventd.mt**.rc | 2 ++
5 files changed, 32 insertions(+)
create mode 100644 mediatek/mt***/hal/camera/external_camera_config.xml
diff --git a/mediatek/mt***/device.mk b/mediatek/mt***/device.mk
index 00e67370..f1db63b6 100644
--- a/mediatek/mt**/device.mk
+++ b/mediatek/mt**/device.mk
@@ -226,6 +226,10 @@ PRODUCT_PACKAGES +=
vendor.mediatek.hardware.camera.isphal@1.0-impl
PRODUCT_PACKAGES += camerahalserver
PRODUCT_PACKAGES += android.hardware.camera.provider@2.6-service-mediatek
PRODUCT_PACKAGES += android.hardware.camera.provider@2.6-impl-mediatek
+PRODUCT_PACKAGES += android.hardware.camera.provider@2.4-impl
+PRODUCT_PACKAGES += android.hardware.camera.provider@2.4-external-service
+PRODUCT_COPY_FILES += \
+
device/mediatek/mt***/hal/camera/external_camera_config.xml:$(TARGET_COPY_OUT_VE
NDOR)/etc/external_camera_config.xml
PRODUCT_PACKAGES += libmtkcam_device1
PRODUCT_PACKAGES += libmtkcam_device3
PRODUCT_PACKAGES += camera.mt**
diff --git a/mediatek/mt6877/hal/camera/external_camera_config.xml
b/mediatek/mt6877/hal/camera/external_camera_config.xml
new file mode 100644
index 00000000..2884e131--- /dev/null
+++ b/mediatek/mt***/hal/camera/external_camera_config.xml
@@ -0,0 +1,20 @@
+<ExternalCamera>
+ <Provider>
+ <ignore> <!-- Internal video devices to be ignored by external camera
HAL -->
+ <id>0</id>
+ <id>1</id>
+ <id>2</id>
+ <id>3</id>
+ </ignore>
+ </Provider>
+ <Device>
+ <MaxJpegBufferSize bytes="3145728"/>
+ <NumVideoBuffers count="4"/>
+ <NumStillBuffers count="2"/>
+ <FpsList>
+ <Limit width="640" height="480" fpsBound="30.0"/>
+ <Limit width="1280" height="720" fpsBound="30.0"/>
+ <Limit width="1920" height="1080" fpsBound="30.0"/>
+ </FpsList>
+ </Device>
+</ExternalCamera>diff --git a/mediatek/mt***/init.mt***.rc b/mediatek/mt***/init.mt***.rc
index 415bc50e..9b401e5f 100644
--- a/mediatek/mt***/init.mt***.rc
+++ b/mediatek/mt***/init.mt***.rc
@@ -415,6 +415,11 @@ on post-fs-data
chown media system /dev/video0
chmod 0660 /dev/video1
chown media system /dev/video1
+ chmod 0660 /dev/video5
+ chown media system /dev/video5
+ chmod 0660 /dev/video4
+ chown media system /dev/video4
+
mkdir /data/vendor/vcodec 0777 media system
mkdir /data/vendor/vcodec/log 0770 media system
mkdir /data/vendor/vcodec/in 0770 media system
diff --git a/mediatek/mt***/manifest.xml b/mediatek/mt***/manifest.xml
index 455924cb..badea3ec 100644
--- a/mediatek/mt***/manifest.xml
+++ b/mediatek/mt***/manifest.xml
@@ -27,6 +27,7 @@
<name>ICameraProvider</name>
<instance>internal/0</instance>
</interface>
+ <fqname>@2.4::ICameraProvider/external/0</fqname>
</hal>
<hal format="hidl">
<name>android.hardware.gatekeeper</name>diff --git a/mediatek/mt***/ueventd.mt***.rc b/mediatek/mt***/ueventd.mt***.rc
index 9b71ee5d..efcf2599 100644
--- a/mediatek/mt***/ueventd.mt***.rc
+++ b/mediatek/mt***/ueventd.mt***.rc
@@ -89,6 +89,8 @@
#v4l2 device
/dev/video* 0660 camera system
+/dev/video4 0666 media system
+/dev/video5 0666 media system
/dev/media* 0660 camera system
/dev/v4l-subdev* 0660 camera system
--
2 ) 修改目录 /system/
---
sepolicy/prebuilts/api/31.0/public/cameraserver.te | 5 +++++
sepolicy/public/cameraserver.te | 5 +++++
2 files changed, 10 insertions(+)
diff --git a/sepolicy/prebuilts/api/31.0/public/cameraserver.te
b/sepolicy/prebuilts/api/31.0/public/cameraserver.te
index 7a29240c..f08dbc7b 100644
--- a/sepolicy/prebuilts/api/31.0/public/cameraserver.te
+++ b/sepolicy/prebuilts/api/31.0/public/cameraserver.te
@@ -74,3 +74,8 @@ userdebug_or_eng(`
allow cameraserver su:fifo_file { read write };
allow cameraserver su:unix_stream_socket { read write };
')
+
+
+allow cameraserver device:dir r_dir_perms;
+allow cameraserver video_device:dir r_dir_perms;
+allow cameraserver video_device:chr_file rw_file_perms;
diff --git a/sepolicy/public/cameraserver.te b/sepolicy/public/cameraserver.te
index 7a29240c..f08dbc7b 100644
--- a/sepolicy/public/cameraserver.te
+++ b/sepolicy/public/cameraserver.te@@ -74,3 +74,8 @@ userdebug_or_eng(`
allow cameraserver su:fifo_file { read write };
allow cameraserver su:unix_stream_socket { read write };
')
+
+
+allow cameraserver device:dir r_dir_perms;
+allow cameraserver video_device:dir r_dir_perms;
+allow cameraserver video_device:chr_file rw_file_perms;
--
3 ) kernel config 添加如下内容
kernel-***/arch/arm64/configs/tb***_defconfig
具体添加位置试实际项目情况来定
CONFIG_USB_VIDEO_CLASS=y
CONFIG_MEDIA_USB_SUPPORT=y
4 ) 上面内容修改完后,发现能检测到设备,不过打开黑图
从下面的日志里面可以看到,由于还有其它的 selinux 权限问题,导致 camera error 。本地可以先通过
setenforce 0 来确认,解决 selinux 问题是否就正常。
后面再根据实际的 avc 异常信息,添加 selinux 权限
------- 这里遇到的问题,是在 hal_camera_default.te 中添加如下内容,问题解决。
allow hal_camera_default hal_graphics_mapper_hwservice:hwservice_manager find;
11-07 15:01:26.314 D/hwcomposer( 632): (0:3) Layer+
(mva=0x0/sec=0/prot=0/fmt=1/alpha=1:0xff/blend=0002/dim=0/range=20000000/x=0 y=0
w=480 h=52 s=480 -> x=0 y=0 w=480 h=52/ext_layer=2 ds=0 fbdc=1 is_mml=0) =
11-07 15:01:26.322 E/SELinux ( 482): avc: denied { find } for
interface=android.hardware.graphics.mapper::IMapper sid=u:r:hal_camera_default:s0
pid=5451 scontext=u:r:hal_camera_default:s0
tcontext=u:object_r:hal_graphics_mapper_hwservice:s0 tclass=hwservice_manager
permissive=0
11-07 15:01:26.323 E/SELinux ( 482): avc: denied { find } for
interface=android.hardware.graphics.mapper::IMapper sid=u:r:hal_camera_default:s0
pid=5451 scontext=u:r:hal_camera_default:s0
tcontext=u:object_r:hal_graphics_mapper_hwservice:s0 tclass=hwservice_manager
permissive=0
11-07 15:01:26.323 E/SELinux ( 482): avc: denied { find } for
interface=android.hardware.graphics.mapper::IMapper sid=u:r:hal_camera_default:s0
pid=5451 scontext=u:r:hal_camera_default:s0
tcontext=u:object_r:hal_graphics_mapper_hwservice:s0 tclass=hwservice_manager
permissive=0
11-07 15:01:26.323 E/HandleImporter( 5451): initializeLocked: cannnot acccess
graphics mapper HAL!
二、Qcom 平台
------ platform/vendor/qcom/***/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="30.0"/>
<!-- image size larger than the last entry will not be supported-->
</FpsList>
</Device>
</ExternalCamera>
----- platform/vendor/qcom/***/***.mk
PRODUCT_PACKAGES += android.hardware.camera.provider@2.4-external-service
PRODUCT_COPY_FILES += \
device/qcom/holi/external_camera_config.xml:$(TARGET_COPY_OUT_VENDOR)/etc/extern
al_camera_config.xml
----- platform/vendor/qcom/***/manifest.xml
<hal format="hidl">
<name>android.hardware.camera.provider</name>
<transport>hwbinder</transport>
<version>2.4</version>
<interface>
<name>ICameraProvider</name>
<instance>legacy/0</instance>
<instance>external/0</instance> <!-- new add for usb camera-->
</interface>
</hal>
---- kernel/msm-***/ arch/arm64/configs/vendor/***.config
CONFIG_USB_VIDEO_CLASS=y
另外需要把自己的 app 包名,写到 vendor.camera.aux.packagelist property 属性里面,通过
getCameraIdList 才能获取到多于 2 颗摄像头的 id。
-----------------------------------------------------------------------------------------
文章内容所有权归@公众号:小驰行动派,禁止宣传转载,谢谢~