Home | History | Annotate | Download | only in client2
      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 <inttypes.h>
     22 
     23 #include <utils/Log.h>
     24 #include <utils/Trace.h>
     25 #include <utils/Vector.h>
     26 
     27 #include "api1/Camera2Client.h"
     28 #include "api1/client2/CaptureSequencer.h"
     29 #include "api1/client2/Parameters.h"
     30 #include "api1/client2/ZslProcessor.h"
     31 
     32 namespace android {
     33 namespace camera2 {
     34 
     35 /** Public members */
     36 
     37 CaptureSequencer::CaptureSequencer(wp<Camera2Client> client):
     38         Thread(false),
     39         mStartCapture(false),
     40         mBusy(false),
     41         mNewAEState(false),
     42         mNewFrameReceived(false),
     43         mNewCaptureReceived(false),
     44         mNewCaptureErrorCnt(0),
     45         mShutterNotified(false),
     46         mHalNotifiedShutter(false),
     47         mShutterCaptureId(-1),
     48         mClient(client),
     49         mCaptureState(IDLE),
     50         mStateTransitionCount(0),
     51         mTriggerId(0),
     52         mTimeoutCount(0),
     53         mCaptureId(Camera2Client::kCaptureRequestIdStart) {
     54     ALOGV("%s", __FUNCTION__);
     55 }
     56 
     57 CaptureSequencer::~CaptureSequencer() {
     58     ALOGV("%s: Exit", __FUNCTION__);
     59 }
     60 
     61 void CaptureSequencer::setZslProcessor(const wp<ZslProcessor>& processor) {
     62     Mutex::Autolock l(mInputMutex);
     63     mZslProcessor = processor;
     64 }
     65 
     66 status_t CaptureSequencer::startCapture() {
     67     ALOGV("%s", __FUNCTION__);
     68     ATRACE_CALL();
     69     Mutex::Autolock l(mInputMutex);
     70     if (mBusy) {
     71         ALOGE("%s: Already busy capturing!", __FUNCTION__);
     72         return INVALID_OPERATION;
     73     }
     74     if (!mStartCapture) {
     75         mStartCapture = true;
     76         mStartCaptureSignal.signal();
     77     }
     78     return OK;
     79 }
     80 
     81 status_t CaptureSequencer::waitUntilIdle(nsecs_t timeout) {
     82     ATRACE_CALL();
     83     ALOGV("%s: Waiting for idle", __FUNCTION__);
     84     Mutex::Autolock l(mStateMutex);
     85     status_t res = -1;
     86     while (mCaptureState != IDLE) {
     87         nsecs_t startTime = systemTime();
     88 
     89         res = mStateChanged.waitRelative(mStateMutex, timeout);
     90         if (res != OK) return res;
     91 
     92         timeout -= (systemTime() - startTime);
     93     }
     94     ALOGV("%s: Now idle", __FUNCTION__);
     95     return OK;
     96 }
     97 
     98 void CaptureSequencer::notifyAutoExposure(uint8_t newState, int triggerId) {
     99     ATRACE_CALL();
    100     Mutex::Autolock l(mInputMutex);
    101     mAEState = newState;
    102     mAETriggerId = triggerId;
    103     if (!mNewAEState) {
    104         mNewAEState = true;
    105         mNewNotifySignal.signal();
    106     }
    107 }
    108 
    109 void CaptureSequencer::notifyShutter(const CaptureResultExtras& resultExtras,
    110                                      nsecs_t timestamp) {
    111     ATRACE_CALL();
    112     (void) timestamp;
    113     Mutex::Autolock l(mInputMutex);
    114     if (!mHalNotifiedShutter && resultExtras.requestId == mShutterCaptureId) {
    115         mHalNotifiedShutter = true;
    116         mShutterNotifySignal.signal();
    117     }
    118 }
    119 
    120 void CaptureSequencer::notifyError(int32_t errorCode, const CaptureResultExtras& resultExtras) {
    121     ATRACE_CALL();
    122     bool jpegBufferLost = false;
    123     if (errorCode == hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER) {
    124         sp<Camera2Client> client = mClient.promote();
    125         if (client == nullptr) {
    126             return;
    127         }
    128         int captureStreamId = client->getCaptureStreamId();
    129         if (captureStreamId == resultExtras.errorStreamId) {
    130             jpegBufferLost = true;
    131         }
    132     } else if (errorCode ==
    133             hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST) {
    134         if (resultExtras.requestId == mShutterCaptureId) {
    135             jpegBufferLost = true;
    136         }
    137     }
    138 
    139     if (jpegBufferLost) {
    140         sp<MemoryBase> emptyBuffer;
    141         onCaptureAvailable(/*timestamp*/0, emptyBuffer, /*captureError*/true);
    142     }
    143 }
    144 
    145 void CaptureSequencer::onResultAvailable(const CaptureResult &result) {
    146     ATRACE_CALL();
    147     ALOGV("%s: New result available.", __FUNCTION__);
    148     Mutex::Autolock l(mInputMutex);
    149     mNewFrameId = result.mResultExtras.requestId;
    150     mNewFrame = result.mMetadata;
    151     if (!mNewFrameReceived) {
    152         mNewFrameReceived = true;
    153         mNewFrameSignal.signal();
    154     }
    155 }
    156 
    157 void CaptureSequencer::onCaptureAvailable(nsecs_t timestamp,
    158         const sp<MemoryBase>& captureBuffer, bool captureError) {
    159     ATRACE_CALL();
    160     ALOGV("%s", __FUNCTION__);
    161     Mutex::Autolock l(mInputMutex);
    162     mCaptureTimestamp = timestamp;
    163     mCaptureBuffer = captureBuffer;
    164     if (!mNewCaptureReceived) {
    165         mNewCaptureReceived = true;
    166         if (captureError) {
    167             mNewCaptureErrorCnt++;
    168         } else {
    169             mNewCaptureErrorCnt = 0;
    170         }
    171         mNewCaptureSignal.signal();
    172     }
    173 }
    174 
    175 
    176 void CaptureSequencer::dump(int fd, const Vector<String16>& /*args*/) {
    177     String8 result;
    178     if (mCaptureRequest.entryCount() != 0) {
    179         result = "    Capture request:\n";
    180         write(fd, result.string(), result.size());
    181         mCaptureRequest.dump(fd, 2, 6);
    182     } else {
    183         result = "    Capture request: undefined\n";
    184         write(fd, result.string(), result.size());
    185     }
    186     result = String8::format("    Current capture state: %s\n",
    187             kStateNames[mCaptureState]);
    188     result.append("    Latest captured frame:\n");
    189     write(fd, result.string(), result.size());
    190     mNewFrame.dump(fd, 2, 6);
    191 }
    192 
    193 /** Private members */
    194 
    195 const char* CaptureSequencer::kStateNames[CaptureSequencer::NUM_CAPTURE_STATES+1] =
    196 {
    197     "IDLE",
    198     "START",
    199     "ZSL_START",
    200     "ZSL_WAITING",
    201     "ZSL_REPROCESSING",
    202     "STANDARD_START",
    203     "STANDARD_PRECAPTURE_WAIT",
    204     "STANDARD_CAPTURE",
    205     "STANDARD_CAPTURE_WAIT",
    206     "DONE",
    207     "ERROR",
    208     "UNKNOWN"
    209 };
    210 
    211 const CaptureSequencer::StateManager
    212         CaptureSequencer::kStateManagers[CaptureSequencer::NUM_CAPTURE_STATES-1] = {
    213     &CaptureSequencer::manageIdle,
    214     &CaptureSequencer::manageStart,
    215     &CaptureSequencer::manageZslStart,
    216     &CaptureSequencer::manageZslWaiting,
    217     &CaptureSequencer::manageZslReprocessing,
    218     &CaptureSequencer::manageStandardStart,
    219     &CaptureSequencer::manageStandardPrecaptureWait,
    220     &CaptureSequencer::manageStandardCapture,
    221     &CaptureSequencer::manageStandardCaptureWait,
    222     &CaptureSequencer::manageDone,
    223 };
    224 
    225 bool CaptureSequencer::threadLoop() {
    226 
    227     sp<Camera2Client> client = mClient.promote();
    228     if (client == 0) return false;
    229 
    230     CaptureState currentState;
    231     {
    232         Mutex::Autolock l(mStateMutex);
    233         currentState = mCaptureState;
    234     }
    235 
    236     currentState = (this->*kStateManagers[currentState])(client);
    237 
    238     Mutex::Autolock l(mStateMutex);
    239     if (currentState != mCaptureState) {
    240         if (mCaptureState != IDLE) {
    241             ATRACE_ASYNC_END(kStateNames[mCaptureState], mStateTransitionCount);
    242         }
    243         mCaptureState = currentState;
    244         mStateTransitionCount++;
    245         if (mCaptureState != IDLE) {
    246             ATRACE_ASYNC_BEGIN(kStateNames[mCaptureState], mStateTransitionCount);
    247         }
    248         ALOGV("Camera %d: New capture state %s",
    249                 client->getCameraId(), kStateNames[mCaptureState]);
    250         mStateChanged.signal();
    251     }
    252 
    253     if (mCaptureState == ERROR) {
    254         ALOGE("Camera %d: Stopping capture sequencer due to error",
    255                 client->getCameraId());
    256         return false;
    257     }
    258 
    259     return true;
    260 }
    261 
    262 CaptureSequencer::CaptureState CaptureSequencer::manageIdle(
    263         sp<Camera2Client> &/*client*/) {
    264     status_t res;
    265     Mutex::Autolock l(mInputMutex);
    266     while (!mStartCapture) {
    267         res = mStartCaptureSignal.waitRelative(mInputMutex,
    268                 kWaitDuration);
    269         if (res == TIMED_OUT) break;
    270     }
    271     if (mStartCapture) {
    272         mStartCapture = false;
    273         mBusy = true;
    274         return START;
    275     }
    276     return IDLE;
    277 }
    278 
    279 CaptureSequencer::CaptureState CaptureSequencer::manageDone(sp<Camera2Client> &client) {
    280     status_t res = OK;
    281     ATRACE_CALL();
    282     mCaptureId++;
    283     if (mCaptureId >= Camera2Client::kCaptureRequestIdEnd) {
    284         mCaptureId = Camera2Client::kCaptureRequestIdStart;
    285     }
    286     {
    287         Mutex::Autolock l(mInputMutex);
    288         mBusy = false;
    289     }
    290 
    291     int takePictureCounter = 0;
    292     {
    293         SharedParameters::Lock l(client->getParameters());
    294         switch (l.mParameters.state) {
    295             case Parameters::DISCONNECTED:
    296                 ALOGW("%s: Camera %d: Discarding image data during shutdown ",
    297                         __FUNCTION__, client->getCameraId());
    298                 res = INVALID_OPERATION;
    299                 break;
    300             case Parameters::STILL_CAPTURE:
    301                 res = client->getCameraDevice()->waitUntilDrained();
    302                 if (res != OK) {
    303                     ALOGE("%s: Camera %d: Can't idle after still capture: "
    304                             "%s (%d)", __FUNCTION__, client->getCameraId(),
    305                             strerror(-res), res);
    306                 }
    307                 l.mParameters.state = Parameters::STOPPED;
    308                 break;
    309             case Parameters::VIDEO_SNAPSHOT:
    310                 l.mParameters.state = Parameters::RECORD;
    311                 break;
    312             default:
    313                 ALOGE("%s: Camera %d: Still image produced unexpectedly "
    314                         "in state %s!",
    315                         __FUNCTION__, client->getCameraId(),
    316                         Parameters::getStateName(l.mParameters.state));
    317                 res = INVALID_OPERATION;
    318         }
    319         takePictureCounter = l.mParameters.takePictureCounter;
    320     }
    321     sp<ZslProcessor> processor = mZslProcessor.promote();
    322     if (processor != 0) {
    323         ALOGV("%s: Memory optimization, clearing ZSL queue",
    324               __FUNCTION__);
    325         processor->clearZslQueue();
    326     }
    327 
    328     /**
    329      * Fire the jpegCallback in Camera#takePicture(..., jpegCallback)
    330      */
    331     if (mCaptureBuffer != 0 && res == OK) {
    332         ATRACE_ASYNC_END(Camera2Client::kTakepictureLabel, takePictureCounter);
    333 
    334         Camera2Client::SharedCameraCallbacks::Lock
    335             l(client->mSharedCameraCallbacks);
    336         ALOGV("%s: Sending still image to client", __FUNCTION__);
    337         if (l.mRemoteCallback != 0) {
    338             l.mRemoteCallback->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE,
    339                     mCaptureBuffer, NULL);
    340         } else {
    341             ALOGV("%s: No client!", __FUNCTION__);
    342         }
    343     }
    344     mCaptureBuffer.clear();
    345 
    346     return IDLE;
    347 }
    348 
    349 CaptureSequencer::CaptureState CaptureSequencer::manageStart(
    350         sp<Camera2Client> &client) {
    351     ALOGV("%s", __FUNCTION__);
    352     status_t res;
    353     ATRACE_CALL();
    354     SharedParameters::Lock l(client->getParameters());
    355     CaptureState nextState = DONE;
    356 
    357     res = updateCaptureRequest(l.mParameters, client);
    358     if (res != OK ) {
    359         ALOGE("%s: Camera %d: Can't update still image capture request: %s (%d)",
    360                 __FUNCTION__, client->getCameraId(), strerror(-res), res);
    361         return DONE;
    362     }
    363 
    364     else if (l.mParameters.useZeroShutterLag() &&
    365             l.mParameters.state == Parameters::STILL_CAPTURE &&
    366             l.mParameters.flashMode != Parameters::FLASH_MODE_ON) {
    367         nextState = ZSL_START;
    368     } else {
    369         nextState = STANDARD_START;
    370     }
    371     {
    372         Mutex::Autolock l(mInputMutex);
    373         mShutterCaptureId = mCaptureId;
    374         mHalNotifiedShutter = false;
    375     }
    376     mShutterNotified = false;
    377 
    378     return nextState;
    379 }
    380 
    381 CaptureSequencer::CaptureState CaptureSequencer::manageZslStart(
    382         sp<Camera2Client> &client) {
    383     ALOGV("%s", __FUNCTION__);
    384     status_t res;
    385     sp<ZslProcessor> processor = mZslProcessor.promote();
    386     if (processor == 0) {
    387         ALOGE("%s: No ZSL queue to use!", __FUNCTION__);
    388         return DONE;
    389     }
    390 
    391     // We don't want to get partial results for ZSL capture.
    392     client->registerFrameListener(mCaptureId, mCaptureId + 1,
    393             this,
    394             /*sendPartials*/false);
    395 
    396     // TODO: Actually select the right thing here.
    397     res = processor->pushToReprocess(mCaptureId);
    398     if (res != OK) {
    399         if (res == NOT_ENOUGH_DATA) {
    400             ALOGV("%s: Camera %d: ZSL queue doesn't have good frame, "
    401                     "falling back to normal capture", __FUNCTION__,
    402                     client->getCameraId());
    403         } else {
    404             ALOGE("%s: Camera %d: Error in ZSL queue: %s (%d)",
    405                     __FUNCTION__, client->getCameraId(), strerror(-res), res);
    406         }
    407         return STANDARD_START;
    408     }
    409 
    410     SharedParameters::Lock l(client->getParameters());
    411     /* warning: this also locks a SharedCameraCallbacks */
    412     shutterNotifyLocked(l.mParameters, client);
    413     mShutterNotified = true;
    414     mTimeoutCount = kMaxTimeoutsForCaptureEnd;
    415     return STANDARD_CAPTURE_WAIT;
    416 }
    417 
    418 CaptureSequencer::CaptureState CaptureSequencer::manageZslWaiting(
    419         sp<Camera2Client> &/*client*/) {
    420     ALOGV("%s", __FUNCTION__);
    421     return DONE;
    422 }
    423 
    424 CaptureSequencer::CaptureState CaptureSequencer::manageZslReprocessing(
    425         sp<Camera2Client> &/*client*/) {
    426     ALOGV("%s", __FUNCTION__);
    427     return START;
    428 }
    429 
    430 CaptureSequencer::CaptureState CaptureSequencer::manageStandardStart(
    431         sp<Camera2Client> &client) {
    432     ATRACE_CALL();
    433 
    434     bool isAeConverged = false;
    435     // Get the onFrameAvailable callback when the requestID == mCaptureId
    436     // We don't want to get partial results for normal capture, as we need
    437     // Get ANDROID_SENSOR_TIMESTAMP from the capture result, but partial
    438     // result doesn't have to have this metadata available.
    439     // TODO: Update to use the HALv3 shutter notification for remove the
    440     // need for this listener and make it faster. see bug 12530628.
    441     client->registerFrameListener(mCaptureId, mCaptureId + 1,
    442             this,
    443             /*sendPartials*/false);
    444 
    445     {
    446         Mutex::Autolock l(mInputMutex);
    447         isAeConverged = (mAEState == ANDROID_CONTROL_AE_STATE_CONVERGED);
    448     }
    449 
    450     {
    451         SharedParameters::Lock l(client->getParameters());
    452         // Skip AE precapture when it is already converged and not in force flash mode.
    453         if (l.mParameters.flashMode != Parameters::FLASH_MODE_ON && isAeConverged) {
    454             return STANDARD_CAPTURE;
    455         }
    456 
    457         mTriggerId = l.mParameters.precaptureTriggerCounter++;
    458     }
    459     client->getCameraDevice()->triggerPrecaptureMetering(mTriggerId);
    460 
    461     mAeInPrecapture = false;
    462     mTimeoutCount = kMaxTimeoutsForPrecaptureStart;
    463     return STANDARD_PRECAPTURE_WAIT;
    464 }
    465 
    466 CaptureSequencer::CaptureState CaptureSequencer::manageStandardPrecaptureWait(
    467         sp<Camera2Client> &/*client*/) {
    468     status_t res;
    469     ATRACE_CALL();
    470     Mutex::Autolock l(mInputMutex);
    471     while (!mNewAEState) {
    472         res = mNewNotifySignal.waitRelative(mInputMutex, kWaitDuration);
    473         if (res == TIMED_OUT) {
    474             mTimeoutCount--;
    475             break;
    476         }
    477     }
    478     if (mTimeoutCount <= 0) {
    479         ALOGW("Timed out waiting for precapture %s",
    480                 mAeInPrecapture ? "end" : "start");
    481         return STANDARD_CAPTURE;
    482     }
    483     if (mNewAEState) {
    484         if (!mAeInPrecapture) {
    485             // Waiting to see PRECAPTURE state
    486             if (mAETriggerId == mTriggerId) {
    487                 if (mAEState == ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
    488                     ALOGV("%s: Got precapture start", __FUNCTION__);
    489                     mAeInPrecapture = true;
    490                     mTimeoutCount = kMaxTimeoutsForPrecaptureEnd;
    491                 } else if (mAEState == ANDROID_CONTROL_AE_STATE_CONVERGED ||
    492                         mAEState == ANDROID_CONTROL_AE_STATE_FLASH_REQUIRED) {
    493                     // It is legal to transit to CONVERGED or FLASH_REQUIRED
    494                     // directly after a trigger.
    495                     ALOGV("%s: AE is already in good state, start capture", __FUNCTION__);
    496                     return STANDARD_CAPTURE;
    497                 }
    498             }
    499         } else {
    500             // Waiting to see PRECAPTURE state end
    501             if (mAETriggerId == mTriggerId &&
    502                     mAEState != ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
    503                 ALOGV("%s: Got precapture end", __FUNCTION__);
    504                 return STANDARD_CAPTURE;
    505             }
    506         }
    507         mNewAEState = false;
    508     }
    509     return STANDARD_PRECAPTURE_WAIT;
    510 }
    511 
    512 CaptureSequencer::CaptureState CaptureSequencer::manageStandardCapture(
    513         sp<Camera2Client> &client) {
    514     status_t res;
    515     ATRACE_CALL();
    516     SharedParameters::Lock l(client->getParameters());
    517     Vector<int32_t> outputStreams;
    518 
    519     /**
    520      * Set up output streams in the request
    521      *  - preview
    522      *  - capture/jpeg
    523      *  - callback (if preview callbacks enabled)
    524      *  - recording (if recording enabled)
    525      */
    526     outputStreams.push(client->getPreviewStreamId());
    527 
    528     int captureStreamId = client->getCaptureStreamId();
    529     if (captureStreamId == Camera2Client::NO_STREAM) {
    530         res = client->createJpegStreamL(l.mParameters);
    531         if (res != OK || client->getCaptureStreamId() == Camera2Client::NO_STREAM) {
    532             ALOGE("%s: Camera %d: cannot create jpeg stream for slowJpeg mode: %s (%d)",
    533                   __FUNCTION__, client->getCameraId(), strerror(-res), res);
    534             return DONE;
    535         }
    536     }
    537 
    538     outputStreams.push(client->getCaptureStreamId());
    539 
    540     if (l.mParameters.previewCallbackFlags &
    541             CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
    542         outputStreams.push(client->getCallbackStreamId());
    543     }
    544 
    545     if (l.mParameters.state == Parameters::VIDEO_SNAPSHOT) {
    546         outputStreams.push(client->getRecordingStreamId());
    547     }
    548 
    549     res = mCaptureRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
    550             outputStreams);
    551     if (res == OK) {
    552         res = mCaptureRequest.update(ANDROID_REQUEST_ID,
    553                 &mCaptureId, 1);
    554     }
    555     if (res == OK) {
    556         res = mCaptureRequest.sort();
    557     }
    558 
    559     if (res != OK) {
    560         ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)",
    561                 __FUNCTION__, client->getCameraId(), strerror(-res), res);
    562         return DONE;
    563     }
    564 
    565     // Create a capture copy since CameraDeviceBase#capture takes ownership
    566     CameraMetadata captureCopy = mCaptureRequest;
    567     if (captureCopy.entryCount() == 0) {
    568         ALOGE("%s: Camera %d: Unable to copy capture request for HAL device",
    569                 __FUNCTION__, client->getCameraId());
    570         return DONE;
    571     }
    572 
    573     if ((l.mParameters.isDeviceZslSupported) && (l.mParameters.state != Parameters::RECORD) &&
    574             (l.mParameters.state != Parameters::VIDEO_SNAPSHOT)) {
    575         // If device ZSL is supported, drop all pending preview buffers to reduce the chance of
    576         // rendering preview frames newer than the still frame.
    577         // Additionally, preview must not get interrupted during video recording.
    578         client->getCameraDevice()->dropStreamBuffers(true, client->getPreviewStreamId());
    579     }
    580 
    581     /**
    582      * Clear the streaming request for still-capture pictures
    583      *   (as opposed to i.e. video snapshots)
    584      */
    585     if (l.mParameters.state == Parameters::STILL_CAPTURE) {
    586         // API definition of takePicture() - stop preview before taking pic
    587         res = client->stopStream();
    588         if (res != OK) {
    589             ALOGE("%s: Camera %d: Unable to stop preview for still capture: "
    590                     "%s (%d)",
    591                     __FUNCTION__, client->getCameraId(), strerror(-res), res);
    592             return DONE;
    593         }
    594     }
    595 
    596     // TODO: Capture should be atomic with setStreamingRequest here
    597     res = client->getCameraDevice()->capture(captureCopy);
    598     if (res != OK) {
    599         ALOGE("%s: Camera %d: Unable to submit still image capture request: "
    600                 "%s (%d)",
    601                 __FUNCTION__, client->getCameraId(), strerror(-res), res);
    602         return DONE;
    603     }
    604 
    605     mTimeoutCount = kMaxTimeoutsForCaptureEnd;
    606     return STANDARD_CAPTURE_WAIT;
    607 }
    608 
    609 CaptureSequencer::CaptureState CaptureSequencer::manageStandardCaptureWait(
    610         sp<Camera2Client> &client) {
    611     status_t res;
    612     ATRACE_CALL();
    613     Mutex::Autolock l(mInputMutex);
    614 
    615 
    616     // Wait for shutter callback
    617     while (!mHalNotifiedShutter) {
    618         if (mTimeoutCount <= 0) {
    619             break;
    620         }
    621         res = mShutterNotifySignal.waitRelative(mInputMutex, kWaitDuration);
    622         if (res == TIMED_OUT) {
    623             mTimeoutCount--;
    624             return STANDARD_CAPTURE_WAIT;
    625         }
    626     }
    627 
    628     if (mHalNotifiedShutter) {
    629         if (!mShutterNotified) {
    630             SharedParameters::Lock l(client->getParameters());
    631             /* warning: this also locks a SharedCameraCallbacks */
    632             shutterNotifyLocked(l.mParameters, client);
    633             mShutterNotified = true;
    634         }
    635     } else if (mTimeoutCount <= 0) {
    636         ALOGW("Timed out waiting for shutter notification");
    637         return DONE;
    638     }
    639 
    640     // Wait for new metadata result (mNewFrame)
    641     while (!mNewFrameReceived) {
    642         res = mNewFrameSignal.waitRelative(mInputMutex, kWaitDuration);
    643         if (res == TIMED_OUT) {
    644             mTimeoutCount--;
    645             break;
    646         }
    647     }
    648 
    649     // Wait until jpeg was captured by JpegProcessor
    650     while (mNewFrameReceived && !mNewCaptureReceived) {
    651         res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration);
    652         if (res == TIMED_OUT) {
    653             mTimeoutCount--;
    654             break;
    655         }
    656     }
    657     if (mNewCaptureReceived) {
    658         if (mNewCaptureErrorCnt > kMaxRetryCount) {
    659             ALOGW("Exceeding multiple retry limit of %d due to buffer drop", kMaxRetryCount);
    660             return DONE;
    661         } else if (mNewCaptureErrorCnt > 0) {
    662             ALOGW("Capture error happened, retry %d...", mNewCaptureErrorCnt);
    663             mNewCaptureReceived = false;
    664             return STANDARD_CAPTURE;
    665         }
    666     }
    667 
    668     if (mTimeoutCount <= 0) {
    669         ALOGW("Timed out waiting for capture to complete");
    670         return DONE;
    671     }
    672     if (mNewFrameReceived && mNewCaptureReceived) {
    673 
    674         if (mNewFrameId != mCaptureId) {
    675             ALOGW("Mismatched capture frame IDs: Expected %d, got %d",
    676                     mCaptureId, mNewFrameId);
    677         }
    678         camera_metadata_entry_t entry;
    679         entry = mNewFrame.find(ANDROID_SENSOR_TIMESTAMP);
    680         if (entry.count == 0) {
    681             ALOGE("No timestamp field in capture frame!");
    682         } else if (entry.count == 1) {
    683             if (entry.data.i64[0] != mCaptureTimestamp) {
    684                 ALOGW("Mismatched capture timestamps: Metadata frame %" PRId64 ","
    685                         " captured buffer %" PRId64,
    686                         entry.data.i64[0],
    687                         mCaptureTimestamp);
    688             }
    689         } else {
    690             ALOGE("Timestamp metadata is malformed!");
    691         }
    692         client->removeFrameListener(mCaptureId, mCaptureId + 1, this);
    693 
    694         mNewFrameReceived = false;
    695         mNewCaptureReceived = false;
    696         return DONE;
    697     }
    698     return STANDARD_CAPTURE_WAIT;
    699 }
    700 
    701 status_t CaptureSequencer::updateCaptureRequest(const Parameters &params,
    702         sp<Camera2Client> &client) {
    703     ATRACE_CALL();
    704     status_t res;
    705     uint8_t captureIntent = static_cast<uint8_t>(ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE);
    706 
    707     if (mCaptureRequest.entryCount() == 0) {
    708         res = client->getCameraDevice()->createDefaultRequest(
    709                 CAMERA2_TEMPLATE_STILL_CAPTURE,
    710                 &mCaptureRequest);
    711         if (res != OK) {
    712             ALOGE("%s: Camera %d: Unable to create default still image request:"
    713                     " %s (%d)", __FUNCTION__, client->getCameraId(),
    714                     strerror(-res), res);
    715             return res;
    716         }
    717     }
    718 
    719     if (params.state == Parameters::VIDEO_SNAPSHOT) {
    720         captureIntent = static_cast<uint8_t>(ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
    721     }
    722     res = mCaptureRequest.update(ANDROID_CONTROL_CAPTURE_INTENT, &captureIntent, 1);
    723     if (res != OK) {
    724         ALOGE("%s: Camera %d: Unable to update capture intent: %s (%d)",
    725                 __FUNCTION__, client->getCameraId(), strerror(-res), res);
    726         return res;
    727     }
    728 
    729     res = params.updateRequest(&mCaptureRequest);
    730     if (res != OK) {
    731         ALOGE("%s: Camera %d: Unable to update common entries of capture "
    732                 "request: %s (%d)", __FUNCTION__, client->getCameraId(),
    733                 strerror(-res), res);
    734         return res;
    735     }
    736 
    737     res = params.updateRequestJpeg(&mCaptureRequest);
    738     if (res != OK) {
    739         ALOGE("%s: Camera %d: Unable to update JPEG entries of capture "
    740                 "request: %s (%d)", __FUNCTION__, client->getCameraId(),
    741                 strerror(-res), res);
    742         return res;
    743     }
    744 
    745     return OK;
    746 }
    747 
    748 /*static*/ void CaptureSequencer::shutterNotifyLocked(const Parameters &params,
    749             const sp<Camera2Client>& client) {
    750     ATRACE_CALL();
    751 
    752     if (params.state == Parameters::STILL_CAPTURE
    753         && params.playShutterSound) {
    754         client->getCameraService()->playSound(CameraService::SOUND_SHUTTER);
    755     }
    756 
    757     {
    758         Camera2Client::SharedCameraCallbacks::Lock
    759             l(client->mSharedCameraCallbacks);
    760 
    761         ALOGV("%s: Notifying of shutter close to client", __FUNCTION__);
    762         if (l.mRemoteCallback != 0) {
    763             // ShutterCallback
    764             l.mRemoteCallback->notifyCallback(CAMERA_MSG_SHUTTER,
    765                                             /*ext1*/0, /*ext2*/0);
    766 
    767             // RawCallback with null buffer
    768             l.mRemoteCallback->notifyCallback(CAMERA_MSG_RAW_IMAGE_NOTIFY,
    769                                             /*ext1*/0, /*ext2*/0);
    770         } else {
    771             ALOGV("%s: No client!", __FUNCTION__);
    772         }
    773     }
    774 }
    775 
    776 
    777 }; // namespace camera2
    778 }; // namespace android
    779