Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright (C) 2008, The Android Open Source Project |
| 4 | ** Copyright (C) 2008 HTC Inc. |
| 5 | ** |
| 6 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | ** you may not use this file except in compliance with the License. |
| 8 | ** You may obtain a copy of the License at |
| 9 | ** |
| 10 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | ** |
| 12 | ** Unless required by applicable law or agreed to in writing, software |
| 13 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | ** See the License for the specific language governing permissions and |
| 16 | ** limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #define LOG_TAG "CameraService" |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <pthread.h> |
| 24 | |
| 25 | #include <binder/IPCThreadState.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/MemoryBase.h> |
| 28 | #include <binder/MemoryHeapBase.h> |
| 29 | #include <cutils/atomic.h> |
| 30 | #include <hardware/hardware.h> |
| 31 | #include <media/AudioSystem.h> |
| 32 | #include <media/mediaplayer.h> |
| 33 | #include <surfaceflinger/ISurface.h> |
| 34 | #include <ui/Overlay.h> |
| 35 | #include <utils/Errors.h> |
| 36 | #include <utils/Log.h> |
| 37 | #include <utils/String16.h> |
| 38 | |
| 39 | #include "CameraService.h" |
| 40 | |
| 41 | namespace android { |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | // Logging support -- this is for debugging only |
| 45 | // Use "adb shell dumpsys media.camera -v 1" to change it. |
| 46 | static volatile int32_t gLogLevel = 0; |
| 47 | |
| 48 | #define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__); |
| 49 | #define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__); |
| 50 | |
| 51 | static void setLogLevel(int level) { |
| 52 | android_atomic_write(level, &gLogLevel); |
| 53 | } |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
| 57 | static int getCallingPid() { |
| 58 | return IPCThreadState::self()->getCallingPid(); |
| 59 | } |
| 60 | |
| 61 | static int getCallingUid() { |
| 62 | return IPCThreadState::self()->getCallingUid(); |
| 63 | } |
| 64 | |
| 65 | // ---------------------------------------------------------------------------- |
| 66 | |
| 67 | // This is ugly and only safe if we never re-create the CameraService, but |
| 68 | // should be ok for now. |
| 69 | static CameraService *gCameraService; |
| 70 | |
| 71 | CameraService::CameraService() |
| 72 | :mSoundRef(0) |
| 73 | { |
| 74 | LOGI("CameraService started (pid=%d)", getpid()); |
| 75 | |
| 76 | mNumberOfCameras = HAL_getNumberOfCameras(); |
| 77 | if (mNumberOfCameras > MAX_CAMERAS) { |
| 78 | LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).", |
| 79 | mNumberOfCameras, MAX_CAMERAS); |
| 80 | mNumberOfCameras = MAX_CAMERAS; |
| 81 | } |
| 82 | |
| 83 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 84 | setCameraFree(i); |
| 85 | } |
| 86 | |
| 87 | gCameraService = this; |
| 88 | } |
| 89 | |
| 90 | CameraService::~CameraService() { |
| 91 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 92 | if (mBusy[i]) { |
| 93 | LOGE("camera %d is still in use in destructor!", i); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | gCameraService = NULL; |
| 98 | } |
| 99 | |
| 100 | int32_t CameraService::getNumberOfCameras() { |
| 101 | return mNumberOfCameras; |
| 102 | } |
| 103 | |
| 104 | status_t CameraService::getCameraInfo(int cameraId, |
| 105 | struct CameraInfo* cameraInfo) { |
| 106 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 107 | return BAD_VALUE; |
| 108 | } |
| 109 | |
| 110 | HAL_getCameraInfo(cameraId, cameraInfo); |
| 111 | return OK; |
| 112 | } |
| 113 | |
| 114 | sp<ICamera> CameraService::connect( |
| 115 | const sp<ICameraClient>& cameraClient, int cameraId) { |
| 116 | int callingPid = getCallingPid(); |
| 117 | LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId); |
| 118 | |
| 119 | sp<Client> client; |
| 120 | if (cameraId < 0 || cameraId >= mNumberOfCameras) { |
| 121 | LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).", |
| 122 | callingPid, cameraId); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | Mutex::Autolock lock(mServiceLock); |
| 127 | if (mClient[cameraId] != 0) { |
| 128 | client = mClient[cameraId].promote(); |
| 129 | if (client != 0) { |
| 130 | if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { |
| 131 | LOG1("CameraService::connect X (pid %d) (the same client)", |
| 132 | callingPid); |
| 133 | return client; |
| 134 | } else { |
| 135 | LOGW("CameraService::connect X (pid %d) rejected (existing client).", |
| 136 | callingPid); |
| 137 | return NULL; |
| 138 | } |
| 139 | } |
| 140 | mClient[cameraId].clear(); |
| 141 | } |
| 142 | |
| 143 | if (mBusy[cameraId]) { |
| 144 | LOGW("CameraService::connect X (pid %d) rejected" |
| 145 | " (camera %d is still busy).", callingPid, cameraId); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | client = new Client(this, cameraClient, cameraId, callingPid); |
| 150 | mClient[cameraId] = client; |
| 151 | LOG1("CameraService::connect X"); |
| 152 | return client; |
| 153 | } |
| 154 | |
| 155 | void CameraService::removeClient(const sp<ICameraClient>& cameraClient) { |
| 156 | int callingPid = getCallingPid(); |
| 157 | LOG1("CameraService::removeClient E (pid %d)", callingPid); |
| 158 | |
| 159 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 160 | // Declare this before the lock to make absolutely sure the |
| 161 | // destructor won't be called with the lock held. |
| 162 | sp<Client> client; |
| 163 | |
| 164 | Mutex::Autolock lock(mServiceLock); |
| 165 | |
| 166 | // This happens when we have already disconnected (or this is |
| 167 | // just another unused camera). |
| 168 | if (mClient[i] == 0) continue; |
| 169 | |
| 170 | // Promote mClient. It can fail if we are called from this path: |
| 171 | // Client::~Client() -> disconnect() -> removeClient(). |
| 172 | client = mClient[i].promote(); |
| 173 | |
| 174 | if (client == 0) { |
| 175 | mClient[i].clear(); |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) { |
| 180 | // Found our camera, clear and leave. |
| 181 | LOG1("removeClient: clear camera %d", i); |
| 182 | mClient[i].clear(); |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | LOG1("CameraService::removeClient X (pid %d)", callingPid); |
| 188 | } |
| 189 | |
| 190 | sp<CameraService::Client> CameraService::getClientById(int cameraId) { |
| 191 | if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL; |
| 192 | return mClient[cameraId].promote(); |
| 193 | } |
| 194 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 195 | status_t CameraService::onTransact( |
| 196 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 197 | // Permission checks |
| 198 | switch (code) { |
| 199 | case BnCameraService::CONNECT: |
| 200 | const int pid = getCallingPid(); |
| 201 | const int self_pid = getpid(); |
| 202 | if (pid != self_pid) { |
| 203 | // we're called from a different process, do the real check |
| 204 | if (!checkCallingPermission( |
| 205 | String16("android.permission.CAMERA"))) { |
| 206 | const int uid = getCallingUid(); |
| 207 | LOGE("Permission Denial: " |
| 208 | "can't use the camera pid=%d, uid=%d", pid, uid); |
| 209 | return PERMISSION_DENIED; |
| 210 | } |
| 211 | } |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | return BnCameraService::onTransact(code, data, reply, flags); |
| 216 | } |
| 217 | |
| 218 | // The reason we need this busy bit is a new CameraService::connect() request |
| 219 | // may come in while the previous Client's destructor has not been run or is |
| 220 | // still running. If the last strong reference of the previous Client is gone |
| 221 | // but the destructor has not been finished, we should not allow the new Client |
| 222 | // to be created because we need to wait for the previous Client to tear down |
| 223 | // the hardware first. |
| 224 | void CameraService::setCameraBusy(int cameraId) { |
| 225 | android_atomic_write(1, &mBusy[cameraId]); |
| 226 | } |
| 227 | |
| 228 | void CameraService::setCameraFree(int cameraId) { |
| 229 | android_atomic_write(0, &mBusy[cameraId]); |
| 230 | } |
| 231 | |
| 232 | // We share the media players for shutter and recording sound for all clients. |
| 233 | // A reference count is kept to determine when we will actually release the |
| 234 | // media players. |
| 235 | |
| 236 | static MediaPlayer* newMediaPlayer(const char *file) { |
| 237 | MediaPlayer* mp = new MediaPlayer(); |
| 238 | if (mp->setDataSource(file, NULL) == NO_ERROR) { |
| 239 | mp->setAudioStreamType(AudioSystem::ENFORCED_AUDIBLE); |
| 240 | mp->prepare(); |
| 241 | } else { |
| 242 | LOGE("Failed to load CameraService sounds: %s", file); |
| 243 | return NULL; |
| 244 | } |
| 245 | return mp; |
| 246 | } |
| 247 | |
| 248 | void CameraService::loadSound() { |
| 249 | Mutex::Autolock lock(mSoundLock); |
| 250 | LOG1("CameraService::loadSound ref=%d", mSoundRef); |
| 251 | if (mSoundRef++) return; |
| 252 | |
| 253 | mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg"); |
| 254 | mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg"); |
| 255 | } |
| 256 | |
| 257 | void CameraService::releaseSound() { |
| 258 | Mutex::Autolock lock(mSoundLock); |
| 259 | LOG1("CameraService::releaseSound ref=%d", mSoundRef); |
| 260 | if (--mSoundRef) return; |
| 261 | |
| 262 | for (int i = 0; i < NUM_SOUNDS; i++) { |
| 263 | if (mSoundPlayer[i] != 0) { |
| 264 | mSoundPlayer[i]->disconnect(); |
| 265 | mSoundPlayer[i].clear(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void CameraService::playSound(sound_kind kind) { |
| 271 | LOG1("playSound(%d)", kind); |
| 272 | Mutex::Autolock lock(mSoundLock); |
| 273 | sp<MediaPlayer> player = mSoundPlayer[kind]; |
| 274 | if (player != 0) { |
| 275 | // do not play the sound if stream volume is 0 |
| 276 | // (typically because ringer mode is silent). |
| 277 | int index; |
| 278 | AudioSystem::getStreamVolumeIndex(AudioSystem::ENFORCED_AUDIBLE, &index); |
| 279 | if (index != 0) { |
| 280 | player->seekTo(0); |
| 281 | player->start(); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // ---------------------------------------------------------------------------- |
| 287 | |
| 288 | CameraService::Client::Client(const sp<CameraService>& cameraService, |
| 289 | const sp<ICameraClient>& cameraClient, int cameraId, int clientPid) { |
| 290 | int callingPid = getCallingPid(); |
| 291 | LOG1("Client::Client E (pid %d)", callingPid); |
| 292 | |
| 293 | mCameraService = cameraService; |
| 294 | mCameraClient = cameraClient; |
| 295 | mCameraId = cameraId; |
| 296 | mClientPid = clientPid; |
| 297 | |
| 298 | mHardware = HAL_openCameraHardware(cameraId); |
| 299 | mUseOverlay = mHardware->useOverlay(); |
| 300 | mMsgEnabled = 0; |
| 301 | |
| 302 | mHardware->setCallbacks(notifyCallback, |
| 303 | dataCallback, |
| 304 | dataCallbackTimestamp, |
| 305 | (void *)cameraId); |
| 306 | |
| 307 | // Enable zoom, error, and focus messages by default |
| 308 | enableMsgType(CAMERA_MSG_ERROR | |
| 309 | CAMERA_MSG_ZOOM | |
| 310 | CAMERA_MSG_FOCUS); |
| 311 | mOverlayW = 0; |
| 312 | mOverlayH = 0; |
| 313 | |
| 314 | // Callback is disabled by default |
| 315 | mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP; |
| 316 | mOrientation = 0; |
| 317 | cameraService->setCameraBusy(cameraId); |
| 318 | cameraService->loadSound(); |
| 319 | LOG1("Client::Client X (pid %d)", callingPid); |
| 320 | } |
| 321 | |
| 322 | static void *unregister_surface(void *arg) { |
| 323 | ISurface *surface = (ISurface *)arg; |
| 324 | surface->unregisterBuffers(); |
| 325 | IPCThreadState::self()->flushCommands(); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
| 329 | // tear down the client |
| 330 | CameraService::Client::~Client() { |
| 331 | int callingPid = getCallingPid(); |
| 332 | LOG1("Client::~Client E (pid %d, this %p)", callingPid, this); |
| 333 | |
| 334 | if (mSurface != 0 && !mUseOverlay) { |
| 335 | pthread_t thr; |
| 336 | // We unregister the buffers in a different thread because binder does |
| 337 | // not let us make sychronous transactions in a binder destructor (that |
| 338 | // is, upon our reaching a refcount of zero.) |
| 339 | pthread_create(&thr, |
| 340 | NULL, // attr |
| 341 | unregister_surface, |
| 342 | mSurface.get()); |
| 343 | pthread_join(thr, NULL); |
| 344 | } |
| 345 | |
| 346 | // set mClientPid to let disconnet() tear down the hardware |
| 347 | mClientPid = callingPid; |
| 348 | disconnect(); |
| 349 | mCameraService->releaseSound(); |
| 350 | LOG1("Client::~Client X (pid %d, this %p)", callingPid, this); |
| 351 | } |
| 352 | |
| 353 | // ---------------------------------------------------------------------------- |
| 354 | |
| 355 | status_t CameraService::Client::checkPid() const { |
| 356 | int callingPid = getCallingPid(); |
| 357 | if (callingPid == mClientPid) return NO_ERROR; |
| 358 | |
| 359 | LOGW("attempt to use a locked camera from a different process" |
| 360 | " (old pid %d, new pid %d)", mClientPid, callingPid); |
| 361 | return EBUSY; |
| 362 | } |
| 363 | |
| 364 | status_t CameraService::Client::checkPidAndHardware() const { |
| 365 | status_t result = checkPid(); |
| 366 | if (result != NO_ERROR) return result; |
| 367 | if (mHardware == 0) { |
| 368 | LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid()); |
| 369 | return INVALID_OPERATION; |
| 370 | } |
| 371 | return NO_ERROR; |
| 372 | } |
| 373 | |
| 374 | status_t CameraService::Client::lock() { |
| 375 | int callingPid = getCallingPid(); |
| 376 | LOG1("lock (pid %d)", callingPid); |
| 377 | Mutex::Autolock lock(mLock); |
| 378 | |
| 379 | // lock camera to this client if the the camera is unlocked |
| 380 | if (mClientPid == 0) { |
| 381 | mClientPid = callingPid; |
| 382 | return NO_ERROR; |
| 383 | } |
| 384 | |
| 385 | // returns NO_ERROR if the client already owns the camera, EBUSY otherwise |
| 386 | return checkPid(); |
| 387 | } |
| 388 | |
| 389 | status_t CameraService::Client::unlock() { |
| 390 | int callingPid = getCallingPid(); |
| 391 | LOG1("unlock (pid %d)", callingPid); |
| 392 | Mutex::Autolock lock(mLock); |
| 393 | |
| 394 | // allow anyone to use camera (after they lock the camera) |
| 395 | status_t result = checkPid(); |
| 396 | if (result == NO_ERROR) { |
| 397 | mClientPid = 0; |
| 398 | LOG1("clear mCameraClient (pid %d)", callingPid); |
| 399 | // we need to remove the reference to ICameraClient so that when the app |
| 400 | // goes away, the reference count goes to 0. |
| 401 | mCameraClient.clear(); |
| 402 | } |
| 403 | return result; |
| 404 | } |
| 405 | |
| 406 | // connect a new client to the camera |
| 407 | status_t CameraService::Client::connect(const sp<ICameraClient>& client) { |
| 408 | int callingPid = getCallingPid(); |
| 409 | LOG1("connect E (pid %d)", callingPid); |
| 410 | Mutex::Autolock lock(mLock); |
| 411 | |
| 412 | if (mClientPid != 0 && checkPid() != NO_ERROR) { |
| 413 | LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)", |
| 414 | mClientPid, callingPid); |
| 415 | return EBUSY; |
| 416 | } |
| 417 | |
| 418 | if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) { |
| 419 | LOG1("Connect to the same client"); |
| 420 | return NO_ERROR; |
| 421 | } |
| 422 | |
| 423 | mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP; |
| 424 | mClientPid = callingPid; |
| 425 | mCameraClient = client; |
| 426 | |
| 427 | LOG1("connect X (pid %d)", callingPid); |
| 428 | return NO_ERROR; |
| 429 | } |
| 430 | |
| 431 | void CameraService::Client::disconnect() { |
| 432 | int callingPid = getCallingPid(); |
| 433 | LOG1("disconnect E (pid %d)", callingPid); |
| 434 | Mutex::Autolock lock(mLock); |
| 435 | |
| 436 | if (checkPid() != NO_ERROR) { |
| 437 | LOGW("different client - don't disconnect"); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | if (mClientPid <= 0) { |
| 442 | LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | // Make sure disconnect() is done once and once only, whether it is called |
| 447 | // from the user directly, or called by the destructor. |
| 448 | if (mHardware == 0) return; |
| 449 | |
| 450 | LOG1("hardware teardown"); |
| 451 | // Before destroying mHardware, we must make sure it's in the |
| 452 | // idle state. |
| 453 | // Turn off all messages. |
| 454 | disableMsgType(CAMERA_MSG_ALL_MSGS); |
| 455 | mHardware->stopPreview(); |
| 456 | mHardware->cancelPicture(); |
| 457 | // Release the hardware resources. |
| 458 | mHardware->release(); |
| 459 | // Release the held overlay resources. |
| 460 | if (mUseOverlay) { |
| 461 | mOverlayRef = 0; |
| 462 | } |
| 463 | mHardware.clear(); |
| 464 | |
| 465 | mCameraService->removeClient(mCameraClient); |
| 466 | mCameraService->setCameraFree(mCameraId); |
| 467 | |
| 468 | LOG1("disconnect X (pid %d)", callingPid); |
| 469 | } |
| 470 | |
| 471 | // ---------------------------------------------------------------------------- |
| 472 | |
| 473 | // set the ISurface that the preview will use |
| 474 | status_t CameraService::Client::setPreviewDisplay(const sp<ISurface>& surface) { |
| 475 | LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid()); |
| 476 | Mutex::Autolock lock(mLock); |
| 477 | status_t result = checkPidAndHardware(); |
| 478 | if (result != NO_ERROR) return result; |
| 479 | |
| 480 | result = NO_ERROR; |
| 481 | |
| 482 | // return if no change in surface. |
| 483 | // asBinder() is safe on NULL (returns NULL) |
| 484 | if (surface->asBinder() == mSurface->asBinder()) { |
| 485 | return result; |
| 486 | } |
| 487 | |
| 488 | if (mSurface != 0) { |
| 489 | LOG1("clearing old preview surface %p", mSurface.get()); |
| 490 | if (mUseOverlay) { |
| 491 | // Force the destruction of any previous overlay |
| 492 | sp<Overlay> dummy; |
| 493 | mHardware->setOverlay(dummy); |
| 494 | } else { |
| 495 | mSurface->unregisterBuffers(); |
| 496 | } |
| 497 | } |
| 498 | mSurface = surface; |
| 499 | mOverlayRef = 0; |
| 500 | // If preview has been already started, set overlay or register preview |
| 501 | // buffers now. |
| 502 | if (mHardware->previewEnabled()) { |
| 503 | if (mUseOverlay) { |
| 504 | result = setOverlay(); |
| 505 | } else if (mSurface != 0) { |
| 506 | result = registerPreviewBuffers(); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return result; |
| 511 | } |
| 512 | |
| 513 | status_t CameraService::Client::registerPreviewBuffers() { |
| 514 | int w, h; |
| 515 | CameraParameters params(mHardware->getParameters()); |
| 516 | params.getPreviewSize(&w, &h); |
| 517 | |
| 518 | // FIXME: don't use a hardcoded format here. |
| 519 | ISurface::BufferHeap buffers(w, h, w, h, |
| 520 | HAL_PIXEL_FORMAT_YCrCb_420_SP, |
| 521 | mOrientation, |
| 522 | 0, |
| 523 | mHardware->getPreviewHeap()); |
| 524 | |
| 525 | status_t result = mSurface->registerBuffers(buffers); |
| 526 | if (result != NO_ERROR) { |
| 527 | LOGE("registerBuffers failed with status %d", result); |
| 528 | } |
| 529 | return result; |
| 530 | } |
| 531 | |
| 532 | status_t CameraService::Client::setOverlay() { |
| 533 | int w, h; |
| 534 | CameraParameters params(mHardware->getParameters()); |
| 535 | params.getPreviewSize(&w, &h); |
| 536 | |
| 537 | if (w != mOverlayW || h != mOverlayH) { |
| 538 | // Force the destruction of any previous overlay |
| 539 | sp<Overlay> dummy; |
| 540 | mHardware->setOverlay(dummy); |
| 541 | mOverlayRef = 0; |
| 542 | } |
| 543 | |
| 544 | status_t result = NO_ERROR; |
| 545 | if (mSurface == 0) { |
| 546 | result = mHardware->setOverlay(NULL); |
| 547 | } else { |
| 548 | if (mOverlayRef == 0) { |
| 549 | // FIXME: |
| 550 | // Surfaceflinger may hold onto the previous overlay reference for some |
| 551 | // time after we try to destroy it. retry a few times. In the future, we |
| 552 | // should make the destroy call block, or possibly specify that we can |
| 553 | // wait in the createOverlay call if the previous overlay is in the |
| 554 | // process of being destroyed. |
| 555 | for (int retry = 0; retry < 50; ++retry) { |
| 556 | mOverlayRef = mSurface->createOverlay(w, h, OVERLAY_FORMAT_DEFAULT, |
| 557 | mOrientation); |
| 558 | if (mOverlayRef != 0) break; |
| 559 | LOGW("Overlay create failed - retrying"); |
| 560 | usleep(20000); |
| 561 | } |
| 562 | if (mOverlayRef == 0) { |
| 563 | LOGE("Overlay Creation Failed!"); |
| 564 | return -EINVAL; |
| 565 | } |
| 566 | result = mHardware->setOverlay(new Overlay(mOverlayRef)); |
| 567 | } |
| 568 | } |
| 569 | if (result != NO_ERROR) { |
| 570 | LOGE("mHardware->setOverlay() failed with status %d\n", result); |
| 571 | return result; |
| 572 | } |
| 573 | |
| 574 | mOverlayW = w; |
| 575 | mOverlayH = h; |
| 576 | |
| 577 | return result; |
| 578 | } |
| 579 | |
| 580 | // set the preview callback flag to affect how the received frames from |
| 581 | // preview are handled. |
| 582 | void CameraService::Client::setPreviewCallbackFlag(int callback_flag) { |
| 583 | LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid()); |
| 584 | Mutex::Autolock lock(mLock); |
| 585 | if (checkPidAndHardware() != NO_ERROR) return; |
| 586 | |
| 587 | mPreviewCallbackFlag = callback_flag; |
| 588 | |
| 589 | // If we don't use overlay, we always need the preview frame for display. |
| 590 | // If we do use overlay, we only need the preview frame if the user |
| 591 | // wants the data. |
| 592 | if (mUseOverlay) { |
| 593 | if(mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ENABLE_MASK) { |
| 594 | enableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 595 | } else { |
| 596 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | // start preview mode |
| 602 | status_t CameraService::Client::startPreview() { |
| 603 | LOG1("startPreview (pid %d)", getCallingPid()); |
| 604 | return startCameraMode(CAMERA_PREVIEW_MODE); |
| 605 | } |
| 606 | |
| 607 | // start recording mode |
| 608 | status_t CameraService::Client::startRecording() { |
| 609 | LOG1("startRecording (pid %d)", getCallingPid()); |
| 610 | return startCameraMode(CAMERA_RECORDING_MODE); |
| 611 | } |
| 612 | |
| 613 | // start preview or recording |
| 614 | status_t CameraService::Client::startCameraMode(camera_mode mode) { |
| 615 | LOG1("startCameraMode(%d)", mode); |
| 616 | Mutex::Autolock lock(mLock); |
| 617 | status_t result = checkPidAndHardware(); |
| 618 | if (result != NO_ERROR) return result; |
| 619 | |
| 620 | switch(mode) { |
| 621 | case CAMERA_PREVIEW_MODE: |
| 622 | if (mSurface == 0) { |
| 623 | LOG1("mSurface is not set yet."); |
| 624 | // still able to start preview in this case. |
| 625 | } |
| 626 | return startPreviewMode(); |
| 627 | case CAMERA_RECORDING_MODE: |
| 628 | if (mSurface == 0) { |
| 629 | LOGE("mSurface must be set before startRecordingMode."); |
| 630 | return INVALID_OPERATION; |
| 631 | } |
| 632 | return startRecordingMode(); |
| 633 | default: |
| 634 | return UNKNOWN_ERROR; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | status_t CameraService::Client::startPreviewMode() { |
| 639 | LOG1("startPreviewMode"); |
| 640 | status_t result = NO_ERROR; |
| 641 | |
| 642 | // if preview has been enabled, nothing needs to be done |
| 643 | if (mHardware->previewEnabled()) { |
| 644 | return NO_ERROR; |
| 645 | } |
| 646 | |
| 647 | if (mUseOverlay) { |
| 648 | // If preview display has been set, set overlay now. |
| 649 | if (mSurface != 0) { |
| 650 | result = setOverlay(); |
| 651 | } |
| 652 | if (result != NO_ERROR) return result; |
| 653 | result = mHardware->startPreview(); |
| 654 | } else { |
| 655 | enableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 656 | result = mHardware->startPreview(); |
| 657 | if (result != NO_ERROR) return result; |
| 658 | // If preview display has been set, register preview buffers now. |
| 659 | if (mSurface != 0) { |
| 660 | // Unregister here because the surface may be previously registered |
| 661 | // with the raw (snapshot) heap. |
| 662 | mSurface->unregisterBuffers(); |
| 663 | result = registerPreviewBuffers(); |
| 664 | } |
| 665 | } |
| 666 | return result; |
| 667 | } |
| 668 | |
| 669 | status_t CameraService::Client::startRecordingMode() { |
| 670 | LOG1("startRecordingMode"); |
| 671 | status_t result = NO_ERROR; |
| 672 | |
| 673 | // if recording has been enabled, nothing needs to be done |
| 674 | if (mHardware->recordingEnabled()) { |
| 675 | return NO_ERROR; |
| 676 | } |
| 677 | |
| 678 | // if preview has not been started, start preview first |
| 679 | if (!mHardware->previewEnabled()) { |
| 680 | result = startPreviewMode(); |
| 681 | if (result != NO_ERROR) { |
| 682 | return result; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | // start recording mode |
| 687 | enableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 688 | mCameraService->playSound(SOUND_RECORDING); |
| 689 | result = mHardware->startRecording(); |
| 690 | if (result != NO_ERROR) { |
| 691 | LOGE("mHardware->startRecording() failed with status %d", result); |
| 692 | } |
| 693 | return result; |
| 694 | } |
| 695 | |
| 696 | // stop preview mode |
| 697 | void CameraService::Client::stopPreview() { |
| 698 | LOG1("stopPreview (pid %d)", getCallingPid()); |
| 699 | Mutex::Autolock lock(mLock); |
| 700 | if (checkPidAndHardware() != NO_ERROR) return; |
| 701 | |
| 702 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 703 | mHardware->stopPreview(); |
| 704 | |
| 705 | if (mSurface != 0 && !mUseOverlay) { |
| 706 | mSurface->unregisterBuffers(); |
| 707 | } |
| 708 | |
| 709 | mPreviewBuffer.clear(); |
| 710 | } |
| 711 | |
| 712 | // stop recording mode |
| 713 | void CameraService::Client::stopRecording() { |
| 714 | LOG1("stopRecording (pid %d)", getCallingPid()); |
| 715 | Mutex::Autolock lock(mLock); |
| 716 | if (checkPidAndHardware() != NO_ERROR) return; |
| 717 | |
| 718 | mCameraService->playSound(SOUND_RECORDING); |
| 719 | disableMsgType(CAMERA_MSG_VIDEO_FRAME); |
| 720 | mHardware->stopRecording(); |
| 721 | |
| 722 | mPreviewBuffer.clear(); |
| 723 | } |
| 724 | |
| 725 | // release a recording frame |
| 726 | void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) { |
| 727 | Mutex::Autolock lock(mLock); |
| 728 | if (checkPidAndHardware() != NO_ERROR) return; |
| 729 | mHardware->releaseRecordingFrame(mem); |
| 730 | } |
| 731 | |
| 732 | bool CameraService::Client::previewEnabled() { |
| 733 | LOG1("previewEnabled (pid %d)", getCallingPid()); |
| 734 | |
| 735 | Mutex::Autolock lock(mLock); |
| 736 | if (checkPidAndHardware() != NO_ERROR) return false; |
| 737 | return mHardware->previewEnabled(); |
| 738 | } |
| 739 | |
| 740 | bool CameraService::Client::recordingEnabled() { |
| 741 | LOG1("recordingEnabled (pid %d)", getCallingPid()); |
| 742 | |
| 743 | Mutex::Autolock lock(mLock); |
| 744 | if (checkPidAndHardware() != NO_ERROR) return false; |
| 745 | return mHardware->recordingEnabled(); |
| 746 | } |
| 747 | |
| 748 | status_t CameraService::Client::autoFocus() { |
| 749 | LOG1("autoFocus (pid %d)", getCallingPid()); |
| 750 | |
| 751 | Mutex::Autolock lock(mLock); |
| 752 | status_t result = checkPidAndHardware(); |
| 753 | if (result != NO_ERROR) return result; |
| 754 | |
| 755 | return mHardware->autoFocus(); |
| 756 | } |
| 757 | |
| 758 | status_t CameraService::Client::cancelAutoFocus() { |
| 759 | LOG1("cancelAutoFocus (pid %d)", getCallingPid()); |
| 760 | |
| 761 | Mutex::Autolock lock(mLock); |
| 762 | status_t result = checkPidAndHardware(); |
| 763 | if (result != NO_ERROR) return result; |
| 764 | |
| 765 | return mHardware->cancelAutoFocus(); |
| 766 | } |
| 767 | |
| 768 | // take a picture - image is returned in callback |
| 769 | status_t CameraService::Client::takePicture() { |
| 770 | LOG1("takePicture (pid %d)", getCallingPid()); |
| 771 | |
| 772 | Mutex::Autolock lock(mLock); |
| 773 | status_t result = checkPidAndHardware(); |
| 774 | if (result != NO_ERROR) return result; |
| 775 | |
| 776 | enableMsgType(CAMERA_MSG_SHUTTER | |
| 777 | CAMERA_MSG_POSTVIEW_FRAME | |
| 778 | CAMERA_MSG_RAW_IMAGE | |
| 779 | CAMERA_MSG_COMPRESSED_IMAGE); |
| 780 | |
| 781 | return mHardware->takePicture(); |
| 782 | } |
| 783 | |
| 784 | // set preview/capture parameters - key/value pairs |
| 785 | status_t CameraService::Client::setParameters(const String8& params) { |
| 786 | LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string()); |
| 787 | |
| 788 | Mutex::Autolock lock(mLock); |
| 789 | status_t result = checkPidAndHardware(); |
| 790 | if (result != NO_ERROR) return result; |
| 791 | |
| 792 | CameraParameters p(params); |
| 793 | return mHardware->setParameters(p); |
| 794 | } |
| 795 | |
| 796 | // get preview/capture parameters - key/value pairs |
| 797 | String8 CameraService::Client::getParameters() const { |
| 798 | Mutex::Autolock lock(mLock); |
| 799 | if (checkPidAndHardware() != NO_ERROR) return String8(); |
| 800 | |
| 801 | String8 params(mHardware->getParameters().flatten()); |
| 802 | LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string()); |
| 803 | return params; |
| 804 | } |
| 805 | |
| 806 | status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { |
| 807 | LOG1("sendCommand (pid %d)", getCallingPid()); |
| 808 | Mutex::Autolock lock(mLock); |
| 809 | status_t result = checkPidAndHardware(); |
| 810 | if (result != NO_ERROR) return result; |
| 811 | |
| 812 | if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) { |
| 813 | // The orientation cannot be set during preview. |
| 814 | if (mHardware->previewEnabled()) { |
| 815 | return INVALID_OPERATION; |
| 816 | } |
| 817 | switch (arg1) { |
| 818 | case 0: |
| 819 | mOrientation = ISurface::BufferHeap::ROT_0; |
| 820 | break; |
| 821 | case 90: |
| 822 | mOrientation = ISurface::BufferHeap::ROT_90; |
| 823 | break; |
| 824 | case 180: |
| 825 | mOrientation = ISurface::BufferHeap::ROT_180; |
| 826 | break; |
| 827 | case 270: |
| 828 | mOrientation = ISurface::BufferHeap::ROT_270; |
| 829 | break; |
| 830 | default: |
| 831 | return BAD_VALUE; |
| 832 | } |
| 833 | return OK; |
| 834 | } |
| 835 | |
| 836 | return mHardware->sendCommand(cmd, arg1, arg2); |
| 837 | } |
| 838 | |
| 839 | // ---------------------------------------------------------------------------- |
| 840 | |
| 841 | void CameraService::Client::enableMsgType(int32_t msgType) { |
| 842 | android_atomic_or(msgType, &mMsgEnabled); |
| 843 | mHardware->enableMsgType(msgType); |
| 844 | } |
| 845 | |
| 846 | void CameraService::Client::disableMsgType(int32_t msgType) { |
| 847 | android_atomic_and(~msgType, &mMsgEnabled); |
| 848 | mHardware->disableMsgType(msgType); |
| 849 | } |
| 850 | |
| 851 | #define CHECK_MESSAGE_INTERVAL 10 // 10ms |
| 852 | bool CameraService::Client::lockIfMessageWanted(int32_t msgType) { |
| 853 | int sleepCount = 0; |
| 854 | while (mMsgEnabled & msgType) { |
| 855 | if (mLock.tryLock() == NO_ERROR) { |
| 856 | if (sleepCount > 0) { |
| 857 | LOG1("lockIfMessageWanted(%d): waited for %d ms", |
| 858 | msgType, sleepCount * CHECK_MESSAGE_INTERVAL); |
| 859 | } |
| 860 | return true; |
| 861 | } |
| 862 | if (sleepCount++ == 0) { |
| 863 | LOG1("lockIfMessageWanted(%d): enter sleep", msgType); |
| 864 | } |
| 865 | usleep(CHECK_MESSAGE_INTERVAL * 1000); |
| 866 | } |
| 867 | LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType); |
| 868 | return false; |
| 869 | } |
| 870 | |
| 871 | // ---------------------------------------------------------------------------- |
| 872 | |
| 873 | // Converts from a raw pointer to the client to a strong pointer during a |
| 874 | // hardware callback. This requires the callbacks only happen when the client |
| 875 | // is still alive. |
| 876 | sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) { |
| 877 | sp<Client> client = gCameraService->getClientById((int) user); |
| 878 | |
| 879 | // This could happen if the Client is in the process of shutting down (the |
| 880 | // last strong reference is gone, but the destructor hasn't finished |
| 881 | // stopping the hardware). |
| 882 | if (client == 0) return NULL; |
| 883 | |
| 884 | // The checks below are not necessary and are for debugging only. |
| 885 | if (client->mCameraService.get() != gCameraService) { |
| 886 | LOGE("mismatch service!"); |
| 887 | return NULL; |
| 888 | } |
| 889 | |
| 890 | if (client->mHardware == 0) { |
| 891 | LOGE("mHardware == 0: callback after disconnect()?"); |
| 892 | return NULL; |
| 893 | } |
| 894 | |
| 895 | return client; |
| 896 | } |
| 897 | |
| 898 | // Callback messages can be dispatched to internal handlers or pass to our |
| 899 | // client's callback functions, depending on the message type. |
| 900 | // |
| 901 | // notifyCallback: |
| 902 | // CAMERA_MSG_SHUTTER handleShutter |
| 903 | // (others) c->notifyCallback |
| 904 | // dataCallback: |
| 905 | // CAMERA_MSG_PREVIEW_FRAME handlePreviewData |
| 906 | // CAMERA_MSG_POSTVIEW_FRAME handlePostview |
| 907 | // CAMERA_MSG_RAW_IMAGE handleRawPicture |
| 908 | // CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture |
| 909 | // (others) c->dataCallback |
| 910 | // dataCallbackTimestamp |
| 911 | // (others) c->dataCallbackTimestamp |
| 912 | // |
| 913 | // NOTE: the *Callback functions grab mLock of the client before passing |
| 914 | // control to handle* functions. So the handle* functions must release the |
| 915 | // lock before calling the ICameraClient's callbacks, so those callbacks can |
| 916 | // invoke methods in the Client class again (For example, the preview frame |
| 917 | // callback may want to releaseRecordingFrame). The handle* functions must |
| 918 | // release the lock after all accesses to member variables, so it must be |
| 919 | // handled very carefully. |
| 920 | |
| 921 | void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1, |
| 922 | int32_t ext2, void* user) { |
| 923 | LOG2("notifyCallback(%d)", msgType); |
| 924 | |
| 925 | sp<Client> client = getClientFromCookie(user); |
| 926 | if (client == 0) return; |
| 927 | if (!client->lockIfMessageWanted(msgType)) return; |
| 928 | |
| 929 | switch (msgType) { |
| 930 | case CAMERA_MSG_SHUTTER: |
| 931 | // ext1 is the dimension of the yuv picture. |
| 932 | client->handleShutter((image_rect_type *)ext1); |
| 933 | break; |
| 934 | default: |
| 935 | client->handleGenericNotify(msgType, ext1, ext2); |
| 936 | break; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | void CameraService::Client::dataCallback(int32_t msgType, |
| 941 | const sp<IMemory>& dataPtr, void* user) { |
| 942 | LOG2("dataCallback(%d)", msgType); |
| 943 | |
| 944 | sp<Client> client = getClientFromCookie(user); |
| 945 | if (client == 0) return; |
| 946 | if (!client->lockIfMessageWanted(msgType)) return; |
| 947 | |
| 948 | if (dataPtr == 0) { |
| 949 | LOGE("Null data returned in data callback"); |
| 950 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | switch (msgType) { |
| 955 | case CAMERA_MSG_PREVIEW_FRAME: |
| 956 | client->handlePreviewData(dataPtr); |
| 957 | break; |
| 958 | case CAMERA_MSG_POSTVIEW_FRAME: |
| 959 | client->handlePostview(dataPtr); |
| 960 | break; |
| 961 | case CAMERA_MSG_RAW_IMAGE: |
| 962 | client->handleRawPicture(dataPtr); |
| 963 | break; |
| 964 | case CAMERA_MSG_COMPRESSED_IMAGE: |
| 965 | client->handleCompressedPicture(dataPtr); |
| 966 | break; |
| 967 | default: |
| 968 | client->handleGenericData(msgType, dataPtr); |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp, |
| 974 | int32_t msgType, const sp<IMemory>& dataPtr, void* user) { |
| 975 | LOG2("dataCallbackTimestamp(%d)", msgType); |
| 976 | |
| 977 | sp<Client> client = getClientFromCookie(user); |
| 978 | if (client == 0) return; |
| 979 | if (!client->lockIfMessageWanted(msgType)) return; |
| 980 | |
| 981 | if (dataPtr == 0) { |
| 982 | LOGE("Null data returned in data with timestamp callback"); |
| 983 | client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0); |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | client->handleGenericDataTimestamp(timestamp, msgType, dataPtr); |
| 988 | } |
| 989 | |
| 990 | // snapshot taken callback |
| 991 | // "size" is the width and height of yuv picture for registerBuffer. |
| 992 | // If it is NULL, use the picture size from parameters. |
| 993 | void CameraService::Client::handleShutter(image_rect_type *size) { |
| 994 | mCameraService->playSound(SOUND_SHUTTER); |
| 995 | |
| 996 | // Screen goes black after the buffer is unregistered. |
| 997 | if (mSurface != 0 && !mUseOverlay) { |
| 998 | mSurface->unregisterBuffers(); |
| 999 | } |
| 1000 | |
| 1001 | sp<ICameraClient> c = mCameraClient; |
| 1002 | if (c != 0) { |
| 1003 | mLock.unlock(); |
| 1004 | c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0); |
| 1005 | if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return; |
| 1006 | } |
| 1007 | disableMsgType(CAMERA_MSG_SHUTTER); |
| 1008 | |
| 1009 | // It takes some time before yuvPicture callback to be called. |
| 1010 | // Register the buffer for raw image here to reduce latency. |
| 1011 | if (mSurface != 0 && !mUseOverlay) { |
| 1012 | int w, h; |
| 1013 | CameraParameters params(mHardware->getParameters()); |
| 1014 | if (size == NULL) { |
| 1015 | params.getPictureSize(&w, &h); |
| 1016 | } else { |
| 1017 | w = size->width; |
| 1018 | h = size->height; |
| 1019 | w &= ~1; |
| 1020 | h &= ~1; |
| 1021 | LOG1("Snapshot image width=%d, height=%d", w, h); |
| 1022 | } |
| 1023 | // FIXME: don't use hardcoded format constants here |
| 1024 | ISurface::BufferHeap buffers(w, h, w, h, |
| 1025 | HAL_PIXEL_FORMAT_YCrCb_420_SP, mOrientation, 0, |
| 1026 | mHardware->getRawHeap()); |
| 1027 | |
| 1028 | mSurface->registerBuffers(buffers); |
| 1029 | IPCThreadState::self()->flushCommands(); |
| 1030 | } |
| 1031 | |
| 1032 | mLock.unlock(); |
| 1033 | } |
| 1034 | |
| 1035 | // preview callback - frame buffer update |
| 1036 | void CameraService::Client::handlePreviewData(const sp<IMemory>& mem) { |
| 1037 | ssize_t offset; |
| 1038 | size_t size; |
| 1039 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1040 | |
| 1041 | if (!mUseOverlay) { |
| 1042 | if (mSurface != 0) { |
| 1043 | mSurface->postBuffer(offset); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | // local copy of the callback flags |
| 1048 | int flags = mPreviewCallbackFlag; |
| 1049 | |
| 1050 | // is callback enabled? |
| 1051 | if (!(flags & FRAME_CALLBACK_FLAG_ENABLE_MASK)) { |
| 1052 | // If the enable bit is off, the copy-out and one-shot bits are ignored |
| 1053 | LOG2("frame callback is disabled"); |
| 1054 | mLock.unlock(); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | // hold a strong pointer to the client |
| 1059 | sp<ICameraClient> c = mCameraClient; |
| 1060 | |
| 1061 | // clear callback flags if no client or one-shot mode |
| 1062 | if (c == 0 || (mPreviewCallbackFlag & FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) { |
| 1063 | LOG2("Disable preview callback"); |
| 1064 | mPreviewCallbackFlag &= ~(FRAME_CALLBACK_FLAG_ONE_SHOT_MASK | |
| 1065 | FRAME_CALLBACK_FLAG_COPY_OUT_MASK | |
| 1066 | FRAME_CALLBACK_FLAG_ENABLE_MASK); |
| 1067 | if (mUseOverlay) { |
| 1068 | disableMsgType(CAMERA_MSG_PREVIEW_FRAME); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | if (c != 0) { |
| 1073 | // Is the received frame copied out or not? |
| 1074 | if (flags & FRAME_CALLBACK_FLAG_COPY_OUT_MASK) { |
| 1075 | LOG2("frame is copied"); |
| 1076 | copyFrameAndPostCopiedFrame(c, heap, offset, size); |
| 1077 | } else { |
| 1078 | LOG2("frame is forwarded"); |
| 1079 | mLock.unlock(); |
| 1080 | c->dataCallback(CAMERA_MSG_PREVIEW_FRAME, mem); |
| 1081 | } |
| 1082 | } else { |
| 1083 | mLock.unlock(); |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | // picture callback - postview image ready |
| 1088 | void CameraService::Client::handlePostview(const sp<IMemory>& mem) { |
| 1089 | disableMsgType(CAMERA_MSG_POSTVIEW_FRAME); |
| 1090 | |
| 1091 | sp<ICameraClient> c = mCameraClient; |
| 1092 | mLock.unlock(); |
| 1093 | if (c != 0) { |
| 1094 | c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | // picture callback - raw image ready |
| 1099 | void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) { |
| 1100 | disableMsgType(CAMERA_MSG_RAW_IMAGE); |
| 1101 | |
| 1102 | ssize_t offset; |
| 1103 | size_t size; |
| 1104 | sp<IMemoryHeap> heap = mem->getMemory(&offset, &size); |
| 1105 | |
| 1106 | // Put the YUV version of the snapshot in the preview display. |
| 1107 | if (mSurface != 0 && !mUseOverlay) { |
| 1108 | mSurface->postBuffer(offset); |
| 1109 | } |
| 1110 | |
| 1111 | sp<ICameraClient> c = mCameraClient; |
| 1112 | mLock.unlock(); |
| 1113 | if (c != 0) { |
| 1114 | c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | // picture callback - compressed picture ready |
| 1119 | void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) { |
| 1120 | disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE); |
| 1121 | |
| 1122 | sp<ICameraClient> c = mCameraClient; |
| 1123 | mLock.unlock(); |
| 1124 | if (c != 0) { |
| 1125 | c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | void CameraService::Client::handleGenericNotify(int32_t msgType, |
| 1131 | int32_t ext1, int32_t ext2) { |
| 1132 | sp<ICameraClient> c = mCameraClient; |
| 1133 | mLock.unlock(); |
| 1134 | if (c != 0) { |
| 1135 | c->notifyCallback(msgType, ext1, ext2); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | void CameraService::Client::handleGenericData(int32_t msgType, |
| 1140 | const sp<IMemory>& dataPtr) { |
| 1141 | sp<ICameraClient> c = mCameraClient; |
| 1142 | mLock.unlock(); |
| 1143 | if (c != 0) { |
| 1144 | c->dataCallback(msgType, dataPtr); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp, |
| 1149 | int32_t msgType, const sp<IMemory>& dataPtr) { |
| 1150 | sp<ICameraClient> c = mCameraClient; |
| 1151 | mLock.unlock(); |
| 1152 | if (c != 0) { |
| 1153 | c->dataCallbackTimestamp(timestamp, msgType, dataPtr); |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | void CameraService::Client::copyFrameAndPostCopiedFrame( |
| 1158 | const sp<ICameraClient>& client, const sp<IMemoryHeap>& heap, |
| 1159 | size_t offset, size_t size) { |
| 1160 | LOG2("copyFrameAndPostCopiedFrame"); |
| 1161 | // It is necessary to copy out of pmem before sending this to |
| 1162 | // the callback. For efficiency, reuse the same MemoryHeapBase |
| 1163 | // provided it's big enough. Don't allocate the memory or |
| 1164 | // perform the copy if there's no callback. |
| 1165 | // hold the preview lock while we grab a reference to the preview buffer |
| 1166 | sp<MemoryHeapBase> previewBuffer; |
| 1167 | |
| 1168 | if (mPreviewBuffer == 0) { |
| 1169 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1170 | } else if (size > mPreviewBuffer->virtualSize()) { |
| 1171 | mPreviewBuffer.clear(); |
| 1172 | mPreviewBuffer = new MemoryHeapBase(size, 0, NULL); |
| 1173 | } |
| 1174 | if (mPreviewBuffer == 0) { |
| 1175 | LOGE("failed to allocate space for preview buffer"); |
| 1176 | mLock.unlock(); |
| 1177 | return; |
| 1178 | } |
| 1179 | previewBuffer = mPreviewBuffer; |
| 1180 | |
| 1181 | memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size); |
| 1182 | |
| 1183 | sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size); |
| 1184 | if (frame == 0) { |
| 1185 | LOGE("failed to allocate space for frame callback"); |
| 1186 | mLock.unlock(); |
| 1187 | return; |
| 1188 | } |
| 1189 | |
| 1190 | mLock.unlock(); |
| 1191 | client->dataCallback(CAMERA_MSG_PREVIEW_FRAME, frame); |
| 1192 | } |
| 1193 | |
| 1194 | // ---------------------------------------------------------------------------- |
| 1195 | |
| 1196 | static const int kDumpLockRetries = 50; |
| 1197 | static const int kDumpLockSleep = 60000; |
| 1198 | |
| 1199 | static bool tryLock(Mutex& mutex) |
| 1200 | { |
| 1201 | bool locked = false; |
| 1202 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 1203 | if (mutex.tryLock() == NO_ERROR) { |
| 1204 | locked = true; |
| 1205 | break; |
| 1206 | } |
| 1207 | usleep(kDumpLockSleep); |
| 1208 | } |
| 1209 | return locked; |
| 1210 | } |
| 1211 | |
| 1212 | status_t CameraService::dump(int fd, const Vector<String16>& args) { |
| 1213 | static const char* kDeadlockedString = "CameraService may be deadlocked\n"; |
| 1214 | |
| 1215 | const size_t SIZE = 256; |
| 1216 | char buffer[SIZE]; |
| 1217 | String8 result; |
| 1218 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 1219 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1220 | "can't dump CameraService from pid=%d, uid=%d\n", |
| 1221 | getCallingPid(), |
| 1222 | getCallingUid()); |
| 1223 | result.append(buffer); |
| 1224 | write(fd, result.string(), result.size()); |
| 1225 | } else { |
| 1226 | bool locked = tryLock(mServiceLock); |
| 1227 | // failed to lock - CameraService is probably deadlocked |
| 1228 | if (!locked) { |
| 1229 | String8 result(kDeadlockedString); |
| 1230 | write(fd, result.string(), result.size()); |
| 1231 | } |
| 1232 | |
| 1233 | bool hasClient = false; |
| 1234 | for (int i = 0; i < mNumberOfCameras; i++) { |
| 1235 | sp<Client> client = mClient[i].promote(); |
| 1236 | if (client == 0) continue; |
| 1237 | hasClient = true; |
| 1238 | sprintf(buffer, "Client[%d] (%p) PID: %d\n", |
| 1239 | i, |
| 1240 | client->getCameraClient()->asBinder().get(), |
| 1241 | client->mClientPid); |
| 1242 | result.append(buffer); |
| 1243 | write(fd, result.string(), result.size()); |
| 1244 | client->mHardware->dump(fd, args); |
| 1245 | } |
| 1246 | if (!hasClient) { |
| 1247 | result.append("No camera client yet.\n"); |
| 1248 | write(fd, result.string(), result.size()); |
| 1249 | } |
| 1250 | |
| 1251 | if (locked) mServiceLock.unlock(); |
| 1252 | |
| 1253 | // change logging level |
| 1254 | int n = args.size(); |
| 1255 | for (int i = 0; i + 1 < n; i++) { |
| 1256 | if (args[i] == String16("-v")) { |
| 1257 | String8 levelStr(args[i+1]); |
| 1258 | int level = atoi(levelStr.string()); |
| 1259 | sprintf(buffer, "Set Log Level to %d", level); |
| 1260 | result.append(buffer); |
| 1261 | setLogLevel(level); |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | return NO_ERROR; |
| 1266 | } |
| 1267 | |
| 1268 | }; // namespace android |