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_CAMERACLIENT_H
18#define ANDROID_SERVERS_CAMERA_CAMERACLIENT_H
19
20#include "CameraService.h"
21
22namespace android {
23
24class MemoryHeapBase;
25class CameraHardwareInterface;
26
27/**
28 * Interface between android.hardware.Camera API and Camera HAL device for version
29 * CAMERA_DEVICE_API_VERSION_1_0.
30 */
31
32class CameraClient : public CameraService::Client
33{
34public:
35    // ICamera interface (see ICamera for details)
36    virtual binder::Status  disconnect();
37    virtual status_t        connect(const sp<hardware::ICameraClient>& client);
38    virtual status_t        lock();
39    virtual status_t        unlock();
40    virtual status_t        setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer);
41    virtual void            setPreviewCallbackFlag(int flag);
42    virtual status_t        setPreviewCallbackTarget(
43            const sp<IGraphicBufferProducer>& callbackProducer);
44    virtual status_t        startPreview();
45    virtual void            stopPreview();
46    virtual bool            previewEnabled();
47    virtual status_t        setVideoBufferMode(int32_t videoBufferMode);
48    virtual status_t        startRecording();
49    virtual void            stopRecording();
50    virtual bool            recordingEnabled();
51    virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
52    virtual void            releaseRecordingFrameHandle(native_handle_t *handle);
53    virtual status_t        autoFocus();
54    virtual status_t        cancelAutoFocus();
55    virtual status_t        takePicture(int msgType);
56    virtual status_t        setParameters(const String8& params);
57    virtual String8         getParameters() const;
58    virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
59    virtual status_t        setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer);
60
61    // Interface used by CameraService
62    CameraClient(const sp<CameraService>& cameraService,
63            const sp<hardware::ICameraClient>& cameraClient,
64            const String16& clientPackageName,
65            int cameraId,
66            int cameraFacing,
67            int clientPid,
68            int clientUid,
69            int servicePid,
70            bool legacyMode = false);
71    ~CameraClient();
72
73    status_t initialize(CameraModule *module);
74
75    virtual status_t dump(int fd, const Vector<String16>& args);
76
77    virtual status_t dumpClient(int fd, const Vector<String16>& args);
78
79private:
80
81    // check whether the calling process matches mClientPid.
82    status_t                checkPid() const;
83    status_t                checkPidAndHardware() const;  // also check mHardware != 0
84
85    // these are internal functions used to set up preview buffers
86    status_t                registerPreviewBuffers();
87
88    // camera operation mode
89    enum camera_mode {
90        CAMERA_PREVIEW_MODE   = 0,  // frame automatically released
91        CAMERA_RECORDING_MODE = 1,  // frame has to be explicitly released by releaseRecordingFrame()
92    };
93    // these are internal functions used for preview/recording
94    status_t                startCameraMode(camera_mode mode);
95    status_t                startPreviewMode();
96    status_t                startRecordingMode();
97
98    // internal function used by sendCommand to enable/disable shutter sound.
99    status_t                enableShutterSound(bool enable);
100
101    // these are static callback functions
102    static void             notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
103    static void             dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
104            camera_frame_metadata_t *metadata, void* user);
105    static void             dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
106    // handlers for messages
107    void                    handleShutter(void);
108    void                    handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
109            camera_frame_metadata_t *metadata);
110    void                    handlePostview(const sp<IMemory>& mem);
111    void                    handleRawPicture(const sp<IMemory>& mem);
112    void                    handleCompressedPicture(const sp<IMemory>& mem);
113    void                    handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
114    void                    handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
115            camera_frame_metadata_t *metadata);
116    void                    handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
117
118    void                    copyFrameAndPostCopiedFrame(
119        int32_t msgType,
120        const sp<hardware::ICameraClient>& client,
121        const sp<IMemoryHeap>& heap,
122        size_t offset, size_t size,
123        camera_frame_metadata_t *metadata);
124
125    int                     getOrientation(int orientation, bool mirror);
126
127    status_t                setPreviewWindow(
128        const sp<IBinder>& binder,
129        const sp<ANativeWindow>& window);
130
131
132    // these are initialized in the constructor.
133    sp<CameraHardwareInterface>     mHardware;       // cleared after disconnect()
134    int                             mPreviewCallbackFlag;
135    int                             mOrientation;     // Current display orientation
136    bool                            mPlayShutterSound;
137    bool                            mLegacyMode; // camera2 api legacy mode?
138
139    // Ensures atomicity among the public methods
140    mutable Mutex                   mLock;
141    // This is a binder of Surface or Surface.
142    sp<IBinder>                     mSurface;
143    sp<ANativeWindow>               mPreviewWindow;
144
145    // If the user want us to return a copy of the preview frame (instead
146    // of the original one), we allocate mPreviewBuffer and reuse it if possible.
147    sp<MemoryHeapBase>              mPreviewBuffer;
148
149    // Debugging information
150    CameraParameters                mLatestSetParameters;
151
152    // mAvailableCallbackBuffers stores sp<IMemory> that HAL uses to send VideoNativeHandleMetadata.
153    // It will be used to send VideoNativeHandleMetadata back to HAL when camera receives the
154    // native handle from releaseRecordingFrameHandle.
155    Mutex                           mAvailableCallbackBuffersLock;
156    std::vector<sp<IMemory>>        mAvailableCallbackBuffers;
157
158    // We need to avoid the deadlock when the incoming command thread and
159    // the CameraHardwareInterface callback thread both want to grab mLock.
160    // An extra flag is used to tell the callback thread that it should stop
161    // trying to deliver the callback messages if the client is not
162    // interested in it anymore. For example, if the client is calling
163    // stopPreview(), the preview frame messages do not need to be delivered
164    // anymore.
165
166    // This function takes the same parameter as the enableMsgType() and
167    // disableMsgType() functions in CameraHardwareInterface.
168    void                    enableMsgType(int32_t msgType);
169    void                    disableMsgType(int32_t msgType);
170    volatile int32_t        mMsgEnabled;
171
172    // This function keeps trying to grab mLock, or give up if the message
173    // is found to be disabled. It returns true if mLock is grabbed.
174    bool                    lockIfMessageWanted(int32_t msgType);
175};
176
177}
178
179#endif
180