CaptureSequencer.h revision da6665cbd06ca58d3357c3002b7366d13e23f152
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 <utils/Thread.h>
21#include <utils/String16.h>
22#include <utils/Vector.h>
23#include <utils/Mutex.h>
24#include <utils/Condition.h>
25#include "CameraMetadata.h"
26#include "Parameters.h"
27#include "FrameProcessor.h"
28
29namespace android {
30
31class Camera2Client;
32
33namespace camera2 {
34
35class ZslProcessor;
36
37/**
38 * Manages the still image capture process for
39 * zero-shutter-lag, regular, and video snapshots.
40 */
41class CaptureSequencer:
42            virtual public Thread,
43            virtual public FrameProcessor::FilteredListener {
44  public:
45    CaptureSequencer(wp<Camera2Client> client);
46    ~CaptureSequencer();
47
48    // Get reference to the ZslProcessor, which holds the ZSL buffers and frames
49    void setZslProcessor(wp<ZslProcessor> processor);
50
51    // Begin still image capture
52    status_t startCapture();
53
54    // Notifications about AE state changes
55    void notifyAutoExposure(uint8_t newState, int triggerId);
56
57    // Notifications from the frame processor
58    virtual void onFrameAvailable(int32_t frameId, CameraMetadata &frame);
59
60    // Notifications from the capture processor
61    void onCaptureAvailable(nsecs_t timestamp);
62
63    void dump(int fd, const Vector<String16>& args);
64
65  private:
66    /**
67     * Accessed by other threads
68     */
69    Mutex mInputMutex;
70
71    bool mStartCapture;
72    bool mBusy;
73    Condition mStartCaptureSignal;
74
75    bool mNewAEState;
76    uint8_t mAEState;
77    int mAETriggerId;
78    Condition mNewNotifySignal;
79
80    bool mNewFrameReceived;
81    int32_t mNewFrameId;
82    CameraMetadata mNewFrame;
83    Condition mNewFrameSignal;
84
85    bool mNewCaptureReceived;
86    nsecs_t mCaptureTimestamp;
87    Condition mNewCaptureSignal;
88
89    /**
90     * Internal to CaptureSequencer
91     */
92    static const nsecs_t kWaitDuration = 100000000; // 100 ms
93    static const int kMaxTimeoutsForPrecaptureStart = 2; // 200 ms
94    static const int kMaxTimeoutsForPrecaptureEnd = 10;  // 1 sec
95    static const int kMaxTimeoutsForCaptureEnd    = 20;  // 2 sec
96
97    wp<Camera2Client> mClient;
98    wp<ZslProcessor> mZslProcessor;
99
100    enum CaptureState {
101        IDLE,
102        START,
103        ZSL_START,
104        ZSL_WAITING,
105        ZSL_REPROCESSING,
106        STANDARD_START,
107        STANDARD_PRECAPTURE_WAIT,
108        STANDARD_CAPTURE,
109        STANDARD_CAPTURE_WAIT,
110        DONE,
111        ERROR,
112        NUM_CAPTURE_STATES
113    } mCaptureState;
114    static const char* kStateNames[];
115
116    typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
117    static const StateManager kStateManagers[];
118
119    CameraMetadata mCaptureRequest;
120
121    int mTriggerId;
122    int mTimeoutCount;
123    bool mAeInPrecapture;
124
125    int32_t mCaptureId;
126
127    // Main internal methods
128
129    virtual bool threadLoop();
130
131    CaptureState manageIdle(sp<Camera2Client> &client);
132    CaptureState manageStart(sp<Camera2Client> &client);
133
134    CaptureState manageZslStart(sp<Camera2Client> &client);
135    CaptureState manageZslWaiting(sp<Camera2Client> &client);
136    CaptureState manageZslReprocessing(sp<Camera2Client> &client);
137
138    CaptureState manageStandardStart(sp<Camera2Client> &client);
139    CaptureState manageStandardPrecaptureWait(sp<Camera2Client> &client);
140    CaptureState manageStandardCapture(sp<Camera2Client> &client);
141    CaptureState manageStandardCaptureWait(sp<Camera2Client> &client);
142
143    CaptureState manageDone(sp<Camera2Client> &client);
144
145    // Utility methods
146
147    status_t updateCaptureRequest(const Parameters &params,
148            sp<Camera2Client> &client);
149};
150
151}; // namespace camera2
152}; // namespace android
153
154#endif
155