CaptureSequencer.h revision cb0652e5a850b2fcd919e977247e87239efaf70e
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    // Notification from the frame processor
66    virtual void onResultAvailable(const CaptureResult &result);
67
68    // Notifications from the JPEG processor
69    void onCaptureAvailable(nsecs_t timestamp, sp<MemoryBase> captureBuffer);
70
71    void dump(int fd, const Vector<String16>& args);
72
73  private:
74    /**
75     * Accessed by other threads
76     */
77    Mutex mInputMutex;
78
79    bool mStartCapture;
80    bool mBusy;
81    Condition mStartCaptureSignal;
82
83    bool mNewAEState;
84    uint8_t mAEState;
85    int mAETriggerId;
86    Condition mNewNotifySignal;
87
88    bool mNewFrameReceived;
89    int32_t mNewFrameId;
90    CameraMetadata mNewFrame;
91    Condition mNewFrameSignal;
92
93    bool mNewCaptureReceived;
94    nsecs_t mCaptureTimestamp;
95    sp<MemoryBase> mCaptureBuffer;
96    Condition mNewCaptureSignal;
97
98    bool mShutterNotified;
99
100    /**
101     * Internal to CaptureSequencer
102     */
103    static const nsecs_t kWaitDuration = 100000000; // 100 ms
104    static const int kMaxTimeoutsForPrecaptureStart = 10; // 1 sec
105    static const int kMaxTimeoutsForPrecaptureEnd = 20;  // 2 sec
106    static const int kMaxTimeoutsForCaptureEnd    = 40;  // 4 sec
107
108    wp<Camera2Client> mClient;
109    wp<ZslProcessorInterface> mZslProcessor;
110    sp<BurstCapture> mBurstCapture;
111
112    enum CaptureState {
113        IDLE,
114        START,
115        ZSL_START,
116        ZSL_WAITING,
117        ZSL_REPROCESSING,
118        STANDARD_START,
119        STANDARD_PRECAPTURE_WAIT,
120        STANDARD_CAPTURE,
121        STANDARD_CAPTURE_WAIT,
122        BURST_CAPTURE_START,
123        BURST_CAPTURE_WAIT,
124        DONE,
125        ERROR,
126        NUM_CAPTURE_STATES
127    } mCaptureState;
128    static const char* kStateNames[];
129    int mStateTransitionCount;
130    Mutex mStateMutex; // Guards mCaptureState
131    Condition mStateChanged;
132
133    typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
134    static const StateManager kStateManagers[];
135
136    CameraMetadata mCaptureRequest;
137
138    int mTriggerId;
139    int mTimeoutCount;
140    bool mAeInPrecapture;
141
142    int32_t mCaptureId;
143    int mMsgType;
144
145    // Main internal methods
146
147    virtual bool threadLoop();
148
149    CaptureState manageIdle(sp<Camera2Client> &client);
150    CaptureState manageStart(sp<Camera2Client> &client);
151
152    CaptureState manageZslStart(sp<Camera2Client> &client);
153    CaptureState manageZslWaiting(sp<Camera2Client> &client);
154    CaptureState manageZslReprocessing(sp<Camera2Client> &client);
155
156    CaptureState manageStandardStart(sp<Camera2Client> &client);
157    CaptureState manageStandardPrecaptureWait(sp<Camera2Client> &client);
158    CaptureState manageStandardCapture(sp<Camera2Client> &client);
159    CaptureState manageStandardCaptureWait(sp<Camera2Client> &client);
160
161    CaptureState manageBurstCaptureStart(sp<Camera2Client> &client);
162    CaptureState manageBurstCaptureWait(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