Camera.cpp revision 627baacc748c5e2ed68bdb256aea4d70fcfe9ce4
1/* 2** 3** Copyright (C) 2008, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18//#define LOG_NDEBUG 0 19#define LOG_TAG "Camera" 20#include <utils/Log.h> 21#include <utils/threads.h> 22 23#include <binder/IServiceManager.h> 24#include <binder/IMemory.h> 25 26#include <camera/Camera.h> 27#include <camera/ICameraService.h> 28 29#include <surfaceflinger/Surface.h> 30 31namespace android { 32 33// client singleton for camera service binder interface 34Mutex Camera::mLock; 35sp<ICameraService> Camera::mCameraService; 36sp<Camera::DeathNotifier> Camera::mDeathNotifier; 37 38// establish binder interface to camera service 39const sp<ICameraService>& Camera::getCameraService() 40{ 41 Mutex::Autolock _l(mLock); 42 if (mCameraService.get() == 0) { 43 sp<IServiceManager> sm = defaultServiceManager(); 44 sp<IBinder> binder; 45 do { 46 binder = sm->getService(String16("media.camera")); 47 if (binder != 0) 48 break; 49 LOGW("CameraService not published, waiting..."); 50 usleep(500000); // 0.5 s 51 } while(true); 52 if (mDeathNotifier == NULL) { 53 mDeathNotifier = new DeathNotifier(); 54 } 55 binder->linkToDeath(mDeathNotifier); 56 mCameraService = interface_cast<ICameraService>(binder); 57 } 58 LOGE_IF(mCameraService==0, "no CameraService!?"); 59 return mCameraService; 60} 61 62// --------------------------------------------------------------------------- 63 64Camera::Camera() 65{ 66 init(); 67} 68 69// construct a camera client from an existing camera remote 70sp<Camera> Camera::create(const sp<ICamera>& camera) 71{ 72 LOGV("create"); 73 if (camera == 0) { 74 LOGE("camera remote is a NULL pointer"); 75 return 0; 76 } 77 78 sp<Camera> c = new Camera(); 79 if (camera->connect(c) == NO_ERROR) { 80 c->mStatus = NO_ERROR; 81 c->mCamera = camera; 82 camera->asBinder()->linkToDeath(c); 83 return c; 84 } 85 return 0; 86} 87 88void Camera::init() 89{ 90 mStatus = UNKNOWN_ERROR; 91} 92 93Camera::~Camera() 94{ 95 // We don't need to call disconnect() here because if the CameraService 96 // thinks we are the owner of the hardware, it will hold a (strong) 97 // reference to us, and we can't possibly be here. We also don't want to 98 // call disconnect() here if we are in the same process as mediaserver, 99 // because we may be invoked by CameraService::Client::connect() and will 100 // deadlock if we call any method of ICamera here. 101} 102 103int32_t Camera::getNumberOfCameras() 104{ 105 const sp<ICameraService>& cs = getCameraService(); 106 if (cs == 0) return 0; 107 return cs->getNumberOfCameras(); 108} 109 110status_t Camera::getCameraInfo(int cameraId, 111 struct CameraInfo* cameraInfo) { 112 const sp<ICameraService>& cs = getCameraService(); 113 if (cs == 0) return UNKNOWN_ERROR; 114 return cs->getCameraInfo(cameraId, cameraInfo); 115} 116 117sp<Camera> Camera::connect(int cameraId) 118{ 119 LOGV("connect"); 120 sp<Camera> c = new Camera(); 121 const sp<ICameraService>& cs = getCameraService(); 122 if (cs != 0) { 123 c->mCamera = cs->connect(c, cameraId); 124 } 125 if (c->mCamera != 0) { 126 c->mCamera->asBinder()->linkToDeath(c); 127 c->mStatus = NO_ERROR; 128 } else { 129 c.clear(); 130 } 131 return c; 132} 133 134void Camera::disconnect() 135{ 136 LOGV("disconnect"); 137 if (mCamera != 0) { 138 mCamera->disconnect(); 139 mCamera->asBinder()->unlinkToDeath(this); 140 mCamera = 0; 141 } 142} 143 144status_t Camera::reconnect() 145{ 146 LOGV("reconnect"); 147 sp <ICamera> c = mCamera; 148 if (c == 0) return NO_INIT; 149 return c->connect(this); 150} 151 152sp<ICamera> Camera::remote() 153{ 154 return mCamera; 155} 156 157status_t Camera::lock() 158{ 159 sp <ICamera> c = mCamera; 160 if (c == 0) return NO_INIT; 161 return c->lock(); 162} 163 164status_t Camera::unlock() 165{ 166 sp <ICamera> c = mCamera; 167 if (c == 0) return NO_INIT; 168 return c->unlock(); 169} 170 171// pass the buffered Surface to the camera service 172status_t Camera::setPreviewDisplay(const sp<Surface>& surface) 173{ 174 LOGV("setPreviewDisplay(%p)", surface.get()); 175 sp <ICamera> c = mCamera; 176 if (c == 0) return NO_INIT; 177 if (surface != 0) { 178 return c->setPreviewDisplay(surface); 179 } else { 180 LOGD("app passed NULL surface"); 181 return c->setPreviewDisplay(0); 182 } 183} 184 185// start preview mode 186status_t Camera::startPreview() 187{ 188 LOGV("startPreview"); 189 sp <ICamera> c = mCamera; 190 if (c == 0) return NO_INIT; 191 return c->startPreview(); 192} 193 194int32_t Camera::getNumberOfVideoBuffers() const 195{ 196 LOGV("getNumberOfVideoBuffers"); 197 sp <ICamera> c = mCamera; 198 if (c == 0) return 0; 199 return c->getNumberOfVideoBuffers(); 200} 201 202sp<IMemory> Camera::getVideoBuffer(int32_t index) const 203{ 204 LOGV("getVideoBuffer: %d", index); 205 sp <ICamera> c = mCamera; 206 if (c == 0) return 0; 207 return c->getVideoBuffer(index); 208} 209 210status_t Camera::storeMetaDataInBuffers(bool enabled) 211{ 212 LOGV("storeMetaDataInBuffers: %s", 213 enabled? "true": "false"); 214 sp <ICamera> c = mCamera; 215 if (c == 0) return NO_INIT; 216 return c->storeMetaDataInBuffers(enabled); 217} 218 219// start recording mode, must call setPreviewDisplay first 220status_t Camera::startRecording() 221{ 222 LOGV("startRecording"); 223 sp <ICamera> c = mCamera; 224 if (c == 0) return NO_INIT; 225 return c->startRecording(); 226} 227 228// stop preview mode 229void Camera::stopPreview() 230{ 231 LOGV("stopPreview"); 232 sp <ICamera> c = mCamera; 233 if (c == 0) return; 234 c->stopPreview(); 235} 236 237// stop recording mode 238void Camera::stopRecording() 239{ 240 LOGV("stopRecording"); 241 sp <ICamera> c = mCamera; 242 if (c == 0) return; 243 c->stopRecording(); 244} 245 246// release a recording frame 247void Camera::releaseRecordingFrame(const sp<IMemory>& mem) 248{ 249 LOGV("releaseRecordingFrame"); 250 sp <ICamera> c = mCamera; 251 if (c == 0) return; 252 c->releaseRecordingFrame(mem); 253} 254 255// get preview state 256bool Camera::previewEnabled() 257{ 258 LOGV("previewEnabled"); 259 sp <ICamera> c = mCamera; 260 if (c == 0) return false; 261 return c->previewEnabled(); 262} 263 264// get recording state 265bool Camera::recordingEnabled() 266{ 267 LOGV("recordingEnabled"); 268 sp <ICamera> c = mCamera; 269 if (c == 0) return false; 270 return c->recordingEnabled(); 271} 272 273status_t Camera::autoFocus() 274{ 275 LOGV("autoFocus"); 276 sp <ICamera> c = mCamera; 277 if (c == 0) return NO_INIT; 278 return c->autoFocus(); 279} 280 281status_t Camera::cancelAutoFocus() 282{ 283 LOGV("cancelAutoFocus"); 284 sp <ICamera> c = mCamera; 285 if (c == 0) return NO_INIT; 286 return c->cancelAutoFocus(); 287} 288 289// take a picture 290status_t Camera::takePicture() 291{ 292 LOGV("takePicture"); 293 sp <ICamera> c = mCamera; 294 if (c == 0) return NO_INIT; 295 return c->takePicture(); 296} 297 298// set preview/capture parameters - key/value pairs 299status_t Camera::setParameters(const String8& params) 300{ 301 LOGV("setParameters"); 302 sp <ICamera> c = mCamera; 303 if (c == 0) return NO_INIT; 304 return c->setParameters(params); 305} 306 307// get preview/capture parameters - key/value pairs 308String8 Camera::getParameters() const 309{ 310 LOGV("getParameters"); 311 String8 params; 312 sp <ICamera> c = mCamera; 313 if (c != 0) params = mCamera->getParameters(); 314 return params; 315} 316 317// send command to camera driver 318status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) 319{ 320 LOGV("sendCommand"); 321 sp <ICamera> c = mCamera; 322 if (c == 0) return NO_INIT; 323 return c->sendCommand(cmd, arg1, arg2); 324} 325 326void Camera::setListener(const sp<CameraListener>& listener) 327{ 328 Mutex::Autolock _l(mLock); 329 mListener = listener; 330} 331 332void Camera::setPreviewCallbackFlags(int flag) 333{ 334 LOGV("setPreviewCallbackFlags"); 335 sp <ICamera> c = mCamera; 336 if (c == 0) return; 337 mCamera->setPreviewCallbackFlag(flag); 338} 339 340// callback from camera service 341void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) 342{ 343 sp<CameraListener> listener; 344 { 345 Mutex::Autolock _l(mLock); 346 listener = mListener; 347 } 348 if (listener != NULL) { 349 listener->notify(msgType, ext1, ext2); 350 } 351} 352 353// callback from camera service when frame or image is ready 354void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr) 355{ 356 sp<CameraListener> listener; 357 { 358 Mutex::Autolock _l(mLock); 359 listener = mListener; 360 } 361 if (listener != NULL) { 362 listener->postData(msgType, dataPtr); 363 } 364} 365 366// callback from camera service when timestamped frame is ready 367void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) 368{ 369 sp<CameraListener> listener; 370 { 371 Mutex::Autolock _l(mLock); 372 listener = mListener; 373 } 374 if (listener != NULL) { 375 listener->postDataTimestamp(timestamp, msgType, dataPtr); 376 } else { 377 LOGW("No listener was set. Drop a recording frame."); 378 releaseRecordingFrame(dataPtr); 379 } 380} 381 382void Camera::binderDied(const wp<IBinder>& who) { 383 LOGW("ICamera died"); 384 notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, 0); 385} 386 387void Camera::DeathNotifier::binderDied(const wp<IBinder>& who) { 388 LOGV("binderDied"); 389 Mutex::Autolock _l(Camera::mLock); 390 Camera::mCameraService.clear(); 391 LOGW("Camera server died!"); 392} 393 394}; // namespace android 395