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