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