CameraDeviceClient.cpp revision d1d6467d3bcbc1305eeba0176a2edf04925c368e
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 27#include "common/CameraDeviceBase.h" 28#include "api2/CameraDeviceClient.h" 29 30 31 32namespace android { 33using namespace camera2; 34 35CameraDeviceClientBase::CameraDeviceClientBase( 36 const sp<CameraService>& cameraService, 37 const sp<ICameraDeviceCallbacks>& remoteCallback, 38 const String16& clientPackageName, 39 int cameraId, 40 int cameraFacing, 41 int clientPid, 42 uid_t clientUid, 43 int servicePid) : 44 BasicClient(cameraService, remoteCallback->asBinder(), clientPackageName, 45 cameraId, cameraFacing, clientPid, clientUid, servicePid), 46 mRemoteCallback(remoteCallback) { 47} 48void CameraDeviceClientBase::notifyError() { 49 // Thread safe. Don't bother locking. 50 sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback; 51 52 if (remoteCb != 0) { 53 remoteCb->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0); 54 } 55} 56 57// Interface used by CameraService 58 59CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService, 60 const sp<ICameraDeviceCallbacks>& remoteCallback, 61 const String16& clientPackageName, 62 int cameraId, 63 int cameraFacing, 64 int clientPid, 65 uid_t clientUid, 66 int servicePid) : 67 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, 68 cameraId, cameraFacing, clientPid, clientUid, servicePid), 69 mRequestIdCounter(0) { 70 71 ATRACE_CALL(); 72 ALOGI("CameraDeviceClient %d: Opened", cameraId); 73} 74 75status_t CameraDeviceClient::initialize(camera_module_t *module) 76{ 77 ATRACE_CALL(); 78 status_t res; 79 80 res = Camera2ClientBase::initialize(module); 81 if (res != OK) { 82 return res; 83 } 84 85 String8 threadName; 86 mFrameProcessor = new FrameProcessorBase(mDevice); 87 threadName = String8::format("CDU-%d-FrameProc", mCameraId); 88 mFrameProcessor->run(threadName.string()); 89 90 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID, 91 FRAME_PROCESSOR_LISTENER_MAX_ID, 92 /*listener*/this); 93 94 return OK; 95} 96 97CameraDeviceClient::~CameraDeviceClient() { 98} 99 100status_t CameraDeviceClient::submitRequest(sp<CaptureRequest> request, 101 bool streaming) { 102 ATRACE_CALL(); 103 ALOGV("%s", __FUNCTION__); 104 105 status_t res; 106 107 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 108 109 Mutex::Autolock icl(mBinderSerializationLock); 110 111 if (!mDevice.get()) return DEAD_OBJECT; 112 113 if (request == 0) { 114 ALOGE("%s: Camera %d: Sent null request. Rejecting request.", 115 __FUNCTION__, mCameraId); 116 return BAD_VALUE; 117 } 118 119 CameraMetadata metadata(request->mMetadata); 120 121 if (metadata.isEmpty()) { 122 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.", 123 __FUNCTION__, mCameraId); 124 return BAD_VALUE; 125 } else if (request->mSurfaceList.size() == 0) { 126 ALOGE("%s: Camera %d: Requests must have at least one surface target. " 127 "Rejecting request.", __FUNCTION__, mCameraId); 128 return BAD_VALUE; 129 } 130 131 if (!enforceRequestPermissions(metadata)) { 132 // Callee logs 133 return PERMISSION_DENIED; 134 } 135 136 /** 137 * Write in the output stream IDs which we calculate from 138 * the capture request's list of surface targets 139 */ 140 Vector<int32_t> outputStreamIds; 141 outputStreamIds.setCapacity(request->mSurfaceList.size()); 142 for (size_t i = 0; i < request->mSurfaceList.size(); ++i) { 143 sp<Surface> surface = request->mSurfaceList[i]; 144 145 if (surface == 0) continue; 146 147 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer(); 148 int idx = mStreamMap.indexOfKey(gbp->asBinder()); 149 150 // Trying to submit request with surface that wasn't created 151 if (idx == NAME_NOT_FOUND) { 152 ALOGE("%s: Camera %d: Tried to submit a request with a surface that" 153 " we have not called createStream on", 154 __FUNCTION__, mCameraId); 155 return BAD_VALUE; 156 } 157 158 int streamId = mStreamMap.valueAt(idx); 159 outputStreamIds.push_back(streamId); 160 ALOGV("%s: Camera %d: Appending output stream %d to request", 161 __FUNCTION__, mCameraId, streamId); 162 } 163 164 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0], 165 outputStreamIds.size()); 166 167 // TODO: @hide ANDROID_REQUEST_ID, or use another request token 168 int32_t requestId = mRequestIdCounter++; 169 metadata.update(ANDROID_REQUEST_ID, &requestId, /*size*/1); 170 ALOGV("%s: Camera %d: Submitting request with ID %d", 171 __FUNCTION__, mCameraId, requestId); 172 173 if (streaming) { 174 res = mDevice->setStreamingRequest(metadata); 175 if (res != OK) { 176 ALOGE("%s: Camera %d: Got error %d after trying to set streaming " 177 "request", __FUNCTION__, mCameraId, res); 178 } else { 179 mStreamingRequestList.push_back(requestId); 180 } 181 } else { 182 res = mDevice->capture(metadata); 183 if (res != OK) { 184 ALOGE("%s: Camera %d: Got error %d after trying to set capture", 185 __FUNCTION__, mCameraId, res); 186 } 187 } 188 189 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId); 190 if (res == OK) { 191 return requestId; 192 } 193 194 return res; 195} 196 197status_t CameraDeviceClient::cancelRequest(int requestId) { 198 ATRACE_CALL(); 199 ALOGV("%s, requestId = %d", __FUNCTION__, requestId); 200 201 status_t res; 202 203 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 204 205 Mutex::Autolock icl(mBinderSerializationLock); 206 207 if (!mDevice.get()) return DEAD_OBJECT; 208 209 Vector<int>::iterator it, end; 210 for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end(); 211 it != end; ++it) { 212 if (*it == requestId) { 213 break; 214 } 215 } 216 217 if (it == end) { 218 ALOGE("%s: Camera%d: Did not find request id %d in list of streaming " 219 "requests", __FUNCTION__, mCameraId, requestId); 220 return BAD_VALUE; 221 } 222 223 res = mDevice->clearStreamingRequest(); 224 225 if (res == OK) { 226 ALOGV("%s: Camera %d: Successfully cleared streaming request", 227 __FUNCTION__, mCameraId); 228 mStreamingRequestList.erase(it); 229 } 230 231 return res; 232} 233 234status_t CameraDeviceClient::deleteStream(int streamId) { 235 ATRACE_CALL(); 236 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId); 237 238 status_t res; 239 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 240 241 Mutex::Autolock icl(mBinderSerializationLock); 242 243 if (!mDevice.get()) return DEAD_OBJECT; 244 245 // Guard against trying to delete non-created streams 246 ssize_t index = NAME_NOT_FOUND; 247 for (size_t i = 0; i < mStreamMap.size(); ++i) { 248 if (streamId == mStreamMap.valueAt(i)) { 249 index = i; 250 break; 251 } 252 } 253 254 if (index == NAME_NOT_FOUND) { 255 ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream " 256 "created yet", __FUNCTION__, mCameraId, streamId); 257 return BAD_VALUE; 258 } 259 260 // Also returns BAD_VALUE if stream ID was not valid 261 res = mDevice->deleteStream(streamId); 262 263 if (res == BAD_VALUE) { 264 ALOGE("%s: Camera %d: Unexpected BAD_VALUE when deleting stream, but we" 265 " already checked and the stream ID (%d) should be valid.", 266 __FUNCTION__, mCameraId, streamId); 267 } else if (res == OK) { 268 mStreamMap.removeItemsAt(index); 269 270 ALOGV("%s: Camera %d: Successfully deleted stream ID (%d)", 271 __FUNCTION__, mCameraId, streamId); 272 } 273 274 return res; 275} 276 277status_t CameraDeviceClient::createStream(int width, int height, int format, 278 const sp<IGraphicBufferProducer>& bufferProducer) 279{ 280 ATRACE_CALL(); 281 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format); 282 283 status_t res; 284 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 285 286 Mutex::Autolock icl(mBinderSerializationLock); 287 288 if (!mDevice.get()) return DEAD_OBJECT; 289 290 // Don't create multiple streams for the same target surface 291 { 292 ssize_t index = mStreamMap.indexOfKey(bufferProducer->asBinder()); 293 if (index != NAME_NOT_FOUND) { 294 ALOGW("%s: Camera %d: Buffer producer already has a stream for it " 295 "(ID %d)", 296 __FUNCTION__, mCameraId, index); 297 return ALREADY_EXISTS; 298 } 299 } 300 301 sp<IBinder> binder; 302 sp<ANativeWindow> anw; 303 if (bufferProducer != 0) { 304 binder = bufferProducer->asBinder(); 305 anw = new Surface(bufferProducer); 306 } 307 308 // TODO: remove w,h,f since we are ignoring them 309 310 if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) { 311 ALOGE("%s: Camera %d: Failed to query Surface width", __FUNCTION__, 312 mCameraId); 313 return res; 314 } 315 if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) { 316 ALOGE("%s: Camera %d: Failed to query Surface height", __FUNCTION__, 317 mCameraId); 318 return res; 319 } 320 if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &format)) != OK) { 321 ALOGE("%s: Camera %d: Failed to query Surface format", __FUNCTION__, 322 mCameraId); 323 return res; 324 } 325 326 // FIXME: remove this override since the default format should be 327 // IMPLEMENTATION_DEFINED. b/9487482 328 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 && 329 format <= HAL_PIXEL_FORMAT_BGRA_8888) { 330 ALOGW("%s: Camera %d: Overriding format 0x%x to IMPLEMENTATION_DEFINED", 331 __FUNCTION__, mCameraId, format); 332 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED; 333 } 334 335 // TODO: add startConfigure/stopConfigure call to CameraDeviceBase 336 // this will make it so Camera3Device doesn't call configure_streams 337 // after each call, but only once we are done with all. 338 339 int streamId = -1; 340 if (format == HAL_PIXEL_FORMAT_BLOB) { 341 // JPEG buffers need to be sized for maximum possible compressed size 342 CameraMetadata staticInfo = mDevice->info(); 343 camera_metadata_entry_t entry = staticInfo.find(ANDROID_JPEG_MAX_SIZE); 344 if (entry.count == 0) { 345 ALOGE("%s: Camera %d: Can't find maximum JPEG size in " 346 "static metadata!", __FUNCTION__, mCameraId); 347 return INVALID_OPERATION; 348 } 349 int32_t maxJpegSize = entry.data.i32[0]; 350 res = mDevice->createStream(anw, width, height, format, maxJpegSize, 351 &streamId); 352 } else { 353 // All other streams are a known size 354 res = mDevice->createStream(anw, width, height, format, /*size*/0, 355 &streamId); 356 } 357 358 if (res == OK) { 359 mStreamMap.add(bufferProducer->asBinder(), streamId); 360 361 ALOGV("%s: Camera %d: Successfully created a new stream ID %d", 362 __FUNCTION__, mCameraId, streamId); 363 return streamId; 364 } 365 366 return res; 367} 368 369// Create a request object from a template. 370status_t CameraDeviceClient::createDefaultRequest(int templateId, 371 /*out*/ 372 CameraMetadata* request) 373{ 374 ATRACE_CALL(); 375 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId); 376 377 status_t res; 378 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 379 380 Mutex::Autolock icl(mBinderSerializationLock); 381 382 if (!mDevice.get()) return DEAD_OBJECT; 383 384 CameraMetadata metadata; 385 if ( (res = mDevice->createDefaultRequest(templateId, &metadata) ) == OK && 386 request != NULL) { 387 388 request->swap(metadata); 389 } 390 391 return res; 392} 393 394status_t CameraDeviceClient::getCameraInfo(/*out*/CameraMetadata* info) 395{ 396 ATRACE_CALL(); 397 ALOGV("%s", __FUNCTION__); 398 399 status_t res = OK; 400 401 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 402 403 Mutex::Autolock icl(mBinderSerializationLock); 404 405 if (!mDevice.get()) return DEAD_OBJECT; 406 407 if (info != NULL) { 408 *info = mDevice->info(); // static camera metadata 409 // TODO: merge with device-specific camera metadata 410 } 411 412 return res; 413} 414 415status_t CameraDeviceClient::waitUntilIdle() 416{ 417 ATRACE_CALL(); 418 ALOGV("%s", __FUNCTION__); 419 420 status_t res = OK; 421 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 422 423 Mutex::Autolock icl(mBinderSerializationLock); 424 425 if (!mDevice.get()) return DEAD_OBJECT; 426 427 // FIXME: Also need check repeating burst. 428 if (!mStreamingRequestList.isEmpty()) { 429 ALOGE("%s: Camera %d: Try to waitUntilIdle when there are active streaming requests", 430 __FUNCTION__, mCameraId); 431 return INVALID_OPERATION; 432 } 433 res = mDevice->waitUntilDrained(); 434 ALOGV("%s Done", __FUNCTION__); 435 436 return res; 437} 438 439status_t CameraDeviceClient::flush() { 440 ATRACE_CALL(); 441 ALOGV("%s", __FUNCTION__); 442 443 status_t res = OK; 444 if ( (res = checkPid(__FUNCTION__) ) != OK) return res; 445 446 Mutex::Autolock icl(mBinderSerializationLock); 447 448 if (!mDevice.get()) return DEAD_OBJECT; 449 450 return mDevice->flush(); 451} 452 453status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) { 454 String8 result; 455 result.appendFormat("CameraDeviceClient[%d] (%p) PID: %d, dump:\n", 456 mCameraId, 457 getRemoteCallback()->asBinder().get(), 458 mClientPid); 459 result.append(" State: "); 460 461 // TODO: print dynamic/request section from most recent requests 462 mFrameProcessor->dump(fd, args); 463 464 return dumpDevice(fd, args); 465} 466 467// TODO: refactor the code below this with IProCameraUser. 468// it's 100% copy-pasted, so lets not change it right now to make it easier. 469 470void CameraDeviceClient::detachDevice() { 471 if (mDevice == 0) return; 472 473 ALOGV("Camera %d: Stopping processors", mCameraId); 474 475 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID, 476 FRAME_PROCESSOR_LISTENER_MAX_ID, 477 /*listener*/this); 478 mFrameProcessor->requestExit(); 479 ALOGV("Camera %d: Waiting for threads", mCameraId); 480 mFrameProcessor->join(); 481 ALOGV("Camera %d: Disconnecting device", mCameraId); 482 483 // WORKAROUND: HAL refuses to disconnect while there's streams in flight 484 { 485 mDevice->clearStreamingRequest(); 486 487 status_t code; 488 if ((code = mDevice->waitUntilDrained()) != OK) { 489 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__, 490 code); 491 } 492 } 493 494 Camera2ClientBase::detachDevice(); 495} 496 497/** Device-related methods */ 498void CameraDeviceClient::onFrameAvailable(int32_t frameId, 499 const CameraMetadata& frame) { 500 ATRACE_CALL(); 501 ALOGV("%s", __FUNCTION__); 502 503 // Thread-safe. No lock necessary. 504 sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback; 505 if (remoteCb != NULL) { 506 ALOGV("%s: frame = %p ", __FUNCTION__, &frame); 507 remoteCb->onResultReceived(frameId, frame); 508 } 509} 510 511// TODO: move to Camera2ClientBase 512bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) { 513 514 const int pid = IPCThreadState::self()->getCallingPid(); 515 const int selfPid = getpid(); 516 camera_metadata_entry_t entry; 517 518 /** 519 * Mixin default important security values 520 * - android.led.transmit = defaulted ON 521 */ 522 CameraMetadata staticInfo = mDevice->info(); 523 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS); 524 for(size_t i = 0; i < entry.count; ++i) { 525 uint8_t led = entry.data.u8[i]; 526 527 switch(led) { 528 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: { 529 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON; 530 if (!metadata.exists(ANDROID_LED_TRANSMIT)) { 531 metadata.update(ANDROID_LED_TRANSMIT, 532 &transmitDefault, 1); 533 } 534 break; 535 } 536 } 537 } 538 539 // We can do anything! 540 if (pid == selfPid) { 541 return true; 542 } 543 544 /** 545 * Permission check special fields in the request 546 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT 547 */ 548 entry = metadata.find(ANDROID_LED_TRANSMIT); 549 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) { 550 String16 permissionString = 551 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED"); 552 if (!checkCallingPermission(permissionString)) { 553 const int uid = IPCThreadState::self()->getCallingUid(); 554 ALOGE("Permission Denial: " 555 "can't disable transmit LED pid=%d, uid=%d", pid, uid); 556 return false; 557 } 558 } 559 560 return true; 561} 562 563} // namespace android 564