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