Camera架构
一、APP层
获取摄像头ID列表:getCameraIdList()
打开摄像头:openCamera();
二、frameworks层:
CameraManager:
代码路径:\frameworks\base\core\java\android\hardware\camera2\CameraManager.java
private CameraDevice openCameraDeviceUserAsync(String cameraId,
CameraDevice.StateCallback callback, Executor executor, final int uid)
throws CameraAccessException {
//省略一部分代码
//...
// Use cameraservice's cameradeviceclient implementation for HAL3.2+ devices
ICameraService cameraService = CameraManagerGlobal.get().getCameraService();
if (cameraService == null) {
throw new ServiceSpecificException(
ICameraService.ERROR_DISCONNECTED,
"Camera service is currently unavailable");
}
//通过CameraService连接摄像头设备
cameraUser = cameraService.connectDevice(callbacks, cameraId,
mContext.getOpPackageName(), uid);
//省略一部分代码
//...
}
三、C++ Libraries层代码:
CameraService
代码路径:frameworks\av\services\camera\libcameraservice\CameraService.cpp
// CameraService::connectDevice流程
{
//省略一部分代码
//...
// 创建不同HAL版本对应的相机Client
sp<BasicClient> tmp = nullptr;
if(!(ret = makeClient(this, cameraCb, clientPackageName,
cameraId, api1CameraId, facing,
clientPid, clientUid, getpid(), legacyMode,
halVersion, deviceVersion, effectiveApiLevel,
/*out*/&tmp)).isOk()) {
return ret;
}
//省略一部分代码
//...
}
// 根据HAL版本创建对应的相机Clinet,这里以创建最新版本的CAMERA_DEVICE_API_VERSION_3_4版本,
// 以Camera2 API为例的版本
Status CameraService::makeClient() {
//省略一部分代码
//...
// Camera2 API route
sp<hardware::camera2::ICameraDeviceCallbacks> tmp = static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
*client = new CameraDeviceClient(cameraService, tmp, packageName, cameraId, facing, clientPid, clientUid, servicePid);
//省略一部分代码
//...
}
frameworks\av\services\camera\libcameraservice\api2\CameraDeviceClient.cpp
frameworks\av\services\camera\libcameraservice\common\Camera2ClientBase.cpp
frameworks\av\services\camera\libcameraservice\device3\Camera3Device.cpp
status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
//在初始化时,调用了 CameraProviderManager 的 openSession 方法,开启了远端的 Session
sp<ICameraDeviceSession> session;
ATRACE_BEGIN("CameraHal::openSession");
status_t res = manager->openSession(mId.string(), this,
/*out*/ &session);
ATRACE_END();
if (res != OK) {
SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
return res;
}
//生成HIDL接口层对象
mInterface = new HalInterface(session, queue);
}
frameworks\av\services\camera\libcameraservice\common\CameraProviderManager.cpp
//V3_2::ICameraDeviceCallback 调用V3.2版本的HIDL接口
status_t CameraProviderManager::openSession(const std::string &id,
const sp<hardware::camera::device::V3_2::ICameraDeviceCallback>& callback,
/*out*/
sp<hardware::camera::device::V3_2::ICameraDeviceSession> *session) {
std::lock_guard<std::mutex> lock(mInterfaceMutex);
auto deviceInfo = findDeviceInfoLocked(id,
/*minVersion*/ {3,0}, /*maxVersion*/ {4,0});
if (deviceInfo == nullptr) return NAME_NOT_FOUND;
auto *deviceInfo3 = static_cast<ProviderInfo::DeviceInfo3*>(deviceInfo);
Status status;
hardware::Return<void> ret;
//通过HIDL层接口调用open()
ret = deviceInfo3->mInterface->open(callback, [&status, &session]
(Status s, const sp<device::V3_2::ICameraDeviceSession>& cameraSession) {
status = s;
if (status == Status::OK) {
*session = cameraSession;
}
});
if (!ret.isOk()) {
ALOGE("%s: Transaction error opening a session for camera device %s: %s",
__FUNCTION__, id.c_str(), ret.description().c_str());
return DEAD_OBJECT;
}
return mapToStatusT(status);
}
四、HIDL层和HAL层
文件路径:
hardware\interfaces\camera\device\3.2\default\CameraDevice.cpp
Return<void> CameraDevice::open(const sp<ICameraDeviceCallback>& callback, open_cb _hidl_cb) {
/** Open HAL device */
status_t res;
camera3_device_t *device;
ATRACE_BEGIN("camera3->open");
//调用CameraModule中的open
res = mModule->open(mCameraId.c_str(), reinterpret_cast<hw_device_t**>(&device));
ATRACE_END();
}
hardware\interfaces\camera\common\1.0\default\CameraModule.cpp
int CameraModule::open(const char* id, struct hw_device_t** device) {
int res;
ATRACE_BEGIN("camera_module->open");
//mModule是一个camera_module_t对象,此openfa方法将调用Driver层代码进行打开摄像头操作
res = filterOpenErrorCode(mModule->common.methods->open(&mModule->common, id, device));
ATRACE_END();
return res;
}
//camera_module_t涉及到CAMERA_HARDWARE_MODULE_ID的创建和绑定
camera_module_t文件路径:hardware\libhardware\include\hardware\camera_common.h
五、Driver层
Driver层是硬件厂商定制的,所以,不同的芯片厂商,Driver层代码不一样。
驱动层执行一些跟摄像头设备相关和图形显示,视频数据的一些操作,涉及到一些设备文件操作。
主要方法和解析如下:
// open camera dev nodes, etc
int32_t ret = mVideoStream->openDev(mDevPath);
主要执行:
// name为"/dev/video0"摄像头设备,
mDev = open(name, O_RDWR);
// flush camera dev nodes.
return mVideoStream->flushDev();
主要执行:
// 这里执行了两个重要的操作VIDIOC_DQBUF和VIDIOC_QBUF,取出摄像头视频图像Buffer
ret = ioctl(mDev, VIDIOC_DQBUF, &cfilledbuffer);
if (ret < 0) {
ALOGE("%s: VIDIOC_DQBUF Failed: %s (%d)", __func__, strerror(errno), errno);
return BAD_VALUE;
}
ret = ioctl(mDev, VIDIOC_QBUF, &cfilledbuffer);
if (ret < 0) {
ALOGE("%s: VIDIOC_QBUF Failed: %s (%d)", __func__, strerror(errno), errno);
return BAD_VALUE;
}
// close camera dev nodes, etc
mVideoStream->closeDev();
主要执行:
// 关闭摄像头设备
close(mDev);