blob: 26188387f6d4fd807ca57c9c5d56ffaf8e2ef147 [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) {
510 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700511 }
512
Shuzhen Wang758c2152017-01-10 18:26:18 -0800513 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700514 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800515 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700516
Zhijun He125684a2015-12-26 15:07:30 -0800517 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800518 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
519 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800520 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang758c2152017-01-10 18:26:18 -0800521 &streamId, outputConfiguration.getSurfaceSetID(), isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700522
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800523 if (err != OK) {
524 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800525 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800526 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
527 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800528 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700529 int i = 0;
530 for (auto& binder : binders) {
531 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
532 __FUNCTION__, binder.get(), streamId, i);
533 mStreamMap.add(binder, StreamSurfaceId(streamId, i++));
534 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800535
536 mStreamInfoMap[streamId] = streamInfo;
537
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800538 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700539 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800540 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
541 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700542
Zhijun He5d677d12016-05-29 16:52:39 -0700543 // Set transform flags to ensure preview to be rotated correctly.
544 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700545
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700547 }
548
549 return res;
550}
551
Zhijun He5d677d12016-05-29 16:52:39 -0700552binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
553 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800554 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700555 /*out*/
556 int* newStreamId) {
557 int width, height, format, surfaceType;
558 int32_t consumerUsage;
559 android_dataspace dataSpace;
560 status_t err;
561 binder::Status res;
562
563 if (!mDevice.get()) {
564 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
565 }
566
567 // Infer the surface info for deferred surface stream creation.
568 width = outputConfiguration.getWidth();
569 height = outputConfiguration.getHeight();
570 surfaceType = outputConfiguration.getSurfaceType();
571 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
572 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
573 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
574 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
575 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
576 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
577 }
578 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700579 std::vector<sp<Surface>> noSurface;
580 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
581 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700582 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang758c2152017-01-10 18:26:18 -0800583 &streamId, outputConfiguration.getSurfaceSetID(), isShared, consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700584
585 if (err != OK) {
586 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800587 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
588 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700589 } else {
590 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
591 // a separate list to track. Once the deferred surface is set, this id will be
592 // relocated to mStreamMap.
593 mDeferredStreams.push_back(streamId);
594
Shuzhen Wang758c2152017-01-10 18:26:18 -0800595 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
596 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
597
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800598 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700599 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800600 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700601
602 // Set transform flags to ensure preview to be rotated correctly.
603 res = setStreamTransformLocked(streamId);
604
605 *newStreamId = streamId;
606 }
607 return res;
608}
609
610binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
611 int32_t transform = 0;
612 status_t err;
613 binder::Status res;
614
615 if (!mDevice.get()) {
616 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
617 }
618
619 err = getRotationTransformLocked(&transform);
620 if (err != OK) {
621 // Error logged by getRotationTransformLocked.
622 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
623 "Unable to calculate rotation transform for new stream");
624 }
625
626 err = mDevice->setStreamTransform(streamId, transform);
627 if (err != OK) {
628 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
629 streamId);
630 ALOGE("%s: %s", __FUNCTION__, msg.string());
631 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
632 }
633
634 return res;
635}
Ruben Brunkbba75572014-11-20 17:29:50 -0800636
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800637binder::Status CameraDeviceClient::createInputStream(
638 int width, int height, int format,
639 /*out*/
640 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700641
642 ATRACE_CALL();
643 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
644
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800645 binder::Status res;
646 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700647
648 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800649
650 if (!mDevice.get()) {
651 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
652 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700653
654 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800655 String8 msg = String8::format("Camera %s: Already has an input stream "
656 "configured (ID %zd)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800657 ALOGE("%s: %s", __FUNCTION__, msg.string() );
658 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700659 }
660
661 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800662 status_t err = mDevice->createInputStream(width, height, format, &streamId);
663 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700664 mInputStream.configured = true;
665 mInputStream.width = width;
666 mInputStream.height = height;
667 mInputStream.format = format;
668 mInputStream.id = streamId;
669
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800670 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
671 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700672
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800673 *newStreamId = streamId;
674 } else {
675 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800676 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800677 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700678 }
679
680 return res;
681}
682
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800683binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700684
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800685 binder::Status res;
686 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
687
688 if (inputSurface == NULL) {
689 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700690 }
691
692 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800693 if (!mDevice.get()) {
694 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
695 }
696 sp<IGraphicBufferProducer> producer;
697 status_t err = mDevice->getInputBufferProducer(&producer);
698 if (err != OK) {
699 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800700 "Camera %s: Error getting input Surface: %s (%d)",
701 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800702 } else {
703 inputSurface->name = String16("CameraInput");
704 inputSurface->graphicBufferProducer = producer;
705 }
706 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700707}
708
Eman Copty6d7af0e2016-06-17 20:46:40 -0700709bool CameraDeviceClient::isPublicFormat(int32_t format)
710{
711 switch(format) {
712 case HAL_PIXEL_FORMAT_RGBA_8888:
713 case HAL_PIXEL_FORMAT_RGBX_8888:
714 case HAL_PIXEL_FORMAT_RGB_888:
715 case HAL_PIXEL_FORMAT_RGB_565:
716 case HAL_PIXEL_FORMAT_BGRA_8888:
717 case HAL_PIXEL_FORMAT_YV12:
718 case HAL_PIXEL_FORMAT_Y8:
719 case HAL_PIXEL_FORMAT_Y16:
720 case HAL_PIXEL_FORMAT_RAW16:
721 case HAL_PIXEL_FORMAT_RAW10:
722 case HAL_PIXEL_FORMAT_RAW12:
723 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
724 case HAL_PIXEL_FORMAT_BLOB:
725 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
726 case HAL_PIXEL_FORMAT_YCbCr_420_888:
727 case HAL_PIXEL_FORMAT_YCbCr_422_888:
728 case HAL_PIXEL_FORMAT_YCbCr_444_888:
729 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
730 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
731 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
732 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
733 case HAL_PIXEL_FORMAT_YCbCr_422_I:
734 return true;
735 default:
736 return false;
737 }
738}
739
Shuzhen Wang758c2152017-01-10 18:26:18 -0800740binder::Status CameraDeviceClient::createSurfaceFromGbp(
741 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
742 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
743
744 // bufferProducer must be non-null
745 if (gbp == nullptr) {
746 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
747 ALOGW("%s: %s", __FUNCTION__, msg.string());
748 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
749 }
750 // HACK b/10949105
751 // Query consumer usage bits to set async operation mode for
752 // GLConsumer using controlledByApp parameter.
753 bool useAsync = false;
754 int32_t consumerUsage;
755 status_t err;
756 if ((err = gbp->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
757 &consumerUsage)) != OK) {
758 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
759 mCameraIdStr.string(), strerror(-err), err);
760 ALOGE("%s: %s", __FUNCTION__, msg.string());
761 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
762 }
763 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
764 ALOGW("%s: Camera %s with consumer usage flag: 0x%x: Forcing asynchronous mode for stream",
765 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
766 useAsync = true;
767 }
768
769 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
770 GRALLOC_USAGE_RENDERSCRIPT;
771 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
772 GraphicBuffer::USAGE_HW_TEXTURE |
773 GraphicBuffer::USAGE_HW_COMPOSER;
774 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
775 (consumerUsage & allowedFlags) != 0;
776
777 surface = new Surface(gbp, useAsync);
778 ANativeWindow *anw = surface.get();
779
780 int width, height, format;
781 android_dataspace dataSpace;
782 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
783 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
784 mCameraIdStr.string(), strerror(-err), err);
785 ALOGE("%s: %s", __FUNCTION__, msg.string());
786 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
787 }
788 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
789 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
790 mCameraIdStr.string(), strerror(-err), err);
791 ALOGE("%s: %s", __FUNCTION__, msg.string());
792 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
793 }
794 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
795 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
796 mCameraIdStr.string(), strerror(-err), err);
797 ALOGE("%s: %s", __FUNCTION__, msg.string());
798 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
799 }
800 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
801 reinterpret_cast<int*>(&dataSpace))) != OK) {
802 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
803 mCameraIdStr.string(), strerror(-err), err);
804 ALOGE("%s: %s", __FUNCTION__, msg.string());
805 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
806 }
807
808 // FIXME: remove this override since the default format should be
809 // IMPLEMENTATION_DEFINED. b/9487482
810 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
811 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
812 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
813 __FUNCTION__, mCameraIdStr.string(), format);
814 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
815 }
816 // Round dimensions to the nearest dimensions available for this format
817 if (flexibleConsumer && isPublicFormat(format) &&
818 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
819 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
820 String8 msg = String8::format("Camera %s: No supported stream configurations with "
821 "format %#x defined, failed to create output stream",
822 mCameraIdStr.string(), format);
823 ALOGE("%s: %s", __FUNCTION__, msg.string());
824 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
825 }
826
827 if (!isStreamInfoValid) {
828 streamInfo.width = width;
829 streamInfo.height = height;
830 streamInfo.format = format;
831 streamInfo.dataSpace = dataSpace;
832 streamInfo.consumerUsage = consumerUsage;
833 return binder::Status::ok();
834 }
835 if (width != streamInfo.width) {
836 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
837 mCameraIdStr.string(), width, streamInfo.width);
838 ALOGE("%s: %s", __FUNCTION__, msg.string());
839 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
840 }
841 if (height != streamInfo.height) {
842 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
843 mCameraIdStr.string(), height, streamInfo.height);
844 ALOGE("%s: %s", __FUNCTION__, msg.string());
845 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
846 }
847 if (format != streamInfo.format) {
848 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
849 mCameraIdStr.string(), format, streamInfo.format);
850 ALOGE("%s: %s", __FUNCTION__, msg.string());
851 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
852 }
853 if (dataSpace != streamInfo.dataSpace) {
854 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
855 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
856 ALOGE("%s: %s", __FUNCTION__, msg.string());
857 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
858 }
859 //At the native side, there isn't a way to check whether 2 surfaces come from the same
860 //surface class type. Use usage flag to approximate the comparison. Treat
861 //different preview surface usage flags as the same.
862 int32_t previewUsageMask =
863 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_HW_COMPOSER;
864 if ((consumerUsage & ~previewUsageMask) != (streamInfo.consumerUsage & ~previewUsageMask)) {
865 String8 msg = String8::format(
866 "Camera %s:Surface usage flag doesn't match 0x%x vs 0x%x",
867 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
868 ALOGE("%s: %s", __FUNCTION__, msg.string());
869 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
870 }
871 return binder::Status::ok();
872}
873
Ruben Brunkbba75572014-11-20 17:29:50 -0800874bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800875 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800876 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
877
878 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800879 (dataSpace == HAL_DATASPACE_DEPTH) ?
880 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800881 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
882
883 int32_t bestWidth = -1;
884 int32_t bestHeight = -1;
885
886 // Iterate through listed stream configurations and find the one with the smallest euclidean
887 // distance from the given dimensions for the given format.
888 for (size_t i = 0; i < streamConfigs.count; i += 4) {
889 int32_t fmt = streamConfigs.data.i32[i];
890 int32_t w = streamConfigs.data.i32[i + 1];
891 int32_t h = streamConfigs.data.i32[i + 2];
892
893 // Ignore input/output type for now
894 if (fmt == format) {
895 if (w == width && h == height) {
896 bestWidth = width;
897 bestHeight = height;
898 break;
899 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
900 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
901 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
902 bestWidth = w;
903 bestHeight = h;
904 }
905 }
906 }
907
908 if (bestWidth == -1) {
909 // Return false if no configurations for this format were listed
910 return false;
911 }
912
913 // Set the outputs to the closet width/height
914 if (outWidth != NULL) {
915 *outWidth = bestWidth;
916 }
917 if (outHeight != NULL) {
918 *outHeight = bestHeight;
919 }
920
921 // Return true if at least one configuration for this format was listed
922 return true;
923}
924
925int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
926 int64_t d0 = x0 - x1;
927 int64_t d1 = y0 - y1;
928 return d0 * d0 + d1 * d1;
929}
930
Igor Murashkine7ee7632013-06-11 18:10:18 -0700931// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800932binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
933 /*out*/
934 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700935{
936 ATRACE_CALL();
937 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
938
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800939 binder::Status res;
940 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700941
942 Mutex::Autolock icl(mBinderSerializationLock);
943
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800944 if (!mDevice.get()) {
945 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
946 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700947
948 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800949 status_t err;
950 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700951 request != NULL) {
952
953 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800954 } else if (err == BAD_VALUE) {
955 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800956 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
957 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800958
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800959 } else {
960 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800961 "Camera %s: Error creating default request for template %d: %s (%d)",
962 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700963 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700964 return res;
965}
966
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800967binder::Status CameraDeviceClient::getCameraInfo(
968 /*out*/
969 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700970{
971 ATRACE_CALL();
972 ALOGV("%s", __FUNCTION__);
973
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800974 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700975
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800976 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700977
978 Mutex::Autolock icl(mBinderSerializationLock);
979
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800980 if (!mDevice.get()) {
981 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
982 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700983
Igor Murashkin099b4572013-07-12 17:52:16 -0700984 if (info != NULL) {
985 *info = mDevice->info(); // static camera metadata
986 // TODO: merge with device-specific camera metadata
987 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700988
989 return res;
990}
991
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800992binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -0700993{
994 ATRACE_CALL();
995 ALOGV("%s", __FUNCTION__);
996
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800997 binder::Status res;
998 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -0700999
1000 Mutex::Autolock icl(mBinderSerializationLock);
1001
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001002 if (!mDevice.get()) {
1003 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1004 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001005
1006 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001007 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001008 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001009 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001010 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1011 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001012 ALOGE("%s: %s", __FUNCTION__, msg.string());
1013 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001014 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001015 status_t err = mDevice->waitUntilDrained();
1016 if (err != OK) {
1017 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001018 "Camera %s: Error waiting to drain: %s (%d)",
1019 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001020 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001021 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001022 return res;
1023}
1024
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001025binder::Status CameraDeviceClient::flush(
1026 /*out*/
1027 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001028 ATRACE_CALL();
1029 ALOGV("%s", __FUNCTION__);
1030
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001031 binder::Status res;
1032 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001033
1034 Mutex::Autolock icl(mBinderSerializationLock);
1035
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001036 if (!mDevice.get()) {
1037 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1038 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001039
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001040 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001041 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001042 status_t err = mDevice->flush(lastFrameNumber);
1043 if (err != OK) {
1044 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001045 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001046 }
1047 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001048}
1049
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001050binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001051 ATRACE_CALL();
1052 ALOGV("%s", __FUNCTION__);
1053
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001054 binder::Status res;
1055 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001056
1057 Mutex::Autolock icl(mBinderSerializationLock);
1058
1059 // Guard against trying to prepare non-created streams
1060 ssize_t index = NAME_NOT_FOUND;
1061 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001062 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001063 index = i;
1064 break;
1065 }
1066 }
1067
1068 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001069 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1070 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001071 ALOGW("%s: %s", __FUNCTION__, msg.string());
1072 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001073 }
1074
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001075 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1076 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001077 status_t err = mDevice->prepare(streamId);
1078 if (err == BAD_VALUE) {
1079 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001080 "Camera %s: Stream %d has already been used, and cannot be prepared",
1081 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001082 } else if (err != OK) {
1083 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001084 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001085 strerror(-err), err);
1086 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001087 return res;
1088}
1089
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001090binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001091 ATRACE_CALL();
1092 ALOGV("%s", __FUNCTION__);
1093
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001094 binder::Status res;
1095 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001096
1097 Mutex::Autolock icl(mBinderSerializationLock);
1098
1099 // Guard against trying to prepare non-created streams
1100 ssize_t index = NAME_NOT_FOUND;
1101 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001102 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001103 index = i;
1104 break;
1105 }
1106 }
1107
1108 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001109 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1110 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001111 ALOGW("%s: %s", __FUNCTION__, msg.string());
1112 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001113 }
1114
1115 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001116 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1117 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001118 ALOGE("%s: %s", __FUNCTION__, msg.string());
1119 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001120 }
1121
1122 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1123 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001124 status_t err = mDevice->prepare(maxCount, streamId);
1125 if (err == BAD_VALUE) {
1126 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001127 "Camera %s: Stream %d has already been used, and cannot be prepared",
1128 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001129 } else if (err != OK) {
1130 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001131 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001132 strerror(-err), err);
1133 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001134
1135 return res;
1136}
1137
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001138binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001139 ATRACE_CALL();
1140 ALOGV("%s", __FUNCTION__);
1141
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001142 binder::Status res;
1143 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001144
1145 Mutex::Autolock icl(mBinderSerializationLock);
1146
1147 // Guard against trying to prepare non-created streams
1148 ssize_t index = NAME_NOT_FOUND;
1149 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001150 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001151 index = i;
1152 break;
1153 }
1154 }
1155
1156 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001157 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1158 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001159 ALOGW("%s: %s", __FUNCTION__, msg.string());
1160 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001161 }
1162
1163 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1164 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001165 status_t err = mDevice->tearDown(streamId);
1166 if (err == BAD_VALUE) {
1167 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001168 "Camera %s: Stream %d is still in use, cannot be torn down",
1169 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001170 } else if (err != OK) {
1171 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001172 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001173 strerror(-err), err);
1174 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001175
1176 return res;
1177}
1178
Shuzhen Wang758c2152017-01-10 18:26:18 -08001179binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001180 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1181 ATRACE_CALL();
1182
1183 binder::Status res;
1184 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1185
1186 Mutex::Autolock icl(mBinderSerializationLock);
1187
Shuzhen Wang0129d522016-10-30 22:43:41 -07001188 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1189 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001190
Shuzhen Wang0129d522016-10-30 22:43:41 -07001191 if (bufferProducers.size() == 0) {
1192 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001193 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1194 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001195
Shuzhen Wang758c2152017-01-10 18:26:18 -08001196 // streamId should be in mStreamMap if this stream already has a surface attached
1197 // to it. Otherwise, it should be in mDeferredStreams.
1198 bool streamIdConfigured = false;
1199 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1200 for (size_t i = 0; i < mStreamMap.size(); i++) {
1201 if (mStreamMap.valueAt(i).streamId() == streamId) {
1202 streamIdConfigured = true;
1203 break;
1204 }
Zhijun He5d677d12016-05-29 16:52:39 -07001205 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001206 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1207 if (streamId == mDeferredStreams[i]) {
1208 deferredStreamIndex = i;
1209 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001210 }
1211
Shuzhen Wang758c2152017-01-10 18:26:18 -08001212 }
1213 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1214 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1215 "(ID %d)", mCameraIdStr.string(), streamId);
1216 ALOGW("%s: %s", __FUNCTION__, msg.string());
1217 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001218 }
1219
1220 if (!mDevice.get()) {
1221 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1222 }
1223
Shuzhen Wang758c2152017-01-10 18:26:18 -08001224 std::vector<sp<Surface>> consumerSurfaces;
1225 std::vector<size_t> consumerSurfaceIds;
1226 size_t surfaceId = 0;
1227 for (auto& bufferProducer : bufferProducers) {
1228 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001229 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1230 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001231 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001232 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001233 surfaceId++;
1234 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001235 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001236
1237 sp<Surface> surface;
1238 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1239 surface, bufferProducer);
1240
1241 if (!res.isOk())
1242 return res;
1243
1244 consumerSurfaces.push_back(surface);
1245 consumerSurfaceIds.push_back(surfaceId);
1246 surfaceId++;
Zhijun He5d677d12016-05-29 16:52:39 -07001247 }
1248
Shuzhen Wang758c2152017-01-10 18:26:18 -08001249 if (consumerSurfaces.size() == 0) {
1250 String8 msg = String8::format("Camera %s: New OutputConfiguration has the same surfaces"
1251 " for stream (ID %d)", mCameraIdStr.string(), streamId);
1252 ALOGW("%s: %s", __FUNCTION__, msg.string());
1253 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1254 }
Zhijun He5d677d12016-05-29 16:52:39 -07001255
1256 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001257 status_t err;
1258 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces);
Zhijun He5d677d12016-05-29 16:52:39 -07001259 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001260 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1261 sp<IBinder> binder = IInterface::asBinder(
1262 consumerSurfaces[i]->getIGraphicBufferProducer());
1263 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %zu", __FUNCTION__,
1264 binder.get(), streamId, consumerSurfaceIds[i]);
1265 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1266 }
1267 if (deferredStreamIndex != NAME_NOT_FOUND) {
1268 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001269 }
Zhijun He5d677d12016-05-29 16:52:39 -07001270 } else if (err == NO_INIT) {
1271 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001272 "Camera %s: Deferred surface is invalid: %s (%d)",
1273 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001274 } else {
1275 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001276 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1277 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001278 }
1279
1280 return res;
1281}
1282
Igor Murashkine7ee7632013-06-11 18:10:18 -07001283status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001284 return BasicClient::dump(fd, args);
1285}
1286
1287status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001288 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001289 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001290 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001291 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001292 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001293
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001294 dprintf(fd, " State:\n");
1295 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001296 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001297 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001298 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001299 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001300 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001301 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001302 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001303 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001304 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001305 mStreamMap.valueAt(i).streamId(),
1306 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001307 }
Zhijun He5d677d12016-05-29 16:52:39 -07001308 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001309 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001310 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001311 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001312 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001313 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001314 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001315 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001316 // TODO: print dynamic/request section from most recent requests
1317 mFrameProcessor->dump(fd, args);
1318
1319 return dumpDevice(fd, args);
1320}
1321
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001322void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001323 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001324 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001325 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001326
1327 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001328 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001329 }
1330}
1331
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001332void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1333 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1334
1335 if (remoteCb != 0) {
1336 remoteCb->onRepeatingRequestError(lastFrameNumber);
1337 }
1338
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001339 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001340 mStreamingRequestId = REQUEST_ID_NONE;
1341}
1342
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001343void CameraDeviceClient::notifyIdle() {
1344 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001345 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001346
1347 if (remoteCb != 0) {
1348 remoteCb->onDeviceIdle();
1349 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001350 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001351}
1352
Jianing Weicb0652e2014-03-12 18:29:36 -07001353void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001354 nsecs_t timestamp) {
1355 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001356 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001357 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001358 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001359 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001360 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001361}
1362
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001363void CameraDeviceClient::notifyPrepared(int streamId) {
1364 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001365 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001366 if (remoteCb != 0) {
1367 remoteCb->onPrepared(streamId);
1368 }
1369}
1370
Shuzhen Wang9d066012016-09-30 11:30:20 -07001371void CameraDeviceClient::notifyRequestQueueEmpty() {
1372 // Thread safe. Don't bother locking.
1373 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1374 if (remoteCb != 0) {
1375 remoteCb->onRequestQueueEmpty();
1376 }
1377}
1378
Igor Murashkine7ee7632013-06-11 18:10:18 -07001379void CameraDeviceClient::detachDevice() {
1380 if (mDevice == 0) return;
1381
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001382 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001383
1384 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1385 FRAME_PROCESSOR_LISTENER_MAX_ID,
1386 /*listener*/this);
1387 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001388 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001389 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001390 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001391
1392 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1393 {
1394 mDevice->clearStreamingRequest();
1395
1396 status_t code;
1397 if ((code = mDevice->waitUntilDrained()) != OK) {
1398 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1399 code);
1400 }
1401 }
1402
1403 Camera2ClientBase::detachDevice();
1404}
1405
1406/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001407void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001408 ATRACE_CALL();
1409 ALOGV("%s", __FUNCTION__);
1410
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001411 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001412 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001413 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001414 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001415 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001416}
1417
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001418binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001419 if (mDisconnected) {
1420 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1421 "The camera device has been disconnected");
1422 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001423 status_t res = checkPid(checkLocation);
1424 return (res == OK) ? binder::Status::ok() :
1425 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1426 "Attempt to use camera from a different process than original client");
1427}
1428
Igor Murashkine7ee7632013-06-11 18:10:18 -07001429// TODO: move to Camera2ClientBase
1430bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1431
1432 const int pid = IPCThreadState::self()->getCallingPid();
1433 const int selfPid = getpid();
1434 camera_metadata_entry_t entry;
1435
1436 /**
1437 * Mixin default important security values
1438 * - android.led.transmit = defaulted ON
1439 */
1440 CameraMetadata staticInfo = mDevice->info();
1441 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1442 for(size_t i = 0; i < entry.count; ++i) {
1443 uint8_t led = entry.data.u8[i];
1444
1445 switch(led) {
1446 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1447 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1448 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1449 metadata.update(ANDROID_LED_TRANSMIT,
1450 &transmitDefault, 1);
1451 }
1452 break;
1453 }
1454 }
1455 }
1456
1457 // We can do anything!
1458 if (pid == selfPid) {
1459 return true;
1460 }
1461
1462 /**
1463 * Permission check special fields in the request
1464 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1465 */
1466 entry = metadata.find(ANDROID_LED_TRANSMIT);
1467 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1468 String16 permissionString =
1469 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1470 if (!checkCallingPermission(permissionString)) {
1471 const int uid = IPCThreadState::self()->getCallingUid();
1472 ALOGE("Permission Denial: "
1473 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1474 return false;
1475 }
1476 }
1477
1478 return true;
1479}
1480
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001481status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1482 ALOGV("%s: begin", __FUNCTION__);
1483
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001484 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001485 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001486}
1487
Igor Murashkine7ee7632013-06-11 18:10:18 -07001488} // namespace android