3 ** Copyright 2008, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define LOG_TAG "BpCameraService"
19 #include <utils/Log.h>
20 #include <utils/Errors.h>
21 #include <utils/String16.h>
24 #include <sys/types.h>
26 #include <binder/Parcel.h>
27 #include <binder/IPCThreadState.h>
28 #include <binder/IServiceManager.h>
30 #include <camera/ICameraService.h>
31 #include <camera/ICameraServiceListener.h>
32 #include <camera/IProCameraUser.h>
33 #include <camera/IProCameraCallbacks.h>
34 #include <camera/ICamera.h>
35 #include <camera/ICameraClient.h>
36 #include <camera/camera2/ICameraDeviceUser.h>
37 #include <camera/camera2/ICameraDeviceCallbacks.h>
38 #include <camera/CameraMetadata.h>
39 #include <camera/VendorTagDescriptor.h>
47 EX_BAD_PARCELABLE = -2,
48 EX_ILLEGAL_ARGUMENT = -3,
50 EX_ILLEGAL_STATE = -5,
51 EX_HAS_REPLY_HEADER = -128, // special; see below
54 static bool readExceptionCode(Parcel& reply) {
55 int32_t exceptionCode = reply.readExceptionCode();
57 if (exceptionCode != 0) {
59 switch(exceptionCode) {
61 errorMsg = "Security";
63 case EX_BAD_PARCELABLE:
64 errorMsg = "BadParcelable";
67 errorMsg = "NullPointer";
69 case EX_ILLEGAL_STATE:
70 errorMsg = "IllegalState";
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";
81 ALOGE("Binder transmission error %s (%d)", errorMsg, exceptionCode);
90 class BpCameraService: public BpInterface<ICameraService>
93 BpCameraService(const sp<IBinder>& impl)
94 : BpInterface<ICameraService>(impl)
98 // get number of cameras available
99 virtual int32_t getNumberOfCameras()
102 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
103 remote()->transact(BnCameraService::GET_NUMBER_OF_CAMERAS, data, &reply);
105 if (readExceptionCode(reply)) return 0;
106 return reply.readInt32();
109 // get information about a camera
110 virtual status_t getCameraInfo(int cameraId,
111 struct CameraInfo* cameraInfo) {
113 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
114 data.writeInt32(cameraId);
115 remote()->transact(BnCameraService::GET_CAMERA_INFO, data, &reply);
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();
126 // get camera characteristics (static metadata)
127 virtual status_t getCameraCharacteristics(int cameraId,
128 CameraMetadata* cameraInfo) {
130 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
131 data.writeInt32(cameraId);
132 remote()->transact(BnCameraService::GET_CAMERA_CHARACTERISTICS, data, &reply);
134 if (readExceptionCode(reply)) return -EPROTO;
135 status_t result = reply.readInt32();
138 if (reply.readInt32() != 0) {
139 out.readFromParcel(&reply);
142 if (cameraInfo != NULL) {
143 cameraInfo->swap(out);
149 // Get enumeration and description of vendor tags for camera
150 virtual status_t getCameraVendorTagDescriptor(/*out*/sp<VendorTagDescriptor>& desc) {
152 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
153 remote()->transact(BnCameraService::GET_CAMERA_VENDOR_TAG_DESCRIPTOR, data, &reply);
155 if (readExceptionCode(reply)) return -EPROTO;
156 status_t result = reply.readInt32();
158 if (reply.readInt32() != 0) {
159 sp<VendorTagDescriptor> d;
160 if (VendorTagDescriptor::createFromParcel(&reply, /*out*/d) == OK) {
167 // connect to camera service (android.hardware.Camera)
168 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
169 const String16 &clientPackageName, int clientUid,
174 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
175 data.writeStrongBinder(cameraClient->asBinder());
176 data.writeInt32(cameraId);
177 data.writeString16(clientPackageName);
178 data.writeInt32(clientUid);
179 remote()->transact(BnCameraService::CONNECT, data, &reply);
181 if (readExceptionCode(reply)) return -EPROTO;
182 status_t status = reply.readInt32();
183 if (reply.readInt32() != 0) {
184 device = interface_cast<ICamera>(reply.readStrongBinder());
189 // connect to camera service (android.hardware.Camera)
190 virtual status_t connectLegacy(const sp<ICameraClient>& cameraClient, int cameraId,
192 const String16 &clientPackageName, int clientUid,
193 /*out*/sp<ICamera>& device)
196 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
197 data.writeStrongBinder(cameraClient->asBinder());
198 data.writeInt32(cameraId);
199 data.writeInt32(halVersion);
200 data.writeString16(clientPackageName);
201 data.writeInt32(clientUid);
202 remote()->transact(BnCameraService::CONNECT_LEGACY, data, &reply);
204 if (readExceptionCode(reply)) return -EPROTO;
205 status_t status = reply.readInt32();
206 if (reply.readInt32() != 0) {
207 device = interface_cast<ICamera>(reply.readStrongBinder());
212 // connect to camera service (pro client)
213 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb, int cameraId,
214 const String16 &clientPackageName, int clientUid,
216 sp<IProCameraUser>& device)
219 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
220 data.writeStrongBinder(cameraCb->asBinder());
221 data.writeInt32(cameraId);
222 data.writeString16(clientPackageName);
223 data.writeInt32(clientUid);
224 remote()->transact(BnCameraService::CONNECT_PRO, data, &reply);
226 if (readExceptionCode(reply)) return -EPROTO;
227 status_t status = reply.readInt32();
228 if (reply.readInt32() != 0) {
229 device = interface_cast<IProCameraUser>(reply.readStrongBinder());
234 // connect to camera service (android.hardware.camera2.CameraDevice)
235 virtual status_t connectDevice(
236 const sp<ICameraDeviceCallbacks>& cameraCb,
238 const String16& clientPackageName,
241 sp<ICameraDeviceUser>& device)
244 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
245 data.writeStrongBinder(cameraCb->asBinder());
246 data.writeInt32(cameraId);
247 data.writeString16(clientPackageName);
248 data.writeInt32(clientUid);
249 remote()->transact(BnCameraService::CONNECT_DEVICE, data, &reply);
251 if (readExceptionCode(reply)) return -EPROTO;
252 status_t status = reply.readInt32();
253 if (reply.readInt32() != 0) {
254 device = interface_cast<ICameraDeviceUser>(reply.readStrongBinder());
259 virtual status_t addListener(const sp<ICameraServiceListener>& listener)
262 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
263 data.writeStrongBinder(listener->asBinder());
264 remote()->transact(BnCameraService::ADD_LISTENER, data, &reply);
266 if (readExceptionCode(reply)) return -EPROTO;
267 return reply.readInt32();
270 virtual status_t removeListener(const sp<ICameraServiceListener>& listener)
273 data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());
274 data.writeStrongBinder(listener->asBinder());
275 remote()->transact(BnCameraService::REMOVE_LISTENER, data, &reply);
277 if (readExceptionCode(reply)) return -EPROTO;
278 return reply.readInt32();
281 virtual status_t getLegacyParameters(int cameraId, String16* parameters) {
282 if (parameters == NULL) {
283 ALOGE("%s: parameters must not be null", __FUNCTION__);
289 data.writeInt32(cameraId);
290 remote()->transact(BnCameraService::GET_LEGACY_PARAMETERS, data, &reply);
291 if (readExceptionCode(reply)) return -EPROTO;
293 status_t res = data.readInt32();
294 int32_t length = data.readInt32(); // -1 means null
296 *parameters = data.readString16();
298 *parameters = String16();
304 virtual status_t supportsCameraApi(int cameraId, int apiVersion) {
307 data.writeInt32(cameraId);
308 data.writeInt32(apiVersion);
309 remote()->transact(BnCameraService::SUPPORTS_CAMERA_API, data, &reply);
310 if (readExceptionCode(reply)) return -EPROTO;
312 status_t res = data.readInt32();
317 IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService");
319 // ----------------------------------------------------------------------
321 status_t BnCameraService::onTransact(
322 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
325 case GET_NUMBER_OF_CAMERAS: {
326 CHECK_INTERFACE(ICameraService, data, reply);
327 reply->writeNoException();
328 reply->writeInt32(getNumberOfCameras());
331 case GET_CAMERA_INFO: {
332 CHECK_INTERFACE(ICameraService, data, reply);
333 CameraInfo cameraInfo = CameraInfo();
334 memset(&cameraInfo, 0, sizeof(cameraInfo));
335 status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
336 reply->writeNoException();
337 reply->writeInt32(result);
339 // Fake a parcelable object here
340 reply->writeInt32(1); // means the parcelable is included
341 reply->writeInt32(cameraInfo.facing);
342 reply->writeInt32(cameraInfo.orientation);
345 case GET_CAMERA_CHARACTERISTICS: {
346 CHECK_INTERFACE(ICameraService, data, reply);
348 status_t result = getCameraCharacteristics(data.readInt32(), &info);
349 reply->writeNoException();
350 reply->writeInt32(result);
352 // out-variables are after exception and return value
353 reply->writeInt32(1); // means the parcelable is included
354 info.writeToParcel(reply);
357 case GET_CAMERA_VENDOR_TAG_DESCRIPTOR: {
358 CHECK_INTERFACE(ICameraService, data, reply);
359 sp<VendorTagDescriptor> d;
360 status_t result = getCameraVendorTagDescriptor(d);
361 reply->writeNoException();
362 reply->writeInt32(result);
364 // out-variables are after exception and return value
366 reply->writeInt32(0);
368 reply->writeInt32(1); // means the parcelable is included
369 d->writeToParcel(reply);
374 CHECK_INTERFACE(ICameraService, data, reply);
375 sp<ICameraClient> cameraClient =
376 interface_cast<ICameraClient>(data.readStrongBinder());
377 int32_t cameraId = data.readInt32();
378 const String16 clientName = data.readString16();
379 int32_t clientUid = data.readInt32();
381 status_t status = connect(cameraClient, cameraId,
382 clientName, clientUid, /*out*/camera);
383 reply->writeNoException();
384 reply->writeInt32(status);
385 if (camera != NULL) {
386 reply->writeInt32(1);
387 reply->writeStrongBinder(camera->asBinder());
389 reply->writeInt32(0);
394 CHECK_INTERFACE(ICameraService, data, reply);
395 sp<IProCameraCallbacks> cameraClient =
396 interface_cast<IProCameraCallbacks>(data.readStrongBinder());
397 int32_t cameraId = data.readInt32();
398 const String16 clientName = data.readString16();
399 int32_t clientUid = data.readInt32();
400 sp<IProCameraUser> camera;
401 status_t status = connectPro(cameraClient, cameraId,
402 clientName, clientUid, /*out*/camera);
403 reply->writeNoException();
404 reply->writeInt32(status);
405 if (camera != NULL) {
406 reply->writeInt32(1);
407 reply->writeStrongBinder(camera->asBinder());
409 reply->writeInt32(0);
413 case CONNECT_DEVICE: {
414 CHECK_INTERFACE(ICameraService, data, reply);
415 sp<ICameraDeviceCallbacks> cameraClient =
416 interface_cast<ICameraDeviceCallbacks>(data.readStrongBinder());
417 int32_t cameraId = data.readInt32();
418 const String16 clientName = data.readString16();
419 int32_t clientUid = data.readInt32();
420 sp<ICameraDeviceUser> camera;
421 status_t status = connectDevice(cameraClient, cameraId,
422 clientName, clientUid, /*out*/camera);
423 reply->writeNoException();
424 reply->writeInt32(status);
425 if (camera != NULL) {
426 reply->writeInt32(1);
427 reply->writeStrongBinder(camera->asBinder());
429 reply->writeInt32(0);
434 CHECK_INTERFACE(ICameraService, data, reply);
435 sp<ICameraServiceListener> listener =
436 interface_cast<ICameraServiceListener>(data.readStrongBinder());
437 reply->writeNoException();
438 reply->writeInt32(addListener(listener));
441 case REMOVE_LISTENER: {
442 CHECK_INTERFACE(ICameraService, data, reply);
443 sp<ICameraServiceListener> listener =
444 interface_cast<ICameraServiceListener>(data.readStrongBinder());
445 reply->writeNoException();
446 reply->writeInt32(removeListener(listener));
449 case GET_LEGACY_PARAMETERS: {
450 CHECK_INTERFACE(ICameraService, data, reply);
451 int cameraId = data.readInt32();
454 reply->writeNoException();
456 reply->writeInt32(getLegacyParameters(cameraId, ¶meters));
458 reply->writeInt32(1); // parameters is always available
459 reply->writeString16(parameters);
462 case SUPPORTS_CAMERA_API: {
463 CHECK_INTERFACE(ICameraService, data, reply);
464 int cameraId = data.readInt32();
465 int apiVersion = data.readInt32();
467 reply->writeNoException();
469 reply->writeInt32(supportsCameraApi(cameraId, apiVersion));
472 case CONNECT_LEGACY: {
473 CHECK_INTERFACE(ICameraService, data, reply);
474 sp<ICameraClient> cameraClient =
475 interface_cast<ICameraClient>(data.readStrongBinder());
476 int32_t cameraId = data.readInt32();
477 int32_t halVersion = data.readInt32();
478 const String16 clientName = data.readString16();
479 int32_t clientUid = data.readInt32();
481 status_t status = connectLegacy(cameraClient, cameraId, halVersion,
482 clientName, clientUid, /*out*/camera);
483 reply->writeNoException();
484 reply->writeInt32(status);
485 if (camera != NULL) {
486 reply->writeInt32(1);
487 reply->writeStrongBinder(camera->asBinder());
489 reply->writeInt32(0);
494 return BBinder::onTransact(code, data, reply, flags);
498 // ----------------------------------------------------------------------------
500 }; // namespace android