CaptureSequencer.cpp revision 216db7455a19a2f1a5b29e3a9610231365b6c778
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
33namespace android {
34namespace camera2 {
35
36/** Public members */
37
38CaptureSequencer::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
58CaptureSequencer::~CaptureSequencer() {
59    ALOGV("%s: Exit", __FUNCTION__);
60}
61
62void CaptureSequencer::setZslProcessor(wp<ZslProcessorInterface> 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    Mutex::Autolock l(mInputMutex);
115    if (!mHalNotifiedShutter && resultExtras.requestId == mShutterCaptureId) {
116        mHalNotifiedShutter = true;
117        mShutterNotifySignal.signal();
118    }
119}
120
121void 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
133void 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
147void 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
166const 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
184const 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
200bool 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
237CaptureSequencer::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
254CaptureSequencer::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
324CaptureSequencer::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
360CaptureSequencer::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
397CaptureSequencer::CaptureState CaptureSequencer::manageZslWaiting(
398        sp<Camera2Client> &/*client*/) {
399    ALOGV("%s", __FUNCTION__);
400    return DONE;
401}
402
403CaptureSequencer::CaptureState CaptureSequencer::manageZslReprocessing(
404        sp<Camera2Client> &/*client*/) {
405    ALOGV("%s", __FUNCTION__);
406    return START;
407}
408
409CaptureSequencer::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
445CaptureSequencer::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
491CaptureSequencer::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    outputStreams.push(client->getCaptureStreamId());
508
509    if (l.mParameters.previewCallbackFlags &
510            CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
511        outputStreams.push(client->getCallbackStreamId());
512    }
513
514    if (l.mParameters.state == Parameters::VIDEO_SNAPSHOT) {
515        outputStreams.push(client->getRecordingStreamId());
516        captureIntent = static_cast<uint8_t>(ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT);
517    }
518
519    res = mCaptureRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
520            outputStreams);
521    if (res == OK) {
522        res = mCaptureRequest.update(ANDROID_REQUEST_ID,
523                &mCaptureId, 1);
524    }
525    if (res == OK) {
526        res = mCaptureRequest.update(ANDROID_CONTROL_CAPTURE_INTENT,
527                &captureIntent, 1);
528    }
529    if (res == OK) {
530        res = mCaptureRequest.sort();
531    }
532
533    if (res != OK) {
534        ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)",
535                __FUNCTION__, client->getCameraId(), strerror(-res), res);
536        return DONE;
537    }
538
539    // Create a capture copy since CameraDeviceBase#capture takes ownership
540    CameraMetadata captureCopy = mCaptureRequest;
541    if (captureCopy.entryCount() == 0) {
542        ALOGE("%s: Camera %d: Unable to copy capture request for HAL device",
543                __FUNCTION__, client->getCameraId());
544        return DONE;
545    }
546
547    /**
548     * Clear the streaming request for still-capture pictures
549     *   (as opposed to i.e. video snapshots)
550     */
551    if (l.mParameters.state == Parameters::STILL_CAPTURE) {
552        // API definition of takePicture() - stop preview before taking pic
553        res = client->stopStream();
554        if (res != OK) {
555            ALOGE("%s: Camera %d: Unable to stop preview for still capture: "
556                    "%s (%d)",
557                    __FUNCTION__, client->getCameraId(), strerror(-res), res);
558            return DONE;
559        }
560    }
561
562    // TODO: Capture should be atomic with setStreamingRequest here
563    res = client->getCameraDevice()->capture(captureCopy);
564    if (res != OK) {
565        ALOGE("%s: Camera %d: Unable to submit still image capture request: "
566                "%s (%d)",
567                __FUNCTION__, client->getCameraId(), strerror(-res), res);
568        return DONE;
569    }
570
571    mTimeoutCount = kMaxTimeoutsForCaptureEnd;
572    return STANDARD_CAPTURE_WAIT;
573}
574
575CaptureSequencer::CaptureState CaptureSequencer::manageStandardCaptureWait(
576        sp<Camera2Client> &client) {
577    status_t res;
578    ATRACE_CALL();
579    Mutex::Autolock l(mInputMutex);
580
581
582    // Wait for shutter callback
583    while (!mHalNotifiedShutter) {
584        if (mTimeoutCount <= 0) {
585            break;
586        }
587        res = mShutterNotifySignal.waitRelative(mInputMutex, kWaitDuration);
588        if (res == TIMED_OUT) {
589            mTimeoutCount--;
590            return STANDARD_CAPTURE_WAIT;
591        }
592    }
593
594    if (mHalNotifiedShutter) {
595        if (!mShutterNotified) {
596            SharedParameters::Lock l(client->getParameters());
597            /* warning: this also locks a SharedCameraCallbacks */
598            shutterNotifyLocked(l.mParameters, client, mMsgType);
599            mShutterNotified = true;
600        }
601    } else if (mTimeoutCount <= 0) {
602        ALOGW("Timed out waiting for shutter notification");
603        return DONE;
604    }
605
606    // Wait for new metadata result (mNewFrame)
607    while (!mNewFrameReceived) {
608        res = mNewFrameSignal.waitRelative(mInputMutex, kWaitDuration);
609        if (res == TIMED_OUT) {
610            mTimeoutCount--;
611            break;
612        }
613    }
614
615    // Wait until jpeg was captured by JpegProcessor
616    while (mNewFrameReceived && !mNewCaptureReceived) {
617        res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration);
618        if (res == TIMED_OUT) {
619            mTimeoutCount--;
620            break;
621        }
622    }
623    if (mTimeoutCount <= 0) {
624        ALOGW("Timed out waiting for capture to complete");
625        return DONE;
626    }
627    if (mNewFrameReceived && mNewCaptureReceived) {
628
629        if (mNewFrameId != mCaptureId) {
630            ALOGW("Mismatched capture frame IDs: Expected %d, got %d",
631                    mCaptureId, mNewFrameId);
632        }
633        camera_metadata_entry_t entry;
634        entry = mNewFrame.find(ANDROID_SENSOR_TIMESTAMP);
635        if (entry.count == 0) {
636            ALOGE("No timestamp field in capture frame!");
637        } else if (entry.count == 1) {
638            if (entry.data.i64[0] != mCaptureTimestamp) {
639                ALOGW("Mismatched capture timestamps: Metadata frame %" PRId64 ","
640                        " captured buffer %" PRId64,
641                        entry.data.i64[0],
642                        mCaptureTimestamp);
643            }
644        } else {
645            ALOGE("Timestamp metadata is malformed!");
646        }
647        client->removeFrameListener(mCaptureId, mCaptureId + 1, this);
648
649        mNewFrameReceived = false;
650        mNewCaptureReceived = false;
651        return DONE;
652    }
653    return STANDARD_CAPTURE_WAIT;
654}
655
656CaptureSequencer::CaptureState CaptureSequencer::manageBurstCaptureStart(
657        sp<Camera2Client> &client) {
658    ALOGV("%s", __FUNCTION__);
659    status_t res;
660    ATRACE_CALL();
661
662    // check which burst mode is set, create respective burst object
663    {
664        SharedParameters::Lock l(client->getParameters());
665
666        res = updateCaptureRequest(l.mParameters, client);
667        if(res != OK) {
668            return DONE;
669        }
670
671        //
672        // check for burst mode type in mParameters here
673        //
674        mBurstCapture = new BurstCapture(client, this);
675    }
676
677    res = mCaptureRequest.update(ANDROID_REQUEST_ID, &mCaptureId, 1);
678    if (res == OK) {
679        res = mCaptureRequest.sort();
680    }
681    if (res != OK) {
682        ALOGE("%s: Camera %d: Unable to set up still capture request: %s (%d)",
683                __FUNCTION__, client->getCameraId(), strerror(-res), res);
684        return DONE;
685    }
686
687    CameraMetadata captureCopy = mCaptureRequest;
688    if (captureCopy.entryCount() == 0) {
689        ALOGE("%s: Camera %d: Unable to copy capture request for HAL device",
690                __FUNCTION__, client->getCameraId());
691        return DONE;
692    }
693
694    Vector<CameraMetadata> requests;
695    requests.push(mCaptureRequest);
696    res = mBurstCapture->start(requests, mCaptureId);
697    mTimeoutCount = kMaxTimeoutsForCaptureEnd * 10;
698    return BURST_CAPTURE_WAIT;
699}
700
701CaptureSequencer::CaptureState CaptureSequencer::manageBurstCaptureWait(
702        sp<Camera2Client> &/*client*/) {
703    status_t res;
704    ATRACE_CALL();
705    while (!mNewCaptureReceived) {
706        res = mNewCaptureSignal.waitRelative(mInputMutex, kWaitDuration);
707        if (res == TIMED_OUT) {
708            mTimeoutCount--;
709            break;
710        }
711    }
712
713    if (mTimeoutCount <= 0) {
714        ALOGW("Timed out waiting for burst capture to complete");
715        return DONE;
716    }
717    if (mNewCaptureReceived) {
718        mNewCaptureReceived = false;
719        // TODO: update mCaptureId to last burst's capture ID + 1?
720        return DONE;
721    }
722
723    return BURST_CAPTURE_WAIT;
724}
725
726status_t CaptureSequencer::updateCaptureRequest(const Parameters &params,
727        sp<Camera2Client> &client) {
728    ATRACE_CALL();
729    status_t res;
730    if (mCaptureRequest.entryCount() == 0) {
731        res = client->getCameraDevice()->createDefaultRequest(
732                CAMERA2_TEMPLATE_STILL_CAPTURE,
733                &mCaptureRequest);
734        if (res != OK) {
735            ALOGE("%s: Camera %d: Unable to create default still image request:"
736                    " %s (%d)", __FUNCTION__, client->getCameraId(),
737                    strerror(-res), res);
738            return res;
739        }
740    }
741
742    res = params.updateRequest(&mCaptureRequest);
743    if (res != OK) {
744        ALOGE("%s: Camera %d: Unable to update common entries of capture "
745                "request: %s (%d)", __FUNCTION__, client->getCameraId(),
746                strerror(-res), res);
747        return res;
748    }
749
750    res = params.updateRequestJpeg(&mCaptureRequest);
751    if (res != OK) {
752        ALOGE("%s: Camera %d: Unable to update JPEG entries of capture "
753                "request: %s (%d)", __FUNCTION__, client->getCameraId(),
754                strerror(-res), res);
755        return res;
756    }
757
758    return OK;
759}
760
761/*static*/ void CaptureSequencer::shutterNotifyLocked(const Parameters &params,
762            sp<Camera2Client> client, int msgType) {
763    ATRACE_CALL();
764
765    if (params.state == Parameters::STILL_CAPTURE
766        && params.playShutterSound
767        && (msgType & CAMERA_MSG_SHUTTER)) {
768        client->getCameraService()->playSound(CameraService::SOUND_SHUTTER);
769    }
770
771    {
772        Camera2Client::SharedCameraCallbacks::Lock
773            l(client->mSharedCameraCallbacks);
774
775        ALOGV("%s: Notifying of shutter close to client", __FUNCTION__);
776        if (l.mRemoteCallback != 0) {
777            // ShutterCallback
778            l.mRemoteCallback->notifyCallback(CAMERA_MSG_SHUTTER,
779                                            /*ext1*/0, /*ext2*/0);
780
781            // RawCallback with null buffer
782            l.mRemoteCallback->notifyCallback(CAMERA_MSG_RAW_IMAGE_NOTIFY,
783                                            /*ext1*/0, /*ext2*/0);
784        } else {
785            ALOGV("%s: No client!", __FUNCTION__);
786        }
787    }
788}
789
790
791}; // namespace camera2
792}; // namespace android
793