blob: 60daf2da2525fb999dcd1b9ac81388ac9af1eb3c [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"
34
Emilian Peev00420d22018-02-05 21:33:13 +000035#include <camera_metadata_hidden.h>
36
Emilian Peev538c90e2018-12-17 18:03:19 +000037#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080038#include "HeicCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000039
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070041
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080042#define STATUS_ERROR(errorCode, errorString) \
43 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080044 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080045
46#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
47 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080048 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080049 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070050
51namespace android {
52using namespace camera2;
Austin Borgerea931242021-12-13 23:10:41 +000053using namespace camera3;
Emilian Peevf4816702020-04-03 15:44:51 -070054using camera3::camera_stream_rotation_t::CAMERA_STREAM_ROTATION_0;
Igor Murashkine7ee7632013-06-11 18:10:18 -070055
56CameraDeviceClientBase::CameraDeviceClientBase(
57 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080058 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070059 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080060 bool systemNativeClient,
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,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080072 systemNativeClient,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080073 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080074 cameraId,
75 cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070076 sensorOrientation,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080077 clientPid,
78 clientUid,
79 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070080 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080081 // We don't need it for API2 clients, but Camera2ClientBase requires it.
82 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070083}
Igor Murashkine7ee7632013-06-11 18:10:18 -070084
85// Interface used by CameraService
86
87CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080088 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Austin Borger74fca042022-05-23 12:41:21 -070089 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080090 const String16& clientPackageName,
Jayant Chowdharyeb0169f2022-01-31 00:00:02 -080091 bool systemNativeClient,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090092 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080093 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080094 int cameraFacing,
Emilian Peev8b64f282021-03-25 16:49:57 -070095 int sensorOrientation,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080096 int clientPid,
97 uid_t clientUid,
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070098 int servicePid,
99 bool overrideForPerfClass) :
Austin Borger74fca042022-05-23 12:41:21 -0700100 Camera2ClientBase(cameraService, remoteCallback, cameraServiceProxyWrapper, clientPackageName,
101 systemNativeClient, clientFeatureId, cameraId, /*API1 camera ID*/ -1, cameraFacing,
102 sensorOrientation, clientPid, clientUid, servicePid, overrideForPerfClass),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700103 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700104 mStreamingRequestId(REQUEST_ID_NONE),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700105 mRequestIdCounter(0),
106 mOverrideForPerfClass(overrideForPerfClass) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700107
108 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800109 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110}
111
Emilian Peevbd8c5032018-02-14 23:05:40 +0000112status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
113 const String8& monitorTags) {
114 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800115}
116
117template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000118status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700119 ATRACE_CALL();
120 status_t res;
121
Emilian Peevbd8c5032018-02-14 23:05:40 +0000122 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700123 if (res != OK) {
124 return res;
125 }
126
127 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700128 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800129 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Austin Borger7b129542022-06-09 13:23:06 -0700130 res = mFrameProcessor->run(threadName.string());
131 if (res != OK) {
132 ALOGE("%s: Unable to start frame processor thread: %s (%d)",
133 __FUNCTION__, strerror(-res), res);
134 return res;
135 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700136
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800137 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
138 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800139 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700140 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700141
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800142 const CameraMetadata &deviceInfo = mDevice->info();
143 camera_metadata_ro_entry_t physicalKeysEntry = deviceInfo.find(
Emilian Peev00420d22018-02-05 21:33:13 +0000144 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
145 if (physicalKeysEntry.count > 0) {
146 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
147 physicalKeysEntry.data.i32,
148 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
149 }
150
Emilian Peev2295df72021-11-12 18:14:10 -0800151 auto entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
152 mDynamicProfileMap.emplace(
153 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD,
154 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD);
155 if (entry.count > 0) {
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800156 const auto it = std::find(entry.data.u8, entry.data.u8 + entry.count,
Emilian Peev2295df72021-11-12 18:14:10 -0800157 ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT);
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800158 if (it != entry.data.u8 + entry.count) {
Emilian Peev2295df72021-11-12 18:14:10 -0800159 entry = deviceInfo.find(ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP);
Emilian Peevc81a7592022-02-14 17:38:18 -0800160 if (entry.count > 0 || ((entry.count % 3) != 0)) {
161 int64_t standardBitmap =
162 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
163 for (size_t i = 0; i < entry.count; i += 3) {
164 if (entry.data.i64[i] !=
Emilian Peev2295df72021-11-12 18:14:10 -0800165 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800166 mDynamicProfileMap.emplace(entry.data.i64[i], entry.data.i64[i+1]);
167 if ((entry.data.i64[i+1] == 0) || (entry.data.i64[i+1] &
Emilian Peev2295df72021-11-12 18:14:10 -0800168 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD)) {
Emilian Peevc81a7592022-02-14 17:38:18 -0800169 standardBitmap |= entry.data.i64[i];
Emilian Peev2295df72021-11-12 18:14:10 -0800170 }
171 } else {
Emilian Peevc81a7592022-02-14 17:38:18 -0800172 ALOGE("%s: Device %s includes unexpected profile entry: 0x%" PRIx64 "!",
173 __FUNCTION__, mCameraIdStr.c_str(), entry.data.i64[i]);
Emilian Peev2295df72021-11-12 18:14:10 -0800174 }
175 }
Emilian Peevef715e22022-05-19 17:44:33 -0700176 mDynamicProfileMap[ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD] =
177 standardBitmap;
Emilian Peev2295df72021-11-12 18:14:10 -0800178 } else {
179 ALOGE("%s: Device %s supports 10-bit output but doesn't include a dynamic range"
180 " profile map!", __FUNCTION__, mCameraIdStr.c_str());
181 }
182 }
183 }
184
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700185 mProviderManager = providerPtr;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800186 // Cache physical camera ids corresponding to this device and also the high
187 // resolution sensors in this device + physical camera ids
188 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &mPhysicalCameraIds);
189 if (isUltraHighResolutionSensor(mCameraIdStr)) {
190 mHighResolutionSensors.insert(mCameraIdStr.string());
191 }
192 for (auto &physicalId : mPhysicalCameraIds) {
193 if (isUltraHighResolutionSensor(String8(physicalId.c_str()))) {
194 mHighResolutionSensors.insert(physicalId.c_str());
195 }
196 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700197 return OK;
198}
199
200CameraDeviceClient::~CameraDeviceClient() {
201}
202
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800203binder::Status CameraDeviceClient::submitRequest(
204 const hardware::camera2::CaptureRequest& request,
205 bool streaming,
206 /*out*/
207 hardware::camera2::utils::SubmitInfo *submitInfo) {
208 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
209 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700210}
211
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800212binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000213 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000214 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800215 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
216
Emilian Peev2f5d6012022-01-19 16:16:50 -0800217 Mutex::Autolock l(mCompositeLock);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800218 // Trying to submit request with surface that wasn't created
219 if (idx == NAME_NOT_FOUND) {
220 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
221 " we have not called createStream on",
222 __FUNCTION__, mCameraIdStr.string());
223 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
224 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000225 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
226 != NAME_NOT_FOUND) {
227 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
228 currentStreamId);
229 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800230 }
231
232 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
233 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800234 outputStreamIds->push_back(streamSurfaceId.streamId());
235 }
236 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
237
238 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
239 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
240 streamSurfaceId.surfaceId());
241
Emilian Peevf873aa52018-01-26 14:58:28 +0000242 if (currentStreamId != nullptr) {
243 *currentStreamId = streamSurfaceId.streamId();
244 }
245
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800246 return binder::Status::ok();
247}
248
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800249static std::list<int> getIntersection(const std::unordered_set<int> &streamIdsForThisCamera,
250 const Vector<int> &streamIdsForThisRequest) {
251 std::list<int> intersection;
252 for (auto &streamId : streamIdsForThisRequest) {
253 if (streamIdsForThisCamera.find(streamId) != streamIdsForThisCamera.end()) {
254 intersection.emplace_back(streamId);
255 }
256 }
257 return intersection;
258}
259
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800260binder::Status CameraDeviceClient::submitRequestList(
261 const std::vector<hardware::camera2::CaptureRequest>& requests,
262 bool streaming,
263 /*out*/
264 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700265 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700266 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700267
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800268 binder::Status res = binder::Status::ok();
269 status_t err;
270 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
271 return res;
272 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700273
274 Mutex::Autolock icl(mBinderSerializationLock);
275
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800276 if (!mDevice.get()) {
277 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
278 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700279
280 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800281 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
282 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700284 }
285
Emilian Peevaebbe412018-01-15 13:53:24 +0000286 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700287 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800288 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700289 uint32_t loopCounter = 0;
290
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 for (auto&& request: requests) {
292 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700293 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800294 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
295 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800296 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800297 "No input configured for camera %s but request is for reprocessing",
298 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700299 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800300 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
301 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800302 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
303 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000304 } else if (request.mPhysicalCameraSettings.size() > 1) {
305 ALOGE("%s: Camera %s: reprocess requests not supported for "
306 "multiple physical cameras.", __FUNCTION__,
307 mCameraIdStr.string());
308 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
309 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700310 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700311 }
312
Emilian Peevaebbe412018-01-15 13:53:24 +0000313 if (request.mPhysicalCameraSettings.empty()) {
314 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
315 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800316 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000317 "Request doesn't contain any settings");
318 }
319
320 //The first capture settings should always match the logical camera id
321 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
322 if (mDevice->getId() != logicalId) {
323 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
324 mCameraIdStr.string());
325 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
326 "Invalid camera request settings");
327 }
328
Emilian Peevaebbe412018-01-15 13:53:24 +0000329 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800330 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
331 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800332 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
333 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700334 }
335
Jianing Wei90e59c92014-03-12 18:29:36 -0700336 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700337 * Write in the output stream IDs and map from stream ID to surface ID
338 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700339 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700340 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700341 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000342 std::vector<std::string> requestedPhysicalIds;
Emilian Peevc81a7592022-02-14 17:38:18 -0800343 int64_t dynamicProfileBitmap = 0;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800344 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800345 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800346 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700347
Emilian Peevf873aa52018-01-26 14:58:28 +0000348 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800349 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000350 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800351 if (!res.isOk()) {
352 return res;
353 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000354
355 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
356 if (index >= 0) {
357 String8 requestedPhysicalId(
358 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
359 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800360 dynamicProfileBitmap |=
361 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
Emilian Peevf873aa52018-01-26 14:58:28 +0000362 } else {
363 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
364 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700365 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800366 } else {
367 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
368 int streamId = request.mStreamIdxList.itemAt(i);
369 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700370
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800371 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
372 if (index < 0) {
373 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
374 " we have not called createStream on: stream %d",
375 __FUNCTION__, mCameraIdStr.string(), streamId);
376 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
377 "Request targets Surface that is not part of current capture session");
378 }
379
380 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
381 if ((size_t)surfaceIdx >= gbps.size()) {
382 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
383 " we have not called createStream on: stream %d, surfaceIdx %d",
384 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
385 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
386 "Request targets Surface has invalid surface index");
387 }
388
Emilian Peevf873aa52018-01-26 14:58:28 +0000389 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800390 if (!res.isOk()) {
391 return res;
392 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000393
394 String8 requestedPhysicalId(
395 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
396 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Emilian Peev2295df72021-11-12 18:14:10 -0800397 dynamicProfileBitmap |=
398 mConfiguredOutputs.valueAt(index).getDynamicRangeProfile();
399 }
400 }
401
402 if (dynamicProfileBitmap !=
403 ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD) {
404 for (int i = ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD;
405 i < ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_MAX; i <<= 1) {
406 if ((dynamicProfileBitmap & i) == 0) {
407 continue;
408 }
409
410 const auto& it = mDynamicProfileMap.find(i);
411 if (it != mDynamicProfileMap.end()) {
412 if ((it->second == 0) ||
413 ((it->second & dynamicProfileBitmap) == dynamicProfileBitmap)) {
414 continue;
415 } else {
416 ALOGE("%s: Camera %s: Tried to submit a request with a surfaces that"
417 " reference an unsupported dynamic range profile combination"
Emilian Peevc81a7592022-02-14 17:38:18 -0800418 " 0x%" PRIx64 "!", __FUNCTION__, mCameraIdStr.string(),
Emilian Peev2295df72021-11-12 18:14:10 -0800419 dynamicProfileBitmap);
420 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
421 "Request targets an unsupported dynamic range profile"
422 " combination");
423 }
424 } else {
425 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
426 " references unsupported dynamic range profile 0x%x!",
427 __FUNCTION__, mCameraIdStr.string(), i);
428 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
429 "Request targets 10-bit Surface with unsupported dynamic range"
430 " profile");
431 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700432 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700433 }
434
Emilian Peevf873aa52018-01-26 14:58:28 +0000435 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
436 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000437 if (it.settings.isEmpty()) {
438 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
439 __FUNCTION__, mCameraIdStr.string());
440 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
441 "Request settings are empty");
442 }
443
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800444 // Check whether the physical / logical stream has settings
445 // consistent with the sensor pixel mode(s) it was configured with.
446 // mCameraIdToStreamSet will only have ids that are high resolution
447 const auto streamIdSetIt = mHighResolutionCameraIdToStreamIdSet.find(it.id);
448 if (streamIdSetIt != mHighResolutionCameraIdToStreamIdSet.end()) {
449 std::list<int> streamIdsUsedInRequest = getIntersection(streamIdSetIt->second,
450 outputStreamIds);
451 if (!request.mIsReprocess &&
452 !isSensorPixelModeConsistent(streamIdsUsedInRequest, it.settings)) {
453 ALOGE("%s: Camera %s: Request settings CONTROL_SENSOR_PIXEL_MODE not "
454 "consistent with configured streams. Rejecting request.",
455 __FUNCTION__, it.id.c_str());
456 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
457 "Request settings CONTROL_SENSOR_PIXEL_MODE are not consistent with "
458 "streams configured");
459 }
460 }
461
Emilian Peevf873aa52018-01-26 14:58:28 +0000462 String8 physicalId(it.id.c_str());
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700463 bool hasTestPatternModePhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
464 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_MODE) !=
465 mSupportedPhysicalRequestKeys.end();
466 bool hasTestPatternDataPhysicalKey = std::find(mSupportedPhysicalRequestKeys.begin(),
467 mSupportedPhysicalRequestKeys.end(), ANDROID_SENSOR_TEST_PATTERN_DATA) !=
468 mSupportedPhysicalRequestKeys.end();
Emilian Peevf873aa52018-01-26 14:58:28 +0000469 if (physicalId != mDevice->getId()) {
470 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
471 it.id);
472 if (found == requestedPhysicalIds.end()) {
473 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
474 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
475 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
476 "Invalid physical camera id");
477 }
Emilian Peev00420d22018-02-05 21:33:13 +0000478
479 if (!mSupportedPhysicalRequestKeys.empty()) {
480 // Filter out any unsupported physical request keys.
481 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
482 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
483 filteredParams.getAndLock());
484 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
485 filteredParams.unlock(meta);
486
487 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
488 camera_metadata_ro_entry entry = it.settings.find(keyIt);
489 if (entry.count > 0) {
490 filteredParams.update(entry);
491 }
492 }
493
Shuzhen Wang911c6a32021-10-27 13:36:03 -0700494 physicalSettingsList.push_back({it.id, filteredParams,
495 hasTestPatternModePhysicalKey, hasTestPatternDataPhysicalKey});
Emilian Peev00420d22018-02-05 21:33:13 +0000496 }
497 } else {
498 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000499 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000500 }
501
502 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
503 // Callee logs
504 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
505 "Caller does not have permission to change restricted controls");
506 }
507
Emilian Peevaebbe412018-01-15 13:53:24 +0000508 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
509 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700510
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800511 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000512 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
513 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700514 }
515
Emilian Peevaebbe412018-01-15 13:53:24 +0000516 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
517 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700518 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800519 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
520 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
521 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700522
Emilian Peevaebbe412018-01-15 13:53:24 +0000523 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700524 surfaceMapList.push_back(surfaceMap);
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800525
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700526 // Save certain CaptureRequest settings
Shuzhen Wangd26b1862022-03-07 12:05:05 -0800527 if (!request.mUserTag.empty()) {
528 mUserTag = request.mUserTag;
529 }
Shuzhen Wang9372b0b2022-05-11 18:55:19 -0700530 camera_metadata_entry entry =
531 physicalSettingsList.begin()->metadata.find(
532 ANDROID_CONTROL_VIDEO_STABILIZATION_MODE);
533 if (entry.count == 1) {
534 mVideoStabilizationMode = entry.data.u8[0];
535 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700536 }
537 mRequestIdCounter++;
538
539 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700540 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
541 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800542 if (err != OK) {
543 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800544 "Camera %s: Got error %s (%d) after trying to set streaming request",
545 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 ALOGE("%s: %s", __FUNCTION__, msg.string());
547 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
548 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700549 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700550 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700551 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700552 }
553 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700554 err = mDevice->captureList(metadataRequestList, surfaceMapList,
555 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800556 if (err != OK) {
557 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800558 "Camera %s: Got error %s (%d) after trying to submit capture request",
559 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800560 ALOGE("%s: %s", __FUNCTION__, msg.string());
561 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
562 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700563 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800564 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700565 }
566
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800567 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700568 return res;
569}
570
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800571binder::Status CameraDeviceClient::cancelRequest(
572 int requestId,
573 /*out*/
574 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700575 ATRACE_CALL();
576 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
577
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800578 status_t err;
579 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700580
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800581 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700582
583 Mutex::Autolock icl(mBinderSerializationLock);
584
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800585 if (!mDevice.get()) {
586 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
587 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700588
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700589 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700590 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800591 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
592 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800593 ALOGE("%s: %s", __FUNCTION__, msg.string());
594 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700595 }
596
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700598
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800599 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800600 ALOGV("%s: Camera %s: Successfully cleared streaming request",
601 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700602 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800603 } else {
604 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800605 "Camera %s: Error clearing streaming request: %s (%d)",
606 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700607 }
608
609 return res;
610}
611
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800612binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700613 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700614 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700615 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800616 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700617}
618
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100619binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700620 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800621 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700622 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700623 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
624 __FUNCTION__, mInputStream.configured ? 1 : 0,
625 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700626
Zhijun He0effd522016-03-04 10:22:27 -0800627 binder::Status res;
628 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
629
Emilian Peevcc0b7952020-01-07 13:54:47 -0800630 if (offlineStreamIds == nullptr) {
631 String8 msg = String8::format("Invalid offline stream ids");
632 ALOGE("%s: %s", __FUNCTION__, msg.string());
633 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
634 }
635
Zhijun He0effd522016-03-04 10:22:27 -0800636 Mutex::Autolock icl(mBinderSerializationLock);
637
638 if (!mDevice.get()) {
639 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
640 }
641
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700642 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000643 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000644 if (!res.isOk()) {
645 return res;
646 }
647
648 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
649 if (err == BAD_VALUE) {
650 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
651 mCameraIdStr.string());
652 ALOGE("%s: %s", __FUNCTION__, msg.string());
653 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
654 } else if (err != OK) {
655 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
656 mCameraIdStr.string(), strerror(-err), err);
657 ALOGE("%s: %s", __FUNCTION__, msg.string());
658 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000659 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800660 offlineStreamIds->clear();
661 mDevice->getOfflineStreamIds(offlineStreamIds);
662
Emilian Peev2f5d6012022-01-19 16:16:50 -0800663 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000664 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
665 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800666 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000667 String8 msg = String8::format("Camera %s: Error configuring composite "
668 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
669 ALOGE("%s: %s", __FUNCTION__, msg.string());
670 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
671 break;
672 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800673
674 // Composite streams can only support offline mode in case all individual internal
675 // streams are also supported.
676 std::vector<int> internalStreams;
677 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800678 offlineStreamIds->erase(
679 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800680 [&internalStreams] (int streamId) {
681 auto it = std::find(internalStreams.begin(), internalStreams.end(),
682 streamId);
683 if (it != internalStreams.end()) {
684 internalStreams.erase(it);
685 return true;
686 }
687
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800688 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800689 if (internalStreams.empty()) {
690 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
691 }
692 }
693
694 for (const auto& offlineStreamId : *offlineStreamIds) {
695 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000696 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700697
698 nsecs_t configureEnd = systemTime();
699 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
Austin Borger74fca042022-05-23 12:41:21 -0700700 mCameraServiceProxyWrapper->logStreamConfigured(mCameraIdStr, operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700701 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000702 }
703
704 return res;
705}
706
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800707binder::Status CameraDeviceClient::isSessionConfigurationSupported(
708 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800709
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800710 ATRACE_CALL();
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800711 binder::Status res;
712 status_t ret = OK;
713 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
714
715 Mutex::Autolock icl(mBinderSerializationLock);
716
717 if (!mDevice.get()) {
718 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
719 }
720
721 auto operatingMode = sessionConfiguration.getOperatingMode();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700722 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000723 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800724 if (!res.isOk()) {
725 return res;
726 }
727
728 if (status == nullptr) {
729 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
730 ALOGE("%s: %s", __FUNCTION__, msg.string());
731 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
732 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700733
Emilian Peev35ae8262018-11-08 13:11:32 +0000734 *status = false;
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700735 camera3::metadataGetter getMetadata = [this](const String8 &id, bool /*overrideForPerfClass*/) {
736 return mDevice->infoPhysical(id);};
Emilian Peev35ae8262018-11-08 13:11:32 +0000737 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
Shuzhen Wanga79a64d2022-04-24 19:56:30 -0700738 sessionConfiguration, mOverrideForPerfClass, getMetadata, status);
Emilian Peev35ae8262018-11-08 13:11:32 +0000739 switch (ret) {
740 case OK:
741 // Expected, do nothing.
742 break;
743 case INVALID_OPERATION: {
744 String8 msg = String8::format(
745 "Camera %s: Session configuration query not supported!",
746 mCameraIdStr.string());
747 ALOGD("%s: %s", __FUNCTION__, msg.string());
748 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
749 }
750
751 break;
752 default: {
753 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
754 strerror(-ret), ret);
755 ALOGE("%s: %s", __FUNCTION__, msg.string());
756 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
757 msg.string());
758 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800759 }
760
761 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700762}
763
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800764binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700765 ATRACE_CALL();
766 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
767
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800768 binder::Status res;
769 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700770
771 Mutex::Autolock icl(mBinderSerializationLock);
772
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800773 if (!mDevice.get()) {
774 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
775 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700776
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700777 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700778 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700779 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000780 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700781
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700782 if (mInputStream.configured && mInputStream.id == streamId) {
783 isInput = true;
784 } else {
785 // Guard against trying to delete non-created streams
786 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700787 if (streamId == mStreamMap.valueAt(i).streamId()) {
788 surfaces.push_back(mStreamMap.keyAt(i));
789 }
790 }
791
792 // See if this stream is one of the deferred streams.
793 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
794 if (streamId == mDeferredStreams[i]) {
795 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700796 break;
797 }
798 }
799
Emilian Peev2f5d6012022-01-19 16:16:50 -0800800 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000801 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
802 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
803 compositeIndex = i;
804 break;
805 }
806 }
807
Shuzhen Wang0129d522016-10-30 22:43:41 -0700808 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
809 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
810 " stream created yet", mCameraIdStr.string(), streamId);
811 ALOGW("%s: %s", __FUNCTION__, msg.string());
812 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700813 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700814 }
815
816 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800817 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700818
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800819 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800820 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
821 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800822 ALOGE("%s: %s", __FUNCTION__, msg.string());
823 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
824 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700825 if (isInput) {
826 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700827 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700828 for (auto& surface : surfaces) {
829 mStreamMap.removeItem(surface);
830 }
831
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800832 mConfiguredOutputs.removeItem(streamId);
833
Shuzhen Wang0129d522016-10-30 22:43:41 -0700834 if (dIndex != NAME_NOT_FOUND) {
835 mDeferredStreams.removeItemsAt(dIndex);
836 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000837
838 if (compositeIndex != NAME_NOT_FOUND) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800839 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000840 status_t ret;
841 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
842 != OK) {
843 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
844 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
845 streamId);
846 ALOGE("%s: %s", __FUNCTION__, msg.string());
847 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
848 }
849 mCompositeStreamMap.removeItemsAt(compositeIndex);
850 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800851 for (auto &mapIt: mHighResolutionCameraIdToStreamIdSet) {
852 auto &streamSet = mapIt.second;
853 if (streamSet.find(streamId) != streamSet.end()) {
854 streamSet.erase(streamId);
855 break;
856 }
857 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700858 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700859 }
860
861 return res;
862}
863
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800864binder::Status CameraDeviceClient::createStream(
865 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
866 /*out*/
867 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700868 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700869
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800870 binder::Status res;
871 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700872
873 Mutex::Autolock icl(mBinderSerializationLock);
874
Shuzhen Wang0129d522016-10-30 22:43:41 -0700875 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
876 outputConfiguration.getGraphicBufferProducers();
877 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800878 bool deferredConsumer = outputConfiguration.isDeferred();
879 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800880 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800881 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800882 bool isMultiResolution = outputConfiguration.isMultiResolution();
Emilian Peevc81a7592022-02-14 17:38:18 -0800883 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800884 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -0800885 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -0800886 int mirrorMode = outputConfiguration.getMirrorMode();
Austin Borger9e2b27c2022-07-15 11:27:24 -0700887 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700888
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700889 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000890 outputConfiguration.getSurfaceType());
891 if (!res.isOk()) {
892 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700893 }
Zhijun He5d677d12016-05-29 16:52:39 -0700894
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800895 if (!mDevice.get()) {
896 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
897 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700898 res = SessionConfigurationUtils::checkPhysicalCameraId(mPhysicalCameraIds,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800899 physicalCameraId, mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000900 if (!res.isOk()) {
901 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800902 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000903
Shuzhen Wang0129d522016-10-30 22:43:41 -0700904 std::vector<sp<Surface>> surfaces;
905 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700906 status_t err;
907
908 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700909 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800910 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700911 }
912
Shuzhen Wang758c2152017-01-10 18:26:18 -0800913 OutputStreamInfo streamInfo;
914 bool isStreamInfoValid = false;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800915 const std::vector<int32_t> &sensorPixelModesUsed =
916 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700917 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700918 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700919 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800920 ssize_t index = mStreamMap.indexOfKey(binder);
921 if (index != NAME_NOT_FOUND) {
922 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
923 "(ID %zd)", mCameraIdStr.string(), index);
924 ALOGW("%s: %s", __FUNCTION__, msg.string());
925 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700926 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700927
Shuzhen Wang758c2152017-01-10 18:26:18 -0800928 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700929 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800930 isStreamInfoValid, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -0800931 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700932 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800933
934 if (!res.isOk())
935 return res;
936
937 if (!isStreamInfoValid) {
938 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700939 }
940
Shuzhen Wang758c2152017-01-10 18:26:18 -0800941 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700942 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800943 }
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700944
945 // If mOverrideForPerfClass is true, do not fail createStream() for small
946 // JPEG sizes because existing createSurfaceFromGbp() logic will find the
947 // closest possible supported size.
948
Zhijun He125684a2015-12-26 15:07:30 -0800949 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100950 std::vector<int> surfaceIds;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800951 bool isDepthCompositeStream =
952 camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800953 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
954 if (isDepthCompositeStream || isHeicCompisiteStream) {
955 sp<CompositeStream> compositeStream;
956 if (isDepthCompositeStream) {
957 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
958 } else {
959 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
960 }
961
Emilian Peev538c90e2018-12-17 18:03:19 +0000962 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
963 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700964 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800965 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
966 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000967 if (err == OK) {
Emilian Peev2f5d6012022-01-19 16:16:50 -0800968 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +0000969 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
970 compositeStream);
971 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800972 } else {
973 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
974 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700975 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800976 &streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
Emilian Peev2295df72021-11-12 18:14:10 -0800977 outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution,
Shuzhen Wange4208922022-02-01 16:52:48 -0800978 /*consumerUsage*/0, streamInfo.dynamicRangeProfile, streamInfo.streamUseCase,
Austin Borger9e2b27c2022-07-15 11:27:24 -0700979 streamInfo.timestampBase, streamInfo.mirrorMode, streamInfo.colorSpace);
Emilian Peev538c90e2018-12-17 18:03:19 +0000980 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700981
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800982 if (err != OK) {
983 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800984 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800985 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
986 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800987 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700988 int i = 0;
989 for (auto& binder : binders) {
990 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
991 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100992 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
993 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700994 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800995
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800996 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800997 mStreamInfoMap[streamId] = streamInfo;
998
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800999 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -07001000 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -08001001 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
1002 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001003
Zhijun He5d677d12016-05-29 16:52:39 -07001004 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001005 res = setStreamTransformLocked(streamId, streamInfo.mirrorMode);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001006
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001007 // Fill in mHighResolutionCameraIdToStreamIdSet map
1008 const String8 &cameraIdUsed =
1009 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1010 const char *cameraIdUsedCStr = cameraIdUsed.string();
1011 // Only needed for high resolution sensors
1012 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1013 mHighResolutionSensors.end()) {
1014 mHighResolutionCameraIdToStreamIdSet[cameraIdUsedCStr].insert(streamId);
1015 }
1016
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001017 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001018 }
1019
1020 return res;
1021}
1022
Zhijun He5d677d12016-05-29 16:52:39 -07001023binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
1024 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001025 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -07001026 /*out*/
1027 int* newStreamId) {
1028 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +01001029 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -07001030 android_dataspace dataSpace;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001031 int32_t colorSpace;
Zhijun He5d677d12016-05-29 16:52:39 -07001032 status_t err;
1033 binder::Status res;
1034
1035 if (!mDevice.get()) {
1036 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1037 }
1038
1039 // Infer the surface info for deferred surface stream creation.
1040 width = outputConfiguration.getWidth();
1041 height = outputConfiguration.getHeight();
1042 surfaceType = outputConfiguration.getSurfaceType();
1043 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1044 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
Austin Borger9e2b27c2022-07-15 11:27:24 -07001045 colorSpace = ANDROID_REQUEST_AVAILABLE_COLOR_SPACE_PROFILES_MAP_UNSPECIFIED;
Zhijun He5d677d12016-05-29 16:52:39 -07001046 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
1047 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
1048 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
1049 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
1050 }
1051 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001052 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +01001053 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001054 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001055 const String8 &cameraIdUsed =
1056 physicalCameraId.size() != 0 ? physicalCameraId : mCameraIdStr;
1057 // Here, we override sensor pixel modes
1058 std::unordered_set<int32_t> overriddenSensorPixelModesUsed;
1059 const std::vector<int32_t> &sensorPixelModesUsed =
1060 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001061 if (SessionConfigurationUtils::checkAndOverrideSensorPixelModesUsed(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001062 sensorPixelModesUsed, format, width, height, getStaticInfo(cameraIdUsed),
1063 /*allowRounding*/ false, &overriddenSensorPixelModesUsed) != OK) {
1064 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1065 "sensor pixel modes used not valid for deferred stream");
1066 }
1067
Shuzhen Wang0129d522016-10-30 22:43:41 -07001068 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
1069 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -07001070 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001071 &streamId, physicalCameraId,
1072 overriddenSensorPixelModesUsed,
1073 &surfaceIds,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001074 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev2295df72021-11-12 18:14:10 -08001075 outputConfiguration.isMultiResolution(), consumerUsage,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001076 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001077 outputConfiguration.getStreamUseCase(),
1078 outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001079
1080 if (err != OK) {
1081 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001082 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
1083 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001084 } else {
1085 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
1086 // a separate list to track. Once the deferred surface is set, this id will be
1087 // relocated to mStreamMap.
1088 mDeferredStreams.push_back(streamId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001089 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001090 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage,
Emilian Peev2295df72021-11-12 18:14:10 -08001091 overriddenSensorPixelModesUsed,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001092 outputConfiguration.getDynamicRangeProfile(),
Shuzhen Wange4208922022-02-01 16:52:48 -08001093 outputConfiguration.getStreamUseCase(),
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001094 outputConfiguration.getTimestampBase(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001095 outputConfiguration.getMirrorMode(),
1096 colorSpace));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001097
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001098 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -07001099 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001100 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -07001101
1102 // Set transform flags to ensure preview to be rotated correctly.
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001103 res = setStreamTransformLocked(streamId, outputConfiguration.getMirrorMode());
Zhijun He5d677d12016-05-29 16:52:39 -07001104
1105 *newStreamId = streamId;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001106 // Fill in mHighResolutionCameraIdToStreamIdSet
1107 const char *cameraIdUsedCStr = cameraIdUsed.string();
1108 // Only needed for high resolution sensors
1109 if (mHighResolutionSensors.find(cameraIdUsedCStr) !=
1110 mHighResolutionSensors.end()) {
1111 mHighResolutionCameraIdToStreamIdSet[cameraIdUsed.string()].insert(streamId);
1112 }
Zhijun He5d677d12016-05-29 16:52:39 -07001113 }
1114 return res;
1115}
1116
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001117binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId, int mirrorMode) {
Zhijun He5d677d12016-05-29 16:52:39 -07001118 int32_t transform = 0;
1119 status_t err;
1120 binder::Status res;
1121
1122 if (!mDevice.get()) {
1123 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1124 }
1125
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001126 err = getRotationTransformLocked(mirrorMode, &transform);
Zhijun He5d677d12016-05-29 16:52:39 -07001127 if (err != OK) {
1128 // Error logged by getRotationTransformLocked.
1129 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
1130 "Unable to calculate rotation transform for new stream");
1131 }
1132
1133 err = mDevice->setStreamTransform(streamId, transform);
1134 if (err != OK) {
1135 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
1136 streamId);
1137 ALOGE("%s: %s", __FUNCTION__, msg.string());
1138 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
1139 }
1140
1141 return res;
1142}
Ruben Brunkbba75572014-11-20 17:29:50 -08001143
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001144binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001145 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001146 /*out*/
1147 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001148
1149 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -08001150 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
1151 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001152
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001153 binder::Status res;
1154 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001155
1156 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001157
1158 if (!mDevice.get()) {
1159 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1160 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001161
1162 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001163 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -08001164 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001165 ALOGE("%s: %s", __FUNCTION__, msg.string() );
1166 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001167 }
1168
1169 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001170 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001171 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001172 mInputStream.configured = true;
1173 mInputStream.width = width;
1174 mInputStream.height = height;
1175 mInputStream.format = format;
1176 mInputStream.id = streamId;
1177
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001178 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
1179 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001180
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001181 *newStreamId = streamId;
1182 } else {
1183 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001184 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001185 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001186 }
1187
1188 return res;
1189}
1190
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001191binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001192
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001193 binder::Status res;
1194 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1195
1196 if (inputSurface == NULL) {
1197 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001198 }
1199
1200 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001201 if (!mDevice.get()) {
1202 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1203 }
1204 sp<IGraphicBufferProducer> producer;
1205 status_t err = mDevice->getInputBufferProducer(&producer);
1206 if (err != OK) {
1207 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001208 "Camera %s: Error getting input Surface: %s (%d)",
1209 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001210 } else {
1211 inputSurface->name = String16("CameraInput");
1212 inputSurface->graphicBufferProducer = producer;
1213 }
1214 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001215}
1216
Emilian Peev40ead602017-09-26 15:46:36 +01001217binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1218 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1219 ATRACE_CALL();
1220
1221 binder::Status res;
1222 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1223
1224 Mutex::Autolock icl(mBinderSerializationLock);
1225
1226 if (!mDevice.get()) {
1227 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1228 }
1229
1230 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1231 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001232 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1233
Emilian Peev40ead602017-09-26 15:46:36 +01001234 auto producerCount = bufferProducers.size();
1235 if (producerCount == 0) {
1236 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1237 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1238 "bufferProducers must not be empty");
1239 }
1240
1241 // The first output is the one associated with the output configuration.
1242 // It should always be present, valid and the corresponding stream id should match.
1243 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1244 ssize_t index = mStreamMap.indexOfKey(binder);
1245 if (index == NAME_NOT_FOUND) {
1246 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1247 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1248 "OutputConfiguration is invalid");
1249 }
1250 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1251 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1252 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1253 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1254 "Stream id is invalid");
1255 }
1256
1257 std::vector<size_t> removedSurfaceIds;
1258 std::vector<sp<IBinder>> removedOutputs;
1259 std::vector<sp<Surface>> newOutputs;
1260 std::vector<OutputStreamInfo> streamInfos;
1261 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1262 for (auto &it : bufferProducers) {
1263 newOutputsMap.add(IInterface::asBinder(it), it);
1264 }
1265
1266 for (size_t i = 0; i < mStreamMap.size(); i++) {
1267 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1268 if (idx == NAME_NOT_FOUND) {
1269 if (mStreamMap[i].streamId() == streamId) {
1270 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1271 removedOutputs.push_back(mStreamMap.keyAt(i));
1272 }
1273 } else {
1274 if (mStreamMap[i].streamId() != streamId) {
1275 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1276 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1277 "Target Surface is invalid");
1278 }
1279 newOutputsMap.removeItemsAt(idx);
1280 }
1281 }
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001282 const std::vector<int32_t> &sensorPixelModesUsed =
1283 outputConfiguration.getSensorPixelModesUsed();
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001284 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001285 int timestampBase = outputConfiguration.getTimestampBase();
Emilian Peevc81a7592022-02-14 17:38:18 -08001286 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001287 int32_t colorSpace = outputConfiguration.getColorSpace();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001288 int mirrorMode = outputConfiguration.getMirrorMode();
Emilian Peev2295df72021-11-12 18:14:10 -08001289
Emilian Peev40ead602017-09-26 15:46:36 +01001290 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1291 OutputStreamInfo outInfo;
1292 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001293 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001294 /*isStreamInfoValid*/ false, surface, newOutputsMap.valueAt(i), mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001295 mDevice->infoPhysical(physicalCameraId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001296 streamUseCase, timestampBase, mirrorMode, colorSpace);
Emilian Peev40ead602017-09-26 15:46:36 +01001297 if (!res.isOk())
1298 return res;
1299
Emilian Peev40ead602017-09-26 15:46:36 +01001300 streamInfos.push_back(outInfo);
1301 newOutputs.push_back(surface);
1302 }
1303
1304 //Trivial case no changes required
1305 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1306 return binder::Status::ok();
1307 }
1308
1309 KeyedVector<sp<Surface>, size_t> outputMap;
1310 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1311 &outputMap);
1312 if (ret != OK) {
1313 switch (ret) {
1314 case NAME_NOT_FOUND:
1315 case BAD_VALUE:
1316 case -EBUSY:
1317 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1318 "Camera %s: Error updating stream: %s (%d)",
1319 mCameraIdStr.string(), strerror(ret), ret);
1320 break;
1321 default:
1322 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1323 "Camera %s: Error updating stream: %s (%d)",
1324 mCameraIdStr.string(), strerror(ret), ret);
1325 break;
1326 }
1327 } else {
1328 for (const auto &it : removedOutputs) {
1329 mStreamMap.removeItem(it);
1330 }
1331
1332 for (size_t i = 0; i < outputMap.size(); i++) {
1333 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1334 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1335 }
1336
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001337 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1338
Emilian Peev40ead602017-09-26 15:46:36 +01001339 ALOGV("%s: Camera %s: Successful stream ID %d update",
1340 __FUNCTION__, mCameraIdStr.string(), streamId);
1341 }
1342
1343 return res;
1344}
1345
Igor Murashkine7ee7632013-06-11 18:10:18 -07001346// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001347binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1348 /*out*/
1349 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001350{
1351 ATRACE_CALL();
1352 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1353
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001354 binder::Status res;
1355 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001356
1357 Mutex::Autolock icl(mBinderSerializationLock);
1358
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001359 if (!mDevice.get()) {
1360 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1361 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001362
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001363 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001364 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1365 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1366
1367 CameraMetadata metadata;
1368 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001369 request != NULL) {
1370
1371 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001372 } else if (err == BAD_VALUE) {
1373 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001374 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1375 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001376
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001377 } else {
1378 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001379 "Camera %s: Error creating default request for template %d: %s (%d)",
1380 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001381 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001382 return res;
1383}
1384
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001385binder::Status CameraDeviceClient::getCameraInfo(
1386 /*out*/
1387 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001388{
1389 ATRACE_CALL();
1390 ALOGV("%s", __FUNCTION__);
1391
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001392 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001393
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001394 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001395
1396 Mutex::Autolock icl(mBinderSerializationLock);
1397
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001398 if (!mDevice.get()) {
1399 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1400 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001401
Igor Murashkin099b4572013-07-12 17:52:16 -07001402 if (info != NULL) {
1403 *info = mDevice->info(); // static camera metadata
1404 // TODO: merge with device-specific camera metadata
1405 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001406
1407 return res;
1408}
1409
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001410binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001411{
1412 ATRACE_CALL();
1413 ALOGV("%s", __FUNCTION__);
1414
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001415 binder::Status res;
1416 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001417
1418 Mutex::Autolock icl(mBinderSerializationLock);
1419
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001420 if (!mDevice.get()) {
1421 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1422 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001423
1424 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001425 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001426 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001428 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1429 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001430 ALOGE("%s: %s", __FUNCTION__, msg.string());
1431 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001432 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001433 status_t err = mDevice->waitUntilDrained();
1434 if (err != OK) {
1435 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001436 "Camera %s: Error waiting to drain: %s (%d)",
1437 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001438 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001439 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001440 return res;
1441}
1442
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001443binder::Status CameraDeviceClient::flush(
1444 /*out*/
1445 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001446 ATRACE_CALL();
1447 ALOGV("%s", __FUNCTION__);
1448
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001449 binder::Status res;
1450 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001451
1452 Mutex::Autolock icl(mBinderSerializationLock);
1453
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001454 if (!mDevice.get()) {
1455 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1456 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001457
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001458 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001459 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001460 status_t err = mDevice->flush(lastFrameNumber);
1461 if (err != OK) {
1462 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001463 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001464 }
1465 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001466}
1467
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001468binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001469 ATRACE_CALL();
1470 ALOGV("%s", __FUNCTION__);
1471
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001472 binder::Status res;
1473 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001474
1475 Mutex::Autolock icl(mBinderSerializationLock);
1476
1477 // Guard against trying to prepare non-created streams
1478 ssize_t index = NAME_NOT_FOUND;
1479 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001480 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001481 index = i;
1482 break;
1483 }
1484 }
1485
1486 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001487 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1488 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001489 ALOGW("%s: %s", __FUNCTION__, msg.string());
1490 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001491 }
1492
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001493 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1494 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001495 status_t err = mDevice->prepare(streamId);
1496 if (err == BAD_VALUE) {
1497 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001498 "Camera %s: Stream %d has already been used, and cannot be prepared",
1499 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001500 } else if (err != OK) {
1501 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001502 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001503 strerror(-err), err);
1504 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001505 return res;
1506}
1507
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001508binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001509 ATRACE_CALL();
1510 ALOGV("%s", __FUNCTION__);
1511
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001512 binder::Status res;
1513 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001514
1515 Mutex::Autolock icl(mBinderSerializationLock);
1516
1517 // Guard against trying to prepare non-created streams
1518 ssize_t index = NAME_NOT_FOUND;
1519 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001520 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001521 index = i;
1522 break;
1523 }
1524 }
1525
1526 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001527 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1528 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001529 ALOGW("%s: %s", __FUNCTION__, msg.string());
1530 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001531 }
1532
1533 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001534 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1535 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001536 ALOGE("%s: %s", __FUNCTION__, msg.string());
1537 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001538 }
1539
1540 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1541 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001542 status_t err = mDevice->prepare(maxCount, streamId);
1543 if (err == BAD_VALUE) {
1544 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001545 "Camera %s: Stream %d has already been used, and cannot be prepared",
1546 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001547 } else if (err != OK) {
1548 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001549 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001550 strerror(-err), err);
1551 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001552
1553 return res;
1554}
1555
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001556binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001557 ATRACE_CALL();
1558 ALOGV("%s", __FUNCTION__);
1559
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001560 binder::Status res;
1561 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001562
1563 Mutex::Autolock icl(mBinderSerializationLock);
1564
1565 // Guard against trying to prepare non-created streams
1566 ssize_t index = NAME_NOT_FOUND;
1567 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001568 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001569 index = i;
1570 break;
1571 }
1572 }
1573
1574 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001575 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1576 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001577 ALOGW("%s: %s", __FUNCTION__, msg.string());
1578 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001579 }
1580
1581 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1582 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001583 status_t err = mDevice->tearDown(streamId);
1584 if (err == BAD_VALUE) {
1585 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001586 "Camera %s: Stream %d is still in use, cannot be torn down",
1587 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001588 } else if (err != OK) {
1589 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001590 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001591 strerror(-err), err);
1592 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001593
1594 return res;
1595}
1596
Shuzhen Wang758c2152017-01-10 18:26:18 -08001597binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001598 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1599 ATRACE_CALL();
1600
1601 binder::Status res;
1602 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1603
1604 Mutex::Autolock icl(mBinderSerializationLock);
1605
Shuzhen Wang0129d522016-10-30 22:43:41 -07001606 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1607 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001608 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001609
Shuzhen Wang0129d522016-10-30 22:43:41 -07001610 if (bufferProducers.size() == 0) {
1611 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001612 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1613 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001614
Shuzhen Wang758c2152017-01-10 18:26:18 -08001615 // streamId should be in mStreamMap if this stream already has a surface attached
1616 // to it. Otherwise, it should be in mDeferredStreams.
1617 bool streamIdConfigured = false;
1618 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1619 for (size_t i = 0; i < mStreamMap.size(); i++) {
1620 if (mStreamMap.valueAt(i).streamId() == streamId) {
1621 streamIdConfigured = true;
1622 break;
1623 }
Zhijun He5d677d12016-05-29 16:52:39 -07001624 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001625 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1626 if (streamId == mDeferredStreams[i]) {
1627 deferredStreamIndex = i;
1628 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001629 }
1630
Shuzhen Wang758c2152017-01-10 18:26:18 -08001631 }
1632 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1633 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1634 "(ID %d)", mCameraIdStr.string(), streamId);
1635 ALOGW("%s: %s", __FUNCTION__, msg.string());
1636 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001637 }
1638
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001639 if (mStreamInfoMap[streamId].finalized) {
1640 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1641 " on stream ID %d", mCameraIdStr.string(), streamId);
1642 ALOGW("%s: %s", __FUNCTION__, msg.string());
1643 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1644 }
1645
Zhijun He5d677d12016-05-29 16:52:39 -07001646 if (!mDevice.get()) {
1647 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1648 }
1649
Shuzhen Wang758c2152017-01-10 18:26:18 -08001650 std::vector<sp<Surface>> consumerSurfaces;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001651 const std::vector<int32_t> &sensorPixelModesUsed =
1652 outputConfiguration.getSensorPixelModesUsed();
Emilian Peevc81a7592022-02-14 17:38:18 -08001653 int64_t dynamicRangeProfile = outputConfiguration.getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001654 int32_t colorSpace = outputConfiguration.getColorSpace();
1655 int64_t streamUseCase = outputConfiguration.getStreamUseCase();
Shuzhen Wange4208922022-02-01 16:52:48 -08001656 int timestampBase = outputConfiguration.getTimestampBase();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001657 int mirrorMode = outputConfiguration.getMirrorMode();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001658 for (auto& bufferProducer : bufferProducers) {
1659 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001660 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1661 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001662 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001663 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001664 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001665 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001666
1667 sp<Surface> surface;
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07001668 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001669 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001670 mDevice->infoPhysical(physicalId), sensorPixelModesUsed, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001671 streamUseCase, timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001672
1673 if (!res.isOk())
1674 return res;
1675
1676 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001677 }
1678
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001679 // Gracefully handle case where finalizeOutputConfigurations is called
1680 // without any new surface.
1681 if (consumerSurfaces.size() == 0) {
1682 mStreamInfoMap[streamId].finalized = true;
1683 return res;
1684 }
1685
Zhijun He5d677d12016-05-29 16:52:39 -07001686 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001687 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001688 std::vector<int> consumerSurfaceIds;
1689 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001690 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001691 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1692 sp<IBinder> binder = IInterface::asBinder(
1693 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001694 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001695 binder.get(), streamId, consumerSurfaceIds[i]);
1696 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1697 }
1698 if (deferredStreamIndex != NAME_NOT_FOUND) {
1699 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001700 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001701 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001702 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001703 } else if (err == NO_INIT) {
1704 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001705 "Camera %s: Deferred surface is invalid: %s (%d)",
1706 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001707 } else {
1708 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001709 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1710 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001711 }
1712
1713 return res;
1714}
1715
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001716binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001717 ATRACE_CALL();
1718 binder::Status res;
1719 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1720
1721 if (!isValidAudioRestriction(mode)) {
1722 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1723 mCameraIdStr.string(), mode);
1724 ALOGW("%s: %s", __FUNCTION__, msg.string());
1725 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1726 }
1727
1728 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001729 BasicClient::setAudioRestriction(mode);
1730 return binder::Status::ok();
1731}
1732
1733binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1734 ATRACE_CALL();
1735 binder::Status res;
1736 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1737 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001738 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001739 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001740 }
1741 return binder::Status::ok();
1742}
1743
Ravneetaeb20dc2022-03-30 05:33:03 +00001744status_t CameraDeviceClient::setCameraServiceWatchdog(bool enabled) {
1745 return mDevice->setCameraServiceWatchdog(enabled);
1746}
1747
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001748status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1749 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1750
1751 return mDevice->setRotateAndCropAutoBehavior(
1752 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1753}
1754
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00001755status_t CameraDeviceClient::setAutoframingOverride(uint8_t autoframingValue) {
1756 if (autoframingValue > ANDROID_CONTROL_AUTOFRAMING_AUTO) return BAD_VALUE;
1757
1758 return mDevice->setAutoframingAutoBehavior(
1759 static_cast<camera_metadata_enum_android_control_autoframing_t>(autoframingValue));
1760}
1761
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001762bool CameraDeviceClient::supportsCameraMute() {
1763 return mDevice->supportsCameraMute();
1764}
1765
1766status_t CameraDeviceClient::setCameraMute(bool enabled) {
1767 return mDevice->setCameraMute(enabled);
1768}
1769
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001770binder::Status CameraDeviceClient::switchToOffline(
1771 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001772 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001773 /*out*/
1774 sp<hardware::camera2::ICameraOfflineSession>* session) {
1775 ATRACE_CALL();
1776
1777 binder::Status res;
1778 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1779
1780 Mutex::Autolock icl(mBinderSerializationLock);
1781
1782 if (!mDevice.get()) {
1783 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1784 }
1785
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001786 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001787 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001788 ALOGE("%s: %s", __FUNCTION__, msg.string());
1789 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1790 }
1791
1792 if (session == nullptr) {
1793 String8 msg = String8::format("Invalid offline session");
1794 ALOGE("%s: %s", __FUNCTION__, msg.string());
1795 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1796 }
1797
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001798 std::vector<int32_t> offlineStreamIds;
1799 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001800 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001801 for (const auto& streamId : offlineOutputIds) {
1802 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001803 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001804 String8 msg = String8::format("Offline surface with id: %d is not registered",
1805 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001806 ALOGE("%s: %s", __FUNCTION__, msg.string());
1807 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1808 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001809
1810 if (!mStreamInfoMap[streamId].supportsOffline) {
1811 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1812 "offline mode", streamId);
1813 ALOGE("%s: %s", __FUNCTION__, msg.string());
1814 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1815 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001816
Emilian Peev2f5d6012022-01-19 16:16:50 -08001817 Mutex::Autolock l(mCompositeLock);
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001818 bool isCompositeStream = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001819 for (const auto& gbp : mConfiguredOutputs.valueAt(index).getGraphicBufferProducers()) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001820 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
Pirama Arumuga Nainar8db21062022-01-28 10:15:12 -08001821 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) ||
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001822 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001823 if (isCompositeStream) {
1824 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1825 if (compositeIdx == NAME_NOT_FOUND) {
1826 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1827 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1828 "Unknown composite stream");
1829 }
1830
1831 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1832 &offlineStreamIds);
1833 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1834 mCompositeStreamMap.valueAt(compositeIdx));
1835 break;
1836 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001837 }
1838
Emilian Peev4697b642019-11-19 17:11:14 -08001839 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001840 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001841 }
1842 }
1843
1844 sp<CameraOfflineSessionBase> offlineSession;
1845 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1846 if (ret != OK) {
1847 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1848 "Camera %s: Error switching to offline mode: %s (%d)",
1849 mCameraIdStr.string(), strerror(ret), ret);
1850 }
1851
Emilian Peevcc0b7952020-01-07 13:54:47 -08001852 sp<CameraOfflineSessionClient> offlineClient;
1853 if (offlineSession.get() != nullptr) {
1854 offlineClient = new CameraOfflineSessionClient(sCameraService,
1855 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
Emilian Peev8b64f282021-03-25 16:49:57 -07001856 mClientFeatureId, mCameraIdStr, mCameraFacing, mOrientation, mClientPid, mClientUid,
1857 mServicePid);
Emilian Peevcc0b7952020-01-07 13:54:47 -08001858 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1859 }
1860
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001861 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001862 // A successful offline session switch must reset the current camera client
1863 // and release any resources occupied by previously configured streams.
1864 mStreamMap.clear();
1865 mConfiguredOutputs.clear();
1866 mDeferredStreams.clear();
1867 mStreamInfoMap.clear();
Emilian Peev2f5d6012022-01-19 16:16:50 -08001868 Mutex::Autolock l(mCompositeLock);
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001869 mCompositeStreamMap.clear();
1870 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001871 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001872 switch(ret) {
1873 case BAD_VALUE:
1874 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1875 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1876 case TIMED_OUT:
1877 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1878 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1879 default:
1880 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1881 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1882 strerror(-ret), ret);
1883 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001884 }
1885
1886 *session = offlineClient;
1887
1888 return binder::Status::ok();
1889}
1890
Igor Murashkine7ee7632013-06-11 18:10:18 -07001891status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001892 return BasicClient::dump(fd, args);
1893}
1894
1895status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001896 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001897 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001898 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001899 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001900 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001901
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001902 dprintf(fd, " State:\n");
1903 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001904 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001905 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001906 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001907 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001908 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001909 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001910 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001911 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001912 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001913 mStreamMap.valueAt(i).streamId(),
1914 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001915 }
Zhijun He5d677d12016-05-29 16:52:39 -07001916 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001917 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001918 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001919 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001920 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001921 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001922 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001923 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001924 // TODO: print dynamic/request section from most recent requests
1925 mFrameProcessor->dump(fd, args);
1926
1927 return dumpDevice(fd, args);
1928}
1929
Avichal Rakesh7e53cad2021-10-05 13:46:30 -07001930status_t CameraDeviceClient::startWatchingTags(const String8 &tags, int out) {
1931 sp<CameraDeviceBase> device = mDevice;
1932 if (!device) {
1933 dprintf(out, " Device is detached.");
1934 return OK;
1935 }
1936 device->startWatchingTags(tags);
1937 return OK;
1938}
1939
1940status_t CameraDeviceClient::stopWatchingTags(int out) {
1941 sp<CameraDeviceBase> device = mDevice;
1942 if (!device) {
1943 dprintf(out, " Device is detached.");
1944 return OK;
1945 }
1946 device->stopWatchingTags();
1947 return OK;
1948}
1949
1950status_t CameraDeviceClient::dumpWatchedEventsToVector(std::vector<std::string> &out) {
1951 sp<CameraDeviceBase> device = mDevice;
1952 if (!device) {
1953 return OK;
1954 }
1955 device->dumpWatchedEventsToVector(out);
1956 return OK;
1957}
1958
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001959void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001960 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001961 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001962 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001963
Emilian Peev538c90e2018-12-17 18:03:19 +00001964 bool skipClientNotification = false;
Emilian Peev2f5d6012022-01-19 16:16:50 -08001965 {
1966 // Access to the composite stream map must be synchronized
1967 Mutex::Autolock l(mCompositeLock);
1968 // Composites can have multiple internal streams. Error notifications coming from such
1969 // internal streams may need to remain within camera service.
1970 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1971 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode,
1972 resultExtras);
1973 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001974 }
1975
1976 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001977 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001978 }
1979}
1980
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001981void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1982 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1983
1984 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001985 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001986 }
1987
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001988 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001989 mStreamingRequestId = REQUEST_ID_NONE;
1990}
1991
Shuzhen Wang316781a2020-08-18 18:11:01 -07001992void CameraDeviceClient::notifyIdle(
1993 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1994 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001995 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001996 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001997
1998 if (remoteCb != 0) {
1999 remoteCb->onDeviceIdle();
2000 }
Shuzhen Wangd26b1862022-03-07 12:05:05 -08002001 Camera2ClientBase::notifyIdleWithUserTag(requestCount, resultErrorCount, deviceError,
Shuzhen Wang9372b0b2022-05-11 18:55:19 -07002002 streamStats, mUserTag, mVideoStabilizationMode);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002003}
2004
Jianing Weicb0652e2014-03-12 18:29:36 -07002005void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002006 nsecs_t timestamp) {
2007 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002008 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002009 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002010 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002011 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07002012 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002013
Emilian Peev2f5d6012022-01-19 16:16:50 -08002014 // Access to the composite stream map must be synchronized
2015 Mutex::Autolock l(mCompositeLock);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08002016 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2017 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
2018 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002019}
2020
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002021void CameraDeviceClient::notifyPrepared(int streamId) {
2022 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002023 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002024 if (remoteCb != 0) {
2025 remoteCb->onPrepared(streamId);
2026 }
2027}
2028
Shuzhen Wang9d066012016-09-30 11:30:20 -07002029void CameraDeviceClient::notifyRequestQueueEmpty() {
2030 // Thread safe. Don't bother locking.
2031 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
2032 if (remoteCb != 0) {
2033 remoteCb->onRequestQueueEmpty();
2034 }
2035}
2036
Igor Murashkine7ee7632013-06-11 18:10:18 -07002037void CameraDeviceClient::detachDevice() {
2038 if (mDevice == 0) return;
2039
Shuzhen Wang316781a2020-08-18 18:11:01 -07002040 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08002041 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07002042
Emilian Peev7ee731f2022-02-08 14:55:52 -08002043 if (mFrameProcessor.get() != nullptr) {
2044 mFrameProcessor->removeListener(
2045 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
2046 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID, /*listener*/this);
2047 mFrameProcessor->requestExit();
2048 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
2049 mFrameProcessor->join();
2050 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
2051 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002052
2053 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
2054 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002055 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07002056 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01002057 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
2058 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
2059 }
2060
Igor Murashkine7ee7632013-06-11 18:10:18 -07002061 if ((code = mDevice->waitUntilDrained()) != OK) {
2062 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
2063 code);
2064 }
2065 }
2066
Emilian Peev2f5d6012022-01-19 16:16:50 -08002067 {
2068 Mutex::Autolock l(mCompositeLock);
2069 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2070 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
2071 if (ret != OK) {
2072 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
2073 strerror(-ret), ret);
2074 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002075 }
Emilian Peev2f5d6012022-01-19 16:16:50 -08002076 mCompositeStreamMap.clear();
Emilian Peev5e4c7322019-10-22 14:20:52 -07002077 }
Emilian Peev5e4c7322019-10-22 14:20:52 -07002078
Igor Murashkine7ee7632013-06-11 18:10:18 -07002079 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002080
2081 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
Austin Borger74fca042022-05-23 12:41:21 -07002082 mCameraServiceProxyWrapper->logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002083}
2084
2085/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07002086void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07002087 ATRACE_CALL();
2088 ALOGV("%s", __FUNCTION__);
2089
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002090 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002091 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07002092 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002093 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
2094 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07002095 }
Emilian Peev538c90e2018-12-17 18:03:19 +00002096
Emilian Peev2f5d6012022-01-19 16:16:50 -08002097 // Access to the composite stream map must be synchronized
2098 Mutex::Autolock l(mCompositeLock);
Emilian Peev538c90e2018-12-17 18:03:19 +00002099 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
2100 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
2101 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07002102}
2103
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002104binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07002105 if (mDisconnected) {
2106 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
2107 "The camera device has been disconnected");
2108 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002109 status_t res = checkPid(checkLocation);
2110 return (res == OK) ? binder::Status::ok() :
2111 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
2112 "Attempt to use camera from a different process than original client");
2113}
2114
Igor Murashkine7ee7632013-06-11 18:10:18 -07002115// TODO: move to Camera2ClientBase
2116bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
2117
Jayant Chowdhary12361932018-08-27 14:46:13 -07002118 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002119 const int selfPid = getpid();
2120 camera_metadata_entry_t entry;
2121
2122 /**
2123 * Mixin default important security values
2124 * - android.led.transmit = defaulted ON
2125 */
2126 CameraMetadata staticInfo = mDevice->info();
2127 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
2128 for(size_t i = 0; i < entry.count; ++i) {
2129 uint8_t led = entry.data.u8[i];
2130
2131 switch(led) {
2132 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
2133 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
2134 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
2135 metadata.update(ANDROID_LED_TRANSMIT,
2136 &transmitDefault, 1);
2137 }
2138 break;
2139 }
2140 }
2141 }
2142
2143 // We can do anything!
2144 if (pid == selfPid) {
2145 return true;
2146 }
2147
2148 /**
2149 * Permission check special fields in the request
2150 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
2151 */
2152 entry = metadata.find(ANDROID_LED_TRANSMIT);
2153 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
2154 String16 permissionString =
2155 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
2156 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07002157 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07002158 ALOGE("Permission Denial: "
2159 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
2160 return false;
2161 }
2162 }
2163
2164 return true;
2165}
2166
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002167status_t CameraDeviceClient::getRotationTransformLocked(int mirrorMode,
2168 int32_t* transform) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002169 ALOGV("%s: begin", __FUNCTION__);
2170
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002171 const CameraMetadata& staticInfo = mDevice->info();
Shuzhen Wang610d7b82022-02-08 14:37:22 -08002172 return CameraUtils::getRotationTransform(staticInfo, mirrorMode, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07002173}
2174
Emilian Peevf4816702020-04-03 15:44:51 -07002175binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
2176 camera_request_template_t* tempId /*out*/) {
2177 binder::Status ret = binder::Status::ok();
2178
2179 if (tempId == nullptr) {
2180 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2181 "Camera %s: Invalid template argument", mCameraIdStr.string());
2182 return ret;
2183 }
2184 switch(templateId) {
2185 case ICameraDeviceUser::TEMPLATE_PREVIEW:
2186 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
2187 break;
2188 case ICameraDeviceUser::TEMPLATE_RECORD:
2189 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
2190 break;
2191 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
2192 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
2193 break;
2194 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
2195 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
2196 break;
2197 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
2198 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
2199 break;
2200 case ICameraDeviceUser::TEMPLATE_MANUAL:
2201 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
2202 break;
2203 default:
2204 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
2205 "Camera %s: Template ID %d is invalid or not supported",
2206 mCameraIdStr.string(), templateId);
2207 return ret;
2208 }
2209
2210 return ret;
2211}
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002212
2213const CameraMetadata &CameraDeviceClient::getStaticInfo(const String8 &cameraId) {
2214 if (mDevice->getId() == cameraId) {
2215 return mDevice->info();
2216 }
2217 return mDevice->infoPhysical(cameraId);
2218}
2219
2220bool CameraDeviceClient::isUltraHighResolutionSensor(const String8 &cameraId) {
2221 const CameraMetadata &deviceInfo = getStaticInfo(cameraId);
Shuzhen Wangd4abdf72021-05-28 11:22:50 -07002222 return SessionConfigurationUtils::isUltraHighResolutionSensor(deviceInfo);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08002223}
2224
2225bool CameraDeviceClient::isSensorPixelModeConsistent(
2226 const std::list<int> &streamIdList, const CameraMetadata &settings) {
2227 // First we get the sensorPixelMode from the settings metadata.
2228 int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
2229 camera_metadata_ro_entry sensorPixelModeEntry = settings.find(ANDROID_SENSOR_PIXEL_MODE);
2230 if (sensorPixelModeEntry.count != 0) {
2231 sensorPixelMode = sensorPixelModeEntry.data.u8[0];
2232 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT &&
2233 sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
2234 ALOGE("%s: Request sensor pixel mode not is not one of the valid values %d",
2235 __FUNCTION__, sensorPixelMode);
2236 return false;
2237 }
2238 }
2239 // Check whether each stream has max resolution allowed.
2240 bool consistent = true;
2241 for (auto it : streamIdList) {
2242 auto const streamInfoIt = mStreamInfoMap.find(it);
2243 if (streamInfoIt == mStreamInfoMap.end()) {
2244 ALOGE("%s: stream id %d not created, skipping", __FUNCTION__, it);
2245 return false;
2246 }
2247 consistent =
2248 streamInfoIt->second.sensorPixelModesUsed.find(sensorPixelMode) !=
2249 streamInfoIt->second.sensorPixelModesUsed.end();
2250 if (!consistent) {
2251 ALOGE("sensorPixelMode used %i not consistent with configured modes", sensorPixelMode);
2252 for (auto m : streamInfoIt->second.sensorPixelModesUsed) {
2253 ALOGE("sensor pixel mode used list: %i", m);
2254 }
2255 break;
2256 }
2257 }
2258
2259 return consistent;
2260}
2261
Igor Murashkine7ee7632013-06-11 18:10:18 -07002262} // namespace android