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