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