EmulatedCameraFactory::EmulatedCameraFactory()
        : mQemuClient(),
          mEmulatedCameras(NULL),
          mEmulatedCameraNum(0),
          mFakeCameraID(-1),
          mConstructedOK(false)

{
    /* Connect to the factory service in the emulator, and create Qemu cameras. */
    if (mQemuClient.connectClient(NULL) == NO_ERROR) {
        /* Connection has succeeded. Create emulated cameras for each camera
         * device, reported by the service. */
        createQemuCameras();
    }

    if (isFakeCameraEmulationOn()) {
        /* ID fake camera with the number of created 'qemud' cameras. */
        mFakeCameraID = mEmulatedCameraNum;
        mEmulatedCameraNum++;

        /* Make sure that array is allocated (in case there were no 'qemu'
         * cameras created. */
        if (mEmulatedCameras == NULL) {
            mEmulatedCameras = new EmulatedCamera*[mEmulatedCameraNum];
            if (mEmulatedCameras == NULL) {
                LOGE("%s: Unable to allocate emulated camera array for %d entries",
                     __FUNCTION__, mEmulatedCameraNum);
                return;
            }
            memset(mEmulatedCameras, 0, mEmulatedCameraNum * sizeof(EmulatedCamera*));
        }

        /* Create, and initialize the fake camera */
        mEmulatedCameras[mFakeCameraID] =
            new EmulatedFakeCamera(mFakeCameraID, &HAL_MODULE_INFO_SYM.common);
        if (mEmulatedCameras[mFakeCameraID] != NULL) {
            if (mEmulatedCameras[mFakeCameraID]->Initialize() != NO_ERROR) {
                delete mEmulatedCameras[mFakeCameraID];
                mEmulatedCameras--;
                mFakeCameraID = -1;
            }
        } else {
            mEmulatedCameras--;
            mFakeCameraID = -1;
            LOGE("%s: Unable to instantiate fake camera class", __FUNCTION__);
        }
    } else {
        LOGD("Fake camera emulation is disabled.");
    }

    LOGV("%d cameras are being emulated. Fake camera ID is %d",
         mEmulatedCameraNum, mFakeCameraID);

    mConstructedOK = true;
}
EmulatedCameraFactory::EmulatedCameraFactory()
        : mQemuClient(),
          mEmulatedCameras(NULL),
          mEmulatedCameraNum(0),
          mFakeCameraNum(0),
          mConstructedOK(false)
{
    status_t res;
    /* Connect to the factory service in the emulator, and create Qemu cameras. */
    if (mQemuClient.connectClient(NULL) == NO_ERROR) {
        /* Connection has succeeded. Create emulated cameras for each camera
         * device, reported by the service. */
        createQemuCameras();
    }

    if (isBackFakeCameraEmulationOn()) {
        /* Camera ID. */
        const int camera_id = mEmulatedCameraNum;
        /* Use fake camera to emulate back-facing camera. */
        mEmulatedCameraNum++;

        /* Make sure that array is allocated (in case there were no 'qemu'
         * cameras created. Note that we preallocate the array so it may contain
         * two fake cameras: one facing back, and another facing front. */
        if (mEmulatedCameras == NULL) {
            mEmulatedCameras = new EmulatedBaseCamera*[mEmulatedCameraNum + 1];
            if (mEmulatedCameras == NULL) {
                ALOGE("%s: Unable to allocate emulated camera array for %d entries",
                     __FUNCTION__, mEmulatedCameraNum);
                return;
            }
            memset(mEmulatedCameras, 0,
                    (mEmulatedCameraNum + 1) * sizeof(EmulatedBaseCamera*));
        }

        /* Create, and initialize the fake camera */
        switch (getBackCameraHalVersion()) {
            case 1:
                mEmulatedCameras[camera_id] =
                        new EmulatedFakeCamera(camera_id, true,
                                &HAL_MODULE_INFO_SYM.common);
                break;
            case 2:
                mEmulatedCameras[camera_id] =
                        new EmulatedFakeCamera2(camera_id, true,
                                &HAL_MODULE_INFO_SYM.common);
                break;
            default:
                ALOGE("%s: Unknown back camera hal version requested: %d", __FUNCTION__,
                        getBackCameraHalVersion());
        }
        if (mEmulatedCameras[camera_id] != NULL) {
            ALOGV("%s: Back camera device version is %d", __FUNCTION__,
                    getBackCameraHalVersion());
            res = mEmulatedCameras[camera_id]->Initialize();
            if (res != NO_ERROR) {
                ALOGE("%s: Unable to intialize back camera %d: %s (%d)",
                        __FUNCTION__, camera_id, strerror(-res), res);
                delete mEmulatedCameras[camera_id];
                mEmulatedCameraNum--;
            }
        } else {
            mEmulatedCameraNum--;
            ALOGE("%s: Unable to instantiate fake camera class", __FUNCTION__);
        }
    }

    if (isFrontFakeCameraEmulationOn()) {
        /* Camera ID. */
        const int camera_id = mEmulatedCameraNum;
        /* Use fake camera to emulate front-facing camera. */
        mEmulatedCameraNum++;

        /* Make sure that array is allocated (in case there were no 'qemu'
         * cameras created. */
        if (mEmulatedCameras == NULL) {
            mEmulatedCameras = new EmulatedBaseCamera*[mEmulatedCameraNum];
            if (mEmulatedCameras == NULL) {
                ALOGE("%s: Unable to allocate emulated camera array for %d entries",
                     __FUNCTION__, mEmulatedCameraNum);
                return;
            }
            memset(mEmulatedCameras, 0,
                    mEmulatedCameraNum * sizeof(EmulatedBaseCamera*));
        }

        /* Create, and initialize the fake camera */
        switch (getFrontCameraHalVersion()) {
            case 1:
                mEmulatedCameras[camera_id] =
                        new EmulatedFakeCamera(camera_id, false,
                                &HAL_MODULE_INFO_SYM.common);
                break;
            case 2:
                mEmulatedCameras[camera_id] =
                        new EmulatedFakeCamera2(camera_id, false,
                                &HAL_MODULE_INFO_SYM.common);
                break;
            default:
                ALOGE("%s: Unknown front camera hal version requested: %d",
                        __FUNCTION__,
                        getFrontCameraHalVersion());
        }
        if (mEmulatedCameras[camera_id] != NULL) {
            ALOGV("%s: Front camera device version is %d", __FUNCTION__,
                    getFrontCameraHalVersion());
            res = mEmulatedCameras[camera_id]->Initialize();
            if (res != NO_ERROR) {
                ALOGE("%s: Unable to intialize front camera %d: %s (%d)",
                        __FUNCTION__, camera_id, strerror(-res), res);
                delete mEmulatedCameras[camera_id];
                mEmulatedCameraNum--;
            }
        } else {
            mEmulatedCameraNum--;
            ALOGE("%s: Unable to instantiate fake camera class", __FUNCTION__);
        }
    }

    ALOGV("%d cameras are being emulated. %d of them are fake cameras.",
          mEmulatedCameraNum, mFakeCameraNum);

    mConstructedOK = true;
}