blob: 5cbc1588df09f6af85f00af673d004d6a1aa3b5b [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
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800124binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
125 SurfaceMap* outSurfaceMap,
126 Vector<int32_t>* outputStreamIds) {
127 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
128
129 // Trying to submit request with surface that wasn't created
130 if (idx == NAME_NOT_FOUND) {
131 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
132 " we have not called createStream on",
133 __FUNCTION__, mCameraIdStr.string());
134 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
135 "Request targets Surface that is not part of current capture session");
136 }
137
138 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
139 if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
140 (*outSurfaceMap)[streamSurfaceId.streamId()] = std::vector<size_t>();
141 outputStreamIds->push_back(streamSurfaceId.streamId());
142 }
143 (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
144
145 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
146 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
147 streamSurfaceId.surfaceId());
148
149 return binder::Status::ok();
150}
151
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800152binder::Status CameraDeviceClient::submitRequestList(
153 const std::vector<hardware::camera2::CaptureRequest>& requests,
154 bool streaming,
155 /*out*/
156 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700157 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700158 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700159
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800160 binder::Status res = binder::Status::ok();
161 status_t err;
162 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
163 return res;
164 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700165
166 Mutex::Autolock icl(mBinderSerializationLock);
167
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800168 if (!mDevice.get()) {
169 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
170 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700171
172 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800173 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
174 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800175 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700176 }
177
178 List<const CameraMetadata> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700179 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800180 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700181 uint32_t loopCounter = 0;
182
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800183 for (auto&& request: requests) {
184 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700185 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800186 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
187 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800188 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800189 "No input configured for camera %s but request is for reprocessing",
190 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700191 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800192 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
193 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800194 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
195 "Repeating reprocess requests not supported");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700196 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700197 }
198
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800199 CameraMetadata metadata(request.mMetadata);
Jianing Wei90e59c92014-03-12 18:29:36 -0700200 if (metadata.isEmpty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800201 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
202 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800203 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
204 "Request settings are empty");
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800205 } else if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800206 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
207 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
209 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700210 }
211
212 if (!enforceRequestPermissions(metadata)) {
213 // Callee logs
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800214 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
215 "Caller does not have permission to change restricted controls");
Jianing Wei90e59c92014-03-12 18:29:36 -0700216 }
217
218 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700219 * Write in the output stream IDs and map from stream ID to surface ID
220 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700221 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700222 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700223 Vector<int32_t> outputStreamIds;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800224 if (request.mSurfaceList.size() > 0) {
225 for (sp<Surface> surface : request.mSurfaceList) {
226 if (surface == 0) continue;
Jianing Wei90e59c92014-03-12 18:29:36 -0700227
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800228 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
229 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds);
230 if (!res.isOk()) {
231 return res;
232 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700233 }
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800234 } else {
235 for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
236 int streamId = request.mStreamIdxList.itemAt(i);
237 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
Jianing Wei90e59c92014-03-12 18:29:36 -0700238
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800239 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
240 if (index < 0) {
241 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
242 " we have not called createStream on: stream %d",
243 __FUNCTION__, mCameraIdStr.string(), streamId);
244 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
245 "Request targets Surface that is not part of current capture session");
246 }
247
248 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
249 if ((size_t)surfaceIdx >= gbps.size()) {
250 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
251 " we have not called createStream on: stream %d, surfaceIdx %d",
252 __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
253 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
254 "Request targets Surface has invalid surface index");
255 }
256
257 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds);
258 if (!res.isOk()) {
259 return res;
260 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700261 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700262 }
263
264 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
265 outputStreamIds.size());
266
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800267 if (request.mIsReprocess) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700268 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
269 }
270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800271 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700272 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800273 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
274 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
275 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700276
277 metadataRequestList.push_back(metadata);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700278 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700279 }
280 mRequestIdCounter++;
281
282 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700283 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
284 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800285 if (err != OK) {
286 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800287 "Camera %s: Got error %s (%d) after trying to set streaming request",
288 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800289 ALOGE("%s: %s", __FUNCTION__, msg.string());
290 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
291 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700292 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700293 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700294 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700295 }
296 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700297 err = mDevice->captureList(metadataRequestList, surfaceMapList,
298 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800299 if (err != OK) {
300 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800301 "Camera %s: Got error %s (%d) after trying to submit capture request",
302 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800303 ALOGE("%s: %s", __FUNCTION__, msg.string());
304 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
305 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700306 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800307 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700308 }
309
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800310 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700311 return res;
312}
313
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800314binder::Status CameraDeviceClient::cancelRequest(
315 int requestId,
316 /*out*/
317 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700318 ATRACE_CALL();
319 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
320
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800321 status_t err;
322 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700323
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800324 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700325
326 Mutex::Autolock icl(mBinderSerializationLock);
327
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800328 if (!mDevice.get()) {
329 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
330 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700331
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700332 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700333 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800334 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
335 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800336 ALOGE("%s: %s", __FUNCTION__, msg.string());
337 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700338 }
339
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800340 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700341
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800342 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800343 ALOGV("%s: Camera %s: Successfully cleared streaming request",
344 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700345 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800346 } else {
347 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800348 "Camera %s: Error clearing streaming request: %s (%d)",
349 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700350 }
351
352 return res;
353}
354
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800355binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700356 // TODO: Implement this.
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700357 ATRACE_CALL();
Zhijun He1fa89992015-06-01 15:44:31 -0700358 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800359 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700360}
361
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100362binder::Status CameraDeviceClient::endConfigure(int operatingMode,
363 const hardware::camera2::impl::CameraMetadataNative& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700364 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700365 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
366 __FUNCTION__, mInputStream.configured ? 1 : 0,
367 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700368
Zhijun He0effd522016-03-04 10:22:27 -0800369 binder::Status res;
370 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
371
372 Mutex::Autolock icl(mBinderSerializationLock);
373
374 if (!mDevice.get()) {
375 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
376 }
377
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800378 if (operatingMode < 0) {
379 String8 msg = String8::format(
380 "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
381 ALOGE("%s: %s", __FUNCTION__, msg.string());
382 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
383 msg.string());
384 }
385
Zhijun He1fa89992015-06-01 15:44:31 -0700386 // Sanitize the high speed session against necessary capability bit.
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800387 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
Zhijun He1fa89992015-06-01 15:44:31 -0700388 if (isConstrainedHighSpeed) {
389 CameraMetadata staticInfo = mDevice->info();
390 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
391 bool isConstrainedHighSpeedSupported = false;
392 for(size_t i = 0; i < entry.count; ++i) {
393 uint8_t capability = entry.data.u8[i];
394 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
395 isConstrainedHighSpeedSupported = true;
396 break;
397 }
398 }
399 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800400 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800401 "Camera %s: Try to create a constrained high speed configuration on a device"
402 " that doesn't support it.", mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800403 ALOGE("%s: %s", __FUNCTION__, msg.string());
404 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
405 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700406 }
407 }
408
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100409 status_t err = mDevice->configureStreams(sessionParams, operatingMode);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700410 if (err == BAD_VALUE) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800411 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
412 mCameraIdStr.string());
Zhijun He5d677d12016-05-29 16:52:39 -0700413 ALOGE("%s: %s", __FUNCTION__, msg.string());
414 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700415 } else if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800416 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
417 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700418 ALOGE("%s: %s", __FUNCTION__, msg.string());
419 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800420 }
421
422 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700423}
424
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800425binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700426 ATRACE_CALL();
427 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
428
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800429 binder::Status res;
430 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700431
432 Mutex::Autolock icl(mBinderSerializationLock);
433
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800434 if (!mDevice.get()) {
435 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
436 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700437
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700438 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700439 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700440 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700441
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700442 if (mInputStream.configured && mInputStream.id == streamId) {
443 isInput = true;
444 } else {
445 // Guard against trying to delete non-created streams
446 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700447 if (streamId == mStreamMap.valueAt(i).streamId()) {
448 surfaces.push_back(mStreamMap.keyAt(i));
449 }
450 }
451
452 // See if this stream is one of the deferred streams.
453 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
454 if (streamId == mDeferredStreams[i]) {
455 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700456 break;
457 }
458 }
459
Shuzhen Wang0129d522016-10-30 22:43:41 -0700460 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
461 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
462 " stream created yet", mCameraIdStr.string(), streamId);
463 ALOGW("%s: %s", __FUNCTION__, msg.string());
464 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700465 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700466 }
467
468 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800469 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700470
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800471 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800472 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
473 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800474 ALOGE("%s: %s", __FUNCTION__, msg.string());
475 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
476 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700477 if (isInput) {
478 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700479 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700480 for (auto& surface : surfaces) {
481 mStreamMap.removeItem(surface);
482 }
483
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800484 mConfiguredOutputs.removeItem(streamId);
485
Shuzhen Wang0129d522016-10-30 22:43:41 -0700486 if (dIndex != NAME_NOT_FOUND) {
487 mDeferredStreams.removeItemsAt(dIndex);
488 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700489 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700490 }
491
492 return res;
493}
494
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800495binder::Status CameraDeviceClient::createStream(
496 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
497 /*out*/
498 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700499 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700500
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800501 binder::Status res;
502 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700503
504 Mutex::Autolock icl(mBinderSerializationLock);
505
Shuzhen Wang0129d522016-10-30 22:43:41 -0700506 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
507 outputConfiguration.getGraphicBufferProducers();
508 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800509 bool deferredConsumer = outputConfiguration.isDeferred();
510 bool isShared = outputConfiguration.isShared();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700511
512 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
513 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
514 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
515 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
516 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800517 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700518 int surfaceType = outputConfiguration.getSurfaceType();
519 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
520 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700521
Zhijun He5d677d12016-05-29 16:52:39 -0700522 if (deferredConsumer && !validSurfaceType) {
523 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700524 __FUNCTION__, bufferProducers[0].get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800525 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700526 }
Zhijun He5d677d12016-05-29 16:52:39 -0700527
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800528 if (!mDevice.get()) {
529 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
530 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700531
Shuzhen Wang0129d522016-10-30 22:43:41 -0700532 std::vector<sp<Surface>> surfaces;
533 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700534 status_t err;
535
536 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700537 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800538 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700539 }
540
Shuzhen Wang758c2152017-01-10 18:26:18 -0800541 OutputStreamInfo streamInfo;
542 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700543 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700544 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700545 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800546 ssize_t index = mStreamMap.indexOfKey(binder);
547 if (index != NAME_NOT_FOUND) {
548 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
549 "(ID %zd)", mCameraIdStr.string(), index);
550 ALOGW("%s: %s", __FUNCTION__, msg.string());
551 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700552 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700553
Shuzhen Wang758c2152017-01-10 18:26:18 -0800554 sp<Surface> surface;
555 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
556
557 if (!res.isOk())
558 return res;
559
560 if (!isStreamInfoValid) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800561 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
562 // formats.
563 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
564 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
565 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
566 ALOGW("%s: %s", __FUNCTION__, msg.string());
567 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
568 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800569 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700570 }
571
Shuzhen Wang758c2152017-01-10 18:26:18 -0800572 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700573 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800574 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700575
Zhijun He125684a2015-12-26 15:07:30 -0800576 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100577 std::vector<int> surfaceIds;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800578 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
579 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800580 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev40ead602017-09-26 15:46:36 +0100581 &streamId, &surfaceIds, outputConfiguration.getSurfaceSetID(), isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700582
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800583 if (err != OK) {
584 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800585 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800586 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
587 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700589 int i = 0;
590 for (auto& binder : binders) {
591 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
592 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100593 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
594 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700595 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800596
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800597 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800598 mStreamInfoMap[streamId] = streamInfo;
599
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800600 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700601 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800602 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
603 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700604
Zhijun He5d677d12016-05-29 16:52:39 -0700605 // Set transform flags to ensure preview to be rotated correctly.
606 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700607
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800608 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700609 }
610
611 return res;
612}
613
Zhijun He5d677d12016-05-29 16:52:39 -0700614binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
615 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800616 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700617 /*out*/
618 int* newStreamId) {
619 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100620 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700621 android_dataspace dataSpace;
622 status_t err;
623 binder::Status res;
624
625 if (!mDevice.get()) {
626 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
627 }
628
629 // Infer the surface info for deferred surface stream creation.
630 width = outputConfiguration.getWidth();
631 height = outputConfiguration.getHeight();
632 surfaceType = outputConfiguration.getSurfaceType();
633 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
634 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
635 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
636 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
637 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
638 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
639 }
640 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700641 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100642 std::vector<int> surfaceIds;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700643 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
644 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700645 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev40ead602017-09-26 15:46:36 +0100646 &streamId, &surfaceIds, outputConfiguration.getSurfaceSetID(), isShared,
647 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700648
649 if (err != OK) {
650 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800651 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
652 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700653 } else {
654 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
655 // a separate list to track. Once the deferred surface is set, this id will be
656 // relocated to mStreamMap.
657 mDeferredStreams.push_back(streamId);
658
Shuzhen Wang758c2152017-01-10 18:26:18 -0800659 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
660 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
661
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800662 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700663 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800664 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700665
666 // Set transform flags to ensure preview to be rotated correctly.
667 res = setStreamTransformLocked(streamId);
668
669 *newStreamId = streamId;
670 }
671 return res;
672}
673
674binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
675 int32_t transform = 0;
676 status_t err;
677 binder::Status res;
678
679 if (!mDevice.get()) {
680 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
681 }
682
683 err = getRotationTransformLocked(&transform);
684 if (err != OK) {
685 // Error logged by getRotationTransformLocked.
686 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
687 "Unable to calculate rotation transform for new stream");
688 }
689
690 err = mDevice->setStreamTransform(streamId, transform);
691 if (err != OK) {
692 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
693 streamId);
694 ALOGE("%s: %s", __FUNCTION__, msg.string());
695 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
696 }
697
698 return res;
699}
Ruben Brunkbba75572014-11-20 17:29:50 -0800700
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800701binder::Status CameraDeviceClient::createInputStream(
702 int width, int height, int format,
703 /*out*/
704 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700705
706 ATRACE_CALL();
707 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
708
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800709 binder::Status res;
710 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700711
712 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800713
714 if (!mDevice.get()) {
715 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
716 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700717
718 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800719 String8 msg = String8::format("Camera %s: Already has an input stream "
Yi Kong0f414de2017-12-15 13:48:50 -0800720 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800721 ALOGE("%s: %s", __FUNCTION__, msg.string() );
722 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700723 }
724
725 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800726 status_t err = mDevice->createInputStream(width, height, format, &streamId);
727 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700728 mInputStream.configured = true;
729 mInputStream.width = width;
730 mInputStream.height = height;
731 mInputStream.format = format;
732 mInputStream.id = streamId;
733
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800734 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
735 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700736
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800737 *newStreamId = streamId;
738 } else {
739 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800740 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800741 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700742 }
743
744 return res;
745}
746
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800747binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700748
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800749 binder::Status res;
750 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
751
752 if (inputSurface == NULL) {
753 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700754 }
755
756 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800757 if (!mDevice.get()) {
758 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
759 }
760 sp<IGraphicBufferProducer> producer;
761 status_t err = mDevice->getInputBufferProducer(&producer);
762 if (err != OK) {
763 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800764 "Camera %s: Error getting input Surface: %s (%d)",
765 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800766 } else {
767 inputSurface->name = String16("CameraInput");
768 inputSurface->graphicBufferProducer = producer;
769 }
770 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700771}
772
Emilian Peev40ead602017-09-26 15:46:36 +0100773binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
774 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
775 ATRACE_CALL();
776
777 binder::Status res;
778 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
779
780 Mutex::Autolock icl(mBinderSerializationLock);
781
782 if (!mDevice.get()) {
783 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
784 }
785
786 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
787 outputConfiguration.getGraphicBufferProducers();
788 auto producerCount = bufferProducers.size();
789 if (producerCount == 0) {
790 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
791 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
792 "bufferProducers must not be empty");
793 }
794
795 // The first output is the one associated with the output configuration.
796 // It should always be present, valid and the corresponding stream id should match.
797 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
798 ssize_t index = mStreamMap.indexOfKey(binder);
799 if (index == NAME_NOT_FOUND) {
800 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
801 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
802 "OutputConfiguration is invalid");
803 }
804 if (mStreamMap.valueFor(binder).streamId() != streamId) {
805 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
806 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
807 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
808 "Stream id is invalid");
809 }
810
811 std::vector<size_t> removedSurfaceIds;
812 std::vector<sp<IBinder>> removedOutputs;
813 std::vector<sp<Surface>> newOutputs;
814 std::vector<OutputStreamInfo> streamInfos;
815 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
816 for (auto &it : bufferProducers) {
817 newOutputsMap.add(IInterface::asBinder(it), it);
818 }
819
820 for (size_t i = 0; i < mStreamMap.size(); i++) {
821 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
822 if (idx == NAME_NOT_FOUND) {
823 if (mStreamMap[i].streamId() == streamId) {
824 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
825 removedOutputs.push_back(mStreamMap.keyAt(i));
826 }
827 } else {
828 if (mStreamMap[i].streamId() != streamId) {
829 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
830 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
831 "Target Surface is invalid");
832 }
833 newOutputsMap.removeItemsAt(idx);
834 }
835 }
836
837 for (size_t i = 0; i < newOutputsMap.size(); i++) {
838 OutputStreamInfo outInfo;
839 sp<Surface> surface;
840 res = createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false, surface,
841 newOutputsMap.valueAt(i));
842 if (!res.isOk())
843 return res;
844
845 // Stream sharing is only supported for IMPLEMENTATION_DEFINED
846 // formats.
847 if (outInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
848 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
849 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
850 ALOGW("%s: %s", __FUNCTION__, msg.string());
851 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
852 }
853 streamInfos.push_back(outInfo);
854 newOutputs.push_back(surface);
855 }
856
857 //Trivial case no changes required
858 if (removedSurfaceIds.empty() && newOutputs.empty()) {
859 return binder::Status::ok();
860 }
861
862 KeyedVector<sp<Surface>, size_t> outputMap;
863 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
864 &outputMap);
865 if (ret != OK) {
866 switch (ret) {
867 case NAME_NOT_FOUND:
868 case BAD_VALUE:
869 case -EBUSY:
870 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
871 "Camera %s: Error updating stream: %s (%d)",
872 mCameraIdStr.string(), strerror(ret), ret);
873 break;
874 default:
875 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
876 "Camera %s: Error updating stream: %s (%d)",
877 mCameraIdStr.string(), strerror(ret), ret);
878 break;
879 }
880 } else {
881 for (const auto &it : removedOutputs) {
882 mStreamMap.removeItem(it);
883 }
884
885 for (size_t i = 0; i < outputMap.size(); i++) {
886 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
887 StreamSurfaceId(streamId, outputMap.valueAt(i)));
888 }
889
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800890 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
891
Emilian Peev40ead602017-09-26 15:46:36 +0100892 ALOGV("%s: Camera %s: Successful stream ID %d update",
893 __FUNCTION__, mCameraIdStr.string(), streamId);
894 }
895
896 return res;
897}
898
Eman Copty6d7af0e2016-06-17 20:46:40 -0700899bool CameraDeviceClient::isPublicFormat(int32_t format)
900{
901 switch(format) {
902 case HAL_PIXEL_FORMAT_RGBA_8888:
903 case HAL_PIXEL_FORMAT_RGBX_8888:
904 case HAL_PIXEL_FORMAT_RGB_888:
905 case HAL_PIXEL_FORMAT_RGB_565:
906 case HAL_PIXEL_FORMAT_BGRA_8888:
907 case HAL_PIXEL_FORMAT_YV12:
908 case HAL_PIXEL_FORMAT_Y8:
909 case HAL_PIXEL_FORMAT_Y16:
910 case HAL_PIXEL_FORMAT_RAW16:
911 case HAL_PIXEL_FORMAT_RAW10:
912 case HAL_PIXEL_FORMAT_RAW12:
913 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
914 case HAL_PIXEL_FORMAT_BLOB:
915 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
916 case HAL_PIXEL_FORMAT_YCbCr_420_888:
917 case HAL_PIXEL_FORMAT_YCbCr_422_888:
918 case HAL_PIXEL_FORMAT_YCbCr_444_888:
919 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
920 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
921 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
922 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
923 case HAL_PIXEL_FORMAT_YCbCr_422_I:
924 return true;
925 default:
926 return false;
927 }
928}
929
Shuzhen Wang758c2152017-01-10 18:26:18 -0800930binder::Status CameraDeviceClient::createSurfaceFromGbp(
931 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
932 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
933
934 // bufferProducer must be non-null
935 if (gbp == nullptr) {
936 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
937 ALOGW("%s: %s", __FUNCTION__, msg.string());
938 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
939 }
940 // HACK b/10949105
941 // Query consumer usage bits to set async operation mode for
942 // GLConsumer using controlledByApp parameter.
943 bool useAsync = false;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100944 uint64_t consumerUsage = 0;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800945 status_t err;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100946 if ((err = gbp->getConsumerUsage(&consumerUsage)) != OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800947 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
948 mCameraIdStr.string(), strerror(-err), err);
949 ALOGE("%s: %s", __FUNCTION__, msg.string());
950 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
951 }
952 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100953 ALOGW("%s: Camera %s with consumer usage flag: %" PRIu64 ": Forcing asynchronous mode for stream",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800954 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
955 useAsync = true;
956 }
957
Emilian Peev050f5dc2017-05-18 14:43:56 +0100958 uint64_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
Shuzhen Wang758c2152017-01-10 18:26:18 -0800959 GRALLOC_USAGE_RENDERSCRIPT;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100960 uint64_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
Shuzhen Wang758c2152017-01-10 18:26:18 -0800961 GraphicBuffer::USAGE_HW_TEXTURE |
962 GraphicBuffer::USAGE_HW_COMPOSER;
963 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
964 (consumerUsage & allowedFlags) != 0;
965
966 surface = new Surface(gbp, useAsync);
967 ANativeWindow *anw = surface.get();
968
969 int width, height, format;
970 android_dataspace dataSpace;
971 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
972 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
973 mCameraIdStr.string(), strerror(-err), err);
974 ALOGE("%s: %s", __FUNCTION__, msg.string());
975 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
976 }
977 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
978 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
979 mCameraIdStr.string(), strerror(-err), err);
980 ALOGE("%s: %s", __FUNCTION__, msg.string());
981 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
982 }
983 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
984 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
985 mCameraIdStr.string(), strerror(-err), err);
986 ALOGE("%s: %s", __FUNCTION__, msg.string());
987 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
988 }
989 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
990 reinterpret_cast<int*>(&dataSpace))) != OK) {
991 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
992 mCameraIdStr.string(), strerror(-err), err);
993 ALOGE("%s: %s", __FUNCTION__, msg.string());
994 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
995 }
996
997 // FIXME: remove this override since the default format should be
998 // IMPLEMENTATION_DEFINED. b/9487482
999 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
1000 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
1001 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
1002 __FUNCTION__, mCameraIdStr.string(), format);
1003 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1004 }
1005 // Round dimensions to the nearest dimensions available for this format
1006 if (flexibleConsumer && isPublicFormat(format) &&
1007 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
1008 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
1009 String8 msg = String8::format("Camera %s: No supported stream configurations with "
1010 "format %#x defined, failed to create output stream",
1011 mCameraIdStr.string(), format);
1012 ALOGE("%s: %s", __FUNCTION__, msg.string());
1013 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1014 }
1015
1016 if (!isStreamInfoValid) {
1017 streamInfo.width = width;
1018 streamInfo.height = height;
1019 streamInfo.format = format;
1020 streamInfo.dataSpace = dataSpace;
1021 streamInfo.consumerUsage = consumerUsage;
1022 return binder::Status::ok();
1023 }
1024 if (width != streamInfo.width) {
1025 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
1026 mCameraIdStr.string(), width, streamInfo.width);
1027 ALOGE("%s: %s", __FUNCTION__, msg.string());
1028 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1029 }
1030 if (height != streamInfo.height) {
1031 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
1032 mCameraIdStr.string(), height, streamInfo.height);
1033 ALOGE("%s: %s", __FUNCTION__, msg.string());
1034 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1035 }
1036 if (format != streamInfo.format) {
1037 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
1038 mCameraIdStr.string(), format, streamInfo.format);
1039 ALOGE("%s: %s", __FUNCTION__, msg.string());
1040 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1041 }
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001042 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1043 if (dataSpace != streamInfo.dataSpace) {
1044 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
1045 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
1046 ALOGE("%s: %s", __FUNCTION__, msg.string());
1047 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1048 }
1049 //At the native side, there isn't a way to check whether 2 surfaces come from the same
1050 //surface class type. Use usage flag to approximate the comparison.
1051 if (consumerUsage != streamInfo.consumerUsage) {
1052 String8 msg = String8::format(
Emilian Peev050f5dc2017-05-18 14:43:56 +01001053 "Camera %s:Surface usage flag doesn't match %" PRIu64 " vs %" PRIu64 "",
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001054 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
1055 ALOGE("%s: %s", __FUNCTION__, msg.string());
1056 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1057 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001058 }
1059 return binder::Status::ok();
1060}
1061
Ruben Brunkbba75572014-11-20 17:29:50 -08001062bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001063 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -08001064 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
1065
1066 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001067 (dataSpace == HAL_DATASPACE_DEPTH) ?
1068 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -08001069 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
1070
1071 int32_t bestWidth = -1;
1072 int32_t bestHeight = -1;
1073
1074 // Iterate through listed stream configurations and find the one with the smallest euclidean
1075 // distance from the given dimensions for the given format.
1076 for (size_t i = 0; i < streamConfigs.count; i += 4) {
1077 int32_t fmt = streamConfigs.data.i32[i];
1078 int32_t w = streamConfigs.data.i32[i + 1];
1079 int32_t h = streamConfigs.data.i32[i + 2];
1080
1081 // Ignore input/output type for now
1082 if (fmt == format) {
1083 if (w == width && h == height) {
1084 bestWidth = width;
1085 bestHeight = height;
1086 break;
1087 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
1088 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
1089 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
1090 bestWidth = w;
1091 bestHeight = h;
1092 }
1093 }
1094 }
1095
1096 if (bestWidth == -1) {
1097 // Return false if no configurations for this format were listed
1098 return false;
1099 }
1100
1101 // Set the outputs to the closet width/height
1102 if (outWidth != NULL) {
1103 *outWidth = bestWidth;
1104 }
1105 if (outHeight != NULL) {
1106 *outHeight = bestHeight;
1107 }
1108
1109 // Return true if at least one configuration for this format was listed
1110 return true;
1111}
1112
1113int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
1114 int64_t d0 = x0 - x1;
1115 int64_t d1 = y0 - y1;
1116 return d0 * d0 + d1 * d1;
1117}
1118
Igor Murashkine7ee7632013-06-11 18:10:18 -07001119// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001120binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1121 /*out*/
1122 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001123{
1124 ATRACE_CALL();
1125 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1126
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001127 binder::Status res;
1128 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001129
1130 Mutex::Autolock icl(mBinderSerializationLock);
1131
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001132 if (!mDevice.get()) {
1133 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1134 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001135
1136 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001137 status_t err;
1138 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001139 request != NULL) {
1140
1141 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001142 } else if (err == BAD_VALUE) {
1143 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001144 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1145 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001146
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001147 } else {
1148 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001149 "Camera %s: Error creating default request for template %d: %s (%d)",
1150 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001151 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001152 return res;
1153}
1154
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001155binder::Status CameraDeviceClient::getCameraInfo(
1156 /*out*/
1157 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001158{
1159 ATRACE_CALL();
1160 ALOGV("%s", __FUNCTION__);
1161
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001162 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001163
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001164 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001165
1166 Mutex::Autolock icl(mBinderSerializationLock);
1167
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001168 if (!mDevice.get()) {
1169 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1170 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001171
Igor Murashkin099b4572013-07-12 17:52:16 -07001172 if (info != NULL) {
1173 *info = mDevice->info(); // static camera metadata
1174 // TODO: merge with device-specific camera metadata
1175 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001176
1177 return res;
1178}
1179
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001180binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001181{
1182 ATRACE_CALL();
1183 ALOGV("%s", __FUNCTION__);
1184
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001185 binder::Status res;
1186 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001187
1188 Mutex::Autolock icl(mBinderSerializationLock);
1189
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001190 if (!mDevice.get()) {
1191 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1192 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001193
1194 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001195 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001196 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001197 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001198 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1199 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001200 ALOGE("%s: %s", __FUNCTION__, msg.string());
1201 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001202 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001203 status_t err = mDevice->waitUntilDrained();
1204 if (err != OK) {
1205 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001206 "Camera %s: Error waiting to drain: %s (%d)",
1207 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001208 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001209 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001210 return res;
1211}
1212
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001213binder::Status CameraDeviceClient::flush(
1214 /*out*/
1215 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001216 ATRACE_CALL();
1217 ALOGV("%s", __FUNCTION__);
1218
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001219 binder::Status res;
1220 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001221
1222 Mutex::Autolock icl(mBinderSerializationLock);
1223
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001224 if (!mDevice.get()) {
1225 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1226 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001227
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001228 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001229 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001230 status_t err = mDevice->flush(lastFrameNumber);
1231 if (err != OK) {
1232 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001233 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001234 }
1235 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001236}
1237
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001238binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001239 ATRACE_CALL();
1240 ALOGV("%s", __FUNCTION__);
1241
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001242 binder::Status res;
1243 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001244
1245 Mutex::Autolock icl(mBinderSerializationLock);
1246
1247 // Guard against trying to prepare non-created streams
1248 ssize_t index = NAME_NOT_FOUND;
1249 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001250 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001251 index = i;
1252 break;
1253 }
1254 }
1255
1256 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001257 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1258 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001259 ALOGW("%s: %s", __FUNCTION__, msg.string());
1260 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001261 }
1262
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001263 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1264 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001265 status_t err = mDevice->prepare(streamId);
1266 if (err == BAD_VALUE) {
1267 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001268 "Camera %s: Stream %d has already been used, and cannot be prepared",
1269 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001270 } else if (err != OK) {
1271 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001272 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001273 strerror(-err), err);
1274 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001275 return res;
1276}
1277
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001278binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001279 ATRACE_CALL();
1280 ALOGV("%s", __FUNCTION__);
1281
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001282 binder::Status res;
1283 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001284
1285 Mutex::Autolock icl(mBinderSerializationLock);
1286
1287 // Guard against trying to prepare non-created streams
1288 ssize_t index = NAME_NOT_FOUND;
1289 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001290 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001291 index = i;
1292 break;
1293 }
1294 }
1295
1296 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001297 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1298 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001299 ALOGW("%s: %s", __FUNCTION__, msg.string());
1300 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001301 }
1302
1303 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001304 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1305 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001306 ALOGE("%s: %s", __FUNCTION__, msg.string());
1307 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001308 }
1309
1310 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1311 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001312 status_t err = mDevice->prepare(maxCount, streamId);
1313 if (err == BAD_VALUE) {
1314 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001315 "Camera %s: Stream %d has already been used, and cannot be prepared",
1316 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001317 } else if (err != OK) {
1318 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001319 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001320 strerror(-err), err);
1321 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001322
1323 return res;
1324}
1325
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001326binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001327 ATRACE_CALL();
1328 ALOGV("%s", __FUNCTION__);
1329
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001330 binder::Status res;
1331 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001332
1333 Mutex::Autolock icl(mBinderSerializationLock);
1334
1335 // Guard against trying to prepare non-created streams
1336 ssize_t index = NAME_NOT_FOUND;
1337 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001338 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001339 index = i;
1340 break;
1341 }
1342 }
1343
1344 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001345 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1346 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001347 ALOGW("%s: %s", __FUNCTION__, msg.string());
1348 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001349 }
1350
1351 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1352 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001353 status_t err = mDevice->tearDown(streamId);
1354 if (err == BAD_VALUE) {
1355 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001356 "Camera %s: Stream %d is still in use, cannot be torn down",
1357 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001358 } else if (err != OK) {
1359 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001360 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001361 strerror(-err), err);
1362 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001363
1364 return res;
1365}
1366
Shuzhen Wang758c2152017-01-10 18:26:18 -08001367binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001368 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1369 ATRACE_CALL();
1370
1371 binder::Status res;
1372 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1373
1374 Mutex::Autolock icl(mBinderSerializationLock);
1375
Shuzhen Wang0129d522016-10-30 22:43:41 -07001376 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1377 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001378
Shuzhen Wang0129d522016-10-30 22:43:41 -07001379 if (bufferProducers.size() == 0) {
1380 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001381 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1382 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001383
Shuzhen Wang758c2152017-01-10 18:26:18 -08001384 // streamId should be in mStreamMap if this stream already has a surface attached
1385 // to it. Otherwise, it should be in mDeferredStreams.
1386 bool streamIdConfigured = false;
1387 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1388 for (size_t i = 0; i < mStreamMap.size(); i++) {
1389 if (mStreamMap.valueAt(i).streamId() == streamId) {
1390 streamIdConfigured = true;
1391 break;
1392 }
Zhijun He5d677d12016-05-29 16:52:39 -07001393 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001394 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1395 if (streamId == mDeferredStreams[i]) {
1396 deferredStreamIndex = i;
1397 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001398 }
1399
Shuzhen Wang758c2152017-01-10 18:26:18 -08001400 }
1401 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1402 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1403 "(ID %d)", mCameraIdStr.string(), streamId);
1404 ALOGW("%s: %s", __FUNCTION__, msg.string());
1405 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001406 }
1407
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001408 if (mStreamInfoMap[streamId].finalized) {
1409 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1410 " on stream ID %d", mCameraIdStr.string(), streamId);
1411 ALOGW("%s: %s", __FUNCTION__, msg.string());
1412 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1413 }
1414
Zhijun He5d677d12016-05-29 16:52:39 -07001415 if (!mDevice.get()) {
1416 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1417 }
1418
Shuzhen Wang758c2152017-01-10 18:26:18 -08001419 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001420 for (auto& bufferProducer : bufferProducers) {
1421 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001422 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1423 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001424 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001425 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001426 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001427 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001428
1429 sp<Surface> surface;
1430 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1431 surface, bufferProducer);
1432
1433 if (!res.isOk())
1434 return res;
1435
1436 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001437 }
1438
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001439 // Gracefully handle case where finalizeOutputConfigurations is called
1440 // without any new surface.
1441 if (consumerSurfaces.size() == 0) {
1442 mStreamInfoMap[streamId].finalized = true;
1443 return res;
1444 }
1445
Zhijun He5d677d12016-05-29 16:52:39 -07001446 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001447 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001448 std::vector<int> consumerSurfaceIds;
1449 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001450 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001451 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1452 sp<IBinder> binder = IInterface::asBinder(
1453 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001454 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001455 binder.get(), streamId, consumerSurfaceIds[i]);
1456 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1457 }
1458 if (deferredStreamIndex != NAME_NOT_FOUND) {
1459 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001460 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001461 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001462 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001463 } else if (err == NO_INIT) {
1464 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001465 "Camera %s: Deferred surface is invalid: %s (%d)",
1466 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001467 } else {
1468 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001469 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1470 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001471 }
1472
1473 return res;
1474}
1475
Igor Murashkine7ee7632013-06-11 18:10:18 -07001476status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001477 return BasicClient::dump(fd, args);
1478}
1479
1480status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001481 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001482 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001483 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001484 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001485 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001486
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001487 dprintf(fd, " State:\n");
1488 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001489 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001490 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001491 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001492 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001493 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001494 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001495 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001496 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001497 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001498 mStreamMap.valueAt(i).streamId(),
1499 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001500 }
Zhijun He5d677d12016-05-29 16:52:39 -07001501 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001502 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001503 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001504 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001505 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001506 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001507 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001508 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001509 // TODO: print dynamic/request section from most recent requests
1510 mFrameProcessor->dump(fd, args);
1511
1512 return dumpDevice(fd, args);
1513}
1514
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001515void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001516 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001517 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001518 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001519
1520 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001521 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001522 }
1523}
1524
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001525void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1526 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1527
1528 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001529 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001530 }
1531
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001532 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001533 mStreamingRequestId = REQUEST_ID_NONE;
1534}
1535
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001536void CameraDeviceClient::notifyIdle() {
1537 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001538 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001539
1540 if (remoteCb != 0) {
1541 remoteCb->onDeviceIdle();
1542 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001543 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001544}
1545
Jianing Weicb0652e2014-03-12 18:29:36 -07001546void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001547 nsecs_t timestamp) {
1548 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001549 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001550 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001551 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001552 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001553 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001554}
1555
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001556void CameraDeviceClient::notifyPrepared(int streamId) {
1557 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001558 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001559 if (remoteCb != 0) {
1560 remoteCb->onPrepared(streamId);
1561 }
1562}
1563
Shuzhen Wang9d066012016-09-30 11:30:20 -07001564void CameraDeviceClient::notifyRequestQueueEmpty() {
1565 // Thread safe. Don't bother locking.
1566 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1567 if (remoteCb != 0) {
1568 remoteCb->onRequestQueueEmpty();
1569 }
1570}
1571
Igor Murashkine7ee7632013-06-11 18:10:18 -07001572void CameraDeviceClient::detachDevice() {
1573 if (mDevice == 0) return;
1574
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001575 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001576
1577 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1578 FRAME_PROCESSOR_LISTENER_MAX_ID,
1579 /*listener*/this);
1580 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001581 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001582 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001583 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001584
1585 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1586 {
1587 mDevice->clearStreamingRequest();
1588
1589 status_t code;
1590 if ((code = mDevice->waitUntilDrained()) != OK) {
1591 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1592 code);
1593 }
1594 }
1595
1596 Camera2ClientBase::detachDevice();
1597}
1598
1599/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001600void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001601 ATRACE_CALL();
1602 ALOGV("%s", __FUNCTION__);
1603
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001604 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001605 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001606 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001607 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001608 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001609}
1610
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001611binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001612 if (mDisconnected) {
1613 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1614 "The camera device has been disconnected");
1615 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001616 status_t res = checkPid(checkLocation);
1617 return (res == OK) ? binder::Status::ok() :
1618 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1619 "Attempt to use camera from a different process than original client");
1620}
1621
Igor Murashkine7ee7632013-06-11 18:10:18 -07001622// TODO: move to Camera2ClientBase
1623bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1624
1625 const int pid = IPCThreadState::self()->getCallingPid();
1626 const int selfPid = getpid();
1627 camera_metadata_entry_t entry;
1628
1629 /**
1630 * Mixin default important security values
1631 * - android.led.transmit = defaulted ON
1632 */
1633 CameraMetadata staticInfo = mDevice->info();
1634 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1635 for(size_t i = 0; i < entry.count; ++i) {
1636 uint8_t led = entry.data.u8[i];
1637
1638 switch(led) {
1639 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1640 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1641 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1642 metadata.update(ANDROID_LED_TRANSMIT,
1643 &transmitDefault, 1);
1644 }
1645 break;
1646 }
1647 }
1648 }
1649
1650 // We can do anything!
1651 if (pid == selfPid) {
1652 return true;
1653 }
1654
1655 /**
1656 * Permission check special fields in the request
1657 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1658 */
1659 entry = metadata.find(ANDROID_LED_TRANSMIT);
1660 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1661 String16 permissionString =
1662 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1663 if (!checkCallingPermission(permissionString)) {
1664 const int uid = IPCThreadState::self()->getCallingUid();
1665 ALOGE("Permission Denial: "
1666 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1667 return false;
1668 }
1669 }
1670
1671 return true;
1672}
1673
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001674status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1675 ALOGV("%s: begin", __FUNCTION__);
1676
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001677 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001678 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001679}
1680
Igor Murashkine7ee7632013-06-11 18:10:18 -07001681} // namespace android