Android2.2平台上支持多camera(三)

时间:2023-02-07 09:21:02

5ICameraService.h文件

Framework/base/include/camera/ICameraService.hCameraService对外的接口文件。

首先增加了三个宏定义,如下:

class ICameraService : public IInterface
{
public:
enum {
GET_NUMBER_OF_CAMERAS = IBinder::FIRST_CALL_TRANSACTION,
GET_CAMERA_INFO,
CONNECT,
SET_CAMERA_ID
};

GET_NUMBER_OF_CAMERASGET_CAMERA_INFOSET_CAMERA_ID对应于那三个函数接口。

接下来,定义了三个纯虚函数,CODE如下:
public:
DECLARE_META_INTERFACE(CameraService);
virtual int32_t getNumberOfCameras() = 0;
virtual status_t getCameraInfo(int cameraId,
struct CameraInfo* cameraInfo) = 0;
virtual int32_t setCameraId(int cameraId) = 0;
virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient) = 0;
};

6ICameraService.cpp文件

Framework/base/libs/camera/ICameraService.cpp是对ICameraService.h文件的实现。

它对下面三个函数进行了定义:

// get number of cameras available

    virtual int32_t getNumberOfCameras()

    {

        Parcel data, reply;

        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());

        remote()->transact(BnCameraService::GET_NUMBER_OF_CAMERAS, data, &reply);

        return reply.readInt32();

    }

 

    // get information about a camera

    virtual status_t getCameraInfo(int cameraId,

                                   struct CameraInfo* cameraInfo) {

        Parcel data, reply;

        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());

        data.writeInt32(cameraId);

        remote()->transact(BnCameraService::GET_CAMERA_INFO, data, &reply);

        cameraInfo->facing = reply.readInt32();

        cameraInfo->orientation = reply.readInt32();

        return reply.readInt32();

    }

    virtual int32_t setCameraId(int cameraId)

    {

        Parcel data, reply;

        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());

    data.writeInt32(cameraId);

        remote()->transact(BnCameraService::SET_CAMERA_ID, data, &reply);

        return reply.readInt32();

    }

 

然后又在函数BnCameraService::onTransact()中做了修改:

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 SET_CAMERA_ID: {

            CHECK_INTERFACE(ICameraService, data, reply);

            reply->writeInt32(setCameraId(data.readInt32()));

            return NO_ERROR;

        } break;    

        case CONNECT: {

            CHECK_INTERFACE(ICameraService, data, reply);

            sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());

            sp<ICamera> camera = connect(cameraClient);

            reply->writeStrongBinder(camera->asBinder());

            return NO_ERROR;

        } break;

        default:

            return BBinder::onTransact(code, data, reply, flags);

    }

}

这里通过BinderCameraService.cpp进行通信。下面介绍CameraService相关的文件。

7CameraService.h文件

Frameworks/base/camera/libcameraservice/CameraService.h中定义了如下三个接口函数:

 

    virtual int32_t     getNumberOfCameras();

    virtual status_t    getCameraInfo(int cameraId,

                                      struct CameraInfo* cameraInfo);

    virtual int32_t     setCameraId(int cameraId);  

其具体实现在CameraService.cpp中。

8CameraService.cpp

frameworks/base/camera/libcameraservice/CameraService.cpp调用Camera HAL层的接口,对CameraService.h中的三个新增接口做了实现。其CODE如下:

int32_t CameraService::getNumberOfCameras() {

    return HAL_getNumberOfCameras();

}

 

status_t CameraService::getCameraInfo(int cameraId,

                                      struct CameraInfo* cameraInfo) {

    int mNumberOfCameras = HAL_getNumberOfCameras();                          

    if (cameraId < 0 || cameraId >= mNumberOfCameras) {

        return BAD_VALUE;

    }

 

    HAL_getCameraInfo(cameraId, cameraInfo);

    return OK;

}

int32_t CameraService::setCameraId(int cameraId) {

    return HAL_setCameraId(cameraId);

}

下面我们将介绍Camera HAL层对多Camera的支持。(待续)