https://deepinout.com/android-camera-native-framework/cameraserver-process-startup-flow-overview.html
1 CameraServer Main函数流程 ( bin 文件)
/frameworks/av/camera/cameraserver/main_cameraserver.cpp
int main(int argc __unused, char** argv __unused)
{
signal(SIGPIPE, SIG_IGN); // 忽略调 SIGPIPE,以免进程被close。
// Set 5 threads for HIDL calls. Now cameraserver will serve HIDL calls in
// addition to consuming them from the Camera HAL as well.
// 配置5个线程来处理HIDL调用
hardware::configureRpcThreadpool(5, /*willjoin*/ false);
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
ALOGI("ServiceManager: %p", sm.get());
CameraService::instantiate();
ALOGI("ServiceManager: %p done instantiate", sm.get());
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
1.1 SIGPIPE信号
在TCP通信中,当通信的双方中的一方close一个连接时,若另一方接着发数据,根据TCP协议的规定,会收到一个RST响应报文,若再往这个服务器发送数据时,系统会发出一个SIGPIPE信号给进程,告诉进程这个连接已经断开了,不能再写入数据。
该信号的缺省行为是终止进程,因此进程必须捕获它以免不情愿的被终止。
若不想退出可以把 SIGPIPE设为SIG_IGN。
1.2 配置5个线程来处理HIDL调用
可以通过ps 命令查看有几个线程
ps -T -p 11657
2 CameraService 实例化流程 (libcameraservice.so)
2.1 注册media.camera服务
/frameworks/av/camera/cameraserver/main_cameraserver.cpp
int main(int argc __unused, char** argv __unused)
{
CameraService::instantiate();
}
/frameworks/native/include/binder/BinderService.h
class BinderService
{
public:
// 2. 添加 service 注册到 ServiceManager
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);
}
static void instantiate() { publish(); } // 1. 调用publish方法
};
查看服务是否注册成功
emulator_x86_64:/ # service list |grep -i "media.camera"
104 media.camera: [android.hardware.ICameraService]
105 media.camera.proxy: [android.hardware.ICameraServiceProxy]
2.2 onFirstRef流程
/frameworks/native/include/binder/BinderService.h
class BinderService
{
public:
// 2. 添加 service 注册到 ServiceManager
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);
}
};
new SERVICE 的时候 会创建一个binder对象并返回其指针 ,onFirstRef机制通过检查强引用计数来判断对象是否是第一次被调用。
如果是第一次调用则, 会调用CameraService::onFirstRef(虚函数)。
/frameworks/native/cmds/servicemanager/ServiceManager.cpp
Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
auto it = mNameToRegistrationCallback.find(name);
if (it != mNameToRegistrationCallback.end()) {
for (const sp<IServiceCallback>& cb : it->second) {
mNameToService[name].guaranteeClient = true;
// permission checked in registerForNotifications
cb->onRegistration(name, binder);
}
}
return Status::ok();
}
CameraService::onFirstRef
void CameraService::onFirstRef()
{
ALOGI("CameraService process starting");
BnCameraService::onFirstRef();
// Update battery life tracking if service is restarting
// 1.复位电量统计
BatteryNotifier& notifier(BatteryNotifier::getInstance());
notifier.noteResetCamera();
notifier.noteResetFlashlight();
//2. 与hal进程交互,查看有几个 provider
status_t res = INVALID_OPERATION;
res = enumerateProviders();
if (res == OK) {
mInitialized = true;
}
//3. 监听UID的状态变化,只有进程UID为active才允许使用camera
mUidPolicy = new UidPolicy(this);
mUidPolicy->registerSelf();
//4. 检查是否启动了隐私管理
mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
mSensorPrivacyPolicy->registerSelf();
mInjectionStatusListener = new InjectionStatusListener(this);
//5. 监听振动、铃声,以保证OIS
mAppOps.setCameraAudioRestriction(mAudioRestriction);
sp hcs = HidlCameraService::getInstance(this);
//6. 注册HIDL service, 让hal访问到framework
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.
//7. ping CameraServiceProxy 以获取信息(折叠屏、usb camera等)
CameraServiceProxyWrapper::pingCameraServiceProxy();
ALOGI("CameraService pinged cameraservice proxy");
}