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