CaptureSequencer.cpp revision ec7710898208162576c3242f5a590651ab42aa2d
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-CaptureSequencer" 18#define ATRACE_TAG ATRACE_TAG_CAMERA 19//#define LOG_NDEBUG 0 20 21#include <utils/Log.h> 22#include <utils/Trace.h> 23#include <utils/Vector.h> 24 25#include "CaptureSequencer.h" 26#include "BurstCapture.h" 27#include "../Camera2Device.h" 28#include "../Camera2Client.h" 29#include "Parameters.h" 30 31namespace android { 32namespace camera2 { 33 34/** Public members */ 35 36CaptureSequencer::CaptureSequencer(wp<Camera2Client> client): 37 Thread(false), 38 mStartCapture(false), 39 mBusy(false), 40 mNewAEState(false), 41 mNewFrameReceived(false), 42 mNewCaptureReceived(false), 43 mClient(client), 44 mCaptureState(IDLE), 45 mTriggerId(0), 46 mTimeoutCount(0), 47 mCaptureId(Camera2Client::kFirstCaptureRequestId) { 48 ALOGV("%s", __FUNCTION__); 49} 50 51CaptureSequencer::~CaptureSequencer() { 52 ALOGV("%s: Exit", __FUNCTION__); 53} 54 55void CaptureSequencer::setZslProcessor(wp<ZslProcessor> processor) { 56 Mutex::Autolock l(mInputMutex); 57 mZslProcessor = processor; 58} 59 60status_t CaptureSequencer::startCapture() { 61 ALOGV("%s", __FUNCTION__); 62 ATRACE_CALL(); 63 Mutex::Autolock l(mInputMutex); 64 if (mBusy) { 65 ALOGE("%s: Already busy capturing!", __FUNCTION__); 66 return INVALID_OPERATION; 67 } 68 if (!mStartCapture) { 69 mStartCapture = true; 70 mStartCaptureSignal.signal(); 71 } 72 return OK; 73} 74 75void CaptureSequencer::notifyAutoExposure(uint8_t newState, int triggerId) { 76 ATRACE_CALL(); 77 Mutex::Autolock l(mInputMutex); 78 mAEState = newState; 79 mAETriggerId = triggerId; 80 if (!mNewAEState) { 81 mNewAEState = true; 82 mNewNotifySignal.signal(); 83 } 84} 85 86void CaptureSequencer::onFrameAvailable(int32_t frameId, 87 CameraMetadata &frame) { 88 ALOGV("%s: Listener found new frame", __FUNCTION__); 89 ATRACE_CALL(); 90 Mutex::Autolock l(mInputMutex); 91 mNewFrameId = frameId; 92 mNewFrame.acquire(frame); 93 if (!mNewFrameReceived) { 94 mNewFrameReceived = true; 95 mNewFrameSignal.signal(); 96 } 97} 98 99void CaptureSequencer::onCaptureAvailable(nsecs_t timestamp, 100 sp<MemoryBase> captureBuffer) { 101 ATRACE_CALL(); 102 ALOGV("%s", __FUNCTION__); 103 Mutex::Autolock l(mInputMutex); 104 mCaptureTimestamp = timestamp; 105 mCaptureBuffer = captureBuffer; 106 if (!mNewCaptureReceived) { 107 mNewCaptureReceived = true; 108 mNewCaptureSignal.signal(); 109 } 110} 111 112 113void CaptureSequencer::dump(int fd, const Vector<String16>& args) { 114 String8 result; 115 if (mCaptureRequest.entryCount() != 0) { 116 result = " Capture request:\n"; 117 write(fd, result.string(), result.size()); 118 mCaptureRequest.dump(fd, 2, 6); 119 } else { 120 result = " Capture request: undefined\n"; 121 write(fd, result.string(), result.size()); 122 } 123 result = String8::format(" Current capture state: %s\n", 124 kStateNames[mCaptureState]); 125 result.append(" Latest captured frame:\n"); 126 write(fd, result.string(), result.size()); 127 mNewFrame.dump(fd, 2, 6); 128} 129 130/** Private members */ 131 132const char* CaptureSequencer::kStateNames[CaptureSequencer::NUM_CAPTURE_STATES+1] = 133{ 134 "IDLE", 135 "START", 136 "ZSL_START", 137 "ZSL_WAITING", 138 "ZSL_REPROCESSING", 139 "STANDARD_START", 140 "STANDARD_PRECAPTURE", 141 "STANDARD_CAPTURING", 142 "BURST_CAPTURE_START", 143 "BURST_CAPTURE_WAIT", 144 "DONE", 145 "ERROR", 146 "UNKNOWN" 147}; 148 149const CaptureSequencer::StateManager 150 CaptureSequencer::kStateManagers[CaptureSequencer::NUM_CAPTURE_STATES-1] = { 151 &CaptureSequencer::manageIdle, 152 &CaptureSequencer::manageStart, 153 &CaptureSequencer::manageZslStart, 154 &CaptureSequencer::manageZslWaiting, 155 &CaptureSequencer::manageZslReprocessing, 156 &CaptureSequencer::manageStandardStart, 157 &CaptureSequencer::manageStandardPrecaptureWait, 158 &CaptureSequencer::manageStandardCapture, 159 &CaptureSequencer::manageStandardCaptureWait, 160 &CaptureSequencer::manageBurstCaptureStart, 161 &CaptureSequencer::manageBurstCaptureWait, 162 &CaptureSequencer::manageDone, 163}; 164 165bool CaptureSequencer::threadLoop() { 166 status_t res; 167 168 sp<Camera2Client> client = mClient.promote(); 169 if (client == 0) return false; 170 171 if (mCaptureState < ERROR) { 172 CaptureState oldState = mCaptureState; 173 mCaptureState = (this->*kStateManagers[mCaptureState])(client); 174 if (ATRACE_ENABLED() && oldState != mCaptureState) { 175 ATRACE_INT("cam2_capt_state", mCaptureState); 176 } 177 } else { 178 ALOGE("%s: Bad capture state: %s", 179 __FUNCTION__, kStateNames[mCaptureState]); 180 return false; 181 } 182 183 return true; 184} 185 186CaptureSequencer::CaptureState CaptureSequencer::manageIdle(sp<Camera2Client> &client) { 187 status_t res; 188 Mutex::Autolock l(mInputMutex); 189 while (!mStartCapture) { 190 res = mStartCaptureSignal.waitRelative(mInputMutex, 191 kWaitDuration); 192 if (res == TIMED_OUT) break; 193 } 194 if (mStartCapture) { 195 mStartCapture = false; 196 mBusy = true; 197 return START; 198 } 199 return IDLE; 200} 201 202CaptureSequencer::CaptureState CaptureSequencer::manageDone(sp<Camera2Client> &client) { 203 status_t res = OK; 204 ATRACE_CALL(); 205 mCaptureId++; 206 207 { 208 Mutex::Autolock l(mInputMutex); 209 mBusy = false; 210 } 211 212 { 213 SharedParameters::Lock l(client->getParameters()); 214 switch (l.mParameters.state) { 215 case Parameters::STILL_CAPTURE: 216 l.mParameters.state = Parameters::STOPPED; 217 break; 218 case Parameters::VIDEO_SNAPSHOT: 219 l.mParameters.state = Parameters::RECORD; 220 break; 221 default: 222 ALOGE("%s: Camera %d: Still image produced unexpectedly " 223 "in state %s!", 224 __FUNCTION__, client->getCameraId(), 225 Parameters::getStateName(l.mParameters.state)); 226 res = INVALID_OPERATION; 227 } 228 } 229 sp<ZslProcessor> processor = mZslProcessor.promote(); 230 if (processor != 0) { 231 processor->clearZslQueue(); 232 } 233 234 if (mCaptureBuffer != 0 && res == OK) { 235 Camera2Client::SharedCameraClient::Lock l(client->mSharedCameraClient); 236 ALOGV("%s: Sending still image to client", __FUNCTION__); 237 if (l.mCameraClient != 0) { 238 l.mCameraClient->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, 239 mCaptureBuffer, NULL); 240 } else { 241 ALOGV("%s: No client!", __FUNCTION__); 242 } 243 } 244 mCaptureBuffer.clear(); 245 246 return IDLE; 247} 248 249CaptureSequencer::CaptureState CaptureSequencer::manageStart( 250 sp<Camera2Client> &client) { 251 ALOGV("%s", __FUNCTION__); 252 status_t res; 253 ATRACE_CALL(); 254 SharedParameters::Lock l(client->getParameters()); 255 CaptureState nextState = DONE; 256 257 res = updateCaptureRequest(l.mParameters, client); 258 if (res != OK ) { 259 ALOGE("%s: Camera %d: Can't update still image capture request: %s (%d)", 260 __FUNCTION__, client->getCameraId(), strerror(-res), res); 261 return DONE; 262 } 263 264 if(l.mParameters.lightFx != Parameters::LIGHTFX_NONE && 265 l.mParameters.state == Parameters::STILL_CAPTURE) { 266 nextState = BURST_CAPTURE_START; 267 } 268 else if (l.mParameters.zslMode && 269 l.mParameters.state == Parameters::STILL_CAPTURE && 270 l.mParameters.flashMode != Parameters::FLASH_MODE_ON) { 271 nextState = ZSL_START; 272 } else { 273 nextState = STANDARD_START; 274 } 275 276 return nextState; 277} 278 279CaptureSequencer::CaptureState CaptureSequencer::manageZslStart( 280 sp<Camera2Client> &client) { 281 ALOGV("%s", __FUNCTION__); 282 status_t res; 283 sp<ZslProcessor> processor = mZslProcessor.promote(); 284 if (processor == 0) { 285 ALOGE("%s: No ZSL queue to use!", __FUNCTION__); 286 return DONE; 287 } 288 289 client->registerFrameListener(mCaptureId, 290 this); 291 292 // TODO: Actually select the right thing here. 293 res = processor->pushToReprocess(mCaptureId); 294 if (res != OK) { 295 if (res == NOT_ENOUGH_DATA) { 296 ALOGV("%s: Camera %d: ZSL queue doesn't have good frame, " 297 "falling back to normal capture", __FUNCTION__, 298 client->getCameraId()); 299 } else { 300 ALOGE("%s: Camera %d: Error in ZSL queue: %s (%d)", 301 __FUNCTION__, client->getCameraId(), strerror(-res), res); 302 } 303 return STANDARD_START; 304 } 305 306 SharedParameters::Lock l(client->getParameters()); 307 /* warning: this also locks a SharedCameraClient */ 308 shutterNotifyLocked(l.mParameters, client); 309 310 mTimeoutCount = kMaxTimeoutsForCaptureEnd; 311 return STANDARD_CAPTURE_WAIT; 312} 313 314CaptureSequencer::CaptureState CaptureSequencer::manageZslWaiting( 315 sp<Camera2Client> &client) { 316 ALOGV("%s", __FUNCTION__); 317 return DONE; 318} 319 320CaptureSequencer::CaptureState CaptureSequencer::manageZslReprocessing( 321 sp<Camera2Client> &client) { 322 ALOGV("%s", __FUNCTION__); 323 return START; 324} 325 326CaptureSequencer::CaptureState CaptureSequencer::manageStandardStart( 327 sp<Camera2Client> &client) { 328 ATRACE_CALL(); 329 client->registerFrameListener(mCaptureId, 330 this); 331 { 332 SharedParameters::Lock l(client->getParameters()); 333 mTriggerId = l.mParameters.precaptureTriggerCounter++; 334 } 335 client->getCameraDevice()->triggerPrecaptureMetering(mTriggerId); 336 337 mAeInPrecapture = false; 338 mTimeoutCount = kMaxTimeoutsForPrecaptureStart; 339 return STANDARD_PRECAPTURE_WAIT; 340} 341 342CaptureSequencer::CaptureState CaptureSequencer::manageStandardPrecaptureWait( 343 sp<Camera2Client> &client) { 344 status_t res; 345 ATRACE_CALL(); 346 Mutex::Autolock l(mInputMutex); 347 while (!mNewAEState) { 348 res = mNewNotifySignal.waitRelative(mInputMutex, kWaitDuration); 349 if (res == TIMED_OUT) { 350 mTimeoutCount--; 351 break; 352 } 353 } 354 if (mTimeoutCount <= 0) { 355 ALOGW("Timed out waiting for precapture %s", 356 mAeInPrecapture ? "end" : "start"); 357 return STANDARD_CAPTURE; 358 } 359 if (mNewAEState) { 360 if (!mAeInPrecapture) { 361 // Waiting to see PRECAPTURE state 362 if (mAETriggerId == mTriggerId && 363 mAEState == ANDROID_CONTROL_AE_STATE_PRECAPTURE) { 364 ALOGV("%s: Got precapture start", __FUNCTION__); 365 mAeInPrecapture = true; 366 mTimeoutCount = kMaxTimeoutsForPrecaptureEnd; 367 } 368 } else { 369 // Waiting to see PRECAPTURE state end 370 if (mAETriggerId == mTriggerId && 371 mAEState != ANDROID_CONTROL_AE_STATE_PRECAPTURE) { 372 ALOGV("%s: Got precapture end", __FUNCTION__); 373 return STANDARD_CAPTURE; 374 } 375 } 376 mNewAEState = false; 377 } 378 return STANDARD_PRECAPTURE_WAIT; 379} 380 381CaptureSequencer::CaptureState CaptureSequencer::manageStandardCapture( 382 sp<Camera2Client> &client) { 383 status_t res; 384 ATRACE_CALL(); 385 SharedParameters::Lock l(client->getParameters()); 386 Vector<uint8_t> outputStreams; 387 388 outputStreams.push(client->getPreviewStreamId()); 389 outputStreams.push(client->getCaptureStreamId()); 390 391 if (l.mParameters.previewCallbackFlags & 392 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) { 393 outputStreams.push(client->getCallbackStreamId()); 394 } 395 396 if (l.mParameters.state == Parameters::VIDEO_SNAPSHOT) { 397 outputStreams.push(client->getRecordingStreamId()); 398 } 399 400 res = mCaptureRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS, 401 outputStreams); 402 if (res == OK) { 403 res = mCaptureRequest.update(ANDROID_REQUEST_ID, 404 &mCaptureId, 1); 405 } 406 if (res == OK) { 407 res = mCaptureRequest.sort(); 408 } 409 410 if (res != OK) { 411 ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)", 412 __FUNCTION__, client->getCameraId(), strerror(-res), res); 413 return DONE; 414 } 415 416 CameraMetadata captureCopy = mCaptureRequest; 417 if (captureCopy.entryCount() == 0) { 418 ALOGE("%s: Camera %d: Unable to copy capture request for HAL device", 419 __FUNCTION__, client->getCameraId()); 420 return DONE; 421 } 422 423 if (l.mParameters.state == Parameters::STILL_CAPTURE) { 424 res = client->getCameraDevice()->clearStreamingRequest(); 425 if (res != OK) { 426 ALOGE("%s: Camera %d: Unable to stop preview for still capture: " 427 "%s (%d)", 428 __FUNCTION__, client->getCameraId(), strerror(-res), res); 429 return DONE; 430 } 431 } 432 // TODO: Capture should be atomic with setStreamingRequest here 433 res = client->getCameraDevice()->capture(captureCopy); 434 if (res != OK) { 435 ALOGE("%s: Camera %d: Unable to submit still image capture request: " 436 "%s (%d)", 437 __FUNCTION__, client->getCameraId(), strerror(-res), res); 438 return DONE; 439 } 440 441 /* warning: this also locks a SharedCameraClient */ 442 shutterNotifyLocked(l.mParameters, client); 443 444 mTimeoutCount = kMaxTimeoutsForCaptureEnd; 445 return STANDARD_CAPTURE_WAIT; 446} 447 448CaptureSequencer::CaptureState CaptureSequencer::manageStandardCaptureWait( 449 sp<Camera2Client> &client) { 450 status_t res; 451 ATRACE_CALL(); 452 Mutex::Autolock l(mInputMutex); 453 while (!mNewFrameReceived) { 454 res = mNewFrameSignal.waitRelative(mInputMutex, kWaitDuration); 455 if (res == TIMED_OUT) { 456 mTimeoutCount--; 457 break; 458 } 459 } 460 while (!mNewCaptureReceived) { 461 res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration); 462 if (res == TIMED_OUT) { 463 mTimeoutCount--; 464 break; 465 } 466 } 467 if (mTimeoutCount <= 0) { 468 ALOGW("Timed out waiting for capture to complete"); 469 return DONE; 470 } 471 if (mNewFrameReceived && mNewCaptureReceived) { 472 if (mNewFrameId != mCaptureId) { 473 ALOGW("Mismatched capture frame IDs: Expected %d, got %d", 474 mCaptureId, mNewFrameId); 475 } 476 camera_metadata_entry_t entry; 477 entry = mNewFrame.find(ANDROID_SENSOR_TIMESTAMP); 478 if (entry.count == 0) { 479 ALOGE("No timestamp field in capture frame!"); 480 } 481 if (entry.data.i64[0] != mCaptureTimestamp) { 482 ALOGW("Mismatched capture timestamps: Metadata frame %lld," 483 " captured buffer %lld", entry.data.i64[0], mCaptureTimestamp); 484 } 485 client->removeFrameListener(mCaptureId); 486 487 mNewFrameReceived = false; 488 mNewCaptureReceived = false; 489 return DONE; 490 } 491 return STANDARD_CAPTURE_WAIT; 492} 493 494CaptureSequencer::CaptureState CaptureSequencer::manageBurstCaptureStart( 495 sp<Camera2Client> &client) { 496 ALOGV("%s", __FUNCTION__); 497 status_t res; 498 ATRACE_CALL(); 499 500 // check which burst mode is set, create respective burst object 501 { 502 SharedParameters::Lock l(client->getParameters()); 503 504 res = updateCaptureRequest(l.mParameters, client); 505 if(res != OK) { 506 return DONE; 507 } 508 509 // 510 // check for burst mode type in mParameters here 511 // 512 mBurstCapture = new BurstCapture(client, this); 513 } 514 515 res = mCaptureRequest.update(ANDROID_REQUEST_ID, &mCaptureId, 1); 516 if (res == OK) { 517 res = mCaptureRequest.sort(); 518 } 519 if (res != OK) { 520 ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)", 521 __FUNCTION__, client->getCameraId(), strerror(-res), res); 522 return DONE; 523 } 524 525 CameraMetadata captureCopy = mCaptureRequest; 526 if (captureCopy.entryCount() == 0) { 527 ALOGE("%s: Camera %d: Unable to copy capture request for HAL device", 528 __FUNCTION__, client->getCameraId()); 529 return DONE; 530 } 531 532 Vector<CameraMetadata> requests; 533 requests.push(mCaptureRequest); 534 res = mBurstCapture->start(requests, mCaptureId); 535 mTimeoutCount = kMaxTimeoutsForCaptureEnd * 10; 536 return BURST_CAPTURE_WAIT; 537} 538 539CaptureSequencer::CaptureState CaptureSequencer::manageBurstCaptureWait( 540 sp<Camera2Client> &client) { 541 status_t res; 542 ATRACE_CALL(); 543 544 while (!mNewCaptureReceived) { 545 res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration); 546 if (res == TIMED_OUT) { 547 mTimeoutCount--; 548 break; 549 } 550 } 551 552 if (mTimeoutCount <= 0) { 553 ALOGW("Timed out waiting for burst capture to complete"); 554 return DONE; 555 } 556 if (mNewCaptureReceived) { 557 mNewCaptureReceived = false; 558 // TODO: update mCaptureId to last burst's capture ID + 1? 559 return DONE; 560 } 561 562 return BURST_CAPTURE_WAIT; 563} 564 565status_t CaptureSequencer::updateCaptureRequest(const Parameters ¶ms, 566 sp<Camera2Client> &client) { 567 ATRACE_CALL(); 568 status_t res; 569 if (mCaptureRequest.entryCount() == 0) { 570 res = client->getCameraDevice()->createDefaultRequest( 571 CAMERA2_TEMPLATE_STILL_CAPTURE, 572 &mCaptureRequest); 573 if (res != OK) { 574 ALOGE("%s: Camera %d: Unable to create default still image request:" 575 " %s (%d)", __FUNCTION__, client->getCameraId(), 576 strerror(-res), res); 577 return res; 578 } 579 } 580 581 res = params.updateRequest(&mCaptureRequest); 582 if (res != OK) { 583 ALOGE("%s: Camera %d: Unable to update common entries of capture " 584 "request: %s (%d)", __FUNCTION__, client->getCameraId(), 585 strerror(-res), res); 586 return res; 587 } 588 589 res = params.updateRequestJpeg(&mCaptureRequest); 590 if (res != OK) { 591 ALOGE("%s: Camera %d: Unable to update JPEG entries of capture " 592 "request: %s (%d)", __FUNCTION__, client->getCameraId(), 593 strerror(-res), res); 594 return res; 595 } 596 597 return OK; 598} 599 600/*static*/ void CaptureSequencer::shutterNotifyLocked(const Parameters ¶ms, 601 sp<Camera2Client> client) { 602 ATRACE_CALL(); 603 604 if (params.state == Parameters::STILL_CAPTURE && params.playShutterSound) { 605 client->getCameraService()->playSound(CameraService::SOUND_SHUTTER); 606 } 607 608 { 609 Camera2Client::SharedCameraClient::Lock l(client->mSharedCameraClient); 610 611 ALOGV("%s: Notifying of shutter close to client", __FUNCTION__); 612 if (l.mCameraClient != 0) { 613 // ShutterCallback 614 l.mCameraClient->notifyCallback(CAMERA_MSG_SHUTTER, 615 /*ext1*/0, /*ext2*/0); 616 617 // RawCallback with null buffer 618 l.mCameraClient->notifyCallback(CAMERA_MSG_RAW_IMAGE_NOTIFY, 619 /*ext1*/0, /*ext2*/0); 620 } else { 621 ALOGV("%s: No client!", __FUNCTION__); 622 } 623 } 624} 625 626 627}; // namespace camera2 628}; // namespace android 629