bool CameraClient::recordingEnabled() { LOG1("recordingEnabled (pid %d)", getCallingPid()); Mutex::Autolock lock(mLock); if (checkPidAndHardware() != NO_ERROR) return false; return mHardware->recordingEnabled(); }
// set the Surface that the preview will use status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) { LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid()); sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0); sp<ANativeWindow> window(surface); return setPreviewWindow(binder, window); }
bool CameraService::Client::previewEnabled() { LOG1("previewEnabled (pid %d)", getCallingPid()); Mutex::Autolock lock(mLock); if (checkPidAndHardware() != NO_ERROR) return false; return mHardware->previewEnabled(); }
CameraClient::CameraClient(const sp<CameraService>& cameraService, const sp<ICameraClient>& cameraClient, const String16& clientPackageName, int cameraId, int cameraFacing, int clientPid, int clientUid, int servicePid, bool legacyMode): Client(cameraService, cameraClient, clientPackageName, cameraId, cameraFacing, clientPid, clientUid, servicePid) { int callingPid = getCallingPid(); LOG1("CameraClient::CameraClient E (pid %d, id %d)", callingPid, cameraId); mHardware = NULL; mMsgEnabled = 0; mSurface = 0; mPreviewWindow = 0; mDestructionStarted = false; // Callback is disabled by default mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT); mLegacyMode = legacyMode; mPlayShutterSound = true; LOG1("CameraClient::CameraClient X (pid %d, id %d)", callingPid, cameraId); }
void CameraService::removeClient(const sp<ICameraClient>& cameraClient) { int callingPid = getCallingPid(); LOG1("CameraService::removeClient E (pid %d)", callingPid); for (int i = 0; i < mNumberOfCameras; i++) { // Declare this before the lock to make absolutely sure the // destructor won't be called with the lock held. sp<Client> client; Mutex::Autolock lock(mServiceLock); // This happens when we have already disconnected (or this is // just another unused camera). if (mClient[i] == 0) continue; // Promote mClient. It can fail if we are called from this path: // Client::~Client() -> disconnect() -> removeClient(). client = mClient[i].promote(); if (client == 0) { mClient[i].clear(); continue; } if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { // Found our camera, clear and leave. LOG1("removeClient: clear camera %d", i); mClient[i].clear(); break; } } LOG1("CameraService::removeClient X (pid %d)", callingPid); }
status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { LOG1("sendCommand (pid %d)", getCallingPid()); int orientation; Mutex::Autolock lock(mLock); status_t result = checkPidAndHardware(); if (result != NO_ERROR) return result; if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { // The orientation cannot be set during preview. if (mHardware->previewEnabled()) { return INVALID_OPERATION; } // Mirror the preview if the camera is front-facing. orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT); if (orientation == -1) return BAD_VALUE; if (mOrientation != orientation) { mOrientation = orientation; if (mOverlayRef != 0) mOrientationChanged = true; } return OK; } return mHardware->sendCommand(cmd, arg1, arg2); }
CameraService::Client::Client(const sp<CameraService>& cameraService, const sp<ICameraClient>& cameraClient, int cameraId, int cameraFacing, int clientPid, int servicePid) { #ifdef MTK_CAMERAPROFILE_SUPPORT AutoCPTLog cptlog(Event_CS_newClient); #endif int callingPid = getCallingPid(); LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId); mCameraService = cameraService; mCameraClient = cameraClient; mCameraId = cameraId; mCameraFacing = cameraFacing; mClientPid = clientPid; mServicePid = servicePid; mDestructionStarted = false; cameraService->setCameraBusy(cameraId); #ifndef MTK_CAMERA_BSP_SUPPORT // To avoid release/new MediaPlayer when switching between main/sub sensor, and it will reduce the switch time. #ifdef VENDOR_EDIT //LiChen@CameraApp, 2013/04/08, Remove for speeding up the opening camera cameraService->loadSound(); #endif /* VENDOR_EDIT */ #endif LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId); }
// take a picture - image is returned in callback status_t CameraService::Client::takePicture(int msgType) { char prop[PROPERTY_VALUE_MAX]; LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType); Mutex::Autolock lock(mLock); status_t result = checkPidAndHardware(); if (result != NO_ERROR) return result; //zxj ++ mstopPreview = true; //zxj -- if ((msgType & CAMERA_MSG_RAW_IMAGE) && (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) { LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY" " cannot be both enabled"); return BAD_VALUE; } // We only accept picture related message types // and ignore other types of messages for takePicture(). int picMsgType = msgType & (CAMERA_MSG_SHUTTER | CAMERA_MSG_POSTVIEW_FRAME | CAMERA_MSG_RAW_IMAGE | CAMERA_MSG_RAW_IMAGE_NOTIFY | CAMERA_MSG_COMPRESSED_IMAGE); disableMsgType(CAMERA_MSG_PREVIEW_METADATA); enableMsgType(picMsgType); mburstCnt = mHardware->getParameters().getInt("num-snaps-per-shutter"); if(mburstCnt <= 0) mburstCnt = 1; LOG1("mburstCnt = %d", mburstCnt); return mHardware->takePicture(); }
// take a picture - image is returned in callback status_t CameraClient::takePicture(int msgType) { LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType); Mutex::Autolock lock(mLock); status_t result = checkPidAndHardware(); if (result != NO_ERROR) return result; if ((msgType & CAMERA_MSG_RAW_IMAGE) && (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) { ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY" " cannot be both enabled"); return BAD_VALUE; } // We only accept picture related message types // and ignore other types of messages for takePicture(). int picMsgType = msgType & (CAMERA_MSG_SHUTTER | CAMERA_MSG_POSTVIEW_FRAME | CAMERA_MSG_RAW_IMAGE | CAMERA_MSG_RAW_IMAGE_NOTIFY | CAMERA_MSG_COMPRESSED_IMAGE); enableMsgType(picMsgType); return mHardware->takePicture(); }
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; mMsgEnabled = 0; mSurface = 0; mPreviewWindow = 0; mHardware->setCallbacks(notifyCallback, dataCallback, dataCallbackTimestamp, (void *)cameraId); // Enable zoom, error, focus, and metadata messages by default enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS | CAMERA_MSG_PREVIEW_METADATA); // Callback is disabled by default mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP; mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT); mPlayShutterSound = true; cameraService->setCameraBusy(cameraId); cameraService->loadSound(); LOG1("Client::Client X (pid %d)", callingPid); }
status_t CameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { // Permission checks switch (code) { //!++ #ifdef MTK_CAMERA_BSP_SUPPORT case BnCameraService::GET_PROPERTY: { CHECK_INTERFACE(ICameraService, data, reply); String8 const key = data.readString8(); String8 value; status_t const status = getProperty(key, value); reply->writeString8(value); reply->writeInt32(status); ALOGD("[GET_PROPERTY] - pid=%d, uid=%d (%s)=(%s) \n", getCallingPid(), getCallingUid(), key.string(), value.string()); return OK; } case BnCameraService::SET_PROPERTY: { CHECK_INTERFACE(ICameraService, data, reply); String8 const key = data.readString8(); String8 const value = data.readString8(); status_t const status = setProperty(key, value); reply->writeInt32(status); ALOGD("[SET_PROPERTY] - pid=%d, uid=%d (%s)=(%s) \n", getCallingPid(), getCallingUid(), key.string(), value.string()); return OK; } #endif //MTK_CAMERA_BSP_SUPPORT //!-- case BnCameraService::CONNECT: const int pid = getCallingPid(); const int self_pid = getpid(); if (pid != self_pid) { // we're called from a different process, do the real check if (!checkCallingPermission( String16("android.permission.CAMERA"))) { const int uid = getCallingUid(); ALOGE("Permission Denial: " "can't use the camera pid=%d, uid=%d", pid, uid); return PERMISSION_DENIED; } } break; } return BnCameraService::onTransact(code, data, reply, flags); }
// tear down the client CameraClient::~CameraClient() { mDestructionStarted = true; int callingPid = getCallingPid(); LOG1("CameraClient::~CameraClient E (pid %d, this %p)", callingPid, this); disconnect(); LOG1("CameraClient::~CameraClient X (pid %d, this %p)", callingPid, this); }
status_t CameraService::Client::checkPid() const { int callingPid = getCallingPid(); if (callingPid == mClientPid) return NO_ERROR; LOGW("attempt to use a locked camera from a different process" " (old pid %d, new pid %d)", mClientPid, callingPid); return EBUSY; }
// get preview/capture parameters - key/value pairs String8 CameraService::Client::getParameters() const { Mutex::Autolock lock(mLock); if (checkPidAndHardware() != NO_ERROR) return String8(); String8 params(mHardware->getParameters().flatten()); LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string()); return params; }
status_t CameraClient::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { LOG1("sendCommand (pid %d)", getCallingPid()); int orientation; Mutex::Autolock lock(mLock); status_t result = checkPidAndHardware(); if (result != NO_ERROR) return result; if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { // Mirror the preview if the camera is front-facing. orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT); if (orientation == -1) return BAD_VALUE; if (mOrientation != orientation) { mOrientation = orientation; if (mPreviewWindow != 0) { native_window_set_buffers_transform(mPreviewWindow.get(), mOrientation); } } return OK; } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) { switch (arg1) { case 0: return enableShutterSound(false); case 1: return enableShutterSound(true); default: return BAD_VALUE; } return OK; } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) { mCameraService->playSound(CameraService::SOUND_RECORDING); } else if (cmd == CAMERA_CMD_SET_VIDEO_BUFFER_COUNT) { // Silently ignore this command return INVALID_OPERATION; } else if (cmd == CAMERA_CMD_PING) { // If mHardware is 0, checkPidAndHardware will return error. return OK; #ifdef QCOM_HARDWARE } else if (cmd == CAMERA_CMD_HISTOGRAM_ON) { enableMsgType(CAMERA_MSG_STATS_DATA); } else if (cmd == CAMERA_CMD_HISTOGRAM_OFF) { disableMsgType(CAMERA_MSG_STATS_DATA); } else if (cmd == CAMERA_CMD_METADATA_ON) { enableMsgType(CAMERA_MSG_META_DATA); } else if (cmd == CAMERA_CMD_METADATA_OFF) { disableMsgType(CAMERA_MSG_META_DATA); } else if ( cmd == CAMERA_CMD_LONGSHOT_ON ) { mLongshotEnabled = true; } else if ( cmd == CAMERA_CMD_LONGSHOT_OFF ) { mLongshotEnabled = false; disableMsgType(CAMERA_MSG_SHUTTER); disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE); #endif } return mHardware->sendCommand(cmd, arg1, arg2); }
status_t CameraService::Client::cancelAutoFocus() { LOG1("cancelAutoFocus (pid %d)", getCallingPid()); Mutex::Autolock lock(mLock); status_t result = checkPidAndHardware(); if (result != NO_ERROR) return result; return mHardware->cancelAutoFocus(); }
status_t CameraService::Client::checkPidAndHardware() const { status_t result = checkPid(); if (result != NO_ERROR) return result; if (mHardware == 0) { LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid()); return INVALID_OPERATION; } return NO_ERROR; }
// start preview mode status_t CameraService::Client::startPreview() { LOG1("startPreview (pid %d)", getCallingPid()); //zxj ++ mstopPreview = false; //zxj -- if (mFaceDetection) { enableMsgType(CAMERA_MSG_PREVIEW_METADATA); } return startCameraMode(CAMERA_PREVIEW_MODE); }
// tear down the client CameraService::Client::~Client() { int callingPid = getCallingPid(); LOG1("Client::~Client E (pid %d, this %p)", callingPid, this); // set mClientPid to let disconnet() tear down the hardware mClientPid = callingPid; disconnect(); mCameraService->releaseSound(); LOG1("Client::~Client X (pid %d, this %p)", callingPid, this); }
// set preview/capture parameters - key/value pairs status_t CameraService::Client::setParameters(const String8& params) { LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string()); Mutex::Autolock lock(mLock); status_t result = checkPidAndHardware(); if (result != NO_ERROR) return result; CameraParameters p(params); return mHardware->setParameters(p); }
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; }
status_t CameraService::dump(int fd, const Vector<String16>& args) { static const char* kDeadlockedString = "CameraService may be deadlocked\n"; const size_t SIZE = 256; char buffer[SIZE]; String8 result; if (checkCallingPermission(String16("android.permission.DUMP")) == false) { snprintf(buffer, SIZE, "Permission Denial: " "can't dump CameraService from pid=%d, uid=%d\n", getCallingPid(), getCallingUid()); result.append(buffer); write(fd, result.string(), result.size()); } else { bool locked = tryLock(mServiceLock); // failed to lock - CameraService is probably deadlocked if (!locked) { String8 result(kDeadlockedString); write(fd, result.string(), result.size()); } bool hasClient = false; for (int i = 0; i < mNumberOfCameras; i++) { sp<Client> client = mClient[i].promote(); if (client == 0) continue; hasClient = true; sprintf(buffer, "Client[%d] (%p) PID: %d\n", i, client->getCameraClient()->asBinder().get(), client->mClientPid); result.append(buffer); write(fd, result.string(), result.size()); client->mHardware->dump(fd, args); } if (!hasClient) { result.append("No camera client yet.\n"); write(fd, result.string(), result.size()); } if (locked) mServiceLock.unlock(); // change logging level int n = args.size(); for (int i = 0; i + 1 < n; i++) { if (args[i] == String16("-v")) { String8 levelStr(args[i+1]); int level = atoi(levelStr.string()); sprintf(buffer, "Set Log Level to %d", level); result.append(buffer); setLogLevel(level); } } } return NO_ERROR; }
// stop recording mode void CameraService::Client::stopRecording() { LOG1("stopRecording (pid %d)", getCallingPid()); Mutex::Autolock lock(mLock); if (checkPidAndHardware() != NO_ERROR) return; mCameraService->playSound(SOUND_RECORDING); disableMsgType(CAMERA_MSG_VIDEO_FRAME); mHardware->stopRecording(); mPreviewBuffer.clear(); }
// stop preview mode void CameraService::Client::stopPreview() { LOG1("stopPreview (pid %d)", getCallingPid()); Mutex::Autolock lock(mLock); if (checkPidAndHardware() != NO_ERROR) return; disableMsgType(CAMERA_MSG_PREVIEW_FRAME); mHardware->stopPreview(); mPreviewBuffer.clear(); }
void CameraClient::disableMsgType(int32_t msgType) { android_atomic_and(~msgType, &mMsgEnabled); //!++ #if 1 if (mHardware == 0) { ALOGW("[disableMsgType] mHardware == 0 (CallingPid %d) (tid %d)", getCallingPid(), ::gettid()); return; } #endif //!-- mHardware->disableMsgType(msgType); }
// set the preview callback flag to affect how the received frames from // preview are handled. void CameraService::Client::setPreviewCallbackFlag(int callback_flag) { LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid()); Mutex::Autolock lock(mLock); if (checkPidAndHardware() != NO_ERROR) return; mPreviewCallbackFlag = callback_flag; if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) { enableMsgType(CAMERA_MSG_PREVIEW_FRAME); } else { disableMsgType(CAMERA_MSG_PREVIEW_FRAME); } }
status_t CameraClient::initialize(camera_module_t *module) { int callingPid = getCallingPid(); status_t res; LOG1("CameraClient::initialize E (pid %d, id %d)", callingPid, mCameraId); // Verify ops permissions res = startCameraOps(); if (res != OK) { return res; } char camera_device_name[10]; snprintf(camera_device_name, sizeof(camera_device_name), "%d", mCameraId); mHardware = new CameraHardwareInterface(camera_device_name); res = mHardware->initialize(&module->common); if (res != OK) { ALOGE("%s: Camera %d: unable to initialize device: %s (%d)", __FUNCTION__, mCameraId, strerror(-res), res); mHardware.clear(); return res; } // mtk callback mHardware->setMtkCallbacks( mtkMetadataCallback, (void *)(uintptr_t)mCameraId ); mHardware->setCallbacks( notifyCallback, dataCallback, dataCallbackTimestamp, (void *)(uintptr_t)mCameraId); // Enable zoom, error, focus, and metadata messages by default enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS | CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE); //!++ #if 1 // Enable MTK-extended messages by default enableMsgType(MTK_CAMERA_MSG_EXT_NOTIFY | MTK_CAMERA_MSG_EXT_DATA); #endif //!-- LOG1("CameraClient::initialize X (pid %d, id %d)", callingPid, mCameraId); return OK; }
// take a picture - image is returned in callback status_t CameraClient::takePicture(int msgType) { LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType); Mutex::Autolock lock(mLock); status_t result = checkPidAndHardware(); if (result != NO_ERROR) return result; if ((msgType & CAMERA_MSG_RAW_IMAGE) && (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) { ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY" " cannot be both enabled"); return BAD_VALUE; } // We only accept picture related message types // and ignore other types of messages for takePicture(). int picMsgType = msgType & (CAMERA_MSG_SHUTTER | CAMERA_MSG_POSTVIEW_FRAME | CAMERA_MSG_RAW_IMAGE | CAMERA_MSG_RAW_IMAGE_NOTIFY | #if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE) CAMERA_MSG_RAW_BURST | #endif CAMERA_MSG_COMPRESSED_IMAGE); #if defined(OMAP_ICS_CAMERA) || defined(OMAP_ENHANCEMENT_BURST_CAPTURE) picMsgType |= CAMERA_MSG_COMPRESSED_BURST_IMAGE; #endif #ifdef CAMERA_MSG_MGMT disableMsgType(CAMERA_MSG_PREVIEW_METADATA); #endif enableMsgType(picMsgType); #ifdef QCOM_HARDWARE mBurstCnt = mHardware->getParameters().getInt("num-snaps-per-shutter"); if(mBurstCnt <= 0) mBurstCnt = 1; LOG1("mBurstCnt = %d", mBurstCnt); // HTC HDR mode requires that we snap multiple times, but only get one jpeg int numJpegs = mHardware->getParameters().getInt("num-jpegs-per-shutter"); if (numJpegs == 1 && mBurstCnt > 1) { while (mBurstCnt > 1) { result = mHardware->takePicture(); mBurstCnt--; } } #endif return mHardware->takePicture(); }
// set the SurfaceTexture that the preview will use status_t CameraService::Client::setPreviewTexture( const sp<ISurfaceTexture>& surfaceTexture) { LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(), getCallingPid()); sp<IBinder> binder; sp<ANativeWindow> window; if (surfaceTexture != 0) { binder = surfaceTexture->asBinder(); window = new SurfaceTextureClient(surfaceTexture); } return setPreviewWindow(binder, window); }
// set the SurfaceTextureClient that the preview will use status_t CameraClient::setPreviewTexture( const sp<IGraphicBufferProducer>& bufferProducer) { LOG1("setPreviewTexture(%p) (pid %d)", bufferProducer.get(), getCallingPid()); sp<IBinder> binder; sp<ANativeWindow> window; if (bufferProducer != 0) { binder = bufferProducer->asBinder(); window = new Surface(bufferProducer); } return setPreviewWindow(binder, window); }