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