blob: cab804e53750f312d09d26453fd9c83c3dfc23d3 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian65ab4712010-07-14 17:59:35 -07004**
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 Murashkin634a5152013-02-20 17:15:11 -080021#include <utils/Vector.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080022#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070023#include <binder/BinderService.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080024#include <binder/IAppOpsCallback.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <camera/ICameraService.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070026#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027
Igor Murashkinc073ba52013-02-26 14:32:34 -080028#include <camera/ICamera.h>
29#include <camera/ICameraClient.h>
30#include <camera/IProCameraUser.h>
31#include <camera/IProCameraCallbacks.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070032#include <camera/photography/ICameraDeviceUser.h>
33#include <camera/photography/ICameraDeviceCallbacks.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080034
Igor Murashkinbfc99152013-02-27 12:55:20 -080035#include <camera/ICameraServiceListener.h>
36
Mathias Agopian65ab4712010-07-14 17:59:35 -070037/* This needs to be increased if we can have more cameras */
38#define MAX_CAMERAS 2
39
40namespace android {
41
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070042extern volatile int32_t gLogLevel;
43
Mathias Agopian65ab4712010-07-14 17:59:35 -070044class MemoryHeapBase;
45class MediaPlayer;
46
Mathias Agopian5462fc92010-07-14 18:41:18 -070047class CameraService :
48 public BinderService<CameraService>,
Igor Murashkinecf17e82012-10-02 16:05:11 -070049 public BnCameraService,
Igor Murashkincba2c162013-03-20 15:56:31 -070050 public IBinder::DeathRecipient,
51 public camera_module_callbacks_t
Mathias Agopian65ab4712010-07-14 17:59:35 -070052{
Mathias Agopian5462fc92010-07-14 18:41:18 -070053 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070054public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070055 class Client;
Igor Murashkin634a5152013-02-20 17:15:11 -080056 class BasicClient;
57
58 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070059 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
61 CameraService();
62 virtual ~CameraService();
63
Igor Murashkin634a5152013-02-20 17:15:11 -080064 /////////////////////////////////////////////////////////////////////
Igor Murashkincba2c162013-03-20 15:56:31 -070065 // HAL Callbacks
66 virtual void onDeviceStatusChanged(int cameraId,
67 int newStatus);
68
69 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -080070 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070071 virtual int32_t getNumberOfCameras();
72 virtual status_t getCameraInfo(int cameraId,
73 struct CameraInfo* cameraInfo);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080074
75 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId,
76 const String16& clientPackageName, int clientUid);
77 virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -080078 int cameraId, const String16& clientPackageName, int clientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -070079 virtual sp<ICameraDeviceUser> connect(
80 const sp<ICameraDeviceCallbacks>& cameraCb,
81 int cameraId,
82 const String16& clientPackageName,
83 int clientUid);
Mathias Agopian65ab4712010-07-14 17:59:35 -070084
Igor Murashkinbfc99152013-02-27 12:55:20 -080085 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
86 virtual status_t removeListener(
87 const sp<ICameraServiceListener>& listener);
88
Igor Murashkin634a5152013-02-20 17:15:11 -080089 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -070090 virtual status_t onTransact(uint32_t code, const Parcel& data,
91 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -080092
93 virtual status_t dump(int fd, const Vector<String16>& args);
94
95 /////////////////////////////////////////////////////////////////////
96 // Client functionality
97 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -070098
99 enum sound_kind {
100 SOUND_SHUTTER = 0,
101 SOUND_RECORDING = 1,
102 NUM_SOUNDS
103 };
104
105 void loadSound();
106 void playSound(sound_kind kind);
107 void releaseSound();
108
Igor Murashkin634a5152013-02-20 17:15:11 -0800109
110 /////////////////////////////////////////////////////////////////////
111 // CameraClient functionality
112
113 // returns plain pointer of client. Note that mClientLock should be acquired to
114 // prevent the client from destruction. The result can be NULL.
Igor Murashkine7ee7632013-06-11 18:10:18 -0700115 virtual BasicClient* getClientByIdUnsafe(int cameraId);
Igor Murashkin634a5152013-02-20 17:15:11 -0800116 virtual Mutex* getClientLockById(int cameraId);
117
118 class BasicClient : public virtual RefBase {
119 public:
120 virtual status_t initialize(camera_module_t *module) = 0;
121
122 virtual void disconnect() = 0;
123
Igor Murashkine7ee7632013-06-11 18:10:18 -0700124 // because we can't virtually inherit IInterface, which breaks
125 // virtual inheritance
126 virtual sp<IBinder> asBinderWrapper() = 0;
127
Igor Murashkine6800ce2013-03-04 17:25:57 -0800128 // Return the remote callback binder object (e.g. IProCameraCallbacks)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700129 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800130 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800131 }
132
Igor Murashkine7ee7632013-06-11 18:10:18 -0700133 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
134
Igor Murashkin634a5152013-02-20 17:15:11 -0800135 protected:
136 BasicClient(const sp<CameraService>& cameraService,
137 const sp<IBinder>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800138 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800139 int cameraId,
140 int cameraFacing,
141 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800142 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800143 int servicePid);
144
145 virtual ~BasicClient();
146
147 // the instance is in the middle of destruction. When this is set,
148 // the instance should not be accessed from callback.
149 // CameraService's mClientLock should be acquired to access this.
150 // - subclasses should set this to true in their destructors.
151 bool mDestructionStarted;
152
153 // these are initialized in the constructor.
154 sp<CameraService> mCameraService; // immutable after constructor
155 int mCameraId; // immutable after constructor
156 int mCameraFacing; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800157 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800158 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800159 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800160 pid_t mServicePid; // immutable after constructor
161
162 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700163 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800164
165 // permissions management
166 status_t startCameraOps();
167 status_t finishCameraOps();
168
169 // Notify client about a fatal error
170 virtual void notifyError() = 0;
171 private:
172 AppOpsManager mAppOpsManager;
173
174 class OpsCallback : public BnAppOpsCallback {
175 public:
176 OpsCallback(wp<BasicClient> client);
177 virtual void opChanged(int32_t op, const String16& packageName);
178
179 private:
180 wp<BasicClient> mClient;
181
182 }; // class OpsCallback
183
184 sp<OpsCallback> mOpsCallback;
185 // Track whether startCameraOps was called successfully, to avoid
186 // finishing what we didn't start.
187 bool mOpsActive;
188
189 // IAppOpsCallback interface, indirected through opListener
190 virtual void opChanged(int32_t op, const String16& packageName);
191 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800192
193 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700194 {
195 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800196 typedef ICameraClient TCamCallbacks;
197
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700198 // ICamera interface (see ICamera for details)
199 virtual void disconnect();
200 virtual status_t connect(const sp<ICameraClient>& client) = 0;
201 virtual status_t lock() = 0;
202 virtual status_t unlock() = 0;
203 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden8ba01022012-12-18 09:46:54 -0800204 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700205 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700206 virtual status_t setPreviewCallbackTarget(
207 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700208 virtual status_t startPreview() = 0;
209 virtual void stopPreview() = 0;
210 virtual bool previewEnabled() = 0;
211 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
212 virtual status_t startRecording() = 0;
213 virtual void stopRecording() = 0;
214 virtual bool recordingEnabled() = 0;
215 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
216 virtual status_t autoFocus() = 0;
217 virtual status_t cancelAutoFocus() = 0;
218 virtual status_t takePicture(int msgType) = 0;
219 virtual status_t setParameters(const String8& params) = 0;
220 virtual String8 getParameters() const = 0;
221 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
222
223 // Interface used by CameraService
224 Client(const sp<CameraService>& cameraService,
225 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800226 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700227 int cameraId,
228 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700229 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800230 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700231 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700232 ~Client();
233
234 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800235 const sp<ICameraClient>& getRemoteCallback() {
236 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700237 }
238
Igor Murashkine7ee7632013-06-11 18:10:18 -0700239 virtual sp<IBinder> asBinderWrapper() {
240 return asBinder();
241 }
242
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700243 protected:
244 static Mutex* getClientLockFromCookie(void* user);
245 // convert client from cookie. Client lock should be acquired before getting Client.
246 static Client* getClientFromCookie(void* user);
247
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800248 virtual void notifyError();
249
Igor Murashkin634a5152013-02-20 17:15:11 -0800250 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700251
Igor Murashkin634a5152013-02-20 17:15:11 -0800252 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800253 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800254
255 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800256
257 class ProClient : public BnProCameraUser, public BasicClient {
258 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800259 typedef IProCameraCallbacks TCamCallbacks;
260
Igor Murashkin634a5152013-02-20 17:15:11 -0800261 ProClient(const sp<CameraService>& cameraService,
262 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800263 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800264 int cameraId,
265 int cameraFacing,
266 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800267 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800268 int servicePid);
269
270 virtual ~ProClient();
271
272 const sp<IProCameraCallbacks>& getRemoteCallback() {
273 return mRemoteCallback;
274 }
275
Igor Murashkin634a5152013-02-20 17:15:11 -0800276 /***
277 IProCamera implementation
278 ***/
Igor Murashkine6800ce2013-03-04 17:25:57 -0800279 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
280 = 0;
281 virtual status_t exclusiveTryLock() = 0;
282 virtual status_t exclusiveLock() = 0;
283 virtual status_t exclusiveUnlock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800284
Igor Murashkine6800ce2013-03-04 17:25:57 -0800285 virtual bool hasExclusiveLock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800286
287 // Note that the callee gets a copy of the metadata.
288 virtual int submitRequest(camera_metadata_t* metadata,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800289 bool streaming = false) = 0;
290 virtual status_t cancelRequest(int requestId) = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800291
Igor Murashkinbfc99152013-02-27 12:55:20 -0800292 // Callbacks from camera service
Igor Murashkine6800ce2013-03-04 17:25:57 -0800293 virtual void onExclusiveLockStolen() = 0;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800294
Igor Murashkin634a5152013-02-20 17:15:11 -0800295 protected:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800296 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700297
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800298 sp<IProCameraCallbacks> mRemoteCallback;
299 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700300
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800302
303 // Delay-load the Camera HAL module
304 virtual void onFirstRef();
305
Igor Murashkine6800ce2013-03-04 17:25:57 -0800306 // Step 1. Check if we can connect, before we acquire the service lock.
307 bool validateConnect(int cameraId,
308 /*inout*/
309 int& clientUid) const;
310
311 // Step 2. Check if we can connect, after we acquire the service lock.
312 bool canConnectUnsafe(int cameraId,
313 const String16& clientPackageName,
314 const sp<IBinder>& remoteCallback,
315 /*out*/
Igor Murashkine7ee7632013-06-11 18:10:18 -0700316 sp<BasicClient> &client);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800317
318 // When connection is successful, initialize client and track its death
319 bool connectFinishUnsafe(const sp<BasicClient>& client,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700320 const sp<IBinder>& remoteCallback);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800321
Igor Murashkin634a5152013-02-20 17:15:11 -0800322 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
323
Mathias Agopian65ab4712010-07-14 17:59:35 -0700324 Mutex mServiceLock;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700325 // either a Client or CameraDeviceClient
326 wp<BasicClient> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700327 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 int mNumberOfCameras;
329
Igor Murashkin634a5152013-02-20 17:15:11 -0800330 typedef wp<ProClient> weak_pro_client_ptr;
331 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
332
Igor Murashkinecf17e82012-10-02 16:05:11 -0700333 // needs to be called with mServiceLock held
Igor Murashkine7ee7632013-06-11 18:10:18 -0700334 sp<BasicClient> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkin634a5152013-02-20 17:15:11 -0800335 sp<ProClient> findProClientUnsafe(
336 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700337
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 // atomics to record whether the hardware is allocated to some client.
339 volatile int32_t mBusy[MAX_CAMERAS];
340 void setCameraBusy(int cameraId);
341 void setCameraFree(int cameraId);
342
343 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800344 MediaPlayer* newMediaPlayer(const char *file);
345
Mathias Agopian65ab4712010-07-14 17:59:35 -0700346 Mutex mSoundLock;
347 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
348 int mSoundRef; // reference count (release all MediaPlayer when 0)
349
Iliyan Malchev8951a972011-04-14 16:55:59 -0700350 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700351
Igor Murashkinbfc99152013-02-27 12:55:20 -0800352 Vector<sp<ICameraServiceListener> >
353 mListenerList;
354
355 // guard only mStatusList and the broadcasting of ICameraServiceListener
Igor Murashkincba2c162013-03-20 15:56:31 -0700356 mutable Mutex mStatusMutex;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800357 ICameraServiceListener::Status
358 mStatusList[MAX_CAMERAS];
359
Igor Murashkincba2c162013-03-20 15:56:31 -0700360 // Read the current status (locks mStatusMutex)
361 ICameraServiceListener::Status
362 getStatus(int cameraId) const;
363
Igor Murashkin93747b92013-05-01 15:42:20 -0700364 typedef Vector<ICameraServiceListener::Status> StatusVector;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800365 // Broadcast the new status if it changed (locks the service mutex)
366 void updateStatus(
367 ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -0700368 int32_t cameraId,
369 const StatusVector *rejectSourceStates = NULL);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800370
Igor Murashkinecf17e82012-10-02 16:05:11 -0700371 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800372 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800373
374 // Helpers
375 int getDeviceVersion(int cameraId, int* facing);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800376
377 bool isValidCameraId(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700378};
379
380} // namespace android
381
382#endif