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 <utils/Vector.h> 22 #include <binder/AppOpsManager.h> 23 #include <binder/BinderService.h> 24 #include <binder/IAppOpsCallback.h> 25 #include <camera/ICameraService.h> 26 #include <hardware/camera.h> 27 28 #include <camera/ICamera.h> 29 #include <camera/ICameraClient.h> 30 #include <camera/IProCameraUser.h> 31 #include <camera/IProCameraCallbacks.h> 32 #include <camera/camera2/ICameraDeviceUser.h> 33 #include <camera/camera2/ICameraDeviceCallbacks.h> 34 35 #include <camera/ICameraServiceListener.h> 36 37 /* This needs to be increased if we can have more cameras */ 38 #define MAX_CAMERAS 2 39 40 namespace android { 41 42 extern volatile int32_t gLogLevel; 43 44 class MemoryHeapBase; 45 class MediaPlayer; 46 47 class CameraService : 48 public BinderService<CameraService>, 49 public BnCameraService, 50 public IBinder::DeathRecipient, 51 public camera_module_callbacks_t 52 { 53 friend class BinderService<CameraService>; 54 public: 55 class Client; 56 class BasicClient; 57 58 // Implementation of BinderService<T> getServiceName()59 static char const* getServiceName() { return "media.camera"; } 60 61 CameraService(); 62 virtual ~CameraService(); 63 64 ///////////////////////////////////////////////////////////////////// 65 // HAL Callbacks 66 virtual void onDeviceStatusChanged(int cameraId, 67 int newStatus); 68 69 ///////////////////////////////////////////////////////////////////// 70 // ICameraService 71 virtual int32_t getNumberOfCameras(); 72 virtual status_t getCameraInfo(int cameraId, 73 struct CameraInfo* cameraInfo); 74 virtual status_t getCameraCharacteristics(int cameraId, 75 CameraMetadata* cameraInfo); 76 77 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId, 78 const String16& clientPackageName, int clientUid, 79 /*out*/ 80 sp<ICamera>& device); 81 82 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb, 83 int cameraId, const String16& clientPackageName, int clientUid, 84 /*out*/ 85 sp<IProCameraUser>& device); 86 87 virtual status_t connectDevice( 88 const sp<ICameraDeviceCallbacks>& cameraCb, 89 int cameraId, 90 const String16& clientPackageName, 91 int clientUid, 92 /*out*/ 93 sp<ICameraDeviceUser>& device); 94 95 virtual status_t addListener(const sp<ICameraServiceListener>& listener); 96 virtual status_t removeListener( 97 const sp<ICameraServiceListener>& listener); 98 99 // Extra permissions checks 100 virtual status_t onTransact(uint32_t code, const Parcel& data, 101 Parcel* reply, uint32_t flags); 102 103 virtual status_t dump(int fd, const Vector<String16>& args); 104 105 ///////////////////////////////////////////////////////////////////// 106 // Client functionality 107 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder); 108 109 enum sound_kind { 110 SOUND_SHUTTER = 0, 111 SOUND_RECORDING = 1, 112 NUM_SOUNDS 113 }; 114 115 void loadSound(); 116 void playSound(sound_kind kind); 117 void releaseSound(); 118 119 ///////////////////////////////////////////////////////////////////// 120 // CameraDeviceFactory functionality 121 int getDeviceVersion(int cameraId, int* facing = NULL); 122 123 124 ///////////////////////////////////////////////////////////////////// 125 // CameraClient functionality 126 127 // returns plain pointer of client. Note that mClientLock should be acquired to 128 // prevent the client from destruction. The result can be NULL. 129 virtual BasicClient* getClientByIdUnsafe(int cameraId); 130 virtual Mutex* getClientLockById(int cameraId); 131 132 class BasicClient : public virtual RefBase { 133 public: 134 virtual status_t initialize(camera_module_t *module) = 0; 135 136 virtual void disconnect() = 0; 137 138 // because we can't virtually inherit IInterface, which breaks 139 // virtual inheritance 140 virtual sp<IBinder> asBinderWrapper() = 0; 141 142 // Return the remote callback binder object (e.g. IProCameraCallbacks) getRemote()143 sp<IBinder> getRemote() { 144 return mRemoteBinder; 145 } 146 147 virtual status_t dump(int fd, const Vector<String16>& args) = 0; 148 149 protected: 150 BasicClient(const sp<CameraService>& cameraService, 151 const sp<IBinder>& remoteCallback, 152 const String16& clientPackageName, 153 int cameraId, 154 int cameraFacing, 155 int clientPid, 156 uid_t clientUid, 157 int servicePid); 158 159 virtual ~BasicClient(); 160 161 // the instance is in the middle of destruction. When this is set, 162 // the instance should not be accessed from callback. 163 // CameraService's mClientLock should be acquired to access this. 164 // - subclasses should set this to true in their destructors. 165 bool mDestructionStarted; 166 167 // these are initialized in the constructor. 168 sp<CameraService> mCameraService; // immutable after constructor 169 int mCameraId; // immutable after constructor 170 int mCameraFacing; // immutable after constructor 171 const String16 mClientPackageName; 172 pid_t mClientPid; 173 uid_t mClientUid; // immutable after constructor 174 pid_t mServicePid; // immutable after constructor 175 176 // - The app-side Binder interface to receive callbacks from us 177 sp<IBinder> mRemoteBinder; // immutable after constructor 178 179 // permissions management 180 status_t startCameraOps(); 181 status_t finishCameraOps(); 182 183 // Notify client about a fatal error 184 virtual void notifyError() = 0; 185 private: 186 AppOpsManager mAppOpsManager; 187 188 class OpsCallback : public BnAppOpsCallback { 189 public: 190 OpsCallback(wp<BasicClient> client); 191 virtual void opChanged(int32_t op, const String16& packageName); 192 193 private: 194 wp<BasicClient> mClient; 195 196 }; // class OpsCallback 197 198 sp<OpsCallback> mOpsCallback; 199 // Track whether startCameraOps was called successfully, to avoid 200 // finishing what we didn't start. 201 bool mOpsActive; 202 203 // IAppOpsCallback interface, indirected through opListener 204 virtual void opChanged(int32_t op, const String16& packageName); 205 }; // class BasicClient 206 207 class Client : public BnCamera, public BasicClient 208 { 209 public: 210 typedef ICameraClient TCamCallbacks; 211 212 // ICamera interface (see ICamera for details) 213 virtual void disconnect(); 214 virtual status_t connect(const sp<ICameraClient>& client) = 0; 215 virtual status_t lock() = 0; 216 virtual status_t unlock() = 0; 217 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0; 218 virtual void setPreviewCallbackFlag(int flag) = 0; 219 virtual status_t setPreviewCallbackTarget( 220 const sp<IGraphicBufferProducer>& callbackProducer) = 0; 221 virtual status_t startPreview() = 0; 222 virtual void stopPreview() = 0; 223 virtual bool previewEnabled() = 0; 224 virtual status_t storeMetaDataInBuffers(bool enabled) = 0; 225 virtual status_t startRecording() = 0; 226 virtual void stopRecording() = 0; 227 virtual bool recordingEnabled() = 0; 228 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0; 229 virtual status_t autoFocus() = 0; 230 virtual status_t cancelAutoFocus() = 0; 231 virtual status_t takePicture(int msgType) = 0; 232 virtual status_t setParameters(const String8& params) = 0; 233 virtual String8 getParameters() const = 0; 234 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0; 235 236 // Interface used by CameraService 237 Client(const sp<CameraService>& cameraService, 238 const sp<ICameraClient>& cameraClient, 239 const String16& clientPackageName, 240 int cameraId, 241 int cameraFacing, 242 int clientPid, 243 uid_t clientUid, 244 int servicePid); 245 ~Client(); 246 247 // return our camera client getRemoteCallback()248 const sp<ICameraClient>& getRemoteCallback() { 249 return mRemoteCallback; 250 } 251 asBinderWrapper()252 virtual sp<IBinder> asBinderWrapper() { 253 return asBinder(); 254 } 255 256 protected: 257 static Mutex* getClientLockFromCookie(void* user); 258 // convert client from cookie. Client lock should be acquired before getting Client. 259 static Client* getClientFromCookie(void* user); 260 261 virtual void notifyError(); 262 263 // Initialized in constructor 264 265 // - The app-side Binder interface to receive callbacks from us 266 sp<ICameraClient> mRemoteCallback; 267 268 }; // class Client 269 270 class ProClient : public BnProCameraUser, public BasicClient { 271 public: 272 typedef IProCameraCallbacks TCamCallbacks; 273 274 ProClient(const sp<CameraService>& cameraService, 275 const sp<IProCameraCallbacks>& remoteCallback, 276 const String16& clientPackageName, 277 int cameraId, 278 int cameraFacing, 279 int clientPid, 280 uid_t clientUid, 281 int servicePid); 282 283 virtual ~ProClient(); 284 getRemoteCallback()285 const sp<IProCameraCallbacks>& getRemoteCallback() { 286 return mRemoteCallback; 287 } 288 289 /*** 290 IProCamera implementation 291 ***/ 292 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks) 293 = 0; 294 virtual status_t exclusiveTryLock() = 0; 295 virtual status_t exclusiveLock() = 0; 296 virtual status_t exclusiveUnlock() = 0; 297 298 virtual bool hasExclusiveLock() = 0; 299 300 // Note that the callee gets a copy of the metadata. 301 virtual int submitRequest(camera_metadata_t* metadata, 302 bool streaming = false) = 0; 303 virtual status_t cancelRequest(int requestId) = 0; 304 305 // Callbacks from camera service 306 virtual void onExclusiveLockStolen() = 0; 307 308 protected: 309 virtual void notifyError(); 310 311 sp<IProCameraCallbacks> mRemoteCallback; 312 }; // class ProClient 313 314 private: 315 316 // Delay-load the Camera HAL module 317 virtual void onFirstRef(); 318 319 // Step 1. Check if we can connect, before we acquire the service lock. 320 status_t validateConnect(int cameraId, 321 /*inout*/ 322 int& clientUid) const; 323 324 // Step 2. Check if we can connect, after we acquire the service lock. 325 bool canConnectUnsafe(int cameraId, 326 const String16& clientPackageName, 327 const sp<IBinder>& remoteCallback, 328 /*out*/ 329 sp<BasicClient> &client); 330 331 // When connection is successful, initialize client and track its death 332 status_t connectFinishUnsafe(const sp<BasicClient>& client, 333 const sp<IBinder>& remoteCallback); 334 335 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient); 336 337 Mutex mServiceLock; 338 // either a Client or CameraDeviceClient 339 wp<BasicClient> mClient[MAX_CAMERAS]; // protected by mServiceLock 340 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks 341 int mNumberOfCameras; 342 343 typedef wp<ProClient> weak_pro_client_ptr; 344 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS]; 345 346 // needs to be called with mServiceLock held 347 sp<BasicClient> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex); 348 sp<ProClient> findProClientUnsafe( 349 const wp<IBinder>& cameraCallbacksRemote); 350 351 // atomics to record whether the hardware is allocated to some client. 352 volatile int32_t mBusy[MAX_CAMERAS]; 353 void setCameraBusy(int cameraId); 354 void setCameraFree(int cameraId); 355 356 // sounds 357 MediaPlayer* newMediaPlayer(const char *file); 358 359 Mutex mSoundLock; 360 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; 361 int mSoundRef; // reference count (release all MediaPlayer when 0) 362 363 camera_module_t *mModule; 364 365 Vector<sp<ICameraServiceListener> > 366 mListenerList; 367 368 // guard only mStatusList and the broadcasting of ICameraServiceListener 369 mutable Mutex mStatusMutex; 370 ICameraServiceListener::Status 371 mStatusList[MAX_CAMERAS]; 372 373 // Read the current status (locks mStatusMutex) 374 ICameraServiceListener::Status 375 getStatus(int cameraId) const; 376 377 typedef Vector<ICameraServiceListener::Status> StatusVector; 378 // Broadcast the new status if it changed (locks the service mutex) 379 void updateStatus( 380 ICameraServiceListener::Status status, 381 int32_t cameraId, 382 const StatusVector *rejectSourceStates = NULL); 383 384 // IBinder::DeathRecipient implementation 385 virtual void binderDied(const wp<IBinder> &who); 386 387 // Helpers 388 389 bool isValidCameraId(int cameraId); 390 }; 391 392 } // namespace android 393 394 #endif 395