blob: d93aa73db86dae47737ed1cd62839df09be7e3b1 [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 Murashkinbfb5d5e2013-02-20 17:15:11 -080021#include <utils/Vector.h>
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080022#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070023#include <binder/BinderService.h>
Eino-Ville Talvala48af7e82013-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 Murashkinb84d9352013-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
Mathias Agopian65ab4712010-07-14 17:59:35 -070033/* This needs to be increased if we can have more cameras */
34#define MAX_CAMERAS 2
35
36namespace android {
37
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070038extern volatile int32_t gLogLevel;
39
Mathias Agopian65ab4712010-07-14 17:59:35 -070040class MemoryHeapBase;
41class MediaPlayer;
42
Mathias Agopian5462fc92010-07-14 18:41:18 -070043class CameraService :
44 public BinderService<CameraService>,
Igor Murashkin8dcdb952012-10-02 16:05:11 -070045 public BnCameraService,
46 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070047{
Mathias Agopian5462fc92010-07-14 18:41:18 -070048 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070049public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070050 class Client;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080051 class BasicClient;
52
53 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070054 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
56 CameraService();
57 virtual ~CameraService();
58
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080059 /////////////////////////////////////////////////////////////////////
60 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070061 virtual int32_t getNumberOfCameras();
62 virtual status_t getCameraInfo(int cameraId,
63 struct CameraInfo* cameraInfo);
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080064
65 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId,
66 const String16& clientPackageName, int clientUid);
67 virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb,
Igor Murashkinb84d9352013-02-26 14:32:34 -080068 int cameraId, const String16& clientPackageName, int clientUid);
Mathias Agopian65ab4712010-07-14 17:59:35 -070069
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080070 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -070071 virtual status_t onTransact(uint32_t code, const Parcel& data,
72 Parcel* reply, uint32_t flags);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080073
74 virtual status_t dump(int fd, const Vector<String16>& args);
75
76 /////////////////////////////////////////////////////////////////////
77 // Client functionality
78 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -070079
80 enum sound_kind {
81 SOUND_SHUTTER = 0,
82 SOUND_RECORDING = 1,
83 NUM_SOUNDS
84 };
85
86 void loadSound();
87 void playSound(sound_kind kind);
88 void releaseSound();
89
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080090
91 /////////////////////////////////////////////////////////////////////
92 // CameraClient functionality
93
94 // returns plain pointer of client. Note that mClientLock should be acquired to
95 // prevent the client from destruction. The result can be NULL.
96 virtual Client* getClientByIdUnsafe(int cameraId);
97 virtual Mutex* getClientLockById(int cameraId);
98
99 class BasicClient : public virtual RefBase {
100 public:
101 virtual status_t initialize(camera_module_t *module) = 0;
102
103 virtual void disconnect() = 0;
104
105 wp<IBinder> getRemote() {
106 return mRemoteCallback;
107 }
108
109 protected:
110 BasicClient(const sp<CameraService>& cameraService,
111 const sp<IBinder>& remoteCallback,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800112 const String16& clientPackageName,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800113 int cameraId,
114 int cameraFacing,
115 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800116 uid_t clientUid,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800117 int servicePid);
118
119 virtual ~BasicClient();
120
121 // the instance is in the middle of destruction. When this is set,
122 // the instance should not be accessed from callback.
123 // CameraService's mClientLock should be acquired to access this.
124 // - subclasses should set this to true in their destructors.
125 bool mDestructionStarted;
126
127 // these are initialized in the constructor.
128 sp<CameraService> mCameraService; // immutable after constructor
129 int mCameraId; // immutable after constructor
130 int mCameraFacing; // immutable after constructor
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800131 const String16 mClientPackageName;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800132 pid_t mClientPid;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800133 uid_t mClientUid; // immutable after constructor
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800134 pid_t mServicePid; // immutable after constructor
135
136 // - The app-side Binder interface to receive callbacks from us
137 wp<IBinder> mRemoteCallback; // immutable after constructor
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800138
139 // permissions management
140 status_t startCameraOps();
141 status_t finishCameraOps();
142
143 // Notify client about a fatal error
144 virtual void notifyError() = 0;
145 private:
146 AppOpsManager mAppOpsManager;
147
148 class OpsCallback : public BnAppOpsCallback {
149 public:
150 OpsCallback(wp<BasicClient> client);
151 virtual void opChanged(int32_t op, const String16& packageName);
152
153 private:
154 wp<BasicClient> mClient;
155
156 }; // class OpsCallback
157
158 sp<OpsCallback> mOpsCallback;
159 // Track whether startCameraOps was called successfully, to avoid
160 // finishing what we didn't start.
161 bool mOpsActive;
162
163 // IAppOpsCallback interface, indirected through opListener
164 virtual void opChanged(int32_t op, const String16& packageName);
165 }; // class BasicClient
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800166
167 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700168 {
169 public:
170 // ICamera interface (see ICamera for details)
171 virtual void disconnect();
172 virtual status_t connect(const sp<ICameraClient>& client) = 0;
173 virtual status_t lock() = 0;
174 virtual status_t unlock() = 0;
175 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden484566c2012-12-18 09:46:54 -0800176 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700177 virtual void setPreviewCallbackFlag(int flag) = 0;
178 virtual status_t startPreview() = 0;
179 virtual void stopPreview() = 0;
180 virtual bool previewEnabled() = 0;
181 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
182 virtual status_t startRecording() = 0;
183 virtual void stopRecording() = 0;
184 virtual bool recordingEnabled() = 0;
185 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
186 virtual status_t autoFocus() = 0;
187 virtual status_t cancelAutoFocus() = 0;
188 virtual status_t takePicture(int msgType) = 0;
189 virtual status_t setParameters(const String8& params) = 0;
190 virtual String8 getParameters() const = 0;
191 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
192
193 // Interface used by CameraService
194 Client(const sp<CameraService>& cameraService,
195 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800196 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700197 int cameraId,
198 int cameraFacing,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700199 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800200 uid_t clientUid,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700201 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700202 ~Client();
203
204 // return our camera client
205 const sp<ICameraClient>& getCameraClient() {
206 return mCameraClient;
207 }
208
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700209 protected:
210 static Mutex* getClientLockFromCookie(void* user);
211 // convert client from cookie. Client lock should be acquired before getting Client.
212 static Client* getClientFromCookie(void* user);
213
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800214 virtual void notifyError();
215
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800216 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700217
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800218 // - The app-side Binder interface to receive callbacks from us
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700219 sp<ICameraClient> mCameraClient;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800220
221 }; // class Client
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800222
223 class ProClient : public BnProCameraUser, public BasicClient {
224 public:
225 ProClient(const sp<CameraService>& cameraService,
226 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800227 const String16& clientPackageName,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800228 int cameraId,
229 int cameraFacing,
230 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800231 uid_t clientUid,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800232 int servicePid);
233
234 virtual ~ProClient();
235
236 const sp<IProCameraCallbacks>& getRemoteCallback() {
237 return mRemoteCallback;
238 }
239
240 // BasicClient implementation
241 virtual status_t initialize(camera_module_t *module);
242
243 /***
244 IProCamera implementation
245 ***/
246
247
248 virtual status_t connect(
249 const sp<IProCameraCallbacks>& callbacks);
250 virtual void disconnect();
251
252 virtual status_t exclusiveTryLock();
253 virtual status_t exclusiveLock();
254 virtual status_t exclusiveUnlock();
255
256 virtual bool hasExclusiveLock();
257
258 // Note that the callee gets a copy of the metadata.
259 virtual int submitRequest(camera_metadata_t* metadata,
260 bool streaming = false);
261 virtual status_t cancelRequest(int requestId);
262
263 virtual status_t requestStream(int streamId);
264 virtual status_t cancelStream(int streamId);
265
266 protected:
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800267 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700268
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800269 sp<IProCameraCallbacks> mRemoteCallback;
270 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700271
Mathias Agopian65ab4712010-07-14 17:59:35 -0700272private:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800273
274 // Delay-load the Camera HAL module
275 virtual void onFirstRef();
276
277 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
278
Mathias Agopian65ab4712010-07-14 17:59:35 -0700279 Mutex mServiceLock;
280 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700281 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700282 int mNumberOfCameras;
283
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800284 typedef wp<ProClient> weak_pro_client_ptr;
285 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
286
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700287 // needs to be called with mServiceLock held
Igor Murashkin507994d2012-10-05 10:44:57 -0700288 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800289 sp<ProClient> findProClientUnsafe(
290 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700291
Mathias Agopian65ab4712010-07-14 17:59:35 -0700292 // atomics to record whether the hardware is allocated to some client.
293 volatile int32_t mBusy[MAX_CAMERAS];
294 void setCameraBusy(int cameraId);
295 void setCameraFree(int cameraId);
296
297 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800298 MediaPlayer* newMediaPlayer(const char *file);
299
Mathias Agopian65ab4712010-07-14 17:59:35 -0700300 Mutex mSoundLock;
301 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
302 int mSoundRef; // reference count (release all MediaPlayer when 0)
303
Iliyan Malchev8951a972011-04-14 16:55:59 -0700304 camera_module_t *mModule;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700305
306 // IBinder::DeathRecipient implementation
307 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800308
309 // Helpers
310 int getDeviceVersion(int cameraId, int* facing);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700311};
312
313} // namespace android
314
315#endif