blob: 9e0f62a53f4af57534cec9c4a981263b2d92533a [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>
Mathias Agopian5462fc92010-07-14 18:41:18 -070022#include <binder/BinderService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070023#include <camera/ICameraService.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070024#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025
26/* This needs to be increased if we can have more cameras */
27#define MAX_CAMERAS 2
28
29namespace android {
30
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070031extern volatile int32_t gLogLevel;
32
Mathias Agopian65ab4712010-07-14 17:59:35 -070033class MemoryHeapBase;
34class MediaPlayer;
35
Mathias Agopian5462fc92010-07-14 18:41:18 -070036class CameraService :
37 public BinderService<CameraService>,
Igor Murashkin8dcdb952012-10-02 16:05:11 -070038 public BnCameraService,
39 public IBinder::DeathRecipient
Mathias Agopian65ab4712010-07-14 17:59:35 -070040{
Mathias Agopian5462fc92010-07-14 18:41:18 -070041 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070042public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070043 class Client;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080044 class BasicClient;
45
46 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070047 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070048
49 CameraService();
50 virtual ~CameraService();
51
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080052 /////////////////////////////////////////////////////////////////////
53 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070054 virtual int32_t getNumberOfCameras();
55 virtual status_t getCameraInfo(int cameraId,
56 struct CameraInfo* cameraInfo);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +080057 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080058 virtual sp<IProCameraUser>
59 connect(const sp<IProCameraCallbacks>& cameraCb, int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080061 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -070062 virtual status_t onTransact(uint32_t code, const Parcel& data,
63 Parcel* reply, uint32_t flags);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080064
65 virtual status_t dump(int fd, const Vector<String16>& args);
66
67 /////////////////////////////////////////////////////////////////////
68 // Client functionality
69 virtual void removeClientByRemote(const wp<IBinder>& remoteBinder);
Mathias Agopian65ab4712010-07-14 17:59:35 -070070
71 enum sound_kind {
72 SOUND_SHUTTER = 0,
73 SOUND_RECORDING = 1,
74 NUM_SOUNDS
75 };
76
77 void loadSound();
78 void playSound(sound_kind kind);
79 void releaseSound();
80
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -080081
82 /////////////////////////////////////////////////////////////////////
83 // CameraClient functionality
84
85 // returns plain pointer of client. Note that mClientLock should be acquired to
86 // prevent the client from destruction. The result can be NULL.
87 virtual Client* getClientByIdUnsafe(int cameraId);
88 virtual Mutex* getClientLockById(int cameraId);
89
90 class BasicClient : public virtual RefBase {
91 public:
92 virtual status_t initialize(camera_module_t *module) = 0;
93
94 virtual void disconnect() = 0;
95
96 wp<IBinder> getRemote() {
97 return mRemoteCallback;
98 }
99
100 protected:
101 BasicClient(const sp<CameraService>& cameraService,
102 const sp<IBinder>& remoteCallback,
103 int cameraId,
104 int cameraFacing,
105 int clientPid,
106 int servicePid);
107
108 virtual ~BasicClient();
109
110 // the instance is in the middle of destruction. When this is set,
111 // the instance should not be accessed from callback.
112 // CameraService's mClientLock should be acquired to access this.
113 // - subclasses should set this to true in their destructors.
114 bool mDestructionStarted;
115
116 // these are initialized in the constructor.
117 sp<CameraService> mCameraService; // immutable after constructor
118 int mCameraId; // immutable after constructor
119 int mCameraFacing; // immutable after constructor
120 pid_t mClientPid;
121 pid_t mServicePid; // immutable after constructor
122
123 // - The app-side Binder interface to receive callbacks from us
124 wp<IBinder> mRemoteCallback; // immutable after constructor
125 };
126
127 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700128 {
129 public:
130 // ICamera interface (see ICamera for details)
131 virtual void disconnect();
132 virtual status_t connect(const sp<ICameraClient>& client) = 0;
133 virtual status_t lock() = 0;
134 virtual status_t unlock() = 0;
135 virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0;
Andy McFadden484566c2012-12-18 09:46:54 -0800136 virtual status_t setPreviewTexture(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700137 virtual void setPreviewCallbackFlag(int flag) = 0;
138 virtual status_t startPreview() = 0;
139 virtual void stopPreview() = 0;
140 virtual bool previewEnabled() = 0;
141 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
142 virtual status_t startRecording() = 0;
143 virtual void stopRecording() = 0;
144 virtual bool recordingEnabled() = 0;
145 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
146 virtual status_t autoFocus() = 0;
147 virtual status_t cancelAutoFocus() = 0;
148 virtual status_t takePicture(int msgType) = 0;
149 virtual status_t setParameters(const String8& params) = 0;
150 virtual String8 getParameters() const = 0;
151 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
152
153 // Interface used by CameraService
154 Client(const sp<CameraService>& cameraService,
155 const sp<ICameraClient>& cameraClient,
156 int cameraId,
157 int cameraFacing,
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700158 int clientPid,
159 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700160 ~Client();
161
162 // return our camera client
163 const sp<ICameraClient>& getCameraClient() {
164 return mCameraClient;
165 }
166
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700167 protected:
168 static Mutex* getClientLockFromCookie(void* user);
169 // convert client from cookie. Client lock should be acquired before getting Client.
170 static Client* getClientFromCookie(void* user);
171
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800172 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700173
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800174 // - The app-side Binder interface to receive callbacks from us
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700175 sp<ICameraClient> mCameraClient;
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800176 };
177
178 class ProClient : public BnProCameraUser, public BasicClient {
179 public:
180 ProClient(const sp<CameraService>& cameraService,
181 const sp<IProCameraCallbacks>& remoteCallback,
182 int cameraId,
183 int cameraFacing,
184 int clientPid,
185 int servicePid);
186
187 virtual ~ProClient();
188
189 const sp<IProCameraCallbacks>& getRemoteCallback() {
190 return mRemoteCallback;
191 }
192
193 // BasicClient implementation
194 virtual status_t initialize(camera_module_t *module);
195
196 /***
197 IProCamera implementation
198 ***/
199
200
201 virtual status_t connect(
202 const sp<IProCameraCallbacks>& callbacks);
203 virtual void disconnect();
204
205 virtual status_t exclusiveTryLock();
206 virtual status_t exclusiveLock();
207 virtual status_t exclusiveUnlock();
208
209 virtual bool hasExclusiveLock();
210
211 // Note that the callee gets a copy of the metadata.
212 virtual int submitRequest(camera_metadata_t* metadata,
213 bool streaming = false);
214 virtual status_t cancelRequest(int requestId);
215
216 virtual status_t requestStream(int streamId);
217 virtual status_t cancelStream(int streamId);
218
219 protected:
220 sp<IProCameraCallbacks> mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700221
222 };
223
Mathias Agopian65ab4712010-07-14 17:59:35 -0700224private:
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800225
226 // Delay-load the Camera HAL module
227 virtual void onFirstRef();
228
229 virtual sp<BasicClient> getClientByRemote(const wp<IBinder>& cameraClient);
230
Mathias Agopian65ab4712010-07-14 17:59:35 -0700231 Mutex mServiceLock;
232 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -0700233 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700234 int mNumberOfCameras;
235
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800236 typedef wp<ProClient> weak_pro_client_ptr;
237 Vector<weak_pro_client_ptr> mProClientList[MAX_CAMERAS];
238
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700239 // needs to be called with mServiceLock held
Igor Murashkin507994d2012-10-05 10:44:57 -0700240 sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800241 sp<ProClient> findProClientUnsafe(
242 const wp<IBinder>& cameraCallbacksRemote);
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700243
Mathias Agopian65ab4712010-07-14 17:59:35 -0700244 // atomics to record whether the hardware is allocated to some client.
245 volatile int32_t mBusy[MAX_CAMERAS];
246 void setCameraBusy(int cameraId);
247 void setCameraFree(int cameraId);
248
249 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800250 MediaPlayer* newMediaPlayer(const char *file);
251
Mathias Agopian65ab4712010-07-14 17:59:35 -0700252 Mutex mSoundLock;
253 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
254 int mSoundRef; // reference count (release all MediaPlayer when 0)
255
Iliyan Malchev8951a972011-04-14 16:55:59 -0700256 camera_module_t *mModule;
Igor Murashkin8dcdb952012-10-02 16:05:11 -0700257
258 // IBinder::DeathRecipient implementation
259 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkinbfb5d5e2013-02-20 17:15:11 -0800260
261 // Helpers
262 int getDeviceVersion(int cameraId, int* facing);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700263};
264
265} // namespace android
266
267#endif