blob: c5e495fe5d500bfc618a67e7219e9cb2b9b7f9f2 [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 Murashkinbfb5d5e2013-02-20 17:15:11 -080021#include <utils/Vector.h>
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080022#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070023#include <binder/BinderService.h>
Eino-Ville Talvala48af7e82013-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 Murashkinb84d9352013-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>
32
Igor Murashkin8fdfbe22013-02-27 12:55:20 -080033#include <camera/ICameraServiceListener.h>
34
Mathias Agopian65ab4712010-07-14 17:59:35 -070035/* This needs to be increased if we can have more cameras */
36#define MAX_CAMERAS 2
37
38namespace android {
39
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070040extern volatile int32_t gLogLevel;
41
Mathias Agopian65ab4712010-07-14 17:59:35 -070042class MemoryHeapBase;
43class MediaPlayer;
44
Mathias Agopian5462fc92010-07-14 18:41:18 -070045class CameraService :
46 public BinderService<CameraService>,
Igor Murashkin8dcdb952012-10-02 16:05:11 -070047 public BnCameraService,
48 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070049{
Mathias Agopian5462fc92010-07-14 18:41:18 -070050 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070051public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070052 class Client;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080053 class BasicClient;
54
55 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070056 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070057
58 CameraService();
59 virtual ~CameraService();
60
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080061 /////////////////////////////////////////////////////////////////////
62 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070063 virtual int32_t getNumberOfCameras();
64 virtual status_t getCameraInfo(int cameraId,
65 struct CameraInfo* cameraInfo);
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080066
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,
Igor Murashkinb84d9352013-02-26 14:32:34 -080070 int cameraId, const String16& clientPackageName, int clientUid);
Mathias Agopian65ab4712010-07-14 17:59:35 -070071
Igor Murashkin8fdfbe22013-02-27 12:55:20 -080072 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
73 virtual status_t removeListener(
74 const sp<ICameraServiceListener>& listener);
75
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080076 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -070077 virtual status_t onTransact(uint32_t code, const Parcel& data,
78 Parcel* reply, uint32_t flags);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080079
80 virtual status_t dump(int fd, const Vector<String16>& args);
81
82 /////////////////////////////////////////////////////////////////////
83 // Client functionality
84 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -070085
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
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080096
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
Igor Murashkin9ab909b2013-03-04 17:25:57 -0800111 // Return the remote callback binder object (e.g. IProCameraCallbacks)
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800112 wp<IBinder> getRemote() {
Igor Murashkina2e203b2013-03-01 16:22:28 -0800113 return mRemoteBinder;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800114 }
115
116 protected:
117 BasicClient(const sp<CameraService>& cameraService,
118 const sp<IBinder>& remoteCallback,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800119 const String16& clientPackageName,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800120 int cameraId,
121 int cameraFacing,
122 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800123 uid_t clientUid,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800124 int servicePid);
125
126 virtual ~BasicClient();
127
128 // the instance is in the middle of destruction. When this is set,
129 // the instance should not be accessed from callback.
130 // CameraService's mClientLock should be acquired to access this.
131 // - subclasses should set this to true in their destructors.
132 bool mDestructionStarted;
133
134 // these are initialized in the constructor.
135 sp<CameraService> mCameraService; // immutable after constructor
136 int mCameraId; // immutable after constructor
137 int mCameraFacing; // immutable after constructor
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800138 const String16 mClientPackageName;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800139 pid_t mClientPid;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800140 uid_t mClientUid; // immutable after constructor
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800141 pid_t mServicePid; // immutable after constructor
142
143 // - The app-side Binder interface to receive callbacks from us
Igor Murashkina2e203b2013-03-01 16:22:28 -0800144 wp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800145
146 // permissions management
147 status_t startCameraOps();
148 status_t finishCameraOps();
149
150 // Notify client about a fatal error
151 virtual void notifyError() = 0;
152 private:
153 AppOpsManager mAppOpsManager;
154
155 class OpsCallback : public BnAppOpsCallback {
156 public:
157 OpsCallback(wp<BasicClient> client);
158 virtual void opChanged(int32_t op, const String16& packageName);
159
160 private:
161 wp<BasicClient> mClient;
162
163 }; // class OpsCallback
164
165 sp<OpsCallback> mOpsCallback;
166 // Track whether startCameraOps was called successfully, to avoid
167 // finishing what we didn't start.
168 bool mOpsActive;
169
170 // IAppOpsCallback interface, indirected through opListener
171 virtual void opChanged(int32_t op, const String16& packageName);
172 }; // class BasicClient
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800173
174 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700175 {
176 public:
Igor Murashkina2e203b2013-03-01 16:22:28 -0800177 typedef ICameraClient TCamCallbacks;
178
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700179 // ICamera interface (see ICamera for details)
180 virtual void disconnect();
181 virtual status_t connect(const sp<ICameraClient>& client) = 0;
182 virtual status_t lock() = 0;
183 virtual status_t unlock() = 0;
184 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden484566c2012-12-18 09:46:54 -0800185 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700186 virtual void setPreviewCallbackFlag(int flag) = 0;
187 virtual status_t startPreview() = 0;
188 virtual void stopPreview() = 0;
189 virtual bool previewEnabled() = 0;
190 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
191 virtual status_t startRecording() = 0;
192 virtual void stopRecording() = 0;
193 virtual bool recordingEnabled() = 0;
194 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
195 virtual status_t autoFocus() = 0;
196 virtual status_t cancelAutoFocus() = 0;
197 virtual status_t takePicture(int msgType) = 0;
198 virtual status_t setParameters(const String8& params) = 0;
199 virtual String8 getParameters() const = 0;
200 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
201
202 // Interface used by CameraService
203 Client(const sp<CameraService>& cameraService,
204 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800205 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700206 int cameraId,
207 int cameraFacing,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700208 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800209 uid_t clientUid,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700210 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700211 ~Client();
212
213 // return our camera client
Igor Murashkina2e203b2013-03-01 16:22:28 -0800214 const sp<ICameraClient>& getRemoteCallback() {
215 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700216 }
217
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700218 protected:
219 static Mutex* getClientLockFromCookie(void* user);
220 // convert client from cookie. Client lock should be acquired before getting Client.
221 static Client* getClientFromCookie(void* user);
222
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800223 virtual void notifyError();
224
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800225 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700226
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800227 // - The app-side Binder interface to receive callbacks from us
Igor Murashkina2e203b2013-03-01 16:22:28 -0800228 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800229
230 }; // class Client
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800231
232 class ProClient : public BnProCameraUser, public BasicClient {
233 public:
Igor Murashkina2e203b2013-03-01 16:22:28 -0800234 typedef IProCameraCallbacks TCamCallbacks;
235
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800236 ProClient(const sp<CameraService>& cameraService,
237 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800238 const String16& clientPackageName,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800239 int cameraId,
240 int cameraFacing,
241 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800242 uid_t clientUid,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800243 int servicePid);
244
245 virtual ~ProClient();
246
247 const sp<IProCameraCallbacks>& getRemoteCallback() {
248 return mRemoteCallback;
249 }
250
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800251 /***
252 IProCamera implementation
253 ***/
Igor Murashkin9ab909b2013-03-04 17:25:57 -0800254 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
255 = 0;
256 virtual status_t exclusiveTryLock() = 0;
257 virtual status_t exclusiveLock() = 0;
258 virtual status_t exclusiveUnlock() = 0;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800259
Igor Murashkin9ab909b2013-03-04 17:25:57 -0800260 virtual bool hasExclusiveLock() = 0;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800261
262 // Note that the callee gets a copy of the metadata.
263 virtual int submitRequest(camera_metadata_t* metadata,
Igor Murashkin9ab909b2013-03-04 17:25:57 -0800264 bool streaming = false) = 0;
265 virtual status_t cancelRequest(int requestId) = 0;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800266
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800267 // Callbacks from camera service
Igor Murashkin9ab909b2013-03-04 17:25:57 -0800268 virtual void onExclusiveLockStolen() = 0;
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800269
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800270 protected:
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800271 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700272
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800273 sp<IProCameraCallbacks> mRemoteCallback;
274 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700275
Mathias Agopian65ab4712010-07-14 17:59:35 -0700276private:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800277
278 // Delay-load the Camera HAL module
279 virtual void onFirstRef();
280
Igor Murashkin9ab909b2013-03-04 17:25:57 -0800281 // Step 1. Check if we can connect, before we acquire the service lock.
282 bool validateConnect(int cameraId,
283 /*inout*/
284 int& clientUid) const;
285
286 // Step 2. Check if we can connect, after we acquire the service lock.
287 bool canConnectUnsafe(int cameraId,
288 const String16& clientPackageName,
289 const sp<IBinder>& remoteCallback,
290 /*out*/
291 sp<Client> &client);
292
293 // When connection is successful, initialize client and track its death
294 bool connectFinishUnsafe(const sp<BasicClient>& client,
295 const sp<IBinder>& clientBinder);
296
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800297 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
298
Mathias Agopian65ab4712010-07-14 17:59:35 -0700299 Mutex mServiceLock;
300 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700301 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700302 int mNumberOfCameras;
303
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800304 typedef wp<ProClient> weak_pro_client_ptr;
305 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
306
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700307 // needs to be called with mServiceLock held
Igor Murashkin507994d2012-10-05 10:44:57 -0700308 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800309 sp<ProClient> findProClientUnsafe(
310 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700311
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312 // atomics to record whether the hardware is allocated to some client.
313 volatile int32_t mBusy[MAX_CAMERAS];
314 void setCameraBusy(int cameraId);
315 void setCameraFree(int cameraId);
316
317 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800318 MediaPlayer* newMediaPlayer(const char *file);
319
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 Mutex mSoundLock;
321 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
322 int mSoundRef; // reference count (release all MediaPlayer when 0)
323
Iliyan Malchev8951a972011-04-14 16:55:59 -0700324 camera_module_t *mModule;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700325
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800326 Vector<sp<ICameraServiceListener> >
327 mListenerList;
328
329 // guard only mStatusList and the broadcasting of ICameraServiceListener
330 Mutex mStatusMutex;
331 ICameraServiceListener::Status
332 mStatusList[MAX_CAMERAS];
333
334 // Broadcast the new status if it changed (locks the service mutex)
335 void updateStatus(
336 ICameraServiceListener::Status status,
337 int32_t cameraId);
338 // Call this one when the service mutex is already held (idempotent)
339 void updateStatusUnsafe(
340 ICameraServiceListener::Status status,
341 int32_t cameraId);
342
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700343 // IBinder::DeathRecipient implementation
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800344 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800345
346 // Helpers
347 int getDeviceVersion(int cameraId, int* facing);
Igor Murashkin8fdfbe22013-02-27 12:55:20 -0800348
349 bool isValidCameraId(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700350};
351
352} // namespace android
353
354#endif