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