CaptureSequencer.h revision 2b07e0207da44d7b3cc63c369fd10c9f12a5e2cd
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 "Parameters.h"
28#include "FrameProcessor.h"
29
30namespace android {
31
32class Camera2Client;
33
34namespace camera2 {
35
36class ZslProcessorInterface;
37class BurstCapture;
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<ZslProcessorInterface> 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 from the frame processor
65    virtual void onFrameAvailable(int32_t requestId, const CameraMetadata &frame);
66
67    // Notifications from the JPEG processor
68    void onCaptureAvailable(nsecs_t timestamp, sp<MemoryBase> captureBuffer);
69
70    void dump(int fd, const Vector<String16>& args);
71
72  private:
73    /**
74     * Accessed by other threads
75     */
76    Mutex mInputMutex;
77
78    bool mStartCapture;
79    bool mBusy;
80    Condition mStartCaptureSignal;
81
82    bool mNewAEState;
83    uint8_t mAEState;
84    int mAETriggerId;
85    Condition mNewNotifySignal;
86
87    bool mNewFrameReceived;
88    int32_t mNewFrameId;
89    CameraMetadata mNewFrame;
90    Condition mNewFrameSignal;
91
92    bool mNewCaptureReceived;
93    nsecs_t mCaptureTimestamp;
94    sp<MemoryBase> mCaptureBuffer;
95    Condition mNewCaptureSignal;
96
97    bool mShutterNotified;
98
99    /**
100     * Internal to CaptureSequencer
101     */
102    static const nsecs_t kWaitDuration = 100000000; // 100 ms
103    static const int kMaxTimeoutsForPrecaptureStart = 10; // 1 sec
104    static const int kMaxTimeoutsForPrecaptureEnd = 20;  // 2 sec
105    static const int kMaxTimeoutsForCaptureEnd    = 40;  // 4 sec
106
107    wp<Camera2Client> mClient;
108    wp<ZslProcessorInterface> mZslProcessor;
109    sp<BurstCapture> mBurstCapture;
110
111    enum CaptureState {
112        IDLE,
113        START,
114        ZSL_START,
115        ZSL_WAITING,
116        ZSL_REPROCESSING,
117        STANDARD_START,
118        STANDARD_PRECAPTURE_WAIT,
119        STANDARD_CAPTURE,
120        STANDARD_CAPTURE_WAIT,
121        BURST_CAPTURE_START,
122        BURST_CAPTURE_WAIT,
123        DONE,
124        ERROR,
125        NUM_CAPTURE_STATES
126    } mCaptureState;
127    static const char* kStateNames[];
128    int mStateTransitionCount;
129    Mutex mStateMutex; // Guards mCaptureState
130    Condition mStateChanged;
131
132    typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
133    static const StateManager kStateManagers[];
134
135    CameraMetadata mCaptureRequest;
136
137    int mTriggerId;
138    int mTimeoutCount;
139    bool mAeInPrecapture;
140
141    int32_t mCaptureId;
142    int mMsgType;
143
144    // Main internal methods
145
146    virtual bool threadLoop();
147
148    CaptureState manageIdle(sp<Camera2Client> &client);
149    CaptureState manageStart(sp<Camera2Client> &client);
150
151    CaptureState manageZslStart(sp<Camera2Client> &client);
152    CaptureState manageZslWaiting(sp<Camera2Client> &client);
153    CaptureState manageZslReprocessing(sp<Camera2Client> &client);
154
155    CaptureState manageStandardStart(sp<Camera2Client> &client);
156    CaptureState manageStandardPrecaptureWait(sp<Camera2Client> &client);
157    CaptureState manageStandardCapture(sp<Camera2Client> &client);
158    CaptureState manageStandardCaptureWait(sp<Camera2Client> &client);
159
160    CaptureState manageBurstCaptureStart(sp<Camera2Client> &client);
161    CaptureState manageBurstCaptureWait(sp<Camera2Client> &client);
162
163    CaptureState manageDone(sp<Camera2Client> &client);
164
165    // Utility methods
166
167    status_t updateCaptureRequest(const Parameters &params,
168            sp<Camera2Client> &client);
169
170    // Emit Shutter/Raw callback to java, and maybe play a shutter sound
171    static void shutterNotifyLocked(const Parameters &params,
172            sp<Camera2Client> client, int msgType);
173};
174
175}; // namespace camera2
176}; // namespace android
177
178#endif
179