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#ifndef ANDROID_SERVERS_CAMERA_CAMERA2_CAPTURESEQUENCER_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2_CAPTURESEQUENCER_H
19
20#include <binder/MemoryBase.h>
21#include <utils/Thread.h>
22#include <utils/String16.h>
23#include <utils/Vector.h>
24#include <utils/Mutex.h>
25#include <utils/Condition.h>
26#include "camera/CameraMetadata.h"
27#include "camera/CaptureResult.h"
28#include "Parameters.h"
29#include "FrameProcessor.h"
30
31namespace android {
32
33class Camera2Client;
34
35namespace camera2 {
36
37class ZslProcessor;
38
39/**
40 * Manages the still image capture process for
41 * zero-shutter-lag, regular, and video snapshots.
42 */
43class CaptureSequencer:
44            virtual public Thread,
45            virtual public FrameProcessor::FilteredListener {
46  public:
47    explicit CaptureSequencer(wp<Camera2Client> client);
48    ~CaptureSequencer();
49
50    // Get reference to the ZslProcessor, which holds the ZSL buffers and frames
51    void setZslProcessor(const wp<ZslProcessor>& processor);
52
53    // Begin still image capture
54    status_t startCapture(int msgType);
55
56    // Wait until current image capture completes; returns immediately if no
57    // capture is active. Returns TIMED_OUT if capture does not complete during
58    // the specified duration.
59    status_t waitUntilIdle(nsecs_t timeout);
60
61    // Notifications about AE state changes
62    void notifyAutoExposure(uint8_t newState, int triggerId);
63
64    // Notifications about shutter (capture start)
65    void notifyShutter(const CaptureResultExtras& resultExtras,
66                       nsecs_t timestamp);
67
68    // Notification from the frame processor
69    virtual void onResultAvailable(const CaptureResult &result);
70
71    // Notifications from the JPEG processor
72    void onCaptureAvailable(nsecs_t timestamp, const sp<MemoryBase>& captureBuffer, bool captureError);
73
74    void dump(int fd, const Vector<String16>& args);
75
76  private:
77    /**
78     * Accessed by other threads
79     */
80    Mutex mInputMutex;
81
82    bool mStartCapture;
83    bool mBusy;
84    Condition mStartCaptureSignal;
85
86    bool mNewAEState;
87    uint8_t mAEState;
88    int mAETriggerId;
89    Condition mNewNotifySignal;
90
91    bool mNewFrameReceived;
92    int32_t mNewFrameId;
93    CameraMetadata mNewFrame;
94    Condition mNewFrameSignal;
95
96    bool mNewCaptureReceived;
97    int32_t mNewCaptureErrorCnt;
98    nsecs_t mCaptureTimestamp;
99    sp<MemoryBase> mCaptureBuffer;
100    Condition mNewCaptureSignal;
101
102    bool mShutterNotified; // Has CaptureSequencer sent shutter to Client
103    bool mHalNotifiedShutter; // Has HAL sent shutter to CaptureSequencer
104    int32_t mShutterCaptureId; // The captureId which is waiting for shutter notification
105    Condition mShutterNotifySignal;
106
107    /**
108     * Internal to CaptureSequencer
109     */
110    static const nsecs_t kWaitDuration = 100000000; // 100 ms
111    static const int kMaxTimeoutsForPrecaptureStart = 10; // 1 sec
112    static const int kMaxTimeoutsForPrecaptureEnd = 20;  // 2 sec
113    static const int kMaxTimeoutsForCaptureEnd    = 40;  // 4 sec
114    static const int kMaxRetryCount = 3; // 3 retries in case of buffer drop
115
116    wp<Camera2Client> mClient;
117    wp<ZslProcessor> mZslProcessor;
118
119    enum CaptureState {
120        IDLE,
121        START,
122        ZSL_START,
123        ZSL_WAITING,
124        ZSL_REPROCESSING,
125        STANDARD_START,
126        STANDARD_PRECAPTURE_WAIT,
127        STANDARD_CAPTURE,
128        STANDARD_CAPTURE_WAIT,
129        DONE,
130        ERROR,
131        NUM_CAPTURE_STATES
132    } mCaptureState;
133    static const char* kStateNames[];
134    int mStateTransitionCount;
135    Mutex mStateMutex; // Guards mCaptureState
136    Condition mStateChanged;
137
138    typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
139    static const StateManager kStateManagers[];
140
141    CameraMetadata mCaptureRequest;
142
143    int mTriggerId;
144    int mTimeoutCount;
145    bool mAeInPrecapture;
146
147    int32_t mCaptureId;
148    int mMsgType;
149
150    // Main internal methods
151
152    virtual bool threadLoop();
153
154    CaptureState manageIdle(sp<Camera2Client> &client);
155    CaptureState manageStart(sp<Camera2Client> &client);
156
157    CaptureState manageZslStart(sp<Camera2Client> &client);
158    CaptureState manageZslWaiting(sp<Camera2Client> &client);
159    CaptureState manageZslReprocessing(sp<Camera2Client> &client);
160
161    CaptureState manageStandardStart(sp<Camera2Client> &client);
162    CaptureState manageStandardPrecaptureWait(sp<Camera2Client> &client);
163    CaptureState manageStandardCapture(sp<Camera2Client> &client);
164    CaptureState manageStandardCaptureWait(sp<Camera2Client> &client);
165
166    CaptureState manageDone(sp<Camera2Client> &client);
167
168    // Utility methods
169
170    status_t updateCaptureRequest(const Parameters &params,
171            sp<Camera2Client> &client);
172
173    // Emit Shutter/Raw callback to java, and maybe play a shutter sound
174    static void shutterNotifyLocked(const Parameters &params,
175            const sp<Camera2Client>& client, int msgType);
176};
177
178}; // namespace camera2
179}; // namespace android
180
181#endif
182