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