blob: f2e8df8c34040ac8fda51bcc66386257d2afa2fe [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 Talvalabbbbe842017-02-28 17:50:56 -0800324binder::Status CameraDeviceClient::endConfigure(int operatingMode) {
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
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800338 if (operatingMode < 0) {
339 String8 msg = String8::format(
340 "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
341 ALOGE("%s: %s", __FUNCTION__, msg.string());
342 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
343 msg.string());
344 }
345
Zhijun He1fa89992015-06-01 15:44:31 -0700346 // Sanitize the high speed session against necessary capability bit.
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800347 bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
Zhijun He1fa89992015-06-01 15:44:31 -0700348 if (isConstrainedHighSpeed) {
349 CameraMetadata staticInfo = mDevice->info();
350 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
351 bool isConstrainedHighSpeedSupported = false;
352 for(size_t i = 0; i < entry.count; ++i) {
353 uint8_t capability = entry.data.u8[i];
354 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
355 isConstrainedHighSpeedSupported = true;
356 break;
357 }
358 }
359 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800360 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800361 "Camera %s: Try to create a constrained high speed configuration on a device"
362 " that doesn't support it.", mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800363 ALOGE("%s: %s", __FUNCTION__, msg.string());
364 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
365 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700366 }
367 }
368
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800369 status_t err = mDevice->configureStreams(operatingMode);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700370 if (err == BAD_VALUE) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800371 String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
372 mCameraIdStr.string());
Zhijun He5d677d12016-05-29 16:52:39 -0700373 ALOGE("%s: %s", __FUNCTION__, msg.string());
374 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700375 } else if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800376 String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
377 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700378 ALOGE("%s: %s", __FUNCTION__, msg.string());
379 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800380 }
381
382 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700383}
384
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800385binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700386 ATRACE_CALL();
387 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800389 binder::Status res;
390 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700391
392 Mutex::Autolock icl(mBinderSerializationLock);
393
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800394 if (!mDevice.get()) {
395 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
396 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700397
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700398 bool isInput = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700399 std::vector<sp<IBinder>> surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -0700400 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700401
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700402 if (mInputStream.configured && mInputStream.id == streamId) {
403 isInput = true;
404 } else {
405 // Guard against trying to delete non-created streams
406 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700407 if (streamId == mStreamMap.valueAt(i).streamId()) {
408 surfaces.push_back(mStreamMap.keyAt(i));
409 }
410 }
411
412 // See if this stream is one of the deferred streams.
413 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
414 if (streamId == mDeferredStreams[i]) {
415 dIndex = i;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700416 break;
417 }
418 }
419
Shuzhen Wang0129d522016-10-30 22:43:41 -0700420 if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
421 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
422 " stream created yet", mCameraIdStr.string(), streamId);
423 ALOGW("%s: %s", __FUNCTION__, msg.string());
424 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700425 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700426 }
427
428 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800429 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700430
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800431 if (err != OK) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800432 String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
433 mCameraIdStr.string(), strerror(-err), err, streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800434 ALOGE("%s: %s", __FUNCTION__, msg.string());
435 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
436 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700437 if (isInput) {
438 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700439 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700440 for (auto& surface : surfaces) {
441 mStreamMap.removeItem(surface);
442 }
443
444 if (dIndex != NAME_NOT_FOUND) {
445 mDeferredStreams.removeItemsAt(dIndex);
446 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700447 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700448 }
449
450 return res;
451}
452
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800453binder::Status CameraDeviceClient::createStream(
454 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
455 /*out*/
456 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700457 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700458
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800459 binder::Status res;
460 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700461
462 Mutex::Autolock icl(mBinderSerializationLock);
463
Shuzhen Wang0129d522016-10-30 22:43:41 -0700464 const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
465 outputConfiguration.getGraphicBufferProducers();
466 size_t numBufferProducers = bufferProducers.size();
Shuzhen Wang758c2152017-01-10 18:26:18 -0800467 bool deferredConsumer = outputConfiguration.isDeferred();
468 bool isShared = outputConfiguration.isShared();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700469
470 if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
471 ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
472 __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
473 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
474 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800475 bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700476 int surfaceType = outputConfiguration.getSurfaceType();
477 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
478 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700479
Zhijun He5d677d12016-05-29 16:52:39 -0700480 if (deferredConsumer && !validSurfaceType) {
481 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
Shuzhen Wang0129d522016-10-30 22:43:41 -0700482 __FUNCTION__, bufferProducers[0].get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800483 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700484 }
Zhijun He5d677d12016-05-29 16:52:39 -0700485
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800486 if (!mDevice.get()) {
487 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
488 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700489
Shuzhen Wang0129d522016-10-30 22:43:41 -0700490 std::vector<sp<Surface>> surfaces;
491 std::vector<sp<IBinder>> binders;
Zhijun He5d677d12016-05-29 16:52:39 -0700492 status_t err;
493
494 // Create stream for deferred surface case.
Shuzhen Wang0129d522016-10-30 22:43:41 -0700495 if (deferredConsumerOnly) {
Shuzhen Wang758c2152017-01-10 18:26:18 -0800496 return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
Zhijun He5d677d12016-05-29 16:52:39 -0700497 }
498
Shuzhen Wang758c2152017-01-10 18:26:18 -0800499 OutputStreamInfo streamInfo;
500 bool isStreamInfoValid = false;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700501 for (auto& bufferProducer : bufferProducers) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700502 // Don't create multiple streams for the same target surface
Shuzhen Wang0129d522016-10-30 22:43:41 -0700503 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800504 ssize_t index = mStreamMap.indexOfKey(binder);
505 if (index != NAME_NOT_FOUND) {
506 String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
507 "(ID %zd)", mCameraIdStr.string(), index);
508 ALOGW("%s: %s", __FUNCTION__, msg.string());
509 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Shuzhen Wang0129d522016-10-30 22:43:41 -0700510 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700511
Shuzhen Wang758c2152017-01-10 18:26:18 -0800512 sp<Surface> surface;
513 res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
514
515 if (!res.isOk())
516 return res;
517
518 if (!isStreamInfoValid) {
Shuzhen Wangbee0f0a2017-01-24 14:51:37 -0800519 // Streaming sharing is only supported for IMPLEMENTATION_DEFINED
520 // formats.
521 if (isShared && streamInfo.format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
522 String8 msg = String8::format("Camera %s: Stream sharing is only supported for "
523 "IMPLEMENTATION_DEFINED format", mCameraIdStr.string());
524 ALOGW("%s: %s", __FUNCTION__, msg.string());
525 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
526 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800527 isStreamInfoValid = true;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700528 }
529
Shuzhen Wang758c2152017-01-10 18:26:18 -0800530 binders.push_back(IInterface::asBinder(bufferProducer));
Shuzhen Wang0129d522016-10-30 22:43:41 -0700531 surfaces.push_back(surface);
Ruben Brunkbba75572014-11-20 17:29:50 -0800532 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700533
Zhijun He125684a2015-12-26 15:07:30 -0800534 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang758c2152017-01-10 18:26:18 -0800535 err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
536 streamInfo.height, streamInfo.format, streamInfo.dataSpace,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang758c2152017-01-10 18:26:18 -0800538 &streamId, outputConfiguration.getSurfaceSetID(), isShared);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700539
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800540 if (err != OK) {
541 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800542 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800543 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
544 streamInfo.dataSpace, strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700546 int i = 0;
547 for (auto& binder : binders) {
548 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
549 __FUNCTION__, binder.get(), streamId, i);
550 mStreamMap.add(binder, StreamSurfaceId(streamId, i++));
551 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800552
553 mStreamInfoMap[streamId] = streamInfo;
554
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800555 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
Shuzhen Wang0129d522016-10-30 22:43:41 -0700556 " (%d x %d) with format 0x%x.",
Shuzhen Wang758c2152017-01-10 18:26:18 -0800557 __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
558 streamInfo.height, streamInfo.format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700559
Zhijun He5d677d12016-05-29 16:52:39 -0700560 // Set transform flags to ensure preview to be rotated correctly.
561 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700562
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800563 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700564 }
565
566 return res;
567}
568
Zhijun He5d677d12016-05-29 16:52:39 -0700569binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
570 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800571 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700572 /*out*/
573 int* newStreamId) {
574 int width, height, format, surfaceType;
575 int32_t consumerUsage;
576 android_dataspace dataSpace;
577 status_t err;
578 binder::Status res;
579
580 if (!mDevice.get()) {
581 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
582 }
583
584 // Infer the surface info for deferred surface stream creation.
585 width = outputConfiguration.getWidth();
586 height = outputConfiguration.getHeight();
587 surfaceType = outputConfiguration.getSurfaceType();
588 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
589 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
590 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
591 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
592 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
593 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
594 }
595 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700596 std::vector<sp<Surface>> noSurface;
597 err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
598 height, format, dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -0700599 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
Shuzhen Wang758c2152017-01-10 18:26:18 -0800600 &streamId, outputConfiguration.getSurfaceSetID(), isShared, consumerUsage);
Zhijun He5d677d12016-05-29 16:52:39 -0700601
602 if (err != OK) {
603 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800604 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
605 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700606 } else {
607 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
608 // a separate list to track. Once the deferred surface is set, this id will be
609 // relocated to mStreamMap.
610 mDeferredStreams.push_back(streamId);
611
Shuzhen Wang758c2152017-01-10 18:26:18 -0800612 mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
613 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
614
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800615 ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
Zhijun He5d677d12016-05-29 16:52:39 -0700616 " (%d x %d) stream with format 0x%x.",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800617 __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
Zhijun He5d677d12016-05-29 16:52:39 -0700618
619 // Set transform flags to ensure preview to be rotated correctly.
620 res = setStreamTransformLocked(streamId);
621
622 *newStreamId = streamId;
623 }
624 return res;
625}
626
627binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
628 int32_t transform = 0;
629 status_t err;
630 binder::Status res;
631
632 if (!mDevice.get()) {
633 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
634 }
635
636 err = getRotationTransformLocked(&transform);
637 if (err != OK) {
638 // Error logged by getRotationTransformLocked.
639 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
640 "Unable to calculate rotation transform for new stream");
641 }
642
643 err = mDevice->setStreamTransform(streamId, transform);
644 if (err != OK) {
645 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
646 streamId);
647 ALOGE("%s: %s", __FUNCTION__, msg.string());
648 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
649 }
650
651 return res;
652}
Ruben Brunkbba75572014-11-20 17:29:50 -0800653
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800654binder::Status CameraDeviceClient::createInputStream(
655 int width, int height, int format,
656 /*out*/
657 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700658
659 ATRACE_CALL();
660 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
661
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800662 binder::Status res;
663 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700664
665 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800666
667 if (!mDevice.get()) {
668 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
669 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700670
671 if (mInputStream.configured) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800672 String8 msg = String8::format("Camera %s: Already has an input stream "
673 "configured (ID %zd)", mCameraIdStr.string(), mInputStream.id);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800674 ALOGE("%s: %s", __FUNCTION__, msg.string() );
675 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700676 }
677
678 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800679 status_t err = mDevice->createInputStream(width, height, format, &streamId);
680 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700681 mInputStream.configured = true;
682 mInputStream.width = width;
683 mInputStream.height = height;
684 mInputStream.format = format;
685 mInputStream.id = streamId;
686
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800687 ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
688 __FUNCTION__, mCameraIdStr.string(), streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700689
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800690 *newStreamId = streamId;
691 } else {
692 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800693 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800694 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700695 }
696
697 return res;
698}
699
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800700binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700701
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800702 binder::Status res;
703 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
704
705 if (inputSurface == NULL) {
706 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700707 }
708
709 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800710 if (!mDevice.get()) {
711 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
712 }
713 sp<IGraphicBufferProducer> producer;
714 status_t err = mDevice->getInputBufferProducer(&producer);
715 if (err != OK) {
716 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800717 "Camera %s: Error getting input Surface: %s (%d)",
718 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800719 } else {
720 inputSurface->name = String16("CameraInput");
721 inputSurface->graphicBufferProducer = producer;
722 }
723 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700724}
725
Eman Copty6d7af0e2016-06-17 20:46:40 -0700726bool CameraDeviceClient::isPublicFormat(int32_t format)
727{
728 switch(format) {
729 case HAL_PIXEL_FORMAT_RGBA_8888:
730 case HAL_PIXEL_FORMAT_RGBX_8888:
731 case HAL_PIXEL_FORMAT_RGB_888:
732 case HAL_PIXEL_FORMAT_RGB_565:
733 case HAL_PIXEL_FORMAT_BGRA_8888:
734 case HAL_PIXEL_FORMAT_YV12:
735 case HAL_PIXEL_FORMAT_Y8:
736 case HAL_PIXEL_FORMAT_Y16:
737 case HAL_PIXEL_FORMAT_RAW16:
738 case HAL_PIXEL_FORMAT_RAW10:
739 case HAL_PIXEL_FORMAT_RAW12:
740 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
741 case HAL_PIXEL_FORMAT_BLOB:
742 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
743 case HAL_PIXEL_FORMAT_YCbCr_420_888:
744 case HAL_PIXEL_FORMAT_YCbCr_422_888:
745 case HAL_PIXEL_FORMAT_YCbCr_444_888:
746 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
747 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
748 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
749 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
750 case HAL_PIXEL_FORMAT_YCbCr_422_I:
751 return true;
752 default:
753 return false;
754 }
755}
756
Shuzhen Wang758c2152017-01-10 18:26:18 -0800757binder::Status CameraDeviceClient::createSurfaceFromGbp(
758 OutputStreamInfo& streamInfo, bool isStreamInfoValid,
759 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
760
761 // bufferProducer must be non-null
762 if (gbp == nullptr) {
763 String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
764 ALOGW("%s: %s", __FUNCTION__, msg.string());
765 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
766 }
767 // HACK b/10949105
768 // Query consumer usage bits to set async operation mode for
769 // GLConsumer using controlledByApp parameter.
770 bool useAsync = false;
771 int32_t consumerUsage;
772 status_t err;
773 if ((err = gbp->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
774 &consumerUsage)) != OK) {
775 String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
776 mCameraIdStr.string(), strerror(-err), err);
777 ALOGE("%s: %s", __FUNCTION__, msg.string());
778 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
779 }
780 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
781 ALOGW("%s: Camera %s with consumer usage flag: 0x%x: Forcing asynchronous mode for stream",
782 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
783 useAsync = true;
784 }
785
786 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
787 GRALLOC_USAGE_RENDERSCRIPT;
788 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
789 GraphicBuffer::USAGE_HW_TEXTURE |
790 GraphicBuffer::USAGE_HW_COMPOSER;
791 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
792 (consumerUsage & allowedFlags) != 0;
793
794 surface = new Surface(gbp, useAsync);
795 ANativeWindow *anw = surface.get();
796
797 int width, height, format;
798 android_dataspace dataSpace;
799 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
800 String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
801 mCameraIdStr.string(), strerror(-err), err);
802 ALOGE("%s: %s", __FUNCTION__, msg.string());
803 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
804 }
805 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
806 String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
807 mCameraIdStr.string(), strerror(-err), err);
808 ALOGE("%s: %s", __FUNCTION__, msg.string());
809 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
810 }
811 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
812 String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
813 mCameraIdStr.string(), strerror(-err), err);
814 ALOGE("%s: %s", __FUNCTION__, msg.string());
815 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
816 }
817 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
818 reinterpret_cast<int*>(&dataSpace))) != OK) {
819 String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
820 mCameraIdStr.string(), strerror(-err), err);
821 ALOGE("%s: %s", __FUNCTION__, msg.string());
822 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
823 }
824
825 // FIXME: remove this override since the default format should be
826 // IMPLEMENTATION_DEFINED. b/9487482
827 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
828 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
829 ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
830 __FUNCTION__, mCameraIdStr.string(), format);
831 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
832 }
833 // Round dimensions to the nearest dimensions available for this format
834 if (flexibleConsumer && isPublicFormat(format) &&
835 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
836 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
837 String8 msg = String8::format("Camera %s: No supported stream configurations with "
838 "format %#x defined, failed to create output stream",
839 mCameraIdStr.string(), format);
840 ALOGE("%s: %s", __FUNCTION__, msg.string());
841 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
842 }
843
844 if (!isStreamInfoValid) {
845 streamInfo.width = width;
846 streamInfo.height = height;
847 streamInfo.format = format;
848 streamInfo.dataSpace = dataSpace;
849 streamInfo.consumerUsage = consumerUsage;
850 return binder::Status::ok();
851 }
852 if (width != streamInfo.width) {
853 String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
854 mCameraIdStr.string(), width, streamInfo.width);
855 ALOGE("%s: %s", __FUNCTION__, msg.string());
856 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
857 }
858 if (height != streamInfo.height) {
859 String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
860 mCameraIdStr.string(), height, streamInfo.height);
861 ALOGE("%s: %s", __FUNCTION__, msg.string());
862 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
863 }
864 if (format != streamInfo.format) {
865 String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
866 mCameraIdStr.string(), format, streamInfo.format);
867 ALOGE("%s: %s", __FUNCTION__, msg.string());
868 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
869 }
Shuzhen Wange8ecda92017-02-20 17:10:28 -0800870 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
871 if (dataSpace != streamInfo.dataSpace) {
872 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
873 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
874 ALOGE("%s: %s", __FUNCTION__, msg.string());
875 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
876 }
877 //At the native side, there isn't a way to check whether 2 surfaces come from the same
878 //surface class type. Use usage flag to approximate the comparison.
879 if (consumerUsage != streamInfo.consumerUsage) {
880 String8 msg = String8::format(
881 "Camera %s:Surface usage flag doesn't match 0x%x vs 0x%x",
882 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
883 ALOGE("%s: %s", __FUNCTION__, msg.string());
884 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
885 }
Shuzhen Wang758c2152017-01-10 18:26:18 -0800886 }
887 return binder::Status::ok();
888}
889
Ruben Brunkbba75572014-11-20 17:29:50 -0800890bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800891 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800892 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
893
894 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800895 (dataSpace == HAL_DATASPACE_DEPTH) ?
896 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800897 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
898
899 int32_t bestWidth = -1;
900 int32_t bestHeight = -1;
901
902 // Iterate through listed stream configurations and find the one with the smallest euclidean
903 // distance from the given dimensions for the given format.
904 for (size_t i = 0; i < streamConfigs.count; i += 4) {
905 int32_t fmt = streamConfigs.data.i32[i];
906 int32_t w = streamConfigs.data.i32[i + 1];
907 int32_t h = streamConfigs.data.i32[i + 2];
908
909 // Ignore input/output type for now
910 if (fmt == format) {
911 if (w == width && h == height) {
912 bestWidth = width;
913 bestHeight = height;
914 break;
915 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
916 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
917 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
918 bestWidth = w;
919 bestHeight = h;
920 }
921 }
922 }
923
924 if (bestWidth == -1) {
925 // Return false if no configurations for this format were listed
926 return false;
927 }
928
929 // Set the outputs to the closet width/height
930 if (outWidth != NULL) {
931 *outWidth = bestWidth;
932 }
933 if (outHeight != NULL) {
934 *outHeight = bestHeight;
935 }
936
937 // Return true if at least one configuration for this format was listed
938 return true;
939}
940
941int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
942 int64_t d0 = x0 - x1;
943 int64_t d1 = y0 - y1;
944 return d0 * d0 + d1 * d1;
945}
946
Igor Murashkine7ee7632013-06-11 18:10:18 -0700947// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800948binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
949 /*out*/
950 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700951{
952 ATRACE_CALL();
953 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
954
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800955 binder::Status res;
956 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700957
958 Mutex::Autolock icl(mBinderSerializationLock);
959
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800960 if (!mDevice.get()) {
961 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
962 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700963
964 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800965 status_t err;
966 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700967 request != NULL) {
968
969 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800970 } else if (err == BAD_VALUE) {
971 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800972 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
973 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800974
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800975 } else {
976 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800977 "Camera %s: Error creating default request for template %d: %s (%d)",
978 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700979 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700980 return res;
981}
982
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800983binder::Status CameraDeviceClient::getCameraInfo(
984 /*out*/
985 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700986{
987 ATRACE_CALL();
988 ALOGV("%s", __FUNCTION__);
989
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800990 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700991
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800992 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700993
994 Mutex::Autolock icl(mBinderSerializationLock);
995
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800996 if (!mDevice.get()) {
997 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
998 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700999
Igor Murashkin099b4572013-07-12 17:52:16 -07001000 if (info != NULL) {
1001 *info = mDevice->info(); // static camera metadata
1002 // TODO: merge with device-specific camera metadata
1003 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001004
1005 return res;
1006}
1007
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001008binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001009{
1010 ATRACE_CALL();
1011 ALOGV("%s", __FUNCTION__);
1012
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001013 binder::Status res;
1014 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001015
1016 Mutex::Autolock icl(mBinderSerializationLock);
1017
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001018 if (!mDevice.get()) {
1019 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1020 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001021
1022 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001023 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001024 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001025 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001026 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1027 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001028 ALOGE("%s: %s", __FUNCTION__, msg.string());
1029 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001030 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001031 status_t err = mDevice->waitUntilDrained();
1032 if (err != OK) {
1033 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001034 "Camera %s: Error waiting to drain: %s (%d)",
1035 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001036 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001037 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001038 return res;
1039}
1040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001041binder::Status CameraDeviceClient::flush(
1042 /*out*/
1043 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001044 ATRACE_CALL();
1045 ALOGV("%s", __FUNCTION__);
1046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001047 binder::Status res;
1048 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001049
1050 Mutex::Autolock icl(mBinderSerializationLock);
1051
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001052 if (!mDevice.get()) {
1053 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1054 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001055
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001056 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001057 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001058 status_t err = mDevice->flush(lastFrameNumber);
1059 if (err != OK) {
1060 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001061 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001062 }
1063 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001064}
1065
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001066binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001067 ATRACE_CALL();
1068 ALOGV("%s", __FUNCTION__);
1069
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001070 binder::Status res;
1071 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001072
1073 Mutex::Autolock icl(mBinderSerializationLock);
1074
1075 // Guard against trying to prepare non-created streams
1076 ssize_t index = NAME_NOT_FOUND;
1077 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001078 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001079 index = i;
1080 break;
1081 }
1082 }
1083
1084 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001085 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1086 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001087 ALOGW("%s: %s", __FUNCTION__, msg.string());
1088 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001089 }
1090
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001091 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1092 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001093 status_t err = mDevice->prepare(streamId);
1094 if (err == BAD_VALUE) {
1095 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001096 "Camera %s: Stream %d has already been used, and cannot be prepared",
1097 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001098 } else if (err != OK) {
1099 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001100 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001101 strerror(-err), err);
1102 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001103 return res;
1104}
1105
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001106binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001107 ATRACE_CALL();
1108 ALOGV("%s", __FUNCTION__);
1109
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001110 binder::Status res;
1111 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001112
1113 Mutex::Autolock icl(mBinderSerializationLock);
1114
1115 // Guard against trying to prepare non-created streams
1116 ssize_t index = NAME_NOT_FOUND;
1117 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001118 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001119 index = i;
1120 break;
1121 }
1122 }
1123
1124 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001125 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1126 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001127 ALOGW("%s: %s", __FUNCTION__, msg.string());
1128 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001129 }
1130
1131 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001132 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1133 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001134 ALOGE("%s: %s", __FUNCTION__, msg.string());
1135 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001136 }
1137
1138 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1139 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001140 status_t err = mDevice->prepare(maxCount, streamId);
1141 if (err == BAD_VALUE) {
1142 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001143 "Camera %s: Stream %d has already been used, and cannot be prepared",
1144 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001145 } else if (err != OK) {
1146 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001147 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001148 strerror(-err), err);
1149 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001150
1151 return res;
1152}
1153
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001154binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001155 ATRACE_CALL();
1156 ALOGV("%s", __FUNCTION__);
1157
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001158 binder::Status res;
1159 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001160
1161 Mutex::Autolock icl(mBinderSerializationLock);
1162
1163 // Guard against trying to prepare non-created streams
1164 ssize_t index = NAME_NOT_FOUND;
1165 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001166 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001167 index = i;
1168 break;
1169 }
1170 }
1171
1172 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001173 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1174 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001175 ALOGW("%s: %s", __FUNCTION__, msg.string());
1176 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001177 }
1178
1179 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1180 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001181 status_t err = mDevice->tearDown(streamId);
1182 if (err == BAD_VALUE) {
1183 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001184 "Camera %s: Stream %d is still in use, cannot be torn down",
1185 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001186 } else if (err != OK) {
1187 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001188 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001189 strerror(-err), err);
1190 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001191
1192 return res;
1193}
1194
Shuzhen Wang758c2152017-01-10 18:26:18 -08001195binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001196 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1197 ATRACE_CALL();
1198
1199 binder::Status res;
1200 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1201
1202 Mutex::Autolock icl(mBinderSerializationLock);
1203
Shuzhen Wang0129d522016-10-30 22:43:41 -07001204 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1205 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001206
Shuzhen Wang0129d522016-10-30 22:43:41 -07001207 if (bufferProducers.size() == 0) {
1208 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001209 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1210 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001211
Shuzhen Wang758c2152017-01-10 18:26:18 -08001212 // streamId should be in mStreamMap if this stream already has a surface attached
1213 // to it. Otherwise, it should be in mDeferredStreams.
1214 bool streamIdConfigured = false;
1215 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1216 for (size_t i = 0; i < mStreamMap.size(); i++) {
1217 if (mStreamMap.valueAt(i).streamId() == streamId) {
1218 streamIdConfigured = true;
1219 break;
1220 }
Zhijun He5d677d12016-05-29 16:52:39 -07001221 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001222 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1223 if (streamId == mDeferredStreams[i]) {
1224 deferredStreamIndex = i;
1225 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001226 }
1227
Shuzhen Wang758c2152017-01-10 18:26:18 -08001228 }
1229 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1230 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1231 "(ID %d)", mCameraIdStr.string(), streamId);
1232 ALOGW("%s: %s", __FUNCTION__, msg.string());
1233 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001234 }
1235
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001236 if (mStreamInfoMap[streamId].finalized) {
1237 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1238 " on stream ID %d", mCameraIdStr.string(), streamId);
1239 ALOGW("%s: %s", __FUNCTION__, msg.string());
1240 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1241 }
1242
Zhijun He5d677d12016-05-29 16:52:39 -07001243 if (!mDevice.get()) {
1244 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1245 }
1246
Shuzhen Wang758c2152017-01-10 18:26:18 -08001247 std::vector<sp<Surface>> consumerSurfaces;
1248 std::vector<size_t> consumerSurfaceIds;
1249 size_t surfaceId = 0;
1250 for (auto& bufferProducer : bufferProducers) {
1251 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001252 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1253 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001254 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001255 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001256 surfaceId++;
1257 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001258 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001259
1260 sp<Surface> surface;
1261 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1262 surface, bufferProducer);
1263
1264 if (!res.isOk())
1265 return res;
1266
1267 consumerSurfaces.push_back(surface);
1268 consumerSurfaceIds.push_back(surfaceId);
1269 surfaceId++;
Zhijun He5d677d12016-05-29 16:52:39 -07001270 }
1271
Shuzhen Wanga81ce342017-04-13 15:18:36 -07001272 // Gracefully handle case where finalizeOutputConfigurations is called
1273 // without any new surface.
1274 if (consumerSurfaces.size() == 0) {
1275 mStreamInfoMap[streamId].finalized = true;
1276 return res;
1277 }
1278
Zhijun He5d677d12016-05-29 16:52:39 -07001279 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001280 status_t err;
1281 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces);
Zhijun He5d677d12016-05-29 16:52:39 -07001282 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001283 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1284 sp<IBinder> binder = IInterface::asBinder(
1285 consumerSurfaces[i]->getIGraphicBufferProducer());
1286 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %zu", __FUNCTION__,
1287 binder.get(), streamId, consumerSurfaceIds[i]);
1288 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1289 }
1290 if (deferredStreamIndex != NAME_NOT_FOUND) {
1291 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001292 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001293 mStreamInfoMap[streamId].finalized = true;
Zhijun He5d677d12016-05-29 16:52:39 -07001294 } else if (err == NO_INIT) {
1295 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001296 "Camera %s: Deferred surface is invalid: %s (%d)",
1297 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001298 } else {
1299 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001300 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1301 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001302 }
1303
1304 return res;
1305}
1306
Igor Murashkine7ee7632013-06-11 18:10:18 -07001307status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001308 return BasicClient::dump(fd, args);
1309}
1310
1311status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001312 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001313 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001314 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001315 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001316 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001317
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001318 dprintf(fd, " State:\n");
1319 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001320 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001321 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001322 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001323 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001324 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001325 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001326 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001327 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001328 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001329 mStreamMap.valueAt(i).streamId(),
1330 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001331 }
Zhijun He5d677d12016-05-29 16:52:39 -07001332 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001333 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001334 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001335 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001336 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001337 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001338 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001339 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001340 // TODO: print dynamic/request section from most recent requests
1341 mFrameProcessor->dump(fd, args);
1342
1343 return dumpDevice(fd, args);
1344}
1345
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001346void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001347 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001348 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001349 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001350
1351 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001352 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001353 }
1354}
1355
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001356void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1357 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1358
1359 if (remoteCb != 0) {
1360 remoteCb->onRepeatingRequestError(lastFrameNumber);
1361 }
1362
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001363 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001364 mStreamingRequestId = REQUEST_ID_NONE;
1365}
1366
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001367void CameraDeviceClient::notifyIdle() {
1368 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001369 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001370
1371 if (remoteCb != 0) {
1372 remoteCb->onDeviceIdle();
1373 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001374 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375}
1376
Jianing Weicb0652e2014-03-12 18:29:36 -07001377void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001378 nsecs_t timestamp) {
1379 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001380 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001381 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001382 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001383 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001384 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001385}
1386
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001387void CameraDeviceClient::notifyPrepared(int streamId) {
1388 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001389 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001390 if (remoteCb != 0) {
1391 remoteCb->onPrepared(streamId);
1392 }
1393}
1394
Shuzhen Wang9d066012016-09-30 11:30:20 -07001395void CameraDeviceClient::notifyRequestQueueEmpty() {
1396 // Thread safe. Don't bother locking.
1397 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1398 if (remoteCb != 0) {
1399 remoteCb->onRequestQueueEmpty();
1400 }
1401}
1402
Igor Murashkine7ee7632013-06-11 18:10:18 -07001403void CameraDeviceClient::detachDevice() {
1404 if (mDevice == 0) return;
1405
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001406 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001407
1408 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1409 FRAME_PROCESSOR_LISTENER_MAX_ID,
1410 /*listener*/this);
1411 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001412 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001413 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001414 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001415
1416 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1417 {
1418 mDevice->clearStreamingRequest();
1419
1420 status_t code;
1421 if ((code = mDevice->waitUntilDrained()) != OK) {
1422 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1423 code);
1424 }
1425 }
1426
1427 Camera2ClientBase::detachDevice();
1428}
1429
1430/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001431void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001432 ATRACE_CALL();
1433 ALOGV("%s", __FUNCTION__);
1434
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001435 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001437 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001438 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001439 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001440}
1441
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001442binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001443 if (mDisconnected) {
1444 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1445 "The camera device has been disconnected");
1446 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001447 status_t res = checkPid(checkLocation);
1448 return (res == OK) ? binder::Status::ok() :
1449 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1450 "Attempt to use camera from a different process than original client");
1451}
1452
Igor Murashkine7ee7632013-06-11 18:10:18 -07001453// TODO: move to Camera2ClientBase
1454bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1455
1456 const int pid = IPCThreadState::self()->getCallingPid();
1457 const int selfPid = getpid();
1458 camera_metadata_entry_t entry;
1459
1460 /**
1461 * Mixin default important security values
1462 * - android.led.transmit = defaulted ON
1463 */
1464 CameraMetadata staticInfo = mDevice->info();
1465 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1466 for(size_t i = 0; i < entry.count; ++i) {
1467 uint8_t led = entry.data.u8[i];
1468
1469 switch(led) {
1470 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1471 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1472 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1473 metadata.update(ANDROID_LED_TRANSMIT,
1474 &transmitDefault, 1);
1475 }
1476 break;
1477 }
1478 }
1479 }
1480
1481 // We can do anything!
1482 if (pid == selfPid) {
1483 return true;
1484 }
1485
1486 /**
1487 * Permission check special fields in the request
1488 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1489 */
1490 entry = metadata.find(ANDROID_LED_TRANSMIT);
1491 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1492 String16 permissionString =
1493 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1494 if (!checkCallingPermission(permissionString)) {
1495 const int uid = IPCThreadState::self()->getCallingUid();
1496 ALOGE("Permission Denial: "
1497 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1498 return false;
1499 }
1500 }
1501
1502 return true;
1503}
1504
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001505status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1506 ALOGV("%s: begin", __FUNCTION__);
1507
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001508 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001509 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001510}
1511
Igor Murashkine7ee7632013-06-11 18:10:18 -07001512} // namespace android