blob: 66eda5dd96cbf8f69a47da95b94b1bf4d448a819 [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;
54
55CameraDeviceClientBase::CameraDeviceClientBase(
56 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080057 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070058 const String16& clientPackageName,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090059 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080060 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080061 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070062 int cameraFacing,
63 int clientPid,
64 uid_t clientUid,
65 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080066 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080067 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080068 clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080069 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080070 cameraId,
71 cameraFacing,
72 clientPid,
73 clientUid,
74 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070075 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080076 // We don't need it for API2 clients, but Camera2ClientBase requires it.
77 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070078}
Igor Murashkine7ee7632013-06-11 18:10:18 -070079
80// Interface used by CameraService
81
82CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080083 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
84 const String16& clientPackageName,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090085 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080086 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080087 int cameraFacing,
88 int clientPid,
89 uid_t clientUid,
90 int servicePid) :
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080091 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080092 cameraId, /*API1 camera ID*/ -1,
93 cameraFacing, clientPid, clientUid, servicePid),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070094 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070095 mStreamingRequestId(REQUEST_ID_NONE),
Igor Murashkine7ee7632013-06-11 18:10:18 -070096 mRequestIdCounter(0) {
97
98 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080099 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700100}
101
Emilian Peevbd8c5032018-02-14 23:05:40 +0000102status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
103 const String8& monitorTags) {
104 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800105}
106
107template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000108status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700109 ATRACE_CALL();
110 status_t res;
111
Emilian Peevbd8c5032018-02-14 23:05:40 +0000112 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700113 if (res != OK) {
114 return res;
115 }
116
117 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700118 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800119 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700120 mFrameProcessor->run(threadName.string());
121
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800122 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
123 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800124 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700125 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700126
Emilian Peev00420d22018-02-05 21:33:13 +0000127 auto deviceInfo = mDevice->info();
128 camera_metadata_entry_t physicalKeysEntry = deviceInfo.find(
129 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
130 if (physicalKeysEntry.count > 0) {
131 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
132 physicalKeysEntry.data.i32,
133 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
134 }
135
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700136 mProviderManager = providerPtr;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700137 return OK;
138}
139
140CameraDeviceClient::~CameraDeviceClient() {
141}
142
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800143binder::Status CameraDeviceClient::submitRequest(
144 const hardware::camera2::CaptureRequest& request,
145 bool streaming,
146 /*out*/
147 hardware::camera2::utils::SubmitInfo *submitInfo) {
148 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
149 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700150}
151
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800152binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000153 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000154 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800155 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
156
157 // Trying to submit request with surface that wasn't created
158 if (idx == NAME_NOT_FOUND) {
159 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
160 " we have not called createStream on",
161 __FUNCTION__, mCameraIdStr.string());
162 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
163 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000164 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
165 != NAME_NOT_FOUND) {
166 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
167 currentStreamId);
168 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800169 }
170
171 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
172 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
173 (*outSurfaceMap)[streamSurfaceId.streamId()] = std::vector<size_t>();
174 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,
810 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
811 &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,
820 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
821 &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,
Zhijun He5d677d12016-05-29 16:52:39 -0700888 static_cast<camera3_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
1155 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001156 status_t err;
1157 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001158 request != NULL) {
1159
1160 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001161 } else if (err == BAD_VALUE) {
1162 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001163 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1164 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001165
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001166 } else {
1167 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001168 "Camera %s: Error creating default request for template %d: %s (%d)",
1169 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001170 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001171 return res;
1172}
1173
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001174binder::Status CameraDeviceClient::getCameraInfo(
1175 /*out*/
1176 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001177{
1178 ATRACE_CALL();
1179 ALOGV("%s", __FUNCTION__);
1180
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001181 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001182
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001183 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001184
1185 Mutex::Autolock icl(mBinderSerializationLock);
1186
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001187 if (!mDevice.get()) {
1188 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1189 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001190
Igor Murashkin099b4572013-07-12 17:52:16 -07001191 if (info != NULL) {
1192 *info = mDevice->info(); // static camera metadata
1193 // TODO: merge with device-specific camera metadata
1194 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001195
1196 return res;
1197}
1198
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001199binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001200{
1201 ATRACE_CALL();
1202 ALOGV("%s", __FUNCTION__);
1203
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001204 binder::Status res;
1205 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001206
1207 Mutex::Autolock icl(mBinderSerializationLock);
1208
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001209 if (!mDevice.get()) {
1210 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1211 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001212
1213 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001214 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001215 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001216 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001217 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1218 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001219 ALOGE("%s: %s", __FUNCTION__, msg.string());
1220 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001221 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001222 status_t err = mDevice->waitUntilDrained();
1223 if (err != OK) {
1224 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001225 "Camera %s: Error waiting to drain: %s (%d)",
1226 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001227 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001228 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001229 return res;
1230}
1231
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001232binder::Status CameraDeviceClient::flush(
1233 /*out*/
1234 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001235 ATRACE_CALL();
1236 ALOGV("%s", __FUNCTION__);
1237
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001238 binder::Status res;
1239 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001240
1241 Mutex::Autolock icl(mBinderSerializationLock);
1242
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001243 if (!mDevice.get()) {
1244 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1245 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001246
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001247 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001248 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001249 status_t err = mDevice->flush(lastFrameNumber);
1250 if (err != OK) {
1251 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001252 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001253 }
1254 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001255}
1256
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001257binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001258 ATRACE_CALL();
1259 ALOGV("%s", __FUNCTION__);
1260
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001261 binder::Status res;
1262 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001263
1264 Mutex::Autolock icl(mBinderSerializationLock);
1265
1266 // Guard against trying to prepare non-created streams
1267 ssize_t index = NAME_NOT_FOUND;
1268 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001269 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001270 index = i;
1271 break;
1272 }
1273 }
1274
1275 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001276 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1277 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001278 ALOGW("%s: %s", __FUNCTION__, msg.string());
1279 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001280 }
1281
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001282 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1283 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001284 status_t err = mDevice->prepare(streamId);
1285 if (err == BAD_VALUE) {
1286 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001287 "Camera %s: Stream %d has already been used, and cannot be prepared",
1288 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001289 } else if (err != OK) {
1290 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001291 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001292 strerror(-err), err);
1293 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001294 return res;
1295}
1296
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001297binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001298 ATRACE_CALL();
1299 ALOGV("%s", __FUNCTION__);
1300
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001301 binder::Status res;
1302 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001303
1304 Mutex::Autolock icl(mBinderSerializationLock);
1305
1306 // Guard against trying to prepare non-created streams
1307 ssize_t index = NAME_NOT_FOUND;
1308 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001309 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001310 index = i;
1311 break;
1312 }
1313 }
1314
1315 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001316 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1317 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001318 ALOGW("%s: %s", __FUNCTION__, msg.string());
1319 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001320 }
1321
1322 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001323 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1324 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325 ALOGE("%s: %s", __FUNCTION__, msg.string());
1326 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001327 }
1328
1329 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1330 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001331 status_t err = mDevice->prepare(maxCount, streamId);
1332 if (err == BAD_VALUE) {
1333 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001334 "Camera %s: Stream %d has already been used, and cannot be prepared",
1335 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001336 } else if (err != OK) {
1337 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001338 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001339 strerror(-err), err);
1340 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001341
1342 return res;
1343}
1344
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001345binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001346 ATRACE_CALL();
1347 ALOGV("%s", __FUNCTION__);
1348
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001349 binder::Status res;
1350 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001351
1352 Mutex::Autolock icl(mBinderSerializationLock);
1353
1354 // Guard against trying to prepare non-created streams
1355 ssize_t index = NAME_NOT_FOUND;
1356 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001357 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001358 index = i;
1359 break;
1360 }
1361 }
1362
1363 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001364 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1365 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001366 ALOGW("%s: %s", __FUNCTION__, msg.string());
1367 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001368 }
1369
1370 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1371 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001372 status_t err = mDevice->tearDown(streamId);
1373 if (err == BAD_VALUE) {
1374 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001375 "Camera %s: Stream %d is still in use, cannot be torn down",
1376 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001377 } else if (err != OK) {
1378 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001379 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380 strerror(-err), err);
1381 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001382
1383 return res;
1384}
1385
Shuzhen Wang758c2152017-01-10 18:26:18 -08001386binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001387 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1388 ATRACE_CALL();
1389
1390 binder::Status res;
1391 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1392
1393 Mutex::Autolock icl(mBinderSerializationLock);
1394
Shuzhen Wang0129d522016-10-30 22:43:41 -07001395 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1396 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001397 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001398
Shuzhen Wang0129d522016-10-30 22:43:41 -07001399 if (bufferProducers.size() == 0) {
1400 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001401 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1402 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001403
Shuzhen Wang758c2152017-01-10 18:26:18 -08001404 // streamId should be in mStreamMap if this stream already has a surface attached
1405 // to it. Otherwise, it should be in mDeferredStreams.
1406 bool streamIdConfigured = false;
1407 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1408 for (size_t i = 0; i < mStreamMap.size(); i++) {
1409 if (mStreamMap.valueAt(i).streamId() == streamId) {
1410 streamIdConfigured = true;
1411 break;
1412 }
Zhijun He5d677d12016-05-29 16:52:39 -07001413 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001414 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1415 if (streamId == mDeferredStreams[i]) {
1416 deferredStreamIndex = i;
1417 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001418 }
1419
Shuzhen Wang758c2152017-01-10 18:26:18 -08001420 }
1421 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1422 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1423 "(ID %d)", mCameraIdStr.string(), streamId);
1424 ALOGW("%s: %s", __FUNCTION__, msg.string());
1425 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001426 }
1427
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001428 if (mStreamInfoMap[streamId].finalized) {
1429 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1430 " on stream ID %d", mCameraIdStr.string(), streamId);
1431 ALOGW("%s: %s", __FUNCTION__, msg.string());
1432 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1433 }
1434
Zhijun He5d677d12016-05-29 16:52:39 -07001435 if (!mDevice.get()) {
1436 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1437 }
1438
Shuzhen Wang758c2152017-01-10 18:26:18 -08001439 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001440 for (auto& bufferProducer : bufferProducers) {
1441 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001442 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1443 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001444 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001445 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001446 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001447 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001448
1449 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001450 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
1451 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
1452 mDevice->infoPhysical(physicalId));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001453
1454 if (!res.isOk())
1455 return res;
1456
1457 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001458 }
1459
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001460 // Gracefully handle case where finalizeOutputConfigurations is called
1461 // without any new surface.
1462 if (consumerSurfaces.size() == 0) {
1463 mStreamInfoMap[streamId].finalized = true;
1464 return res;
1465 }
1466
Zhijun He5d677d12016-05-29 16:52:39 -07001467 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001468 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001469 std::vector<int> consumerSurfaceIds;
1470 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001471 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001472 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1473 sp<IBinder> binder = IInterface::asBinder(
1474 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001475 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001476 binder.get(), streamId, consumerSurfaceIds[i]);
1477 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1478 }
1479 if (deferredStreamIndex != NAME_NOT_FOUND) {
1480 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001481 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001482 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001483 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001484 } else if (err == NO_INIT) {
1485 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001486 "Camera %s: Deferred surface is invalid: %s (%d)",
1487 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001488 } else {
1489 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001490 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1491 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001492 }
1493
1494 return res;
1495}
1496
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001497binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001498 ATRACE_CALL();
1499 binder::Status res;
1500 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1501
1502 if (!isValidAudioRestriction(mode)) {
1503 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1504 mCameraIdStr.string(), mode);
1505 ALOGW("%s: %s", __FUNCTION__, msg.string());
1506 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1507 }
1508
1509 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001510 BasicClient::setAudioRestriction(mode);
1511 return binder::Status::ok();
1512}
1513
1514binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1515 ATRACE_CALL();
1516 binder::Status res;
1517 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1518 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001519 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001520 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001521 }
1522 return binder::Status::ok();
1523}
1524
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001525status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1526 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1527
1528 return mDevice->setRotateAndCropAutoBehavior(
1529 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1530}
1531
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001532binder::Status CameraDeviceClient::switchToOffline(
1533 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001534 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001535 /*out*/
1536 sp<hardware::camera2::ICameraOfflineSession>* session) {
1537 ATRACE_CALL();
1538
1539 binder::Status res;
1540 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1541
1542 Mutex::Autolock icl(mBinderSerializationLock);
1543
1544 if (!mDevice.get()) {
1545 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1546 }
1547
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001548 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001549 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001550 ALOGE("%s: %s", __FUNCTION__, msg.string());
1551 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1552 }
1553
1554 if (session == nullptr) {
1555 String8 msg = String8::format("Invalid offline session");
1556 ALOGE("%s: %s", __FUNCTION__, msg.string());
1557 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1558 }
1559
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001560 std::vector<int32_t> offlineStreamIds;
1561 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001562 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001563 for (const auto& streamId : offlineOutputIds) {
1564 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001565 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001566 String8 msg = String8::format("Offline surface with id: %d is not registered",
1567 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001568 ALOGE("%s: %s", __FUNCTION__, msg.string());
1569 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1570 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001571
1572 if (!mStreamInfoMap[streamId].supportsOffline) {
1573 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1574 "offline mode", streamId);
1575 ALOGE("%s: %s", __FUNCTION__, msg.string());
1576 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1577 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001578
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001579 bool isCompositeStream = false;
1580 for (const auto& gbp : mConfiguredOutputs[streamId].getGraphicBufferProducers()) {
1581 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
1582 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) |
1583 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001584 if (isCompositeStream) {
1585 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1586 if (compositeIdx == NAME_NOT_FOUND) {
1587 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1588 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1589 "Unknown composite stream");
1590 }
1591
1592 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1593 &offlineStreamIds);
1594 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1595 mCompositeStreamMap.valueAt(compositeIdx));
1596 break;
1597 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001598 }
1599
Emilian Peev4697b642019-11-19 17:11:14 -08001600 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001601 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001602 }
1603 }
1604
1605 sp<CameraOfflineSessionBase> offlineSession;
1606 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1607 if (ret != OK) {
1608 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1609 "Camera %s: Error switching to offline mode: %s (%d)",
1610 mCameraIdStr.string(), strerror(ret), ret);
1611 }
1612
Emilian Peevcc0b7952020-01-07 13:54:47 -08001613 sp<CameraOfflineSessionClient> offlineClient;
1614 if (offlineSession.get() != nullptr) {
1615 offlineClient = new CameraOfflineSessionClient(sCameraService,
1616 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
1617 mClientFeatureId, mCameraIdStr, mCameraFacing, mClientPid, mClientUid, mServicePid);
1618 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1619 }
1620
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001621 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001622 // A successful offline session switch must reset the current camera client
1623 // and release any resources occupied by previously configured streams.
1624 mStreamMap.clear();
1625 mConfiguredOutputs.clear();
1626 mDeferredStreams.clear();
1627 mStreamInfoMap.clear();
1628 mCompositeStreamMap.clear();
1629 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001630 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001631 switch(ret) {
1632 case BAD_VALUE:
1633 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1634 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1635 case TIMED_OUT:
1636 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1637 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1638 default:
1639 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1640 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1641 strerror(-ret), ret);
1642 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001643 }
1644
1645 *session = offlineClient;
1646
1647 return binder::Status::ok();
1648}
1649
Igor Murashkine7ee7632013-06-11 18:10:18 -07001650status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001651 return BasicClient::dump(fd, args);
1652}
1653
1654status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001655 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001656 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001657 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001658 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001659 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001660
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001661 dprintf(fd, " State:\n");
1662 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001663 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001664 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001665 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001666 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001667 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001668 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001669 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001670 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001671 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001672 mStreamMap.valueAt(i).streamId(),
1673 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001674 }
Zhijun He5d677d12016-05-29 16:52:39 -07001675 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001676 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001677 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001678 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001679 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001680 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001681 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001682 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001683 // TODO: print dynamic/request section from most recent requests
1684 mFrameProcessor->dump(fd, args);
1685
1686 return dumpDevice(fd, args);
1687}
1688
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001689void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001690 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001691 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001692 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001693
Emilian Peev538c90e2018-12-17 18:03:19 +00001694 // Composites can have multiple internal streams. Error notifications coming from such internal
1695 // streams may need to remain within camera service.
1696 bool skipClientNotification = false;
1697 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1698 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
1699 }
1700
1701 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001702 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001703 }
1704}
1705
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001706void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1707 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1708
1709 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001710 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001711 }
1712
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001713 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001714 mStreamingRequestId = REQUEST_ID_NONE;
1715}
1716
Shuzhen Wang316781a2020-08-18 18:11:01 -07001717void CameraDeviceClient::notifyIdle(
1718 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1719 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001720 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001721 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001722
1723 if (remoteCb != 0) {
1724 remoteCb->onDeviceIdle();
1725 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001726 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001727}
1728
Jianing Weicb0652e2014-03-12 18:29:36 -07001729void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001730 nsecs_t timestamp) {
1731 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001732 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001733 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001734 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001735 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001736 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001737
1738 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1739 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1740 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001741}
1742
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001743void CameraDeviceClient::notifyPrepared(int streamId) {
1744 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001745 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001746 if (remoteCb != 0) {
1747 remoteCb->onPrepared(streamId);
1748 }
1749}
1750
Shuzhen Wang9d066012016-09-30 11:30:20 -07001751void CameraDeviceClient::notifyRequestQueueEmpty() {
1752 // Thread safe. Don't bother locking.
1753 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1754 if (remoteCb != 0) {
1755 remoteCb->onRequestQueueEmpty();
1756 }
1757}
1758
Igor Murashkine7ee7632013-06-11 18:10:18 -07001759void CameraDeviceClient::detachDevice() {
1760 if (mDevice == 0) return;
1761
Shuzhen Wang316781a2020-08-18 18:11:01 -07001762 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001763 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001764
Emilian Peevfaa4bde2020-01-23 12:19:37 -08001765 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
1766 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Igor Murashkine7ee7632013-06-11 18:10:18 -07001767 /*listener*/this);
1768 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001769 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001770 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001771 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001772
1773 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1774 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001775 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001776 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001777 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
1778 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
1779 }
1780
Igor Murashkine7ee7632013-06-11 18:10:18 -07001781 if ((code = mDevice->waitUntilDrained()) != OK) {
1782 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1783 code);
1784 }
1785 }
1786
Emilian Peev5e4c7322019-10-22 14:20:52 -07001787 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1788 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
1789 if (ret != OK) {
1790 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
1791 strerror(-ret), ret);
1792 }
1793 }
1794 mCompositeStreamMap.clear();
1795
Igor Murashkine7ee7632013-06-11 18:10:18 -07001796 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001797
1798 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
1799 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001800}
1801
1802/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001803void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001804 ATRACE_CALL();
1805 ALOGV("%s", __FUNCTION__);
1806
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001807 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001808 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001809 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001810 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
1811 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001812 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001813
1814 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1815 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
1816 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001817}
1818
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001819binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001820 if (mDisconnected) {
1821 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1822 "The camera device has been disconnected");
1823 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001824 status_t res = checkPid(checkLocation);
1825 return (res == OK) ? binder::Status::ok() :
1826 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1827 "Attempt to use camera from a different process than original client");
1828}
1829
Igor Murashkine7ee7632013-06-11 18:10:18 -07001830// TODO: move to Camera2ClientBase
1831bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1832
Jayant Chowdhary12361932018-08-27 14:46:13 -07001833 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001834 const int selfPid = getpid();
1835 camera_metadata_entry_t entry;
1836
1837 /**
1838 * Mixin default important security values
1839 * - android.led.transmit = defaulted ON
1840 */
1841 CameraMetadata staticInfo = mDevice->info();
1842 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1843 for(size_t i = 0; i < entry.count; ++i) {
1844 uint8_t led = entry.data.u8[i];
1845
1846 switch(led) {
1847 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1848 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1849 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1850 metadata.update(ANDROID_LED_TRANSMIT,
1851 &transmitDefault, 1);
1852 }
1853 break;
1854 }
1855 }
1856 }
1857
1858 // We can do anything!
1859 if (pid == selfPid) {
1860 return true;
1861 }
1862
1863 /**
1864 * Permission check special fields in the request
1865 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1866 */
1867 entry = metadata.find(ANDROID_LED_TRANSMIT);
1868 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1869 String16 permissionString =
1870 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1871 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001872 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001873 ALOGE("Permission Denial: "
1874 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1875 return false;
1876 }
1877 }
1878
1879 return true;
1880}
1881
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001882status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1883 ALOGV("%s: begin", __FUNCTION__);
1884
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001885 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001886 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001887}
1888
Igor Murashkine7ee7632013-06-11 18:10:18 -07001889} // namespace android