blob: 7f3691a945574636f69335fe3e233345649ed29f [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>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032#include <camera/camera2/ICameraDeviceUser.h>
33#include <camera/camera2/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
Eino-Ville Talvalac9ab2b02016-01-13 10:07:04 -080058 // Event log ID
59 static const int SN_EVENT_LOG_ID = 0x534e4554;
60
Igor Murashkin634a5152013-02-20 17:15:11 -080061 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070062 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070063
64 CameraService();
65 virtual ~CameraService();
66
Igor Murashkin634a5152013-02-20 17:15:11 -080067 /////////////////////////////////////////////////////////////////////
Igor Murashkincba2c162013-03-20 15:56:31 -070068 // HAL Callbacks
69 virtual void onDeviceStatusChanged(int cameraId,
70 int newStatus);
71
72 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -080073 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070074 virtual int32_t getNumberOfCameras();
75 virtual status_t getCameraInfo(int cameraId,
76 struct CameraInfo* cameraInfo);
Zhijun He2b59be82013-09-25 10:14:30 -070077 virtual status_t getCameraCharacteristics(int cameraId,
78 CameraMetadata* cameraInfo);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080079
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070080 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
81 const String16& clientPackageName, int clientUid,
82 /*out*/
83 sp<ICamera>& device);
84
85 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb,
86 int cameraId, const String16& clientPackageName, int clientUid,
87 /*out*/
88 sp<IProCameraUser>& device);
89
90 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -070091 const sp<ICameraDeviceCallbacks>& cameraCb,
92 int cameraId,
93 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070094 int clientUid,
95 /*out*/
96 sp<ICameraDeviceUser>& device);
Mathias Agopian65ab4712010-07-14 17:59:35 -070097
Igor Murashkinbfc99152013-02-27 12:55:20 -080098 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
99 virtual status_t removeListener(
100 const sp<ICameraServiceListener>& listener);
101
Igor Murashkin634a5152013-02-20 17:15:11 -0800102 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700103 virtual status_t onTransact(uint32_t code, const Parcel& data,
104 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -0800105
106 virtual status_t dump(int fd, const Vector<String16>& args);
107
108 /////////////////////////////////////////////////////////////////////
109 // Client functionality
110 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111
112 enum sound_kind {
113 SOUND_SHUTTER = 0,
114 SOUND_RECORDING = 1,
115 NUM_SOUNDS
116 };
117
118 void loadSound();
119 void playSound(sound_kind kind);
120 void releaseSound();
121
Igor Murashkin98e24722013-06-19 19:51:04 -0700122 /////////////////////////////////////////////////////////////////////
123 // CameraDeviceFactory functionality
124 int getDeviceVersion(int cameraId, int* facing = NULL);
125
Igor Murashkin634a5152013-02-20 17:15:11 -0800126
127 /////////////////////////////////////////////////////////////////////
128 // CameraClient functionality
129
130 // returns plain pointer of client. Note that mClientLock should be acquired to
131 // prevent the client from destruction. The result can be NULL.
Igor Murashkine7ee7632013-06-11 18:10:18 -0700132 virtual BasicClient* getClientByIdUnsafe(int cameraId);
Igor Murashkin634a5152013-02-20 17:15:11 -0800133 virtual Mutex* getClientLockById(int cameraId);
134
135 class BasicClient : public virtual RefBase {
136 public:
137 virtual status_t initialize(camera_module_t *module) = 0;
138
139 virtual void disconnect() = 0;
140
Igor Murashkine7ee7632013-06-11 18:10:18 -0700141 // because we can't virtually inherit IInterface, which breaks
142 // virtual inheritance
143 virtual sp<IBinder> asBinderWrapper() = 0;
144
Igor Murashkine6800ce2013-03-04 17:25:57 -0800145 // Return the remote callback binder object (e.g. IProCameraCallbacks)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700146 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800147 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800148 }
149
Eino-Ville Talvalac9ab2b02016-01-13 10:07:04 -0800150 // Disallows dumping over binder interface
151 virtual status_t dump(int fd, const Vector<String16>& args);
152 // Internal dump method to be called by CameraService
153 virtual status_t dumpClient(int fd, const Vector<String16>& args) = 0;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700154
Igor Murashkin634a5152013-02-20 17:15:11 -0800155 protected:
156 BasicClient(const sp<CameraService>& cameraService,
157 const sp<IBinder>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800158 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800159 int cameraId,
160 int cameraFacing,
161 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800162 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800163 int servicePid);
164
165 virtual ~BasicClient();
166
167 // the instance is in the middle of destruction. When this is set,
168 // the instance should not be accessed from callback.
169 // CameraService's mClientLock should be acquired to access this.
170 // - subclasses should set this to true in their destructors.
171 bool mDestructionStarted;
172
173 // these are initialized in the constructor.
174 sp<CameraService> mCameraService; // immutable after constructor
175 int mCameraId; // immutable after constructor
176 int mCameraFacing; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800177 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800178 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800179 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800180 pid_t mServicePid; // immutable after constructor
181
182 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700183 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800184
185 // permissions management
186 status_t startCameraOps();
187 status_t finishCameraOps();
188
189 // Notify client about a fatal error
190 virtual void notifyError() = 0;
191 private:
192 AppOpsManager mAppOpsManager;
193
194 class OpsCallback : public BnAppOpsCallback {
195 public:
196 OpsCallback(wp<BasicClient> client);
197 virtual void opChanged(int32_t op, const String16& packageName);
198
199 private:
200 wp<BasicClient> mClient;
201
202 }; // class OpsCallback
203
204 sp<OpsCallback> mOpsCallback;
205 // Track whether startCameraOps was called successfully, to avoid
206 // finishing what we didn't start.
207 bool mOpsActive;
208
209 // IAppOpsCallback interface, indirected through opListener
210 virtual void opChanged(int32_t op, const String16& packageName);
211 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800212
213 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700214 {
215 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800216 typedef ICameraClient TCamCallbacks;
217
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700218 // ICamera interface (see ICamera for details)
219 virtual void disconnect();
220 virtual status_t connect(const sp<ICameraClient>& client) = 0;
221 virtual status_t lock() = 0;
222 virtual status_t unlock() = 0;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700223 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700224 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700225 virtual status_t setPreviewCallbackTarget(
226 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700227 virtual status_t startPreview() = 0;
228 virtual void stopPreview() = 0;
229 virtual bool previewEnabled() = 0;
230 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
231 virtual status_t startRecording() = 0;
232 virtual void stopRecording() = 0;
233 virtual bool recordingEnabled() = 0;
234 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
235 virtual status_t autoFocus() = 0;
236 virtual status_t cancelAutoFocus() = 0;
237 virtual status_t takePicture(int msgType) = 0;
238 virtual status_t setParameters(const String8& params) = 0;
239 virtual String8 getParameters() const = 0;
240 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
241
242 // Interface used by CameraService
243 Client(const sp<CameraService>& cameraService,
244 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800245 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700246 int cameraId,
247 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700248 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800249 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700250 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700251 ~Client();
252
253 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800254 const sp<ICameraClient>& getRemoteCallback() {
255 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700256 }
257
Igor Murashkine7ee7632013-06-11 18:10:18 -0700258 virtual sp<IBinder> asBinderWrapper() {
259 return asBinder();
260 }
261
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700262 protected:
263 static Mutex* getClientLockFromCookie(void* user);
264 // convert client from cookie. Client lock should be acquired before getting Client.
265 static Client* getClientFromCookie(void* user);
266
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800267 virtual void notifyError();
268
Igor Murashkin634a5152013-02-20 17:15:11 -0800269 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700270
Igor Murashkin634a5152013-02-20 17:15:11 -0800271 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800272 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800273
274 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800275
276 class ProClient : public BnProCameraUser, public BasicClient {
277 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800278 typedef IProCameraCallbacks TCamCallbacks;
279
Igor Murashkin634a5152013-02-20 17:15:11 -0800280 ProClient(const sp<CameraService>& cameraService,
281 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800282 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800283 int cameraId,
284 int cameraFacing,
285 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800286 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800287 int servicePid);
288
289 virtual ~ProClient();
290
291 const sp<IProCameraCallbacks>& getRemoteCallback() {
292 return mRemoteCallback;
293 }
294
Igor Murashkin634a5152013-02-20 17:15:11 -0800295 /***
296 IProCamera implementation
297 ***/
Igor Murashkine6800ce2013-03-04 17:25:57 -0800298 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
299 = 0;
300 virtual status_t exclusiveTryLock() = 0;
301 virtual status_t exclusiveLock() = 0;
302 virtual status_t exclusiveUnlock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800303
Igor Murashkine6800ce2013-03-04 17:25:57 -0800304 virtual bool hasExclusiveLock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800305
306 // Note that the callee gets a copy of the metadata.
307 virtual int submitRequest(camera_metadata_t* metadata,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800308 bool streaming = false) = 0;
309 virtual status_t cancelRequest(int requestId) = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800310
Igor Murashkinbfc99152013-02-27 12:55:20 -0800311 // Callbacks from camera service
Igor Murashkine6800ce2013-03-04 17:25:57 -0800312 virtual void onExclusiveLockStolen() = 0;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800313
Igor Murashkin634a5152013-02-20 17:15:11 -0800314 protected:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800315 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700316
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800317 sp<IProCameraCallbacks> mRemoteCallback;
318 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700319
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800321
322 // Delay-load the Camera HAL module
323 virtual void onFirstRef();
324
Igor Murashkine6800ce2013-03-04 17:25:57 -0800325 // Step 1. Check if we can connect, before we acquire the service lock.
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700326 status_t validateConnect(int cameraId,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800327 /*inout*/
328 int& clientUid) const;
329
330 // Step 2. Check if we can connect, after we acquire the service lock.
331 bool canConnectUnsafe(int cameraId,
332 const String16& clientPackageName,
333 const sp<IBinder>& remoteCallback,
334 /*out*/
Igor Murashkine7ee7632013-06-11 18:10:18 -0700335 sp<BasicClient> &client);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800336
337 // When connection is successful, initialize client and track its death
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700338 status_t connectFinishUnsafe(const sp<BasicClient>& client,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700339 const sp<IBinder>& remoteCallback);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800340
Igor Murashkin634a5152013-02-20 17:15:11 -0800341 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
342
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343 Mutex mServiceLock;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700344 // either a Client or CameraDeviceClient
345 wp<BasicClient> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700346 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700347 int mNumberOfCameras;
348
Igor Murashkin634a5152013-02-20 17:15:11 -0800349 typedef wp<ProClient> weak_pro_client_ptr;
350 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
351
Igor Murashkinecf17e82012-10-02 16:05:11 -0700352 // needs to be called with mServiceLock held
Igor Murashkine7ee7632013-06-11 18:10:18 -0700353 sp<BasicClient> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkin634a5152013-02-20 17:15:11 -0800354 sp<ProClient> findProClientUnsafe(
355 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700356
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 // atomics to record whether the hardware is allocated to some client.
358 volatile int32_t mBusy[MAX_CAMERAS];
359 void setCameraBusy(int cameraId);
360 void setCameraFree(int cameraId);
361
362 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800363 MediaPlayer* newMediaPlayer(const char *file);
364
Mathias Agopian65ab4712010-07-14 17:59:35 -0700365 Mutex mSoundLock;
366 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
367 int mSoundRef; // reference count (release all MediaPlayer when 0)
368
Iliyan Malchev8951a972011-04-14 16:55:59 -0700369 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700370
Igor Murashkinbfc99152013-02-27 12:55:20 -0800371 Vector<sp<ICameraServiceListener> >
372 mListenerList;
373
374 // guard only mStatusList and the broadcasting of ICameraServiceListener
Igor Murashkincba2c162013-03-20 15:56:31 -0700375 mutable Mutex mStatusMutex;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800376 ICameraServiceListener::Status
377 mStatusList[MAX_CAMERAS];
378
Igor Murashkincba2c162013-03-20 15:56:31 -0700379 // Read the current status (locks mStatusMutex)
380 ICameraServiceListener::Status
381 getStatus(int cameraId) const;
382
Igor Murashkin93747b92013-05-01 15:42:20 -0700383 typedef Vector<ICameraServiceListener::Status> StatusVector;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800384 // Broadcast the new status if it changed (locks the service mutex)
385 void updateStatus(
386 ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -0700387 int32_t cameraId,
388 const StatusVector *rejectSourceStates = NULL);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800389
Igor Murashkinecf17e82012-10-02 16:05:11 -0700390 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800391 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800392
393 // Helpers
Igor Murashkinbfc99152013-02-27 12:55:20 -0800394
395 bool isValidCameraId(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700396};
397
398} // namespace android
399
400#endif