1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "Camera2" 18 #define ATRACE_TAG ATRACE_TAG_CAMERA 19 //#define LOG_NDEBUG 0 20 21 #include <utils/Log.h> 22 #include <utils/Trace.h> 23 24 #include <cutils/properties.h> 25 #include <gui/Surface.h> 26 #include "camera2/Parameters.h" 27 #include "Camera2Client.h" 28 #include "Camera2Device.h" 29 #include "Camera3Device.h" 30 31 #include "camera2/ZslProcessor.h" 32 #include "camera2/ZslProcessor3.h" 33 34 #define ALOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__); 35 #define ALOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__); 36 37 namespace android { 38 using namespace camera2; 39 40 static int getCallingPid() { 41 return IPCThreadState::self()->getCallingPid(); 42 } 43 44 // Interface used by CameraService 45 46 Camera2Client::Camera2Client(const sp<CameraService>& cameraService, 47 const sp<ICameraClient>& cameraClient, 48 const String16& clientPackageName, 49 int cameraId, 50 int cameraFacing, 51 int clientPid, 52 uid_t clientUid, 53 int servicePid, 54 int deviceVersion): 55 Camera2ClientBase(cameraService, cameraClient, clientPackageName, 56 cameraId, cameraFacing, clientPid, clientUid, servicePid), 57 mParameters(cameraId, cameraFacing), 58 mDeviceVersion(deviceVersion) 59 { 60 ATRACE_CALL(); 61 ALOGI("Camera %d: Opened", cameraId); 62 63 switch (mDeviceVersion) { 64 case CAMERA_DEVICE_API_VERSION_2_0: 65 mDevice = new Camera2Device(cameraId); 66 break; 67 case CAMERA_DEVICE_API_VERSION_3_0: 68 mDevice = new Camera3Device(cameraId); 69 break; 70 default: 71 ALOGE("Camera %d: Unknown HAL device version %d", 72 cameraId, mDeviceVersion); 73 mDevice = NULL; 74 break; 75 } 76 77 78 SharedParameters::Lock l(mParameters); 79 l.mParameters.state = Parameters::DISCONNECTED; 80 } 81 82 status_t Camera2Client::initialize(camera_module_t *module) 83 { 84 ATRACE_CALL(); 85 ALOGV("%s: Initializing client for camera %d", __FUNCTION__, mCameraId); 86 status_t res; 87 88 res = Camera2ClientBase::initialize(module); 89 if (res != OK) { 90 return res; 91 } 92 93 SharedParameters::Lock l(mParameters); 94 95 res = l.mParameters.initialize(&(mDevice->info())); 96 if (res != OK) { 97 ALOGE("%s: Camera %d: unable to build defaults: %s (%d)", 98 __FUNCTION__, mCameraId, strerror(-res), res); 99 return NO_INIT; 100 } 101 102 String8 threadName; 103 104 mStreamingProcessor = new StreamingProcessor(this); 105 threadName = String8::format("C2-%d-StreamProc", 106 mCameraId); 107 mStreamingProcessor->run(threadName.string()); 108 109 mFrameProcessor = new FrameProcessor(mDevice, this); 110 threadName = String8::format("C2-%d-FrameProc", 111 mCameraId); 112 mFrameProcessor->run(threadName.string()); 113 114 mCaptureSequencer = new CaptureSequencer(this); 115 threadName = String8::format("C2-%d-CaptureSeq", 116 mCameraId); 117 mCaptureSequencer->run(threadName.string()); 118 119 mJpegProcessor = new JpegProcessor(this, mCaptureSequencer); 120 threadName = String8::format("C2-%d-JpegProc", 121 mCameraId); 122 mJpegProcessor->run(threadName.string()); 123 124 switch (mDeviceVersion) { 125 case CAMERA_DEVICE_API_VERSION_2_0: { 126 sp<ZslProcessor> zslProc = 127 new ZslProcessor(this, mCaptureSequencer); 128 mZslProcessor = zslProc; 129 mZslProcessorThread = zslProc; 130 break; 131 } 132 case CAMERA_DEVICE_API_VERSION_3_0:{ 133 sp<ZslProcessor3> zslProc = 134 new ZslProcessor3(this, mCaptureSequencer); 135 mZslProcessor = zslProc; 136 mZslProcessorThread = zslProc; 137 break; 138 } 139 default: 140 break; 141 } 142 threadName = String8::format("C2-%d-ZslProc", 143 mCameraId); 144 mZslProcessorThread->run(threadName.string()); 145 146 mCallbackProcessor = new CallbackProcessor(this); 147 threadName = String8::format("C2-%d-CallbkProc", 148 mCameraId); 149 mCallbackProcessor->run(threadName.string()); 150 151 if (gLogLevel >= 1) { 152 ALOGD("%s: Default parameters converted from camera %d:", __FUNCTION__, 153 mCameraId); 154 ALOGD("%s", l.mParameters.paramsFlattened.string()); 155 } 156 157 return OK; 158 } 159 160 Camera2Client::~Camera2Client() { 161 ATRACE_CALL(); 162 ALOGV("~Camera2Client"); 163 164 mDestructionStarted = true; 165 166 disconnect(); 167 168 ALOGI("Camera %d: Closed", mCameraId); 169 } 170 171 status_t Camera2Client::dump(int fd, const Vector<String16>& args) { 172 String8 result; 173 result.appendFormat("Client2[%d] (%p) Client: %s PID: %d, dump:\n", 174 mCameraId, 175 getRemoteCallback()->asBinder().get(), 176 String8(mClientPackageName).string(), 177 mClientPid); 178 result.append(" State: "); 179 #define CASE_APPEND_ENUM(x) case x: result.append(#x "\n"); break; 180 181 const Parameters& p = mParameters.unsafeAccess(); 182 183 result.append(Parameters::getStateName(p.state)); 184 185 result.append("\n Current parameters:\n"); 186 result.appendFormat(" Preview size: %d x %d\n", 187 p.previewWidth, p.previewHeight); 188 result.appendFormat(" Preview FPS range: %d - %d\n", 189 p.previewFpsRange[0], p.previewFpsRange[1]); 190 result.appendFormat(" Preview HAL pixel format: 0x%x\n", 191 p.previewFormat); 192 result.appendFormat(" Preview transform: %x\n", 193 p.previewTransform); 194 result.appendFormat(" Picture size: %d x %d\n", 195 p.pictureWidth, p.pictureHeight); 196 result.appendFormat(" Jpeg thumbnail size: %d x %d\n", 197 p.jpegThumbSize[0], p.jpegThumbSize[1]); 198 result.appendFormat(" Jpeg quality: %d, thumbnail quality: %d\n", 199 p.jpegQuality, p.jpegThumbQuality); 200 result.appendFormat(" Jpeg rotation: %d\n", p.jpegRotation); 201 result.appendFormat(" GPS tags %s\n", 202 p.gpsEnabled ? "enabled" : "disabled"); 203 if (p.gpsEnabled) { 204 result.appendFormat(" GPS lat x long x alt: %f x %f x %f\n", 205 p.gpsCoordinates[0], p.gpsCoordinates[1], 206 p.gpsCoordinates[2]); 207 result.appendFormat(" GPS timestamp: %lld\n", 208 p.gpsTimestamp); 209 result.appendFormat(" GPS processing method: %s\n", 210 p.gpsProcessingMethod.string()); 211 } 212 213 result.append(" White balance mode: "); 214 switch (p.wbMode) { 215 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_AUTO) 216 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_INCANDESCENT) 217 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_FLUORESCENT) 218 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_WARM_FLUORESCENT) 219 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_DAYLIGHT) 220 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_CLOUDY_DAYLIGHT) 221 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_TWILIGHT) 222 CASE_APPEND_ENUM(ANDROID_CONTROL_AWB_MODE_SHADE) 223 default: result.append("UNKNOWN\n"); 224 } 225 226 result.append(" Effect mode: "); 227 switch (p.effectMode) { 228 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_OFF) 229 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_MONO) 230 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_NEGATIVE) 231 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_SOLARIZE) 232 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_SEPIA) 233 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_POSTERIZE) 234 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_WHITEBOARD) 235 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_BLACKBOARD) 236 CASE_APPEND_ENUM(ANDROID_CONTROL_EFFECT_MODE_AQUA) 237 default: result.append("UNKNOWN\n"); 238 } 239 240 result.append(" Antibanding mode: "); 241 switch (p.antibandingMode) { 242 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO) 243 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_OFF) 244 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_50HZ) 245 CASE_APPEND_ENUM(ANDROID_CONTROL_AE_ANTIBANDING_MODE_60HZ) 246 default: result.append("UNKNOWN\n"); 247 } 248 249 result.append(" Scene mode: "); 250 switch (p.sceneMode) { 251 case ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED: 252 result.append("AUTO\n"); break; 253 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_ACTION) 254 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_PORTRAIT) 255 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_LANDSCAPE) 256 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_NIGHT) 257 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_NIGHT_PORTRAIT) 258 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_THEATRE) 259 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_BEACH) 260 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_SNOW) 261 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_SUNSET) 262 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_STEADYPHOTO) 263 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_FIREWORKS) 264 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_SPORTS) 265 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_PARTY) 266 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_CANDLELIGHT) 267 CASE_APPEND_ENUM(ANDROID_CONTROL_SCENE_MODE_BARCODE) 268 default: result.append("UNKNOWN\n"); 269 } 270 271 result.append(" Flash mode: "); 272 switch (p.flashMode) { 273 CASE_APPEND_ENUM(Parameters::FLASH_MODE_OFF) 274 CASE_APPEND_ENUM(Parameters::FLASH_MODE_AUTO) 275 CASE_APPEND_ENUM(Parameters::FLASH_MODE_ON) 276 CASE_APPEND_ENUM(Parameters::FLASH_MODE_TORCH) 277 CASE_APPEND_ENUM(Parameters::FLASH_MODE_RED_EYE) 278 CASE_APPEND_ENUM(Parameters::FLASH_MODE_INVALID) 279 default: result.append("UNKNOWN\n"); 280 } 281 282 result.append(" Focus mode: "); 283 switch (p.focusMode) { 284 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_AUTO) 285 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_MACRO) 286 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_CONTINUOUS_VIDEO) 287 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_CONTINUOUS_PICTURE) 288 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_EDOF) 289 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_INFINITY) 290 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_FIXED) 291 CASE_APPEND_ENUM(Parameters::FOCUS_MODE_INVALID) 292 default: result.append("UNKNOWN\n"); 293 } 294 295 result.append(" Focus state: "); 296 switch (p.focusState) { 297 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_INACTIVE) 298 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN) 299 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED) 300 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN) 301 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED) 302 CASE_APPEND_ENUM(ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) 303 default: result.append("UNKNOWN\n"); 304 } 305 306 result.append(" Focusing areas:\n"); 307 for (size_t i = 0; i < p.focusingAreas.size(); i++) { 308 result.appendFormat(" [ (%d, %d, %d, %d), weight %d ]\n", 309 p.focusingAreas[i].left, 310 p.focusingAreas[i].top, 311 p.focusingAreas[i].right, 312 p.focusingAreas[i].bottom, 313 p.focusingAreas[i].weight); 314 } 315 316 result.appendFormat(" Exposure compensation index: %d\n", 317 p.exposureCompensation); 318 319 result.appendFormat(" AE lock %s, AWB lock %s\n", 320 p.autoExposureLock ? "enabled" : "disabled", 321 p.autoWhiteBalanceLock ? "enabled" : "disabled" ); 322 323 result.appendFormat(" Metering areas:\n"); 324 for (size_t i = 0; i < p.meteringAreas.size(); i++) { 325 result.appendFormat(" [ (%d, %d, %d, %d), weight %d ]\n", 326 p.meteringAreas[i].left, 327 p.meteringAreas[i].top, 328 p.meteringAreas[i].right, 329 p.meteringAreas[i].bottom, 330 p.meteringAreas[i].weight); 331 } 332 333 result.appendFormat(" Zoom index: %d\n", p.zoom); 334 result.appendFormat(" Video size: %d x %d\n", p.videoWidth, 335 p.videoHeight); 336 337 result.appendFormat(" Recording hint is %s\n", 338 p.recordingHint ? "set" : "not set"); 339 340 result.appendFormat(" Video stabilization is %s\n", 341 p.videoStabilization ? "enabled" : "disabled"); 342 343 result.append(" Current streams:\n"); 344 result.appendFormat(" Preview stream ID: %d\n", 345 getPreviewStreamId()); 346 result.appendFormat(" Capture stream ID: %d\n", 347 getCaptureStreamId()); 348 result.appendFormat(" Recording stream ID: %d\n", 349 getRecordingStreamId()); 350 351 result.append(" Quirks for this camera:\n"); 352 bool haveQuirk = false; 353 if (p.quirks.triggerAfWithAuto) { 354 result.appendFormat(" triggerAfWithAuto\n"); 355 haveQuirk = true; 356 } 357 if (p.quirks.useZslFormat) { 358 result.appendFormat(" useZslFormat\n"); 359 haveQuirk = true; 360 } 361 if (p.quirks.meteringCropRegion) { 362 result.appendFormat(" meteringCropRegion\n"); 363 haveQuirk = true; 364 } 365 if (!haveQuirk) { 366 result.appendFormat(" none\n"); 367 } 368 369 write(fd, result.string(), result.size()); 370 371 mStreamingProcessor->dump(fd, args); 372 373 mCaptureSequencer->dump(fd, args); 374 375 mFrameProcessor->dump(fd, args); 376 377 mZslProcessor->dump(fd, args); 378 379 return dumpDevice(fd, args); 380 #undef CASE_APPEND_ENUM 381 } 382 383 // ICamera interface 384 385 void Camera2Client::disconnect() { 386 ATRACE_CALL(); 387 Mutex::Autolock icl(mBinderSerializationLock); 388 389 // Allow both client and the media server to disconnect at all times 390 int callingPid = getCallingPid(); 391 if (callingPid != mClientPid && callingPid != mServicePid) return; 392 393 if (mDevice == 0) return; 394 395 ALOGV("Camera %d: Shutting down", mCameraId); 396 397 /** 398 * disconnect() cannot call any methods that might need to promote a 399 * wp<Camera2Client>, since disconnect can be called from the destructor, at 400 * which point all such promotions will fail. 401 */ 402 403 stopPreviewL(); 404 405 { 406 SharedParameters::Lock l(mParameters); 407 if (l.mParameters.state == Parameters::DISCONNECTED) return; 408 l.mParameters.state = Parameters::DISCONNECTED; 409 } 410 411 mStreamingProcessor->deletePreviewStream(); 412 mStreamingProcessor->deleteRecordingStream(); 413 mJpegProcessor->deleteStream(); 414 mCallbackProcessor->deleteStream(); 415 mZslProcessor->deleteStream(); 416 417 mStreamingProcessor->requestExit(); 418 mFrameProcessor->requestExit(); 419 mCaptureSequencer->requestExit(); 420 mJpegProcessor->requestExit(); 421 mZslProcessorThread->requestExit(); 422 mCallbackProcessor->requestExit(); 423 424 ALOGV("Camera %d: Waiting for threads", mCameraId); 425 426 mStreamingProcessor->join(); 427 mFrameProcessor->join(); 428 mCaptureSequencer->join(); 429 mJpegProcessor->join(); 430 mZslProcessorThread->join(); 431 mCallbackProcessor->join(); 432 433 ALOGV("Camera %d: Disconnecting device", mCameraId); 434 435 mDevice->disconnect(); 436 437 mDevice.clear(); 438 439 CameraService::Client::disconnect(); 440 } 441 442 status_t Camera2Client::connect(const sp<ICameraClient>& client) { 443 ATRACE_CALL(); 444 ALOGV("%s: E", __FUNCTION__); 445 Mutex::Autolock icl(mBinderSerializationLock); 446 447 if (mClientPid != 0 && getCallingPid() != mClientPid) { 448 ALOGE("%s: Camera %d: Connection attempt from pid %d; " 449 "current locked to pid %d", __FUNCTION__, 450 mCameraId, getCallingPid(), mClientPid); 451 return BAD_VALUE; 452 } 453 454 mClientPid = getCallingPid(); 455 456 mRemoteCallback = client; 457 mSharedCameraCallbacks = client; 458 459 return OK; 460 } 461 462 status_t Camera2Client::lock() { 463 ATRACE_CALL(); 464 ALOGV("%s: E", __FUNCTION__); 465 Mutex::Autolock icl(mBinderSerializationLock); 466 ALOGV("%s: Camera %d: Lock call from pid %d; current client pid %d", 467 __FUNCTION__, mCameraId, getCallingPid(), mClientPid); 468 469 if (mClientPid == 0) { 470 mClientPid = getCallingPid(); 471 return OK; 472 } 473 474 if (mClientPid != getCallingPid()) { 475 ALOGE("%s: Camera %d: Lock call from pid %d; currently locked to pid %d", 476 __FUNCTION__, mCameraId, getCallingPid(), mClientPid); 477 return EBUSY; 478 } 479 480 return OK; 481 } 482 483 status_t Camera2Client::unlock() { 484 ATRACE_CALL(); 485 ALOGV("%s: E", __FUNCTION__); 486 Mutex::Autolock icl(mBinderSerializationLock); 487 ALOGV("%s: Camera %d: Unlock call from pid %d; current client pid %d", 488 __FUNCTION__, mCameraId, getCallingPid(), mClientPid); 489 490 if (mClientPid == getCallingPid()) { 491 SharedParameters::Lock l(mParameters); 492 if (l.mParameters.state == Parameters::RECORD || 493 l.mParameters.state == Parameters::VIDEO_SNAPSHOT) { 494 ALOGD("Not allowed to unlock camera during recording."); 495 return INVALID_OPERATION; 496 } 497 mClientPid = 0; 498 mRemoteCallback.clear(); 499 mSharedCameraCallbacks.clear(); 500 return OK; 501 } 502 503 ALOGE("%s: Camera %d: Unlock call from pid %d; currently locked to pid %d", 504 __FUNCTION__, mCameraId, getCallingPid(), mClientPid); 505 return EBUSY; 506 } 507 508 status_t Camera2Client::setPreviewDisplay( 509 const sp<Surface>& surface) { 510 ATRACE_CALL(); 511 ALOGV("%s: E", __FUNCTION__); 512 Mutex::Autolock icl(mBinderSerializationLock); 513 status_t res; 514 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 515 516 sp<IBinder> binder; 517 sp<ANativeWindow> window; 518 if (surface != 0) { 519 binder = surface->getIGraphicBufferProducer()->asBinder(); 520 window = surface; 521 } 522 523 return setPreviewWindowL(binder,window); 524 } 525 526 status_t Camera2Client::setPreviewTexture( 527 const sp<IGraphicBufferProducer>& bufferProducer) { 528 ATRACE_CALL(); 529 ALOGV("%s: E", __FUNCTION__); 530 Mutex::Autolock icl(mBinderSerializationLock); 531 status_t res; 532 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 533 534 sp<IBinder> binder; 535 sp<ANativeWindow> window; 536 if (bufferProducer != 0) { 537 binder = bufferProducer->asBinder(); 538 window = new Surface(bufferProducer); 539 } 540 return setPreviewWindowL(binder, window); 541 } 542 543 status_t Camera2Client::setPreviewWindowL(const sp<IBinder>& binder, 544 sp<ANativeWindow> window) { 545 ATRACE_CALL(); 546 status_t res; 547 548 if (binder == mPreviewSurface) { 549 ALOGV("%s: Camera %d: New window is same as old window", 550 __FUNCTION__, mCameraId); 551 return NO_ERROR; 552 } 553 554 Parameters::State state; 555 { 556 SharedParameters::Lock l(mParameters); 557 state = l.mParameters.state; 558 } 559 switch (state) { 560 case Parameters::DISCONNECTED: 561 case Parameters::RECORD: 562 case Parameters::STILL_CAPTURE: 563 case Parameters::VIDEO_SNAPSHOT: 564 ALOGE("%s: Camera %d: Cannot set preview display while in state %s", 565 __FUNCTION__, mCameraId, 566 Parameters::getStateName(state)); 567 return INVALID_OPERATION; 568 case Parameters::STOPPED: 569 case Parameters::WAITING_FOR_PREVIEW_WINDOW: 570 // OK 571 break; 572 case Parameters::PREVIEW: 573 // Already running preview - need to stop and create a new stream 574 res = stopStream(); 575 if (res != OK) { 576 ALOGE("%s: Unable to stop preview to swap windows: %s (%d)", 577 __FUNCTION__, strerror(-res), res); 578 return res; 579 } 580 state = Parameters::WAITING_FOR_PREVIEW_WINDOW; 581 break; 582 } 583 584 mPreviewSurface = binder; 585 res = mStreamingProcessor->setPreviewWindow(window); 586 if (res != OK) { 587 ALOGE("%s: Unable to set new preview window: %s (%d)", 588 __FUNCTION__, strerror(-res), res); 589 return res; 590 } 591 592 if (state == Parameters::WAITING_FOR_PREVIEW_WINDOW) { 593 SharedParameters::Lock l(mParameters); 594 l.mParameters.state = state; 595 return startPreviewL(l.mParameters, false); 596 } 597 598 return OK; 599 } 600 601 void Camera2Client::setPreviewCallbackFlag(int flag) { 602 ATRACE_CALL(); 603 ALOGV("%s: Camera %d: Flag 0x%x", __FUNCTION__, mCameraId, flag); 604 Mutex::Autolock icl(mBinderSerializationLock); 605 606 if ( checkPid(__FUNCTION__) != OK) return; 607 608 SharedParameters::Lock l(mParameters); 609 setPreviewCallbackFlagL(l.mParameters, flag); 610 } 611 612 void Camera2Client::setPreviewCallbackFlagL(Parameters ¶ms, int flag) { 613 status_t res = OK; 614 615 switch(params.state) { 616 case Parameters::STOPPED: 617 case Parameters::WAITING_FOR_PREVIEW_WINDOW: 618 case Parameters::PREVIEW: 619 case Parameters::STILL_CAPTURE: 620 // OK 621 break; 622 default: 623 if (flag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) { 624 ALOGE("%s: Camera %d: Can't use preview callbacks " 625 "in state %d", __FUNCTION__, mCameraId, params.state); 626 return; 627 } 628 } 629 630 if (flag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK) { 631 ALOGV("%s: setting oneshot", __FUNCTION__); 632 params.previewCallbackOneShot = true; 633 } 634 if (params.previewCallbackFlags != (uint32_t)flag) { 635 params.previewCallbackFlags = flag; 636 637 if (params.state == Parameters::PREVIEW) { 638 res = startPreviewL(params, true); 639 if (res != OK) { 640 ALOGE("%s: Camera %d: Unable to refresh request in state %s", 641 __FUNCTION__, mCameraId, 642 Parameters::getStateName(params.state)); 643 } 644 } 645 } 646 647 } 648 649 status_t Camera2Client::startPreview() { 650 ATRACE_CALL(); 651 ALOGV("%s: E", __FUNCTION__); 652 Mutex::Autolock icl(mBinderSerializationLock); 653 status_t res; 654 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 655 SharedParameters::Lock l(mParameters); 656 return startPreviewL(l.mParameters, false); 657 } 658 659 status_t Camera2Client::startPreviewL(Parameters ¶ms, bool restart) { 660 ATRACE_CALL(); 661 status_t res; 662 663 ALOGV("%s: state == %d, restart = %d", __FUNCTION__, params.state, restart); 664 665 if ( (params.state == Parameters::PREVIEW || 666 params.state == Parameters::RECORD || 667 params.state == Parameters::VIDEO_SNAPSHOT) 668 && !restart) { 669 // Succeed attempt to re-enter a streaming state 670 ALOGI("%s: Camera %d: Preview already active, ignoring restart", 671 __FUNCTION__, mCameraId); 672 return OK; 673 } 674 if (params.state > Parameters::PREVIEW && !restart) { 675 ALOGE("%s: Can't start preview in state %s", 676 __FUNCTION__, 677 Parameters::getStateName(params.state)); 678 return INVALID_OPERATION; 679 } 680 681 if (!mStreamingProcessor->haveValidPreviewWindow()) { 682 params.state = Parameters::WAITING_FOR_PREVIEW_WINDOW; 683 return OK; 684 } 685 params.state = Parameters::STOPPED; 686 687 res = mStreamingProcessor->updatePreviewStream(params); 688 if (res != OK) { 689 ALOGE("%s: Camera %d: Unable to update preview stream: %s (%d)", 690 __FUNCTION__, mCameraId, strerror(-res), res); 691 return res; 692 } 693 694 // We could wait to create the JPEG output stream until first actual use 695 // (first takePicture call). However, this would substantially increase the 696 // first capture latency on HAL3 devices, and potentially on some HAL2 697 // devices. So create it unconditionally at preview start. As a drawback, 698 // this increases gralloc memory consumption for applications that don't 699 // ever take a picture. 700 // TODO: Find a better compromise, though this likely would involve HAL 701 // changes. 702 res = updateProcessorStream(mJpegProcessor, params); 703 if (res != OK) { 704 ALOGE("%s: Camera %d: Can't pre-configure still image " 705 "stream: %s (%d)", 706 __FUNCTION__, mCameraId, strerror(-res), res); 707 return res; 708 } 709 710 Vector<uint8_t> outputStreams; 711 bool callbacksEnabled = params.previewCallbackFlags & 712 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK; 713 if (callbacksEnabled) { 714 // Can't have recording stream hanging around when enabling callbacks, 715 // since it exceeds the max stream count on some devices. 716 if (mStreamingProcessor->getRecordingStreamId() != NO_STREAM) { 717 ALOGV("%s: Camera %d: Clearing out recording stream before " 718 "creating callback stream", __FUNCTION__, mCameraId); 719 res = mStreamingProcessor->stopStream(); 720 if (res != OK) { 721 ALOGE("%s: Camera %d: Can't stop streaming to delete " 722 "recording stream", __FUNCTION__, mCameraId); 723 return res; 724 } 725 res = mStreamingProcessor->deleteRecordingStream(); 726 if (res != OK) { 727 ALOGE("%s: Camera %d: Unable to delete recording stream before " 728 "enabling callbacks: %s (%d)", __FUNCTION__, mCameraId, 729 strerror(-res), res); 730 return res; 731 } 732 } 733 734 res = mCallbackProcessor->updateStream(params); 735 if (res != OK) { 736 ALOGE("%s: Camera %d: Unable to update callback stream: %s (%d)", 737 __FUNCTION__, mCameraId, strerror(-res), res); 738 return res; 739 } 740 outputStreams.push(getCallbackStreamId()); 741 } 742 if (params.zslMode && !params.recordingHint) { 743 res = updateProcessorStream(mZslProcessor, params); 744 if (res != OK) { 745 ALOGE("%s: Camera %d: Unable to update ZSL stream: %s (%d)", 746 __FUNCTION__, mCameraId, strerror(-res), res); 747 return res; 748 } 749 outputStreams.push(getZslStreamId()); 750 } 751 752 outputStreams.push(getPreviewStreamId()); 753 754 if (!params.recordingHint) { 755 if (!restart) { 756 res = mStreamingProcessor->updatePreviewRequest(params); 757 if (res != OK) { 758 ALOGE("%s: Camera %d: Can't set up preview request: " 759 "%s (%d)", __FUNCTION__, mCameraId, 760 strerror(-res), res); 761 return res; 762 } 763 } 764 res = mStreamingProcessor->startStream(StreamingProcessor::PREVIEW, 765 outputStreams); 766 } else { 767 if (!restart) { 768 res = mStreamingProcessor->updateRecordingRequest(params); 769 if (res != OK) { 770 ALOGE("%s: Camera %d: Can't set up preview request with " 771 "record hint: %s (%d)", __FUNCTION__, mCameraId, 772 strerror(-res), res); 773 return res; 774 } 775 } 776 res = mStreamingProcessor->startStream(StreamingProcessor::RECORD, 777 outputStreams); 778 } 779 if (res != OK) { 780 ALOGE("%s: Camera %d: Unable to start streaming preview: %s (%d)", 781 __FUNCTION__, mCameraId, strerror(-res), res); 782 return res; 783 } 784 785 params.state = Parameters::PREVIEW; 786 return OK; 787 } 788 789 void Camera2Client::stopPreview() { 790 ATRACE_CALL(); 791 ALOGV("%s: E", __FUNCTION__); 792 Mutex::Autolock icl(mBinderSerializationLock); 793 status_t res; 794 if ( (res = checkPid(__FUNCTION__) ) != OK) return; 795 stopPreviewL(); 796 } 797 798 void Camera2Client::stopPreviewL() { 799 ATRACE_CALL(); 800 status_t res; 801 const nsecs_t kStopCaptureTimeout = 3000000000LL; // 3 seconds 802 Parameters::State state; 803 { 804 SharedParameters::Lock l(mParameters); 805 state = l.mParameters.state; 806 } 807 808 switch (state) { 809 case Parameters::DISCONNECTED: 810 // Nothing to do. 811 break; 812 case Parameters::STOPPED: 813 case Parameters::VIDEO_SNAPSHOT: 814 case Parameters::STILL_CAPTURE: 815 mCaptureSequencer->waitUntilIdle(kStopCaptureTimeout); 816 // no break 817 case Parameters::RECORD: 818 case Parameters::PREVIEW: 819 res = stopStream(); 820 if (res != OK) { 821 ALOGE("%s: Camera %d: Can't stop streaming: %s (%d)", 822 __FUNCTION__, mCameraId, strerror(-res), res); 823 } 824 res = mDevice->waitUntilDrained(); 825 if (res != OK) { 826 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)", 827 __FUNCTION__, mCameraId, strerror(-res), res); 828 } 829 // no break 830 case Parameters::WAITING_FOR_PREVIEW_WINDOW: { 831 SharedParameters::Lock l(mParameters); 832 l.mParameters.state = Parameters::STOPPED; 833 commandStopFaceDetectionL(l.mParameters); 834 break; 835 } 836 default: 837 ALOGE("%s: Camera %d: Unknown state %d", __FUNCTION__, mCameraId, 838 state); 839 } 840 } 841 842 bool Camera2Client::previewEnabled() { 843 ATRACE_CALL(); 844 Mutex::Autolock icl(mBinderSerializationLock); 845 status_t res; 846 if ( (res = checkPid(__FUNCTION__) ) != OK) return false; 847 848 SharedParameters::Lock l(mParameters); 849 return l.mParameters.state == Parameters::PREVIEW; 850 } 851 852 status_t Camera2Client::storeMetaDataInBuffers(bool enabled) { 853 ATRACE_CALL(); 854 Mutex::Autolock icl(mBinderSerializationLock); 855 status_t res; 856 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 857 858 SharedParameters::Lock l(mParameters); 859 switch (l.mParameters.state) { 860 case Parameters::RECORD: 861 case Parameters::VIDEO_SNAPSHOT: 862 ALOGE("%s: Camera %d: Can't be called in state %s", 863 __FUNCTION__, mCameraId, 864 Parameters::getStateName(l.mParameters.state)); 865 return INVALID_OPERATION; 866 default: 867 // OK 868 break; 869 } 870 871 l.mParameters.storeMetadataInBuffers = enabled; 872 873 return OK; 874 } 875 876 status_t Camera2Client::startRecording() { 877 ATRACE_CALL(); 878 ALOGV("%s: E", __FUNCTION__); 879 Mutex::Autolock icl(mBinderSerializationLock); 880 status_t res; 881 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 882 SharedParameters::Lock l(mParameters); 883 884 return startRecordingL(l.mParameters, false); 885 } 886 887 status_t Camera2Client::startRecordingL(Parameters ¶ms, bool restart) { 888 status_t res; 889 890 ALOGV("%s: state == %d, restart = %d", __FUNCTION__, params.state, restart); 891 892 switch (params.state) { 893 case Parameters::STOPPED: 894 res = startPreviewL(params, false); 895 if (res != OK) return res; 896 break; 897 case Parameters::PREVIEW: 898 // Ready to go 899 break; 900 case Parameters::RECORD: 901 case Parameters::VIDEO_SNAPSHOT: 902 // OK to call this when recording is already on, just skip unless 903 // we're looking to restart 904 if (!restart) return OK; 905 break; 906 default: 907 ALOGE("%s: Camera %d: Can't start recording in state %s", 908 __FUNCTION__, mCameraId, 909 Parameters::getStateName(params.state)); 910 return INVALID_OPERATION; 911 }; 912 913 if (!params.storeMetadataInBuffers) { 914 ALOGE("%s: Camera %d: Recording only supported in metadata mode, but " 915 "non-metadata recording mode requested!", __FUNCTION__, 916 mCameraId); 917 return INVALID_OPERATION; 918 } 919 920 if (!restart) { 921 mCameraService->playSound(CameraService::SOUND_RECORDING); 922 mStreamingProcessor->updateRecordingRequest(params); 923 if (res != OK) { 924 ALOGE("%s: Camera %d: Unable to update recording request: %s (%d)", 925 __FUNCTION__, mCameraId, strerror(-res), res); 926 return res; 927 } 928 } 929 930 // Not all devices can support a preview callback stream and a recording 931 // stream at the same time, so assume none of them can. 932 if (mCallbackProcessor->getStreamId() != NO_STREAM) { 933 ALOGV("%s: Camera %d: Clearing out callback stream before " 934 "creating recording stream", __FUNCTION__, mCameraId); 935 res = mStreamingProcessor->stopStream(); 936 if (res != OK) { 937 ALOGE("%s: Camera %d: Can't stop streaming to delete callback stream", 938 __FUNCTION__, mCameraId); 939 return res; 940 } 941 res = mCallbackProcessor->deleteStream(); 942 if (res != OK) { 943 ALOGE("%s: Camera %d: Unable to delete callback stream before " 944 "record: %s (%d)", __FUNCTION__, mCameraId, 945 strerror(-res), res); 946 return res; 947 } 948 } 949 // Disable callbacks if they're enabled; can't record and use callbacks, 950 // and we can't fail record start without stagefright asserting. 951 params.previewCallbackFlags = 0; 952 953 res = updateProcessorStream< 954 StreamingProcessor, 955 &StreamingProcessor::updateRecordingStream>(mStreamingProcessor, 956 params); 957 if (res != OK) { 958 ALOGE("%s: Camera %d: Unable to update recording stream: %s (%d)", 959 __FUNCTION__, mCameraId, strerror(-res), res); 960 return res; 961 } 962 963 Vector<uint8_t> outputStreams; 964 outputStreams.push(getPreviewStreamId()); 965 outputStreams.push(getRecordingStreamId()); 966 967 res = mStreamingProcessor->startStream(StreamingProcessor::RECORD, 968 outputStreams); 969 if (res != OK) { 970 ALOGE("%s: Camera %d: Unable to start recording stream: %s (%d)", 971 __FUNCTION__, mCameraId, strerror(-res), res); 972 return res; 973 } 974 975 if (params.state < Parameters::RECORD) { 976 params.state = Parameters::RECORD; 977 } 978 979 return OK; 980 } 981 982 void Camera2Client::stopRecording() { 983 ATRACE_CALL(); 984 ALOGV("%s: E", __FUNCTION__); 985 Mutex::Autolock icl(mBinderSerializationLock); 986 SharedParameters::Lock l(mParameters); 987 988 status_t res; 989 if ( (res = checkPid(__FUNCTION__) ) != OK) return; 990 991 switch (l.mParameters.state) { 992 case Parameters::RECORD: 993 // OK to stop 994 break; 995 case Parameters::STOPPED: 996 case Parameters::PREVIEW: 997 case Parameters::STILL_CAPTURE: 998 case Parameters::VIDEO_SNAPSHOT: 999 default: 1000 ALOGE("%s: Camera %d: Can't stop recording in state %s", 1001 __FUNCTION__, mCameraId, 1002 Parameters::getStateName(l.mParameters.state)); 1003 return; 1004 }; 1005 1006 mCameraService->playSound(CameraService::SOUND_RECORDING); 1007 1008 res = startPreviewL(l.mParameters, true); 1009 if (res != OK) { 1010 ALOGE("%s: Camera %d: Unable to return to preview", 1011 __FUNCTION__, mCameraId); 1012 } 1013 } 1014 1015 bool Camera2Client::recordingEnabled() { 1016 ATRACE_CALL(); 1017 Mutex::Autolock icl(mBinderSerializationLock); 1018 1019 if ( checkPid(__FUNCTION__) != OK) return false; 1020 1021 return recordingEnabledL(); 1022 } 1023 1024 bool Camera2Client::recordingEnabledL() { 1025 ATRACE_CALL(); 1026 SharedParameters::Lock l(mParameters); 1027 1028 return (l.mParameters.state == Parameters::RECORD 1029 || l.mParameters.state == Parameters::VIDEO_SNAPSHOT); 1030 } 1031 1032 void Camera2Client::releaseRecordingFrame(const sp<IMemory>& mem) { 1033 ATRACE_CALL(); 1034 Mutex::Autolock icl(mBinderSerializationLock); 1035 if ( checkPid(__FUNCTION__) != OK) return; 1036 1037 mStreamingProcessor->releaseRecordingFrame(mem); 1038 } 1039 1040 status_t Camera2Client::autoFocus() { 1041 ATRACE_CALL(); 1042 Mutex::Autolock icl(mBinderSerializationLock); 1043 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId); 1044 status_t res; 1045 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 1046 1047 int triggerId; 1048 bool notifyImmediately = false; 1049 bool notifySuccess = false; 1050 { 1051 SharedParameters::Lock l(mParameters); 1052 if (l.mParameters.state < Parameters::PREVIEW) { 1053 return INVALID_OPERATION; 1054 } 1055 1056 /** 1057 * If the camera does not support auto-focus, it is a no-op and 1058 * onAutoFocus(boolean, Camera) callback will be called immediately 1059 * with a fake value of success set to true. 1060 * 1061 * Similarly, if focus mode is set to INFINITY, there's no reason to 1062 * bother the HAL. 1063 */ 1064 if (l.mParameters.focusMode == Parameters::FOCUS_MODE_FIXED || 1065 l.mParameters.focusMode == Parameters::FOCUS_MODE_INFINITY) { 1066 notifyImmediately = true; 1067 notifySuccess = true; 1068 } 1069 /** 1070 * If we're in CAF mode, and AF has already been locked, just fire back 1071 * the callback right away; the HAL would not send a notification since 1072 * no state change would happen on a AF trigger. 1073 */ 1074 if ( (l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_PICTURE || 1075 l.mParameters.focusMode == Parameters::FOCUS_MODE_CONTINUOUS_VIDEO) && 1076 l.mParameters.focusState == ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED ) { 1077 notifyImmediately = true; 1078 notifySuccess = true; 1079 } 1080 /** 1081 * Send immediate notification back to client 1082 */ 1083 if (notifyImmediately) { 1084 SharedCameraCallbacks::Lock l(mSharedCameraCallbacks); 1085 if (l.mRemoteCallback != 0) { 1086 l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS, 1087 notifySuccess ? 1 : 0, 0); 1088 } 1089 return OK; 1090 } 1091 /** 1092 * Handle quirk mode for AF in scene modes 1093 */ 1094 if (l.mParameters.quirks.triggerAfWithAuto && 1095 l.mParameters.sceneMode != ANDROID_CONTROL_SCENE_MODE_UNSUPPORTED && 1096 l.mParameters.focusMode != Parameters::FOCUS_MODE_AUTO && 1097 !l.mParameters.focusingAreas[0].isEmpty()) { 1098 ALOGV("%s: Quirk: Switching from focusMode %d to AUTO", 1099 __FUNCTION__, l.mParameters.focusMode); 1100 l.mParameters.shadowFocusMode = l.mParameters.focusMode; 1101 l.mParameters.focusMode = Parameters::FOCUS_MODE_AUTO; 1102 updateRequests(l.mParameters); 1103 } 1104 1105 l.mParameters.currentAfTriggerId = ++l.mParameters.afTriggerCounter; 1106 triggerId = l.mParameters.currentAfTriggerId; 1107 } 1108 syncWithDevice(); 1109 1110 mDevice->triggerAutofocus(triggerId); 1111 1112 return OK; 1113 } 1114 1115 status_t Camera2Client::cancelAutoFocus() { 1116 ATRACE_CALL(); 1117 Mutex::Autolock icl(mBinderSerializationLock); 1118 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId); 1119 status_t res; 1120 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 1121 1122 int triggerId; 1123 { 1124 SharedParameters::Lock l(mParameters); 1125 // Canceling does nothing in FIXED or INFINITY modes 1126 if (l.mParameters.focusMode == Parameters::FOCUS_MODE_FIXED || 1127 l.mParameters.focusMode == Parameters::FOCUS_MODE_INFINITY) { 1128 return OK; 1129 } 1130 triggerId = ++l.mParameters.afTriggerCounter; 1131 1132 // When using triggerAfWithAuto quirk, may need to reset focus mode to 1133 // the real state at this point. No need to cancel explicitly if 1134 // changing the AF mode. 1135 if (l.mParameters.shadowFocusMode != Parameters::FOCUS_MODE_INVALID) { 1136 ALOGV("%s: Quirk: Restoring focus mode to %d", __FUNCTION__, 1137 l.mParameters.shadowFocusMode); 1138 l.mParameters.focusMode = l.mParameters.shadowFocusMode; 1139 l.mParameters.shadowFocusMode = Parameters::FOCUS_MODE_INVALID; 1140 updateRequests(l.mParameters); 1141 1142 return OK; 1143 } 1144 } 1145 syncWithDevice(); 1146 1147 mDevice->triggerCancelAutofocus(triggerId); 1148 1149 return OK; 1150 } 1151 1152 status_t Camera2Client::takePicture(int msgType) { 1153 ATRACE_CALL(); 1154 Mutex::Autolock icl(mBinderSerializationLock); 1155 status_t res; 1156 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 1157 1158 { 1159 SharedParameters::Lock l(mParameters); 1160 switch (l.mParameters.state) { 1161 case Parameters::DISCONNECTED: 1162 case Parameters::STOPPED: 1163 case Parameters::WAITING_FOR_PREVIEW_WINDOW: 1164 ALOGE("%s: Camera %d: Cannot take picture without preview enabled", 1165 __FUNCTION__, mCameraId); 1166 return INVALID_OPERATION; 1167 case Parameters::PREVIEW: 1168 // Good to go for takePicture 1169 res = commandStopFaceDetectionL(l.mParameters); 1170 if (res != OK) { 1171 ALOGE("%s: Camera %d: Unable to stop face detection for still capture", 1172 __FUNCTION__, mCameraId); 1173 return res; 1174 } 1175 l.mParameters.state = Parameters::STILL_CAPTURE; 1176 break; 1177 case Parameters::RECORD: 1178 // Good to go for video snapshot 1179 l.mParameters.state = Parameters::VIDEO_SNAPSHOT; 1180 break; 1181 case Parameters::STILL_CAPTURE: 1182 case Parameters::VIDEO_SNAPSHOT: 1183 ALOGE("%s: Camera %d: Already taking a picture", 1184 __FUNCTION__, mCameraId); 1185 return INVALID_OPERATION; 1186 } 1187 1188 ALOGV("%s: Camera %d: Starting picture capture", __FUNCTION__, mCameraId); 1189 1190 res = updateProcessorStream(mJpegProcessor, l.mParameters); 1191 if (res != OK) { 1192 ALOGE("%s: Camera %d: Can't set up still image stream: %s (%d)", 1193 __FUNCTION__, mCameraId, strerror(-res), res); 1194 return res; 1195 } 1196 } 1197 1198 // Need HAL to have correct settings before (possibly) triggering precapture 1199 syncWithDevice(); 1200 1201 res = mCaptureSequencer->startCapture(msgType); 1202 if (res != OK) { 1203 ALOGE("%s: Camera %d: Unable to start capture: %s (%d)", 1204 __FUNCTION__, mCameraId, strerror(-res), res); 1205 } 1206 1207 return res; 1208 } 1209 1210 status_t Camera2Client::setParameters(const String8& params) { 1211 ATRACE_CALL(); 1212 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId); 1213 Mutex::Autolock icl(mBinderSerializationLock); 1214 status_t res; 1215 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 1216 1217 SharedParameters::Lock l(mParameters); 1218 1219 res = l.mParameters.set(params); 1220 if (res != OK) return res; 1221 1222 res = updateRequests(l.mParameters); 1223 1224 return res; 1225 } 1226 1227 String8 Camera2Client::getParameters() const { 1228 ATRACE_CALL(); 1229 ALOGV("%s: Camera %d", __FUNCTION__, mCameraId); 1230 Mutex::Autolock icl(mBinderSerializationLock); 1231 if ( checkPid(__FUNCTION__) != OK) return String8(); 1232 1233 SharedParameters::ReadLock l(mParameters); 1234 1235 return l.mParameters.get(); 1236 } 1237 1238 status_t Camera2Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) { 1239 ATRACE_CALL(); 1240 Mutex::Autolock icl(mBinderSerializationLock); 1241 status_t res; 1242 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 1243 1244 ALOGV("%s: Camera %d: Command %d (%d, %d)", __FUNCTION__, mCameraId, 1245 cmd, arg1, arg2); 1246 1247 switch (cmd) { 1248 case CAMERA_CMD_START_SMOOTH_ZOOM: 1249 return commandStartSmoothZoomL(); 1250 case CAMERA_CMD_STOP_SMOOTH_ZOOM: 1251 return commandStopSmoothZoomL(); 1252 case CAMERA_CMD_SET_DISPLAY_ORIENTATION: 1253 return commandSetDisplayOrientationL(arg1); 1254 case CAMERA_CMD_ENABLE_SHUTTER_SOUND: 1255 return commandEnableShutterSoundL(arg1 == 1); 1256 case CAMERA_CMD_PLAY_RECORDING_SOUND: 1257 return commandPlayRecordingSoundL(); 1258 case CAMERA_CMD_START_FACE_DETECTION: 1259 return commandStartFaceDetectionL(arg1); 1260 case CAMERA_CMD_STOP_FACE_DETECTION: { 1261 SharedParameters::Lock l(mParameters); 1262 return commandStopFaceDetectionL(l.mParameters); 1263 } 1264 case CAMERA_CMD_ENABLE_FOCUS_MOVE_MSG: 1265 return commandEnableFocusMoveMsgL(arg1 == 1); 1266 case CAMERA_CMD_PING: 1267 return commandPingL(); 1268 case CAMERA_CMD_SET_VIDEO_BUFFER_COUNT: 1269 return commandSetVideoBufferCountL(arg1); 1270 default: 1271 ALOGE("%s: Unknown command %d (arguments %d, %d)", 1272 __FUNCTION__, cmd, arg1, arg2); 1273 return BAD_VALUE; 1274 } 1275 } 1276 1277 status_t Camera2Client::commandStartSmoothZoomL() { 1278 ALOGE("%s: Unimplemented!", __FUNCTION__); 1279 return OK; 1280 } 1281 1282 status_t Camera2Client::commandStopSmoothZoomL() { 1283 ALOGE("%s: Unimplemented!", __FUNCTION__); 1284 return OK; 1285 } 1286 1287 status_t Camera2Client::commandSetDisplayOrientationL(int degrees) { 1288 int transform = Parameters::degToTransform(degrees, 1289 mCameraFacing == CAMERA_FACING_FRONT); 1290 if (transform == -1) { 1291 ALOGE("%s: Camera %d: Error setting %d as display orientation value", 1292 __FUNCTION__, mCameraId, degrees); 1293 return BAD_VALUE; 1294 } 1295 SharedParameters::Lock l(mParameters); 1296 if (transform != l.mParameters.previewTransform && 1297 getPreviewStreamId() != NO_STREAM) { 1298 mDevice->setStreamTransform(getPreviewStreamId(), transform); 1299 } 1300 l.mParameters.previewTransform = transform; 1301 return OK; 1302 } 1303 1304 status_t Camera2Client::commandEnableShutterSoundL(bool enable) { 1305 SharedParameters::Lock l(mParameters); 1306 if (enable) { 1307 l.mParameters.playShutterSound = true; 1308 return OK; 1309 } 1310 1311 // Disabling shutter sound may not be allowed. In that case only 1312 // allow the mediaserver process to disable the sound. 1313 char value[PROPERTY_VALUE_MAX]; 1314 property_get("ro.camera.sound.forced", value, "0"); 1315 if (strncmp(value, "0", 2) != 0) { 1316 // Disabling shutter sound is not allowed. Deny if the current 1317 // process is not mediaserver. 1318 if (getCallingPid() != getpid()) { 1319 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", 1320 getCallingPid()); 1321 return PERMISSION_DENIED; 1322 } 1323 } 1324 1325 l.mParameters.playShutterSound = false; 1326 return OK; 1327 } 1328 1329 status_t Camera2Client::commandPlayRecordingSoundL() { 1330 mCameraService->playSound(CameraService::SOUND_RECORDING); 1331 return OK; 1332 } 1333 1334 status_t Camera2Client::commandStartFaceDetectionL(int /*type*/) { 1335 ALOGV("%s: Camera %d: Starting face detection", 1336 __FUNCTION__, mCameraId); 1337 status_t res; 1338 SharedParameters::Lock l(mParameters); 1339 switch (l.mParameters.state) { 1340 case Parameters::DISCONNECTED: 1341 case Parameters::STOPPED: 1342 case Parameters::WAITING_FOR_PREVIEW_WINDOW: 1343 case Parameters::STILL_CAPTURE: 1344 ALOGE("%s: Camera %d: Cannot start face detection without preview active", 1345 __FUNCTION__, mCameraId); 1346 return INVALID_OPERATION; 1347 case Parameters::PREVIEW: 1348 case Parameters::RECORD: 1349 case Parameters::VIDEO_SNAPSHOT: 1350 // Good to go for starting face detect 1351 break; 1352 } 1353 // Ignoring type 1354 if (l.mParameters.fastInfo.bestFaceDetectMode == 1355 ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) { 1356 ALOGE("%s: Camera %d: Face detection not supported", 1357 __FUNCTION__, mCameraId); 1358 return BAD_VALUE; 1359 } 1360 if (l.mParameters.enableFaceDetect) return OK; 1361 1362 l.mParameters.enableFaceDetect = true; 1363 1364 res = updateRequests(l.mParameters); 1365 1366 return res; 1367 } 1368 1369 status_t Camera2Client::commandStopFaceDetectionL(Parameters ¶ms) { 1370 status_t res = OK; 1371 ALOGV("%s: Camera %d: Stopping face detection", 1372 __FUNCTION__, mCameraId); 1373 1374 if (!params.enableFaceDetect) return OK; 1375 1376 params.enableFaceDetect = false; 1377 1378 if (params.state == Parameters::PREVIEW 1379 || params.state == Parameters::RECORD 1380 || params.state == Parameters::VIDEO_SNAPSHOT) { 1381 res = updateRequests(params); 1382 } 1383 1384 return res; 1385 } 1386 1387 status_t Camera2Client::commandEnableFocusMoveMsgL(bool enable) { 1388 SharedParameters::Lock l(mParameters); 1389 l.mParameters.enableFocusMoveMessages = enable; 1390 1391 return OK; 1392 } 1393 1394 status_t Camera2Client::commandPingL() { 1395 // Always ping back if access is proper and device is alive 1396 SharedParameters::Lock l(mParameters); 1397 if (l.mParameters.state != Parameters::DISCONNECTED) { 1398 return OK; 1399 } else { 1400 return NO_INIT; 1401 } 1402 } 1403 1404 status_t Camera2Client::commandSetVideoBufferCountL(size_t count) { 1405 if (recordingEnabledL()) { 1406 ALOGE("%s: Camera %d: Error setting video buffer count after " 1407 "recording was started", __FUNCTION__, mCameraId); 1408 return INVALID_OPERATION; 1409 } 1410 1411 return mStreamingProcessor->setRecordingBufferCount(count); 1412 } 1413 1414 /** Device-related methods */ 1415 void Camera2Client::notifyAutoFocus(uint8_t newState, int triggerId) { 1416 ALOGV("%s: Autofocus state now %d, last trigger %d", 1417 __FUNCTION__, newState, triggerId); 1418 bool sendCompletedMessage = false; 1419 bool sendMovingMessage = false; 1420 1421 bool success = false; 1422 bool afInMotion = false; 1423 { 1424 SharedParameters::Lock l(mParameters); 1425 l.mParameters.focusState = newState; 1426 switch (l.mParameters.focusMode) { 1427 case Parameters::FOCUS_MODE_AUTO: 1428 case Parameters::FOCUS_MODE_MACRO: 1429 // Don't send notifications upstream if they're not for the current AF 1430 // trigger. For example, if cancel was called in between, or if we 1431 // already sent a notification about this AF call. 1432 if (triggerId != l.mParameters.currentAfTriggerId) break; 1433 switch (newState) { 1434 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED: 1435 success = true; 1436 // no break 1437 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED: 1438 sendCompletedMessage = true; 1439 l.mParameters.currentAfTriggerId = -1; 1440 break; 1441 case ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN: 1442 // Just starting focusing, ignore 1443 break; 1444 case ANDROID_CONTROL_AF_STATE_INACTIVE: 1445 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN: 1446 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED: 1447 default: 1448 // Unexpected in AUTO/MACRO mode 1449 ALOGE("%s: Unexpected AF state transition in AUTO/MACRO mode: %d", 1450 __FUNCTION__, newState); 1451 break; 1452 } 1453 break; 1454 case Parameters::FOCUS_MODE_CONTINUOUS_VIDEO: 1455 case Parameters::FOCUS_MODE_CONTINUOUS_PICTURE: 1456 switch (newState) { 1457 case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED: 1458 success = true; 1459 // no break 1460 case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED: 1461 // Don't send notifications upstream if they're not for 1462 // the current AF trigger. For example, if cancel was 1463 // called in between, or if we already sent a 1464 // notification about this AF call. 1465 // Send both a 'AF done' callback and a 'AF move' callback 1466 if (triggerId != l.mParameters.currentAfTriggerId) break; 1467 sendCompletedMessage = true; 1468 afInMotion = false; 1469 if (l.mParameters.enableFocusMoveMessages && 1470 l.mParameters.afInMotion) { 1471 sendMovingMessage = true; 1472 } 1473 l.mParameters.currentAfTriggerId = -1; 1474 break; 1475 case ANDROID_CONTROL_AF_STATE_INACTIVE: 1476 // Cancel was called, or we switched state; care if 1477 // currently moving 1478 afInMotion = false; 1479 if (l.mParameters.enableFocusMoveMessages && 1480 l.mParameters.afInMotion) { 1481 sendMovingMessage = true; 1482 } 1483 break; 1484 case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN: 1485 // Start passive scan, inform upstream 1486 afInMotion = true; 1487 // no break 1488 case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED: 1489 // Stop passive scan, inform upstream 1490 if (l.mParameters.enableFocusMoveMessages) { 1491 sendMovingMessage = true; 1492 } 1493 break; 1494 } 1495 l.mParameters.afInMotion = afInMotion; 1496 break; 1497 case Parameters::FOCUS_MODE_EDOF: 1498 case Parameters::FOCUS_MODE_INFINITY: 1499 case Parameters::FOCUS_MODE_FIXED: 1500 default: 1501 if (newState != ANDROID_CONTROL_AF_STATE_INACTIVE) { 1502 ALOGE("%s: Unexpected AF state change %d " 1503 "(ID %d) in focus mode %d", 1504 __FUNCTION__, newState, triggerId, 1505 l.mParameters.focusMode); 1506 } 1507 } 1508 } 1509 if (sendMovingMessage) { 1510 SharedCameraCallbacks::Lock l(mSharedCameraCallbacks); 1511 if (l.mRemoteCallback != 0) { 1512 l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS_MOVE, 1513 afInMotion ? 1 : 0, 0); 1514 } 1515 } 1516 if (sendCompletedMessage) { 1517 SharedCameraCallbacks::Lock l(mSharedCameraCallbacks); 1518 if (l.mRemoteCallback != 0) { 1519 l.mRemoteCallback->notifyCallback(CAMERA_MSG_FOCUS, 1520 success ? 1 : 0, 0); 1521 } 1522 } 1523 } 1524 1525 void Camera2Client::notifyAutoExposure(uint8_t newState, int triggerId) { 1526 ALOGV("%s: Autoexposure state now %d, last trigger %d", 1527 __FUNCTION__, newState, triggerId); 1528 mCaptureSequencer->notifyAutoExposure(newState, triggerId); 1529 } 1530 1531 camera2::SharedParameters& Camera2Client::getParameters() { 1532 return mParameters; 1533 } 1534 1535 int Camera2Client::getPreviewStreamId() const { 1536 return mStreamingProcessor->getPreviewStreamId(); 1537 } 1538 1539 int Camera2Client::getCaptureStreamId() const { 1540 return mJpegProcessor->getStreamId(); 1541 } 1542 1543 int Camera2Client::getCallbackStreamId() const { 1544 return mCallbackProcessor->getStreamId(); 1545 } 1546 1547 int Camera2Client::getRecordingStreamId() const { 1548 return mStreamingProcessor->getRecordingStreamId(); 1549 } 1550 1551 int Camera2Client::getZslStreamId() const { 1552 return mZslProcessor->getStreamId(); 1553 } 1554 1555 status_t Camera2Client::registerFrameListener(int32_t minId, int32_t maxId, 1556 wp<camera2::FrameProcessor::FilteredListener> listener) { 1557 return mFrameProcessor->registerListener(minId, maxId, listener); 1558 } 1559 1560 status_t Camera2Client::removeFrameListener(int32_t minId, int32_t maxId, 1561 wp<camera2::FrameProcessor::FilteredListener> listener) { 1562 return mFrameProcessor->removeListener(minId, maxId, listener); 1563 } 1564 1565 status_t Camera2Client::stopStream() { 1566 return mStreamingProcessor->stopStream(); 1567 } 1568 1569 const int32_t Camera2Client::kPreviewRequestIdStart; 1570 const int32_t Camera2Client::kPreviewRequestIdEnd; 1571 const int32_t Camera2Client::kRecordingRequestIdStart; 1572 const int32_t Camera2Client::kRecordingRequestIdEnd; 1573 const int32_t Camera2Client::kCaptureRequestIdStart; 1574 const int32_t Camera2Client::kCaptureRequestIdEnd; 1575 1576 /** Utility methods */ 1577 1578 status_t Camera2Client::updateRequests(Parameters ¶ms) { 1579 status_t res; 1580 1581 ALOGV("%s: Camera %d: state = %d", __FUNCTION__, getCameraId(), params.state); 1582 1583 res = mStreamingProcessor->incrementStreamingIds(); 1584 if (res != OK) { 1585 ALOGE("%s: Camera %d: Unable to increment request IDs: %s (%d)", 1586 __FUNCTION__, mCameraId, strerror(-res), res); 1587 return res; 1588 } 1589 1590 res = mStreamingProcessor->updatePreviewRequest(params); 1591 if (res != OK) { 1592 ALOGE("%s: Camera %d: Unable to update preview request: %s (%d)", 1593 __FUNCTION__, mCameraId, strerror(-res), res); 1594 return res; 1595 } 1596 res = mStreamingProcessor->updateRecordingRequest(params); 1597 if (res != OK) { 1598 ALOGE("%s: Camera %d: Unable to update recording request: %s (%d)", 1599 __FUNCTION__, mCameraId, strerror(-res), res); 1600 return res; 1601 } 1602 1603 if (params.state == Parameters::PREVIEW) { 1604 res = startPreviewL(params, true); 1605 if (res != OK) { 1606 ALOGE("%s: Camera %d: Error streaming new preview request: %s (%d)", 1607 __FUNCTION__, mCameraId, strerror(-res), res); 1608 return res; 1609 } 1610 } else if (params.state == Parameters::RECORD || 1611 params.state == Parameters::VIDEO_SNAPSHOT) { 1612 res = startRecordingL(params, true); 1613 if (res != OK) { 1614 ALOGE("%s: Camera %d: Error streaming new record request: %s (%d)", 1615 __FUNCTION__, mCameraId, strerror(-res), res); 1616 return res; 1617 } 1618 } 1619 return res; 1620 } 1621 1622 1623 size_t Camera2Client::calculateBufferSize(int width, int height, 1624 int format, int stride) { 1625 switch (format) { 1626 case HAL_PIXEL_FORMAT_YCbCr_422_SP: // NV16 1627 return width * height * 2; 1628 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21 1629 return width * height * 3 / 2; 1630 case HAL_PIXEL_FORMAT_YCbCr_422_I: // YUY2 1631 return width * height * 2; 1632 case HAL_PIXEL_FORMAT_YV12: { // YV12 1633 size_t ySize = stride * height; 1634 size_t uvStride = (stride / 2 + 0xF) & ~0xF; 1635 size_t uvSize = uvStride * height / 2; 1636 return ySize + uvSize * 2; 1637 } 1638 case HAL_PIXEL_FORMAT_RGB_565: 1639 return width * height * 2; 1640 case HAL_PIXEL_FORMAT_RGBA_8888: 1641 return width * height * 4; 1642 case HAL_PIXEL_FORMAT_RAW_SENSOR: 1643 return width * height * 2; 1644 default: 1645 ALOGE("%s: Unknown preview format: %x", 1646 __FUNCTION__, format); 1647 return 0; 1648 } 1649 } 1650 1651 status_t Camera2Client::syncWithDevice() { 1652 ATRACE_CALL(); 1653 const nsecs_t kMaxSyncTimeout = 500000000; // 500 ms 1654 status_t res; 1655 1656 int32_t activeRequestId = mStreamingProcessor->getActiveRequestId(); 1657 if (activeRequestId == 0) return OK; 1658 1659 res = mDevice->waitUntilRequestReceived(activeRequestId, kMaxSyncTimeout); 1660 if (res == TIMED_OUT) { 1661 ALOGE("%s: Camera %d: Timed out waiting sync with HAL", 1662 __FUNCTION__, mCameraId); 1663 } else if (res != OK) { 1664 ALOGE("%s: Camera %d: Error while waiting to sync with HAL", 1665 __FUNCTION__, mCameraId); 1666 } 1667 return res; 1668 } 1669 1670 template <typename ProcessorT> 1671 status_t Camera2Client::updateProcessorStream(sp<ProcessorT> processor, 1672 camera2::Parameters params) { 1673 // No default template arguments until C++11, so we need this overload 1674 return updateProcessorStream<ProcessorT, &ProcessorT::updateStream>( 1675 processor, params); 1676 } 1677 1678 template <typename ProcessorT, 1679 status_t (ProcessorT::*updateStreamF)(const Parameters &)> 1680 status_t Camera2Client::updateProcessorStream(sp<ProcessorT> processor, 1681 Parameters params) { 1682 status_t res; 1683 1684 // Get raw pointer since sp<T> doesn't have operator->* 1685 ProcessorT *processorPtr = processor.get(); 1686 res = (processorPtr->*updateStreamF)(params); 1687 1688 /** 1689 * Can't update the stream if it's busy? 1690 * 1691 * Then we need to stop the device (by temporarily clearing the request 1692 * queue) and then try again. Resume streaming once we're done. 1693 */ 1694 if (res == -EBUSY) { 1695 ALOGV("%s: Camera %d: Pausing to update stream", __FUNCTION__, 1696 mCameraId); 1697 res = mStreamingProcessor->togglePauseStream(/*pause*/true); 1698 if (res != OK) { 1699 ALOGE("%s: Camera %d: Can't pause streaming: %s (%d)", 1700 __FUNCTION__, mCameraId, strerror(-res), res); 1701 } 1702 1703 res = mDevice->waitUntilDrained(); 1704 if (res != OK) { 1705 ALOGE("%s: Camera %d: Waiting to stop streaming failed: %s (%d)", 1706 __FUNCTION__, mCameraId, strerror(-res), res); 1707 } 1708 1709 res = (processorPtr->*updateStreamF)(params); 1710 if (res != OK) { 1711 ALOGE("%s: Camera %d: Failed to update processing stream " 1712 " despite having halted streaming first: %s (%d)", 1713 __FUNCTION__, mCameraId, strerror(-res), res); 1714 } 1715 1716 res = mStreamingProcessor->togglePauseStream(/*pause*/false); 1717 if (res != OK) { 1718 ALOGE("%s: Camera %d: Can't unpause streaming: %s (%d)", 1719 __FUNCTION__, mCameraId, strerror(-res), res); 1720 } 1721 } 1722 1723 return res; 1724 } 1725 1726 } // namespace android 1727