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