blob: 79c33f9ff774250cf86e478e9af416b6e859d499 [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
Igor Murashkinbef3f232013-05-30 17:47:38 -070018#define LOG_TAG "BpCameraService"
19#include <utils/Log.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080020#include <utils/Errors.h>
Igor Murashkin65d14b92014-06-17 12:03:20 -070021#include <utils/String16.h>
Igor Murashkinbef3f232013-05-30 17:47:38 -070022
Mathias Agopian3cf61352010-02-09 17:46:37 -080023#include <stdint.h>
24#include <sys/types.h>
25
26#include <binder/Parcel.h>
27#include <binder/IPCThreadState.h>
28#include <binder/IServiceManager.h>
29
30#include <camera/ICameraService.h>
Igor Murashkinbfc99152013-02-27 12:55:20 -080031#include <camera/ICameraServiceListener.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080032#include <camera/IProCameraUser.h>
33#include <camera/IProCameraCallbacks.h>
34#include <camera/ICamera.h>
35#include <camera/ICameraClient.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070036#include <camera/camera2/ICameraDeviceUser.h>
37#include <camera/camera2/ICameraDeviceCallbacks.h>
Zhijun He2b59be82013-09-25 10:14:30 -070038#include <camera/CameraMetadata.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080039#include <camera/VendorTagDescriptor.h>
Mathias Agopian3cf61352010-02-09 17:46:37 -080040
41namespace android {
42
Igor Murashkinbef3f232013-05-30 17:47:38 -070043namespace {
44
45enum {
46 EX_SECURITY = -1,
47 EX_BAD_PARCELABLE = -2,
48 EX_ILLEGAL_ARGUMENT = -3,
49 EX_NULL_POINTER = -4,
50 EX_ILLEGAL_STATE = -5,
51 EX_HAS_REPLY_HEADER = -128, // special; see below
52};
53
54static bool readExceptionCode(Parcel& reply) {
55 int32_t exceptionCode = reply.readExceptionCode();
56
57 if (exceptionCode != 0) {
58 const char* errorMsg;
59 switch(exceptionCode) {
60 case EX_SECURITY:
61 errorMsg = "Security";
62 break;
63 case EX_BAD_PARCELABLE:
64 errorMsg = "BadParcelable";
65 break;
66 case EX_NULL_POINTER:
67 errorMsg = "NullPointer";
68 break;
69 case EX_ILLEGAL_STATE:
70 errorMsg = "IllegalState";
71 break;
72 // Binder should be handling this code inside Parcel::readException
73 // but lets have a to-string here anyway just in case.
74 case EX_HAS_REPLY_HEADER:
75 errorMsg = "HasReplyHeader";
76 break;
77 default:
78 errorMsg = "Unknown";
79 }
80
81 ALOGE("Binder transmission error %s (%d)", errorMsg, exceptionCode);
82 return true;
83 }
84
85 return false;
86}
87
88};
89
Mathias Agopian3cf61352010-02-09 17:46:37 -080090class BpCameraService: public BpInterface<ICameraService>
91{
92public:
93 BpCameraService(const sp<IBinder>& impl)
94 : BpInterface<ICameraService>(impl)
95 {
96 }
97
Chih-Chung Chang35a055b2010-05-06 16:36:58 +080098 // get number of cameras available
99 virtual int32_t getNumberOfCameras()
100 {
101 Parcel data, reply;
102 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
103 remote()->transact(BnCameraService::GET_NUMBER_OF_CAMERAS, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700104
105 if (readExceptionCode(reply)) return 0;
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800106 return reply.readInt32();
107 }
108
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800109 // get information about a camera
110 virtual status_t getCameraInfo(int cameraId,
111 struct CameraInfo* cameraInfo) {
112 Parcel data, reply;
113 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
114 data.writeInt32(cameraId);
115 remote()->transact(BnCameraService::GET_CAMERA_INFO, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700116
117 if (readExceptionCode(reply)) return -EPROTO;
118 status_t result = reply.readInt32();
119 if (reply.readInt32() != 0) {
120 cameraInfo->facing = reply.readInt32();
121 cameraInfo->orientation = reply.readInt32();
122 }
123 return result;
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800124 }
125
Zhijun He2b59be82013-09-25 10:14:30 -0700126 // get camera characteristics (static metadata)
127 virtual status_t getCameraCharacteristics(int cameraId,
128 CameraMetadata* cameraInfo) {
129 Parcel data, reply;
130 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
131 data.writeInt32(cameraId);
132 remote()->transact(BnCameraService::GET_CAMERA_CHARACTERISTICS, data, &reply);
133
134 if (readExceptionCode(reply)) return -EPROTO;
135 status_t result = reply.readInt32();
136
137 CameraMetadata out;
138 if (reply.readInt32() != 0) {
139 out.readFromParcel(&reply);
140 }
141
142 if (cameraInfo != NULL) {
143 cameraInfo->swap(out);
144 }
145
146 return result;
147 }
148
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800149 // Get enumeration and description of vendor tags for camera
150 virtual status_t getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
151 Parcel data, reply;
152 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
153 remote()->transact(BnCameraService::GET_CAMERA_VENDOR_TAG_DESCRIPTOR, data, &reply);
154
155 if (readExceptionCode(reply)) return -EPROTO;
156 status_t result = reply.readInt32();
157
158 if (reply.readInt32() != 0) {
159 sp<VendorTagDescriptor> d;
160 if (VendorTagDescriptor::createFromParcel(&reply, /*out*/d) == OK) {
161 desc = d;
162 }
163 }
164 return result;
165 }
166
Igor Murashkine7ee7632013-06-11 18:10:18 -0700167 // connect to camera service (android.hardware.Camera)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700168 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
169 const String16 &clientPackageName, int clientUid,
170 /*out*/
171 sp<ICamera>& device)
Mathias Agopian3cf61352010-02-09 17:46:37 -0800172 {
173 Parcel data, reply;
174 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
175 data.writeStrongBinder(cameraClient->asBinder());
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800176 data.writeInt32(cameraId);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800177 data.writeString16(clientPackageName);
178 data.writeInt32(clientUid);
Mathias Agopian3cf61352010-02-09 17:46:37 -0800179 remote()->transact(BnCameraService::CONNECT, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700180
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700181 if (readExceptionCode(reply)) return -EPROTO;
182 status_t status = reply.readInt32();
183 if (reply.readInt32() != 0) {
184 device = interface_cast<ICamera>(reply.readStrongBinder());
185 }
186 return status;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800187 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800188
189 // connect to camera service (pro client)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700190 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb, int cameraId,
191 const String16 &clientPackageName, int clientUid,
192 /*out*/
193 sp<IProCameraUser>& device)
Igor Murashkin634a5152013-02-20 17:15:11 -0800194 {
195 Parcel data, reply;
196 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
197 data.writeStrongBinder(cameraCb->asBinder());
198 data.writeInt32(cameraId);
Igor Murashkinc073ba52013-02-26 14:32:34 -0800199 data.writeString16(clientPackageName);
200 data.writeInt32(clientUid);
Igor Murashkin634a5152013-02-20 17:15:11 -0800201 remote()->transact(BnCameraService::CONNECT_PRO, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700202
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700203 if (readExceptionCode(reply)) return -EPROTO;
204 status_t status = reply.readInt32();
205 if (reply.readInt32() != 0) {
206 device = interface_cast<IProCameraUser>(reply.readStrongBinder());
207 }
208 return status;
Igor Murashkin634a5152013-02-20 17:15:11 -0800209 }
Igor Murashkinbfc99152013-02-27 12:55:20 -0800210
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700211 // connect to camera service (android.hardware.camera2.CameraDevice)
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700212 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700213 const sp<ICameraDeviceCallbacks>& cameraCb,
214 int cameraId,
215 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700216 int clientUid,
217 /*out*/
218 sp<ICameraDeviceUser>& device)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700219 {
220 Parcel data, reply;
221 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
222 data.writeStrongBinder(cameraCb->asBinder());
223 data.writeInt32(cameraId);
224 data.writeString16(clientPackageName);
225 data.writeInt32(clientUid);
226 remote()->transact(BnCameraService::CONNECT_DEVICE, data, &reply);
227
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700228 if (readExceptionCode(reply)) return -EPROTO;
229 status_t status = reply.readInt32();
230 if (reply.readInt32() != 0) {
231 device = interface_cast<ICameraDeviceUser>(reply.readStrongBinder());
232 }
233 return status;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700234 }
235
Igor Murashkinbfc99152013-02-27 12:55:20 -0800236 virtual status_t addListener(const sp<ICameraServiceListener>& listener)
237 {
238 Parcel data, reply;
239 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
240 data.writeStrongBinder(listener->asBinder());
241 remote()->transact(BnCameraService::ADD_LISTENER, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700242
243 if (readExceptionCode(reply)) return -EPROTO;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800244 return reply.readInt32();
245 }
246
247 virtual status_t removeListener(const sp<ICameraServiceListener>& listener)
248 {
249 Parcel data, reply;
250 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
251 data.writeStrongBinder(listener->asBinder());
252 remote()->transact(BnCameraService::REMOVE_LISTENER, data, &reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700253
254 if (readExceptionCode(reply)) return -EPROTO;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800255 return reply.readInt32();
256 }
Igor Murashkin65d14b92014-06-17 12:03:20 -0700257
258 virtual status_t getLegacyParameters(int cameraId, String16* parameters) {
259 if (parameters == NULL) {
260 ALOGE("%s: parameters must not be null", __FUNCTION__);
261 return BAD_VALUE;
262 }
263
264 Parcel data, reply;
265
266 data.writeInt32(cameraId);
267 remote()->transact(BnCameraService::GET_LEGACY_PARAMETERS, data, &reply);
268 if (readExceptionCode(reply)) return -EPROTO;
269
270 status_t res = data.readInt32();
271 int32_t length = data.readInt32(); // -1 means null
272 if (length > 0) {
273 *parameters = data.readString16();
274 } else {
275 *parameters = String16();
276 }
277
278 return res;
279 }
280
281 virtual status_t supportsCameraApi(int cameraId, int apiVersion) {
282 Parcel data, reply;
283
284 data.writeInt32(cameraId);
285 data.writeInt32(apiVersion);
286 remote()->transact(BnCameraService::SUPPORTS_CAMERA_API, data, &reply);
287 if (readExceptionCode(reply)) return -EPROTO;
288
289 status_t res = data.readInt32();
290 return res;
291 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800292};
293
294IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService");
295
296// ----------------------------------------------------------------------
297
298status_t BnCameraService::onTransact(
299 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
300{
301 switch(code) {
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800302 case GET_NUMBER_OF_CAMERAS: {
303 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700304 reply->writeNoException();
Chih-Chung Chang35a055b2010-05-06 16:36:58 +0800305 reply->writeInt32(getNumberOfCameras());
306 return NO_ERROR;
307 } break;
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800308 case GET_CAMERA_INFO: {
309 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700310 CameraInfo cameraInfo = CameraInfo();
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800311 memset(&cameraInfo, 0, sizeof(cameraInfo));
312 status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700313 reply->writeNoException();
314 reply->writeInt32(result);
315
316 // Fake a parcelable object here
317 reply->writeInt32(1); // means the parcelable is included
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800318 reply->writeInt32(cameraInfo.facing);
319 reply->writeInt32(cameraInfo.orientation);
Chih-Chung Changddbdb352010-06-10 13:32:16 +0800320 return NO_ERROR;
321 } break;
Zhijun He2b59be82013-09-25 10:14:30 -0700322 case GET_CAMERA_CHARACTERISTICS: {
323 CHECK_INTERFACE(ICameraService, data, reply);
324 CameraMetadata info;
325 status_t result = getCameraCharacteristics(data.readInt32(), &info);
326 reply->writeNoException();
327 reply->writeInt32(result);
328
329 // out-variables are after exception and return value
330 reply->writeInt32(1); // means the parcelable is included
331 info.writeToParcel(reply);
332 return NO_ERROR;
333 } break;
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800334 case GET_CAMERA_VENDOR_TAG_DESCRIPTOR: {
335 CHECK_INTERFACE(ICameraService, data, reply);
336 sp<VendorTagDescriptor> d;
337 status_t result = getCameraVendorTagDescriptor(d);
338 reply->writeNoException();
339 reply->writeInt32(result);
340
341 // out-variables are after exception and return value
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800342 if (d == NULL) {
343 reply->writeInt32(0);
344 } else {
Igor Murashkine1445da2014-03-17 14:00:29 -0700345 reply->writeInt32(1); // means the parcelable is included
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800346 d->writeToParcel(reply);
347 }
348 return NO_ERROR;
349 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800350 case CONNECT: {
351 CHECK_INTERFACE(ICameraService, data, reply);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800352 sp<ICameraClient> cameraClient =
353 interface_cast<ICameraClient>(data.readStrongBinder());
354 int32_t cameraId = data.readInt32();
355 const String16 clientName = data.readString16();
356 int32_t clientUid = data.readInt32();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700357 sp<ICamera> camera;
358 status_t status = connect(cameraClient, cameraId,
Igor Murashkine1445da2014-03-17 14:00:29 -0700359 clientName, clientUid, /*out*/camera);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700360 reply->writeNoException();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700361 reply->writeInt32(status);
362 if (camera != NULL) {
363 reply->writeInt32(1);
364 reply->writeStrongBinder(camera->asBinder());
365 } else {
366 reply->writeInt32(0);
367 }
Mathias Agopian3cf61352010-02-09 17:46:37 -0800368 return NO_ERROR;
369 } break;
Igor Murashkin634a5152013-02-20 17:15:11 -0800370 case CONNECT_PRO: {
371 CHECK_INTERFACE(ICameraService, data, reply);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700372 sp<IProCameraCallbacks> cameraClient =
373 interface_cast<IProCameraCallbacks>(data.readStrongBinder());
Igor Murashkinc073ba52013-02-26 14:32:34 -0800374 int32_t cameraId = data.readInt32();
375 const String16 clientName = data.readString16();
376 int32_t clientUid = data.readInt32();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700377 sp<IProCameraUser> camera;
378 status_t status = connectPro(cameraClient, cameraId,
Igor Murashkine1445da2014-03-17 14:00:29 -0700379 clientName, clientUid, /*out*/camera);
Igor Murashkinbef3f232013-05-30 17:47:38 -0700380 reply->writeNoException();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700381 reply->writeInt32(status);
382 if (camera != NULL) {
383 reply->writeInt32(1);
384 reply->writeStrongBinder(camera->asBinder());
385 } else {
386 reply->writeInt32(0);
387 }
Igor Murashkin634a5152013-02-20 17:15:11 -0800388 return NO_ERROR;
389 } break;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700390 case CONNECT_DEVICE: {
391 CHECK_INTERFACE(ICameraService, data, reply);
392 sp<ICameraDeviceCallbacks> cameraClient =
393 interface_cast<ICameraDeviceCallbacks>(data.readStrongBinder());
394 int32_t cameraId = data.readInt32();
395 const String16 clientName = data.readString16();
396 int32_t clientUid = data.readInt32();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700397 sp<ICameraDeviceUser> camera;
398 status_t status = connectDevice(cameraClient, cameraId,
Igor Murashkine1445da2014-03-17 14:00:29 -0700399 clientName, clientUid, /*out*/camera);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700400 reply->writeNoException();
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700401 reply->writeInt32(status);
402 if (camera != NULL) {
403 reply->writeInt32(1);
404 reply->writeStrongBinder(camera->asBinder());
405 } else {
406 reply->writeInt32(0);
407 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700408 return NO_ERROR;
409 } break;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800410 case ADD_LISTENER: {
411 CHECK_INTERFACE(ICameraService, data, reply);
412 sp<ICameraServiceListener> listener =
413 interface_cast<ICameraServiceListener>(data.readStrongBinder());
Igor Murashkinbef3f232013-05-30 17:47:38 -0700414 reply->writeNoException();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800415 reply->writeInt32(addListener(listener));
416 return NO_ERROR;
417 } break;
418 case REMOVE_LISTENER: {
419 CHECK_INTERFACE(ICameraService, data, reply);
420 sp<ICameraServiceListener> listener =
421 interface_cast<ICameraServiceListener>(data.readStrongBinder());
Igor Murashkinbef3f232013-05-30 17:47:38 -0700422 reply->writeNoException();
Igor Murashkinbfc99152013-02-27 12:55:20 -0800423 reply->writeInt32(removeListener(listener));
424 return NO_ERROR;
425 } break;
Igor Murashkin65d14b92014-06-17 12:03:20 -0700426 case GET_LEGACY_PARAMETERS: {
427 CHECK_INTERFACE(ICameraService, data, reply);
428 int cameraId = data.readInt32();
429 String16 parameters;
430
431 reply->writeNoException();
432 // return value
433 reply->writeInt32(getLegacyParameters(cameraId, &parameters));
434 // out parameters
435 reply->writeInt32(1); // parameters is always available
436 reply->writeString16(parameters);
437 return NO_ERROR;
438 } break;
439 case SUPPORTS_CAMERA_API: {
440 CHECK_INTERFACE(ICameraService, data, reply);
441 int cameraId = data.readInt32();
442 int apiVersion = data.readInt32();
443
444 reply->writeNoException();
445 // return value
446 reply->writeInt32(supportsCameraApi(cameraId, apiVersion));
447 return NO_ERROR;
448 } break;
Mathias Agopian3cf61352010-02-09 17:46:37 -0800449 default:
450 return BBinder::onTransact(code, data, reply, flags);
451 }
452}
453
454// ----------------------------------------------------------------------------
455
456}; // namespace android