blob: ea7dc27e093bfcb454f173ac4b2b9587603c47eb [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
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>
Igor Murashkine7ee7632013-06-11 18:10:18 -070022#include <utils/Log.h>
23#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070024#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070026#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027
28#include "common/CameraDeviceBase.h"
29#include "api2/CameraDeviceClient.h"
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080033#define STATUS_ERROR(errorCode, errorString) \
34 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080035 String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080036
37#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
38 binder::Status::fromServiceSpecificError(errorCode, \
Eino-Ville Talvala02bf0322016-02-18 12:41:10 -080039 String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080040 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070041
42namespace android {
43using namespace camera2;
44
45CameraDeviceClientBase::CameraDeviceClientBase(
46 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070048 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080049 const String8& cameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070050 int cameraFacing,
51 int clientPid,
52 uid_t clientUid,
53 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080054 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080055 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080056 clientPackageName,
57 cameraId,
58 cameraFacing,
59 clientPid,
60 clientUid,
61 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070062 mRemoteCallback(remoteCallback) {
63}
Igor Murashkine7ee7632013-06-11 18:10:18 -070064
65// Interface used by CameraService
66
67CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080068 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
69 const String16& clientPackageName,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080070 const String8& cameraId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080071 int cameraFacing,
72 int clientPid,
73 uid_t clientUid,
74 int servicePid) :
Igor Murashkine7ee7632013-06-11 18:10:18 -070075 Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
76 cameraId, cameraFacing, clientPid, clientUid, servicePid),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070077 mInputStream(),
Chien-Yu Chene8c535e2016-04-14 12:18:26 -070078 mStreamingRequestId(REQUEST_ID_NONE),
Igor Murashkine7ee7632013-06-11 18:10:18 -070079 mRequestIdCounter(0) {
80
81 ATRACE_CALL();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080082 ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -070083}
84
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080085status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager) {
86 return initializeImpl(manager);
87}
88
89template<typename TProviderPtr>
90status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr) {
Igor Murashkine7ee7632013-06-11 18:10:18 -070091 ATRACE_CALL();
92 status_t res;
93
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080094 res = Camera2ClientBase::initialize(providerPtr);
Igor Murashkine7ee7632013-06-11 18:10:18 -070095 if (res != OK) {
96 return res;
97 }
98
99 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700100 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800101 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700102 mFrameProcessor->run(threadName.string());
103
104 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
105 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800106 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700107 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700108
109 return OK;
110}
111
112CameraDeviceClient::~CameraDeviceClient() {
113}
114
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800115binder::Status CameraDeviceClient::submitRequest(
116 const hardware::camera2::CaptureRequest& request,
117 bool streaming,
118 /*out*/
119 hardware::camera2::utils::SubmitInfo *submitInfo) {
120 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
121 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700122}
123
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800124binder::Status CameraDeviceClient::submitRequestList(
125 const std::vector<hardware::camera2::CaptureRequest>& requests,
126 bool streaming,
127 /*out*/
128 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700129 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700130 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700131
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800132 binder::Status res = binder::Status::ok();
133 status_t err;
134 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
135 return res;
136 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700137
138 Mutex::Autolock icl(mBinderSerializationLock);
139
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800140 if (!mDevice.get()) {
141 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
142 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700143
144 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800145 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
146 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800147 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700148 }
149
150 List<const CameraMetadata> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700151 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800152 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700153 uint32_t loopCounter = 0;
154
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800155 for (auto&& request: requests) {
156 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700157 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800158 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
159 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800160 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800161 "No input configured for camera %s but request is for reprocessing",
162 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700163 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800164 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
165 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800166 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
167 "Repeating reprocess requests not supported");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700168 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700169 }
170
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800171 CameraMetadata metadata(request.mMetadata);
Jianing Wei90e59c92014-03-12 18:29:36 -0700172 if (metadata.isEmpty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800173 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
174 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800175 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
176 "Request settings are empty");
177 } else if (request.mSurfaceList.isEmpty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800178 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
179 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800180 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
181 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700182 }
183
184 if (!enforceRequestPermissions(metadata)) {
185 // Callee logs
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800186 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
187 "Caller does not have permission to change restricted controls");
Jianing Wei90e59c92014-03-12 18:29:36 -0700188 }
189
190 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700191 * Write in the output stream IDs and map from stream ID to surface ID
192 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700193 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700194 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700195 Vector<int32_t> outputStreamIds;
Chih-Hung Hsieh48fc6192017-08-04 14:37:31 -0700196 for (const sp<Surface>& surface : request.mSurfaceList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700197 if (surface == 0) continue;
198
199 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Marco Nelissenf8880202014-11-14 07:58:25 -0800200 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
Jianing Wei90e59c92014-03-12 18:29:36 -0700201
202 // Trying to submit request with surface that wasn't created
203 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800204 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800205 " we have not called createStream on",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800206 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800207 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
208 "Request targets Surface that is not part of current capture session");
Jianing Wei90e59c92014-03-12 18:29:36 -0700209 }
210
Shuzhen Wang0129d522016-10-30 22:43:41 -0700211 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
212 if (surfaceMap.find(streamSurfaceId.streamId()) == surfaceMap.end()) {
213 surfaceMap[streamSurfaceId.streamId()] = std::vector<size_t>();
214 outputStreamIds.push_back(streamSurfaceId.streamId());
215 }
216 surfaceMap[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
217
218 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
219 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
220 streamSurfaceId.surfaceId());
Jianing Wei90e59c92014-03-12 18:29:36 -0700221 }
222
223 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
224 outputStreamIds.size());
225
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800226 if (request.mIsReprocess) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700227 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
228 }
229
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800230 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700231 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800232 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
233 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
234 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700235
236 metadataRequestList.push_back(metadata);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700237 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700238 }
239 mRequestIdCounter++;
240
241 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700242 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
243 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800244 if (err != OK) {
245 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800246 "Camera %s: Got error %s (%d) after trying to set streaming request",
247 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800248 ALOGE("%s: %s", __FUNCTION__, msg.string());
249 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
250 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700251 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700252 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700253 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700254 }
255 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700256 err = mDevice->captureList(metadataRequestList, surfaceMapList,
257 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800258 if (err != OK) {
259 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800260 "Camera %s: Got error %s (%d) after trying to submit capture request",
261 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800262 ALOGE("%s: %s", __FUNCTION__, msg.string());
263 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
264 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700265 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800266 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700267 }
268
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800269 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700270 return res;
271}
272
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800273binder::Status CameraDeviceClient::cancelRequest(
274 int requestId,
275 /*out*/
276 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700277 ATRACE_CALL();
278 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
279
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800280 status_t err;
281 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700282
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700284
285 Mutex::Autolock icl(mBinderSerializationLock);
286
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 if (!mDevice.get()) {
288 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
289 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700290
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700291 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700292 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800293 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
294 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800295 ALOGE("%s: %s", __FUNCTION__, msg.string());
296 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700297 }
298
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800299 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700300
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800301 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800302 ALOGV("%s: Camera %s: Successfully cleared streaming request",
303 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700304 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800305 } else {
306 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800307 "Camera %s: Error clearing streaming request: %s (%d)",
308 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700309 }
310
311 return res;
312}
313
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800314binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700315 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700316 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700317 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800318 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700319}
320
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100321binder::Status CameraDeviceClient::endConfigure(int operatingMode,
322 const hardware::camera2::impl::CameraMetadataNative& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700323 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700324 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
325 __FUNCTION__, mInputStream.configured ? 1 : 0,
326 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700327
Zhijun He0effd522016-03-04 10:22:27 -0800328 binder::Status res;
329 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
330
331 Mutex::Autolock icl(mBinderSerializationLock);
332
333 if (!mDevice.get()) {
334 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
335 }
336
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800337 if (operatingMode < 0) {
338 String8 msg = String8::format(
339 "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
340 ALOGE("%s: %s", __FUNCTION__, msg.string());
341 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
342 msg.string());
343 }
344
Zhijun He1fa89992015-06-01 15:44:31 -0700345 // Sanitize the high speed session against necessary capability bit.
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800346 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
Zhijun He1fa89992015-06-01 15:44:31 -0700347 if (isConstrainedHighSpeed) {
348 CameraMetadata staticInfo = mDevice->info();
349 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
350 bool isConstrainedHighSpeedSupported = false;
351 for(size_t i = 0; i < entry.count; ++i) {
352 uint8_t capability = entry.data.u8[i];
353 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
354 isConstrainedHighSpeedSupported = true;
355 break;
356 }
357 }
358 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800359 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800360 "Camera %s: Try to create a constrained high speed configuration on a device"
361 " that doesn't support it.", mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800362 ALOGE("%s: %s", __FUNCTION__, msg.string());
363 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
364 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700365 }
366 }
367
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100368 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700369 if (err == BAD_VALUE) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800370 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
371 mCameraIdStr.string());
Zhijun He5d677d12016-05-29 16:52:39 -0700372 ALOGE("%s: %s", __FUNCTION__, msg.string());
373 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700374 } else if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800375 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
376 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700377 ALOGE("%s: %s", __FUNCTION__, msg.string());
378 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800379 }
380
381 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700382}
383
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800384binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700385 ATRACE_CALL();
386 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
387
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800388 binder::Status res;
389 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700390
391 Mutex::Autolock icl(mBinderSerializationLock);
392
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800393 if (!mDevice.get()) {
394 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
395 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700396
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700397 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700398 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700399 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700400
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700401 if (mInputStream.configured && mInputStream.id == streamId) {
402 isInput = true;
403 } else {
404 // Guard against trying to delete non-created streams
405 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700406 if (streamId == mStreamMap.valueAt(i).streamId()) {
407 surfaces.push_back(mStreamMap.keyAt(i));
408 }
409 }
410
411 // See if this stream is one of the deferred streams.
412 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
413 if (streamId == mDeferredStreams[i]) {
414 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700415 break;
416 }
417 }
418
Shuzhen Wang0129d522016-10-30 22:43:41 -0700419 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
420 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
421 " stream created yet", mCameraIdStr.string(), streamId);
422 ALOGW("%s: %s", __FUNCTION__, msg.string());
423 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700424 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700425 }
426
427 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800428 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700429
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800430 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800431 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
432 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800433 ALOGE("%s: %s", __FUNCTION__, msg.string());
434 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
435 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700436 if (isInput) {
437 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700438 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700439 for (auto& surface : surfaces) {
440 mStreamMap.removeItem(surface);
441 }
442
443 if (dIndex != NAME_NOT_FOUND) {
444 mDeferredStreams.removeItemsAt(dIndex);
445 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700446 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700447 }
448
449 return res;
450}
451
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800452binder::Status CameraDeviceClient::createStream(
453 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
454 /*out*/
455 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700456 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700457
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800458 binder::Status res;
459 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700460
461 Mutex::Autolock icl(mBinderSerializationLock);
462
Shuzhen Wang0129d522016-10-30 22:43:41 -0700463 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
464 outputConfiguration.getGraphicBufferProducers();
465 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800466 bool deferredConsumer = outputConfiguration.isDeferred();
467 bool isShared = outputConfiguration.isShared();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700468
469 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
470 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
471 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
472 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
473 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800474 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700475 int surfaceType = outputConfiguration.getSurfaceType();
476 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
477 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700478
Zhijun He5d677d12016-05-29 16:52:39 -0700479 if (deferredConsumer && !validSurfaceType) {
480 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700481 __FUNCTION__, bufferProducers[0].get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800482 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700483 }
Zhijun He5d677d12016-05-29 16:52:39 -0700484
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800485 if (!mDevice.get()) {
486 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
487 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700488
Shuzhen Wang0129d522016-10-30 22:43:41 -0700489 std::vector<sp<Surface>> surfaces;
490 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700491 status_t err;
492
493 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700494 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800495 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700496 }
497
Shuzhen Wang758c2152017-01-10 18:26:18 -0800498 OutputStreamInfo streamInfo;
499 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700500 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700501 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700502 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800503 ssize_t index = mStreamMap.indexOfKey(binder);
504 if (index != NAME_NOT_FOUND) {
505 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
506 "(ID %zd)", mCameraIdStr.string(), index);
507 ALOGW("%s: %s", __FUNCTION__, msg.string());
508 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700509 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700510
Shuzhen Wang758c2152017-01-10 18:26:18 -0800511 sp<Surface> surface;
512 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
513
514 if (!res.isOk())
515 return res;
516
517 if (!isStreamInfoValid) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800518 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
519 // formats.
520 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
521 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
522 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
523 ALOGW("%s: %s", __FUNCTION__, msg.string());
524 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
525 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800526 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700527 }
528
Shuzhen Wang758c2152017-01-10 18:26:18 -0800529 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700530 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800531 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700532
Zhijun He125684a2015-12-26 15:07:30 -0800533 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100534 std::vector<int> surfaceIds;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800535 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
536 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev40ead602017-09-26 15:46:36 +0100538 &streamId, &surfaceIds, outputConfiguration.getSurfaceSetID(), isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700539
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800540 if (err != OK) {
541 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800542 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800543 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
544 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700546 int i = 0;
547 for (auto& binder : binders) {
548 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
549 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100550 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
551 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700552 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800553
554 mStreamInfoMap[streamId] = streamInfo;
555
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800556 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700557 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800558 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
559 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700560
Zhijun He5d677d12016-05-29 16:52:39 -0700561 // Set transform flags to ensure preview to be rotated correctly.
562 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700563
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800564 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700565 }
566
567 return res;
568}
569
Zhijun He5d677d12016-05-29 16:52:39 -0700570binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
571 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800572 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700573 /*out*/
574 int* newStreamId) {
575 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100576 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700577 android_dataspace dataSpace;
578 status_t err;
579 binder::Status res;
580
581 if (!mDevice.get()) {
582 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
583 }
584
585 // Infer the surface info for deferred surface stream creation.
586 width = outputConfiguration.getWidth();
587 height = outputConfiguration.getHeight();
588 surfaceType = outputConfiguration.getSurfaceType();
589 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
590 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
591 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
592 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
593 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
594 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
595 }
596 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700597 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100598 std::vector<int> surfaceIds;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700599 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
600 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700601 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev40ead602017-09-26 15:46:36 +0100602 &streamId, &surfaceIds, outputConfiguration.getSurfaceSetID(), isShared,
603 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700604
605 if (err != OK) {
606 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800607 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
608 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700609 } else {
610 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
611 // a separate list to track. Once the deferred surface is set, this id will be
612 // relocated to mStreamMap.
613 mDeferredStreams.push_back(streamId);
614
Shuzhen Wang758c2152017-01-10 18:26:18 -0800615 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
616 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
617
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800618 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700619 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800620 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700621
622 // Set transform flags to ensure preview to be rotated correctly.
623 res = setStreamTransformLocked(streamId);
624
625 *newStreamId = streamId;
626 }
627 return res;
628}
629
630binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
631 int32_t transform = 0;
632 status_t err;
633 binder::Status res;
634
635 if (!mDevice.get()) {
636 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
637 }
638
639 err = getRotationTransformLocked(&transform);
640 if (err != OK) {
641 // Error logged by getRotationTransformLocked.
642 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
643 "Unable to calculate rotation transform for new stream");
644 }
645
646 err = mDevice->setStreamTransform(streamId, transform);
647 if (err != OK) {
648 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
649 streamId);
650 ALOGE("%s: %s", __FUNCTION__, msg.string());
651 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
652 }
653
654 return res;
655}
Ruben Brunkbba75572014-11-20 17:29:50 -0800656
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800657binder::Status CameraDeviceClient::createInputStream(
658 int width, int height, int format,
659 /*out*/
660 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700661
662 ATRACE_CALL();
663 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
664
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800665 binder::Status res;
666 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700667
668 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800669
670 if (!mDevice.get()) {
671 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
672 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700673
674 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800675 String8 msg = String8::format("Camera %s: Already has an input stream "
676 "configured (ID %zd)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800677 ALOGE("%s: %s", __FUNCTION__, msg.string() );
678 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700679 }
680
681 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800682 status_t err = mDevice->createInputStream(width, height, format, &streamId);
683 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700684 mInputStream.configured = true;
685 mInputStream.width = width;
686 mInputStream.height = height;
687 mInputStream.format = format;
688 mInputStream.id = streamId;
689
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800690 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
691 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700692
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800693 *newStreamId = streamId;
694 } else {
695 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800696 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800697 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700698 }
699
700 return res;
701}
702
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800703binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700704
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800705 binder::Status res;
706 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
707
708 if (inputSurface == NULL) {
709 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700710 }
711
712 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800713 if (!mDevice.get()) {
714 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
715 }
716 sp<IGraphicBufferProducer> producer;
717 status_t err = mDevice->getInputBufferProducer(&producer);
718 if (err != OK) {
719 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800720 "Camera %s: Error getting input Surface: %s (%d)",
721 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800722 } else {
723 inputSurface->name = String16("CameraInput");
724 inputSurface->graphicBufferProducer = producer;
725 }
726 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700727}
728
Emilian Peev40ead602017-09-26 15:46:36 +0100729binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
730 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
731 ATRACE_CALL();
732
733 binder::Status res;
734 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
735
736 Mutex::Autolock icl(mBinderSerializationLock);
737
738 if (!mDevice.get()) {
739 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
740 }
741
742 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
743 outputConfiguration.getGraphicBufferProducers();
744 auto producerCount = bufferProducers.size();
745 if (producerCount == 0) {
746 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
747 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
748 "bufferProducers must not be empty");
749 }
750
751 // The first output is the one associated with the output configuration.
752 // It should always be present, valid and the corresponding stream id should match.
753 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
754 ssize_t index = mStreamMap.indexOfKey(binder);
755 if (index == NAME_NOT_FOUND) {
756 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
757 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
758 "OutputConfiguration is invalid");
759 }
760 if (mStreamMap.valueFor(binder).streamId() != streamId) {
761 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
762 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
763 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
764 "Stream id is invalid");
765 }
766
767 std::vector<size_t> removedSurfaceIds;
768 std::vector<sp<IBinder>> removedOutputs;
769 std::vector<sp<Surface>> newOutputs;
770 std::vector<OutputStreamInfo> streamInfos;
771 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
772 for (auto &it : bufferProducers) {
773 newOutputsMap.add(IInterface::asBinder(it), it);
774 }
775
776 for (size_t i = 0; i < mStreamMap.size(); i++) {
777 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
778 if (idx == NAME_NOT_FOUND) {
779 if (mStreamMap[i].streamId() == streamId) {
780 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
781 removedOutputs.push_back(mStreamMap.keyAt(i));
782 }
783 } else {
784 if (mStreamMap[i].streamId() != streamId) {
785 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
786 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
787 "Target Surface is invalid");
788 }
789 newOutputsMap.removeItemsAt(idx);
790 }
791 }
792
793 for (size_t i = 0; i < newOutputsMap.size(); i++) {
794 OutputStreamInfo outInfo;
795 sp<Surface> surface;
796 res = createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false, surface,
797 newOutputsMap.valueAt(i));
798 if (!res.isOk())
799 return res;
800
801 // Stream sharing is only supported for IMPLEMENTATION_DEFINED
802 // formats.
803 if (outInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
804 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
805 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
806 ALOGW("%s: %s", __FUNCTION__, msg.string());
807 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
808 }
809 streamInfos.push_back(outInfo);
810 newOutputs.push_back(surface);
811 }
812
813 //Trivial case no changes required
814 if (removedSurfaceIds.empty() && newOutputs.empty()) {
815 return binder::Status::ok();
816 }
817
818 KeyedVector<sp<Surface>, size_t> outputMap;
819 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
820 &outputMap);
821 if (ret != OK) {
822 switch (ret) {
823 case NAME_NOT_FOUND:
824 case BAD_VALUE:
825 case -EBUSY:
826 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
827 "Camera %s: Error updating stream: %s (%d)",
828 mCameraIdStr.string(), strerror(ret), ret);
829 break;
830 default:
831 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
832 "Camera %s: Error updating stream: %s (%d)",
833 mCameraIdStr.string(), strerror(ret), ret);
834 break;
835 }
836 } else {
837 for (const auto &it : removedOutputs) {
838 mStreamMap.removeItem(it);
839 }
840
841 for (size_t i = 0; i < outputMap.size(); i++) {
842 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
843 StreamSurfaceId(streamId, outputMap.valueAt(i)));
844 }
845
846 ALOGV("%s: Camera %s: Successful stream ID %d update",
847 __FUNCTION__, mCameraIdStr.string(), streamId);
848 }
849
850 return res;
851}
852
Eman Copty6d7af0e2016-06-17 20:46:40 -0700853bool CameraDeviceClient::isPublicFormat(int32_t format)
854{
855 switch(format) {
856 case HAL_PIXEL_FORMAT_RGBA_8888:
857 case HAL_PIXEL_FORMAT_RGBX_8888:
858 case HAL_PIXEL_FORMAT_RGB_888:
859 case HAL_PIXEL_FORMAT_RGB_565:
860 case HAL_PIXEL_FORMAT_BGRA_8888:
861 case HAL_PIXEL_FORMAT_YV12:
862 case HAL_PIXEL_FORMAT_Y8:
863 case HAL_PIXEL_FORMAT_Y16:
864 case HAL_PIXEL_FORMAT_RAW16:
865 case HAL_PIXEL_FORMAT_RAW10:
866 case HAL_PIXEL_FORMAT_RAW12:
867 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
868 case HAL_PIXEL_FORMAT_BLOB:
869 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
870 case HAL_PIXEL_FORMAT_YCbCr_420_888:
871 case HAL_PIXEL_FORMAT_YCbCr_422_888:
872 case HAL_PIXEL_FORMAT_YCbCr_444_888:
873 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
874 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
875 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
876 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
877 case HAL_PIXEL_FORMAT_YCbCr_422_I:
878 return true;
879 default:
880 return false;
881 }
882}
883
Shuzhen Wang758c2152017-01-10 18:26:18 -0800884binder::Status CameraDeviceClient::createSurfaceFromGbp(
885 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
886 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
887
888 // bufferProducer must be non-null
889 if (gbp == nullptr) {
890 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
891 ALOGW("%s: %s", __FUNCTION__, msg.string());
892 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
893 }
894 // HACK b/10949105
895 // Query consumer usage bits to set async operation mode for
896 // GLConsumer using controlledByApp parameter.
897 bool useAsync = false;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100898 uint64_t consumerUsage = 0;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800899 status_t err;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100900 if ((err = gbp->getConsumerUsage(&consumerUsage)) != OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800901 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
902 mCameraIdStr.string(), strerror(-err), err);
903 ALOGE("%s: %s", __FUNCTION__, msg.string());
904 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
905 }
906 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100907 ALOGW("%s: Camera %s with consumer usage flag: %" PRIu64 ": Forcing asynchronous mode for stream",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800908 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
909 useAsync = true;
910 }
911
Emilian Peev050f5dc2017-05-18 14:43:56 +0100912 uint64_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
Shuzhen Wang758c2152017-01-10 18:26:18 -0800913 GRALLOC_USAGE_RENDERSCRIPT;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100914 uint64_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
Shuzhen Wang758c2152017-01-10 18:26:18 -0800915 GraphicBuffer::USAGE_HW_TEXTURE |
916 GraphicBuffer::USAGE_HW_COMPOSER;
917 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
918 (consumerUsage & allowedFlags) != 0;
919
920 surface = new Surface(gbp, useAsync);
921 ANativeWindow *anw = surface.get();
922
923 int width, height, format;
924 android_dataspace dataSpace;
925 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
926 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
927 mCameraIdStr.string(), strerror(-err), err);
928 ALOGE("%s: %s", __FUNCTION__, msg.string());
929 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
930 }
931 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
932 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
933 mCameraIdStr.string(), strerror(-err), err);
934 ALOGE("%s: %s", __FUNCTION__, msg.string());
935 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
936 }
937 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
938 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
939 mCameraIdStr.string(), strerror(-err), err);
940 ALOGE("%s: %s", __FUNCTION__, msg.string());
941 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
942 }
943 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
944 reinterpret_cast<int*>(&dataSpace))) != OK) {
945 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
946 mCameraIdStr.string(), strerror(-err), err);
947 ALOGE("%s: %s", __FUNCTION__, msg.string());
948 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
949 }
950
951 // FIXME: remove this override since the default format should be
952 // IMPLEMENTATION_DEFINED. b/9487482
953 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
954 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
955 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
956 __FUNCTION__, mCameraIdStr.string(), format);
957 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
958 }
959 // Round dimensions to the nearest dimensions available for this format
960 if (flexibleConsumer && isPublicFormat(format) &&
961 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
962 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
963 String8 msg = String8::format("Camera %s: No supported stream configurations with "
964 "format %#x defined, failed to create output stream",
965 mCameraIdStr.string(), format);
966 ALOGE("%s: %s", __FUNCTION__, msg.string());
967 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
968 }
969
970 if (!isStreamInfoValid) {
971 streamInfo.width = width;
972 streamInfo.height = height;
973 streamInfo.format = format;
974 streamInfo.dataSpace = dataSpace;
975 streamInfo.consumerUsage = consumerUsage;
976 return binder::Status::ok();
977 }
978 if (width != streamInfo.width) {
979 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
980 mCameraIdStr.string(), width, streamInfo.width);
981 ALOGE("%s: %s", __FUNCTION__, msg.string());
982 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
983 }
984 if (height != streamInfo.height) {
985 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
986 mCameraIdStr.string(), height, streamInfo.height);
987 ALOGE("%s: %s", __FUNCTION__, msg.string());
988 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
989 }
990 if (format != streamInfo.format) {
991 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
992 mCameraIdStr.string(), format, streamInfo.format);
993 ALOGE("%s: %s", __FUNCTION__, msg.string());
994 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
995 }
Shuzhen Wange8ecda92017-02-20 17:10:28 -0800996 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
997 if (dataSpace != streamInfo.dataSpace) {
998 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
999 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
1000 ALOGE("%s: %s", __FUNCTION__, msg.string());
1001 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1002 }
1003 //At the native side, there isn't a way to check whether 2 surfaces come from the same
1004 //surface class type. Use usage flag to approximate the comparison.
1005 if (consumerUsage != streamInfo.consumerUsage) {
1006 String8 msg = String8::format(
Emilian Peev050f5dc2017-05-18 14:43:56 +01001007 "Camera %s:Surface usage flag doesn't match %" PRIu64 " vs %" PRIu64 "",
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001008 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
1009 ALOGE("%s: %s", __FUNCTION__, msg.string());
1010 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1011 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001012 }
1013 return binder::Status::ok();
1014}
1015
Ruben Brunkbba75572014-11-20 17:29:50 -08001016bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001017 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -08001018 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
1019
1020 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001021 (dataSpace == HAL_DATASPACE_DEPTH) ?
1022 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -08001023 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
1024
1025 int32_t bestWidth = -1;
1026 int32_t bestHeight = -1;
1027
1028 // Iterate through listed stream configurations and find the one with the smallest euclidean
1029 // distance from the given dimensions for the given format.
1030 for (size_t i = 0; i < streamConfigs.count; i += 4) {
1031 int32_t fmt = streamConfigs.data.i32[i];
1032 int32_t w = streamConfigs.data.i32[i + 1];
1033 int32_t h = streamConfigs.data.i32[i + 2];
1034
1035 // Ignore input/output type for now
1036 if (fmt == format) {
1037 if (w == width && h == height) {
1038 bestWidth = width;
1039 bestHeight = height;
1040 break;
1041 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
1042 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
1043 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
1044 bestWidth = w;
1045 bestHeight = h;
1046 }
1047 }
1048 }
1049
1050 if (bestWidth == -1) {
1051 // Return false if no configurations for this format were listed
1052 return false;
1053 }
1054
1055 // Set the outputs to the closet width/height
1056 if (outWidth != NULL) {
1057 *outWidth = bestWidth;
1058 }
1059 if (outHeight != NULL) {
1060 *outHeight = bestHeight;
1061 }
1062
1063 // Return true if at least one configuration for this format was listed
1064 return true;
1065}
1066
1067int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
1068 int64_t d0 = x0 - x1;
1069 int64_t d1 = y0 - y1;
1070 return d0 * d0 + d1 * d1;
1071}
1072
Igor Murashkine7ee7632013-06-11 18:10:18 -07001073// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001074binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1075 /*out*/
1076 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001077{
1078 ATRACE_CALL();
1079 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1080
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001081 binder::Status res;
1082 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001083
1084 Mutex::Autolock icl(mBinderSerializationLock);
1085
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001086 if (!mDevice.get()) {
1087 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1088 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001089
1090 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001091 status_t err;
1092 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001093 request != NULL) {
1094
1095 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001096 } else if (err == BAD_VALUE) {
1097 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001098 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1099 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001100
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001101 } else {
1102 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001103 "Camera %s: Error creating default request for template %d: %s (%d)",
1104 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001105 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001106 return res;
1107}
1108
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001109binder::Status CameraDeviceClient::getCameraInfo(
1110 /*out*/
1111 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001112{
1113 ATRACE_CALL();
1114 ALOGV("%s", __FUNCTION__);
1115
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001116 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001117
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001118 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001119
1120 Mutex::Autolock icl(mBinderSerializationLock);
1121
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001122 if (!mDevice.get()) {
1123 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1124 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001125
Igor Murashkin099b4572013-07-12 17:52:16 -07001126 if (info != NULL) {
1127 *info = mDevice->info(); // static camera metadata
1128 // TODO: merge with device-specific camera metadata
1129 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001130
1131 return res;
1132}
1133
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001134binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001135{
1136 ATRACE_CALL();
1137 ALOGV("%s", __FUNCTION__);
1138
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001139 binder::Status res;
1140 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001141
1142 Mutex::Autolock icl(mBinderSerializationLock);
1143
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001144 if (!mDevice.get()) {
1145 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1146 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001147
1148 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001149 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001150 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001151 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001152 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1153 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001154 ALOGE("%s: %s", __FUNCTION__, msg.string());
1155 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001156 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001157 status_t err = mDevice->waitUntilDrained();
1158 if (err != OK) {
1159 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001160 "Camera %s: Error waiting to drain: %s (%d)",
1161 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001162 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001163 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001164 return res;
1165}
1166
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001167binder::Status CameraDeviceClient::flush(
1168 /*out*/
1169 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001170 ATRACE_CALL();
1171 ALOGV("%s", __FUNCTION__);
1172
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001173 binder::Status res;
1174 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001175
1176 Mutex::Autolock icl(mBinderSerializationLock);
1177
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001178 if (!mDevice.get()) {
1179 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1180 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001181
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001182 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001183 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184 status_t err = mDevice->flush(lastFrameNumber);
1185 if (err != OK) {
1186 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001187 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001188 }
1189 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001190}
1191
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001192binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001193 ATRACE_CALL();
1194 ALOGV("%s", __FUNCTION__);
1195
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001196 binder::Status res;
1197 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001198
1199 Mutex::Autolock icl(mBinderSerializationLock);
1200
1201 // Guard against trying to prepare non-created streams
1202 ssize_t index = NAME_NOT_FOUND;
1203 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001204 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001205 index = i;
1206 break;
1207 }
1208 }
1209
1210 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001211 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1212 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001213 ALOGW("%s: %s", __FUNCTION__, msg.string());
1214 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001215 }
1216
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001217 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1218 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001219 status_t err = mDevice->prepare(streamId);
1220 if (err == BAD_VALUE) {
1221 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001222 "Camera %s: Stream %d has already been used, and cannot be prepared",
1223 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001224 } else if (err != OK) {
1225 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001226 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001227 strerror(-err), err);
1228 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001229 return res;
1230}
1231
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001232binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001233 ATRACE_CALL();
1234 ALOGV("%s", __FUNCTION__);
1235
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001236 binder::Status res;
1237 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001238
1239 Mutex::Autolock icl(mBinderSerializationLock);
1240
1241 // Guard against trying to prepare non-created streams
1242 ssize_t index = NAME_NOT_FOUND;
1243 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001244 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001245 index = i;
1246 break;
1247 }
1248 }
1249
1250 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001251 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1252 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001253 ALOGW("%s: %s", __FUNCTION__, msg.string());
1254 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001255 }
1256
1257 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001258 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1259 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001260 ALOGE("%s: %s", __FUNCTION__, msg.string());
1261 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001262 }
1263
1264 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1265 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001266 status_t err = mDevice->prepare(maxCount, streamId);
1267 if (err == BAD_VALUE) {
1268 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001269 "Camera %s: Stream %d has already been used, and cannot be prepared",
1270 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001271 } else if (err != OK) {
1272 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001273 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001274 strerror(-err), err);
1275 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001276
1277 return res;
1278}
1279
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001280binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001281 ATRACE_CALL();
1282 ALOGV("%s", __FUNCTION__);
1283
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001284 binder::Status res;
1285 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001286
1287 Mutex::Autolock icl(mBinderSerializationLock);
1288
1289 // Guard against trying to prepare non-created streams
1290 ssize_t index = NAME_NOT_FOUND;
1291 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001292 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001293 index = i;
1294 break;
1295 }
1296 }
1297
1298 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001299 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1300 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001301 ALOGW("%s: %s", __FUNCTION__, msg.string());
1302 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001303 }
1304
1305 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1306 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001307 status_t err = mDevice->tearDown(streamId);
1308 if (err == BAD_VALUE) {
1309 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001310 "Camera %s: Stream %d is still in use, cannot be torn down",
1311 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001312 } else if (err != OK) {
1313 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001314 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001315 strerror(-err), err);
1316 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001317
1318 return res;
1319}
1320
Shuzhen Wang758c2152017-01-10 18:26:18 -08001321binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001322 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1323 ATRACE_CALL();
1324
1325 binder::Status res;
1326 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1327
1328 Mutex::Autolock icl(mBinderSerializationLock);
1329
Shuzhen Wang0129d522016-10-30 22:43:41 -07001330 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1331 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001332
Shuzhen Wang0129d522016-10-30 22:43:41 -07001333 if (bufferProducers.size() == 0) {
1334 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001335 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1336 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001337
Shuzhen Wang758c2152017-01-10 18:26:18 -08001338 // streamId should be in mStreamMap if this stream already has a surface attached
1339 // to it. Otherwise, it should be in mDeferredStreams.
1340 bool streamIdConfigured = false;
1341 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1342 for (size_t i = 0; i < mStreamMap.size(); i++) {
1343 if (mStreamMap.valueAt(i).streamId() == streamId) {
1344 streamIdConfigured = true;
1345 break;
1346 }
Zhijun He5d677d12016-05-29 16:52:39 -07001347 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001348 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1349 if (streamId == mDeferredStreams[i]) {
1350 deferredStreamIndex = i;
1351 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001352 }
1353
Shuzhen Wang758c2152017-01-10 18:26:18 -08001354 }
1355 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1356 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1357 "(ID %d)", mCameraIdStr.string(), streamId);
1358 ALOGW("%s: %s", __FUNCTION__, msg.string());
1359 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001360 }
1361
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001362 if (mStreamInfoMap[streamId].finalized) {
1363 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1364 " on stream ID %d", mCameraIdStr.string(), streamId);
1365 ALOGW("%s: %s", __FUNCTION__, msg.string());
1366 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1367 }
1368
Zhijun He5d677d12016-05-29 16:52:39 -07001369 if (!mDevice.get()) {
1370 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1371 }
1372
Shuzhen Wang758c2152017-01-10 18:26:18 -08001373 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001374 for (auto& bufferProducer : bufferProducers) {
1375 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001376 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1377 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001378 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001379 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001380 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001381 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001382
1383 sp<Surface> surface;
1384 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1385 surface, bufferProducer);
1386
1387 if (!res.isOk())
1388 return res;
1389
1390 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001391 }
1392
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001393 // Gracefully handle case where finalizeOutputConfigurations is called
1394 // without any new surface.
1395 if (consumerSurfaces.size() == 0) {
1396 mStreamInfoMap[streamId].finalized = true;
1397 return res;
1398 }
1399
Zhijun He5d677d12016-05-29 16:52:39 -07001400 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001401 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001402 std::vector<int> consumerSurfaceIds;
1403 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001404 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001405 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1406 sp<IBinder> binder = IInterface::asBinder(
1407 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001408 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001409 binder.get(), streamId, consumerSurfaceIds[i]);
1410 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1411 }
1412 if (deferredStreamIndex != NAME_NOT_FOUND) {
1413 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001414 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001415 mStreamInfoMap[streamId].finalized = true;
Zhijun He5d677d12016-05-29 16:52:39 -07001416 } else if (err == NO_INIT) {
1417 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001418 "Camera %s: Deferred surface is invalid: %s (%d)",
1419 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001420 } else {
1421 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001422 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1423 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001424 }
1425
1426 return res;
1427}
1428
Igor Murashkine7ee7632013-06-11 18:10:18 -07001429status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001430 return BasicClient::dump(fd, args);
1431}
1432
1433status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001434 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001435 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001436 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001437 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001438 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001439
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001440 dprintf(fd, " State:\n");
1441 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001442 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001443 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001444 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001445 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001446 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001447 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001448 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001449 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001450 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001451 mStreamMap.valueAt(i).streamId(),
1452 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001453 }
Zhijun He5d677d12016-05-29 16:52:39 -07001454 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001455 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001456 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001457 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001458 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001459 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001460 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001461 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001462 // TODO: print dynamic/request section from most recent requests
1463 mFrameProcessor->dump(fd, args);
1464
1465 return dumpDevice(fd, args);
1466}
1467
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001468void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001469 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001470 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001471 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001472
1473 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001474 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001475 }
1476}
1477
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001478void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1479 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1480
1481 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001482 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001483 }
1484
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001485 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001486 mStreamingRequestId = REQUEST_ID_NONE;
1487}
1488
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001489void CameraDeviceClient::notifyIdle() {
1490 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001491 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001492
1493 if (remoteCb != 0) {
1494 remoteCb->onDeviceIdle();
1495 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001496 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001497}
1498
Jianing Weicb0652e2014-03-12 18:29:36 -07001499void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001500 nsecs_t timestamp) {
1501 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001502 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001503 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001504 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001505 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001506 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001507}
1508
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001509void CameraDeviceClient::notifyPrepared(int streamId) {
1510 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001511 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001512 if (remoteCb != 0) {
1513 remoteCb->onPrepared(streamId);
1514 }
1515}
1516
Shuzhen Wang9d066012016-09-30 11:30:20 -07001517void CameraDeviceClient::notifyRequestQueueEmpty() {
1518 // Thread safe. Don't bother locking.
1519 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1520 if (remoteCb != 0) {
1521 remoteCb->onRequestQueueEmpty();
1522 }
1523}
1524
Igor Murashkine7ee7632013-06-11 18:10:18 -07001525void CameraDeviceClient::detachDevice() {
1526 if (mDevice == 0) return;
1527
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001528 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001529
1530 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1531 FRAME_PROCESSOR_LISTENER_MAX_ID,
1532 /*listener*/this);
1533 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001534 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001535 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001536 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001537
1538 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1539 {
1540 mDevice->clearStreamingRequest();
1541
1542 status_t code;
1543 if ((code = mDevice->waitUntilDrained()) != OK) {
1544 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1545 code);
1546 }
1547 }
1548
1549 Camera2ClientBase::detachDevice();
1550}
1551
1552/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001553void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001554 ATRACE_CALL();
1555 ALOGV("%s", __FUNCTION__);
1556
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001557 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001558 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001559 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001560 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001561 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001562}
1563
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001564binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001565 if (mDisconnected) {
1566 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1567 "The camera device has been disconnected");
1568 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001569 status_t res = checkPid(checkLocation);
1570 return (res == OK) ? binder::Status::ok() :
1571 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1572 "Attempt to use camera from a different process than original client");
1573}
1574
Igor Murashkine7ee7632013-06-11 18:10:18 -07001575// TODO: move to Camera2ClientBase
1576bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1577
1578 const int pid = IPCThreadState::self()->getCallingPid();
1579 const int selfPid = getpid();
1580 camera_metadata_entry_t entry;
1581
1582 /**
1583 * Mixin default important security values
1584 * - android.led.transmit = defaulted ON
1585 */
1586 CameraMetadata staticInfo = mDevice->info();
1587 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1588 for(size_t i = 0; i < entry.count; ++i) {
1589 uint8_t led = entry.data.u8[i];
1590
1591 switch(led) {
1592 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1593 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1594 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1595 metadata.update(ANDROID_LED_TRANSMIT,
1596 &transmitDefault, 1);
1597 }
1598 break;
1599 }
1600 }
1601 }
1602
1603 // We can do anything!
1604 if (pid == selfPid) {
1605 return true;
1606 }
1607
1608 /**
1609 * Permission check special fields in the request
1610 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1611 */
1612 entry = metadata.find(ANDROID_LED_TRANSMIT);
1613 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1614 String16 permissionString =
1615 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1616 if (!checkCallingPermission(permissionString)) {
1617 const int uid = IPCThreadState::self()->getCallingUid();
1618 ALOGE("Permission Denial: "
1619 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1620 return false;
1621 }
1622 }
1623
1624 return true;
1625}
1626
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001627status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1628 ALOGV("%s: begin", __FUNCTION__);
1629
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001630 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001631 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001632}
1633
Igor Murashkine7ee7632013-06-11 18:10:18 -07001634} // namespace android