blob: 8cccbb18afe993fce173dbe1b94316166ee0507c [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070034#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070035
Emilian Peev00420d22018-02-05 21:33:13 +000036#include <camera_metadata_hidden.h>
37
Emilian Peev538c90e2018-12-17 18:03:19 +000038#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080039#include "HeicCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043#define STATUS_ERROR(errorCode, errorString) \
44 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080045 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080046
47#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
48 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080049 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070051
52namespace android {
53using namespace camera2;
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,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090060 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080061 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080062 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070063 int cameraFacing,
64 int clientPid,
65 uid_t clientUid,
66 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080067 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080068 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080069 clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080070 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080071 cameraId,
72 cameraFacing,
73 clientPid,
74 clientUid,
75 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070076 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080077 // We don't need it for API2 clients, but Camera2ClientBase requires it.
78 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070079}
Igor Murashkine7ee7632013-06-11 18:10:18 -070080
81// Interface used by CameraService
82
83CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080084 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
85 const String16& clientPackageName,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090086 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080087 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080088 int cameraFacing,
89 int clientPid,
90 uid_t clientUid,
91 int servicePid) :
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080092 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080093 cameraId, /*API1 camera ID*/ -1,
94 cameraFacing, clientPid, clientUid, servicePid),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070095 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070096 mStreamingRequestId(REQUEST_ID_NONE),
Igor Murashkine7ee7632013-06-11 18:10:18 -070097 mRequestIdCounter(0) {
98
99 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800100 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700101}
102
Emilian Peevbd8c5032018-02-14 23:05:40 +0000103status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
104 const String8& monitorTags) {
105 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800106}
107
108template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000109status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110 ATRACE_CALL();
111 status_t res;
112
Emilian Peevbd8c5032018-02-14 23:05:40 +0000113 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700114 if (res != OK) {
115 return res;
116 }
117
118 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700119 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800120 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700121 mFrameProcessor->run(threadName.string());
122
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800123 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
124 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800125 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700126 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700127
Emilian Peev00420d22018-02-05 21:33:13 +0000128 auto deviceInfo = mDevice->info();
129 camera_metadata_entry_t physicalKeysEntry = deviceInfo.find(
130 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
131 if (physicalKeysEntry.count > 0) {
132 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
133 physicalKeysEntry.data.i32,
134 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
135 }
136
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700137 mProviderManager = providerPtr;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700138 return OK;
139}
140
141CameraDeviceClient::~CameraDeviceClient() {
142}
143
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800144binder::Status CameraDeviceClient::submitRequest(
145 const hardware::camera2::CaptureRequest& request,
146 bool streaming,
147 /*out*/
148 hardware::camera2::utils::SubmitInfo *submitInfo) {
149 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
150 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700151}
152
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800153binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000154 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000155 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800156 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
157
158 // Trying to submit request with surface that wasn't created
159 if (idx == NAME_NOT_FOUND) {
160 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
161 " we have not called createStream on",
162 __FUNCTION__, mCameraIdStr.string());
163 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
164 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000165 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
166 != NAME_NOT_FOUND) {
167 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
168 currentStreamId);
169 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800170 }
171
172 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
173 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800174 outputStreamIds->push_back(streamSurfaceId.streamId());
175 }
176 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
177
178 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
179 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
180 streamSurfaceId.surfaceId());
181
Emilian Peevf873aa52018-01-26 14:58:28 +0000182 if (currentStreamId != nullptr) {
183 *currentStreamId = streamSurfaceId.streamId();
184 }
185
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800186 return binder::Status::ok();
187}
188
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800189binder::Status CameraDeviceClient::submitRequestList(
190 const std::vector<hardware::camera2::CaptureRequest>& requests,
191 bool streaming,
192 /*out*/
193 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700194 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700195 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700196
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800197 binder::Status res = binder::Status::ok();
198 status_t err;
199 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
200 return res;
201 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700202
203 Mutex::Autolock icl(mBinderSerializationLock);
204
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800205 if (!mDevice.get()) {
206 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
207 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700208
209 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800210 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
211 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800212 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700213 }
214
Emilian Peevaebbe412018-01-15 13:53:24 +0000215 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700216 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800217 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700218 uint32_t loopCounter = 0;
219
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800220 for (auto&& request: requests) {
221 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700222 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800223 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
224 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800225 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800226 "No input configured for camera %s but request is for reprocessing",
227 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700228 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800229 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
230 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800231 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
232 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000233 } else if (request.mPhysicalCameraSettings.size() > 1) {
234 ALOGE("%s: Camera %s: reprocess requests not supported for "
235 "multiple physical cameras.", __FUNCTION__,
236 mCameraIdStr.string());
237 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
238 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700239 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700240 }
241
Emilian Peevaebbe412018-01-15 13:53:24 +0000242 if (request.mPhysicalCameraSettings.empty()) {
243 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
244 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800245 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000246 "Request doesn't contain any settings");
247 }
248
249 //The first capture settings should always match the logical camera id
250 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
251 if (mDevice->getId() != logicalId) {
252 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
253 mCameraIdStr.string());
254 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
255 "Invalid camera request settings");
256 }
257
Emilian Peevaebbe412018-01-15 13:53:24 +0000258 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800259 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
260 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800261 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
262 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700263 }
264
Jianing Wei90e59c92014-03-12 18:29:36 -0700265 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700266 * Write in the output stream IDs and map from stream ID to surface ID
267 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700268 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700269 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700270 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000271 std::vector<std::string> requestedPhysicalIds;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800272 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800273 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800274 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700275
Emilian Peevf873aa52018-01-26 14:58:28 +0000276 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800277 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000278 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800279 if (!res.isOk()) {
280 return res;
281 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000282
283 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
284 if (index >= 0) {
285 String8 requestedPhysicalId(
286 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
287 requestedPhysicalIds.push_back(requestedPhysicalId.string());
288 } else {
289 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
290 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700291 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800292 } else {
293 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
294 int streamId = request.mStreamIdxList.itemAt(i);
295 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700296
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800297 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
298 if (index < 0) {
299 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
300 " we have not called createStream on: stream %d",
301 __FUNCTION__, mCameraIdStr.string(), streamId);
302 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
303 "Request targets Surface that is not part of current capture session");
304 }
305
306 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
307 if ((size_t)surfaceIdx >= gbps.size()) {
308 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
309 " we have not called createStream on: stream %d, surfaceIdx %d",
310 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
311 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
312 "Request targets Surface has invalid surface index");
313 }
314
Emilian Peevf873aa52018-01-26 14:58:28 +0000315 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800316 if (!res.isOk()) {
317 return res;
318 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000319
320 String8 requestedPhysicalId(
321 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
322 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700323 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700324 }
325
Emilian Peevf873aa52018-01-26 14:58:28 +0000326 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
327 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000328 if (it.settings.isEmpty()) {
329 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
330 __FUNCTION__, mCameraIdStr.string());
331 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
332 "Request settings are empty");
333 }
334
Emilian Peevf873aa52018-01-26 14:58:28 +0000335 String8 physicalId(it.id.c_str());
336 if (physicalId != mDevice->getId()) {
337 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
338 it.id);
339 if (found == requestedPhysicalIds.end()) {
340 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
341 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
342 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
343 "Invalid physical camera id");
344 }
Emilian Peev00420d22018-02-05 21:33:13 +0000345
346 if (!mSupportedPhysicalRequestKeys.empty()) {
347 // Filter out any unsupported physical request keys.
348 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
349 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
350 filteredParams.getAndLock());
351 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
352 filteredParams.unlock(meta);
353
354 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
355 camera_metadata_ro_entry entry = it.settings.find(keyIt);
356 if (entry.count > 0) {
357 filteredParams.update(entry);
358 }
359 }
360
361 physicalSettingsList.push_back({it.id, filteredParams});
362 }
363 } else {
364 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000365 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000366 }
367
368 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
369 // Callee logs
370 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
371 "Caller does not have permission to change restricted controls");
372 }
373
Emilian Peevaebbe412018-01-15 13:53:24 +0000374 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
375 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700376
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800377 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000378 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
379 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700380 }
381
Emilian Peevaebbe412018-01-15 13:53:24 +0000382 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
383 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700384 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800385 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
386 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
387 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700388
Emilian Peevaebbe412018-01-15 13:53:24 +0000389 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700390 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700391 }
392 mRequestIdCounter++;
393
394 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700395 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
396 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800397 if (err != OK) {
398 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800399 "Camera %s: Got error %s (%d) after trying to set streaming request",
400 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800401 ALOGE("%s: %s", __FUNCTION__, msg.string());
402 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
403 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700404 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700405 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700406 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700407 }
408 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700409 err = mDevice->captureList(metadataRequestList, surfaceMapList,
410 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800411 if (err != OK) {
412 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800413 "Camera %s: Got error %s (%d) after trying to submit capture request",
414 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800415 ALOGE("%s: %s", __FUNCTION__, msg.string());
416 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
417 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700418 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800419 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700420 }
421
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800422 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700423 return res;
424}
425
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800426binder::Status CameraDeviceClient::cancelRequest(
427 int requestId,
428 /*out*/
429 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700430 ATRACE_CALL();
431 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800433 status_t err;
434 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700435
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800436 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700437
438 Mutex::Autolock icl(mBinderSerializationLock);
439
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800440 if (!mDevice.get()) {
441 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
442 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700443
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700444 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700445 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800446 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
447 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800448 ALOGE("%s: %s", __FUNCTION__, msg.string());
449 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700450 }
451
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800452 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700453
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800454 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800455 ALOGV("%s: Camera %s: Successfully cleared streaming request",
456 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700457 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800458 } else {
459 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800460 "Camera %s: Error clearing streaming request: %s (%d)",
461 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700462 }
463
464 return res;
465}
466
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800467binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700468 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700469 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700470 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800471 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700472}
473
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100474binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700475 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800476 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700477 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700478 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
479 __FUNCTION__, mInputStream.configured ? 1 : 0,
480 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700481
Zhijun He0effd522016-03-04 10:22:27 -0800482 binder::Status res;
483 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
484
Emilian Peevcc0b7952020-01-07 13:54:47 -0800485 if (offlineStreamIds == nullptr) {
486 String8 msg = String8::format("Invalid offline stream ids");
487 ALOGE("%s: %s", __FUNCTION__, msg.string());
488 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
489 }
490
Zhijun He0effd522016-03-04 10:22:27 -0800491 Mutex::Autolock icl(mBinderSerializationLock);
492
493 if (!mDevice.get()) {
494 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
495 }
496
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000497 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
498 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000499 if (!res.isOk()) {
500 return res;
501 }
502
503 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
504 if (err == BAD_VALUE) {
505 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
506 mCameraIdStr.string());
507 ALOGE("%s: %s", __FUNCTION__, msg.string());
508 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
509 } else if (err != OK) {
510 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
511 mCameraIdStr.string(), strerror(-err), err);
512 ALOGE("%s: %s", __FUNCTION__, msg.string());
513 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000514 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800515 offlineStreamIds->clear();
516 mDevice->getOfflineStreamIds(offlineStreamIds);
517
Emilian Peev538c90e2018-12-17 18:03:19 +0000518 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
519 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800520 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000521 String8 msg = String8::format("Camera %s: Error configuring composite "
522 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
523 ALOGE("%s: %s", __FUNCTION__, msg.string());
524 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
525 break;
526 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800527
528 // Composite streams can only support offline mode in case all individual internal
529 // streams are also supported.
530 std::vector<int> internalStreams;
531 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800532 offlineStreamIds->erase(
533 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800534 [&internalStreams] (int streamId) {
535 auto it = std::find(internalStreams.begin(), internalStreams.end(),
536 streamId);
537 if (it != internalStreams.end()) {
538 internalStreams.erase(it);
539 return true;
540 }
541
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800542 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800543 if (internalStreams.empty()) {
544 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
545 }
546 }
547
548 for (const auto& offlineStreamId : *offlineStreamIds) {
549 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000550 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700551
552 nsecs_t configureEnd = systemTime();
553 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
554 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
555 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000556 }
557
558 return res;
559}
560
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800561binder::Status CameraDeviceClient::isSessionConfigurationSupported(
562 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
563 ATRACE_CALL();
564
565 binder::Status res;
566 status_t ret = OK;
567 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
568
569 Mutex::Autolock icl(mBinderSerializationLock);
570
571 if (!mDevice.get()) {
572 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
573 }
574
575 auto operatingMode = sessionConfiguration.getOperatingMode();
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000576 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
577 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800578 if (!res.isOk()) {
579 return res;
580 }
581
582 if (status == nullptr) {
583 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
584 ALOGE("%s: %s", __FUNCTION__, msg.string());
585 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
586 }
Shuzhen Wang83bff122020-11-20 15:51:39 -0800587 hardware::camera::device::V3_7::StreamConfiguration streamConfiguration;
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800588 bool earlyExit = false;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800589 metadataGetter getMetadata = [this](const String8 &id) {return mDevice->infoPhysical(id);};
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800590 std::vector<std::string> physicalCameraIds;
591 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &physicalCameraIds);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000592 res = SessionConfigurationUtils::convertToHALStreamCombination(sessionConfiguration,
593 mCameraIdStr, mDevice->info(), getMetadata, physicalCameraIds, streamConfiguration,
594 &earlyExit);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800595 if (!res.isOk()) {
596 return res;
597 }
598
599 if (earlyExit) {
600 *status = false;
601 return binder::Status::ok();
602 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000603
604 *status = false;
605 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
606 streamConfiguration, status);
607 switch (ret) {
608 case OK:
609 // Expected, do nothing.
610 break;
611 case INVALID_OPERATION: {
612 String8 msg = String8::format(
613 "Camera %s: Session configuration query not supported!",
614 mCameraIdStr.string());
615 ALOGD("%s: %s", __FUNCTION__, msg.string());
616 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
617 }
618
619 break;
620 default: {
621 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
622 strerror(-ret), ret);
623 ALOGE("%s: %s", __FUNCTION__, msg.string());
624 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
625 msg.string());
626 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800627 }
628
629 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700630}
631
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800632binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700633 ATRACE_CALL();
634 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
635
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800636 binder::Status res;
637 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700638
639 Mutex::Autolock icl(mBinderSerializationLock);
640
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800641 if (!mDevice.get()) {
642 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
643 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700644
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700645 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700646 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700647 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000648 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700649
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700650 if (mInputStream.configured && mInputStream.id == streamId) {
651 isInput = true;
652 } else {
653 // Guard against trying to delete non-created streams
654 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700655 if (streamId == mStreamMap.valueAt(i).streamId()) {
656 surfaces.push_back(mStreamMap.keyAt(i));
657 }
658 }
659
660 // See if this stream is one of the deferred streams.
661 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
662 if (streamId == mDeferredStreams[i]) {
663 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700664 break;
665 }
666 }
667
Emilian Peev538c90e2018-12-17 18:03:19 +0000668 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
669 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
670 compositeIndex = i;
671 break;
672 }
673 }
674
Shuzhen Wang0129d522016-10-30 22:43:41 -0700675 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
676 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
677 " stream created yet", mCameraIdStr.string(), streamId);
678 ALOGW("%s: %s", __FUNCTION__, msg.string());
679 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700680 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700681 }
682
683 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800684 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700685
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800686 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800687 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
688 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800689 ALOGE("%s: %s", __FUNCTION__, msg.string());
690 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
691 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700692 if (isInput) {
693 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700694 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700695 for (auto& surface : surfaces) {
696 mStreamMap.removeItem(surface);
697 }
698
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800699 mConfiguredOutputs.removeItem(streamId);
700
Shuzhen Wang0129d522016-10-30 22:43:41 -0700701 if (dIndex != NAME_NOT_FOUND) {
702 mDeferredStreams.removeItemsAt(dIndex);
703 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000704
705 if (compositeIndex != NAME_NOT_FOUND) {
706 status_t ret;
707 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
708 != OK) {
709 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
710 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
711 streamId);
712 ALOGE("%s: %s", __FUNCTION__, msg.string());
713 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
714 }
715 mCompositeStreamMap.removeItemsAt(compositeIndex);
716 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700717 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700718 }
719
720 return res;
721}
722
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800723binder::Status CameraDeviceClient::createStream(
724 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
725 /*out*/
726 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700727 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700728
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800729 binder::Status res;
730 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700731
732 Mutex::Autolock icl(mBinderSerializationLock);
733
Shuzhen Wang0129d522016-10-30 22:43:41 -0700734 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
735 outputConfiguration.getGraphicBufferProducers();
736 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800737 bool deferredConsumer = outputConfiguration.isDeferred();
738 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800739 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800740 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800741 bool isMultiResolution = outputConfiguration.isMultiResolution();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700742
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000743 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000744 outputConfiguration.getSurfaceType());
745 if (!res.isOk()) {
746 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700747 }
Zhijun He5d677d12016-05-29 16:52:39 -0700748
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800749 if (!mDevice.get()) {
750 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
751 }
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800752 std::vector<std::string> physicalCameraIds;
753 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &physicalCameraIds);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000754 res = SessionConfigurationUtils::checkPhysicalCameraId(physicalCameraIds, physicalCameraId,
755 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000756 if (!res.isOk()) {
757 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800758 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000759
Shuzhen Wang0129d522016-10-30 22:43:41 -0700760 std::vector<sp<Surface>> surfaces;
761 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700762 status_t err;
763
764 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700765 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800766 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700767 }
768
Shuzhen Wang758c2152017-01-10 18:26:18 -0800769 OutputStreamInfo streamInfo;
770 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700771 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700772 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800774 ssize_t index = mStreamMap.indexOfKey(binder);
775 if (index != NAME_NOT_FOUND) {
776 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
777 "(ID %zd)", mCameraIdStr.string(), index);
778 ALOGW("%s: %s", __FUNCTION__, msg.string());
779 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700780 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700781
Shuzhen Wang758c2152017-01-10 18:26:18 -0800782 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000783 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo, isStreamInfoValid,
784 surface, bufferProducer, mCameraIdStr, mDevice->infoPhysical(physicalCameraId));
Shuzhen Wang758c2152017-01-10 18:26:18 -0800785
786 if (!res.isOk())
787 return res;
788
789 if (!isStreamInfoValid) {
790 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700791 }
792
Shuzhen Wang758c2152017-01-10 18:26:18 -0800793 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700794 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800795 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700796
Zhijun He125684a2015-12-26 15:07:30 -0800797 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100798 std::vector<int> surfaceIds;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800799 bool isDepthCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
800 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
801 if (isDepthCompositeStream || isHeicCompisiteStream) {
802 sp<CompositeStream> compositeStream;
803 if (isDepthCompositeStream) {
804 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
805 } else {
806 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
807 }
808
Emilian Peev538c90e2018-12-17 18:03:19 +0000809 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
810 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700811 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev538c90e2018-12-17 18:03:19 +0000812 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
Shuzhen Wang83bff122020-11-20 15:51:39 -0800813 isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000814 if (err == OK) {
815 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
816 compositeStream);
817 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800818 } else {
819 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
820 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700821 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800822 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
Shuzhen Wang83bff122020-11-20 15:51:39 -0800823 isShared, isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +0000824 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700825
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800826 if (err != OK) {
827 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800828 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800829 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
830 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800831 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700832 int i = 0;
833 for (auto& binder : binders) {
834 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
835 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100836 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
837 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700838 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800839
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800840 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800841 mStreamInfoMap[streamId] = streamInfo;
842
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800843 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700844 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800845 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
846 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700847
Zhijun He5d677d12016-05-29 16:52:39 -0700848 // Set transform flags to ensure preview to be rotated correctly.
849 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700850
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800851 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700852 }
853
854 return res;
855}
856
Zhijun He5d677d12016-05-29 16:52:39 -0700857binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
858 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800859 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700860 /*out*/
861 int* newStreamId) {
862 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100863 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700864 android_dataspace dataSpace;
865 status_t err;
866 binder::Status res;
867
868 if (!mDevice.get()) {
869 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
870 }
871
872 // Infer the surface info for deferred surface stream creation.
873 width = outputConfiguration.getWidth();
874 height = outputConfiguration.getHeight();
875 surfaceType = outputConfiguration.getSurfaceType();
876 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
877 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
878 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
879 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
880 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
881 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
882 }
883 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700884 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100885 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800886 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700887 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
888 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700889 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800890 &streamId, physicalCameraId, &surfaceIds,
891 outputConfiguration.getSurfaceSetID(), isShared,
Shuzhen Wang83bff122020-11-20 15:51:39 -0800892 outputConfiguration.isMultiResolution(), consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700893
894 if (err != OK) {
895 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800896 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
897 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700898 } else {
899 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
900 // a separate list to track. Once the deferred surface is set, this id will be
901 // relocated to mStreamMap.
902 mDeferredStreams.push_back(streamId);
903
Shuzhen Wang758c2152017-01-10 18:26:18 -0800904 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
905 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
906
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800907 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700908 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800909 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700910
911 // Set transform flags to ensure preview to be rotated correctly.
912 res = setStreamTransformLocked(streamId);
913
914 *newStreamId = streamId;
915 }
916 return res;
917}
918
919binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
920 int32_t transform = 0;
921 status_t err;
922 binder::Status res;
923
924 if (!mDevice.get()) {
925 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
926 }
927
928 err = getRotationTransformLocked(&transform);
929 if (err != OK) {
930 // Error logged by getRotationTransformLocked.
931 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
932 "Unable to calculate rotation transform for new stream");
933 }
934
935 err = mDevice->setStreamTransform(streamId, transform);
936 if (err != OK) {
937 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
938 streamId);
939 ALOGE("%s: %s", __FUNCTION__, msg.string());
940 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
941 }
942
943 return res;
944}
Ruben Brunkbba75572014-11-20 17:29:50 -0800945
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800946binder::Status CameraDeviceClient::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800947 int width, int height, int format, bool isMultiResolution,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800948 /*out*/
949 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700950
951 ATRACE_CALL();
Shuzhen Wang83bff122020-11-20 15:51:39 -0800952 ALOGV("%s (w = %d, h = %d, f = 0x%x, isMultiResolution %d)", __FUNCTION__,
953 width, height, format, isMultiResolution);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700954
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800955 binder::Status res;
956 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700957
958 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800959
960 if (!mDevice.get()) {
961 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
962 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700963
964 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800965 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -0800966 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800967 ALOGE("%s: %s", __FUNCTION__, msg.string() );
968 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700969 }
970
971 int streamId = -1;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800972 status_t err = mDevice->createInputStream(width, height, format, isMultiResolution, &streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800973 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700974 mInputStream.configured = true;
975 mInputStream.width = width;
976 mInputStream.height = height;
977 mInputStream.format = format;
978 mInputStream.id = streamId;
979
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800980 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
981 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700982
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800983 *newStreamId = streamId;
984 } else {
985 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800986 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800987 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700988 }
989
990 return res;
991}
992
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800993binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700994
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800995 binder::Status res;
996 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
997
998 if (inputSurface == NULL) {
999 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001000 }
1001
1002 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001003 if (!mDevice.get()) {
1004 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1005 }
1006 sp<IGraphicBufferProducer> producer;
1007 status_t err = mDevice->getInputBufferProducer(&producer);
1008 if (err != OK) {
1009 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001010 "Camera %s: Error getting input Surface: %s (%d)",
1011 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001012 } else {
1013 inputSurface->name = String16("CameraInput");
1014 inputSurface->graphicBufferProducer = producer;
1015 }
1016 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001017}
1018
Emilian Peev40ead602017-09-26 15:46:36 +01001019binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1020 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1021 ATRACE_CALL();
1022
1023 binder::Status res;
1024 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1025
1026 Mutex::Autolock icl(mBinderSerializationLock);
1027
1028 if (!mDevice.get()) {
1029 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1030 }
1031
1032 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1033 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001034 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1035
Emilian Peev40ead602017-09-26 15:46:36 +01001036 auto producerCount = bufferProducers.size();
1037 if (producerCount == 0) {
1038 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1039 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1040 "bufferProducers must not be empty");
1041 }
1042
1043 // The first output is the one associated with the output configuration.
1044 // It should always be present, valid and the corresponding stream id should match.
1045 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1046 ssize_t index = mStreamMap.indexOfKey(binder);
1047 if (index == NAME_NOT_FOUND) {
1048 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1049 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1050 "OutputConfiguration is invalid");
1051 }
1052 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1053 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1054 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1055 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1056 "Stream id is invalid");
1057 }
1058
1059 std::vector<size_t> removedSurfaceIds;
1060 std::vector<sp<IBinder>> removedOutputs;
1061 std::vector<sp<Surface>> newOutputs;
1062 std::vector<OutputStreamInfo> streamInfos;
1063 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1064 for (auto &it : bufferProducers) {
1065 newOutputsMap.add(IInterface::asBinder(it), it);
1066 }
1067
1068 for (size_t i = 0; i < mStreamMap.size(); i++) {
1069 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1070 if (idx == NAME_NOT_FOUND) {
1071 if (mStreamMap[i].streamId() == streamId) {
1072 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1073 removedOutputs.push_back(mStreamMap.keyAt(i));
1074 }
1075 } else {
1076 if (mStreamMap[i].streamId() != streamId) {
1077 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1078 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1079 "Target Surface is invalid");
1080 }
1081 newOutputsMap.removeItemsAt(idx);
1082 }
1083 }
1084
1085 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1086 OutputStreamInfo outInfo;
1087 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001088 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false,
1089 surface, newOutputsMap.valueAt(i), mCameraIdStr,
1090 mDevice->infoPhysical(physicalCameraId));
Emilian Peev40ead602017-09-26 15:46:36 +01001091 if (!res.isOk())
1092 return res;
1093
Emilian Peev40ead602017-09-26 15:46:36 +01001094 streamInfos.push_back(outInfo);
1095 newOutputs.push_back(surface);
1096 }
1097
1098 //Trivial case no changes required
1099 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1100 return binder::Status::ok();
1101 }
1102
1103 KeyedVector<sp<Surface>, size_t> outputMap;
1104 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1105 &outputMap);
1106 if (ret != OK) {
1107 switch (ret) {
1108 case NAME_NOT_FOUND:
1109 case BAD_VALUE:
1110 case -EBUSY:
1111 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1112 "Camera %s: Error updating stream: %s (%d)",
1113 mCameraIdStr.string(), strerror(ret), ret);
1114 break;
1115 default:
1116 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1117 "Camera %s: Error updating stream: %s (%d)",
1118 mCameraIdStr.string(), strerror(ret), ret);
1119 break;
1120 }
1121 } else {
1122 for (const auto &it : removedOutputs) {
1123 mStreamMap.removeItem(it);
1124 }
1125
1126 for (size_t i = 0; i < outputMap.size(); i++) {
1127 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1128 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1129 }
1130
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001131 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1132
Emilian Peev40ead602017-09-26 15:46:36 +01001133 ALOGV("%s: Camera %s: Successful stream ID %d update",
1134 __FUNCTION__, mCameraIdStr.string(), streamId);
1135 }
1136
1137 return res;
1138}
1139
Igor Murashkine7ee7632013-06-11 18:10:18 -07001140// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001141binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1142 /*out*/
1143 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001144{
1145 ATRACE_CALL();
1146 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1147
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001148 binder::Status res;
1149 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001150
1151 Mutex::Autolock icl(mBinderSerializationLock);
1152
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001153 if (!mDevice.get()) {
1154 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1155 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001156
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001157 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001158 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1159 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1160
1161 CameraMetadata metadata;
1162 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001163 request != NULL) {
1164
1165 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001166 } else if (err == BAD_VALUE) {
1167 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001168 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1169 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001170
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001171 } else {
1172 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001173 "Camera %s: Error creating default request for template %d: %s (%d)",
1174 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001175 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001176 return res;
1177}
1178
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001179binder::Status CameraDeviceClient::getCameraInfo(
1180 /*out*/
1181 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001182{
1183 ATRACE_CALL();
1184 ALOGV("%s", __FUNCTION__);
1185
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001186 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001187
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001188 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001189
1190 Mutex::Autolock icl(mBinderSerializationLock);
1191
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001192 if (!mDevice.get()) {
1193 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1194 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001195
Igor Murashkin099b4572013-07-12 17:52:16 -07001196 if (info != NULL) {
1197 *info = mDevice->info(); // static camera metadata
1198 // TODO: merge with device-specific camera metadata
1199 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001200
1201 return res;
1202}
1203
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001204binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001205{
1206 ATRACE_CALL();
1207 ALOGV("%s", __FUNCTION__);
1208
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001209 binder::Status res;
1210 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001211
1212 Mutex::Autolock icl(mBinderSerializationLock);
1213
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001214 if (!mDevice.get()) {
1215 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1216 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001217
1218 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001219 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001220 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001221 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001222 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1223 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001224 ALOGE("%s: %s", __FUNCTION__, msg.string());
1225 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001226 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001227 status_t err = mDevice->waitUntilDrained();
1228 if (err != OK) {
1229 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001230 "Camera %s: Error waiting to drain: %s (%d)",
1231 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001232 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001233 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001234 return res;
1235}
1236
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001237binder::Status CameraDeviceClient::flush(
1238 /*out*/
1239 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001240 ATRACE_CALL();
1241 ALOGV("%s", __FUNCTION__);
1242
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001243 binder::Status res;
1244 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001245
1246 Mutex::Autolock icl(mBinderSerializationLock);
1247
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001248 if (!mDevice.get()) {
1249 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1250 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001251
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001252 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001253 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001254 status_t err = mDevice->flush(lastFrameNumber);
1255 if (err != OK) {
1256 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001257 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001258 }
1259 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001260}
1261
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001262binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001263 ATRACE_CALL();
1264 ALOGV("%s", __FUNCTION__);
1265
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001266 binder::Status res;
1267 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001268
1269 Mutex::Autolock icl(mBinderSerializationLock);
1270
1271 // Guard against trying to prepare non-created streams
1272 ssize_t index = NAME_NOT_FOUND;
1273 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001274 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001275 index = i;
1276 break;
1277 }
1278 }
1279
1280 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001281 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1282 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001283 ALOGW("%s: %s", __FUNCTION__, msg.string());
1284 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001285 }
1286
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001287 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1288 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001289 status_t err = mDevice->prepare(streamId);
1290 if (err == BAD_VALUE) {
1291 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001292 "Camera %s: Stream %d has already been used, and cannot be prepared",
1293 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001294 } else if (err != OK) {
1295 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001296 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001297 strerror(-err), err);
1298 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001299 return res;
1300}
1301
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001302binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001303 ATRACE_CALL();
1304 ALOGV("%s", __FUNCTION__);
1305
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001306 binder::Status res;
1307 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001308
1309 Mutex::Autolock icl(mBinderSerializationLock);
1310
1311 // Guard against trying to prepare non-created streams
1312 ssize_t index = NAME_NOT_FOUND;
1313 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001314 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001315 index = i;
1316 break;
1317 }
1318 }
1319
1320 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001321 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1322 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001323 ALOGW("%s: %s", __FUNCTION__, msg.string());
1324 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001325 }
1326
1327 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001328 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1329 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001330 ALOGE("%s: %s", __FUNCTION__, msg.string());
1331 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001332 }
1333
1334 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1335 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001336 status_t err = mDevice->prepare(maxCount, streamId);
1337 if (err == BAD_VALUE) {
1338 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001339 "Camera %s: Stream %d has already been used, and cannot be prepared",
1340 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001341 } else if (err != OK) {
1342 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001343 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001344 strerror(-err), err);
1345 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001346
1347 return res;
1348}
1349
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001350binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001351 ATRACE_CALL();
1352 ALOGV("%s", __FUNCTION__);
1353
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001354 binder::Status res;
1355 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001356
1357 Mutex::Autolock icl(mBinderSerializationLock);
1358
1359 // Guard against trying to prepare non-created streams
1360 ssize_t index = NAME_NOT_FOUND;
1361 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001362 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001363 index = i;
1364 break;
1365 }
1366 }
1367
1368 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001369 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1370 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001371 ALOGW("%s: %s", __FUNCTION__, msg.string());
1372 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001373 }
1374
1375 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1376 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001377 status_t err = mDevice->tearDown(streamId);
1378 if (err == BAD_VALUE) {
1379 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001380 "Camera %s: Stream %d is still in use, cannot be torn down",
1381 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001382 } else if (err != OK) {
1383 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001384 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001385 strerror(-err), err);
1386 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001387
1388 return res;
1389}
1390
Shuzhen Wang758c2152017-01-10 18:26:18 -08001391binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001392 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1393 ATRACE_CALL();
1394
1395 binder::Status res;
1396 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1397
1398 Mutex::Autolock icl(mBinderSerializationLock);
1399
Shuzhen Wang0129d522016-10-30 22:43:41 -07001400 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1401 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001402 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001403
Shuzhen Wang0129d522016-10-30 22:43:41 -07001404 if (bufferProducers.size() == 0) {
1405 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001406 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1407 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001408
Shuzhen Wang758c2152017-01-10 18:26:18 -08001409 // streamId should be in mStreamMap if this stream already has a surface attached
1410 // to it. Otherwise, it should be in mDeferredStreams.
1411 bool streamIdConfigured = false;
1412 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1413 for (size_t i = 0; i < mStreamMap.size(); i++) {
1414 if (mStreamMap.valueAt(i).streamId() == streamId) {
1415 streamIdConfigured = true;
1416 break;
1417 }
Zhijun He5d677d12016-05-29 16:52:39 -07001418 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001419 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1420 if (streamId == mDeferredStreams[i]) {
1421 deferredStreamIndex = i;
1422 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001423 }
1424
Shuzhen Wang758c2152017-01-10 18:26:18 -08001425 }
1426 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1427 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1428 "(ID %d)", mCameraIdStr.string(), streamId);
1429 ALOGW("%s: %s", __FUNCTION__, msg.string());
1430 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001431 }
1432
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001433 if (mStreamInfoMap[streamId].finalized) {
1434 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1435 " on stream ID %d", mCameraIdStr.string(), streamId);
1436 ALOGW("%s: %s", __FUNCTION__, msg.string());
1437 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1438 }
1439
Zhijun He5d677d12016-05-29 16:52:39 -07001440 if (!mDevice.get()) {
1441 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1442 }
1443
Shuzhen Wang758c2152017-01-10 18:26:18 -08001444 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001445 for (auto& bufferProducer : bufferProducers) {
1446 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001447 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1448 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001449 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001450 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001451 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001452 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001453
1454 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001455 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
1456 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
1457 mDevice->infoPhysical(physicalId));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001458
1459 if (!res.isOk())
1460 return res;
1461
1462 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001463 }
1464
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001465 // Gracefully handle case where finalizeOutputConfigurations is called
1466 // without any new surface.
1467 if (consumerSurfaces.size() == 0) {
1468 mStreamInfoMap[streamId].finalized = true;
1469 return res;
1470 }
1471
Zhijun He5d677d12016-05-29 16:52:39 -07001472 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001473 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001474 std::vector<int> consumerSurfaceIds;
1475 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001476 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001477 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1478 sp<IBinder> binder = IInterface::asBinder(
1479 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001480 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001481 binder.get(), streamId, consumerSurfaceIds[i]);
1482 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1483 }
1484 if (deferredStreamIndex != NAME_NOT_FOUND) {
1485 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001486 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001487 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001488 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001489 } else if (err == NO_INIT) {
1490 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001491 "Camera %s: Deferred surface is invalid: %s (%d)",
1492 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001493 } else {
1494 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001495 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1496 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001497 }
1498
1499 return res;
1500}
1501
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001502binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001503 ATRACE_CALL();
1504 binder::Status res;
1505 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1506
1507 if (!isValidAudioRestriction(mode)) {
1508 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1509 mCameraIdStr.string(), mode);
1510 ALOGW("%s: %s", __FUNCTION__, msg.string());
1511 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1512 }
1513
1514 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001515 BasicClient::setAudioRestriction(mode);
1516 return binder::Status::ok();
1517}
1518
1519binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1520 ATRACE_CALL();
1521 binder::Status res;
1522 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1523 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001524 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001525 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001526 }
1527 return binder::Status::ok();
1528}
1529
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001530status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1531 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1532
1533 return mDevice->setRotateAndCropAutoBehavior(
1534 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1535}
1536
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08001537bool CameraDeviceClient::supportsCameraMute() {
1538 return mDevice->supportsCameraMute();
1539}
1540
1541status_t CameraDeviceClient::setCameraMute(bool enabled) {
1542 return mDevice->setCameraMute(enabled);
1543}
1544
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001545binder::Status CameraDeviceClient::switchToOffline(
1546 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001547 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001548 /*out*/
1549 sp<hardware::camera2::ICameraOfflineSession>* session) {
1550 ATRACE_CALL();
1551
1552 binder::Status res;
1553 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1554
1555 Mutex::Autolock icl(mBinderSerializationLock);
1556
1557 if (!mDevice.get()) {
1558 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1559 }
1560
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001561 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001562 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001563 ALOGE("%s: %s", __FUNCTION__, msg.string());
1564 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1565 }
1566
1567 if (session == nullptr) {
1568 String8 msg = String8::format("Invalid offline session");
1569 ALOGE("%s: %s", __FUNCTION__, msg.string());
1570 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1571 }
1572
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001573 std::vector<int32_t> offlineStreamIds;
1574 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001575 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001576 for (const auto& streamId : offlineOutputIds) {
1577 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001578 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001579 String8 msg = String8::format("Offline surface with id: %d is not registered",
1580 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001581 ALOGE("%s: %s", __FUNCTION__, msg.string());
1582 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1583 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001584
1585 if (!mStreamInfoMap[streamId].supportsOffline) {
1586 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1587 "offline mode", streamId);
1588 ALOGE("%s: %s", __FUNCTION__, msg.string());
1589 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1590 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001591
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001592 bool isCompositeStream = false;
1593 for (const auto& gbp : mConfiguredOutputs[streamId].getGraphicBufferProducers()) {
1594 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
1595 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) |
1596 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001597 if (isCompositeStream) {
1598 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1599 if (compositeIdx == NAME_NOT_FOUND) {
1600 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1601 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1602 "Unknown composite stream");
1603 }
1604
1605 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1606 &offlineStreamIds);
1607 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1608 mCompositeStreamMap.valueAt(compositeIdx));
1609 break;
1610 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001611 }
1612
Emilian Peev4697b642019-11-19 17:11:14 -08001613 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001614 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001615 }
1616 }
1617
1618 sp<CameraOfflineSessionBase> offlineSession;
1619 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1620 if (ret != OK) {
1621 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1622 "Camera %s: Error switching to offline mode: %s (%d)",
1623 mCameraIdStr.string(), strerror(ret), ret);
1624 }
1625
Emilian Peevcc0b7952020-01-07 13:54:47 -08001626 sp<CameraOfflineSessionClient> offlineClient;
1627 if (offlineSession.get() != nullptr) {
1628 offlineClient = new CameraOfflineSessionClient(sCameraService,
1629 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
1630 mClientFeatureId, mCameraIdStr, mCameraFacing, mClientPid, mClientUid, mServicePid);
1631 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1632 }
1633
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001634 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001635 // A successful offline session switch must reset the current camera client
1636 // and release any resources occupied by previously configured streams.
1637 mStreamMap.clear();
1638 mConfiguredOutputs.clear();
1639 mDeferredStreams.clear();
1640 mStreamInfoMap.clear();
1641 mCompositeStreamMap.clear();
1642 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001643 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001644 switch(ret) {
1645 case BAD_VALUE:
1646 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1647 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1648 case TIMED_OUT:
1649 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1650 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1651 default:
1652 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1653 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1654 strerror(-ret), ret);
1655 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001656 }
1657
1658 *session = offlineClient;
1659
1660 return binder::Status::ok();
1661}
1662
Igor Murashkine7ee7632013-06-11 18:10:18 -07001663status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001664 return BasicClient::dump(fd, args);
1665}
1666
1667status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001668 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001669 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001670 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001671 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001672 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001673
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001674 dprintf(fd, " State:\n");
1675 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001676 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001677 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001678 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001679 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001680 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001681 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001682 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001683 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001684 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001685 mStreamMap.valueAt(i).streamId(),
1686 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001687 }
Zhijun He5d677d12016-05-29 16:52:39 -07001688 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001689 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001690 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001691 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001692 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001693 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001694 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001695 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001696 // TODO: print dynamic/request section from most recent requests
1697 mFrameProcessor->dump(fd, args);
1698
1699 return dumpDevice(fd, args);
1700}
1701
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001702void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001703 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001704 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001705 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001706
Emilian Peev538c90e2018-12-17 18:03:19 +00001707 // Composites can have multiple internal streams. Error notifications coming from such internal
1708 // streams may need to remain within camera service.
1709 bool skipClientNotification = false;
1710 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1711 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
1712 }
1713
1714 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001715 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001716 }
1717}
1718
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001719void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1720 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1721
1722 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001723 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001724 }
1725
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001726 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001727 mStreamingRequestId = REQUEST_ID_NONE;
1728}
1729
Shuzhen Wang316781a2020-08-18 18:11:01 -07001730void CameraDeviceClient::notifyIdle(
1731 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1732 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001733 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001734 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001735
1736 if (remoteCb != 0) {
1737 remoteCb->onDeviceIdle();
1738 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001739 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001740}
1741
Jianing Weicb0652e2014-03-12 18:29:36 -07001742void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001743 nsecs_t timestamp) {
1744 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001745 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001746 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001747 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001748 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001749 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001750
1751 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1752 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1753 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001754}
1755
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001756void CameraDeviceClient::notifyPrepared(int streamId) {
1757 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001758 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001759 if (remoteCb != 0) {
1760 remoteCb->onPrepared(streamId);
1761 }
1762}
1763
Shuzhen Wang9d066012016-09-30 11:30:20 -07001764void CameraDeviceClient::notifyRequestQueueEmpty() {
1765 // Thread safe. Don't bother locking.
1766 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1767 if (remoteCb != 0) {
1768 remoteCb->onRequestQueueEmpty();
1769 }
1770}
1771
Igor Murashkine7ee7632013-06-11 18:10:18 -07001772void CameraDeviceClient::detachDevice() {
1773 if (mDevice == 0) return;
1774
Shuzhen Wang316781a2020-08-18 18:11:01 -07001775 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001776 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001777
Emilian Peevfaa4bde2020-01-23 12:19:37 -08001778 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
1779 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Igor Murashkine7ee7632013-06-11 18:10:18 -07001780 /*listener*/this);
1781 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001782 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001783 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001784 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001785
1786 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1787 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001788 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001789 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001790 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
1791 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
1792 }
1793
Igor Murashkine7ee7632013-06-11 18:10:18 -07001794 if ((code = mDevice->waitUntilDrained()) != OK) {
1795 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1796 code);
1797 }
1798 }
1799
Emilian Peev5e4c7322019-10-22 14:20:52 -07001800 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1801 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
1802 if (ret != OK) {
1803 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
1804 strerror(-ret), ret);
1805 }
1806 }
1807 mCompositeStreamMap.clear();
1808
Igor Murashkine7ee7632013-06-11 18:10:18 -07001809 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001810
1811 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
1812 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001813}
1814
1815/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001816void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001817 ATRACE_CALL();
1818 ALOGV("%s", __FUNCTION__);
1819
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001820 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001821 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001822 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001823 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
1824 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001825 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001826
1827 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1828 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
1829 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001830}
1831
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001832binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001833 if (mDisconnected) {
1834 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1835 "The camera device has been disconnected");
1836 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001837 status_t res = checkPid(checkLocation);
1838 return (res == OK) ? binder::Status::ok() :
1839 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1840 "Attempt to use camera from a different process than original client");
1841}
1842
Igor Murashkine7ee7632013-06-11 18:10:18 -07001843// TODO: move to Camera2ClientBase
1844bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1845
Jayant Chowdhary12361932018-08-27 14:46:13 -07001846 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001847 const int selfPid = getpid();
1848 camera_metadata_entry_t entry;
1849
1850 /**
1851 * Mixin default important security values
1852 * - android.led.transmit = defaulted ON
1853 */
1854 CameraMetadata staticInfo = mDevice->info();
1855 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1856 for(size_t i = 0; i < entry.count; ++i) {
1857 uint8_t led = entry.data.u8[i];
1858
1859 switch(led) {
1860 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1861 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1862 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1863 metadata.update(ANDROID_LED_TRANSMIT,
1864 &transmitDefault, 1);
1865 }
1866 break;
1867 }
1868 }
1869 }
1870
1871 // We can do anything!
1872 if (pid == selfPid) {
1873 return true;
1874 }
1875
1876 /**
1877 * Permission check special fields in the request
1878 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1879 */
1880 entry = metadata.find(ANDROID_LED_TRANSMIT);
1881 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1882 String16 permissionString =
1883 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1884 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001885 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001886 ALOGE("Permission Denial: "
1887 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1888 return false;
1889 }
1890 }
1891
1892 return true;
1893}
1894
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001895status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1896 ALOGV("%s: begin", __FUNCTION__);
1897
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001898 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001899 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001900}
1901
Emilian Peevf4816702020-04-03 15:44:51 -07001902binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
1903 camera_request_template_t* tempId /*out*/) {
1904 binder::Status ret = binder::Status::ok();
1905
1906 if (tempId == nullptr) {
1907 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1908 "Camera %s: Invalid template argument", mCameraIdStr.string());
1909 return ret;
1910 }
1911 switch(templateId) {
1912 case ICameraDeviceUser::TEMPLATE_PREVIEW:
1913 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
1914 break;
1915 case ICameraDeviceUser::TEMPLATE_RECORD:
1916 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
1917 break;
1918 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
1919 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
1920 break;
1921 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
1922 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
1923 break;
1924 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
1925 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
1926 break;
1927 case ICameraDeviceUser::TEMPLATE_MANUAL:
1928 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
1929 break;
1930 default:
1931 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1932 "Camera %s: Template ID %d is invalid or not supported",
1933 mCameraIdStr.string(), templateId);
1934 return ret;
1935 }
1936
1937 return ret;
1938}
Igor Murashkine7ee7632013-06-11 18:10:18 -07001939} // namespace android