CaptureSequencer.h revision d309fb9c8a2c4564d88fffba19c4e3688e4b862b
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    CaptureSequencer(wp<Camera2Client> client);
48    ~CaptureSequencer();
49
50    // Get reference to the ZslProcessor, which holds the ZSL buffers and frames
51    void setZslProcessor(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, sp<MemoryBase> captureBuffer);
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    nsecs_t mCaptureTimestamp;
98    sp<MemoryBase> mCaptureBuffer;
99    Condition mNewCaptureSignal;
100
101    bool mShutterNotified; // Has CaptureSequencer sent shutter to Client
102    bool mHalNotifiedShutter; // Has HAL sent shutter to CaptureSequencer
103    int32_t mShutterCaptureId; // The captureId which is waiting for shutter notification
104    Condition mShutterNotifySignal;
105
106    /**
107     * Internal to CaptureSequencer
108     */
109    static const nsecs_t kWaitDuration = 100000000; // 100 ms
110    static const int kMaxTimeoutsForPrecaptureStart = 10; // 1 sec
111    static const int kMaxTimeoutsForPrecaptureEnd = 20;  // 2 sec
112    static const int kMaxTimeoutsForCaptureEnd    = 40;  // 4 sec
113
114    wp<Camera2Client> mClient;
115    wp<ZslProcessor> mZslProcessor;
116
117    enum CaptureState {
118        IDLE,
119        START,
120        ZSL_START,
121        ZSL_WAITING,
122        ZSL_REPROCESSING,
123        STANDARD_START,
124        STANDARD_PRECAPTURE_WAIT,
125        STANDARD_CAPTURE,
126        STANDARD_CAPTURE_WAIT,
127        DONE,
128        ERROR,
129        NUM_CAPTURE_STATES
130    } mCaptureState;
131    static const char* kStateNames[];
132    int mStateTransitionCount;
133    Mutex mStateMutex; // Guards mCaptureState
134    Condition mStateChanged;
135
136    typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
137    static const StateManager kStateManagers[];
138
139    CameraMetadata mCaptureRequest;
140
141    int mTriggerId;
142    int mTimeoutCount;
143    bool mAeInPrecapture;
144
145    int32_t mCaptureId;
146    int mMsgType;
147
148    // Main internal methods
149
150    virtual bool threadLoop();
151
152    CaptureState manageIdle(sp<Camera2Client> &client);
153    CaptureState manageStart(sp<Camera2Client> &client);
154
155    CaptureState manageZslStart(sp<Camera2Client> &client);
156    CaptureState manageZslWaiting(sp<Camera2Client> &client);
157    CaptureState manageZslReprocessing(sp<Camera2Client> &client);
158
159    CaptureState manageStandardStart(sp<Camera2Client> &client);
160    CaptureState manageStandardPrecaptureWait(sp<Camera2Client> &client);
161    CaptureState manageStandardCapture(sp<Camera2Client> &client);
162    CaptureState manageStandardCaptureWait(sp<Camera2Client> &client);
163
164    CaptureState manageDone(sp<Camera2Client> &client);
165
166    // Utility methods
167
168    status_t updateCaptureRequest(const Parameters &params,
169            sp<Camera2Client> &client);
170
171    // Emit Shutter/Raw callback to java, and maybe play a shutter sound
172    static void shutterNotifyLocked(const Parameters &params,
173            sp<Camera2Client> client, int msgType);
174};
175
176}; // namespace camera2
177}; // namespace android
178
179#endif
180