接着上一遍文章,我们将理解CameraService的获取与构造。
CameraService的启动:
av/camera/cameraserver/Android.bp
->av/camera/cameraserver/cameraserver.rc
->av/camera/cameraserver/main_cameraserver.cpp
- Andorid.mk将
cameraserver.rc 打包到system/etc/init目录下,并指定源代码为main_cameraserver.cpp ; - Android架构启动时启动
init.rc 进程,启动了位于system/etc/init目录下的cameraserver.rc 。值得一提的是,同时也会启动zygote进程(第一个Java层进程,也是Java层所有进程的父进程)、ServiceManager、mediaserver(多媒体服务进程,Android7.0之前cameraserver属于mediaserver,和mediaserver位于同一进程)、surfaceflinger(屏幕渲染相关的进程)等。之后zygote会孵化出启动system_server进程,Android framework里面的所有service(ActivityManagerService、WindowManagerService等)都是由system_server启动。关于system_server启动暂不讲述。 - 启动
cameraserver.rc 时,执行main_cameraserver.cpp 中的main 方法,通过CameraServe::instantiaicte() 初始化CameraService
/******************************************封存********************************************/
OpenCamera中使用CameraService.
如何传递到CameraService.cpp中
base/core/java/android/hardware/camera2/CameraManager.java
connectDevice->
av/camera/aidl/android/hardware/ICameraService.aidl
connectDevice->
av/services/camera/libcameraservice/hidl/HidlCameraService.cpp
connectDevice->
av/services/camera/libcameraservice/CameraService.cpp
connectDevice->
ret = connectHelper<hardware::camera2::ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
/*api1CameraId*/-1,
CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageNameAdj, clientFeatureId,
clientUid, USE_CALLING_PID, API_2, /*shimUpdateOnly*/ false, /*out*/client);
cameraCb;ICameraDeviceCallbacks
clientPackageNameAdj:getOpPackageName包名
id:cameraId
client:CameraDeviceClient
//此处传入参数mCameraProviderManager暂未分析,
if(!(ret = makeClient(this, cameraCb, clientPackageName, clientFeatureId,
cameraId, api1CameraId, facing,
clientPid, clientUid, getpid(),
halVersion, deviceVersion, effectiveApiLevel,
/*out*/&tmp)).isOk()) {
return ret;
}
....
err = client->initialize(mCameraProviderManager, mMonitorTags);
makeClient:
else { // Camera2 API route
sp<hardware::camera2::ICameraDeviceCallbacks> tmp =
static_cast<hardware::camera2::ICameraDeviceCallbacks*>(cameraCb.get());
*client = new CameraDeviceClient(cameraService, tmp, packageName, featureId,
cameraId, facing, clientPid, clientUid, servicePid);
}
ICameraDeviceCallbacks
CameraDeviceClient
CameraDeviceClient->Camera2ClientBase->
CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,//回调函数
const String16& clientPackageName,
const std::unique_ptr<String16>& clientFeatureId,
const String8& cameraId,
int cameraFacing,
int clientPid,
uid_t clientUid,
int servicePid) :
Camera2ClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
cameraId, /*API1 camera ID*/ -1,
cameraFacing, clientPid, clientUid, servicePid),
mInputStream(),
mStreamingRequestId(REQUEST_ID_NONE),
mRequestIdCounter(0) {
ATRACE_CALL();
ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
}
template <typename TClientBase>
Camera2ClientBase<TClientBase>::Camera2ClientBase(
const sp<CameraService>& cameraService,
const sp<TCamCallbacks>& remoteCallback,
const String16& clientPackageName,
const std::unique_ptr<String16>& clientFeatureId,
const String8& cameraId,
int api1CameraId,
int cameraFacing,
int clientPid,
uid_t clientUid,
int servicePid):
TClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
cameraId, api1CameraId, cameraFacing, clientPid, clientUid, servicePid),
mSharedCameraCallbacks(remoteCallback),
mDeviceVersion(cameraService->getDeviceVersion(TClientBase::mCameraIdStr)),
mDevice(new Camera3Device(cameraId)),
mDeviceActive(false), mApi1CameraId(api1CameraId)
{
ALOGI("Camera %s: Opened. Client: %s (PID %d, UID %d)", cameraId.string(),
String8(clientPackageName).string(), clientPid, clientUid);
mInitialClientPid = clientPid;
LOG_ALWAYS_FATAL_IF(mDevice == 0, "Device should never be NULL here.");
}
?
|