blob: e80838bf73eb9e4e68d90af3a937129c5315d672 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Jayant Chowdhary12361932018-08-27 14:46:13 -070022#include <utils/CameraThreadState.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <utils/Log.h>
Colin Crossb8a9dbb2020-08-27 04:12:26 +000024#include <utils/SessionConfigurationUtils.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070025#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070026#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070028#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070029
30#include "common/CameraDeviceBase.h"
Emilian Peev35ae8262018-11-08 13:11:32 +000031#include "device3/Camera3Device.h"
32#include "device3/Camera3OutputStream.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070033#include "api2/CameraDeviceClient.h"
34
Emilian Peev00420d22018-02-05 21:33:13 +000035#include <camera_metadata_hidden.h>
36
Emilian Peev538c90e2018-12-17 18:03:19 +000037#include "DepthCompositeStream.h"
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080038#include "HeicCompositeStream.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000039
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070041
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080042#define STATUS_ERROR(errorCode, errorString) \
43 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080044 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080045
46#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
47 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080048 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080049 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070050
51namespace android {
52using namespace camera2;
53
54CameraDeviceClientBase::CameraDeviceClientBase(
55 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080056 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070057 const String16& clientPackageName,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090058 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080059 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080060 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070061 int cameraFacing,
62 int clientPid,
63 uid_t clientUid,
64 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080065 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080066 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080067 clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080068 clientFeatureId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080069 cameraId,
70 cameraFacing,
71 clientPid,
72 clientUid,
73 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070074 mRemoteCallback(remoteCallback) {
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080075 // We don't need it for API2 clients, but Camera2ClientBase requires it.
76 (void) api1CameraId;
Igor Murashkine7ee7632013-06-11 18:10:18 -070077}
Igor Murashkine7ee7632013-06-11 18:10:18 -070078
79// Interface used by CameraService
80
81CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080082 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
83 const String16& clientPackageName,
Jooyung Han3f9a3b42020-01-23 12:27:18 +090084 const std::optional<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080085 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080086 int cameraFacing,
87 int clientPid,
88 uid_t clientUid,
89 int servicePid) :
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080090 Camera2ClientBase(cameraService, remoteCallback, clientPackageName, clientFeatureId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080091 cameraId, /*API1 camera ID*/ -1,
92 cameraFacing, clientPid, clientUid, servicePid),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070093 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070094 mStreamingRequestId(REQUEST_ID_NONE),
Igor Murashkine7ee7632013-06-11 18:10:18 -070095 mRequestIdCounter(0) {
96
97 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080098 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -070099}
100
Emilian Peevbd8c5032018-02-14 23:05:40 +0000101status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
102 const String8& monitorTags) {
103 return initializeImpl(manager, monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800104}
105
106template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000107status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700108 ATRACE_CALL();
109 status_t res;
110
Emilian Peevbd8c5032018-02-14 23:05:40 +0000111 res = Camera2ClientBase::initialize(providerPtr, monitorTags);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112 if (res != OK) {
113 return res;
114 }
115
116 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700117 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800118 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700119 mFrameProcessor->run(threadName.string());
120
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800121 mFrameProcessor->registerListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
122 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800123 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700124 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700125
Emilian Peev00420d22018-02-05 21:33:13 +0000126 auto deviceInfo = mDevice->info();
127 camera_metadata_entry_t physicalKeysEntry = deviceInfo.find(
128 ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
129 if (physicalKeysEntry.count > 0) {
130 mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
131 physicalKeysEntry.data.i32,
132 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
133 }
134
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700135 mProviderManager = providerPtr;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700136 return OK;
137}
138
139CameraDeviceClient::~CameraDeviceClient() {
140}
141
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800142binder::Status CameraDeviceClient::submitRequest(
143 const hardware::camera2::CaptureRequest& request,
144 bool streaming,
145 /*out*/
146 hardware::camera2::utils::SubmitInfo *submitInfo) {
147 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
148 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700149}
150
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800151binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000152 SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
Emilian Peev538c90e2018-12-17 18:03:19 +0000153 int compositeIdx;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800154 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
155
156 // Trying to submit request with surface that wasn't created
157 if (idx == NAME_NOT_FOUND) {
158 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
159 " we have not called createStream on",
160 __FUNCTION__, mCameraIdStr.string());
161 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
162 "Request targets Surface that is not part of current capture session");
Emilian Peev538c90e2018-12-17 18:03:19 +0000163 } else if ((compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp)))
164 != NAME_NOT_FOUND) {
165 mCompositeStreamMap.valueAt(compositeIdx)->insertGbp(outSurfaceMap, outputStreamIds,
166 currentStreamId);
167 return binder::Status::ok();
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800168 }
169
170 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
171 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
172 (*outSurfaceMap)[streamSurfaceId.streamId()] = std::vector<size_t>();
173 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,
Emilian Peevcc0b7952020-01-07 13:54:47 -0800474 const hardware::camera2::impl::CameraMetadataNative& sessionParams,
475 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 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000550 }
551
552 return res;
553}
554
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800555binder::Status CameraDeviceClient::isSessionConfigurationSupported(
556 const SessionConfiguration& sessionConfiguration, bool *status /*out*/) {
557 ATRACE_CALL();
558
559 binder::Status res;
560 status_t ret = OK;
561 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
562
563 Mutex::Autolock icl(mBinderSerializationLock);
564
565 if (!mDevice.get()) {
566 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
567 }
568
569 auto operatingMode = sessionConfiguration.getOperatingMode();
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000570 res = SessionConfigurationUtils::checkOperatingMode(operatingMode, mDevice->info(),
571 mCameraIdStr);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800572 if (!res.isOk()) {
573 return res;
574 }
575
576 if (status == nullptr) {
577 String8 msg = String8::format( "Camera %s: Invalid status!", mCameraIdStr.string());
578 ALOGE("%s: %s", __FUNCTION__, msg.string());
579 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
580 }
581 hardware::camera::device::V3_4::StreamConfiguration streamConfiguration;
582 bool earlyExit = false;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800583 metadataGetter getMetadata = [this](const String8 &id) {return mDevice->infoPhysical(id);};
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800584 std::vector<std::string> physicalCameraIds;
585 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &physicalCameraIds);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000586 res = SessionConfigurationUtils::convertToHALStreamCombination(sessionConfiguration,
587 mCameraIdStr, mDevice->info(), getMetadata, physicalCameraIds, streamConfiguration,
588 &earlyExit);
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800589 if (!res.isOk()) {
590 return res;
591 }
592
593 if (earlyExit) {
594 *status = false;
595 return binder::Status::ok();
596 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000597
598 *status = false;
599 ret = mProviderManager->isSessionConfigurationSupported(mCameraIdStr.string(),
600 streamConfiguration, status);
601 switch (ret) {
602 case OK:
603 // Expected, do nothing.
604 break;
605 case INVALID_OPERATION: {
606 String8 msg = String8::format(
607 "Camera %s: Session configuration query not supported!",
608 mCameraIdStr.string());
609 ALOGD("%s: %s", __FUNCTION__, msg.string());
610 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
611 }
612
613 break;
614 default: {
615 String8 msg = String8::format( "Camera %s: Error: %s (%d)", mCameraIdStr.string(),
616 strerror(-ret), ret);
617 ALOGE("%s: %s", __FUNCTION__, msg.string());
618 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
619 msg.string());
620 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800621 }
622
623 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700624}
625
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800626binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700627 ATRACE_CALL();
628 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
629
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800630 binder::Status res;
631 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700632
633 Mutex::Autolock icl(mBinderSerializationLock);
634
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800635 if (!mDevice.get()) {
636 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
637 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700638
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700639 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700640 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700641 ssize_t dIndex = NAME_NOT_FOUND;
Emilian Peev538c90e2018-12-17 18:03:19 +0000642 ssize_t compositeIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700643
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700644 if (mInputStream.configured && mInputStream.id == streamId) {
645 isInput = true;
646 } else {
647 // Guard against trying to delete non-created streams
648 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700649 if (streamId == mStreamMap.valueAt(i).streamId()) {
650 surfaces.push_back(mStreamMap.keyAt(i));
651 }
652 }
653
654 // See if this stream is one of the deferred streams.
655 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
656 if (streamId == mDeferredStreams[i]) {
657 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700658 break;
659 }
660 }
661
Emilian Peev538c90e2018-12-17 18:03:19 +0000662 for (size_t i = 0; i < mCompositeStreamMap.size(); ++i) {
663 if (streamId == mCompositeStreamMap.valueAt(i)->getStreamId()) {
664 compositeIndex = i;
665 break;
666 }
667 }
668
Shuzhen Wang0129d522016-10-30 22:43:41 -0700669 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
670 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
671 " stream created yet", mCameraIdStr.string(), streamId);
672 ALOGW("%s: %s", __FUNCTION__, msg.string());
673 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700674 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700675 }
676
677 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800678 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700679
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800680 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800681 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
682 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800683 ALOGE("%s: %s", __FUNCTION__, msg.string());
684 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
685 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700686 if (isInput) {
687 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700688 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700689 for (auto& surface : surfaces) {
690 mStreamMap.removeItem(surface);
691 }
692
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800693 mConfiguredOutputs.removeItem(streamId);
694
Shuzhen Wang0129d522016-10-30 22:43:41 -0700695 if (dIndex != NAME_NOT_FOUND) {
696 mDeferredStreams.removeItemsAt(dIndex);
697 }
Emilian Peev538c90e2018-12-17 18:03:19 +0000698
699 if (compositeIndex != NAME_NOT_FOUND) {
700 status_t ret;
701 if ((ret = mCompositeStreamMap.valueAt(compositeIndex)->deleteStream())
702 != OK) {
703 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when "
704 "deleting composite stream %d", mCameraIdStr.string(), strerror(-err), err,
705 streamId);
706 ALOGE("%s: %s", __FUNCTION__, msg.string());
707 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
708 }
709 mCompositeStreamMap.removeItemsAt(compositeIndex);
710 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700711 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700712 }
713
714 return res;
715}
716
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800717binder::Status CameraDeviceClient::createStream(
718 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
719 /*out*/
720 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700721 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700722
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800723 binder::Status res;
724 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700725
726 Mutex::Autolock icl(mBinderSerializationLock);
727
Shuzhen Wang0129d522016-10-30 22:43:41 -0700728 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
729 outputConfiguration.getGraphicBufferProducers();
730 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800731 bool deferredConsumer = outputConfiguration.isDeferred();
732 bool isShared = outputConfiguration.isShared();
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800733 String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang758c2152017-01-10 18:26:18 -0800734 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700735
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000736 res = SessionConfigurationUtils::checkSurfaceType(numBufferProducers, deferredConsumer,
Emilian Peev35ae8262018-11-08 13:11:32 +0000737 outputConfiguration.getSurfaceType());
738 if (!res.isOk()) {
739 return res;
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700740 }
Zhijun He5d677d12016-05-29 16:52:39 -0700741
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800742 if (!mDevice.get()) {
743 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
744 }
Jayant Chowdhary2bbdce42020-01-12 14:55:41 -0800745 std::vector<std::string> physicalCameraIds;
746 mProviderManager->isLogicalCamera(mCameraIdStr.string(), &physicalCameraIds);
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000747 res = SessionConfigurationUtils::checkPhysicalCameraId(physicalCameraIds, physicalCameraId,
748 mCameraIdStr);
Emilian Peev35ae8262018-11-08 13:11:32 +0000749 if (!res.isOk()) {
750 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800751 }
Emilian Peev35ae8262018-11-08 13:11:32 +0000752
Shuzhen Wang0129d522016-10-30 22:43:41 -0700753 std::vector<sp<Surface>> surfaces;
754 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700755 status_t err;
756
757 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700758 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800759 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700760 }
761
Shuzhen Wang758c2152017-01-10 18:26:18 -0800762 OutputStreamInfo streamInfo;
763 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700764 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700765 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700766 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800767 ssize_t index = mStreamMap.indexOfKey(binder);
768 if (index != NAME_NOT_FOUND) {
769 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
770 "(ID %zd)", mCameraIdStr.string(), index);
771 ALOGW("%s: %s", __FUNCTION__, msg.string());
772 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700774
Shuzhen Wang758c2152017-01-10 18:26:18 -0800775 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +0000776 res = SessionConfigurationUtils::createSurfaceFromGbp(streamInfo, isStreamInfoValid,
777 surface, bufferProducer, mCameraIdStr, mDevice->infoPhysical(physicalCameraId));
Shuzhen Wang758c2152017-01-10 18:26:18 -0800778
779 if (!res.isOk())
780 return res;
781
782 if (!isStreamInfoValid) {
783 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700784 }
785
Shuzhen Wang758c2152017-01-10 18:26:18 -0800786 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700787 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800788 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700789
Zhijun He125684a2015-12-26 15:07:30 -0800790 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100791 std::vector<int> surfaceIds;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800792 bool isDepthCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(surfaces[0]);
793 bool isHeicCompisiteStream = camera3::HeicCompositeStream::isHeicCompositeStream(surfaces[0]);
794 if (isDepthCompositeStream || isHeicCompisiteStream) {
795 sp<CompositeStream> compositeStream;
796 if (isDepthCompositeStream) {
797 compositeStream = new camera3::DepthCompositeStream(mDevice, getRemoteCallback());
798 } else {
799 compositeStream = new camera3::HeicCompositeStream(mDevice, getRemoteCallback());
800 }
801
Emilian Peev538c90e2018-12-17 18:03:19 +0000802 err = compositeStream->createStream(surfaces, deferredConsumer, streamInfo.width,
803 streamInfo.height, streamInfo.format,
804 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
805 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
806 isShared);
807 if (err == OK) {
808 mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
809 compositeStream);
810 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -0800811 } else {
812 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
813 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
814 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
815 &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
816 isShared);
Emilian Peev538c90e2018-12-17 18:03:19 +0000817 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700818
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800819 if (err != OK) {
820 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800821 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800822 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
823 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800824 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700825 int i = 0;
826 for (auto& binder : binders) {
827 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
828 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100829 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
830 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700831 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800832
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800833 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800834 mStreamInfoMap[streamId] = streamInfo;
835
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800836 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700837 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800838 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
839 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700840
Zhijun He5d677d12016-05-29 16:52:39 -0700841 // Set transform flags to ensure preview to be rotated correctly.
842 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700843
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800844 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700845 }
846
847 return res;
848}
849
Zhijun He5d677d12016-05-29 16:52:39 -0700850binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
851 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800852 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700853 /*out*/
854 int* newStreamId) {
855 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100856 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700857 android_dataspace dataSpace;
858 status_t err;
859 binder::Status res;
860
861 if (!mDevice.get()) {
862 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
863 }
864
865 // Infer the surface info for deferred surface stream creation.
866 width = outputConfiguration.getWidth();
867 height = outputConfiguration.getHeight();
868 surfaceType = outputConfiguration.getSurfaceType();
869 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
870 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
871 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
872 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
873 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
874 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
875 }
876 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700877 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100878 std::vector<int> surfaceIds;
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800879 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700880 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
881 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700882 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800883 &streamId, physicalCameraId, &surfaceIds,
884 outputConfiguration.getSurfaceSetID(), isShared,
Emilian Peev40ead602017-09-26 15:46:36 +0100885 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700886
887 if (err != OK) {
888 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800889 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
890 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700891 } else {
892 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
893 // a separate list to track. Once the deferred surface is set, this id will be
894 // relocated to mStreamMap.
895 mDeferredStreams.push_back(streamId);
896
Shuzhen Wang758c2152017-01-10 18:26:18 -0800897 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
898 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
899
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800900 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700901 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800902 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700903
904 // Set transform flags to ensure preview to be rotated correctly.
905 res = setStreamTransformLocked(streamId);
906
907 *newStreamId = streamId;
908 }
909 return res;
910}
911
912binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
913 int32_t transform = 0;
914 status_t err;
915 binder::Status res;
916
917 if (!mDevice.get()) {
918 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
919 }
920
921 err = getRotationTransformLocked(&transform);
922 if (err != OK) {
923 // Error logged by getRotationTransformLocked.
924 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
925 "Unable to calculate rotation transform for new stream");
926 }
927
928 err = mDevice->setStreamTransform(streamId, transform);
929 if (err != OK) {
930 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
931 streamId);
932 ALOGE("%s: %s", __FUNCTION__, msg.string());
933 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
934 }
935
936 return res;
937}
Ruben Brunkbba75572014-11-20 17:29:50 -0800938
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800939binder::Status CameraDeviceClient::createInputStream(
940 int width, int height, int format,
941 /*out*/
942 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700943
944 ATRACE_CALL();
945 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
946
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800947 binder::Status res;
948 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700949
950 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800951
952 if (!mDevice.get()) {
953 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
954 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700955
956 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800957 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -0800958 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800959 ALOGE("%s: %s", __FUNCTION__, msg.string() );
960 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700961 }
962
963 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800964 status_t err = mDevice->createInputStream(width, height, format, &streamId);
965 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700966 mInputStream.configured = true;
967 mInputStream.width = width;
968 mInputStream.height = height;
969 mInputStream.format = format;
970 mInputStream.id = streamId;
971
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800972 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
973 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700974
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800975 *newStreamId = streamId;
976 } else {
977 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800978 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800979 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700980 }
981
982 return res;
983}
984
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800985binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700986
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800987 binder::Status res;
988 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
989
990 if (inputSurface == NULL) {
991 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700992 }
993
994 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800995 if (!mDevice.get()) {
996 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
997 }
998 sp<IGraphicBufferProducer> producer;
999 status_t err = mDevice->getInputBufferProducer(&producer);
1000 if (err != OK) {
1001 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001002 "Camera %s: Error getting input Surface: %s (%d)",
1003 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001004 } else {
1005 inputSurface->name = String16("CameraInput");
1006 inputSurface->graphicBufferProducer = producer;
1007 }
1008 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001009}
1010
Emilian Peev40ead602017-09-26 15:46:36 +01001011binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
1012 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1013 ATRACE_CALL();
1014
1015 binder::Status res;
1016 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1017
1018 Mutex::Autolock icl(mBinderSerializationLock);
1019
1020 if (!mDevice.get()) {
1021 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1022 }
1023
1024 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1025 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001026 String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
1027
Emilian Peev40ead602017-09-26 15:46:36 +01001028 auto producerCount = bufferProducers.size();
1029 if (producerCount == 0) {
1030 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
1031 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1032 "bufferProducers must not be empty");
1033 }
1034
1035 // The first output is the one associated with the output configuration.
1036 // It should always be present, valid and the corresponding stream id should match.
1037 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
1038 ssize_t index = mStreamMap.indexOfKey(binder);
1039 if (index == NAME_NOT_FOUND) {
1040 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
1041 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1042 "OutputConfiguration is invalid");
1043 }
1044 if (mStreamMap.valueFor(binder).streamId() != streamId) {
1045 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
1046 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
1047 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1048 "Stream id is invalid");
1049 }
1050
1051 std::vector<size_t> removedSurfaceIds;
1052 std::vector<sp<IBinder>> removedOutputs;
1053 std::vector<sp<Surface>> newOutputs;
1054 std::vector<OutputStreamInfo> streamInfos;
1055 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
1056 for (auto &it : bufferProducers) {
1057 newOutputsMap.add(IInterface::asBinder(it), it);
1058 }
1059
1060 for (size_t i = 0; i < mStreamMap.size(); i++) {
1061 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
1062 if (idx == NAME_NOT_FOUND) {
1063 if (mStreamMap[i].streamId() == streamId) {
1064 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
1065 removedOutputs.push_back(mStreamMap.keyAt(i));
1066 }
1067 } else {
1068 if (mStreamMap[i].streamId() != streamId) {
1069 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
1070 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1071 "Target Surface is invalid");
1072 }
1073 newOutputsMap.removeItemsAt(idx);
1074 }
1075 }
1076
1077 for (size_t i = 0; i < newOutputsMap.size(); i++) {
1078 OutputStreamInfo outInfo;
1079 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001080 res = SessionConfigurationUtils::createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false,
1081 surface, newOutputsMap.valueAt(i), mCameraIdStr,
1082 mDevice->infoPhysical(physicalCameraId));
Emilian Peev40ead602017-09-26 15:46:36 +01001083 if (!res.isOk())
1084 return res;
1085
Emilian Peev40ead602017-09-26 15:46:36 +01001086 streamInfos.push_back(outInfo);
1087 newOutputs.push_back(surface);
1088 }
1089
1090 //Trivial case no changes required
1091 if (removedSurfaceIds.empty() && newOutputs.empty()) {
1092 return binder::Status::ok();
1093 }
1094
1095 KeyedVector<sp<Surface>, size_t> outputMap;
1096 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
1097 &outputMap);
1098 if (ret != OK) {
1099 switch (ret) {
1100 case NAME_NOT_FOUND:
1101 case BAD_VALUE:
1102 case -EBUSY:
1103 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1104 "Camera %s: Error updating stream: %s (%d)",
1105 mCameraIdStr.string(), strerror(ret), ret);
1106 break;
1107 default:
1108 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1109 "Camera %s: Error updating stream: %s (%d)",
1110 mCameraIdStr.string(), strerror(ret), ret);
1111 break;
1112 }
1113 } else {
1114 for (const auto &it : removedOutputs) {
1115 mStreamMap.removeItem(it);
1116 }
1117
1118 for (size_t i = 0; i < outputMap.size(); i++) {
1119 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
1120 StreamSurfaceId(streamId, outputMap.valueAt(i)));
1121 }
1122
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001123 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
1124
Emilian Peev40ead602017-09-26 15:46:36 +01001125 ALOGV("%s: Camera %s: Successful stream ID %d update",
1126 __FUNCTION__, mCameraIdStr.string(), streamId);
1127 }
1128
1129 return res;
1130}
1131
Igor Murashkine7ee7632013-06-11 18:10:18 -07001132// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001133binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1134 /*out*/
1135 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001136{
1137 ATRACE_CALL();
1138 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1139
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001140 binder::Status res;
1141 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001142
1143 Mutex::Autolock icl(mBinderSerializationLock);
1144
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001145 if (!mDevice.get()) {
1146 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1147 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001148
1149 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001150 status_t err;
1151 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001152 request != NULL) {
1153
1154 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001155 } else if (err == BAD_VALUE) {
1156 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001157 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1158 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001159
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001160 } else {
1161 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001162 "Camera %s: Error creating default request for template %d: %s (%d)",
1163 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001164 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001165 return res;
1166}
1167
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001168binder::Status CameraDeviceClient::getCameraInfo(
1169 /*out*/
1170 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001171{
1172 ATRACE_CALL();
1173 ALOGV("%s", __FUNCTION__);
1174
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001175 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001176
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001177 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001178
1179 Mutex::Autolock icl(mBinderSerializationLock);
1180
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001181 if (!mDevice.get()) {
1182 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1183 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001184
Igor Murashkin099b4572013-07-12 17:52:16 -07001185 if (info != NULL) {
1186 *info = mDevice->info(); // static camera metadata
1187 // TODO: merge with device-specific camera metadata
1188 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001189
1190 return res;
1191}
1192
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001193binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001194{
1195 ATRACE_CALL();
1196 ALOGV("%s", __FUNCTION__);
1197
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001198 binder::Status res;
1199 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001200
1201 Mutex::Autolock icl(mBinderSerializationLock);
1202
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 if (!mDevice.get()) {
1204 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1205 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001206
1207 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001208 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001209 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001210 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001211 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1212 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001213 ALOGE("%s: %s", __FUNCTION__, msg.string());
1214 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001215 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001216 status_t err = mDevice->waitUntilDrained();
1217 if (err != OK) {
1218 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001219 "Camera %s: Error waiting to drain: %s (%d)",
1220 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001221 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001222 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001223 return res;
1224}
1225
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001226binder::Status CameraDeviceClient::flush(
1227 /*out*/
1228 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001229 ATRACE_CALL();
1230 ALOGV("%s", __FUNCTION__);
1231
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001232 binder::Status res;
1233 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001234
1235 Mutex::Autolock icl(mBinderSerializationLock);
1236
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001237 if (!mDevice.get()) {
1238 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1239 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001240
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001241 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001242 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001243 status_t err = mDevice->flush(lastFrameNumber);
1244 if (err != OK) {
1245 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001246 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001247 }
1248 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001249}
1250
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001251binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001252 ATRACE_CALL();
1253 ALOGV("%s", __FUNCTION__);
1254
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001255 binder::Status res;
1256 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001257
1258 Mutex::Autolock icl(mBinderSerializationLock);
1259
1260 // Guard against trying to prepare non-created streams
1261 ssize_t index = NAME_NOT_FOUND;
1262 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001263 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001264 index = i;
1265 break;
1266 }
1267 }
1268
1269 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001270 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1271 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001272 ALOGW("%s: %s", __FUNCTION__, msg.string());
1273 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001274 }
1275
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001276 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1277 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001278 status_t err = mDevice->prepare(streamId);
1279 if (err == BAD_VALUE) {
1280 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001281 "Camera %s: Stream %d has already been used, and cannot be prepared",
1282 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001283 } else if (err != OK) {
1284 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001285 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001286 strerror(-err), err);
1287 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001288 return res;
1289}
1290
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001291binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001292 ATRACE_CALL();
1293 ALOGV("%s", __FUNCTION__);
1294
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001295 binder::Status res;
1296 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001297
1298 Mutex::Autolock icl(mBinderSerializationLock);
1299
1300 // Guard against trying to prepare non-created streams
1301 ssize_t index = NAME_NOT_FOUND;
1302 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001303 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001304 index = i;
1305 break;
1306 }
1307 }
1308
1309 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001310 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1311 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001312 ALOGW("%s: %s", __FUNCTION__, msg.string());
1313 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001314 }
1315
1316 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001317 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1318 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001319 ALOGE("%s: %s", __FUNCTION__, msg.string());
1320 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001321 }
1322
1323 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1324 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325 status_t err = mDevice->prepare(maxCount, streamId);
1326 if (err == BAD_VALUE) {
1327 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001328 "Camera %s: Stream %d has already been used, and cannot be prepared",
1329 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001330 } else if (err != OK) {
1331 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001332 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001333 strerror(-err), err);
1334 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001335
1336 return res;
1337}
1338
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001339binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001340 ATRACE_CALL();
1341 ALOGV("%s", __FUNCTION__);
1342
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001343 binder::Status res;
1344 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001345
1346 Mutex::Autolock icl(mBinderSerializationLock);
1347
1348 // Guard against trying to prepare non-created streams
1349 ssize_t index = NAME_NOT_FOUND;
1350 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001351 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001352 index = i;
1353 break;
1354 }
1355 }
1356
1357 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001358 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1359 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001360 ALOGW("%s: %s", __FUNCTION__, msg.string());
1361 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001362 }
1363
1364 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1365 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001366 status_t err = mDevice->tearDown(streamId);
1367 if (err == BAD_VALUE) {
1368 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001369 "Camera %s: Stream %d is still in use, cannot be torn down",
1370 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001371 } else if (err != OK) {
1372 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001373 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374 strerror(-err), err);
1375 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001376
1377 return res;
1378}
1379
Shuzhen Wang758c2152017-01-10 18:26:18 -08001380binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001381 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1382 ATRACE_CALL();
1383
1384 binder::Status res;
1385 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1386
1387 Mutex::Autolock icl(mBinderSerializationLock);
1388
Shuzhen Wang0129d522016-10-30 22:43:41 -07001389 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1390 outputConfiguration.getGraphicBufferProducers();
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -07001391 String8 physicalId(outputConfiguration.getPhysicalCameraId());
Zhijun He5d677d12016-05-29 16:52:39 -07001392
Shuzhen Wang0129d522016-10-30 22:43:41 -07001393 if (bufferProducers.size() == 0) {
1394 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001395 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1396 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001397
Shuzhen Wang758c2152017-01-10 18:26:18 -08001398 // streamId should be in mStreamMap if this stream already has a surface attached
1399 // to it. Otherwise, it should be in mDeferredStreams.
1400 bool streamIdConfigured = false;
1401 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1402 for (size_t i = 0; i < mStreamMap.size(); i++) {
1403 if (mStreamMap.valueAt(i).streamId() == streamId) {
1404 streamIdConfigured = true;
1405 break;
1406 }
Zhijun He5d677d12016-05-29 16:52:39 -07001407 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001408 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1409 if (streamId == mDeferredStreams[i]) {
1410 deferredStreamIndex = i;
1411 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001412 }
1413
Shuzhen Wang758c2152017-01-10 18:26:18 -08001414 }
1415 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1416 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1417 "(ID %d)", mCameraIdStr.string(), streamId);
1418 ALOGW("%s: %s", __FUNCTION__, msg.string());
1419 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001420 }
1421
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001422 if (mStreamInfoMap[streamId].finalized) {
1423 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1424 " on stream ID %d", mCameraIdStr.string(), streamId);
1425 ALOGW("%s: %s", __FUNCTION__, msg.string());
1426 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1427 }
1428
Zhijun He5d677d12016-05-29 16:52:39 -07001429 if (!mDevice.get()) {
1430 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1431 }
1432
Shuzhen Wang758c2152017-01-10 18:26:18 -08001433 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001434 for (auto& bufferProducer : bufferProducers) {
1435 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001436 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1437 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001438 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001439 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001440 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001441 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001442
1443 sp<Surface> surface;
Colin Crossb8a9dbb2020-08-27 04:12:26 +00001444 res = SessionConfigurationUtils::createSurfaceFromGbp(mStreamInfoMap[streamId],
1445 true /*isStreamInfoValid*/, surface, bufferProducer, mCameraIdStr,
1446 mDevice->infoPhysical(physicalId));
Shuzhen Wang758c2152017-01-10 18:26:18 -08001447
1448 if (!res.isOk())
1449 return res;
1450
1451 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001452 }
1453
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001454 // Gracefully handle case where finalizeOutputConfigurations is called
1455 // without any new surface.
1456 if (consumerSurfaces.size() == 0) {
1457 mStreamInfoMap[streamId].finalized = true;
1458 return res;
1459 }
1460
Zhijun He5d677d12016-05-29 16:52:39 -07001461 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001462 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001463 std::vector<int> consumerSurfaceIds;
1464 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001465 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001466 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1467 sp<IBinder> binder = IInterface::asBinder(
1468 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001469 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001470 binder.get(), streamId, consumerSurfaceIds[i]);
1471 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1472 }
1473 if (deferredStreamIndex != NAME_NOT_FOUND) {
1474 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001475 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001476 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001477 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001478 } else if (err == NO_INIT) {
1479 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001480 "Camera %s: Deferred surface is invalid: %s (%d)",
1481 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001482 } else {
1483 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001484 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1485 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001486 }
1487
1488 return res;
1489}
1490
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001491binder::Status CameraDeviceClient::setCameraAudioRestriction(int32_t mode) {
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001492 ATRACE_CALL();
1493 binder::Status res;
1494 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1495
1496 if (!isValidAudioRestriction(mode)) {
1497 String8 msg = String8::format("Camera %s: invalid audio restriction mode %d",
1498 mCameraIdStr.string(), mode);
1499 ALOGW("%s: %s", __FUNCTION__, msg.string());
1500 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1501 }
1502
1503 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001504 BasicClient::setAudioRestriction(mode);
1505 return binder::Status::ok();
1506}
1507
1508binder::Status CameraDeviceClient::getGlobalAudioRestriction(/*out*/ int32_t* outMode) {
1509 ATRACE_CALL();
1510 binder::Status res;
1511 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1512 Mutex::Autolock icl(mBinderSerializationLock);
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001513 if (outMode != nullptr) {
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -07001514 *outMode = BasicClient::getServiceAudioRestriction();
Yin-Chia Yehdba03232019-08-19 15:54:28 -07001515 }
1516 return binder::Status::ok();
1517}
1518
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001519status_t CameraDeviceClient::setRotateAndCropOverride(uint8_t rotateAndCrop) {
1520 if (rotateAndCrop > ANDROID_SCALER_ROTATE_AND_CROP_AUTO) return BAD_VALUE;
1521
1522 return mDevice->setRotateAndCropAutoBehavior(
1523 static_cast<camera_metadata_enum_android_scaler_rotate_and_crop_t>(rotateAndCrop));
1524}
1525
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001526binder::Status CameraDeviceClient::switchToOffline(
1527 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001528 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001529 /*out*/
1530 sp<hardware::camera2::ICameraOfflineSession>* session) {
1531 ATRACE_CALL();
1532
1533 binder::Status res;
1534 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1535
1536 Mutex::Autolock icl(mBinderSerializationLock);
1537
1538 if (!mDevice.get()) {
1539 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1540 }
1541
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001542 if (offlineOutputIds.empty()) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001543 String8 msg = String8::format("Offline surfaces must not be empty");
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001544 ALOGE("%s: %s", __FUNCTION__, msg.string());
1545 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1546 }
1547
1548 if (session == nullptr) {
1549 String8 msg = String8::format("Invalid offline session");
1550 ALOGE("%s: %s", __FUNCTION__, msg.string());
1551 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1552 }
1553
Yin-Chia Yeh87fccca2020-01-28 09:37:18 -08001554 std::vector<int32_t> offlineStreamIds;
1555 offlineStreamIds.reserve(offlineOutputIds.size());
Emilian Peev4697b642019-11-19 17:11:14 -08001556 KeyedVector<sp<IBinder>, sp<CompositeStream>> offlineCompositeStreamMap;
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001557 for (const auto& streamId : offlineOutputIds) {
1558 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001559 if (index == NAME_NOT_FOUND) {
Emilian Peevcc0b7952020-01-07 13:54:47 -08001560 String8 msg = String8::format("Offline surface with id: %d is not registered",
1561 streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001562 ALOGE("%s: %s", __FUNCTION__, msg.string());
1563 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1564 }
Emilian Peevcc0b7952020-01-07 13:54:47 -08001565
1566 if (!mStreamInfoMap[streamId].supportsOffline) {
1567 String8 msg = String8::format("Offline surface with id: %d doesn't support "
1568 "offline mode", streamId);
1569 ALOGE("%s: %s", __FUNCTION__, msg.string());
1570 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1571 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001572
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001573 bool isCompositeStream = false;
1574 for (const auto& gbp : mConfiguredOutputs[streamId].getGraphicBufferProducers()) {
1575 sp<Surface> s = new Surface(gbp, false /*controlledByApp*/);
1576 isCompositeStream = camera3::DepthCompositeStream::isDepthCompositeStream(s) |
1577 camera3::HeicCompositeStream::isHeicCompositeStream(s);
Emilian Peev4697b642019-11-19 17:11:14 -08001578 if (isCompositeStream) {
1579 auto compositeIdx = mCompositeStreamMap.indexOfKey(IInterface::asBinder(gbp));
1580 if (compositeIdx == NAME_NOT_FOUND) {
1581 ALOGE("%s: Unknown composite stream", __FUNCTION__);
1582 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
1583 "Unknown composite stream");
1584 }
1585
1586 mCompositeStreamMap.valueAt(compositeIdx)->insertCompositeStreamIds(
1587 &offlineStreamIds);
1588 offlineCompositeStreamMap.add(mCompositeStreamMap.keyAt(compositeIdx),
1589 mCompositeStreamMap.valueAt(compositeIdx));
1590 break;
1591 }
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001592 }
1593
Emilian Peev4697b642019-11-19 17:11:14 -08001594 if (!isCompositeStream) {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001595 offlineStreamIds.push_back(streamId);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001596 }
1597 }
1598
1599 sp<CameraOfflineSessionBase> offlineSession;
1600 auto ret = mDevice->switchToOffline(offlineStreamIds, &offlineSession);
1601 if (ret != OK) {
1602 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1603 "Camera %s: Error switching to offline mode: %s (%d)",
1604 mCameraIdStr.string(), strerror(ret), ret);
1605 }
1606
Emilian Peevcc0b7952020-01-07 13:54:47 -08001607 sp<CameraOfflineSessionClient> offlineClient;
1608 if (offlineSession.get() != nullptr) {
1609 offlineClient = new CameraOfflineSessionClient(sCameraService,
1610 offlineSession, offlineCompositeStreamMap, cameraCb, mClientPackageName,
1611 mClientFeatureId, mCameraIdStr, mCameraFacing, mClientPid, mClientUid, mServicePid);
1612 ret = sCameraService->addOfflineClient(mCameraIdStr, offlineClient);
1613 }
1614
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001615 if (ret == OK) {
Emilian Peevd99c8ae2019-11-26 13:19:13 -08001616 // A successful offline session switch must reset the current camera client
1617 // and release any resources occupied by previously configured streams.
1618 mStreamMap.clear();
1619 mConfiguredOutputs.clear();
1620 mDeferredStreams.clear();
1621 mStreamInfoMap.clear();
1622 mCompositeStreamMap.clear();
1623 mInputStream = {false, 0, 0, 0, 0};
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001624 } else {
Emilian Peevb2bc5a42019-11-20 16:02:14 -08001625 switch(ret) {
1626 case BAD_VALUE:
1627 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1628 "Illegal argument to HAL module for camera \"%s\"", mCameraIdStr.c_str());
1629 case TIMED_OUT:
1630 return STATUS_ERROR_FMT(CameraService::ERROR_CAMERA_IN_USE,
1631 "Camera \"%s\" is already open", mCameraIdStr.c_str());
1632 default:
1633 return STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1634 "Failed to initialize camera \"%s\": %s (%d)", mCameraIdStr.c_str(),
1635 strerror(-ret), ret);
1636 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07001637 }
1638
1639 *session = offlineClient;
1640
1641 return binder::Status::ok();
1642}
1643
Igor Murashkine7ee7632013-06-11 18:10:18 -07001644status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001645 return BasicClient::dump(fd, args);
1646}
1647
1648status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001649 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001650 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001651 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001652 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001653 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001654
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001655 dprintf(fd, " State:\n");
1656 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001657 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001658 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001659 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001660 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001661 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001662 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001663 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001664 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001665 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001666 mStreamMap.valueAt(i).streamId(),
1667 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001668 }
Zhijun He5d677d12016-05-29 16:52:39 -07001669 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001670 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001671 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001672 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001673 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001674 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001675 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001676 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001677 // TODO: print dynamic/request section from most recent requests
1678 mFrameProcessor->dump(fd, args);
1679
1680 return dumpDevice(fd, args);
1681}
1682
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001683void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001684 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001685 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001686 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001687
Emilian Peev538c90e2018-12-17 18:03:19 +00001688 // Composites can have multiple internal streams. Error notifications coming from such internal
1689 // streams may need to remain within camera service.
1690 bool skipClientNotification = false;
1691 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1692 skipClientNotification |= mCompositeStreamMap.valueAt(i)->onError(errorCode, resultExtras);
1693 }
1694
1695 if ((remoteCb != 0) && (!skipClientNotification)) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001696 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001697 }
1698}
1699
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001700void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1701 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1702
1703 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001704 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001705 }
1706
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001707 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001708 mStreamingRequestId = REQUEST_ID_NONE;
1709}
1710
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001711void CameraDeviceClient::notifyIdle() {
1712 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001713 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001714
1715 if (remoteCb != 0) {
1716 remoteCb->onDeviceIdle();
1717 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001718 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001719}
1720
Jianing Weicb0652e2014-03-12 18:29:36 -07001721void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001722 nsecs_t timestamp) {
1723 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001724 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001725 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001726 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001727 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001728 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001729
1730 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1731 mCompositeStreamMap.valueAt(i)->onShutter(resultExtras, timestamp);
1732 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001733}
1734
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001735void CameraDeviceClient::notifyPrepared(int streamId) {
1736 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001737 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001738 if (remoteCb != 0) {
1739 remoteCb->onPrepared(streamId);
1740 }
1741}
1742
Shuzhen Wang9d066012016-09-30 11:30:20 -07001743void CameraDeviceClient::notifyRequestQueueEmpty() {
1744 // Thread safe. Don't bother locking.
1745 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1746 if (remoteCb != 0) {
1747 remoteCb->onRequestQueueEmpty();
1748 }
1749}
1750
Igor Murashkine7ee7632013-06-11 18:10:18 -07001751void CameraDeviceClient::detachDevice() {
1752 if (mDevice == 0) return;
1753
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001754 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001755
Emilian Peevfaa4bde2020-01-23 12:19:37 -08001756 mFrameProcessor->removeListener(camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MIN_ID,
1757 camera2::FrameProcessorBase::FRAME_PROCESSOR_LISTENER_MAX_ID,
Igor Murashkine7ee7632013-06-11 18:10:18 -07001758 /*listener*/this);
1759 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001760 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001761 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001762 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001763
1764 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1765 {
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001766 int64_t lastFrameNumber;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001767 status_t code;
Emilian Peev6b51d7d2018-07-23 11:41:44 +01001768 if ((code = mDevice->flush(&lastFrameNumber)) != OK) {
1769 ALOGE("%s: flush failed with code 0x%x", __FUNCTION__, code);
1770 }
1771
Igor Murashkine7ee7632013-06-11 18:10:18 -07001772 if ((code = mDevice->waitUntilDrained()) != OK) {
1773 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1774 code);
1775 }
1776 }
1777
Emilian Peev5e4c7322019-10-22 14:20:52 -07001778 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1779 auto ret = mCompositeStreamMap.valueAt(i)->deleteInternalStreams();
1780 if (ret != OK) {
1781 ALOGE("%s: Failed removing composite stream %s (%d)", __FUNCTION__,
1782 strerror(-ret), ret);
1783 }
1784 }
1785 mCompositeStreamMap.clear();
1786
Igor Murashkine7ee7632013-06-11 18:10:18 -07001787 Camera2ClientBase::detachDevice();
1788}
1789
1790/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001791void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001792 ATRACE_CALL();
1793 ALOGV("%s", __FUNCTION__);
1794
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001795 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001796 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001797 if (remoteCb != NULL) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001798 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
1799 result.mPhysicalMetadatas);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001800 }
Emilian Peev538c90e2018-12-17 18:03:19 +00001801
1802 for (size_t i = 0; i < mCompositeStreamMap.size(); i++) {
1803 mCompositeStreamMap.valueAt(i)->onResultAvailable(result);
1804 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001805}
1806
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001807binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001808 if (mDisconnected) {
1809 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1810 "The camera device has been disconnected");
1811 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001812 status_t res = checkPid(checkLocation);
1813 return (res == OK) ? binder::Status::ok() :
1814 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1815 "Attempt to use camera from a different process than original client");
1816}
1817
Igor Murashkine7ee7632013-06-11 18:10:18 -07001818// TODO: move to Camera2ClientBase
1819bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1820
Jayant Chowdhary12361932018-08-27 14:46:13 -07001821 const int pid = CameraThreadState::getCallingPid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001822 const int selfPid = getpid();
1823 camera_metadata_entry_t entry;
1824
1825 /**
1826 * Mixin default important security values
1827 * - android.led.transmit = defaulted ON
1828 */
1829 CameraMetadata staticInfo = mDevice->info();
1830 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1831 for(size_t i = 0; i < entry.count; ++i) {
1832 uint8_t led = entry.data.u8[i];
1833
1834 switch(led) {
1835 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1836 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1837 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1838 metadata.update(ANDROID_LED_TRANSMIT,
1839 &transmitDefault, 1);
1840 }
1841 break;
1842 }
1843 }
1844 }
1845
1846 // We can do anything!
1847 if (pid == selfPid) {
1848 return true;
1849 }
1850
1851 /**
1852 * Permission check special fields in the request
1853 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1854 */
1855 entry = metadata.find(ANDROID_LED_TRANSMIT);
1856 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1857 String16 permissionString =
1858 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1859 if (!checkCallingPermission(permissionString)) {
Jayant Chowdhary12361932018-08-27 14:46:13 -07001860 const int uid = CameraThreadState::getCallingUid();
Igor Murashkine7ee7632013-06-11 18:10:18 -07001861 ALOGE("Permission Denial: "
1862 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1863 return false;
1864 }
1865 }
1866
1867 return true;
1868}
1869
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001870status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1871 ALOGV("%s: begin", __FUNCTION__);
1872
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001873 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001874 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001875}
1876
Igor Murashkine7ee7632013-06-11 18:10:18 -07001877} // namespace android