blob: 9943be64c4363ccd2ef25b48e709d980b1c2ea4f [file] [log] [blame]
Mathias Agopian3cf61352010-02-09 17:46:37 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
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//#define LOG_NDEBUG 0
19#define LOG_TAG "ICamera"
20#include <utils/Log.h>
21#include <stdint.h>
22#include <sys/types.h>
23#include <binder/Parcel.h>
24#include <camera/ICamera.h>
Andy McFadden8ba01022012-12-18 09:46:54 -080025#include <gui/IGraphicBufferProducer.h>
Mathias Agopiandf712ea2012-02-25 18:48:35 -080026#include <gui/Surface.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080027
28namespace android {
29
30enum {
31 DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -070032 SET_PREVIEW_TARGET,
Mathias Agopian3cf61352010-02-09 17:46:37 -080033 SET_PREVIEW_CALLBACK_FLAG,
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -070034 SET_PREVIEW_CALLBACK_TARGET,
Mathias Agopian3cf61352010-02-09 17:46:37 -080035 START_PREVIEW,
36 STOP_PREVIEW,
37 AUTO_FOCUS,
38 CANCEL_AUTO_FOCUS,
39 TAKE_PICTURE,
40 SET_PARAMETERS,
41 GET_PARAMETERS,
42 SEND_COMMAND,
43 CONNECT,
44 LOCK,
45 UNLOCK,
46 PREVIEW_ENABLED,
47 START_RECORDING,
48 STOP_RECORDING,
49 RECORDING_ENABLED,
50 RELEASE_RECORDING_FRAME,
James Donge2ad6732010-10-18 20:42:51 -070051 STORE_META_DATA_IN_BUFFERS,
Mathias Agopian3cf61352010-02-09 17:46:37 -080052};
53
54class BpCamera: public BpInterface<ICamera>
55{
56public:
57 BpCamera(const sp<IBinder>& impl)
58 : BpInterface<ICamera>(impl)
59 {
60 }
61
62 // disconnect from camera service
63 void disconnect()
64 {
Steve Block3856b092011-10-20 11:56:00 +010065 ALOGV("disconnect");
Mathias Agopian3cf61352010-02-09 17:46:37 -080066 Parcel data, reply;
67 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
68 remote()->transact(DISCONNECT, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -070069 reply.readExceptionCode();
Mathias Agopian3cf61352010-02-09 17:46:37 -080070 }
71
Andy McFadden8ba01022012-12-18 09:46:54 -080072 // pass the buffered IGraphicBufferProducer to the camera service
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -070073 status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080074 {
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -070075 ALOGV("setPreviewTarget");
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080076 Parcel data, reply;
77 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -080078 sp<IBinder> b(IInterface::asBinder(bufferProducer));
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080079 data.writeStrongBinder(b);
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -070080 remote()->transact(SET_PREVIEW_TARGET, data, &reply);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080081 return reply.readInt32();
82 }
83
Mathias Agopian3cf61352010-02-09 17:46:37 -080084 // set the preview callback flag to affect how the received frames from
85 // preview are handled. See Camera.h for details.
86 void setPreviewCallbackFlag(int flag)
87 {
Steve Block3856b092011-10-20 11:56:00 +010088 ALOGV("setPreviewCallbackFlag(%d)", flag);
Mathias Agopian3cf61352010-02-09 17:46:37 -080089 Parcel data, reply;
90 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
91 data.writeInt32(flag);
92 remote()->transact(SET_PREVIEW_CALLBACK_FLAG, data, &reply);
93 }
94
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -070095 status_t setPreviewCallbackTarget(
96 const sp<IGraphicBufferProducer>& callbackProducer)
97 {
98 ALOGV("setPreviewCallbackTarget");
99 Parcel data, reply;
100 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800101 sp<IBinder> b(IInterface::asBinder(callbackProducer));
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700102 data.writeStrongBinder(b);
103 remote()->transact(SET_PREVIEW_CALLBACK_TARGET, data, &reply);
104 return reply.readInt32();
105 }
106
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700107 // start preview mode, must call setPreviewTarget first
Mathias Agopian3cf61352010-02-09 17:46:37 -0800108 status_t startPreview()
109 {
Steve Block3856b092011-10-20 11:56:00 +0100110 ALOGV("startPreview");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800111 Parcel data, reply;
112 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
113 remote()->transact(START_PREVIEW, data, &reply);
114 return reply.readInt32();
115 }
116
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700117 // start recording mode, must call setPreviewTarget first
Mathias Agopian3cf61352010-02-09 17:46:37 -0800118 status_t startRecording()
119 {
Steve Block3856b092011-10-20 11:56:00 +0100120 ALOGV("startRecording");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800121 Parcel data, reply;
122 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
123 remote()->transact(START_RECORDING, data, &reply);
124 return reply.readInt32();
125 }
126
127 // stop preview mode
128 void stopPreview()
129 {
Steve Block3856b092011-10-20 11:56:00 +0100130 ALOGV("stopPreview");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800131 Parcel data, reply;
132 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
133 remote()->transact(STOP_PREVIEW, data, &reply);
134 }
135
136 // stop recording mode
137 void stopRecording()
138 {
Steve Block3856b092011-10-20 11:56:00 +0100139 ALOGV("stopRecording");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800140 Parcel data, reply;
141 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
142 remote()->transact(STOP_RECORDING, data, &reply);
143 }
144
145 void releaseRecordingFrame(const sp<IMemory>& mem)
146 {
Steve Block3856b092011-10-20 11:56:00 +0100147 ALOGV("releaseRecordingFrame");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800148 Parcel data, reply;
149 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800150 data.writeStrongBinder(IInterface::asBinder(mem));
Mathias Agopian3cf61352010-02-09 17:46:37 -0800151 remote()->transact(RELEASE_RECORDING_FRAME, data, &reply);
152 }
153
James Donge2ad6732010-10-18 20:42:51 -0700154 status_t storeMetaDataInBuffers(bool enabled)
155 {
Steve Block3856b092011-10-20 11:56:00 +0100156 ALOGV("storeMetaDataInBuffers: %s", enabled? "true": "false");
James Donge2ad6732010-10-18 20:42:51 -0700157 Parcel data, reply;
158 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
159 data.writeInt32(enabled);
160 remote()->transact(STORE_META_DATA_IN_BUFFERS, data, &reply);
161 return reply.readInt32();
162 }
163
Mathias Agopian3cf61352010-02-09 17:46:37 -0800164 // check preview state
165 bool previewEnabled()
166 {
Steve Block3856b092011-10-20 11:56:00 +0100167 ALOGV("previewEnabled");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800168 Parcel data, reply;
169 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
170 remote()->transact(PREVIEW_ENABLED, data, &reply);
171 return reply.readInt32();
172 }
173
174 // check recording state
175 bool recordingEnabled()
176 {
Steve Block3856b092011-10-20 11:56:00 +0100177 ALOGV("recordingEnabled");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800178 Parcel data, reply;
179 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
180 remote()->transact(RECORDING_ENABLED, data, &reply);
181 return reply.readInt32();
182 }
183
184 // auto focus
185 status_t autoFocus()
186 {
Steve Block3856b092011-10-20 11:56:00 +0100187 ALOGV("autoFocus");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800188 Parcel data, reply;
189 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
190 remote()->transact(AUTO_FOCUS, data, &reply);
191 status_t ret = reply.readInt32();
192 return ret;
193 }
194
195 // cancel focus
196 status_t cancelAutoFocus()
197 {
Steve Block3856b092011-10-20 11:56:00 +0100198 ALOGV("cancelAutoFocus");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800199 Parcel data, reply;
200 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
201 remote()->transact(CANCEL_AUTO_FOCUS, data, &reply);
202 status_t ret = reply.readInt32();
203 return ret;
204 }
205
206 // take a picture - returns an IMemory (ref-counted mmap)
James Donge468ac52011-02-17 16:38:06 -0800207 status_t takePicture(int msgType)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800208 {
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("takePicture: 0x%x", msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800210 Parcel data, reply;
211 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
James Donge468ac52011-02-17 16:38:06 -0800212 data.writeInt32(msgType);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800213 remote()->transact(TAKE_PICTURE, data, &reply);
214 status_t ret = reply.readInt32();
215 return ret;
216 }
217
218 // set preview/capture parameters - key/value pairs
219 status_t setParameters(const String8& params)
220 {
Steve Block3856b092011-10-20 11:56:00 +0100221 ALOGV("setParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800222 Parcel data, reply;
223 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
224 data.writeString8(params);
225 remote()->transact(SET_PARAMETERS, data, &reply);
226 return reply.readInt32();
227 }
228
229 // get preview/capture parameters - key/value pairs
230 String8 getParameters() const
231 {
Steve Block3856b092011-10-20 11:56:00 +0100232 ALOGV("getParameters");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800233 Parcel data, reply;
234 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
235 remote()->transact(GET_PARAMETERS, data, &reply);
236 return reply.readString8();
237 }
238 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2)
239 {
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("sendCommand");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800241 Parcel data, reply;
242 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
243 data.writeInt32(cmd);
244 data.writeInt32(arg1);
245 data.writeInt32(arg2);
246 remote()->transact(SEND_COMMAND, data, &reply);
247 return reply.readInt32();
248 }
249 virtual status_t connect(const sp<ICameraClient>& cameraClient)
250 {
251 Parcel data, reply;
252 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
Marco Nelissenf8880202014-11-14 07:58:25 -0800253 data.writeStrongBinder(IInterface::asBinder(cameraClient));
Mathias Agopian3cf61352010-02-09 17:46:37 -0800254 remote()->transact(CONNECT, data, &reply);
255 return reply.readInt32();
256 }
257 virtual status_t lock()
258 {
259 Parcel data, reply;
260 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
261 remote()->transact(LOCK, data, &reply);
262 return reply.readInt32();
263 }
264 virtual status_t unlock()
265 {
266 Parcel data, reply;
267 data.writeInterfaceToken(ICamera::getInterfaceDescriptor());
268 remote()->transact(UNLOCK, data, &reply);
269 return reply.readInt32();
270 }
271};
272
273IMPLEMENT_META_INTERFACE(Camera, "android.hardware.ICamera");
274
275// ----------------------------------------------------------------------
276
277status_t BnCamera::onTransact(
278 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
279{
280 switch(code) {
281 case DISCONNECT: {
Steve Block3856b092011-10-20 11:56:00 +0100282 ALOGV("DISCONNECT");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800283 CHECK_INTERFACE(ICamera, data, reply);
284 disconnect();
Igor Murashkinbef3f232013-05-30 17:47:38 -0700285 reply->writeNoException();
Mathias Agopian3cf61352010-02-09 17:46:37 -0800286 return NO_ERROR;
287 } break;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700288 case SET_PREVIEW_TARGET: {
289 ALOGV("SET_PREVIEW_TARGET");
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800290 CHECK_INTERFACE(ICamera, data, reply);
Andy McFadden8ba01022012-12-18 09:46:54 -0800291 sp<IGraphicBufferProducer> st =
292 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700293 reply->writeInt32(setPreviewTarget(st));
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800294 return NO_ERROR;
295 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800296 case SET_PREVIEW_CALLBACK_FLAG: {
Steve Block3856b092011-10-20 11:56:00 +0100297 ALOGV("SET_PREVIEW_CALLBACK_TYPE");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800298 CHECK_INTERFACE(ICamera, data, reply);
299 int callback_flag = data.readInt32();
300 setPreviewCallbackFlag(callback_flag);
301 return NO_ERROR;
302 } break;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700303 case SET_PREVIEW_CALLBACK_TARGET: {
304 ALOGV("SET_PREVIEW_CALLBACK_TARGET");
305 CHECK_INTERFACE(ICamera, data, reply);
306 sp<IGraphicBufferProducer> cp =
307 interface_cast<IGraphicBufferProducer>(data.readStrongBinder());
308 reply->writeInt32(setPreviewCallbackTarget(cp));
309 return NO_ERROR;
310 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800311 case START_PREVIEW: {
Steve Block3856b092011-10-20 11:56:00 +0100312 ALOGV("START_PREVIEW");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800313 CHECK_INTERFACE(ICamera, data, reply);
314 reply->writeInt32(startPreview());
315 return NO_ERROR;
316 } break;
317 case START_RECORDING: {
Steve Block3856b092011-10-20 11:56:00 +0100318 ALOGV("START_RECORDING");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800319 CHECK_INTERFACE(ICamera, data, reply);
320 reply->writeInt32(startRecording());
321 return NO_ERROR;
322 } break;
323 case STOP_PREVIEW: {
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("STOP_PREVIEW");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800325 CHECK_INTERFACE(ICamera, data, reply);
326 stopPreview();
327 return NO_ERROR;
328 } break;
329 case STOP_RECORDING: {
Steve Block3856b092011-10-20 11:56:00 +0100330 ALOGV("STOP_RECORDING");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800331 CHECK_INTERFACE(ICamera, data, reply);
332 stopRecording();
333 return NO_ERROR;
334 } break;
335 case RELEASE_RECORDING_FRAME: {
Steve Block3856b092011-10-20 11:56:00 +0100336 ALOGV("RELEASE_RECORDING_FRAME");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800337 CHECK_INTERFACE(ICamera, data, reply);
338 sp<IMemory> mem = interface_cast<IMemory>(data.readStrongBinder());
339 releaseRecordingFrame(mem);
340 return NO_ERROR;
341 } break;
James Donge2ad6732010-10-18 20:42:51 -0700342 case STORE_META_DATA_IN_BUFFERS: {
Steve Block3856b092011-10-20 11:56:00 +0100343 ALOGV("STORE_META_DATA_IN_BUFFERS");
James Donge2ad6732010-10-18 20:42:51 -0700344 CHECK_INTERFACE(ICamera, data, reply);
345 bool enabled = data.readInt32();
346 reply->writeInt32(storeMetaDataInBuffers(enabled));
347 return NO_ERROR;
348 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800349 case PREVIEW_ENABLED: {
Steve Block3856b092011-10-20 11:56:00 +0100350 ALOGV("PREVIEW_ENABLED");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800351 CHECK_INTERFACE(ICamera, data, reply);
352 reply->writeInt32(previewEnabled());
353 return NO_ERROR;
354 } break;
355 case RECORDING_ENABLED: {
Steve Block3856b092011-10-20 11:56:00 +0100356 ALOGV("RECORDING_ENABLED");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800357 CHECK_INTERFACE(ICamera, data, reply);
358 reply->writeInt32(recordingEnabled());
359 return NO_ERROR;
360 } break;
361 case AUTO_FOCUS: {
Steve Block3856b092011-10-20 11:56:00 +0100362 ALOGV("AUTO_FOCUS");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800363 CHECK_INTERFACE(ICamera, data, reply);
364 reply->writeInt32(autoFocus());
365 return NO_ERROR;
366 } break;
367 case CANCEL_AUTO_FOCUS: {
Steve Block3856b092011-10-20 11:56:00 +0100368 ALOGV("CANCEL_AUTO_FOCUS");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800369 CHECK_INTERFACE(ICamera, data, reply);
370 reply->writeInt32(cancelAutoFocus());
371 return NO_ERROR;
372 } break;
373 case TAKE_PICTURE: {
Steve Block3856b092011-10-20 11:56:00 +0100374 ALOGV("TAKE_PICTURE");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800375 CHECK_INTERFACE(ICamera, data, reply);
James Donge468ac52011-02-17 16:38:06 -0800376 int msgType = data.readInt32();
377 reply->writeInt32(takePicture(msgType));
Mathias Agopian3cf61352010-02-09 17:46:37 -0800378 return NO_ERROR;
379 } break;
380 case SET_PARAMETERS: {
Steve Block3856b092011-10-20 11:56:00 +0100381 ALOGV("SET_PARAMETERS");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800382 CHECK_INTERFACE(ICamera, data, reply);
383 String8 params(data.readString8());
384 reply->writeInt32(setParameters(params));
385 return NO_ERROR;
386 } break;
387 case GET_PARAMETERS: {
Steve Block3856b092011-10-20 11:56:00 +0100388 ALOGV("GET_PARAMETERS");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800389 CHECK_INTERFACE(ICamera, data, reply);
390 reply->writeString8(getParameters());
391 return NO_ERROR;
392 } break;
393 case SEND_COMMAND: {
Steve Block3856b092011-10-20 11:56:00 +0100394 ALOGV("SEND_COMMAND");
Mathias Agopian3cf61352010-02-09 17:46:37 -0800395 CHECK_INTERFACE(ICamera, data, reply);
396 int command = data.readInt32();
397 int arg1 = data.readInt32();
398 int arg2 = data.readInt32();
399 reply->writeInt32(sendCommand(command, arg1, arg2));
400 return NO_ERROR;
401 } break;
402 case CONNECT: {
403 CHECK_INTERFACE(ICamera, data, reply);
404 sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
405 reply->writeInt32(connect(cameraClient));
406 return NO_ERROR;
407 } break;
408 case LOCK: {
409 CHECK_INTERFACE(ICamera, data, reply);
410 reply->writeInt32(lock());
411 return NO_ERROR;
412 } break;
413 case UNLOCK: {
414 CHECK_INTERFACE(ICamera, data, reply);
415 reply->writeInt32(unlock());
416 return NO_ERROR;
417 } break;
418 default:
419 return BBinder::onTransact(code, data, reply, flags);
420 }
421}
422
423// ----------------------------------------------------------------------------
424
425}; // namespace android