• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013-2018 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 #ifdef LOG_NNDEBUG
20 #define ALOGVV(...) ALOGV(__VA_ARGS__)
21 #else
22 #define ALOGVV(...) ((void)0)
23 #endif
24 //#define LOG_NDEBUG 0
25 
26 #include <camera/CameraUtils.h>
27 #include <camera/StringUtils.h>
28 #include <camera/camera2/CaptureRequest.h>
29 #include <com_android_internal_camera_flags.h>
30 #include <cutils/properties.h>
31 #include <gui/Surface.h>
32 #include <utils/Log.h>
33 #include <utils/SessionConfigurationUtils.h>
34 #include <utils/Trace.h>
35 
36 #include "common/CameraDeviceBase.h"
37 #include "device3/Camera3Device.h"
38 #include "device3/Camera3OutputStream.h"
39 #include "api2/CameraDeviceClient.h"
40 
41 #include <camera_metadata_hidden.h>
42 
43 #include "DepthCompositeStream.h"
44 #include "HeicCompositeStream.h"
45 #include "JpegRCompositeStream.h"
46 
47 // Convenience methods for constructing binder::Status objects for error returns
48 constexpr int32_t METADATA_QUEUE_SIZE = 1 << 20;
49 
50 #define STATUS_ERROR(errorCode, errorString) \
51     binder::Status::fromServiceSpecificError(errorCode, \
52             fmt::sprintf("%s:%d: %s", __FUNCTION__, __LINE__, errorString).c_str())
53 
54 #define STATUS_ERROR_FMT(errorCode, errorString, ...) \
55     binder::Status::fromServiceSpecificError(errorCode, \
56             fmt::sprintf("%s:%d: " errorString, __FUNCTION__, __LINE__, \
57                     __VA_ARGS__).c_str())
58 
59 namespace android {
60 using namespace camera2;
61 using namespace camera3;
62 using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
63 using hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
64 
65 namespace flags = com::android::internal::camera::flags;
66 
CameraDeviceClientBase(const sp<CameraService> & cameraService,const sp<hardware::camera2::ICameraDeviceCallbacks> & remoteCallback,std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,const AttributionSourceState & clientAttribution,int callingPid,bool systemNativeClient,const std::string & cameraId,int api1CameraId,int cameraFacing,int sensorOrientation,int servicePid,int rotationOverride,bool sharedMode)67 CameraDeviceClientBase::CameraDeviceClientBase(
68         const sp<CameraService>& cameraService,
69         const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
70         std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
71         const AttributionSourceState& clientAttribution, int callingPid, bool systemNativeClient,
72         const std::string& cameraId, [[maybe_unused]] int api1CameraId, int cameraFacing,
73         int sensorOrientation, int servicePid, int rotationOverride, bool sharedMode)
74     : BasicClient(cameraService, IInterface::asBinder(remoteCallback),
75                   attributionAndPermissionUtils, clientAttribution, callingPid, systemNativeClient,
76                   cameraId, cameraFacing, sensorOrientation, servicePid, rotationOverride,
77                   sharedMode),
78       mRemoteCallback(remoteCallback) {}
79 
80 // Interface used by CameraService
81 
CameraDeviceClient(const sp<CameraService> & cameraService,const sp<hardware::camera2::ICameraDeviceCallbacks> & remoteCallback,std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,const AttributionSourceState & clientAttribution,int callingPid,bool systemNativeClient,const std::string & cameraId,int cameraFacing,int sensorOrientation,int servicePid,bool overrideForPerfClass,int rotationOverride,const std::string & originalCameraId,bool sharedMode,bool isVendorClient)82 CameraDeviceClient::CameraDeviceClient(
83         const sp<CameraService>& cameraService,
84         const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
85         std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
86         std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
87         const AttributionSourceState& clientAttribution, int callingPid, bool systemNativeClient,
88         const std::string& cameraId, int cameraFacing, int sensorOrientation, int servicePid,
89         bool overrideForPerfClass, int rotationOverride, const std::string& originalCameraId,
90         bool sharedMode, bool isVendorClient)
91     : Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper,
92                         attributionAndPermissionUtils, clientAttribution, callingPid,
93                         systemNativeClient, cameraId, /*API1 camera ID*/ -1, cameraFacing,
94                         sensorOrientation, servicePid, overrideForPerfClass, rotationOverride,
95                         sharedMode, isVendorClient),
96       mInputStream(),
97       mStreamingRequestId(REQUEST_ID_NONE),
98       mStreamingRequestLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
99       mRequestIdCounter(0),
100       mOverrideForPerfClass(overrideForPerfClass),
101       mOriginalCameraId(originalCameraId),
102       mIsVendorClient(isVendorClient) {
103     ATRACE_CALL();
104     ALOGI("CameraDeviceClient %s: Opened", cameraId.c_str());
105 }
106 
initialize(sp<CameraProviderManager> manager,const std::string & monitorTags)107 status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
108         const std::string& monitorTags) {
109     return initializeImpl(manager, monitorTags);
110 }
111 
112 template<typename TProviderPtr>
initializeImpl(TProviderPtr providerPtr,const std::string & monitorTags)113 status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr,
114         const std::string& monitorTags) {
115     ATRACE_CALL();
116     status_t res;
117 
118     res = Camera2ClientBase::initialize(providerPtr, monitorTags);
119     if (res != OK) {
120         return res;
121     }
122 
123     if (flags::camera_multi_client() && mSharedMode) {
124         // In shared camera device mode, there can be more than one clients and
125         // frame processor thread is started by shared camera device.
126         mFrameProcessor = mDevice->getSharedFrameProcessor();
127         if (mFrameProcessor == nullptr) {
128             ALOGE("%s: Unable to start frame processor thread", __FUNCTION__);
129             return UNKNOWN_ERROR;
130         }
131     } else {
132         mFrameProcessor = new FrameProcessorBase(mDevice);
133         std::string threadName = std::string("CDU-") + mCameraIdStr + "-FrameProc";
134         res = mFrameProcessor->run(threadName.c_str());
135         if (res != OK) {
136             ALOGE("%s: Unable to start frame processor thread: %s (%d)",
137                     __FUNCTION__, strerror(-res), res);
138             return res;
139         }
140     }
141 
142     mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
143                                       camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
144                                       /*listener*/this,
145                                       /*sendPartials*/true);
146 
147     const CameraMetadata &deviceInfo = mDevice->info();
148     camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
149             ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
150     if (physicalKeysEntry.count > 0) {
151         mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
152                 physicalKeysEntry.data.i32,
153                 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
154     }
155 
156     auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
157     mDynamicProfileMap.emplace(
158             ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
159             ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
160     if (entry.count > 0) {
161         const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
162                 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
163         if (it != entry.data.u8 + entry.count) {
164             entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
165             if (entry.count > 0 || ((entry.count % 3) != 0)) {
166                 int64_t standardBitmap =
167                         ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
168                 for (size_t i = 0; i < entry.count; i += 3) {
169                     if (entry.data.i64[i] !=
170                             ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
171                         mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
172                         if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
173                                 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
174                             standardBitmap |= entry.data.i64[i];
175                         }
176                     } else {
177                         ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
178                                 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
179                     }
180                 }
181                 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
182                         standardBitmap;
183             } else {
184                 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
185                         " profile map!", __FUNCTION__, mCameraIdStr.c_str());
186             }
187         }
188     }
189 
190     mProviderManager = providerPtr;
191     // Cache physical camera ids corresponding to this device and also the high
192     // resolution sensors in this device + physical camera ids
193     mProviderManager->isLogicalCamera(mCameraIdStr, &mPhysicalCameraIds);
194     if (supportsUltraHighResolutionCapture(mCameraIdStr)) {
195         mHighResolutionSensors.insert(mCameraIdStr);
196     }
197     for (auto &physicalId : mPhysicalCameraIds) {
198         if (supportsUltraHighResolutionCapture(physicalId)) {
199             mHighResolutionSensors.insert(physicalId);
200         }
201     }
202     size_t fmqHalSize = mDevice->getCaptureResultFMQSize();
203     size_t resultMQSize =
204             property_get_int32("ro.camera.resultFmqSize", /*default*/0);
205     resultMQSize = resultMQSize > 0 ? resultMQSize : fmqHalSize;
206     res = CreateMetadataQueue(&mResultMetadataQueue, resultMQSize);
207     if (res != OK) {
208         ALOGE("%s: Creating result metadata queue failed: %s(%d)", __FUNCTION__,
209             strerror(-res), res);
210         return res;
211     }
212     return OK;
213 }
214 
~CameraDeviceClient()215 CameraDeviceClient::~CameraDeviceClient() {
216 }
217 
submitRequest(const hardware::camera2::CaptureRequest & request,bool streaming,hardware::camera2::utils::SubmitInfo * submitInfo)218 binder::Status CameraDeviceClient::submitRequest(
219         const hardware::camera2::CaptureRequest& request,
220         bool streaming,
221         /*out*/
222         hardware::camera2::utils::SubmitInfo *submitInfo) {
223     std::vector<hardware::camera2::CaptureRequest> requestList = { request };
224     return submitRequestList(requestList, streaming, submitInfo);
225 }
226 
getSurfaceKey(ParcelableSurfaceType surface,SurfaceKey * out) const227 status_t CameraDeviceClient::getSurfaceKey(ParcelableSurfaceType surface, SurfaceKey* out) const {
228 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
229     auto ret = surface.getUniqueId(out);
230     if (ret != OK) {
231         ALOGE("%s: Camera %s: Could not getUniqueId.", __FUNCTION__, mCameraIdStr.c_str());
232         return ret;
233     }
234     return OK;
235 #else
236     *out = IInterface::asBinder(surface);
237     return OK;
238 #endif
239 }
240 
getSurfaceKey(sp<Surface> surface,SurfaceKey * out) const241 status_t CameraDeviceClient::getSurfaceKey(sp<Surface> surface, SurfaceKey* out) const {
242 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
243     auto ret = surface->getUniqueId(out);
244     if (ret != OK) {
245         ALOGE("%s: Camera %s: Could not getUniqueId.", __FUNCTION__, mCameraIdStr.c_str());
246         return ret;
247     }
248     return OK;
249 #else
250     *out = IInterface::asBinder(surface->getIGraphicBufferProducer());
251     return OK;
252 #endif
253 }
254 
insertSurfaceLocked(const ParcelableSurfaceType & surface,SurfaceMap * outSurfaceMap,Vector<int32_t> * outputStreamIds,int32_t * currentStreamId)255 binder::Status CameraDeviceClient::insertSurfaceLocked(const ParcelableSurfaceType& surface,
256         SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
257     int compositeIdx;
258     SurfaceKey surfaceKey;
259     status_t ret = getSurfaceKey(surface, &surfaceKey);
260     if(ret != OK) {
261         ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__, mCameraIdStr.c_str());
262         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, "Could not get the SurfaceKey");
263     }
264     int idx = mStreamMap.indexOfKey(surfaceKey);
265 
266     Mutex::Autolock l(mCompositeLock);
267     // Trying to submit request with surface that wasn't created
268     if (idx == NAME_NOT_FOUND) {
269         ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
270                 " we have not called createStream on",
271                 __FUNCTION__, mCameraIdStr.c_str());
272         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
273                 "Request targets Surface that is not part of current capture session");
274     } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(surfaceKey))
275             != NAME_NOT_FOUND) {
276         mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
277                 currentStreamId);
278         return binder::Status::ok();
279     }
280 
281     const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
282     if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
283         outputStreamIds->push_back(streamSurfaceId.streamId());
284     }
285     (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
286 
287     ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
288             __FUNCTION__, mCameraIdStr.c_str(), streamSurfaceId.streamId(),
289             streamSurfaceId.surfaceId());
290 
291     if (currentStreamId != nullptr) {
292         *currentStreamId = streamSurfaceId.streamId();
293     }
294 
295     return binder::Status::ok();
296 }
297 
getIntersection(const std::unordered_set<int> & streamIdsForThisCamera,const Vector<int> & streamIdsForThisRequest)298 static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
299         const Vector<int> &streamIdsForThisRequest) {
300     std::list<int> intersection;
301     for (auto &streamId : streamIdsForThisRequest) {
302         if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
303             intersection.emplace_back(streamId);
304         }
305     }
306     return intersection;
307 }
308 
startStreaming(const std::vector<int> & streamIds,const std::vector<int> & surfaceIds,hardware::camera2::utils::SubmitInfo * submitInfo)309 binder::Status CameraDeviceClient::startStreaming(const std::vector<int>& streamIds,
310             const std::vector<int>& surfaceIds,
311             /*out*/
312             hardware::camera2::utils::SubmitInfo *submitInfo) {
313     ATRACE_CALL();
314     ALOGV("%s-start of function. Stream list size %zu. Surface list size %zu", __FUNCTION__,
315             streamIds.size(), surfaceIds.size());
316 
317     binder::Status res = binder::Status::ok();
318     status_t err;
319     if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
320         return res;
321     }
322 
323     Mutex::Autolock icl(mBinderSerializationLock);
324 
325     if (!mDevice.get()) {
326         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
327     }
328 
329     if (!flags::camera_multi_client() || !mSharedMode) {
330         ALOGE("%s: Camera %s: Invalid operation.", __FUNCTION__, mCameraIdStr.c_str());
331         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, "Invalid operation");
332     }
333 
334     if (streamIds.empty() || surfaceIds.empty()) {
335         ALOGE("%s: Camera %s: Sent empty streamIds or surface Ids. Rejecting request.",
336               __FUNCTION__, mCameraIdStr.c_str());
337         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty Stream or surface Ids");
338     }
339 
340     if (streamIds.size() != surfaceIds.size()) {
341         ALOGE("%s: Camera %s: Sent different size array for stream and surface Ids.",
342               __FUNCTION__, mCameraIdStr.c_str());
343         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
344                 "Stream and surface Ids are not of same size");
345     }
346 
347     submitInfo->mRequestId = mRequestIdCounter;
348     SurfaceMap surfaceMap;
349     Vector<int32_t> outputStreamIds;
350     for (size_t i = 0; i < streamIds.size(); i++) {
351         int streamId = streamIds[i];
352         int surfaceIdx = surfaceIds[i];
353 
354         ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
355         if (index < 0) {
356             ALOGE("%s: Camera %s: Tried to start streaming with a surface that"
357                     " we have not called createStream on: stream %d",
358                     __FUNCTION__, mCameraIdStr.c_str(), streamId);
359             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
360                     "Start streaming targets Surface that is not part of current capture session");
361         }
362 
363         const auto& surfaces = mConfiguredOutputs.valueAt(index).getSurfaces();
364         if ((size_t)surfaceIdx >= surfaces.size()) {
365             ALOGE("%s: Camera %s: Tried to start streaming with a surface that"
366                     " we have not called createStream on: stream %d, surfaceIdx %d",
367                      __FUNCTION__, mCameraIdStr.c_str(), streamId, surfaceIdx);
368             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
369                     "Start streaming targets Surface has invalid surface index");
370         }
371 
372         res = insertSurfaceLocked(surfaces[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
373 
374         if (!res.isOk()) {
375             return res;
376         }
377     }
378 
379     mRequestIdCounter++;
380     int sharedReqID;
381 
382     err = mDevice->startStreaming(submitInfo->mRequestId, surfaceMap, &sharedReqID,
383             &(submitInfo->mLastFrameNumber));
384     if (err != OK) {
385         std::string msg = fmt::sprintf(
386             "Camera %s:  Got error %s (%d) after trying to start streaming request",
387             mCameraIdStr.c_str(), strerror(-err), err);
388         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
389         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
390     } else {
391         Mutex::Autolock idLock(mStreamingRequestIdLock);
392         mStreamingRequestId = submitInfo->mRequestId;
393         mSharedStreamingRequest = {sharedReqID, submitInfo->mRequestId};
394     }
395 
396     markClientActive();
397     ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.c_str());
398     return binder::Status::ok();
399 }
400 
submitRequestList(const std::vector<hardware::camera2::CaptureRequest> & requests,bool streaming,hardware::camera2::utils::SubmitInfo * submitInfo)401 binder::Status CameraDeviceClient::submitRequestList(
402         const std::vector<hardware::camera2::CaptureRequest>& requests,
403         bool streaming,
404         /*out*/
405         hardware::camera2::utils::SubmitInfo *submitInfo) {
406     ATRACE_CALL();
407     ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
408 
409     binder::Status res = binder::Status::ok();
410     status_t err;
411     if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
412         return res;
413     }
414 
415     Mutex::Autolock icl(mBinderSerializationLock);
416 
417     if (!mDevice.get()) {
418         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
419     }
420 
421     if (requests.empty()) {
422         ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
423               __FUNCTION__, mCameraIdStr.c_str());
424         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
425     }
426 
427     if (flags::camera_multi_client() && mSharedMode && !mIsPrimaryClient) {
428         ALOGE("%s: Camera %s: This client is not a primary client of the shared camera device.",
429               __FUNCTION__, mCameraIdStr.c_str());
430         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, "Invalid Operation.");
431     }
432 
433     List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
434     std::list<SurfaceMap> surfaceMapList;
435     submitInfo->mRequestId = mRequestIdCounter;
436     uint32_t loopCounter = 0;
437 
438     for (auto&& request: requests) {
439         if (request.mIsReprocess) {
440             if (!mInputStream.configured) {
441                 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
442                         mCameraIdStr.c_str());
443                 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
444                         "No input configured for camera %s but request is for reprocessing",
445                         mCameraIdStr.c_str());
446             } else if (streaming) {
447                 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
448                         mCameraIdStr.c_str());
449                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
450                         "Repeating reprocess requests not supported");
451             } else if (request.mPhysicalCameraSettings.size() > 1) {
452                 ALOGE("%s: Camera %s: reprocess requests not supported for "
453                         "multiple physical cameras.", __FUNCTION__,
454                         mCameraIdStr.c_str());
455                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
456                         "Reprocess requests not supported for multiple cameras");
457             }
458         }
459 
460         if (request.mPhysicalCameraSettings.empty()) {
461             ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
462                     mCameraIdStr.c_str());
463             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
464                     "Request doesn't contain any settings");
465         }
466 
467         //The first capture settings should always match the logical camera id
468         const std::string &logicalId = request.mPhysicalCameraSettings.begin()->id;
469         if (mDevice->getId() != logicalId && mOriginalCameraId != logicalId) {
470             ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
471                     mCameraIdStr.c_str());
472             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
473                     "Invalid camera request settings");
474         }
475 
476         if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
477             ALOGE("%s: Camera %s: Requests must have at least one surface target. "
478                     "Rejecting request.", __FUNCTION__, mCameraIdStr.c_str());
479             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
480                     "Request has no output targets");
481         }
482 
483         /**
484          * Write in the output stream IDs and map from stream ID to surface ID
485          * which we calculate from the capture request's list of surface target
486          */
487         SurfaceMap surfaceMap;
488         Vector<int32_t> outputStreamIds;
489         std::vector<std::string> requestedPhysicalIds;
490         int64_t dynamicProfileBitmap = 0;
491         if (request.mSurfaceList.size() > 0) {
492             for (const sp<Surface>& surface : request.mSurfaceList) {
493                 if (surface == 0) continue;
494 
495                 int32_t streamId;
496 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
497                 ParcelableSurfaceType surface_type = view::Surface::fromSurface(surface);
498 #else
499                 ParcelableSurfaceType surface_type = surface->getIGraphicBufferProducer();
500 #endif
501                 res = insertSurfaceLocked(surface_type, &surfaceMap, &outputStreamIds, &streamId);
502                 if (!res.isOk()) {
503                     return res;
504                 }
505 
506                 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
507                 if (index >= 0) {
508                     const std::string &requestedPhysicalId =
509                             mConfiguredOutputs.valueAt(index).getPhysicalCameraId();
510                     requestedPhysicalIds.push_back(requestedPhysicalId);
511                     dynamicProfileBitmap |=
512                             mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
513                 } else {
514                     ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
515                 }
516             }
517         } else {
518             for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
519                 int streamId = request.mStreamIdxList.itemAt(i);
520                 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
521 
522                 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
523                 if (index < 0) {
524                     ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
525                             " we have not called createStream on: stream %d",
526                             __FUNCTION__, mCameraIdStr.c_str(), streamId);
527                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
528                             "Request targets Surface that is not part of current capture session");
529                 }
530 
531                 const auto& surfaces = mConfiguredOutputs.valueAt(index).getSurfaces();
532                 if ((size_t)surfaceIdx >= surfaces.size()) {
533                     ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
534                             " we have not called createStream on: stream %d, surfaceIdx %d",
535                             __FUNCTION__, mCameraIdStr.c_str(), streamId, surfaceIdx);
536                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
537                             "Request targets Surface has invalid surface index");
538                 }
539 
540                 res = insertSurfaceLocked(surfaces[surfaceIdx], &surfaceMap, &outputStreamIds,
541                                           nullptr);
542 
543                 if (!res.isOk()) {
544                     return res;
545                 }
546 
547                 const std::string &requestedPhysicalId =
548                         mConfiguredOutputs.valueAt(index).getPhysicalCameraId();
549                 requestedPhysicalIds.push_back(requestedPhysicalId);
550                 dynamicProfileBitmap |=
551                         mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
552             }
553         }
554 
555         if (dynamicProfileBitmap !=
556                     ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
557             for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
558                     i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
559                 if ((dynamicProfileBitmap & i) == 0) {
560                     continue;
561                 }
562 
563                 const auto& it = mDynamicProfileMap.find(i);
564                 if (it != mDynamicProfileMap.end()) {
565                     if ((it->second == 0) ||
566                             ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
567                         continue;
568                     } else {
569                         ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
570                                 " reference an unsupported dynamic range profile combination"
571                                 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.c_str(),
572                                 dynamicProfileBitmap);
573                         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
574                                 "Request targets an unsupported dynamic range profile"
575                                 " combination");
576                     }
577                 } else {
578                     ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
579                             " references unsupported dynamic range profile 0x%x!",
580                             __FUNCTION__, mCameraIdStr.c_str(), i);
581                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
582                             "Request targets 10-bit Surface with unsupported dynamic range"
583                             " profile");
584                 }
585             }
586         }
587 
588         CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
589         for (const auto& it : request.mPhysicalCameraSettings) {
590             const std::string resolvedId = (mOriginalCameraId == it.id) ? mDevice->getId() : it.id;
591             if (it.settings.isEmpty()) {
592                 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
593                         __FUNCTION__, mCameraIdStr.c_str());
594                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
595                         "Request settings are empty");
596             }
597 
598             // Check whether the physical / logical stream has settings
599             // consistent with the sensor pixel mode(s) it was configured with.
600             // mCameraIdToStreamSet will only have ids that are high resolution
601             const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(resolvedId);
602             if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
603                 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
604                         outputStreamIds);
605                 if (!request.mIsReprocess &&
606                         !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
607                      ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
608                             "consistent with configured streams. Rejecting request.",
609                             __FUNCTION__, resolvedId.c_str());
610                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
611                         "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
612                         "streams configured");
613                 }
614             }
615 
616             const std::string &physicalId = resolvedId;
617             bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
618                     mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
619                     mSupportedPhysicalRequestKeys.end();
620             bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
621                     mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
622                     mSupportedPhysicalRequestKeys.end();
623             if (physicalId != mDevice->getId()) {
624                 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
625                         resolvedId);
626                 if (found == requestedPhysicalIds.end()) {
627                     ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
628                             __FUNCTION__, mCameraIdStr.c_str(), physicalId.c_str());
629                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
630                             "Invalid physical camera id");
631                 }
632 
633                 if (!mSupportedPhysicalRequestKeys.empty()) {
634                     // Filter out any unsupported physical request keys.
635                     CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
636                     camera_metadata_t *meta = const_cast<camera_metadata_t *>(
637                             filteredParams.getAndLock());
638                     set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
639                     filteredParams.unlock(meta);
640 
641                     for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
642                         camera_metadata_ro_entry entry = it.settings.find(keyIt);
643                         if (entry.count > 0) {
644                             filteredParams.update(entry);
645                         }
646                     }
647 
648                     physicalSettingsList.push_back({resolvedId, filteredParams,
649                             hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
650                 }
651             } else {
652                 physicalSettingsList.push_back({resolvedId, it.settings});
653             }
654         }
655 
656         if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
657             // Callee logs
658             return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
659                     "Caller does not have permission to change restricted controls");
660         }
661 
662         physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
663                 &outputStreamIds[0], outputStreamIds.size());
664 
665         if (request.mIsReprocess) {
666             physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
667                     &mInputStream.id, 1);
668         }
669 
670         physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
671                 &(submitInfo->mRequestId), /*size*/1);
672         loopCounter++; // loopCounter starts from 1
673         ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
674                 __FUNCTION__, mCameraIdStr.c_str(), submitInfo->mRequestId,
675                 loopCounter, requests.size());
676 
677         metadataRequestList.push_back(physicalSettingsList);
678         surfaceMapList.push_back(surfaceMap);
679 
680         // Save certain CaptureRequest settings
681         if (!request.mUserTag.empty()) {
682             mRunningSessionStats.mUserTag = request.mUserTag;
683         }
684         camera_metadata_entry entry =
685                 physicalSettingsList.begin()->metadata.find(
686                         ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
687         if (entry.count == 1) {
688             mRunningSessionStats.mVideoStabilizationMode = entry.data.u8[0];
689         }
690 
691         if (!mRunningSessionStats.mUsedUltraWide) {
692             entry = physicalSettingsList.begin()->metadata.find(
693                     ANDROID_CONTROL_ZOOM_RATIO);
694             if (entry.count == 1 && entry.data.f[0] < 1.0f ) {
695                 mRunningSessionStats.mUsedUltraWide = true;
696             }
697         }
698         if (!mRunningSessionStats.mUsedSettingsOverrideZoom) {
699             entry = physicalSettingsList.begin()->metadata.find(
700                     ANDROID_CONTROL_SETTINGS_OVERRIDE);
701             if (entry.count == 1 && entry.data.i32[0] ==
702                     ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM) {
703                 mRunningSessionStats.mUsedSettingsOverrideZoom = true;
704             }
705         }
706     }
707     mRequestIdCounter++;
708 
709     int32_t sharedReqID;
710     if (streaming) {
711         if (flags::camera_multi_client() && mSharedMode) {
712             err = mDevice->setSharedStreamingRequest(*metadataRequestList.begin(),
713                     *surfaceMapList.begin(), &sharedReqID, &(submitInfo->mLastFrameNumber));
714         } else {
715             err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
716                     &(submitInfo->mLastFrameNumber));
717         }
718 
719         if (err != OK) {
720             std::string msg = fmt::sprintf(
721                 "Camera %s:  Got error %s (%d) after trying to set streaming request",
722                 mCameraIdStr.c_str(), strerror(-err), err);
723             ALOGE("%s: %s", __FUNCTION__, msg.c_str());
724             res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
725                     msg.c_str());
726         } else {
727             Mutex::Autolock idLock(mStreamingRequestIdLock);
728             mStreamingRequestId = submitInfo->mRequestId;
729             if (flags::camera_multi_client() && mSharedMode) {
730                 mSharedStreamingRequest = {sharedReqID, submitInfo->mRequestId};
731                 markClientActive();
732             }
733         }
734     } else {
735         if (flags::camera_multi_client() && mSharedMode) {
736             err = mDevice->setSharedCaptureRequest(*metadataRequestList.begin(),
737                     *surfaceMapList.begin(), &sharedReqID, &(submitInfo->mLastFrameNumber));
738          } else {
739             err = mDevice->captureList(metadataRequestList, surfaceMapList,
740                     &(submitInfo->mLastFrameNumber));
741         }
742         if (err != OK) {
743             std::string msg = fmt::sprintf(
744                 "Camera %s: Got error %s (%d) after trying to submit capture request",
745                 mCameraIdStr.c_str(), strerror(-err), err);
746             ALOGE("%s: %s", __FUNCTION__, msg.c_str());
747             res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
748                     msg.c_str());
749         }
750         if (flags::camera_multi_client() && mSharedMode) {
751             mSharedRequestMap[sharedReqID] = submitInfo->mRequestId;
752             markClientActive();
753         }
754         ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
755     }
756 
757     ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.c_str());
758     return res;
759 }
760 
cancelRequest(int requestId,int64_t * lastFrameNumber)761 binder::Status CameraDeviceClient::cancelRequest(
762         int requestId,
763         /*out*/
764         int64_t* lastFrameNumber) {
765     ATRACE_CALL();
766     ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
767 
768     status_t err;
769     binder::Status res;
770 
771     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
772 
773     Mutex::Autolock icl(mBinderSerializationLock);
774 
775     if (!mDevice.get()) {
776         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
777     }
778 
779     Mutex::Autolock idLock(mStreamingRequestIdLock);
780     if (mStreamingRequestId != requestId) {
781         std::string msg = fmt::sprintf("Camera %s: Canceling request ID %d doesn't match "
782                 "current request ID %d", mCameraIdStr.c_str(), requestId, mStreamingRequestId);
783         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
784         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
785     }
786 
787     if (flags::camera_multi_client() && mSharedMode) {
788         err = mDevice->clearSharedStreamingRequest(lastFrameNumber);
789     } else {
790         err = mDevice->clearStreamingRequest(lastFrameNumber);
791     }
792 
793     if (err == OK) {
794         ALOGV("%s: Camera %s: Successfully cleared streaming request",
795                 __FUNCTION__, mCameraIdStr.c_str());
796         mStreamingRequestId = REQUEST_ID_NONE;
797         if (flags::camera_multi_client() && mSharedMode) {
798             mStreamingRequestLastFrameNumber = *lastFrameNumber;
799         }
800     } else {
801         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
802                 "Camera %s: Error clearing streaming request: %s (%d)",
803                 mCameraIdStr.c_str(), strerror(-err), err);
804     }
805 
806     return res;
807 }
808 
beginConfigure()809 binder::Status CameraDeviceClient::beginConfigure() {
810     ATRACE_CALL();
811     if (!flags::camera_multi_client()) {
812         return binder::Status::ok();
813     }
814     if (!mDevice.get()) {
815         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
816     }
817     status_t res = mDevice->beginConfigure();
818     if (res != OK) {
819         std::string msg = fmt::sprintf("Camera %s: Error beginning stream configuration: %s (%d)",
820                 mCameraIdStr.c_str(), strerror(-res), res);
821         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
822         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
823     }
824     return binder::Status::ok();
825 }
826 
endConfigure(int operatingMode,const hardware::camera2::impl::CameraMetadataNative & sessionParams,int64_t startTimeMs,std::vector<int> * offlineStreamIds)827 binder::Status CameraDeviceClient::endConfigure(int operatingMode,
828         const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
829         std::vector<int>* offlineStreamIds /*out*/) {
830     ATRACE_CALL();
831     ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
832             __FUNCTION__, mInputStream.configured ? 1 : 0,
833             mStreamMap.size());
834 
835     binder::Status res;
836     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
837 
838     if (offlineStreamIds == nullptr) {
839         std::string msg = "Invalid offline stream ids";
840         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
841         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
842     }
843 
844     Mutex::Autolock icl(mBinderSerializationLock);
845 
846     if (!mDevice.get()) {
847         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
848     }
849 
850     res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
851             mCameraIdStr);
852     if (!res.isOk()) {
853         return res;
854     }
855 
856     if (flags::camera_multi_client() && mSharedMode) {
857         // For shared camera session, streams are already configured
858         // earlier, hence no need to do it here.
859         return res;
860     }
861 
862     status_t err = mDevice->configureStreams(sessionParams, operatingMode);
863     if (err == BAD_VALUE) {
864         std::string msg = fmt::sprintf("Camera %s: Unsupported set of inputs/outputs provided",
865                 mCameraIdStr.c_str());
866         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
867         res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
868     } else if (err != OK) {
869         std::string msg = fmt::sprintf("Camera %s: Error configuring streams: %s (%d)",
870                 mCameraIdStr.c_str(), strerror(-err), err);
871         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
872         res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
873     } else {
874         offlineStreamIds->clear();
875         mDevice->getOfflineStreamIds(offlineStreamIds);
876 
877         Mutex::Autolock l(mCompositeLock);
878         for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
879             err = mCompositeStreamMap.valueAt(i)->configureStream();
880             if (err != OK) {
881                 std::string msg = fmt::sprintf("Camera %s: Error configuring composite "
882                         "streams: %s (%d)", mCameraIdStr.c_str(), strerror(-err), err);
883                 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
884                 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
885                 break;
886             }
887 
888             // Composite streams can only support offline mode in case all individual internal
889             // streams are also supported.
890             std::vector<int> internalStreams;
891             mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
892             offlineStreamIds->erase(
893                     std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
894                     [&internalStreams] (int streamId) {
895                         auto it = std::find(internalStreams.begin(), internalStreams.end(),
896                                 streamId);
897                         if (it != internalStreams.end()) {
898                             internalStreams.erase(it);
899                             return true;
900                         }
901 
902                         return false;}), offlineStreamIds->end());
903             if (internalStreams.empty()) {
904                 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
905             }
906         }
907 
908         for (const auto& offlineStreamId : *offlineStreamIds) {
909             mStreamInfoMap[offlineStreamId].supportsOffline = true;
910         }
911 
912         nsecs_t configureEnd = systemTime();
913         int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
914         mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
915                 false /*internalReconfig*/, configureDurationMs);
916     }
917 
918     return res;
919 }
920 
isSessionConfigurationSupported(const SessionConfiguration & sessionConfiguration,bool * status)921 binder::Status CameraDeviceClient::isSessionConfigurationSupported(
922         const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
923 
924     ATRACE_CALL();
925     binder::Status res;
926     status_t ret = OK;
927     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
928 
929     Mutex::Autolock icl(mBinderSerializationLock);
930 
931     if (!mDevice.get()) {
932         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
933     }
934 
935     if (status == nullptr) {
936         std::string msg = fmt::sprintf( "Camera %s: Invalid status!", mCameraIdStr.c_str());
937         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
938         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
939     }
940 
941     *status = false;
942     ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.c_str(),
943             sessionConfiguration, mOverrideForPerfClass, /*checkSessionParams*/false,
944             status);
945     switch (ret) {
946         case OK:
947             // Expected, do nothing.
948             break;
949         case INVALID_OPERATION: {
950                 std::string msg = fmt::sprintf(
951                         "Camera %s: Session configuration query not supported!",
952                         mCameraIdStr.c_str());
953                 ALOGD("%s: %s", __FUNCTION__, msg.c_str());
954                 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
955             }
956 
957             break;
958         default: {
959                 std::string msg = fmt::sprintf( "Camera %s: Error: %s (%d)", mCameraIdStr.c_str(),
960                         strerror(-ret), ret);
961                 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
962                 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
963                         msg.c_str());
964             }
965     }
966 
967     return res;
968 }
969 
deleteStream(int streamId)970 binder::Status CameraDeviceClient::deleteStream(int streamId) {
971     ATRACE_CALL();
972     ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
973 
974     binder::Status res;
975     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
976 
977     Mutex::Autolock icl(mBinderSerializationLock);
978 
979     if (!mDevice.get()) {
980         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
981     }
982 
983     bool isInput = false;
984     std::vector<SurfaceKey> surfaces;
985     std::vector<size_t> removedSurfaceIds;
986     ssize_t dIndex = NAME_NOT_FOUND;
987     ssize_t compositeIndex  = NAME_NOT_FOUND;
988 
989     if (mInputStream.configured && mInputStream.id == streamId) {
990         isInput = true;
991     } else {
992         // Guard against trying to delete non-created streams
993         for (size_t i = 0; i < mStreamMap.size(); ++i) {
994             if (streamId == mStreamMap.valueAt(i).streamId()) {
995                 surfaces.push_back(mStreamMap.keyAt(i));
996                 if (flags::camera_multi_client() && mSharedMode) {
997                     removedSurfaceIds.push_back(mStreamMap.valueAt(i).surfaceId());
998                 }
999             }
1000         }
1001 
1002         // See if this stream is one of the deferred streams.
1003         for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
1004             if (streamId == mDeferredStreams[i]) {
1005                 dIndex = i;
1006                 break;
1007             }
1008         }
1009 
1010         Mutex::Autolock l(mCompositeLock);
1011         for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
1012             if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
1013                 compositeIndex = i;
1014                 break;
1015             }
1016         }
1017 
1018         if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
1019             std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no such"
1020                     " stream created yet", mCameraIdStr.c_str(), streamId);
1021             ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1022             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1023         }
1024     }
1025 
1026 
1027     status_t err;
1028     if (flags::camera_multi_client() && mSharedMode) {
1029         err = mDevice->removeSharedSurfaces(streamId, removedSurfaceIds);
1030     } else {
1031         // Also returns BAD_VALUE if stream ID was not valid
1032         err = mDevice->deleteStream(streamId);
1033     }
1034 
1035     if (err != OK) {
1036         std::string msg = fmt::sprintf("Camera %s: Unexpected error %s (%d) when deleting stream "
1037                 "%d", mCameraIdStr.c_str(), strerror(-err), err, streamId);
1038         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1039         res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
1040     } else {
1041         if (isInput) {
1042             mInputStream.configured = false;
1043         } else {
1044             for (auto& surface : surfaces) {
1045                 mStreamMap.removeItem(surface);
1046             }
1047 
1048             mConfiguredOutputs.removeItem(streamId);
1049 
1050             if (dIndex != NAME_NOT_FOUND) {
1051                 mDeferredStreams.removeItemsAt(dIndex);
1052             }
1053 
1054             if (compositeIndex != NAME_NOT_FOUND) {
1055                 Mutex::Autolock l(mCompositeLock);
1056                 status_t ret;
1057                 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
1058                         != OK) {
1059                     std::string msg = fmt::sprintf("Camera %s: Unexpected error %s (%d) when "
1060                             "deleting composite stream %d", mCameraIdStr.c_str(), strerror(-err),
1061                             err, streamId);
1062                     ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1063                     res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
1064                 }
1065                 mCompositeStreamMap.removeItemsAt(compositeIndex);
1066             }
1067             for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
1068                 auto &streamSet = mapIt.second;
1069                 if (streamSet.find(streamId) != streamSet.end()) {
1070                     streamSet.erase(streamId);
1071                     break;
1072                 }
1073             }
1074         }
1075     }
1076 
1077     return res;
1078 }
1079 
createStream(const hardware::camera2::params::OutputConfiguration & outputConfiguration,int32_t * newStreamId)1080 binder::Status CameraDeviceClient::createStream(
1081         const hardware::camera2::params::OutputConfiguration &outputConfiguration,
1082         /*out*/
1083         int32_t* newStreamId) {
1084     ATRACE_CALL();
1085 
1086     binder::Status res;
1087     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1088 
1089     Mutex::Autolock icl(mBinderSerializationLock);
1090 
1091     if (!outputConfiguration.isComplete()) {
1092         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1093                 "OutputConfiguration isn't valid!");
1094     }
1095 
1096     const std::vector<ParcelableSurfaceType>& surfaces = outputConfiguration.getSurfaces();
1097     size_t numSurfaces = surfaces.size();
1098     bool deferredConsumer = outputConfiguration.isDeferred();
1099     bool isShared = outputConfiguration.isShared();
1100     const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
1101     bool deferredConsumerOnly = deferredConsumer && numSurfaces == 0;
1102     bool isMultiResolution = outputConfiguration.isMultiResolution();
1103     int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
1104     int64_t streamUseCase = outputConfiguration.getStreamUseCase();
1105     int timestampBase = outputConfiguration.getTimestampBase();
1106     int32_t colorSpace = outputConfiguration.getColorSpace();
1107     bool useReadoutTimestamp = outputConfiguration.useReadoutTimestamp();
1108 
1109     res = SessionConfigurationUtils::checkSurfaceType(numSurfaces, deferredConsumer,
1110             outputConfiguration.getSurfaceType(), /*isConfigurationComplete*/true);
1111     if (!res.isOk()) {
1112         return res;
1113     }
1114 
1115     if (!mDevice.get()) {
1116         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1117     }
1118     res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
1119             physicalCameraId, mCameraIdStr);
1120     if (!res.isOk()) {
1121         return res;
1122     }
1123 
1124     std::vector<SurfaceHolder> surfaceHolders;
1125     std::vector<SurfaceKey> surfaceKeys;
1126     std::vector<OutputStreamInfo> streamInfos;
1127     status_t err;
1128 
1129     // Create stream for deferred surface case.
1130     if (deferredConsumerOnly) {
1131         return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
1132     }
1133 
1134     OutputStreamInfo streamInfo;
1135     bool isStreamInfoValid = false;
1136     const std::vector<int32_t> &sensorPixelModesUsed =
1137             outputConfiguration.getSensorPixelModesUsed();
1138 
1139     for (auto& surface : surfaces) {
1140         // Don't create multiple streams for the same target surface
1141         SurfaceKey surfaceKey;
1142         status_t ret = getSurfaceKey(surface, &surfaceKey);
1143         if(ret != OK) {
1144             ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__,
1145                 mCameraIdStr.c_str());
1146             return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1147                 "Could not get the SurfaceKey");
1148         }
1149 
1150         ssize_t index = mStreamMap.indexOfKey(surfaceKey);
1151         if (index != NAME_NOT_FOUND) {
1152             std::string msg = std::string("Camera ") + mCameraIdStr
1153                     + ": Surface already has a stream created for it (ID "
1154                     + std::to_string(index) + ")";
1155             ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1156             return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.c_str());
1157         }
1158 
1159         int mirrorMode = outputConfiguration.getMirrorMode(surface);
1160         sp<Surface> outSurface;
1161         res = SessionConfigurationUtils::createConfiguredSurface(streamInfo,
1162                 isStreamInfoValid, outSurface,
1163                 flagtools::convertParcelableSurfaceTypeToSurface(surface), mCameraIdStr,
1164                 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
1165                 streamUseCase, timestampBase, mirrorMode, colorSpace, /*respectSurfaceSize*/false);
1166 
1167         if (!res.isOk())
1168             return res;
1169 
1170         if (!isStreamInfoValid) {
1171             isStreamInfoValid = true;
1172         }
1173 
1174         surfaceKeys.push_back(surfaceKey);
1175         surfaceHolders.push_back({outSurface, mirrorMode});
1176         if (flags::camera_multi_client() && mSharedMode) {
1177             streamInfos.push_back(streamInfo);
1178         }
1179     }
1180 
1181     int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
1182     std::vector<int> surfaceIds;
1183     if (flags::camera_multi_client() && mSharedMode) {
1184         err = mDevice->getSharedStreamId(streamInfo, &streamId);
1185         if (err == OK) {
1186             err = mDevice->addSharedSurfaces(streamId, streamInfos, surfaceHolders, &surfaceIds);
1187         }
1188     } else {
1189         bool isDepthCompositeStream =
1190                 camera3::DepthCompositeStream::isDepthCompositeStream(surfaceHolders[0].mSurface);
1191         bool isHeicCompositeStream = camera3::HeicCompositeStream::isHeicCompositeStream(
1192                 surfaceHolders[0].mSurface, mDevice->isCompositeHeicDisabled(),
1193                 mDevice->isCompositeHeicUltraHDRDisabled());
1194         bool isJpegRCompositeStream =
1195             camera3::JpegRCompositeStream::isJpegRCompositeStream(surfaceHolders[0].mSurface) &&
1196             !mDevice->isCompositeJpegRDisabled();
1197         if (isDepthCompositeStream || isHeicCompositeStream || isJpegRCompositeStream) {
1198             sp<CompositeStream> compositeStream;
1199             if (isDepthCompositeStream) {
1200                 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
1201             } else if (isHeicCompositeStream) {
1202                 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
1203             } else {
1204                 compositeStream = new camera3::JpegRCompositeStream(mDevice, getRemoteCallback());
1205             }
1206             err = compositeStream->createStream(surfaceHolders, deferredConsumer, streamInfo.width,
1207                 streamInfo.height, streamInfo.format,
1208                 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
1209                 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
1210                 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
1211                 streamInfo.colorSpace, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
1212                 useReadoutTimestamp);
1213             if (err == OK) {
1214                 Mutex::Autolock l(mCompositeLock);
1215                 SurfaceKey surfaceKey;
1216                 status_t ret = getSurfaceKey(surfaceHolders[0].mSurface, &surfaceKey);
1217                 if(ret != OK) {
1218                     ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__,
1219                         mCameraIdStr.c_str());
1220                     return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1221                         "Could not get the SurfaceKey");
1222                 }
1223                 mCompositeStreamMap.add(surfaceKey, compositeStream);
1224             }
1225         } else {
1226             err = mDevice->createStream(surfaceHolders, deferredConsumer, streamInfo.width,
1227                     streamInfo.height, streamInfo.format, streamInfo.dataSpace,
1228                     static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
1229                     &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
1230                     outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
1231                     /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
1232                     streamInfo.timestampBase, streamInfo.colorSpace, useReadoutTimestamp);
1233         }
1234     }
1235 
1236     if (err != OK) {
1237         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1238                 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1239                 mCameraIdStr.c_str(), streamInfo.width, streamInfo.height, streamInfo.format,
1240                 static_cast<int>(streamInfo.dataSpace), strerror(-err), err);
1241     } else {
1242         int i = 0;
1243         for (auto& surfaceKey : surfaceKeys) {
1244 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
1245             ALOGV("%s: mStreamMap add surfaceKey %" PRIu64 " streamId %d, surfaceId %d",
1246                   __FUNCTION__, surfaceKey, streamId, i);
1247 #else
1248             ALOGV("%s: mStreamMap add surfaceKey %p streamId %d, surfaceId %d",
1249                     __FUNCTION__, surfaceKey.get(), streamId, i);
1250 #endif
1251             mStreamMap.add(surfaceKey, StreamSurfaceId(streamId, surfaceIds[i]));
1252             i++;
1253         }
1254 
1255         mConfiguredOutputs.add(streamId, outputConfiguration);
1256         mStreamInfoMap[streamId] = streamInfo;
1257 
1258         ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
1259                     " (%d x %d) with format 0x%x.",
1260                   __FUNCTION__, mCameraIdStr.c_str(), streamId, streamInfo.width,
1261                   streamInfo.height, streamInfo.format);
1262 
1263         // Fill in mHighResolutionCameraIdToStreamIdSet map
1264         const std::string &cameraIdUsed =
1265                 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1266         // Only needed for high resolution sensors
1267         if (mHighResolutionSensors.find(cameraIdUsed) !=
1268                 mHighResolutionSensors.end()) {
1269             mHighResolutionCameraIdToStreamIdSet[cameraIdUsed].insert(streamId);
1270         }
1271 
1272         *newStreamId = streamId;
1273     }
1274 
1275     return res;
1276 }
1277 
createDeferredSurfaceStreamLocked(const hardware::camera2::params::OutputConfiguration & outputConfiguration,bool isShared,int * newStreamId)1278 binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1279         const hardware::camera2::params::OutputConfiguration &outputConfiguration,
1280         bool isShared,
1281         /*out*/
1282         int* newStreamId) {
1283     int width, height, format, surfaceType;
1284     uint64_t consumerUsage;
1285     android_dataspace dataSpace;
1286     int32_t colorSpace;
1287     status_t err;
1288     binder::Status res;
1289 
1290     if (!mDevice.get()) {
1291         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1292     }
1293     if (!outputConfiguration.isComplete()) {
1294         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1295                 "OutputConfiguration isn't valid!");
1296     }
1297 
1298     // Infer the surface info for deferred surface stream creation.
1299     width = outputConfiguration.getWidth();
1300     height = outputConfiguration.getHeight();
1301     surfaceType = outputConfiguration.getSurfaceType();
1302     format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1303     dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1304     colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
1305     // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1306     consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1307     if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1308         consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1309     }
1310     int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
1311     std::vector<SurfaceHolder> noSurface;
1312     std::vector<int> surfaceIds;
1313     const std::string &physicalCameraId = outputConfiguration.getPhysicalCameraId();
1314     const std::string &cameraIdUsed =
1315             physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1316     // Here, we override sensor pixel modes
1317     std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1318     const std::vector<int32_t> &sensorPixelModesUsed =
1319             outputConfiguration.getSensorPixelModesUsed();
1320     if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
1321             sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1322             &overriddenSensorPixelModesUsed) != OK) {
1323         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1324                 "sensor pixel modes used not valid for deferred stream");
1325     }
1326 
1327     err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1328             height, format, dataSpace,
1329             static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
1330             &streamId, physicalCameraId,
1331             overriddenSensorPixelModesUsed,
1332             &surfaceIds,
1333             outputConfiguration.getSurfaceSetID(), isShared,
1334             outputConfiguration.isMultiResolution(), consumerUsage,
1335             outputConfiguration.getDynamicRangeProfile(),
1336             outputConfiguration.getStreamUseCase(),
1337             outputConfiguration.useReadoutTimestamp());
1338 
1339     if (err != OK) {
1340         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1341                 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1342                 mCameraIdStr.c_str(), width, height, format, static_cast<int>(dataSpace),
1343                 strerror(-err), err);
1344     } else {
1345         // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1346         // a separate list to track. Once the deferred surface is set, this id will be
1347         // relocated to mStreamMap.
1348         mDeferredStreams.push_back(streamId);
1349         mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
1350                 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
1351                         overriddenSensorPixelModesUsed,
1352                         outputConfiguration.getDynamicRangeProfile(),
1353                         outputConfiguration.getStreamUseCase(),
1354                         outputConfiguration.getTimestampBase(),
1355                         colorSpace));
1356 
1357         ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
1358                 " (%d x %d) stream with format 0x%x.",
1359               __FUNCTION__, mCameraIdStr.c_str(), streamId, width, height, format);
1360 
1361         *newStreamId = streamId;
1362         // Fill in mHighResolutionCameraIdToStreamIdSet
1363         // Only needed for high resolution sensors
1364         if (mHighResolutionSensors.find(cameraIdUsed) !=
1365                 mHighResolutionSensors.end()) {
1366             mHighResolutionCameraIdToStreamIdSet[cameraIdUsed].insert(streamId);
1367         }
1368     }
1369     return res;
1370 }
1371 
createInputStream(int width,int height,int format,bool isMultiResolution,int32_t * newStreamId)1372 binder::Status CameraDeviceClient::createInputStream(
1373         int width, int height, int format, bool isMultiResolution,
1374         /*out*/
1375         int32_t* newStreamId) {
1376 
1377     ATRACE_CALL();
1378     ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1379             width, height, format, isMultiResolution);
1380 
1381     binder::Status res;
1382     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1383 
1384     Mutex::Autolock icl(mBinderSerializationLock);
1385 
1386     if (!mDevice.get()) {
1387         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1388     }
1389 
1390     if (mInputStream.configured) {
1391         std::string msg = fmt::sprintf("Camera %s: Already has an input stream "
1392                 "configured (ID %d)", mCameraIdStr.c_str(), mInputStream.id);
1393         ALOGE("%s: %s", __FUNCTION__, msg.c_str() );
1394         return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.c_str());
1395     }
1396 
1397     int streamId = -1;
1398     status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
1399     if (err == OK) {
1400         mInputStream.configured = true;
1401         mInputStream.width = width;
1402         mInputStream.height = height;
1403         mInputStream.format = format;
1404         mInputStream.id = streamId;
1405 
1406         ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1407                 __FUNCTION__, mCameraIdStr.c_str(), streamId);
1408 
1409         *newStreamId = streamId;
1410     } else {
1411         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1412                 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.c_str(),
1413                 strerror(-err), err);
1414     }
1415 
1416     return res;
1417 }
1418 
getInputSurface(view::Surface * inputSurface)1419 binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
1420 
1421     binder::Status res;
1422     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1423 
1424     if (inputSurface == NULL) {
1425         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
1426     }
1427 
1428     Mutex::Autolock icl(mBinderSerializationLock);
1429     if (!mDevice.get()) {
1430         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1431     }
1432 #if WB_CAMERA3_AND_PROCESSORS_WITH_DEPENDENCIES
1433     sp<Surface> surface;
1434     status_t err = mDevice->getInputSurface(&surface);
1435     if (err != OK) {
1436         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1437                 "Camera %s: Error getting input Surface: %s (%d)",
1438                 mCameraIdStr.c_str(), strerror(-err), err);
1439     } else {
1440         inputSurface->name = toString16("CameraInput");
1441         inputSurface->graphicBufferProducer = surface->getIGraphicBufferProducer();
1442     }
1443 #else
1444     sp<IGraphicBufferProducer> producer;
1445     status_t err = mDevice->getInputBufferProducer(&producer);
1446     if (err != OK) {
1447         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1448                 "Camera %s: Error getting input Surface: %s (%d)",
1449                 mCameraIdStr.c_str(), strerror(-err), err);
1450     } else {
1451         inputSurface->name = toString16("CameraInput");
1452         inputSurface->graphicBufferProducer = producer;
1453     }
1454 #endif
1455     return res;
1456 }
1457 
updateOutputConfiguration(int streamId,const hardware::camera2::params::OutputConfiguration & outputConfiguration)1458 binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1459         const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1460     ATRACE_CALL();
1461 
1462     binder::Status res;
1463     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1464 
1465     Mutex::Autolock icl(mBinderSerializationLock);
1466 
1467     if (!mDevice.get()) {
1468         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1469     }
1470     if (!outputConfiguration.isComplete()) {
1471         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1472                 "OutputConfiguration isn't valid!");
1473     }
1474 
1475     const std::vector<ParcelableSurfaceType>& surfaces = outputConfiguration.getSurfaces();
1476     const std::string& physicalCameraId = outputConfiguration.getPhysicalCameraId();
1477 
1478     auto producerCount = surfaces.size();
1479     if (producerCount == 0) {
1480         ALOGE("%s: surfaces must not be empty", __FUNCTION__);
1481         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1482                             "surfaces must not be empty");
1483     }
1484 
1485     // The first output is the one associated with the output configuration.
1486     // It should always be present, valid and the corresponding stream id should match.
1487     SurfaceKey surfaceKey;
1488     status_t ret = getSurfaceKey(surfaces[0], &surfaceKey);
1489     if(ret != OK) {
1490         ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__, mCameraIdStr.c_str());
1491         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, "Could not get the SurfaceKey");
1492     }
1493     ssize_t index = mStreamMap.indexOfKey(surfaceKey);
1494     if (index == NAME_NOT_FOUND) {
1495         ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1496         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1497                 "OutputConfiguration is invalid");
1498     }
1499     if (mStreamMap.valueFor(surfaceKey).streamId() != streamId) {
1500         ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1501                 __FUNCTION__, streamId, mStreamMap.valueFor(surfaceKey).streamId());
1502         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1503                 "Stream id is invalid");
1504     }
1505 
1506     std::vector<size_t> removedSurfaceIds;
1507     std::vector<SurfaceKey> removedOutputs;
1508     std::vector<SurfaceHolder> newOutputs;
1509     std::vector<OutputStreamInfo> streamInfos;
1510     KeyedVector<SurfaceKey, ParcelableSurfaceType> newOutputsMap;
1511     for (auto& surface : surfaces) {
1512         SurfaceKey surfaceKey;
1513         status_t ret = getSurfaceKey(surface, &surfaceKey);
1514         if(ret != OK) {
1515             ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__,
1516                  mCameraIdStr.c_str());
1517             return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1518                  "Could not get the SurfaceKey");
1519         }
1520         newOutputsMap.add(surfaceKey, surface);
1521     }
1522 
1523     for (size_t i = 0; i < mStreamMap.size(); i++) {
1524         ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1525         if (idx == NAME_NOT_FOUND) {
1526             if (mStreamMap[i].streamId() == streamId) {
1527                 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1528                 removedOutputs.push_back(mStreamMap.keyAt(i));
1529             }
1530         } else {
1531             if (mStreamMap[i].streamId() != streamId) {
1532                 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1533                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1534                         "Target Surface is invalid");
1535             }
1536             newOutputsMap.removeItemsAt(idx);
1537         }
1538     }
1539     const std::vector<int32_t> &sensorPixelModesUsed =
1540             outputConfiguration.getSensorPixelModesUsed();
1541     int64_t streamUseCase = outputConfiguration.getStreamUseCase();
1542     int timestampBase = outputConfiguration.getTimestampBase();
1543     int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
1544     int32_t colorSpace = outputConfiguration.getColorSpace();
1545 
1546     for (size_t i = 0; i < newOutputsMap.size(); i++) {
1547         OutputStreamInfo outInfo;
1548         sp<Surface> outSurface;
1549         int mirrorMode = outputConfiguration.getMirrorMode(newOutputsMap.valueAt(i));
1550         res = SessionConfigurationUtils::createConfiguredSurface(
1551                 outInfo,
1552                 /*isStreamInfoValid*/ false, outSurface,
1553                 flagtools::convertParcelableSurfaceTypeToSurface(newOutputsMap.valueAt(i)),
1554                 mCameraIdStr, mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed,
1555                 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode, colorSpace,
1556                 /*respectSurfaceSize*/ false);
1557         if (!res.isOk()) return res;
1558 
1559         streamInfos.push_back(outInfo);
1560         newOutputs.push_back({outSurface, mirrorMode});
1561     }
1562 
1563     //Trivial case no changes required
1564     if (removedSurfaceIds.empty() && newOutputs.empty()) {
1565         return binder::Status::ok();
1566     }
1567 
1568     KeyedVector<sp<Surface>, size_t> outputMap;
1569     ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds, &outputMap);
1570     if (ret != OK) {
1571         switch (ret) {
1572             case NAME_NOT_FOUND:
1573             case BAD_VALUE:
1574             case -EBUSY:
1575                 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1576                         "Camera %s: Error updating stream: %s (%d)",
1577                         mCameraIdStr.c_str(), strerror(ret), ret);
1578                 break;
1579             default:
1580                 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1581                         "Camera %s: Error updating stream: %s (%d)",
1582                         mCameraIdStr.c_str(), strerror(ret), ret);
1583                 break;
1584         }
1585     } else {
1586         for (const auto &it : removedOutputs) {
1587             mStreamMap.removeItem(it);
1588         }
1589 
1590         for (size_t i = 0; i < outputMap.size(); i++) {
1591             SurfaceKey surfaceKey;
1592             status_t ret = getSurfaceKey(outputMap.keyAt(i), &surfaceKey);
1593             if(ret != OK) {
1594                 ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__,
1595                      mCameraIdStr.c_str());
1596                 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1597                      "Could not get the SurfaceKey");
1598             }
1599             mStreamMap.add(surfaceKey, StreamSurfaceId(streamId, outputMap.valueAt(i)));
1600         }
1601 
1602         mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1603 
1604         ALOGV("%s: Camera %s: Successful stream ID %d update",
1605                   __FUNCTION__, mCameraIdStr.c_str(), streamId);
1606     }
1607 
1608     return res;
1609 }
1610 
1611 // Create a request object from a template.
createDefaultRequest(int templateId,hardware::camera2::impl::CameraMetadataNative * request)1612 binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1613         /*out*/
1614         hardware::camera2::impl::CameraMetadataNative* request)
1615 {
1616     ATRACE_CALL();
1617     ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1618 
1619     binder::Status res;
1620     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1621 
1622     Mutex::Autolock icl(mBinderSerializationLock);
1623 
1624     if (!mDevice.get()) {
1625         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1626     }
1627 
1628     status_t err;
1629     camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1630     res = SessionConfigurationUtils::mapRequestTemplateFromClient(
1631             mCameraIdStr, templateId, &tempId);
1632     if (!res.isOk()) return res;
1633 
1634     CameraMetadata metadata;
1635     if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
1636         request != NULL) {
1637 
1638         request->swap(metadata);
1639     } else if (err == BAD_VALUE) {
1640         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1641                 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1642                 mCameraIdStr.c_str(), templateId, strerror(-err), err);
1643 
1644     } else {
1645         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1646                 "Camera %s: Error creating default request for template %d: %s (%d)",
1647                 mCameraIdStr.c_str(), templateId, strerror(-err), err);
1648     }
1649     return res;
1650 }
1651 
getCameraInfo(hardware::camera2::impl::CameraMetadataNative * info)1652 binder::Status CameraDeviceClient::getCameraInfo(
1653         /*out*/
1654         hardware::camera2::impl::CameraMetadataNative* info)
1655 {
1656     ATRACE_CALL();
1657     ALOGV("%s", __FUNCTION__);
1658 
1659     binder::Status res;
1660 
1661     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1662 
1663     Mutex::Autolock icl(mBinderSerializationLock);
1664 
1665     if (!mDevice.get()) {
1666         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1667     }
1668 
1669     if (info != NULL) {
1670         *info = mDevice->info(); // static camera metadata
1671         // TODO: merge with device-specific camera metadata
1672     }
1673 
1674     return res;
1675 }
1676 
waitUntilIdle()1677 binder::Status CameraDeviceClient::waitUntilIdle()
1678 {
1679     ATRACE_CALL();
1680     ALOGV("%s", __FUNCTION__);
1681 
1682     binder::Status res;
1683     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1684 
1685     Mutex::Autolock icl(mBinderSerializationLock);
1686 
1687     if (!mDevice.get()) {
1688         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1689     }
1690 
1691     // FIXME: Also need check repeating burst.
1692     Mutex::Autolock idLock(mStreamingRequestIdLock);
1693     if (mStreamingRequestId != REQUEST_ID_NONE) {
1694         std::string msg = fmt::sprintf(
1695             "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1696             mCameraIdStr.c_str());
1697         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1698         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.c_str());
1699     }
1700     status_t err = mDevice->waitUntilDrained();
1701     if (err != OK) {
1702         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1703                 "Camera %s: Error waiting to drain: %s (%d)",
1704                 mCameraIdStr.c_str(), strerror(-err), err);
1705     }
1706     ALOGV("%s Done", __FUNCTION__);
1707     return res;
1708 }
1709 
flush(int64_t * lastFrameNumber)1710 binder::Status CameraDeviceClient::flush(
1711         /*out*/
1712         int64_t* lastFrameNumber) {
1713     ATRACE_CALL();
1714     ALOGV("%s", __FUNCTION__);
1715 
1716     binder::Status res;
1717     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1718 
1719     Mutex::Autolock icl(mBinderSerializationLock);
1720 
1721     if (!mDevice.get()) {
1722         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1723     }
1724 
1725     Mutex::Autolock idLock(mStreamingRequestIdLock);
1726     mStreamingRequestId = REQUEST_ID_NONE;
1727     status_t err = mDevice->flush(lastFrameNumber);
1728     if (err != OK) {
1729         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1730                 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.c_str(), strerror(-err),
1731                 err);
1732     }
1733     if (flags::camera_multi_client() && mSharedMode) {
1734         mSharedRequestMap.clear();
1735         mStreamingRequestLastFrameNumber = *lastFrameNumber;
1736     }
1737     return res;
1738 }
1739 
prepare(int streamId)1740 binder::Status CameraDeviceClient::prepare(int streamId) {
1741     ATRACE_CALL();
1742     ALOGV("%s stream id %d", __FUNCTION__, streamId);
1743 
1744     binder::Status res;
1745     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1746 
1747     Mutex::Autolock icl(mBinderSerializationLock);
1748 
1749     // Guard against trying to prepare non-created streams
1750     ssize_t index = NAME_NOT_FOUND;
1751     for (size_t i = 0; i < mStreamMap.size(); ++i) {
1752         if (streamId == mStreamMap.valueAt(i).streamId()) {
1753             index = i;
1754             break;
1755         }
1756     }
1757 
1758     if (index == NAME_NOT_FOUND) {
1759         std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1760               "with that ID exists", mCameraIdStr.c_str(), streamId);
1761         ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1762         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1763     }
1764 
1765     // Also returns BAD_VALUE if stream ID was not valid, or stream already
1766     // has been used
1767     status_t err = mDevice->prepare(streamId);
1768     if (err == BAD_VALUE) {
1769         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1770                 "Camera %s: Stream %d has already been used, and cannot be prepared",
1771                 mCameraIdStr.c_str(), streamId);
1772     } else if (err != OK) {
1773         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1774                 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
1775                 strerror(-err), err);
1776     }
1777     return res;
1778 }
1779 
prepare2(int maxCount,int streamId)1780 binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
1781     ATRACE_CALL();
1782     ALOGV("%s stream id %d", __FUNCTION__, streamId);
1783 
1784     binder::Status res;
1785     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1786 
1787     Mutex::Autolock icl(mBinderSerializationLock);
1788 
1789     // Guard against trying to prepare non-created streams
1790     ssize_t index = NAME_NOT_FOUND;
1791     for (size_t i = 0; i < mStreamMap.size(); ++i) {
1792         if (streamId == mStreamMap.valueAt(i).streamId()) {
1793             index = i;
1794             break;
1795         }
1796     }
1797 
1798     if (index == NAME_NOT_FOUND) {
1799         std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1800               "with that ID exists", mCameraIdStr.c_str(), streamId);
1801         ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1802         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1803     }
1804 
1805     if (maxCount <= 0) {
1806         std::string msg = fmt::sprintf("Camera %s: maxCount (%d) must be greater than 0",
1807                 mCameraIdStr.c_str(), maxCount);
1808         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
1809         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1810     }
1811 
1812     // Also returns BAD_VALUE if stream ID was not valid, or stream already
1813     // has been used
1814     status_t err = mDevice->prepare(maxCount, streamId);
1815     if (err == BAD_VALUE) {
1816         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1817                 "Camera %s: Stream %d has already been used, and cannot be prepared",
1818                 mCameraIdStr.c_str(), streamId);
1819     } else if (err != OK) {
1820         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1821                 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
1822                 strerror(-err), err);
1823     }
1824 
1825     return res;
1826 }
1827 
tearDown(int streamId)1828 binder::Status CameraDeviceClient::tearDown(int streamId) {
1829     ATRACE_CALL();
1830     ALOGV("%s", __FUNCTION__);
1831 
1832     binder::Status res;
1833     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1834 
1835     Mutex::Autolock icl(mBinderSerializationLock);
1836 
1837     // Guard against trying to prepare non-created streams
1838     ssize_t index = NAME_NOT_FOUND;
1839     for (size_t i = 0; i < mStreamMap.size(); ++i) {
1840         if (streamId == mStreamMap.valueAt(i).streamId()) {
1841             index = i;
1842             break;
1843         }
1844     }
1845 
1846     if (index == NAME_NOT_FOUND) {
1847         std::string msg = fmt::sprintf("Camera %s: Invalid stream ID (%d) specified, no stream "
1848               "with that ID exists", mCameraIdStr.c_str(), streamId);
1849         ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1850         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1851     }
1852 
1853     // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1854     // use
1855     status_t err = mDevice->tearDown(streamId);
1856     if (err == BAD_VALUE) {
1857         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1858                 "Camera %s: Stream %d is still in use, cannot be torn down",
1859                 mCameraIdStr.c_str(), streamId);
1860     } else if (err != OK) {
1861         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1862                 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.c_str(), streamId,
1863                 strerror(-err), err);
1864     }
1865 
1866     return res;
1867 }
1868 
finalizeOutputConfigurations(int32_t streamId,const hardware::camera2::params::OutputConfiguration & outputConfiguration)1869 binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
1870         const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1871     ATRACE_CALL();
1872 
1873     binder::Status res;
1874     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1875 
1876     Mutex::Autolock icl(mBinderSerializationLock);
1877 
1878     if (!outputConfiguration.isComplete()) {
1879         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1880                 "OutputConfiguration isn't valid!");
1881     }
1882 
1883     const std::vector<ParcelableSurfaceType>& surfaces = outputConfiguration.getSurfaces();
1884     const std::string& physicalId = outputConfiguration.getPhysicalCameraId();
1885 
1886     if (surfaces.size() == 0) {
1887         ALOGE("%s: surfaces must not be empty", __FUNCTION__);
1888         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1889     }
1890 
1891     // streamId should be in mStreamMap if this stream already has a surface attached
1892     // to it. Otherwise, it should be in mDeferredStreams.
1893     bool streamIdConfigured = false;
1894     ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1895     for (size_t i = 0; i < mStreamMap.size(); i++) {
1896         if (mStreamMap.valueAt(i).streamId() == streamId) {
1897             streamIdConfigured = true;
1898             break;
1899         }
1900     }
1901     for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1902         if (streamId == mDeferredStreams[i]) {
1903             deferredStreamIndex = i;
1904             break;
1905         }
1906 
1907     }
1908     if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1909         std::string msg = fmt::sprintf("Camera %s: deferred surface is set to a unknown stream"
1910                 "(ID %d)", mCameraIdStr.c_str(), streamId);
1911         ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1912         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1913     }
1914 
1915     if (mStreamInfoMap[streamId].finalized) {
1916         std::string msg = fmt::sprintf("Camera %s: finalizeOutputConfigurations has been called"
1917                 " on stream ID %d", mCameraIdStr.c_str(), streamId);
1918         ALOGW("%s: %s", __FUNCTION__, msg.c_str());
1919         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
1920     }
1921 
1922     if (!mDevice.get()) {
1923         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1924     }
1925 
1926     std::vector<SurfaceHolder> consumerSurfaceHolders;
1927     const std::vector<int32_t>& sensorPixelModesUsed =
1928             outputConfiguration.getSensorPixelModesUsed();
1929     int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
1930     int32_t colorSpace = outputConfiguration.getColorSpace();
1931     int64_t streamUseCase = outputConfiguration.getStreamUseCase();
1932     int timestampBase = outputConfiguration.getTimestampBase();
1933 
1934     for (auto& surface : surfaces) {
1935         // Don't create multiple streams for the same target surface
1936         SurfaceKey surfaceKey;
1937         status_t ret = getSurfaceKey(surface, &surfaceKey);
1938         if(ret != OK) {
1939             ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__,
1940                  mCameraIdStr.c_str());
1941             return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1942                  "Could not get the SurfaceKey");
1943         }
1944         ssize_t index = mStreamMap.indexOfKey(surfaceKey);
1945         if (index != NAME_NOT_FOUND) {
1946             ALOGV("Camera %s: Surface already has a stream created "
1947                   " for it (ID %zd)",
1948                   mCameraIdStr.c_str(), index);
1949             continue;
1950         }
1951 
1952         sp<Surface> outSurface;
1953         int mirrorMode = outputConfiguration.getMirrorMode(surface);
1954         res = SessionConfigurationUtils::createConfiguredSurface(
1955                 mStreamInfoMap[streamId], true /*isStreamInfoValid*/, outSurface,
1956                 flagtools::convertParcelableSurfaceTypeToSurface(surface), mCameraIdStr,
1957                 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
1958                 streamUseCase, timestampBase, mirrorMode, colorSpace, /*respectSurfaceSize*/ false);
1959 
1960         if (!res.isOk()) return res;
1961 
1962         consumerSurfaceHolders.push_back({outSurface, mirrorMode});
1963     }
1964     // Gracefully handle case where finalizeOutputConfigurations is called
1965     // without any new surface.
1966     if (consumerSurfaceHolders.size() == 0) {
1967         mStreamInfoMap[streamId].finalized = true;
1968         return res;
1969     }
1970 
1971     // Finish the deferred stream configuration with the surface.
1972     status_t err;
1973     std::vector<int> consumerSurfaceIds;
1974     err = mDevice->setConsumerSurfaces(streamId, consumerSurfaceHolders, &consumerSurfaceIds);
1975     if (err == OK) {
1976         for (size_t i = 0; i < consumerSurfaceHolders.size(); i++) {
1977             SurfaceKey surfaceKey;
1978             status_t ret = getSurfaceKey(consumerSurfaceHolders[i].mSurface, &surfaceKey);
1979             if(ret != OK) {
1980                 ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__,
1981                      mCameraIdStr.c_str());
1982                 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1983                      "Could not get the SurfaceKey");
1984             }
1985 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
1986             ALOGV("%s: mStreamMap add surface_key %" PRIu64 " streamId %d, surfaceId %d",
1987                   __FUNCTION__, surfaceKey, streamId, consumerSurfaceIds[i]);
1988 #else
1989             ALOGV("%s: mStreamMap add surface_key %p streamId %d, surfaceId %d", __FUNCTION__,
1990                     surfaceKey.get(), streamId, consumerSurfaceIds[i]);
1991 #endif
1992             mStreamMap.add(surfaceKey, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1993         }
1994         if (deferredStreamIndex != NAME_NOT_FOUND) {
1995             mDeferredStreams.removeItemsAt(deferredStreamIndex);
1996         }
1997         mStreamInfoMap[streamId].finalized = true;
1998         mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1999     } else if (err == NO_INIT) {
2000         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2001                 "Camera %s: Deferred surface is invalid: %s (%d)",
2002                 mCameraIdStr.c_str(), strerror(-err), err);
2003     } else {
2004         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
2005                 "Camera %s: Error setting output stream deferred surface: %s (%d)",
2006                 mCameraIdStr.c_str(), strerror(-err), err);
2007     }
2008 
2009     return res;
2010 }
2011 
setCameraAudioRestriction(int32_t mode)2012 binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
2013     ATRACE_CALL();
2014     binder::Status res;
2015     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
2016 
2017     if (!isValidAudioRestriction(mode)) {
2018         std::string msg = fmt::sprintf("Camera %s: invalid audio restriction mode %d",
2019                 mCameraIdStr.c_str(), mode);
2020         ALOGW("%s: %s", __FUNCTION__, msg.c_str());
2021         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
2022     }
2023 
2024     Mutex::Autolock icl(mBinderSerializationLock);
2025     BasicClient::setAudioRestriction(mode);
2026     return binder::Status::ok();
2027 }
2028 
CreateMetadataQueue(std::unique_ptr<MetadataQueue> * metadata_queue,size_t size_bytes)2029 status_t CameraDeviceClient::CreateMetadataQueue(
2030         std::unique_ptr<MetadataQueue>* metadata_queue, size_t size_bytes) {
2031         if (metadata_queue == nullptr) {
2032             ALOGE("%s: metadata_queue is nullptr", __FUNCTION__);
2033             return BAD_VALUE;
2034         }
2035 
2036         *metadata_queue =
2037                 std::make_unique<MetadataQueue>(size_bytes,
2038                         /*configureEventFlagWord*/ false);
2039         if (!(*metadata_queue)->isValid()) {
2040             ALOGE("%s: Creating metadata queue (size %zu) failed.", __FUNCTION__, size_bytes);
2041             return NO_INIT;
2042         }
2043 
2044         return OK;
2045 }
2046 
getCaptureResultMetadataQueue(android::hardware::common::fmq::MQDescriptor<int8_t,android::hardware::common::fmq::SynchronizedReadWrite> * aidl_return)2047 binder::Status CameraDeviceClient::getCaptureResultMetadataQueue(
2048           android::hardware::common::fmq::MQDescriptor<
2049           int8_t, android::hardware::common::fmq::SynchronizedReadWrite>* aidl_return) {
2050 
2051     *aidl_return = mResultMetadataQueue->dupeDesc();
2052     return binder::Status::ok();
2053 }
2054 
getGlobalAudioRestriction(int32_t * outMode)2055 binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
2056     ATRACE_CALL();
2057     binder::Status res;
2058     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
2059     Mutex::Autolock icl(mBinderSerializationLock);
2060     if (outMode != nullptr) {
2061         *outMode = BasicClient::getServiceAudioRestriction();
2062     }
2063     return binder::Status::ok();
2064 }
2065 
isPrimaryClient(bool * isPrimary)2066 binder::Status CameraDeviceClient::isPrimaryClient(/*out*/bool* isPrimary) {
2067     ATRACE_CALL();
2068     binder::Status res =  binder::Status::ok();
2069     if (!flags::camera_multi_client()) {
2070         return res;
2071     }
2072     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
2073     if (isPrimary != nullptr) {
2074         status_t ret = BasicClient::isPrimaryClient(isPrimary);
2075         return  binder::Status::fromStatusT(ret);
2076     }
2077     return res;
2078 }
2079 
setCameraServiceWatchdog(bool enabled)2080 status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
2081     return mDevice->setCameraServiceWatchdog(enabled);
2082 }
2083 
setRotateAndCropOverride(uint8_t rotateAndCrop,bool fromHal)2084 status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop, bool fromHal) {
2085     if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
2086 
2087     return mDevice->setRotateAndCropAutoBehavior(
2088         static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop), fromHal);
2089 }
2090 
setAutoframingOverride(uint8_t autoframingValue)2091 status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
2092     if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
2093 
2094     return mDevice->setAutoframingAutoBehavior(
2095         static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
2096 }
2097 
supportsCameraMute()2098 bool CameraDeviceClient::supportsCameraMute() {
2099     return mDevice->supportsCameraMute();
2100 }
2101 
setCameraMute(bool enabled)2102 status_t CameraDeviceClient::setCameraMute(bool enabled) {
2103     return mDevice->setCameraMute(enabled);
2104 }
2105 
setStreamUseCaseOverrides(const std::vector<int64_t> & useCaseOverrides)2106 void CameraDeviceClient::setStreamUseCaseOverrides(
2107         const std::vector<int64_t>& useCaseOverrides) {
2108     mDevice->setStreamUseCaseOverrides(useCaseOverrides);
2109 }
2110 
clearStreamUseCaseOverrides()2111 void CameraDeviceClient::clearStreamUseCaseOverrides() {
2112     mDevice->clearStreamUseCaseOverrides();
2113 }
2114 
supportsZoomOverride()2115 bool CameraDeviceClient::supportsZoomOverride() {
2116     return mDevice->supportsZoomOverride();
2117 }
2118 
setZoomOverride(int32_t zoomOverride)2119 status_t CameraDeviceClient::setZoomOverride(int32_t zoomOverride) {
2120     return mDevice->setZoomOverride(zoomOverride);
2121 }
2122 
switchToOffline(const sp<hardware::camera2::ICameraDeviceCallbacks> & cameraCb,const std::vector<int> & offlineOutputIds,sp<hardware::camera2::ICameraOfflineSession> * session)2123 binder::Status CameraDeviceClient::switchToOffline(
2124         const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
2125         const std::vector<int>& offlineOutputIds,
2126         /*out*/
2127         sp<hardware::camera2::ICameraOfflineSession>* session) {
2128     ATRACE_CALL();
2129 
2130     binder::Status res;
2131     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
2132 
2133     Mutex::Autolock icl(mBinderSerializationLock);
2134 
2135     if (!mDevice.get()) {
2136         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
2137     }
2138 
2139     if (offlineOutputIds.empty()) {
2140         std::string msg = "Offline surfaces must not be empty";
2141         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
2142         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
2143     }
2144 
2145     if (session == nullptr) {
2146         std::string msg = "Invalid offline session";
2147         ALOGE("%s: %s", __FUNCTION__, msg.c_str());
2148         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
2149     }
2150 
2151     std::vector<int32_t> offlineStreamIds;
2152     offlineStreamIds.reserve(offlineOutputIds.size());
2153     KeyedVector<SurfaceKey, sp<CompositeStream>> offlineCompositeStreamMap;
2154     for (const auto& streamId : offlineOutputIds) {
2155         ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
2156         if (index == NAME_NOT_FOUND) {
2157             std::string msg = fmt::sprintf("Offline surface with id: %d is not registered",
2158                     streamId);
2159             ALOGE("%s: %s", __FUNCTION__, msg.c_str());
2160             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
2161         }
2162 
2163         if (!mStreamInfoMap[streamId].supportsOffline) {
2164             std::string msg = fmt::sprintf("Offline surface with id: %d doesn't support "
2165                     "offline mode", streamId);
2166             ALOGE("%s: %s", __FUNCTION__, msg.c_str());
2167             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.c_str());
2168         }
2169 
2170         Mutex::Autolock l(mCompositeLock);
2171         bool isCompositeStream = false;
2172 
2173         for (const auto& surface : mConfiguredOutputs.valueAt(index).getSurfaces()) {
2174 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
2175             sp<Surface> s = surface.toSurface();
2176 #else
2177             sp<Surface> s = new Surface(surface, false /*controlledByApp*/);
2178 #endif
2179             isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
2180                                 camera3::HeicCompositeStream::isHeicCompositeStream(
2181                                         s, mDevice->isCompositeHeicDisabled(),
2182                                         mDevice->isCompositeHeicUltraHDRDisabled()) ||
2183                                 (camera3::JpegRCompositeStream::isJpegRCompositeStream(s) &&
2184                                  !mDevice->isCompositeJpegRDisabled());
2185             if (isCompositeStream) {
2186                 SurfaceKey surfaceKey;
2187                 status_t ret = getSurfaceKey(surface, &surfaceKey);
2188                 if(ret != OK) {
2189                     ALOGE("%s: Camera %s: Could not get the SurfaceKey", __FUNCTION__,
2190                         mCameraIdStr.c_str());
2191                     return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
2192                         "Could not get the SurfaceKey");
2193                 }
2194                 auto compositeIdx = mCompositeStreamMap.indexOfKey(surfaceKey);
2195                 if (compositeIdx == NAME_NOT_FOUND) {
2196                     ALOGE("%s: Unknown composite stream", __FUNCTION__);
2197                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
2198                                         "Unknown composite stream");
2199                 }
2200 
2201                 mCompositeStreamMap.valueAt(compositeIdx)
2202                         ->insertCompositeStreamIds(&offlineStreamIds);
2203                 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
2204                                               mCompositeStreamMap.valueAt(compositeIdx));
2205                 break;
2206             }
2207         }
2208 
2209         if (!isCompositeStream) {
2210             offlineStreamIds.push_back(streamId);
2211         }
2212     }
2213 
2214     sp<CameraOfflineSessionBase> offlineSession;
2215     auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
2216     if (ret != OK) {
2217         return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2218                 "Camera %s: Error switching to offline mode: %s (%d)",
2219                 mCameraIdStr.c_str(), strerror(ret), ret);
2220     }
2221 
2222     sp<CameraOfflineSessionClient> offlineClient;
2223     if (offlineSession.get() != nullptr) {
2224         offlineClient = new CameraOfflineSessionClient(
2225                 sCameraService, offlineSession, offlineCompositeStreamMap, cameraCb,
2226                 mAttributionAndPermissionUtils, mClientAttribution, mCallingPid, mCameraIdStr,
2227                 mCameraFacing, mOrientation, mServicePid, /*sharedMode*/false);
2228         ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
2229     }
2230 
2231     if (ret == OK) {
2232         // A successful offline session switch must reset the current camera client
2233         // and release any resources occupied by previously configured streams.
2234         mStreamMap.clear();
2235         mConfiguredOutputs.clear();
2236         mDeferredStreams.clear();
2237         mStreamInfoMap.clear();
2238         Mutex::Autolock l(mCompositeLock);
2239         mCompositeStreamMap.clear();
2240         mInputStream = {false, 0, 0, 0, 0};
2241     } else {
2242         // In case we failed to register the offline client, ensure that it still initialized
2243         // so that all failing requests can return back correctly once the object is released.
2244         offlineClient->initialize(nullptr /*cameraProviderManager*/, std::string()/*monitorTags*/);
2245 
2246         switch(ret) {
2247             case BAD_VALUE:
2248                 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2249                         "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
2250             case TIMED_OUT:
2251                 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
2252                         "Camera \"%s\" is already open", mCameraIdStr.c_str());
2253             default:
2254                 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
2255                         "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
2256                         strerror(-ret), ret);
2257         }
2258     }
2259 
2260     *session = offlineClient;
2261 
2262     return binder::Status::ok();
2263 }
2264 
dump(int fd,const Vector<String16> & args)2265 status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
2266     return BasicClient::dump(fd, args);
2267 }
2268 
dumpClient(int fd,const Vector<String16> & args)2269 status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
2270     dprintf(fd, "  CameraDeviceClient[%s] (%p) dump:\n",
2271             mCameraIdStr.c_str(),
2272             (getRemoteCallback() != NULL ?
2273                     IInterface::asBinder(getRemoteCallback()).get() : NULL) );
2274     dprintf(fd, "    Current client UID %u\n", getClientUid());
2275 
2276     dprintf(fd, "    State:\n");
2277     dprintf(fd, "      Request ID counter: %d\n", mRequestIdCounter);
2278     if (mInputStream.configured) {
2279         dprintf(fd, "      Current input stream ID: %d\n", mInputStream.id);
2280     } else {
2281         dprintf(fd, "      No input stream configured.\n");
2282     }
2283     if (!mStreamMap.isEmpty()) {
2284         dprintf(fd, "      Current output stream/surface IDs:\n");
2285         for (size_t i = 0; i < mStreamMap.size(); i++) {
2286             dprintf(fd, "        Stream %d Surface %d\n",
2287                                 mStreamMap.valueAt(i).streamId(),
2288                                 mStreamMap.valueAt(i).surfaceId());
2289         }
2290     } else if (!mDeferredStreams.isEmpty()) {
2291         dprintf(fd, "      Current deferred surface output stream IDs:\n");
2292         for (auto& streamId : mDeferredStreams) {
2293             dprintf(fd, "        Stream %d\n", streamId);
2294         }
2295     } else {
2296         dprintf(fd, "      No output streams configured.\n");
2297     }
2298     // TODO: print dynamic/request section from most recent requests
2299     mFrameProcessor->dump(fd, args);
2300 
2301     return dumpDevice(fd, args);
2302 }
2303 
startWatchingTags(const std::string & tags,int out)2304 status_t CameraDeviceClient::startWatchingTags(const std::string &tags, int out) {
2305     sp<CameraDeviceBase> device = mDevice;
2306     if (!device) {
2307         dprintf(out, "  Device is detached.");
2308         return OK;
2309     }
2310     device->startWatchingTags(tags);
2311     return OK;
2312 }
2313 
stopWatchingTags(int out)2314 status_t CameraDeviceClient::stopWatchingTags(int out) {
2315     sp<CameraDeviceBase> device = mDevice;
2316     if (!device) {
2317         dprintf(out, "  Device is detached.");
2318         return OK;
2319     }
2320     device->stopWatchingTags();
2321     return OK;
2322 }
2323 
dumpWatchedEventsToVector(std::vector<std::string> & out)2324 status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
2325     sp<CameraDeviceBase> device = mDevice;
2326     if (!device) {
2327         return OK;
2328     }
2329     device->dumpWatchedEventsToVector(out);
2330     return OK;
2331 }
2332 
notifyError(int32_t errorCode,const CaptureResultExtras & resultExtras)2333 void CameraDeviceClient::notifyError(int32_t errorCode,
2334                                      const CaptureResultExtras& resultExtras) {
2335     // Thread safe. Don't bother locking.
2336     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2337     bool skipClientNotification = false;
2338     if (flags::camera_multi_client() && mSharedMode && (resultExtras.requestId != -1)) {
2339         int clientReqId;
2340         bool matchStreamingRequest = matchSharedStreamingRequest(resultExtras.requestId);
2341         bool matchCaptureRequest = matchSharedCaptureRequest(resultExtras.requestId);
2342         if (matchStreamingRequest) {
2343             clientReqId = mSharedStreamingRequest.second;
2344         } else if (matchCaptureRequest) {
2345             clientReqId = mSharedRequestMap[resultExtras.requestId];
2346             mSharedRequestMap.erase(resultExtras.requestId);
2347         } else {
2348             return;
2349         }
2350         CaptureResultExtras mutableResultExtras = resultExtras;
2351         mutableResultExtras.requestId = clientReqId;
2352         if (remoteCb != 0) {
2353             remoteCb->onDeviceError(errorCode, mutableResultExtras);
2354         }
2355         return;
2356     }
2357     {
2358         // Access to the composite stream map must be synchronized
2359         Mutex::Autolock l(mCompositeLock);
2360         // Composites can have multiple internal streams. Error notifications coming from such
2361         // internal streams may need to remain within camera service.
2362         for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2363             skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
2364                     resultExtras);
2365         }
2366     }
2367 
2368     if ((remoteCb != 0) && (!skipClientNotification)) {
2369         remoteCb->onDeviceError(errorCode, resultExtras);
2370     }
2371 }
2372 
notifyRepeatingRequestError(long lastFrameNumber)2373 void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
2374     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2375 
2376     if (remoteCb != 0) {
2377         remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
2378     }
2379 
2380     Mutex::Autolock idLock(mStreamingRequestIdLock);
2381     mStreamingRequestId = REQUEST_ID_NONE;
2382 }
2383 
notifyIdle(int64_t requestCount,int64_t resultErrorCount,bool deviceError,std::pair<int32_t,int32_t> mostRequestedFpsRange,const std::vector<hardware::CameraStreamStats> & streamStats)2384 void CameraDeviceClient::notifyIdle(
2385         int64_t requestCount, int64_t resultErrorCount, bool deviceError,
2386         std::pair<int32_t, int32_t> mostRequestedFpsRange,
2387         const std::vector<hardware::CameraStreamStats>& streamStats) {
2388     // Thread safe. Don't bother locking.
2389     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2390 
2391     if (remoteCb != 0) {
2392         remoteCb->onDeviceIdle();
2393     }
2394 
2395     std::vector<hardware::CameraStreamStats> fullStreamStats = streamStats;
2396     {
2397         Mutex::Autolock l(mCompositeLock);
2398         for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2399             hardware::CameraStreamStats compositeStats;
2400             mCompositeStreamMap.valueAt(i)->getStreamStats(&compositeStats);
2401             if (compositeStats.mWidth > 0) {
2402                 fullStreamStats.push_back(compositeStats);
2403             }
2404         }
2405     }
2406     Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
2407             mostRequestedFpsRange,
2408             fullStreamStats,
2409             mRunningSessionStats.mUserTag,
2410             mRunningSessionStats.mVideoStabilizationMode,
2411             mRunningSessionStats.mUsedUltraWide,
2412             mRunningSessionStats.mUsedSettingsOverrideZoom);
2413 }
2414 
notifyShutter(const CaptureResultExtras & resultExtras,nsecs_t timestamp)2415 void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
2416         nsecs_t timestamp) {
2417     // Thread safe. Don't bother locking.
2418     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2419     CaptureResultExtras mutableResultExtras = resultExtras;
2420     if (flags::camera_multi_client() && mSharedMode) {
2421         int clientReqId;
2422         bool matchStreamingRequest = matchSharedStreamingRequest(resultExtras.requestId);
2423         bool matchCaptureRequest = matchSharedCaptureRequest(resultExtras.requestId);
2424         if (matchStreamingRequest) {
2425             clientReqId = mSharedStreamingRequest.second;
2426         } else if (matchCaptureRequest) {
2427             clientReqId = mSharedRequestMap[resultExtras.requestId];
2428         } else {
2429             return;
2430         }
2431         mutableResultExtras.requestId = clientReqId;
2432     }
2433 
2434     if (remoteCb != 0) {
2435         remoteCb->onCaptureStarted(mutableResultExtras, timestamp);
2436     }
2437     Camera2ClientBase::notifyShutter(mutableResultExtras, timestamp);
2438     if (flags::camera_multi_client() && mSharedMode) {
2439         // When camera is opened in shared mode, composite streams are not
2440         // supported.
2441         return;
2442     }
2443 
2444     // Access to the composite stream map must be synchronized
2445     Mutex::Autolock l(mCompositeLock);
2446     for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2447         mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2448     }
2449 }
2450 
notifyPrepared(int streamId)2451 void CameraDeviceClient::notifyPrepared(int streamId) {
2452     // Thread safe. Don't bother locking.
2453     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2454     if (remoteCb != 0) {
2455         ALOGV("%s: stream id %d", __FUNCTION__, streamId);
2456         remoteCb->onPrepared(streamId);
2457     }
2458 }
2459 
notifyRequestQueueEmpty()2460 void CameraDeviceClient::notifyRequestQueueEmpty() {
2461     // Thread safe. Don't bother locking.
2462     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2463     if (remoteCb != 0) {
2464         remoteCb->onRequestQueueEmpty();
2465     }
2466 }
2467 
notifyClientSharedAccessPriorityChanged(bool primaryClient)2468 void CameraDeviceClient::notifyClientSharedAccessPriorityChanged(bool primaryClient) {
2469     // Thread safe. Don't bother locking.
2470     if (!flags::camera_multi_client()) {
2471         return;
2472     }
2473     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2474     if (remoteCb != 0) {
2475         remoteCb->onClientSharedAccessPriorityChanged(primaryClient);
2476     }
2477 }
2478 
detachDevice()2479 void CameraDeviceClient::detachDevice() {
2480     if (mDevice == 0) return;
2481 
2482     nsecs_t startTime = systemTime();
2483     if (mFrameProcessor.get() != nullptr) {
2484             mFrameProcessor->removeListener(
2485                     camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2486                     camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2487     }
2488 
2489     if (flags::camera_multi_client() && mSharedMode) {
2490         for (auto streamInfo : mStreamInfoMap) {
2491             int streamToDelete = streamInfo.first;
2492             std::vector<size_t> removedSurfaceIds;
2493             for (size_t i = 0; i < mStreamMap.size(); ++i) {
2494                 if (streamToDelete == mStreamMap.valueAt(i).streamId()) {
2495                     removedSurfaceIds.push_back(mStreamMap.valueAt(i).surfaceId());
2496                 }
2497             }
2498             status_t err = mDevice->removeSharedSurfaces(streamToDelete, removedSurfaceIds);
2499             if (err != OK) {
2500                 std::string msg = fmt::sprintf("Camera %s: Unexpected error %s (%d) when removing"
2501                         "shared surfaces from stream %d", mCameraIdStr.c_str(), strerror(-err),
2502                         err, streamToDelete);
2503                 ALOGE("%s: %s", __FUNCTION__, msg.c_str());
2504             }
2505         }
2506     }
2507 
2508     if (!flags::camera_multi_client() || !mSharedMode ||
2509             (mSharedMode && sCameraService->isOnlyClient(this))){
2510         ALOGV("Camera %s: Stopping processors", mCameraIdStr.c_str());
2511 
2512         if (mFrameProcessor.get() != nullptr) {
2513             mFrameProcessor->requestExit();
2514             ALOGV("Camera %s: Waiting for threads", mCameraIdStr.c_str());
2515             mFrameProcessor->join();
2516             ALOGV("Camera %s: Disconnecting device", mCameraIdStr.c_str());
2517         }
2518 
2519         // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2520         {
2521             int64_t lastFrameNumber;
2522             status_t code;
2523             if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2524                 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2525             }
2526 
2527             if ((code = mDevice->waitUntilDrained()) != OK) {
2528                 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2529                         code);
2530             }
2531         }
2532 
2533         {
2534             Mutex::Autolock l(mCompositeLock);
2535             for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2536                 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2537                 if (ret != OK) {
2538                     ALOGE("%s: Failed removing composite stream  %s (%d)", __FUNCTION__,
2539                             strerror(-ret), ret);
2540                 }
2541             }
2542             mCompositeStreamMap.clear();
2543         }
2544     }
2545 
2546     bool hasDeviceError = mDevice->hasDeviceError();
2547     Camera2ClientBase::detachDevice();
2548 
2549     int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2550     mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs, hasDeviceError);
2551 }
2552 
writeResultMetadataIntoResultQueue(const CameraMetadata & resultMetadata)2553 size_t CameraDeviceClient::writeResultMetadataIntoResultQueue(
2554         const CameraMetadata &resultMetadata) {
2555     ATRACE_CALL();
2556 
2557     const camera_metadata_t *resultMetadataP = resultMetadata.getAndLock();
2558     size_t resultSize = get_camera_metadata_size(resultMetadataP);
2559     if (mResultMetadataQueue != nullptr &&
2560         mResultMetadataQueue->write(reinterpret_cast<const int8_t*>(resultMetadataP),
2561                 resultSize)) {
2562         resultMetadata.unlock(resultMetadataP);
2563         return resultSize;
2564     }
2565     resultMetadata.unlock(resultMetadataP);
2566     ALOGE(" %s couldn't write metadata into result queue ", __FUNCTION__);
2567     return 0;
2568 }
2569 
2570 /** Device-related methods */
convertToFMQ(const std::vector<PhysicalCaptureResultInfo> & physicalResults)2571 std::vector<PhysicalCaptureResultInfo> CameraDeviceClient::convertToFMQ(
2572         const std::vector<PhysicalCaptureResultInfo> &physicalResults) {
2573     std::vector<PhysicalCaptureResultInfo> retVal;
2574     ALOGVV("%s E", __FUNCTION__);
2575     for (const auto &srcPhysicalResult : physicalResults) {
2576         size_t fmqSize = 0;
2577         if (!mIsVendorClient && flags::fmq_metadata()) {
2578             fmqSize = writeResultMetadataIntoResultQueue(
2579                     srcPhysicalResult.mCameraMetadataInfo.get<CameraMetadataInfo::metadata>());
2580         }
2581         ALOGVV("%s physical metadata write size is %d", __FUNCTION__, (int)fmqSize);
2582         if (fmqSize != 0) {
2583             retVal.emplace_back(srcPhysicalResult.mPhysicalCameraId, fmqSize);
2584         } else {
2585             // The flag was off / we're serving VNDK shim call or FMQ write failed.
2586             retVal.emplace_back(srcPhysicalResult.mPhysicalCameraId,
2587                     srcPhysicalResult.mCameraMetadataInfo.get<CameraMetadataInfo::metadata>());
2588         }
2589     }
2590     ALOGVV("%s X", __FUNCTION__);
2591     return retVal;
2592 }
2593 
matchSharedStreamingRequest(int reqId)2594 bool CameraDeviceClient::matchSharedStreamingRequest(int reqId) {
2595     if (!flags::camera_multi_client() || !mSharedMode) {
2596         return false;
2597     }
2598     // In shared mode, check if the result req id matches the streaming request
2599     // sent by client.
2600     if (reqId == mSharedStreamingRequest.first) {
2601         return true;
2602     }
2603     return false;
2604 }
2605 
matchSharedCaptureRequest(int reqId)2606 bool CameraDeviceClient::matchSharedCaptureRequest(int reqId) {
2607     if (!flags::camera_multi_client() || !mSharedMode) {
2608         return false;
2609     }
2610     // In shared mode, only primary clients can send the capture request. If the
2611     // result req id does not match the streaming request id, check against the
2612     // capture request ids sent by the primary client.
2613     if (mIsPrimaryClient) {
2614         auto iter = mSharedRequestMap.find(reqId);
2615         if (iter != mSharedRequestMap.end()) {
2616             return true;
2617         }
2618     }
2619     return false;
2620 }
2621 
onResultAvailable(const CaptureResult & result)2622 void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
2623     ATRACE_CALL();
2624     ALOGVV("%s E", __FUNCTION__);
2625     CaptureResult mutableResult = result;
2626     bool matchStreamingRequest, matchCaptureRequest, sharedStreamingLastFrame;
2627     if (flags::camera_multi_client() && mSharedMode) {
2628         int clientReqId;
2629         matchStreamingRequest = matchSharedStreamingRequest(result.mResultExtras.requestId);
2630         matchCaptureRequest = matchSharedCaptureRequest(result.mResultExtras.requestId);
2631         if (matchStreamingRequest) {
2632             clientReqId = mSharedStreamingRequest.second;
2633             // When a client stops streaming using cancelRequest, we still need to deliver couple
2634             // more capture results to the client, till the lastframe number returned by the
2635             // cancelRequest. Therefore, only clean the shared streaming request once all the frames for
2636             // the repeating request have been delivered to the client.
2637             sharedStreamingLastFrame = (mStreamingRequestId == REQUEST_ID_NONE)
2638                     && (result.mResultExtras.frameNumber >= mStreamingRequestLastFrameNumber);
2639             if (sharedStreamingLastFrame) {
2640                 mSharedStreamingRequest.first = REQUEST_ID_NONE;
2641                 mSharedStreamingRequest.second = REQUEST_ID_NONE;
2642             }
2643         } else if (matchCaptureRequest) {
2644             clientReqId = mSharedRequestMap[result.mResultExtras.requestId];
2645             mSharedRequestMap.erase(result.mResultExtras.requestId);
2646         } else {
2647             return;
2648         }
2649         mutableResult.mResultExtras.requestId = clientReqId;
2650         if (mutableResult.mMetadata.update(ANDROID_REQUEST_ID, &clientReqId, 1) != OK) {
2651             ALOGE("%s Failed to set request ID in metadata.", __FUNCTION__);
2652             return;
2653         }
2654     }
2655 
2656     // Thread-safe. No lock necessary.
2657     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
2658     if (remoteCb != NULL) {
2659         // Write  result metadata into metadataQueue
2660         size_t fmqMetadataSize = 0;
2661         // Vendor clients need to modify metadata and also this call is in process
2662         // before going through FMQ to vendor clients. So don't use FMQ here.
2663         if (!mIsVendorClient && flags::fmq_metadata()) {
2664             fmqMetadataSize = writeResultMetadataIntoResultQueue(mutableResult.mMetadata);
2665         }
2666         hardware::camera2::impl::CameraMetadataNative resultMetadata;
2667         CameraMetadataInfo resultInfo;
2668         if (fmqMetadataSize == 0) {
2669             // The flag was off / we're serving VNDK shim call or FMQ write failed.
2670             resultMetadata = mutableResult.mMetadata;
2671             resultInfo.set<CameraMetadataInfo::metadata>(resultMetadata);
2672         } else {
2673             resultInfo.set<CameraMetadataInfo::fmqSize>(fmqMetadataSize);
2674         }
2675 
2676         std::vector<PhysicalCaptureResultInfo> physicalMetadatas =
2677                 convertToFMQ(mutableResult.mPhysicalMetadatas);
2678 
2679         remoteCb->onResultReceived(resultInfo, mutableResult.mResultExtras,
2680                 physicalMetadatas);
2681         if (flags::camera_multi_client() && mSharedMode) {
2682             // If all the capture requests for this client has been processed,
2683             // send onDeviceidle callback.
2684             if ((mSharedStreamingRequest.first == REQUEST_ID_NONE) && mSharedRequestMap.empty() ) {
2685                 markClientIdle();
2686             }
2687         }
2688     }
2689 
2690     // Access to the composite stream map must be synchronized
2691     Mutex::Autolock l(mCompositeLock);
2692     for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2693         mCompositeStreamMap.valueAt(i)->onResultAvailable(mutableResult);
2694     }
2695     ALOGVV("%s X", __FUNCTION__);
2696 }
2697 
markClientActive()2698 void CameraDeviceClient::markClientActive() {
2699     Mutex::Autolock l(mDevice->mSharedDeviceActiveLock);
2700     if (mDeviceActive) {
2701         // Already in active state.
2702         return;
2703     }
2704     status_t res = startCameraStreamingOps();
2705     if (res != OK) {
2706         ALOGE("%s: Camera %s: Error starting camera streaming ops: %d", __FUNCTION__,
2707                 mCameraIdStr.c_str(), res);
2708     }
2709     mDeviceActive = true;
2710 }
2711 
markClientIdle()2712 void CameraDeviceClient::markClientIdle() {
2713     Mutex::Autolock l(mDevice->mSharedDeviceActiveLock);
2714     if (!mDeviceActive) {
2715         // Already in idle state.
2716         return;
2717     }
2718     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
2719     if (remoteCb != NULL) {
2720         remoteCb->onDeviceIdle();
2721     }
2722     status_t res = finishCameraStreamingOps();
2723     if (res != OK) {
2724         ALOGE("%s: Camera %s: Error finishing streaming ops: %d", __FUNCTION__,
2725                 mCameraIdStr.c_str(), res);
2726     }
2727     mDeviceActive = false;
2728 }
2729 
checkPidStatus(const char * checkLocation)2730 binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
2731     if (mDisconnected) {
2732         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2733                 "The camera device has been disconnected");
2734     }
2735     status_t res = checkPid(checkLocation);
2736     return (res == OK) ? binder::Status::ok() :
2737             STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2738                     "Attempt to use camera from a different process than original client");
2739 }
2740 
2741 // TODO: move to Camera2ClientBase
enforceRequestPermissions(CameraMetadata & metadata)2742 bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2743 
2744     const int pid = getCallingPid();
2745     const int selfPid = getpid();
2746     camera_metadata_entry_t entry;
2747 
2748     /**
2749      * Mixin default important security values
2750      * - android.led.transmit = defaulted ON
2751      */
2752     CameraMetadata staticInfo = mDevice->info();
2753     entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2754     for(size_t i = 0; i < entry.count; ++i) {
2755         uint8_t led = entry.data.u8[i];
2756 
2757         switch(led) {
2758             case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2759                 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2760                 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2761                     metadata.update(ANDROID_LED_TRANSMIT,
2762                                     &transmitDefault, 1);
2763                 }
2764                 break;
2765             }
2766         }
2767     }
2768 
2769     // We can do anything!
2770     if (pid == selfPid) {
2771         return true;
2772     }
2773 
2774     /**
2775      * Permission check special fields in the request
2776      * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2777      */
2778     entry = metadata.find(ANDROID_LED_TRANSMIT);
2779     if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2780         String16 permissionString =
2781             toString16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2782         if (!checkCallingPermission(permissionString)) {
2783             const int uid = getCallingUid();
2784             ALOGE("Permission Denial: "
2785                   "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2786             return false;
2787         }
2788     }
2789 
2790     return true;
2791 }
2792 
getStaticInfo(const std::string & cameraId)2793 const CameraMetadata &CameraDeviceClient::getStaticInfo(const std::string &cameraId) {
2794     if (mDevice->getId() == cameraId) {
2795         return mDevice->info();
2796     }
2797     return mDevice->infoPhysical(cameraId);
2798 }
2799 
supportsUltraHighResolutionCapture(const std::string & cameraId)2800 bool CameraDeviceClient::supportsUltraHighResolutionCapture(const std::string &cameraId) {
2801     const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
2802     return SessionConfigurationUtils::supportsUltraHighResolutionCapture(deviceInfo);
2803 }
2804 
isSensorPixelModeConsistent(const std::list<int> & streamIdList,const CameraMetadata & settings)2805 bool CameraDeviceClient::isSensorPixelModeConsistent(
2806         const std::list<int> &streamIdList, const CameraMetadata &settings) {
2807     // First we get the sensorPixelMode from the settings metadata.
2808     int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2809     camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2810     if (sensorPixelModeEntry.count != 0) {
2811         sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2812         if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2813             sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2814             ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2815                       __FUNCTION__, sensorPixelMode);
2816             return false;
2817         }
2818     }
2819     // Check whether each stream has max resolution allowed.
2820     bool consistent = true;
2821     for (auto it : streamIdList) {
2822         auto const streamInfoIt = mStreamInfoMap.find(it);
2823         if (streamInfoIt == mStreamInfoMap.end()) {
2824             ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2825             return false;
2826         }
2827         consistent =
2828                 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2829                         streamInfoIt->second.sensorPixelModesUsed.end();
2830         if (!consistent) {
2831             ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2832             for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2833                 ALOGE("sensor pixel mode used list: %i", m);
2834             }
2835             break;
2836         }
2837     }
2838 
2839     return consistent;
2840 }
2841 
2842 } // namespace android
2843