blob: d4ac88d87c811e860eeaacaae89bdea037c19b3d [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
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
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
34
Emilian Peev00420d22018-02-05 21:33:13 +000035#include <camera_metadata_hidden.h>
36
Emilian Peev538c90e2018-12-17 18:03:19 +000037#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080038#include "HeicCompositeStream.h"
Emilian Peev434248e2022-10-06 14:58:54 -070039#include "JpegRCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043#define STATUS_ERROR(errorCode, errorString) \
44 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080045 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046
47#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
48 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080049 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070051
52namespace android {
53using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000054using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070055using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070056
57CameraDeviceClientBase::CameraDeviceClientBase(
58 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080059 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070060 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080061 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090062 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080063 const String8& cameraId,
Jing Mikec7f9b132023-03-12 11:12:04 +080064 [[maybe_unused]] int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070065 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070066 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070067 int clientPid,
68 uid_t clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -070069 int servicePid,
70 bool overrideToPortrait) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080071 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080072 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080073 clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080074 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080075 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080076 cameraId,
77 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070078 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080079 clientPid,
80 clientUid,
Austin Borger18b30a72022-10-27 12:20:29 -070081 servicePid,
82 overrideToPortrait),
Igor Murashkine7ee7632013-06-11 18:10:18 -070083 mRemoteCallback(remoteCallback) {
84}
Igor Murashkine7ee7632013-06-11 18:10:18 -070085
86// Interface used by CameraService
87
88CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080089 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070090 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080091 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080092 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090093 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080094 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080095 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070096 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080097 int clientPid,
98 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070099 int servicePid,
Austin Borger18b30a72022-10-27 12:20:29 -0700100 bool overrideForPerfClass,
101 bool overrideToPortrait) :
Austin Borger74fca042022-05-23 12:41:21 -0700102 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper, clientPackageName,
103 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
Austin Borger18b30a72022-10-27 12:20:29 -0700104 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass,
105 overrideToPortrait),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700106 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700107 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700108 mRequestIdCounter(0),
109 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110
111 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800112 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700113}
114
Emilian Peevbd8c5032018-02-14 23:05:40 +0000115status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
116 const String8& monitorTags) {
117 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800118}
119
120template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000121status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700122 ATRACE_CALL();
123 status_t res;
124
Emilian Peevbd8c5032018-02-14 23:05:40 +0000125 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700126 if (res != OK) {
127 return res;
128 }
129
130 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700131 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800132 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Austin Borger7b129542022-06-09 13:23:06 -0700133 res = mFrameProcessor->run(threadName.string());
134 if (res != OK) {
135 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
136 __FUNCTION__, strerror(-res), res);
137 return res;
138 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700139
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800140 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
141 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800142 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700143 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700144
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800145 const CameraMetadata &deviceInfo = mDevice->info();
146 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000147 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
148 if (physicalKeysEntry.count > 0) {
149 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
150 physicalKeysEntry.data.i32,
151 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
152 }
153
Emilian Peev2295df72021-11-12 18:14:10 -0800154 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
155 mDynamicProfileMap.emplace(
156 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
157 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
158 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800159 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800160 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800161 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800162 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800163 if (entry.count > 0 || ((entry.count % 3) != 0)) {
164 int64_t standardBitmap =
165 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
166 for (size_t i = 0; i < entry.count; i += 3) {
167 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800168 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800169 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
170 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800171 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800172 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800173 }
174 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800175 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
176 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800177 }
178 }
Emilian Peevef715e22022-05-19 17:44:33 -0700179 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
180 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800181 } else {
182 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
183 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
184 }
185 }
186 }
187
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700188 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800189 // Cache physical camera ids corresponding to this device and also the high
190 // resolution sensors in this device + physical camera ids
191 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800192 if (supportsUltraHighResolutionCapture(mCameraIdStr)) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800193 mHighResolutionSensors.insert(mCameraIdStr.string());
194 }
195 for (auto &physicalId : mPhysicalCameraIds) {
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800196 if (supportsUltraHighResolutionCapture(String8(physicalId.c_str()))) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800197 mHighResolutionSensors.insert(physicalId.c_str());
198 }
199 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700200 return OK;
201}
202
203CameraDeviceClient::~CameraDeviceClient() {
204}
205
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800206binder::Status CameraDeviceClient::submitRequest(
207 const hardware::camera2::CaptureRequest& request,
208 bool streaming,
209 /*out*/
210 hardware::camera2::utils::SubmitInfo *submitInfo) {
211 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
212 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700213}
214
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800215binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000216 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000217 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800218 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
219
Emilian Peev2f5d6012022-01-19 16:16:50 -0800220 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800221 // Trying to submit request with surface that wasn't created
222 if (idx == NAME_NOT_FOUND) {
223 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
224 " we have not called createStream on",
225 __FUNCTION__, mCameraIdStr.string());
226 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
227 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000228 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
229 != NAME_NOT_FOUND) {
230 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
231 currentStreamId);
232 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800233 }
234
235 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
236 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800237 outputStreamIds->push_back(streamSurfaceId.streamId());
238 }
239 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
240
241 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
242 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
243 streamSurfaceId.surfaceId());
244
Emilian Peevf873aa52018-01-26 14:58:28 +0000245 if (currentStreamId != nullptr) {
246 *currentStreamId = streamSurfaceId.streamId();
247 }
248
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800249 return binder::Status::ok();
250}
251
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800252static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
253 const Vector<int> &streamIdsForThisRequest) {
254 std::list<int> intersection;
255 for (auto &streamId : streamIdsForThisRequest) {
256 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
257 intersection.emplace_back(streamId);
258 }
259 }
260 return intersection;
261}
262
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800263binder::Status CameraDeviceClient::submitRequestList(
264 const std::vector<hardware::camera2::CaptureRequest>& requests,
265 bool streaming,
266 /*out*/
267 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700268 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700269 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800271 binder::Status res = binder::Status::ok();
272 status_t err;
273 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
274 return res;
275 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700276
277 Mutex::Autolock icl(mBinderSerializationLock);
278
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800279 if (!mDevice.get()) {
280 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
281 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700282
283 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800284 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
285 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800286 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700287 }
288
Emilian Peevaebbe412018-01-15 13:53:24 +0000289 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700290 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700292 uint32_t loopCounter = 0;
293
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800294 for (auto&& request: requests) {
295 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700296 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800297 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
298 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800299 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800300 "No input configured for camera %s but request is for reprocessing",
301 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700302 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800303 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
304 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800305 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
306 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000307 } else if (request.mPhysicalCameraSettings.size() > 1) {
308 ALOGE("%s: Camera %s: reprocess requests not supported for "
309 "multiple physical cameras.", __FUNCTION__,
310 mCameraIdStr.string());
311 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
312 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700313 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700314 }
315
Emilian Peevaebbe412018-01-15 13:53:24 +0000316 if (request.mPhysicalCameraSettings.empty()) {
317 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
318 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800319 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000320 "Request doesn't contain any settings");
321 }
322
323 //The first capture settings should always match the logical camera id
324 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
325 if (mDevice->getId() != logicalId) {
326 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
327 mCameraIdStr.string());
328 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
329 "Invalid camera request settings");
330 }
331
Emilian Peevaebbe412018-01-15 13:53:24 +0000332 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800333 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
334 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800335 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
336 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700337 }
338
Jianing Wei90e59c92014-03-12 18:29:36 -0700339 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700340 * Write in the output stream IDs and map from stream ID to surface ID
341 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700342 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700343 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700344 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000345 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800346 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800347 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800348 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800349 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700350
Emilian Peevf873aa52018-01-26 14:58:28 +0000351 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800352 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000353 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800354 if (!res.isOk()) {
355 return res;
356 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000357
358 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
359 if (index >= 0) {
360 String8 requestedPhysicalId(
361 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
362 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800363 dynamicProfileBitmap |=
364 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000365 } else {
366 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
367 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700368 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800369 } else {
370 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
371 int streamId = request.mStreamIdxList.itemAt(i);
372 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700373
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800374 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
375 if (index < 0) {
376 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
377 " we have not called createStream on: stream %d",
378 __FUNCTION__, mCameraIdStr.string(), streamId);
379 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
380 "Request targets Surface that is not part of current capture session");
381 }
382
383 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
384 if ((size_t)surfaceIdx >= gbps.size()) {
385 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
386 " we have not called createStream on: stream %d, surfaceIdx %d",
387 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
388 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
389 "Request targets Surface has invalid surface index");
390 }
391
Emilian Peevf873aa52018-01-26 14:58:28 +0000392 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800393 if (!res.isOk()) {
394 return res;
395 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000396
397 String8 requestedPhysicalId(
398 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
399 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800400 dynamicProfileBitmap |=
401 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
402 }
403 }
404
405 if (dynamicProfileBitmap !=
406 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
407 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
408 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
409 if ((dynamicProfileBitmap & i) == 0) {
410 continue;
411 }
412
413 const auto& it = mDynamicProfileMap.find(i);
414 if (it != mDynamicProfileMap.end()) {
415 if ((it->second == 0) ||
416 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
417 continue;
418 } else {
419 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
420 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800421 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800422 dynamicProfileBitmap);
423 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
424 "Request targets an unsupported dynamic range profile"
425 " combination");
426 }
427 } else {
428 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
429 " references unsupported dynamic range profile 0x%x!",
430 __FUNCTION__, mCameraIdStr.string(), i);
431 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
432 "Request targets 10-bit Surface with unsupported dynamic range"
433 " profile");
434 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700435 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700436 }
437
Emilian Peevf873aa52018-01-26 14:58:28 +0000438 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
439 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000440 if (it.settings.isEmpty()) {
441 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
442 __FUNCTION__, mCameraIdStr.string());
443 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
444 "Request settings are empty");
445 }
446
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800447 // Check whether the physical / logical stream has settings
448 // consistent with the sensor pixel mode(s) it was configured with.
449 // mCameraIdToStreamSet will only have ids that are high resolution
450 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
451 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
452 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
453 outputStreamIds);
454 if (!request.mIsReprocess &&
455 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
456 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
457 "consistent with configured streams. Rejecting request.",
458 __FUNCTION__, it.id.c_str());
459 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
460 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
461 "streams configured");
462 }
463 }
464
Emilian Peevf873aa52018-01-26 14:58:28 +0000465 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700466 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
467 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
468 mSupportedPhysicalRequestKeys.end();
469 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
470 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
471 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000472 if (physicalId != mDevice->getId()) {
473 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
474 it.id);
475 if (found == requestedPhysicalIds.end()) {
476 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
477 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
478 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
479 "Invalid physical camera id");
480 }
Emilian Peev00420d22018-02-05 21:33:13 +0000481
482 if (!mSupportedPhysicalRequestKeys.empty()) {
483 // Filter out any unsupported physical request keys.
484 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
485 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
486 filteredParams.getAndLock());
487 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
488 filteredParams.unlock(meta);
489
490 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
491 camera_metadata_ro_entry entry = it.settings.find(keyIt);
492 if (entry.count > 0) {
493 filteredParams.update(entry);
494 }
495 }
496
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700497 physicalSettingsList.push_back({it.id, filteredParams,
498 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000499 }
500 } else {
501 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000502 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000503 }
504
505 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
506 // Callee logs
507 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
508 "Caller does not have permission to change restricted controls");
509 }
510
Emilian Peevaebbe412018-01-15 13:53:24 +0000511 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
512 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700513
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800514 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000515 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
516 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700517 }
518
Emilian Peevaebbe412018-01-15 13:53:24 +0000519 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
520 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700521 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800522 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
523 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
524 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700525
Emilian Peevaebbe412018-01-15 13:53:24 +0000526 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700527 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800528
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700529 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800530 if (!request.mUserTag.empty()) {
531 mUserTag = request.mUserTag;
532 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700533 camera_metadata_entry entry =
534 physicalSettingsList.begin()->metadata.find(
535 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
536 if (entry.count == 1) {
537 mVideoStabilizationMode = entry.data.u8[0];
538 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700539 }
540 mRequestIdCounter++;
541
542 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700543 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
544 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 if (err != OK) {
546 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800547 "Camera %s: Got error %s (%d) after trying to set streaming request",
548 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 ALOGE("%s: %s", __FUNCTION__, msg.string());
550 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
551 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700552 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700553 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700554 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 }
556 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700557 err = mDevice->captureList(metadataRequestList, surfaceMapList,
558 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559 if (err != OK) {
560 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800561 "Camera %s: Got error %s (%d) after trying to submit capture request",
562 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800563 ALOGE("%s: %s", __FUNCTION__, msg.string());
564 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
565 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700566 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800567 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700568 }
569
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800570 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700571 return res;
572}
573
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800574binder::Status CameraDeviceClient::cancelRequest(
575 int requestId,
576 /*out*/
577 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700578 ATRACE_CALL();
579 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
580
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800581 status_t err;
582 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700583
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800584 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585
586 Mutex::Autolock icl(mBinderSerializationLock);
587
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 if (!mDevice.get()) {
589 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
590 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700591
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700592 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700593 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800594 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
595 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800596 ALOGE("%s: %s", __FUNCTION__, msg.string());
597 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700598 }
599
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700601
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800602 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800603 ALOGV("%s: Camera %s: Successfully cleared streaming request",
604 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700605 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800606 } else {
607 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800608 "Camera %s: Error clearing streaming request: %s (%d)",
609 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700610 }
611
612 return res;
613}
614
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800615binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700616 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700617 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700618 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800619 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700620}
621
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100622binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700623 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800624 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700625 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700626 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
627 __FUNCTION__, mInputStream.configured ? 1 : 0,
628 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700629
Zhijun He0effd522016-03-04 10:22:27 -0800630 binder::Status res;
631 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
632
Emilian Peevcc0b7952020-01-07 13:54:47 -0800633 if (offlineStreamIds == nullptr) {
634 String8 msg = String8::format("Invalid offline stream ids");
635 ALOGE("%s: %s", __FUNCTION__, msg.string());
636 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
637 }
638
Zhijun He0effd522016-03-04 10:22:27 -0800639 Mutex::Autolock icl(mBinderSerializationLock);
640
641 if (!mDevice.get()) {
642 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
643 }
644
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700645 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000646 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000647 if (!res.isOk()) {
648 return res;
649 }
650
651 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
652 if (err == BAD_VALUE) {
653 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
654 mCameraIdStr.string());
655 ALOGE("%s: %s", __FUNCTION__, msg.string());
656 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
657 } else if (err != OK) {
658 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
659 mCameraIdStr.string(), strerror(-err), err);
660 ALOGE("%s: %s", __FUNCTION__, msg.string());
661 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000662 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800663 offlineStreamIds->clear();
664 mDevice->getOfflineStreamIds(offlineStreamIds);
665
Emilian Peev2f5d6012022-01-19 16:16:50 -0800666 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000667 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
668 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800669 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000670 String8 msg = String8::format("Camera %s: Error configuring composite "
671 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
672 ALOGE("%s: %s", __FUNCTION__, msg.string());
673 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
674 break;
675 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800676
677 // Composite streams can only support offline mode in case all individual internal
678 // streams are also supported.
679 std::vector<int> internalStreams;
680 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800681 offlineStreamIds->erase(
682 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800683 [&internalStreams] (int streamId) {
684 auto it = std::find(internalStreams.begin(), internalStreams.end(),
685 streamId);
686 if (it != internalStreams.end()) {
687 internalStreams.erase(it);
688 return true;
689 }
690
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800691 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800692 if (internalStreams.empty()) {
693 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
694 }
695 }
696
697 for (const auto& offlineStreamId : *offlineStreamIds) {
698 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000699 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700700
701 nsecs_t configureEnd = systemTime();
702 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
Austin Borger74fca042022-05-23 12:41:21 -0700703 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700704 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000705 }
706
707 return res;
708}
709
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800710binder::Status CameraDeviceClient::isSessionConfigurationSupported(
711 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800712
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800713 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800714 binder::Status res;
715 status_t ret = OK;
716 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
717
718 Mutex::Autolock icl(mBinderSerializationLock);
719
720 if (!mDevice.get()) {
721 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
722 }
723
724 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700725 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000726 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800727 if (!res.isOk()) {
728 return res;
729 }
730
731 if (status == nullptr) {
732 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
733 ALOGE("%s: %s", __FUNCTION__, msg.string());
734 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
735 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700736
Emilian Peev35ae8262018-11-08 13:11:32 +0000737 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700738 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
739 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000740 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700741 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000742 switch (ret) {
743 case OK:
744 // Expected, do nothing.
745 break;
746 case INVALID_OPERATION: {
747 String8 msg = String8::format(
748 "Camera %s: Session configuration query not supported!",
749 mCameraIdStr.string());
750 ALOGD("%s: %s", __FUNCTION__, msg.string());
751 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
752 }
753
754 break;
755 default: {
756 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
757 strerror(-ret), ret);
758 ALOGE("%s: %s", __FUNCTION__, msg.string());
759 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
760 msg.string());
761 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800762 }
763
764 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700765}
766
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800767binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700768 ATRACE_CALL();
769 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
770
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 binder::Status res;
772 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700773
774 Mutex::Autolock icl(mBinderSerializationLock);
775
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800776 if (!mDevice.get()) {
777 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
778 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700779
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700780 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700781 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700782 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000783 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700784
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700785 if (mInputStream.configured && mInputStream.id == streamId) {
786 isInput = true;
787 } else {
788 // Guard against trying to delete non-created streams
789 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700790 if (streamId == mStreamMap.valueAt(i).streamId()) {
791 surfaces.push_back(mStreamMap.keyAt(i));
792 }
793 }
794
795 // See if this stream is one of the deferred streams.
796 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
797 if (streamId == mDeferredStreams[i]) {
798 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700799 break;
800 }
801 }
802
Emilian Peev2f5d6012022-01-19 16:16:50 -0800803 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000804 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
805 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
806 compositeIndex = i;
807 break;
808 }
809 }
810
Shuzhen Wang0129d522016-10-30 22:43:41 -0700811 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
812 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
813 " stream created yet", mCameraIdStr.string(), streamId);
814 ALOGW("%s: %s", __FUNCTION__, msg.string());
815 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700816 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700817 }
818
819 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800820 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700821
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800823 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
824 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800825 ALOGE("%s: %s", __FUNCTION__, msg.string());
826 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
827 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700828 if (isInput) {
829 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700830 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700831 for (auto& surface : surfaces) {
832 mStreamMap.removeItem(surface);
833 }
834
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800835 mConfiguredOutputs.removeItem(streamId);
836
Shuzhen Wang0129d522016-10-30 22:43:41 -0700837 if (dIndex != NAME_NOT_FOUND) {
838 mDeferredStreams.removeItemsAt(dIndex);
839 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000840
841 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800842 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000843 status_t ret;
844 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
845 != OK) {
846 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
847 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
848 streamId);
849 ALOGE("%s: %s", __FUNCTION__, msg.string());
850 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
851 }
852 mCompositeStreamMap.removeItemsAt(compositeIndex);
853 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800854 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
855 auto &streamSet = mapIt.second;
856 if (streamSet.find(streamId) != streamSet.end()) {
857 streamSet.erase(streamId);
858 break;
859 }
860 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700861 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700862 }
863
864 return res;
865}
866
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800867binder::Status CameraDeviceClient::createStream(
868 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
869 /*out*/
870 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700871 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700872
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800873 binder::Status res;
874 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700875
876 Mutex::Autolock icl(mBinderSerializationLock);
877
Shuzhen Wang0129d522016-10-30 22:43:41 -0700878 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
879 outputConfiguration.getGraphicBufferProducers();
880 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800881 bool deferredConsumer = outputConfiguration.isDeferred();
882 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800883 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800884 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800885 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800886 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800887 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800888 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800889 int mirrorMode = outputConfiguration.getMirrorMode();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700890 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000891 bool useReadoutTimestamp = outputConfiguration.useReadoutTimestamp();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700892
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700893 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000894 outputConfiguration.getSurfaceType());
895 if (!res.isOk()) {
896 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700897 }
Zhijun He5d677d12016-05-29 16:52:39 -0700898
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800899 if (!mDevice.get()) {
900 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
901 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700902 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800903 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000904 if (!res.isOk()) {
905 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800906 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000907
Shuzhen Wang0129d522016-10-30 22:43:41 -0700908 std::vector<sp<Surface>> surfaces;
909 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700910 status_t err;
911
912 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700913 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800914 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700915 }
916
Shuzhen Wang758c2152017-01-10 18:26:18 -0800917 OutputStreamInfo streamInfo;
918 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800919 const std::vector<int32_t> &sensorPixelModesUsed =
920 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700921 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700922 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700923 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800924 ssize_t index = mStreamMap.indexOfKey(binder);
925 if (index != NAME_NOT_FOUND) {
926 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
927 "(ID %zd)", mCameraIdStr.string(), index);
928 ALOGW("%s: %s", __FUNCTION__, msg.string());
929 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700930 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700931
Shuzhen Wang758c2152017-01-10 18:26:18 -0800932 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700933 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800934 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800935 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700936 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800937
938 if (!res.isOk())
939 return res;
940
941 if (!isStreamInfoValid) {
942 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700943 }
944
Shuzhen Wang758c2152017-01-10 18:26:18 -0800945 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700946 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800947 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700948
949 // If mOverrideForPerfClass is true, do not fail createStream() for small
950 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
951 // closest possible supported size.
952
Zhijun He125684a2015-12-26 15:07:30 -0800953 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100954 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800955 bool isDepthCompositeStream =
956 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800957 bool isHeicCompositeStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
Emilian Peev434248e2022-10-06 14:58:54 -0700958 bool isJpegRCompositeStream =
959 camera3::JpegRCompositeStream::isJpegRCompositeStream(surfaces[0]);
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800960 if (isDepthCompositeStream || isHeicCompositeStream || isJpegRCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800961 sp<CompositeStream> compositeStream;
962 if (isDepthCompositeStream) {
963 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
Chema Gonzalez67de9c82022-12-21 08:59:30 -0800964 } else if (isHeicCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800965 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
Emilian Peev434248e2022-10-06 14:58:54 -0700966 } else {
967 compositeStream = new camera3::JpegRCompositeStream(mDevice, getRemoteCallback());
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800968 }
969
Emilian Peev538c90e2018-12-17 18:03:19 +0000970 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
971 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700972 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800973 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev434248e2022-10-06 14:58:54 -0700974 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000975 streamInfo.colorSpace, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
976 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000977 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800978 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000979 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
980 compositeStream);
981 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800982 } else {
983 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
984 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700985 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800986 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800987 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800988 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +0000989 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace,
990 useReadoutTimestamp);
Emilian Peev538c90e2018-12-17 18:03:19 +0000991 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700992
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800993 if (err != OK) {
994 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800995 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800996 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
997 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800998 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700999 int i = 0;
1000 for (auto& binder : binders) {
1001 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
1002 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +01001003 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
1004 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001005 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001006
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001007 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001008 mStreamInfoMap[streamId] = streamInfo;
1009
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001010 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -07001011 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001012 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
1013 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001014
Zhijun He5d677d12016-05-29 16:52:39 -07001015 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001016 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001017
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001018 // Fill in mHighResolutionCameraIdToStreamIdSet map
1019 const String8 &cameraIdUsed =
1020 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1021 const char *cameraIdUsedCStr = cameraIdUsed.string();
1022 // Only needed for high resolution sensors
1023 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1024 mHighResolutionSensors.end()) {
1025 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1026 }
1027
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001028 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001029 }
1030
1031 return res;
1032}
1033
Zhijun He5d677d12016-05-29 16:52:39 -07001034binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1035 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001036 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001037 /*out*/
1038 int* newStreamId) {
1039 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001040 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001041 android_dataspace dataSpace;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001042 int32_t colorSpace;
Zhijun He5d677d12016-05-29 16:52:39 -07001043 status_t err;
1044 binder::Status res;
1045
1046 if (!mDevice.get()) {
1047 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1048 }
1049
1050 // Infer the surface info for deferred surface stream creation.
1051 width = outputConfiguration.getWidth();
1052 height = outputConfiguration.getHeight();
1053 surfaceType = outputConfiguration.getSurfaceType();
1054 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1055 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001056 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
Zhijun He5d677d12016-05-29 16:52:39 -07001057 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1058 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1059 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1060 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1061 }
1062 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001063 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001064 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001065 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001066 const String8 &cameraIdUsed =
1067 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1068 // Here, we override sensor pixel modes
1069 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1070 const std::vector<int32_t> &sensorPixelModesUsed =
1071 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001072 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001073 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1074 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1075 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1076 "sensor pixel modes used not valid for deferred stream");
1077 }
1078
Shuzhen Wang0129d522016-10-30 22:43:41 -07001079 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1080 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001081 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001082 &streamId, physicalCameraId,
1083 overriddenSensorPixelModesUsed,
1084 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001085 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001086 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001087 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001088 outputConfiguration.getStreamUseCase(),
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001089 outputConfiguration.getMirrorMode(),
1090 outputConfiguration.useReadoutTimestamp());
Zhijun He5d677d12016-05-29 16:52:39 -07001091
1092 if (err != OK) {
1093 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001094 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1095 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001096 } else {
1097 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1098 // a separate list to track. Once the deferred surface is set, this id will be
1099 // relocated to mStreamMap.
1100 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001101 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001102 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001103 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001104 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001105 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001106 outputConfiguration.getTimestampBase(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001107 outputConfiguration.getMirrorMode(),
1108 colorSpace));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001109
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001110 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001111 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001112 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001113
1114 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001115 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001116
1117 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001118 // Fill in mHighResolutionCameraIdToStreamIdSet
1119 const char *cameraIdUsedCStr = cameraIdUsed.string();
1120 // Only needed for high resolution sensors
1121 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1122 mHighResolutionSensors.end()) {
1123 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1124 }
Zhijun He5d677d12016-05-29 16:52:39 -07001125 }
1126 return res;
1127}
1128
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001129binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001130 int32_t transform = 0;
1131 status_t err;
1132 binder::Status res;
1133
1134 if (!mDevice.get()) {
1135 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1136 }
1137
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001138 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001139 if (err != OK) {
1140 // Error logged by getRotationTransformLocked.
1141 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1142 "Unable to calculate rotation transform for new stream");
1143 }
1144
1145 err = mDevice->setStreamTransform(streamId, transform);
1146 if (err != OK) {
1147 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1148 streamId);
1149 ALOGE("%s: %s", __FUNCTION__, msg.string());
1150 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1151 }
1152
1153 return res;
1154}
Ruben Brunkbba75572014-11-20 17:29:50 -08001155
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001156binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001157 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001158 /*out*/
1159 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001160
1161 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001162 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1163 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001164
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001165 binder::Status res;
1166 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001167
1168 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001169
1170 if (!mDevice.get()) {
1171 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1172 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001173
1174 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001175 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001176 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001177 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1178 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001179 }
1180
1181 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001182 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001183 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001184 mInputStream.configured = true;
1185 mInputStream.width = width;
1186 mInputStream.height = height;
1187 mInputStream.format = format;
1188 mInputStream.id = streamId;
1189
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001190 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1191 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001192
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001193 *newStreamId = streamId;
1194 } else {
1195 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001196 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001197 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001198 }
1199
1200 return res;
1201}
1202
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001204
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001205 binder::Status res;
1206 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1207
1208 if (inputSurface == NULL) {
1209 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001210 }
1211
1212 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001213 if (!mDevice.get()) {
1214 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1215 }
1216 sp<IGraphicBufferProducer> producer;
1217 status_t err = mDevice->getInputBufferProducer(&producer);
1218 if (err != OK) {
1219 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001220 "Camera %s: Error getting input Surface: %s (%d)",
1221 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001222 } else {
1223 inputSurface->name = String16("CameraInput");
1224 inputSurface->graphicBufferProducer = producer;
1225 }
1226 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001227}
1228
Emilian Peev40ead602017-09-26 15:46:36 +01001229binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1230 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1231 ATRACE_CALL();
1232
1233 binder::Status res;
1234 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1235
1236 Mutex::Autolock icl(mBinderSerializationLock);
1237
1238 if (!mDevice.get()) {
1239 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1240 }
1241
1242 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1243 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001244 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1245
Emilian Peev40ead602017-09-26 15:46:36 +01001246 auto producerCount = bufferProducers.size();
1247 if (producerCount == 0) {
1248 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1249 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1250 "bufferProducers must not be empty");
1251 }
1252
1253 // The first output is the one associated with the output configuration.
1254 // It should always be present, valid and the corresponding stream id should match.
1255 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1256 ssize_t index = mStreamMap.indexOfKey(binder);
1257 if (index == NAME_NOT_FOUND) {
1258 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1259 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1260 "OutputConfiguration is invalid");
1261 }
1262 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1263 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1264 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1265 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1266 "Stream id is invalid");
1267 }
1268
1269 std::vector<size_t> removedSurfaceIds;
1270 std::vector<sp<IBinder>> removedOutputs;
1271 std::vector<sp<Surface>> newOutputs;
1272 std::vector<OutputStreamInfo> streamInfos;
1273 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1274 for (auto &it : bufferProducers) {
1275 newOutputsMap.add(IInterface::asBinder(it), it);
1276 }
1277
1278 for (size_t i = 0; i < mStreamMap.size(); i++) {
1279 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1280 if (idx == NAME_NOT_FOUND) {
1281 if (mStreamMap[i].streamId() == streamId) {
1282 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1283 removedOutputs.push_back(mStreamMap.keyAt(i));
1284 }
1285 } else {
1286 if (mStreamMap[i].streamId() != streamId) {
1287 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1288 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1289 "Target Surface is invalid");
1290 }
1291 newOutputsMap.removeItemsAt(idx);
1292 }
1293 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001294 const std::vector<int32_t> &sensorPixelModesUsed =
1295 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001296 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001297 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001298 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001299 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001300 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001301
Emilian Peev40ead602017-09-26 15:46:36 +01001302 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1303 OutputStreamInfo outInfo;
1304 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001305 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001306 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001307 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001308 streamUseCase, timestampBase, mirrorMode, colorSpace);
Emilian Peev40ead602017-09-26 15:46:36 +01001309 if (!res.isOk())
1310 return res;
1311
Emilian Peev40ead602017-09-26 15:46:36 +01001312 streamInfos.push_back(outInfo);
1313 newOutputs.push_back(surface);
1314 }
1315
1316 //Trivial case no changes required
1317 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1318 return binder::Status::ok();
1319 }
1320
1321 KeyedVector<sp<Surface>, size_t> outputMap;
1322 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1323 &outputMap);
1324 if (ret != OK) {
1325 switch (ret) {
1326 case NAME_NOT_FOUND:
1327 case BAD_VALUE:
1328 case -EBUSY:
1329 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1330 "Camera %s: Error updating stream: %s (%d)",
1331 mCameraIdStr.string(), strerror(ret), ret);
1332 break;
1333 default:
1334 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1335 "Camera %s: Error updating stream: %s (%d)",
1336 mCameraIdStr.string(), strerror(ret), ret);
1337 break;
1338 }
1339 } else {
1340 for (const auto &it : removedOutputs) {
1341 mStreamMap.removeItem(it);
1342 }
1343
1344 for (size_t i = 0; i < outputMap.size(); i++) {
1345 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1346 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1347 }
1348
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001349 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1350
Emilian Peev40ead602017-09-26 15:46:36 +01001351 ALOGV("%s: Camera %s: Successful stream ID %d update",
1352 __FUNCTION__, mCameraIdStr.string(), streamId);
1353 }
1354
1355 return res;
1356}
1357
Igor Murashkine7ee7632013-06-11 18:10:18 -07001358// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001359binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1360 /*out*/
1361 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001362{
1363 ATRACE_CALL();
1364 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1365
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001366 binder::Status res;
1367 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001368
1369 Mutex::Autolock icl(mBinderSerializationLock);
1370
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001371 if (!mDevice.get()) {
1372 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1373 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001374
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001376 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1377 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1378
1379 CameraMetadata metadata;
1380 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001381 request != NULL) {
1382
1383 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001384 } else if (err == BAD_VALUE) {
1385 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001386 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1387 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389 } else {
1390 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001391 "Camera %s: Error creating default request for template %d: %s (%d)",
1392 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001393 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001394 return res;
1395}
1396
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001397binder::Status CameraDeviceClient::getCameraInfo(
1398 /*out*/
1399 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001400{
1401 ATRACE_CALL();
1402 ALOGV("%s", __FUNCTION__);
1403
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001404 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001405
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001406 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001407
1408 Mutex::Autolock icl(mBinderSerializationLock);
1409
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001410 if (!mDevice.get()) {
1411 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1412 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001413
Igor Murashkin099b4572013-07-12 17:52:16 -07001414 if (info != NULL) {
1415 *info = mDevice->info(); // static camera metadata
1416 // TODO: merge with device-specific camera metadata
1417 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001418
1419 return res;
1420}
1421
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001422binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001423{
1424 ATRACE_CALL();
1425 ALOGV("%s", __FUNCTION__);
1426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427 binder::Status res;
1428 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001429
1430 Mutex::Autolock icl(mBinderSerializationLock);
1431
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001432 if (!mDevice.get()) {
1433 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1434 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001435
1436 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001437 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001438 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001439 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001440 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1441 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 ALOGE("%s: %s", __FUNCTION__, msg.string());
1443 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001444 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445 status_t err = mDevice->waitUntilDrained();
1446 if (err != OK) {
1447 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001448 "Camera %s: Error waiting to drain: %s (%d)",
1449 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001450 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001451 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001452 return res;
1453}
1454
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001455binder::Status CameraDeviceClient::flush(
1456 /*out*/
1457 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001458 ATRACE_CALL();
1459 ALOGV("%s", __FUNCTION__);
1460
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001461 binder::Status res;
1462 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001463
1464 Mutex::Autolock icl(mBinderSerializationLock);
1465
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001466 if (!mDevice.get()) {
1467 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1468 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001469
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001470 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001471 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001472 status_t err = mDevice->flush(lastFrameNumber);
1473 if (err != OK) {
1474 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001475 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001476 }
1477 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001478}
1479
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001481 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00001482 ALOGV("%s stream id %d", __FUNCTION__, streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001483
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001484 binder::Status res;
1485 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001486
1487 Mutex::Autolock icl(mBinderSerializationLock);
1488
1489 // Guard against trying to prepare non-created streams
1490 ssize_t index = NAME_NOT_FOUND;
1491 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001492 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001493 index = i;
1494 break;
1495 }
1496 }
1497
1498 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001499 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1500 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001501 ALOGW("%s: %s", __FUNCTION__, msg.string());
1502 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001503 }
1504
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001505 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1506 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001507 status_t err = mDevice->prepare(streamId);
1508 if (err == BAD_VALUE) {
1509 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001510 "Camera %s: Stream %d has already been used, and cannot be prepared",
1511 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001512 } else if (err != OK) {
1513 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001514 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001515 strerror(-err), err);
1516 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001517 return res;
1518}
1519
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001520binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001521 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00001522 ALOGV("%s stream id %d", __FUNCTION__, streamId);
Ruben Brunkc78ac262015-08-13 17:58:46 -07001523
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001524 binder::Status res;
1525 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001526
1527 Mutex::Autolock icl(mBinderSerializationLock);
1528
1529 // Guard against trying to prepare non-created streams
1530 ssize_t index = NAME_NOT_FOUND;
1531 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001532 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001533 index = i;
1534 break;
1535 }
1536 }
1537
1538 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001539 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1540 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001541 ALOGW("%s: %s", __FUNCTION__, msg.string());
1542 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001543 }
1544
1545 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001546 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1547 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001548 ALOGE("%s: %s", __FUNCTION__, msg.string());
1549 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001550 }
1551
1552 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1553 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001554 status_t err = mDevice->prepare(maxCount, streamId);
1555 if (err == BAD_VALUE) {
1556 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001557 "Camera %s: Stream %d has already been used, and cannot be prepared",
1558 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001559 } else if (err != OK) {
1560 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001561 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001562 strerror(-err), err);
1563 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001564
1565 return res;
1566}
1567
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001568binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001569 ATRACE_CALL();
1570 ALOGV("%s", __FUNCTION__);
1571
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001572 binder::Status res;
1573 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001574
1575 Mutex::Autolock icl(mBinderSerializationLock);
1576
1577 // Guard against trying to prepare non-created streams
1578 ssize_t index = NAME_NOT_FOUND;
1579 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001580 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001581 index = i;
1582 break;
1583 }
1584 }
1585
1586 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001587 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1588 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001589 ALOGW("%s: %s", __FUNCTION__, msg.string());
1590 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001591 }
1592
1593 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1594 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001595 status_t err = mDevice->tearDown(streamId);
1596 if (err == BAD_VALUE) {
1597 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001598 "Camera %s: Stream %d is still in use, cannot be torn down",
1599 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001600 } else if (err != OK) {
1601 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001602 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001603 strerror(-err), err);
1604 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001605
1606 return res;
1607}
1608
Shuzhen Wang758c2152017-01-10 18:26:18 -08001609binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001610 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1611 ATRACE_CALL();
1612
1613 binder::Status res;
1614 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1615
1616 Mutex::Autolock icl(mBinderSerializationLock);
1617
Shuzhen Wang0129d522016-10-30 22:43:41 -07001618 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1619 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001620 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001621
Shuzhen Wang0129d522016-10-30 22:43:41 -07001622 if (bufferProducers.size() == 0) {
1623 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001624 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1625 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001626
Shuzhen Wang758c2152017-01-10 18:26:18 -08001627 // streamId should be in mStreamMap if this stream already has a surface attached
1628 // to it. Otherwise, it should be in mDeferredStreams.
1629 bool streamIdConfigured = false;
1630 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1631 for (size_t i = 0; i < mStreamMap.size(); i++) {
1632 if (mStreamMap.valueAt(i).streamId() == streamId) {
1633 streamIdConfigured = true;
1634 break;
1635 }
Zhijun He5d677d12016-05-29 16:52:39 -07001636 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001637 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1638 if (streamId == mDeferredStreams[i]) {
1639 deferredStreamIndex = i;
1640 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001641 }
1642
Shuzhen Wang758c2152017-01-10 18:26:18 -08001643 }
1644 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1645 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1646 "(ID %d)", mCameraIdStr.string(), streamId);
1647 ALOGW("%s: %s", __FUNCTION__, msg.string());
1648 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001649 }
1650
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001651 if (mStreamInfoMap[streamId].finalized) {
1652 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1653 " on stream ID %d", mCameraIdStr.string(), streamId);
1654 ALOGW("%s: %s", __FUNCTION__, msg.string());
1655 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1656 }
1657
Zhijun He5d677d12016-05-29 16:52:39 -07001658 if (!mDevice.get()) {
1659 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1660 }
1661
Shuzhen Wang758c2152017-01-10 18:26:18 -08001662 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001663 const std::vector<int32_t> &sensorPixelModesUsed =
1664 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001665 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001666 int32_t colorSpace = outputConfiguration.getColorSpace();
1667 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001668 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001669 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001670 for (auto& bufferProducer : bufferProducers) {
1671 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001672 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1673 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001674 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001675 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001676 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001677 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001678
1679 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001680 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001681 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001682 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001683 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001684
1685 if (!res.isOk())
1686 return res;
1687
1688 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001689 }
1690
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001691 // Gracefully handle case where finalizeOutputConfigurations is called
1692 // without any new surface.
1693 if (consumerSurfaces.size() == 0) {
1694 mStreamInfoMap[streamId].finalized = true;
1695 return res;
1696 }
1697
Zhijun He5d677d12016-05-29 16:52:39 -07001698 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001699 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001700 std::vector<int> consumerSurfaceIds;
1701 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001702 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001703 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1704 sp<IBinder> binder = IInterface::asBinder(
1705 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001706 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001707 binder.get(), streamId, consumerSurfaceIds[i]);
1708 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1709 }
1710 if (deferredStreamIndex != NAME_NOT_FOUND) {
1711 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001712 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001713 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001714 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001715 } else if (err == NO_INIT) {
1716 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001717 "Camera %s: Deferred surface is invalid: %s (%d)",
1718 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001719 } else {
1720 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001721 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1722 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001723 }
1724
1725 return res;
1726}
1727
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001728binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001729 ATRACE_CALL();
1730 binder::Status res;
1731 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1732
1733 if (!isValidAudioRestriction(mode)) {
1734 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1735 mCameraIdStr.string(), mode);
1736 ALOGW("%s: %s", __FUNCTION__, msg.string());
1737 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1738 }
1739
1740 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001741 BasicClient::setAudioRestriction(mode);
1742 return binder::Status::ok();
1743}
1744
1745binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1746 ATRACE_CALL();
1747 binder::Status res;
1748 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1749 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001750 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001751 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001752 }
1753 return binder::Status::ok();
1754}
1755
Ravneetaeb20dc2022-03-30 05:33:03 +00001756status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1757 return mDevice->setCameraServiceWatchdog(enabled);
1758}
1759
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001760status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1761 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1762
1763 return mDevice->setRotateAndCropAutoBehavior(
1764 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1765}
1766
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001767status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1768 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1769
1770 return mDevice->setAutoframingAutoBehavior(
1771 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1772}
1773
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001774bool CameraDeviceClient::supportsCameraMute() {
1775 return mDevice->supportsCameraMute();
1776}
1777
1778status_t CameraDeviceClient::setCameraMute(bool enabled) {
1779 return mDevice->setCameraMute(enabled);
1780}
1781
Shuzhen Wang16610a62022-12-15 22:38:07 -08001782void CameraDeviceClient::setStreamUseCaseOverrides(
1783 const std::vector<int64_t>& useCaseOverrides) {
1784 mDevice->setStreamUseCaseOverrides(useCaseOverrides);
1785}
1786
1787void CameraDeviceClient::clearStreamUseCaseOverrides() {
1788 mDevice->clearStreamUseCaseOverrides();
1789}
1790
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001791binder::Status CameraDeviceClient::switchToOffline(
1792 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001793 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001794 /*out*/
1795 sp<hardware::camera2::ICameraOfflineSession>* session) {
1796 ATRACE_CALL();
1797
1798 binder::Status res;
1799 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1800
1801 Mutex::Autolock icl(mBinderSerializationLock);
1802
1803 if (!mDevice.get()) {
1804 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1805 }
1806
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001807 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001808 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001809 ALOGE("%s: %s", __FUNCTION__, msg.string());
1810 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1811 }
1812
1813 if (session == nullptr) {
1814 String8 msg = String8::format("Invalid offline session");
1815 ALOGE("%s: %s", __FUNCTION__, msg.string());
1816 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1817 }
1818
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001819 std::vector<int32_t> offlineStreamIds;
1820 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001821 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001822 for (const auto& streamId : offlineOutputIds) {
1823 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001824 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001825 String8 msg = String8::format("Offline surface with id: %d is not registered",
1826 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001827 ALOGE("%s: %s", __FUNCTION__, msg.string());
1828 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1829 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001830
1831 if (!mStreamInfoMap[streamId].supportsOffline) {
1832 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1833 "offline mode", streamId);
1834 ALOGE("%s: %s", __FUNCTION__, msg.string());
1835 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1836 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001837
Emilian Peev2f5d6012022-01-19 16:16:50 -08001838 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001839 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001840 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001841 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001842 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peev434248e2022-10-06 14:58:54 -07001843 camera3::HeicCompositeStream::isHeicCompositeStream(s) ||
1844 camera3::JpegRCompositeStream::isJpegRCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001845 if (isCompositeStream) {
1846 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1847 if (compositeIdx == NAME_NOT_FOUND) {
1848 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1849 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1850 "Unknown composite stream");
1851 }
1852
1853 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1854 &offlineStreamIds);
1855 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1856 mCompositeStreamMap.valueAt(compositeIdx));
1857 break;
1858 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001859 }
1860
Emilian Peev4697b642019-11-19 17:11:14 -08001861 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001862 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001863 }
1864 }
1865
1866 sp<CameraOfflineSessionBase> offlineSession;
1867 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1868 if (ret != OK) {
1869 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1870 "Camera %s: Error switching to offline mode: %s (%d)",
1871 mCameraIdStr.string(), strerror(ret), ret);
1872 }
1873
Emilian Peevcc0b7952020-01-07 13:54:47 -08001874 sp<CameraOfflineSessionClient> offlineClient;
1875 if (offlineSession.get() != nullptr) {
1876 offlineClient = new CameraOfflineSessionClient(sCameraService,
1877 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001878 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1879 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001880 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1881 }
1882
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001883 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001884 // A successful offline session switch must reset the current camera client
1885 // and release any resources occupied by previously configured streams.
1886 mStreamMap.clear();
1887 mConfiguredOutputs.clear();
1888 mDeferredStreams.clear();
1889 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001890 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001891 mCompositeStreamMap.clear();
1892 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001893 } else {
Emilian Peev886ac212023-02-07 14:10:24 -08001894 // In case we failed to register the offline client, ensure that it still initialized
1895 // so that all failing requests can return back correctly once the object is released.
1896 offlineClient->initialize(nullptr /*cameraProviderManager*/, String8()/*monitorTags*/);
1897
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001898 switch(ret) {
1899 case BAD_VALUE:
1900 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1901 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1902 case TIMED_OUT:
1903 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1904 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1905 default:
1906 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1907 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1908 strerror(-ret), ret);
1909 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001910 }
1911
1912 *session = offlineClient;
1913
1914 return binder::Status::ok();
1915}
1916
Igor Murashkine7ee7632013-06-11 18:10:18 -07001917status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001918 return BasicClient::dump(fd, args);
1919}
1920
1921status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001922 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001923 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001924 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001925 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001926 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001927
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001928 dprintf(fd, " State:\n");
1929 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001930 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001931 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001932 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001933 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001934 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001935 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001936 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001937 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001938 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001939 mStreamMap.valueAt(i).streamId(),
1940 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001941 }
Zhijun He5d677d12016-05-29 16:52:39 -07001942 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001943 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001944 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001945 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001946 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001947 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001948 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001949 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001950 // TODO: print dynamic/request section from most recent requests
1951 mFrameProcessor->dump(fd, args);
1952
1953 return dumpDevice(fd, args);
1954}
1955
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001956status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1957 sp<CameraDeviceBase> device = mDevice;
1958 if (!device) {
1959 dprintf(out, " Device is detached.");
1960 return OK;
1961 }
1962 device->startWatchingTags(tags);
1963 return OK;
1964}
1965
1966status_t CameraDeviceClient::stopWatchingTags(int out) {
1967 sp<CameraDeviceBase> device = mDevice;
1968 if (!device) {
1969 dprintf(out, " Device is detached.");
1970 return OK;
1971 }
1972 device->stopWatchingTags();
1973 return OK;
1974}
1975
1976status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1977 sp<CameraDeviceBase> device = mDevice;
1978 if (!device) {
1979 return OK;
1980 }
1981 device->dumpWatchedEventsToVector(out);
1982 return OK;
1983}
1984
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001985void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001986 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001987 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001988 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001989
Emilian Peev538c90e2018-12-17 18:03:19 +00001990 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001991 {
1992 // Access to the composite stream map must be synchronized
1993 Mutex::Autolock l(mCompositeLock);
1994 // Composites can have multiple internal streams. Error notifications coming from such
1995 // internal streams may need to remain within camera service.
1996 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1997 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1998 resultExtras);
1999 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002000 }
2001
2002 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002003 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002004 }
2005}
2006
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002007void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
2008 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2009
2010 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07002011 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002012 }
2013
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07002014 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07002015 mStreamingRequestId = REQUEST_ID_NONE;
2016}
2017
Shuzhen Wang316781a2020-08-18 18:11:01 -07002018void CameraDeviceClient::notifyIdle(
2019 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
2020 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002021 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002022 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002023
2024 if (remoteCb != 0) {
2025 remoteCb->onDeviceIdle();
2026 }
Emilian Peev567c31c2023-03-06 15:02:37 -08002027
2028 std::vector<hardware::CameraStreamStats> fullStreamStats = streamStats;
2029 {
2030 Mutex::Autolock l(mCompositeLock);
2031 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2032 hardware::CameraStreamStats compositeStats;
2033 mCompositeStreamMap.valueAt(i)->getStreamStats(&compositeStats);
2034 if (compositeStats.mWidth > 0) {
2035 fullStreamStats.push_back(compositeStats);
2036 }
2037 }
2038 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08002039 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Emilian Peev567c31c2023-03-06 15:02:37 -08002040 fullStreamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002041}
2042
Jianing Weicb0652e2014-03-12 18:29:36 -07002043void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002044 nsecs_t timestamp) {
2045 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002046 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002047 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002048 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002049 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002050 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002051
Emilian Peev2f5d6012022-01-19 16:16:50 -08002052 // Access to the composite stream map must be synchronized
2053 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002054 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2055 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2056 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002057}
2058
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002059void CameraDeviceClient::notifyPrepared(int streamId) {
2060 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002061 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002062 if (remoteCb != 0) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00002063 ALOGV("%s: stream id %d", __FUNCTION__, streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002064 remoteCb->onPrepared(streamId);
2065 }
2066}
2067
Shuzhen Wang9d066012016-09-30 11:30:20 -07002068void CameraDeviceClient::notifyRequestQueueEmpty() {
2069 // Thread safe. Don't bother locking.
2070 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2071 if (remoteCb != 0) {
2072 remoteCb->onRequestQueueEmpty();
2073 }
2074}
2075
Igor Murashkine7ee7632013-06-11 18:10:18 -07002076void CameraDeviceClient::detachDevice() {
2077 if (mDevice == 0) return;
2078
Shuzhen Wang316781a2020-08-18 18:11:01 -07002079 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002080 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002081
Emilian Peev7ee731f2022-02-08 14:55:52 -08002082 if (mFrameProcessor.get() != nullptr) {
2083 mFrameProcessor->removeListener(
2084 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2085 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2086 mFrameProcessor->requestExit();
2087 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2088 mFrameProcessor->join();
2089 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2090 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002091
2092 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2093 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002094 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002095 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002096 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2097 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2098 }
2099
Igor Murashkine7ee7632013-06-11 18:10:18 -07002100 if ((code = mDevice->waitUntilDrained()) != OK) {
2101 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2102 code);
2103 }
2104 }
2105
Emilian Peev2f5d6012022-01-19 16:16:50 -08002106 {
2107 Mutex::Autolock l(mCompositeLock);
2108 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2109 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2110 if (ret != OK) {
2111 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2112 strerror(-ret), ret);
2113 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002114 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002115 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002116 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002117
Shuzhen Wang03fe6232023-02-05 12:41:15 -08002118 bool hasDeviceError = mDevice->hasDeviceError();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002119 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002120
2121 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
Shuzhen Wang03fe6232023-02-05 12:41:15 -08002122 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs, hasDeviceError);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002123}
2124
2125/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002126void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002127 ATRACE_CALL();
2128 ALOGV("%s", __FUNCTION__);
2129
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002130 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002131 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002132 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002133 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2134 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002135 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002136
Emilian Peev2f5d6012022-01-19 16:16:50 -08002137 // Access to the composite stream map must be synchronized
2138 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002139 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2140 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2141 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002142}
2143
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002144binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002145 if (mDisconnected) {
2146 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2147 "The camera device has been disconnected");
2148 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002149 status_t res = checkPid(checkLocation);
2150 return (res == OK) ? binder::Status::ok() :
2151 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2152 "Attempt to use camera from a different process than original client");
2153}
2154
Igor Murashkine7ee7632013-06-11 18:10:18 -07002155// TODO: move to Camera2ClientBase
2156bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2157
Jayant Chowdhary12361932018-08-27 14:46:13 -07002158 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002159 const int selfPid = getpid();
2160 camera_metadata_entry_t entry;
2161
2162 /**
2163 * Mixin default important security values
2164 * - android.led.transmit = defaulted ON
2165 */
2166 CameraMetadata staticInfo = mDevice->info();
2167 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2168 for(size_t i = 0; i < entry.count; ++i) {
2169 uint8_t led = entry.data.u8[i];
2170
2171 switch(led) {
2172 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2173 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2174 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2175 metadata.update(ANDROID_LED_TRANSMIT,
2176 &transmitDefault, 1);
2177 }
2178 break;
2179 }
2180 }
2181 }
2182
2183 // We can do anything!
2184 if (pid == selfPid) {
2185 return true;
2186 }
2187
2188 /**
2189 * Permission check special fields in the request
2190 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2191 */
2192 entry = metadata.find(ANDROID_LED_TRANSMIT);
2193 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2194 String16 permissionString =
2195 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2196 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002197 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002198 ALOGE("Permission Denial: "
2199 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2200 return false;
2201 }
2202 }
2203
2204 return true;
2205}
2206
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002207status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2208 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002209 ALOGV("%s: begin", __FUNCTION__);
2210
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002211 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002212 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002213}
2214
Emilian Peevf4816702020-04-03 15:44:51 -07002215binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2216 camera_request_template_t* tempId /*out*/) {
2217 binder::Status ret = binder::Status::ok();
2218
2219 if (tempId == nullptr) {
2220 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2221 "Camera %s: Invalid template argument", mCameraIdStr.string());
2222 return ret;
2223 }
2224 switch(templateId) {
2225 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2226 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2227 break;
2228 case ICameraDeviceUser::TEMPLATE_RECORD:
2229 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2230 break;
2231 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2232 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2233 break;
2234 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2235 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2236 break;
2237 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2238 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2239 break;
2240 case ICameraDeviceUser::TEMPLATE_MANUAL:
2241 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2242 break;
2243 default:
2244 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2245 "Camera %s: Template ID %d is invalid or not supported",
2246 mCameraIdStr.string(), templateId);
2247 return ret;
2248 }
2249
2250 return ret;
2251}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002252
2253const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2254 if (mDevice->getId() == cameraId) {
2255 return mDevice->info();
2256 }
2257 return mDevice->infoPhysical(cameraId);
2258}
2259
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08002260bool CameraDeviceClient::supportsUltraHighResolutionCapture(const String8 &cameraId) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002261 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -08002262 return SessionConfigurationUtils::supportsUltraHighResolutionCapture(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002263}
2264
2265bool CameraDeviceClient::isSensorPixelModeConsistent(
2266 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2267 // First we get the sensorPixelMode from the settings metadata.
2268 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2269 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2270 if (sensorPixelModeEntry.count != 0) {
2271 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2272 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2273 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2274 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2275 __FUNCTION__, sensorPixelMode);
2276 return false;
2277 }
2278 }
2279 // Check whether each stream has max resolution allowed.
2280 bool consistent = true;
2281 for (auto it : streamIdList) {
2282 auto const streamInfoIt = mStreamInfoMap.find(it);
2283 if (streamInfoIt == mStreamInfoMap.end()) {
2284 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2285 return false;
2286 }
2287 consistent =
2288 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2289 streamInfoIt->second.sensorPixelModesUsed.end();
2290 if (!consistent) {
2291 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2292 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2293 ALOGE("sensor pixel mode used list: %i", m);
2294 }
2295 break;
2296 }
2297 }
2298
2299 return consistent;
2300}
2301
Igor Murashkine7ee7632013-06-11 18:10:18 -07002302} // namespace android