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