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 ZslProcessorInterface;
38class BurstCapture;
39
40/**
41 * Manages the still image capture process for
42 * zero-shutter-lag, regular, and video snapshots.
43 */
44class CaptureSequencer:
45            virtual public Thread,
46            virtual public FrameProcessor::FilteredListener {
47  public:
48    CaptureSequencer(wp<Camera2Client> client);
49    ~CaptureSequencer();
50
51    // Get reference to the ZslProcessor, which holds the ZSL buffers and frames
52    void setZslProcessor(wp<ZslProcessorInterface> processor);
53
54    // Begin still image capture
55    status_t startCapture(int msgType);
56
57    // Wait until current image capture completes; returns immediately if no
58    // capture is active. Returns TIMED_OUT if capture does not complete during
59    // the specified duration.
60    status_t waitUntilIdle(nsecs_t timeout);
61
62    // Notifications about AE state changes
63    void notifyAutoExposure(uint8_t newState, int triggerId);
64
65    // Notifications about shutter (capture start)
66    void notifyShutter(const CaptureResultExtras& resultExtras,
67                       nsecs_t timestamp);
68
69    // Notification from the frame processor
70    virtual void onResultAvailable(const CaptureResult &result);
71
72    // Notifications from the JPEG processor
73    void onCaptureAvailable(nsecs_t timestamp, sp<MemoryBase> captureBuffer);
74
75    void dump(int fd, const Vector<String16>& args);
76
77  private:
78    /**
79     * Accessed by other threads
80     */
81    Mutex mInputMutex;
82
83    bool mStartCapture;
84    bool mBusy;
85    Condition mStartCaptureSignal;
86
87    bool mNewAEState;
88    uint8_t mAEState;
89    int mAETriggerId;
90    Condition mNewNotifySignal;
91
92    bool mNewFrameReceived;
93    int32_t mNewFrameId;
94    CameraMetadata mNewFrame;
95    Condition mNewFrameSignal;
96
97    bool mNewCaptureReceived;
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
115    wp<Camera2Client> mClient;
116    wp<ZslProcessorInterface> mZslProcessor;
117    sp<BurstCapture> mBurstCapture;
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        BURST_CAPTURE_START,
130        BURST_CAPTURE_WAIT,
131        DONE,
132        ERROR,
133        NUM_CAPTURE_STATES
134    } mCaptureState;
135    static const char* kStateNames[];
136    int mStateTransitionCount;
137    Mutex mStateMutex; // Guards mCaptureState
138    Condition mStateChanged;
139
140    typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
141    static const StateManager kStateManagers[];
142
143    CameraMetadata mCaptureRequest;
144
145    int mTriggerId;
146    int mTimeoutCount;
147    bool mAeInPrecapture;
148
149    int32_t mCaptureId;
150    int mMsgType;
151
152    // Main internal methods
153
154    virtual bool threadLoop();
155
156    CaptureState manageIdle(sp<Camera2Client> &client);
157    CaptureState manageStart(sp<Camera2Client> &client);
158
159    CaptureState manageZslStart(sp<Camera2Client> &client);
160    CaptureState manageZslWaiting(sp<Camera2Client> &client);
161    CaptureState manageZslReprocessing(sp<Camera2Client> &client);
162
163    CaptureState manageStandardStart(sp<Camera2Client> &client);
164    CaptureState manageStandardPrecaptureWait(sp<Camera2Client> &client);
165    CaptureState manageStandardCapture(sp<Camera2Client> &client);
166    CaptureState manageStandardCaptureWait(sp<Camera2Client> &client);
167
168    CaptureState manageBurstCaptureStart(sp<Camera2Client> &client);
169    CaptureState manageBurstCaptureWait(sp<Camera2Client> &client);
170
171    CaptureState manageDone(sp<Camera2Client> &client);
172
173    // Utility methods
174
175    status_t updateCaptureRequest(const Parameters &params,
176            sp<Camera2Client> &client);
177
178    // Emit Shutter/Raw callback to java, and maybe play a shutter sound
179    static void shutterNotifyLocked(const Parameters &params,
180            sp<Camera2Client> client, int msgType);
181};
182
183}; // namespace camera2
184}; // namespace android
185
186#endif
187