Home | History | Annotate | Download | only in client2
      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 
     31 namespace android {
     32 
     33 class Camera2Client;
     34 
     35 namespace camera2 {
     36 
     37 class ZslProcessor;
     38 
     39 /**
     40  * Manages the still image capture process for
     41  * zero-shutter-lag, regular, and video snapshots.
     42  */
     43 class CaptureSequencer:
     44             virtual public Thread,
     45             virtual public FrameProcessor::FilteredListener {
     46   public:
     47     explicit CaptureSequencer(wp<Camera2Client> client);
     48     ~CaptureSequencer();
     49 
     50     // Get reference to the ZslProcessor, which holds the ZSL buffers and frames
     51     void setZslProcessor(const wp<ZslProcessor>& processor);
     52 
     53     // Begin still image capture
     54     status_t startCapture();
     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     // Notifications about shutter (capture start)
     69     void notifyError(int32_t errorCode, const CaptureResultExtras& resultExtras);
     70 
     71     // Notification from the frame processor
     72     virtual void onResultAvailable(const CaptureResult &result);
     73 
     74     // Notifications from the JPEG processor
     75     void onCaptureAvailable(nsecs_t timestamp, const sp<MemoryBase>& captureBuffer, bool captureError);
     76 
     77     void dump(int fd, const Vector<String16>& args);
     78 
     79   private:
     80     /**
     81      * Accessed by other threads
     82      */
     83     Mutex mInputMutex;
     84 
     85     bool mStartCapture;
     86     bool mBusy;
     87     Condition mStartCaptureSignal;
     88 
     89     bool mNewAEState;
     90     uint8_t mAEState;
     91     int mAETriggerId;
     92     Condition mNewNotifySignal;
     93 
     94     bool mNewFrameReceived;
     95     int32_t mNewFrameId;
     96     CameraMetadata mNewFrame;
     97     Condition mNewFrameSignal;
     98 
     99     bool mNewCaptureReceived;
    100     int32_t mNewCaptureErrorCnt;
    101     nsecs_t mCaptureTimestamp;
    102     sp<MemoryBase> mCaptureBuffer;
    103     Condition mNewCaptureSignal;
    104 
    105     bool mShutterNotified; // Has CaptureSequencer sent shutter to Client
    106     bool mHalNotifiedShutter; // Has HAL sent shutter to CaptureSequencer
    107     int32_t mShutterCaptureId; // The captureId which is waiting for shutter notification
    108     Condition mShutterNotifySignal;
    109 
    110     /**
    111      * Internal to CaptureSequencer
    112      */
    113     static const nsecs_t kWaitDuration = 100000000; // 100 ms
    114     static const int kMaxTimeoutsForPrecaptureStart = 10; // 1 sec
    115     static const int kMaxTimeoutsForPrecaptureEnd = 20;  // 2 sec
    116     static const int kMaxTimeoutsForCaptureEnd    = 40;  // 4 sec
    117     static const int kMaxRetryCount = 3; // 3 retries in case of buffer drop
    118 
    119     wp<Camera2Client> mClient;
    120     wp<ZslProcessor> mZslProcessor;
    121 
    122     enum CaptureState {
    123         IDLE,
    124         START,
    125         ZSL_START,
    126         ZSL_WAITING,
    127         ZSL_REPROCESSING,
    128         STANDARD_START,
    129         STANDARD_PRECAPTURE_WAIT,
    130         STANDARD_CAPTURE,
    131         STANDARD_CAPTURE_WAIT,
    132         DONE,
    133         ERROR,
    134         NUM_CAPTURE_STATES
    135     } mCaptureState;
    136     static const char* kStateNames[];
    137     int mStateTransitionCount;
    138     Mutex mStateMutex; // Guards mCaptureState
    139     Condition mStateChanged;
    140 
    141     typedef CaptureState (CaptureSequencer::*StateManager)(sp<Camera2Client> &client);
    142     static const StateManager kStateManagers[];
    143 
    144     CameraMetadata mCaptureRequest;
    145 
    146     int mTriggerId;
    147     int mTimeoutCount;
    148     bool mAeInPrecapture;
    149 
    150     int32_t mCaptureId;
    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 manageDone(sp<Camera2Client> &client);
    169 
    170     // Utility methods
    171 
    172     status_t updateCaptureRequest(const Parameters &params,
    173             sp<Camera2Client> &client);
    174 
    175     // Emit Shutter/Raw callback to java, and maybe play a shutter sound
    176     static void shutterNotifyLocked(const Parameters &params,
    177             const sp<Camera2Client>& client);
    178 };
    179 
    180 }; // namespace camera2
    181 }; // namespace android
    182 
    183 #endif
    184