Camera2Client.h revision 174181eec57c963a2a17bd156d81c3f18b3ba75c
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#include "MediaConsumer.h"
27
28namespace android {
29
30/**
31 * Implements the android.hardware.camera API on top of
32 * camera device HAL version 2.
33 */
34class Camera2Client : public CameraService::Client,
35                      public Camera2Device::NotificationListener
36{
37public:
38    // ICamera interface (see ICamera for details)
39
40    virtual void            disconnect();
41    virtual status_t        connect(const sp<ICameraClient>& client);
42    virtual status_t        lock();
43    virtual status_t        unlock();
44    virtual status_t        setPreviewDisplay(const sp<Surface>& surface);
45    virtual status_t        setPreviewTexture(
46        const sp<ISurfaceTexture>& surfaceTexture);
47    virtual void            setPreviewCallbackFlag(int flag);
48    virtual status_t        startPreview();
49    virtual void            stopPreview();
50    virtual bool            previewEnabled();
51    virtual status_t        storeMetaDataInBuffers(bool enabled);
52    virtual status_t        startRecording();
53    virtual void            stopRecording();
54    virtual bool            recordingEnabled();
55    virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
56    virtual status_t        autoFocus();
57    virtual status_t        cancelAutoFocus();
58    virtual status_t        takePicture(int msgType);
59    virtual status_t        setParameters(const String8& params);
60    virtual String8         getParameters() const;
61    virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
62
63    // Interface used by CameraService
64
65    Camera2Client(const sp<CameraService>& cameraService,
66            const sp<ICameraClient>& cameraClient,
67            int cameraId,
68            int cameraFacing,
69            int clientPid);
70    virtual ~Camera2Client();
71
72    status_t initialize(camera_module_t *module);
73
74    virtual status_t dump(int fd, const Vector<String16>& args);
75
76    // Interface used by CameraDevice
77
78    virtual void notifyError(int errorCode, int arg1, int arg2);
79    virtual void notifyShutter(int frameNumber, nsecs_t timestamp);
80    virtual void notifyAutoFocus(uint8_t newState, int triggerId);
81    virtual void notifyAutoExposure(uint8_t newState, int triggerId);
82    virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId);
83
84private:
85    enum State {
86        DISCONNECTED,
87        STOPPED,
88        WAITING_FOR_PREVIEW_WINDOW,
89        PREVIEW,
90        RECORD,
91        STILL_CAPTURE,
92        VIDEO_SNAPSHOT
93    } mState;
94
95    static const char *getStateName(State state);
96
97    /** ICamera interface-related private members */
98
99    // Mutex that must be locked by methods implementing the ICamera interface.
100    // Ensures serialization between incoming ICamera calls. All methods below
101    // that append 'L' to the name assume that mICameraLock is locked when
102    // they're called
103    mutable Mutex mICameraLock;
104
105    status_t setPreviewWindowL(const sp<IBinder>& binder,
106            sp<ANativeWindow> window);
107
108    void stopPreviewL();
109    status_t startPreviewL();
110
111    bool recordingEnabledL();
112
113    // Individual commands for sendCommand()
114    status_t commandStartSmoothZoomL();
115    status_t commandStopSmoothZoomL();
116    status_t commandSetDisplayOrientationL(int degrees);
117    status_t commandEnableShutterSoundL(bool enable);
118    status_t commandPlayRecordingSoundL();
119    status_t commandStartFaceDetectionL(int type);
120    status_t commandStopFaceDetectionL();
121    status_t commandEnableFocusMoveMsgL(bool enable);
122    status_t commandPingL();
123    status_t commandSetVideoBufferCountL(size_t count);
124
125    // Current camera state; this is the contents of the CameraParameters object
126    // in a more-efficient format. The enum values are mostly based off the
127    // corresponding camera2 enums, not the camera1 strings. A few are defined
128    // here if they don't cleanly map to camera2 values.
129    struct Parameters {
130        int previewWidth, previewHeight;
131        int32_t previewFpsRange[2];
132        int previewFps; // deprecated, here only for tracking changes
133        int previewFormat;
134
135        int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION
136
137        int pictureWidth, pictureHeight;
138
139        int32_t jpegThumbSize[2];
140        int32_t jpegQuality, jpegThumbQuality;
141        int32_t jpegRotation;
142
143        bool gpsEnabled;
144        double gpsCoordinates[3];
145        int64_t gpsTimestamp;
146        String8 gpsProcessingMethod;
147
148        int wbMode;
149        int effectMode;
150        int antibandingMode;
151        int sceneMode;
152
153        enum flashMode_t {
154            FLASH_MODE_OFF = 0,
155            FLASH_MODE_AUTO,
156            FLASH_MODE_ON,
157            FLASH_MODE_TORCH,
158            FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
159            FLASH_MODE_INVALID = -1
160        } flashMode;
161
162        enum focusMode_t {
163            FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
164            FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
165            FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
166            FOCUS_MODE_CONTINUOUS_PICTURE =
167                ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
168            FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
169            FOCUS_MODE_INFINITY,
170            FOCUS_MODE_FIXED,
171            FOCUS_MODE_INVALID = -1
172        } focusMode;
173
174        struct Area {
175            int left, top, right, bottom;
176            int weight;
177            Area() {}
178            Area(int left, int top, int right, int bottom, int weight):
179                    left(left), top(top), right(right), bottom(bottom),
180                    weight(weight) {}
181        };
182        Vector<Area> focusingAreas;
183
184        int32_t exposureCompensation;
185        bool autoExposureLock;
186        bool autoWhiteBalanceLock;
187
188        Vector<Area> meteringAreas;
189
190        int zoom;
191
192        int videoWidth, videoHeight;
193
194        bool recordingHint;
195        bool videoStabilization;
196
197        String8 paramsFlattened;
198
199        // These parameters are also part of the camera API-visible state, but not directly
200        // listed in Camera.Parameters
201        bool storeMetadataInBuffers;
202        bool playShutterSound;
203        bool enableFocusMoveMessages;
204
205        int afTriggerCounter;
206        int currentAfTriggerId;
207        bool afInMotion;
208    };
209
210    class LockedParameters {
211      public:
212        class Key {
213          public:
214            Key(LockedParameters &p):
215                    mParameters(p.mParameters),
216                    mLockedParameters(p) {
217                mLockedParameters.mLock.lock();
218            }
219
220            ~Key() {
221                mLockedParameters.mLock.unlock();
222            }
223            Parameters &mParameters;
224          private:
225            // Disallow copying, default construction
226            Key();
227            Key(const Key &);
228            Key &operator=(const Key &);
229            LockedParameters &mLockedParameters;
230        };
231        class ReadKey {
232          public:
233            ReadKey(const LockedParameters &p):
234                    mParameters(p.mParameters),
235                    mLockedParameters(p) {
236                mLockedParameters.mLock.lock();
237            }
238
239            ~ReadKey() {
240                mLockedParameters.mLock.unlock();
241            }
242            const Parameters &mParameters;
243          private:
244            // Disallow copying, default construction
245            ReadKey();
246            ReadKey(const ReadKey &);
247            ReadKey &operator=(const ReadKey &);
248            const LockedParameters &mLockedParameters;
249        };
250
251        // Only use for dumping or other debugging
252        const Parameters &unsafeUnlock() {
253            return mParameters;
254        }
255      private:
256        Parameters mParameters;
257        mutable Mutex mLock;
258
259    } mParameters;
260
261    /** Camera device-related private members */
262
263    class Camera2Heap;
264
265    // Number of zoom steps to simulate
266    static const unsigned int NUM_ZOOM_STEPS = 10;
267    // Used with stream IDs
268    static const int NO_STREAM = -1;
269
270    /* Preview related members */
271
272    int mPreviewStreamId;
273    camera_metadata_t *mPreviewRequest;
274    sp<IBinder> mPreviewSurface;
275    sp<ANativeWindow> mPreviewWindow;
276
277    status_t updatePreviewRequest(const Parameters &params);
278    status_t updatePreviewStream(const Parameters &params);
279
280    /* Still image capture related members */
281
282    int mCaptureStreamId;
283    sp<CpuConsumer>    mCaptureConsumer;
284    sp<ANativeWindow>  mCaptureWindow;
285    // Simple listener that forwards frame available notifications from
286    // a CPU consumer to the capture notification
287    class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
288      public:
289        CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
290        void onFrameAvailable() { mParent->onCaptureAvailable(); }
291      private:
292        Camera2Client *mParent;
293    };
294    sp<CaptureWaiter>  mCaptureWaiter;
295    camera_metadata_t *mCaptureRequest;
296    sp<Camera2Heap>    mCaptureHeap;
297    // Handle captured image buffers
298    void onCaptureAvailable();
299
300    status_t updateCaptureRequest(const Parameters &params);
301    status_t updateCaptureStream(const Parameters &params);
302
303    /* Recording related members */
304
305    int mRecordingStreamId;
306    sp<MediaConsumer>    mRecordingConsumer;
307    sp<ANativeWindow>  mRecordingWindow;
308    // Simple listener that forwards frame available notifications from
309    // a CPU consumer to the recording notification
310    class RecordingWaiter: public MediaConsumer::FrameAvailableListener {
311      public:
312        RecordingWaiter(Camera2Client *parent) : mParent(parent) {}
313        void onFrameAvailable() { mParent->onRecordingFrameAvailable(); }
314      private:
315        Camera2Client *mParent;
316    };
317    sp<RecordingWaiter>  mRecordingWaiter;
318    camera_metadata_t *mRecordingRequest;
319    sp<Camera2Heap> mRecordingHeap;
320
321    static const size_t kDefaultRecordingHeapCount = 8;
322    size_t mRecordingHeapCount;
323    size_t mRecordingHeapHead, mRecordingHeapFree;
324    // Handle new recording image buffers
325    void onRecordingFrameAvailable();
326
327    status_t updateRecordingRequest(const Parameters &params);
328    status_t updateRecordingStream(const Parameters &params);
329
330    /** Notification-related members */
331
332    bool mAfInMotion;
333
334    /** Camera2Device instance wrapping HAL2 entry */
335
336    sp<Camera2Device> mDevice;
337
338    /** Utility members */
339
340    // Verify that caller is the owner of the camera
341    status_t checkPid(const char *checkLocation) const;
342
343    // Utility class for managing a set of IMemory blocks
344    class Camera2Heap : public RefBase {
345    public:
346        Camera2Heap(size_t buf_size, uint_t num_buffers = 1,
347                const char *name = NULL) :
348                         mBufSize(buf_size),
349                         mNumBufs(num_buffers) {
350            mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name);
351            mBuffers = new sp<MemoryBase>[mNumBufs];
352            for (uint_t i = 0; i < mNumBufs; i++)
353                mBuffers[i] = new MemoryBase(mHeap,
354                                             i * mBufSize,
355                                             mBufSize);
356        }
357
358        virtual ~Camera2Heap()
359        {
360            delete [] mBuffers;
361        }
362
363        size_t mBufSize;
364        uint_t mNumBufs;
365        sp<MemoryHeapBase> mHeap;
366        sp<MemoryBase> *mBuffers;
367    };
368
369    // Get values for static camera info entry. min/maxCount are used for error
370    // checking the number of values in the entry. 0 for max/minCount means to
371    // do no bounds check in that direction. In case of error, the entry data
372    // pointer is null and the count is 0.
373    camera_metadata_entry_t staticInfo(uint32_t tag,
374            size_t minCount=0, size_t maxCount=0);
375
376    // Convert static camera info from a camera2 device to the
377    // old API parameter map.
378    status_t buildDefaultParameters();
379
380    // Update parameters all requests use, based on mParameters
381    status_t updateRequestCommon(camera_metadata_t *request, const Parameters &params);
382
383    // Update specific metadata entry with new values. Adds entry if it does not
384    // exist, which will invalidate sorting
385    static status_t updateEntry(camera_metadata_t *buffer,
386            uint32_t tag, const void *data, size_t data_count);
387
388    // Remove metadata entry. Will invalidate sorting. If entry does not exist,
389    // does nothing.
390    static status_t deleteEntry(camera_metadata_t *buffer,
391            uint32_t tag);
392
393    // Convert camera1 preview format string to camera2 enum
394    static int formatStringToEnum(const char *format);
395    static const char *formatEnumToString(int format);
396
397    static int wbModeStringToEnum(const char *wbMode);
398    static int effectModeStringToEnum(const char *effectMode);
399    static int abModeStringToEnum(const char *abMode);
400    static int sceneModeStringToEnum(const char *sceneMode);
401    static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode);
402    static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode);
403    static status_t parseAreas(const char *areasCStr,
404            Vector<Parameters::Area> *areas);
405    static status_t validateAreas(const Vector<Parameters::Area> &areas,
406                                  size_t maxRegions);
407    static bool boolFromString(const char *boolStr);
408
409    // Map from camera orientation + facing to gralloc transform enum
410    static int degToTransform(int degrees, bool mirror);
411
412};
413
414}; // namespace android
415
416#endif
417