blob: 973df19fe649b12daa8e12ff59941d9c334cfed0 [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
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800362binder::Status CameraDeviceClient::endConfigure(int operatingMode) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -0700363 ATRACE_CALL();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700364 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
365 __FUNCTION__, mInputStream.configured ? 1 : 0,
366 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700367
Zhijun He0effd522016-03-04 10:22:27 -0800368 binder::Status res;
369 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
370
371 Mutex::Autolock icl(mBinderSerializationLock);
372
373 if (!mDevice.get()) {
374 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
375 }
376
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800377 if (operatingMode < 0) {
378 String8 msg = String8::format(
379 "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
380 ALOGE("%s: %s", __FUNCTION__, msg.string());
381 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
382 msg.string());
383 }
384
Zhijun He1fa89992015-06-01 15:44:31 -0700385 // Sanitize the high speed session against necessary capability bit.
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800386 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
Zhijun He1fa89992015-06-01 15:44:31 -0700387 if (isConstrainedHighSpeed) {
388 CameraMetadata staticInfo = mDevice->info();
389 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
390 bool isConstrainedHighSpeedSupported = false;
391 for(size_t i = 0; i < entry.count; ++i) {
392 uint8_t capability = entry.data.u8[i];
393 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
394 isConstrainedHighSpeedSupported = true;
395 break;
396 }
397 }
398 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800399 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800400 "Camera %s: Try to create a constrained high speed configuration on a device"
401 " that doesn't support it.", mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800402 ALOGE("%s: %s", __FUNCTION__, msg.string());
403 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
404 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700405 }
406 }
407
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800408 status_t err = mDevice->configureStreams(operatingMode);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700409 if (err == BAD_VALUE) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800410 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
411 mCameraIdStr.string());
Zhijun He5d677d12016-05-29 16:52:39 -0700412 ALOGE("%s: %s", __FUNCTION__, msg.string());
413 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700414 } else if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800415 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
416 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700417 ALOGE("%s: %s", __FUNCTION__, msg.string());
418 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800419 }
420
421 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700422}
423
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800424binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700425 ATRACE_CALL();
426 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
427
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800428 binder::Status res;
429 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700430
431 Mutex::Autolock icl(mBinderSerializationLock);
432
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800433 if (!mDevice.get()) {
434 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
435 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700436
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700437 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700438 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700439 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700440
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700441 if (mInputStream.configured && mInputStream.id == streamId) {
442 isInput = true;
443 } else {
444 // Guard against trying to delete non-created streams
445 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700446 if (streamId == mStreamMap.valueAt(i).streamId()) {
447 surfaces.push_back(mStreamMap.keyAt(i));
448 }
449 }
450
451 // See if this stream is one of the deferred streams.
452 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
453 if (streamId == mDeferredStreams[i]) {
454 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700455 break;
456 }
457 }
458
Shuzhen Wang0129d522016-10-30 22:43:41 -0700459 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
460 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
461 " stream created yet", mCameraIdStr.string(), streamId);
462 ALOGW("%s: %s", __FUNCTION__, msg.string());
463 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700464 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700465 }
466
467 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800468 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700469
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800470 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800471 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
472 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800473 ALOGE("%s: %s", __FUNCTION__, msg.string());
474 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
475 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700476 if (isInput) {
477 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700478 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700479 for (auto& surface : surfaces) {
480 mStreamMap.removeItem(surface);
481 }
482
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800483 mConfiguredOutputs.removeItem(streamId);
484
Shuzhen Wang0129d522016-10-30 22:43:41 -0700485 if (dIndex != NAME_NOT_FOUND) {
486 mDeferredStreams.removeItemsAt(dIndex);
487 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700488 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700489 }
490
491 return res;
492}
493
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800494binder::Status CameraDeviceClient::createStream(
495 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
496 /*out*/
497 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700498 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700499
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800500 binder::Status res;
501 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700502
503 Mutex::Autolock icl(mBinderSerializationLock);
504
Shuzhen Wang0129d522016-10-30 22:43:41 -0700505 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
506 outputConfiguration.getGraphicBufferProducers();
507 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800508 bool deferredConsumer = outputConfiguration.isDeferred();
509 bool isShared = outputConfiguration.isShared();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700510
511 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
512 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
513 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
514 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
515 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800516 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700517 int surfaceType = outputConfiguration.getSurfaceType();
518 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
519 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700520
Zhijun He5d677d12016-05-29 16:52:39 -0700521 if (deferredConsumer && !validSurfaceType) {
522 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700523 __FUNCTION__, bufferProducers[0].get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800524 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700525 }
Zhijun He5d677d12016-05-29 16:52:39 -0700526
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800527 if (!mDevice.get()) {
528 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
529 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700530
Shuzhen Wang0129d522016-10-30 22:43:41 -0700531 std::vector<sp<Surface>> surfaces;
532 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700533 status_t err;
534
535 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700536 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800537 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700538 }
539
Shuzhen Wang758c2152017-01-10 18:26:18 -0800540 OutputStreamInfo streamInfo;
541 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700542 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700543 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700544 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800545 ssize_t index = mStreamMap.indexOfKey(binder);
546 if (index != NAME_NOT_FOUND) {
547 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
548 "(ID %zd)", mCameraIdStr.string(), index);
549 ALOGW("%s: %s", __FUNCTION__, msg.string());
550 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700551 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700552
Shuzhen Wang758c2152017-01-10 18:26:18 -0800553 sp<Surface> surface;
554 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
555
556 if (!res.isOk())
557 return res;
558
559 if (!isStreamInfoValid) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800560 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
561 // formats.
562 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
563 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
564 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
565 ALOGW("%s: %s", __FUNCTION__, msg.string());
566 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
567 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800568 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700569 }
570
Shuzhen Wang758c2152017-01-10 18:26:18 -0800571 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700572 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800573 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700574
Zhijun He125684a2015-12-26 15:07:30 -0800575 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Emilian Peev40ead602017-09-26 15:46:36 +0100576 std::vector<int> surfaceIds;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800577 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
578 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800579 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev40ead602017-09-26 15:46:36 +0100580 &streamId, &surfaceIds, outputConfiguration.getSurfaceSetID(), isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700581
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800582 if (err != OK) {
583 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800584 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800585 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
586 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800587 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700588 int i = 0;
589 for (auto& binder : binders) {
590 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
591 __FUNCTION__, binder.get(), streamId, i);
Emilian Peev40ead602017-09-26 15:46:36 +0100592 mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
593 i++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700594 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800595
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800596 mConfiguredOutputs.add(streamId, outputConfiguration);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800597 mStreamInfoMap[streamId] = streamInfo;
598
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800599 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700600 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800601 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
602 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700603
Zhijun He5d677d12016-05-29 16:52:39 -0700604 // Set transform flags to ensure preview to be rotated correctly.
605 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700606
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800607 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700608 }
609
610 return res;
611}
612
Zhijun He5d677d12016-05-29 16:52:39 -0700613binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
614 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800615 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700616 /*out*/
617 int* newStreamId) {
618 int width, height, format, surfaceType;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100619 uint64_t consumerUsage;
Zhijun He5d677d12016-05-29 16:52:39 -0700620 android_dataspace dataSpace;
621 status_t err;
622 binder::Status res;
623
624 if (!mDevice.get()) {
625 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
626 }
627
628 // Infer the surface info for deferred surface stream creation.
629 width = outputConfiguration.getWidth();
630 height = outputConfiguration.getHeight();
631 surfaceType = outputConfiguration.getSurfaceType();
632 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
633 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
634 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
635 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
636 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
637 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
638 }
639 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700640 std::vector<sp<Surface>> noSurface;
Emilian Peev40ead602017-09-26 15:46:36 +0100641 std::vector<int> surfaceIds;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700642 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
643 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700644 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Emilian Peev40ead602017-09-26 15:46:36 +0100645 &streamId, &surfaceIds, outputConfiguration.getSurfaceSetID(), isShared,
646 consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700647
648 if (err != OK) {
649 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800650 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
651 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700652 } else {
653 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
654 // a separate list to track. Once the deferred surface is set, this id will be
655 // relocated to mStreamMap.
656 mDeferredStreams.push_back(streamId);
657
Shuzhen Wang758c2152017-01-10 18:26:18 -0800658 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
659 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
660
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800661 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700662 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800663 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700664
665 // Set transform flags to ensure preview to be rotated correctly.
666 res = setStreamTransformLocked(streamId);
667
668 *newStreamId = streamId;
669 }
670 return res;
671}
672
673binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
674 int32_t transform = 0;
675 status_t err;
676 binder::Status res;
677
678 if (!mDevice.get()) {
679 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
680 }
681
682 err = getRotationTransformLocked(&transform);
683 if (err != OK) {
684 // Error logged by getRotationTransformLocked.
685 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
686 "Unable to calculate rotation transform for new stream");
687 }
688
689 err = mDevice->setStreamTransform(streamId, transform);
690 if (err != OK) {
691 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
692 streamId);
693 ALOGE("%s: %s", __FUNCTION__, msg.string());
694 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
695 }
696
697 return res;
698}
Ruben Brunkbba75572014-11-20 17:29:50 -0800699
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800700binder::Status CameraDeviceClient::createInputStream(
701 int width, int height, int format,
702 /*out*/
703 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700704
705 ATRACE_CALL();
706 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
707
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800708 binder::Status res;
709 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700710
711 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800712
713 if (!mDevice.get()) {
714 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
715 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700716
717 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800718 String8 msg = String8::format("Camera %s: Already has an input stream "
719 "configured (ID %zd)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800720 ALOGE("%s: %s", __FUNCTION__, msg.string() );
721 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700722 }
723
724 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800725 status_t err = mDevice->createInputStream(width, height, format, &streamId);
726 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700727 mInputStream.configured = true;
728 mInputStream.width = width;
729 mInputStream.height = height;
730 mInputStream.format = format;
731 mInputStream.id = streamId;
732
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800733 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
734 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700735
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800736 *newStreamId = streamId;
737 } else {
738 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800739 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800740 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700741 }
742
743 return res;
744}
745
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800746binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700747
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800748 binder::Status res;
749 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
750
751 if (inputSurface == NULL) {
752 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700753 }
754
755 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800756 if (!mDevice.get()) {
757 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
758 }
759 sp<IGraphicBufferProducer> producer;
760 status_t err = mDevice->getInputBufferProducer(&producer);
761 if (err != OK) {
762 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800763 "Camera %s: Error getting input Surface: %s (%d)",
764 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800765 } else {
766 inputSurface->name = String16("CameraInput");
767 inputSurface->graphicBufferProducer = producer;
768 }
769 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700770}
771
Emilian Peev40ead602017-09-26 15:46:36 +0100772binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
773 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
774 ATRACE_CALL();
775
776 binder::Status res;
777 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
778
779 Mutex::Autolock icl(mBinderSerializationLock);
780
781 if (!mDevice.get()) {
782 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
783 }
784
785 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
786 outputConfiguration.getGraphicBufferProducers();
787 auto producerCount = bufferProducers.size();
788 if (producerCount == 0) {
789 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
790 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
791 "bufferProducers must not be empty");
792 }
793
794 // The first output is the one associated with the output configuration.
795 // It should always be present, valid and the corresponding stream id should match.
796 sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
797 ssize_t index = mStreamMap.indexOfKey(binder);
798 if (index == NAME_NOT_FOUND) {
799 ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
800 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
801 "OutputConfiguration is invalid");
802 }
803 if (mStreamMap.valueFor(binder).streamId() != streamId) {
804 ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
805 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
806 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
807 "Stream id is invalid");
808 }
809
810 std::vector<size_t> removedSurfaceIds;
811 std::vector<sp<IBinder>> removedOutputs;
812 std::vector<sp<Surface>> newOutputs;
813 std::vector<OutputStreamInfo> streamInfos;
814 KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
815 for (auto &it : bufferProducers) {
816 newOutputsMap.add(IInterface::asBinder(it), it);
817 }
818
819 for (size_t i = 0; i < mStreamMap.size(); i++) {
820 ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
821 if (idx == NAME_NOT_FOUND) {
822 if (mStreamMap[i].streamId() == streamId) {
823 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
824 removedOutputs.push_back(mStreamMap.keyAt(i));
825 }
826 } else {
827 if (mStreamMap[i].streamId() != streamId) {
828 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
829 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
830 "Target Surface is invalid");
831 }
832 newOutputsMap.removeItemsAt(idx);
833 }
834 }
835
836 for (size_t i = 0; i < newOutputsMap.size(); i++) {
837 OutputStreamInfo outInfo;
838 sp<Surface> surface;
839 res = createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false, surface,
840 newOutputsMap.valueAt(i));
841 if (!res.isOk())
842 return res;
843
844 // Stream sharing is only supported for IMPLEMENTATION_DEFINED
845 // formats.
846 if (outInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
847 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
848 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
849 ALOGW("%s: %s", __FUNCTION__, msg.string());
850 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
851 }
852 streamInfos.push_back(outInfo);
853 newOutputs.push_back(surface);
854 }
855
856 //Trivial case no changes required
857 if (removedSurfaceIds.empty() && newOutputs.empty()) {
858 return binder::Status::ok();
859 }
860
861 KeyedVector<sp<Surface>, size_t> outputMap;
862 auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
863 &outputMap);
864 if (ret != OK) {
865 switch (ret) {
866 case NAME_NOT_FOUND:
867 case BAD_VALUE:
868 case -EBUSY:
869 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
870 "Camera %s: Error updating stream: %s (%d)",
871 mCameraIdStr.string(), strerror(ret), ret);
872 break;
873 default:
874 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
875 "Camera %s: Error updating stream: %s (%d)",
876 mCameraIdStr.string(), strerror(ret), ret);
877 break;
878 }
879 } else {
880 for (const auto &it : removedOutputs) {
881 mStreamMap.removeItem(it);
882 }
883
884 for (size_t i = 0; i < outputMap.size(); i++) {
885 mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
886 StreamSurfaceId(streamId, outputMap.valueAt(i)));
887 }
888
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800889 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
890
Emilian Peev40ead602017-09-26 15:46:36 +0100891 ALOGV("%s: Camera %s: Successful stream ID %d update",
892 __FUNCTION__, mCameraIdStr.string(), streamId);
893 }
894
895 return res;
896}
897
Eman Copty6d7af0e2016-06-17 20:46:40 -0700898bool CameraDeviceClient::isPublicFormat(int32_t format)
899{
900 switch(format) {
901 case HAL_PIXEL_FORMAT_RGBA_8888:
902 case HAL_PIXEL_FORMAT_RGBX_8888:
903 case HAL_PIXEL_FORMAT_RGB_888:
904 case HAL_PIXEL_FORMAT_RGB_565:
905 case HAL_PIXEL_FORMAT_BGRA_8888:
906 case HAL_PIXEL_FORMAT_YV12:
907 case HAL_PIXEL_FORMAT_Y8:
908 case HAL_PIXEL_FORMAT_Y16:
909 case HAL_PIXEL_FORMAT_RAW16:
910 case HAL_PIXEL_FORMAT_RAW10:
911 case HAL_PIXEL_FORMAT_RAW12:
912 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
913 case HAL_PIXEL_FORMAT_BLOB:
914 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
915 case HAL_PIXEL_FORMAT_YCbCr_420_888:
916 case HAL_PIXEL_FORMAT_YCbCr_422_888:
917 case HAL_PIXEL_FORMAT_YCbCr_444_888:
918 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
919 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
920 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
921 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
922 case HAL_PIXEL_FORMAT_YCbCr_422_I:
923 return true;
924 default:
925 return false;
926 }
927}
928
Shuzhen Wang758c2152017-01-10 18:26:18 -0800929binder::Status CameraDeviceClient::createSurfaceFromGbp(
930 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
931 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
932
933 // bufferProducer must be non-null
934 if (gbp == nullptr) {
935 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
936 ALOGW("%s: %s", __FUNCTION__, msg.string());
937 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
938 }
939 // HACK b/10949105
940 // Query consumer usage bits to set async operation mode for
941 // GLConsumer using controlledByApp parameter.
942 bool useAsync = false;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100943 uint64_t consumerUsage = 0;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800944 status_t err;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100945 if ((err = gbp->getConsumerUsage(&consumerUsage)) != OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800946 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
947 mCameraIdStr.string(), strerror(-err), err);
948 ALOGE("%s: %s", __FUNCTION__, msg.string());
949 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
950 }
951 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
Emilian Peev050f5dc2017-05-18 14:43:56 +0100952 ALOGW("%s: Camera %s with consumer usage flag: %" PRIu64 ": Forcing asynchronous mode for stream",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800953 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
954 useAsync = true;
955 }
956
Emilian Peev050f5dc2017-05-18 14:43:56 +0100957 uint64_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
Shuzhen Wang758c2152017-01-10 18:26:18 -0800958 GRALLOC_USAGE_RENDERSCRIPT;
Emilian Peev050f5dc2017-05-18 14:43:56 +0100959 uint64_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
Shuzhen Wang758c2152017-01-10 18:26:18 -0800960 GraphicBuffer::USAGE_HW_TEXTURE |
961 GraphicBuffer::USAGE_HW_COMPOSER;
962 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
963 (consumerUsage & allowedFlags) != 0;
964
965 surface = new Surface(gbp, useAsync);
966 ANativeWindow *anw = surface.get();
967
968 int width, height, format;
969 android_dataspace dataSpace;
970 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
971 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
972 mCameraIdStr.string(), strerror(-err), err);
973 ALOGE("%s: %s", __FUNCTION__, msg.string());
974 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
975 }
976 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
977 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
978 mCameraIdStr.string(), strerror(-err), err);
979 ALOGE("%s: %s", __FUNCTION__, msg.string());
980 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
981 }
982 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
983 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
984 mCameraIdStr.string(), strerror(-err), err);
985 ALOGE("%s: %s", __FUNCTION__, msg.string());
986 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
987 }
988 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
989 reinterpret_cast<int*>(&dataSpace))) != OK) {
990 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
991 mCameraIdStr.string(), strerror(-err), err);
992 ALOGE("%s: %s", __FUNCTION__, msg.string());
993 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
994 }
995
996 // FIXME: remove this override since the default format should be
997 // IMPLEMENTATION_DEFINED. b/9487482
998 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
999 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
1000 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
1001 __FUNCTION__, mCameraIdStr.string(), format);
1002 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
1003 }
1004 // Round dimensions to the nearest dimensions available for this format
1005 if (flexibleConsumer && isPublicFormat(format) &&
1006 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
1007 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
1008 String8 msg = String8::format("Camera %s: No supported stream configurations with "
1009 "format %#x defined, failed to create output stream",
1010 mCameraIdStr.string(), format);
1011 ALOGE("%s: %s", __FUNCTION__, msg.string());
1012 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1013 }
1014
1015 if (!isStreamInfoValid) {
1016 streamInfo.width = width;
1017 streamInfo.height = height;
1018 streamInfo.format = format;
1019 streamInfo.dataSpace = dataSpace;
1020 streamInfo.consumerUsage = consumerUsage;
1021 return binder::Status::ok();
1022 }
1023 if (width != streamInfo.width) {
1024 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
1025 mCameraIdStr.string(), width, streamInfo.width);
1026 ALOGE("%s: %s", __FUNCTION__, msg.string());
1027 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1028 }
1029 if (height != streamInfo.height) {
1030 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
1031 mCameraIdStr.string(), height, streamInfo.height);
1032 ALOGE("%s: %s", __FUNCTION__, msg.string());
1033 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1034 }
1035 if (format != streamInfo.format) {
1036 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
1037 mCameraIdStr.string(), format, streamInfo.format);
1038 ALOGE("%s: %s", __FUNCTION__, msg.string());
1039 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1040 }
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001041 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1042 if (dataSpace != streamInfo.dataSpace) {
1043 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
1044 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
1045 ALOGE("%s: %s", __FUNCTION__, msg.string());
1046 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1047 }
1048 //At the native side, there isn't a way to check whether 2 surfaces come from the same
1049 //surface class type. Use usage flag to approximate the comparison.
1050 if (consumerUsage != streamInfo.consumerUsage) {
1051 String8 msg = String8::format(
Emilian Peev050f5dc2017-05-18 14:43:56 +01001052 "Camera %s:Surface usage flag doesn't match %" PRIu64 " vs %" PRIu64 "",
Shuzhen Wange8ecda92017-02-20 17:10:28 -08001053 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
1054 ALOGE("%s: %s", __FUNCTION__, msg.string());
1055 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1056 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001057 }
1058 return binder::Status::ok();
1059}
1060
Ruben Brunkbba75572014-11-20 17:29:50 -08001061bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001062 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -08001063 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
1064
1065 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001066 (dataSpace == HAL_DATASPACE_DEPTH) ?
1067 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -08001068 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
1069
1070 int32_t bestWidth = -1;
1071 int32_t bestHeight = -1;
1072
1073 // Iterate through listed stream configurations and find the one with the smallest euclidean
1074 // distance from the given dimensions for the given format.
1075 for (size_t i = 0; i < streamConfigs.count; i += 4) {
1076 int32_t fmt = streamConfigs.data.i32[i];
1077 int32_t w = streamConfigs.data.i32[i + 1];
1078 int32_t h = streamConfigs.data.i32[i + 2];
1079
1080 // Ignore input/output type for now
1081 if (fmt == format) {
1082 if (w == width && h == height) {
1083 bestWidth = width;
1084 bestHeight = height;
1085 break;
1086 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
1087 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
1088 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
1089 bestWidth = w;
1090 bestHeight = h;
1091 }
1092 }
1093 }
1094
1095 if (bestWidth == -1) {
1096 // Return false if no configurations for this format were listed
1097 return false;
1098 }
1099
1100 // Set the outputs to the closet width/height
1101 if (outWidth != NULL) {
1102 *outWidth = bestWidth;
1103 }
1104 if (outHeight != NULL) {
1105 *outHeight = bestHeight;
1106 }
1107
1108 // Return true if at least one configuration for this format was listed
1109 return true;
1110}
1111
1112int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
1113 int64_t d0 = x0 - x1;
1114 int64_t d1 = y0 - y1;
1115 return d0 * d0 + d1 * d1;
1116}
1117
Igor Murashkine7ee7632013-06-11 18:10:18 -07001118// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001119binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
1120 /*out*/
1121 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001122{
1123 ATRACE_CALL();
1124 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
1125
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001126 binder::Status res;
1127 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001128
1129 Mutex::Autolock icl(mBinderSerializationLock);
1130
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001131 if (!mDevice.get()) {
1132 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1133 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001134
1135 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001136 status_t err;
1137 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -07001138 request != NULL) {
1139
1140 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001141 } else if (err == BAD_VALUE) {
1142 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001143 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
1144 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001145
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001146 } else {
1147 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001148 "Camera %s: Error creating default request for template %d: %s (%d)",
1149 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001150 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001151 return res;
1152}
1153
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001154binder::Status CameraDeviceClient::getCameraInfo(
1155 /*out*/
1156 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -07001157{
1158 ATRACE_CALL();
1159 ALOGV("%s", __FUNCTION__);
1160
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001161 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001162
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001163 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -07001164
1165 Mutex::Autolock icl(mBinderSerializationLock);
1166
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001167 if (!mDevice.get()) {
1168 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1169 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001170
Igor Murashkin099b4572013-07-12 17:52:16 -07001171 if (info != NULL) {
1172 *info = mDevice->info(); // static camera metadata
1173 // TODO: merge with device-specific camera metadata
1174 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001175
1176 return res;
1177}
1178
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001179binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001180{
1181 ATRACE_CALL();
1182 ALOGV("%s", __FUNCTION__);
1183
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001184 binder::Status res;
1185 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001186
1187 Mutex::Autolock icl(mBinderSerializationLock);
1188
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001189 if (!mDevice.get()) {
1190 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1191 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001192
1193 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001194 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001195 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001196 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001197 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1198 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001199 ALOGE("%s: %s", __FUNCTION__, msg.string());
1200 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001201 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001202 status_t err = mDevice->waitUntilDrained();
1203 if (err != OK) {
1204 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001205 "Camera %s: Error waiting to drain: %s (%d)",
1206 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001207 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001208 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001209 return res;
1210}
1211
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001212binder::Status CameraDeviceClient::flush(
1213 /*out*/
1214 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001215 ATRACE_CALL();
1216 ALOGV("%s", __FUNCTION__);
1217
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001218 binder::Status res;
1219 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001220
1221 Mutex::Autolock icl(mBinderSerializationLock);
1222
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001223 if (!mDevice.get()) {
1224 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1225 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001226
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001227 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001228 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001229 status_t err = mDevice->flush(lastFrameNumber);
1230 if (err != OK) {
1231 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001232 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001233 }
1234 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001235}
1236
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001237binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001238 ATRACE_CALL();
1239 ALOGV("%s", __FUNCTION__);
1240
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001241 binder::Status res;
1242 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001243
1244 Mutex::Autolock icl(mBinderSerializationLock);
1245
1246 // Guard against trying to prepare non-created streams
1247 ssize_t index = NAME_NOT_FOUND;
1248 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001249 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001250 index = i;
1251 break;
1252 }
1253 }
1254
1255 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001256 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1257 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001258 ALOGW("%s: %s", __FUNCTION__, msg.string());
1259 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001260 }
1261
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001262 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1263 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001264 status_t err = mDevice->prepare(streamId);
1265 if (err == BAD_VALUE) {
1266 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001267 "Camera %s: Stream %d has already been used, and cannot be prepared",
1268 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001269 } else if (err != OK) {
1270 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001271 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001272 strerror(-err), err);
1273 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001274 return res;
1275}
1276
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001277binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001278 ATRACE_CALL();
1279 ALOGV("%s", __FUNCTION__);
1280
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001281 binder::Status res;
1282 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001283
1284 Mutex::Autolock icl(mBinderSerializationLock);
1285
1286 // Guard against trying to prepare non-created streams
1287 ssize_t index = NAME_NOT_FOUND;
1288 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001289 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001290 index = i;
1291 break;
1292 }
1293 }
1294
1295 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001296 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1297 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001298 ALOGW("%s: %s", __FUNCTION__, msg.string());
1299 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001300 }
1301
1302 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001303 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1304 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001305 ALOGE("%s: %s", __FUNCTION__, msg.string());
1306 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001307 }
1308
1309 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1310 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001311 status_t err = mDevice->prepare(maxCount, streamId);
1312 if (err == BAD_VALUE) {
1313 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001314 "Camera %s: Stream %d has already been used, and cannot be prepared",
1315 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001316 } else if (err != OK) {
1317 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001318 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001319 strerror(-err), err);
1320 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001321
1322 return res;
1323}
1324
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001326 ATRACE_CALL();
1327 ALOGV("%s", __FUNCTION__);
1328
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001329 binder::Status res;
1330 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001331
1332 Mutex::Autolock icl(mBinderSerializationLock);
1333
1334 // Guard against trying to prepare non-created streams
1335 ssize_t index = NAME_NOT_FOUND;
1336 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001337 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001338 index = i;
1339 break;
1340 }
1341 }
1342
1343 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001344 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1345 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001346 ALOGW("%s: %s", __FUNCTION__, msg.string());
1347 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001348 }
1349
1350 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1351 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001352 status_t err = mDevice->tearDown(streamId);
1353 if (err == BAD_VALUE) {
1354 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001355 "Camera %s: Stream %d is still in use, cannot be torn down",
1356 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001357 } else if (err != OK) {
1358 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001359 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001360 strerror(-err), err);
1361 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001362
1363 return res;
1364}
1365
Shuzhen Wang758c2152017-01-10 18:26:18 -08001366binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001367 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1368 ATRACE_CALL();
1369
1370 binder::Status res;
1371 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1372
1373 Mutex::Autolock icl(mBinderSerializationLock);
1374
Shuzhen Wang0129d522016-10-30 22:43:41 -07001375 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1376 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001377
Shuzhen Wang0129d522016-10-30 22:43:41 -07001378 if (bufferProducers.size() == 0) {
1379 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001380 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1381 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001382
Shuzhen Wang758c2152017-01-10 18:26:18 -08001383 // streamId should be in mStreamMap if this stream already has a surface attached
1384 // to it. Otherwise, it should be in mDeferredStreams.
1385 bool streamIdConfigured = false;
1386 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1387 for (size_t i = 0; i < mStreamMap.size(); i++) {
1388 if (mStreamMap.valueAt(i).streamId() == streamId) {
1389 streamIdConfigured = true;
1390 break;
1391 }
Zhijun He5d677d12016-05-29 16:52:39 -07001392 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001393 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1394 if (streamId == mDeferredStreams[i]) {
1395 deferredStreamIndex = i;
1396 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001397 }
1398
Shuzhen Wang758c2152017-01-10 18:26:18 -08001399 }
1400 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1401 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1402 "(ID %d)", mCameraIdStr.string(), streamId);
1403 ALOGW("%s: %s", __FUNCTION__, msg.string());
1404 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001405 }
1406
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001407 if (mStreamInfoMap[streamId].finalized) {
1408 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1409 " on stream ID %d", mCameraIdStr.string(), streamId);
1410 ALOGW("%s: %s", __FUNCTION__, msg.string());
1411 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1412 }
1413
Zhijun He5d677d12016-05-29 16:52:39 -07001414 if (!mDevice.get()) {
1415 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1416 }
1417
Shuzhen Wang758c2152017-01-10 18:26:18 -08001418 std::vector<sp<Surface>> consumerSurfaces;
Shuzhen Wang758c2152017-01-10 18:26:18 -08001419 for (auto& bufferProducer : bufferProducers) {
1420 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001421 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1422 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001423 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001424 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001425 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001426 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001427
1428 sp<Surface> surface;
1429 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1430 surface, bufferProducer);
1431
1432 if (!res.isOk())
1433 return res;
1434
1435 consumerSurfaces.push_back(surface);
Zhijun He5d677d12016-05-29 16:52:39 -07001436 }
1437
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001438 // Gracefully handle case where finalizeOutputConfigurations is called
1439 // without any new surface.
1440 if (consumerSurfaces.size() == 0) {
1441 mStreamInfoMap[streamId].finalized = true;
1442 return res;
1443 }
1444
Zhijun He5d677d12016-05-29 16:52:39 -07001445 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001446 status_t err;
Emilian Peev40ead602017-09-26 15:46:36 +01001447 std::vector<int> consumerSurfaceIds;
1448 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
Zhijun He5d677d12016-05-29 16:52:39 -07001449 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001450 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1451 sp<IBinder> binder = IInterface::asBinder(
1452 consumerSurfaces[i]->getIGraphicBufferProducer());
Emilian Peev40ead602017-09-26 15:46:36 +01001453 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001454 binder.get(), streamId, consumerSurfaceIds[i]);
1455 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1456 }
1457 if (deferredStreamIndex != NAME_NOT_FOUND) {
1458 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001459 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001460 mStreamInfoMap[streamId].finalized = true;
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -08001461 mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
Zhijun He5d677d12016-05-29 16:52:39 -07001462 } else if (err == NO_INIT) {
1463 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001464 "Camera %s: Deferred surface is invalid: %s (%d)",
1465 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001466 } else {
1467 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001468 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1469 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001470 }
1471
1472 return res;
1473}
1474
Igor Murashkine7ee7632013-06-11 18:10:18 -07001475status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001476 return BasicClient::dump(fd, args);
1477}
1478
1479status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001480 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001481 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001482 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001483 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001484 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001485
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001486 dprintf(fd, " State:\n");
1487 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001488 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001489 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001490 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001491 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001492 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001493 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001494 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001495 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001496 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001497 mStreamMap.valueAt(i).streamId(),
1498 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001499 }
Zhijun He5d677d12016-05-29 16:52:39 -07001500 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001501 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001502 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001503 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001504 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001505 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001506 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001507 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001508 // TODO: print dynamic/request section from most recent requests
1509 mFrameProcessor->dump(fd, args);
1510
1511 return dumpDevice(fd, args);
1512}
1513
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001514void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001515 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001516 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001517 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001518
1519 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001520 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001521 }
1522}
1523
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001524void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1525 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1526
1527 if (remoteCb != 0) {
Yin-Chia Yeh8ca23dc2017-09-05 18:15:56 -07001528 remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001529 }
1530
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001531 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001532 mStreamingRequestId = REQUEST_ID_NONE;
1533}
1534
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001535void CameraDeviceClient::notifyIdle() {
1536 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001537 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001538
1539 if (remoteCb != 0) {
1540 remoteCb->onDeviceIdle();
1541 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001542 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001543}
1544
Jianing Weicb0652e2014-03-12 18:29:36 -07001545void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001546 nsecs_t timestamp) {
1547 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001548 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001549 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001550 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001551 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001552 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001553}
1554
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001555void CameraDeviceClient::notifyPrepared(int streamId) {
1556 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001557 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001558 if (remoteCb != 0) {
1559 remoteCb->onPrepared(streamId);
1560 }
1561}
1562
Shuzhen Wang9d066012016-09-30 11:30:20 -07001563void CameraDeviceClient::notifyRequestQueueEmpty() {
1564 // Thread safe. Don't bother locking.
1565 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1566 if (remoteCb != 0) {
1567 remoteCb->onRequestQueueEmpty();
1568 }
1569}
1570
Igor Murashkine7ee7632013-06-11 18:10:18 -07001571void CameraDeviceClient::detachDevice() {
1572 if (mDevice == 0) return;
1573
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001574 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001575
1576 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1577 FRAME_PROCESSOR_LISTENER_MAX_ID,
1578 /*listener*/this);
1579 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001580 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001581 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001582 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001583
1584 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1585 {
1586 mDevice->clearStreamingRequest();
1587
1588 status_t code;
1589 if ((code = mDevice->waitUntilDrained()) != OK) {
1590 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1591 code);
1592 }
1593 }
1594
1595 Camera2ClientBase::detachDevice();
1596}
1597
1598/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001599void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001600 ATRACE_CALL();
1601 ALOGV("%s", __FUNCTION__);
1602
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001603 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001604 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001605 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001606 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001607 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001608}
1609
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001610binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001611 if (mDisconnected) {
1612 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1613 "The camera device has been disconnected");
1614 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001615 status_t res = checkPid(checkLocation);
1616 return (res == OK) ? binder::Status::ok() :
1617 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1618 "Attempt to use camera from a different process than original client");
1619}
1620
Igor Murashkine7ee7632013-06-11 18:10:18 -07001621// TODO: move to Camera2ClientBase
1622bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1623
1624 const int pid = IPCThreadState::self()->getCallingPid();
1625 const int selfPid = getpid();
1626 camera_metadata_entry_t entry;
1627
1628 /**
1629 * Mixin default important security values
1630 * - android.led.transmit = defaulted ON
1631 */
1632 CameraMetadata staticInfo = mDevice->info();
1633 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1634 for(size_t i = 0; i < entry.count; ++i) {
1635 uint8_t led = entry.data.u8[i];
1636
1637 switch(led) {
1638 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1639 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1640 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1641 metadata.update(ANDROID_LED_TRANSMIT,
1642 &transmitDefault, 1);
1643 }
1644 break;
1645 }
1646 }
1647 }
1648
1649 // We can do anything!
1650 if (pid == selfPid) {
1651 return true;
1652 }
1653
1654 /**
1655 * Permission check special fields in the request
1656 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1657 */
1658 entry = metadata.find(ANDROID_LED_TRANSMIT);
1659 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1660 String16 permissionString =
1661 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1662 if (!checkCallingPermission(permissionString)) {
1663 const int uid = IPCThreadState::self()->getCallingUid();
1664 ALOGE("Permission Denial: "
1665 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1666 return false;
1667 }
1668 }
1669
1670 return true;
1671}
1672
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001673status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1674 ALOGV("%s: begin", __FUNCTION__);
1675
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001676 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001677 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001678}
1679
Igor Murashkine7ee7632013-06-11 18:10:18 -07001680} // namespace android