Android Camera CameraService启动流程 (androidP)

Posted we1less

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Camera CameraService启动流程 (androidP)相关的知识,希望对你有一定的参考价值。

cameraserver.rc

路径  frameworks/av/camera/cameraserver/cameraserver.rc

        CameraService是由这个.rc文件启动的关于rc文件参见流程1

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_cameraserver.cpp 

路径  frameworks/av/camera/cameraserver/main_cameraserver.cpp 

        CameraService::instantiate() 接口定义在 BinderService 中,CameraService继承它,instantiate() 函数定义在

路径  android/frameworks/native/libs/binder/include/binder/BinderService.h

创建一个new CameraService,名为media.camera并将其加入到 ServiceManager 中。

        getServiceName() = "media.camera"

        因为SP指针的原因下一步调用到的是CameraService::onFirstRef()

int main(int argc __unused, char** argv __unused)

    signal(SIGPIPE, SIG_IGN);

    // Set 3 threads for HIDL calls
    hardware::configureRpcThreadpool(3, /*willjoin*/ false);

    sp<ProcessState> proc(ProcessState::self());
    sp<IServiceManager> sm = defaultServiceManager();
    ALOGI("ServiceManager: %p", sm.get());
    CameraService::instantiate();
    ProcessState::self()->startThreadPool();
    IPCThreadState::self()->joinThreadPool();



//路径  android/frameworks/native/libs/binder/include/binder/BinderService.h

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);
    

//路径  frameworks/av/services/camera/libcameraservice/CameraService.h
static char const* getServiceName()  return "media.camera"; 

onFirstRef

路径  frameworks/av/services/camera/libcameraservice/CameraService.cpp

        在 CameraService::onFirstRef() 函数中,主要是调用 enumerateProviders() 函数获取CameraProvider信息

        new CameraProviderManager() 之后调用 initialize(), 并将CameraService作为 CameraProviderManager 的成员变量传递进去

void CameraService::onFirstRef()

    res = enumerateProviders();
    ...


status_t CameraService::enumerateProviders()     
     ...
        if (nullptr == mCameraProviderManager.get()) 
            mCameraProviderManager = new CameraProviderManager();
            res = mCameraProviderManager->initialize(this);
            ...
        
        ...
        deviceIds = mCameraProviderManager->getCameraDeviceIds();
    ...
    for (auto& cameraId : deviceIds) 
        String8 id8 = String8(cameraId.c_str());
        onDeviceStatusChanged(id8, CameraDeviceStatus::PRESENT);
    

    return OK;

CameraProviderManager::initialize

路径

  frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

        将上文传递进来的CameraService作为监听保存在CameraProviderManager中,  

        mServiceProxy = sHardwareServiceInteractionProxy       

//路径  
//frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.h

status_t initialize(wp<StatusListener> listener,
            ServiceInteractionProxy *proxy = &sHardwareServiceInteractionProxy);

//路径  
//frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

const std::string kLegacyProviderName("legacy/0");
const std::string kExternalProviderName("external/0");

CameraProviderManager::sHardwareServiceInteractionProxy;

status_t CameraProviderManager::initialize(wp<CameraProviderManager::StatusListener> listener,
        ServiceInteractionProxy* proxy) 
    ...
    mListener = listener;
    mServiceProxy = proxy;
    ...
    bool success = mServiceProxy->registerForNotifications(
        /* instance name, empty means no filter */ "",
        this);
    ...
    addProviderLocked(kLegacyProviderName, /*expected*/ false);
    addProviderLocked(kExternalProviderName, /*expected*/ false);

    return OK;

 addProviderLocked

路径  

frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

        newProvider 参数分别是 "legacy/0" 和 "external/0"

        expected = false

        mServiceProxy = sHardwareServiceInteractionProxy

        getService() 在路径为

frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.h
     getService()将会调用         hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName)

        ICameraProvider::getService流程在这篇分析过Android Camera(一) Provider启动流程 (androidP)(HIDL)_we1less的博客-CSDN博客
       所以  interface = CameraProvider 代理(通过ServiceManager获得的)

        在获得CameraProvider代理之后,将会根据 ICameraProvider 填充相应的 providerInfo

status_t CameraProviderManager::addProviderLocked(const std::string& newProvider, bool expected) 
    //循环判断是否已经添加provider
    for (const auto& providerInfo : mProviders) 
     ...
    
    
    sp<provider::V2_4::ICameraProvider> interface;
    interface = mServiceProxy->getService(newProvider);
    ...
    sp<ProviderInfo> providerInfo =
            new ProviderInfo(newProvider, interface, this);
    status_t res = providerInfo->initialize();
    ...
    mProviders.push_back(providerInfo);

    return OK;


//路径
//frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.h

// Standard use case - call into the normal generated static methods which invoke
    // the real hardware service manager
    struct HardwareServiceInteractionProxy : public ServiceInteractionProxy 
        virtual bool registerForNotifications(
                const std::string &serviceName,
                const sp<hidl::manager::V1_0::IServiceNotification>
                &notification) 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);
        
    ;

hardware::camera::provider::V2_4::ICameraProvider::getService(serviceName)

路径  

out/soong/.intermediates/hardware/interfaces/camera/provider/2.4/android.hardware.camera.provider@2.4_genc++/gen/android/hardware/camera/provider/2.4/CameraProviderAll.cpp

        getStub = false 时,走此流程,拿到 cameraprovider  一般用于其他服务获取cameraprovider服务,此部分在Android Camera(一) Provider启动流程 (androidP)(HIDL)_we1less的博客-CSDN博客这篇已经分析过所以此处就做个结论:此处的 getRawServiceInternal() 调用参数为 false,getRawServiceInternal() 函数在 CameraProvider 注册为服务的时候调用了,只是调用参数不一致,所以,这里返回的将会是 CameraProvider 代理。

//在路径
/* out/soong/.intermediates/hardware/interfaces/camera/provider/2.4/android.hardware.camera.provider@2.4_genc++_headers/gen/android/hardware/camera/provider/2.4/ICameraProvider.h
中有定义*/

static ::android::sp<ICameraProvider> getService(const ::android::hardware::hidl_string& serviceName, bool getStub=false)   std::string str(serviceName.c_str());      return getService(str, getStub); 


//调用的则是  注意此时  getStub=false

::android::sp<ICameraProvider> ICameraProvider::getService(const std::string &serviceName, const bool getStub) 
    return ::android::hardware::details::getServiceInternal<BpHwCameraProvider>(serviceName, true, getStub);

CameraProviderManager::ProviderInfo::initialize

CameraProviderManager::ProviderInfo::ProviderInfo

路径  

frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

        可以从上文new ProviderInfo(newProvider, interface, this);中看出

        ProviderInfo::mInterface =  CameraProviderManager::interface = CameraProvider 代理

        providerName 分别为  "legacy/0" "external/0"

        在初始化initialize的过程中 调用了mInterface的

                setCallback(this)

                linkToDeath

                getCameraIdList 

                实际上就是调用了HAL层的CameraProvider中的相关函数,关于这个在上一篇说到过,在路径

hardware/interfaces/camera/provider/2.4/ICameraProvider.hal

中有定义 getCameraIdList() generates (Status status, vec<string> cameraDeviceNames);

返回一个由cameraDeviceNames组成的列表,将返回的设备填装到devices中,载将这个取得到的camera信息保存到 mDevices中,至此,CameraService与CameraProvider已经关联起来。
        device的值一般类似以下: device@3.3/legacy/0
        device的值是CameraProvider服务获取device之后,添加 mCameraDeviceNames,
        该变量的含义如下:@3.3  第一个 3 是与camera HAL的版本号相关,只要不是 CAMERA_DEVICE_API_VERSION_1_0,都是3
                                                   第二个 3 是从系统属性 ro.vendor.camera.wrapper.hal3TrebleMinorVersion 获取的,没有则是3
                                         legacy/0 : 则与 CameraProvider 服务相关

//构造器
CameraProviderManager::ProviderInfo::ProviderInfo(
        const std::string &providerName,
        sp<provider::V2_4::ICameraProvider>& interface,
        CameraProviderManager *manager) :
        mProviderName(providerName),
        mInterface(interface),
        mProviderTagid(generateVendorTagId(providerName)),
        mUniqueDeviceCount(0),
        mManager(manager) 
    (void) mManager;


status_t CameraProviderManager::ProviderInfo::initialize() 
    status_t res = parseProviderName(mProviderName, &mType, &mId);
    ...
    hardware::Return<Status> status = mInterface->setCallback(this);
    ...
    hardware::Return<bool> linked = mInterface->linkToDeath(this, /*cookie*/ mId);
    ...
    std::vector<std::string> devices;
    hardware::Return<void> ret = mInterface->getCameraIdList([&status, &devices](
            Status idStatus,
            const hardware::hidl_vec<hardware::hidl_string>& cameraDeviceNames) 
        status = idStatus;
        if (status == Status::OK) 
            for (size_t i = 0; i < cameraDeviceNames.size(); i++) 
                //将返回的设备填装到devices中
                devices.push_back(cameraDeviceNames[i]);
            
         );
    ...

    sp<StatusListener> listener = mManager->getStatusListener();
    for (auto& device : devices) 
        std::string id;
        status_t res = addDevice(device,
                hardware::camera::common::V1_0::CameraDeviceStatus::PRESENT, &id);
        ...
    
    ...
    mInitialized = true;
    return OK;

CameraProviderManager::ProviderInfo::addDevice

 路径  frameworks/av/services/camera/libcameraservice/common/CameraProviderManager.cpp

        根据HAL版本,创建相应的 DeviceInfo

        将上述获取得到的camera信息保存到 mDevices

status_t CameraProviderManager::ProviderInfo::addDevice(const std::string& name,
        CameraDeviceStatus initialStatus, /*out*/ std::string* parsedId) 
    ...
    uint16_t major, minor;
    std::string type, id;

    status_t res = parseDeviceName(name, &major, &minor, &type, &id);
    ...
    std::unique_ptr<DeviceInfo> deviceInfo;
    switch (major) 
        case 1:
            deviceInfo = initializeDeviceInfo<DeviceInfo1>(name, mProviderTagid,
                    id, minor);
            break;
        case 3:
            deviceInfo = initializeDeviceInfo<DeviceInfo3>(name, mProviderTagid,
                    id, minor);
            break;
        ...
    mDevices.push_back(std::move(deviceInfo));

    mUniqueCameraIds.insert(id);
    ...
    return OK;

以上是关于Android Camera CameraService启动流程 (androidP)的主要内容,如果未能解决你的问题,请参考以下文章

android中的camera和camera2权限

问题支持Android相机的API和camera2 API问题,怎么解决

Android Camera2 拍照入门学习

android camera:camera模组CMM介绍

每次“Camera camera = Camera.open();”时,Android都会抛出错误叫做

Android Camera2 教程 · 第一章 · 概览