Camera2Client.h revision be0573b93e2d6fa133579c885583af9ed16bc29d
1c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/*
2c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Copyright (C) 2012 The Android Open Source Project
3c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
4c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Licensed under the Apache License, Version 2.0 (the "License");
5c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * you may not use this file except in compliance with the License.
6c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * You may obtain a copy of the License at
7c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
8c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *      http://www.apache.org/licenses/LICENSE-2.0
9c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville *
10c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Unless required by applicable law or agreed to in writing, software
11c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * distributed under the License is distributed on an "AS IS" BASIS,
12c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * See the License for the specific language governing permissions and
14c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * limitations under the License.
15c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
16c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
17c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#ifndef ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H
18c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H
19c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
20c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#include "Camera2Device.h"
21c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#include "CameraService.h"
22c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#include "camera/CameraParameters.h"
23c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#include <binder/MemoryBase.h>
24c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#include <binder/MemoryHeapBase.h>
25c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville#include <gui/CpuConsumer.h>
26c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
27c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savillenamespace android {
28c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
29c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville/**
30c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * Implements the android.hardware.camera API on top of
31c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville * camera device HAL version 2.
32c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville */
33c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleclass Camera2Client : public CameraService::Client
34c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville{
35c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savillepublic:
36c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // ICamera interface (see ICamera for details)
37c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual void            disconnect();
38c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        connect(const sp<ICameraClient>& client);
39c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        lock();
40c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        unlock();
41c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        setPreviewDisplay(const sp<Surface>& surface);
42c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        setPreviewTexture(
43c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        const sp<ISurfaceTexture>& surfaceTexture);
44c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual void            setPreviewCallbackFlag(int flag);
45c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        startPreview();
46c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual void            stopPreview();
47c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual bool            previewEnabled();
48c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        storeMetaDataInBuffers(bool enabled);
49c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        startRecording();
50c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual void            stopRecording();
51c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual bool            recordingEnabled();
52c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
53c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        autoFocus();
54c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        cancelAutoFocus();
55c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        takePicture(int msgType);
56c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        setParameters(const String8& params);
57c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual String8         getParameters() const;
58c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
59c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
60e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka    // Interface used by CameraService
61e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka    Camera2Client(const sp<CameraService>& cameraService,
62e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            const sp<ICameraClient>& cameraClient,
63e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            int cameraId,
64e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            int cameraFacing,
65e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            int clientPid);
66e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka    ~Camera2Client();
67c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
68c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    status_t initialize(camera_module_t *module);
69c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
70c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    virtual status_t dump(int fd, const Vector<String16>& args);
71c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
72c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Savilleprivate:
73c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    enum State {
74c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        NOT_INITIALIZED,
75c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        STOPPED,
76c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        WAITING_FOR_PREVIEW_WINDOW,
77c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        PREVIEW,
78c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        RECORD,
79c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        STILL_CAPTURE,
80c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        VIDEO_SNAPSHOT
81c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    } mState;
82c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
83c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    static const char *getStateName(State state);
84c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
85c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    /** ICamera interface-related private members */
86c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
87c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // Mutex that must be locked by methods implementing the ICamera interface.
88c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // Ensures serialization between incoming ICamera calls
89c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    mutable Mutex mICameraLock;
90c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
91c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // The following must be called with mICamaeraLock already locked
92c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
93c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    status_t setPreviewWindowLocked(const sp<IBinder>& binder,
94c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            sp<ANativeWindow> window);
95c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
96c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    void stopPreviewLocked();
97c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    status_t startPreviewLocked();
98c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
99c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // Mutex that must be locked before accessing mParameters, mParamsFlattened
100c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    mutable Mutex mParamsLock;
101c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    String8 mParamsFlattened;
102c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // Current camera state; this is the contents of the CameraParameters object
103c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // in a more-efficient format. The enum values are mostly based off the
104c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // corresponding camera2 enums, not the camera1 strings. A few are defined
105c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    // here if they don't cleanly map to camera2 values.
106c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville    struct Parameters {
107c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int previewWidth, previewHeight;
108c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int32_t previewFpsRange[2];
109c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int previewFps; // deprecated, here only for tracking changes
110c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int previewFormat;
111e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka
112c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION
113c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
114c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int pictureWidth, pictureHeight;
115c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
116e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka        int32_t jpegThumbSize[2];
117c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int32_t jpegQuality, jpegThumbQuality;
118c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int32_t jpegRotation;
119c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
120e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka        bool gpsEnabled;
121c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        double gpsCoordinates[3];
122c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int64_t gpsTimestamp;
123c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        String8 gpsProcessingMethod;
124c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
125c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int wbMode;
126c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int effectMode;
127e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka        int antibandingMode;
128c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        int sceneMode;
129c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
130c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        enum flashMode_t {
131e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            FLASH_MODE_OFF = 0,
132e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            FLASH_MODE_AUTO,
133e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            FLASH_MODE_ON,
134e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            FLASH_MODE_TORCH,
135e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
136e287feac673ff68565b766e0e463d105fa9cef9dAlex Yakavenka            FLASH_MODE_INVALID = -1
137c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        } flashMode;
138c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville
139c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville        enum focusMode_t {
140c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
141c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
142c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
143c38bb60d867c5d61d90b7179a9ed2b2d1848124fWink Saville            FOCUS_MODE_CONTINUOUS_PICTURE =
144                ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
145            FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
146            FOCUS_MODE_INFINITY,
147            FOCUS_MODE_FIXED,
148            FOCUS_MODE_INVALID = -1
149        } focusMode;
150
151        struct Area {
152            int left, top, right, bottom;
153            int weight;
154            Area() {}
155            Area(int left, int top, int right, int bottom, int weight):
156                    left(left), top(top), right(right), bottom(bottom),
157                    weight(weight) {}
158        };
159        Vector<Area> focusingAreas;
160
161        int32_t exposureCompensation;
162        bool autoExposureLock;
163        bool autoWhiteBalanceLock;
164
165        Vector<Area> meteringAreas;
166
167        int zoom;
168
169        int videoWidth, videoHeight;
170
171        bool recordingHint;
172        bool videoStabilization;
173    } mParameters;
174
175    /** Camera device-related private members */
176
177    // Simple listener that forwards frame available notifications from
178    // a CPU consumer to the capture notification
179    class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
180      public:
181        CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
182        void onFrameAvailable() { mParent->onCaptureAvailable(); }
183      private:
184        Camera2Client *mParent;
185    };
186
187    void onCaptureAvailable();
188
189    // Number of zoom steps to simulate
190    static const unsigned int NUM_ZOOM_STEPS = 10;
191    // Used with mPreviewStreamId, mCaptureStreamId
192    static const int NO_STREAM = -1;
193
194    sp<IBinder> mPreviewSurface;
195    sp<ANativeWindow> mPreviewWindow;
196
197    int mPreviewStreamId;
198    camera_metadata_t *mPreviewRequest;
199
200    int mCaptureStreamId;
201    sp<CpuConsumer>    mCaptureConsumer;
202    sp<ANativeWindow>  mCaptureWindow;
203    sp<CaptureWaiter>  mCaptureWaiter;
204    camera_metadata_t *mCaptureRequest;
205    sp<MemoryHeapBase> mCaptureHeap;
206    sp<MemoryBase>     mCaptureMemory;
207
208    sp<Camera2Device> mDevice;
209
210
211    // Get values for static camera info entry. min/maxCount are used for error
212    // checking the number of values in the entry. 0 for max/minCount means to
213    // do no bounds check in that direction. In case of error, the entry data
214    // pointer is null and the count is 0.
215    camera_metadata_entry_t staticInfo(uint32_t tag,
216            size_t minCount=0, size_t maxCount=0);
217
218    /** Utility methods */
219
220    // Convert static camera info from a camera2 device to the
221    // old API parameter map.
222    status_t buildDefaultParameters();
223
224    // Update preview request based on mParameters
225    status_t updatePreviewRequest();
226    // Update preview stream based on mParameters
227    status_t updatePreviewStream();
228
229    // Update capture request based on mParameters
230    status_t updateCaptureRequest();
231    // Update capture stream based on mParameters
232    status_t updateCaptureStream();
233
234    // Update parameters all requests use, based on mParameters
235    status_t updateRequestCommon(camera_metadata_t *request);
236
237    // Update specific metadata entry with new values. Adds entry if it does not
238    // exist, which will invalidate sorting
239    static status_t updateEntry(camera_metadata_t *buffer,
240            uint32_t tag, const void *data, size_t data_count);
241
242    // Remove metadata entry. Will invalidate sorting. If entry does not exist,
243    // does nothing.
244    static status_t deleteEntry(camera_metadata_t *buffer,
245            uint32_t tag);
246
247    // Convert camera1 preview format string to camera2 enum
248    static int formatStringToEnum(const char *format);
249    static const char *formatEnumToString(int format);
250
251    static int wbModeStringToEnum(const char *wbMode);
252    static int effectModeStringToEnum(const char *effectMode);
253    static int abModeStringToEnum(const char *abMode);
254    static int sceneModeStringToEnum(const char *sceneMode);
255    static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode);
256    static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode);
257    static status_t parseAreas(const char *areasCStr,
258            Vector<Parameters::Area> *areas);
259    static status_t validateAreas(const Vector<Parameters::Area> &areas,
260                                  size_t maxRegions);
261    static bool boolFromString(const char *boolStr);
262};
263
264}; // namespace android
265
266#endif
267