blob: 8acc63f35a9b018af2e2b4b3de07e099cb4c4551 [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() {
112 return mRemoteCallback;
113 }
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
143 wp<IBinder> mRemoteCallback; // 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:
176 // ICamera interface (see ICamera for details)
177 virtual void disconnect();
178 virtual status_t connect(const sp<ICameraClient>& client) = 0;
179 virtual status_t lock() = 0;
180 virtual status_t unlock() = 0;
181 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden8ba01022012-12-18 09:46:54 -0800182 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700183 virtual void setPreviewCallbackFlag(int flag) = 0;
184 virtual status_t startPreview() = 0;
185 virtual void stopPreview() = 0;
186 virtual bool previewEnabled() = 0;
187 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
188 virtual status_t startRecording() = 0;
189 virtual void stopRecording() = 0;
190 virtual bool recordingEnabled() = 0;
191 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
192 virtual status_t autoFocus() = 0;
193 virtual status_t cancelAutoFocus() = 0;
194 virtual status_t takePicture(int msgType) = 0;
195 virtual status_t setParameters(const String8& params) = 0;
196 virtual String8 getParameters() const = 0;
197 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
198
199 // Interface used by CameraService
200 Client(const sp<CameraService>& cameraService,
201 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800202 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700203 int cameraId,
204 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700205 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800206 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700207 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700208 ~Client();
209
210 // return our camera client
211 const sp<ICameraClient>& getCameraClient() {
212 return mCameraClient;
213 }
214
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700215 protected:
216 static Mutex* getClientLockFromCookie(void* user);
217 // convert client from cookie. Client lock should be acquired before getting Client.
218 static Client* getClientFromCookie(void* user);
219
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800220 virtual void notifyError();
221
Igor Murashkin634a5152013-02-20 17:15:11 -0800222 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700223
Igor Murashkin634a5152013-02-20 17:15:11 -0800224 // - The app-side Binder interface to receive callbacks from us
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700225 sp<ICameraClient> mCameraClient;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800226
227 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800228
229 class ProClient : public BnProCameraUser, public BasicClient {
230 public:
231 ProClient(const sp<CameraService>& cameraService,
232 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800233 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800234 int cameraId,
235 int cameraFacing,
236 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800237 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800238 int servicePid);
239
240 virtual ~ProClient();
241
242 const sp<IProCameraCallbacks>& getRemoteCallback() {
243 return mRemoteCallback;
244 }
245
246 // BasicClient implementation
247 virtual status_t initialize(camera_module_t *module);
248
249 /***
250 IProCamera implementation
251 ***/
252
253
254 virtual status_t connect(
255 const sp<IProCameraCallbacks>& callbacks);
256 virtual void disconnect();
257
258 virtual status_t exclusiveTryLock();
259 virtual status_t exclusiveLock();
260 virtual status_t exclusiveUnlock();
261
262 virtual bool hasExclusiveLock();
263
264 // Note that the callee gets a copy of the metadata.
265 virtual int submitRequest(camera_metadata_t* metadata,
266 bool streaming = false);
267 virtual status_t cancelRequest(int requestId);
268
269 virtual status_t requestStream(int streamId);
270 virtual status_t cancelStream(int streamId);
271
Igor Murashkinbfc99152013-02-27 12:55:20 -0800272 // Callbacks from camera service
273 virtual void onExclusiveLockStolen();
274
Igor Murashkin634a5152013-02-20 17:15:11 -0800275 protected:
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800276 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700277
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800278 sp<IProCameraCallbacks> mRemoteCallback;
279 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700280
Mathias Agopian65ab4712010-07-14 17:59:35 -0700281private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800282
283 // Delay-load the Camera HAL module
284 virtual void onFirstRef();
285
286 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
287
Mathias Agopian65ab4712010-07-14 17:59:35 -0700288 Mutex mServiceLock;
289 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700290 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700291 int mNumberOfCameras;
292
Igor Murashkin634a5152013-02-20 17:15:11 -0800293 typedef wp<ProClient> weak_pro_client_ptr;
294 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
295
Igor Murashkinecf17e82012-10-02 16:05:11 -0700296 // needs to be called with mServiceLock held
Igor Murashkin294d0ec2012-10-05 10:44:57 -0700297 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkin634a5152013-02-20 17:15:11 -0800298 sp<ProClient> findProClientUnsafe(
299 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkinecf17e82012-10-02 16:05:11 -0700300
Mathias Agopian65ab4712010-07-14 17:59:35 -0700301 // atomics to record whether the hardware is allocated to some client.
302 volatile int32_t mBusy[MAX_CAMERAS];
303 void setCameraBusy(int cameraId);
304 void setCameraFree(int cameraId);
305
306 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800307 MediaPlayer* newMediaPlayer(const char *file);
308
Mathias Agopian65ab4712010-07-14 17:59:35 -0700309 Mutex mSoundLock;
310 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
311 int mSoundRef; // reference count (release all MediaPlayer when 0)
312
Iliyan Malchev8951a972011-04-14 16:55:59 -0700313 camera_module_t *mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700314
Igor Murashkinbfc99152013-02-27 12:55:20 -0800315 Vector<sp<ICameraServiceListener> >
316 mListenerList;
317
318 // guard only mStatusList and the broadcasting of ICameraServiceListener
319 Mutex mStatusMutex;
320 ICameraServiceListener::Status
321 mStatusList[MAX_CAMERAS];
322
323 // Broadcast the new status if it changed (locks the service mutex)
324 void updateStatus(
325 ICameraServiceListener::Status status,
326 int32_t cameraId);
327 // Call this one when the service mutex is already held (idempotent)
328 void updateStatusUnsafe(
329 ICameraServiceListener::Status status,
330 int32_t cameraId);
331
Igor Murashkinecf17e82012-10-02 16:05:11 -0700332 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800333 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800334
335 // Helpers
336 int getDeviceVersion(int cameraId, int* facing);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800337
338 bool isValidCameraId(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700339};
340
341} // namespace android
342
343#endif