blob: 8853e487789e788acbe284bd0e343108d2b73968 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mathias Agopian65ab4712010-07-14 17:59:35 -070016
17#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
18#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
19
Igor Murashkin634a5152013-02-20 17:15:11 -080020#include <utils/Vector.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080021#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070022#include <binder/BinderService.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080023#include <binder/IAppOpsCallback.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024#include <camera/ICameraService.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070025#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070026
Igor Murashkinc073ba52013-02-26 14:32:34 -080027#include <camera/ICamera.h>
28#include <camera/ICameraClient.h>
29#include <camera/IProCameraUser.h>
30#include <camera/IProCameraCallbacks.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070031#include <camera/camera2/ICameraDeviceUser.h>
32#include <camera/camera2/ICameraDeviceCallbacks.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080033#include <camera/VendorTagDescriptor.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);
Zhijun He2b59be82013-09-25 10:14:30 -070074 virtual status_t getCameraCharacteristics(int cameraId,
75 CameraMetadata* cameraInfo);
Ruben Brunkd1176ef2014-02-21 10:51:38 -080076 virtual status_t getCameraVendorTagDescriptor(/*out*/ sp<VendorTagDescriptor>& desc);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080077
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070078 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
79 const String16& clientPackageName, int clientUid,
80 /*out*/
81 sp<ICamera>& device);
82
83 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb,
84 int cameraId, const String16& clientPackageName, int clientUid,
85 /*out*/
86 sp<IProCameraUser>& device);
87
88 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -070089 const sp<ICameraDeviceCallbacks>& cameraCb,
90 int cameraId,
91 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070092 int clientUid,
93 /*out*/
94 sp<ICameraDeviceUser>& device);
Mathias Agopian65ab4712010-07-14 17:59:35 -070095
Igor Murashkinbfc99152013-02-27 12:55:20 -080096 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
97 virtual status_t removeListener(
98 const sp<ICameraServiceListener>& listener);
99
Igor Murashkin634a5152013-02-20 17:15:11 -0800100 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700101 virtual status_t onTransact(uint32_t code, const Parcel& data,
102 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -0800103
104 virtual status_t dump(int fd, const Vector<String16>& args);
105
106 /////////////////////////////////////////////////////////////////////
107 // Client functionality
108 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700109
110 enum sound_kind {
111 SOUND_SHUTTER = 0,
112 SOUND_RECORDING = 1,
113 NUM_SOUNDS
114 };
115
116 void loadSound();
117 void playSound(sound_kind kind);
118 void releaseSound();
119
Igor Murashkin98e24722013-06-19 19:51:04 -0700120 /////////////////////////////////////////////////////////////////////
121 // CameraDeviceFactory functionality
122 int getDeviceVersion(int cameraId, int* facing = NULL);
123
Igor Murashkin634a5152013-02-20 17:15:11 -0800124
125 /////////////////////////////////////////////////////////////////////
126 // CameraClient functionality
127
128 // returns plain pointer of client. Note that mClientLock should be acquired to
129 // prevent the client from destruction. The result can be NULL.
Igor Murashkine7ee7632013-06-11 18:10:18 -0700130 virtual BasicClient* getClientByIdUnsafe(int cameraId);
Igor Murashkin634a5152013-02-20 17:15:11 -0800131 virtual Mutex* getClientLockById(int cameraId);
132
133 class BasicClient : public virtual RefBase {
134 public:
135 virtual status_t initialize(camera_module_t *module) = 0;
136
137 virtual void disconnect() = 0;
138
Igor Murashkine7ee7632013-06-11 18:10:18 -0700139 // because we can't virtually inherit IInterface, which breaks
140 // virtual inheritance
141 virtual sp<IBinder> asBinderWrapper() = 0;
142
Igor Murashkine6800ce2013-03-04 17:25:57 -0800143 // Return the remote callback binder object (e.g. IProCameraCallbacks)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700144 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800145 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800146 }
147
Igor Murashkine7ee7632013-06-11 18:10:18 -0700148 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
149
Igor Murashkin634a5152013-02-20 17:15:11 -0800150 protected:
151 BasicClient(const sp<CameraService>& cameraService,
152 const sp<IBinder>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800153 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800154 int cameraId,
155 int cameraFacing,
156 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800157 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800158 int servicePid);
159
160 virtual ~BasicClient();
161
162 // the instance is in the middle of destruction. When this is set,
163 // the instance should not be accessed from callback.
164 // CameraService's mClientLock should be acquired to access this.
165 // - subclasses should set this to true in their destructors.
166 bool mDestructionStarted;
167
168 // these are initialized in the constructor.
169 sp<CameraService> mCameraService; // immutable after constructor
170 int mCameraId; // immutable after constructor
171 int mCameraFacing; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800172 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800173 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800174 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800175 pid_t mServicePid; // immutable after constructor
176
177 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700178 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800179
180 // permissions management
181 status_t startCameraOps();
182 status_t finishCameraOps();
183
184 // Notify client about a fatal error
185 virtual void notifyError() = 0;
186 private:
187 AppOpsManager mAppOpsManager;
188
189 class OpsCallback : public BnAppOpsCallback {
190 public:
191 OpsCallback(wp<BasicClient> client);
192 virtual void opChanged(int32_t op, const String16& packageName);
193
194 private:
195 wp<BasicClient> mClient;
196
197 }; // class OpsCallback
198
199 sp<OpsCallback> mOpsCallback;
200 // Track whether startCameraOps was called successfully, to avoid
201 // finishing what we didn't start.
202 bool mOpsActive;
203
204 // IAppOpsCallback interface, indirected through opListener
205 virtual void opChanged(int32_t op, const String16& packageName);
206 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800207
208 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700209 {
210 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800211 typedef ICameraClient TCamCallbacks;
212
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700213 // ICamera interface (see ICamera for details)
214 virtual void disconnect();
215 virtual status_t connect(const sp<ICameraClient>& client) = 0;
216 virtual status_t lock() = 0;
217 virtual status_t unlock() = 0;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700218 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700219 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700220 virtual status_t setPreviewCallbackTarget(
221 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700222 virtual status_t startPreview() = 0;
223 virtual void stopPreview() = 0;
224 virtual bool previewEnabled() = 0;
225 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
226 virtual status_t startRecording() = 0;
227 virtual void stopRecording() = 0;
228 virtual bool recordingEnabled() = 0;
229 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
230 virtual status_t autoFocus() = 0;
231 virtual status_t cancelAutoFocus() = 0;
232 virtual status_t takePicture(int msgType) = 0;
233 virtual status_t setParameters(const String8& params) = 0;
234 virtual String8 getParameters() const = 0;
235 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
236
237 // Interface used by CameraService
238 Client(const sp<CameraService>& cameraService,
239 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800240 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700241 int cameraId,
242 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700243 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800244 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700245 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700246 ~Client();
247
248 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800249 const sp<ICameraClient>& getRemoteCallback() {
250 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700251 }
252
Igor Murashkine7ee7632013-06-11 18:10:18 -0700253 virtual sp<IBinder> asBinderWrapper() {
254 return asBinder();
255 }
256
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700257 protected:
258 static Mutex* getClientLockFromCookie(void* user);
259 // convert client from cookie. Client lock should be acquired before getting Client.
260 static Client* getClientFromCookie(void* user);
261
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800262 virtual void notifyError();
263
Igor Murashkin634a5152013-02-20 17:15:11 -0800264 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700265
Igor Murashkin634a5152013-02-20 17:15:11 -0800266 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800267 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800268
269 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800270
271 class ProClient : public BnProCameraUser, public BasicClient {
272 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800273 typedef IProCameraCallbacks TCamCallbacks;
274
Igor Murashkin634a5152013-02-20 17:15:11 -0800275 ProClient(const sp<CameraService>& cameraService,
276 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800277 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800278 int cameraId,
279 int cameraFacing,
280 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800281 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800282 int servicePid);
283
284 virtual ~ProClient();
285
286 const sp<IProCameraCallbacks>& getRemoteCallback() {
287 return mRemoteCallback;
288 }
289
Igor Murashkin634a5152013-02-20 17:15:11 -0800290 /***
291 IProCamera implementation
292 ***/
Igor Murashkine6800ce2013-03-04 17:25:57 -0800293 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
294 = 0;
295 virtual status_t exclusiveTryLock() = 0;
296 virtual status_t exclusiveLock() = 0;
297 virtual status_t exclusiveUnlock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800298
Igor Murashkine6800ce2013-03-04 17:25:57 -0800299 virtual bool hasExclusiveLock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800300
301 // Note that the callee gets a copy of the metadata.
302 virtual int submitRequest(camera_metadata_t* metadata,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800303 bool streaming = false) = 0;
304 virtual status_t cancelRequest(int requestId) = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800305
Igor Murashkinbfc99152013-02-27 12:55:20 -0800306 // Callbacks from camera service
Igor Murashkine6800ce2013-03-04 17:25:57 -0800307 virtual void onExclusiveLockStolen() = 0;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800308
Igor Murashkin634a5152013-02-20 17:15:11 -0800309 protected:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800310 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700311
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800312 sp<IProCameraCallbacks> mRemoteCallback;
313 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700314
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800316
317 // Delay-load the Camera HAL module
318 virtual void onFirstRef();
319
Igor Murashkine6800ce2013-03-04 17:25:57 -0800320 // Step 1. Check if we can connect, before we acquire the service lock.
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700321 status_t validateConnect(int cameraId,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800322 /*inout*/
323 int& clientUid) const;
324
325 // Step 2. Check if we can connect, after we acquire the service lock.
326 bool canConnectUnsafe(int cameraId,
327 const String16& clientPackageName,
328 const sp<IBinder>& remoteCallback,
329 /*out*/
Igor Murashkine7ee7632013-06-11 18:10:18 -0700330 sp<BasicClient> &client);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800331
332 // When connection is successful, initialize client and track its death
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700333 status_t connectFinishUnsafe(const sp<BasicClient>& client,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700334 const sp<IBinder>& remoteCallback);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800335
Igor Murashkin634a5152013-02-20 17:15:11 -0800336 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
337
Mathias Agopian65ab4712010-07-14 17:59:35 -0700338 Mutex mServiceLock;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700339 // either a Client or CameraDeviceClient
340 wp<BasicClient> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700341 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700342 int mNumberOfCameras;
343
Igor Murashkin634a5152013-02-20 17:15:11 -0800344 typedef wp<ProClient> weak_pro_client_ptr;
345 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
346
Igor Murashkinecf17e82012-10-02 16:05:11 -0700347 // needs to be called with mServiceLock held
Igor Murashkine7ee7632013-06-11 18:10:18 -0700348 sp<BasicClient> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkin634a5152013-02-20 17:15:11 -0800349 sp<ProClient> findProClientUnsafe(
350 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700351
Mathias Agopian65ab4712010-07-14 17:59:35 -0700352 // atomics to record whether the hardware is allocated to some client.
353 volatile int32_t mBusy[MAX_CAMERAS];
354 void setCameraBusy(int cameraId);
355 void setCameraFree(int cameraId);
356
357 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800358 MediaPlayer* newMediaPlayer(const char *file);
359
Mathias Agopian65ab4712010-07-14 17:59:35 -0700360 Mutex mSoundLock;
361 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
362 int mSoundRef; // reference count (release all MediaPlayer when 0)
363
Iliyan Malchev8951a972011-04-14 16:55:59 -0700364 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700365
Igor Murashkinbfc99152013-02-27 12:55:20 -0800366 Vector<sp<ICameraServiceListener> >
367 mListenerList;
368
369 // guard only mStatusList and the broadcasting of ICameraServiceListener
Igor Murashkincba2c162013-03-20 15:56:31 -0700370 mutable Mutex mStatusMutex;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800371 ICameraServiceListener::Status
372 mStatusList[MAX_CAMERAS];
373
Igor Murashkincba2c162013-03-20 15:56:31 -0700374 // Read the current status (locks mStatusMutex)
375 ICameraServiceListener::Status
376 getStatus(int cameraId) const;
377
Igor Murashkin93747b92013-05-01 15:42:20 -0700378 typedef Vector<ICameraServiceListener::Status> StatusVector;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800379 // Broadcast the new status if it changed (locks the service mutex)
380 void updateStatus(
381 ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -0700382 int32_t cameraId,
383 const StatusVector *rejectSourceStates = NULL);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800384
Igor Murashkinecf17e82012-10-02 16:05:11 -0700385 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800386 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800387
388 // Helpers
Igor Murashkinbfc99152013-02-27 12:55:20 -0800389
390 bool isValidCameraId(int cameraId);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800391
392 bool setUpVendorTags();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700393};
394
395} // namespace android
396
397#endif