blob: d7a336cab188a1eb06c9bd954c29b210c028f747 [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,
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 Murashkin634a5152013-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 Murashkin634a5152013-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 Talvalaceb388d2013-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 Murashkinc073ba52013-02-26 14:32:34 -080070 int cameraId, const String16& clientPackageName, int clientUid);
Mathias Agopian65ab4712010-07-14 17:59:35 -070071
Igor Murashkinbfc99152013-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 Murashkin634a5152013-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 Murashkin634a5152013-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 Murashkin634a5152013-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
111 wp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800112 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800113 }
114
115 protected:
116 BasicClient(const sp<CameraService>& cameraService,
117 const sp<IBinder>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800118 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800119 int cameraId,
120 int cameraFacing,
121 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800122 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800123 int servicePid);
124
125 virtual ~BasicClient();
126
127 // the instance is in the middle of destruction. When this is set,
128 // the instance should not be accessed from callback.
129 // CameraService's mClientLock should be acquired to access this.
130 // - subclasses should set this to true in their destructors.
131 bool mDestructionStarted;
132
133 // these are initialized in the constructor.
134 sp<CameraService> mCameraService; // immutable after constructor
135 int mCameraId; // immutable after constructor
136 int mCameraFacing; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800137 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800138 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800139 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800140 pid_t mServicePid; // immutable after constructor
141
142 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800143 wp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800144
145 // permissions management
146 status_t startCameraOps();
147 status_t finishCameraOps();
148
149 // Notify client about a fatal error
150 virtual void notifyError() = 0;
151 private:
152 AppOpsManager mAppOpsManager;
153
154 class OpsCallback : public BnAppOpsCallback {
155 public:
156 OpsCallback(wp<BasicClient> client);
157 virtual void opChanged(int32_t op, const String16& packageName);
158
159 private:
160 wp<BasicClient> mClient;
161
162 }; // class OpsCallback
163
164 sp<OpsCallback> mOpsCallback;
165 // Track whether startCameraOps was called successfully, to avoid
166 // finishing what we didn't start.
167 bool mOpsActive;
168
169 // IAppOpsCallback interface, indirected through opListener
170 virtual void opChanged(int32_t op, const String16& packageName);
171 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800172
173 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700174 {
175 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800176 typedef ICameraClient TCamCallbacks;
177
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700178 // ICamera interface (see ICamera for details)
179 virtual void disconnect();
180 virtual status_t connect(const sp<ICameraClient>& client) = 0;
181 virtual status_t lock() = 0;
182 virtual status_t unlock() = 0;
183 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden8ba01022012-12-18 09:46:54 -0800184 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700185 virtual void setPreviewCallbackFlag(int flag) = 0;
186 virtual status_t startPreview() = 0;
187 virtual void stopPreview() = 0;
188 virtual bool previewEnabled() = 0;
189 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
190 virtual status_t startRecording() = 0;
191 virtual void stopRecording() = 0;
192 virtual bool recordingEnabled() = 0;
193 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
194 virtual status_t autoFocus() = 0;
195 virtual status_t cancelAutoFocus() = 0;
196 virtual status_t takePicture(int msgType) = 0;
197 virtual status_t setParameters(const String8& params) = 0;
198 virtual String8 getParameters() const = 0;
199 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
200
201 // Interface used by CameraService
202 Client(const sp<CameraService>& cameraService,
203 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800204 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700205 int cameraId,
206 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700207 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800208 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700209 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700210 ~Client();
211
212 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800213 const sp<ICameraClient>& getRemoteCallback() {
214 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700215 }
216
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700217 protected:
218 static Mutex* getClientLockFromCookie(void* user);
219 // convert client from cookie. Client lock should be acquired before getting Client.
220 static Client* getClientFromCookie(void* user);
221
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800222 virtual void notifyError();
223
Igor Murashkin634a5152013-02-20 17:15:11 -0800224 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700225
Igor Murashkin634a5152013-02-20 17:15:11 -0800226 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800227 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800228
229 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800230
231 class ProClient : public BnProCameraUser, public BasicClient {
232 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800233 typedef IProCameraCallbacks TCamCallbacks;
234
Igor Murashkin634a5152013-02-20 17:15:11 -0800235 ProClient(const sp<CameraService>& cameraService,
236 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800237 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800238 int cameraId,
239 int cameraFacing,
240 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800241 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800242 int servicePid);
243
244 virtual ~ProClient();
245
246 const sp<IProCameraCallbacks>& getRemoteCallback() {
247 return mRemoteCallback;
248 }
249
250 // BasicClient implementation
251 virtual status_t initialize(camera_module_t *module);
252
253 /***
254 IProCamera implementation
255 ***/
256
257
258 virtual status_t connect(
259 const sp<IProCameraCallbacks>& callbacks);
260 virtual void disconnect();
261
262 virtual status_t exclusiveTryLock();
263 virtual status_t exclusiveLock();
264 virtual status_t exclusiveUnlock();
265
266 virtual bool hasExclusiveLock();
267
268 // Note that the callee gets a copy of the metadata.
269 virtual int submitRequest(camera_metadata_t* metadata,
270 bool streaming = false);
271 virtual status_t cancelRequest(int requestId);
272
273 virtual status_t requestStream(int streamId);
274 virtual status_t cancelStream(int streamId);
275
Igor Murashkinbfc99152013-02-27 12:55:20 -0800276 // Callbacks from camera service
277 virtual void onExclusiveLockStolen();
278
Igor Murashkin634a5152013-02-20 17:15:11 -0800279 protected:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800280 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700281
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800282 sp<IProCameraCallbacks> mRemoteCallback;
283 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700284
Mathias Agopian65ab4712010-07-14 17:59:35 -0700285private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800286
287 // Delay-load the Camera HAL module
288 virtual void onFirstRef();
289
290 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
291
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292 Mutex mServiceLock;
293 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700294 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 int mNumberOfCameras;
296
Igor Murashkin634a5152013-02-20 17:15:11 -0800297 typedef wp<ProClient> weak_pro_client_ptr;
298 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
299
Igor Murashkinecf17e82012-10-02 16:05:11 -0700300 // needs to be called with mServiceLock held
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700301 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkin634a5152013-02-20 17:15:11 -0800302 sp<ProClient> findProClientUnsafe(
303 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700304
Mathias Agopian65ab4712010-07-14 17:59:35 -0700305 // atomics to record whether the hardware is allocated to some client.
306 volatile int32_t mBusy[MAX_CAMERAS];
307 void setCameraBusy(int cameraId);
308 void setCameraFree(int cameraId);
309
310 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800311 MediaPlayer* newMediaPlayer(const char *file);
312
Mathias Agopian65ab4712010-07-14 17:59:35 -0700313 Mutex mSoundLock;
314 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
315 int mSoundRef; // reference count (release all MediaPlayer when 0)
316
Iliyan Malchev8951a972011-04-14 16:55:59 -0700317 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700318
Igor Murashkinbfc99152013-02-27 12:55:20 -0800319 Vector<sp<ICameraServiceListener> >
320 mListenerList;
321
322 // guard only mStatusList and the broadcasting of ICameraServiceListener
323 Mutex mStatusMutex;
324 ICameraServiceListener::Status
325 mStatusList[MAX_CAMERAS];
326
327 // Broadcast the new status if it changed (locks the service mutex)
328 void updateStatus(
329 ICameraServiceListener::Status status,
330 int32_t cameraId);
331 // Call this one when the service mutex is already held (idempotent)
332 void updateStatusUnsafe(
333 ICameraServiceListener::Status status,
334 int32_t cameraId);
335
Igor Murashkinecf17e82012-10-02 16:05:11 -0700336 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800337 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800338
339 // Helpers
340 int getDeviceVersion(int cameraId, int* facing);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800341
342 bool isValidCameraId(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700343};
344
345} // namespace android
346
347#endif