1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "CameraDeviceClient" 18 #define ATRACE_TAG ATRACE_TAG_CAMERA 19 //#define LOG_NDEBUG 0 20 21 #include <cutils/properties.h> 22 #include <utils/Log.h> 23 #include <utils/Trace.h> 24 #include <gui/Surface.h> 25 #include <camera/camera2/CaptureRequest.h> 26 #include <camera/CameraUtils.h> 27 28 #include "common/CameraDeviceBase.h" 29 #include "api2/CameraDeviceClient.h" 30 31 // Convenience methods for constructing binder::Status objects for error returns 32 33 #define STATUS_ERROR(errorCode, errorString) \ 34 binder::Status::fromServiceSpecificError(errorCode, \ 35 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString)) 36 37 #define STATUS_ERROR_FMT(errorCode, errorString, ...) \ 38 binder::Status::fromServiceSpecificError(errorCode, \ 39 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \ 40 __VA_ARGS__)) 41 42 namespace android { 43 using namespace camera2; 44 45 CameraDeviceClientBase::CameraDeviceClientBase( 46 const sp<CameraService>& cameraService, 47 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, 48 const String16& clientPackageName, 49 int cameraId, 50 int cameraFacing, 51 int clientPid, 52 uid_t clientUid, 53 int servicePid) : 54 BasicClient(cameraService, 55 IInterface::asBinder(remoteCallback), 56 clientPackageName, 57 cameraId, 58 cameraFacing, 59 clientPid, 60 clientUid, 61 servicePid), 62 mRemoteCallback(remoteCallback) { 63 } 64 65 // Interface used by CameraService 66 67 CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService, 68 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback, 69 const String16& clientPackageName, 70 int cameraId, 71 int cameraFacing, 72 int clientPid, 73 uid_t clientUid, 74 int servicePid) : 75 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, 76 cameraId, cameraFacing, clientPid, clientUid, servicePid), 77 mInputStream(), 78 mStreamingRequestId(REQUEST_ID_NONE), 79 mRequestIdCounter(0) { 80 81 ATRACE_CALL(); 82 ALOGI("CameraDeviceClient %d: Opened", cameraId); 83 } 84 85 status_t CameraDeviceClient::initialize(CameraModule *module) 86 { 87 ATRACE_CALL(); 88 status_t res; 89 90 res = Camera2ClientBase::initialize(module); 91 if (res != OK) { 92 return res; 93 } 94 95 String8 threadName; 96 mFrameProcessor = new FrameProcessorBase(mDevice); 97 threadName = String8::format("CDU-%d-FrameProc", mCameraId); 98 mFrameProcessor->run(threadName.string()); 99 100 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID, 101 FRAME_PROCESSOR_LISTENER_MAX_ID, 102 /*listener*/this, 103 /*sendPartials*/true); 104 105 return OK; 106 } 107 108 CameraDeviceClient::~CameraDeviceClient() { 109 } 110 111 binder::Status CameraDeviceClient::submitRequest( 112 const hardware::camera2::CaptureRequest& request, 113 bool streaming, 114 /*out*/ 115 hardware::camera2::utils::SubmitInfo *submitInfo) { 116 std::vector<hardware::camera2::CaptureRequest> requestList = { request }; 117 return submitRequestList(requestList, streaming, submitInfo); 118 } 119 120 binder::Status CameraDeviceClient::submitRequestList( 121 const std::vector<hardware::camera2::CaptureRequest>& requests, 122 bool streaming, 123 /*out*/ 124 hardware::camera2::utils::SubmitInfo *submitInfo) { 125 ATRACE_CALL(); 126 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size()); 127 128 binder::Status res = binder::Status::ok(); 129 status_t err; 130 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) { 131 return res; 132 } 133 134 Mutex::Autolock icl(mBinderSerializationLock); 135 136 if (!mDevice.get()) { 137 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 138 } 139 140 if (requests.empty()) { 141 ALOGE("%s: Camera %d: Sent null request. Rejecting request.", 142 __FUNCTION__, mCameraId); 143 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list"); 144 } 145 146 List<const CameraMetadata> metadataRequestList; 147 submitInfo->mRequestId = mRequestIdCounter; 148 uint32_t loopCounter = 0; 149 150 for (auto&& request: requests) { 151 if (request.mIsReprocess) { 152 if (!mInputStream.configured) { 153 ALOGE("%s: Camera %d: no input stream is configured.", __FUNCTION__, mCameraId); 154 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, 155 "No input configured for camera %d but request is for reprocessing", 156 mCameraId); 157 } else if (streaming) { 158 ALOGE("%s: Camera %d: streaming reprocess requests not supported.", __FUNCTION__, 159 mCameraId); 160 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, 161 "Repeating reprocess requests not supported"); 162 } 163 } 164 165 CameraMetadata metadata(request.mMetadata); 166 if (metadata.isEmpty()) { 167 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.", 168 __FUNCTION__, mCameraId); 169 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, 170 "Request settings are empty"); 171 } else if (request.mSurfaceList.isEmpty()) { 172 ALOGE("%s: Camera %d: Requests must have at least one surface target. " 173 "Rejecting request.", __FUNCTION__, mCameraId); 174 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, 175 "Request has no output targets"); 176 } 177 178 if (!enforceRequestPermissions(metadata)) { 179 // Callee logs 180 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED, 181 "Caller does not have permission to change restricted controls"); 182 } 183 184 /** 185 * Write in the output stream IDs which we calculate from 186 * the capture request's list of surface targets 187 */ 188 Vector<int32_t> outputStreamIds; 189 outputStreamIds.setCapacity(request.mSurfaceList.size()); 190 for (sp<Surface> surface : request.mSurfaceList) { 191 if (surface == 0) continue; 192 193 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer(); 194 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp)); 195 196 // Trying to submit request with surface that wasn't created 197 if (idx == NAME_NOT_FOUND) { 198 ALOGE("%s: Camera %d: Tried to submit a request with a surface that" 199 " we have not called createStream on", 200 __FUNCTION__, mCameraId); 201 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, 202 "Request targets Surface that is not part of current capture session"); 203 } 204 205 int streamId = mStreamMap.valueAt(idx); 206 outputStreamIds.push_back(streamId); 207 ALOGV("%s: Camera %d: Appending output stream %d to request", 208 __FUNCTION__, mCameraId, streamId); 209 } 210 211 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0], 212 outputStreamIds.size()); 213 214 if (request.mIsReprocess) { 215 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1); 216 } 217 218 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1); 219 loopCounter++; // loopCounter starts from 1 220 ALOGV("%s: Camera %d: Creating request with ID %d (%d of %zu)", 221 __FUNCTION__, mCameraId, submitInfo->mRequestId, loopCounter, requests.size()); 222 223 metadataRequestList.push_back(metadata); 224 } 225 mRequestIdCounter++; 226 227 if (streaming) { 228 err = mDevice->setStreamingRequestList(metadataRequestList, &(submitInfo->mLastFrameNumber)); 229 if (err != OK) { 230 String8 msg = String8::format( 231 "Camera %d: Got error %s (%d) after trying to set streaming request", 232 mCameraId, strerror(-err), err); 233 ALOGE("%s: %s", __FUNCTION__, msg.string()); 234 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, 235 msg.string()); 236 } else { 237 Mutex::Autolock idLock(mStreamingRequestIdLock); 238 mStreamingRequestId = submitInfo->mRequestId; 239 } 240 } else { 241 err = mDevice->captureList(metadataRequestList, &(submitInfo->mLastFrameNumber)); 242 if (err != OK) { 243 String8 msg = String8::format( 244 "Camera %d: Got error %s (%d) after trying to submit capture request", 245 mCameraId, strerror(-err), err); 246 ALOGE("%s: %s", __FUNCTION__, msg.string()); 247 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, 248 msg.string()); 249 } 250 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId); 251 } 252 253 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId); 254 return res; 255 } 256 257 binder::Status CameraDeviceClient::cancelRequest( 258 int requestId, 259 /*out*/ 260 int64_t* lastFrameNumber) { 261 ATRACE_CALL(); 262 ALOGV("%s, requestId = %d", __FUNCTION__, requestId); 263 264 status_t err; 265 binder::Status res; 266 267 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 268 269 Mutex::Autolock icl(mBinderSerializationLock); 270 271 if (!mDevice.get()) { 272 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 273 } 274 275 Mutex::Autolock idLock(mStreamingRequestIdLock); 276 if (mStreamingRequestId != requestId) { 277 String8 msg = String8::format("Camera %d: Canceling request ID %d doesn't match " 278 "current request ID %d", mCameraId, requestId, mStreamingRequestId); 279 ALOGE("%s: %s", __FUNCTION__, msg.string()); 280 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string()); 281 } 282 283 err = mDevice->clearStreamingRequest(lastFrameNumber); 284 285 if (err == OK) { 286 ALOGV("%s: Camera %d: Successfully cleared streaming request", 287 __FUNCTION__, mCameraId); 288 mStreamingRequestId = REQUEST_ID_NONE; 289 } else { 290 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 291 "Camera %d: Error clearing streaming request: %s (%d)", 292 mCameraId, strerror(-err), err); 293 } 294 295 return res; 296 } 297 298 binder::Status CameraDeviceClient::beginConfigure() { 299 // TODO: Implement this. 300 ALOGV("%s: Not implemented yet.", __FUNCTION__); 301 return binder::Status::ok(); 302 } 303 304 binder::Status CameraDeviceClient::endConfigure(bool isConstrainedHighSpeed) { 305 ALOGV("%s: ending configure (%d input stream, %zu output streams)", 306 __FUNCTION__, mInputStream.configured ? 1 : 0, mStreamMap.size()); 307 308 binder::Status res; 309 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 310 311 Mutex::Autolock icl(mBinderSerializationLock); 312 313 if (!mDevice.get()) { 314 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 315 } 316 317 // Sanitize the high speed session against necessary capability bit. 318 if (isConstrainedHighSpeed) { 319 CameraMetadata staticInfo = mDevice->info(); 320 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES); 321 bool isConstrainedHighSpeedSupported = false; 322 for(size_t i = 0; i < entry.count; ++i) { 323 uint8_t capability = entry.data.u8[i]; 324 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) { 325 isConstrainedHighSpeedSupported = true; 326 break; 327 } 328 } 329 if (!isConstrainedHighSpeedSupported) { 330 String8 msg = String8::format( 331 "Camera %d: Try to create a constrained high speed configuration on a device" 332 " that doesn't support it.", mCameraId); 333 ALOGE("%s: %s", __FUNCTION__, msg.string()); 334 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, 335 msg.string()); 336 } 337 } 338 339 status_t err = mDevice->configureStreams(isConstrainedHighSpeed); 340 if (err == BAD_VALUE) { 341 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, 342 "Camera %d: Unsupported set of inputs/outputs provided", 343 mCameraId); 344 } else if (err != OK) { 345 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 346 "Camera %d: Error configuring streams: %s (%d)", 347 mCameraId, strerror(-err), err); 348 } 349 350 return res; 351 } 352 353 binder::Status CameraDeviceClient::deleteStream(int streamId) { 354 ATRACE_CALL(); 355 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId); 356 357 binder::Status res; 358 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 359 360 Mutex::Autolock icl(mBinderSerializationLock); 361 362 if (!mDevice.get()) { 363 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 364 } 365 366 bool isInput = false; 367 ssize_t index = NAME_NOT_FOUND; 368 369 if (mInputStream.configured && mInputStream.id == streamId) { 370 isInput = true; 371 } else { 372 // Guard against trying to delete non-created streams 373 for (size_t i = 0; i < mStreamMap.size(); ++i) { 374 if (streamId == mStreamMap.valueAt(i)) { 375 index = i; 376 break; 377 } 378 } 379 380 if (index == NAME_NOT_FOUND) { 381 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no such " 382 "stream created yet", mCameraId, streamId); 383 ALOGW("%s: %s", __FUNCTION__, msg.string()); 384 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string()); 385 } 386 } 387 388 // Also returns BAD_VALUE if stream ID was not valid 389 status_t err = mDevice->deleteStream(streamId); 390 391 if (err != OK) { 392 String8 msg = String8::format("Camera %d: Unexpected error %s (%d) when deleting stream %d", 393 mCameraId, strerror(-err), err, streamId); 394 ALOGE("%s: %s", __FUNCTION__, msg.string()); 395 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 396 } else { 397 if (isInput) { 398 mInputStream.configured = false; 399 } else { 400 mStreamMap.removeItemsAt(index); 401 } 402 } 403 404 return res; 405 } 406 407 binder::Status CameraDeviceClient::createStream( 408 const hardware::camera2::params::OutputConfiguration &outputConfiguration, 409 /*out*/ 410 int32_t* newStreamId) { 411 ATRACE_CALL(); 412 413 binder::Status res; 414 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 415 416 Mutex::Autolock icl(mBinderSerializationLock); 417 418 sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer(); 419 if (bufferProducer == NULL) { 420 ALOGE("%s: bufferProducer must not be null", __FUNCTION__); 421 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid"); 422 } 423 if (!mDevice.get()) { 424 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 425 } 426 427 // Don't create multiple streams for the same target surface 428 { 429 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer)); 430 if (index != NAME_NOT_FOUND) { 431 String8 msg = String8::format("Camera %d: Surface already has a stream created for it " 432 "(ID %zd)", mCameraId, index); 433 ALOGW("%s: %s", __FUNCTION__, msg.string()); 434 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string()); 435 } 436 } 437 438 status_t err; 439 440 // HACK b/10949105 441 // Query consumer usage bits to set async operation mode for 442 // GLConsumer using controlledByApp parameter. 443 bool useAsync = false; 444 int32_t consumerUsage; 445 if ((err = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, 446 &consumerUsage)) != OK) { 447 String8 msg = String8::format("Camera %d: Failed to query Surface consumer usage: %s (%d)", 448 mCameraId, strerror(-err), err); 449 ALOGE("%s: %s", __FUNCTION__, msg.string()); 450 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 451 } 452 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) { 453 ALOGW("%s: Camera %d: Forcing asynchronous mode for stream", 454 __FUNCTION__, mCameraId); 455 useAsync = true; 456 } 457 458 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER | 459 GRALLOC_USAGE_RENDERSCRIPT; 460 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK | 461 GraphicBuffer::USAGE_HW_TEXTURE | 462 GraphicBuffer::USAGE_HW_COMPOSER; 463 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 && 464 (consumerUsage & allowedFlags) != 0; 465 466 sp<IBinder> binder = IInterface::asBinder(bufferProducer); 467 sp<Surface> surface = new Surface(bufferProducer, useAsync); 468 ANativeWindow *anw = surface.get(); 469 470 int width, height, format; 471 android_dataspace dataSpace; 472 473 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) { 474 String8 msg = String8::format("Camera %d: Failed to query Surface width: %s (%d)", 475 mCameraId, strerror(-err), err); 476 ALOGE("%s: %s", __FUNCTION__, msg.string()); 477 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 478 } 479 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) { 480 String8 msg = String8::format("Camera %d: Failed to query Surface height: %s (%d)", 481 mCameraId, strerror(-err), err); 482 ALOGE("%s: %s", __FUNCTION__, msg.string()); 483 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 484 } 485 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) { 486 String8 msg = String8::format("Camera %d: Failed to query Surface format: %s (%d)", 487 mCameraId, strerror(-err), err); 488 ALOGE("%s: %s", __FUNCTION__, msg.string()); 489 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 490 } 491 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE, 492 reinterpret_cast<int*>(&dataSpace))) != OK) { 493 String8 msg = String8::format("Camera %d: Failed to query Surface dataspace: %s (%d)", 494 mCameraId, strerror(-err), err); 495 ALOGE("%s: %s", __FUNCTION__, msg.string()); 496 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 497 } 498 499 // FIXME: remove this override since the default format should be 500 // IMPLEMENTATION_DEFINED. b/9487482 501 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 && 502 format <= HAL_PIXEL_FORMAT_BGRA_8888) { 503 ALOGW("%s: Camera %d: Overriding format %#x to IMPLEMENTATION_DEFINED", 504 __FUNCTION__, mCameraId, format); 505 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; 506 } 507 508 // Round dimensions to the nearest dimensions available for this format 509 if (flexibleConsumer && !CameraDeviceClient::roundBufferDimensionNearest(width, height, 510 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) { 511 String8 msg = String8::format("Camera %d: No supported stream configurations with " 512 "format %#x defined, failed to create output stream", mCameraId, format); 513 ALOGE("%s: %s", __FUNCTION__, msg.string()); 514 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string()); 515 } 516 517 int streamId = camera3::CAMERA3_STREAM_ID_INVALID; 518 err = mDevice->createStream(surface, width, height, format, dataSpace, 519 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()), 520 &streamId, outputConfiguration.getSurfaceSetID()); 521 522 if (err != OK) { 523 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 524 "Camera %d: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)", 525 mCameraId, width, height, format, dataSpace, strerror(-err), err); 526 } else { 527 mStreamMap.add(binder, streamId); 528 529 ALOGV("%s: Camera %d: Successfully created a new stream ID %d", 530 __FUNCTION__, mCameraId, streamId); 531 532 /** 533 * Set the stream transform flags to automatically 534 * rotate the camera stream for preview use cases. 535 */ 536 int32_t transform = 0; 537 err = getRotationTransformLocked(&transform); 538 539 if (err != OK) { 540 // Error logged by getRotationTransformLocked. 541 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, 542 "Unable to calculate rotation transform for new stream"); 543 } 544 545 err = mDevice->setStreamTransform(streamId, transform); 546 if (err != OK) { 547 String8 msg = String8::format("Failed to set stream transform (stream id %d)", 548 streamId); 549 ALOGE("%s: %s", __FUNCTION__, msg.string()); 550 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 551 } 552 553 *newStreamId = streamId; 554 } 555 556 return res; 557 } 558 559 560 binder::Status CameraDeviceClient::createInputStream( 561 int width, int height, int format, 562 /*out*/ 563 int32_t* newStreamId) { 564 565 ATRACE_CALL(); 566 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format); 567 568 binder::Status res; 569 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 570 571 Mutex::Autolock icl(mBinderSerializationLock); 572 573 if (!mDevice.get()) { 574 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 575 } 576 577 if (mInputStream.configured) { 578 String8 msg = String8::format("Camera %d: Already has an input stream " 579 "configured (ID %zd)", mCameraId, mInputStream.id); 580 ALOGE("%s: %s", __FUNCTION__, msg.string() ); 581 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string()); 582 } 583 584 int streamId = -1; 585 status_t err = mDevice->createInputStream(width, height, format, &streamId); 586 if (err == OK) { 587 mInputStream.configured = true; 588 mInputStream.width = width; 589 mInputStream.height = height; 590 mInputStream.format = format; 591 mInputStream.id = streamId; 592 593 ALOGV("%s: Camera %d: Successfully created a new input stream ID %d", 594 __FUNCTION__, mCameraId, streamId); 595 596 *newStreamId = streamId; 597 } else { 598 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 599 "Camera %d: Error creating new input stream: %s (%d)", mCameraId, 600 strerror(-err), err); 601 } 602 603 return res; 604 } 605 606 binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) { 607 608 binder::Status res; 609 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 610 611 if (inputSurface == NULL) { 612 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface"); 613 } 614 615 Mutex::Autolock icl(mBinderSerializationLock); 616 if (!mDevice.get()) { 617 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 618 } 619 sp<IGraphicBufferProducer> producer; 620 status_t err = mDevice->getInputBufferProducer(&producer); 621 if (err != OK) { 622 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 623 "Camera %d: Error getting input Surface: %s (%d)", 624 mCameraId, strerror(-err), err); 625 } else { 626 inputSurface->name = String16("CameraInput"); 627 inputSurface->graphicBufferProducer = producer; 628 } 629 return res; 630 } 631 632 bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height, 633 int32_t format, android_dataspace dataSpace, const CameraMetadata& info, 634 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) { 635 636 camera_metadata_ro_entry streamConfigs = 637 (dataSpace == HAL_DATASPACE_DEPTH) ? 638 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) : 639 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS); 640 641 int32_t bestWidth = -1; 642 int32_t bestHeight = -1; 643 644 // Iterate through listed stream configurations and find the one with the smallest euclidean 645 // distance from the given dimensions for the given format. 646 for (size_t i = 0; i < streamConfigs.count; i += 4) { 647 int32_t fmt = streamConfigs.data.i32[i]; 648 int32_t w = streamConfigs.data.i32[i + 1]; 649 int32_t h = streamConfigs.data.i32[i + 2]; 650 651 // Ignore input/output type for now 652 if (fmt == format) { 653 if (w == width && h == height) { 654 bestWidth = width; 655 bestHeight = height; 656 break; 657 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 || 658 CameraDeviceClient::euclidDistSquare(w, h, width, height) < 659 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) { 660 bestWidth = w; 661 bestHeight = h; 662 } 663 } 664 } 665 666 if (bestWidth == -1) { 667 // Return false if no configurations for this format were listed 668 return false; 669 } 670 671 // Set the outputs to the closet width/height 672 if (outWidth != NULL) { 673 *outWidth = bestWidth; 674 } 675 if (outHeight != NULL) { 676 *outHeight = bestHeight; 677 } 678 679 // Return true if at least one configuration for this format was listed 680 return true; 681 } 682 683 int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) { 684 int64_t d0 = x0 - x1; 685 int64_t d1 = y0 - y1; 686 return d0 * d0 + d1 * d1; 687 } 688 689 // Create a request object from a template. 690 binder::Status CameraDeviceClient::createDefaultRequest(int templateId, 691 /*out*/ 692 hardware::camera2::impl::CameraMetadataNative* request) 693 { 694 ATRACE_CALL(); 695 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId); 696 697 binder::Status res; 698 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 699 700 Mutex::Autolock icl(mBinderSerializationLock); 701 702 if (!mDevice.get()) { 703 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 704 } 705 706 CameraMetadata metadata; 707 status_t err; 708 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK && 709 request != NULL) { 710 711 request->swap(metadata); 712 } else if (err == BAD_VALUE) { 713 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, 714 "Camera %d: Template ID %d is invalid or not supported: %s (%d)", 715 mCameraId, templateId, strerror(-err), err); 716 717 } else { 718 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 719 "Camera %d: Error creating default request for template %d: %s (%d)", 720 mCameraId, templateId, strerror(-err), err); 721 } 722 return res; 723 } 724 725 binder::Status CameraDeviceClient::getCameraInfo( 726 /*out*/ 727 hardware::camera2::impl::CameraMetadataNative* info) 728 { 729 ATRACE_CALL(); 730 ALOGV("%s", __FUNCTION__); 731 732 binder::Status res; 733 734 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 735 736 Mutex::Autolock icl(mBinderSerializationLock); 737 738 if (!mDevice.get()) { 739 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 740 } 741 742 if (info != NULL) { 743 *info = mDevice->info(); // static camera metadata 744 // TODO: merge with device-specific camera metadata 745 } 746 747 return res; 748 } 749 750 binder::Status CameraDeviceClient::waitUntilIdle() 751 { 752 ATRACE_CALL(); 753 ALOGV("%s", __FUNCTION__); 754 755 binder::Status res; 756 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 757 758 Mutex::Autolock icl(mBinderSerializationLock); 759 760 if (!mDevice.get()) { 761 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 762 } 763 764 // FIXME: Also need check repeating burst. 765 Mutex::Autolock idLock(mStreamingRequestIdLock); 766 if (mStreamingRequestId != REQUEST_ID_NONE) { 767 String8 msg = String8::format( 768 "Camera %d: Try to waitUntilIdle when there are active streaming requests", 769 mCameraId); 770 ALOGE("%s: %s", __FUNCTION__, msg.string()); 771 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string()); 772 } 773 status_t err = mDevice->waitUntilDrained(); 774 if (err != OK) { 775 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 776 "Camera %d: Error waiting to drain: %s (%d)", 777 mCameraId, strerror(-err), err); 778 } 779 ALOGV("%s Done", __FUNCTION__); 780 return res; 781 } 782 783 binder::Status CameraDeviceClient::flush( 784 /*out*/ 785 int64_t* lastFrameNumber) { 786 ATRACE_CALL(); 787 ALOGV("%s", __FUNCTION__); 788 789 binder::Status res; 790 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 791 792 Mutex::Autolock icl(mBinderSerializationLock); 793 794 if (!mDevice.get()) { 795 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive"); 796 } 797 798 Mutex::Autolock idLock(mStreamingRequestIdLock); 799 mStreamingRequestId = REQUEST_ID_NONE; 800 status_t err = mDevice->flush(lastFrameNumber); 801 if (err != OK) { 802 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 803 "Camera %d: Error flushing device: %s (%d)", mCameraId, strerror(-err), err); 804 } 805 return res; 806 } 807 808 binder::Status CameraDeviceClient::prepare(int streamId) { 809 ATRACE_CALL(); 810 ALOGV("%s", __FUNCTION__); 811 812 binder::Status res; 813 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 814 815 Mutex::Autolock icl(mBinderSerializationLock); 816 817 // Guard against trying to prepare non-created streams 818 ssize_t index = NAME_NOT_FOUND; 819 for (size_t i = 0; i < mStreamMap.size(); ++i) { 820 if (streamId == mStreamMap.valueAt(i)) { 821 index = i; 822 break; 823 } 824 } 825 826 if (index == NAME_NOT_FOUND) { 827 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream " 828 "with that ID exists", mCameraId, streamId); 829 ALOGW("%s: %s", __FUNCTION__, msg.string()); 830 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string()); 831 } 832 833 // Also returns BAD_VALUE if stream ID was not valid, or stream already 834 // has been used 835 status_t err = mDevice->prepare(streamId); 836 if (err == BAD_VALUE) { 837 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, 838 "Camera %d: Stream %d has already been used, and cannot be prepared", 839 mCameraId, streamId); 840 } else if (err != OK) { 841 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 842 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId, 843 strerror(-err), err); 844 } 845 return res; 846 } 847 848 binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) { 849 ATRACE_CALL(); 850 ALOGV("%s", __FUNCTION__); 851 852 binder::Status res; 853 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 854 855 Mutex::Autolock icl(mBinderSerializationLock); 856 857 // Guard against trying to prepare non-created streams 858 ssize_t index = NAME_NOT_FOUND; 859 for (size_t i = 0; i < mStreamMap.size(); ++i) { 860 if (streamId == mStreamMap.valueAt(i)) { 861 index = i; 862 break; 863 } 864 } 865 866 if (index == NAME_NOT_FOUND) { 867 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream " 868 "with that ID exists", mCameraId, streamId); 869 ALOGW("%s: %s", __FUNCTION__, msg.string()); 870 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string()); 871 } 872 873 if (maxCount <= 0) { 874 String8 msg = String8::format("Camera %d: maxCount (%d) must be greater than 0", 875 mCameraId, maxCount); 876 ALOGE("%s: %s", __FUNCTION__, msg.string()); 877 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string()); 878 } 879 880 // Also returns BAD_VALUE if stream ID was not valid, or stream already 881 // has been used 882 status_t err = mDevice->prepare(maxCount, streamId); 883 if (err == BAD_VALUE) { 884 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, 885 "Camera %d: Stream %d has already been used, and cannot be prepared", 886 mCameraId, streamId); 887 } else if (err != OK) { 888 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 889 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId, 890 strerror(-err), err); 891 } 892 893 return res; 894 } 895 896 binder::Status CameraDeviceClient::tearDown(int streamId) { 897 ATRACE_CALL(); 898 ALOGV("%s", __FUNCTION__); 899 900 binder::Status res; 901 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res; 902 903 Mutex::Autolock icl(mBinderSerializationLock); 904 905 // Guard against trying to prepare non-created streams 906 ssize_t index = NAME_NOT_FOUND; 907 for (size_t i = 0; i < mStreamMap.size(); ++i) { 908 if (streamId == mStreamMap.valueAt(i)) { 909 index = i; 910 break; 911 } 912 } 913 914 if (index == NAME_NOT_FOUND) { 915 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream " 916 "with that ID exists", mCameraId, streamId); 917 ALOGW("%s: %s", __FUNCTION__, msg.string()); 918 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string()); 919 } 920 921 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in 922 // use 923 status_t err = mDevice->tearDown(streamId); 924 if (err == BAD_VALUE) { 925 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT, 926 "Camera %d: Stream %d is still in use, cannot be torn down", 927 mCameraId, streamId); 928 } else if (err != OK) { 929 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION, 930 "Camera %d: Error tearing down stream %d: %s (%d)", mCameraId, streamId, 931 strerror(-err), err); 932 } 933 934 return res; 935 } 936 937 status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) { 938 return BasicClient::dump(fd, args); 939 } 940 941 status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) { 942 String8 result; 943 result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n", 944 mCameraId, 945 (getRemoteCallback() != NULL ? 946 IInterface::asBinder(getRemoteCallback()).get() : NULL) ); 947 result.appendFormat(" Current client UID %u\n", mClientUid); 948 949 result.append(" State:\n"); 950 result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter); 951 if (mInputStream.configured) { 952 result.appendFormat(" Current input stream ID: %d\n", 953 mInputStream.id); 954 } else { 955 result.append(" No input stream configured.\n"); 956 } 957 if (!mStreamMap.isEmpty()) { 958 result.append(" Current output stream IDs:\n"); 959 for (size_t i = 0; i < mStreamMap.size(); i++) { 960 result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i)); 961 } 962 } else { 963 result.append(" No output streams configured.\n"); 964 } 965 write(fd, result.string(), result.size()); 966 // TODO: print dynamic/request section from most recent requests 967 mFrameProcessor->dump(fd, args); 968 969 return dumpDevice(fd, args); 970 } 971 972 void CameraDeviceClient::notifyError(int32_t errorCode, 973 const CaptureResultExtras& resultExtras) { 974 // Thread safe. Don't bother locking. 975 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); 976 977 if (remoteCb != 0) { 978 remoteCb->onDeviceError(errorCode, resultExtras); 979 } 980 } 981 982 void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) { 983 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); 984 985 if (remoteCb != 0) { 986 remoteCb->onRepeatingRequestError(lastFrameNumber); 987 } 988 989 Mutex::Autolock idLock(mStreamingRequestIdLock); 990 mStreamingRequestId = REQUEST_ID_NONE; 991 } 992 993 void CameraDeviceClient::notifyIdle() { 994 // Thread safe. Don't bother locking. 995 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); 996 997 if (remoteCb != 0) { 998 remoteCb->onDeviceIdle(); 999 } 1000 Camera2ClientBase::notifyIdle(); 1001 } 1002 1003 void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras, 1004 nsecs_t timestamp) { 1005 // Thread safe. Don't bother locking. 1006 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); 1007 if (remoteCb != 0) { 1008 remoteCb->onCaptureStarted(resultExtras, timestamp); 1009 } 1010 Camera2ClientBase::notifyShutter(resultExtras, timestamp); 1011 } 1012 1013 void CameraDeviceClient::notifyPrepared(int streamId) { 1014 // Thread safe. Don't bother locking. 1015 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback(); 1016 if (remoteCb != 0) { 1017 remoteCb->onPrepared(streamId); 1018 } 1019 } 1020 1021 void CameraDeviceClient::detachDevice() { 1022 if (mDevice == 0) return; 1023 1024 ALOGV("Camera %d: Stopping processors", mCameraId); 1025 1026 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID, 1027 FRAME_PROCESSOR_LISTENER_MAX_ID, 1028 /*listener*/this); 1029 mFrameProcessor->requestExit(); 1030 ALOGV("Camera %d: Waiting for threads", mCameraId); 1031 mFrameProcessor->join(); 1032 ALOGV("Camera %d: Disconnecting device", mCameraId); 1033 1034 // WORKAROUND: HAL refuses to disconnect while there's streams in flight 1035 { 1036 mDevice->clearStreamingRequest(); 1037 1038 status_t code; 1039 if ((code = mDevice->waitUntilDrained()) != OK) { 1040 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__, 1041 code); 1042 } 1043 } 1044 1045 Camera2ClientBase::detachDevice(); 1046 } 1047 1048 /** Device-related methods */ 1049 void CameraDeviceClient::onResultAvailable(const CaptureResult& result) { 1050 ATRACE_CALL(); 1051 ALOGV("%s", __FUNCTION__); 1052 1053 // Thread-safe. No lock necessary. 1054 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback; 1055 if (remoteCb != NULL) { 1056 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras); 1057 } 1058 } 1059 1060 binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) { 1061 if (mDisconnected) { 1062 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, 1063 "The camera device has been disconnected"); 1064 } 1065 status_t res = checkPid(checkLocation); 1066 return (res == OK) ? binder::Status::ok() : 1067 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED, 1068 "Attempt to use camera from a different process than original client"); 1069 } 1070 1071 // TODO: move to Camera2ClientBase 1072 bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) { 1073 1074 const int pid = IPCThreadState::self()->getCallingPid(); 1075 const int selfPid = getpid(); 1076 camera_metadata_entry_t entry; 1077 1078 /** 1079 * Mixin default important security values 1080 * - android.led.transmit = defaulted ON 1081 */ 1082 CameraMetadata staticInfo = mDevice->info(); 1083 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS); 1084 for(size_t i = 0; i < entry.count; ++i) { 1085 uint8_t led = entry.data.u8[i]; 1086 1087 switch(led) { 1088 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: { 1089 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON; 1090 if (!metadata.exists(ANDROID_LED_TRANSMIT)) { 1091 metadata.update(ANDROID_LED_TRANSMIT, 1092 &transmitDefault, 1); 1093 } 1094 break; 1095 } 1096 } 1097 } 1098 1099 // We can do anything! 1100 if (pid == selfPid) { 1101 return true; 1102 } 1103 1104 /** 1105 * Permission check special fields in the request 1106 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT 1107 */ 1108 entry = metadata.find(ANDROID_LED_TRANSMIT); 1109 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) { 1110 String16 permissionString = 1111 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED"); 1112 if (!checkCallingPermission(permissionString)) { 1113 const int uid = IPCThreadState::self()->getCallingUid(); 1114 ALOGE("Permission Denial: " 1115 "can't disable transmit LED pid=%d, uid=%d", pid, uid); 1116 return false; 1117 } 1118 } 1119 1120 return true; 1121 } 1122 1123 status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) { 1124 ALOGV("%s: begin", __FUNCTION__); 1125 1126 const CameraMetadata& staticInfo = mDevice->info(); 1127 return CameraUtils::getRotationTransform(staticInfo, transform); 1128 } 1129 1130 } // namespace android 1131