blob: bb3fb2534b8a397aba693205bcf4c86f6c07c07e [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>
32
Igor Murashkinbfc99152013-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 Murashkinecf17e82012-10-02 16:05:11 -070047 public BnCameraService,
Igor Murashkincba2c162013-03-20 15:56:31 -070048 public IBinder::DeathRecipient,
49 public camera_module_callbacks_t
Mathias Agopian65ab4712010-07-14 17:59:35 -070050{
Mathias Agopian5462fc92010-07-14 18:41:18 -070051 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070052public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070053 class Client;
Igor Murashkin634a5152013-02-20 17:15:11 -080054 class BasicClient;
55
56 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070057 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070058
59 CameraService();
60 virtual ~CameraService();
61
Igor Murashkin634a5152013-02-20 17:15:11 -080062 /////////////////////////////////////////////////////////////////////
Igor Murashkincba2c162013-03-20 15:56:31 -070063 // HAL Callbacks
64 virtual void onDeviceStatusChanged(int cameraId,
65 int newStatus);
66
67 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -080068 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070069 virtual int32_t getNumberOfCameras();
70 virtual status_t getCameraInfo(int cameraId,
71 struct CameraInfo* cameraInfo);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080072
73 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId,
74 const String16& clientPackageName, int clientUid);
75 virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinc073ba52013-02-26 14:32:34 -080076 int cameraId, const String16& clientPackageName, int clientUid);
Mathias Agopian65ab4712010-07-14 17:59:35 -070077
Igor Murashkinbfc99152013-02-27 12:55:20 -080078 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
79 virtual status_t removeListener(
80 const sp<ICameraServiceListener>& listener);
81
Igor Murashkin634a5152013-02-20 17:15:11 -080082 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -070083 virtual status_t onTransact(uint32_t code, const Parcel& data,
84 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -080085
86 virtual status_t dump(int fd, const Vector<String16>& args);
87
88 /////////////////////////////////////////////////////////////////////
89 // Client functionality
90 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -070091
92 enum sound_kind {
93 SOUND_SHUTTER = 0,
94 SOUND_RECORDING = 1,
95 NUM_SOUNDS
96 };
97
98 void loadSound();
99 void playSound(sound_kind kind);
100 void releaseSound();
101
Igor Murashkin634a5152013-02-20 17:15:11 -0800102
103 /////////////////////////////////////////////////////////////////////
104 // CameraClient functionality
105
106 // returns plain pointer of client. Note that mClientLock should be acquired to
107 // prevent the client from destruction. The result can be NULL.
108 virtual Client* getClientByIdUnsafe(int cameraId);
109 virtual Mutex* getClientLockById(int cameraId);
110
111 class BasicClient : public virtual RefBase {
112 public:
113 virtual status_t initialize(camera_module_t *module) = 0;
114
115 virtual void disconnect() = 0;
116
Igor Murashkine6800ce2013-03-04 17:25:57 -0800117 // Return the remote callback binder object (e.g. IProCameraCallbacks)
Igor Murashkin634a5152013-02-20 17:15:11 -0800118 wp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800119 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800120 }
121
122 protected:
123 BasicClient(const sp<CameraService>& cameraService,
124 const sp<IBinder>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800125 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800126 int cameraId,
127 int cameraFacing,
128 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800129 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800130 int servicePid);
131
132 virtual ~BasicClient();
133
134 // the instance is in the middle of destruction. When this is set,
135 // the instance should not be accessed from callback.
136 // CameraService's mClientLock should be acquired to access this.
137 // - subclasses should set this to true in their destructors.
138 bool mDestructionStarted;
139
140 // these are initialized in the constructor.
141 sp<CameraService> mCameraService; // immutable after constructor
142 int mCameraId; // immutable after constructor
143 int mCameraFacing; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800144 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800145 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800146 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800147 pid_t mServicePid; // immutable after constructor
148
149 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800150 wp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800151
152 // permissions management
153 status_t startCameraOps();
154 status_t finishCameraOps();
155
156 // Notify client about a fatal error
157 virtual void notifyError() = 0;
158 private:
159 AppOpsManager mAppOpsManager;
160
161 class OpsCallback : public BnAppOpsCallback {
162 public:
163 OpsCallback(wp<BasicClient> client);
164 virtual void opChanged(int32_t op, const String16& packageName);
165
166 private:
167 wp<BasicClient> mClient;
168
169 }; // class OpsCallback
170
171 sp<OpsCallback> mOpsCallback;
172 // Track whether startCameraOps was called successfully, to avoid
173 // finishing what we didn't start.
174 bool mOpsActive;
175
176 // IAppOpsCallback interface, indirected through opListener
177 virtual void opChanged(int32_t op, const String16& packageName);
178 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800179
180 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700181 {
182 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800183 typedef ICameraClient TCamCallbacks;
184
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700185 // ICamera interface (see ICamera for details)
186 virtual void disconnect();
187 virtual status_t connect(const sp<ICameraClient>& client) = 0;
188 virtual status_t lock() = 0;
189 virtual status_t unlock() = 0;
190 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden8ba01022012-12-18 09:46:54 -0800191 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700192 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700193 virtual status_t setPreviewCallbackTarget(
194 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700195 virtual status_t startPreview() = 0;
196 virtual void stopPreview() = 0;
197 virtual bool previewEnabled() = 0;
198 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
199 virtual status_t startRecording() = 0;
200 virtual void stopRecording() = 0;
201 virtual bool recordingEnabled() = 0;
202 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
203 virtual status_t autoFocus() = 0;
204 virtual status_t cancelAutoFocus() = 0;
205 virtual status_t takePicture(int msgType) = 0;
206 virtual status_t setParameters(const String8& params) = 0;
207 virtual String8 getParameters() const = 0;
208 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
209
210 // Interface used by CameraService
211 Client(const sp<CameraService>& cameraService,
212 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800213 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700214 int cameraId,
215 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700216 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800217 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700218 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700219 ~Client();
220
221 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800222 const sp<ICameraClient>& getRemoteCallback() {
223 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700224 }
225
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700226 protected:
227 static Mutex* getClientLockFromCookie(void* user);
228 // convert client from cookie. Client lock should be acquired before getting Client.
229 static Client* getClientFromCookie(void* user);
230
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800231 virtual void notifyError();
232
Igor Murashkin634a5152013-02-20 17:15:11 -0800233 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700234
Igor Murashkin634a5152013-02-20 17:15:11 -0800235 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800236 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800237
238 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800239
240 class ProClient : public BnProCameraUser, public BasicClient {
241 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800242 typedef IProCameraCallbacks TCamCallbacks;
243
Igor Murashkin634a5152013-02-20 17:15:11 -0800244 ProClient(const sp<CameraService>& cameraService,
245 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800246 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800247 int cameraId,
248 int cameraFacing,
249 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800250 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800251 int servicePid);
252
253 virtual ~ProClient();
254
255 const sp<IProCameraCallbacks>& getRemoteCallback() {
256 return mRemoteCallback;
257 }
258
Igor Murashkin634a5152013-02-20 17:15:11 -0800259 /***
260 IProCamera implementation
261 ***/
Igor Murashkine6800ce2013-03-04 17:25:57 -0800262 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
263 = 0;
264 virtual status_t exclusiveTryLock() = 0;
265 virtual status_t exclusiveLock() = 0;
266 virtual status_t exclusiveUnlock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800267
Igor Murashkine6800ce2013-03-04 17:25:57 -0800268 virtual bool hasExclusiveLock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800269
270 // Note that the callee gets a copy of the metadata.
271 virtual int submitRequest(camera_metadata_t* metadata,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800272 bool streaming = false) = 0;
273 virtual status_t cancelRequest(int requestId) = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800274
Igor Murashkinbfc99152013-02-27 12:55:20 -0800275 // Callbacks from camera service
Igor Murashkine6800ce2013-03-04 17:25:57 -0800276 virtual void onExclusiveLockStolen() = 0;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800277
Igor Murashkin634a5152013-02-20 17:15:11 -0800278 protected:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800279 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700280
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800281 sp<IProCameraCallbacks> mRemoteCallback;
282 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700283
Mathias Agopian65ab4712010-07-14 17:59:35 -0700284private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800285
286 // Delay-load the Camera HAL module
287 virtual void onFirstRef();
288
Igor Murashkine6800ce2013-03-04 17:25:57 -0800289 // Step 1. Check if we can connect, before we acquire the service lock.
290 bool validateConnect(int cameraId,
291 /*inout*/
292 int& clientUid) const;
293
294 // Step 2. Check if we can connect, after we acquire the service lock.
295 bool canConnectUnsafe(int cameraId,
296 const String16& clientPackageName,
297 const sp<IBinder>& remoteCallback,
298 /*out*/
299 sp<Client> &client);
300
301 // When connection is successful, initialize client and track its death
302 bool connectFinishUnsafe(const sp<BasicClient>& client,
303 const sp<IBinder>& clientBinder);
304
Igor Murashkin634a5152013-02-20 17:15:11 -0800305 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
306
Mathias Agopian65ab4712010-07-14 17:59:35 -0700307 Mutex mServiceLock;
308 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700309 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700310 int mNumberOfCameras;
311
Igor Murashkin634a5152013-02-20 17:15:11 -0800312 typedef wp<ProClient> weak_pro_client_ptr;
313 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
314
Igor Murashkinecf17e82012-10-02 16:05:11 -0700315 // needs to be called with mServiceLock held
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700316 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkin634a5152013-02-20 17:15:11 -0800317 sp<ProClient> findProClientUnsafe(
318 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700319
Mathias Agopian65ab4712010-07-14 17:59:35 -0700320 // atomics to record whether the hardware is allocated to some client.
321 volatile int32_t mBusy[MAX_CAMERAS];
322 void setCameraBusy(int cameraId);
323 void setCameraFree(int cameraId);
324
325 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800326 MediaPlayer* newMediaPlayer(const char *file);
327
Mathias Agopian65ab4712010-07-14 17:59:35 -0700328 Mutex mSoundLock;
329 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
330 int mSoundRef; // reference count (release all MediaPlayer when 0)
331
Iliyan Malchev8951a972011-04-14 16:55:59 -0700332 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700333
Igor Murashkinbfc99152013-02-27 12:55:20 -0800334 Vector<sp<ICameraServiceListener> >
335 mListenerList;
336
337 // guard only mStatusList and the broadcasting of ICameraServiceListener
Igor Murashkincba2c162013-03-20 15:56:31 -0700338 mutable Mutex mStatusMutex;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800339 ICameraServiceListener::Status
340 mStatusList[MAX_CAMERAS];
341
Igor Murashkincba2c162013-03-20 15:56:31 -0700342 // Read the current status (locks mStatusMutex)
343 ICameraServiceListener::Status
344 getStatus(int cameraId) const;
345
Igor Murashkinbfc99152013-02-27 12:55:20 -0800346 // Broadcast the new status if it changed (locks the service mutex)
347 void updateStatus(
348 ICameraServiceListener::Status status,
349 int32_t cameraId);
350 // Call this one when the service mutex is already held (idempotent)
351 void updateStatusUnsafe(
352 ICameraServiceListener::Status status,
353 int32_t cameraId);
354
Igor Murashkinecf17e82012-10-02 16:05:11 -0700355 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800356 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800357
358 // Helpers
359 int getDeviceVersion(int cameraId, int* facing);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800360
361 bool isValidCameraId(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700362};
363
364} // namespace android
365
366#endif