blob: b0175055915ee2532c36474213d2585be5890f13 [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
28/* This needs to be increased if we can have more cameras */
29#define MAX_CAMERAS 2
30
31namespace android {
32
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070033extern volatile int32_t gLogLevel;
34
Mathias Agopian65ab4712010-07-14 17:59:35 -070035class MemoryHeapBase;
36class MediaPlayer;
37
Mathias Agopian5462fc92010-07-14 18:41:18 -070038class CameraService :
39 public BinderService<CameraService>,
Igor Murashkin8dcdb952012-10-02 16:05:11 -070040 public BnCameraService,
41 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070042{
Mathias Agopian5462fc92010-07-14 18:41:18 -070043 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070044public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070045 class Client;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080046 class BasicClient;
47
48 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070049 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51 CameraService();
52 virtual ~CameraService();
53
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080054 /////////////////////////////////////////////////////////////////////
55 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070056 virtual int32_t getNumberOfCameras();
57 virtual status_t getCameraInfo(int cameraId,
58 struct CameraInfo* cameraInfo);
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -080059
60 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId,
61 const String16& clientPackageName, int clientUid);
62 virtual sp<IProCameraUser> connect(const sp<IProCameraCallbacks>& cameraCb,
63 int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070064
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080065 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -070066 virtual status_t onTransact(uint32_t code, const Parcel& data,
67 Parcel* reply, uint32_t flags);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080068
69 virtual status_t dump(int fd, const Vector<String16>& args);
70
71 /////////////////////////////////////////////////////////////////////
72 // Client functionality
73 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -070074
75 enum sound_kind {
76 SOUND_SHUTTER = 0,
77 SOUND_RECORDING = 1,
78 NUM_SOUNDS
79 };
80
81 void loadSound();
82 void playSound(sound_kind kind);
83 void releaseSound();
84
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080085
86 /////////////////////////////////////////////////////////////////////
87 // CameraClient functionality
88
89 // returns plain pointer of client. Note that mClientLock should be acquired to
90 // prevent the client from destruction. The result can be NULL.
91 virtual Client* getClientByIdUnsafe(int cameraId);
92 virtual Mutex* getClientLockById(int cameraId);
93
94 class BasicClient : public virtual RefBase {
95 public:
96 virtual status_t initialize(camera_module_t *module) = 0;
97
98 virtual void disconnect() = 0;
99
100 wp<IBinder> getRemote() {
101 return mRemoteCallback;
102 }
103
104 protected:
105 BasicClient(const sp<CameraService>& cameraService,
106 const sp<IBinder>& remoteCallback,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800107 const String16& clientPackageName,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800108 int cameraId,
109 int cameraFacing,
110 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800111 uid_t clientUid,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800112 int servicePid);
113
114 virtual ~BasicClient();
115
116 // the instance is in the middle of destruction. When this is set,
117 // the instance should not be accessed from callback.
118 // CameraService's mClientLock should be acquired to access this.
119 // - subclasses should set this to true in their destructors.
120 bool mDestructionStarted;
121
122 // these are initialized in the constructor.
123 sp<CameraService> mCameraService; // immutable after constructor
124 int mCameraId; // immutable after constructor
125 int mCameraFacing; // immutable after constructor
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800126 const String16 mClientPackageName;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800127 pid_t mClientPid;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800128 uid_t mClientUid; // immutable after constructor
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800129 pid_t mServicePid; // immutable after constructor
130
131 // - The app-side Binder interface to receive callbacks from us
132 wp<IBinder> mRemoteCallback; // immutable after constructor
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800133
134 // permissions management
135 status_t startCameraOps();
136 status_t finishCameraOps();
137
138 // Notify client about a fatal error
139 virtual void notifyError() = 0;
140 private:
141 AppOpsManager mAppOpsManager;
142
143 class OpsCallback : public BnAppOpsCallback {
144 public:
145 OpsCallback(wp<BasicClient> client);
146 virtual void opChanged(int32_t op, const String16& packageName);
147
148 private:
149 wp<BasicClient> mClient;
150
151 }; // class OpsCallback
152
153 sp<OpsCallback> mOpsCallback;
154 // Track whether startCameraOps was called successfully, to avoid
155 // finishing what we didn't start.
156 bool mOpsActive;
157
158 // IAppOpsCallback interface, indirected through opListener
159 virtual void opChanged(int32_t op, const String16& packageName);
160 }; // class BasicClient
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800161
162 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700163 {
164 public:
165 // ICamera interface (see ICamera for details)
166 virtual void disconnect();
167 virtual status_t connect(const sp<ICameraClient>& client) = 0;
168 virtual status_t lock() = 0;
169 virtual status_t unlock() = 0;
170 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden484566c2012-12-18 09:46:54 -0800171 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700172 virtual void setPreviewCallbackFlag(int flag) = 0;
173 virtual status_t startPreview() = 0;
174 virtual void stopPreview() = 0;
175 virtual bool previewEnabled() = 0;
176 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
177 virtual status_t startRecording() = 0;
178 virtual void stopRecording() = 0;
179 virtual bool recordingEnabled() = 0;
180 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
181 virtual status_t autoFocus() = 0;
182 virtual status_t cancelAutoFocus() = 0;
183 virtual status_t takePicture(int msgType) = 0;
184 virtual status_t setParameters(const String8& params) = 0;
185 virtual String8 getParameters() const = 0;
186 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
187
188 // Interface used by CameraService
189 Client(const sp<CameraService>& cameraService,
190 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800191 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700192 int cameraId,
193 int cameraFacing,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700194 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800195 uid_t clientUid,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700196 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700197 ~Client();
198
199 // return our camera client
200 const sp<ICameraClient>& getCameraClient() {
201 return mCameraClient;
202 }
203
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700204 protected:
205 static Mutex* getClientLockFromCookie(void* user);
206 // convert client from cookie. Client lock should be acquired before getting Client.
207 static Client* getClientFromCookie(void* user);
208
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800209 virtual void notifyError();
210
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800211 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700212
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800213 // - The app-side Binder interface to receive callbacks from us
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700214 sp<ICameraClient> mCameraClient;
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800215
216 }; // class Client
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800217
218 class ProClient : public BnProCameraUser, public BasicClient {
219 public:
220 ProClient(const sp<CameraService>& cameraService,
221 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800222 const String16& clientPackageName,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800223 int cameraId,
224 int cameraFacing,
225 int clientPid,
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800226 uid_t clientUid,
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800227 int servicePid);
228
229 virtual ~ProClient();
230
231 const sp<IProCameraCallbacks>& getRemoteCallback() {
232 return mRemoteCallback;
233 }
234
235 // BasicClient implementation
236 virtual status_t initialize(camera_module_t *module);
237
238 /***
239 IProCamera implementation
240 ***/
241
242
243 virtual status_t connect(
244 const sp<IProCameraCallbacks>& callbacks);
245 virtual void disconnect();
246
247 virtual status_t exclusiveTryLock();
248 virtual status_t exclusiveLock();
249 virtual status_t exclusiveUnlock();
250
251 virtual bool hasExclusiveLock();
252
253 // Note that the callee gets a copy of the metadata.
254 virtual int submitRequest(camera_metadata_t* metadata,
255 bool streaming = false);
256 virtual status_t cancelRequest(int requestId);
257
258 virtual status_t requestStream(int streamId);
259 virtual status_t cancelStream(int streamId);
260
261 protected:
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800262 virtual void notifyError();
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700263
Eino-Ville Talvala48af7e82013-02-19 10:40:14 -0800264 sp<IProCameraCallbacks> mRemoteCallback;
265 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700266
Mathias Agopian65ab4712010-07-14 17:59:35 -0700267private:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800268
269 // Delay-load the Camera HAL module
270 virtual void onFirstRef();
271
272 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
273
Mathias Agopian65ab4712010-07-14 17:59:35 -0700274 Mutex mServiceLock;
275 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700276 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700277 int mNumberOfCameras;
278
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800279 typedef wp<ProClient> weak_pro_client_ptr;
280 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
281
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700282 // needs to be called with mServiceLock held
Igor Murashkin507994d2012-10-05 10:44:57 -0700283 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800284 sp<ProClient> findProClientUnsafe(
285 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700286
Mathias Agopian65ab4712010-07-14 17:59:35 -0700287 // atomics to record whether the hardware is allocated to some client.
288 volatile int32_t mBusy[MAX_CAMERAS];
289 void setCameraBusy(int cameraId);
290 void setCameraFree(int cameraId);
291
292 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800293 MediaPlayer* newMediaPlayer(const char *file);
294
Mathias Agopian65ab4712010-07-14 17:59:35 -0700295 Mutex mSoundLock;
296 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
297 int mSoundRef; // reference count (release all MediaPlayer when 0)
298
Iliyan Malchev8951a972011-04-14 16:55:59 -0700299 camera_module_t *mModule;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700300
301 // IBinder::DeathRecipient implementation
302 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800303
304 // Helpers
305 int getDeviceVersion(int cameraId, int* facing);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700306};
307
308} // namespace android
309
310#endif