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