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