Android Camera 打开预览流程分析--CameraService中的打开流程
Posted Give.Me.Five
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Camera 打开预览流程分析--CameraService中的打开流程相关的知识,希望对你有一定的参考价值。
上篇分析了打开Camera 在FW中SDK部分的流程,这篇分析在CameraService中的流程。
CameraService 分几个部分:
- CameraServer定义和初始化 —frameworks/av/camera/cameraserver
- libcamera_client 调用库 —frameworks/av/camera/
- libcameraservice 服务调用库 — frameworks/av/service/camera/libcameraservice
CameraService的启动
CameraService 是通过cameraserver.rc 启动的
service cameraserver /system/bin/cameraserver
class main
user cameraserver
group audio camera input drmrpc
ioprio rt 4
writepid /dev/cpuset/camera-daemon/tasks /dev/stune/foreground/tasks
rlimit rtprio 10 10
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
ALOGI("ServiceManager: %p", sm.get());
//实例化了一个CameraService
CameraService::instantiate();
CameraService 其实就是实例化了一个CameraService 的对象,看下CameraService 实例化过程中都做了哪些事情:
void CameraService::onFirstRef()
ALOGI("CameraService process starting");
BnCameraService::onFirstRef();
//枚举所有的Camera信息
res = enumerateProviders();
if (res == OK)
mInitialized = true;
mUidPolicy = new UidPolicy(this);
mUidPolicy->registerSelf();
mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
mSensorPrivacyPolicy->registerSelf();
sp<HidlCameraService> hcs = HidlCameraService::getInstance(this);
if (hcs->registerAsService() != android::OK)
ALOGE("%s: Failed to register default android.frameworks.cameraservice.service@1.0",
__FUNCTION__);
// This needs to be last call in this function, so that it's as close to
// ServiceManager::addService() as possible.
CameraService::pingCameraServiceProxy();
ALOGI("CameraService pinged cameraservice proxy");
CameraService 初始化时,枚举出所有的camera设备,camera 设备是什么时候获取到的呢?
status_t CameraService::enumerateProviders()
status_t res;
std::vector<std::string> deviceIds;
Mutex::Autolock l(mServiceLock);
//初始化CameraProviderManager
if (nullptr == mCameraProviderManager.get())
mCameraProviderManager = new CameraProviderManager();
res = mCameraProviderManager->initialize(this);
if (res != OK)
ALOGE("%s: Unable to initialize camera provider manager: %s (%d)",
__FUNCTION__, strerror(-res), res);
return res;
mCameraProviderManager->setUpVendorTags();
if (nullptr == mFlashlight.get())
mFlashlight = new CameraFlashlight(mCameraProviderManager, this);
res = mFlashlight->findFlashUnits();
if (res != OK)
ALOGE("Failed to enumerate flash units: %s (%d)", strerror(-res), res);
//获取CameraDeviceIds
deviceIds = mCameraProviderManager->getCameraDeviceIds();
for (auto& cameraId : deviceIds)
String8 id8 = String8(cameraId.c_str());
if (getCameraState(id8) == nullptr)
//回调Camera的状态,表示camera 已经存在了
onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT);
return OK;
这里面主要做了两件事,一个是CameraProviderManager的initialize。另外一个是CameraProviderManager的获取cameradevices的所有id.
那就继续看initialize 做了哪些事?
status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
ServiceInteractionProxy* proxy)
std::lock_guard<std::mutex> lock(mInterfaceMutex);
if (proxy == nullptr)
ALOGE("%s: No valid service interaction proxy provided", __FUNCTION__);
return BAD_VALUE;
mListener = listener;
mServiceProxy = proxy;
mDeviceState = static_cast<hardware::hidl_bitfield<provider::V2_5::DeviceState>>(
provider::V2_5::DeviceState::NORMAL);
// Registering will trigger notifications for all already-known providers
bool success = mServiceProxy->registerForNotifications(
/* instance name, empty means no filter */ "",
this);
//遍历所有的代理服务, 其实也是 provider::V2_4::ICameraProvider 注册服务
for (const auto& instance : mServiceProxy->listServices())
//增加实例化的CameraProvider
this->addProviderLocked(instance);
IPCThreadState::self()->flushCommands();
return OK;
我们看看CameraProviderManager->addProviderLocked 的实现功能:
status_t CameraProviderManager::addProviderLocked(const std::string& newProvider)
......
//查找指定的CameraProvder
sp<provider::V2_4::ICameraProvider> interface;
interface = mServiceProxy->getService(newProvider);
//对指定的ProviderInfo 进行初始化
sp<ProviderInfo> providerInfo = new ProviderInfo(newProvider, this);
status_t res = providerInfo->initialize(interface, mDeviceState);
mProviders.push_back(providerInfo);
return OK;
那就看ProviderInfo 是如何进行初始化的
status_t CameraProviderManager::ProviderInfo::initialize(
sp<provider::V2_4::ICameraProvider>& interface,
hardware::hidl_bitfield<provider::V2_5::DeviceState> currentDeviceState)
......
notifyDeviceStateChange(currentDeviceState);
res = setUpVendorTags();
// Get initial list of camera devices, if any
std::vector<std::string> devices;
hardware::Return<void> ret = interface->getCameraIdList([&status, this, &devices](
Status idStatus,
const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames)
status = idStatus;
if (status == Status::OK)
for (auto& name : cameraDeviceNames)
uint16_t major, minor;
std::string type, id;
status_t res = parseDeviceName(name, &major, &minor, &type, &id);
if (res != OK)
ALOGE("%s: Error parsing deviceName: %s: %d", __FUNCTION__, name.c_str(), res);
status = Status::INTERNAL_ERROR;
else
devices.push_back(name);
mProviderPublicCameraIds.push_back(id);
);
......
sp<StatusListener> listener = mManager->getStatusListener();
for (auto& device : devices)
std::string id;
//
status_t res = addDevice(device, common::V1_0::CameraDeviceStatus::PRESENT, &id);
if (res != OK)
ALOGE("%s: Unable to enumerate camera device '%s': %s (%d)",
__FUNCTION__, device.c_str(), strerror(-res), res);
continue;
从ProviderInfo的initialize的代码中可以看到,通过ICameraProvider HAL层的接口 getCameraIdList (camera HAL 层已经遍历出来了所有的camera 的id),将cameraDeviceNames 遍历出来给到devices设备,同时把id 添加到mProviderPublicCameraIds中。
到这里就把cameraService 初始化的流程分析完了。
以上是关于Android Camera 打开预览流程分析--CameraService中的打开流程的主要内容,如果未能解决你的问题,请参考以下文章
Android Camera 打开预览流程分析--CameraService中的打开流程
Android Camera 打开预览流程分析--打开camera的SDK流程
Android Camera 打开预览流程分析--打开camera的SDK流程
Android Camera 打开预览流程分析--打开camera的SDK流程