blob: 79e7ff0bf35243d209faacff1335e86c0d3f3c65 [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(CameraModule *module) {
86 return initializeImpl(module);
87}
88
89status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager) {
90 return initializeImpl(manager);
91}
92
93template<typename TProviderPtr>
94status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr) {
Igor Murashkine7ee7632013-06-11 18:10:18 -070095 ATRACE_CALL();
96 status_t res;
97
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080098 res = Camera2ClientBase::initialize(providerPtr);
Igor Murashkine7ee7632013-06-11 18:10:18 -070099 if (res != OK) {
100 return res;
101 }
102
103 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700104 mFrameProcessor = new FrameProcessorBase(mDevice);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800105 threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700106 mFrameProcessor->run(threadName.string());
107
108 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
109 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800110 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700111 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700112
113 return OK;
114}
115
116CameraDeviceClient::~CameraDeviceClient() {
117}
118
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800119binder::Status CameraDeviceClient::submitRequest(
120 const hardware::camera2::CaptureRequest& request,
121 bool streaming,
122 /*out*/
123 hardware::camera2::utils::SubmitInfo *submitInfo) {
124 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
125 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700126}
127
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800128binder::Status CameraDeviceClient::submitRequestList(
129 const std::vector<hardware::camera2::CaptureRequest>& requests,
130 bool streaming,
131 /*out*/
132 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700133 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700134 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700135
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800136 binder::Status res = binder::Status::ok();
137 status_t err;
138 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
139 return res;
140 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700141
142 Mutex::Autolock icl(mBinderSerializationLock);
143
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800144 if (!mDevice.get()) {
145 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
146 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700147
148 if (requests.empty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800149 ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
150 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800151 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700152 }
153
154 List<const CameraMetadata> metadataRequestList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700155 std::list<const SurfaceMap> surfaceMapList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800156 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700157 uint32_t loopCounter = 0;
158
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800159 for (auto&& request: requests) {
160 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700161 if (!mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800162 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
163 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800164 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800165 "No input configured for camera %s but request is for reprocessing",
166 mCameraIdStr.string());
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700167 } else if (streaming) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800168 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
169 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800170 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
171 "Repeating reprocess requests not supported");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700172 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700173 }
174
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800175 CameraMetadata metadata(request.mMetadata);
Jianing Wei90e59c92014-03-12 18:29:36 -0700176 if (metadata.isEmpty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800177 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
178 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800179 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
180 "Request settings are empty");
181 } else if (request.mSurfaceList.isEmpty()) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800182 ALOGE("%s: Camera %s: Requests must have at least one surface target. "
183 "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800184 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
185 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700186 }
187
188 if (!enforceRequestPermissions(metadata)) {
189 // Callee logs
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800190 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
191 "Caller does not have permission to change restricted controls");
Jianing Wei90e59c92014-03-12 18:29:36 -0700192 }
193
194 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700195 * Write in the output stream IDs and map from stream ID to surface ID
196 * which we calculate from the capture request's list of surface target
Jianing Wei90e59c92014-03-12 18:29:36 -0700197 */
Shuzhen Wang0129d522016-10-30 22:43:41 -0700198 SurfaceMap surfaceMap;
Jianing Wei90e59c92014-03-12 18:29:36 -0700199 Vector<int32_t> outputStreamIds;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800200 for (sp<Surface> surface : request.mSurfaceList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700201 if (surface == 0) continue;
202
203 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Marco Nelissenf8880202014-11-14 07:58:25 -0800204 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
Jianing Wei90e59c92014-03-12 18:29:36 -0700205
206 // Trying to submit request with surface that wasn't created
207 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800208 ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800209 " we have not called createStream on",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800210 __FUNCTION__, mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800211 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
212 "Request targets Surface that is not part of current capture session");
Jianing Wei90e59c92014-03-12 18:29:36 -0700213 }
214
Shuzhen Wang0129d522016-10-30 22:43:41 -0700215 const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
216 if (surfaceMap.find(streamSurfaceId.streamId()) == surfaceMap.end()) {
217 surfaceMap[streamSurfaceId.streamId()] = std::vector<size_t>();
218 outputStreamIds.push_back(streamSurfaceId.streamId());
219 }
220 surfaceMap[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
221
222 ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
223 __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
224 streamSurfaceId.surfaceId());
Jianing Wei90e59c92014-03-12 18:29:36 -0700225 }
226
227 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
228 outputStreamIds.size());
229
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800230 if (request.mIsReprocess) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700231 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
232 }
233
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800234 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700235 loopCounter++; // loopCounter starts from 1
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800236 ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
237 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
238 loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700239
240 metadataRequestList.push_back(metadata);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700241 surfaceMapList.push_back(surfaceMap);
Jianing Wei90e59c92014-03-12 18:29:36 -0700242 }
243 mRequestIdCounter++;
244
245 if (streaming) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700246 err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
247 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800248 if (err != OK) {
249 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800250 "Camera %s: Got error %s (%d) after trying to set streaming request",
251 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800252 ALOGE("%s: %s", __FUNCTION__, msg.string());
253 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
254 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700255 } else {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700256 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700257 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700258 }
259 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700260 err = mDevice->captureList(metadataRequestList, surfaceMapList,
261 &(submitInfo->mLastFrameNumber));
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800262 if (err != OK) {
263 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800264 "Camera %s: Got error %s (%d) after trying to submit capture request",
265 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800266 ALOGE("%s: %s", __FUNCTION__, msg.string());
267 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
268 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700269 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800270 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700271 }
272
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800273 ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700274 return res;
275}
276
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800277binder::Status CameraDeviceClient::cancelRequest(
278 int requestId,
279 /*out*/
280 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700281 ATRACE_CALL();
282 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
283
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800284 status_t err;
285 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700286
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700288
289 Mutex::Autolock icl(mBinderSerializationLock);
290
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 if (!mDevice.get()) {
292 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
293 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700294
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700295 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700296 if (mStreamingRequestId != requestId) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800297 String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
298 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800299 ALOGE("%s: %s", __FUNCTION__, msg.string());
300 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700301 }
302
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800303 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700304
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800305 if (err == OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800306 ALOGV("%s: Camera %s: Successfully cleared streaming request",
307 __FUNCTION__, mCameraIdStr.string());
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700308 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800309 } else {
310 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800311 "Camera %s: Error clearing streaming request: %s (%d)",
312 mCameraIdStr.string(), strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700313 }
314
315 return res;
316}
317
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800318binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700319 // TODO: Implement this.
Zhijun He1fa89992015-06-01 15:44:31 -0700320 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800321 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700322}
323
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800324binder::Status CameraDeviceClient::endConfigure(bool isConstrainedHighSpeed) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700325 ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
326 __FUNCTION__, mInputStream.configured ? 1 : 0,
327 mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700328
Zhijun He0effd522016-03-04 10:22:27 -0800329 binder::Status res;
330 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
331
332 Mutex::Autolock icl(mBinderSerializationLock);
333
334 if (!mDevice.get()) {
335 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
336 }
337
Zhijun He1fa89992015-06-01 15:44:31 -0700338 // Sanitize the high speed session against necessary capability bit.
339 if (isConstrainedHighSpeed) {
340 CameraMetadata staticInfo = mDevice->info();
341 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
342 bool isConstrainedHighSpeedSupported = false;
343 for(size_t i = 0; i < entry.count; ++i) {
344 uint8_t capability = entry.data.u8[i];
345 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
346 isConstrainedHighSpeedSupported = true;
347 break;
348 }
349 }
350 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800351 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800352 "Camera %s: Try to create a constrained high speed configuration on a device"
353 " that doesn't support it.", mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800354 ALOGE("%s: %s", __FUNCTION__, msg.string());
355 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
356 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700357 }
358 }
359
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800360 status_t err = mDevice->configureStreams(isConstrainedHighSpeed);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700361 if (err == BAD_VALUE) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800362 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
363 mCameraIdStr.string());
Zhijun He5d677d12016-05-29 16:52:39 -0700364 ALOGE("%s: %s", __FUNCTION__, msg.string());
365 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700366 } else if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800367 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
368 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700369 ALOGE("%s: %s", __FUNCTION__, msg.string());
370 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800371 }
372
373 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700374}
375
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800376binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700377 ATRACE_CALL();
378 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
379
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800380 binder::Status res;
381 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700382
383 Mutex::Autolock icl(mBinderSerializationLock);
384
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800385 if (!mDevice.get()) {
386 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
387 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700388
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700389 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700390 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700391 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700392
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700393 if (mInputStream.configured && mInputStream.id == streamId) {
394 isInput = true;
395 } else {
396 // Guard against trying to delete non-created streams
397 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700398 if (streamId == mStreamMap.valueAt(i).streamId()) {
399 surfaces.push_back(mStreamMap.keyAt(i));
400 }
401 }
402
403 // See if this stream is one of the deferred streams.
404 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
405 if (streamId == mDeferredStreams[i]) {
406 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700407 break;
408 }
409 }
410
Shuzhen Wang0129d522016-10-30 22:43:41 -0700411 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
412 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
413 " stream created yet", mCameraIdStr.string(), streamId);
414 ALOGW("%s: %s", __FUNCTION__, msg.string());
415 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700416 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700417 }
418
419 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800420 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700421
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800422 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800423 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
424 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800425 ALOGE("%s: %s", __FUNCTION__, msg.string());
426 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
427 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700428 if (isInput) {
429 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700430 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700431 for (auto& surface : surfaces) {
432 mStreamMap.removeItem(surface);
433 }
434
435 if (dIndex != NAME_NOT_FOUND) {
436 mDeferredStreams.removeItemsAt(dIndex);
437 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700438 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700439 }
440
441 return res;
442}
443
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800444binder::Status CameraDeviceClient::createStream(
445 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
446 /*out*/
447 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700448 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700449
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800450 binder::Status res;
451 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700452
453 Mutex::Autolock icl(mBinderSerializationLock);
454
Shuzhen Wang0129d522016-10-30 22:43:41 -0700455 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
456 outputConfiguration.getGraphicBufferProducers();
457 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800458 bool deferredConsumer = outputConfiguration.isDeferred();
459 bool isShared = outputConfiguration.isShared();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700460
461 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
462 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
463 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
464 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
465 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800466 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700467 int surfaceType = outputConfiguration.getSurfaceType();
468 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
469 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700470
Zhijun He5d677d12016-05-29 16:52:39 -0700471 if (deferredConsumer && !validSurfaceType) {
472 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700473 __FUNCTION__, bufferProducers[0].get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800474 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700475 }
Zhijun He5d677d12016-05-29 16:52:39 -0700476
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800477 if (!mDevice.get()) {
478 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
479 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700480
Shuzhen Wang0129d522016-10-30 22:43:41 -0700481 std::vector<sp<Surface>> surfaces;
482 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700483 status_t err;
484
485 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700486 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800487 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700488 }
489
Shuzhen Wang758c2152017-01-10 18:26:18 -0800490 OutputStreamInfo streamInfo;
491 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700492 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700493 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700494 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800495 ssize_t index = mStreamMap.indexOfKey(binder);
496 if (index != NAME_NOT_FOUND) {
497 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
498 "(ID %zd)", mCameraIdStr.string(), index);
499 ALOGW("%s: %s", __FUNCTION__, msg.string());
500 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700501 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700502
Shuzhen Wang758c2152017-01-10 18:26:18 -0800503 sp<Surface> surface;
504 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
505
506 if (!res.isOk())
507 return res;
508
509 if (!isStreamInfoValid) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800510 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
511 // formats.
512 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
513 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
514 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
515 ALOGW("%s: %s", __FUNCTION__, msg.string());
516 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
517 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800518 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700519 }
520
Shuzhen Wang758c2152017-01-10 18:26:18 -0800521 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700522 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800523 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700524
Zhijun He125684a2015-12-26 15:07:30 -0800525 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800526 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
527 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800528 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang758c2152017-01-10 18:26:18 -0800529 &streamId, outputConfiguration.getSurfaceSetID(), isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700530
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800531 if (err != OK) {
532 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800533 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800534 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
535 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800536 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700537 int i = 0;
538 for (auto& binder : binders) {
539 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
540 __FUNCTION__, binder.get(), streamId, i);
541 mStreamMap.add(binder, StreamSurfaceId(streamId, i++));
542 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800543
544 mStreamInfoMap[streamId] = streamInfo;
545
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800546 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700547 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800548 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
549 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700550
Zhijun He5d677d12016-05-29 16:52:39 -0700551 // Set transform flags to ensure preview to be rotated correctly.
552 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700553
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800554 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700555 }
556
557 return res;
558}
559
Zhijun He5d677d12016-05-29 16:52:39 -0700560binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
561 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800562 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700563 /*out*/
564 int* newStreamId) {
565 int width, height, format, surfaceType;
566 int32_t consumerUsage;
567 android_dataspace dataSpace;
568 status_t err;
569 binder::Status res;
570
571 if (!mDevice.get()) {
572 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
573 }
574
575 // Infer the surface info for deferred surface stream creation.
576 width = outputConfiguration.getWidth();
577 height = outputConfiguration.getHeight();
578 surfaceType = outputConfiguration.getSurfaceType();
579 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
580 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
581 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
582 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
583 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
584 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
585 }
586 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700587 std::vector<sp<Surface>> noSurface;
588 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
589 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700590 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang758c2152017-01-10 18:26:18 -0800591 &streamId, outputConfiguration.getSurfaceSetID(), isShared, consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700592
593 if (err != OK) {
594 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800595 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
596 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700597 } else {
598 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
599 // a separate list to track. Once the deferred surface is set, this id will be
600 // relocated to mStreamMap.
601 mDeferredStreams.push_back(streamId);
602
Shuzhen Wang758c2152017-01-10 18:26:18 -0800603 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
604 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
605
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800606 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700607 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800608 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700609
610 // Set transform flags to ensure preview to be rotated correctly.
611 res = setStreamTransformLocked(streamId);
612
613 *newStreamId = streamId;
614 }
615 return res;
616}
617
618binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
619 int32_t transform = 0;
620 status_t err;
621 binder::Status res;
622
623 if (!mDevice.get()) {
624 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
625 }
626
627 err = getRotationTransformLocked(&transform);
628 if (err != OK) {
629 // Error logged by getRotationTransformLocked.
630 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
631 "Unable to calculate rotation transform for new stream");
632 }
633
634 err = mDevice->setStreamTransform(streamId, transform);
635 if (err != OK) {
636 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
637 streamId);
638 ALOGE("%s: %s", __FUNCTION__, msg.string());
639 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
640 }
641
642 return res;
643}
Ruben Brunkbba75572014-11-20 17:29:50 -0800644
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800645binder::Status CameraDeviceClient::createInputStream(
646 int width, int height, int format,
647 /*out*/
648 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700649
650 ATRACE_CALL();
651 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
652
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800653 binder::Status res;
654 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700655
656 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800657
658 if (!mDevice.get()) {
659 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
660 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700661
662 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800663 String8 msg = String8::format("Camera %s: Already has an input stream "
664 "configured (ID %zd)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800665 ALOGE("%s: %s", __FUNCTION__, msg.string() );
666 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700667 }
668
669 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800670 status_t err = mDevice->createInputStream(width, height, format, &streamId);
671 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700672 mInputStream.configured = true;
673 mInputStream.width = width;
674 mInputStream.height = height;
675 mInputStream.format = format;
676 mInputStream.id = streamId;
677
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800678 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
679 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700680
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800681 *newStreamId = streamId;
682 } else {
683 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800684 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800685 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700686 }
687
688 return res;
689}
690
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800691binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700692
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800693 binder::Status res;
694 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
695
696 if (inputSurface == NULL) {
697 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700698 }
699
700 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800701 if (!mDevice.get()) {
702 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
703 }
704 sp<IGraphicBufferProducer> producer;
705 status_t err = mDevice->getInputBufferProducer(&producer);
706 if (err != OK) {
707 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800708 "Camera %s: Error getting input Surface: %s (%d)",
709 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800710 } else {
711 inputSurface->name = String16("CameraInput");
712 inputSurface->graphicBufferProducer = producer;
713 }
714 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700715}
716
Eman Copty6d7af0e2016-06-17 20:46:40 -0700717bool CameraDeviceClient::isPublicFormat(int32_t format)
718{
719 switch(format) {
720 case HAL_PIXEL_FORMAT_RGBA_8888:
721 case HAL_PIXEL_FORMAT_RGBX_8888:
722 case HAL_PIXEL_FORMAT_RGB_888:
723 case HAL_PIXEL_FORMAT_RGB_565:
724 case HAL_PIXEL_FORMAT_BGRA_8888:
725 case HAL_PIXEL_FORMAT_YV12:
726 case HAL_PIXEL_FORMAT_Y8:
727 case HAL_PIXEL_FORMAT_Y16:
728 case HAL_PIXEL_FORMAT_RAW16:
729 case HAL_PIXEL_FORMAT_RAW10:
730 case HAL_PIXEL_FORMAT_RAW12:
731 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
732 case HAL_PIXEL_FORMAT_BLOB:
733 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
734 case HAL_PIXEL_FORMAT_YCbCr_420_888:
735 case HAL_PIXEL_FORMAT_YCbCr_422_888:
736 case HAL_PIXEL_FORMAT_YCbCr_444_888:
737 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
738 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
739 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
740 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
741 case HAL_PIXEL_FORMAT_YCbCr_422_I:
742 return true;
743 default:
744 return false;
745 }
746}
747
Shuzhen Wang758c2152017-01-10 18:26:18 -0800748binder::Status CameraDeviceClient::createSurfaceFromGbp(
749 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
750 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
751
752 // bufferProducer must be non-null
753 if (gbp == nullptr) {
754 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
755 ALOGW("%s: %s", __FUNCTION__, msg.string());
756 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
757 }
758 // HACK b/10949105
759 // Query consumer usage bits to set async operation mode for
760 // GLConsumer using controlledByApp parameter.
761 bool useAsync = false;
762 int32_t consumerUsage;
763 status_t err;
764 if ((err = gbp->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
765 &consumerUsage)) != OK) {
766 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
767 mCameraIdStr.string(), strerror(-err), err);
768 ALOGE("%s: %s", __FUNCTION__, msg.string());
769 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
770 }
771 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
772 ALOGW("%s: Camera %s with consumer usage flag: 0x%x: Forcing asynchronous mode for stream",
773 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
774 useAsync = true;
775 }
776
777 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
778 GRALLOC_USAGE_RENDERSCRIPT;
779 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
780 GraphicBuffer::USAGE_HW_TEXTURE |
781 GraphicBuffer::USAGE_HW_COMPOSER;
782 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
783 (consumerUsage & allowedFlags) != 0;
784
785 surface = new Surface(gbp, useAsync);
786 ANativeWindow *anw = surface.get();
787
788 int width, height, format;
789 android_dataspace dataSpace;
790 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
791 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
792 mCameraIdStr.string(), strerror(-err), err);
793 ALOGE("%s: %s", __FUNCTION__, msg.string());
794 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
795 }
796 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
797 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
798 mCameraIdStr.string(), strerror(-err), err);
799 ALOGE("%s: %s", __FUNCTION__, msg.string());
800 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
801 }
802 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
803 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
804 mCameraIdStr.string(), strerror(-err), err);
805 ALOGE("%s: %s", __FUNCTION__, msg.string());
806 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
807 }
808 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
809 reinterpret_cast<int*>(&dataSpace))) != OK) {
810 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
811 mCameraIdStr.string(), strerror(-err), err);
812 ALOGE("%s: %s", __FUNCTION__, msg.string());
813 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
814 }
815
816 // FIXME: remove this override since the default format should be
817 // IMPLEMENTATION_DEFINED. b/9487482
818 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
819 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
820 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
821 __FUNCTION__, mCameraIdStr.string(), format);
822 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
823 }
824 // Round dimensions to the nearest dimensions available for this format
825 if (flexibleConsumer && isPublicFormat(format) &&
826 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
827 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
828 String8 msg = String8::format("Camera %s: No supported stream configurations with "
829 "format %#x defined, failed to create output stream",
830 mCameraIdStr.string(), format);
831 ALOGE("%s: %s", __FUNCTION__, msg.string());
832 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
833 }
834
835 if (!isStreamInfoValid) {
836 streamInfo.width = width;
837 streamInfo.height = height;
838 streamInfo.format = format;
839 streamInfo.dataSpace = dataSpace;
840 streamInfo.consumerUsage = consumerUsage;
841 return binder::Status::ok();
842 }
843 if (width != streamInfo.width) {
844 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
845 mCameraIdStr.string(), width, streamInfo.width);
846 ALOGE("%s: %s", __FUNCTION__, msg.string());
847 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
848 }
849 if (height != streamInfo.height) {
850 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
851 mCameraIdStr.string(), height, streamInfo.height);
852 ALOGE("%s: %s", __FUNCTION__, msg.string());
853 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
854 }
855 if (format != streamInfo.format) {
856 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
857 mCameraIdStr.string(), format, streamInfo.format);
858 ALOGE("%s: %s", __FUNCTION__, msg.string());
859 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
860 }
861 if (dataSpace != streamInfo.dataSpace) {
862 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
863 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
864 ALOGE("%s: %s", __FUNCTION__, msg.string());
865 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
866 }
867 //At the native side, there isn't a way to check whether 2 surfaces come from the same
868 //surface class type. Use usage flag to approximate the comparison. Treat
869 //different preview surface usage flags as the same.
870 int32_t previewUsageMask =
871 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_HW_COMPOSER;
872 if ((consumerUsage & ~previewUsageMask) != (streamInfo.consumerUsage & ~previewUsageMask)) {
873 String8 msg = String8::format(
874 "Camera %s:Surface usage flag doesn't match 0x%x vs 0x%x",
875 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
876 ALOGE("%s: %s", __FUNCTION__, msg.string());
877 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
878 }
879 return binder::Status::ok();
880}
881
Ruben Brunkbba75572014-11-20 17:29:50 -0800882bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800883 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800884 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
885
886 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800887 (dataSpace == HAL_DATASPACE_DEPTH) ?
888 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800889 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
890
891 int32_t bestWidth = -1;
892 int32_t bestHeight = -1;
893
894 // Iterate through listed stream configurations and find the one with the smallest euclidean
895 // distance from the given dimensions for the given format.
896 for (size_t i = 0; i < streamConfigs.count; i += 4) {
897 int32_t fmt = streamConfigs.data.i32[i];
898 int32_t w = streamConfigs.data.i32[i + 1];
899 int32_t h = streamConfigs.data.i32[i + 2];
900
901 // Ignore input/output type for now
902 if (fmt == format) {
903 if (w == width && h == height) {
904 bestWidth = width;
905 bestHeight = height;
906 break;
907 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
908 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
909 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
910 bestWidth = w;
911 bestHeight = h;
912 }
913 }
914 }
915
916 if (bestWidth == -1) {
917 // Return false if no configurations for this format were listed
918 return false;
919 }
920
921 // Set the outputs to the closet width/height
922 if (outWidth != NULL) {
923 *outWidth = bestWidth;
924 }
925 if (outHeight != NULL) {
926 *outHeight = bestHeight;
927 }
928
929 // Return true if at least one configuration for this format was listed
930 return true;
931}
932
933int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
934 int64_t d0 = x0 - x1;
935 int64_t d1 = y0 - y1;
936 return d0 * d0 + d1 * d1;
937}
938
Igor Murashkine7ee7632013-06-11 18:10:18 -0700939// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800940binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
941 /*out*/
942 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700943{
944 ATRACE_CALL();
945 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
946
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800947 binder::Status res;
948 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700949
950 Mutex::Autolock icl(mBinderSerializationLock);
951
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800952 if (!mDevice.get()) {
953 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
954 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700955
956 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800957 status_t err;
958 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700959 request != NULL) {
960
961 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800962 } else if (err == BAD_VALUE) {
963 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800964 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
965 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800966
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800967 } else {
968 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800969 "Camera %s: Error creating default request for template %d: %s (%d)",
970 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700971 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700972 return res;
973}
974
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800975binder::Status CameraDeviceClient::getCameraInfo(
976 /*out*/
977 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700978{
979 ATRACE_CALL();
980 ALOGV("%s", __FUNCTION__);
981
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800982 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700983
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800984 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700985
986 Mutex::Autolock icl(mBinderSerializationLock);
987
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800988 if (!mDevice.get()) {
989 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
990 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700991
Igor Murashkin099b4572013-07-12 17:52:16 -0700992 if (info != NULL) {
993 *info = mDevice->info(); // static camera metadata
994 // TODO: merge with device-specific camera metadata
995 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700996
997 return res;
998}
999
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001000binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001001{
1002 ATRACE_CALL();
1003 ALOGV("%s", __FUNCTION__);
1004
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001005 binder::Status res;
1006 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001007
1008 Mutex::Autolock icl(mBinderSerializationLock);
1009
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001010 if (!mDevice.get()) {
1011 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1012 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001013
1014 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001015 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001016 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001017 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001018 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1019 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001020 ALOGE("%s: %s", __FUNCTION__, msg.string());
1021 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001022 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001023 status_t err = mDevice->waitUntilDrained();
1024 if (err != OK) {
1025 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001026 "Camera %s: Error waiting to drain: %s (%d)",
1027 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001028 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001029 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001030 return res;
1031}
1032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001033binder::Status CameraDeviceClient::flush(
1034 /*out*/
1035 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001036 ATRACE_CALL();
1037 ALOGV("%s", __FUNCTION__);
1038
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001039 binder::Status res;
1040 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001041
1042 Mutex::Autolock icl(mBinderSerializationLock);
1043
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001044 if (!mDevice.get()) {
1045 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1046 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001047
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001048 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001049 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001050 status_t err = mDevice->flush(lastFrameNumber);
1051 if (err != OK) {
1052 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001053 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001054 }
1055 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001056}
1057
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001058binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001059 ATRACE_CALL();
1060 ALOGV("%s", __FUNCTION__);
1061
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001062 binder::Status res;
1063 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001064
1065 Mutex::Autolock icl(mBinderSerializationLock);
1066
1067 // Guard against trying to prepare non-created streams
1068 ssize_t index = NAME_NOT_FOUND;
1069 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001070 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001071 index = i;
1072 break;
1073 }
1074 }
1075
1076 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001077 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1078 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001079 ALOGW("%s: %s", __FUNCTION__, msg.string());
1080 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001081 }
1082
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001083 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1084 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001085 status_t err = mDevice->prepare(streamId);
1086 if (err == BAD_VALUE) {
1087 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001088 "Camera %s: Stream %d has already been used, and cannot be prepared",
1089 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001090 } else if (err != OK) {
1091 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001092 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001093 strerror(-err), err);
1094 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001095 return res;
1096}
1097
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001098binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001099 ATRACE_CALL();
1100 ALOGV("%s", __FUNCTION__);
1101
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001102 binder::Status res;
1103 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001104
1105 Mutex::Autolock icl(mBinderSerializationLock);
1106
1107 // Guard against trying to prepare non-created streams
1108 ssize_t index = NAME_NOT_FOUND;
1109 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001110 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001111 index = i;
1112 break;
1113 }
1114 }
1115
1116 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001117 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1118 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001119 ALOGW("%s: %s", __FUNCTION__, msg.string());
1120 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001121 }
1122
1123 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001124 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1125 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001126 ALOGE("%s: %s", __FUNCTION__, msg.string());
1127 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001128 }
1129
1130 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1131 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001132 status_t err = mDevice->prepare(maxCount, streamId);
1133 if (err == BAD_VALUE) {
1134 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001135 "Camera %s: Stream %d has already been used, and cannot be prepared",
1136 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001137 } else if (err != OK) {
1138 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001139 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001140 strerror(-err), err);
1141 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001142
1143 return res;
1144}
1145
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001146binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001147 ATRACE_CALL();
1148 ALOGV("%s", __FUNCTION__);
1149
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001150 binder::Status res;
1151 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001152
1153 Mutex::Autolock icl(mBinderSerializationLock);
1154
1155 // Guard against trying to prepare non-created streams
1156 ssize_t index = NAME_NOT_FOUND;
1157 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001158 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001159 index = i;
1160 break;
1161 }
1162 }
1163
1164 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001165 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1166 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001167 ALOGW("%s: %s", __FUNCTION__, msg.string());
1168 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001169 }
1170
1171 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1172 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001173 status_t err = mDevice->tearDown(streamId);
1174 if (err == BAD_VALUE) {
1175 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001176 "Camera %s: Stream %d is still in use, cannot be torn down",
1177 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001178 } else if (err != OK) {
1179 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001180 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001181 strerror(-err), err);
1182 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001183
1184 return res;
1185}
1186
Shuzhen Wang758c2152017-01-10 18:26:18 -08001187binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001188 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1189 ATRACE_CALL();
1190
1191 binder::Status res;
1192 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1193
1194 Mutex::Autolock icl(mBinderSerializationLock);
1195
Shuzhen Wang0129d522016-10-30 22:43:41 -07001196 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1197 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001198
Shuzhen Wang0129d522016-10-30 22:43:41 -07001199 if (bufferProducers.size() == 0) {
1200 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001201 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1202 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001203
Shuzhen Wang758c2152017-01-10 18:26:18 -08001204 // streamId should be in mStreamMap if this stream already has a surface attached
1205 // to it. Otherwise, it should be in mDeferredStreams.
1206 bool streamIdConfigured = false;
1207 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1208 for (size_t i = 0; i < mStreamMap.size(); i++) {
1209 if (mStreamMap.valueAt(i).streamId() == streamId) {
1210 streamIdConfigured = true;
1211 break;
1212 }
Zhijun He5d677d12016-05-29 16:52:39 -07001213 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001214 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1215 if (streamId == mDeferredStreams[i]) {
1216 deferredStreamIndex = i;
1217 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001218 }
1219
Shuzhen Wang758c2152017-01-10 18:26:18 -08001220 }
1221 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1222 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1223 "(ID %d)", mCameraIdStr.string(), streamId);
1224 ALOGW("%s: %s", __FUNCTION__, msg.string());
1225 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001226 }
1227
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001228 if (mStreamInfoMap[streamId].finalized) {
1229 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1230 " on stream ID %d", mCameraIdStr.string(), streamId);
1231 ALOGW("%s: %s", __FUNCTION__, msg.string());
1232 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1233 }
1234
Zhijun He5d677d12016-05-29 16:52:39 -07001235 if (!mDevice.get()) {
1236 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1237 }
1238
Shuzhen Wang758c2152017-01-10 18:26:18 -08001239 std::vector<sp<Surface>> consumerSurfaces;
1240 std::vector<size_t> consumerSurfaceIds;
1241 size_t surfaceId = 0;
1242 for (auto& bufferProducer : bufferProducers) {
1243 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001244 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1245 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001246 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001247 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001248 surfaceId++;
1249 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001250 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001251
1252 sp<Surface> surface;
1253 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1254 surface, bufferProducer);
1255
1256 if (!res.isOk())
1257 return res;
1258
1259 consumerSurfaces.push_back(surface);
1260 consumerSurfaceIds.push_back(surfaceId);
1261 surfaceId++;
Zhijun He5d677d12016-05-29 16:52:39 -07001262 }
1263
Zhijun He5d677d12016-05-29 16:52:39 -07001264 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001265 status_t err;
1266 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces);
Zhijun He5d677d12016-05-29 16:52:39 -07001267 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001268 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1269 sp<IBinder> binder = IInterface::asBinder(
1270 consumerSurfaces[i]->getIGraphicBufferProducer());
1271 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %zu", __FUNCTION__,
1272 binder.get(), streamId, consumerSurfaceIds[i]);
1273 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1274 }
1275 if (deferredStreamIndex != NAME_NOT_FOUND) {
1276 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001277 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001278 mStreamInfoMap[streamId].finalized = true;
Zhijun He5d677d12016-05-29 16:52:39 -07001279 } else if (err == NO_INIT) {
1280 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001281 "Camera %s: Deferred surface is invalid: %s (%d)",
1282 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001283 } else {
1284 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001285 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1286 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001287 }
1288
1289 return res;
1290}
1291
Igor Murashkine7ee7632013-06-11 18:10:18 -07001292status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001293 return BasicClient::dump(fd, args);
1294}
1295
1296status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001297 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001298 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001299 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001300 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001301 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001302
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001303 dprintf(fd, " State:\n");
1304 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001305 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001306 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001307 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001308 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001309 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001310 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001311 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001312 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001313 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001314 mStreamMap.valueAt(i).streamId(),
1315 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001316 }
Zhijun He5d677d12016-05-29 16:52:39 -07001317 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001318 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001319 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001320 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001321 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001322 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001323 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001324 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001325 // TODO: print dynamic/request section from most recent requests
1326 mFrameProcessor->dump(fd, args);
1327
1328 return dumpDevice(fd, args);
1329}
1330
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001331void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001332 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001334 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001335
1336 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001337 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001338 }
1339}
1340
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001341void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1342 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1343
1344 if (remoteCb != 0) {
1345 remoteCb->onRepeatingRequestError(lastFrameNumber);
1346 }
1347
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001348 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001349 mStreamingRequestId = REQUEST_ID_NONE;
1350}
1351
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001352void CameraDeviceClient::notifyIdle() {
1353 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001354 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001355
1356 if (remoteCb != 0) {
1357 remoteCb->onDeviceIdle();
1358 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001359 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001360}
1361
Jianing Weicb0652e2014-03-12 18:29:36 -07001362void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001363 nsecs_t timestamp) {
1364 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001365 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001366 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001367 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001368 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001369 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001370}
1371
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001372void CameraDeviceClient::notifyPrepared(int streamId) {
1373 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001375 if (remoteCb != 0) {
1376 remoteCb->onPrepared(streamId);
1377 }
1378}
1379
Shuzhen Wang9d066012016-09-30 11:30:20 -07001380void CameraDeviceClient::notifyRequestQueueEmpty() {
1381 // Thread safe. Don't bother locking.
1382 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1383 if (remoteCb != 0) {
1384 remoteCb->onRequestQueueEmpty();
1385 }
1386}
1387
Igor Murashkine7ee7632013-06-11 18:10:18 -07001388void CameraDeviceClient::detachDevice() {
1389 if (mDevice == 0) return;
1390
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001391 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001392
1393 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1394 FRAME_PROCESSOR_LISTENER_MAX_ID,
1395 /*listener*/this);
1396 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001397 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001398 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001399 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001400
1401 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1402 {
1403 mDevice->clearStreamingRequest();
1404
1405 status_t code;
1406 if ((code = mDevice->waitUntilDrained()) != OK) {
1407 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1408 code);
1409 }
1410 }
1411
1412 Camera2ClientBase::detachDevice();
1413}
1414
1415/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001416void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001417 ATRACE_CALL();
1418 ALOGV("%s", __FUNCTION__);
1419
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001420 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001421 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001422 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001423 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001424 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001425}
1426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001427binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001428 if (mDisconnected) {
1429 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1430 "The camera device has been disconnected");
1431 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001432 status_t res = checkPid(checkLocation);
1433 return (res == OK) ? binder::Status::ok() :
1434 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1435 "Attempt to use camera from a different process than original client");
1436}
1437
Igor Murashkine7ee7632013-06-11 18:10:18 -07001438// TODO: move to Camera2ClientBase
1439bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1440
1441 const int pid = IPCThreadState::self()->getCallingPid();
1442 const int selfPid = getpid();
1443 camera_metadata_entry_t entry;
1444
1445 /**
1446 * Mixin default important security values
1447 * - android.led.transmit = defaulted ON
1448 */
1449 CameraMetadata staticInfo = mDevice->info();
1450 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1451 for(size_t i = 0; i < entry.count; ++i) {
1452 uint8_t led = entry.data.u8[i];
1453
1454 switch(led) {
1455 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1456 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1457 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1458 metadata.update(ANDROID_LED_TRANSMIT,
1459 &transmitDefault, 1);
1460 }
1461 break;
1462 }
1463 }
1464 }
1465
1466 // We can do anything!
1467 if (pid == selfPid) {
1468 return true;
1469 }
1470
1471 /**
1472 * Permission check special fields in the request
1473 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1474 */
1475 entry = metadata.find(ANDROID_LED_TRANSMIT);
1476 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1477 String16 permissionString =
1478 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1479 if (!checkCallingPermission(permissionString)) {
1480 const int uid = IPCThreadState::self()->getCallingUid();
1481 ALOGE("Permission Denial: "
1482 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1483 return false;
1484 }
1485 }
1486
1487 return true;
1488}
1489
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001490status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1491 ALOGV("%s: begin", __FUNCTION__);
1492
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001493 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001494 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001495}
1496
Igor Murashkine7ee7632013-06-11 18:10:18 -07001497} // namespace android