blob: 6e1aba9ac01645c74c40a36b066c3fcb3ce42235 [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 }
587 hardware::camera::device::V3_4::StreamConfiguration streamConfiguration;
588 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 Wang0129d522016-10-30 22:43:41 -0700741
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000742 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000743 outputConfiguration.getSurfaceType());
744 if (!res.isOk()) {
745 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700746 }
Zhijun He5d677d12016-05-29 16:52:39 -0700747
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800748 if (!mDevice.get()) {
749 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
750 }
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800751 std::vector<std::string> physicalCameraIds;
752 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &physicalCameraIds);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000753 res = SessionConfigurationUtils::checkPhysicalCameraId(physicalCameraIds, physicalCameraId,
754 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000755 if (!res.isOk()) {
756 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800757 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000758
Shuzhen Wang0129d522016-10-30 22:43:41 -0700759 std::vector<sp<Surface>> surfaces;
760 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700761 status_t err;
762
763 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700764 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800765 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700766 }
767
Shuzhen Wang758c2152017-01-10 18:26:18 -0800768 OutputStreamInfo streamInfo;
769 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700770 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700771 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700772 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800773 ssize_t index = mStreamMap.indexOfKey(binder);
774 if (index != NAME_NOT_FOUND) {
775 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
776 "(ID %zd)", mCameraIdStr.string(), index);
777 ALOGW("%s: %s", __FUNCTION__, msg.string());
778 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700779 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700780
Shuzhen Wang758c2152017-01-10 18:26:18 -0800781 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000782 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo, isStreamInfoValid,
783 surface, bufferProducer, mCameraIdStr, mDevice->infoPhysical(physicalCameraId));
Shuzhen Wang758c2152017-01-10 18:26:18 -0800784
785 if (!res.isOk())
786 return res;
787
788 if (!isStreamInfoValid) {
789 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700790 }
791
Shuzhen Wang758c2152017-01-10 18:26:18 -0800792 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700793 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800794 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700795
Zhijun He125684a2015-12-26 15:07:30 -0800796 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100797 std::vector<int> surfaceIds;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800798 bool isDepthCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
799 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
800 if (isDepthCompositeStream || isHeicCompisiteStream) {
801 sp<CompositeStream> compositeStream;
802 if (isDepthCompositeStream) {
803 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
804 } else {
805 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
806 }
807
Emilian Peev538c90e2018-12-17 18:03:19 +0000808 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
809 streamInfo.height, streamInfo.format,
Emilian Peevf4816702020-04-03 15:44:51 -0700810 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev538c90e2018-12-17 18:03:19 +0000811 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
812 isShared);
813 if (err == OK) {
814 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
815 compositeStream);
816 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800817 } else {
818 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
819 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700820 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800821 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
822 isShared);
Emilian Peev538c90e2018-12-17 18:03:19 +0000823 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700824
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800825 if (err != OK) {
826 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800827 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800828 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
829 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800830 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700831 int i = 0;
832 for (auto& binder : binders) {
833 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
834 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100835 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
836 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700837 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800838
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800839 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800840 mStreamInfoMap[streamId] = streamInfo;
841
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800842 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700843 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800844 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
845 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700846
Zhijun He5d677d12016-05-29 16:52:39 -0700847 // Set transform flags to ensure preview to be rotated correctly.
848 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700849
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800850 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700851 }
852
853 return res;
854}
855
Zhijun He5d677d12016-05-29 16:52:39 -0700856binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
857 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800858 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700859 /*out*/
860 int* newStreamId) {
861 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100862 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700863 android_dataspace dataSpace;
864 status_t err;
865 binder::Status res;
866
867 if (!mDevice.get()) {
868 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
869 }
870
871 // Infer the surface info for deferred surface stream creation.
872 width = outputConfiguration.getWidth();
873 height = outputConfiguration.getHeight();
874 surfaceType = outputConfiguration.getSurfaceType();
875 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
876 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
877 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
878 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
879 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
880 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
881 }
882 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700883 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100884 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800885 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700886 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
887 height, format, dataSpace,
Emilian Peevf4816702020-04-03 15:44:51 -0700888 static_cast<camera_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800889 &streamId, physicalCameraId, &surfaceIds,
890 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev40ead602017-09-26 15:46:36 +0100891 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700892
893 if (err != OK) {
894 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800895 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
896 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700897 } else {
898 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
899 // a separate list to track. Once the deferred surface is set, this id will be
900 // relocated to mStreamMap.
901 mDeferredStreams.push_back(streamId);
902
Shuzhen Wang758c2152017-01-10 18:26:18 -0800903 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
904 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
905
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800906 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700907 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800908 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700909
910 // Set transform flags to ensure preview to be rotated correctly.
911 res = setStreamTransformLocked(streamId);
912
913 *newStreamId = streamId;
914 }
915 return res;
916}
917
918binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
919 int32_t transform = 0;
920 status_t err;
921 binder::Status res;
922
923 if (!mDevice.get()) {
924 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
925 }
926
927 err = getRotationTransformLocked(&transform);
928 if (err != OK) {
929 // Error logged by getRotationTransformLocked.
930 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
931 "Unable to calculate rotation transform for new stream");
932 }
933
934 err = mDevice->setStreamTransform(streamId, transform);
935 if (err != OK) {
936 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
937 streamId);
938 ALOGE("%s: %s", __FUNCTION__, msg.string());
939 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
940 }
941
942 return res;
943}
Ruben Brunkbba75572014-11-20 17:29:50 -0800944
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800945binder::Status CameraDeviceClient::createInputStream(
946 int width, int height, int format,
947 /*out*/
948 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700949
950 ATRACE_CALL();
951 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
952
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800953 binder::Status res;
954 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700955
956 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800957
958 if (!mDevice.get()) {
959 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
960 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700961
962 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800963 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -0800964 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800965 ALOGE("%s: %s", __FUNCTION__, msg.string() );
966 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700967 }
968
969 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800970 status_t err = mDevice->createInputStream(width, height, format, &streamId);
971 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700972 mInputStream.configured = true;
973 mInputStream.width = width;
974 mInputStream.height = height;
975 mInputStream.format = format;
976 mInputStream.id = streamId;
977
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800978 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
979 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700980
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800981 *newStreamId = streamId;
982 } else {
983 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800984 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800985 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700986 }
987
988 return res;
989}
990
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800991binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700992
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800993 binder::Status res;
994 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
995
996 if (inputSurface == NULL) {
997 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700998 }
999
1000 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001001 if (!mDevice.get()) {
1002 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1003 }
1004 sp<IGraphicBufferProducer> producer;
1005 status_t err = mDevice->getInputBufferProducer(&producer);
1006 if (err != OK) {
1007 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001008 "Camera %s: Error getting input Surface: %s (%d)",
1009 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001010 } else {
1011 inputSurface->name = String16("CameraInput");
1012 inputSurface->graphicBufferProducer = producer;
1013 }
1014 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001015}
1016
Emilian Peev40ead602017-09-26 15:46:36 +01001017binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1018 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1019 ATRACE_CALL();
1020
1021 binder::Status res;
1022 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1023
1024 Mutex::Autolock icl(mBinderSerializationLock);
1025
1026 if (!mDevice.get()) {
1027 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1028 }
1029
1030 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1031 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001032 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1033
Emilian Peev40ead602017-09-26 15:46:36 +01001034 auto producerCount = bufferProducers.size();
1035 if (producerCount == 0) {
1036 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1037 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1038 "bufferProducers must not be empty");
1039 }
1040
1041 // The first output is the one associated with the output configuration.
1042 // It should always be present, valid and the corresponding stream id should match.
1043 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1044 ssize_t index = mStreamMap.indexOfKey(binder);
1045 if (index == NAME_NOT_FOUND) {
1046 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1047 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1048 "OutputConfiguration is invalid");
1049 }
1050 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1051 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1052 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1053 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1054 "Stream id is invalid");
1055 }
1056
1057 std::vector<size_t> removedSurfaceIds;
1058 std::vector<sp<IBinder>> removedOutputs;
1059 std::vector<sp<Surface>> newOutputs;
1060 std::vector<OutputStreamInfo> streamInfos;
1061 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1062 for (auto &it : bufferProducers) {
1063 newOutputsMap.add(IInterface::asBinder(it), it);
1064 }
1065
1066 for (size_t i = 0; i < mStreamMap.size(); i++) {
1067 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1068 if (idx == NAME_NOT_FOUND) {
1069 if (mStreamMap[i].streamId() == streamId) {
1070 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1071 removedOutputs.push_back(mStreamMap.keyAt(i));
1072 }
1073 } else {
1074 if (mStreamMap[i].streamId() != streamId) {
1075 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1076 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1077 "Target Surface is invalid");
1078 }
1079 newOutputsMap.removeItemsAt(idx);
1080 }
1081 }
1082
1083 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1084 OutputStreamInfo outInfo;
1085 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001086 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false,
1087 surface, newOutputsMap.valueAt(i), mCameraIdStr,
1088 mDevice->infoPhysical(physicalCameraId));
Emilian Peev40ead602017-09-26 15:46:36 +01001089 if (!res.isOk())
1090 return res;
1091
Emilian Peev40ead602017-09-26 15:46:36 +01001092 streamInfos.push_back(outInfo);
1093 newOutputs.push_back(surface);
1094 }
1095
1096 //Trivial case no changes required
1097 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1098 return binder::Status::ok();
1099 }
1100
1101 KeyedVector<sp<Surface>, size_t> outputMap;
1102 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1103 &outputMap);
1104 if (ret != OK) {
1105 switch (ret) {
1106 case NAME_NOT_FOUND:
1107 case BAD_VALUE:
1108 case -EBUSY:
1109 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1110 "Camera %s: Error updating stream: %s (%d)",
1111 mCameraIdStr.string(), strerror(ret), ret);
1112 break;
1113 default:
1114 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1115 "Camera %s: Error updating stream: %s (%d)",
1116 mCameraIdStr.string(), strerror(ret), ret);
1117 break;
1118 }
1119 } else {
1120 for (const auto &it : removedOutputs) {
1121 mStreamMap.removeItem(it);
1122 }
1123
1124 for (size_t i = 0; i < outputMap.size(); i++) {
1125 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1126 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1127 }
1128
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001129 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1130
Emilian Peev40ead602017-09-26 15:46:36 +01001131 ALOGV("%s: Camera %s: Successful stream ID %d update",
1132 __FUNCTION__, mCameraIdStr.string(), streamId);
1133 }
1134
1135 return res;
1136}
1137
Igor Murashkine7ee7632013-06-11 18:10:18 -07001138// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001139binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1140 /*out*/
1141 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001142{
1143 ATRACE_CALL();
1144 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1145
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001146 binder::Status res;
1147 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001148
1149 Mutex::Autolock icl(mBinderSerializationLock);
1150
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001151 if (!mDevice.get()) {
1152 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1153 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001154
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001155 status_t err;
Emilian Peevf4816702020-04-03 15:44:51 -07001156 camera_request_template_t tempId = camera_request_template_t::CAMERA_TEMPLATE_COUNT;
1157 if (!(res = mapRequestTemplate(templateId, &tempId)).isOk()) return res;
1158
1159 CameraMetadata metadata;
1160 if ( (err = mDevice->createDefaultRequest(tempId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001161 request != NULL) {
1162
1163 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001164 } else if (err == BAD_VALUE) {
1165 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001166 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1167 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001168
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001169 } else {
1170 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001171 "Camera %s: Error creating default request for template %d: %s (%d)",
1172 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001173 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001174 return res;
1175}
1176
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001177binder::Status CameraDeviceClient::getCameraInfo(
1178 /*out*/
1179 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001180{
1181 ATRACE_CALL();
1182 ALOGV("%s", __FUNCTION__);
1183
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001185
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001186 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001187
1188 Mutex::Autolock icl(mBinderSerializationLock);
1189
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001190 if (!mDevice.get()) {
1191 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1192 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001193
Igor Murashkin099b4572013-07-12 17:52:16 -07001194 if (info != NULL) {
1195 *info = mDevice->info(); // static camera metadata
1196 // TODO: merge with device-specific camera metadata
1197 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001198
1199 return res;
1200}
1201
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001202binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001203{
1204 ATRACE_CALL();
1205 ALOGV("%s", __FUNCTION__);
1206
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001207 binder::Status res;
1208 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001209
1210 Mutex::Autolock icl(mBinderSerializationLock);
1211
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001212 if (!mDevice.get()) {
1213 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1214 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001215
1216 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001217 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001218 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001219 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001220 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1221 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001222 ALOGE("%s: %s", __FUNCTION__, msg.string());
1223 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001224 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001225 status_t err = mDevice->waitUntilDrained();
1226 if (err != OK) {
1227 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001228 "Camera %s: Error waiting to drain: %s (%d)",
1229 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001230 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001231 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001232 return res;
1233}
1234
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001235binder::Status CameraDeviceClient::flush(
1236 /*out*/
1237 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001238 ATRACE_CALL();
1239 ALOGV("%s", __FUNCTION__);
1240
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001241 binder::Status res;
1242 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001243
1244 Mutex::Autolock icl(mBinderSerializationLock);
1245
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001246 if (!mDevice.get()) {
1247 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1248 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001249
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001250 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001251 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001252 status_t err = mDevice->flush(lastFrameNumber);
1253 if (err != OK) {
1254 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001255 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001256 }
1257 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001258}
1259
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001260binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001261 ATRACE_CALL();
1262 ALOGV("%s", __FUNCTION__);
1263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001264 binder::Status res;
1265 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001266
1267 Mutex::Autolock icl(mBinderSerializationLock);
1268
1269 // Guard against trying to prepare non-created streams
1270 ssize_t index = NAME_NOT_FOUND;
1271 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001272 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001273 index = i;
1274 break;
1275 }
1276 }
1277
1278 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001279 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1280 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001281 ALOGW("%s: %s", __FUNCTION__, msg.string());
1282 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001283 }
1284
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001285 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1286 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001287 status_t err = mDevice->prepare(streamId);
1288 if (err == BAD_VALUE) {
1289 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001290 "Camera %s: Stream %d has already been used, and cannot be prepared",
1291 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001292 } else if (err != OK) {
1293 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001294 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001295 strerror(-err), err);
1296 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001297 return res;
1298}
1299
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001300binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001301 ATRACE_CALL();
1302 ALOGV("%s", __FUNCTION__);
1303
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001304 binder::Status res;
1305 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001306
1307 Mutex::Autolock icl(mBinderSerializationLock);
1308
1309 // Guard against trying to prepare non-created streams
1310 ssize_t index = NAME_NOT_FOUND;
1311 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001312 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001313 index = i;
1314 break;
1315 }
1316 }
1317
1318 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001319 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1320 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001321 ALOGW("%s: %s", __FUNCTION__, msg.string());
1322 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001323 }
1324
1325 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001326 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1327 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001328 ALOGE("%s: %s", __FUNCTION__, msg.string());
1329 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001330 }
1331
1332 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1333 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001334 status_t err = mDevice->prepare(maxCount, streamId);
1335 if (err == BAD_VALUE) {
1336 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001337 "Camera %s: Stream %d has already been used, and cannot be prepared",
1338 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001339 } else if (err != OK) {
1340 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001341 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001342 strerror(-err), err);
1343 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001344
1345 return res;
1346}
1347
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001348binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001349 ATRACE_CALL();
1350 ALOGV("%s", __FUNCTION__);
1351
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001352 binder::Status res;
1353 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001354
1355 Mutex::Autolock icl(mBinderSerializationLock);
1356
1357 // Guard against trying to prepare non-created streams
1358 ssize_t index = NAME_NOT_FOUND;
1359 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001360 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001361 index = i;
1362 break;
1363 }
1364 }
1365
1366 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001367 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1368 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001369 ALOGW("%s: %s", __FUNCTION__, msg.string());
1370 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001371 }
1372
1373 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1374 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001375 status_t err = mDevice->tearDown(streamId);
1376 if (err == BAD_VALUE) {
1377 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001378 "Camera %s: Stream %d is still in use, cannot be torn down",
1379 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380 } else if (err != OK) {
1381 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001382 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001383 strerror(-err), err);
1384 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001385
1386 return res;
1387}
1388
Shuzhen Wang758c2152017-01-10 18:26:18 -08001389binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001390 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1391 ATRACE_CALL();
1392
1393 binder::Status res;
1394 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1395
1396 Mutex::Autolock icl(mBinderSerializationLock);
1397
Shuzhen Wang0129d522016-10-30 22:43:41 -07001398 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1399 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001400 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001401
Shuzhen Wang0129d522016-10-30 22:43:41 -07001402 if (bufferProducers.size() == 0) {
1403 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001404 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1405 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001406
Shuzhen Wang758c2152017-01-10 18:26:18 -08001407 // streamId should be in mStreamMap if this stream already has a surface attached
1408 // to it. Otherwise, it should be in mDeferredStreams.
1409 bool streamIdConfigured = false;
1410 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1411 for (size_t i = 0; i < mStreamMap.size(); i++) {
1412 if (mStreamMap.valueAt(i).streamId() == streamId) {
1413 streamIdConfigured = true;
1414 break;
1415 }
Zhijun He5d677d12016-05-29 16:52:39 -07001416 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001417 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1418 if (streamId == mDeferredStreams[i]) {
1419 deferredStreamIndex = i;
1420 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001421 }
1422
Shuzhen Wang758c2152017-01-10 18:26:18 -08001423 }
1424 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1425 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1426 "(ID %d)", mCameraIdStr.string(), streamId);
1427 ALOGW("%s: %s", __FUNCTION__, msg.string());
1428 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001429 }
1430
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001431 if (mStreamInfoMap[streamId].finalized) {
1432 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1433 " on stream ID %d", mCameraIdStr.string(), streamId);
1434 ALOGW("%s: %s", __FUNCTION__, msg.string());
1435 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1436 }
1437
Zhijun He5d677d12016-05-29 16:52:39 -07001438 if (!mDevice.get()) {
1439 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1440 }
1441
Shuzhen Wang758c2152017-01-10 18:26:18 -08001442 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001443 for (auto& bufferProducer : bufferProducers) {
1444 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001445 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1446 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001447 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001448 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001449 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001450 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001451
1452 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001453 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
1454 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
1455 mDevice->infoPhysical(physicalId));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001456
1457 if (!res.isOk())
1458 return res;
1459
1460 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001461 }
1462
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001463 // Gracefully handle case where finalizeOutputConfigurations is called
1464 // without any new surface.
1465 if (consumerSurfaces.size() == 0) {
1466 mStreamInfoMap[streamId].finalized = true;
1467 return res;
1468 }
1469
Zhijun He5d677d12016-05-29 16:52:39 -07001470 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001471 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001472 std::vector<int> consumerSurfaceIds;
1473 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001474 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001475 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1476 sp<IBinder> binder = IInterface::asBinder(
1477 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001478 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001479 binder.get(), streamId, consumerSurfaceIds[i]);
1480 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1481 }
1482 if (deferredStreamIndex != NAME_NOT_FOUND) {
1483 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001484 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001485 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001486 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001487 } else if (err == NO_INIT) {
1488 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001489 "Camera %s: Deferred surface is invalid: %s (%d)",
1490 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001491 } else {
1492 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001493 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1494 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001495 }
1496
1497 return res;
1498}
1499
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001500binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001501 ATRACE_CALL();
1502 binder::Status res;
1503 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1504
1505 if (!isValidAudioRestriction(mode)) {
1506 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1507 mCameraIdStr.string(), mode);
1508 ALOGW("%s: %s", __FUNCTION__, msg.string());
1509 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1510 }
1511
1512 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001513 BasicClient::setAudioRestriction(mode);
1514 return binder::Status::ok();
1515}
1516
1517binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1518 ATRACE_CALL();
1519 binder::Status res;
1520 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1521 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001522 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001523 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001524 }
1525 return binder::Status::ok();
1526}
1527
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001528status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1529 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1530
1531 return mDevice->setRotateAndCropAutoBehavior(
1532 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1533}
1534
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001535binder::Status CameraDeviceClient::switchToOffline(
1536 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001537 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001538 /*out*/
1539 sp<hardware::camera2::ICameraOfflineSession>* session) {
1540 ATRACE_CALL();
1541
1542 binder::Status res;
1543 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1544
1545 Mutex::Autolock icl(mBinderSerializationLock);
1546
1547 if (!mDevice.get()) {
1548 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1549 }
1550
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001551 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001552 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001553 ALOGE("%s: %s", __FUNCTION__, msg.string());
1554 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1555 }
1556
1557 if (session == nullptr) {
1558 String8 msg = String8::format("Invalid offline session");
1559 ALOGE("%s: %s", __FUNCTION__, msg.string());
1560 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1561 }
1562
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001563 std::vector<int32_t> offlineStreamIds;
1564 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001565 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001566 for (const auto& streamId : offlineOutputIds) {
1567 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001568 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001569 String8 msg = String8::format("Offline surface with id: %d is not registered",
1570 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001571 ALOGE("%s: %s", __FUNCTION__, msg.string());
1572 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1573 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001574
1575 if (!mStreamInfoMap[streamId].supportsOffline) {
1576 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1577 "offline mode", streamId);
1578 ALOGE("%s: %s", __FUNCTION__, msg.string());
1579 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1580 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001581
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001582 bool isCompositeStream = false;
1583 for (const auto& gbp : mConfiguredOutputs[streamId].getGraphicBufferProducers()) {
1584 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
1585 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) |
1586 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001587 if (isCompositeStream) {
1588 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1589 if (compositeIdx == NAME_NOT_FOUND) {
1590 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1591 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1592 "Unknown composite stream");
1593 }
1594
1595 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1596 &offlineStreamIds);
1597 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1598 mCompositeStreamMap.valueAt(compositeIdx));
1599 break;
1600 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001601 }
1602
Emilian Peev4697b642019-11-19 17:11:14 -08001603 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001604 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001605 }
1606 }
1607
1608 sp<CameraOfflineSessionBase> offlineSession;
1609 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1610 if (ret != OK) {
1611 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1612 "Camera %s: Error switching to offline mode: %s (%d)",
1613 mCameraIdStr.string(), strerror(ret), ret);
1614 }
1615
Emilian Peevcc0b7952020-01-07 13:54:47 -08001616 sp<CameraOfflineSessionClient> offlineClient;
1617 if (offlineSession.get() != nullptr) {
1618 offlineClient = new CameraOfflineSessionClient(sCameraService,
1619 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
1620 mClientFeatureId, mCameraIdStr, mCameraFacing, mClientPid, mClientUid, mServicePid);
1621 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1622 }
1623
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001624 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001625 // A successful offline session switch must reset the current camera client
1626 // and release any resources occupied by previously configured streams.
1627 mStreamMap.clear();
1628 mConfiguredOutputs.clear();
1629 mDeferredStreams.clear();
1630 mStreamInfoMap.clear();
1631 mCompositeStreamMap.clear();
1632 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001633 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001634 switch(ret) {
1635 case BAD_VALUE:
1636 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1637 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1638 case TIMED_OUT:
1639 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1640 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1641 default:
1642 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1643 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1644 strerror(-ret), ret);
1645 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001646 }
1647
1648 *session = offlineClient;
1649
1650 return binder::Status::ok();
1651}
1652
Igor Murashkine7ee7632013-06-11 18:10:18 -07001653status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001654 return BasicClient::dump(fd, args);
1655}
1656
1657status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001658 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001659 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001660 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001661 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001662 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001663
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001664 dprintf(fd, " State:\n");
1665 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001666 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001667 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001668 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001669 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001670 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001671 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001672 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001673 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001674 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001675 mStreamMap.valueAt(i).streamId(),
1676 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001677 }
Zhijun He5d677d12016-05-29 16:52:39 -07001678 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001679 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001680 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001681 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001682 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001683 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001684 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001685 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001686 // TODO: print dynamic/request section from most recent requests
1687 mFrameProcessor->dump(fd, args);
1688
1689 return dumpDevice(fd, args);
1690}
1691
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001692void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001693 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001694 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001695 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001696
Emilian Peev538c90e2018-12-17 18:03:19 +00001697 // Composites can have multiple internal streams. Error notifications coming from such internal
1698 // streams may need to remain within camera service.
1699 bool skipClientNotification = false;
1700 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1701 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
1702 }
1703
1704 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001705 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001706 }
1707}
1708
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001709void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1710 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1711
1712 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001713 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001714 }
1715
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001716 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001717 mStreamingRequestId = REQUEST_ID_NONE;
1718}
1719
Shuzhen Wang316781a2020-08-18 18:11:01 -07001720void CameraDeviceClient::notifyIdle(
1721 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1722 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001723 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001724 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001725
1726 if (remoteCb != 0) {
1727 remoteCb->onDeviceIdle();
1728 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001729 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001730}
1731
Jianing Weicb0652e2014-03-12 18:29:36 -07001732void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001733 nsecs_t timestamp) {
1734 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001735 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001736 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001737 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001738 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001739 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001740
1741 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1742 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1743 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001744}
1745
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001746void CameraDeviceClient::notifyPrepared(int streamId) {
1747 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001748 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001749 if (remoteCb != 0) {
1750 remoteCb->onPrepared(streamId);
1751 }
1752}
1753
Shuzhen Wang9d066012016-09-30 11:30:20 -07001754void CameraDeviceClient::notifyRequestQueueEmpty() {
1755 // Thread safe. Don't bother locking.
1756 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1757 if (remoteCb != 0) {
1758 remoteCb->onRequestQueueEmpty();
1759 }
1760}
1761
Igor Murashkine7ee7632013-06-11 18:10:18 -07001762void CameraDeviceClient::detachDevice() {
1763 if (mDevice == 0) return;
1764
Shuzhen Wang316781a2020-08-18 18:11:01 -07001765 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001766 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001767
Emilian Peevfaa4bde2020-01-23 12:19:37 -08001768 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
1769 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Igor Murashkine7ee7632013-06-11 18:10:18 -07001770 /*listener*/this);
1771 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001772 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001773 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001774 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001775
1776 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1777 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001778 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001779 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001780 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
1781 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
1782 }
1783
Igor Murashkine7ee7632013-06-11 18:10:18 -07001784 if ((code = mDevice->waitUntilDrained()) != OK) {
1785 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1786 code);
1787 }
1788 }
1789
Emilian Peev5e4c7322019-10-22 14:20:52 -07001790 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1791 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
1792 if (ret != OK) {
1793 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
1794 strerror(-ret), ret);
1795 }
1796 }
1797 mCompositeStreamMap.clear();
1798
Igor Murashkine7ee7632013-06-11 18:10:18 -07001799 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001800
1801 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
1802 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001803}
1804
1805/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001806void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001807 ATRACE_CALL();
1808 ALOGV("%s", __FUNCTION__);
1809
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001810 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001811 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001812 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001813 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
1814 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001815 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001816
1817 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1818 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
1819 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001820}
1821
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001822binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001823 if (mDisconnected) {
1824 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1825 "The camera device has been disconnected");
1826 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001827 status_t res = checkPid(checkLocation);
1828 return (res == OK) ? binder::Status::ok() :
1829 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1830 "Attempt to use camera from a different process than original client");
1831}
1832
Igor Murashkine7ee7632013-06-11 18:10:18 -07001833// TODO: move to Camera2ClientBase
1834bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1835
Jayant Chowdhary12361932018-08-27 14:46:13 -07001836 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001837 const int selfPid = getpid();
1838 camera_metadata_entry_t entry;
1839
1840 /**
1841 * Mixin default important security values
1842 * - android.led.transmit = defaulted ON
1843 */
1844 CameraMetadata staticInfo = mDevice->info();
1845 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1846 for(size_t i = 0; i < entry.count; ++i) {
1847 uint8_t led = entry.data.u8[i];
1848
1849 switch(led) {
1850 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1851 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1852 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1853 metadata.update(ANDROID_LED_TRANSMIT,
1854 &transmitDefault, 1);
1855 }
1856 break;
1857 }
1858 }
1859 }
1860
1861 // We can do anything!
1862 if (pid == selfPid) {
1863 return true;
1864 }
1865
1866 /**
1867 * Permission check special fields in the request
1868 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1869 */
1870 entry = metadata.find(ANDROID_LED_TRANSMIT);
1871 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1872 String16 permissionString =
1873 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1874 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001875 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001876 ALOGE("Permission Denial: "
1877 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1878 return false;
1879 }
1880 }
1881
1882 return true;
1883}
1884
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001885status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1886 ALOGV("%s: begin", __FUNCTION__);
1887
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001888 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001889 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001890}
1891
Emilian Peevf4816702020-04-03 15:44:51 -07001892binder::Status CameraDeviceClient::mapRequestTemplate(int templateId,
1893 camera_request_template_t* tempId /*out*/) {
1894 binder::Status ret = binder::Status::ok();
1895
1896 if (tempId == nullptr) {
1897 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1898 "Camera %s: Invalid template argument", mCameraIdStr.string());
1899 return ret;
1900 }
1901 switch(templateId) {
1902 case ICameraDeviceUser::TEMPLATE_PREVIEW:
1903 *tempId = camera_request_template_t::CAMERA_TEMPLATE_PREVIEW;
1904 break;
1905 case ICameraDeviceUser::TEMPLATE_RECORD:
1906 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_RECORD;
1907 break;
1908 case ICameraDeviceUser::TEMPLATE_STILL_CAPTURE:
1909 *tempId = camera_request_template_t::CAMERA_TEMPLATE_STILL_CAPTURE;
1910 break;
1911 case ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT:
1912 *tempId = camera_request_template_t::CAMERA_TEMPLATE_VIDEO_SNAPSHOT;
1913 break;
1914 case ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG:
1915 *tempId = camera_request_template_t::CAMERA_TEMPLATE_ZERO_SHUTTER_LAG;
1916 break;
1917 case ICameraDeviceUser::TEMPLATE_MANUAL:
1918 *tempId = camera_request_template_t::CAMERA_TEMPLATE_MANUAL;
1919 break;
1920 default:
1921 ret = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1922 "Camera %s: Template ID %d is invalid or not supported",
1923 mCameraIdStr.string(), templateId);
1924 return ret;
1925 }
1926
1927 return ret;
1928}
Igor Murashkine7ee7632013-06-11 18:10:18 -07001929} // namespace android