blob: 1020c13a541602b36eb5b5890add60e8389b4f08 [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);
155 if (entry.count > 0 || ((entry.count % 2) != 0)) {
156 int standardBitmap = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
157 for (size_t i = 0; i < entry.count; i += 2) {
158 if (entry.data.i32[i] !=
159 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
160 mDynamicProfileMap.emplace(entry.data.i32[i], entry.data.i32[i+1]);
161 if ((entry.data.i32[i+1] == 0) || (entry.data.i32[i+1] &
162 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
163 standardBitmap |= entry.data.i32[i];
164 }
165 } else {
166 ALOGE("%s: Device %s includes unexpected profile entry: 0x%x!",
167 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i32[i]);
168 }
169 }
170 mDynamicProfileMap.emplace(
171 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
172 standardBitmap);
173 } else {
174 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
175 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
176 }
177 }
178 }
179
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700180 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800181 // Cache physical camera ids corresponding to this device and also the high
182 // resolution sensors in this device + physical camera ids
183 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
184 if (isUltraHighResolutionSensor(mCameraIdStr)) {
185 mHighResolutionSensors.insert(mCameraIdStr.string());
186 }
187 for (auto &physicalId : mPhysicalCameraIds) {
188 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
189 mHighResolutionSensors.insert(physicalId.c_str());
190 }
191 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700192 return OK;
193}
194
195CameraDeviceClient::~CameraDeviceClient() {
196}
197
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800198binder::Status CameraDeviceClient::submitRequest(
199 const hardware::camera2::CaptureRequest& request,
200 bool streaming,
201 /*out*/
202 hardware::camera2::utils::SubmitInfo *submitInfo) {
203 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
204 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700205}
206
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800207binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000208 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000209 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800210 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
211
Emilian Peev2f5d6012022-01-19 16:16:50 -0800212 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800213 // Trying to submit request with surface that wasn't created
214 if (idx == NAME_NOT_FOUND) {
215 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
216 " we have not called createStream on",
217 __FUNCTION__, mCameraIdStr.string());
218 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
219 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000220 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
221 != NAME_NOT_FOUND) {
222 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
223 currentStreamId);
224 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800225 }
226
227 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
228 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800229 outputStreamIds->push_back(streamSurfaceId.streamId());
230 }
231 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
232
233 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
234 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
235 streamSurfaceId.surfaceId());
236
Emilian Peevf873aa52018-01-26 14:58:28 +0000237 if (currentStreamId != nullptr) {
238 *currentStreamId = streamSurfaceId.streamId();
239 }
240
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800241 return binder::Status::ok();
242}
243
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800244static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
245 const Vector<int> &streamIdsForThisRequest) {
246 std::list<int> intersection;
247 for (auto &streamId : streamIdsForThisRequest) {
248 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
249 intersection.emplace_back(streamId);
250 }
251 }
252 return intersection;
253}
254
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800255binder::Status CameraDeviceClient::submitRequestList(
256 const std::vector<hardware::camera2::CaptureRequest>& requests,
257 bool streaming,
258 /*out*/
259 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700260 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700261 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700262
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800263 binder::Status res = binder::Status::ok();
264 status_t err;
265 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
266 return res;
267 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700268
269 Mutex::Autolock icl(mBinderSerializationLock);
270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800271 if (!mDevice.get()) {
272 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
273 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700274
275 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800276 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
277 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800278 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700279 }
280
Emilian Peevaebbe412018-01-15 13:53:24 +0000281 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700282 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700284 uint32_t loopCounter = 0;
285
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800286 for (auto&& request: requests) {
287 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700288 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800289 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
290 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800292 "No input configured for camera %s but request is for reprocessing",
293 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700294 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800295 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
296 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800297 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
298 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000299 } else if (request.mPhysicalCameraSettings.size() > 1) {
300 ALOGE("%s: Camera %s: reprocess requests not supported for "
301 "multiple physical cameras.", __FUNCTION__,
302 mCameraIdStr.string());
303 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
304 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700305 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700306 }
307
Emilian Peevaebbe412018-01-15 13:53:24 +0000308 if (request.mPhysicalCameraSettings.empty()) {
309 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
310 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800311 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000312 "Request doesn't contain any settings");
313 }
314
315 //The first capture settings should always match the logical camera id
316 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
317 if (mDevice->getId() != logicalId) {
318 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
319 mCameraIdStr.string());
320 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
321 "Invalid camera request settings");
322 }
323
Emilian Peevaebbe412018-01-15 13:53:24 +0000324 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800325 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
326 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800327 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
328 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700329 }
330
Jianing Wei90e59c92014-03-12 18:29:36 -0700331 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700332 * Write in the output stream IDs and map from stream ID to surface ID
333 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700334 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700335 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700336 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000337 std::vector<std::string> requestedPhysicalIds;
Emilian Peev2295df72021-11-12 18:14:10 -0800338 int dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800339 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800340 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800341 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700342
Emilian Peevf873aa52018-01-26 14:58:28 +0000343 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800344 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000345 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800346 if (!res.isOk()) {
347 return res;
348 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000349
350 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
351 if (index >= 0) {
352 String8 requestedPhysicalId(
353 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
354 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800355 dynamicProfileBitmap |=
356 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000357 } else {
358 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
359 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700360 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800361 } else {
362 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
363 int streamId = request.mStreamIdxList.itemAt(i);
364 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700365
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800366 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
367 if (index < 0) {
368 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
369 " we have not called createStream on: stream %d",
370 __FUNCTION__, mCameraIdStr.string(), streamId);
371 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
372 "Request targets Surface that is not part of current capture session");
373 }
374
375 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
376 if ((size_t)surfaceIdx >= gbps.size()) {
377 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
378 " we have not called createStream on: stream %d, surfaceIdx %d",
379 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
380 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
381 "Request targets Surface has invalid surface index");
382 }
383
Emilian Peevf873aa52018-01-26 14:58:28 +0000384 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800385 if (!res.isOk()) {
386 return res;
387 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000388
389 String8 requestedPhysicalId(
390 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
391 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800392 dynamicProfileBitmap |=
393 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
394 }
395 }
396
397 if (dynamicProfileBitmap !=
398 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
399 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
400 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
401 if ((dynamicProfileBitmap & i) == 0) {
402 continue;
403 }
404
405 const auto& it = mDynamicProfileMap.find(i);
406 if (it != mDynamicProfileMap.end()) {
407 if ((it->second == 0) ||
408 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
409 continue;
410 } else {
411 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
412 " reference an unsupported dynamic range profile combination"
413 " 0x%x!", __FUNCTION__, mCameraIdStr.string(),
414 dynamicProfileBitmap);
415 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
416 "Request targets an unsupported dynamic range profile"
417 " combination");
418 }
419 } else {
420 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
421 " references unsupported dynamic range profile 0x%x!",
422 __FUNCTION__, mCameraIdStr.string(), i);
423 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
424 "Request targets 10-bit Surface with unsupported dynamic range"
425 " profile");
426 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700427 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700428 }
429
Emilian Peevf873aa52018-01-26 14:58:28 +0000430 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
431 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000432 if (it.settings.isEmpty()) {
433 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
434 __FUNCTION__, mCameraIdStr.string());
435 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
436 "Request settings are empty");
437 }
438
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800439 // Check whether the physical / logical stream has settings
440 // consistent with the sensor pixel mode(s) it was configured with.
441 // mCameraIdToStreamSet will only have ids that are high resolution
442 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
443 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
444 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
445 outputStreamIds);
446 if (!request.mIsReprocess &&
447 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
448 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
449 "consistent with configured streams. Rejecting request.",
450 __FUNCTION__, it.id.c_str());
451 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
452 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
453 "streams configured");
454 }
455 }
456
Emilian Peevf873aa52018-01-26 14:58:28 +0000457 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700458 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
459 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
460 mSupportedPhysicalRequestKeys.end();
461 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
462 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
463 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000464 if (physicalId != mDevice->getId()) {
465 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
466 it.id);
467 if (found == requestedPhysicalIds.end()) {
468 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
469 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
470 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
471 "Invalid physical camera id");
472 }
Emilian Peev00420d22018-02-05 21:33:13 +0000473
474 if (!mSupportedPhysicalRequestKeys.empty()) {
475 // Filter out any unsupported physical request keys.
476 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
477 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
478 filteredParams.getAndLock());
479 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
480 filteredParams.unlock(meta);
481
482 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
483 camera_metadata_ro_entry entry = it.settings.find(keyIt);
484 if (entry.count > 0) {
485 filteredParams.update(entry);
486 }
487 }
488
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700489 physicalSettingsList.push_back({it.id, filteredParams,
490 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000491 }
492 } else {
493 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000494 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000495 }
496
497 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
498 // Callee logs
499 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
500 "Caller does not have permission to change restricted controls");
501 }
502
Emilian Peevaebbe412018-01-15 13:53:24 +0000503 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
504 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700505
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800506 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000507 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
508 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700509 }
510
Emilian Peevaebbe412018-01-15 13:53:24 +0000511 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
512 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700513 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800514 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
515 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
516 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700517
Emilian Peevaebbe412018-01-15 13:53:24 +0000518 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700519 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700520 }
521 mRequestIdCounter++;
522
523 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700524 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
525 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800526 if (err != OK) {
527 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800528 "Camera %s: Got error %s (%d) after trying to set streaming request",
529 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800530 ALOGE("%s: %s", __FUNCTION__, msg.string());
531 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
532 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700533 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700534 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700535 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700536 }
537 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700538 err = mDevice->captureList(metadataRequestList, surfaceMapList,
539 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800540 if (err != OK) {
541 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800542 "Camera %s: Got error %s (%d) after trying to submit capture request",
543 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800544 ALOGE("%s: %s", __FUNCTION__, msg.string());
545 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
546 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700547 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800548 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700549 }
550
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800551 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700552 return res;
553}
554
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800555binder::Status CameraDeviceClient::cancelRequest(
556 int requestId,
557 /*out*/
558 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700559 ATRACE_CALL();
560 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
561
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800562 status_t err;
563 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700564
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800565 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700566
567 Mutex::Autolock icl(mBinderSerializationLock);
568
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800569 if (!mDevice.get()) {
570 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
571 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700572
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700573 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700574 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800575 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
576 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800577 ALOGE("%s: %s", __FUNCTION__, msg.string());
578 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700579 }
580
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800581 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700582
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800583 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800584 ALOGV("%s: Camera %s: Successfully cleared streaming request",
585 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700586 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800587 } else {
588 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800589 "Camera %s: Error clearing streaming request: %s (%d)",
590 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700591 }
592
593 return res;
594}
595
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800596binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700597 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700598 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700599 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700601}
602
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100603binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700604 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800605 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700606 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700607 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
608 __FUNCTION__, mInputStream.configured ? 1 : 0,
609 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700610
Zhijun He0effd522016-03-04 10:22:27 -0800611 binder::Status res;
612 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
613
Emilian Peevcc0b7952020-01-07 13:54:47 -0800614 if (offlineStreamIds == nullptr) {
615 String8 msg = String8::format("Invalid offline stream ids");
616 ALOGE("%s: %s", __FUNCTION__, msg.string());
617 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
618 }
619
Zhijun He0effd522016-03-04 10:22:27 -0800620 Mutex::Autolock icl(mBinderSerializationLock);
621
622 if (!mDevice.get()) {
623 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
624 }
625
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700626 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000627 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000628 if (!res.isOk()) {
629 return res;
630 }
631
632 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
633 if (err == BAD_VALUE) {
634 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
635 mCameraIdStr.string());
636 ALOGE("%s: %s", __FUNCTION__, msg.string());
637 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
638 } else if (err != OK) {
639 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
640 mCameraIdStr.string(), strerror(-err), err);
641 ALOGE("%s: %s", __FUNCTION__, msg.string());
642 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000643 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800644 offlineStreamIds->clear();
645 mDevice->getOfflineStreamIds(offlineStreamIds);
646
Emilian Peev2f5d6012022-01-19 16:16:50 -0800647 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000648 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
649 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800650 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000651 String8 msg = String8::format("Camera %s: Error configuring composite "
652 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
653 ALOGE("%s: %s", __FUNCTION__, msg.string());
654 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
655 break;
656 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800657
658 // Composite streams can only support offline mode in case all individual internal
659 // streams are also supported.
660 std::vector<int> internalStreams;
661 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800662 offlineStreamIds->erase(
663 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800664 [&internalStreams] (int streamId) {
665 auto it = std::find(internalStreams.begin(), internalStreams.end(),
666 streamId);
667 if (it != internalStreams.end()) {
668 internalStreams.erase(it);
669 return true;
670 }
671
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800672 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800673 if (internalStreams.empty()) {
674 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
675 }
676 }
677
678 for (const auto& offlineStreamId : *offlineStreamIds) {
679 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000680 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700681
682 nsecs_t configureEnd = systemTime();
683 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
684 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
685 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000686 }
687
688 return res;
689}
690
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800691binder::Status CameraDeviceClient::isSessionConfigurationSupported(
692 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800693
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800694 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800695 binder::Status res;
696 status_t ret = OK;
697 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
698
699 Mutex::Autolock icl(mBinderSerializationLock);
700
701 if (!mDevice.get()) {
702 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
703 }
704
705 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700706 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000707 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800708 if (!res.isOk()) {
709 return res;
710 }
711
712 if (status == nullptr) {
713 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
714 ALOGE("%s: %s", __FUNCTION__, msg.string());
715 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
716 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700717
Emilian Peev35ae8262018-11-08 13:11:32 +0000718 *status = false;
719 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700720 sessionConfiguration, mOverrideForPerfClass, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000721 switch (ret) {
722 case OK:
723 // Expected, do nothing.
724 break;
725 case INVALID_OPERATION: {
726 String8 msg = String8::format(
727 "Camera %s: Session configuration query not supported!",
728 mCameraIdStr.string());
729 ALOGD("%s: %s", __FUNCTION__, msg.string());
730 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
731 }
732
733 break;
734 default: {
735 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
736 strerror(-ret), ret);
737 ALOGE("%s: %s", __FUNCTION__, msg.string());
738 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
739 msg.string());
740 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800741 }
742
743 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700744}
745
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800746binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700747 ATRACE_CALL();
748 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
749
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800750 binder::Status res;
751 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700752
753 Mutex::Autolock icl(mBinderSerializationLock);
754
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800755 if (!mDevice.get()) {
756 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
757 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700758
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700759 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700760 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700761 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000762 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700763
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700764 if (mInputStream.configured && mInputStream.id == streamId) {
765 isInput = true;
766 } else {
767 // Guard against trying to delete non-created streams
768 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700769 if (streamId == mStreamMap.valueAt(i).streamId()) {
770 surfaces.push_back(mStreamMap.keyAt(i));
771 }
772 }
773
774 // See if this stream is one of the deferred streams.
775 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
776 if (streamId == mDeferredStreams[i]) {
777 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700778 break;
779 }
780 }
781
Emilian Peev2f5d6012022-01-19 16:16:50 -0800782 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000783 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
784 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
785 compositeIndex = i;
786 break;
787 }
788 }
789
Shuzhen Wang0129d522016-10-30 22:43:41 -0700790 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
791 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
792 " stream created yet", mCameraIdStr.string(), streamId);
793 ALOGW("%s: %s", __FUNCTION__, msg.string());
794 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700795 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700796 }
797
798 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800799 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700800
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800801 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800802 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
803 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800804 ALOGE("%s: %s", __FUNCTION__, msg.string());
805 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
806 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700807 if (isInput) {
808 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700809 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700810 for (auto& surface : surfaces) {
811 mStreamMap.removeItem(surface);
812 }
813
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800814 mConfiguredOutputs.removeItem(streamId);
815
Shuzhen Wang0129d522016-10-30 22:43:41 -0700816 if (dIndex != NAME_NOT_FOUND) {
817 mDeferredStreams.removeItemsAt(dIndex);
818 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000819
820 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800821 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000822 status_t ret;
823 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
824 != OK) {
825 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
826 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
827 streamId);
828 ALOGE("%s: %s", __FUNCTION__, msg.string());
829 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
830 }
831 mCompositeStreamMap.removeItemsAt(compositeIndex);
832 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800833 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
834 auto &streamSet = mapIt.second;
835 if (streamSet.find(streamId) != streamSet.end()) {
836 streamSet.erase(streamId);
837 break;
838 }
839 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700840 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700841 }
842
843 return res;
844}
845
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800846binder::Status CameraDeviceClient::createStream(
847 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
848 /*out*/
849 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700850 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700851
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800852 binder::Status res;
853 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700854
855 Mutex::Autolock icl(mBinderSerializationLock);
856
Shuzhen Wang0129d522016-10-30 22:43:41 -0700857 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
858 outputConfiguration.getGraphicBufferProducers();
859 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800860 bool deferredConsumer = outputConfiguration.isDeferred();
861 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800862 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800863 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800864 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peev2295df72021-11-12 18:14:10 -0800865 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800866 int streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800867 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800868 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700869
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700870 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000871 outputConfiguration.getSurfaceType());
872 if (!res.isOk()) {
873 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700874 }
Zhijun He5d677d12016-05-29 16:52:39 -0700875
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800876 if (!mDevice.get()) {
877 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
878 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700879 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800880 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000881 if (!res.isOk()) {
882 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800883 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000884
Shuzhen Wang0129d522016-10-30 22:43:41 -0700885 std::vector<sp<Surface>> surfaces;
886 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700887 status_t err;
888
889 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700890 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800891 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700892 }
893
Shuzhen Wang758c2152017-01-10 18:26:18 -0800894 OutputStreamInfo streamInfo;
895 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800896 const std::vector<int32_t> &sensorPixelModesUsed =
897 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700898 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700899 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700900 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800901 ssize_t index = mStreamMap.indexOfKey(binder);
902 if (index != NAME_NOT_FOUND) {
903 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
904 "(ID %zd)", mCameraIdStr.string(), index);
905 ALOGW("%s: %s", __FUNCTION__, msg.string());
906 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700907 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700908
Shuzhen Wang758c2152017-01-10 18:26:18 -0800909 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700910 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800911 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800912 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800913 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800914
915 if (!res.isOk())
916 return res;
917
918 if (!isStreamInfoValid) {
919 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700920 }
921
Shuzhen Wang758c2152017-01-10 18:26:18 -0800922 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700923 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800924 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700925
926 // If mOverrideForPerfClass is true, do not fail createStream() for small
927 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
928 // closest possible supported size.
929
Zhijun He125684a2015-12-26 15:07:30 -0800930 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100931 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800932 bool isDepthCompositeStream =
933 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800934 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
935 if (isDepthCompositeStream || isHeicCompisiteStream) {
936 sp<CompositeStream> compositeStream;
937 if (isDepthCompositeStream) {
938 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
939 } else {
940 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
941 }
942
Emilian Peev538c90e2018-12-17 18:03:19 +0000943 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
944 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700945 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800946 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
947 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000948 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800949 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000950 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
951 compositeStream);
952 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800953 } else {
954 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
955 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700956 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800957 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800958 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800959 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800960 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000961 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700962
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800963 if (err != OK) {
964 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800965 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800966 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
967 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800968 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700969 int i = 0;
970 for (auto& binder : binders) {
971 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
972 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100973 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
974 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700975 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800976
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800977 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800978 mStreamInfoMap[streamId] = streamInfo;
979
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800980 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700981 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800982 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
983 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700984
Zhijun He5d677d12016-05-29 16:52:39 -0700985 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800986 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700987
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800988 // Fill in mHighResolutionCameraIdToStreamIdSet map
989 const String8 &cameraIdUsed =
990 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
991 const char *cameraIdUsedCStr = cameraIdUsed.string();
992 // Only needed for high resolution sensors
993 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
994 mHighResolutionSensors.end()) {
995 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
996 }
997
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800998 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700999 }
1000
1001 return res;
1002}
1003
Zhijun He5d677d12016-05-29 16:52:39 -07001004binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1005 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001006 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001007 /*out*/
1008 int* newStreamId) {
1009 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001010 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001011 android_dataspace dataSpace;
1012 status_t err;
1013 binder::Status res;
1014
1015 if (!mDevice.get()) {
1016 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1017 }
1018
1019 // Infer the surface info for deferred surface stream creation.
1020 width = outputConfiguration.getWidth();
1021 height = outputConfiguration.getHeight();
1022 surfaceType = outputConfiguration.getSurfaceType();
1023 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1024 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1025 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1026 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1027 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1028 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1029 }
1030 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001031 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001032 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001033 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001034 const String8 &cameraIdUsed =
1035 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1036 // Here, we override sensor pixel modes
1037 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1038 const std::vector<int32_t> &sensorPixelModesUsed =
1039 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001040 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001041 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1042 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1043 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1044 "sensor pixel modes used not valid for deferred stream");
1045 }
1046
Shuzhen Wang0129d522016-10-30 22:43:41 -07001047 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1048 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001049 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001050 &streamId, physicalCameraId,
1051 overriddenSensorPixelModesUsed,
1052 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001053 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001054 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001055 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001056 outputConfiguration.getStreamUseCase(),
1057 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001058
1059 if (err != OK) {
1060 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001061 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1062 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001063 } else {
1064 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1065 // a separate list to track. Once the deferred surface is set, this id will be
1066 // relocated to mStreamMap.
1067 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001068 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001069 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001070 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001071 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001072 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001073 outputConfiguration.getTimestampBase(),
1074 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001075
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001076 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001077 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001078 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001079
1080 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001081 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001082
1083 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001084 // Fill in mHighResolutionCameraIdToStreamIdSet
1085 const char *cameraIdUsedCStr = cameraIdUsed.string();
1086 // Only needed for high resolution sensors
1087 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1088 mHighResolutionSensors.end()) {
1089 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1090 }
Zhijun He5d677d12016-05-29 16:52:39 -07001091 }
1092 return res;
1093}
1094
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001095binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001096 int32_t transform = 0;
1097 status_t err;
1098 binder::Status res;
1099
1100 if (!mDevice.get()) {
1101 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1102 }
1103
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001104 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001105 if (err != OK) {
1106 // Error logged by getRotationTransformLocked.
1107 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1108 "Unable to calculate rotation transform for new stream");
1109 }
1110
1111 err = mDevice->setStreamTransform(streamId, transform);
1112 if (err != OK) {
1113 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1114 streamId);
1115 ALOGE("%s: %s", __FUNCTION__, msg.string());
1116 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1117 }
1118
1119 return res;
1120}
Ruben Brunkbba75572014-11-20 17:29:50 -08001121
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001122binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001123 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001124 /*out*/
1125 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001126
1127 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001128 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1129 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001130
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001131 binder::Status res;
1132 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001133
1134 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001135
1136 if (!mDevice.get()) {
1137 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1138 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001139
1140 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001141 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001142 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001143 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1144 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001145 }
1146
1147 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001148 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001149 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001150 mInputStream.configured = true;
1151 mInputStream.width = width;
1152 mInputStream.height = height;
1153 mInputStream.format = format;
1154 mInputStream.id = streamId;
1155
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001156 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1157 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001158
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001159 *newStreamId = streamId;
1160 } else {
1161 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001162 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001163 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001164 }
1165
1166 return res;
1167}
1168
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001169binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001170
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001171 binder::Status res;
1172 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1173
1174 if (inputSurface == NULL) {
1175 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001176 }
1177
1178 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001179 if (!mDevice.get()) {
1180 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1181 }
1182 sp<IGraphicBufferProducer> producer;
1183 status_t err = mDevice->getInputBufferProducer(&producer);
1184 if (err != OK) {
1185 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001186 "Camera %s: Error getting input Surface: %s (%d)",
1187 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001188 } else {
1189 inputSurface->name = String16("CameraInput");
1190 inputSurface->graphicBufferProducer = producer;
1191 }
1192 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001193}
1194
Emilian Peev40ead602017-09-26 15:46:36 +01001195binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1196 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1197 ATRACE_CALL();
1198
1199 binder::Status res;
1200 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1201
1202 Mutex::Autolock icl(mBinderSerializationLock);
1203
1204 if (!mDevice.get()) {
1205 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1206 }
1207
1208 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1209 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001210 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1211
Emilian Peev40ead602017-09-26 15:46:36 +01001212 auto producerCount = bufferProducers.size();
1213 if (producerCount == 0) {
1214 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1215 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1216 "bufferProducers must not be empty");
1217 }
1218
1219 // The first output is the one associated with the output configuration.
1220 // It should always be present, valid and the corresponding stream id should match.
1221 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1222 ssize_t index = mStreamMap.indexOfKey(binder);
1223 if (index == NAME_NOT_FOUND) {
1224 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1225 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1226 "OutputConfiguration is invalid");
1227 }
1228 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1229 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1230 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1231 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1232 "Stream id is invalid");
1233 }
1234
1235 std::vector<size_t> removedSurfaceIds;
1236 std::vector<sp<IBinder>> removedOutputs;
1237 std::vector<sp<Surface>> newOutputs;
1238 std::vector<OutputStreamInfo> streamInfos;
1239 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1240 for (auto &it : bufferProducers) {
1241 newOutputsMap.add(IInterface::asBinder(it), it);
1242 }
1243
1244 for (size_t i = 0; i < mStreamMap.size(); i++) {
1245 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1246 if (idx == NAME_NOT_FOUND) {
1247 if (mStreamMap[i].streamId() == streamId) {
1248 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1249 removedOutputs.push_back(mStreamMap.keyAt(i));
1250 }
1251 } else {
1252 if (mStreamMap[i].streamId() != streamId) {
1253 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1254 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1255 "Target Surface is invalid");
1256 }
1257 newOutputsMap.removeItemsAt(idx);
1258 }
1259 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001260 const std::vector<int32_t> &sensorPixelModesUsed =
1261 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001262 int streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001263 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peev2295df72021-11-12 18:14:10 -08001264 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001265 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001266
Emilian Peev40ead602017-09-26 15:46:36 +01001267 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1268 OutputStreamInfo outInfo;
1269 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001270 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001271 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001272 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001273 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001274 if (!res.isOk())
1275 return res;
1276
Emilian Peev40ead602017-09-26 15:46:36 +01001277 streamInfos.push_back(outInfo);
1278 newOutputs.push_back(surface);
1279 }
1280
1281 //Trivial case no changes required
1282 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1283 return binder::Status::ok();
1284 }
1285
1286 KeyedVector<sp<Surface>, size_t> outputMap;
1287 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1288 &outputMap);
1289 if (ret != OK) {
1290 switch (ret) {
1291 case NAME_NOT_FOUND:
1292 case BAD_VALUE:
1293 case -EBUSY:
1294 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1295 "Camera %s: Error updating stream: %s (%d)",
1296 mCameraIdStr.string(), strerror(ret), ret);
1297 break;
1298 default:
1299 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1300 "Camera %s: Error updating stream: %s (%d)",
1301 mCameraIdStr.string(), strerror(ret), ret);
1302 break;
1303 }
1304 } else {
1305 for (const auto &it : removedOutputs) {
1306 mStreamMap.removeItem(it);
1307 }
1308
1309 for (size_t i = 0; i < outputMap.size(); i++) {
1310 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1311 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1312 }
1313
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001314 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1315
Emilian Peev40ead602017-09-26 15:46:36 +01001316 ALOGV("%s: Camera %s: Successful stream ID %d update",
1317 __FUNCTION__, mCameraIdStr.string(), streamId);
1318 }
1319
1320 return res;
1321}
1322
Igor Murashkine7ee7632013-06-11 18:10:18 -07001323// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001324binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1325 /*out*/
1326 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001327{
1328 ATRACE_CALL();
1329 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1330
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001331 binder::Status res;
1332 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001333
1334 Mutex::Autolock icl(mBinderSerializationLock);
1335
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001336 if (!mDevice.get()) {
1337 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1338 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001339
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001340 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001341 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1342 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1343
1344 CameraMetadata metadata;
1345 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001346 request != NULL) {
1347
1348 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001349 } else if (err == BAD_VALUE) {
1350 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001351 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1352 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001353
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001354 } else {
1355 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001356 "Camera %s: Error creating default request for template %d: %s (%d)",
1357 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001358 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001359 return res;
1360}
1361
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001362binder::Status CameraDeviceClient::getCameraInfo(
1363 /*out*/
1364 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001365{
1366 ATRACE_CALL();
1367 ALOGV("%s", __FUNCTION__);
1368
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001369 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001370
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001371 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001372
1373 Mutex::Autolock icl(mBinderSerializationLock);
1374
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375 if (!mDevice.get()) {
1376 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1377 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001378
Igor Murashkin099b4572013-07-12 17:52:16 -07001379 if (info != NULL) {
1380 *info = mDevice->info(); // static camera metadata
1381 // TODO: merge with device-specific camera metadata
1382 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001383
1384 return res;
1385}
1386
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001387binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001388{
1389 ATRACE_CALL();
1390 ALOGV("%s", __FUNCTION__);
1391
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001392 binder::Status res;
1393 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001394
1395 Mutex::Autolock icl(mBinderSerializationLock);
1396
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001397 if (!mDevice.get()) {
1398 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1399 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001400
1401 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001402 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001403 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001404 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001405 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1406 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001407 ALOGE("%s: %s", __FUNCTION__, msg.string());
1408 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001409 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001410 status_t err = mDevice->waitUntilDrained();
1411 if (err != OK) {
1412 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001413 "Camera %s: Error waiting to drain: %s (%d)",
1414 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001415 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001416 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001417 return res;
1418}
1419
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001420binder::Status CameraDeviceClient::flush(
1421 /*out*/
1422 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001423 ATRACE_CALL();
1424 ALOGV("%s", __FUNCTION__);
1425
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001426 binder::Status res;
1427 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001428
1429 Mutex::Autolock icl(mBinderSerializationLock);
1430
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001431 if (!mDevice.get()) {
1432 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1433 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001434
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001435 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001436 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001437 status_t err = mDevice->flush(lastFrameNumber);
1438 if (err != OK) {
1439 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001440 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001441 }
1442 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001443}
1444
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001445binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001446 ATRACE_CALL();
1447 ALOGV("%s", __FUNCTION__);
1448
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001449 binder::Status res;
1450 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001451
1452 Mutex::Autolock icl(mBinderSerializationLock);
1453
1454 // Guard against trying to prepare non-created streams
1455 ssize_t index = NAME_NOT_FOUND;
1456 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001457 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001458 index = i;
1459 break;
1460 }
1461 }
1462
1463 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001464 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1465 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001466 ALOGW("%s: %s", __FUNCTION__, msg.string());
1467 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001468 }
1469
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001470 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1471 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001472 status_t err = mDevice->prepare(streamId);
1473 if (err == BAD_VALUE) {
1474 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001475 "Camera %s: Stream %d has already been used, and cannot be prepared",
1476 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001477 } else if (err != OK) {
1478 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001479 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001480 strerror(-err), err);
1481 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001482 return res;
1483}
1484
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001485binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001486 ATRACE_CALL();
1487 ALOGV("%s", __FUNCTION__);
1488
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001489 binder::Status res;
1490 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001491
1492 Mutex::Autolock icl(mBinderSerializationLock);
1493
1494 // Guard against trying to prepare non-created streams
1495 ssize_t index = NAME_NOT_FOUND;
1496 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001497 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001498 index = i;
1499 break;
1500 }
1501 }
1502
1503 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001504 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1505 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001506 ALOGW("%s: %s", __FUNCTION__, msg.string());
1507 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001508 }
1509
1510 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001511 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1512 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001513 ALOGE("%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 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1518 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001519 status_t err = mDevice->prepare(maxCount, streamId);
1520 if (err == BAD_VALUE) {
1521 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001522 "Camera %s: Stream %d has already been used, and cannot be prepared",
1523 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001524 } else if (err != OK) {
1525 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001526 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001527 strerror(-err), err);
1528 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001529
1530 return res;
1531}
1532
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001533binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001534 ATRACE_CALL();
1535 ALOGV("%s", __FUNCTION__);
1536
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001537 binder::Status res;
1538 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001539
1540 Mutex::Autolock icl(mBinderSerializationLock);
1541
1542 // Guard against trying to prepare non-created streams
1543 ssize_t index = NAME_NOT_FOUND;
1544 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001545 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001546 index = i;
1547 break;
1548 }
1549 }
1550
1551 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001552 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1553 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001554 ALOGW("%s: %s", __FUNCTION__, msg.string());
1555 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001556 }
1557
1558 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1559 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001560 status_t err = mDevice->tearDown(streamId);
1561 if (err == BAD_VALUE) {
1562 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001563 "Camera %s: Stream %d is still in use, cannot be torn down",
1564 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001565 } else if (err != OK) {
1566 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001567 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001568 strerror(-err), err);
1569 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001570
1571 return res;
1572}
1573
Shuzhen Wang758c2152017-01-10 18:26:18 -08001574binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001575 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1576 ATRACE_CALL();
1577
1578 binder::Status res;
1579 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1580
1581 Mutex::Autolock icl(mBinderSerializationLock);
1582
Shuzhen Wang0129d522016-10-30 22:43:41 -07001583 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1584 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001585 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001586
Shuzhen Wang0129d522016-10-30 22:43:41 -07001587 if (bufferProducers.size() == 0) {
1588 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001589 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1590 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001591
Shuzhen Wang758c2152017-01-10 18:26:18 -08001592 // streamId should be in mStreamMap if this stream already has a surface attached
1593 // to it. Otherwise, it should be in mDeferredStreams.
1594 bool streamIdConfigured = false;
1595 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1596 for (size_t i = 0; i < mStreamMap.size(); i++) {
1597 if (mStreamMap.valueAt(i).streamId() == streamId) {
1598 streamIdConfigured = true;
1599 break;
1600 }
Zhijun He5d677d12016-05-29 16:52:39 -07001601 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001602 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1603 if (streamId == mDeferredStreams[i]) {
1604 deferredStreamIndex = i;
1605 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001606 }
1607
Shuzhen Wang758c2152017-01-10 18:26:18 -08001608 }
1609 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1610 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1611 "(ID %d)", mCameraIdStr.string(), streamId);
1612 ALOGW("%s: %s", __FUNCTION__, msg.string());
1613 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001614 }
1615
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001616 if (mStreamInfoMap[streamId].finalized) {
1617 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1618 " on stream ID %d", mCameraIdStr.string(), streamId);
1619 ALOGW("%s: %s", __FUNCTION__, msg.string());
1620 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1621 }
1622
Zhijun He5d677d12016-05-29 16:52:39 -07001623 if (!mDevice.get()) {
1624 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1625 }
1626
Shuzhen Wang758c2152017-01-10 18:26:18 -08001627 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001628 const std::vector<int32_t> &sensorPixelModesUsed =
1629 outputConfiguration.getSensorPixelModesUsed();
Emilian Peev2295df72021-11-12 18:14:10 -08001630 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001631 int streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001632 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001633 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001634 for (auto& bufferProducer : bufferProducers) {
1635 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001636 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1637 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001638 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001639 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001640 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001641 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001642
1643 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001644 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001645 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001646 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001647 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001648
1649 if (!res.isOk())
1650 return res;
1651
1652 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001653 }
1654
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001655 // Gracefully handle case where finalizeOutputConfigurations is called
1656 // without any new surface.
1657 if (consumerSurfaces.size() == 0) {
1658 mStreamInfoMap[streamId].finalized = true;
1659 return res;
1660 }
1661
Zhijun He5d677d12016-05-29 16:52:39 -07001662 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001663 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001664 std::vector<int> consumerSurfaceIds;
1665 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001666 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001667 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1668 sp<IBinder> binder = IInterface::asBinder(
1669 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001670 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001671 binder.get(), streamId, consumerSurfaceIds[i]);
1672 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1673 }
1674 if (deferredStreamIndex != NAME_NOT_FOUND) {
1675 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001676 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001677 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001678 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001679 } else if (err == NO_INIT) {
1680 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001681 "Camera %s: Deferred surface is invalid: %s (%d)",
1682 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001683 } else {
1684 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001685 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1686 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001687 }
1688
1689 return res;
1690}
1691
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001692binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001693 ATRACE_CALL();
1694 binder::Status res;
1695 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1696
1697 if (!isValidAudioRestriction(mode)) {
1698 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1699 mCameraIdStr.string(), mode);
1700 ALOGW("%s: %s", __FUNCTION__, msg.string());
1701 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1702 }
1703
1704 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001705 BasicClient::setAudioRestriction(mode);
1706 return binder::Status::ok();
1707}
1708
1709binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1710 ATRACE_CALL();
1711 binder::Status res;
1712 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1713 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001714 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001715 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001716 }
1717 return binder::Status::ok();
1718}
1719
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001720status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1721 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1722
1723 return mDevice->setRotateAndCropAutoBehavior(
1724 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1725}
1726
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001727bool CameraDeviceClient::supportsCameraMute() {
1728 return mDevice->supportsCameraMute();
1729}
1730
1731status_t CameraDeviceClient::setCameraMute(bool enabled) {
1732 return mDevice->setCameraMute(enabled);
1733}
1734
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001735binder::Status CameraDeviceClient::switchToOffline(
1736 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001737 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001738 /*out*/
1739 sp<hardware::camera2::ICameraOfflineSession>* session) {
1740 ATRACE_CALL();
1741
1742 binder::Status res;
1743 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1744
1745 Mutex::Autolock icl(mBinderSerializationLock);
1746
1747 if (!mDevice.get()) {
1748 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1749 }
1750
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001751 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001752 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001753 ALOGE("%s: %s", __FUNCTION__, msg.string());
1754 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1755 }
1756
1757 if (session == nullptr) {
1758 String8 msg = String8::format("Invalid offline session");
1759 ALOGE("%s: %s", __FUNCTION__, msg.string());
1760 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1761 }
1762
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001763 std::vector<int32_t> offlineStreamIds;
1764 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001765 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001766 for (const auto& streamId : offlineOutputIds) {
1767 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001768 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001769 String8 msg = String8::format("Offline surface with id: %d is not registered",
1770 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001771 ALOGE("%s: %s", __FUNCTION__, msg.string());
1772 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1773 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001774
1775 if (!mStreamInfoMap[streamId].supportsOffline) {
1776 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1777 "offline mode", streamId);
1778 ALOGE("%s: %s", __FUNCTION__, msg.string());
1779 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1780 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001781
Emilian Peev2f5d6012022-01-19 16:16:50 -08001782 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001783 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001784 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001785 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001786 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001787 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001788 if (isCompositeStream) {
1789 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1790 if (compositeIdx == NAME_NOT_FOUND) {
1791 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1792 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1793 "Unknown composite stream");
1794 }
1795
1796 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1797 &offlineStreamIds);
1798 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1799 mCompositeStreamMap.valueAt(compositeIdx));
1800 break;
1801 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001802 }
1803
Emilian Peev4697b642019-11-19 17:11:14 -08001804 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001805 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001806 }
1807 }
1808
1809 sp<CameraOfflineSessionBase> offlineSession;
1810 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1811 if (ret != OK) {
1812 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1813 "Camera %s: Error switching to offline mode: %s (%d)",
1814 mCameraIdStr.string(), strerror(ret), ret);
1815 }
1816
Emilian Peevcc0b7952020-01-07 13:54:47 -08001817 sp<CameraOfflineSessionClient> offlineClient;
1818 if (offlineSession.get() != nullptr) {
1819 offlineClient = new CameraOfflineSessionClient(sCameraService,
1820 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001821 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1822 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001823 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1824 }
1825
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001826 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001827 // A successful offline session switch must reset the current camera client
1828 // and release any resources occupied by previously configured streams.
1829 mStreamMap.clear();
1830 mConfiguredOutputs.clear();
1831 mDeferredStreams.clear();
1832 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001833 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001834 mCompositeStreamMap.clear();
1835 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001836 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001837 switch(ret) {
1838 case BAD_VALUE:
1839 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1840 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1841 case TIMED_OUT:
1842 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1843 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1844 default:
1845 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1846 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1847 strerror(-ret), ret);
1848 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001849 }
1850
1851 *session = offlineClient;
1852
1853 return binder::Status::ok();
1854}
1855
Igor Murashkine7ee7632013-06-11 18:10:18 -07001856status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001857 return BasicClient::dump(fd, args);
1858}
1859
1860status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001861 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001862 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001863 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001864 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001865 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001866
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001867 dprintf(fd, " State:\n");
1868 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001869 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001870 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001871 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001872 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001873 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001874 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001875 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001876 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001877 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001878 mStreamMap.valueAt(i).streamId(),
1879 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001880 }
Zhijun He5d677d12016-05-29 16:52:39 -07001881 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001882 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001883 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001884 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001885 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001886 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001887 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001888 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001889 // TODO: print dynamic/request section from most recent requests
1890 mFrameProcessor->dump(fd, args);
1891
1892 return dumpDevice(fd, args);
1893}
1894
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001895status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1896 sp<CameraDeviceBase> device = mDevice;
1897 if (!device) {
1898 dprintf(out, " Device is detached.");
1899 return OK;
1900 }
1901 device->startWatchingTags(tags);
1902 return OK;
1903}
1904
1905status_t CameraDeviceClient::stopWatchingTags(int out) {
1906 sp<CameraDeviceBase> device = mDevice;
1907 if (!device) {
1908 dprintf(out, " Device is detached.");
1909 return OK;
1910 }
1911 device->stopWatchingTags();
1912 return OK;
1913}
1914
1915status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1916 sp<CameraDeviceBase> device = mDevice;
1917 if (!device) {
1918 return OK;
1919 }
1920 device->dumpWatchedEventsToVector(out);
1921 return OK;
1922}
1923
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001924void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001925 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001926 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001927 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001928
Emilian Peev538c90e2018-12-17 18:03:19 +00001929 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001930 {
1931 // Access to the composite stream map must be synchronized
1932 Mutex::Autolock l(mCompositeLock);
1933 // Composites can have multiple internal streams. Error notifications coming from such
1934 // internal streams may need to remain within camera service.
1935 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1936 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1937 resultExtras);
1938 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001939 }
1940
1941 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001942 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001943 }
1944}
1945
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001946void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1947 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1948
1949 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001950 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001951 }
1952
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001953 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001954 mStreamingRequestId = REQUEST_ID_NONE;
1955}
1956
Shuzhen Wang316781a2020-08-18 18:11:01 -07001957void CameraDeviceClient::notifyIdle(
1958 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1959 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001960 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001961 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001962
1963 if (remoteCb != 0) {
1964 remoteCb->onDeviceIdle();
1965 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001966 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001967}
1968
Jianing Weicb0652e2014-03-12 18:29:36 -07001969void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001970 nsecs_t timestamp) {
1971 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001972 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001973 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001974 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001975 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001976 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001977
Emilian Peev2f5d6012022-01-19 16:16:50 -08001978 // Access to the composite stream map must be synchronized
1979 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001980 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1981 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1982 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001983}
1984
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001985void CameraDeviceClient::notifyPrepared(int streamId) {
1986 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001987 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001988 if (remoteCb != 0) {
1989 remoteCb->onPrepared(streamId);
1990 }
1991}
1992
Shuzhen Wang9d066012016-09-30 11:30:20 -07001993void CameraDeviceClient::notifyRequestQueueEmpty() {
1994 // Thread safe. Don't bother locking.
1995 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1996 if (remoteCb != 0) {
1997 remoteCb->onRequestQueueEmpty();
1998 }
1999}
2000
Igor Murashkine7ee7632013-06-11 18:10:18 -07002001void CameraDeviceClient::detachDevice() {
2002 if (mDevice == 0) return;
2003
Shuzhen Wang316781a2020-08-18 18:11:01 -07002004 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002005 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002006
Emilian Peev7ee731f2022-02-08 14:55:52 -08002007 if (mFrameProcessor.get() != nullptr) {
2008 mFrameProcessor->removeListener(
2009 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2010 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2011 mFrameProcessor->requestExit();
2012 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2013 mFrameProcessor->join();
2014 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2015 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002016
2017 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2018 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002019 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002020 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002021 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2022 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2023 }
2024
Igor Murashkine7ee7632013-06-11 18:10:18 -07002025 if ((code = mDevice->waitUntilDrained()) != OK) {
2026 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2027 code);
2028 }
2029 }
2030
Emilian Peev2f5d6012022-01-19 16:16:50 -08002031 {
2032 Mutex::Autolock l(mCompositeLock);
2033 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2034 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2035 if (ret != OK) {
2036 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2037 strerror(-ret), ret);
2038 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002039 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002040 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002041 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002042
Igor Murashkine7ee7632013-06-11 18:10:18 -07002043 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002044
2045 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2046 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002047}
2048
2049/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002050void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002051 ATRACE_CALL();
2052 ALOGV("%s", __FUNCTION__);
2053
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002054 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002055 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002056 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002057 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2058 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002059 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002060
Emilian Peev2f5d6012022-01-19 16:16:50 -08002061 // Access to the composite stream map must be synchronized
2062 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002063 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2064 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2065 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002066}
2067
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002068binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002069 if (mDisconnected) {
2070 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2071 "The camera device has been disconnected");
2072 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002073 status_t res = checkPid(checkLocation);
2074 return (res == OK) ? binder::Status::ok() :
2075 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2076 "Attempt to use camera from a different process than original client");
2077}
2078
Igor Murashkine7ee7632013-06-11 18:10:18 -07002079// TODO: move to Camera2ClientBase
2080bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2081
Jayant Chowdhary12361932018-08-27 14:46:13 -07002082 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002083 const int selfPid = getpid();
2084 camera_metadata_entry_t entry;
2085
2086 /**
2087 * Mixin default important security values
2088 * - android.led.transmit = defaulted ON
2089 */
2090 CameraMetadata staticInfo = mDevice->info();
2091 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2092 for(size_t i = 0; i < entry.count; ++i) {
2093 uint8_t led = entry.data.u8[i];
2094
2095 switch(led) {
2096 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2097 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2098 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2099 metadata.update(ANDROID_LED_TRANSMIT,
2100 &transmitDefault, 1);
2101 }
2102 break;
2103 }
2104 }
2105 }
2106
2107 // We can do anything!
2108 if (pid == selfPid) {
2109 return true;
2110 }
2111
2112 /**
2113 * Permission check special fields in the request
2114 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2115 */
2116 entry = metadata.find(ANDROID_LED_TRANSMIT);
2117 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2118 String16 permissionString =
2119 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2120 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002121 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002122 ALOGE("Permission Denial: "
2123 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2124 return false;
2125 }
2126 }
2127
2128 return true;
2129}
2130
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002131status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2132 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002133 ALOGV("%s: begin", __FUNCTION__);
2134
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002135 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002136 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002137}
2138
Emilian Peevf4816702020-04-03 15:44:51 -07002139binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2140 camera_request_template_t* tempId /*out*/) {
2141 binder::Status ret = binder::Status::ok();
2142
2143 if (tempId == nullptr) {
2144 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2145 "Camera %s: Invalid template argument", mCameraIdStr.string());
2146 return ret;
2147 }
2148 switch(templateId) {
2149 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2150 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2151 break;
2152 case ICameraDeviceUser::TEMPLATE_RECORD:
2153 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2154 break;
2155 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2156 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2157 break;
2158 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2159 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2160 break;
2161 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2162 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2163 break;
2164 case ICameraDeviceUser::TEMPLATE_MANUAL:
2165 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2166 break;
2167 default:
2168 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2169 "Camera %s: Template ID %d is invalid or not supported",
2170 mCameraIdStr.string(), templateId);
2171 return ret;
2172 }
2173
2174 return ret;
2175}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002176
2177const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2178 if (mDevice->getId() == cameraId) {
2179 return mDevice->info();
2180 }
2181 return mDevice->infoPhysical(cameraId);
2182}
2183
2184bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2185 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002186 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002187}
2188
2189bool CameraDeviceClient::isSensorPixelModeConsistent(
2190 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2191 // First we get the sensorPixelMode from the settings metadata.
2192 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2193 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2194 if (sensorPixelModeEntry.count != 0) {
2195 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2196 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2197 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2198 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2199 __FUNCTION__, sensorPixelMode);
2200 return false;
2201 }
2202 }
2203 // Check whether each stream has max resolution allowed.
2204 bool consistent = true;
2205 for (auto it : streamIdList) {
2206 auto const streamInfoIt = mStreamInfoMap.find(it);
2207 if (streamInfoIt == mStreamInfoMap.end()) {
2208 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2209 return false;
2210 }
2211 consistent =
2212 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2213 streamInfoIt->second.sensorPixelModesUsed.end();
2214 if (!consistent) {
2215 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2216 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2217 ALOGE("sensor pixel mode used list: %i", m);
2218 }
2219 break;
2220 }
2221 }
2222
2223 return consistent;
2224}
2225
Igor Murashkine7ee7632013-06-11 18:10:18 -07002226} // namespace android