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