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