Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright (C) 2008, The Android Open Source Project |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 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 | |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 21 | #include <utils/Vector.h> |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 22 | #include <binder/BinderService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 23 | #include <camera/ICameraService.h> |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 24 | #include <hardware/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 25 | |
| 26 | /* This needs to be increased if we can have more cameras */ |
| 27 | #define MAX_CAMERAS 2 |
| 28 | |
| 29 | namespace android { |
| 30 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 31 | extern volatile int32_t gLogLevel; |
| 32 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | class MemoryHeapBase; |
| 34 | class MediaPlayer; |
| 35 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 36 | class CameraService : |
| 37 | public BinderService<CameraService>, |
Igor Murashkin | 8dcdb95 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 38 | public BnCameraService, |
| 39 | public IBinder::DeathRecipient |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | { |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 41 | friend class BinderService<CameraService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | public: |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 43 | class Client; |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 44 | class BasicClient; |
| 45 | |
| 46 | // Implementation of BinderService<T> |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 47 | static char const* getServiceName() { return "media.camera"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 48 | |
| 49 | CameraService(); |
| 50 | virtual ~CameraService(); |
| 51 | |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 52 | ///////////////////////////////////////////////////////////////////// |
| 53 | // ICameraService |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 54 | virtual int32_t getNumberOfCameras(); |
| 55 | virtual status_t getCameraInfo(int cameraId, |
| 56 | struct CameraInfo* cameraInfo); |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 57 | virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId); |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 58 | virtual sp<IProCameraUser> |
| 59 | connect(const sp<IProCameraCallbacks>& cameraCb, int cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 61 | // Extra permissions checks |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 62 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 63 | Parcel* reply, uint32_t flags); |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 64 | |
| 65 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 66 | |
| 67 | ///////////////////////////////////////////////////////////////////// |
| 68 | // Client functionality |
| 69 | virtual void removeClientByRemote(const wp<IBinder>& remoteBinder); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 70 | |
| 71 | enum sound_kind { |
| 72 | SOUND_SHUTTER = 0, |
| 73 | SOUND_RECORDING = 1, |
| 74 | NUM_SOUNDS |
| 75 | }; |
| 76 | |
| 77 | void loadSound(); |
| 78 | void playSound(sound_kind kind); |
| 79 | void releaseSound(); |
| 80 | |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 81 | |
| 82 | ///////////////////////////////////////////////////////////////////// |
| 83 | // CameraClient functionality |
| 84 | |
| 85 | // returns plain pointer of client. Note that mClientLock should be acquired to |
| 86 | // prevent the client from destruction. The result can be NULL. |
| 87 | virtual Client* getClientByIdUnsafe(int cameraId); |
| 88 | virtual Mutex* getClientLockById(int cameraId); |
| 89 | |
| 90 | class BasicClient : public virtual RefBase { |
| 91 | public: |
| 92 | virtual status_t initialize(camera_module_t *module) = 0; |
| 93 | |
| 94 | virtual void disconnect() = 0; |
| 95 | |
| 96 | wp<IBinder> getRemote() { |
| 97 | return mRemoteCallback; |
| 98 | } |
| 99 | |
| 100 | protected: |
| 101 | BasicClient(const sp<CameraService>& cameraService, |
| 102 | const sp<IBinder>& remoteCallback, |
| 103 | int cameraId, |
| 104 | int cameraFacing, |
| 105 | int clientPid, |
| 106 | int servicePid); |
| 107 | |
| 108 | virtual ~BasicClient(); |
| 109 | |
| 110 | // the instance is in the middle of destruction. When this is set, |
| 111 | // the instance should not be accessed from callback. |
| 112 | // CameraService's mClientLock should be acquired to access this. |
| 113 | // - subclasses should set this to true in their destructors. |
| 114 | bool mDestructionStarted; |
| 115 | |
| 116 | // these are initialized in the constructor. |
| 117 | sp<CameraService> mCameraService; // immutable after constructor |
| 118 | int mCameraId; // immutable after constructor |
| 119 | int mCameraFacing; // immutable after constructor |
| 120 | pid_t mClientPid; |
| 121 | pid_t mServicePid; // immutable after constructor |
| 122 | |
| 123 | // - The app-side Binder interface to receive callbacks from us |
| 124 | wp<IBinder> mRemoteCallback; // immutable after constructor |
| 125 | }; |
| 126 | |
| 127 | class Client : public BnCamera, public BasicClient |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 128 | { |
| 129 | public: |
| 130 | // ICamera interface (see ICamera for details) |
| 131 | virtual void disconnect(); |
| 132 | virtual status_t connect(const sp<ICameraClient>& client) = 0; |
| 133 | virtual status_t lock() = 0; |
| 134 | virtual status_t unlock() = 0; |
| 135 | virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0; |
Andy McFadden | 484566c | 2012-12-18 09:46:54 -0800 | [diff] [blame] | 136 | virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 137 | virtual void setPreviewCallbackFlag(int flag) = 0; |
| 138 | virtual status_t startPreview() = 0; |
| 139 | virtual void stopPreview() = 0; |
| 140 | virtual bool previewEnabled() = 0; |
| 141 | virtual status_t storeMetaDataInBuffers(bool enabled) = 0; |
| 142 | virtual status_t startRecording() = 0; |
| 143 | virtual void stopRecording() = 0; |
| 144 | virtual bool recordingEnabled() = 0; |
| 145 | virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0; |
| 146 | virtual status_t autoFocus() = 0; |
| 147 | virtual status_t cancelAutoFocus() = 0; |
| 148 | virtual status_t takePicture(int msgType) = 0; |
| 149 | virtual status_t setParameters(const String8& params) = 0; |
| 150 | virtual String8 getParameters() const = 0; |
| 151 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0; |
| 152 | |
| 153 | // Interface used by CameraService |
| 154 | Client(const sp<CameraService>& cameraService, |
| 155 | const sp<ICameraClient>& cameraClient, |
| 156 | int cameraId, |
| 157 | int cameraFacing, |
Igor Murashkin | 8dcdb95 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 158 | int clientPid, |
| 159 | int servicePid); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 160 | ~Client(); |
| 161 | |
| 162 | // return our camera client |
| 163 | const sp<ICameraClient>& getCameraClient() { |
| 164 | return mCameraClient; |
| 165 | } |
| 166 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 167 | protected: |
| 168 | static Mutex* getClientLockFromCookie(void* user); |
| 169 | // convert client from cookie. Client lock should be acquired before getting Client. |
| 170 | static Client* getClientFromCookie(void* user); |
| 171 | |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 172 | // Initialized in constructor |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 173 | |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 174 | // - The app-side Binder interface to receive callbacks from us |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 175 | sp<ICameraClient> mCameraClient; |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | class ProClient : public BnProCameraUser, public BasicClient { |
| 179 | public: |
| 180 | ProClient(const sp<CameraService>& cameraService, |
| 181 | const sp<IProCameraCallbacks>& remoteCallback, |
| 182 | int cameraId, |
| 183 | int cameraFacing, |
| 184 | int clientPid, |
| 185 | int servicePid); |
| 186 | |
| 187 | virtual ~ProClient(); |
| 188 | |
| 189 | const sp<IProCameraCallbacks>& getRemoteCallback() { |
| 190 | return mRemoteCallback; |
| 191 | } |
| 192 | |
| 193 | // BasicClient implementation |
| 194 | virtual status_t initialize(camera_module_t *module); |
| 195 | |
| 196 | /*** |
| 197 | IProCamera implementation |
| 198 | ***/ |
| 199 | |
| 200 | |
| 201 | virtual status_t connect( |
| 202 | const sp<IProCameraCallbacks>& callbacks); |
| 203 | virtual void disconnect(); |
| 204 | |
| 205 | virtual status_t exclusiveTryLock(); |
| 206 | virtual status_t exclusiveLock(); |
| 207 | virtual status_t exclusiveUnlock(); |
| 208 | |
| 209 | virtual bool hasExclusiveLock(); |
| 210 | |
| 211 | // Note that the callee gets a copy of the metadata. |
| 212 | virtual int submitRequest(camera_metadata_t* metadata, |
| 213 | bool streaming = false); |
| 214 | virtual status_t cancelRequest(int requestId); |
| 215 | |
| 216 | virtual status_t requestStream(int streamId); |
| 217 | virtual status_t cancelStream(int streamId); |
| 218 | |
| 219 | protected: |
| 220 | sp<IProCameraCallbacks> mRemoteCallback; |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 221 | |
| 222 | }; |
| 223 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 224 | private: |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 225 | |
| 226 | // Delay-load the Camera HAL module |
| 227 | virtual void onFirstRef(); |
| 228 | |
| 229 | virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient); |
| 230 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 231 | Mutex mServiceLock; |
| 232 | wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 233 | Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 234 | int mNumberOfCameras; |
| 235 | |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 236 | typedef wp<ProClient> weak_pro_client_ptr; |
| 237 | Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS]; |
| 238 | |
Igor Murashkin | 8dcdb95 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 239 | // needs to be called with mServiceLock held |
Igor Murashkin | 507994d | 2012-10-05 10:44:57 -0700 | [diff] [blame] | 240 | sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex); |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 241 | sp<ProClient> findProClientUnsafe( |
| 242 | const wp<IBinder>& cameraCallbacksRemote); |
Igor Murashkin | 8dcdb95 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 243 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 244 | // atomics to record whether the hardware is allocated to some client. |
| 245 | volatile int32_t mBusy[MAX_CAMERAS]; |
| 246 | void setCameraBusy(int cameraId); |
| 247 | void setCameraFree(int cameraId); |
| 248 | |
| 249 | // sounds |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 250 | MediaPlayer* newMediaPlayer(const char *file); |
| 251 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 252 | Mutex mSoundLock; |
| 253 | sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; |
| 254 | int mSoundRef; // reference count (release all MediaPlayer when 0) |
| 255 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 256 | camera_module_t *mModule; |
Igor Murashkin | 8dcdb95 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 257 | |
| 258 | // IBinder::DeathRecipient implementation |
| 259 | virtual void binderDied(const wp<IBinder> &who); |
Igor Murashkin | bfb5d5e | 2013-02-20 17:15:11 -0800 | [diff] [blame] | 260 | |
| 261 | // Helpers |
| 262 | int getDeviceVersion(int cameraId, int* facing); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | } // namespace android |
| 266 | |
| 267 | #endif |