blob: e821f6c7e62a75c81c0288f48f59ea9f43454af2 [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()) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800173 outputStreamIds->push_back(streamSurfaceId.streamId());
174 }
175 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
176
177 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
178 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
179 streamSurfaceId.surfaceId());
180
Emilian Peevf873aa52018-01-26 14:58:28 +0000181 if (currentStreamId != nullptr) {
182 *currentStreamId = streamSurfaceId.streamId();
183 }
184
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800185 return binder::Status::ok();
186}
187
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800188binder::Status CameraDeviceClient::submitRequestList(
189 const std::vector<hardware::camera2::CaptureRequest>& requests,
190 bool streaming,
191 /*out*/
192 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700193 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700194 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700195
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800196 binder::Status res = binder::Status::ok();
197 status_t err;
198 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
199 return res;
200 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700201
202 Mutex::Autolock icl(mBinderSerializationLock);
203
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800204 if (!mDevice.get()) {
205 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
206 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700207
208 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800209 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
210 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800211 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700212 }
213
Emilian Peevaebbe412018-01-15 13:53:24 +0000214 List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700215 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800216 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700217 uint32_t loopCounter = 0;
218
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800219 for (auto&& request: requests) {
220 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700221 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800222 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
223 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800224 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800225 "No input configured for camera %s but request is for reprocessing",
226 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700227 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800228 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
229 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800230 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
231 "Repeating reprocess requests not supported");
Emilian Peevaebbe412018-01-15 13:53:24 +0000232 } else if (request.mPhysicalCameraSettings.size() > 1) {
233 ALOGE("%s: Camera %s: reprocess requests not supported for "
234 "multiple physical cameras.", __FUNCTION__,
235 mCameraIdStr.string());
236 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
237 "Reprocess requests not supported for multiple cameras");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700238 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700239 }
240
Emilian Peevaebbe412018-01-15 13:53:24 +0000241 if (request.mPhysicalCameraSettings.empty()) {
242 ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
243 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800244 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
Emilian Peevaebbe412018-01-15 13:53:24 +0000245 "Request doesn't contain any settings");
246 }
247
248 //The first capture settings should always match the logical camera id
249 String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
250 if (mDevice->getId() != logicalId) {
251 ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
252 mCameraIdStr.string());
253 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
254 "Invalid camera request settings");
255 }
256
Emilian Peevaebbe412018-01-15 13:53:24 +0000257 if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800258 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
259 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800260 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
261 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700262 }
263
Jianing Wei90e59c92014-03-12 18:29:36 -0700264 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700265 * Write in the output stream IDs and map from stream ID to surface ID
266 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700267 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700268 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700269 Vector<int32_t> outputStreamIds;
Emilian Peevf873aa52018-01-26 14:58:28 +0000270 std::vector<std::string> requestedPhysicalIds;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800271 if (request.mSurfaceList.size() > 0) {
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800272 for (const sp<Surface>& surface : request.mSurfaceList) {
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800273 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700274
Emilian Peevf873aa52018-01-26 14:58:28 +0000275 int32_t streamId;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800276 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Emilian Peevf873aa52018-01-26 14:58:28 +0000277 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800278 if (!res.isOk()) {
279 return res;
280 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000281
282 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
283 if (index >= 0) {
284 String8 requestedPhysicalId(
285 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
286 requestedPhysicalIds.push_back(requestedPhysicalId.string());
287 } else {
288 ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
289 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700290 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800291 } else {
292 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
293 int streamId = request.mStreamIdxList.itemAt(i);
294 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700295
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800296 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
297 if (index < 0) {
298 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
299 " we have not called createStream on: stream %d",
300 __FUNCTION__, mCameraIdStr.string(), streamId);
301 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
302 "Request targets Surface that is not part of current capture session");
303 }
304
305 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
306 if ((size_t)surfaceIdx >= gbps.size()) {
307 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
308 " we have not called createStream on: stream %d, surfaceIdx %d",
309 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
310 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
311 "Request targets Surface has invalid surface index");
312 }
313
Emilian Peevf873aa52018-01-26 14:58:28 +0000314 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800315 if (!res.isOk()) {
316 return res;
317 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000318
319 String8 requestedPhysicalId(
320 mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
321 requestedPhysicalIds.push_back(requestedPhysicalId.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700322 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700323 }
324
Emilian Peevf873aa52018-01-26 14:58:28 +0000325 CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
326 for (const auto& it : request.mPhysicalCameraSettings) {
Emilian Peev00420d22018-02-05 21:33:13 +0000327 if (it.settings.isEmpty()) {
328 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
329 __FUNCTION__, mCameraIdStr.string());
330 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
331 "Request settings are empty");
332 }
333
Emilian Peevf873aa52018-01-26 14:58:28 +0000334 String8 physicalId(it.id.c_str());
335 if (physicalId != mDevice->getId()) {
336 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
337 it.id);
338 if (found == requestedPhysicalIds.end()) {
339 ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
340 __FUNCTION__, mCameraIdStr.string(), physicalId.string());
341 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
342 "Invalid physical camera id");
343 }
Emilian Peev00420d22018-02-05 21:33:13 +0000344
345 if (!mSupportedPhysicalRequestKeys.empty()) {
346 // Filter out any unsupported physical request keys.
347 CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
348 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
349 filteredParams.getAndLock());
350 set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
351 filteredParams.unlock(meta);
352
353 for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
354 camera_metadata_ro_entry entry = it.settings.find(keyIt);
355 if (entry.count > 0) {
356 filteredParams.update(entry);
357 }
358 }
359
360 physicalSettingsList.push_back({it.id, filteredParams});
361 }
362 } else {
363 physicalSettingsList.push_back({it.id, it.settings});
Emilian Peevf873aa52018-01-26 14:58:28 +0000364 }
Emilian Peevf873aa52018-01-26 14:58:28 +0000365 }
366
367 if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
368 // Callee logs
369 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
370 "Caller does not have permission to change restricted controls");
371 }
372
Emilian Peevaebbe412018-01-15 13:53:24 +0000373 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
374 &outputStreamIds[0], outputStreamIds.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800376 if (request.mIsReprocess) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000377 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
378 &mInputStream.id, 1);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700379 }
380
Emilian Peevaebbe412018-01-15 13:53:24 +0000381 physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
382 &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700383 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800384 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
385 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
386 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700387
Emilian Peevaebbe412018-01-15 13:53:24 +0000388 metadataRequestList.push_back(physicalSettingsList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700389 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700390 }
391 mRequestIdCounter++;
392
393 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700394 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
395 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800396 if (err != OK) {
397 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800398 "Camera %s: Got error %s (%d) after trying to set streaming request",
399 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800400 ALOGE("%s: %s", __FUNCTION__, msg.string());
401 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
402 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700403 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700404 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700405 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700406 }
407 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700408 err = mDevice->captureList(metadataRequestList, surfaceMapList,
409 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800410 if (err != OK) {
411 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800412 "Camera %s: Got error %s (%d) after trying to submit capture request",
413 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800414 ALOGE("%s: %s", __FUNCTION__, msg.string());
415 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
416 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700417 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800418 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700419 }
420
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800421 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700422 return res;
423}
424
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800425binder::Status CameraDeviceClient::cancelRequest(
426 int requestId,
427 /*out*/
428 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700429 ATRACE_CALL();
430 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
431
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800432 status_t err;
433 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700434
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800435 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700436
437 Mutex::Autolock icl(mBinderSerializationLock);
438
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800439 if (!mDevice.get()) {
440 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
441 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700442
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700443 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700444 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800445 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
446 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800447 ALOGE("%s: %s", __FUNCTION__, msg.string());
448 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700449 }
450
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800451 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700452
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800453 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800454 ALOGV("%s: Camera %s: Successfully cleared streaming request",
455 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700456 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800457 } else {
458 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800459 "Camera %s: Error clearing streaming request: %s (%d)",
460 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700461 }
462
463 return res;
464}
465
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800466binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700467 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700468 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700469 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800470 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700471}
472
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100473binder::Status CameraDeviceClient::endConfigure(int operatingMode,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700474 const hardware::camera2::impl::CameraMetadataNative& sessionParams, int64_t startTimeMs,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800475 std::vector<int>* offlineStreamIds /*out*/) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700476 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700477 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
478 __FUNCTION__, mInputStream.configured ? 1 : 0,
479 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700480
Zhijun He0effd522016-03-04 10:22:27 -0800481 binder::Status res;
482 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
483
Emilian Peevcc0b7952020-01-07 13:54:47 -0800484 if (offlineStreamIds == nullptr) {
485 String8 msg = String8::format("Invalid offline stream ids");
486 ALOGE("%s: %s", __FUNCTION__, msg.string());
487 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
488 }
489
Zhijun He0effd522016-03-04 10:22:27 -0800490 Mutex::Autolock icl(mBinderSerializationLock);
491
492 if (!mDevice.get()) {
493 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
494 }
495
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000496 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
497 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000498 if (!res.isOk()) {
499 return res;
500 }
501
502 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
503 if (err == BAD_VALUE) {
504 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
505 mCameraIdStr.string());
506 ALOGE("%s: %s", __FUNCTION__, msg.string());
507 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
508 } else if (err != OK) {
509 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
510 mCameraIdStr.string(), strerror(-err), err);
511 ALOGE("%s: %s", __FUNCTION__, msg.string());
512 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Emilian Peev538c90e2018-12-17 18:03:19 +0000513 } else {
Emilian Peevcc0b7952020-01-07 13:54:47 -0800514 offlineStreamIds->clear();
515 mDevice->getOfflineStreamIds(offlineStreamIds);
516
Emilian Peev538c90e2018-12-17 18:03:19 +0000517 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
518 err = mCompositeStreamMap.valueAt(i)->configureStream();
Emilian Peevcc0b7952020-01-07 13:54:47 -0800519 if (err != OK) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000520 String8 msg = String8::format("Camera %s: Error configuring composite "
521 "streams: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
522 ALOGE("%s: %s", __FUNCTION__, msg.string());
523 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
524 break;
525 }
Emilian Peevcc0b7952020-01-07 13:54:47 -0800526
527 // Composite streams can only support offline mode in case all individual internal
528 // streams are also supported.
529 std::vector<int> internalStreams;
530 mCompositeStreamMap.valueAt(i)->insertCompositeStreamIds(&internalStreams);
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800531 offlineStreamIds->erase(
532 std::remove_if(offlineStreamIds->begin(), offlineStreamIds->end(),
Emilian Peevcc0b7952020-01-07 13:54:47 -0800533 [&internalStreams] (int streamId) {
534 auto it = std::find(internalStreams.begin(), internalStreams.end(),
535 streamId);
536 if (it != internalStreams.end()) {
537 internalStreams.erase(it);
538 return true;
539 }
540
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800541 return false;}), offlineStreamIds->end());
Emilian Peevcc0b7952020-01-07 13:54:47 -0800542 if (internalStreams.empty()) {
543 offlineStreamIds->push_back(mCompositeStreamMap.valueAt(i)->getStreamId());
544 }
545 }
546
547 for (const auto& offlineStreamId : *offlineStreamIds) {
548 mStreamInfoMap[offlineStreamId].supportsOffline = true;
Emilian Peev538c90e2018-12-17 18:03:19 +0000549 }
Shuzhen Wang316781a2020-08-18 18:11:01 -0700550
551 nsecs_t configureEnd = systemTime();
552 int32_t configureDurationMs = ns2ms(configureEnd) - startTimeMs;
553 CameraServiceProxyWrapper::logStreamConfigured(mCameraIdStr, operatingMode,
554 false /*internalReconfig*/, configureDurationMs);
Emilian Peev35ae8262018-11-08 13:11:32 +0000555 }
556
557 return res;
558}
559
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800560binder::Status CameraDeviceClient::isSessionConfigurationSupported(
561 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
562 ATRACE_CALL();
563
564 binder::Status res;
565 status_t ret = OK;
566 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
567
568 Mutex::Autolock icl(mBinderSerializationLock);
569
570 if (!mDevice.get()) {
571 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
572 }
573
574 auto operatingMode = sessionConfiguration.getOperatingMode();
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000575 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
576 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800577 if (!res.isOk()) {
578 return res;
579 }
580
581 if (status == nullptr) {
582 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
583 ALOGE("%s: %s", __FUNCTION__, msg.string());
584 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
585 }
586 hardware::camera::device::V3_4::StreamConfiguration streamConfiguration;
587 bool earlyExit = false;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800588 metadataGetter getMetadata = [this](const String8 &id) {return mDevice->infoPhysical(id);};
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800589 std::vector<std::string> physicalCameraIds;
590 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &physicalCameraIds);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000591 res = SessionConfigurationUtils::convertToHALStreamCombination(sessionConfiguration,
592 mCameraIdStr, mDevice->info(), getMetadata, physicalCameraIds, streamConfiguration,
593 &earlyExit);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800594 if (!res.isOk()) {
595 return res;
596 }
597
598 if (earlyExit) {
599 *status = false;
600 return binder::Status::ok();
601 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000602
603 *status = false;
604 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
605 streamConfiguration, status);
606 switch (ret) {
607 case OK:
608 // Expected, do nothing.
609 break;
610 case INVALID_OPERATION: {
611 String8 msg = String8::format(
612 "Camera %s: Session configuration query not supported!",
613 mCameraIdStr.string());
614 ALOGD("%s: %s", __FUNCTION__, msg.string());
615 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
616 }
617
618 break;
619 default: {
620 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
621 strerror(-ret), ret);
622 ALOGE("%s: %s", __FUNCTION__, msg.string());
623 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
624 msg.string());
625 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800626 }
627
628 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700629}
630
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800631binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700632 ATRACE_CALL();
633 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
634
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800635 binder::Status res;
636 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700637
638 Mutex::Autolock icl(mBinderSerializationLock);
639
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800640 if (!mDevice.get()) {
641 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
642 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700643
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700644 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700645 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700646 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000647 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700648
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700649 if (mInputStream.configured && mInputStream.id == streamId) {
650 isInput = true;
651 } else {
652 // Guard against trying to delete non-created streams
653 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700654 if (streamId == mStreamMap.valueAt(i).streamId()) {
655 surfaces.push_back(mStreamMap.keyAt(i));
656 }
657 }
658
659 // See if this stream is one of the deferred streams.
660 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
661 if (streamId == mDeferredStreams[i]) {
662 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700663 break;
664 }
665 }
666
Emilian Peev538c90e2018-12-17 18:03:19 +0000667 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
668 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
669 compositeIndex = i;
670 break;
671 }
672 }
673
Shuzhen Wang0129d522016-10-30 22:43:41 -0700674 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
675 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
676 " stream created yet", mCameraIdStr.string(), streamId);
677 ALOGW("%s: %s", __FUNCTION__, msg.string());
678 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700679 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700680 }
681
682 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800683 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700684
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800685 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800686 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
687 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800688 ALOGE("%s: %s", __FUNCTION__, msg.string());
689 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
690 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700691 if (isInput) {
692 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700693 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700694 for (auto& surface : surfaces) {
695 mStreamMap.removeItem(surface);
696 }
697
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800698 mConfiguredOutputs.removeItem(streamId);
699
Shuzhen Wang0129d522016-10-30 22:43:41 -0700700 if (dIndex != NAME_NOT_FOUND) {
701 mDeferredStreams.removeItemsAt(dIndex);
702 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000703
704 if (compositeIndex != NAME_NOT_FOUND) {
705 status_t ret;
706 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
707 != OK) {
708 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
709 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
710 streamId);
711 ALOGE("%s: %s", __FUNCTION__, msg.string());
712 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
713 }
714 mCompositeStreamMap.removeItemsAt(compositeIndex);
715 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700716 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700717 }
718
719 return res;
720}
721
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800722binder::Status CameraDeviceClient::createStream(
723 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
724 /*out*/
725 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700726 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700727
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800728 binder::Status res;
729 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700730
731 Mutex::Autolock icl(mBinderSerializationLock);
732
Shuzhen Wang0129d522016-10-30 22:43:41 -0700733 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
734 outputConfiguration.getGraphicBufferProducers();
735 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800736 bool deferredConsumer = outputConfiguration.isDeferred();
737 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800738 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800739 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700740
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000741 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000742 outputConfiguration.getSurfaceType());
743 if (!res.isOk()) {
744 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700745 }
Zhijun He5d677d12016-05-29 16:52:39 -0700746
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800747 if (!mDevice.get()) {
748 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
749 }
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800750 std::vector<std::string> physicalCameraIds;
751 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &physicalCameraIds);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000752 res = SessionConfigurationUtils::checkPhysicalCameraId(physicalCameraIds, physicalCameraId,
753 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000754 if (!res.isOk()) {
755 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800756 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000757
Shuzhen Wang0129d522016-10-30 22:43:41 -0700758 std::vector<sp<Surface>> surfaces;
759 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700760 status_t err;
761
762 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700763 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800764 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700765 }
766
Shuzhen Wang758c2152017-01-10 18:26:18 -0800767 OutputStreamInfo streamInfo;
768 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700769 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700770 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700771 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800772 ssize_t index = mStreamMap.indexOfKey(binder);
773 if (index != NAME_NOT_FOUND) {
774 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
775 "(ID %zd)", mCameraIdStr.string(), index);
776 ALOGW("%s: %s", __FUNCTION__, msg.string());
777 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700778 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700779
Shuzhen Wang758c2152017-01-10 18:26:18 -0800780 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000781 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo, isStreamInfoValid,
782 surface, bufferProducer, mCameraIdStr, mDevice->infoPhysical(physicalCameraId));
Shuzhen Wang758c2152017-01-10 18:26:18 -0800783
784 if (!res.isOk())
785 return res;
786
787 if (!isStreamInfoValid) {
788 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700789 }
790
Shuzhen Wang758c2152017-01-10 18:26:18 -0800791 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700792 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800793 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700794
Zhijun He125684a2015-12-26 15:07:30 -0800795 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100796 std::vector<int> surfaceIds;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800797 bool isDepthCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
798 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
799 if (isDepthCompositeStream || isHeicCompisiteStream) {
800 sp<CompositeStream> compositeStream;
801 if (isDepthCompositeStream) {
802 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
803 } else {
804 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
805 }
806
Emilian Peev538c90e2018-12-17 18:03:19 +0000807 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
808 streamInfo.height, streamInfo.format,
809 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
810 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
811 isShared);
812 if (err == OK) {
813 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
814 compositeStream);
815 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800816 } else {
817 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
818 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
819 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
820 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
821 isShared);
Emilian Peev538c90e2018-12-17 18:03:19 +0000822 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700823
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800824 if (err != OK) {
825 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800826 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800827 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
828 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800829 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700830 int i = 0;
831 for (auto& binder : binders) {
832 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
833 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100834 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
835 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700836 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800837
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800838 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800839 mStreamInfoMap[streamId] = streamInfo;
840
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800841 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700842 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800843 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
844 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700845
Zhijun He5d677d12016-05-29 16:52:39 -0700846 // Set transform flags to ensure preview to be rotated correctly.
847 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700848
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800849 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700850 }
851
852 return res;
853}
854
Zhijun He5d677d12016-05-29 16:52:39 -0700855binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
856 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800857 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700858 /*out*/
859 int* newStreamId) {
860 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100861 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700862 android_dataspace dataSpace;
863 status_t err;
864 binder::Status res;
865
866 if (!mDevice.get()) {
867 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
868 }
869
870 // Infer the surface info for deferred surface stream creation.
871 width = outputConfiguration.getWidth();
872 height = outputConfiguration.getHeight();
873 surfaceType = outputConfiguration.getSurfaceType();
874 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
875 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
876 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
877 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
878 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
879 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
880 }
881 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100883 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800884 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700885 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
886 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700887 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800888 &streamId, physicalCameraId, &surfaceIds,
889 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev40ead602017-09-26 15:46:36 +0100890 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700891
892 if (err != OK) {
893 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800894 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
895 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700896 } else {
897 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
898 // a separate list to track. Once the deferred surface is set, this id will be
899 // relocated to mStreamMap.
900 mDeferredStreams.push_back(streamId);
901
Shuzhen Wang758c2152017-01-10 18:26:18 -0800902 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
903 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
904
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800905 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700906 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800907 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700908
909 // Set transform flags to ensure preview to be rotated correctly.
910 res = setStreamTransformLocked(streamId);
911
912 *newStreamId = streamId;
913 }
914 return res;
915}
916
917binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
918 int32_t transform = 0;
919 status_t err;
920 binder::Status res;
921
922 if (!mDevice.get()) {
923 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
924 }
925
926 err = getRotationTransformLocked(&transform);
927 if (err != OK) {
928 // Error logged by getRotationTransformLocked.
929 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
930 "Unable to calculate rotation transform for new stream");
931 }
932
933 err = mDevice->setStreamTransform(streamId, transform);
934 if (err != OK) {
935 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
936 streamId);
937 ALOGE("%s: %s", __FUNCTION__, msg.string());
938 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
939 }
940
941 return res;
942}
Ruben Brunkbba75572014-11-20 17:29:50 -0800943
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800944binder::Status CameraDeviceClient::createInputStream(
945 int width, int height, int format,
946 /*out*/
947 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700948
949 ATRACE_CALL();
950 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
951
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800952 binder::Status res;
953 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700954
955 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800956
957 if (!mDevice.get()) {
958 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
959 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700960
961 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800962 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -0800963 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800964 ALOGE("%s: %s", __FUNCTION__, msg.string() );
965 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700966 }
967
968 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969 status_t err = mDevice->createInputStream(width, height, format, &streamId);
970 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700971 mInputStream.configured = true;
972 mInputStream.width = width;
973 mInputStream.height = height;
974 mInputStream.format = format;
975 mInputStream.id = streamId;
976
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800977 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
978 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700979
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800980 *newStreamId = streamId;
981 } else {
982 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800983 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800984 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700985 }
986
987 return res;
988}
989
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800990binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700991
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800992 binder::Status res;
993 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
994
995 if (inputSurface == NULL) {
996 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700997 }
998
999 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001000 if (!mDevice.get()) {
1001 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1002 }
1003 sp<IGraphicBufferProducer> producer;
1004 status_t err = mDevice->getInputBufferProducer(&producer);
1005 if (err != OK) {
1006 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001007 "Camera %s: Error getting input Surface: %s (%d)",
1008 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001009 } else {
1010 inputSurface->name = String16("CameraInput");
1011 inputSurface->graphicBufferProducer = producer;
1012 }
1013 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001014}
1015
Emilian Peev40ead602017-09-26 15:46:36 +01001016binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1017 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1018 ATRACE_CALL();
1019
1020 binder::Status res;
1021 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1022
1023 Mutex::Autolock icl(mBinderSerializationLock);
1024
1025 if (!mDevice.get()) {
1026 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1027 }
1028
1029 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1030 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001031 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1032
Emilian Peev40ead602017-09-26 15:46:36 +01001033 auto producerCount = bufferProducers.size();
1034 if (producerCount == 0) {
1035 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1036 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1037 "bufferProducers must not be empty");
1038 }
1039
1040 // The first output is the one associated with the output configuration.
1041 // It should always be present, valid and the corresponding stream id should match.
1042 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1043 ssize_t index = mStreamMap.indexOfKey(binder);
1044 if (index == NAME_NOT_FOUND) {
1045 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1046 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1047 "OutputConfiguration is invalid");
1048 }
1049 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1050 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1051 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1052 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1053 "Stream id is invalid");
1054 }
1055
1056 std::vector<size_t> removedSurfaceIds;
1057 std::vector<sp<IBinder>> removedOutputs;
1058 std::vector<sp<Surface>> newOutputs;
1059 std::vector<OutputStreamInfo> streamInfos;
1060 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1061 for (auto &it : bufferProducers) {
1062 newOutputsMap.add(IInterface::asBinder(it), it);
1063 }
1064
1065 for (size_t i = 0; i < mStreamMap.size(); i++) {
1066 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1067 if (idx == NAME_NOT_FOUND) {
1068 if (mStreamMap[i].streamId() == streamId) {
1069 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1070 removedOutputs.push_back(mStreamMap.keyAt(i));
1071 }
1072 } else {
1073 if (mStreamMap[i].streamId() != streamId) {
1074 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1075 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1076 "Target Surface is invalid");
1077 }
1078 newOutputsMap.removeItemsAt(idx);
1079 }
1080 }
1081
1082 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1083 OutputStreamInfo outInfo;
1084 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001085 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false,
1086 surface, newOutputsMap.valueAt(i), mCameraIdStr,
1087 mDevice->infoPhysical(physicalCameraId));
Emilian Peev40ead602017-09-26 15:46:36 +01001088 if (!res.isOk())
1089 return res;
1090
Emilian Peev40ead602017-09-26 15:46:36 +01001091 streamInfos.push_back(outInfo);
1092 newOutputs.push_back(surface);
1093 }
1094
1095 //Trivial case no changes required
1096 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1097 return binder::Status::ok();
1098 }
1099
1100 KeyedVector<sp<Surface>, size_t> outputMap;
1101 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1102 &outputMap);
1103 if (ret != OK) {
1104 switch (ret) {
1105 case NAME_NOT_FOUND:
1106 case BAD_VALUE:
1107 case -EBUSY:
1108 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1109 "Camera %s: Error updating stream: %s (%d)",
1110 mCameraIdStr.string(), strerror(ret), ret);
1111 break;
1112 default:
1113 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1114 "Camera %s: Error updating stream: %s (%d)",
1115 mCameraIdStr.string(), strerror(ret), ret);
1116 break;
1117 }
1118 } else {
1119 for (const auto &it : removedOutputs) {
1120 mStreamMap.removeItem(it);
1121 }
1122
1123 for (size_t i = 0; i < outputMap.size(); i++) {
1124 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1125 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1126 }
1127
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001128 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1129
Emilian Peev40ead602017-09-26 15:46:36 +01001130 ALOGV("%s: Camera %s: Successful stream ID %d update",
1131 __FUNCTION__, mCameraIdStr.string(), streamId);
1132 }
1133
1134 return res;
1135}
1136
Igor Murashkine7ee7632013-06-11 18:10:18 -07001137// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001138binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1139 /*out*/
1140 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001141{
1142 ATRACE_CALL();
1143 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1144
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001145 binder::Status res;
1146 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001147
1148 Mutex::Autolock icl(mBinderSerializationLock);
1149
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001150 if (!mDevice.get()) {
1151 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1152 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001153
1154 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001155 status_t err;
1156 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001157 request != NULL) {
1158
1159 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001160 } else if (err == BAD_VALUE) {
1161 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001162 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1163 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001164
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001165 } else {
1166 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001167 "Camera %s: Error creating default request for template %d: %s (%d)",
1168 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001169 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001170 return res;
1171}
1172
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001173binder::Status CameraDeviceClient::getCameraInfo(
1174 /*out*/
1175 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001176{
1177 ATRACE_CALL();
1178 ALOGV("%s", __FUNCTION__);
1179
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001180 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001181
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001182 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001183
1184 Mutex::Autolock icl(mBinderSerializationLock);
1185
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001186 if (!mDevice.get()) {
1187 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1188 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001189
Igor Murashkin099b4572013-07-12 17:52:16 -07001190 if (info != NULL) {
1191 *info = mDevice->info(); // static camera metadata
1192 // TODO: merge with device-specific camera metadata
1193 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001194
1195 return res;
1196}
1197
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001198binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001199{
1200 ATRACE_CALL();
1201 ALOGV("%s", __FUNCTION__);
1202
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 binder::Status res;
1204 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001205
1206 Mutex::Autolock icl(mBinderSerializationLock);
1207
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001208 if (!mDevice.get()) {
1209 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1210 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001211
1212 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001213 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001214 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001215 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001216 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1217 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001218 ALOGE("%s: %s", __FUNCTION__, msg.string());
1219 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001220 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001221 status_t err = mDevice->waitUntilDrained();
1222 if (err != OK) {
1223 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001224 "Camera %s: Error waiting to drain: %s (%d)",
1225 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001226 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001227 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001228 return res;
1229}
1230
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001231binder::Status CameraDeviceClient::flush(
1232 /*out*/
1233 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001234 ATRACE_CALL();
1235 ALOGV("%s", __FUNCTION__);
1236
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001237 binder::Status res;
1238 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001239
1240 Mutex::Autolock icl(mBinderSerializationLock);
1241
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001242 if (!mDevice.get()) {
1243 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1244 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001245
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001246 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001247 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001248 status_t err = mDevice->flush(lastFrameNumber);
1249 if (err != OK) {
1250 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001251 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001252 }
1253 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001254}
1255
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001256binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001257 ATRACE_CALL();
1258 ALOGV("%s", __FUNCTION__);
1259
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001260 binder::Status res;
1261 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001262
1263 Mutex::Autolock icl(mBinderSerializationLock);
1264
1265 // Guard against trying to prepare non-created streams
1266 ssize_t index = NAME_NOT_FOUND;
1267 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001268 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001269 index = i;
1270 break;
1271 }
1272 }
1273
1274 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001275 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1276 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001277 ALOGW("%s: %s", __FUNCTION__, msg.string());
1278 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001279 }
1280
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001281 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1282 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001283 status_t err = mDevice->prepare(streamId);
1284 if (err == BAD_VALUE) {
1285 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001286 "Camera %s: Stream %d has already been used, and cannot be prepared",
1287 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001288 } else if (err != OK) {
1289 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001290 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001291 strerror(-err), err);
1292 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001293 return res;
1294}
1295
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001296binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001297 ATRACE_CALL();
1298 ALOGV("%s", __FUNCTION__);
1299
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001300 binder::Status res;
1301 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001302
1303 Mutex::Autolock icl(mBinderSerializationLock);
1304
1305 // Guard against trying to prepare non-created streams
1306 ssize_t index = NAME_NOT_FOUND;
1307 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001308 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001309 index = i;
1310 break;
1311 }
1312 }
1313
1314 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001315 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1316 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001317 ALOGW("%s: %s", __FUNCTION__, msg.string());
1318 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001319 }
1320
1321 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001322 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1323 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001324 ALOGE("%s: %s", __FUNCTION__, msg.string());
1325 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001326 }
1327
1328 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1329 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001330 status_t err = mDevice->prepare(maxCount, streamId);
1331 if (err == BAD_VALUE) {
1332 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001333 "Camera %s: Stream %d has already been used, and cannot be prepared",
1334 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001335 } else if (err != OK) {
1336 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001337 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001338 strerror(-err), err);
1339 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001340
1341 return res;
1342}
1343
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001344binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001345 ATRACE_CALL();
1346 ALOGV("%s", __FUNCTION__);
1347
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001348 binder::Status res;
1349 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001350
1351 Mutex::Autolock icl(mBinderSerializationLock);
1352
1353 // Guard against trying to prepare non-created streams
1354 ssize_t index = NAME_NOT_FOUND;
1355 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001356 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001357 index = i;
1358 break;
1359 }
1360 }
1361
1362 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001363 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1364 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001365 ALOGW("%s: %s", __FUNCTION__, msg.string());
1366 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001367 }
1368
1369 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1370 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001371 status_t err = mDevice->tearDown(streamId);
1372 if (err == BAD_VALUE) {
1373 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001374 "Camera %s: Stream %d is still in use, cannot be torn down",
1375 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001376 } else if (err != OK) {
1377 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001378 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001379 strerror(-err), err);
1380 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001381
1382 return res;
1383}
1384
Shuzhen Wang758c2152017-01-10 18:26:18 -08001385binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001386 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1387 ATRACE_CALL();
1388
1389 binder::Status res;
1390 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1391
1392 Mutex::Autolock icl(mBinderSerializationLock);
1393
Shuzhen Wang0129d522016-10-30 22:43:41 -07001394 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1395 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001396 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001397
Shuzhen Wang0129d522016-10-30 22:43:41 -07001398 if (bufferProducers.size() == 0) {
1399 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001400 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1401 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001402
Shuzhen Wang758c2152017-01-10 18:26:18 -08001403 // streamId should be in mStreamMap if this stream already has a surface attached
1404 // to it. Otherwise, it should be in mDeferredStreams.
1405 bool streamIdConfigured = false;
1406 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1407 for (size_t i = 0; i < mStreamMap.size(); i++) {
1408 if (mStreamMap.valueAt(i).streamId() == streamId) {
1409 streamIdConfigured = true;
1410 break;
1411 }
Zhijun He5d677d12016-05-29 16:52:39 -07001412 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001413 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1414 if (streamId == mDeferredStreams[i]) {
1415 deferredStreamIndex = i;
1416 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001417 }
1418
Shuzhen Wang758c2152017-01-10 18:26:18 -08001419 }
1420 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1421 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1422 "(ID %d)", mCameraIdStr.string(), streamId);
1423 ALOGW("%s: %s", __FUNCTION__, msg.string());
1424 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001425 }
1426
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001427 if (mStreamInfoMap[streamId].finalized) {
1428 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1429 " on stream ID %d", mCameraIdStr.string(), streamId);
1430 ALOGW("%s: %s", __FUNCTION__, msg.string());
1431 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1432 }
1433
Zhijun He5d677d12016-05-29 16:52:39 -07001434 if (!mDevice.get()) {
1435 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1436 }
1437
Shuzhen Wang758c2152017-01-10 18:26:18 -08001438 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001439 for (auto& bufferProducer : bufferProducers) {
1440 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001441 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1442 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001443 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001444 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001445 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001446 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001447
1448 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001449 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
1450 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
1451 mDevice->infoPhysical(physicalId));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001452
1453 if (!res.isOk())
1454 return res;
1455
1456 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001457 }
1458
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001459 // Gracefully handle case where finalizeOutputConfigurations is called
1460 // without any new surface.
1461 if (consumerSurfaces.size() == 0) {
1462 mStreamInfoMap[streamId].finalized = true;
1463 return res;
1464 }
1465
Zhijun He5d677d12016-05-29 16:52:39 -07001466 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001467 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001468 std::vector<int> consumerSurfaceIds;
1469 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001470 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001471 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1472 sp<IBinder> binder = IInterface::asBinder(
1473 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001474 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001475 binder.get(), streamId, consumerSurfaceIds[i]);
1476 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1477 }
1478 if (deferredStreamIndex != NAME_NOT_FOUND) {
1479 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001480 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001481 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001482 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001483 } else if (err == NO_INIT) {
1484 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001485 "Camera %s: Deferred surface is invalid: %s (%d)",
1486 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001487 } else {
1488 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001489 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1490 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001491 }
1492
1493 return res;
1494}
1495
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001496binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001497 ATRACE_CALL();
1498 binder::Status res;
1499 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1500
1501 if (!isValidAudioRestriction(mode)) {
1502 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1503 mCameraIdStr.string(), mode);
1504 ALOGW("%s: %s", __FUNCTION__, msg.string());
1505 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1506 }
1507
1508 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001509 BasicClient::setAudioRestriction(mode);
1510 return binder::Status::ok();
1511}
1512
1513binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1514 ATRACE_CALL();
1515 binder::Status res;
1516 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1517 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001518 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001519 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001520 }
1521 return binder::Status::ok();
1522}
1523
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001524status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1525 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1526
1527 return mDevice->setRotateAndCropAutoBehavior(
1528 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1529}
1530
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001531binder::Status CameraDeviceClient::switchToOffline(
1532 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001533 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001534 /*out*/
1535 sp<hardware::camera2::ICameraOfflineSession>* session) {
1536 ATRACE_CALL();
1537
1538 binder::Status res;
1539 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1540
1541 Mutex::Autolock icl(mBinderSerializationLock);
1542
1543 if (!mDevice.get()) {
1544 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1545 }
1546
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001547 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001548 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001549 ALOGE("%s: %s", __FUNCTION__, msg.string());
1550 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1551 }
1552
1553 if (session == nullptr) {
1554 String8 msg = String8::format("Invalid offline session");
1555 ALOGE("%s: %s", __FUNCTION__, msg.string());
1556 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1557 }
1558
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001559 std::vector<int32_t> offlineStreamIds;
1560 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001561 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001562 for (const auto& streamId : offlineOutputIds) {
1563 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001564 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001565 String8 msg = String8::format("Offline surface with id: %d is not registered",
1566 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001567 ALOGE("%s: %s", __FUNCTION__, msg.string());
1568 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1569 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001570
1571 if (!mStreamInfoMap[streamId].supportsOffline) {
1572 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1573 "offline mode", streamId);
1574 ALOGE("%s: %s", __FUNCTION__, msg.string());
1575 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1576 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001577
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001578 bool isCompositeStream = false;
1579 for (const auto& gbp : mConfiguredOutputs[streamId].getGraphicBufferProducers()) {
1580 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
1581 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) |
1582 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001583 if (isCompositeStream) {
1584 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1585 if (compositeIdx == NAME_NOT_FOUND) {
1586 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1587 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1588 "Unknown composite stream");
1589 }
1590
1591 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1592 &offlineStreamIds);
1593 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1594 mCompositeStreamMap.valueAt(compositeIdx));
1595 break;
1596 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001597 }
1598
Emilian Peev4697b642019-11-19 17:11:14 -08001599 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001600 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001601 }
1602 }
1603
1604 sp<CameraOfflineSessionBase> offlineSession;
1605 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1606 if (ret != OK) {
1607 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1608 "Camera %s: Error switching to offline mode: %s (%d)",
1609 mCameraIdStr.string(), strerror(ret), ret);
1610 }
1611
Emilian Peevcc0b7952020-01-07 13:54:47 -08001612 sp<CameraOfflineSessionClient> offlineClient;
1613 if (offlineSession.get() != nullptr) {
1614 offlineClient = new CameraOfflineSessionClient(sCameraService,
1615 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
1616 mClientFeatureId, mCameraIdStr, mCameraFacing, mClientPid, mClientUid, mServicePid);
1617 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1618 }
1619
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001620 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001621 // A successful offline session switch must reset the current camera client
1622 // and release any resources occupied by previously configured streams.
1623 mStreamMap.clear();
1624 mConfiguredOutputs.clear();
1625 mDeferredStreams.clear();
1626 mStreamInfoMap.clear();
1627 mCompositeStreamMap.clear();
1628 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001629 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001630 switch(ret) {
1631 case BAD_VALUE:
1632 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1633 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1634 case TIMED_OUT:
1635 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1636 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1637 default:
1638 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1639 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1640 strerror(-ret), ret);
1641 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001642 }
1643
1644 *session = offlineClient;
1645
1646 return binder::Status::ok();
1647}
1648
Igor Murashkine7ee7632013-06-11 18:10:18 -07001649status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001650 return BasicClient::dump(fd, args);
1651}
1652
1653status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001654 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001655 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001656 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001657 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001658 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001659
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001660 dprintf(fd, " State:\n");
1661 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001662 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001663 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001664 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001665 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001666 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001667 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001668 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001669 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001670 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001671 mStreamMap.valueAt(i).streamId(),
1672 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001673 }
Zhijun He5d677d12016-05-29 16:52:39 -07001674 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001675 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001676 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001677 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001678 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001679 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001680 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001681 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001682 // TODO: print dynamic/request section from most recent requests
1683 mFrameProcessor->dump(fd, args);
1684
1685 return dumpDevice(fd, args);
1686}
1687
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001688void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001689 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001690 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001691 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001692
Emilian Peev538c90e2018-12-17 18:03:19 +00001693 // Composites can have multiple internal streams. Error notifications coming from such internal
1694 // streams may need to remain within camera service.
1695 bool skipClientNotification = false;
1696 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1697 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
1698 }
1699
1700 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001701 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001702 }
1703}
1704
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001705void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1706 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1707
1708 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001709 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001710 }
1711
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001712 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001713 mStreamingRequestId = REQUEST_ID_NONE;
1714}
1715
Shuzhen Wang316781a2020-08-18 18:11:01 -07001716void CameraDeviceClient::notifyIdle(
1717 int64_t requestCount, int64_t resultErrorCount, bool deviceError,
1718 const std::vector<hardware::CameraStreamStats>& streamStats) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001719 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001720 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001721
1722 if (remoteCb != 0) {
1723 remoteCb->onDeviceIdle();
1724 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001725 Camera2ClientBase::notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001726}
1727
Jianing Weicb0652e2014-03-12 18:29:36 -07001728void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001729 nsecs_t timestamp) {
1730 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001731 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001732 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001733 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001734 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001735 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001736
1737 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1738 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1739 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001740}
1741
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001742void CameraDeviceClient::notifyPrepared(int streamId) {
1743 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001744 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001745 if (remoteCb != 0) {
1746 remoteCb->onPrepared(streamId);
1747 }
1748}
1749
Shuzhen Wang9d066012016-09-30 11:30:20 -07001750void CameraDeviceClient::notifyRequestQueueEmpty() {
1751 // Thread safe. Don't bother locking.
1752 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1753 if (remoteCb != 0) {
1754 remoteCb->onRequestQueueEmpty();
1755 }
1756}
1757
Igor Murashkine7ee7632013-06-11 18:10:18 -07001758void CameraDeviceClient::detachDevice() {
1759 if (mDevice == 0) return;
1760
Shuzhen Wang316781a2020-08-18 18:11:01 -07001761 nsecs_t startTime = systemTime();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001762 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001763
Emilian Peevfaa4bde2020-01-23 12:19:37 -08001764 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
1765 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Igor Murashkine7ee7632013-06-11 18:10:18 -07001766 /*listener*/this);
1767 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001768 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001769 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001770 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001771
1772 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1773 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001774 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001775 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001776 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
1777 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
1778 }
1779
Igor Murashkine7ee7632013-06-11 18:10:18 -07001780 if ((code = mDevice->waitUntilDrained()) != OK) {
1781 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1782 code);
1783 }
1784 }
1785
Emilian Peev5e4c7322019-10-22 14:20:52 -07001786 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1787 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
1788 if (ret != OK) {
1789 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
1790 strerror(-ret), ret);
1791 }
1792 }
1793 mCompositeStreamMap.clear();
1794
Igor Murashkine7ee7632013-06-11 18:10:18 -07001795 Camera2ClientBase::detachDevice();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001796
1797 int32_t closeLatencyMs = ns2ms(systemTime() - startTime);
1798 CameraServiceProxyWrapper::logClose(mCameraIdStr, closeLatencyMs);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001799}
1800
1801/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001802void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001803 ATRACE_CALL();
1804 ALOGV("%s", __FUNCTION__);
1805
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001806 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001807 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001808 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001809 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
1810 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001811 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001812
1813 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1814 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
1815 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001816}
1817
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001818binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001819 if (mDisconnected) {
1820 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1821 "The camera device has been disconnected");
1822 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001823 status_t res = checkPid(checkLocation);
1824 return (res == OK) ? binder::Status::ok() :
1825 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1826 "Attempt to use camera from a different process than original client");
1827}
1828
Igor Murashkine7ee7632013-06-11 18:10:18 -07001829// TODO: move to Camera2ClientBase
1830bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1831
Jayant Chowdhary12361932018-08-27 14:46:13 -07001832 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001833 const int selfPid = getpid();
1834 camera_metadata_entry_t entry;
1835
1836 /**
1837 * Mixin default important security values
1838 * - android.led.transmit = defaulted ON
1839 */
1840 CameraMetadata staticInfo = mDevice->info();
1841 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1842 for(size_t i = 0; i < entry.count; ++i) {
1843 uint8_t led = entry.data.u8[i];
1844
1845 switch(led) {
1846 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1847 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1848 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1849 metadata.update(ANDROID_LED_TRANSMIT,
1850 &transmitDefault, 1);
1851 }
1852 break;
1853 }
1854 }
1855 }
1856
1857 // We can do anything!
1858 if (pid == selfPid) {
1859 return true;
1860 }
1861
1862 /**
1863 * Permission check special fields in the request
1864 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1865 */
1866 entry = metadata.find(ANDROID_LED_TRANSMIT);
1867 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1868 String16 permissionString =
1869 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1870 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001871 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001872 ALOGE("Permission Denial: "
1873 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1874 return false;
1875 }
1876 }
1877
1878 return true;
1879}
1880
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001881status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1882 ALOGV("%s: begin", __FUNCTION__);
1883
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001884 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001885 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001886}
1887
Igor Murashkine7ee7632013-06-11 18:10:18 -07001888} // namespace android