sp<ICamera> CameraService::connect( const sp<ICameraClient>& cameraClient, int cameraId) { int callingPid = getCallingPid(); LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId); sp<Client> client; if (cameraId < 0 || cameraId >= mNumberOfCameras) { LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", callingPid, cameraId); return NULL; } Mutex::Autolock lock(mServiceLock); 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; } else { LOGW("CameraService::connect X (pid %d) rejected (existing client).", callingPid); return NULL; } } mClient[cameraId].clear(); } if (mBusy[cameraId]) { LOGW("CameraService::connect X (pid %d) rejected" " (camera %d is still busy).", callingPid, cameraId); return NULL; } sp<CameraHardwareInterface> hardware = HAL_openCameraHardware(cameraId); if (hardware == NULL) { LOGE("Fail to open camera hardware (id=%d)", cameraId); return NULL; } CameraInfo info; HAL_getCameraInfo(cameraId, &info); client = new Client(this, cameraClient, hardware, cameraId, info.facing, callingPid); mClient[cameraId] = client; #ifdef USE_OVERLAY_FORMAT_YCbCr_420_SP if (client->mHardware == NULL) { client = NULL; mClient[cameraId] = NULL; return client; } #endif LOG1("CameraService::connect X"); return client; }
/* HAL should return NULL if it fails to open camera hardware. */ extern "C" int camera_device_open( const struct hw_module_t* module, const char* id, struct hw_device_t** hw_device) { ALOGV("Q%s: E", __func__); int rc = -1; camera_device *device = NULL; if(module && id && hw_device) { int cameraId = atoi(id); if (!strcmp(module->name, camera_common.name)) { device = (camera_device *)malloc(sizeof (struct camera_device)); if(device) { camera_hardware_t *camHal = (camera_hardware_t *) malloc(sizeof (camera_hardware_t)); if(camHal) { memset(camHal, 0, sizeof (camera_hardware_t)); camHal->hardware = HAL_openCameraHardware(cameraId); if (camHal->hardware != NULL) { /*To Do: populate camHal*/ device->common.close = close_camera_device; device->ops = &camera_ops; device->priv = (void *)camHal; rc = 0; } else { free(camHal); free (device); device = NULL; } } else { free (device); device = NULL; } } } } *hw_device = (hw_device_t*)device; return rc; }