blob: b219cc853da098894eb9bfd7a9f61d17850a9e0e [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"
Shuzhen Wang316781a2020-08-18 18:11:01 -070034#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070035
Emilian Peev00420d22018-02-05 21:33:13 +000036#include <camera_metadata_hidden.h>
37
Emilian Peev538c90e2018-12-17 18:03:19 +000038#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080039#include "HeicCompositeStream.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,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080064 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,
69 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080070 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080071 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080072 clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080073 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080074 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080075 cameraId,
76 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070077 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080078 clientPid,
79 clientUid,
80 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070081 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080082 // We don't need it for API2 clients, but Camera2ClientBase requires it.
83 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070084}
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,
90 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080091 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090092 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080093 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080094 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070095 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080096 int clientPid,
97 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070098 int servicePid,
99 bool overrideForPerfClass) :
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -0800100 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, systemNativeClient,
101 clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing, sensorOrientation,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700102 clientPid, clientUid, servicePid, overrideForPerfClass),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700103 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700104 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700105 mRequestIdCounter(0),
106 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700107
108 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800109 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110}
111
Emilian Peevbd8c5032018-02-14 23:05:40 +0000112status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
113 const String8& monitorTags) {
114 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800115}
116
117template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000118status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700119 ATRACE_CALL();
120 status_t res;
121
Emilian Peevbd8c5032018-02-14 23:05:40 +0000122 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700123 if (res != OK) {
124 return res;
125 }
126
127 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700128 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800129 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700130 mFrameProcessor->run(threadName.string());
131
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800132 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
133 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800134 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700135 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700136
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800137 const CameraMetadata &deviceInfo = mDevice->info();
138 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000139 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
140 if (physicalKeysEntry.count > 0) {
141 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
142 physicalKeysEntry.data.i32,
143 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
144 }
145
Emilian Peev2295df72021-11-12 18:14:10 -0800146 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
147 mDynamicProfileMap.emplace(
148 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
149 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
150 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800151 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800152 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800153 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800154 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800155 if (entry.count > 0 || ((entry.count % 3) != 0)) {
156 int64_t standardBitmap =
157 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
158 for (size_t i = 0; i < entry.count; i += 3) {
159 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800160 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800161 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
162 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800163 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800164 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800165 }
166 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800167 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
168 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800169 }
170 }
Emilian Peevef715e22022-05-19 17:44:33 -0700171 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
172 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800173 } else {
174 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
175 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
176 }
177 }
178 }
179
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700180 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800181 // Cache physical camera ids corresponding to this device and also the high
182 // resolution sensors in this device + physical camera ids
183 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
184 if (isUltraHighResolutionSensor(mCameraIdStr)) {
185 mHighResolutionSensors.insert(mCameraIdStr.string());
186 }
187 for (auto &physicalId : mPhysicalCameraIds) {
188 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
189 mHighResolutionSensors.insert(physicalId.c_str());
190 }
191 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700192 return OK;
193}
194
195CameraDeviceClient::~CameraDeviceClient() {
196}
197
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800198binder::Status CameraDeviceClient::submitRequest(
199 const hardware::camera2::CaptureRequest& request,
200 bool streaming,
201 /*out*/
202 hardware::camera2::utils::SubmitInfo *submitInfo) {
203 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
204 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700205}
206
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800207binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000208 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000209 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800210 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
211
Emilian Peev2f5d6012022-01-19 16:16:50 -0800212 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800213 // Trying to submit request with surface that wasn't created
214 if (idx == NAME_NOT_FOUND) {
215 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
216 " we have not called createStream on",
217 __FUNCTION__, mCameraIdStr.string());
218 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
219 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000220 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
221 != NAME_NOT_FOUND) {
222 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
223 currentStreamId);
224 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800225 }
226
227 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
228 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800229 outputStreamIds->push_back(streamSurfaceId.streamId());
230 }
231 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
232
233 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
234 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
235 streamSurfaceId.surfaceId());
236
Emilian Peevf873aa52018-01-26 14:58:28 +0000237 if (currentStreamId != nullptr) {
238 *currentStreamId = streamSurfaceId.streamId();
239 }
240
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800241 return binder::Status::ok();
242}
243
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800244static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
245 const Vector<int> &streamIdsForThisRequest) {
246 std::list<int> intersection;
247 for (auto &streamId : streamIdsForThisRequest) {
248 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
249 intersection.emplace_back(streamId);
250 }
251 }
252 return intersection;
253}
254
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800255binder::Status CameraDeviceClient::submitRequestList(
256 const std::vector<hardware::camera2::CaptureRequest>& requests,
257 bool streaming,
258 /*out*/
259 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700260 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700261 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700262
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800263 binder::Status res = binder::Status::ok();
264 status_t err;
265 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
266 return res;
267 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700268
269 Mutex::Autolock icl(mBinderSerializationLock);
270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800271 if (!mDevice.get()) {
272 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
273 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700274
275 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800276 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
277 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800278 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700279 }
280
Emilian Peevaebbe412018-01-15 13:53:24 +0000281 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700282 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700284 uint32_t loopCounter = 0;
285
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800286 for (auto&& request: requests) {
287 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700288 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800289 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
290 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800292 "No input configured for camera %s but request is for reprocessing",
293 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700294 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800295 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
296 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800297 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
298 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000299 } else if (request.mPhysicalCameraSettings.size() > 1) {
300 ALOGE("%s: Camera %s: reprocess requests not supported for "
301 "multiple physical cameras.", __FUNCTION__,
302 mCameraIdStr.string());
303 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
304 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700305 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700306 }
307
Emilian Peevaebbe412018-01-15 13:53:24 +0000308 if (request.mPhysicalCameraSettings.empty()) {
309 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
310 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800311 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000312 "Request doesn't contain any settings");
313 }
314
315 //The first capture settings should always match the logical camera id
316 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
317 if (mDevice->getId() != logicalId) {
318 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
319 mCameraIdStr.string());
320 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
321 "Invalid camera request settings");
322 }
323
Emilian Peevaebbe412018-01-15 13:53:24 +0000324 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800325 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
326 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800327 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
328 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700329 }
330
Jianing Wei90e59c92014-03-12 18:29:36 -0700331 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700332 * Write in the output stream IDs and map from stream ID to surface ID
333 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700334 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700335 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700336 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000337 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800338 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800339 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800340 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800341 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700342
Emilian Peevf873aa52018-01-26 14:58:28 +0000343 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800344 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000345 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800346 if (!res.isOk()) {
347 return res;
348 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000349
350 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
351 if (index >= 0) {
352 String8 requestedPhysicalId(
353 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
354 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800355 dynamicProfileBitmap |=
356 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000357 } else {
358 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
359 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700360 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800361 } else {
362 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
363 int streamId = request.mStreamIdxList.itemAt(i);
364 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700365
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800366 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
367 if (index < 0) {
368 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
369 " we have not called createStream on: stream %d",
370 __FUNCTION__, mCameraIdStr.string(), streamId);
371 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
372 "Request targets Surface that is not part of current capture session");
373 }
374
375 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
376 if ((size_t)surfaceIdx >= gbps.size()) {
377 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
378 " we have not called createStream on: stream %d, surfaceIdx %d",
379 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
380 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
381 "Request targets Surface has invalid surface index");
382 }
383
Emilian Peevf873aa52018-01-26 14:58:28 +0000384 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800385 if (!res.isOk()) {
386 return res;
387 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000388
389 String8 requestedPhysicalId(
390 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
391 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800392 dynamicProfileBitmap |=
393 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
394 }
395 }
396
397 if (dynamicProfileBitmap !=
398 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
399 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
400 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
401 if ((dynamicProfileBitmap & i) == 0) {
402 continue;
403 }
404
405 const auto& it = mDynamicProfileMap.find(i);
406 if (it != mDynamicProfileMap.end()) {
407 if ((it->second == 0) ||
408 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
409 continue;
410 } else {
411 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
412 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800413 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800414 dynamicProfileBitmap);
415 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
416 "Request targets an unsupported dynamic range profile"
417 " combination");
418 }
419 } else {
420 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
421 " references unsupported dynamic range profile 0x%x!",
422 __FUNCTION__, mCameraIdStr.string(), i);
423 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
424 "Request targets 10-bit Surface with unsupported dynamic range"
425 " profile");
426 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700427 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700428 }
429
Emilian Peevf873aa52018-01-26 14:58:28 +0000430 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
431 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000432 if (it.settings.isEmpty()) {
433 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
434 __FUNCTION__, mCameraIdStr.string());
435 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
436 "Request settings are empty");
437 }
438
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800439 // Check whether the physical / logical stream has settings
440 // consistent with the sensor pixel mode(s) it was configured with.
441 // mCameraIdToStreamSet will only have ids that are high resolution
442 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
443 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
444 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
445 outputStreamIds);
446 if (!request.mIsReprocess &&
447 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
448 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
449 "consistent with configured streams. Rejecting request.",
450 __FUNCTION__, it.id.c_str());
451 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
452 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
453 "streams configured");
454 }
455 }
456
Emilian Peevf873aa52018-01-26 14:58:28 +0000457 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700458 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
459 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
460 mSupportedPhysicalRequestKeys.end();
461 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
462 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
463 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000464 if (physicalId != mDevice->getId()) {
465 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
466 it.id);
467 if (found == requestedPhysicalIds.end()) {
468 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
469 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
470 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
471 "Invalid physical camera id");
472 }
Emilian Peev00420d22018-02-05 21:33:13 +0000473
474 if (!mSupportedPhysicalRequestKeys.empty()) {
475 // Filter out any unsupported physical request keys.
476 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
477 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
478 filteredParams.getAndLock());
479 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
480 filteredParams.unlock(meta);
481
482 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
483 camera_metadata_ro_entry entry = it.settings.find(keyIt);
484 if (entry.count > 0) {
485 filteredParams.update(entry);
486 }
487 }
488
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700489 physicalSettingsList.push_back({it.id, filteredParams,
490 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000491 }
492 } else {
493 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000494 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000495 }
496
497 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
498 // Callee logs
499 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
500 "Caller does not have permission to change restricted controls");
501 }
502
Emilian Peevaebbe412018-01-15 13:53:24 +0000503 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
504 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700505
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800506 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000507 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
508 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700509 }
510
Emilian Peevaebbe412018-01-15 13:53:24 +0000511 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
512 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700513 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800514 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
515 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
516 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700517
Emilian Peevaebbe412018-01-15 13:53:24 +0000518 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700519 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800520
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700521 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800522 if (!request.mUserTag.empty()) {
523 mUserTag = request.mUserTag;
524 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700525 camera_metadata_entry entry =
526 physicalSettingsList.begin()->metadata.find(
527 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
528 if (entry.count == 1) {
529 mVideoStabilizationMode = entry.data.u8[0];
530 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700531 }
532 mRequestIdCounter++;
533
534 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700535 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
536 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537 if (err != OK) {
538 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800539 "Camera %s: Got error %s (%d) after trying to set streaming request",
540 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800541 ALOGE("%s: %s", __FUNCTION__, msg.string());
542 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
543 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700544 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700545 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700546 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700547 }
548 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700549 err = mDevice->captureList(metadataRequestList, surfaceMapList,
550 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 if (err != OK) {
552 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800553 "Camera %s: Got error %s (%d) after trying to submit capture request",
554 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800555 ALOGE("%s: %s", __FUNCTION__, msg.string());
556 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
557 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700558 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700560 }
561
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800562 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700563 return res;
564}
565
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800566binder::Status CameraDeviceClient::cancelRequest(
567 int requestId,
568 /*out*/
569 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700570 ATRACE_CALL();
571 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
572
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800573 status_t err;
574 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700575
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800576 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700577
578 Mutex::Autolock icl(mBinderSerializationLock);
579
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800580 if (!mDevice.get()) {
581 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
582 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700583
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700584 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700585 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800586 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
587 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 ALOGE("%s: %s", __FUNCTION__, msg.string());
589 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700590 }
591
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800592 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700593
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800594 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800595 ALOGV("%s: Camera %s: Successfully cleared streaming request",
596 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700597 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800598 } else {
599 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800600 "Camera %s: Error clearing streaming request: %s (%d)",
601 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700602 }
603
604 return res;
605}
606
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800607binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700608 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700609 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700610 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800611 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700612}
613
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100614binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700615 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800616 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700617 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700618 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
619 __FUNCTION__, mInputStream.configured ? 1 : 0,
620 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700621
Zhijun He0effd522016-03-04 10:22:27 -0800622 binder::Status res;
623 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
624
Emilian Peevcc0b7952020-01-07 13:54:47 -0800625 if (offlineStreamIds == nullptr) {
626 String8 msg = String8::format("Invalid offline stream ids");
627 ALOGE("%s: %s", __FUNCTION__, msg.string());
628 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
629 }
630
Zhijun He0effd522016-03-04 10:22:27 -0800631 Mutex::Autolock icl(mBinderSerializationLock);
632
633 if (!mDevice.get()) {
634 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
635 }
636
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700637 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000638 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000639 if (!res.isOk()) {
640 return res;
641 }
642
643 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
644 if (err == BAD_VALUE) {
645 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
646 mCameraIdStr.string());
647 ALOGE("%s: %s", __FUNCTION__, msg.string());
648 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
649 } else if (err != OK) {
650 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
651 mCameraIdStr.string(), strerror(-err), err);
652 ALOGE("%s: %s", __FUNCTION__, msg.string());
653 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000654 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800655 offlineStreamIds->clear();
656 mDevice->getOfflineStreamIds(offlineStreamIds);
657
Emilian Peev2f5d6012022-01-19 16:16:50 -0800658 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000659 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
660 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800661 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000662 String8 msg = String8::format("Camera %s: Error configuring composite "
663 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
664 ALOGE("%s: %s", __FUNCTION__, msg.string());
665 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
666 break;
667 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800668
669 // Composite streams can only support offline mode in case all individual internal
670 // streams are also supported.
671 std::vector<int> internalStreams;
672 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800673 offlineStreamIds->erase(
674 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800675 [&internalStreams] (int streamId) {
676 auto it = std::find(internalStreams.begin(), internalStreams.end(),
677 streamId);
678 if (it != internalStreams.end()) {
679 internalStreams.erase(it);
680 return true;
681 }
682
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800683 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800684 if (internalStreams.empty()) {
685 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
686 }
687 }
688
689 for (const auto& offlineStreamId : *offlineStreamIds) {
690 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000691 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700692
693 nsecs_t configureEnd = systemTime();
694 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
695 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
696 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000697 }
698
699 return res;
700}
701
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800702binder::Status CameraDeviceClient::isSessionConfigurationSupported(
703 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800704
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800705 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800706 binder::Status res;
707 status_t ret = OK;
708 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
709
710 Mutex::Autolock icl(mBinderSerializationLock);
711
712 if (!mDevice.get()) {
713 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
714 }
715
716 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700717 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000718 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800719 if (!res.isOk()) {
720 return res;
721 }
722
723 if (status == nullptr) {
724 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
725 ALOGE("%s: %s", __FUNCTION__, msg.string());
726 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
727 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700728
Emilian Peev35ae8262018-11-08 13:11:32 +0000729 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700730 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
731 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000732 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700733 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000734 switch (ret) {
735 case OK:
736 // Expected, do nothing.
737 break;
738 case INVALID_OPERATION: {
739 String8 msg = String8::format(
740 "Camera %s: Session configuration query not supported!",
741 mCameraIdStr.string());
742 ALOGD("%s: %s", __FUNCTION__, msg.string());
743 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
744 }
745
746 break;
747 default: {
748 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
749 strerror(-ret), ret);
750 ALOGE("%s: %s", __FUNCTION__, msg.string());
751 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
752 msg.string());
753 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800754 }
755
756 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700757}
758
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800759binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700760 ATRACE_CALL();
761 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
762
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800763 binder::Status res;
764 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700765
766 Mutex::Autolock icl(mBinderSerializationLock);
767
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800768 if (!mDevice.get()) {
769 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
770 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700771
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700772 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700774 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000775 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700776
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700777 if (mInputStream.configured && mInputStream.id == streamId) {
778 isInput = true;
779 } else {
780 // Guard against trying to delete non-created streams
781 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700782 if (streamId == mStreamMap.valueAt(i).streamId()) {
783 surfaces.push_back(mStreamMap.keyAt(i));
784 }
785 }
786
787 // See if this stream is one of the deferred streams.
788 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
789 if (streamId == mDeferredStreams[i]) {
790 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700791 break;
792 }
793 }
794
Emilian Peev2f5d6012022-01-19 16:16:50 -0800795 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000796 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
797 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
798 compositeIndex = i;
799 break;
800 }
801 }
802
Shuzhen Wang0129d522016-10-30 22:43:41 -0700803 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
804 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
805 " stream created yet", mCameraIdStr.string(), streamId);
806 ALOGW("%s: %s", __FUNCTION__, msg.string());
807 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700808 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700809 }
810
811 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800812 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700813
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800814 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800815 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
816 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800817 ALOGE("%s: %s", __FUNCTION__, msg.string());
818 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
819 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700820 if (isInput) {
821 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700822 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700823 for (auto& surface : surfaces) {
824 mStreamMap.removeItem(surface);
825 }
826
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800827 mConfiguredOutputs.removeItem(streamId);
828
Shuzhen Wang0129d522016-10-30 22:43:41 -0700829 if (dIndex != NAME_NOT_FOUND) {
830 mDeferredStreams.removeItemsAt(dIndex);
831 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000832
833 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800834 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000835 status_t ret;
836 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
837 != OK) {
838 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
839 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
840 streamId);
841 ALOGE("%s: %s", __FUNCTION__, msg.string());
842 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
843 }
844 mCompositeStreamMap.removeItemsAt(compositeIndex);
845 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800846 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
847 auto &streamSet = mapIt.second;
848 if (streamSet.find(streamId) != streamSet.end()) {
849 streamSet.erase(streamId);
850 break;
851 }
852 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700853 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700854 }
855
856 return res;
857}
858
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800859binder::Status CameraDeviceClient::createStream(
860 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
861 /*out*/
862 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700863 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700864
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800865 binder::Status res;
866 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700867
868 Mutex::Autolock icl(mBinderSerializationLock);
869
Shuzhen Wang0129d522016-10-30 22:43:41 -0700870 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
871 outputConfiguration.getGraphicBufferProducers();
872 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800873 bool deferredConsumer = outputConfiguration.isDeferred();
874 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800875 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800876 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800877 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800878 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800879 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800880 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800881 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700883 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000884 outputConfiguration.getSurfaceType());
885 if (!res.isOk()) {
886 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700887 }
Zhijun He5d677d12016-05-29 16:52:39 -0700888
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800889 if (!mDevice.get()) {
890 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
891 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700892 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800893 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000894 if (!res.isOk()) {
895 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800896 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000897
Shuzhen Wang0129d522016-10-30 22:43:41 -0700898 std::vector<sp<Surface>> surfaces;
899 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700900 status_t err;
901
902 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700903 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800904 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700905 }
906
Shuzhen Wang758c2152017-01-10 18:26:18 -0800907 OutputStreamInfo streamInfo;
908 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800909 const std::vector<int32_t> &sensorPixelModesUsed =
910 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700911 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700912 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700913 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800914 ssize_t index = mStreamMap.indexOfKey(binder);
915 if (index != NAME_NOT_FOUND) {
916 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
917 "(ID %zd)", mCameraIdStr.string(), index);
918 ALOGW("%s: %s", __FUNCTION__, msg.string());
919 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700920 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700921
Shuzhen Wang758c2152017-01-10 18:26:18 -0800922 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700923 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800924 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800925 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800926 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800927
928 if (!res.isOk())
929 return res;
930
931 if (!isStreamInfoValid) {
932 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700933 }
934
Shuzhen Wang758c2152017-01-10 18:26:18 -0800935 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700936 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800937 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700938
939 // If mOverrideForPerfClass is true, do not fail createStream() for small
940 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
941 // closest possible supported size.
942
Zhijun He125684a2015-12-26 15:07:30 -0800943 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100944 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800945 bool isDepthCompositeStream =
946 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Chema Gonzalezdcc8a912022-12-21 08:59:30 -0800947 bool isHeicCompositeStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
948 if (isDepthCompositeStream || isHeicCompositeStream) {
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800949 sp<CompositeStream> compositeStream;
950 if (isDepthCompositeStream) {
951 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
952 } else {
953 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
954 }
955
Emilian Peev538c90e2018-12-17 18:03:19 +0000956 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
957 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700958 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800959 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
960 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000961 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800962 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000963 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
964 compositeStream);
965 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800966 } else {
967 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
968 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700969 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800970 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800971 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800972 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800973 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000974 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700975
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800976 if (err != OK) {
977 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800978 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800979 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
980 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800981 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700982 int i = 0;
983 for (auto& binder : binders) {
984 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
985 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100986 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
987 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700988 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800989
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800990 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800991 mStreamInfoMap[streamId] = streamInfo;
992
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800993 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700994 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800995 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
996 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700997
Zhijun He5d677d12016-05-29 16:52:39 -0700998 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800999 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001000
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001001 // Fill in mHighResolutionCameraIdToStreamIdSet map
1002 const String8 &cameraIdUsed =
1003 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1004 const char *cameraIdUsedCStr = cameraIdUsed.string();
1005 // Only needed for high resolution sensors
1006 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1007 mHighResolutionSensors.end()) {
1008 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1009 }
1010
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001011 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001012 }
1013
1014 return res;
1015}
1016
Zhijun He5d677d12016-05-29 16:52:39 -07001017binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1018 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001019 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001020 /*out*/
1021 int* newStreamId) {
1022 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001023 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001024 android_dataspace dataSpace;
1025 status_t err;
1026 binder::Status res;
1027
1028 if (!mDevice.get()) {
1029 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1030 }
1031
1032 // Infer the surface info for deferred surface stream creation.
1033 width = outputConfiguration.getWidth();
1034 height = outputConfiguration.getHeight();
1035 surfaceType = outputConfiguration.getSurfaceType();
1036 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1037 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1038 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1039 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1040 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1041 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1042 }
1043 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001044 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001045 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001046 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001047 const String8 &cameraIdUsed =
1048 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1049 // Here, we override sensor pixel modes
1050 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1051 const std::vector<int32_t> &sensorPixelModesUsed =
1052 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001053 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001054 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1055 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1056 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1057 "sensor pixel modes used not valid for deferred stream");
1058 }
1059
Shuzhen Wang0129d522016-10-30 22:43:41 -07001060 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1061 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001062 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001063 &streamId, physicalCameraId,
1064 overriddenSensorPixelModesUsed,
1065 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001066 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001067 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001068 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001069 outputConfiguration.getStreamUseCase(),
1070 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001071
1072 if (err != OK) {
1073 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001074 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1075 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001076 } else {
1077 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1078 // a separate list to track. Once the deferred surface is set, this id will be
1079 // relocated to mStreamMap.
1080 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001081 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001082 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001083 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001084 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001085 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001086 outputConfiguration.getTimestampBase(),
1087 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001088
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001089 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001090 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001091 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001092
1093 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001094 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001095
1096 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001097 // Fill in mHighResolutionCameraIdToStreamIdSet
1098 const char *cameraIdUsedCStr = cameraIdUsed.string();
1099 // Only needed for high resolution sensors
1100 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1101 mHighResolutionSensors.end()) {
1102 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1103 }
Zhijun He5d677d12016-05-29 16:52:39 -07001104 }
1105 return res;
1106}
1107
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001108binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001109 int32_t transform = 0;
1110 status_t err;
1111 binder::Status res;
1112
1113 if (!mDevice.get()) {
1114 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1115 }
1116
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001117 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001118 if (err != OK) {
1119 // Error logged by getRotationTransformLocked.
1120 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1121 "Unable to calculate rotation transform for new stream");
1122 }
1123
1124 err = mDevice->setStreamTransform(streamId, transform);
1125 if (err != OK) {
1126 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1127 streamId);
1128 ALOGE("%s: %s", __FUNCTION__, msg.string());
1129 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1130 }
1131
1132 return res;
1133}
Ruben Brunkbba75572014-11-20 17:29:50 -08001134
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001135binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001136 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001137 /*out*/
1138 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001139
1140 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001141 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1142 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001143
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001144 binder::Status res;
1145 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001146
1147 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001148
1149 if (!mDevice.get()) {
1150 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1151 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001152
1153 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001154 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001155 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001156 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1157 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001158 }
1159
1160 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001161 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001162 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001163 mInputStream.configured = true;
1164 mInputStream.width = width;
1165 mInputStream.height = height;
1166 mInputStream.format = format;
1167 mInputStream.id = streamId;
1168
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001169 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1170 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001171
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001172 *newStreamId = streamId;
1173 } else {
1174 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001175 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001176 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001177 }
1178
1179 return res;
1180}
1181
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001182binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001183
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184 binder::Status res;
1185 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1186
1187 if (inputSurface == NULL) {
1188 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001189 }
1190
1191 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001192 if (!mDevice.get()) {
1193 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1194 }
1195 sp<IGraphicBufferProducer> producer;
1196 status_t err = mDevice->getInputBufferProducer(&producer);
1197 if (err != OK) {
1198 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001199 "Camera %s: Error getting input Surface: %s (%d)",
1200 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001201 } else {
1202 inputSurface->name = String16("CameraInput");
1203 inputSurface->graphicBufferProducer = producer;
1204 }
1205 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001206}
1207
Emilian Peev40ead602017-09-26 15:46:36 +01001208binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1209 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1210 ATRACE_CALL();
1211
1212 binder::Status res;
1213 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1214
1215 Mutex::Autolock icl(mBinderSerializationLock);
1216
1217 if (!mDevice.get()) {
1218 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1219 }
1220
1221 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1222 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001223 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1224
Emilian Peev40ead602017-09-26 15:46:36 +01001225 auto producerCount = bufferProducers.size();
1226 if (producerCount == 0) {
1227 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1228 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1229 "bufferProducers must not be empty");
1230 }
1231
1232 // The first output is the one associated with the output configuration.
1233 // It should always be present, valid and the corresponding stream id should match.
1234 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1235 ssize_t index = mStreamMap.indexOfKey(binder);
1236 if (index == NAME_NOT_FOUND) {
1237 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1238 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1239 "OutputConfiguration is invalid");
1240 }
1241 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1242 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1243 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1244 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1245 "Stream id is invalid");
1246 }
1247
1248 std::vector<size_t> removedSurfaceIds;
1249 std::vector<sp<IBinder>> removedOutputs;
1250 std::vector<sp<Surface>> newOutputs;
1251 std::vector<OutputStreamInfo> streamInfos;
1252 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1253 for (auto &it : bufferProducers) {
1254 newOutputsMap.add(IInterface::asBinder(it), it);
1255 }
1256
1257 for (size_t i = 0; i < mStreamMap.size(); i++) {
1258 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1259 if (idx == NAME_NOT_FOUND) {
1260 if (mStreamMap[i].streamId() == streamId) {
1261 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1262 removedOutputs.push_back(mStreamMap.keyAt(i));
1263 }
1264 } else {
1265 if (mStreamMap[i].streamId() != streamId) {
1266 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1267 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1268 "Target Surface is invalid");
1269 }
1270 newOutputsMap.removeItemsAt(idx);
1271 }
1272 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001273 const std::vector<int32_t> &sensorPixelModesUsed =
1274 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001275 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001276 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001277 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001278 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001279
Emilian Peev40ead602017-09-26 15:46:36 +01001280 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1281 OutputStreamInfo outInfo;
1282 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001283 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001284 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001285 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001286 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001287 if (!res.isOk())
1288 return res;
1289
Emilian Peev40ead602017-09-26 15:46:36 +01001290 streamInfos.push_back(outInfo);
1291 newOutputs.push_back(surface);
1292 }
1293
1294 //Trivial case no changes required
1295 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1296 return binder::Status::ok();
1297 }
1298
1299 KeyedVector<sp<Surface>, size_t> outputMap;
1300 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1301 &outputMap);
1302 if (ret != OK) {
1303 switch (ret) {
1304 case NAME_NOT_FOUND:
1305 case BAD_VALUE:
1306 case -EBUSY:
1307 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1308 "Camera %s: Error updating stream: %s (%d)",
1309 mCameraIdStr.string(), strerror(ret), ret);
1310 break;
1311 default:
1312 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1313 "Camera %s: Error updating stream: %s (%d)",
1314 mCameraIdStr.string(), strerror(ret), ret);
1315 break;
1316 }
1317 } else {
1318 for (const auto &it : removedOutputs) {
1319 mStreamMap.removeItem(it);
1320 }
1321
1322 for (size_t i = 0; i < outputMap.size(); i++) {
1323 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1324 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1325 }
1326
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001327 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1328
Emilian Peev40ead602017-09-26 15:46:36 +01001329 ALOGV("%s: Camera %s: Successful stream ID %d update",
1330 __FUNCTION__, mCameraIdStr.string(), streamId);
1331 }
1332
1333 return res;
1334}
1335
Igor Murashkine7ee7632013-06-11 18:10:18 -07001336// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001337binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1338 /*out*/
1339 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001340{
1341 ATRACE_CALL();
1342 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1343
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001344 binder::Status res;
1345 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001346
1347 Mutex::Autolock icl(mBinderSerializationLock);
1348
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001349 if (!mDevice.get()) {
1350 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1351 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001352
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001353 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001354 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1355 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1356
1357 CameraMetadata metadata;
1358 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001359 request != NULL) {
1360
1361 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001362 } else if (err == BAD_VALUE) {
1363 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001364 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1365 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001366
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001367 } else {
1368 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001369 "Camera %s: Error creating default request for template %d: %s (%d)",
1370 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001371 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001372 return res;
1373}
1374
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375binder::Status CameraDeviceClient::getCameraInfo(
1376 /*out*/
1377 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001378{
1379 ATRACE_CALL();
1380 ALOGV("%s", __FUNCTION__);
1381
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001382 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001383
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001384 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001385
1386 Mutex::Autolock icl(mBinderSerializationLock);
1387
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001388 if (!mDevice.get()) {
1389 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1390 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001391
Igor Murashkin099b4572013-07-12 17:52:16 -07001392 if (info != NULL) {
1393 *info = mDevice->info(); // static camera metadata
1394 // TODO: merge with device-specific camera metadata
1395 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001396
1397 return res;
1398}
1399
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001400binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001401{
1402 ATRACE_CALL();
1403 ALOGV("%s", __FUNCTION__);
1404
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001405 binder::Status res;
1406 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -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 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001413
1414 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001415 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001416 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001417 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001418 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1419 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001420 ALOGE("%s: %s", __FUNCTION__, msg.string());
1421 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001422 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 status_t err = mDevice->waitUntilDrained();
1424 if (err != OK) {
1425 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001426 "Camera %s: Error waiting to drain: %s (%d)",
1427 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001428 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001429 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001430 return res;
1431}
1432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433binder::Status CameraDeviceClient::flush(
1434 /*out*/
1435 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001436 ATRACE_CALL();
1437 ALOGV("%s", __FUNCTION__);
1438
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001439 binder::Status res;
1440 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001441
1442 Mutex::Autolock icl(mBinderSerializationLock);
1443
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001444 if (!mDevice.get()) {
1445 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1446 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001447
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001448 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001449 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001450 status_t err = mDevice->flush(lastFrameNumber);
1451 if (err != OK) {
1452 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001453 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001454 }
1455 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001456}
1457
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001458binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001459 ATRACE_CALL();
1460 ALOGV("%s", __FUNCTION__);
1461
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001462 binder::Status res;
1463 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001464
1465 Mutex::Autolock icl(mBinderSerializationLock);
1466
1467 // Guard against trying to prepare non-created streams
1468 ssize_t index = NAME_NOT_FOUND;
1469 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001470 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001471 index = i;
1472 break;
1473 }
1474 }
1475
1476 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001477 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1478 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001479 ALOGW("%s: %s", __FUNCTION__, msg.string());
1480 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001481 }
1482
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001483 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1484 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001485 status_t err = mDevice->prepare(streamId);
1486 if (err == BAD_VALUE) {
1487 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001488 "Camera %s: Stream %d has already been used, and cannot be prepared",
1489 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001490 } else if (err != OK) {
1491 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001492 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001493 strerror(-err), err);
1494 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001495 return res;
1496}
1497
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001498binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001499 ATRACE_CALL();
1500 ALOGV("%s", __FUNCTION__);
1501
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001502 binder::Status res;
1503 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001504
1505 Mutex::Autolock icl(mBinderSerializationLock);
1506
1507 // Guard against trying to prepare non-created streams
1508 ssize_t index = NAME_NOT_FOUND;
1509 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001510 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001511 index = i;
1512 break;
1513 }
1514 }
1515
1516 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001517 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1518 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001519 ALOGW("%s: %s", __FUNCTION__, msg.string());
1520 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001521 }
1522
1523 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001524 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1525 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001526 ALOGE("%s: %s", __FUNCTION__, msg.string());
1527 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001528 }
1529
1530 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1531 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001532 status_t err = mDevice->prepare(maxCount, streamId);
1533 if (err == BAD_VALUE) {
1534 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001535 "Camera %s: Stream %d has already been used, and cannot be prepared",
1536 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001537 } else if (err != OK) {
1538 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001539 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001540 strerror(-err), err);
1541 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001542
1543 return res;
1544}
1545
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001546binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001547 ATRACE_CALL();
1548 ALOGV("%s", __FUNCTION__);
1549
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001550 binder::Status res;
1551 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001552
1553 Mutex::Autolock icl(mBinderSerializationLock);
1554
1555 // Guard against trying to prepare non-created streams
1556 ssize_t index = NAME_NOT_FOUND;
1557 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001558 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001559 index = i;
1560 break;
1561 }
1562 }
1563
1564 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001565 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1566 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001567 ALOGW("%s: %s", __FUNCTION__, msg.string());
1568 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001569 }
1570
1571 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1572 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001573 status_t err = mDevice->tearDown(streamId);
1574 if (err == BAD_VALUE) {
1575 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001576 "Camera %s: Stream %d is still in use, cannot be torn down",
1577 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001578 } else if (err != OK) {
1579 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001580 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001581 strerror(-err), err);
1582 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001583
1584 return res;
1585}
1586
Shuzhen Wang758c2152017-01-10 18:26:18 -08001587binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001588 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1589 ATRACE_CALL();
1590
1591 binder::Status res;
1592 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1593
1594 Mutex::Autolock icl(mBinderSerializationLock);
1595
Shuzhen Wang0129d522016-10-30 22:43:41 -07001596 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1597 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001598 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001599
Shuzhen Wang0129d522016-10-30 22:43:41 -07001600 if (bufferProducers.size() == 0) {
1601 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001602 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1603 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001604
Shuzhen Wang758c2152017-01-10 18:26:18 -08001605 // streamId should be in mStreamMap if this stream already has a surface attached
1606 // to it. Otherwise, it should be in mDeferredStreams.
1607 bool streamIdConfigured = false;
1608 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1609 for (size_t i = 0; i < mStreamMap.size(); i++) {
1610 if (mStreamMap.valueAt(i).streamId() == streamId) {
1611 streamIdConfigured = true;
1612 break;
1613 }
Zhijun He5d677d12016-05-29 16:52:39 -07001614 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001615 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1616 if (streamId == mDeferredStreams[i]) {
1617 deferredStreamIndex = i;
1618 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001619 }
1620
Shuzhen Wang758c2152017-01-10 18:26:18 -08001621 }
1622 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1623 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1624 "(ID %d)", mCameraIdStr.string(), streamId);
1625 ALOGW("%s: %s", __FUNCTION__, msg.string());
1626 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001627 }
1628
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001629 if (mStreamInfoMap[streamId].finalized) {
1630 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1631 " on stream ID %d", mCameraIdStr.string(), streamId);
1632 ALOGW("%s: %s", __FUNCTION__, msg.string());
1633 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1634 }
1635
Zhijun He5d677d12016-05-29 16:52:39 -07001636 if (!mDevice.get()) {
1637 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1638 }
1639
Shuzhen Wang758c2152017-01-10 18:26:18 -08001640 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001641 const std::vector<int32_t> &sensorPixelModesUsed =
1642 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001643 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001644 int64_t streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001645 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001646 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001647 for (auto& bufferProducer : bufferProducers) {
1648 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001649 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1650 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001651 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001652 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001653 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001654 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001655
1656 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001657 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001658 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001659 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001660 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001661
1662 if (!res.isOk())
1663 return res;
1664
1665 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001666 }
1667
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001668 // Gracefully handle case where finalizeOutputConfigurations is called
1669 // without any new surface.
1670 if (consumerSurfaces.size() == 0) {
1671 mStreamInfoMap[streamId].finalized = true;
1672 return res;
1673 }
1674
Zhijun He5d677d12016-05-29 16:52:39 -07001675 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001676 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001677 std::vector<int> consumerSurfaceIds;
1678 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001679 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001680 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1681 sp<IBinder> binder = IInterface::asBinder(
1682 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001683 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001684 binder.get(), streamId, consumerSurfaceIds[i]);
1685 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1686 }
1687 if (deferredStreamIndex != NAME_NOT_FOUND) {
1688 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001689 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001690 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001691 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001692 } else if (err == NO_INIT) {
1693 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001694 "Camera %s: Deferred surface is invalid: %s (%d)",
1695 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001696 } else {
1697 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001698 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1699 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001700 }
1701
1702 return res;
1703}
1704
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001705binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001706 ATRACE_CALL();
1707 binder::Status res;
1708 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1709
1710 if (!isValidAudioRestriction(mode)) {
1711 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1712 mCameraIdStr.string(), mode);
1713 ALOGW("%s: %s", __FUNCTION__, msg.string());
1714 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1715 }
1716
1717 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001718 BasicClient::setAudioRestriction(mode);
1719 return binder::Status::ok();
1720}
1721
1722binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1723 ATRACE_CALL();
1724 binder::Status res;
1725 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1726 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001727 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001728 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001729 }
1730 return binder::Status::ok();
1731}
1732
Ravneet74cd3732022-03-30 05:33:03 +00001733status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1734 return mDevice->setCameraServiceWatchdog(enabled);
1735}
1736
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001737status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1738 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1739
1740 return mDevice->setRotateAndCropAutoBehavior(
1741 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1742}
1743
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001744bool CameraDeviceClient::supportsCameraMute() {
1745 return mDevice->supportsCameraMute();
1746}
1747
1748status_t CameraDeviceClient::setCameraMute(bool enabled) {
1749 return mDevice->setCameraMute(enabled);
1750}
1751
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001752binder::Status CameraDeviceClient::switchToOffline(
1753 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001754 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001755 /*out*/
1756 sp<hardware::camera2::ICameraOfflineSession>* session) {
1757 ATRACE_CALL();
1758
1759 binder::Status res;
1760 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1761
1762 Mutex::Autolock icl(mBinderSerializationLock);
1763
1764 if (!mDevice.get()) {
1765 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1766 }
1767
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001768 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001769 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001770 ALOGE("%s: %s", __FUNCTION__, msg.string());
1771 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1772 }
1773
1774 if (session == nullptr) {
1775 String8 msg = String8::format("Invalid offline session");
1776 ALOGE("%s: %s", __FUNCTION__, msg.string());
1777 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1778 }
1779
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001780 std::vector<int32_t> offlineStreamIds;
1781 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001782 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001783 for (const auto& streamId : offlineOutputIds) {
1784 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001785 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001786 String8 msg = String8::format("Offline surface with id: %d is not registered",
1787 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001788 ALOGE("%s: %s", __FUNCTION__, msg.string());
1789 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1790 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001791
1792 if (!mStreamInfoMap[streamId].supportsOffline) {
1793 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1794 "offline mode", streamId);
1795 ALOGE("%s: %s", __FUNCTION__, msg.string());
1796 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1797 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001798
Emilian Peev2f5d6012022-01-19 16:16:50 -08001799 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001800 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001801 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001802 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001803 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001804 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001805 if (isCompositeStream) {
1806 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1807 if (compositeIdx == NAME_NOT_FOUND) {
1808 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1809 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1810 "Unknown composite stream");
1811 }
1812
1813 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1814 &offlineStreamIds);
1815 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1816 mCompositeStreamMap.valueAt(compositeIdx));
1817 break;
1818 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001819 }
1820
Emilian Peev4697b642019-11-19 17:11:14 -08001821 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001822 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001823 }
1824 }
1825
1826 sp<CameraOfflineSessionBase> offlineSession;
1827 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1828 if (ret != OK) {
1829 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1830 "Camera %s: Error switching to offline mode: %s (%d)",
1831 mCameraIdStr.string(), strerror(ret), ret);
1832 }
1833
Emilian Peevcc0b7952020-01-07 13:54:47 -08001834 sp<CameraOfflineSessionClient> offlineClient;
1835 if (offlineSession.get() != nullptr) {
1836 offlineClient = new CameraOfflineSessionClient(sCameraService,
1837 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001838 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1839 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001840 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1841 }
1842
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001843 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001844 // A successful offline session switch must reset the current camera client
1845 // and release any resources occupied by previously configured streams.
1846 mStreamMap.clear();
1847 mConfiguredOutputs.clear();
1848 mDeferredStreams.clear();
1849 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001850 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001851 mCompositeStreamMap.clear();
1852 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001853 } else {
Emilian Peev886ac212023-02-07 14:10:24 -08001854 // In case we failed to register the offline client, ensure that it still initialized
1855 // so that all failing requests can return back correctly once the object is released.
1856 offlineClient->initialize(nullptr /*cameraProviderManager*/, String8()/*monitorTags*/);
1857
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001858 switch(ret) {
1859 case BAD_VALUE:
1860 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1861 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1862 case TIMED_OUT:
1863 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1864 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1865 default:
1866 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1867 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1868 strerror(-ret), ret);
1869 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001870 }
1871
1872 *session = offlineClient;
1873
1874 return binder::Status::ok();
1875}
1876
Igor Murashkine7ee7632013-06-11 18:10:18 -07001877status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001878 return BasicClient::dump(fd, args);
1879}
1880
1881status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001882 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001883 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001884 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001885 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001886 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001887
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001888 dprintf(fd, " State:\n");
1889 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001890 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001891 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001892 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001893 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001894 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001895 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001896 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001897 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001898 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001899 mStreamMap.valueAt(i).streamId(),
1900 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001901 }
Zhijun He5d677d12016-05-29 16:52:39 -07001902 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001903 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001904 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001905 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001906 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001907 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001908 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001909 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001910 // TODO: print dynamic/request section from most recent requests
1911 mFrameProcessor->dump(fd, args);
1912
1913 return dumpDevice(fd, args);
1914}
1915
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001916status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1917 sp<CameraDeviceBase> device = mDevice;
1918 if (!device) {
1919 dprintf(out, " Device is detached.");
1920 return OK;
1921 }
1922 device->startWatchingTags(tags);
1923 return OK;
1924}
1925
1926status_t CameraDeviceClient::stopWatchingTags(int out) {
1927 sp<CameraDeviceBase> device = mDevice;
1928 if (!device) {
1929 dprintf(out, " Device is detached.");
1930 return OK;
1931 }
1932 device->stopWatchingTags();
1933 return OK;
1934}
1935
1936status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1937 sp<CameraDeviceBase> device = mDevice;
1938 if (!device) {
1939 return OK;
1940 }
1941 device->dumpWatchedEventsToVector(out);
1942 return OK;
1943}
1944
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001945void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001946 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001947 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001948 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001949
Emilian Peev538c90e2018-12-17 18:03:19 +00001950 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001951 {
1952 // Access to the composite stream map must be synchronized
1953 Mutex::Autolock l(mCompositeLock);
1954 // Composites can have multiple internal streams. Error notifications coming from such
1955 // internal streams may need to remain within camera service.
1956 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1957 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1958 resultExtras);
1959 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001960 }
1961
1962 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001963 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001964 }
1965}
1966
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001967void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1968 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1969
1970 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001971 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001972 }
1973
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001974 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001975 mStreamingRequestId = REQUEST_ID_NONE;
1976}
1977
Shuzhen Wang316781a2020-08-18 18:11:01 -07001978void CameraDeviceClient::notifyIdle(
1979 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1980 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001981 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001982 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001983
1984 if (remoteCb != 0) {
1985 remoteCb->onDeviceIdle();
1986 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08001987 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -07001988 streamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001989}
1990
Jianing Weicb0652e2014-03-12 18:29:36 -07001991void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001992 nsecs_t timestamp) {
1993 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001994 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001995 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001996 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001997 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001998 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001999
Emilian Peev2f5d6012022-01-19 16:16:50 -08002000 // Access to the composite stream map must be synchronized
2001 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002002 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2003 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2004 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002005}
2006
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002007void CameraDeviceClient::notifyPrepared(int streamId) {
2008 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002009 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002010 if (remoteCb != 0) {
2011 remoteCb->onPrepared(streamId);
2012 }
2013}
2014
Shuzhen Wang9d066012016-09-30 11:30:20 -07002015void CameraDeviceClient::notifyRequestQueueEmpty() {
2016 // Thread safe. Don't bother locking.
2017 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2018 if (remoteCb != 0) {
2019 remoteCb->onRequestQueueEmpty();
2020 }
2021}
2022
Igor Murashkine7ee7632013-06-11 18:10:18 -07002023void CameraDeviceClient::detachDevice() {
2024 if (mDevice == 0) return;
2025
Shuzhen Wang316781a2020-08-18 18:11:01 -07002026 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002027 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002028
Emilian Peev7ee731f2022-02-08 14:55:52 -08002029 if (mFrameProcessor.get() != nullptr) {
2030 mFrameProcessor->removeListener(
2031 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2032 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2033 mFrameProcessor->requestExit();
2034 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2035 mFrameProcessor->join();
2036 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2037 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002038
2039 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2040 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002041 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002042 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002043 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2044 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2045 }
2046
Igor Murashkine7ee7632013-06-11 18:10:18 -07002047 if ((code = mDevice->waitUntilDrained()) != OK) {
2048 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2049 code);
2050 }
2051 }
2052
Emilian Peev2f5d6012022-01-19 16:16:50 -08002053 {
2054 Mutex::Autolock l(mCompositeLock);
2055 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2056 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2057 if (ret != OK) {
2058 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2059 strerror(-ret), ret);
2060 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002061 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002062 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002063 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002064
Igor Murashkine7ee7632013-06-11 18:10:18 -07002065 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002066
2067 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2068 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002069}
2070
2071/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002072void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002073 ATRACE_CALL();
2074 ALOGV("%s", __FUNCTION__);
2075
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002076 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002077 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002078 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002079 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2080 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002081 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002082
Emilian Peev2f5d6012022-01-19 16:16:50 -08002083 // Access to the composite stream map must be synchronized
2084 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002085 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2086 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2087 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002088}
2089
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002090binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002091 if (mDisconnected) {
2092 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2093 "The camera device has been disconnected");
2094 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002095 status_t res = checkPid(checkLocation);
2096 return (res == OK) ? binder::Status::ok() :
2097 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2098 "Attempt to use camera from a different process than original client");
2099}
2100
Igor Murashkine7ee7632013-06-11 18:10:18 -07002101// TODO: move to Camera2ClientBase
2102bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2103
Jayant Chowdhary12361932018-08-27 14:46:13 -07002104 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002105 const int selfPid = getpid();
2106 camera_metadata_entry_t entry;
2107
2108 /**
2109 * Mixin default important security values
2110 * - android.led.transmit = defaulted ON
2111 */
2112 CameraMetadata staticInfo = mDevice->info();
2113 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2114 for(size_t i = 0; i < entry.count; ++i) {
2115 uint8_t led = entry.data.u8[i];
2116
2117 switch(led) {
2118 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2119 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2120 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2121 metadata.update(ANDROID_LED_TRANSMIT,
2122 &transmitDefault, 1);
2123 }
2124 break;
2125 }
2126 }
2127 }
2128
2129 // We can do anything!
2130 if (pid == selfPid) {
2131 return true;
2132 }
2133
2134 /**
2135 * Permission check special fields in the request
2136 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2137 */
2138 entry = metadata.find(ANDROID_LED_TRANSMIT);
2139 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2140 String16 permissionString =
2141 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2142 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002143 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002144 ALOGE("Permission Denial: "
2145 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2146 return false;
2147 }
2148 }
2149
2150 return true;
2151}
2152
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002153status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2154 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002155 ALOGV("%s: begin", __FUNCTION__);
2156
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002157 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002158 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002159}
2160
Emilian Peevf4816702020-04-03 15:44:51 -07002161binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2162 camera_request_template_t* tempId /*out*/) {
2163 binder::Status ret = binder::Status::ok();
2164
2165 if (tempId == nullptr) {
2166 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2167 "Camera %s: Invalid template argument", mCameraIdStr.string());
2168 return ret;
2169 }
2170 switch(templateId) {
2171 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2172 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2173 break;
2174 case ICameraDeviceUser::TEMPLATE_RECORD:
2175 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2176 break;
2177 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2178 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2179 break;
2180 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2181 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2182 break;
2183 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2184 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2185 break;
2186 case ICameraDeviceUser::TEMPLATE_MANUAL:
2187 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2188 break;
2189 default:
2190 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2191 "Camera %s: Template ID %d is invalid or not supported",
2192 mCameraIdStr.string(), templateId);
2193 return ret;
2194 }
2195
2196 return ret;
2197}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002198
2199const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2200 if (mDevice->getId() == cameraId) {
2201 return mDevice->info();
2202 }
2203 return mDevice->infoPhysical(cameraId);
2204}
2205
2206bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2207 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002208 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002209}
2210
2211bool CameraDeviceClient::isSensorPixelModeConsistent(
2212 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2213 // First we get the sensorPixelMode from the settings metadata.
2214 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2215 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2216 if (sensorPixelModeEntry.count != 0) {
2217 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2218 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2219 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2220 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2221 __FUNCTION__, sensorPixelMode);
2222 return false;
2223 }
2224 }
2225 // Check whether each stream has max resolution allowed.
2226 bool consistent = true;
2227 for (auto it : streamIdList) {
2228 auto const streamInfoIt = mStreamInfoMap.find(it);
2229 if (streamInfoIt == mStreamInfoMap.end()) {
2230 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2231 return false;
2232 }
2233 consistent =
2234 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2235 streamInfoIt->second.sensorPixelModesUsed.end();
2236 if (!consistent) {
2237 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2238 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2239 ALOGE("sensor pixel mode used list: %i", m);
2240 }
2241 break;
2242 }
2243 }
2244
2245 return consistent;
2246}
2247
Igor Murashkine7ee7632013-06-11 18:10:18 -07002248} // namespace android