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