| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | ** | 
|  | 3 | ** Copyright (C) 2008, The Android Open Source Project | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 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 |  | 
|  | 31 | namespace android { | 
|  | 32 |  | 
|  | 33 | // client singleton for camera service binder interface | 
|  | 34 | Mutex Camera::mLock; | 
|  | 35 | sp<ICameraService> Camera::mCameraService; | 
|  | 36 | sp<Camera::DeathNotifier> Camera::mDeathNotifier; | 
|  | 37 |  | 
|  | 38 | // establish binder interface to camera service | 
|  | 39 | const 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 |  | 
|  | 64 | Camera::Camera() | 
|  | 65 | { | 
|  | 66 | init(); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | // construct a camera client from an existing camera remote | 
|  | 70 | sp<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); | 
| Wu-cheng Li | 627baac | 2011-01-04 20:00:55 +0800 | [diff] [blame] | 83 | return c; | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 84 | } | 
| Wu-cheng Li | 627baac | 2011-01-04 20:00:55 +0800 | [diff] [blame] | 85 | return 0; | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | void Camera::init() | 
|  | 89 | { | 
|  | 90 | mStatus = UNKNOWN_ERROR; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | Camera::~Camera() | 
|  | 94 | { | 
| Chih-Chung Chang | d06618e | 2010-05-13 15:14:24 +0800 | [diff] [blame] | 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. | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 103 | int32_t Camera::getNumberOfCameras() | 
|  | 104 | { | 
|  | 105 | const sp<ICameraService>& cs = getCameraService(); | 
|  | 106 | if (cs == 0) return 0; | 
|  | 107 | return cs->getNumberOfCameras(); | 
|  | 108 | } | 
|  | 109 |  | 
| Chih-Chung Chang | ddbdb35 | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 110 | status_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 |  | 
| Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 117 | sp<Camera> Camera::connect(int cameraId) | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 118 | { | 
|  | 119 | LOGV("connect"); | 
|  | 120 | sp<Camera> c = new Camera(); | 
|  | 121 | const sp<ICameraService>& cs = getCameraService(); | 
|  | 122 | if (cs != 0) { | 
| Chih-Chung Chang | 35a055b | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 123 | c->mCamera = cs->connect(c, cameraId); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 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 |  | 
|  | 134 | void Camera::disconnect() | 
|  | 135 | { | 
|  | 136 | LOGV("disconnect"); | 
|  | 137 | if (mCamera != 0) { | 
|  | 138 | mCamera->disconnect(); | 
| Chih-Chung Chang | f8ed70a | 2010-03-24 16:38:02 -0700 | [diff] [blame] | 139 | mCamera->asBinder()->unlinkToDeath(this); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 140 | mCamera = 0; | 
|  | 141 | } | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | status_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 |  | 
|  | 152 | sp<ICamera> Camera::remote() | 
|  | 153 | { | 
|  | 154 | return mCamera; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | status_t Camera::lock() | 
|  | 158 | { | 
|  | 159 | sp <ICamera> c = mCamera; | 
|  | 160 | if (c == 0) return NO_INIT; | 
|  | 161 | return c->lock(); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | status_t Camera::unlock() | 
|  | 165 | { | 
|  | 166 | sp <ICamera> c = mCamera; | 
|  | 167 | if (c == 0) return NO_INIT; | 
|  | 168 | return c->unlock(); | 
|  | 169 | } | 
|  | 170 |  | 
| Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 171 | // pass the buffered Surface to the camera service | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 172 | status_t Camera::setPreviewDisplay(const sp<Surface>& surface) | 
|  | 173 | { | 
| Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 174 | LOGV("setPreviewDisplay(%p)", surface.get()); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 175 | sp <ICamera> c = mCamera; | 
|  | 176 | if (c == 0) return NO_INIT; | 
|  | 177 | if (surface != 0) { | 
| Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 178 | return c->setPreviewDisplay(surface); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 179 | } else { | 
|  | 180 | LOGD("app passed NULL surface"); | 
|  | 181 | return c->setPreviewDisplay(0); | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 |  | 
| Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 185 | // pass the buffered ISurfaceTexture to the camera service | 
|  | 186 | status_t Camera::setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture) | 
|  | 187 | { | 
|  | 188 | LOGV("setPreviewTexture(%p)", surfaceTexture.get()); | 
|  | 189 | sp <ICamera> c = mCamera; | 
|  | 190 | if (c == 0) return NO_INIT; | 
|  | 191 | if (surfaceTexture != 0) { | 
|  | 192 | return c->setPreviewTexture(surfaceTexture); | 
|  | 193 | } else { | 
|  | 194 | LOGD("app passed NULL surface"); | 
|  | 195 | return c->setPreviewTexture(0); | 
|  | 196 | } | 
|  | 197 | } | 
|  | 198 |  | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 199 | // start preview mode | 
|  | 200 | status_t Camera::startPreview() | 
|  | 201 | { | 
|  | 202 | LOGV("startPreview"); | 
|  | 203 | sp <ICamera> c = mCamera; | 
|  | 204 | if (c == 0) return NO_INIT; | 
|  | 205 | return c->startPreview(); | 
|  | 206 | } | 
|  | 207 |  | 
| James Dong | e2ad673 | 2010-10-18 20:42:51 -0700 | [diff] [blame] | 208 | status_t Camera::storeMetaDataInBuffers(bool enabled) | 
|  | 209 | { | 
|  | 210 | LOGV("storeMetaDataInBuffers: %s", | 
|  | 211 | enabled? "true": "false"); | 
|  | 212 | sp <ICamera> c = mCamera; | 
|  | 213 | if (c == 0) return NO_INIT; | 
|  | 214 | return c->storeMetaDataInBuffers(enabled); | 
|  | 215 | } | 
|  | 216 |  | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 217 | // start recording mode, must call setPreviewDisplay first | 
|  | 218 | status_t Camera::startRecording() | 
|  | 219 | { | 
|  | 220 | LOGV("startRecording"); | 
|  | 221 | sp <ICamera> c = mCamera; | 
|  | 222 | if (c == 0) return NO_INIT; | 
|  | 223 | return c->startRecording(); | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | // stop preview mode | 
|  | 227 | void Camera::stopPreview() | 
|  | 228 | { | 
|  | 229 | LOGV("stopPreview"); | 
|  | 230 | sp <ICamera> c = mCamera; | 
|  | 231 | if (c == 0) return; | 
|  | 232 | c->stopPreview(); | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | // stop recording mode | 
|  | 236 | void Camera::stopRecording() | 
|  | 237 | { | 
|  | 238 | LOGV("stopRecording"); | 
|  | 239 | sp <ICamera> c = mCamera; | 
|  | 240 | if (c == 0) return; | 
|  | 241 | c->stopRecording(); | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | // release a recording frame | 
|  | 245 | void Camera::releaseRecordingFrame(const sp<IMemory>& mem) | 
|  | 246 | { | 
|  | 247 | LOGV("releaseRecordingFrame"); | 
|  | 248 | sp <ICamera> c = mCamera; | 
|  | 249 | if (c == 0) return; | 
|  | 250 | c->releaseRecordingFrame(mem); | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | // get preview state | 
|  | 254 | bool Camera::previewEnabled() | 
|  | 255 | { | 
|  | 256 | LOGV("previewEnabled"); | 
|  | 257 | sp <ICamera> c = mCamera; | 
|  | 258 | if (c == 0) return false; | 
|  | 259 | return c->previewEnabled(); | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | // get recording state | 
|  | 263 | bool Camera::recordingEnabled() | 
|  | 264 | { | 
|  | 265 | LOGV("recordingEnabled"); | 
|  | 266 | sp <ICamera> c = mCamera; | 
|  | 267 | if (c == 0) return false; | 
|  | 268 | return c->recordingEnabled(); | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | status_t Camera::autoFocus() | 
|  | 272 | { | 
|  | 273 | LOGV("autoFocus"); | 
|  | 274 | sp <ICamera> c = mCamera; | 
|  | 275 | if (c == 0) return NO_INIT; | 
|  | 276 | return c->autoFocus(); | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | status_t Camera::cancelAutoFocus() | 
|  | 280 | { | 
|  | 281 | LOGV("cancelAutoFocus"); | 
|  | 282 | sp <ICamera> c = mCamera; | 
|  | 283 | if (c == 0) return NO_INIT; | 
|  | 284 | return c->cancelAutoFocus(); | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | // take a picture | 
| James Dong | e468ac5 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 288 | status_t Camera::takePicture(int msgType) | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 289 | { | 
| James Dong | e468ac5 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 290 | LOGV("takePicture: 0x%x", msgType); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 291 | sp <ICamera> c = mCamera; | 
|  | 292 | if (c == 0) return NO_INIT; | 
| James Dong | e468ac5 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 293 | return c->takePicture(msgType); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 294 | } | 
|  | 295 |  | 
|  | 296 | // set preview/capture parameters - key/value pairs | 
|  | 297 | status_t Camera::setParameters(const String8& params) | 
|  | 298 | { | 
|  | 299 | LOGV("setParameters"); | 
|  | 300 | sp <ICamera> c = mCamera; | 
|  | 301 | if (c == 0) return NO_INIT; | 
|  | 302 | return c->setParameters(params); | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | // get preview/capture parameters - key/value pairs | 
|  | 306 | String8 Camera::getParameters() const | 
|  | 307 | { | 
|  | 308 | LOGV("getParameters"); | 
|  | 309 | String8 params; | 
|  | 310 | sp <ICamera> c = mCamera; | 
|  | 311 | if (c != 0) params = mCamera->getParameters(); | 
|  | 312 | return params; | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | // send command to camera driver | 
|  | 316 | status_t Camera::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) | 
|  | 317 | { | 
|  | 318 | LOGV("sendCommand"); | 
|  | 319 | sp <ICamera> c = mCamera; | 
|  | 320 | if (c == 0) return NO_INIT; | 
|  | 321 | return c->sendCommand(cmd, arg1, arg2); | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | void Camera::setListener(const sp<CameraListener>& listener) | 
|  | 325 | { | 
|  | 326 | Mutex::Autolock _l(mLock); | 
|  | 327 | mListener = listener; | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | void Camera::setPreviewCallbackFlags(int flag) | 
|  | 331 | { | 
|  | 332 | LOGV("setPreviewCallbackFlags"); | 
|  | 333 | sp <ICamera> c = mCamera; | 
|  | 334 | if (c == 0) return; | 
|  | 335 | mCamera->setPreviewCallbackFlag(flag); | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | // callback from camera service | 
|  | 339 | void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) | 
|  | 340 | { | 
|  | 341 | sp<CameraListener> listener; | 
|  | 342 | { | 
|  | 343 | Mutex::Autolock _l(mLock); | 
|  | 344 | listener = mListener; | 
|  | 345 | } | 
|  | 346 | if (listener != NULL) { | 
|  | 347 | listener->notify(msgType, ext1, ext2); | 
|  | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
|  | 351 | // callback from camera service when frame or image is ready | 
|  | 352 | void Camera::dataCallback(int32_t msgType, const sp<IMemory>& dataPtr) | 
|  | 353 | { | 
|  | 354 | sp<CameraListener> listener; | 
|  | 355 | { | 
|  | 356 | Mutex::Autolock _l(mLock); | 
|  | 357 | listener = mListener; | 
|  | 358 | } | 
|  | 359 | if (listener != NULL) { | 
|  | 360 | listener->postData(msgType, dataPtr); | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | // callback from camera service when timestamped frame is ready | 
|  | 365 | void Camera::dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) | 
|  | 366 | { | 
|  | 367 | sp<CameraListener> listener; | 
|  | 368 | { | 
|  | 369 | Mutex::Autolock _l(mLock); | 
|  | 370 | listener = mListener; | 
|  | 371 | } | 
|  | 372 | if (listener != NULL) { | 
|  | 373 | listener->postDataTimestamp(timestamp, msgType, dataPtr); | 
| James Dong | c42478e | 2010-11-15 10:38:37 -0800 | [diff] [blame] | 374 | } else { | 
|  | 375 | LOGW("No listener was set. Drop a recording frame."); | 
|  | 376 | releaseRecordingFrame(dataPtr); | 
| Mathias Agopian | 3cf6135 | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 377 | } | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | void Camera::binderDied(const wp<IBinder>& who) { | 
|  | 381 | LOGW("ICamera died"); | 
|  | 382 | notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_SERVER_DIED, 0); | 
|  | 383 | } | 
|  | 384 |  | 
|  | 385 | void Camera::DeathNotifier::binderDied(const wp<IBinder>& who) { | 
|  | 386 | LOGV("binderDied"); | 
|  | 387 | Mutex::Autolock _l(Camera::mLock); | 
|  | 388 | Camera::mCameraService.clear(); | 
|  | 389 | LOGW("Camera server died!"); | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | }; // namespace android |