一:init.rc
文件位置:Android6.0/system/core/rootdir/init.rc
665 service media /system/bin/mediaserver
666 class main
667 user media
668 group audio camera inet net_bt net_bt_admin net_bw_acct drmrpc mediadrm
669 ioprio rt 4
二:aidl
源码位置: android6.0/frameworks/base/core/java/android/hardware/ICameraService.aidl
二:mediaserver
源码位置:android6.0/frameworks/av/media/mediaserver/main_mediaserver.cpp
if (doLog) {
prctl(PR_SET_PDEATHSIG, SIGKILL); // if parent media.log dies before me, kill me also
setpgid(0, 0); // but if I die first, don't kill my parent
}
InitializeIcuOrDie();
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
ALOGI("ServiceManager: %p", sm.get());
AudioFlinger::instantiate();
MediaPlayerService::instantiate();
ResourceManagerService::instantiate();
CameraService::instantiate();//启动Camera Service服务
AudioPolicyService::instantiate();
SoundTriggerHwService::instantiate();
RadioService::instantiate();
registerExtensions();
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
三:CameraService类
源码位置:android6.0/frameworks/av/services/camera/libcameraservice/CameraService.cpp
class CameraService :
public BinderService<CameraService>, //模板类 主要是封装了将Binder 添加到ServiceManager进程中
public virtual ::android::hardware::BnCameraService, // ICameraService的服务端需要继承BnCameraService(native binder的写法)
public virtual IBinder::DeathRecipient, //Binder死亡监听 监听客户端进程的死亡情况
public virtual CameraProviderManager::StatusListener{}
// CameraService::instantiate(); 调用这里的instantiate() 主要是向sm注册了ICameraService的服务端binder
template<typename SERVICE>
class BinderService
{
public:
static status_t publish(bool allowIsolated = false,int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
sp<IServiceManager> sm(defaultServiceManager());
// 这里就是向sm进程中注册Binder 这里的Binder的名称是 "media.camera"
return sm->addService(String16(SERVICE::getServiceName()), new SERVICE(), allowIsolated,dumpFlags);
}
static void publishAndJoinThreadPool(bool allowIsolated = false,int dumpFlags = IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT) {
publish(allowIsolated, dumpFlags);
joinThreadPool();
}
static void instantiate() { publish(); }
static status_t shutdown() { return NO_ERROR; }
private:
static void joinThreadPool() {
sp<ProcessState> ps(ProcessState::self());
ps->startThreadPool();
ps->giveThreadPoolName();
IPCThreadState::self()->joinThreadPool();
}
};
上面的new Service()会调用CameraService的构造函数。CameraService继承BnCameraService(这个是AIDL--cpp生成的代码)所以CameraService.cpp是ICameraService.adil的IPC机制的服务端。构造函数如下:
static CameraService *gCameraService;
CameraService::CameraService() : mEventLog(DEFAULT_EVENT_LOG_LENGTH), mAllowedUsers(),
mSoundRef(0), mModule(0), mFlashlight(0) {
ALOGI("CameraService started (pid=%d)", getpid());
gCameraService = this;
this->camera_device_status_change = android::camera_device_status_change;
this->torch_mode_status_change = android::torch_mode_status_change;
mServiceLockWrapper = std::make_shared<WaitableMutexWrapper>(&mServiceLock);
}
构造函数执行完成之后,sm里面注册了该服务的binder。然后因为CamerService继承了RefBase所以CamerService对象在第一次被创建的时候会调用到onFirstRef()函数 。
四:CameraService::onFirstRef
源码位置:android6.0/frameworks/av/services/camera/libcameraservice/CameraService.cpp
void CameraService::onFirstRef()
{
ALOGI("CameraService process starting");
BnCameraService::onFirstRef();
// Update battery life tracking if service is restarting
BatteryNotifier& notifier(BatteryNotifier::getInstance());
notifier.noteResetCamera();
notifier.noteResetFlashlight();
camera_module_t *rawModule;
int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
(const hw_module_t **)&rawModule);
if (err < 0) {
ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
logServiceError("Could not load camera HAL module", err);
mNumberOfCameras = 0;
mNumberOfNormalCameras = 0;
return;
}
mModule = new CameraModule(rawModule);
err = mModule->init();
if (err != OK) {
ALOGE("Could not initialize camera HAL module: %d (%s)", err,
strerror(-err));
logServiceError("Could not initialize camera HAL module", err);
mNumberOfCameras = 0;
delete mModule;
mModule = nullptr;
return;
}
ALOGI("Loaded \"%s\" camera module", mModule->getModuleName());
mNumberOfCameras = mModule->getNumberOfCameras();
mNumberOfNormalCameras = mNumberOfCameras;
hw_get_module(CAMERA_HARDWARE_MODULE_ID,(const hw_module_t **)&rawModule);函数加载Android hal层库函数,libcamera。new CameraModule(rawModule),mModule->init()会执行android hal函数,扫描加载的摄像头信息。
参考:Android Camera Hal
五:客户端连接CameraService
1.CameraService::connectDevice
android6.0/frameworks/av/services/camera/libcameraservice/CameraService.cpp
status_t CameraService::connectDevice(
const sp<ICameraDeviceCallbacks>& cameraCb,
int cameraId,
const String16& clientPackageName,
int clientUid,
/*out*/
sp<ICameraDeviceUser>& device) {
ATRACE_CALL();
status_t ret = NO_ERROR;
String8 id = String8::format("%d", cameraId);
sp<CameraDeviceClient> client = nullptr;
ret = connectHelper<ICameraDeviceCallbacks,CameraDeviceClient>(cameraCb, id,
CAMERA_HAL_API_VERSION_UNSPECIFIED, clientPackageName, clientUid, API_2, false, false,
/*out*/client);
if(ret != NO_ERROR) {
logRejected(id, getCallingPid(), String8(clientPackageName),
String8::format("%s (%d)", strerror(-ret), ret));
return ret;
}
device = client;
return NO_ERROR;
}
2.CameraService::connectHelper
android6.0/frameworks/av/services/camera/libcameraservice/CameraService.h
主要执行功能。获取互斥锁,验证权限,验证优先级,
参考资源:
Android 多进程同时打开相机 - 代码先锋网 (codeleading.com)
(43条消息) Android源码分析--MediaServer源码分析(一)_Joe_c 的专栏-CSDN博客
(43条消息) Camera应用在调试中遇到的一些问题_飞翔大企鹅的专栏-CSDN博客