blob: 1f0e095d7a48b3cf118fb616aa01888d164f14fc [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 }
171 mDynamicProfileMap.emplace(
172 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
173 standardBitmap);
174 } else {
175 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
176 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
177 }
178 }
179 }
180
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700181 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800182 // Cache physical camera ids corresponding to this device and also the high
183 // resolution sensors in this device + physical camera ids
184 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
185 if (isUltraHighResolutionSensor(mCameraIdStr)) {
186 mHighResolutionSensors.insert(mCameraIdStr.string());
187 }
188 for (auto &physicalId : mPhysicalCameraIds) {
189 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
190 mHighResolutionSensors.insert(physicalId.c_str());
191 }
192 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700193 return OK;
194}
195
196CameraDeviceClient::~CameraDeviceClient() {
197}
198
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800199binder::Status CameraDeviceClient::submitRequest(
200 const hardware::camera2::CaptureRequest& request,
201 bool streaming,
202 /*out*/
203 hardware::camera2::utils::SubmitInfo *submitInfo) {
204 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
205 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700206}
207
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800208binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000209 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000210 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800211 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
212
Emilian Peev2f5d6012022-01-19 16:16:50 -0800213 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800214 // Trying to submit request with surface that wasn't created
215 if (idx == NAME_NOT_FOUND) {
216 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
217 " we have not called createStream on",
218 __FUNCTION__, mCameraIdStr.string());
219 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
220 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000221 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
222 != NAME_NOT_FOUND) {
223 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
224 currentStreamId);
225 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800226 }
227
228 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
229 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800230 outputStreamIds->push_back(streamSurfaceId.streamId());
231 }
232 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
233
234 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
235 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
236 streamSurfaceId.surfaceId());
237
Emilian Peevf873aa52018-01-26 14:58:28 +0000238 if (currentStreamId != nullptr) {
239 *currentStreamId = streamSurfaceId.streamId();
240 }
241
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800242 return binder::Status::ok();
243}
244
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800245static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
246 const Vector<int> &streamIdsForThisRequest) {
247 std::list<int> intersection;
248 for (auto &streamId : streamIdsForThisRequest) {
249 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
250 intersection.emplace_back(streamId);
251 }
252 }
253 return intersection;
254}
255
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800256binder::Status CameraDeviceClient::submitRequestList(
257 const std::vector<hardware::camera2::CaptureRequest>& requests,
258 bool streaming,
259 /*out*/
260 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700261 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700262 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800264 binder::Status res = binder::Status::ok();
265 status_t err;
266 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
267 return res;
268 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700269
270 Mutex::Autolock icl(mBinderSerializationLock);
271
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800272 if (!mDevice.get()) {
273 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
274 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700275
276 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800277 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
278 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800279 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700280 }
281
Emilian Peevaebbe412018-01-15 13:53:24 +0000282 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700283 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800284 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700285 uint32_t loopCounter = 0;
286
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 for (auto&& request: requests) {
288 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700289 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800290 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
291 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800292 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800293 "No input configured for camera %s but request is for reprocessing",
294 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700295 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800296 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
297 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800298 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
299 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000300 } else if (request.mPhysicalCameraSettings.size() > 1) {
301 ALOGE("%s: Camera %s: reprocess requests not supported for "
302 "multiple physical cameras.", __FUNCTION__,
303 mCameraIdStr.string());
304 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
305 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700306 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700307 }
308
Emilian Peevaebbe412018-01-15 13:53:24 +0000309 if (request.mPhysicalCameraSettings.empty()) {
310 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
311 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800312 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000313 "Request doesn't contain any settings");
314 }
315
316 //The first capture settings should always match the logical camera id
317 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
318 if (mDevice->getId() != logicalId) {
319 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
320 mCameraIdStr.string());
321 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
322 "Invalid camera request settings");
323 }
324
Emilian Peevaebbe412018-01-15 13:53:24 +0000325 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800326 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
327 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800328 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
329 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700330 }
331
Jianing Wei90e59c92014-03-12 18:29:36 -0700332 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700333 * Write in the output stream IDs and map from stream ID to surface ID
334 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700335 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700336 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700337 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000338 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800339 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800340 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800341 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800342 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700343
Emilian Peevf873aa52018-01-26 14:58:28 +0000344 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800345 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000346 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800347 if (!res.isOk()) {
348 return res;
349 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000350
351 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
352 if (index >= 0) {
353 String8 requestedPhysicalId(
354 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
355 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800356 dynamicProfileBitmap |=
357 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000358 } else {
359 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
360 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700361 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800362 } else {
363 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
364 int streamId = request.mStreamIdxList.itemAt(i);
365 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700366
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800367 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
368 if (index < 0) {
369 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
370 " we have not called createStream on: stream %d",
371 __FUNCTION__, mCameraIdStr.string(), streamId);
372 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
373 "Request targets Surface that is not part of current capture session");
374 }
375
376 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
377 if ((size_t)surfaceIdx >= gbps.size()) {
378 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
379 " we have not called createStream on: stream %d, surfaceIdx %d",
380 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
381 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
382 "Request targets Surface has invalid surface index");
383 }
384
Emilian Peevf873aa52018-01-26 14:58:28 +0000385 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800386 if (!res.isOk()) {
387 return res;
388 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000389
390 String8 requestedPhysicalId(
391 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
392 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800393 dynamicProfileBitmap |=
394 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
395 }
396 }
397
398 if (dynamicProfileBitmap !=
399 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
400 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
401 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
402 if ((dynamicProfileBitmap & i) == 0) {
403 continue;
404 }
405
406 const auto& it = mDynamicProfileMap.find(i);
407 if (it != mDynamicProfileMap.end()) {
408 if ((it->second == 0) ||
409 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
410 continue;
411 } else {
412 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
413 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800414 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800415 dynamicProfileBitmap);
416 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
417 "Request targets an unsupported dynamic range profile"
418 " combination");
419 }
420 } else {
421 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
422 " references unsupported dynamic range profile 0x%x!",
423 __FUNCTION__, mCameraIdStr.string(), i);
424 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
425 "Request targets 10-bit Surface with unsupported dynamic range"
426 " profile");
427 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700428 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700429 }
430
Emilian Peevf873aa52018-01-26 14:58:28 +0000431 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
432 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000433 if (it.settings.isEmpty()) {
434 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
435 __FUNCTION__, mCameraIdStr.string());
436 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
437 "Request settings are empty");
438 }
439
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800440 // Check whether the physical / logical stream has settings
441 // consistent with the sensor pixel mode(s) it was configured with.
442 // mCameraIdToStreamSet will only have ids that are high resolution
443 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
444 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
445 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
446 outputStreamIds);
447 if (!request.mIsReprocess &&
448 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
449 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
450 "consistent with configured streams. Rejecting request.",
451 __FUNCTION__, it.id.c_str());
452 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
453 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
454 "streams configured");
455 }
456 }
457
Emilian Peevf873aa52018-01-26 14:58:28 +0000458 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700459 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
460 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
461 mSupportedPhysicalRequestKeys.end();
462 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
463 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
464 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000465 if (physicalId != mDevice->getId()) {
466 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
467 it.id);
468 if (found == requestedPhysicalIds.end()) {
469 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
470 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
471 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
472 "Invalid physical camera id");
473 }
Emilian Peev00420d22018-02-05 21:33:13 +0000474
475 if (!mSupportedPhysicalRequestKeys.empty()) {
476 // Filter out any unsupported physical request keys.
477 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
478 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
479 filteredParams.getAndLock());
480 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
481 filteredParams.unlock(meta);
482
483 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
484 camera_metadata_ro_entry entry = it.settings.find(keyIt);
485 if (entry.count > 0) {
486 filteredParams.update(entry);
487 }
488 }
489
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700490 physicalSettingsList.push_back({it.id, filteredParams,
491 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000492 }
493 } else {
494 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000495 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000496 }
497
498 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
499 // Callee logs
500 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
501 "Caller does not have permission to change restricted controls");
502 }
503
Emilian Peevaebbe412018-01-15 13:53:24 +0000504 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
505 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700506
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800507 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000508 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
509 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700510 }
511
Emilian Peevaebbe412018-01-15 13:53:24 +0000512 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
513 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700514 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800515 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
516 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
517 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700518
Emilian Peevaebbe412018-01-15 13:53:24 +0000519 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700520 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800521
522 if (!request.mUserTag.empty()) {
523 mUserTag = request.mUserTag;
524 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700525 }
526 mRequestIdCounter++;
527
528 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700529 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
530 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 if (err != OK) {
532 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800533 "Camera %s: Got error %s (%d) after trying to set streaming request",
534 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800535 ALOGE("%s: %s", __FUNCTION__, msg.string());
536 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
537 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700538 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700539 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700540 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700541 }
542 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700543 err = mDevice->captureList(metadataRequestList, surfaceMapList,
544 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 if (err != OK) {
546 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800547 "Camera %s: Got error %s (%d) after trying to submit capture request",
548 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 ALOGE("%s: %s", __FUNCTION__, msg.string());
550 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
551 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700552 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800553 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700554 }
555
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800556 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 return res;
558}
559
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800560binder::Status CameraDeviceClient::cancelRequest(
561 int requestId,
562 /*out*/
563 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700564 ATRACE_CALL();
565 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
566
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800567 status_t err;
568 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700569
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800570 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700571
572 Mutex::Autolock icl(mBinderSerializationLock);
573
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800574 if (!mDevice.get()) {
575 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
576 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700577
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700578 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700579 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800580 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
581 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800582 ALOGE("%s: %s", __FUNCTION__, msg.string());
583 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700584 }
585
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800586 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700587
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800589 ALOGV("%s: Camera %s: Successfully cleared streaming request",
590 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700591 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800592 } else {
593 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800594 "Camera %s: Error clearing streaming request: %s (%d)",
595 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700596 }
597
598 return res;
599}
600
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800601binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700602 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700603 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700604 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800605 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700606}
607
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100608binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700609 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800610 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700611 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700612 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
613 __FUNCTION__, mInputStream.configured ? 1 : 0,
614 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700615
Zhijun He0effd522016-03-04 10:22:27 -0800616 binder::Status res;
617 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
618
Emilian Peevcc0b7952020-01-07 13:54:47 -0800619 if (offlineStreamIds == nullptr) {
620 String8 msg = String8::format("Invalid offline stream ids");
621 ALOGE("%s: %s", __FUNCTION__, msg.string());
622 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
623 }
624
Zhijun He0effd522016-03-04 10:22:27 -0800625 Mutex::Autolock icl(mBinderSerializationLock);
626
627 if (!mDevice.get()) {
628 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
629 }
630
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700631 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000632 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000633 if (!res.isOk()) {
634 return res;
635 }
636
637 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
638 if (err == BAD_VALUE) {
639 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
640 mCameraIdStr.string());
641 ALOGE("%s: %s", __FUNCTION__, msg.string());
642 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
643 } else if (err != OK) {
644 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
645 mCameraIdStr.string(), strerror(-err), err);
646 ALOGE("%s: %s", __FUNCTION__, msg.string());
647 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000648 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800649 offlineStreamIds->clear();
650 mDevice->getOfflineStreamIds(offlineStreamIds);
651
Emilian Peev2f5d6012022-01-19 16:16:50 -0800652 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000653 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
654 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800655 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000656 String8 msg = String8::format("Camera %s: Error configuring composite "
657 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
658 ALOGE("%s: %s", __FUNCTION__, msg.string());
659 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
660 break;
661 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800662
663 // Composite streams can only support offline mode in case all individual internal
664 // streams are also supported.
665 std::vector<int> internalStreams;
666 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800667 offlineStreamIds->erase(
668 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800669 [&internalStreams] (int streamId) {
670 auto it = std::find(internalStreams.begin(), internalStreams.end(),
671 streamId);
672 if (it != internalStreams.end()) {
673 internalStreams.erase(it);
674 return true;
675 }
676
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800677 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800678 if (internalStreams.empty()) {
679 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
680 }
681 }
682
683 for (const auto& offlineStreamId : *offlineStreamIds) {
684 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000685 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700686
687 nsecs_t configureEnd = systemTime();
688 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
689 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
690 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000691 }
692
693 return res;
694}
695
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800696binder::Status CameraDeviceClient::isSessionConfigurationSupported(
697 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800698
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800699 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800700 binder::Status res;
701 status_t ret = OK;
702 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
703
704 Mutex::Autolock icl(mBinderSerializationLock);
705
706 if (!mDevice.get()) {
707 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
708 }
709
710 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700711 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000712 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800713 if (!res.isOk()) {
714 return res;
715 }
716
717 if (status == nullptr) {
718 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
719 ALOGE("%s: %s", __FUNCTION__, msg.string());
720 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
721 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700722
Emilian Peev35ae8262018-11-08 13:11:32 +0000723 *status = false;
724 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700725 sessionConfiguration, mOverrideForPerfClass, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000726 switch (ret) {
727 case OK:
728 // Expected, do nothing.
729 break;
730 case INVALID_OPERATION: {
731 String8 msg = String8::format(
732 "Camera %s: Session configuration query not supported!",
733 mCameraIdStr.string());
734 ALOGD("%s: %s", __FUNCTION__, msg.string());
735 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
736 }
737
738 break;
739 default: {
740 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
741 strerror(-ret), ret);
742 ALOGE("%s: %s", __FUNCTION__, msg.string());
743 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
744 msg.string());
745 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800746 }
747
748 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700749}
750
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800751binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700752 ATRACE_CALL();
753 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
754
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800755 binder::Status res;
756 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700757
758 Mutex::Autolock icl(mBinderSerializationLock);
759
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800760 if (!mDevice.get()) {
761 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
762 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700763
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700764 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700765 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700766 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000767 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700768
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700769 if (mInputStream.configured && mInputStream.id == streamId) {
770 isInput = true;
771 } else {
772 // Guard against trying to delete non-created streams
773 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774 if (streamId == mStreamMap.valueAt(i).streamId()) {
775 surfaces.push_back(mStreamMap.keyAt(i));
776 }
777 }
778
779 // See if this stream is one of the deferred streams.
780 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
781 if (streamId == mDeferredStreams[i]) {
782 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700783 break;
784 }
785 }
786
Emilian Peev2f5d6012022-01-19 16:16:50 -0800787 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000788 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
789 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
790 compositeIndex = i;
791 break;
792 }
793 }
794
Shuzhen Wang0129d522016-10-30 22:43:41 -0700795 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
796 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
797 " stream created yet", mCameraIdStr.string(), streamId);
798 ALOGW("%s: %s", __FUNCTION__, msg.string());
799 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700800 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700801 }
802
803 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800804 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700805
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800806 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800807 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
808 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800809 ALOGE("%s: %s", __FUNCTION__, msg.string());
810 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
811 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700812 if (isInput) {
813 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700814 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700815 for (auto& surface : surfaces) {
816 mStreamMap.removeItem(surface);
817 }
818
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800819 mConfiguredOutputs.removeItem(streamId);
820
Shuzhen Wang0129d522016-10-30 22:43:41 -0700821 if (dIndex != NAME_NOT_FOUND) {
822 mDeferredStreams.removeItemsAt(dIndex);
823 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000824
825 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800826 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000827 status_t ret;
828 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
829 != OK) {
830 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
831 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
832 streamId);
833 ALOGE("%s: %s", __FUNCTION__, msg.string());
834 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
835 }
836 mCompositeStreamMap.removeItemsAt(compositeIndex);
837 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800838 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
839 auto &streamSet = mapIt.second;
840 if (streamSet.find(streamId) != streamSet.end()) {
841 streamSet.erase(streamId);
842 break;
843 }
844 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700845 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700846 }
847
848 return res;
849}
850
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800851binder::Status CameraDeviceClient::createStream(
852 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
853 /*out*/
854 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700855 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700856
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800857 binder::Status res;
858 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700859
860 Mutex::Autolock icl(mBinderSerializationLock);
861
Shuzhen Wang0129d522016-10-30 22:43:41 -0700862 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
863 outputConfiguration.getGraphicBufferProducers();
864 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800865 bool deferredConsumer = outputConfiguration.isDeferred();
866 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800867 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800868 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800869 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800870 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800871 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800872 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800873 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700874
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700875 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000876 outputConfiguration.getSurfaceType());
877 if (!res.isOk()) {
878 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700879 }
Zhijun He5d677d12016-05-29 16:52:39 -0700880
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800881 if (!mDevice.get()) {
882 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
883 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700884 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800885 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000886 if (!res.isOk()) {
887 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800888 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000889
Shuzhen Wang0129d522016-10-30 22:43:41 -0700890 std::vector<sp<Surface>> surfaces;
891 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700892 status_t err;
893
894 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700895 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800896 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700897 }
898
Shuzhen Wang758c2152017-01-10 18:26:18 -0800899 OutputStreamInfo streamInfo;
900 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800901 const std::vector<int32_t> &sensorPixelModesUsed =
902 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700903 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700904 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700905 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800906 ssize_t index = mStreamMap.indexOfKey(binder);
907 if (index != NAME_NOT_FOUND) {
908 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
909 "(ID %zd)", mCameraIdStr.string(), index);
910 ALOGW("%s: %s", __FUNCTION__, msg.string());
911 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700912 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700913
Shuzhen Wang758c2152017-01-10 18:26:18 -0800914 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700915 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800916 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800917 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800918 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800919
920 if (!res.isOk())
921 return res;
922
923 if (!isStreamInfoValid) {
924 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700925 }
926
Shuzhen Wang758c2152017-01-10 18:26:18 -0800927 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700928 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800929 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700930
931 // If mOverrideForPerfClass is true, do not fail createStream() for small
932 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
933 // closest possible supported size.
934
Zhijun He125684a2015-12-26 15:07:30 -0800935 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100936 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800937 bool isDepthCompositeStream =
938 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800939 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
940 if (isDepthCompositeStream || isHeicCompisiteStream) {
941 sp<CompositeStream> compositeStream;
942 if (isDepthCompositeStream) {
943 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
944 } else {
945 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
946 }
947
Emilian Peev538c90e2018-12-17 18:03:19 +0000948 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
949 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700950 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800951 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
952 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000953 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800954 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000955 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
956 compositeStream);
957 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800958 } else {
959 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
960 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700961 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800962 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800963 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800964 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800965 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000966 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700967
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800968 if (err != OK) {
969 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800970 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800971 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
972 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800973 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700974 int i = 0;
975 for (auto& binder : binders) {
976 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
977 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100978 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
979 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700980 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800981
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800982 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800983 mStreamInfoMap[streamId] = streamInfo;
984
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800985 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700986 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800987 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
988 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700989
Zhijun He5d677d12016-05-29 16:52:39 -0700990 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800991 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700992
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800993 // Fill in mHighResolutionCameraIdToStreamIdSet map
994 const String8 &cameraIdUsed =
995 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
996 const char *cameraIdUsedCStr = cameraIdUsed.string();
997 // Only needed for high resolution sensors
998 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
999 mHighResolutionSensors.end()) {
1000 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1001 }
1002
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001003 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001004 }
1005
1006 return res;
1007}
1008
Zhijun He5d677d12016-05-29 16:52:39 -07001009binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1010 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001011 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001012 /*out*/
1013 int* newStreamId) {
1014 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001015 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001016 android_dataspace dataSpace;
1017 status_t err;
1018 binder::Status res;
1019
1020 if (!mDevice.get()) {
1021 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1022 }
1023
1024 // Infer the surface info for deferred surface stream creation.
1025 width = outputConfiguration.getWidth();
1026 height = outputConfiguration.getHeight();
1027 surfaceType = outputConfiguration.getSurfaceType();
1028 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1029 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1030 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1031 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1032 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1033 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1034 }
1035 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001036 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001037 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001038 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001039 const String8 &cameraIdUsed =
1040 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1041 // Here, we override sensor pixel modes
1042 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1043 const std::vector<int32_t> &sensorPixelModesUsed =
1044 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001045 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001046 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1047 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1048 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1049 "sensor pixel modes used not valid for deferred stream");
1050 }
1051
Shuzhen Wang0129d522016-10-30 22:43:41 -07001052 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1053 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001054 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001055 &streamId, physicalCameraId,
1056 overriddenSensorPixelModesUsed,
1057 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001058 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001059 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001060 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001061 outputConfiguration.getStreamUseCase(),
1062 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001063
1064 if (err != OK) {
1065 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001066 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1067 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001068 } else {
1069 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1070 // a separate list to track. Once the deferred surface is set, this id will be
1071 // relocated to mStreamMap.
1072 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001073 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001074 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001075 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001076 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001077 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001078 outputConfiguration.getTimestampBase(),
1079 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001080
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001081 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001082 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001083 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001084
1085 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001086 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001087
1088 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001089 // Fill in mHighResolutionCameraIdToStreamIdSet
1090 const char *cameraIdUsedCStr = cameraIdUsed.string();
1091 // Only needed for high resolution sensors
1092 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1093 mHighResolutionSensors.end()) {
1094 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1095 }
Zhijun He5d677d12016-05-29 16:52:39 -07001096 }
1097 return res;
1098}
1099
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001100binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001101 int32_t transform = 0;
1102 status_t err;
1103 binder::Status res;
1104
1105 if (!mDevice.get()) {
1106 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1107 }
1108
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001109 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001110 if (err != OK) {
1111 // Error logged by getRotationTransformLocked.
1112 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1113 "Unable to calculate rotation transform for new stream");
1114 }
1115
1116 err = mDevice->setStreamTransform(streamId, transform);
1117 if (err != OK) {
1118 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1119 streamId);
1120 ALOGE("%s: %s", __FUNCTION__, msg.string());
1121 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1122 }
1123
1124 return res;
1125}
Ruben Brunkbba75572014-11-20 17:29:50 -08001126
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001127binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001128 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001129 /*out*/
1130 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001131
1132 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001133 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1134 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001135
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001136 binder::Status res;
1137 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001138
1139 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001140
1141 if (!mDevice.get()) {
1142 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1143 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001144
1145 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001146 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001147 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001148 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1149 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001150 }
1151
1152 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001153 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001154 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001155 mInputStream.configured = true;
1156 mInputStream.width = width;
1157 mInputStream.height = height;
1158 mInputStream.format = format;
1159 mInputStream.id = streamId;
1160
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001161 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1162 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001163
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001164 *newStreamId = streamId;
1165 } else {
1166 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001167 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001168 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001169 }
1170
1171 return res;
1172}
1173
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001174binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001175
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001176 binder::Status res;
1177 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1178
1179 if (inputSurface == NULL) {
1180 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001181 }
1182
1183 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184 if (!mDevice.get()) {
1185 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1186 }
1187 sp<IGraphicBufferProducer> producer;
1188 status_t err = mDevice->getInputBufferProducer(&producer);
1189 if (err != OK) {
1190 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001191 "Camera %s: Error getting input Surface: %s (%d)",
1192 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001193 } else {
1194 inputSurface->name = String16("CameraInput");
1195 inputSurface->graphicBufferProducer = producer;
1196 }
1197 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001198}
1199
Emilian Peev40ead602017-09-26 15:46:36 +01001200binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1201 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1202 ATRACE_CALL();
1203
1204 binder::Status res;
1205 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1206
1207 Mutex::Autolock icl(mBinderSerializationLock);
1208
1209 if (!mDevice.get()) {
1210 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1211 }
1212
1213 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1214 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001215 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1216
Emilian Peev40ead602017-09-26 15:46:36 +01001217 auto producerCount = bufferProducers.size();
1218 if (producerCount == 0) {
1219 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1220 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1221 "bufferProducers must not be empty");
1222 }
1223
1224 // The first output is the one associated with the output configuration.
1225 // It should always be present, valid and the corresponding stream id should match.
1226 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1227 ssize_t index = mStreamMap.indexOfKey(binder);
1228 if (index == NAME_NOT_FOUND) {
1229 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1230 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1231 "OutputConfiguration is invalid");
1232 }
1233 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1234 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1235 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1236 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1237 "Stream id is invalid");
1238 }
1239
1240 std::vector<size_t> removedSurfaceIds;
1241 std::vector<sp<IBinder>> removedOutputs;
1242 std::vector<sp<Surface>> newOutputs;
1243 std::vector<OutputStreamInfo> streamInfos;
1244 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1245 for (auto &it : bufferProducers) {
1246 newOutputsMap.add(IInterface::asBinder(it), it);
1247 }
1248
1249 for (size_t i = 0; i < mStreamMap.size(); i++) {
1250 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1251 if (idx == NAME_NOT_FOUND) {
1252 if (mStreamMap[i].streamId() == streamId) {
1253 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1254 removedOutputs.push_back(mStreamMap.keyAt(i));
1255 }
1256 } else {
1257 if (mStreamMap[i].streamId() != streamId) {
1258 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1259 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1260 "Target Surface is invalid");
1261 }
1262 newOutputsMap.removeItemsAt(idx);
1263 }
1264 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001265 const std::vector<int32_t> &sensorPixelModesUsed =
1266 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001267 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001268 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001269 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001270 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001271
Emilian Peev40ead602017-09-26 15:46:36 +01001272 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1273 OutputStreamInfo outInfo;
1274 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001275 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001276 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001277 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001278 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001279 if (!res.isOk())
1280 return res;
1281
Emilian Peev40ead602017-09-26 15:46:36 +01001282 streamInfos.push_back(outInfo);
1283 newOutputs.push_back(surface);
1284 }
1285
1286 //Trivial case no changes required
1287 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1288 return binder::Status::ok();
1289 }
1290
1291 KeyedVector<sp<Surface>, size_t> outputMap;
1292 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1293 &outputMap);
1294 if (ret != OK) {
1295 switch (ret) {
1296 case NAME_NOT_FOUND:
1297 case BAD_VALUE:
1298 case -EBUSY:
1299 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1300 "Camera %s: Error updating stream: %s (%d)",
1301 mCameraIdStr.string(), strerror(ret), ret);
1302 break;
1303 default:
1304 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1305 "Camera %s: Error updating stream: %s (%d)",
1306 mCameraIdStr.string(), strerror(ret), ret);
1307 break;
1308 }
1309 } else {
1310 for (const auto &it : removedOutputs) {
1311 mStreamMap.removeItem(it);
1312 }
1313
1314 for (size_t i = 0; i < outputMap.size(); i++) {
1315 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1316 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1317 }
1318
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001319 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1320
Emilian Peev40ead602017-09-26 15:46:36 +01001321 ALOGV("%s: Camera %s: Successful stream ID %d update",
1322 __FUNCTION__, mCameraIdStr.string(), streamId);
1323 }
1324
1325 return res;
1326}
1327
Igor Murashkine7ee7632013-06-11 18:10:18 -07001328// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001329binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1330 /*out*/
1331 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001332{
1333 ATRACE_CALL();
1334 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1335
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001336 binder::Status res;
1337 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001338
1339 Mutex::Autolock icl(mBinderSerializationLock);
1340
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001341 if (!mDevice.get()) {
1342 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1343 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001344
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001345 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001346 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1347 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1348
1349 CameraMetadata metadata;
1350 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001351 request != NULL) {
1352
1353 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001354 } else if (err == BAD_VALUE) {
1355 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001356 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1357 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001358
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001359 } else {
1360 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001361 "Camera %s: Error creating default request for template %d: %s (%d)",
1362 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001363 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001364 return res;
1365}
1366
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001367binder::Status CameraDeviceClient::getCameraInfo(
1368 /*out*/
1369 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001370{
1371 ATRACE_CALL();
1372 ALOGV("%s", __FUNCTION__);
1373
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001377
1378 Mutex::Autolock icl(mBinderSerializationLock);
1379
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380 if (!mDevice.get()) {
1381 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1382 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001383
Igor Murashkin099b4572013-07-12 17:52:16 -07001384 if (info != NULL) {
1385 *info = mDevice->info(); // static camera metadata
1386 // TODO: merge with device-specific camera metadata
1387 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001388
1389 return res;
1390}
1391
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001392binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001393{
1394 ATRACE_CALL();
1395 ALOGV("%s", __FUNCTION__);
1396
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001397 binder::Status res;
1398 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001399
1400 Mutex::Autolock icl(mBinderSerializationLock);
1401
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001402 if (!mDevice.get()) {
1403 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1404 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001405
1406 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001407 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001408 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001409 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001410 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1411 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 ALOGE("%s: %s", __FUNCTION__, msg.string());
1413 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001414 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001415 status_t err = mDevice->waitUntilDrained();
1416 if (err != OK) {
1417 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001418 "Camera %s: Error waiting to drain: %s (%d)",
1419 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001420 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001421 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001422 return res;
1423}
1424
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001425binder::Status CameraDeviceClient::flush(
1426 /*out*/
1427 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001428 ATRACE_CALL();
1429 ALOGV("%s", __FUNCTION__);
1430
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001431 binder::Status res;
1432 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001433
1434 Mutex::Autolock icl(mBinderSerializationLock);
1435
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436 if (!mDevice.get()) {
1437 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1438 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001439
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001440 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001441 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 status_t err = mDevice->flush(lastFrameNumber);
1443 if (err != OK) {
1444 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001445 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001446 }
1447 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001448}
1449
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001450binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001451 ATRACE_CALL();
1452 ALOGV("%s", __FUNCTION__);
1453
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001454 binder::Status res;
1455 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001456
1457 Mutex::Autolock icl(mBinderSerializationLock);
1458
1459 // Guard against trying to prepare non-created streams
1460 ssize_t index = NAME_NOT_FOUND;
1461 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001462 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001463 index = i;
1464 break;
1465 }
1466 }
1467
1468 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001469 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1470 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001471 ALOGW("%s: %s", __FUNCTION__, msg.string());
1472 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001473 }
1474
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001475 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1476 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001477 status_t err = mDevice->prepare(streamId);
1478 if (err == BAD_VALUE) {
1479 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001480 "Camera %s: Stream %d has already been used, and cannot be prepared",
1481 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001482 } else if (err != OK) {
1483 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001484 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001485 strerror(-err), err);
1486 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001487 return res;
1488}
1489
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001490binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001491 ATRACE_CALL();
1492 ALOGV("%s", __FUNCTION__);
1493
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001494 binder::Status res;
1495 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001496
1497 Mutex::Autolock icl(mBinderSerializationLock);
1498
1499 // Guard against trying to prepare non-created streams
1500 ssize_t index = NAME_NOT_FOUND;
1501 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001502 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001503 index = i;
1504 break;
1505 }
1506 }
1507
1508 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001509 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1510 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001511 ALOGW("%s: %s", __FUNCTION__, msg.string());
1512 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001513 }
1514
1515 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001516 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1517 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001518 ALOGE("%s: %s", __FUNCTION__, msg.string());
1519 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001520 }
1521
1522 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1523 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001524 status_t err = mDevice->prepare(maxCount, streamId);
1525 if (err == BAD_VALUE) {
1526 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001527 "Camera %s: Stream %d has already been used, and cannot be prepared",
1528 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001529 } else if (err != OK) {
1530 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001531 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001532 strerror(-err), err);
1533 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001534
1535 return res;
1536}
1537
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001538binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001539 ATRACE_CALL();
1540 ALOGV("%s", __FUNCTION__);
1541
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001542 binder::Status res;
1543 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001544
1545 Mutex::Autolock icl(mBinderSerializationLock);
1546
1547 // Guard against trying to prepare non-created streams
1548 ssize_t index = NAME_NOT_FOUND;
1549 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001550 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001551 index = i;
1552 break;
1553 }
1554 }
1555
1556 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001557 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1558 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001559 ALOGW("%s: %s", __FUNCTION__, msg.string());
1560 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001561 }
1562
1563 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1564 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001565 status_t err = mDevice->tearDown(streamId);
1566 if (err == BAD_VALUE) {
1567 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001568 "Camera %s: Stream %d is still in use, cannot be torn down",
1569 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001570 } else if (err != OK) {
1571 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001572 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001573 strerror(-err), err);
1574 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001575
1576 return res;
1577}
1578
Shuzhen Wang758c2152017-01-10 18:26:18 -08001579binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001580 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1581 ATRACE_CALL();
1582
1583 binder::Status res;
1584 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1585
1586 Mutex::Autolock icl(mBinderSerializationLock);
1587
Shuzhen Wang0129d522016-10-30 22:43:41 -07001588 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1589 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001590 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001591
Shuzhen Wang0129d522016-10-30 22:43:41 -07001592 if (bufferProducers.size() == 0) {
1593 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001594 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1595 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001596
Shuzhen Wang758c2152017-01-10 18:26:18 -08001597 // streamId should be in mStreamMap if this stream already has a surface attached
1598 // to it. Otherwise, it should be in mDeferredStreams.
1599 bool streamIdConfigured = false;
1600 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1601 for (size_t i = 0; i < mStreamMap.size(); i++) {
1602 if (mStreamMap.valueAt(i).streamId() == streamId) {
1603 streamIdConfigured = true;
1604 break;
1605 }
Zhijun He5d677d12016-05-29 16:52:39 -07001606 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001607 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1608 if (streamId == mDeferredStreams[i]) {
1609 deferredStreamIndex = i;
1610 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001611 }
1612
Shuzhen Wang758c2152017-01-10 18:26:18 -08001613 }
1614 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1615 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1616 "(ID %d)", mCameraIdStr.string(), streamId);
1617 ALOGW("%s: %s", __FUNCTION__, msg.string());
1618 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001619 }
1620
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001621 if (mStreamInfoMap[streamId].finalized) {
1622 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1623 " on stream ID %d", mCameraIdStr.string(), streamId);
1624 ALOGW("%s: %s", __FUNCTION__, msg.string());
1625 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1626 }
1627
Zhijun He5d677d12016-05-29 16:52:39 -07001628 if (!mDevice.get()) {
1629 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1630 }
1631
Shuzhen Wang758c2152017-01-10 18:26:18 -08001632 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001633 const std::vector<int32_t> &sensorPixelModesUsed =
1634 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001635 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001636 int64_t streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001637 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001638 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001639 for (auto& bufferProducer : bufferProducers) {
1640 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001641 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1642 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001643 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001644 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001645 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001646 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001647
1648 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001649 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001650 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001651 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001652 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001653
1654 if (!res.isOk())
1655 return res;
1656
1657 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001658 }
1659
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001660 // Gracefully handle case where finalizeOutputConfigurations is called
1661 // without any new surface.
1662 if (consumerSurfaces.size() == 0) {
1663 mStreamInfoMap[streamId].finalized = true;
1664 return res;
1665 }
1666
Zhijun He5d677d12016-05-29 16:52:39 -07001667 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001668 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001669 std::vector<int> consumerSurfaceIds;
1670 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001671 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001672 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1673 sp<IBinder> binder = IInterface::asBinder(
1674 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001675 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001676 binder.get(), streamId, consumerSurfaceIds[i]);
1677 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1678 }
1679 if (deferredStreamIndex != NAME_NOT_FOUND) {
1680 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001681 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001682 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001683 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001684 } else if (err == NO_INIT) {
1685 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001686 "Camera %s: Deferred surface is invalid: %s (%d)",
1687 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001688 } else {
1689 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001690 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1691 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001692 }
1693
1694 return res;
1695}
1696
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001697binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001698 ATRACE_CALL();
1699 binder::Status res;
1700 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1701
1702 if (!isValidAudioRestriction(mode)) {
1703 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1704 mCameraIdStr.string(), mode);
1705 ALOGW("%s: %s", __FUNCTION__, msg.string());
1706 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1707 }
1708
1709 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001710 BasicClient::setAudioRestriction(mode);
1711 return binder::Status::ok();
1712}
1713
1714binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1715 ATRACE_CALL();
1716 binder::Status res;
1717 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1718 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001719 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001720 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001721 }
1722 return binder::Status::ok();
1723}
1724
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001725status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1726 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1727
1728 return mDevice->setRotateAndCropAutoBehavior(
1729 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1730}
1731
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001732bool CameraDeviceClient::supportsCameraMute() {
1733 return mDevice->supportsCameraMute();
1734}
1735
1736status_t CameraDeviceClient::setCameraMute(bool enabled) {
1737 return mDevice->setCameraMute(enabled);
1738}
1739
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001740binder::Status CameraDeviceClient::switchToOffline(
1741 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001742 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001743 /*out*/
1744 sp<hardware::camera2::ICameraOfflineSession>* session) {
1745 ATRACE_CALL();
1746
1747 binder::Status res;
1748 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1749
1750 Mutex::Autolock icl(mBinderSerializationLock);
1751
1752 if (!mDevice.get()) {
1753 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1754 }
1755
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001756 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001757 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001758 ALOGE("%s: %s", __FUNCTION__, msg.string());
1759 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1760 }
1761
1762 if (session == nullptr) {
1763 String8 msg = String8::format("Invalid offline session");
1764 ALOGE("%s: %s", __FUNCTION__, msg.string());
1765 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1766 }
1767
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001768 std::vector<int32_t> offlineStreamIds;
1769 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001770 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001771 for (const auto& streamId : offlineOutputIds) {
1772 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001773 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001774 String8 msg = String8::format("Offline surface with id: %d is not registered",
1775 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001776 ALOGE("%s: %s", __FUNCTION__, msg.string());
1777 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1778 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001779
1780 if (!mStreamInfoMap[streamId].supportsOffline) {
1781 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1782 "offline mode", streamId);
1783 ALOGE("%s: %s", __FUNCTION__, msg.string());
1784 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1785 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001786
Emilian Peev2f5d6012022-01-19 16:16:50 -08001787 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001788 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001789 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001790 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001791 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001792 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001793 if (isCompositeStream) {
1794 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1795 if (compositeIdx == NAME_NOT_FOUND) {
1796 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1797 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1798 "Unknown composite stream");
1799 }
1800
1801 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1802 &offlineStreamIds);
1803 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1804 mCompositeStreamMap.valueAt(compositeIdx));
1805 break;
1806 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001807 }
1808
Emilian Peev4697b642019-11-19 17:11:14 -08001809 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001810 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001811 }
1812 }
1813
1814 sp<CameraOfflineSessionBase> offlineSession;
1815 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1816 if (ret != OK) {
1817 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1818 "Camera %s: Error switching to offline mode: %s (%d)",
1819 mCameraIdStr.string(), strerror(ret), ret);
1820 }
1821
Emilian Peevcc0b7952020-01-07 13:54:47 -08001822 sp<CameraOfflineSessionClient> offlineClient;
1823 if (offlineSession.get() != nullptr) {
1824 offlineClient = new CameraOfflineSessionClient(sCameraService,
1825 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001826 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1827 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001828 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1829 }
1830
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001831 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001832 // A successful offline session switch must reset the current camera client
1833 // and release any resources occupied by previously configured streams.
1834 mStreamMap.clear();
1835 mConfiguredOutputs.clear();
1836 mDeferredStreams.clear();
1837 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001838 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001839 mCompositeStreamMap.clear();
1840 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001841 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001842 switch(ret) {
1843 case BAD_VALUE:
1844 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1845 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1846 case TIMED_OUT:
1847 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1848 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1849 default:
1850 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1851 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1852 strerror(-ret), ret);
1853 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001854 }
1855
1856 *session = offlineClient;
1857
1858 return binder::Status::ok();
1859}
1860
Igor Murashkine7ee7632013-06-11 18:10:18 -07001861status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001862 return BasicClient::dump(fd, args);
1863}
1864
1865status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001866 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001867 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001868 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001869 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001870 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001871
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001872 dprintf(fd, " State:\n");
1873 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001874 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001875 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001876 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001877 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001878 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001879 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001880 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001881 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001882 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001883 mStreamMap.valueAt(i).streamId(),
1884 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001885 }
Zhijun He5d677d12016-05-29 16:52:39 -07001886 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001887 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001888 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001889 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001890 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001891 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001892 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001893 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001894 // TODO: print dynamic/request section from most recent requests
1895 mFrameProcessor->dump(fd, args);
1896
1897 return dumpDevice(fd, args);
1898}
1899
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001900status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1901 sp<CameraDeviceBase> device = mDevice;
1902 if (!device) {
1903 dprintf(out, " Device is detached.");
1904 return OK;
1905 }
1906 device->startWatchingTags(tags);
1907 return OK;
1908}
1909
1910status_t CameraDeviceClient::stopWatchingTags(int out) {
1911 sp<CameraDeviceBase> device = mDevice;
1912 if (!device) {
1913 dprintf(out, " Device is detached.");
1914 return OK;
1915 }
1916 device->stopWatchingTags();
1917 return OK;
1918}
1919
1920status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1921 sp<CameraDeviceBase> device = mDevice;
1922 if (!device) {
1923 return OK;
1924 }
1925 device->dumpWatchedEventsToVector(out);
1926 return OK;
1927}
1928
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001929void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001930 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001931 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001932 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001933
Emilian Peev538c90e2018-12-17 18:03:19 +00001934 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001935 {
1936 // Access to the composite stream map must be synchronized
1937 Mutex::Autolock l(mCompositeLock);
1938 // Composites can have multiple internal streams. Error notifications coming from such
1939 // internal streams may need to remain within camera service.
1940 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1941 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1942 resultExtras);
1943 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001944 }
1945
1946 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001947 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001948 }
1949}
1950
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001951void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1952 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1953
1954 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001955 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001956 }
1957
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001958 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001959 mStreamingRequestId = REQUEST_ID_NONE;
1960}
1961
Shuzhen Wang316781a2020-08-18 18:11:01 -07001962void CameraDeviceClient::notifyIdle(
1963 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1964 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001965 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001966 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001967
1968 if (remoteCb != 0) {
1969 remoteCb->onDeviceIdle();
1970 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08001971 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
1972 streamStats, mUserTag);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001973}
1974
Jianing Weicb0652e2014-03-12 18:29:36 -07001975void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001976 nsecs_t timestamp) {
1977 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001978 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001979 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001980 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001981 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001982 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001983
Emilian Peev2f5d6012022-01-19 16:16:50 -08001984 // Access to the composite stream map must be synchronized
1985 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001986 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1987 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1988 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001989}
1990
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001991void CameraDeviceClient::notifyPrepared(int streamId) {
1992 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001993 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001994 if (remoteCb != 0) {
1995 remoteCb->onPrepared(streamId);
1996 }
1997}
1998
Shuzhen Wang9d066012016-09-30 11:30:20 -07001999void CameraDeviceClient::notifyRequestQueueEmpty() {
2000 // Thread safe. Don't bother locking.
2001 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2002 if (remoteCb != 0) {
2003 remoteCb->onRequestQueueEmpty();
2004 }
2005}
2006
Igor Murashkine7ee7632013-06-11 18:10:18 -07002007void CameraDeviceClient::detachDevice() {
2008 if (mDevice == 0) return;
2009
Shuzhen Wang316781a2020-08-18 18:11:01 -07002010 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002011 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002012
Emilian Peev7ee731f2022-02-08 14:55:52 -08002013 if (mFrameProcessor.get() != nullptr) {
2014 mFrameProcessor->removeListener(
2015 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2016 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2017 mFrameProcessor->requestExit();
2018 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2019 mFrameProcessor->join();
2020 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2021 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002022
2023 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2024 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002025 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002026 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002027 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2028 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2029 }
2030
Igor Murashkine7ee7632013-06-11 18:10:18 -07002031 if ((code = mDevice->waitUntilDrained()) != OK) {
2032 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2033 code);
2034 }
2035 }
2036
Emilian Peev2f5d6012022-01-19 16:16:50 -08002037 {
2038 Mutex::Autolock l(mCompositeLock);
2039 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2040 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2041 if (ret != OK) {
2042 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2043 strerror(-ret), ret);
2044 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002045 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002046 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002047 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002048
Igor Murashkine7ee7632013-06-11 18:10:18 -07002049 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002050
2051 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2052 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002053}
2054
2055/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002056void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002057 ATRACE_CALL();
2058 ALOGV("%s", __FUNCTION__);
2059
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002060 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002061 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002062 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002063 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2064 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002065 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002066
Emilian Peev2f5d6012022-01-19 16:16:50 -08002067 // Access to the composite stream map must be synchronized
2068 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002069 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2070 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2071 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002072}
2073
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002074binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002075 if (mDisconnected) {
2076 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2077 "The camera device has been disconnected");
2078 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002079 status_t res = checkPid(checkLocation);
2080 return (res == OK) ? binder::Status::ok() :
2081 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2082 "Attempt to use camera from a different process than original client");
2083}
2084
Igor Murashkine7ee7632013-06-11 18:10:18 -07002085// TODO: move to Camera2ClientBase
2086bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2087
Jayant Chowdhary12361932018-08-27 14:46:13 -07002088 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002089 const int selfPid = getpid();
2090 camera_metadata_entry_t entry;
2091
2092 /**
2093 * Mixin default important security values
2094 * - android.led.transmit = defaulted ON
2095 */
2096 CameraMetadata staticInfo = mDevice->info();
2097 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2098 for(size_t i = 0; i < entry.count; ++i) {
2099 uint8_t led = entry.data.u8[i];
2100
2101 switch(led) {
2102 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2103 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2104 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2105 metadata.update(ANDROID_LED_TRANSMIT,
2106 &transmitDefault, 1);
2107 }
2108 break;
2109 }
2110 }
2111 }
2112
2113 // We can do anything!
2114 if (pid == selfPid) {
2115 return true;
2116 }
2117
2118 /**
2119 * Permission check special fields in the request
2120 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2121 */
2122 entry = metadata.find(ANDROID_LED_TRANSMIT);
2123 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2124 String16 permissionString =
2125 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2126 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002127 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002128 ALOGE("Permission Denial: "
2129 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2130 return false;
2131 }
2132 }
2133
2134 return true;
2135}
2136
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002137status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2138 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002139 ALOGV("%s: begin", __FUNCTION__);
2140
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002141 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002142 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002143}
2144
Emilian Peevf4816702020-04-03 15:44:51 -07002145binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2146 camera_request_template_t* tempId /*out*/) {
2147 binder::Status ret = binder::Status::ok();
2148
2149 if (tempId == nullptr) {
2150 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2151 "Camera %s: Invalid template argument", mCameraIdStr.string());
2152 return ret;
2153 }
2154 switch(templateId) {
2155 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2156 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2157 break;
2158 case ICameraDeviceUser::TEMPLATE_RECORD:
2159 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2160 break;
2161 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2162 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2163 break;
2164 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2165 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2166 break;
2167 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2168 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2169 break;
2170 case ICameraDeviceUser::TEMPLATE_MANUAL:
2171 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2172 break;
2173 default:
2174 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2175 "Camera %s: Template ID %d is invalid or not supported",
2176 mCameraIdStr.string(), templateId);
2177 return ret;
2178 }
2179
2180 return ret;
2181}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002182
2183const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2184 if (mDevice->getId() == cameraId) {
2185 return mDevice->info();
2186 }
2187 return mDevice->infoPhysical(cameraId);
2188}
2189
2190bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2191 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002192 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002193}
2194
2195bool CameraDeviceClient::isSensorPixelModeConsistent(
2196 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2197 // First we get the sensorPixelMode from the settings metadata.
2198 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2199 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2200 if (sensorPixelModeEntry.count != 0) {
2201 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2202 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2203 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2204 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2205 __FUNCTION__, sensorPixelMode);
2206 return false;
2207 }
2208 }
2209 // Check whether each stream has max resolution allowed.
2210 bool consistent = true;
2211 for (auto it : streamIdList) {
2212 auto const streamInfoIt = mStreamInfoMap.find(it);
2213 if (streamInfoIt == mStreamInfoMap.end()) {
2214 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2215 return false;
2216 }
2217 consistent =
2218 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2219 streamInfoIt->second.sensorPixelModesUsed.end();
2220 if (!consistent) {
2221 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2222 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2223 ALOGE("sensor pixel mode used list: %i", m);
2224 }
2225 break;
2226 }
2227 }
2228
2229 return consistent;
2230}
2231
Igor Murashkine7ee7632013-06-11 18:10:18 -07002232} // namespace android