Camera服务还是从rc文件开始
frameworks/av/camera/cameraserver/cameraserver.rc
service cameraserver /system/bin/cameraserver
class main
user cameraserver
group audio camera input drmrpc readproc
ioprio rt 4
writepid /dev/cpuset/camera-daemon/tasks /dev/stune/top-app/tasks
rlimit rtprio 10 10
接下来是main函数
int main(int argc __unused, char** argv __unused)
{
signal(SIGPIPE, SIG_IGN);
hardware::configureRpcThreadpool(3, /*willjoin*/ false);
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
CameraService::instantiate();
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
这些跟cameraprovider道理相同,打开的dev/hwbinder节点 ,最后进入自己的接收循环,这里主要分析CameraService::instantiate();
查看CameraService.h 类中没有instantiate函数,查找父类BinderService里面有这个函数static void instantiate() { publish(); }
static status_t publish(bool allowIsolated = false,
int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
sp<IServiceManager> sm(defaultServiceManager());
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated, dumpFlags);
}
这里做了2个动作(1) 实例化Service类;(2) 注册Service类到ServiceManager中
RefBase 是安卓的基类,因此在实例化的时候会调用onFirstRef()函
void CameraService::onFirstRef()
{
BnCameraService::onFirstRef();
BatteryNotifier& notifier(BatteryNotifier::getInstance());
notifier.noteResetCamera();
notifier.noteResetFlashlight();
status_t res = INVALID_OPERATION;
res = enumerateProviders();
if (res == OK) {
mInitialized = true;
}
CameraService::pingCameraServiceProxy();
mUidPolicy = new UidPolicy(this);
mUidPolicy->registerSelf();
}
Camera的服务端,是一个BnCameraService,同时还是一个BinderService,看到Bn,肯定就是本地服务端,带有onTransact接口,详细情况可以了解aidl。
类似这种:
out\soong.intermediates\frameworks\av\camera\libcamera_client\android_arm64_armv8-a_cortex-a53_core_shared\gen\aidl\android\hardware\BnCameraService.h
class BnCameraService : public ::android::BnInterface {
public:
::android::status_t onTransact(uint32_t _aidl_code, const ::android::Parcel& _aidl_data, ::android::Parcel* _aidl_reply, uint32_t _aidl_flags = 0) override;
};
上面是题外话,直接看enumerateProviders(),重点来了
status_t CameraService::enumerateProviders() {
……
mCameraProviderManager = new CameraProviderManager();//provdier管理
res = mCameraProviderManager->initialize(this);
……
status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,ServiceInteractionProxy* proxy) {
…...
bool success = mServiceProxy->registerForNotifications( this);
addProviderLocked(kLegacyProviderName, /*expected*/ false);
addProviderLocked(kExternalProviderName, /*expected*/ false);
camera Service与camera provider间通信的工程管理类, 函数initialize直接指定代理,initialize后面的参数是固定的,定义如下
status_t initialize(wp listener,
ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);
另外
struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
…..
return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
是可以直接获取CameraProvider实例的,但是具体如何通过名字获取,暂时没找到定义,但是猜测是跟hwservice manager通信,暂时当做黑盒子。
registerForNotifications
virtual bool registerForNotifications(
const std::string &serviceName,
const sp<hidl::manager::V1_0::IServiceNotification>
¬ification) override {
return hardware::camera::provider::V2_4::ICameraProvider::registerForNotifications(
serviceName, notification);
}
这个是注册cameraproviders硬件服务管理通知,而后当然要加入两个ProviderName,legacy/0 extrenal/0
addProviderLocked(kLegacyProviderName, /expected/ false);
addProviderLocked(kExternalProviderName, /expected/ false);//说明管理2个
相当于链接上CameraProvider。
CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) {
……//这里是通过名字来获取hal的ICameraProvider,执行//hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);//out 目录生成
interface = mServiceProxy->getService(newProvider);
//后面两句创建和初始化providerInfo
sp<ProviderInfo> providerInfo = new ProviderInfo(newProvider, interface, this);
status_t res = providerInfo->initialize();
用providerInfo来保存camera provider信息,这样可以方便调用。
struct ProviderInfo :
virtual public hardware::camera::provider::V2_4::ICameraProviderCallback,
virtual public hardware::hidl_death_recipient{
……
ProviderInfo继承了 hardware:📷:provider::V2_4::ICameraProviderCallback 与 hardware::hidl_death_recipient,其中ProviderInfo 第 2个参数就是camera service与cameraprovider通信的IPC接口.
Cameraserver 与cameraprovider通信都是通过ProviderInfo
providerInfo->initialize 看下他的初始化
status_t CameraProviderManager::ProviderInfo::initialize() {
……
hardware::Return<Status> status = mInterface->setCallback(this);
……//设置死亡代理
hardware::Return<bool> linked = mInterface->linkToDeath(this, /*cookie*/ mId);
……//执行到CameraProvider::getCameraIdList,hidl
hardware::Return<void> ret = mInterface->getCameraIdList([&status, &devices](Status idStatus,const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) ……
devices.push_back(cameraDeviceNames[i]);
……
status_t res = addDevice(device,
hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id);
先关注mInterface类,在ProviderInfo里的定义是
const sphardware::camera::provider::V2_4::ICameraProvider mInterface;
hardware\interfaces\camera\provider\2.4\ICameraProvider.hal
interface ICameraProvider {
CameraProvider是继承ICameraProvider的,所以之前addProviderLocked就是把hal的两个CameraProvider保存mInterface
hidl_vec<hidl_string> hidlDeviceNameList(deviceNameList)
_hidl_cb(Status::OK, hidlDeviceNameList);
这个是在CameraProvider返回值
CameraProviderManager::ProviderInfo::addDevice->
status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name,
CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) {
……//先创建然后添加结构
deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid, id, minor);
mDevices.push_back(std::move(deviceInfo));
mUniqueCameraIds.insert(id);
通过调用获取信息,存入ProviderInfo的下面结构体里。
struct DeviceInfo3 : public DeviceInfo
std::vector<std::unique_ptr> mDevices;
std::unordered_setstd::string mUniqueCameraIds;
initializeDeviceInfo(name, mProviderTagid, id, minor);
先执行getDeviceInterface函数最终执行
new DeviceInfoT(name, tagId, id, minorVersion, resourceCost,
cameraInterface));//模板类,DeviceInfoT是DeviceInfo3
CameraProviderManager::ProviderInfo::DeviceInfo3::DeviceInfo3
传入的最后参数是(cameraInterface)是getDeviceInterface(name);获得
struct DeviceInfo3 : public DeviceInfo {
typedef hardware:📷:device::V3_2::ICameraDevice InterfaceT;
DeviceInfo3中定义的InterfaceT 是hardware:📷:device::V3_2::ICameraDevice 类型
CameraProviderManager::ProviderInfo::getDeviceInterface
//认为cameraInterface就是对下接口。是CameraDevice,下面讲解
mInterface->getCameraDeviceInterface_V3_x(name, [&status, &cameraInterface](
Status s, spdevice::V3_2::ICameraDevice interface)
CameraProvider::getCameraDeviceInterface_V3_x
这个函数执行了new 如下CameraDevice类,把CameraProvider里面之前的打开hal的module传入,也就是CameraDevice里面包含了hal。
android::hardware:📷:device::V3_2::implementation::CameraDevice
最后执行到hardware\interfaces\camera\device\3.2\default\ CameraDevice.cpp
CameraDevice::CameraDevice
这里面通过才做module操作到hal,device = deviceImpl; 所以返回的是CameraDevice,对于service是ICameraDevice
CameraProviderManager::ProviderInfo::DeviceInfo3::DeviceInfo3
保存了mInterface就是之前的CameraDevice,也就可以访问hal了。
总结
所以最早跑provider,里面跑hal,最后跑cameraserver,cameraserver可以通过provider再调用到hal。
cameraserver主要调用CameraProviderManager(包含mProviders(内部和外部两个ProviderInfo)都包含mDevices(多个DeviceInfo3),DeviceInfo3包含ICameraDevice)。CameraDevice里面有provider打开的hal模块句柄,就能操作到hal
camera开机时候各个服务启动与加载就这么完成了,接下来是等待用户调用api接口操作