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
CameraDeviceClientBase(const sp<CameraService> & cameraService,const sp<hardware::camera2::ICameraDeviceCallbacks> & remoteCallback,const String16 & clientPackageName,const String8 & cameraId,int cameraFacing,int clientPid,uid_t clientUid,int servicePid)45 CameraDeviceClientBase::CameraDeviceClientBase(
46 const sp<CameraService>& cameraService,
47 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
48 const String16& clientPackageName,
49 const String8& 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
CameraDeviceClient(const sp<CameraService> & cameraService,const sp<hardware::camera2::ICameraDeviceCallbacks> & remoteCallback,const String16 & clientPackageName,const String8 & cameraId,int cameraFacing,int clientPid,uid_t clientUid,int servicePid)67 CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
68 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
69 const String16& clientPackageName,
70 const String8& 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 %s: Opened", cameraId.string());
83 }
84
initialize(sp<CameraProviderManager> manager)85 status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager) {
86 return initializeImpl(manager);
87 }
88
89 template<typename TProviderPtr>
initializeImpl(TProviderPtr providerPtr)90 status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr) {
91 ATRACE_CALL();
92 status_t res;
93
94 res = Camera2ClientBase::initialize(providerPtr);
95 if (res != OK) {
96 return res;
97 }
98
99 String8 threadName;
100 mFrameProcessor = new FrameProcessorBase(mDevice);
101 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
102 mFrameProcessor->run(threadName.string());
103
104 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
105 FRAME_PROCESSOR_LISTENER_MAX_ID,
106 /*listener*/this,
107 /*sendPartials*/true);
108
109 return OK;
110 }
111
~CameraDeviceClient()112 CameraDeviceClient::~CameraDeviceClient() {
113 }
114
submitRequest(const hardware::camera2::CaptureRequest & request,bool streaming,hardware::camera2::utils::SubmitInfo * submitInfo)115 binder::Status CameraDeviceClient::submitRequest(
116 const hardware::camera2::CaptureRequest& request,
117 bool streaming,
118 /*out*/
119 hardware::camera2::utils::SubmitInfo *submitInfo) {
120 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
121 return submitRequestList(requestList, streaming, submitInfo);
122 }
123
submitRequestList(const std::vector<hardware::camera2::CaptureRequest> & requests,bool streaming,hardware::camera2::utils::SubmitInfo * submitInfo)124 binder::Status CameraDeviceClient::submitRequestList(
125 const std::vector<hardware::camera2::CaptureRequest>& requests,
126 bool streaming,
127 /*out*/
128 hardware::camera2::utils::SubmitInfo *submitInfo) {
129 ATRACE_CALL();
130 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
131
132 binder::Status res = binder::Status::ok();
133 status_t err;
134 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
135 return res;
136 }
137
138 Mutex::Autolock icl(mBinderSerializationLock);
139
140 if (!mDevice.get()) {
141 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
142 }
143
144 if (requests.empty()) {
145 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
146 __FUNCTION__, mCameraIdStr.string());
147 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
148 }
149
150 List<const CameraMetadata> metadataRequestList;
151 std::list<const SurfaceMap> surfaceMapList;
152 submitInfo->mRequestId = mRequestIdCounter;
153 uint32_t loopCounter = 0;
154
155 for (auto&& request: requests) {
156 if (request.mIsReprocess) {
157 if (!mInputStream.configured) {
158 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
159 mCameraIdStr.string());
160 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
161 "No input configured for camera %s but request is for reprocessing",
162 mCameraIdStr.string());
163 } else if (streaming) {
164 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
165 mCameraIdStr.string());
166 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
167 "Repeating reprocess requests not supported");
168 }
169 }
170
171 CameraMetadata metadata(request.mMetadata);
172 if (metadata.isEmpty()) {
173 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
174 __FUNCTION__, mCameraIdStr.string());
175 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
176 "Request settings are empty");
177 } else if (request.mSurfaceList.isEmpty()) {
178 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
179 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
180 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
181 "Request has no output targets");
182 }
183
184 if (!enforceRequestPermissions(metadata)) {
185 // Callee logs
186 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
187 "Caller does not have permission to change restricted controls");
188 }
189
190 /**
191 * Write in the output stream IDs and map from stream ID to surface ID
192 * which we calculate from the capture request's list of surface target
193 */
194 SurfaceMap surfaceMap;
195 Vector<int32_t> outputStreamIds;
196 for (sp<Surface> surface : request.mSurfaceList) {
197 if (surface == 0) continue;
198
199 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
200 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
201
202 // Trying to submit request with surface that wasn't created
203 if (idx == NAME_NOT_FOUND) {
204 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
205 " we have not called createStream on",
206 __FUNCTION__, mCameraIdStr.string());
207 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
208 "Request targets Surface that is not part of current capture session");
209 }
210
211 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
212 if (surfaceMap.find(streamSurfaceId.streamId()) == surfaceMap.end()) {
213 surfaceMap[streamSurfaceId.streamId()] = std::vector<size_t>();
214 outputStreamIds.push_back(streamSurfaceId.streamId());
215 }
216 surfaceMap[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
217
218 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
219 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
220 streamSurfaceId.surfaceId());
221 }
222
223 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
224 outputStreamIds.size());
225
226 if (request.mIsReprocess) {
227 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
228 }
229
230 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1);
231 loopCounter++; // loopCounter starts from 1
232 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
233 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
234 loopCounter, requests.size());
235
236 metadataRequestList.push_back(metadata);
237 surfaceMapList.push_back(surfaceMap);
238 }
239 mRequestIdCounter++;
240
241 if (streaming) {
242 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
243 &(submitInfo->mLastFrameNumber));
244 if (err != OK) {
245 String8 msg = String8::format(
246 "Camera %s: Got error %s (%d) after trying to set streaming request",
247 mCameraIdStr.string(), strerror(-err), err);
248 ALOGE("%s: %s", __FUNCTION__, msg.string());
249 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
250 msg.string());
251 } else {
252 Mutex::Autolock idLock(mStreamingRequestIdLock);
253 mStreamingRequestId = submitInfo->mRequestId;
254 }
255 } else {
256 err = mDevice->captureList(metadataRequestList, surfaceMapList,
257 &(submitInfo->mLastFrameNumber));
258 if (err != OK) {
259 String8 msg = String8::format(
260 "Camera %s: Got error %s (%d) after trying to submit capture request",
261 mCameraIdStr.string(), strerror(-err), err);
262 ALOGE("%s: %s", __FUNCTION__, msg.string());
263 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
264 msg.string());
265 }
266 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
267 }
268
269 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
270 return res;
271 }
272
cancelRequest(int requestId,int64_t * lastFrameNumber)273 binder::Status CameraDeviceClient::cancelRequest(
274 int requestId,
275 /*out*/
276 int64_t* lastFrameNumber) {
277 ATRACE_CALL();
278 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
279
280 status_t err;
281 binder::Status res;
282
283 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
284
285 Mutex::Autolock icl(mBinderSerializationLock);
286
287 if (!mDevice.get()) {
288 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
289 }
290
291 Mutex::Autolock idLock(mStreamingRequestIdLock);
292 if (mStreamingRequestId != requestId) {
293 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
294 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
295 ALOGE("%s: %s", __FUNCTION__, msg.string());
296 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
297 }
298
299 err = mDevice->clearStreamingRequest(lastFrameNumber);
300
301 if (err == OK) {
302 ALOGV("%s: Camera %s: Successfully cleared streaming request",
303 __FUNCTION__, mCameraIdStr.string());
304 mStreamingRequestId = REQUEST_ID_NONE;
305 } else {
306 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
307 "Camera %s: Error clearing streaming request: %s (%d)",
308 mCameraIdStr.string(), strerror(-err), err);
309 }
310
311 return res;
312 }
313
beginConfigure()314 binder::Status CameraDeviceClient::beginConfigure() {
315 // TODO: Implement this.
316 ALOGV("%s: Not implemented yet.", __FUNCTION__);
317 return binder::Status::ok();
318 }
319
endConfigure(int operatingMode)320 binder::Status CameraDeviceClient::endConfigure(int operatingMode) {
321 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
322 __FUNCTION__, mInputStream.configured ? 1 : 0,
323 mStreamMap.size());
324
325 binder::Status res;
326 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
327
328 Mutex::Autolock icl(mBinderSerializationLock);
329
330 if (!mDevice.get()) {
331 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
332 }
333
334 if (operatingMode < 0) {
335 String8 msg = String8::format(
336 "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
337 ALOGE("%s: %s", __FUNCTION__, msg.string());
338 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
339 msg.string());
340 }
341
342 // Sanitize the high speed session against necessary capability bit.
343 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
344 if (isConstrainedHighSpeed) {
345 CameraMetadata staticInfo = mDevice->info();
346 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
347 bool isConstrainedHighSpeedSupported = false;
348 for(size_t i = 0; i < entry.count; ++i) {
349 uint8_t capability = entry.data.u8[i];
350 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
351 isConstrainedHighSpeedSupported = true;
352 break;
353 }
354 }
355 if (!isConstrainedHighSpeedSupported) {
356 String8 msg = String8::format(
357 "Camera %s: Try to create a constrained high speed configuration on a device"
358 " that doesn't support it.", mCameraIdStr.string());
359 ALOGE("%s: %s", __FUNCTION__, msg.string());
360 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
361 msg.string());
362 }
363 }
364
365 status_t err = mDevice->configureStreams(operatingMode);
366 if (err == BAD_VALUE) {
367 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
368 mCameraIdStr.string());
369 ALOGE("%s: %s", __FUNCTION__, msg.string());
370 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
371 } else if (err != OK) {
372 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
373 mCameraIdStr.string(), strerror(-err), err);
374 ALOGE("%s: %s", __FUNCTION__, msg.string());
375 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
376 }
377
378 return res;
379 }
380
deleteStream(int streamId)381 binder::Status CameraDeviceClient::deleteStream(int streamId) {
382 ATRACE_CALL();
383 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
384
385 binder::Status res;
386 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
387
388 Mutex::Autolock icl(mBinderSerializationLock);
389
390 if (!mDevice.get()) {
391 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
392 }
393
394 bool isInput = false;
395 std::vector<sp<IBinder>> surfaces;
396 ssize_t dIndex = NAME_NOT_FOUND;
397
398 if (mInputStream.configured && mInputStream.id == streamId) {
399 isInput = true;
400 } else {
401 // Guard against trying to delete non-created streams
402 for (size_t i = 0; i < mStreamMap.size(); ++i) {
403 if (streamId == mStreamMap.valueAt(i).streamId()) {
404 surfaces.push_back(mStreamMap.keyAt(i));
405 }
406 }
407
408 // See if this stream is one of the deferred streams.
409 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
410 if (streamId == mDeferredStreams[i]) {
411 dIndex = i;
412 break;
413 }
414 }
415
416 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
417 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
418 " stream created yet", mCameraIdStr.string(), streamId);
419 ALOGW("%s: %s", __FUNCTION__, msg.string());
420 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
421 }
422 }
423
424 // Also returns BAD_VALUE if stream ID was not valid
425 status_t err = mDevice->deleteStream(streamId);
426
427 if (err != OK) {
428 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
429 mCameraIdStr.string(), strerror(-err), err, streamId);
430 ALOGE("%s: %s", __FUNCTION__, msg.string());
431 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
432 } else {
433 if (isInput) {
434 mInputStream.configured = false;
435 } else {
436 for (auto& surface : surfaces) {
437 mStreamMap.removeItem(surface);
438 }
439
440 if (dIndex != NAME_NOT_FOUND) {
441 mDeferredStreams.removeItemsAt(dIndex);
442 }
443 }
444 }
445
446 return res;
447 }
448
createStream(const hardware::camera2::params::OutputConfiguration & outputConfiguration,int32_t * newStreamId)449 binder::Status CameraDeviceClient::createStream(
450 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
451 /*out*/
452 int32_t* newStreamId) {
453 ATRACE_CALL();
454
455 binder::Status res;
456 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
457
458 Mutex::Autolock icl(mBinderSerializationLock);
459
460 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
461 outputConfiguration.getGraphicBufferProducers();
462 size_t numBufferProducers = bufferProducers.size();
463 bool deferredConsumer = outputConfiguration.isDeferred();
464 bool isShared = outputConfiguration.isShared();
465
466 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
467 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
468 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
469 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
470 }
471 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
472 int surfaceType = outputConfiguration.getSurfaceType();
473 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
474 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
475
476 if (deferredConsumer && !validSurfaceType) {
477 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
478 __FUNCTION__, bufferProducers[0].get(), surfaceType);
479 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
480 }
481
482 if (!mDevice.get()) {
483 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
484 }
485
486 std::vector<sp<Surface>> surfaces;
487 std::vector<sp<IBinder>> binders;
488 status_t err;
489
490 // Create stream for deferred surface case.
491 if (deferredConsumerOnly) {
492 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
493 }
494
495 OutputStreamInfo streamInfo;
496 bool isStreamInfoValid = false;
497 for (auto& bufferProducer : bufferProducers) {
498 // Don't create multiple streams for the same target surface
499 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
500 ssize_t index = mStreamMap.indexOfKey(binder);
501 if (index != NAME_NOT_FOUND) {
502 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
503 "(ID %zd)", mCameraIdStr.string(), index);
504 ALOGW("%s: %s", __FUNCTION__, msg.string());
505 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
506 }
507
508 sp<Surface> surface;
509 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
510
511 if (!res.isOk())
512 return res;
513
514 if (!isStreamInfoValid) {
515 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
516 // formats.
517 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
518 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
519 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
520 ALOGW("%s: %s", __FUNCTION__, msg.string());
521 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
522 }
523 isStreamInfoValid = true;
524 }
525
526 binders.push_back(IInterface::asBinder(bufferProducer));
527 surfaces.push_back(surface);
528 }
529
530 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
531 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
532 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
533 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
534 &streamId, outputConfiguration.getSurfaceSetID(), isShared);
535
536 if (err != OK) {
537 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
538 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
539 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
540 streamInfo.dataSpace, strerror(-err), err);
541 } else {
542 int i = 0;
543 for (auto& binder : binders) {
544 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
545 __FUNCTION__, binder.get(), streamId, i);
546 mStreamMap.add(binder, StreamSurfaceId(streamId, i++));
547 }
548
549 mStreamInfoMap[streamId] = streamInfo;
550
551 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
552 " (%d x %d) with format 0x%x.",
553 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
554 streamInfo.height, streamInfo.format);
555
556 // Set transform flags to ensure preview to be rotated correctly.
557 res = setStreamTransformLocked(streamId);
558
559 *newStreamId = streamId;
560 }
561
562 return res;
563 }
564
createDeferredSurfaceStreamLocked(const hardware::camera2::params::OutputConfiguration & outputConfiguration,bool isShared,int * newStreamId)565 binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
566 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
567 bool isShared,
568 /*out*/
569 int* newStreamId) {
570 int width, height, format, surfaceType;
571 int32_t consumerUsage;
572 android_dataspace dataSpace;
573 status_t err;
574 binder::Status res;
575
576 if (!mDevice.get()) {
577 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
578 }
579
580 // Infer the surface info for deferred surface stream creation.
581 width = outputConfiguration.getWidth();
582 height = outputConfiguration.getHeight();
583 surfaceType = outputConfiguration.getSurfaceType();
584 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
585 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
586 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
587 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
588 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
589 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
590 }
591 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
592 std::vector<sp<Surface>> noSurface;
593 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
594 height, format, dataSpace,
595 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
596 &streamId, outputConfiguration.getSurfaceSetID(), isShared, consumerUsage);
597
598 if (err != OK) {
599 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
600 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
601 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
602 } else {
603 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
604 // a separate list to track. Once the deferred surface is set, this id will be
605 // relocated to mStreamMap.
606 mDeferredStreams.push_back(streamId);
607
608 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
609 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
610
611 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
612 " (%d x %d) stream with format 0x%x.",
613 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
614
615 // Set transform flags to ensure preview to be rotated correctly.
616 res = setStreamTransformLocked(streamId);
617
618 *newStreamId = streamId;
619 }
620 return res;
621 }
622
setStreamTransformLocked(int streamId)623 binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
624 int32_t transform = 0;
625 status_t err;
626 binder::Status res;
627
628 if (!mDevice.get()) {
629 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
630 }
631
632 err = getRotationTransformLocked(&transform);
633 if (err != OK) {
634 // Error logged by getRotationTransformLocked.
635 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
636 "Unable to calculate rotation transform for new stream");
637 }
638
639 err = mDevice->setStreamTransform(streamId, transform);
640 if (err != OK) {
641 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
642 streamId);
643 ALOGE("%s: %s", __FUNCTION__, msg.string());
644 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
645 }
646
647 return res;
648 }
649
createInputStream(int width,int height,int format,int32_t * newStreamId)650 binder::Status CameraDeviceClient::createInputStream(
651 int width, int height, int format,
652 /*out*/
653 int32_t* newStreamId) {
654
655 ATRACE_CALL();
656 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
657
658 binder::Status res;
659 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
660
661 Mutex::Autolock icl(mBinderSerializationLock);
662
663 if (!mDevice.get()) {
664 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
665 }
666
667 if (mInputStream.configured) {
668 String8 msg = String8::format("Camera %s: Already has an input stream "
669 "configured (ID %zd)", mCameraIdStr.string(), mInputStream.id);
670 ALOGE("%s: %s", __FUNCTION__, msg.string() );
671 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
672 }
673
674 int streamId = -1;
675 status_t err = mDevice->createInputStream(width, height, format, &streamId);
676 if (err == OK) {
677 mInputStream.configured = true;
678 mInputStream.width = width;
679 mInputStream.height = height;
680 mInputStream.format = format;
681 mInputStream.id = streamId;
682
683 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
684 __FUNCTION__, mCameraIdStr.string(), streamId);
685
686 *newStreamId = streamId;
687 } else {
688 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
689 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
690 strerror(-err), err);
691 }
692
693 return res;
694 }
695
getInputSurface(view::Surface * inputSurface)696 binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
697
698 binder::Status res;
699 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
700
701 if (inputSurface == NULL) {
702 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
703 }
704
705 Mutex::Autolock icl(mBinderSerializationLock);
706 if (!mDevice.get()) {
707 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
708 }
709 sp<IGraphicBufferProducer> producer;
710 status_t err = mDevice->getInputBufferProducer(&producer);
711 if (err != OK) {
712 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
713 "Camera %s: Error getting input Surface: %s (%d)",
714 mCameraIdStr.string(), strerror(-err), err);
715 } else {
716 inputSurface->name = String16("CameraInput");
717 inputSurface->graphicBufferProducer = producer;
718 }
719 return res;
720 }
721
isPublicFormat(int32_t format)722 bool CameraDeviceClient::isPublicFormat(int32_t format)
723 {
724 switch(format) {
725 case HAL_PIXEL_FORMAT_RGBA_8888:
726 case HAL_PIXEL_FORMAT_RGBX_8888:
727 case HAL_PIXEL_FORMAT_RGB_888:
728 case HAL_PIXEL_FORMAT_RGB_565:
729 case HAL_PIXEL_FORMAT_BGRA_8888:
730 case HAL_PIXEL_FORMAT_YV12:
731 case HAL_PIXEL_FORMAT_Y8:
732 case HAL_PIXEL_FORMAT_Y16:
733 case HAL_PIXEL_FORMAT_RAW16:
734 case HAL_PIXEL_FORMAT_RAW10:
735 case HAL_PIXEL_FORMAT_RAW12:
736 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
737 case HAL_PIXEL_FORMAT_BLOB:
738 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
739 case HAL_PIXEL_FORMAT_YCbCr_420_888:
740 case HAL_PIXEL_FORMAT_YCbCr_422_888:
741 case HAL_PIXEL_FORMAT_YCbCr_444_888:
742 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
743 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
744 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
745 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
746 case HAL_PIXEL_FORMAT_YCbCr_422_I:
747 return true;
748 default:
749 return false;
750 }
751 }
752
createSurfaceFromGbp(OutputStreamInfo & streamInfo,bool isStreamInfoValid,sp<Surface> & surface,const sp<IGraphicBufferProducer> & gbp)753 binder::Status CameraDeviceClient::createSurfaceFromGbp(
754 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
755 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
756
757 // bufferProducer must be non-null
758 if (gbp == nullptr) {
759 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
760 ALOGW("%s: %s", __FUNCTION__, msg.string());
761 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
762 }
763 // HACK b/10949105
764 // Query consumer usage bits to set async operation mode for
765 // GLConsumer using controlledByApp parameter.
766 bool useAsync = false;
767 int32_t consumerUsage;
768 status_t err;
769 if ((err = gbp->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
770 &consumerUsage)) != OK) {
771 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
772 mCameraIdStr.string(), strerror(-err), err);
773 ALOGE("%s: %s", __FUNCTION__, msg.string());
774 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
775 }
776 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
777 ALOGW("%s: Camera %s with consumer usage flag: 0x%x: Forcing asynchronous mode for stream",
778 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
779 useAsync = true;
780 }
781
782 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
783 GRALLOC_USAGE_RENDERSCRIPT;
784 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
785 GraphicBuffer::USAGE_HW_TEXTURE |
786 GraphicBuffer::USAGE_HW_COMPOSER;
787 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
788 (consumerUsage & allowedFlags) != 0;
789
790 surface = new Surface(gbp, useAsync);
791 ANativeWindow *anw = surface.get();
792
793 int width, height, format;
794 android_dataspace dataSpace;
795 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
796 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
797 mCameraIdStr.string(), strerror(-err), err);
798 ALOGE("%s: %s", __FUNCTION__, msg.string());
799 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
800 }
801 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
802 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
803 mCameraIdStr.string(), strerror(-err), err);
804 ALOGE("%s: %s", __FUNCTION__, msg.string());
805 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
806 }
807 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
808 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
809 mCameraIdStr.string(), strerror(-err), err);
810 ALOGE("%s: %s", __FUNCTION__, msg.string());
811 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
812 }
813 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
814 reinterpret_cast<int*>(&dataSpace))) != OK) {
815 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
816 mCameraIdStr.string(), strerror(-err), err);
817 ALOGE("%s: %s", __FUNCTION__, msg.string());
818 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
819 }
820
821 // FIXME: remove this override since the default format should be
822 // IMPLEMENTATION_DEFINED. b/9487482
823 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
824 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
825 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
826 __FUNCTION__, mCameraIdStr.string(), format);
827 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
828 }
829 // Round dimensions to the nearest dimensions available for this format
830 if (flexibleConsumer && isPublicFormat(format) &&
831 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
832 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
833 String8 msg = String8::format("Camera %s: No supported stream configurations with "
834 "format %#x defined, failed to create output stream",
835 mCameraIdStr.string(), format);
836 ALOGE("%s: %s", __FUNCTION__, msg.string());
837 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
838 }
839
840 if (!isStreamInfoValid) {
841 streamInfo.width = width;
842 streamInfo.height = height;
843 streamInfo.format = format;
844 streamInfo.dataSpace = dataSpace;
845 streamInfo.consumerUsage = consumerUsage;
846 return binder::Status::ok();
847 }
848 if (width != streamInfo.width) {
849 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
850 mCameraIdStr.string(), width, streamInfo.width);
851 ALOGE("%s: %s", __FUNCTION__, msg.string());
852 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
853 }
854 if (height != streamInfo.height) {
855 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
856 mCameraIdStr.string(), height, streamInfo.height);
857 ALOGE("%s: %s", __FUNCTION__, msg.string());
858 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
859 }
860 if (format != streamInfo.format) {
861 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
862 mCameraIdStr.string(), format, streamInfo.format);
863 ALOGE("%s: %s", __FUNCTION__, msg.string());
864 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
865 }
866 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
867 if (dataSpace != streamInfo.dataSpace) {
868 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
869 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
870 ALOGE("%s: %s", __FUNCTION__, msg.string());
871 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
872 }
873 //At the native side, there isn't a way to check whether 2 surfaces come from the same
874 //surface class type. Use usage flag to approximate the comparison.
875 if (consumerUsage != streamInfo.consumerUsage) {
876 String8 msg = String8::format(
877 "Camera %s:Surface usage flag doesn't match 0x%x vs 0x%x",
878 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
879 ALOGE("%s: %s", __FUNCTION__, msg.string());
880 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
881 }
882 }
883 return binder::Status::ok();
884 }
885
roundBufferDimensionNearest(int32_t width,int32_t height,int32_t format,android_dataspace dataSpace,const CameraMetadata & info,int32_t * outWidth,int32_t * outHeight)886 bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
887 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
888 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
889
890 camera_metadata_ro_entry streamConfigs =
891 (dataSpace == HAL_DATASPACE_DEPTH) ?
892 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
893 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
894
895 int32_t bestWidth = -1;
896 int32_t bestHeight = -1;
897
898 // Iterate through listed stream configurations and find the one with the smallest euclidean
899 // distance from the given dimensions for the given format.
900 for (size_t i = 0; i < streamConfigs.count; i += 4) {
901 int32_t fmt = streamConfigs.data.i32[i];
902 int32_t w = streamConfigs.data.i32[i + 1];
903 int32_t h = streamConfigs.data.i32[i + 2];
904
905 // Ignore input/output type for now
906 if (fmt == format) {
907 if (w == width && h == height) {
908 bestWidth = width;
909 bestHeight = height;
910 break;
911 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
912 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
913 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
914 bestWidth = w;
915 bestHeight = h;
916 }
917 }
918 }
919
920 if (bestWidth == -1) {
921 // Return false if no configurations for this format were listed
922 return false;
923 }
924
925 // Set the outputs to the closet width/height
926 if (outWidth != NULL) {
927 *outWidth = bestWidth;
928 }
929 if (outHeight != NULL) {
930 *outHeight = bestHeight;
931 }
932
933 // Return true if at least one configuration for this format was listed
934 return true;
935 }
936
euclidDistSquare(int32_t x0,int32_t y0,int32_t x1,int32_t y1)937 int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
938 int64_t d0 = x0 - x1;
939 int64_t d1 = y0 - y1;
940 return d0 * d0 + d1 * d1;
941 }
942
943 // Create a request object from a template.
createDefaultRequest(int templateId,hardware::camera2::impl::CameraMetadataNative * request)944 binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
945 /*out*/
946 hardware::camera2::impl::CameraMetadataNative* request)
947 {
948 ATRACE_CALL();
949 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
950
951 binder::Status res;
952 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
953
954 Mutex::Autolock icl(mBinderSerializationLock);
955
956 if (!mDevice.get()) {
957 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
958 }
959
960 CameraMetadata metadata;
961 status_t err;
962 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
963 request != NULL) {
964
965 request->swap(metadata);
966 } else if (err == BAD_VALUE) {
967 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
968 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
969 mCameraIdStr.string(), templateId, strerror(-err), err);
970
971 } else {
972 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
973 "Camera %s: Error creating default request for template %d: %s (%d)",
974 mCameraIdStr.string(), templateId, strerror(-err), err);
975 }
976 return res;
977 }
978
getCameraInfo(hardware::camera2::impl::CameraMetadataNative * info)979 binder::Status CameraDeviceClient::getCameraInfo(
980 /*out*/
981 hardware::camera2::impl::CameraMetadataNative* info)
982 {
983 ATRACE_CALL();
984 ALOGV("%s", __FUNCTION__);
985
986 binder::Status res;
987
988 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
989
990 Mutex::Autolock icl(mBinderSerializationLock);
991
992 if (!mDevice.get()) {
993 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
994 }
995
996 if (info != NULL) {
997 *info = mDevice->info(); // static camera metadata
998 // TODO: merge with device-specific camera metadata
999 }
1000
1001 return res;
1002 }
1003
waitUntilIdle()1004 binder::Status CameraDeviceClient::waitUntilIdle()
1005 {
1006 ATRACE_CALL();
1007 ALOGV("%s", __FUNCTION__);
1008
1009 binder::Status res;
1010 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1011
1012 Mutex::Autolock icl(mBinderSerializationLock);
1013
1014 if (!mDevice.get()) {
1015 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1016 }
1017
1018 // FIXME: Also need check repeating burst.
1019 Mutex::Autolock idLock(mStreamingRequestIdLock);
1020 if (mStreamingRequestId != REQUEST_ID_NONE) {
1021 String8 msg = String8::format(
1022 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1023 mCameraIdStr.string());
1024 ALOGE("%s: %s", __FUNCTION__, msg.string());
1025 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1026 }
1027 status_t err = mDevice->waitUntilDrained();
1028 if (err != OK) {
1029 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1030 "Camera %s: Error waiting to drain: %s (%d)",
1031 mCameraIdStr.string(), strerror(-err), err);
1032 }
1033 ALOGV("%s Done", __FUNCTION__);
1034 return res;
1035 }
1036
flush(int64_t * lastFrameNumber)1037 binder::Status CameraDeviceClient::flush(
1038 /*out*/
1039 int64_t* lastFrameNumber) {
1040 ATRACE_CALL();
1041 ALOGV("%s", __FUNCTION__);
1042
1043 binder::Status res;
1044 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1045
1046 Mutex::Autolock icl(mBinderSerializationLock);
1047
1048 if (!mDevice.get()) {
1049 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1050 }
1051
1052 Mutex::Autolock idLock(mStreamingRequestIdLock);
1053 mStreamingRequestId = REQUEST_ID_NONE;
1054 status_t err = mDevice->flush(lastFrameNumber);
1055 if (err != OK) {
1056 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1057 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
1058 }
1059 return res;
1060 }
1061
prepare(int streamId)1062 binder::Status CameraDeviceClient::prepare(int streamId) {
1063 ATRACE_CALL();
1064 ALOGV("%s", __FUNCTION__);
1065
1066 binder::Status res;
1067 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1068
1069 Mutex::Autolock icl(mBinderSerializationLock);
1070
1071 // Guard against trying to prepare non-created streams
1072 ssize_t index = NAME_NOT_FOUND;
1073 for (size_t i = 0; i < mStreamMap.size(); ++i) {
1074 if (streamId == mStreamMap.valueAt(i).streamId()) {
1075 index = i;
1076 break;
1077 }
1078 }
1079
1080 if (index == NAME_NOT_FOUND) {
1081 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1082 "with that ID exists", mCameraIdStr.string(), streamId);
1083 ALOGW("%s: %s", __FUNCTION__, msg.string());
1084 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1085 }
1086
1087 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1088 // has been used
1089 status_t err = mDevice->prepare(streamId);
1090 if (err == BAD_VALUE) {
1091 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1092 "Camera %s: Stream %d has already been used, and cannot be prepared",
1093 mCameraIdStr.string(), streamId);
1094 } else if (err != OK) {
1095 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1096 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
1097 strerror(-err), err);
1098 }
1099 return res;
1100 }
1101
prepare2(int maxCount,int streamId)1102 binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
1103 ATRACE_CALL();
1104 ALOGV("%s", __FUNCTION__);
1105
1106 binder::Status res;
1107 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1108
1109 Mutex::Autolock icl(mBinderSerializationLock);
1110
1111 // Guard against trying to prepare non-created streams
1112 ssize_t index = NAME_NOT_FOUND;
1113 for (size_t i = 0; i < mStreamMap.size(); ++i) {
1114 if (streamId == mStreamMap.valueAt(i).streamId()) {
1115 index = i;
1116 break;
1117 }
1118 }
1119
1120 if (index == NAME_NOT_FOUND) {
1121 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1122 "with that ID exists", mCameraIdStr.string(), streamId);
1123 ALOGW("%s: %s", __FUNCTION__, msg.string());
1124 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1125 }
1126
1127 if (maxCount <= 0) {
1128 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1129 mCameraIdStr.string(), maxCount);
1130 ALOGE("%s: %s", __FUNCTION__, msg.string());
1131 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1132 }
1133
1134 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1135 // has been used
1136 status_t err = mDevice->prepare(maxCount, streamId);
1137 if (err == BAD_VALUE) {
1138 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1139 "Camera %s: Stream %d has already been used, and cannot be prepared",
1140 mCameraIdStr.string(), streamId);
1141 } else if (err != OK) {
1142 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1143 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
1144 strerror(-err), err);
1145 }
1146
1147 return res;
1148 }
1149
tearDown(int streamId)1150 binder::Status CameraDeviceClient::tearDown(int streamId) {
1151 ATRACE_CALL();
1152 ALOGV("%s", __FUNCTION__);
1153
1154 binder::Status res;
1155 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1156
1157 Mutex::Autolock icl(mBinderSerializationLock);
1158
1159 // Guard against trying to prepare non-created streams
1160 ssize_t index = NAME_NOT_FOUND;
1161 for (size_t i = 0; i < mStreamMap.size(); ++i) {
1162 if (streamId == mStreamMap.valueAt(i).streamId()) {
1163 index = i;
1164 break;
1165 }
1166 }
1167
1168 if (index == NAME_NOT_FOUND) {
1169 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1170 "with that ID exists", mCameraIdStr.string(), streamId);
1171 ALOGW("%s: %s", __FUNCTION__, msg.string());
1172 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1173 }
1174
1175 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1176 // use
1177 status_t err = mDevice->tearDown(streamId);
1178 if (err == BAD_VALUE) {
1179 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1180 "Camera %s: Stream %d is still in use, cannot be torn down",
1181 mCameraIdStr.string(), streamId);
1182 } else if (err != OK) {
1183 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1184 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
1185 strerror(-err), err);
1186 }
1187
1188 return res;
1189 }
1190
finalizeOutputConfigurations(int32_t streamId,const hardware::camera2::params::OutputConfiguration & outputConfiguration)1191 binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
1192 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1193 ATRACE_CALL();
1194
1195 binder::Status res;
1196 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1197
1198 Mutex::Autolock icl(mBinderSerializationLock);
1199
1200 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1201 outputConfiguration.getGraphicBufferProducers();
1202
1203 if (bufferProducers.size() == 0) {
1204 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1205 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1206 }
1207
1208 // streamId should be in mStreamMap if this stream already has a surface attached
1209 // to it. Otherwise, it should be in mDeferredStreams.
1210 bool streamIdConfigured = false;
1211 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1212 for (size_t i = 0; i < mStreamMap.size(); i++) {
1213 if (mStreamMap.valueAt(i).streamId() == streamId) {
1214 streamIdConfigured = true;
1215 break;
1216 }
1217 }
1218 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1219 if (streamId == mDeferredStreams[i]) {
1220 deferredStreamIndex = i;
1221 break;
1222 }
1223
1224 }
1225 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1226 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1227 "(ID %d)", mCameraIdStr.string(), streamId);
1228 ALOGW("%s: %s", __FUNCTION__, msg.string());
1229 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1230 }
1231
1232 if (mStreamInfoMap[streamId].finalized) {
1233 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1234 " on stream ID %d", mCameraIdStr.string(), streamId);
1235 ALOGW("%s: %s", __FUNCTION__, msg.string());
1236 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1237 }
1238
1239 if (!mDevice.get()) {
1240 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1241 }
1242
1243 std::vector<sp<Surface>> consumerSurfaces;
1244 std::vector<size_t> consumerSurfaceIds;
1245 size_t surfaceId = 0;
1246 for (auto& bufferProducer : bufferProducers) {
1247 // Don't create multiple streams for the same target surface
1248 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1249 if (index != NAME_NOT_FOUND) {
1250 ALOGV("Camera %s: Surface already has a stream created "
1251 " for it (ID %zd)", mCameraIdStr.string(), index);
1252 surfaceId++;
1253 continue;
1254 }
1255
1256 sp<Surface> surface;
1257 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1258 surface, bufferProducer);
1259
1260 if (!res.isOk())
1261 return res;
1262
1263 consumerSurfaces.push_back(surface);
1264 consumerSurfaceIds.push_back(surfaceId);
1265 surfaceId++;
1266 }
1267
1268 // Gracefully handle case where finalizeOutputConfigurations is called
1269 // without any new surface.
1270 if (consumerSurfaces.size() == 0) {
1271 mStreamInfoMap[streamId].finalized = true;
1272 return res;
1273 }
1274
1275 // Finish the deferred stream configuration with the surface.
1276 status_t err;
1277 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces);
1278 if (err == OK) {
1279 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1280 sp<IBinder> binder = IInterface::asBinder(
1281 consumerSurfaces[i]->getIGraphicBufferProducer());
1282 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %zu", __FUNCTION__,
1283 binder.get(), streamId, consumerSurfaceIds[i]);
1284 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1285 }
1286 if (deferredStreamIndex != NAME_NOT_FOUND) {
1287 mDeferredStreams.removeItemsAt(deferredStreamIndex);
1288 }
1289 mStreamInfoMap[streamId].finalized = true;
1290 } else if (err == NO_INIT) {
1291 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1292 "Camera %s: Deferred surface is invalid: %s (%d)",
1293 mCameraIdStr.string(), strerror(-err), err);
1294 } else {
1295 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1296 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1297 mCameraIdStr.string(), strerror(-err), err);
1298 }
1299
1300 return res;
1301 }
1302
dump(int fd,const Vector<String16> & args)1303 status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
1304 return BasicClient::dump(fd, args);
1305 }
1306
dumpClient(int fd,const Vector<String16> & args)1307 status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
1308 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
1309 mCameraIdStr.string(),
1310 (getRemoteCallback() != NULL ?
1311 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
1312 dprintf(fd, " Current client UID %u\n", mClientUid);
1313
1314 dprintf(fd, " State:\n");
1315 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
1316 if (mInputStream.configured) {
1317 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
1318 } else {
1319 dprintf(fd, " No input stream configured.\n");
1320 }
1321 if (!mStreamMap.isEmpty()) {
1322 dprintf(fd, " Current output stream/surface IDs:\n");
1323 for (size_t i = 0; i < mStreamMap.size(); i++) {
1324 dprintf(fd, " Stream %d Surface %d\n",
1325 mStreamMap.valueAt(i).streamId(),
1326 mStreamMap.valueAt(i).surfaceId());
1327 }
1328 } else if (!mDeferredStreams.isEmpty()) {
1329 dprintf(fd, " Current deferred surface output stream IDs:\n");
1330 for (auto& streamId : mDeferredStreams) {
1331 dprintf(fd, " Stream %d\n", streamId);
1332 }
1333 } else {
1334 dprintf(fd, " No output streams configured.\n");
1335 }
1336 // TODO: print dynamic/request section from most recent requests
1337 mFrameProcessor->dump(fd, args);
1338
1339 return dumpDevice(fd, args);
1340 }
1341
notifyError(int32_t errorCode,const CaptureResultExtras & resultExtras)1342 void CameraDeviceClient::notifyError(int32_t errorCode,
1343 const CaptureResultExtras& resultExtras) {
1344 // Thread safe. Don't bother locking.
1345 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1346
1347 if (remoteCb != 0) {
1348 remoteCb->onDeviceError(errorCode, resultExtras);
1349 }
1350 }
1351
notifyRepeatingRequestError(long lastFrameNumber)1352 void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1353 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1354
1355 if (remoteCb != 0) {
1356 remoteCb->onRepeatingRequestError(lastFrameNumber);
1357 }
1358
1359 Mutex::Autolock idLock(mStreamingRequestIdLock);
1360 mStreamingRequestId = REQUEST_ID_NONE;
1361 }
1362
notifyIdle()1363 void CameraDeviceClient::notifyIdle() {
1364 // Thread safe. Don't bother locking.
1365 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1366
1367 if (remoteCb != 0) {
1368 remoteCb->onDeviceIdle();
1369 }
1370 Camera2ClientBase::notifyIdle();
1371 }
1372
notifyShutter(const CaptureResultExtras & resultExtras,nsecs_t timestamp)1373 void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
1374 nsecs_t timestamp) {
1375 // Thread safe. Don't bother locking.
1376 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1377 if (remoteCb != 0) {
1378 remoteCb->onCaptureStarted(resultExtras, timestamp);
1379 }
1380 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
1381 }
1382
notifyPrepared(int streamId)1383 void CameraDeviceClient::notifyPrepared(int streamId) {
1384 // Thread safe. Don't bother locking.
1385 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1386 if (remoteCb != 0) {
1387 remoteCb->onPrepared(streamId);
1388 }
1389 }
1390
notifyRequestQueueEmpty()1391 void CameraDeviceClient::notifyRequestQueueEmpty() {
1392 // Thread safe. Don't bother locking.
1393 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1394 if (remoteCb != 0) {
1395 remoteCb->onRequestQueueEmpty();
1396 }
1397 }
1398
detachDevice()1399 void CameraDeviceClient::detachDevice() {
1400 if (mDevice == 0) return;
1401
1402 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
1403
1404 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1405 FRAME_PROCESSOR_LISTENER_MAX_ID,
1406 /*listener*/this);
1407 mFrameProcessor->requestExit();
1408 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
1409 mFrameProcessor->join();
1410 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
1411
1412 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1413 {
1414 mDevice->clearStreamingRequest();
1415
1416 status_t code;
1417 if ((code = mDevice->waitUntilDrained()) != OK) {
1418 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1419 code);
1420 }
1421 }
1422
1423 Camera2ClientBase::detachDevice();
1424 }
1425
1426 /** Device-related methods */
onResultAvailable(const CaptureResult & result)1427 void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
1428 ATRACE_CALL();
1429 ALOGV("%s", __FUNCTION__);
1430
1431 // Thread-safe. No lock necessary.
1432 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
1433 if (remoteCb != NULL) {
1434 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
1435 }
1436 }
1437
checkPidStatus(const char * checkLocation)1438 binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
1439 if (mDisconnected) {
1440 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1441 "The camera device has been disconnected");
1442 }
1443 status_t res = checkPid(checkLocation);
1444 return (res == OK) ? binder::Status::ok() :
1445 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1446 "Attempt to use camera from a different process than original client");
1447 }
1448
1449 // TODO: move to Camera2ClientBase
enforceRequestPermissions(CameraMetadata & metadata)1450 bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1451
1452 const int pid = IPCThreadState::self()->getCallingPid();
1453 const int selfPid = getpid();
1454 camera_metadata_entry_t entry;
1455
1456 /**
1457 * Mixin default important security values
1458 * - android.led.transmit = defaulted ON
1459 */
1460 CameraMetadata staticInfo = mDevice->info();
1461 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1462 for(size_t i = 0; i < entry.count; ++i) {
1463 uint8_t led = entry.data.u8[i];
1464
1465 switch(led) {
1466 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1467 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1468 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1469 metadata.update(ANDROID_LED_TRANSMIT,
1470 &transmitDefault, 1);
1471 }
1472 break;
1473 }
1474 }
1475 }
1476
1477 // We can do anything!
1478 if (pid == selfPid) {
1479 return true;
1480 }
1481
1482 /**
1483 * Permission check special fields in the request
1484 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1485 */
1486 entry = metadata.find(ANDROID_LED_TRANSMIT);
1487 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1488 String16 permissionString =
1489 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1490 if (!checkCallingPermission(permissionString)) {
1491 const int uid = IPCThreadState::self()->getCallingUid();
1492 ALOGE("Permission Denial: "
1493 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1494 return false;
1495 }
1496 }
1497
1498 return true;
1499 }
1500
getRotationTransformLocked(int32_t * transform)1501 status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1502 ALOGV("%s: begin", __FUNCTION__);
1503
1504 const CameraMetadata& staticInfo = mDevice->info();
1505 return CameraUtils::getRotationTransform(staticInfo, transform);
1506 }
1507
1508 } // namespace android
1509