Camera2Client.h revision a16733eeb9c40db4793bec408f29b4204e5f23b1
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 "camera2/Parameters.h"
23#include "camera2/FrameProcessor.h"
24#include <binder/MemoryBase.h>
25#include <binder/MemoryHeapBase.h>
26#include <gui/CpuConsumer.h>
27#include <gui/BufferItemConsumer.h>
28
29namespace android {
30
31/**
32 * Implements the android.hardware.camera API on top of
33 * camera device HAL version 2.
34 */
35class Camera2Client :
36        public CameraService::Client,
37        public Camera2Device::NotificationListener
38{
39public:
40    // ICamera interface (see ICamera for details)
41
42    virtual void            disconnect();
43    virtual status_t        connect(const sp<ICameraClient>& client);
44    virtual status_t        lock();
45    virtual status_t        unlock();
46    virtual status_t        setPreviewDisplay(const sp<Surface>& surface);
47    virtual status_t        setPreviewTexture(
48        const sp<ISurfaceTexture>& surfaceTexture);
49    virtual void            setPreviewCallbackFlag(int flag);
50    virtual status_t        startPreview();
51    virtual void            stopPreview();
52    virtual bool            previewEnabled();
53    virtual status_t        storeMetaDataInBuffers(bool enabled);
54    virtual status_t        startRecording();
55    virtual void            stopRecording();
56    virtual bool            recordingEnabled();
57    virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
58    virtual status_t        autoFocus();
59    virtual status_t        cancelAutoFocus();
60    virtual status_t        takePicture(int msgType);
61    virtual status_t        setParameters(const String8& params);
62    virtual String8         getParameters() const;
63    virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
64
65    // Interface used by CameraService
66
67    Camera2Client(const sp<CameraService>& cameraService,
68            const sp<ICameraClient>& cameraClient,
69            int cameraId,
70            int cameraFacing,
71            int clientPid);
72    virtual ~Camera2Client();
73
74    status_t initialize(camera_module_t *module);
75
76    virtual status_t dump(int fd, const Vector<String16>& args);
77
78    // Interface used by CameraDevice
79
80    virtual void notifyError(int errorCode, int arg1, int arg2);
81    virtual void notifyShutter(int frameNumber, nsecs_t timestamp);
82    virtual void notifyAutoFocus(uint8_t newState, int triggerId);
83    virtual void notifyAutoExposure(uint8_t newState, int triggerId);
84    virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId);
85
86    // Interface used by independent components of Camera2Client.
87
88    int getCameraId();
89    const sp<Camera2Device>& getCameraDevice();
90    camera2::SharedParameters& getParameters();
91
92    // Simple class to ensure that access to ICameraClient is serialized by
93    // requiring mCameraClientLock to be locked before access to mCameraClient
94    // is possible.
95    class SharedCameraClient {
96      public:
97        class Lock {
98          public:
99            Lock(SharedCameraClient &client);
100            ~Lock();
101            sp<ICameraClient> &mCameraClient;
102          private:
103            SharedCameraClient &mSharedClient;
104        };
105        SharedCameraClient& operator=(const sp<ICameraClient>& client);
106        void clear();
107      private:
108        sp<ICameraClient> mCameraClient;
109        mutable Mutex mCameraClientLock;
110    } mSharedCameraClient;
111
112private:
113    /** ICamera interface-related private members */
114
115    // Mutex that must be locked by methods implementing the ICamera interface.
116    // Ensures serialization between incoming ICamera calls. All methods below
117    // that append 'L' to the name assume that mICameraLock is locked when
118    // they're called
119    mutable Mutex mICameraLock;
120
121    typedef camera2::Parameters Parameters;
122    typedef camera2::CameraMetadata CameraMetadata;
123
124    status_t setPreviewWindowL(const sp<IBinder>& binder,
125            sp<ANativeWindow> window);
126    status_t startPreviewL(Parameters &params, bool restart);
127    void     stopPreviewL();
128    status_t startRecordingL(Parameters &params, bool restart);
129    bool     recordingEnabledL();
130
131    // Individual commands for sendCommand()
132    status_t commandStartSmoothZoomL();
133    status_t commandStopSmoothZoomL();
134    status_t commandSetDisplayOrientationL(int degrees);
135    status_t commandEnableShutterSoundL(bool enable);
136    status_t commandPlayRecordingSoundL();
137    status_t commandStartFaceDetectionL(int type);
138    status_t commandStopFaceDetectionL(Parameters &params);
139    status_t commandEnableFocusMoveMsgL(bool enable);
140    status_t commandPingL();
141    status_t commandSetVideoBufferCountL(size_t count);
142
143    // Current camera device configuration
144    camera2::SharedParameters mParameters;
145
146    /** Camera device-related private members */
147
148    class Camera2Heap;
149
150    void     setPreviewCallbackFlagL(Parameters &params, int flag);
151    status_t updateRequests(const Parameters &params);
152
153    // Used with stream IDs
154    static const int NO_STREAM = -1;
155
156    sp<camera2::FrameProcessor> mFrameProcessor;
157
158    /* Preview related members */
159
160    int mPreviewStreamId;
161    CameraMetadata mPreviewRequest;
162    sp<IBinder> mPreviewSurface;
163    sp<ANativeWindow> mPreviewWindow;
164
165    status_t updatePreviewRequest(const Parameters &params);
166    status_t updatePreviewStream(const Parameters &params);
167
168    /** Preview callback related members */
169
170    int mCallbackStreamId;
171    static const size_t kCallbackHeapCount = 6;
172    sp<CpuConsumer>    mCallbackConsumer;
173    sp<ANativeWindow>  mCallbackWindow;
174    // Simple listener that forwards frame available notifications from
175    // a CPU consumer to the callback notification
176    class CallbackWaiter: public CpuConsumer::FrameAvailableListener {
177      public:
178        CallbackWaiter(Camera2Client *parent) : mParent(parent) {}
179        void onFrameAvailable() { mParent->onCallbackAvailable(); }
180      private:
181        Camera2Client *mParent;
182    };
183    sp<CallbackWaiter>  mCallbackWaiter;
184    sp<Camera2Heap>     mCallbackHeap;
185    int mCallbackHeapId;
186    size_t mCallbackHeapHead, mCallbackHeapFree;
187    // Handle callback image buffers
188    void onCallbackAvailable();
189
190    status_t updateCallbackStream(const Parameters &params);
191
192    /* Still image capture related members */
193
194    int mCaptureStreamId;
195    sp<CpuConsumer>    mCaptureConsumer;
196    sp<ANativeWindow>  mCaptureWindow;
197    // Simple listener that forwards frame available notifications from
198    // a CPU consumer to the capture notification
199    class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
200      public:
201        CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
202        void onFrameAvailable() { mParent->onCaptureAvailable(); }
203      private:
204        Camera2Client *mParent;
205    };
206    sp<CaptureWaiter>  mCaptureWaiter;
207    CameraMetadata mCaptureRequest;
208    sp<Camera2Heap>    mCaptureHeap;
209    // Handle captured image buffers
210    void onCaptureAvailable();
211
212    status_t updateCaptureRequest(const Parameters &params);
213    status_t updateCaptureStream(const Parameters &params);
214
215    /* Recording related members */
216
217    int mRecordingStreamId;
218    int mRecordingFrameCount;
219    sp<BufferItemConsumer>    mRecordingConsumer;
220    sp<ANativeWindow>  mRecordingWindow;
221    // Simple listener that forwards frame available notifications from
222    // a CPU consumer to the recording notification
223    class RecordingWaiter: public BufferItemConsumer::FrameAvailableListener {
224      public:
225        RecordingWaiter(Camera2Client *parent) : mParent(parent) {}
226        void onFrameAvailable() { mParent->onRecordingFrameAvailable(); }
227      private:
228        Camera2Client *mParent;
229    };
230    sp<RecordingWaiter>  mRecordingWaiter;
231    CameraMetadata mRecordingRequest;
232    sp<Camera2Heap> mRecordingHeap;
233
234    static const size_t kDefaultRecordingHeapCount = 8;
235    size_t mRecordingHeapCount;
236    Vector<BufferItemConsumer::BufferItem> mRecordingBuffers;
237    size_t mRecordingHeapHead, mRecordingHeapFree;
238    // Handle new recording image buffers
239    void onRecordingFrameAvailable();
240
241    status_t updateRecordingRequest(const Parameters &params);
242    status_t updateRecordingStream(const Parameters &params);
243
244    /** Notification-related members */
245
246    bool mAfInMotion;
247
248    /** Camera2Device instance wrapping HAL2 entry */
249
250    sp<Camera2Device> mDevice;
251
252    /** Utility members */
253
254    // Verify that caller is the owner of the camera
255    status_t checkPid(const char *checkLocation) const;
256
257    // Utility class for managing a set of IMemory blocks
258    class Camera2Heap : public RefBase {
259    public:
260        Camera2Heap(size_t buf_size, uint_t num_buffers = 1,
261                const char *name = NULL) :
262                         mBufSize(buf_size),
263                         mNumBufs(num_buffers) {
264            mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name);
265            mBuffers = new sp<MemoryBase>[mNumBufs];
266            for (uint_t i = 0; i < mNumBufs; i++)
267                mBuffers[i] = new MemoryBase(mHeap,
268                                             i * mBufSize,
269                                             mBufSize);
270        }
271
272        virtual ~Camera2Heap()
273        {
274            delete [] mBuffers;
275        }
276
277        size_t mBufSize;
278        uint_t mNumBufs;
279        sp<MemoryHeapBase> mHeap;
280        sp<MemoryBase> *mBuffers;
281    };
282
283    // Update parameters all requests use, based on mParameters
284    status_t updateRequestCommon(CameraMetadata *request, const Parameters &params) const;
285
286    // Map from sensor active array pixel coordinates to normalized camera
287    // parameter coordinates. The former are (0,0)-(array width - 1, array height
288    // - 1), the latter from (-1000,-1000)-(1000,1000)
289    int normalizedXToArray(int x) const;
290    int normalizedYToArray(int y) const;
291    int arrayXToNormalized(int width) const;
292    int arrayYToNormalized(int height) const;
293
294
295    static size_t calculateBufferSize(int width, int height,
296            int format, int stride);
297};
298
299}; // namespace android
300
301#endif
302