Camera2Client.h revision d4bcfde6bf3e7b28e36f6ec66e6d9e5adebfa949
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_CAMERA2CLIENT_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H
19
20#include "Camera2Device.h"
21#include "CameraService.h"
22#include "camera/CameraParameters.h"
23#include <binder/MemoryBase.h>
24#include <binder/MemoryHeapBase.h>
25#include <gui/CpuConsumer.h>
26
27namespace android {
28
29/**
30 * Implements the android.hardware.camera API on top of
31 * camera device HAL version 2.
32 */
33class Camera2Client : public CameraService::Client
34{
35public:
36    // ICamera interface (see ICamera for details)
37    virtual void            disconnect();
38    virtual status_t        connect(const sp<ICameraClient>& client);
39    virtual status_t        lock();
40    virtual status_t        unlock();
41    virtual status_t        setPreviewDisplay(const sp<Surface>& surface);
42    virtual status_t        setPreviewTexture(
43        const sp<ISurfaceTexture>& surfaceTexture);
44    virtual void            setPreviewCallbackFlag(int flag);
45    virtual status_t        startPreview();
46    virtual void            stopPreview();
47    virtual bool            previewEnabled();
48    virtual status_t        storeMetaDataInBuffers(bool enabled);
49    virtual status_t        startRecording();
50    virtual void            stopRecording();
51    virtual bool            recordingEnabled();
52    virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
53    virtual status_t        autoFocus();
54    virtual status_t        cancelAutoFocus();
55    virtual status_t        takePicture(int msgType);
56    virtual status_t        setParameters(const String8& params);
57    virtual String8         getParameters() const;
58    virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
59
60    // Interface used by CameraService
61    Camera2Client(const sp<CameraService>& cameraService,
62            const sp<ICameraClient>& cameraClient,
63            int cameraId,
64            int cameraFacing,
65            int clientPid);
66    ~Camera2Client();
67
68    status_t initialize(camera_module_t *module);
69
70    virtual status_t dump(int fd, const Vector<String16>& args);
71
72private:
73    enum {
74        NOT_INITIALIZED,
75        STOPPED,
76        WAITING_FOR_PREVIEW_WINDOW,
77        PREVIEW,
78        RECORD,
79        STILL_CAPTURE,
80        VIDEO_SNAPSHOT
81    } mState;
82
83    /** ICamera interface-related private members */
84
85    // Mutex that must be locked by methods implementing the ICamera interface.
86    // Ensures serialization between incoming ICamera calls
87    mutable Mutex mICameraLock;
88
89    // The following must be called with mICamaeraLock already locked
90
91    status_t setPreviewWindowLocked(const sp<IBinder>& binder,
92            const sp<ANativeWindow>& window);
93
94    void stopPreviewLocked();
95    status_t startPreviewLocked();
96
97    // Mutex that must be locked before accessing mParameters, mParamsFlattened
98    mutable Mutex mParamsLock;
99    String8 mParamsFlattened;
100    // Current camera state; this is the contents of the CameraParameters object
101    // in a more-efficient format. The enum values are mostly based off the
102    // corresponding camera2 enums, not the camera1 strings. A few are defined
103    // here if they don't cleanly map to camera2 values.
104    struct Parameters {
105        int previewWidth, previewHeight;
106        int previewFpsRangeMin, previewFpsRangeMax;
107        int previewFps; // deprecated, here only for tracking changes
108        int previewFormat;
109
110        int pictureWidth, pictureHeight;
111
112        int jpegThumbWidth, jpegThumbHeight;
113        int jpegQuality, jpegThumbQuality;
114        int jpegRotation;
115
116        bool gpsEnabled;
117        double gpsLatitude;
118        double gpsLongitude;
119        double gpsAltitude;
120        int64_t gpsTimestamp;
121        String8 gpsProcessingMethod;
122
123        int wbMode;
124        int effectMode;
125        int antibandingMode;
126        int sceneMode;
127
128        enum flashMode_t {
129            FLASH_MODE_OFF = 0,
130            FLASH_MODE_AUTO,
131            FLASH_MODE_ON,
132            FLASH_MODE_TORCH,
133            FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
134            FLASH_MODE_INVALID = -1
135        } flashMode;
136
137        enum focusMode_t {
138            FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
139            FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
140            FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
141            FOCUS_MODE_CONTINUOUS_PICTURE =
142                ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
143            FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
144            FOCUS_MODE_INFINITY,
145            FOCUS_MODE_FIXED,
146            FOCUS_MODE_INVALID = -1
147        } focusMode;
148
149        struct Area {
150            int left, top, right, bottom;
151            int weight;
152            Area() {}
153            Area(int left, int top, int right, int bottom, int weight):
154                    left(left), top(top), right(right), bottom(bottom),
155                    weight(weight) {}
156        };
157        Vector<Area> focusingAreas;
158
159        int exposureCompensation;
160        bool autoExposureLock;
161        bool autoWhiteBalanceLock;
162
163        Vector<Area> meteringAreas;
164
165        int zoom;
166
167        int videoWidth, videoHeight;
168
169        bool recordingHint;
170        bool videoStabilization;
171    } mParameters;
172
173    /** Camera device-related private members */
174
175    // Simple listener that forwards frame available notifications from
176    // a CPU consumer to the capture notification
177    class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
178      public:
179        CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
180        void onFrameAvailable() { mParent->onCaptureAvailable(); }
181      private:
182        Camera2Client *mParent;
183    };
184
185    void onCaptureAvailable();
186
187    // Number of zoom steps to simulate
188    static const unsigned int NUM_ZOOM_STEPS = 10;
189    // Used with mPreviewStreamId, mCaptureStreamId
190    static const int NO_STREAM = -1;
191
192    sp<IBinder> mPreviewSurface;
193    int mPreviewStreamId;
194    camera_metadata_t *mPreviewRequest;
195
196    int mCaptureStreamId;
197    sp<CpuConsumer>    mCaptureConsumer;
198    sp<ANativeWindow>  mCaptureWindow;
199    sp<CaptureWaiter>  mCaptureWaiter;
200    camera_metadata_t *mCaptureRequest;
201    sp<MemoryHeapBase> mCaptureHeap;
202    sp<MemoryBase>     mCaptureMemory;
203
204    sp<Camera2Device> mDevice;
205
206
207    // Get values for static camera info entry. min/maxCount are used for error
208    // checking the number of values in the entry. 0 for max/minCount means to
209    // do no bounds check in that direction. In case of error, the entry data
210    // pointer is null and the count is 0.
211    camera_metadata_entry_t staticInfo(uint32_t tag,
212            size_t minCount=0, size_t maxCount=0);
213
214    /** Utility methods */
215
216    // Convert static camera info from a camera2 device to the
217    // old API parameter map.
218    status_t buildDefaultParameters();
219
220    // Update preview request based on mParams
221    status_t updatePreviewRequest();
222
223    // Update capture request based on mParams
224    status_t updateCaptureRequest();
225    // Update capture stream based on mParams
226    status_t updateCaptureStream();
227
228    // Convert camera1 preview format string to camera2 enum
229    static int formatStringToEnum(const char *format);
230    static const char *formatEnumToString(int format);
231
232    static int wbModeStringToEnum(const char *wbMode);
233    static int effectModeStringToEnum(const char *effectMode);
234    static int abModeStringToEnum(const char *abMode);
235    static int sceneModeStringToEnum(const char *sceneMode);
236    static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode);
237    static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode);
238    static status_t parseAreas(const char *areasCStr,
239            Vector<Parameters::Area> *areas);
240    static status_t validateAreas(const Vector<Parameters::Area> &areas,
241                                  size_t maxRegions);
242    static bool boolFromString(const char *boolStr);
243};
244
245}; // namespace android
246
247#endif
248