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