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/StreamingProcessor.h"
25#include "camera2/JpegProcessor.h"
26#include "camera2/ZslProcessor.h"
27#include "camera2/CaptureSequencer.h"
28#include "camera2/CallbackProcessor.h"
29
30namespace android {
31
32class IMemory;
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            int servicePid);
79    virtual ~Camera2Client();
80
81    status_t initialize(camera_module_t *module);
82
83    virtual status_t dump(int fd, const Vector<String16>& args);
84
85    /**
86     * Interface used by Camera2Device
87     */
88
89    virtual void notifyError(int errorCode, int arg1, int arg2);
90    virtual void notifyShutter(int frameNumber, nsecs_t timestamp);
91    virtual void notifyAutoFocus(uint8_t newState, int triggerId);
92    virtual void notifyAutoExposure(uint8_t newState, int triggerId);
93    virtual void notifyAutoWhitebalance(uint8_t newState, int triggerId);
94
95    /**
96     * Interface used by independent components of Camera2Client.
97     */
98
99    int getCameraId() const;
100    const sp<Camera2Device>& getCameraDevice();
101    const sp<CameraService>& getCameraService();
102    camera2::SharedParameters& getParameters();
103
104    int getPreviewStreamId() const;
105    int getCaptureStreamId() const;
106    int getCallbackStreamId() const;
107    int getRecordingStreamId() const;
108    int getZslStreamId() const;
109
110    status_t registerFrameListener(int32_t minId, int32_t maxId,
111            wp<camera2::FrameProcessor::FilteredListener> listener);
112    status_t removeFrameListener(int32_t minId, int32_t maxId,
113            wp<camera2::FrameProcessor::FilteredListener> listener);
114
115    status_t stopStream();
116
117    // Simple class to ensure that access to ICameraClient is serialized by
118    // requiring mCameraClientLock to be locked before access to mCameraClient
119    // is possible.
120    class SharedCameraClient {
121      public:
122        class Lock {
123          public:
124            Lock(SharedCameraClient &client);
125            ~Lock();
126            sp<ICameraClient> &mCameraClient;
127          private:
128            SharedCameraClient &mSharedClient;
129        };
130        SharedCameraClient(const sp<ICameraClient>& client);
131        SharedCameraClient& operator=(const sp<ICameraClient>& client);
132        void clear();
133      private:
134        sp<ICameraClient> mCameraClient;
135        mutable Mutex mCameraClientLock;
136    } mSharedCameraClient;
137
138    static size_t calculateBufferSize(int width, int height,
139            int format, int stride);
140
141    static const int32_t kPreviewRequestIdStart = 10000000;
142    static const int32_t kPreviewRequestIdEnd   = 20000000;
143
144    static const int32_t kRecordingRequestIdStart  = 20000000;
145    static const int32_t kRecordingRequestIdEnd    = 30000000;
146
147    static const int32_t kCaptureRequestIdStart = 30000000;
148    static const int32_t kCaptureRequestIdEnd   = 40000000;
149
150private:
151    /** ICamera interface-related private members */
152
153    // Mutex that must be locked by methods implementing the ICamera interface.
154    // Ensures serialization between incoming ICamera calls. All methods below
155    // that append 'L' to the name assume that mICameraLock is locked when
156    // they're called
157    mutable Mutex mICameraLock;
158
159    typedef camera2::Parameters Parameters;
160    typedef camera2::CameraMetadata CameraMetadata;
161
162    status_t setPreviewWindowL(const sp<IBinder>& binder,
163            sp<ANativeWindow> window);
164    status_t startPreviewL(Parameters &params, bool restart);
165    void     stopPreviewL();
166    status_t startRecordingL(Parameters &params, bool restart);
167    bool     recordingEnabledL();
168
169    // Individual commands for sendCommand()
170    status_t commandStartSmoothZoomL();
171    status_t commandStopSmoothZoomL();
172    status_t commandSetDisplayOrientationL(int degrees);
173    status_t commandEnableShutterSoundL(bool enable);
174    status_t commandPlayRecordingSoundL();
175    status_t commandStartFaceDetectionL(int type);
176    status_t commandStopFaceDetectionL(Parameters &params);
177    status_t commandEnableFocusMoveMsgL(bool enable);
178    status_t commandPingL();
179    status_t commandSetVideoBufferCountL(size_t count);
180
181    // Current camera device configuration
182    camera2::SharedParameters mParameters;
183
184    /** Camera device-related private members */
185
186    void     setPreviewCallbackFlagL(Parameters &params, int flag);
187    status_t updateRequests(Parameters &params);
188
189    // Used with stream IDs
190    static const int NO_STREAM = -1;
191
192    sp<camera2::FrameProcessor> mFrameProcessor;
193
194    /* Preview/Recording related members */
195
196    sp<IBinder> mPreviewSurface;
197    sp<camera2::StreamingProcessor> mStreamingProcessor;
198
199    /** Preview callback related members */
200
201    sp<camera2::CallbackProcessor> mCallbackProcessor;
202
203    /* Still image capture related members */
204
205    sp<camera2::CaptureSequencer> mCaptureSequencer;
206    sp<camera2::JpegProcessor> mJpegProcessor;
207    sp<camera2::ZslProcessor> mZslProcessor;
208
209    /** Notification-related members */
210
211    bool mAfInMotion;
212
213    /** Camera2Device instance wrapping HAL2 entry */
214
215    sp<Camera2Device> mDevice;
216
217    /** Utility members */
218
219    // Wait until the camera device has received the latest control settings
220    status_t syncWithDevice();
221
222    // Verify that caller is the owner of the camera
223    status_t checkPid(const char *checkLocation) const;
224};
225
226}; // namespace android
227
228#endif
229