blob: 16ea67d21d3f00babc623b435dc9df06ba00d59f [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,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090061 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080062 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080063 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070064 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070065 int sensorOrientation,
Igor Murashkine7ee7632013-06-11 18:10:18 -070066 int clientPid,
67 uid_t clientUid,
68 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080069 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080070 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080071 clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080072 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080073 cameraId,
74 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070075 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080076 clientPid,
77 clientUid,
78 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070079 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080080 // We don't need it for API2 clients, but Camera2ClientBase requires it.
81 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070082}
Igor Murashkine7ee7632013-06-11 18:10:18 -070083
84// Interface used by CameraService
85
86CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080087 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
88 const String16& clientPackageName,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090089 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080090 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080091 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070092 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080093 int clientPid,
94 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070095 int servicePid,
96 bool overrideForPerfClass) :
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080097 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070098 cameraId, /*API1 camera ID*/ -1, cameraFacing, sensorOrientation,
99 clientPid, clientUid, servicePid, overrideForPerfClass),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700100 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700101 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700102 mRequestIdCounter(0),
103 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700104
105 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800106 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700107}
108
Emilian Peevbd8c5032018-02-14 23:05:40 +0000109status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
110 const String8& monitorTags) {
111 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800112}
113
114template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000115status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700116 ATRACE_CALL();
117 status_t res;
118
Emilian Peevbd8c5032018-02-14 23:05:40 +0000119 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700120 if (res != OK) {
121 return res;
122 }
123
124 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700125 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800126 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700127 mFrameProcessor->run(threadName.string());
128
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800129 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
130 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800131 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700132 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700133
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800134 const CameraMetadata &deviceInfo = mDevice->info();
135 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000136 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
137 if (physicalKeysEntry.count > 0) {
138 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
139 physicalKeysEntry.data.i32,
140 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
141 }
142
Emilian Peev2295df72021-11-12 18:14:10 -0800143 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
144 mDynamicProfileMap.emplace(
145 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
146 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
147 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800148 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800149 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800150 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800151 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
152 if (entry.count > 0 || ((entry.count % 2) != 0)) {
153 int standardBitmap = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
154 for (size_t i = 0; i < entry.count; i += 2) {
155 if (entry.data.i32[i] !=
156 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
157 mDynamicProfileMap.emplace(entry.data.i32[i], entry.data.i32[i+1]);
158 if ((entry.data.i32[i+1] == 0) || (entry.data.i32[i+1] &
159 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
160 standardBitmap |= entry.data.i32[i];
161 }
162 } else {
163 ALOGE("%s: Device %s includes unexpected profile entry: 0x%x!",
164 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i32[i]);
165 }
166 }
167 mDynamicProfileMap.emplace(
168 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
169 standardBitmap);
170 } else {
171 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
172 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
173 }
174 }
175 }
176
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700177 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800178 // Cache physical camera ids corresponding to this device and also the high
179 // resolution sensors in this device + physical camera ids
180 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
181 if (isUltraHighResolutionSensor(mCameraIdStr)) {
182 mHighResolutionSensors.insert(mCameraIdStr.string());
183 }
184 for (auto &physicalId : mPhysicalCameraIds) {
185 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
186 mHighResolutionSensors.insert(physicalId.c_str());
187 }
188 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700189 return OK;
190}
191
192CameraDeviceClient::~CameraDeviceClient() {
193}
194
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800195binder::Status CameraDeviceClient::submitRequest(
196 const hardware::camera2::CaptureRequest& request,
197 bool streaming,
198 /*out*/
199 hardware::camera2::utils::SubmitInfo *submitInfo) {
200 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
201 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700202}
203
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800204binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000205 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000206 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800207 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
208
Emilian Peev2f5d6012022-01-19 16:16:50 -0800209 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800210 // Trying to submit request with surface that wasn't created
211 if (idx == NAME_NOT_FOUND) {
212 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
213 " we have not called createStream on",
214 __FUNCTION__, mCameraIdStr.string());
215 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
216 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000217 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
218 != NAME_NOT_FOUND) {
219 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
220 currentStreamId);
221 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800222 }
223
224 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
225 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800226 outputStreamIds->push_back(streamSurfaceId.streamId());
227 }
228 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
229
230 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
231 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
232 streamSurfaceId.surfaceId());
233
Emilian Peevf873aa52018-01-26 14:58:28 +0000234 if (currentStreamId != nullptr) {
235 *currentStreamId = streamSurfaceId.streamId();
236 }
237
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800238 return binder::Status::ok();
239}
240
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800241static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
242 const Vector<int> &streamIdsForThisRequest) {
243 std::list<int> intersection;
244 for (auto &streamId : streamIdsForThisRequest) {
245 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
246 intersection.emplace_back(streamId);
247 }
248 }
249 return intersection;
250}
251
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800252binder::Status CameraDeviceClient::submitRequestList(
253 const std::vector<hardware::camera2::CaptureRequest>& requests,
254 bool streaming,
255 /*out*/
256 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700257 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700258 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700259
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800260 binder::Status res = binder::Status::ok();
261 status_t err;
262 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
263 return res;
264 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700265
266 Mutex::Autolock icl(mBinderSerializationLock);
267
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800268 if (!mDevice.get()) {
269 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
270 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700271
272 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800273 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
274 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800275 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700276 }
277
Emilian Peevaebbe412018-01-15 13:53:24 +0000278 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700279 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800280 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700281 uint32_t loopCounter = 0;
282
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 for (auto&& request: requests) {
284 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700285 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800286 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
287 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800288 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800289 "No input configured for camera %s but request is for reprocessing",
290 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700291 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800292 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
293 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800294 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
295 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000296 } else if (request.mPhysicalCameraSettings.size() > 1) {
297 ALOGE("%s: Camera %s: reprocess requests not supported for "
298 "multiple physical cameras.", __FUNCTION__,
299 mCameraIdStr.string());
300 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
301 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700302 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700303 }
304
Emilian Peevaebbe412018-01-15 13:53:24 +0000305 if (request.mPhysicalCameraSettings.empty()) {
306 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
307 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800308 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000309 "Request doesn't contain any settings");
310 }
311
312 //The first capture settings should always match the logical camera id
313 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
314 if (mDevice->getId() != logicalId) {
315 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
316 mCameraIdStr.string());
317 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
318 "Invalid camera request settings");
319 }
320
Emilian Peevaebbe412018-01-15 13:53:24 +0000321 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800322 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
323 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800324 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
325 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700326 }
327
Jianing Wei90e59c92014-03-12 18:29:36 -0700328 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700329 * Write in the output stream IDs and map from stream ID to surface ID
330 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700331 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700332 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700333 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000334 std::vector<std::string> requestedPhysicalIds;
Emilian Peev2295df72021-11-12 18:14:10 -0800335 int dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800336 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800337 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800338 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700339
Emilian Peevf873aa52018-01-26 14:58:28 +0000340 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800341 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000342 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800343 if (!res.isOk()) {
344 return res;
345 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000346
347 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
348 if (index >= 0) {
349 String8 requestedPhysicalId(
350 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
351 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800352 dynamicProfileBitmap |=
353 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000354 } else {
355 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
356 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700357 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800358 } else {
359 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
360 int streamId = request.mStreamIdxList.itemAt(i);
361 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700362
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800363 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
364 if (index < 0) {
365 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
366 " we have not called createStream on: stream %d",
367 __FUNCTION__, mCameraIdStr.string(), streamId);
368 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
369 "Request targets Surface that is not part of current capture session");
370 }
371
372 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
373 if ((size_t)surfaceIdx >= gbps.size()) {
374 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
375 " we have not called createStream on: stream %d, surfaceIdx %d",
376 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
377 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
378 "Request targets Surface has invalid surface index");
379 }
380
Emilian Peevf873aa52018-01-26 14:58:28 +0000381 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800382 if (!res.isOk()) {
383 return res;
384 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000385
386 String8 requestedPhysicalId(
387 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
388 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800389 dynamicProfileBitmap |=
390 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
391 }
392 }
393
394 if (dynamicProfileBitmap !=
395 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
396 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
397 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
398 if ((dynamicProfileBitmap & i) == 0) {
399 continue;
400 }
401
402 const auto& it = mDynamicProfileMap.find(i);
403 if (it != mDynamicProfileMap.end()) {
404 if ((it->second == 0) ||
405 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
406 continue;
407 } else {
408 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
409 " reference an unsupported dynamic range profile combination"
410 " 0x%x!", __FUNCTION__, mCameraIdStr.string(),
411 dynamicProfileBitmap);
412 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
413 "Request targets an unsupported dynamic range profile"
414 " combination");
415 }
416 } else {
417 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
418 " references unsupported dynamic range profile 0x%x!",
419 __FUNCTION__, mCameraIdStr.string(), i);
420 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
421 "Request targets 10-bit Surface with unsupported dynamic range"
422 " profile");
423 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700424 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700425 }
426
Emilian Peevf873aa52018-01-26 14:58:28 +0000427 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
428 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000429 if (it.settings.isEmpty()) {
430 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
431 __FUNCTION__, mCameraIdStr.string());
432 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
433 "Request settings are empty");
434 }
435
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800436 // Check whether the physical / logical stream has settings
437 // consistent with the sensor pixel mode(s) it was configured with.
438 // mCameraIdToStreamSet will only have ids that are high resolution
439 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
440 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
441 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
442 outputStreamIds);
443 if (!request.mIsReprocess &&
444 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
445 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
446 "consistent with configured streams. Rejecting request.",
447 __FUNCTION__, it.id.c_str());
448 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
449 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
450 "streams configured");
451 }
452 }
453
Emilian Peevf873aa52018-01-26 14:58:28 +0000454 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700455 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
456 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
457 mSupportedPhysicalRequestKeys.end();
458 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
459 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
460 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000461 if (physicalId != mDevice->getId()) {
462 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
463 it.id);
464 if (found == requestedPhysicalIds.end()) {
465 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
466 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
467 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
468 "Invalid physical camera id");
469 }
Emilian Peev00420d22018-02-05 21:33:13 +0000470
471 if (!mSupportedPhysicalRequestKeys.empty()) {
472 // Filter out any unsupported physical request keys.
473 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
474 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
475 filteredParams.getAndLock());
476 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
477 filteredParams.unlock(meta);
478
479 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
480 camera_metadata_ro_entry entry = it.settings.find(keyIt);
481 if (entry.count > 0) {
482 filteredParams.update(entry);
483 }
484 }
485
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700486 physicalSettingsList.push_back({it.id, filteredParams,
487 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000488 }
489 } else {
490 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000491 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000492 }
493
494 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
495 // Callee logs
496 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
497 "Caller does not have permission to change restricted controls");
498 }
499
Emilian Peevaebbe412018-01-15 13:53:24 +0000500 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
501 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700502
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800503 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000504 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
505 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700506 }
507
Emilian Peevaebbe412018-01-15 13:53:24 +0000508 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
509 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700510 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800511 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
512 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
513 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700514
Emilian Peevaebbe412018-01-15 13:53:24 +0000515 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700516 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700517 }
518 mRequestIdCounter++;
519
520 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700521 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
522 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800523 if (err != OK) {
524 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800525 "Camera %s: Got error %s (%d) after trying to set streaming request",
526 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800527 ALOGE("%s: %s", __FUNCTION__, msg.string());
528 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
529 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700530 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700531 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700532 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700533 }
534 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700535 err = mDevice->captureList(metadataRequestList, surfaceMapList,
536 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537 if (err != OK) {
538 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800539 "Camera %s: Got error %s (%d) after trying to submit capture request",
540 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800541 ALOGE("%s: %s", __FUNCTION__, msg.string());
542 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
543 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700544 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700546 }
547
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800548 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700549 return res;
550}
551
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800552binder::Status CameraDeviceClient::cancelRequest(
553 int requestId,
554 /*out*/
555 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700556 ATRACE_CALL();
557 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
558
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800559 status_t err;
560 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700561
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800562 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700563
564 Mutex::Autolock icl(mBinderSerializationLock);
565
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800566 if (!mDevice.get()) {
567 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
568 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700569
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700570 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700571 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800572 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
573 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800574 ALOGE("%s: %s", __FUNCTION__, msg.string());
575 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700576 }
577
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800578 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700579
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800580 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800581 ALOGV("%s: Camera %s: Successfully cleared streaming request",
582 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700583 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800584 } else {
585 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800586 "Camera %s: Error clearing streaming request: %s (%d)",
587 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700588 }
589
590 return res;
591}
592
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800593binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700594 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700595 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700596 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700598}
599
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100600binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700601 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800602 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700603 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700604 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
605 __FUNCTION__, mInputStream.configured ? 1 : 0,
606 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700607
Zhijun He0effd522016-03-04 10:22:27 -0800608 binder::Status res;
609 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
610
Emilian Peevcc0b7952020-01-07 13:54:47 -0800611 if (offlineStreamIds == nullptr) {
612 String8 msg = String8::format("Invalid offline stream ids");
613 ALOGE("%s: %s", __FUNCTION__, msg.string());
614 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
615 }
616
Zhijun He0effd522016-03-04 10:22:27 -0800617 Mutex::Autolock icl(mBinderSerializationLock);
618
619 if (!mDevice.get()) {
620 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
621 }
622
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700623 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000624 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000625 if (!res.isOk()) {
626 return res;
627 }
628
629 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
630 if (err == BAD_VALUE) {
631 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
632 mCameraIdStr.string());
633 ALOGE("%s: %s", __FUNCTION__, msg.string());
634 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
635 } else if (err != OK) {
636 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
637 mCameraIdStr.string(), strerror(-err), err);
638 ALOGE("%s: %s", __FUNCTION__, msg.string());
639 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000640 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800641 offlineStreamIds->clear();
642 mDevice->getOfflineStreamIds(offlineStreamIds);
643
Emilian Peev2f5d6012022-01-19 16:16:50 -0800644 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000645 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
646 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800647 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000648 String8 msg = String8::format("Camera %s: Error configuring composite "
649 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
650 ALOGE("%s: %s", __FUNCTION__, msg.string());
651 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
652 break;
653 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800654
655 // Composite streams can only support offline mode in case all individual internal
656 // streams are also supported.
657 std::vector<int> internalStreams;
658 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800659 offlineStreamIds->erase(
660 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800661 [&internalStreams] (int streamId) {
662 auto it = std::find(internalStreams.begin(), internalStreams.end(),
663 streamId);
664 if (it != internalStreams.end()) {
665 internalStreams.erase(it);
666 return true;
667 }
668
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800669 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800670 if (internalStreams.empty()) {
671 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
672 }
673 }
674
675 for (const auto& offlineStreamId : *offlineStreamIds) {
676 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000677 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700678
679 nsecs_t configureEnd = systemTime();
680 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
681 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
682 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000683 }
684
685 return res;
686}
687
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800688binder::Status CameraDeviceClient::isSessionConfigurationSupported(
689 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800690
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800691 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800692 binder::Status res;
693 status_t ret = OK;
694 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
695
696 Mutex::Autolock icl(mBinderSerializationLock);
697
698 if (!mDevice.get()) {
699 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
700 }
701
702 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700703 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000704 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800705 if (!res.isOk()) {
706 return res;
707 }
708
709 if (status == nullptr) {
710 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
711 ALOGE("%s: %s", __FUNCTION__, msg.string());
712 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
713 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700714
Emilian Peev35ae8262018-11-08 13:11:32 +0000715 *status = false;
716 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Jayant Chowdhary0bd38522021-11-05 17:49:27 -0700717 sessionConfiguration, mOverrideForPerfClass, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000718 switch (ret) {
719 case OK:
720 // Expected, do nothing.
721 break;
722 case INVALID_OPERATION: {
723 String8 msg = String8::format(
724 "Camera %s: Session configuration query not supported!",
725 mCameraIdStr.string());
726 ALOGD("%s: %s", __FUNCTION__, msg.string());
727 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
728 }
729
730 break;
731 default: {
732 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
733 strerror(-ret), ret);
734 ALOGE("%s: %s", __FUNCTION__, msg.string());
735 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
736 msg.string());
737 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800738 }
739
740 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700741}
742
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800743binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700744 ATRACE_CALL();
745 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
746
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800747 binder::Status res;
748 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700749
750 Mutex::Autolock icl(mBinderSerializationLock);
751
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800752 if (!mDevice.get()) {
753 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
754 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700755
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700756 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700757 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700758 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000759 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700760
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700761 if (mInputStream.configured && mInputStream.id == streamId) {
762 isInput = true;
763 } else {
764 // Guard against trying to delete non-created streams
765 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700766 if (streamId == mStreamMap.valueAt(i).streamId()) {
767 surfaces.push_back(mStreamMap.keyAt(i));
768 }
769 }
770
771 // See if this stream is one of the deferred streams.
772 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
773 if (streamId == mDeferredStreams[i]) {
774 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700775 break;
776 }
777 }
778
Emilian Peev2f5d6012022-01-19 16:16:50 -0800779 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000780 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
781 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
782 compositeIndex = i;
783 break;
784 }
785 }
786
Shuzhen Wang0129d522016-10-30 22:43:41 -0700787 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
788 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
789 " stream created yet", mCameraIdStr.string(), streamId);
790 ALOGW("%s: %s", __FUNCTION__, msg.string());
791 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700792 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700793 }
794
795 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800796 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700797
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800798 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800799 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
800 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800801 ALOGE("%s: %s", __FUNCTION__, msg.string());
802 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
803 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700804 if (isInput) {
805 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700806 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700807 for (auto& surface : surfaces) {
808 mStreamMap.removeItem(surface);
809 }
810
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800811 mConfiguredOutputs.removeItem(streamId);
812
Shuzhen Wang0129d522016-10-30 22:43:41 -0700813 if (dIndex != NAME_NOT_FOUND) {
814 mDeferredStreams.removeItemsAt(dIndex);
815 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000816
817 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800818 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000819 status_t ret;
820 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
821 != OK) {
822 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
823 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
824 streamId);
825 ALOGE("%s: %s", __FUNCTION__, msg.string());
826 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
827 }
828 mCompositeStreamMap.removeItemsAt(compositeIndex);
829 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800830 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
831 auto &streamSet = mapIt.second;
832 if (streamSet.find(streamId) != streamSet.end()) {
833 streamSet.erase(streamId);
834 break;
835 }
836 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700837 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700838 }
839
840 return res;
841}
842
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800843binder::Status CameraDeviceClient::createStream(
844 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
845 /*out*/
846 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700847 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700848
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800849 binder::Status res;
850 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700851
852 Mutex::Autolock icl(mBinderSerializationLock);
853
Shuzhen Wang0129d522016-10-30 22:43:41 -0700854 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
855 outputConfiguration.getGraphicBufferProducers();
856 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800857 bool deferredConsumer = outputConfiguration.isDeferred();
858 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800859 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800860 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800861 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peev2295df72021-11-12 18:14:10 -0800862 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800863 int streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800864 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800865 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700866
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700867 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000868 outputConfiguration.getSurfaceType());
869 if (!res.isOk()) {
870 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700871 }
Zhijun He5d677d12016-05-29 16:52:39 -0700872
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800873 if (!mDevice.get()) {
874 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
875 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700876 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800877 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000878 if (!res.isOk()) {
879 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800880 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000881
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882 std::vector<sp<Surface>> surfaces;
883 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700884 status_t err;
885
886 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700887 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800888 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700889 }
890
Shuzhen Wang758c2152017-01-10 18:26:18 -0800891 OutputStreamInfo streamInfo;
892 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800893 const std::vector<int32_t> &sensorPixelModesUsed =
894 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700895 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700896 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700897 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800898 ssize_t index = mStreamMap.indexOfKey(binder);
899 if (index != NAME_NOT_FOUND) {
900 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
901 "(ID %zd)", mCameraIdStr.string(), index);
902 ALOGW("%s: %s", __FUNCTION__, msg.string());
903 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700904 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700905
Shuzhen Wang758c2152017-01-10 18:26:18 -0800906 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700907 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800908 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800909 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800910 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800911
912 if (!res.isOk())
913 return res;
914
915 if (!isStreamInfoValid) {
916 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700917 }
918
Shuzhen Wang758c2152017-01-10 18:26:18 -0800919 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700920 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800921 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700922
923 // If mOverrideForPerfClass is true, do not fail createStream() for small
924 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
925 // closest possible supported size.
926
Zhijun He125684a2015-12-26 15:07:30 -0800927 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100928 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800929 bool isDepthCompositeStream =
930 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800931 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
932 if (isDepthCompositeStream || isHeicCompisiteStream) {
933 sp<CompositeStream> compositeStream;
934 if (isDepthCompositeStream) {
935 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
936 } else {
937 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
938 }
939
Emilian Peev538c90e2018-12-17 18:03:19 +0000940 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
941 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700942 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800943 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
944 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000945 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800946 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000947 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
948 compositeStream);
949 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800950 } else {
951 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
952 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700953 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800954 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800955 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800956 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800957 streamInfo.timestampBase, streamInfo.mirrorMode);
Emilian Peev538c90e2018-12-17 18:03:19 +0000958 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700959
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800960 if (err != OK) {
961 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800962 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800963 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
964 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800965 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700966 int i = 0;
967 for (auto& binder : binders) {
968 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
969 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100970 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
971 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700972 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800973
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800974 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800975 mStreamInfoMap[streamId] = streamInfo;
976
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800977 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700978 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800979 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
980 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700981
Zhijun He5d677d12016-05-29 16:52:39 -0700982 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800983 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700984
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800985 // Fill in mHighResolutionCameraIdToStreamIdSet map
986 const String8 &cameraIdUsed =
987 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
988 const char *cameraIdUsedCStr = cameraIdUsed.string();
989 // Only needed for high resolution sensors
990 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
991 mHighResolutionSensors.end()) {
992 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
993 }
994
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800995 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700996 }
997
998 return res;
999}
1000
Zhijun He5d677d12016-05-29 16:52:39 -07001001binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1002 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001003 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001004 /*out*/
1005 int* newStreamId) {
1006 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001007 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001008 android_dataspace dataSpace;
1009 status_t err;
1010 binder::Status res;
1011
1012 if (!mDevice.get()) {
1013 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1014 }
1015
1016 // Infer the surface info for deferred surface stream creation.
1017 width = outputConfiguration.getWidth();
1018 height = outputConfiguration.getHeight();
1019 surfaceType = outputConfiguration.getSurfaceType();
1020 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1021 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1022 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1023 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1024 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1025 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1026 }
1027 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001028 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001029 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001030 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001031 const String8 &cameraIdUsed =
1032 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1033 // Here, we override sensor pixel modes
1034 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1035 const std::vector<int32_t> &sensorPixelModesUsed =
1036 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001037 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001038 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1039 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1040 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1041 "sensor pixel modes used not valid for deferred stream");
1042 }
1043
Shuzhen Wang0129d522016-10-30 22:43:41 -07001044 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1045 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001046 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001047 &streamId, physicalCameraId,
1048 overriddenSensorPixelModesUsed,
1049 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001050 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001051 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001052 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001053 outputConfiguration.getStreamUseCase(),
1054 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001055
1056 if (err != OK) {
1057 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001058 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1059 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001060 } else {
1061 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1062 // a separate list to track. Once the deferred surface is set, this id will be
1063 // relocated to mStreamMap.
1064 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001065 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001066 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001067 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001068 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001069 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001070 outputConfiguration.getTimestampBase(),
1071 outputConfiguration.getMirrorMode()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001072
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001073 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001074 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001075 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001076
1077 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001078 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001079
1080 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001081 // Fill in mHighResolutionCameraIdToStreamIdSet
1082 const char *cameraIdUsedCStr = cameraIdUsed.string();
1083 // Only needed for high resolution sensors
1084 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1085 mHighResolutionSensors.end()) {
1086 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1087 }
Zhijun He5d677d12016-05-29 16:52:39 -07001088 }
1089 return res;
1090}
1091
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001092binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001093 int32_t transform = 0;
1094 status_t err;
1095 binder::Status res;
1096
1097 if (!mDevice.get()) {
1098 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1099 }
1100
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001101 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001102 if (err != OK) {
1103 // Error logged by getRotationTransformLocked.
1104 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1105 "Unable to calculate rotation transform for new stream");
1106 }
1107
1108 err = mDevice->setStreamTransform(streamId, transform);
1109 if (err != OK) {
1110 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1111 streamId);
1112 ALOGE("%s: %s", __FUNCTION__, msg.string());
1113 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1114 }
1115
1116 return res;
1117}
Ruben Brunkbba75572014-11-20 17:29:50 -08001118
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001119binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001120 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001121 /*out*/
1122 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001123
1124 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001125 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1126 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001127
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001128 binder::Status res;
1129 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001130
1131 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001132
1133 if (!mDevice.get()) {
1134 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1135 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001136
1137 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001138 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001139 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001140 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1141 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001142 }
1143
1144 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001145 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001146 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001147 mInputStream.configured = true;
1148 mInputStream.width = width;
1149 mInputStream.height = height;
1150 mInputStream.format = format;
1151 mInputStream.id = streamId;
1152
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001153 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1154 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001155
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001156 *newStreamId = streamId;
1157 } else {
1158 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001159 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001160 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001161 }
1162
1163 return res;
1164}
1165
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001166binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001167
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001168 binder::Status res;
1169 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1170
1171 if (inputSurface == NULL) {
1172 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001173 }
1174
1175 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001176 if (!mDevice.get()) {
1177 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1178 }
1179 sp<IGraphicBufferProducer> producer;
1180 status_t err = mDevice->getInputBufferProducer(&producer);
1181 if (err != OK) {
1182 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001183 "Camera %s: Error getting input Surface: %s (%d)",
1184 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001185 } else {
1186 inputSurface->name = String16("CameraInput");
1187 inputSurface->graphicBufferProducer = producer;
1188 }
1189 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001190}
1191
Emilian Peev40ead602017-09-26 15:46:36 +01001192binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1193 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1194 ATRACE_CALL();
1195
1196 binder::Status res;
1197 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1198
1199 Mutex::Autolock icl(mBinderSerializationLock);
1200
1201 if (!mDevice.get()) {
1202 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1203 }
1204
1205 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1206 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001207 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1208
Emilian Peev40ead602017-09-26 15:46:36 +01001209 auto producerCount = bufferProducers.size();
1210 if (producerCount == 0) {
1211 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1212 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1213 "bufferProducers must not be empty");
1214 }
1215
1216 // The first output is the one associated with the output configuration.
1217 // It should always be present, valid and the corresponding stream id should match.
1218 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1219 ssize_t index = mStreamMap.indexOfKey(binder);
1220 if (index == NAME_NOT_FOUND) {
1221 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1222 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1223 "OutputConfiguration is invalid");
1224 }
1225 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1226 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1227 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1228 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1229 "Stream id is invalid");
1230 }
1231
1232 std::vector<size_t> removedSurfaceIds;
1233 std::vector<sp<IBinder>> removedOutputs;
1234 std::vector<sp<Surface>> newOutputs;
1235 std::vector<OutputStreamInfo> streamInfos;
1236 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1237 for (auto &it : bufferProducers) {
1238 newOutputsMap.add(IInterface::asBinder(it), it);
1239 }
1240
1241 for (size_t i = 0; i < mStreamMap.size(); i++) {
1242 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1243 if (idx == NAME_NOT_FOUND) {
1244 if (mStreamMap[i].streamId() == streamId) {
1245 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1246 removedOutputs.push_back(mStreamMap.keyAt(i));
1247 }
1248 } else {
1249 if (mStreamMap[i].streamId() != streamId) {
1250 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1251 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1252 "Target Surface is invalid");
1253 }
1254 newOutputsMap.removeItemsAt(idx);
1255 }
1256 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001257 const std::vector<int32_t> &sensorPixelModesUsed =
1258 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001259 int streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001260 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peev2295df72021-11-12 18:14:10 -08001261 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001262 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001263
Emilian Peev40ead602017-09-26 15:46:36 +01001264 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1265 OutputStreamInfo outInfo;
1266 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001267 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001268 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001269 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001270 streamUseCase, timestampBase, mirrorMode);
Emilian Peev40ead602017-09-26 15:46:36 +01001271 if (!res.isOk())
1272 return res;
1273
Emilian Peev40ead602017-09-26 15:46:36 +01001274 streamInfos.push_back(outInfo);
1275 newOutputs.push_back(surface);
1276 }
1277
1278 //Trivial case no changes required
1279 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1280 return binder::Status::ok();
1281 }
1282
1283 KeyedVector<sp<Surface>, size_t> outputMap;
1284 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1285 &outputMap);
1286 if (ret != OK) {
1287 switch (ret) {
1288 case NAME_NOT_FOUND:
1289 case BAD_VALUE:
1290 case -EBUSY:
1291 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1292 "Camera %s: Error updating stream: %s (%d)",
1293 mCameraIdStr.string(), strerror(ret), ret);
1294 break;
1295 default:
1296 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1297 "Camera %s: Error updating stream: %s (%d)",
1298 mCameraIdStr.string(), strerror(ret), ret);
1299 break;
1300 }
1301 } else {
1302 for (const auto &it : removedOutputs) {
1303 mStreamMap.removeItem(it);
1304 }
1305
1306 for (size_t i = 0; i < outputMap.size(); i++) {
1307 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1308 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1309 }
1310
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001311 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1312
Emilian Peev40ead602017-09-26 15:46:36 +01001313 ALOGV("%s: Camera %s: Successful stream ID %d update",
1314 __FUNCTION__, mCameraIdStr.string(), streamId);
1315 }
1316
1317 return res;
1318}
1319
Igor Murashkine7ee7632013-06-11 18:10:18 -07001320// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001321binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1322 /*out*/
1323 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001324{
1325 ATRACE_CALL();
1326 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1327
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001328 binder::Status res;
1329 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001330
1331 Mutex::Autolock icl(mBinderSerializationLock);
1332
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001333 if (!mDevice.get()) {
1334 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1335 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001336
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001337 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001338 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1339 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1340
1341 CameraMetadata metadata;
1342 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001343 request != NULL) {
1344
1345 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001346 } else if (err == BAD_VALUE) {
1347 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001348 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1349 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001350
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001351 } else {
1352 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001353 "Camera %s: Error creating default request for template %d: %s (%d)",
1354 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001355 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001356 return res;
1357}
1358
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001359binder::Status CameraDeviceClient::getCameraInfo(
1360 /*out*/
1361 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001362{
1363 ATRACE_CALL();
1364 ALOGV("%s", __FUNCTION__);
1365
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001366 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001367
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001369
1370 Mutex::Autolock icl(mBinderSerializationLock);
1371
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 if (!mDevice.get()) {
1373 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1374 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001375
Igor Murashkin099b4572013-07-12 17:52:16 -07001376 if (info != NULL) {
1377 *info = mDevice->info(); // static camera metadata
1378 // TODO: merge with device-specific camera metadata
1379 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001380
1381 return res;
1382}
1383
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001384binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001385{
1386 ATRACE_CALL();
1387 ALOGV("%s", __FUNCTION__);
1388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389 binder::Status res;
1390 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001391
1392 Mutex::Autolock icl(mBinderSerializationLock);
1393
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001394 if (!mDevice.get()) {
1395 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1396 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001397
1398 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001399 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001400 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001401 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001402 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1403 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001404 ALOGE("%s: %s", __FUNCTION__, msg.string());
1405 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001406 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001407 status_t err = mDevice->waitUntilDrained();
1408 if (err != OK) {
1409 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001410 "Camera %s: Error waiting to drain: %s (%d)",
1411 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001413 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001414 return res;
1415}
1416
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001417binder::Status CameraDeviceClient::flush(
1418 /*out*/
1419 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001420 ATRACE_CALL();
1421 ALOGV("%s", __FUNCTION__);
1422
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 binder::Status res;
1424 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001425
1426 Mutex::Autolock icl(mBinderSerializationLock);
1427
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001428 if (!mDevice.get()) {
1429 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1430 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001431
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001432 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001433 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001434 status_t err = mDevice->flush(lastFrameNumber);
1435 if (err != OK) {
1436 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001437 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001438 }
1439 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001440}
1441
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001443 ATRACE_CALL();
1444 ALOGV("%s", __FUNCTION__);
1445
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001446 binder::Status res;
1447 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001448
1449 Mutex::Autolock icl(mBinderSerializationLock);
1450
1451 // Guard against trying to prepare non-created streams
1452 ssize_t index = NAME_NOT_FOUND;
1453 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001454 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001455 index = i;
1456 break;
1457 }
1458 }
1459
1460 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001461 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1462 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001463 ALOGW("%s: %s", __FUNCTION__, msg.string());
1464 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001465 }
1466
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001467 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1468 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001469 status_t err = mDevice->prepare(streamId);
1470 if (err == BAD_VALUE) {
1471 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001472 "Camera %s: Stream %d has already been used, and cannot be prepared",
1473 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001474 } else if (err != OK) {
1475 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001476 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001477 strerror(-err), err);
1478 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001479 return res;
1480}
1481
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001482binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001483 ATRACE_CALL();
1484 ALOGV("%s", __FUNCTION__);
1485
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001486 binder::Status res;
1487 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001488
1489 Mutex::Autolock icl(mBinderSerializationLock);
1490
1491 // Guard against trying to prepare non-created streams
1492 ssize_t index = NAME_NOT_FOUND;
1493 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001494 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001495 index = i;
1496 break;
1497 }
1498 }
1499
1500 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001501 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1502 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001503 ALOGW("%s: %s", __FUNCTION__, msg.string());
1504 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001505 }
1506
1507 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001508 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1509 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001510 ALOGE("%s: %s", __FUNCTION__, msg.string());
1511 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001512 }
1513
1514 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1515 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001516 status_t err = mDevice->prepare(maxCount, streamId);
1517 if (err == BAD_VALUE) {
1518 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001519 "Camera %s: Stream %d has already been used, and cannot be prepared",
1520 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001521 } else if (err != OK) {
1522 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001523 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001524 strerror(-err), err);
1525 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001526
1527 return res;
1528}
1529
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001530binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001531 ATRACE_CALL();
1532 ALOGV("%s", __FUNCTION__);
1533
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001534 binder::Status res;
1535 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001536
1537 Mutex::Autolock icl(mBinderSerializationLock);
1538
1539 // Guard against trying to prepare non-created streams
1540 ssize_t index = NAME_NOT_FOUND;
1541 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001542 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001543 index = i;
1544 break;
1545 }
1546 }
1547
1548 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001549 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1550 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001551 ALOGW("%s: %s", __FUNCTION__, msg.string());
1552 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001553 }
1554
1555 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1556 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001557 status_t err = mDevice->tearDown(streamId);
1558 if (err == BAD_VALUE) {
1559 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001560 "Camera %s: Stream %d is still in use, cannot be torn down",
1561 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001562 } else if (err != OK) {
1563 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001564 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001565 strerror(-err), err);
1566 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001567
1568 return res;
1569}
1570
Shuzhen Wang758c2152017-01-10 18:26:18 -08001571binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001572 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1573 ATRACE_CALL();
1574
1575 binder::Status res;
1576 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1577
1578 Mutex::Autolock icl(mBinderSerializationLock);
1579
Shuzhen Wang0129d522016-10-30 22:43:41 -07001580 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1581 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001582 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001583
Shuzhen Wang0129d522016-10-30 22:43:41 -07001584 if (bufferProducers.size() == 0) {
1585 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001586 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1587 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001588
Shuzhen Wang758c2152017-01-10 18:26:18 -08001589 // streamId should be in mStreamMap if this stream already has a surface attached
1590 // to it. Otherwise, it should be in mDeferredStreams.
1591 bool streamIdConfigured = false;
1592 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1593 for (size_t i = 0; i < mStreamMap.size(); i++) {
1594 if (mStreamMap.valueAt(i).streamId() == streamId) {
1595 streamIdConfigured = true;
1596 break;
1597 }
Zhijun He5d677d12016-05-29 16:52:39 -07001598 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001599 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1600 if (streamId == mDeferredStreams[i]) {
1601 deferredStreamIndex = i;
1602 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001603 }
1604
Shuzhen Wang758c2152017-01-10 18:26:18 -08001605 }
1606 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1607 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1608 "(ID %d)", mCameraIdStr.string(), streamId);
1609 ALOGW("%s: %s", __FUNCTION__, msg.string());
1610 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001611 }
1612
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001613 if (mStreamInfoMap[streamId].finalized) {
1614 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1615 " on stream ID %d", mCameraIdStr.string(), streamId);
1616 ALOGW("%s: %s", __FUNCTION__, msg.string());
1617 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1618 }
1619
Zhijun He5d677d12016-05-29 16:52:39 -07001620 if (!mDevice.get()) {
1621 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1622 }
1623
Shuzhen Wang758c2152017-01-10 18:26:18 -08001624 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001625 const std::vector<int32_t> &sensorPixelModesUsed =
1626 outputConfiguration.getSensorPixelModesUsed();
Emilian Peev2295df72021-11-12 18:14:10 -08001627 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001628 int streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001629 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001630 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001631 for (auto& bufferProducer : bufferProducers) {
1632 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001633 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1634 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001635 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001636 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001637 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001638 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001639
1640 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001641 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001642 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001643 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001644 streamUseCase, timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001645
1646 if (!res.isOk())
1647 return res;
1648
1649 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001650 }
1651
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001652 // Gracefully handle case where finalizeOutputConfigurations is called
1653 // without any new surface.
1654 if (consumerSurfaces.size() == 0) {
1655 mStreamInfoMap[streamId].finalized = true;
1656 return res;
1657 }
1658
Zhijun He5d677d12016-05-29 16:52:39 -07001659 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001660 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001661 std::vector<int> consumerSurfaceIds;
1662 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001663 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001664 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1665 sp<IBinder> binder = IInterface::asBinder(
1666 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001667 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001668 binder.get(), streamId, consumerSurfaceIds[i]);
1669 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1670 }
1671 if (deferredStreamIndex != NAME_NOT_FOUND) {
1672 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001673 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001674 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001675 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001676 } else if (err == NO_INIT) {
1677 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001678 "Camera %s: Deferred surface is invalid: %s (%d)",
1679 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001680 } else {
1681 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001682 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1683 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001684 }
1685
1686 return res;
1687}
1688
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001689binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001690 ATRACE_CALL();
1691 binder::Status res;
1692 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1693
1694 if (!isValidAudioRestriction(mode)) {
1695 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1696 mCameraIdStr.string(), mode);
1697 ALOGW("%s: %s", __FUNCTION__, msg.string());
1698 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1699 }
1700
1701 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001702 BasicClient::setAudioRestriction(mode);
1703 return binder::Status::ok();
1704}
1705
1706binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1707 ATRACE_CALL();
1708 binder::Status res;
1709 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1710 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001711 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001712 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001713 }
1714 return binder::Status::ok();
1715}
1716
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001717status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1718 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1719
1720 return mDevice->setRotateAndCropAutoBehavior(
1721 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1722}
1723
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001724bool CameraDeviceClient::supportsCameraMute() {
1725 return mDevice->supportsCameraMute();
1726}
1727
1728status_t CameraDeviceClient::setCameraMute(bool enabled) {
1729 return mDevice->setCameraMute(enabled);
1730}
1731
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001732binder::Status CameraDeviceClient::switchToOffline(
1733 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001734 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001735 /*out*/
1736 sp<hardware::camera2::ICameraOfflineSession>* session) {
1737 ATRACE_CALL();
1738
1739 binder::Status res;
1740 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1741
1742 Mutex::Autolock icl(mBinderSerializationLock);
1743
1744 if (!mDevice.get()) {
1745 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1746 }
1747
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001748 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001749 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001750 ALOGE("%s: %s", __FUNCTION__, msg.string());
1751 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1752 }
1753
1754 if (session == nullptr) {
1755 String8 msg = String8::format("Invalid offline session");
1756 ALOGE("%s: %s", __FUNCTION__, msg.string());
1757 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1758 }
1759
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001760 std::vector<int32_t> offlineStreamIds;
1761 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001762 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001763 for (const auto& streamId : offlineOutputIds) {
1764 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001765 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001766 String8 msg = String8::format("Offline surface with id: %d is not registered",
1767 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001768 ALOGE("%s: %s", __FUNCTION__, msg.string());
1769 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1770 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001771
1772 if (!mStreamInfoMap[streamId].supportsOffline) {
1773 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1774 "offline mode", streamId);
1775 ALOGE("%s: %s", __FUNCTION__, msg.string());
1776 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1777 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001778
Emilian Peev2f5d6012022-01-19 16:16:50 -08001779 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001780 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001781 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001782 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001783 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001784 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001785 if (isCompositeStream) {
1786 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1787 if (compositeIdx == NAME_NOT_FOUND) {
1788 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1789 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1790 "Unknown composite stream");
1791 }
1792
1793 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1794 &offlineStreamIds);
1795 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1796 mCompositeStreamMap.valueAt(compositeIdx));
1797 break;
1798 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001799 }
1800
Emilian Peev4697b642019-11-19 17:11:14 -08001801 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001802 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001803 }
1804 }
1805
1806 sp<CameraOfflineSessionBase> offlineSession;
1807 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1808 if (ret != OK) {
1809 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1810 "Camera %s: Error switching to offline mode: %s (%d)",
1811 mCameraIdStr.string(), strerror(ret), ret);
1812 }
1813
Emilian Peevcc0b7952020-01-07 13:54:47 -08001814 sp<CameraOfflineSessionClient> offlineClient;
1815 if (offlineSession.get() != nullptr) {
1816 offlineClient = new CameraOfflineSessionClient(sCameraService,
1817 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001818 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1819 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001820 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1821 }
1822
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001823 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001824 // A successful offline session switch must reset the current camera client
1825 // and release any resources occupied by previously configured streams.
1826 mStreamMap.clear();
1827 mConfiguredOutputs.clear();
1828 mDeferredStreams.clear();
1829 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001830 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001831 mCompositeStreamMap.clear();
1832 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001833 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001834 switch(ret) {
1835 case BAD_VALUE:
1836 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1837 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1838 case TIMED_OUT:
1839 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1840 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1841 default:
1842 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1843 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1844 strerror(-ret), ret);
1845 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001846 }
1847
1848 *session = offlineClient;
1849
1850 return binder::Status::ok();
1851}
1852
Igor Murashkine7ee7632013-06-11 18:10:18 -07001853status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001854 return BasicClient::dump(fd, args);
1855}
1856
1857status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001858 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001859 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001860 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001861 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001862 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001863
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001864 dprintf(fd, " State:\n");
1865 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001866 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001867 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001868 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001869 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001870 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001871 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001872 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001873 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001874 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001875 mStreamMap.valueAt(i).streamId(),
1876 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001877 }
Zhijun He5d677d12016-05-29 16:52:39 -07001878 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001879 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001880 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001881 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001882 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001883 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001884 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001885 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001886 // TODO: print dynamic/request section from most recent requests
1887 mFrameProcessor->dump(fd, args);
1888
1889 return dumpDevice(fd, args);
1890}
1891
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001892status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1893 sp<CameraDeviceBase> device = mDevice;
1894 if (!device) {
1895 dprintf(out, " Device is detached.");
1896 return OK;
1897 }
1898 device->startWatchingTags(tags);
1899 return OK;
1900}
1901
1902status_t CameraDeviceClient::stopWatchingTags(int out) {
1903 sp<CameraDeviceBase> device = mDevice;
1904 if (!device) {
1905 dprintf(out, " Device is detached.");
1906 return OK;
1907 }
1908 device->stopWatchingTags();
1909 return OK;
1910}
1911
1912status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1913 sp<CameraDeviceBase> device = mDevice;
1914 if (!device) {
1915 return OK;
1916 }
1917 device->dumpWatchedEventsToVector(out);
1918 return OK;
1919}
1920
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001921void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001922 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001923 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001924 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001925
Emilian Peev538c90e2018-12-17 18:03:19 +00001926 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001927 {
1928 // Access to the composite stream map must be synchronized
1929 Mutex::Autolock l(mCompositeLock);
1930 // Composites can have multiple internal streams. Error notifications coming from such
1931 // internal streams may need to remain within camera service.
1932 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1933 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1934 resultExtras);
1935 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001936 }
1937
1938 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001939 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001940 }
1941}
1942
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001943void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1944 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1945
1946 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001947 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001948 }
1949
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001950 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001951 mStreamingRequestId = REQUEST_ID_NONE;
1952}
1953
Shuzhen Wang316781a2020-08-18 18:11:01 -07001954void CameraDeviceClient::notifyIdle(
1955 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1956 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001957 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001958 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001959
1960 if (remoteCb != 0) {
1961 remoteCb->onDeviceIdle();
1962 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001963 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001964}
1965
Jianing Weicb0652e2014-03-12 18:29:36 -07001966void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001967 nsecs_t timestamp) {
1968 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001969 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001970 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001971 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001972 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001973 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001974
Emilian Peev2f5d6012022-01-19 16:16:50 -08001975 // Access to the composite stream map must be synchronized
1976 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001977 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1978 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1979 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001980}
1981
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001982void CameraDeviceClient::notifyPrepared(int streamId) {
1983 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001984 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001985 if (remoteCb != 0) {
1986 remoteCb->onPrepared(streamId);
1987 }
1988}
1989
Shuzhen Wang9d066012016-09-30 11:30:20 -07001990void CameraDeviceClient::notifyRequestQueueEmpty() {
1991 // Thread safe. Don't bother locking.
1992 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1993 if (remoteCb != 0) {
1994 remoteCb->onRequestQueueEmpty();
1995 }
1996}
1997
Igor Murashkine7ee7632013-06-11 18:10:18 -07001998void CameraDeviceClient::detachDevice() {
1999 if (mDevice == 0) return;
2000
Shuzhen Wang316781a2020-08-18 18:11:01 -07002001 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002002 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002003
Emilian Peevfaa4bde2020-01-23 12:19:37 -08002004 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2005 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Igor Murashkine7ee7632013-06-11 18:10:18 -07002006 /*listener*/this);
2007 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002008 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002009 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002010 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002011
2012 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2013 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002014 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002015 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002016 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2017 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2018 }
2019
Igor Murashkine7ee7632013-06-11 18:10:18 -07002020 if ((code = mDevice->waitUntilDrained()) != OK) {
2021 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2022 code);
2023 }
2024 }
2025
Emilian Peev2f5d6012022-01-19 16:16:50 -08002026 {
2027 Mutex::Autolock l(mCompositeLock);
2028 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2029 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2030 if (ret != OK) {
2031 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2032 strerror(-ret), ret);
2033 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002034 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002035 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002036 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002037
Igor Murashkine7ee7632013-06-11 18:10:18 -07002038 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002039
2040 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2041 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002042}
2043
2044/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002045void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002046 ATRACE_CALL();
2047 ALOGV("%s", __FUNCTION__);
2048
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002049 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002050 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002051 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002052 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2053 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002054 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002055
Emilian Peev2f5d6012022-01-19 16:16:50 -08002056 // Access to the composite stream map must be synchronized
2057 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002058 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2059 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2060 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002061}
2062
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002063binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002064 if (mDisconnected) {
2065 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2066 "The camera device has been disconnected");
2067 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002068 status_t res = checkPid(checkLocation);
2069 return (res == OK) ? binder::Status::ok() :
2070 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2071 "Attempt to use camera from a different process than original client");
2072}
2073
Igor Murashkine7ee7632013-06-11 18:10:18 -07002074// TODO: move to Camera2ClientBase
2075bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2076
Jayant Chowdhary12361932018-08-27 14:46:13 -07002077 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002078 const int selfPid = getpid();
2079 camera_metadata_entry_t entry;
2080
2081 /**
2082 * Mixin default important security values
2083 * - android.led.transmit = defaulted ON
2084 */
2085 CameraMetadata staticInfo = mDevice->info();
2086 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2087 for(size_t i = 0; i < entry.count; ++i) {
2088 uint8_t led = entry.data.u8[i];
2089
2090 switch(led) {
2091 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2092 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2093 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2094 metadata.update(ANDROID_LED_TRANSMIT,
2095 &transmitDefault, 1);
2096 }
2097 break;
2098 }
2099 }
2100 }
2101
2102 // We can do anything!
2103 if (pid == selfPid) {
2104 return true;
2105 }
2106
2107 /**
2108 * Permission check special fields in the request
2109 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2110 */
2111 entry = metadata.find(ANDROID_LED_TRANSMIT);
2112 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2113 String16 permissionString =
2114 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2115 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002116 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002117 ALOGE("Permission Denial: "
2118 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2119 return false;
2120 }
2121 }
2122
2123 return true;
2124}
2125
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002126status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2127 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002128 ALOGV("%s: begin", __FUNCTION__);
2129
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002130 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002131 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002132}
2133
Emilian Peevf4816702020-04-03 15:44:51 -07002134binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2135 camera_request_template_t* tempId /*out*/) {
2136 binder::Status ret = binder::Status::ok();
2137
2138 if (tempId == nullptr) {
2139 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2140 "Camera %s: Invalid template argument", mCameraIdStr.string());
2141 return ret;
2142 }
2143 switch(templateId) {
2144 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2145 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2146 break;
2147 case ICameraDeviceUser::TEMPLATE_RECORD:
2148 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2149 break;
2150 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2151 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2152 break;
2153 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2154 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2155 break;
2156 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2157 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2158 break;
2159 case ICameraDeviceUser::TEMPLATE_MANUAL:
2160 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2161 break;
2162 default:
2163 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2164 "Camera %s: Template ID %d is invalid or not supported",
2165 mCameraIdStr.string(), templateId);
2166 return ret;
2167 }
2168
2169 return ret;
2170}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002171
2172const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2173 if (mDevice->getId() == cameraId) {
2174 return mDevice->info();
2175 }
2176 return mDevice->infoPhysical(cameraId);
2177}
2178
2179bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2180 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002181 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002182}
2183
2184bool CameraDeviceClient::isSensorPixelModeConsistent(
2185 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2186 // First we get the sensorPixelMode from the settings metadata.
2187 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2188 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2189 if (sensorPixelModeEntry.count != 0) {
2190 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2191 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2192 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2193 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2194 __FUNCTION__, sensorPixelMode);
2195 return false;
2196 }
2197 }
2198 // Check whether each stream has max resolution allowed.
2199 bool consistent = true;
2200 for (auto it : streamIdList) {
2201 auto const streamInfoIt = mStreamInfoMap.find(it);
2202 if (streamInfoIt == mStreamInfoMap.end()) {
2203 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2204 return false;
2205 }
2206 consistent =
2207 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2208 streamInfoIt->second.sensorPixelModesUsed.end();
2209 if (!consistent) {
2210 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2211 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2212 ALOGE("sensor pixel mode used list: %i", m);
2213 }
2214 break;
2215 }
2216 }
2217
2218 return consistent;
2219}
2220
Igor Murashkine7ee7632013-06-11 18:10:18 -07002221} // namespace android