blob: d26b73007c8f424915df0421a7efd65c925a3e9e [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 Wang0129d522016-10-30 22:43:41 -0700865
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700866 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000867 outputConfiguration.getSurfaceType());
868 if (!res.isOk()) {
869 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700870 }
Zhijun He5d677d12016-05-29 16:52:39 -0700871
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800872 if (!mDevice.get()) {
873 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
874 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700875 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800876 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000877 if (!res.isOk()) {
878 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800879 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000880
Shuzhen Wang0129d522016-10-30 22:43:41 -0700881 std::vector<sp<Surface>> surfaces;
882 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700883 status_t err;
884
885 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700886 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800887 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700888 }
889
Shuzhen Wang758c2152017-01-10 18:26:18 -0800890 OutputStreamInfo streamInfo;
891 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800892 const std::vector<int32_t> &sensorPixelModesUsed =
893 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700894 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700895 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700896 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800897 ssize_t index = mStreamMap.indexOfKey(binder);
898 if (index != NAME_NOT_FOUND) {
899 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
900 "(ID %zd)", mCameraIdStr.string(), index);
901 ALOGW("%s: %s", __FUNCTION__, msg.string());
902 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700903 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700904
Shuzhen Wang758c2152017-01-10 18:26:18 -0800905 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700906 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800907 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800908 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wange4208922022-02-01 16:52:48 -0800909 streamUseCase, timestampBase);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800910
911 if (!res.isOk())
912 return res;
913
914 if (!isStreamInfoValid) {
915 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700916 }
917
Shuzhen Wang758c2152017-01-10 18:26:18 -0800918 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700919 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800920 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700921
922 // If mOverrideForPerfClass is true, do not fail createStream() for small
923 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
924 // closest possible supported size.
925
Zhijun He125684a2015-12-26 15:07:30 -0800926 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100927 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800928 bool isDepthCompositeStream =
929 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800930 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
931 if (isDepthCompositeStream || isHeicCompisiteStream) {
932 sp<CompositeStream> compositeStream;
933 if (isDepthCompositeStream) {
934 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
935 } else {
936 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
937 }
938
Emilian Peev538c90e2018-12-17 18:03:19 +0000939 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
940 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700941 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800942 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
943 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000944 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800945 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000946 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
947 compositeStream);
948 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800949 } else {
950 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
951 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700952 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800953 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800954 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800955 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
956 streamInfo.timestampBase);
Emilian Peev538c90e2018-12-17 18:03:19 +0000957 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700958
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800959 if (err != OK) {
960 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800961 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800962 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
963 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800964 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700965 int i = 0;
966 for (auto& binder : binders) {
967 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
968 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100969 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
970 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700971 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800972
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800973 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800974 mStreamInfoMap[streamId] = streamInfo;
975
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800976 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700977 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800978 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
979 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700980
Zhijun He5d677d12016-05-29 16:52:39 -0700981 // Set transform flags to ensure preview to be rotated correctly.
982 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700983
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800984 // Fill in mHighResolutionCameraIdToStreamIdSet map
985 const String8 &cameraIdUsed =
986 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
987 const char *cameraIdUsedCStr = cameraIdUsed.string();
988 // Only needed for high resolution sensors
989 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
990 mHighResolutionSensors.end()) {
991 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
992 }
993
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800994 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700995 }
996
997 return res;
998}
999
Zhijun He5d677d12016-05-29 16:52:39 -07001000binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1001 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001002 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001003 /*out*/
1004 int* newStreamId) {
1005 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001006 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001007 android_dataspace dataSpace;
1008 status_t err;
1009 binder::Status res;
1010
1011 if (!mDevice.get()) {
1012 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1013 }
1014
1015 // Infer the surface info for deferred surface stream creation.
1016 width = outputConfiguration.getWidth();
1017 height = outputConfiguration.getHeight();
1018 surfaceType = outputConfiguration.getSurfaceType();
1019 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1020 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
1021 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1022 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1023 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1024 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1025 }
1026 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001027 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001028 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001029 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001030 const String8 &cameraIdUsed =
1031 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1032 // Here, we override sensor pixel modes
1033 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1034 const std::vector<int32_t> &sensorPixelModesUsed =
1035 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001036 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001037 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1038 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1039 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1040 "sensor pixel modes used not valid for deferred stream");
1041 }
1042
Shuzhen Wang0129d522016-10-30 22:43:41 -07001043 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1044 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001045 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001046 &streamId, physicalCameraId,
1047 overriddenSensorPixelModesUsed,
1048 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001049 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001050 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001051 outputConfiguration.getDynamicRangeProfile(),
1052 outputConfiguration.getStreamUseCase());
Zhijun He5d677d12016-05-29 16:52:39 -07001053
1054 if (err != OK) {
1055 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001056 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1057 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001058 } else {
1059 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1060 // a separate list to track. Once the deferred surface is set, this id will be
1061 // relocated to mStreamMap.
1062 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001063 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001064 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001065 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001066 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001067 outputConfiguration.getStreamUseCase(),
1068 outputConfiguration.getTimestampBase()));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001069
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001070 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001071 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001072 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001073
1074 // Set transform flags to ensure preview to be rotated correctly.
1075 res = setStreamTransformLocked(streamId);
1076
1077 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001078 // Fill in mHighResolutionCameraIdToStreamIdSet
1079 const char *cameraIdUsedCStr = cameraIdUsed.string();
1080 // Only needed for high resolution sensors
1081 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1082 mHighResolutionSensors.end()) {
1083 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1084 }
Zhijun He5d677d12016-05-29 16:52:39 -07001085 }
1086 return res;
1087}
1088
1089binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
1090 int32_t transform = 0;
1091 status_t err;
1092 binder::Status res;
1093
1094 if (!mDevice.get()) {
1095 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1096 }
1097
1098 err = getRotationTransformLocked(&transform);
1099 if (err != OK) {
1100 // Error logged by getRotationTransformLocked.
1101 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1102 "Unable to calculate rotation transform for new stream");
1103 }
1104
1105 err = mDevice->setStreamTransform(streamId, transform);
1106 if (err != OK) {
1107 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1108 streamId);
1109 ALOGE("%s: %s", __FUNCTION__, msg.string());
1110 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1111 }
1112
1113 return res;
1114}
Ruben Brunkbba75572014-11-20 17:29:50 -08001115
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001116binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001117 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001118 /*out*/
1119 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001120
1121 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001122 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1123 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001124
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001125 binder::Status res;
1126 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001127
1128 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001129
1130 if (!mDevice.get()) {
1131 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1132 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001133
1134 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001135 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001136 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001137 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1138 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001139 }
1140
1141 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001142 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001143 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001144 mInputStream.configured = true;
1145 mInputStream.width = width;
1146 mInputStream.height = height;
1147 mInputStream.format = format;
1148 mInputStream.id = streamId;
1149
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001150 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1151 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001152
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001153 *newStreamId = streamId;
1154 } else {
1155 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001156 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001157 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001158 }
1159
1160 return res;
1161}
1162
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001163binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001164
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001165 binder::Status res;
1166 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1167
1168 if (inputSurface == NULL) {
1169 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001170 }
1171
1172 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001173 if (!mDevice.get()) {
1174 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1175 }
1176 sp<IGraphicBufferProducer> producer;
1177 status_t err = mDevice->getInputBufferProducer(&producer);
1178 if (err != OK) {
1179 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001180 "Camera %s: Error getting input Surface: %s (%d)",
1181 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001182 } else {
1183 inputSurface->name = String16("CameraInput");
1184 inputSurface->graphicBufferProducer = producer;
1185 }
1186 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001187}
1188
Emilian Peev40ead602017-09-26 15:46:36 +01001189binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1190 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1191 ATRACE_CALL();
1192
1193 binder::Status res;
1194 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1195
1196 Mutex::Autolock icl(mBinderSerializationLock);
1197
1198 if (!mDevice.get()) {
1199 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1200 }
1201
1202 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1203 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001204 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1205
Emilian Peev40ead602017-09-26 15:46:36 +01001206 auto producerCount = bufferProducers.size();
1207 if (producerCount == 0) {
1208 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1209 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1210 "bufferProducers must not be empty");
1211 }
1212
1213 // The first output is the one associated with the output configuration.
1214 // It should always be present, valid and the corresponding stream id should match.
1215 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1216 ssize_t index = mStreamMap.indexOfKey(binder);
1217 if (index == NAME_NOT_FOUND) {
1218 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1219 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1220 "OutputConfiguration is invalid");
1221 }
1222 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1223 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1224 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1225 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1226 "Stream id is invalid");
1227 }
1228
1229 std::vector<size_t> removedSurfaceIds;
1230 std::vector<sp<IBinder>> removedOutputs;
1231 std::vector<sp<Surface>> newOutputs;
1232 std::vector<OutputStreamInfo> streamInfos;
1233 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1234 for (auto &it : bufferProducers) {
1235 newOutputsMap.add(IInterface::asBinder(it), it);
1236 }
1237
1238 for (size_t i = 0; i < mStreamMap.size(); i++) {
1239 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1240 if (idx == NAME_NOT_FOUND) {
1241 if (mStreamMap[i].streamId() == streamId) {
1242 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1243 removedOutputs.push_back(mStreamMap.keyAt(i));
1244 }
1245 } else {
1246 if (mStreamMap[i].streamId() != streamId) {
1247 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1248 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1249 "Target Surface is invalid");
1250 }
1251 newOutputsMap.removeItemsAt(idx);
1252 }
1253 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001254 const std::vector<int32_t> &sensorPixelModesUsed =
1255 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001256 int streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001257 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peev2295df72021-11-12 18:14:10 -08001258 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
1259
Emilian Peev40ead602017-09-26 15:46:36 +01001260 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1261 OutputStreamInfo outInfo;
1262 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001263 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001264 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001265 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wange4208922022-02-01 16:52:48 -08001266 streamUseCase, timestampBase);
Emilian Peev40ead602017-09-26 15:46:36 +01001267 if (!res.isOk())
1268 return res;
1269
Emilian Peev40ead602017-09-26 15:46:36 +01001270 streamInfos.push_back(outInfo);
1271 newOutputs.push_back(surface);
1272 }
1273
1274 //Trivial case no changes required
1275 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1276 return binder::Status::ok();
1277 }
1278
1279 KeyedVector<sp<Surface>, size_t> outputMap;
1280 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1281 &outputMap);
1282 if (ret != OK) {
1283 switch (ret) {
1284 case NAME_NOT_FOUND:
1285 case BAD_VALUE:
1286 case -EBUSY:
1287 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1288 "Camera %s: Error updating stream: %s (%d)",
1289 mCameraIdStr.string(), strerror(ret), ret);
1290 break;
1291 default:
1292 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1293 "Camera %s: Error updating stream: %s (%d)",
1294 mCameraIdStr.string(), strerror(ret), ret);
1295 break;
1296 }
1297 } else {
1298 for (const auto &it : removedOutputs) {
1299 mStreamMap.removeItem(it);
1300 }
1301
1302 for (size_t i = 0; i < outputMap.size(); i++) {
1303 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1304 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1305 }
1306
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001307 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1308
Emilian Peev40ead602017-09-26 15:46:36 +01001309 ALOGV("%s: Camera %s: Successful stream ID %d update",
1310 __FUNCTION__, mCameraIdStr.string(), streamId);
1311 }
1312
1313 return res;
1314}
1315
Igor Murashkine7ee7632013-06-11 18:10:18 -07001316// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001317binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1318 /*out*/
1319 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001320{
1321 ATRACE_CALL();
1322 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1323
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001324 binder::Status res;
1325 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001326
1327 Mutex::Autolock icl(mBinderSerializationLock);
1328
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001329 if (!mDevice.get()) {
1330 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1331 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001332
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001333 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001334 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1335 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1336
1337 CameraMetadata metadata;
1338 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001339 request != NULL) {
1340
1341 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001342 } else if (err == BAD_VALUE) {
1343 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001344 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1345 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001346
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001347 } else {
1348 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001349 "Camera %s: Error creating default request for template %d: %s (%d)",
1350 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001351 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001352 return res;
1353}
1354
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001355binder::Status CameraDeviceClient::getCameraInfo(
1356 /*out*/
1357 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001358{
1359 ATRACE_CALL();
1360 ALOGV("%s", __FUNCTION__);
1361
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001362 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001363
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001364 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001365
1366 Mutex::Autolock icl(mBinderSerializationLock);
1367
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001368 if (!mDevice.get()) {
1369 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1370 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001371
Igor Murashkin099b4572013-07-12 17:52:16 -07001372 if (info != NULL) {
1373 *info = mDevice->info(); // static camera metadata
1374 // TODO: merge with device-specific camera metadata
1375 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001376
1377 return res;
1378}
1379
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001381{
1382 ATRACE_CALL();
1383 ALOGV("%s", __FUNCTION__);
1384
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001385 binder::Status res;
1386 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001387
1388 Mutex::Autolock icl(mBinderSerializationLock);
1389
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001390 if (!mDevice.get()) {
1391 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1392 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001393
1394 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001395 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001396 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001397 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001398 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1399 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001400 ALOGE("%s: %s", __FUNCTION__, msg.string());
1401 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001402 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001403 status_t err = mDevice->waitUntilDrained();
1404 if (err != OK) {
1405 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001406 "Camera %s: Error waiting to drain: %s (%d)",
1407 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001408 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001409 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001410 return res;
1411}
1412
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001413binder::Status CameraDeviceClient::flush(
1414 /*out*/
1415 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001416 ATRACE_CALL();
1417 ALOGV("%s", __FUNCTION__);
1418
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001419 binder::Status res;
1420 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001421
1422 Mutex::Autolock icl(mBinderSerializationLock);
1423
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001424 if (!mDevice.get()) {
1425 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1426 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001427
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001428 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001429 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001430 status_t err = mDevice->flush(lastFrameNumber);
1431 if (err != OK) {
1432 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001433 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001434 }
1435 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001436}
1437
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001438binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001439 ATRACE_CALL();
1440 ALOGV("%s", __FUNCTION__);
1441
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442 binder::Status res;
1443 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001444
1445 Mutex::Autolock icl(mBinderSerializationLock);
1446
1447 // Guard against trying to prepare non-created streams
1448 ssize_t index = NAME_NOT_FOUND;
1449 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001450 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001451 index = i;
1452 break;
1453 }
1454 }
1455
1456 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001457 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1458 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001459 ALOGW("%s: %s", __FUNCTION__, msg.string());
1460 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001461 }
1462
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001463 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1464 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001465 status_t err = mDevice->prepare(streamId);
1466 if (err == BAD_VALUE) {
1467 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001468 "Camera %s: Stream %d has already been used, and cannot be prepared",
1469 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001470 } else if (err != OK) {
1471 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001472 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001473 strerror(-err), err);
1474 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001475 return res;
1476}
1477
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001478binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001479 ATRACE_CALL();
1480 ALOGV("%s", __FUNCTION__);
1481
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001482 binder::Status res;
1483 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001484
1485 Mutex::Autolock icl(mBinderSerializationLock);
1486
1487 // Guard against trying to prepare non-created streams
1488 ssize_t index = NAME_NOT_FOUND;
1489 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001490 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001491 index = i;
1492 break;
1493 }
1494 }
1495
1496 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001497 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1498 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001499 ALOGW("%s: %s", __FUNCTION__, msg.string());
1500 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001501 }
1502
1503 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001504 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1505 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001506 ALOGE("%s: %s", __FUNCTION__, msg.string());
1507 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001508 }
1509
1510 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1511 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001512 status_t err = mDevice->prepare(maxCount, streamId);
1513 if (err == BAD_VALUE) {
1514 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001515 "Camera %s: Stream %d has already been used, and cannot be prepared",
1516 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001517 } else if (err != OK) {
1518 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001519 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001520 strerror(-err), err);
1521 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001522
1523 return res;
1524}
1525
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001526binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001527 ATRACE_CALL();
1528 ALOGV("%s", __FUNCTION__);
1529
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001530 binder::Status res;
1531 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001532
1533 Mutex::Autolock icl(mBinderSerializationLock);
1534
1535 // Guard against trying to prepare non-created streams
1536 ssize_t index = NAME_NOT_FOUND;
1537 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001538 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001539 index = i;
1540 break;
1541 }
1542 }
1543
1544 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001545 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1546 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001547 ALOGW("%s: %s", __FUNCTION__, msg.string());
1548 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001549 }
1550
1551 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1552 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001553 status_t err = mDevice->tearDown(streamId);
1554 if (err == BAD_VALUE) {
1555 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001556 "Camera %s: Stream %d is still in use, cannot be torn down",
1557 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001558 } else if (err != OK) {
1559 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001560 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001561 strerror(-err), err);
1562 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001563
1564 return res;
1565}
1566
Shuzhen Wang758c2152017-01-10 18:26:18 -08001567binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001568 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1569 ATRACE_CALL();
1570
1571 binder::Status res;
1572 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1573
1574 Mutex::Autolock icl(mBinderSerializationLock);
1575
Shuzhen Wang0129d522016-10-30 22:43:41 -07001576 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1577 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001578 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001579
Shuzhen Wang0129d522016-10-30 22:43:41 -07001580 if (bufferProducers.size() == 0) {
1581 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001582 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1583 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001584
Shuzhen Wang758c2152017-01-10 18:26:18 -08001585 // streamId should be in mStreamMap if this stream already has a surface attached
1586 // to it. Otherwise, it should be in mDeferredStreams.
1587 bool streamIdConfigured = false;
1588 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1589 for (size_t i = 0; i < mStreamMap.size(); i++) {
1590 if (mStreamMap.valueAt(i).streamId() == streamId) {
1591 streamIdConfigured = true;
1592 break;
1593 }
Zhijun He5d677d12016-05-29 16:52:39 -07001594 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001595 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1596 if (streamId == mDeferredStreams[i]) {
1597 deferredStreamIndex = i;
1598 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001599 }
1600
Shuzhen Wang758c2152017-01-10 18:26:18 -08001601 }
1602 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1603 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1604 "(ID %d)", mCameraIdStr.string(), streamId);
1605 ALOGW("%s: %s", __FUNCTION__, msg.string());
1606 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001607 }
1608
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001609 if (mStreamInfoMap[streamId].finalized) {
1610 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1611 " on stream ID %d", mCameraIdStr.string(), streamId);
1612 ALOGW("%s: %s", __FUNCTION__, msg.string());
1613 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1614 }
1615
Zhijun He5d677d12016-05-29 16:52:39 -07001616 if (!mDevice.get()) {
1617 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1618 }
1619
Shuzhen Wang758c2152017-01-10 18:26:18 -08001620 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001621 const std::vector<int32_t> &sensorPixelModesUsed =
1622 outputConfiguration.getSensorPixelModesUsed();
Emilian Peev2295df72021-11-12 18:14:10 -08001623 int dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001624 int streamUseCase= outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001625 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001626 for (auto& bufferProducer : bufferProducers) {
1627 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001628 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1629 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001630 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001631 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001632 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001633 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001634
1635 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001636 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001637 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001638 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Shuzhen Wange4208922022-02-01 16:52:48 -08001639 streamUseCase, timestampBase);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001640
1641 if (!res.isOk())
1642 return res;
1643
1644 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001645 }
1646
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001647 // Gracefully handle case where finalizeOutputConfigurations is called
1648 // without any new surface.
1649 if (consumerSurfaces.size() == 0) {
1650 mStreamInfoMap[streamId].finalized = true;
1651 return res;
1652 }
1653
Zhijun He5d677d12016-05-29 16:52:39 -07001654 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001655 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001656 std::vector<int> consumerSurfaceIds;
1657 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001658 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001659 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1660 sp<IBinder> binder = IInterface::asBinder(
1661 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001662 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001663 binder.get(), streamId, consumerSurfaceIds[i]);
1664 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1665 }
1666 if (deferredStreamIndex != NAME_NOT_FOUND) {
1667 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001668 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001669 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001670 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001671 } else if (err == NO_INIT) {
1672 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001673 "Camera %s: Deferred surface is invalid: %s (%d)",
1674 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001675 } else {
1676 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001677 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1678 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001679 }
1680
1681 return res;
1682}
1683
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001684binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001685 ATRACE_CALL();
1686 binder::Status res;
1687 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1688
1689 if (!isValidAudioRestriction(mode)) {
1690 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1691 mCameraIdStr.string(), mode);
1692 ALOGW("%s: %s", __FUNCTION__, msg.string());
1693 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1694 }
1695
1696 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001697 BasicClient::setAudioRestriction(mode);
1698 return binder::Status::ok();
1699}
1700
1701binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1702 ATRACE_CALL();
1703 binder::Status res;
1704 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1705 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001706 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001707 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001708 }
1709 return binder::Status::ok();
1710}
1711
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001712status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1713 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1714
1715 return mDevice->setRotateAndCropAutoBehavior(
1716 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1717}
1718
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001719bool CameraDeviceClient::supportsCameraMute() {
1720 return mDevice->supportsCameraMute();
1721}
1722
1723status_t CameraDeviceClient::setCameraMute(bool enabled) {
1724 return mDevice->setCameraMute(enabled);
1725}
1726
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001727binder::Status CameraDeviceClient::switchToOffline(
1728 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001729 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001730 /*out*/
1731 sp<hardware::camera2::ICameraOfflineSession>* session) {
1732 ATRACE_CALL();
1733
1734 binder::Status res;
1735 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1736
1737 Mutex::Autolock icl(mBinderSerializationLock);
1738
1739 if (!mDevice.get()) {
1740 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1741 }
1742
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001743 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001744 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001745 ALOGE("%s: %s", __FUNCTION__, msg.string());
1746 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1747 }
1748
1749 if (session == nullptr) {
1750 String8 msg = String8::format("Invalid offline session");
1751 ALOGE("%s: %s", __FUNCTION__, msg.string());
1752 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1753 }
1754
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001755 std::vector<int32_t> offlineStreamIds;
1756 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001757 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001758 for (const auto& streamId : offlineOutputIds) {
1759 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001760 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001761 String8 msg = String8::format("Offline surface with id: %d is not registered",
1762 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001763 ALOGE("%s: %s", __FUNCTION__, msg.string());
1764 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1765 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001766
1767 if (!mStreamInfoMap[streamId].supportsOffline) {
1768 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1769 "offline mode", streamId);
1770 ALOGE("%s: %s", __FUNCTION__, msg.string());
1771 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1772 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001773
Emilian Peev2f5d6012022-01-19 16:16:50 -08001774 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001775 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001776 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001777 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001778 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001779 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001780 if (isCompositeStream) {
1781 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1782 if (compositeIdx == NAME_NOT_FOUND) {
1783 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1784 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1785 "Unknown composite stream");
1786 }
1787
1788 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1789 &offlineStreamIds);
1790 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1791 mCompositeStreamMap.valueAt(compositeIdx));
1792 break;
1793 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001794 }
1795
Emilian Peev4697b642019-11-19 17:11:14 -08001796 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001797 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001798 }
1799 }
1800
1801 sp<CameraOfflineSessionBase> offlineSession;
1802 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1803 if (ret != OK) {
1804 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1805 "Camera %s: Error switching to offline mode: %s (%d)",
1806 mCameraIdStr.string(), strerror(ret), ret);
1807 }
1808
Emilian Peevcc0b7952020-01-07 13:54:47 -08001809 sp<CameraOfflineSessionClient> offlineClient;
1810 if (offlineSession.get() != nullptr) {
1811 offlineClient = new CameraOfflineSessionClient(sCameraService,
1812 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001813 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1814 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001815 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1816 }
1817
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001818 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001819 // A successful offline session switch must reset the current camera client
1820 // and release any resources occupied by previously configured streams.
1821 mStreamMap.clear();
1822 mConfiguredOutputs.clear();
1823 mDeferredStreams.clear();
1824 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001825 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001826 mCompositeStreamMap.clear();
1827 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001828 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001829 switch(ret) {
1830 case BAD_VALUE:
1831 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1832 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1833 case TIMED_OUT:
1834 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1835 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1836 default:
1837 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1838 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1839 strerror(-ret), ret);
1840 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001841 }
1842
1843 *session = offlineClient;
1844
1845 return binder::Status::ok();
1846}
1847
Igor Murashkine7ee7632013-06-11 18:10:18 -07001848status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001849 return BasicClient::dump(fd, args);
1850}
1851
1852status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001853 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001854 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001855 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001856 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001857 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001858
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001859 dprintf(fd, " State:\n");
1860 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001861 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001862 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001863 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001864 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001865 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001866 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001867 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001868 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001869 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001870 mStreamMap.valueAt(i).streamId(),
1871 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001872 }
Zhijun He5d677d12016-05-29 16:52:39 -07001873 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001874 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001875 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001876 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001877 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001878 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001879 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001880 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001881 // TODO: print dynamic/request section from most recent requests
1882 mFrameProcessor->dump(fd, args);
1883
1884 return dumpDevice(fd, args);
1885}
1886
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001887status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1888 sp<CameraDeviceBase> device = mDevice;
1889 if (!device) {
1890 dprintf(out, " Device is detached.");
1891 return OK;
1892 }
1893 device->startWatchingTags(tags);
1894 return OK;
1895}
1896
1897status_t CameraDeviceClient::stopWatchingTags(int out) {
1898 sp<CameraDeviceBase> device = mDevice;
1899 if (!device) {
1900 dprintf(out, " Device is detached.");
1901 return OK;
1902 }
1903 device->stopWatchingTags();
1904 return OK;
1905}
1906
1907status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1908 sp<CameraDeviceBase> device = mDevice;
1909 if (!device) {
1910 return OK;
1911 }
1912 device->dumpWatchedEventsToVector(out);
1913 return OK;
1914}
1915
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001916void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001917 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001918 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001919 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001920
Emilian Peev538c90e2018-12-17 18:03:19 +00001921 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001922 {
1923 // Access to the composite stream map must be synchronized
1924 Mutex::Autolock l(mCompositeLock);
1925 // Composites can have multiple internal streams. Error notifications coming from such
1926 // internal streams may need to remain within camera service.
1927 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1928 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1929 resultExtras);
1930 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001931 }
1932
1933 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001934 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001935 }
1936}
1937
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001938void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1939 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1940
1941 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001942 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001943 }
1944
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001945 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001946 mStreamingRequestId = REQUEST_ID_NONE;
1947}
1948
Shuzhen Wang316781a2020-08-18 18:11:01 -07001949void CameraDeviceClient::notifyIdle(
1950 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1951 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001953 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001954
1955 if (remoteCb != 0) {
1956 remoteCb->onDeviceIdle();
1957 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001958 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001959}
1960
Jianing Weicb0652e2014-03-12 18:29:36 -07001961void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001962 nsecs_t timestamp) {
1963 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001964 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001965 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001966 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001967 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001968 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001969
Emilian Peev2f5d6012022-01-19 16:16:50 -08001970 // Access to the composite stream map must be synchronized
1971 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001972 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1973 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1974 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001975}
1976
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001977void CameraDeviceClient::notifyPrepared(int streamId) {
1978 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001979 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001980 if (remoteCb != 0) {
1981 remoteCb->onPrepared(streamId);
1982 }
1983}
1984
Shuzhen Wang9d066012016-09-30 11:30:20 -07001985void CameraDeviceClient::notifyRequestQueueEmpty() {
1986 // Thread safe. Don't bother locking.
1987 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1988 if (remoteCb != 0) {
1989 remoteCb->onRequestQueueEmpty();
1990 }
1991}
1992
Igor Murashkine7ee7632013-06-11 18:10:18 -07001993void CameraDeviceClient::detachDevice() {
1994 if (mDevice == 0) return;
1995
Shuzhen Wang316781a2020-08-18 18:11:01 -07001996 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001997 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001998
Emilian Peevfaa4bde2020-01-23 12:19:37 -08001999 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2000 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Igor Murashkine7ee7632013-06-11 18:10:18 -07002001 /*listener*/this);
2002 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002003 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002004 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002005 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002006
2007 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2008 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002009 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002010 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002011 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2012 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2013 }
2014
Igor Murashkine7ee7632013-06-11 18:10:18 -07002015 if ((code = mDevice->waitUntilDrained()) != OK) {
2016 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2017 code);
2018 }
2019 }
2020
Emilian Peev2f5d6012022-01-19 16:16:50 -08002021 {
2022 Mutex::Autolock l(mCompositeLock);
2023 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2024 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2025 if (ret != OK) {
2026 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2027 strerror(-ret), ret);
2028 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002029 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002030 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002031 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002032
Igor Murashkine7ee7632013-06-11 18:10:18 -07002033 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002034
2035 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
2036 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002037}
2038
2039/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002040void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002041 ATRACE_CALL();
2042 ALOGV("%s", __FUNCTION__);
2043
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002044 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002045 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002046 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002047 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2048 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002049 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002050
Emilian Peev2f5d6012022-01-19 16:16:50 -08002051 // Access to the composite stream map must be synchronized
2052 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002053 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2054 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2055 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002056}
2057
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002058binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002059 if (mDisconnected) {
2060 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2061 "The camera device has been disconnected");
2062 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002063 status_t res = checkPid(checkLocation);
2064 return (res == OK) ? binder::Status::ok() :
2065 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2066 "Attempt to use camera from a different process than original client");
2067}
2068
Igor Murashkine7ee7632013-06-11 18:10:18 -07002069// TODO: move to Camera2ClientBase
2070bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2071
Jayant Chowdhary12361932018-08-27 14:46:13 -07002072 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002073 const int selfPid = getpid();
2074 camera_metadata_entry_t entry;
2075
2076 /**
2077 * Mixin default important security values
2078 * - android.led.transmit = defaulted ON
2079 */
2080 CameraMetadata staticInfo = mDevice->info();
2081 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2082 for(size_t i = 0; i < entry.count; ++i) {
2083 uint8_t led = entry.data.u8[i];
2084
2085 switch(led) {
2086 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2087 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2088 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2089 metadata.update(ANDROID_LED_TRANSMIT,
2090 &transmitDefault, 1);
2091 }
2092 break;
2093 }
2094 }
2095 }
2096
2097 // We can do anything!
2098 if (pid == selfPid) {
2099 return true;
2100 }
2101
2102 /**
2103 * Permission check special fields in the request
2104 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2105 */
2106 entry = metadata.find(ANDROID_LED_TRANSMIT);
2107 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2108 String16 permissionString =
2109 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2110 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002111 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002112 ALOGE("Permission Denial: "
2113 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2114 return false;
2115 }
2116 }
2117
2118 return true;
2119}
2120
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002121status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
2122 ALOGV("%s: begin", __FUNCTION__);
2123
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002124 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07002125 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002126}
2127
Emilian Peevf4816702020-04-03 15:44:51 -07002128binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2129 camera_request_template_t* tempId /*out*/) {
2130 binder::Status ret = binder::Status::ok();
2131
2132 if (tempId == nullptr) {
2133 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2134 "Camera %s: Invalid template argument", mCameraIdStr.string());
2135 return ret;
2136 }
2137 switch(templateId) {
2138 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2139 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2140 break;
2141 case ICameraDeviceUser::TEMPLATE_RECORD:
2142 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2143 break;
2144 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2145 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2146 break;
2147 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2148 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2149 break;
2150 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2151 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2152 break;
2153 case ICameraDeviceUser::TEMPLATE_MANUAL:
2154 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2155 break;
2156 default:
2157 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2158 "Camera %s: Template ID %d is invalid or not supported",
2159 mCameraIdStr.string(), templateId);
2160 return ret;
2161 }
2162
2163 return ret;
2164}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002165
2166const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2167 if (mDevice->getId() == cameraId) {
2168 return mDevice->info();
2169 }
2170 return mDevice->infoPhysical(cameraId);
2171}
2172
2173bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2174 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002175 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002176}
2177
2178bool CameraDeviceClient::isSensorPixelModeConsistent(
2179 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2180 // First we get the sensorPixelMode from the settings metadata.
2181 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2182 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2183 if (sensorPixelModeEntry.count != 0) {
2184 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2185 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2186 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2187 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2188 __FUNCTION__, sensorPixelMode);
2189 return false;
2190 }
2191 }
2192 // Check whether each stream has max resolution allowed.
2193 bool consistent = true;
2194 for (auto it : streamIdList) {
2195 auto const streamInfoIt = mStreamInfoMap.find(it);
2196 if (streamInfoIt == mStreamInfoMap.end()) {
2197 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2198 return false;
2199 }
2200 consistent =
2201 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2202 streamInfoIt->second.sensorPixelModesUsed.end();
2203 if (!consistent) {
2204 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2205 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2206 ALOGE("sensor pixel mode used list: %i", m);
2207 }
2208 break;
2209 }
2210 }
2211
2212 return consistent;
2213}
2214
Igor Murashkine7ee7632013-06-11 18:10:18 -07002215} // namespace android