blob: b34a0f6422f51d2a985a4287453157c8ac28d9fd [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
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
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070075 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
76 const String16& clientPackageName, int clientUid,
77 /*out*/
78 sp<ICamera>& device);
79
80 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb,
81 int cameraId, const String16& clientPackageName, int clientUid,
82 /*out*/
83 sp<IProCameraUser>& device);
84
85 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -070086 const sp<ICameraDeviceCallbacks>& cameraCb,
87 int cameraId,
88 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -070089 int clientUid,
90 /*out*/
91 sp<ICameraDeviceUser>& device);
Mathias Agopian65ab4712010-07-14 17:59:35 -070092
Igor Murashkinbfc99152013-02-27 12:55:20 -080093 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
94 virtual status_t removeListener(
95 const sp<ICameraServiceListener>& listener);
96
Igor Murashkin634a5152013-02-20 17:15:11 -080097 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -070098 virtual status_t onTransact(uint32_t code, const Parcel& data,
99 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -0800100
101 virtual status_t dump(int fd, const Vector<String16>& args);
102
103 /////////////////////////////////////////////////////////////////////
104 // Client functionality
105 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700106
107 enum sound_kind {
108 SOUND_SHUTTER = 0,
109 SOUND_RECORDING = 1,
110 NUM_SOUNDS
111 };
112
113 void loadSound();
114 void playSound(sound_kind kind);
115 void releaseSound();
116
Igor Murashkin98e24722013-06-19 19:51:04 -0700117 /////////////////////////////////////////////////////////////////////
118 // CameraDeviceFactory functionality
119 int getDeviceVersion(int cameraId, int* facing = NULL);
120
Igor Murashkin634a5152013-02-20 17:15:11 -0800121
122 /////////////////////////////////////////////////////////////////////
123 // CameraClient functionality
124
125 // returns plain pointer of client. Note that mClientLock should be acquired to
126 // prevent the client from destruction. The result can be NULL.
Igor Murashkine7ee7632013-06-11 18:10:18 -0700127 virtual BasicClient* getClientByIdUnsafe(int cameraId);
Igor Murashkin634a5152013-02-20 17:15:11 -0800128 virtual Mutex* getClientLockById(int cameraId);
129
130 class BasicClient : public virtual RefBase {
131 public:
132 virtual status_t initialize(camera_module_t *module) = 0;
133
134 virtual void disconnect() = 0;
135
Igor Murashkine7ee7632013-06-11 18:10:18 -0700136 // because we can't virtually inherit IInterface, which breaks
137 // virtual inheritance
138 virtual sp<IBinder> asBinderWrapper() = 0;
139
Igor Murashkine6800ce2013-03-04 17:25:57 -0800140 // Return the remote callback binder object (e.g. IProCameraCallbacks)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700141 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800142 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800143 }
144
Igor Murashkine7ee7632013-06-11 18:10:18 -0700145 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
146
Igor Murashkin634a5152013-02-20 17:15:11 -0800147 protected:
148 BasicClient(const sp<CameraService>& cameraService,
149 const sp<IBinder>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800150 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800151 int cameraId,
152 int cameraFacing,
153 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800154 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800155 int servicePid);
156
157 virtual ~BasicClient();
158
159 // the instance is in the middle of destruction. When this is set,
160 // the instance should not be accessed from callback.
161 // CameraService's mClientLock should be acquired to access this.
162 // - subclasses should set this to true in their destructors.
163 bool mDestructionStarted;
164
165 // these are initialized in the constructor.
166 sp<CameraService> mCameraService; // immutable after constructor
167 int mCameraId; // immutable after constructor
168 int mCameraFacing; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800169 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800170 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800171 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800172 pid_t mServicePid; // immutable after constructor
173
174 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700175 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800176
177 // permissions management
178 status_t startCameraOps();
179 status_t finishCameraOps();
180
181 // Notify client about a fatal error
182 virtual void notifyError() = 0;
183 private:
184 AppOpsManager mAppOpsManager;
185
186 class OpsCallback : public BnAppOpsCallback {
187 public:
188 OpsCallback(wp<BasicClient> client);
189 virtual void opChanged(int32_t op, const String16& packageName);
190
191 private:
192 wp<BasicClient> mClient;
193
194 }; // class OpsCallback
195
196 sp<OpsCallback> mOpsCallback;
197 // Track whether startCameraOps was called successfully, to avoid
198 // finishing what we didn't start.
199 bool mOpsActive;
200
201 // IAppOpsCallback interface, indirected through opListener
202 virtual void opChanged(int32_t op, const String16& packageName);
203 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800204
205 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700206 {
207 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800208 typedef ICameraClient TCamCallbacks;
209
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700210 // ICamera interface (see ICamera for details)
211 virtual void disconnect();
212 virtual status_t connect(const sp<ICameraClient>& client) = 0;
213 virtual status_t lock() = 0;
214 virtual status_t unlock() = 0;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700215 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700216 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700217 virtual status_t setPreviewCallbackTarget(
218 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700219 virtual status_t startPreview() = 0;
220 virtual void stopPreview() = 0;
221 virtual bool previewEnabled() = 0;
222 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
223 virtual status_t startRecording() = 0;
224 virtual void stopRecording() = 0;
225 virtual bool recordingEnabled() = 0;
226 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
227 virtual status_t autoFocus() = 0;
228 virtual status_t cancelAutoFocus() = 0;
229 virtual status_t takePicture(int msgType) = 0;
230 virtual status_t setParameters(const String8& params) = 0;
231 virtual String8 getParameters() const = 0;
232 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
233
234 // Interface used by CameraService
235 Client(const sp<CameraService>& cameraService,
236 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800237 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700238 int cameraId,
239 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700240 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800241 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700242 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700243 ~Client();
244
245 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800246 const sp<ICameraClient>& getRemoteCallback() {
247 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700248 }
249
Igor Murashkine7ee7632013-06-11 18:10:18 -0700250 virtual sp<IBinder> asBinderWrapper() {
251 return asBinder();
252 }
253
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700254 protected:
255 static Mutex* getClientLockFromCookie(void* user);
256 // convert client from cookie. Client lock should be acquired before getting Client.
257 static Client* getClientFromCookie(void* user);
258
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800259 virtual void notifyError();
260
Igor Murashkin634a5152013-02-20 17:15:11 -0800261 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700262
Igor Murashkin634a5152013-02-20 17:15:11 -0800263 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800264 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800265
266 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800267
268 class ProClient : public BnProCameraUser, public BasicClient {
269 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800270 typedef IProCameraCallbacks TCamCallbacks;
271
Igor Murashkin634a5152013-02-20 17:15:11 -0800272 ProClient(const sp<CameraService>& cameraService,
273 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800274 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800275 int cameraId,
276 int cameraFacing,
277 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800278 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800279 int servicePid);
280
281 virtual ~ProClient();
282
283 const sp<IProCameraCallbacks>& getRemoteCallback() {
284 return mRemoteCallback;
285 }
286
Igor Murashkin634a5152013-02-20 17:15:11 -0800287 /***
288 IProCamera implementation
289 ***/
Igor Murashkine6800ce2013-03-04 17:25:57 -0800290 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
291 = 0;
292 virtual status_t exclusiveTryLock() = 0;
293 virtual status_t exclusiveLock() = 0;
294 virtual status_t exclusiveUnlock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800295
Igor Murashkine6800ce2013-03-04 17:25:57 -0800296 virtual bool hasExclusiveLock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800297
298 // Note that the callee gets a copy of the metadata.
299 virtual int submitRequest(camera_metadata_t* metadata,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800300 bool streaming = false) = 0;
301 virtual status_t cancelRequest(int requestId) = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800302
Igor Murashkinbfc99152013-02-27 12:55:20 -0800303 // Callbacks from camera service
Igor Murashkine6800ce2013-03-04 17:25:57 -0800304 virtual void onExclusiveLockStolen() = 0;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800305
Igor Murashkin634a5152013-02-20 17:15:11 -0800306 protected:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800307 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700308
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800309 sp<IProCameraCallbacks> mRemoteCallback;
310 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700311
Mathias Agopian65ab4712010-07-14 17:59:35 -0700312private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800313
314 // Delay-load the Camera HAL module
315 virtual void onFirstRef();
316
Igor Murashkine6800ce2013-03-04 17:25:57 -0800317 // Step 1. Check if we can connect, before we acquire the service lock.
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700318 status_t validateConnect(int cameraId,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800319 /*inout*/
320 int& clientUid) const;
321
322 // Step 2. Check if we can connect, after we acquire the service lock.
323 bool canConnectUnsafe(int cameraId,
324 const String16& clientPackageName,
325 const sp<IBinder>& remoteCallback,
326 /*out*/
Igor Murashkine7ee7632013-06-11 18:10:18 -0700327 sp<BasicClient> &client);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800328
329 // When connection is successful, initialize client and track its death
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700330 status_t connectFinishUnsafe(const sp<BasicClient>& client,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700331 const sp<IBinder>& remoteCallback);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800332
Igor Murashkin634a5152013-02-20 17:15:11 -0800333 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
334
Mathias Agopian65ab4712010-07-14 17:59:35 -0700335 Mutex mServiceLock;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700336 // either a Client or CameraDeviceClient
337 wp<BasicClient> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700338 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339 int mNumberOfCameras;
340
Igor Murashkin634a5152013-02-20 17:15:11 -0800341 typedef wp<ProClient> weak_pro_client_ptr;
342 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
343
Igor Murashkinecf17e82012-10-02 16:05:11 -0700344 // needs to be called with mServiceLock held
Igor Murashkine7ee7632013-06-11 18:10:18 -0700345 sp<BasicClient> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkin634a5152013-02-20 17:15:11 -0800346 sp<ProClient> findProClientUnsafe(
347 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700348
Mathias Agopian65ab4712010-07-14 17:59:35 -0700349 // atomics to record whether the hardware is allocated to some client.
350 volatile int32_t mBusy[MAX_CAMERAS];
351 void setCameraBusy(int cameraId);
352 void setCameraFree(int cameraId);
353
354 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800355 MediaPlayer* newMediaPlayer(const char *file);
356
Mathias Agopian65ab4712010-07-14 17:59:35 -0700357 Mutex mSoundLock;
358 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
359 int mSoundRef; // reference count (release all MediaPlayer when 0)
360
Iliyan Malchev8951a972011-04-14 16:55:59 -0700361 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700362
Igor Murashkinbfc99152013-02-27 12:55:20 -0800363 Vector<sp<ICameraServiceListener> >
364 mListenerList;
365
366 // guard only mStatusList and the broadcasting of ICameraServiceListener
Igor Murashkincba2c162013-03-20 15:56:31 -0700367 mutable Mutex mStatusMutex;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800368 ICameraServiceListener::Status
369 mStatusList[MAX_CAMERAS];
370
Igor Murashkincba2c162013-03-20 15:56:31 -0700371 // Read the current status (locks mStatusMutex)
372 ICameraServiceListener::Status
373 getStatus(int cameraId) const;
374
Igor Murashkin93747b92013-05-01 15:42:20 -0700375 typedef Vector<ICameraServiceListener::Status> StatusVector;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800376 // Broadcast the new status if it changed (locks the service mutex)
377 void updateStatus(
378 ICameraServiceListener::Status status,
Igor Murashkin93747b92013-05-01 15:42:20 -0700379 int32_t cameraId,
380 const StatusVector *rejectSourceStates = NULL);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800381
Igor Murashkinecf17e82012-10-02 16:05:11 -0700382 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800383 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800384
385 // Helpers
Igor Murashkinbfc99152013-02-27 12:55:20 -0800386
387 bool isValidCameraId(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700388};
389
390} // namespace android
391
392#endif