CameraService.h revision f69c70ded4316ea3ee504ac779bd024433ed4ef7
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
30extern volatile int32_t gLogLevel;
31
32class MemoryHeapBase;
33class MediaPlayer;
34
35class CameraService :
36    public BinderService<CameraService>,
37    public BnCameraService
38{
39    friend class BinderService<CameraService>;
40public:
41    class Client;
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    // returns plain pointer of client. Note that mClientLock should be acquired to
53    // prevent the client from destruction. The result can be NULL.
54    virtual Client*     getClientByIdUnsafe(int cameraId);
55    virtual Mutex*      getClientLockById(int cameraId);
56
57    virtual status_t    dump(int fd, const Vector<String16>& args);
58    virtual status_t    onTransact(uint32_t code, const Parcel& data,
59                                   Parcel* reply, uint32_t flags);
60    virtual void onFirstRef();
61
62    enum sound_kind {
63        SOUND_SHUTTER = 0,
64        SOUND_RECORDING = 1,
65        NUM_SOUNDS
66    };
67
68    void                loadSound();
69    void                playSound(sound_kind kind);
70    void                releaseSound();
71
72    class Client : public BnCamera
73    {
74    public:
75        // ICamera interface (see ICamera for details)
76        virtual void          disconnect();
77        virtual status_t      connect(const sp<ICameraClient>& client) = 0;
78        virtual status_t      lock() = 0;
79        virtual status_t      unlock() = 0;
80        virtual status_t      setPreviewDisplay(const sp<Surface>& surface) = 0;
81        virtual status_t      setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture) = 0;
82        virtual void          setPreviewCallbackFlag(int flag) = 0;
83        virtual status_t      startPreview() = 0;
84        virtual void          stopPreview() = 0;
85        virtual bool          previewEnabled() = 0;
86        virtual status_t      storeMetaDataInBuffers(bool enabled) = 0;
87        virtual status_t      startRecording() = 0;
88        virtual void          stopRecording() = 0;
89        virtual bool          recordingEnabled() = 0;
90        virtual void          releaseRecordingFrame(const sp<IMemory>& mem) = 0;
91        virtual status_t      autoFocus() = 0;
92        virtual status_t      cancelAutoFocus() = 0;
93        virtual status_t      takePicture(int msgType) = 0;
94        virtual status_t      setParameters(const String8& params) = 0;
95        virtual String8       getParameters() const = 0;
96        virtual status_t      sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
97
98        // Interface used by CameraService
99        Client(const sp<CameraService>& cameraService,
100                const sp<ICameraClient>& cameraClient,
101                int cameraId,
102                int cameraFacing,
103                int clientPid);
104        ~Client();
105
106        // return our camera client
107        const sp<ICameraClient>&    getCameraClient() {
108            return mCameraClient;
109        }
110
111        virtual status_t initialize(camera_module_t *module) = 0;
112
113        virtual status_t dump(int fd, const Vector<String16>& args) = 0;
114
115    protected:
116        static Mutex*        getClientLockFromCookie(void* user);
117        // convert client from cookie. Client lock should be acquired before getting Client.
118        static Client*       getClientFromCookie(void* user);
119
120        // the instance is in the middle of destruction. When this is set,
121        // the instance should not be accessed from callback.
122        // CameraService's mClientLock should be acquired to access this.
123        bool                            mDestructionStarted;
124
125        // these are initialized in the constructor.
126        sp<CameraService>               mCameraService;  // immutable after constructor
127        sp<ICameraClient>               mCameraClient;
128        int                             mCameraId;       // immutable after constructor
129        int                             mCameraFacing;   // immutable after constructor
130        pid_t                           mClientPid;
131
132    };
133
134private:
135    Mutex               mServiceLock;
136    wp<Client>          mClient[MAX_CAMERAS];  // protected by mServiceLock
137    Mutex               mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
138    int                 mNumberOfCameras;
139
140    // atomics to record whether the hardware is allocated to some client.
141    volatile int32_t    mBusy[MAX_CAMERAS];
142    void                setCameraBusy(int cameraId);
143    void                setCameraFree(int cameraId);
144
145    // sounds
146    MediaPlayer*        newMediaPlayer(const char *file);
147
148    Mutex               mSoundLock;
149    sp<MediaPlayer>     mSoundPlayer[NUM_SOUNDS];
150    int                 mSoundRef;  // reference count (release all MediaPlayer when 0)
151
152    camera_module_t *mModule;
153};
154
155} // namespace android
156
157#endif
158