blob: 5db3fa6cecca53b24341cb086c22fcf24ea8ba60 [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;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700724 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
725 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000726 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700727 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000728 switch (ret) {
729 case OK:
730 // Expected, do nothing.
731 break;
732 case INVALID_OPERATION: {
733 String8 msg = String8::format(
734 "Camera %s: Session configuration query not supported!",
735 mCameraIdStr.string());
736 ALOGD("%s: %s", __FUNCTION__, msg.string());
737 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
738 }
739
740 break;
741 default: {
742 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
743 strerror(-ret), ret);
744 ALOGE("%s: %s", __FUNCTION__, msg.string());
745 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
746 msg.string());
747 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800748 }
749
750 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700751}
752
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800753binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700754 ATRACE_CALL();
755 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
756
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800757 binder::Status res;
758 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700759
760 Mutex::Autolock icl(mBinderSerializationLock);
761
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800762 if (!mDevice.get()) {
763 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
764 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700765
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700766 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700767 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700768 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000769 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700770
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700771 if (mInputStream.configured && mInputStream.id == streamId) {
772 isInput = true;
773 } else {
774 // Guard against trying to delete non-created streams
775 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700776 if (streamId == mStreamMap.valueAt(i).streamId()) {
777 surfaces.push_back(mStreamMap.keyAt(i));
778 }
779 }
780
781 // See if this stream is one of the deferred streams.
782 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
783 if (streamId == mDeferredStreams[i]) {
784 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700785 break;
786 }
787 }
788
Emilian Peev2f5d6012022-01-19 16:16:50 -0800789 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000790 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
791 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
792 compositeIndex = i;
793 break;
794 }
795 }
796
Shuzhen Wang0129d522016-10-30 22:43:41 -0700797 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
798 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
799 " stream created yet", mCameraIdStr.string(), streamId);
800 ALOGW("%s: %s", __FUNCTION__, msg.string());
801 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700802 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700803 }
804
805 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800806 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700807
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800808 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800809 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
810 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800811 ALOGE("%s: %s", __FUNCTION__, msg.string());
812 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
813 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700814 if (isInput) {
815 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700816 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700817 for (auto& surface : surfaces) {
818 mStreamMap.removeItem(surface);
819 }
820
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800821 mConfiguredOutputs.removeItem(streamId);
822
Shuzhen Wang0129d522016-10-30 22:43:41 -0700823 if (dIndex != NAME_NOT_FOUND) {
824 mDeferredStreams.removeItemsAt(dIndex);
825 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000826
827 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800828 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000829 status_t ret;
830 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
831 != OK) {
832 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
833 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
834 streamId);
835 ALOGE("%s: %s", __FUNCTION__, msg.string());
836 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
837 }
838 mCompositeStreamMap.removeItemsAt(compositeIndex);
839 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800840 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
841 auto &streamSet = mapIt.second;
842 if (streamSet.find(streamId) != streamSet.end()) {
843 streamSet.erase(streamId);
844 break;
845 }
846 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700847 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700848 }
849
850 return res;
851}
852
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800853binder::Status CameraDeviceClient::createStream(
854 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
855 /*out*/
856 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700857 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700858
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800859 binder::Status res;
860 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700861
862 Mutex::Autolock icl(mBinderSerializationLock);
863
Shuzhen Wang0129d522016-10-30 22:43:41 -0700864 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
865 outputConfiguration.getGraphicBufferProducers();
866 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800867 bool deferredConsumer = outputConfiguration.isDeferred();
868 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800869 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800870 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800871 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800872 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800873 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800874 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800875 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700876
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700877 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000878 outputConfiguration.getSurfaceType());
879 if (!res.isOk()) {
880 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700881 }
Zhijun He5d677d12016-05-29 16:52:39 -0700882
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800883 if (!mDevice.get()) {
884 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
885 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700886 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800887 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000888 if (!res.isOk()) {
889 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800890 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000891
Shuzhen Wang0129d522016-10-30 22:43:41 -0700892 std::vector<sp<Surface>> surfaces;
893 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700894 status_t err;
895
896 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700897 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800898 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700899 }
900
Shuzhen Wang758c2152017-01-10 18:26:18 -0800901 OutputStreamInfo streamInfo;
902 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800903 const std::vector<int32_t> &sensorPixelModesUsed =
904 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700905 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700906 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700907 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800908 ssize_t index = mStreamMap.indexOfKey(binder);
909 if (index != NAME_NOT_FOUND) {
910 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
911 "(ID %zd)", mCameraIdStr.string(), index);
912 ALOGW("%s: %s", __FUNCTION__, msg.string());
913 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700914 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700915
Shuzhen Wang758c2152017-01-10 18:26:18 -0800916 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700917 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800918 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800919 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800920 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800921
922 if (!res.isOk())
923 return res;
924
925 if (!isStreamInfoValid) {
926 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700927 }
928
Shuzhen Wang758c2152017-01-10 18:26:18 -0800929 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700930 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800931 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700932
933 // If mOverrideForPerfClass is true, do not fail createStream() for small
934 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
935 // closest possible supported size.
936
Zhijun He125684a2015-12-26 15:07:30 -0800937 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100938 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800939 bool isDepthCompositeStream =
940 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800941 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
942 if (isDepthCompositeStream || isHeicCompisiteStream) {
943 sp<CompositeStream> compositeStream;
944 if (isDepthCompositeStream) {
945 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
946 } else {
947 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
948 }
949
Emilian Peev538c90e2018-12-17 18:03:19 +0000950 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
951 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700952 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800953 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
954 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000955 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800956 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000957 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
958 compositeStream);
959 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800960 } else {
961 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
962 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700963 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800964 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800965 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800966 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800967 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000968 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700969
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800970 if (err != OK) {
971 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800972 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800973 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
974 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800975 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700976 int i = 0;
977 for (auto& binder : binders) {
978 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
979 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100980 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
981 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700982 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800983
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800984 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800985 mStreamInfoMap[streamId] = streamInfo;
986
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800987 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700988 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800989 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
990 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700991
Zhijun He5d677d12016-05-29 16:52:39 -0700992 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800993 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700994
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800995 // Fill in mHighResolutionCameraIdToStreamIdSet map
996 const String8 &cameraIdUsed =
997 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
998 const char *cameraIdUsedCStr = cameraIdUsed.string();
999 // Only needed for high resolution sensors
1000 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1001 mHighResolutionSensors.end()) {
1002 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1003 }
1004
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001005 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001006 }
1007
1008 return res;
1009}
1010
Zhijun He5d677d12016-05-29 16:52:39 -07001011binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1012 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001013 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001014 /*out*/
1015 int* newStreamId) {
1016 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001017 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001018 android_dataspace dataSpace;
1019 status_t err;
1020 binder::Status res;
1021
1022 if (!mDevice.get()) {
1023 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1024 }
1025
1026 // Infer the surface info for deferred surface stream creation.
1027 width = outputConfiguration.getWidth();
1028 height = outputConfiguration.getHeight();
1029 surfaceType = outputConfiguration.getSurfaceType();
1030 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1031 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1032 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1033 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1034 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1035 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1036 }
1037 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001038 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001039 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001040 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001041 const String8 &cameraIdUsed =
1042 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1043 // Here, we override sensor pixel modes
1044 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1045 const std::vector<int32_t> &sensorPixelModesUsed =
1046 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001047 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001048 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1049 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1050 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1051 "sensor pixel modes used not valid for deferred stream");
1052 }
1053
Shuzhen Wang0129d522016-10-30 22:43:41 -07001054 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1055 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001056 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001057 &streamId, physicalCameraId,
1058 overriddenSensorPixelModesUsed,
1059 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001060 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001061 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001062 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001063 outputConfiguration.getStreamUseCase(),
1064 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001065
1066 if (err != OK) {
1067 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001068 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1069 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001070 } else {
1071 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1072 // a separate list to track. Once the deferred surface is set, this id will be
1073 // relocated to mStreamMap.
1074 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001075 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001076 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001077 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001078 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001079 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001080 outputConfiguration.getTimestampBase(),
1081 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001082
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001083 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001084 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001085 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001086
1087 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001088 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001089
1090 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001091 // Fill in mHighResolutionCameraIdToStreamIdSet
1092 const char *cameraIdUsedCStr = cameraIdUsed.string();
1093 // Only needed for high resolution sensors
1094 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1095 mHighResolutionSensors.end()) {
1096 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1097 }
Zhijun He5d677d12016-05-29 16:52:39 -07001098 }
1099 return res;
1100}
1101
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001102binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001103 int32_t transform = 0;
1104 status_t err;
1105 binder::Status res;
1106
1107 if (!mDevice.get()) {
1108 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1109 }
1110
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001111 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001112 if (err != OK) {
1113 // Error logged by getRotationTransformLocked.
1114 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1115 "Unable to calculate rotation transform for new stream");
1116 }
1117
1118 err = mDevice->setStreamTransform(streamId, transform);
1119 if (err != OK) {
1120 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1121 streamId);
1122 ALOGE("%s: %s", __FUNCTION__, msg.string());
1123 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1124 }
1125
1126 return res;
1127}
Ruben Brunkbba75572014-11-20 17:29:50 -08001128
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001129binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001130 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001131 /*out*/
1132 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001133
1134 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001135 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1136 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001137
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001138 binder::Status res;
1139 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001140
1141 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001142
1143 if (!mDevice.get()) {
1144 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1145 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001146
1147 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001148 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001149 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001150 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1151 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001152 }
1153
1154 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001155 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001156 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001157 mInputStream.configured = true;
1158 mInputStream.width = width;
1159 mInputStream.height = height;
1160 mInputStream.format = format;
1161 mInputStream.id = streamId;
1162
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001163 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1164 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001165
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001166 *newStreamId = streamId;
1167 } else {
1168 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001169 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001170 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001171 }
1172
1173 return res;
1174}
1175
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001176binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001177
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001178 binder::Status res;
1179 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1180
1181 if (inputSurface == NULL) {
1182 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001183 }
1184
1185 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001186 if (!mDevice.get()) {
1187 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1188 }
1189 sp<IGraphicBufferProducer> producer;
1190 status_t err = mDevice->getInputBufferProducer(&producer);
1191 if (err != OK) {
1192 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001193 "Camera %s: Error getting input Surface: %s (%d)",
1194 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001195 } else {
1196 inputSurface->name = String16("CameraInput");
1197 inputSurface->graphicBufferProducer = producer;
1198 }
1199 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001200}
1201
Emilian Peev40ead602017-09-26 15:46:36 +01001202binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1203 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1204 ATRACE_CALL();
1205
1206 binder::Status res;
1207 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1208
1209 Mutex::Autolock icl(mBinderSerializationLock);
1210
1211 if (!mDevice.get()) {
1212 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1213 }
1214
1215 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1216 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001217 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1218
Emilian Peev40ead602017-09-26 15:46:36 +01001219 auto producerCount = bufferProducers.size();
1220 if (producerCount == 0) {
1221 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1222 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1223 "bufferProducers must not be empty");
1224 }
1225
1226 // The first output is the one associated with the output configuration.
1227 // It should always be present, valid and the corresponding stream id should match.
1228 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1229 ssize_t index = mStreamMap.indexOfKey(binder);
1230 if (index == NAME_NOT_FOUND) {
1231 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1232 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1233 "OutputConfiguration is invalid");
1234 }
1235 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1236 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1237 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1238 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1239 "Stream id is invalid");
1240 }
1241
1242 std::vector<size_t> removedSurfaceIds;
1243 std::vector<sp<IBinder>> removedOutputs;
1244 std::vector<sp<Surface>> newOutputs;
1245 std::vector<OutputStreamInfo> streamInfos;
1246 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1247 for (auto &it : bufferProducers) {
1248 newOutputsMap.add(IInterface::asBinder(it), it);
1249 }
1250
1251 for (size_t i = 0; i < mStreamMap.size(); i++) {
1252 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1253 if (idx == NAME_NOT_FOUND) {
1254 if (mStreamMap[i].streamId() == streamId) {
1255 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1256 removedOutputs.push_back(mStreamMap.keyAt(i));
1257 }
1258 } else {
1259 if (mStreamMap[i].streamId() != streamId) {
1260 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1261 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1262 "Target Surface is invalid");
1263 }
1264 newOutputsMap.removeItemsAt(idx);
1265 }
1266 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001267 const std::vector<int32_t> &sensorPixelModesUsed =
1268 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001269 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001270 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001271 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001272 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001273
Emilian Peev40ead602017-09-26 15:46:36 +01001274 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1275 OutputStreamInfo outInfo;
1276 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001277 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001278 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001279 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001280 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001281 if (!res.isOk())
1282 return res;
1283
Emilian Peev40ead602017-09-26 15:46:36 +01001284 streamInfos.push_back(outInfo);
1285 newOutputs.push_back(surface);
1286 }
1287
1288 //Trivial case no changes required
1289 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1290 return binder::Status::ok();
1291 }
1292
1293 KeyedVector<sp<Surface>, size_t> outputMap;
1294 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1295 &outputMap);
1296 if (ret != OK) {
1297 switch (ret) {
1298 case NAME_NOT_FOUND:
1299 case BAD_VALUE:
1300 case -EBUSY:
1301 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1302 "Camera %s: Error updating stream: %s (%d)",
1303 mCameraIdStr.string(), strerror(ret), ret);
1304 break;
1305 default:
1306 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1307 "Camera %s: Error updating stream: %s (%d)",
1308 mCameraIdStr.string(), strerror(ret), ret);
1309 break;
1310 }
1311 } else {
1312 for (const auto &it : removedOutputs) {
1313 mStreamMap.removeItem(it);
1314 }
1315
1316 for (size_t i = 0; i < outputMap.size(); i++) {
1317 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1318 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1319 }
1320
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001321 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1322
Emilian Peev40ead602017-09-26 15:46:36 +01001323 ALOGV("%s: Camera %s: Successful stream ID %d update",
1324 __FUNCTION__, mCameraIdStr.string(), streamId);
1325 }
1326
1327 return res;
1328}
1329
Igor Murashkine7ee7632013-06-11 18:10:18 -07001330// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001331binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1332 /*out*/
1333 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001334{
1335 ATRACE_CALL();
1336 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1337
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001338 binder::Status res;
1339 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001340
1341 Mutex::Autolock icl(mBinderSerializationLock);
1342
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001343 if (!mDevice.get()) {
1344 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1345 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001346
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001347 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001348 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1349 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1350
1351 CameraMetadata metadata;
1352 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001353 request != NULL) {
1354
1355 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001356 } else if (err == BAD_VALUE) {
1357 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001358 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1359 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001360
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001361 } else {
1362 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001363 "Camera %s: Error creating default request for template %d: %s (%d)",
1364 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001365 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001366 return res;
1367}
1368
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001369binder::Status CameraDeviceClient::getCameraInfo(
1370 /*out*/
1371 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001372{
1373 ATRACE_CALL();
1374 ALOGV("%s", __FUNCTION__);
1375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001377
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001378 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001379
1380 Mutex::Autolock icl(mBinderSerializationLock);
1381
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001382 if (!mDevice.get()) {
1383 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1384 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001385
Igor Murashkin099b4572013-07-12 17:52:16 -07001386 if (info != NULL) {
1387 *info = mDevice->info(); // static camera metadata
1388 // TODO: merge with device-specific camera metadata
1389 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001390
1391 return res;
1392}
1393
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001394binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001395{
1396 ATRACE_CALL();
1397 ALOGV("%s", __FUNCTION__);
1398
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001399 binder::Status res;
1400 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001401
1402 Mutex::Autolock icl(mBinderSerializationLock);
1403
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001404 if (!mDevice.get()) {
1405 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1406 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001407
1408 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001409 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001410 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001411 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001412 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1413 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001414 ALOGE("%s: %s", __FUNCTION__, msg.string());
1415 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001416 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001417 status_t err = mDevice->waitUntilDrained();
1418 if (err != OK) {
1419 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001420 "Camera %s: Error waiting to drain: %s (%d)",
1421 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001422 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001423 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001424 return res;
1425}
1426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427binder::Status CameraDeviceClient::flush(
1428 /*out*/
1429 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001430 ATRACE_CALL();
1431 ALOGV("%s", __FUNCTION__);
1432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433 binder::Status res;
1434 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001435
1436 Mutex::Autolock icl(mBinderSerializationLock);
1437
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001438 if (!mDevice.get()) {
1439 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1440 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001441
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001442 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001443 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001444 status_t err = mDevice->flush(lastFrameNumber);
1445 if (err != OK) {
1446 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001447 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001448 }
1449 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001450}
1451
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001452binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001453 ATRACE_CALL();
1454 ALOGV("%s", __FUNCTION__);
1455
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001456 binder::Status res;
1457 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001458
1459 Mutex::Autolock icl(mBinderSerializationLock);
1460
1461 // Guard against trying to prepare non-created streams
1462 ssize_t index = NAME_NOT_FOUND;
1463 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001464 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001465 index = i;
1466 break;
1467 }
1468 }
1469
1470 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001471 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1472 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001473 ALOGW("%s: %s", __FUNCTION__, msg.string());
1474 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001475 }
1476
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001477 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1478 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001479 status_t err = mDevice->prepare(streamId);
1480 if (err == BAD_VALUE) {
1481 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001482 "Camera %s: Stream %d has already been used, and cannot be prepared",
1483 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001484 } else if (err != OK) {
1485 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001486 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001487 strerror(-err), err);
1488 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001489 return res;
1490}
1491
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001492binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001493 ATRACE_CALL();
1494 ALOGV("%s", __FUNCTION__);
1495
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001496 binder::Status res;
1497 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001498
1499 Mutex::Autolock icl(mBinderSerializationLock);
1500
1501 // Guard against trying to prepare non-created streams
1502 ssize_t index = NAME_NOT_FOUND;
1503 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001504 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001505 index = i;
1506 break;
1507 }
1508 }
1509
1510 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001511 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1512 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001513 ALOGW("%s: %s", __FUNCTION__, msg.string());
1514 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001515 }
1516
1517 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001518 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1519 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001520 ALOGE("%s: %s", __FUNCTION__, msg.string());
1521 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001522 }
1523
1524 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1525 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001526 status_t err = mDevice->prepare(maxCount, streamId);
1527 if (err == BAD_VALUE) {
1528 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001529 "Camera %s: Stream %d has already been used, and cannot be prepared",
1530 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001531 } else if (err != OK) {
1532 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001533 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001534 strerror(-err), err);
1535 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001536
1537 return res;
1538}
1539
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001540binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001541 ATRACE_CALL();
1542 ALOGV("%s", __FUNCTION__);
1543
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001544 binder::Status res;
1545 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001546
1547 Mutex::Autolock icl(mBinderSerializationLock);
1548
1549 // Guard against trying to prepare non-created streams
1550 ssize_t index = NAME_NOT_FOUND;
1551 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001552 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001553 index = i;
1554 break;
1555 }
1556 }
1557
1558 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001559 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1560 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001561 ALOGW("%s: %s", __FUNCTION__, msg.string());
1562 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001563 }
1564
1565 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1566 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001567 status_t err = mDevice->tearDown(streamId);
1568 if (err == BAD_VALUE) {
1569 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001570 "Camera %s: Stream %d is still in use, cannot be torn down",
1571 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001572 } else if (err != OK) {
1573 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001574 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001575 strerror(-err), err);
1576 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001577
1578 return res;
1579}
1580
Shuzhen Wang758c2152017-01-10 18:26:18 -08001581binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001582 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1583 ATRACE_CALL();
1584
1585 binder::Status res;
1586 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1587
1588 Mutex::Autolock icl(mBinderSerializationLock);
1589
Shuzhen Wang0129d522016-10-30 22:43:41 -07001590 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1591 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001592 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001593
Shuzhen Wang0129d522016-10-30 22:43:41 -07001594 if (bufferProducers.size() == 0) {
1595 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001596 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1597 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001598
Shuzhen Wang758c2152017-01-10 18:26:18 -08001599 // streamId should be in mStreamMap if this stream already has a surface attached
1600 // to it. Otherwise, it should be in mDeferredStreams.
1601 bool streamIdConfigured = false;
1602 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1603 for (size_t i = 0; i < mStreamMap.size(); i++) {
1604 if (mStreamMap.valueAt(i).streamId() == streamId) {
1605 streamIdConfigured = true;
1606 break;
1607 }
Zhijun He5d677d12016-05-29 16:52:39 -07001608 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001609 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1610 if (streamId == mDeferredStreams[i]) {
1611 deferredStreamIndex = i;
1612 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001613 }
1614
Shuzhen Wang758c2152017-01-10 18:26:18 -08001615 }
1616 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1617 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1618 "(ID %d)", mCameraIdStr.string(), streamId);
1619 ALOGW("%s: %s", __FUNCTION__, msg.string());
1620 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001621 }
1622
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001623 if (mStreamInfoMap[streamId].finalized) {
1624 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1625 " on stream ID %d", mCameraIdStr.string(), streamId);
1626 ALOGW("%s: %s", __FUNCTION__, msg.string());
1627 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1628 }
1629
Zhijun He5d677d12016-05-29 16:52:39 -07001630 if (!mDevice.get()) {
1631 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1632 }
1633
Shuzhen Wang758c2152017-01-10 18:26:18 -08001634 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001635 const std::vector<int32_t> &sensorPixelModesUsed =
1636 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001637 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001638 int64_t streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001639 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001640 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001641 for (auto& bufferProducer : bufferProducers) {
1642 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001643 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1644 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001645 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001646 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001647 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001648 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001649
1650 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001651 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001652 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001653 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001654 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001655
1656 if (!res.isOk())
1657 return res;
1658
1659 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001660 }
1661
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001662 // Gracefully handle case where finalizeOutputConfigurations is called
1663 // without any new surface.
1664 if (consumerSurfaces.size() == 0) {
1665 mStreamInfoMap[streamId].finalized = true;
1666 return res;
1667 }
1668
Zhijun He5d677d12016-05-29 16:52:39 -07001669 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001670 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001671 std::vector<int> consumerSurfaceIds;
1672 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001673 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001674 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1675 sp<IBinder> binder = IInterface::asBinder(
1676 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001677 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001678 binder.get(), streamId, consumerSurfaceIds[i]);
1679 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1680 }
1681 if (deferredStreamIndex != NAME_NOT_FOUND) {
1682 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001683 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001684 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001685 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001686 } else if (err == NO_INIT) {
1687 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001688 "Camera %s: Deferred surface is invalid: %s (%d)",
1689 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001690 } else {
1691 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001692 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1693 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001694 }
1695
1696 return res;
1697}
1698
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001699binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001700 ATRACE_CALL();
1701 binder::Status res;
1702 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1703
1704 if (!isValidAudioRestriction(mode)) {
1705 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1706 mCameraIdStr.string(), mode);
1707 ALOGW("%s: %s", __FUNCTION__, msg.string());
1708 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1709 }
1710
1711 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001712 BasicClient::setAudioRestriction(mode);
1713 return binder::Status::ok();
1714}
1715
1716binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1717 ATRACE_CALL();
1718 binder::Status res;
1719 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1720 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001721 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001722 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001723 }
1724 return binder::Status::ok();
1725}
1726
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001727status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1728 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1729
1730 return mDevice->setRotateAndCropAutoBehavior(
1731 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1732}
1733
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001734bool CameraDeviceClient::supportsCameraMute() {
1735 return mDevice->supportsCameraMute();
1736}
1737
1738status_t CameraDeviceClient::setCameraMute(bool enabled) {
1739 return mDevice->setCameraMute(enabled);
1740}
1741
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001742binder::Status CameraDeviceClient::switchToOffline(
1743 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001744 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001745 /*out*/
1746 sp<hardware::camera2::ICameraOfflineSession>* session) {
1747 ATRACE_CALL();
1748
1749 binder::Status res;
1750 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1751
1752 Mutex::Autolock icl(mBinderSerializationLock);
1753
1754 if (!mDevice.get()) {
1755 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1756 }
1757
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001758 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001759 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001760 ALOGE("%s: %s", __FUNCTION__, msg.string());
1761 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1762 }
1763
1764 if (session == nullptr) {
1765 String8 msg = String8::format("Invalid offline session");
1766 ALOGE("%s: %s", __FUNCTION__, msg.string());
1767 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1768 }
1769
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001770 std::vector<int32_t> offlineStreamIds;
1771 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001772 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001773 for (const auto& streamId : offlineOutputIds) {
1774 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001775 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001776 String8 msg = String8::format("Offline surface with id: %d is not registered",
1777 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001778 ALOGE("%s: %s", __FUNCTION__, msg.string());
1779 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1780 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001781
1782 if (!mStreamInfoMap[streamId].supportsOffline) {
1783 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1784 "offline mode", streamId);
1785 ALOGE("%s: %s", __FUNCTION__, msg.string());
1786 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1787 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001788
Emilian Peev2f5d6012022-01-19 16:16:50 -08001789 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001790 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001791 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001792 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001793 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001794 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001795 if (isCompositeStream) {
1796 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1797 if (compositeIdx == NAME_NOT_FOUND) {
1798 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1799 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1800 "Unknown composite stream");
1801 }
1802
1803 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1804 &offlineStreamIds);
1805 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1806 mCompositeStreamMap.valueAt(compositeIdx));
1807 break;
1808 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001809 }
1810
Emilian Peev4697b642019-11-19 17:11:14 -08001811 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001812 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001813 }
1814 }
1815
1816 sp<CameraOfflineSessionBase> offlineSession;
1817 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1818 if (ret != OK) {
1819 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1820 "Camera %s: Error switching to offline mode: %s (%d)",
1821 mCameraIdStr.string(), strerror(ret), ret);
1822 }
1823
Emilian Peevcc0b7952020-01-07 13:54:47 -08001824 sp<CameraOfflineSessionClient> offlineClient;
1825 if (offlineSession.get() != nullptr) {
1826 offlineClient = new CameraOfflineSessionClient(sCameraService,
1827 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001828 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1829 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001830 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1831 }
1832
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001833 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001834 // A successful offline session switch must reset the current camera client
1835 // and release any resources occupied by previously configured streams.
1836 mStreamMap.clear();
1837 mConfiguredOutputs.clear();
1838 mDeferredStreams.clear();
1839 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001840 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001841 mCompositeStreamMap.clear();
1842 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001843 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001844 switch(ret) {
1845 case BAD_VALUE:
1846 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1847 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1848 case TIMED_OUT:
1849 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1850 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1851 default:
1852 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1853 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1854 strerror(-ret), ret);
1855 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001856 }
1857
1858 *session = offlineClient;
1859
1860 return binder::Status::ok();
1861}
1862
Igor Murashkine7ee7632013-06-11 18:10:18 -07001863status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001864 return BasicClient::dump(fd, args);
1865}
1866
1867status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001868 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001869 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001870 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001871 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001872 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001873
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001874 dprintf(fd, " State:\n");
1875 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001876 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001877 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001878 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001879 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001880 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001881 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001882 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001883 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001884 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001885 mStreamMap.valueAt(i).streamId(),
1886 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001887 }
Zhijun He5d677d12016-05-29 16:52:39 -07001888 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001889 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001890 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001891 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001892 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001893 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001894 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001895 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001896 // TODO: print dynamic/request section from most recent requests
1897 mFrameProcessor->dump(fd, args);
1898
1899 return dumpDevice(fd, args);
1900}
1901
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001902status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1903 sp<CameraDeviceBase> device = mDevice;
1904 if (!device) {
1905 dprintf(out, " Device is detached.");
1906 return OK;
1907 }
1908 device->startWatchingTags(tags);
1909 return OK;
1910}
1911
1912status_t CameraDeviceClient::stopWatchingTags(int out) {
1913 sp<CameraDeviceBase> device = mDevice;
1914 if (!device) {
1915 dprintf(out, " Device is detached.");
1916 return OK;
1917 }
1918 device->stopWatchingTags();
1919 return OK;
1920}
1921
1922status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1923 sp<CameraDeviceBase> device = mDevice;
1924 if (!device) {
1925 return OK;
1926 }
1927 device->dumpWatchedEventsToVector(out);
1928 return OK;
1929}
1930
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001931void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001932 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001933 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001934 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001935
Emilian Peev538c90e2018-12-17 18:03:19 +00001936 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001937 {
1938 // Access to the composite stream map must be synchronized
1939 Mutex::Autolock l(mCompositeLock);
1940 // Composites can have multiple internal streams. Error notifications coming from such
1941 // internal streams may need to remain within camera service.
1942 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1943 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1944 resultExtras);
1945 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001946 }
1947
1948 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001949 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001950 }
1951}
1952
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001953void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1954 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1955
1956 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001957 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001958 }
1959
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001960 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001961 mStreamingRequestId = REQUEST_ID_NONE;
1962}
1963
Shuzhen Wang316781a2020-08-18 18:11:01 -07001964void CameraDeviceClient::notifyIdle(
1965 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1966 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001967 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001968 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001969
1970 if (remoteCb != 0) {
1971 remoteCb->onDeviceIdle();
1972 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08001973 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
1974 streamStats, mUserTag);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001975}
1976
Jianing Weicb0652e2014-03-12 18:29:36 -07001977void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001978 nsecs_t timestamp) {
1979 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001980 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001981 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001982 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001983 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001984 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001985
Emilian Peev2f5d6012022-01-19 16:16:50 -08001986 // Access to the composite stream map must be synchronized
1987 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001988 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1989 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1990 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001991}
1992
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001993void CameraDeviceClient::notifyPrepared(int streamId) {
1994 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001995 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001996 if (remoteCb != 0) {
1997 remoteCb->onPrepared(streamId);
1998 }
1999}
2000
Shuzhen Wang9d066012016-09-30 11:30:20 -07002001void CameraDeviceClient::notifyRequestQueueEmpty() {
2002 // Thread safe. Don't bother locking.
2003 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2004 if (remoteCb != 0) {
2005 remoteCb->onRequestQueueEmpty();
2006 }
2007}
2008
Igor Murashkine7ee7632013-06-11 18:10:18 -07002009void CameraDeviceClient::detachDevice() {
2010 if (mDevice == 0) return;
2011
Shuzhen Wang316781a2020-08-18 18:11:01 -07002012 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002013 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002014
Emilian Peev7ee731f2022-02-08 14:55:52 -08002015 if (mFrameProcessor.get() != nullptr) {
2016 mFrameProcessor->removeListener(
2017 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2018 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2019 mFrameProcessor->requestExit();
2020 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2021 mFrameProcessor->join();
2022 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2023 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002024
2025 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2026 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002027 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002028 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002029 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2030 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2031 }
2032
Igor Murashkine7ee7632013-06-11 18:10:18 -07002033 if ((code = mDevice->waitUntilDrained()) != OK) {
2034 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2035 code);
2036 }
2037 }
2038
Emilian Peev2f5d6012022-01-19 16:16:50 -08002039 {
2040 Mutex::Autolock l(mCompositeLock);
2041 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2042 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2043 if (ret != OK) {
2044 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2045 strerror(-ret), ret);
2046 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002047 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002048 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002049 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002050
Igor Murashkine7ee7632013-06-11 18:10:18 -07002051 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002052
2053 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2054 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002055}
2056
2057/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002058void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002059 ATRACE_CALL();
2060 ALOGV("%s", __FUNCTION__);
2061
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002062 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002063 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002064 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002065 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2066 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002067 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002068
Emilian Peev2f5d6012022-01-19 16:16:50 -08002069 // Access to the composite stream map must be synchronized
2070 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002071 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2072 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2073 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002074}
2075
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002076binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002077 if (mDisconnected) {
2078 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2079 "The camera device has been disconnected");
2080 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002081 status_t res = checkPid(checkLocation);
2082 return (res == OK) ? binder::Status::ok() :
2083 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2084 "Attempt to use camera from a different process than original client");
2085}
2086
Igor Murashkine7ee7632013-06-11 18:10:18 -07002087// TODO: move to Camera2ClientBase
2088bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2089
Jayant Chowdhary12361932018-08-27 14:46:13 -07002090 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002091 const int selfPid = getpid();
2092 camera_metadata_entry_t entry;
2093
2094 /**
2095 * Mixin default important security values
2096 * - android.led.transmit = defaulted ON
2097 */
2098 CameraMetadata staticInfo = mDevice->info();
2099 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2100 for(size_t i = 0; i < entry.count; ++i) {
2101 uint8_t led = entry.data.u8[i];
2102
2103 switch(led) {
2104 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2105 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2106 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2107 metadata.update(ANDROID_LED_TRANSMIT,
2108 &transmitDefault, 1);
2109 }
2110 break;
2111 }
2112 }
2113 }
2114
2115 // We can do anything!
2116 if (pid == selfPid) {
2117 return true;
2118 }
2119
2120 /**
2121 * Permission check special fields in the request
2122 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2123 */
2124 entry = metadata.find(ANDROID_LED_TRANSMIT);
2125 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2126 String16 permissionString =
2127 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2128 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002129 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002130 ALOGE("Permission Denial: "
2131 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2132 return false;
2133 }
2134 }
2135
2136 return true;
2137}
2138
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002139status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2140 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002141 ALOGV("%s: begin", __FUNCTION__);
2142
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002143 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002144 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002145}
2146
Emilian Peevf4816702020-04-03 15:44:51 -07002147binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2148 camera_request_template_t* tempId /*out*/) {
2149 binder::Status ret = binder::Status::ok();
2150
2151 if (tempId == nullptr) {
2152 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2153 "Camera %s: Invalid template argument", mCameraIdStr.string());
2154 return ret;
2155 }
2156 switch(templateId) {
2157 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2158 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2159 break;
2160 case ICameraDeviceUser::TEMPLATE_RECORD:
2161 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2162 break;
2163 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2164 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2165 break;
2166 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2167 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2168 break;
2169 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2170 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2171 break;
2172 case ICameraDeviceUser::TEMPLATE_MANUAL:
2173 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2174 break;
2175 default:
2176 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2177 "Camera %s: Template ID %d is invalid or not supported",
2178 mCameraIdStr.string(), templateId);
2179 return ret;
2180 }
2181
2182 return ret;
2183}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002184
2185const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2186 if (mDevice->getId() == cameraId) {
2187 return mDevice->info();
2188 }
2189 return mDevice->infoPhysical(cameraId);
2190}
2191
2192bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2193 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002194 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002195}
2196
2197bool CameraDeviceClient::isSensorPixelModeConsistent(
2198 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2199 // First we get the sensorPixelMode from the settings metadata.
2200 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2201 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2202 if (sensorPixelModeEntry.count != 0) {
2203 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2204 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2205 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2206 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2207 __FUNCTION__, sensorPixelMode);
2208 return false;
2209 }
2210 }
2211 // Check whether each stream has max resolution allowed.
2212 bool consistent = true;
2213 for (auto it : streamIdList) {
2214 auto const streamInfoIt = mStreamInfoMap.find(it);
2215 if (streamInfoIt == mStreamInfoMap.end()) {
2216 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2217 return false;
2218 }
2219 consistent =
2220 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2221 streamInfoIt->second.sensorPixelModesUsed.end();
2222 if (!consistent) {
2223 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2224 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2225 ALOGE("sensor pixel mode used list: %i", m);
2226 }
2227 break;
2228 }
2229 }
2230
2231 return consistent;
2232}
2233
Igor Murashkine7ee7632013-06-11 18:10:18 -07002234} // namespace android