Camera service

King of heaven and earth tiger 626 2021-04-02 11:42:48 阅读数:253

本文一共[544]字,预计阅读时长:1分钟~
camera service

stay camera service The structure of this end is still very confusing , I just watched it for a long time , To understand the relationship .

service This section includes the following header files :ICamera.h, ICameraService.h, CameraService.h, Corresponding implementation ICamera.cpp, ICameraService.cpp, CameraService.cpp.

CameraService Contains an inner class CameraService::Client, This CameraService::Client It's right ICamera The implementation of the ,camera client Got ICamera This is this. CameraService::Client.

That is to say, with camera client Real communication , This is it CameraService::Client. Let's analyze it step by step service The realization of this .


1.ICameraService

(1)ICameraService.h

It only defines 3 A way :

    virtual int32_t         getNumberOfCameras() = 0;
    virtual status_t        getCameraInfo(int cameraId, struct CameraInfo* cameraInfo) = 0;
    virtual sp<ICamera>     connect(const sp<ICameraClient>& cameraClient,  int cameraId) = 0;


(2) ICameraService.cpp

Look at the code :

status_t BnCameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case GET_NUMBER_OF_CAMERAS: {
            CHECK_INTERFACE(ICameraService, data, reply);
            reply->writeInt32(getNumberOfCameras());
            return NO_ERROR;
        } break;
        case GET_CAMERA_INFO: {
            CHECK_INTERFACE(ICameraService, data, reply);
            CameraInfo cameraInfo;
            memset(&cameraInfo, 0, sizeof(cameraInfo));
            status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
            reply->writeInt32(cameraInfo.facing);
            reply->writeInt32(cameraInfo.orientation);
            reply->writeInt32(result);
            return NO_ERROR;
        } break;
        case CONNECT: {
            CHECK_INTERFACE(ICameraService, data, reply);
            sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
            sp<ICamera> camera = connect(cameraClient, data.readInt32());
            reply->writeStrongBinder(camera->asBinder());
            return NO_ERROR;
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}
this 3 The real implementation of these functions is CameraService.cpp in .
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder()); Get this connection from client, And pass it to connect Function . preservation client The purpose of information is to be kept in CameraService::Client in , Call back at the right time client.

2.CameraService

(1)CameraService.h
class CameraService :  public BinderService<CameraService>, public BnCameraService

This BinderService<CameraService> What is it ? Look at its definition , stay frameworks/base/include/binder Next BinderService.h.

template<typename SERVICE>
class BinderService
{
public:
    static status_t publish() {
        sp<IServiceManager> sm(defaultServiceManager());
        return sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
    }

    static void publishAndJoinThreadPool() {
        sp<ProcessState> proc(ProcessState::self());
        sp<IServiceManager> sm(defaultServiceManager());
        sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
        ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();
    }

    static void instantiate() { publish(); }

    static status_t shutdown() {
        return NO_ERROR;
    }
};

return sm->addService(String16(SERVICE::getServiceName()), new SERVICE()); From this code , It can be seen that , This is a native service In the SM register .

In fact, this template class , Is to native service It provides service The unified way of registration .

publish and publishAndJoinThreadPool The difference is that , Whether to open thread pool to listen after registration .

Because I've seen it before media server Code for , stay media server Of main In the function , That's what's called CameraService.instantiate To register the , because camera service It's running media server In progress , therefore camera service You don't need to turn on the thread to loop listening .

therefore , I feel like publish and publishAndJoinThreadPool The different scenes in the movie are : If this service It's in another process , Just call publish, If it's your own process , Just call publishAndJoinThreadPool.

(2)CameraService.cpp
Realize in ICameraService Defined 3 A function .
getNumberOfCameras() and getCameraInfo() Is very simple to implement .

int32_t CameraService::getNumberOfCameras() {
    return mNumberOfCameras; //mNumberOfCameras Is initialized in the constructor ,mNumberOfCameras = HAL_getNumberOfCameras();
}

status_t CameraService::getCameraInfo(int cameraId,
                                      struct CameraInfo* cameraInfo) {
    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
        return BAD_VALUE;
    }

    HAL_getCameraInfo(cameraId, cameraInfo);
    return OK;
}

It mainly depends on connect function :

sp<ICamera> CameraService::connect( const sp<ICameraClient>& cameraClient, int cameraId) {
    sp<Client> client;  //CameraService::Client
    …………
    if (mClient[cameraId] != 0) {
        client = mClient[cameraId].promote();
        if (client != 0) {
            if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
                LOG1("CameraService::connect X (pid %d) (the same client)",
                    callingPid);
                return client;
            }
        }
    }
    …………
    sp<CameraHardwareInterface> hardware = HAL_openCameraHardware(cameraId); // obtain CameraHardwareInterface Interface , The code structure below Client When passing in
    …………
    CameraInfo info;
    HAL_getCameraInfo(cameraId, &info);
    client = new Client(this, cameraClient, hardware, cameraId, info.facing, callingPid); // This is what I said before , stay onTransact The saved client
    mClient[cameraId] = client;
    return client;
}

3.CameraService::Client

class Client : public BnCamera, Client It's right ICamera The implementation of the .
Constructors :

CameraService::Client::Client(const sp<CameraService>& cameraService,
        const sp<ICameraClient>& cameraClient,
        const sp<CameraHardwareInterface>& hardware,
        int cameraId, int cameraFacing, int clientPid) {
    int callingPid = getCallingPid();
    LOG1("Client::Client E (pid %d)", callingPid);

    mCameraService = cameraService;
    mCameraClient = cameraClient;
    mHardware = hardware;
    mCameraId = cameraId;
    mCameraFacing = cameraFacing;
    mClientPid = clientPid;
    mUseOverlay = mHardware->useOverlay();
    mMsgEnabled = 0;

    mHardware->setCallbacks(notifyCallback,
                            dataCallback,
                            dataCallbackTimestamp,
                            (void *)cameraId);

    // Enable zoom, error, and focus messages by default
    enableMsgType(CAMERA_MSG_ERROR |
                  CAMERA_MSG_ZOOM |
                  CAMERA_MSG_FOCUS);
    mOverlayW = 0;
    mOverlayH = 0;

    // Callback is disabled by default
    mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
    mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
    mOrientationChanged = false;
    cameraService->setCameraBusy(cameraId);
    cameraService->loadSound();
    LOG1("Client::Client X (pid %d)", callingPid);
}

It is mainly used to initialize some member variables , There are mainly mCameraClient, mHardware etc. . It's all in CameraService::connect It's going on .

Client You can take a look at each function of , Mainly right mHardware Call to , This is a CameraHardwareInterface Interface , That is to say HAL A package of layers . such as stopRecording function :

// stop recording mode
void CameraService::Client::stopRecording() {
    ……
    mCameraService->playSound(SOUND_RECORDING);
    disableMsgType(CAMERA_MSG_VIDEO_FRAME);
    mHardware->stopRecording();
    …………
}


4.ICamera

Finally, let's talk about ICamera.

This ICamera The definition is actually camera client And camera service The interface of .

When camera client Connect camera service when , That is to call CameraService::connect when , Return to one ICamera The interface to camera client call , This ICamera The real implementation of the interface is CameraService::Client.
 

版权声明:本文为[King of heaven and earth tiger 626]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/04/20210401091630486x.html