一、CameraService启动流程

CameraService之服务流程图
一、CameraService启动流程
二、CameraProvider启动流程
三、CameraService总流程

1、启动脚本

//framework/av/camera/cameraserver/cameraserver.rc
//通过init进程启动cameraserver进程,启动路径为/system/bin/cameraserver
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/top-app/tasks

进程对应的文件为framework/av/camera/cameraserver/main_cameraserver.cpp

int main(int argc __unused, char** argv __unused)
{
        signal(SIGPIPE, SIG_IGN);
        sp<ProcessState> proc(ProcessState::self());
        sp<IServiceManager> sm = defaultServiceManager();
        ALOGI("ServiceManager: %p", sm.get());
        //初始化CameraService服务
        CameraServe::instantiaicte();
        ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();
}

2、CameraServe::instantiaicte

static void instantiate() {
    publish();
}

3、publish函数

首先通过CameraService::getServiceName()获取CameraService的服务名称"media.camera",而后新建CameraService

static status_t publish(bool allowIsolated = false) {
    //获取IServiceManager服务器、代理对象BpServiceManager
    sp<IServiceManager> sm(defaultServiceManager());
    //注册服务SERVICE为CameraService
    return sm->addService(String16(SERVICE::getServiceName()),new SERVICE(),allowIsolated);
}

4、addService 函数

调用BpServiceManager的addService方法
framework/native/libs/binder/IServiceManager.cpp

virtual stauts_t addService(const String16& name,const sp<IBinder>& service, bool allowIsolated)
{
    Parcel data,reply;
    data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
    data.writeString16(name);
    data.writeStrongBinder(service);
    data.writeInt32(allowIsolated ? 1 : 0);
    stauts_t err = remote()->transact(ADD_SERVICE_TRANSACTION,data,&reply);
    return err == NO_ERROR ? reply.readExceptionCode():err;
}

5、sp

addService的 第二个参数 const sp& service传入的参数是CameraService对象,会自动调用sp的自动类型转换
system/core/libutils/include/utils/StrongPointer.h

//T 为 IBinder; U为 CameraService
templat<typename T>
templat<typename U>
sp<T>::sp(U*other) : m_ptr(other) {
    if(other) {
        (static_cast<T*>(other))->incStrong(this);
    }
}

6、incStrong函数

inStrong 会触发 onFirstRef方法,是获取所有相机 IcameraProvider代理对象BpHwCameraProvider的流程
system/core/libutils/RefBase.cpp

void RefBase::incStrong(const void *id) const
{
        weakref_impl* const refs = mRefs;
    refs->incWeak(id);
     
    refs->addStrongRef(id);
    const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed);
 
    if (c != INITIAL_STRONG_VALUE)  {
        return;
    }
 
    int32_t old = refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE,
            std::memory_order_relaxed);
   //触发onFirstRef方法
    refs->mBase->onFirstRef();
}

7、CameraService::onFirstRef

void CameraService::onFirstRef()
{
    ALOGI("CameraServer process starting");
    BnCameraService::onFirstRef();
    ...
    //在cameraService启动流程-获取ICameraProvider服务代理对象,BpHwCameraProvider ,并由此获得全部相机设备,代理对象BpHwCameraDevice
    res = enumrateProviders();
    ...
    CameraService::pingCameraServiceProxy();
}

8、CameraService::enumerateProviders

status_t CameraService::enumerateProviders() {
    status_t res;
     
    std::vector<std::string> deviceIds;
    {
        Mutex::Autolock 1(mServiceLock);
         
        if(nullptr == mCameraProviderManager.get()) {
            mCameraProviderManager = new CameraProviderManager();
            res = mCameraProviderManager->initialize(this);
        }
    }
}

9、CameraProviderManager::initialize

CameraProviderManager就是管理camera Service与camera provider之间通信的工程管理类,两个参数,其中第二个参数就是远程代理类
framework/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
    ServiceInteractionProxy* proxy) {
        std::lock_guard<std::mutex> lock(mInterfaceMutex);
         
        /*listener = CameraService对象*/
        mListener = listener;
         
        /* proxy = sHardwareServiceInteractionProxy */
        mServiceProxy = proxy;
         
        bool success = mServiceProxy->registerForNotifications("",this);
         
        /*这儿实现了camera service 与 camera provider的桥接*/
        addProviderLocked(kLegacyProviderName, /*expected*/ false);
        addProviderLocked(kExternalProviderName, /*expected*/ false);
        return ok;
    }

proxy 是默认值, proxy = sHardwareServiceInteractionProxy,这个sHardwareServiceInteractionProxy是 ---->
HardwareServiceInteractionProxy 的实例

struct HardwareServiceInteractionProxy : public ServiceInteractionProxy {
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);
}
 
virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(const std::string &serviceName) override {
return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
}

这样一来,就得到了CameraProvider

10、addProviderLocked 函数

tatus_t CameraProviderManger::addProviderLocked(const std::string& newProvider, bool expected){
    sp<provider::V2_4::ICameraProvider> interface;
    /*
        上面分析过 mServiceProxy = sHardwareServiceInteractionProxy
        这里 getService(newProvider) 其实就是得到 CameraProvider 的代理类
        所以CameraService 与 CameraProvider 就联系起来了
        接着分析这个函数 HardwareServiceInteractionProxy::getService()
    */
    interface = mServiceProxy->getService(newProvider);
     
    sp<ProviderInfo> providerInfo = new ProviderInfo(newProvider,interface,this);
    status_t res = providerInfo->initialize();//初始化camera provider
    //CameraProviderManager.h:    std::vector> mProviders;
    mProviders.push_back(providerInfo);
    return OK;
}

getService

virtual sp<hardware::camera::provider::V2_4::ICameraProvider> getService(const std::string &serviceName) override {
    /*
      * 调用了 ICameraProvider::getService()
      * 且 getStub 为默认值 getStub = flase
      * 之前分析过,getStub = flase 会得到 CameraProvider 的代理类(binder)
     */
     return hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName);
}

11、ProviderInfo数据结构

在camera service中 就是使用 ProviderInfo 来和底层的camera provider通信的
ameraProviderManager 中提供了 一个ProviderInfo来保存 Camera provider的信息,方便管理Camera service 调用 camera provider

struct ProviderInfo :
        virtual public hardware::camera::provider::V2_4ICameraProviderCallback,//ICameraProviderCallback 是 camera provider的 回调接口,也是可以IPC间通信的
        virtual public hardware::hidl_death_recipient //hardware::hidl_death_recipient 是hal层的死亡回调接口,方便在底层死亡的时候通知上层
{
    const std::string mProviderName;
    const sp<hardware::camera::provider::V2_4::ICameraProvider> minterface;
     
    //interface 是camera service与cameraprovider通信的IPC接口,保证两层可以顺利通信
    ProviderInfo(const std::string &providerName,
    sp<hardware::camera::provider:V2_4::ICameraProvider>& interface,CameraProviderManager *manager);
 
    /初始化camera provider 并且IPC调用到camera provider 获取camera device信息
    status_t initialize();
 
    //然后调用 addDevice接口将获取的camera device保存在内存中
    //addDevice ----> 将底层获取的camera device信息保存在camera service中,防止多次跨进程调用。
 
    status_t addDevice(const std::string & name,
                       hardware::camera::common::V1_0::CameraDeviceStatus initialStatus =
                       hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT,
                       std::string *parseId = nullptr);
 
    //当camera provider发生变化的时候需要通知上层这些变化
    virtual hardware::Return<void> cameraDeviceStatusChange(
            const hardware::hidl_string& cameraDeviceName,
            hardware::camera::common::V1_0::CameraDeviceStatus newStatus) override;
 
    virtual hardware::Return<void> torchModeStatusChange(
            const hardware::hidl_string& cameraDeviceName,
            hardware::camera::common::V1_0::TorchModeStatus newStatus) override;
 
    virtual void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
}

12、DeviceInfo3的数据结构

ProviderInfo ----> DeviceInfo3数据结构
// HALv3-specific camera fields, including the actual device interface
struct DeviceInfo3 : public DeviceInfo {
    typedef hardware::camera::device::V3_2::ICameraDevice InterfaceT;
    const sp<InterfaceT> mInterface;//hardware::camera::device::V3_2::ICameraDevice
  
    virtual status_t setTorchMode(bool enabled) override;
    virtual status_t getCameraInfo(hardware::CameraInfo *info) const override;
    virtual bool isAPI1Compatible() const override;
    virtual status_t dumpState(int fd) const override;
    virtual status_t getCameraCharacteristics(
            CameraMetadata *characteristics) const override;
  
    DeviceInfo3(const std::string& name, const metadata_vendor_id_t tagId,
            const std::string &id, uint16_t minorVersion,
            const hardware::camera::common::V1_0::CameraResourceCost& resourceCost,
            sp<InterfaceT> interface);
    virtual ~DeviceInfo3();
private:
    CameraMetadata mCameraCharacteristics;
};

一、CameraService启动流程_第1张图片

你可能感兴趣的:(Camera,android)