CameraService.h revision ff4f55c8d77e276dbcceedb4e560ca1d91ba38ba
1/*
2**
3** Copyright (C) 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
19#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
20
21#include <binder/BinderService.h>
22#include <camera/ICameraService.h>
23#include <hardware/camera.h>
24
25/* This needs to be increased if we can have more cameras */
26#define MAX_CAMERAS 2
27
28namespace android {
29
30class MemoryHeapBase;
31class MediaPlayer;
32class CameraHardwareInterface;
33
34class CameraService :
35    public BinderService<CameraService>,
36    public BnCameraService
37{
38    class Client;
39    friend class BinderService<CameraService>;
40public:
41    static char const* getServiceName() { return "media.camera"; }
42
43                        CameraService();
44    virtual             ~CameraService();
45
46    virtual int32_t     getNumberOfCameras();
47    virtual status_t    getCameraInfo(int cameraId,
48                                      struct CameraInfo* cameraInfo);
49    virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId);
50    virtual void        removeClient(const sp<ICameraClient>& cameraClient);
51    virtual sp<Client>  getClientById(int cameraId);
52
53    virtual status_t    dump(int fd, const Vector<String16>& args);
54    virtual status_t    onTransact(uint32_t code, const Parcel& data,
55                                   Parcel* reply, uint32_t flags);
56    virtual void onFirstRef();
57
58    enum sound_kind {
59        SOUND_SHUTTER = 0,
60        SOUND_RECORDING = 1,
61        NUM_SOUNDS
62    };
63
64    void                loadSound();
65    void                playSound(sound_kind kind);
66    void                releaseSound();
67
68private:
69    Mutex               mServiceLock;
70    wp<Client>          mClient[MAX_CAMERAS];  // protected by mServiceLock
71    int                 mNumberOfCameras;
72
73    // atomics to record whether the hardware is allocated to some client.
74    volatile int32_t    mBusy[MAX_CAMERAS];
75    void                setCameraBusy(int cameraId);
76    void                setCameraFree(int cameraId);
77
78    // sounds
79    audio_stream_type_t mAudioStreamType;
80    MediaPlayer*        newMediaPlayer(const char *file);
81
82    Mutex               mSoundLock;
83    sp<MediaPlayer>     mSoundPlayer[NUM_SOUNDS];
84    int                 mSoundRef;  // reference count (release all MediaPlayer when 0)
85
86    class Client : public BnCamera
87    {
88    public:
89        // ICamera interface (see ICamera for details)
90        virtual void            disconnect();
91        virtual status_t        connect(const sp<ICameraClient>& client);
92        virtual status_t        lock();
93        virtual status_t        unlock();
94        virtual status_t        setPreviewDisplay(const sp<Surface>& surface);
95        virtual status_t        setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture);
96        virtual void            setPreviewCallbackFlag(int flag);
97        virtual status_t        startPreview();
98        virtual void            stopPreview();
99        virtual bool            previewEnabled();
100        virtual status_t        storeMetaDataInBuffers(bool enabled);
101        virtual status_t        startRecording();
102        virtual void            stopRecording();
103        virtual bool            recordingEnabled();
104        virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
105        virtual status_t        autoFocus();
106        virtual status_t        cancelAutoFocus();
107        virtual status_t        takePicture(int msgType);
108        virtual status_t        setParameters(const String8& params);
109        virtual String8         getParameters() const;
110        virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
111    private:
112        friend class CameraService;
113                                Client(const sp<CameraService>& cameraService,
114                                       const sp<ICameraClient>& cameraClient,
115                                       const sp<CameraHardwareInterface>& hardware,
116                                       int cameraId,
117                                       int cameraFacing,
118                                       int clientPid);
119                                ~Client();
120
121        // return our camera client
122        const sp<ICameraClient>&    getCameraClient() { return mCameraClient; }
123
124        // check whether the calling process matches mClientPid.
125        status_t                checkPid() const;
126        status_t                checkPidAndHardware() const;  // also check mHardware != 0
127
128        // these are internal functions used to set up preview buffers
129        status_t                registerPreviewBuffers();
130
131        // camera operation mode
132        enum camera_mode {
133            CAMERA_PREVIEW_MODE   = 0,  // frame automatically released
134            CAMERA_RECORDING_MODE = 1,  // frame has to be explicitly released by releaseRecordingFrame()
135        };
136        // these are internal functions used for preview/recording
137        status_t                startCameraMode(camera_mode mode);
138        status_t                startPreviewMode();
139        status_t                startRecordingMode();
140
141        // internal function used by sendCommand to enable/disable shutter sound.
142        status_t                enableShutterSound(bool enable);
143
144        // these are static callback functions
145        static void             notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
146        static void             dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
147                                             camera_frame_metadata_t *metadata, void* user);
148        static void             dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
149        // convert client from cookie
150        static sp<Client>       getClientFromCookie(void* user);
151        // handlers for messages
152        void                    handleShutter(void);
153        void                    handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
154                                                  camera_frame_metadata_t *metadata);
155        void                    handlePostview(const sp<IMemory>& mem);
156        void                    handleRawPicture(const sp<IMemory>& mem);
157        void                    handleCompressedPicture(const sp<IMemory>& mem);
158        void                    handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
159        void                    handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
160                                                  camera_frame_metadata_t *metadata);
161        void                    handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
162
163        void                    copyFrameAndPostCopiedFrame(
164                                    int32_t msgType,
165                                    const sp<ICameraClient>& client,
166                                    const sp<IMemoryHeap>& heap,
167                                    size_t offset, size_t size,
168                                    camera_frame_metadata_t *metadata);
169
170        int                     getOrientation(int orientation, bool mirror);
171
172        status_t                setPreviewWindow(
173                                    const sp<IBinder>& binder,
174                                    const sp<ANativeWindow>& window);
175
176        // these are initialized in the constructor.
177        sp<CameraService>               mCameraService;  // immutable after constructor
178        sp<ICameraClient>               mCameraClient;
179        int                             mCameraId;       // immutable after constructor
180        int                             mCameraFacing;   // immutable after constructor
181        pid_t                           mClientPid;
182        sp<CameraHardwareInterface>     mHardware;       // cleared after disconnect()
183        int                             mPreviewCallbackFlag;
184        int                             mOrientation;     // Current display orientation
185        bool                            mPlayShutterSound;
186
187        // Ensures atomicity among the public methods
188        mutable Mutex                   mLock;
189        // This is a binder of Surface or SurfaceTexture.
190        sp<IBinder>                     mSurface;
191        sp<ANativeWindow>               mPreviewWindow;
192
193        // If the user want us to return a copy of the preview frame (instead
194        // of the original one), we allocate mPreviewBuffer and reuse it if possible.
195        sp<MemoryHeapBase>              mPreviewBuffer;
196
197        // We need to avoid the deadlock when the incoming command thread and
198        // the CameraHardwareInterface callback thread both want to grab mLock.
199        // An extra flag is used to tell the callback thread that it should stop
200        // trying to deliver the callback messages if the client is not
201        // interested in it anymore. For example, if the client is calling
202        // stopPreview(), the preview frame messages do not need to be delivered
203        // anymore.
204
205        // This function takes the same parameter as the enableMsgType() and
206        // disableMsgType() functions in CameraHardwareInterface.
207        void                    enableMsgType(int32_t msgType);
208        void                    disableMsgType(int32_t msgType);
209        volatile int32_t        mMsgEnabled;
210
211        // This function keeps trying to grab mLock, or give up if the message
212        // is found to be disabled. It returns true if mLock is grabbed.
213        bool                    lockIfMessageWanted(int32_t msgType);
214    };
215
216    camera_module_t *mModule;
217};
218
219} // namespace android
220
221#endif
222