blob: 4428f754cb1b3a7b19538fff0361d7e8d28b26ba [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 }
870 if (dataSpace != streamInfo.dataSpace) {
871 String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
872 mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
873 ALOGE("%s: %s", __FUNCTION__, msg.string());
874 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
875 }
876 //At the native side, there isn't a way to check whether 2 surfaces come from the same
877 //surface class type. Use usage flag to approximate the comparison. Treat
878 //different preview surface usage flags as the same.
879 int32_t previewUsageMask =
880 GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_HW_COMPOSER;
881 if ((consumerUsage & ~previewUsageMask) != (streamInfo.consumerUsage & ~previewUsageMask)) {
882 String8 msg = String8::format(
883 "Camera %s:Surface usage flag doesn't match 0x%x vs 0x%x",
884 mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
885 ALOGE("%s: %s", __FUNCTION__, msg.string());
886 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
887 }
888 return binder::Status::ok();
889}
890
Ruben Brunkbba75572014-11-20 17:29:50 -0800891bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800892 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800893 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
894
895 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800896 (dataSpace == HAL_DATASPACE_DEPTH) ?
897 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800898 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
899
900 int32_t bestWidth = -1;
901 int32_t bestHeight = -1;
902
903 // Iterate through listed stream configurations and find the one with the smallest euclidean
904 // distance from the given dimensions for the given format.
905 for (size_t i = 0; i < streamConfigs.count; i += 4) {
906 int32_t fmt = streamConfigs.data.i32[i];
907 int32_t w = streamConfigs.data.i32[i + 1];
908 int32_t h = streamConfigs.data.i32[i + 2];
909
910 // Ignore input/output type for now
911 if (fmt == format) {
912 if (w == width && h == height) {
913 bestWidth = width;
914 bestHeight = height;
915 break;
916 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
917 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
918 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
919 bestWidth = w;
920 bestHeight = h;
921 }
922 }
923 }
924
925 if (bestWidth == -1) {
926 // Return false if no configurations for this format were listed
927 return false;
928 }
929
930 // Set the outputs to the closet width/height
931 if (outWidth != NULL) {
932 *outWidth = bestWidth;
933 }
934 if (outHeight != NULL) {
935 *outHeight = bestHeight;
936 }
937
938 // Return true if at least one configuration for this format was listed
939 return true;
940}
941
942int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
943 int64_t d0 = x0 - x1;
944 int64_t d1 = y0 - y1;
945 return d0 * d0 + d1 * d1;
946}
947
Igor Murashkine7ee7632013-06-11 18:10:18 -0700948// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800949binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
950 /*out*/
951 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700952{
953 ATRACE_CALL();
954 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
955
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800956 binder::Status res;
957 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700958
959 Mutex::Autolock icl(mBinderSerializationLock);
960
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800961 if (!mDevice.get()) {
962 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
963 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700964
965 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800966 status_t err;
967 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700968 request != NULL) {
969
970 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800971 } else if (err == BAD_VALUE) {
972 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800973 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
974 mCameraIdStr.string(), templateId, strerror(-err), err);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800975
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800976 } else {
977 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800978 "Camera %s: Error creating default request for template %d: %s (%d)",
979 mCameraIdStr.string(), templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700980 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700981 return res;
982}
983
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800984binder::Status CameraDeviceClient::getCameraInfo(
985 /*out*/
986 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700987{
988 ATRACE_CALL();
989 ALOGV("%s", __FUNCTION__);
990
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800991 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700992
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800993 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700994
995 Mutex::Autolock icl(mBinderSerializationLock);
996
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800997 if (!mDevice.get()) {
998 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
999 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001000
Igor Murashkin099b4572013-07-12 17:52:16 -07001001 if (info != NULL) {
1002 *info = mDevice->info(); // static camera metadata
1003 // TODO: merge with device-specific camera metadata
1004 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001005
1006 return res;
1007}
1008
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001009binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -07001010{
1011 ATRACE_CALL();
1012 ALOGV("%s", __FUNCTION__);
1013
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001014 binder::Status res;
1015 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -07001016
1017 Mutex::Autolock icl(mBinderSerializationLock);
1018
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001019 if (!mDevice.get()) {
1020 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1021 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001022
1023 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001024 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001025 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001026 String8 msg = String8::format(
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001027 "Camera %s: Try to waitUntilIdle when there are active streaming requests",
1028 mCameraIdStr.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001029 ALOGE("%s: %s", __FUNCTION__, msg.string());
1030 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -07001031 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001032 status_t err = mDevice->waitUntilDrained();
1033 if (err != OK) {
1034 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001035 "Camera %s: Error waiting to drain: %s (%d)",
1036 mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001037 }
Zhijun He2ab500c2013-07-23 08:02:53 -07001038 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -07001039 return res;
1040}
1041
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001042binder::Status CameraDeviceClient::flush(
1043 /*out*/
1044 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001045 ATRACE_CALL();
1046 ALOGV("%s", __FUNCTION__);
1047
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001048 binder::Status res;
1049 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001050
1051 Mutex::Autolock icl(mBinderSerializationLock);
1052
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001053 if (!mDevice.get()) {
1054 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1055 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001056
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001057 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001058 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001059 status_t err = mDevice->flush(lastFrameNumber);
1060 if (err != OK) {
1061 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001062 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001063 }
1064 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001065}
1066
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001067binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001068 ATRACE_CALL();
1069 ALOGV("%s", __FUNCTION__);
1070
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001071 binder::Status res;
1072 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001073
1074 Mutex::Autolock icl(mBinderSerializationLock);
1075
1076 // Guard against trying to prepare non-created streams
1077 ssize_t index = NAME_NOT_FOUND;
1078 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001079 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001080 index = i;
1081 break;
1082 }
1083 }
1084
1085 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001086 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1087 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001088 ALOGW("%s: %s", __FUNCTION__, msg.string());
1089 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001090 }
1091
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001092 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1093 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001094 status_t err = mDevice->prepare(streamId);
1095 if (err == BAD_VALUE) {
1096 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001097 "Camera %s: Stream %d has already been used, and cannot be prepared",
1098 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001099 } else if (err != OK) {
1100 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001101 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001102 strerror(-err), err);
1103 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001104 return res;
1105}
1106
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001107binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001108 ATRACE_CALL();
1109 ALOGV("%s", __FUNCTION__);
1110
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001111 binder::Status res;
1112 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -07001113
1114 Mutex::Autolock icl(mBinderSerializationLock);
1115
1116 // Guard against trying to prepare non-created streams
1117 ssize_t index = NAME_NOT_FOUND;
1118 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001119 if (streamId == mStreamMap.valueAt(i).streamId()) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001120 index = i;
1121 break;
1122 }
1123 }
1124
1125 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001126 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1127 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001128 ALOGW("%s: %s", __FUNCTION__, msg.string());
1129 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001130 }
1131
1132 if (maxCount <= 0) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001133 String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
1134 mCameraIdStr.string(), maxCount);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001135 ALOGE("%s: %s", __FUNCTION__, msg.string());
1136 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -07001137 }
1138
1139 // Also returns BAD_VALUE if stream ID was not valid, or stream already
1140 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001141 status_t err = mDevice->prepare(maxCount, streamId);
1142 if (err == BAD_VALUE) {
1143 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001144 "Camera %s: Stream %d has already been used, and cannot be prepared",
1145 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001146 } else if (err != OK) {
1147 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001148 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001149 strerror(-err), err);
1150 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001151
1152 return res;
1153}
1154
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001155binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001156 ATRACE_CALL();
1157 ALOGV("%s", __FUNCTION__);
1158
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001159 binder::Status res;
1160 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001161
1162 Mutex::Autolock icl(mBinderSerializationLock);
1163
1164 // Guard against trying to prepare non-created streams
1165 ssize_t index = NAME_NOT_FOUND;
1166 for (size_t i = 0; i < mStreamMap.size(); ++i) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001167 if (streamId == mStreamMap.valueAt(i).streamId()) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001168 index = i;
1169 break;
1170 }
1171 }
1172
1173 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001174 String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
1175 "with that ID exists", mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001176 ALOGW("%s: %s", __FUNCTION__, msg.string());
1177 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001178 }
1179
1180 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1181 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001182 status_t err = mDevice->tearDown(streamId);
1183 if (err == BAD_VALUE) {
1184 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001185 "Camera %s: Stream %d is still in use, cannot be torn down",
1186 mCameraIdStr.string(), streamId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001187 } else if (err != OK) {
1188 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001189 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001190 strerror(-err), err);
1191 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001192
1193 return res;
1194}
1195
Shuzhen Wang758c2152017-01-10 18:26:18 -08001196binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
Zhijun He5d677d12016-05-29 16:52:39 -07001197 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1198 ATRACE_CALL();
1199
1200 binder::Status res;
1201 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1202
1203 Mutex::Autolock icl(mBinderSerializationLock);
1204
Shuzhen Wang0129d522016-10-30 22:43:41 -07001205 const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
1206 outputConfiguration.getGraphicBufferProducers();
Zhijun He5d677d12016-05-29 16:52:39 -07001207
Shuzhen Wang0129d522016-10-30 22:43:41 -07001208 if (bufferProducers.size() == 0) {
1209 ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
Zhijun He5d677d12016-05-29 16:52:39 -07001210 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1211 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001212
Shuzhen Wang758c2152017-01-10 18:26:18 -08001213 // streamId should be in mStreamMap if this stream already has a surface attached
1214 // to it. Otherwise, it should be in mDeferredStreams.
1215 bool streamIdConfigured = false;
1216 ssize_t deferredStreamIndex = NAME_NOT_FOUND;
1217 for (size_t i = 0; i < mStreamMap.size(); i++) {
1218 if (mStreamMap.valueAt(i).streamId() == streamId) {
1219 streamIdConfigured = true;
1220 break;
1221 }
Zhijun He5d677d12016-05-29 16:52:39 -07001222 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001223 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1224 if (streamId == mDeferredStreams[i]) {
1225 deferredStreamIndex = i;
1226 break;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001227 }
1228
Shuzhen Wang758c2152017-01-10 18:26:18 -08001229 }
1230 if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
1231 String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
1232 "(ID %d)", mCameraIdStr.string(), streamId);
1233 ALOGW("%s: %s", __FUNCTION__, msg.string());
1234 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Zhijun He5d677d12016-05-29 16:52:39 -07001235 }
1236
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001237 if (mStreamInfoMap[streamId].finalized) {
1238 String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
1239 " on stream ID %d", mCameraIdStr.string(), streamId);
1240 ALOGW("%s: %s", __FUNCTION__, msg.string());
1241 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1242 }
1243
Zhijun He5d677d12016-05-29 16:52:39 -07001244 if (!mDevice.get()) {
1245 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1246 }
1247
Shuzhen Wang758c2152017-01-10 18:26:18 -08001248 std::vector<sp<Surface>> consumerSurfaces;
1249 std::vector<size_t> consumerSurfaceIds;
1250 size_t surfaceId = 0;
1251 for (auto& bufferProducer : bufferProducers) {
1252 // Don't create multiple streams for the same target surface
Zhijun He5d677d12016-05-29 16:52:39 -07001253 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1254 if (index != NAME_NOT_FOUND) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001255 ALOGV("Camera %s: Surface already has a stream created "
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001256 " for it (ID %zd)", mCameraIdStr.string(), index);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001257 surfaceId++;
1258 continue;
Zhijun He5d677d12016-05-29 16:52:39 -07001259 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08001260
1261 sp<Surface> surface;
1262 res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
1263 surface, bufferProducer);
1264
1265 if (!res.isOk())
1266 return res;
1267
1268 consumerSurfaces.push_back(surface);
1269 consumerSurfaceIds.push_back(surfaceId);
1270 surfaceId++;
Zhijun He5d677d12016-05-29 16:52:39 -07001271 }
1272
Zhijun He5d677d12016-05-29 16:52:39 -07001273 // Finish the deferred stream configuration with the surface.
Shuzhen Wang758c2152017-01-10 18:26:18 -08001274 status_t err;
1275 err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces);
Zhijun He5d677d12016-05-29 16:52:39 -07001276 if (err == OK) {
Shuzhen Wang758c2152017-01-10 18:26:18 -08001277 for (size_t i = 0; i < consumerSurfaces.size(); i++) {
1278 sp<IBinder> binder = IInterface::asBinder(
1279 consumerSurfaces[i]->getIGraphicBufferProducer());
1280 ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %zu", __FUNCTION__,
1281 binder.get(), streamId, consumerSurfaceIds[i]);
1282 mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
1283 }
1284 if (deferredStreamIndex != NAME_NOT_FOUND) {
1285 mDeferredStreams.removeItemsAt(deferredStreamIndex);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001286 }
Shuzhen Wang88fd0052017-02-09 16:40:07 -08001287 mStreamInfoMap[streamId].finalized = true;
Zhijun He5d677d12016-05-29 16:52:39 -07001288 } else if (err == NO_INIT) {
1289 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001290 "Camera %s: Deferred surface is invalid: %s (%d)",
1291 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001292 } else {
1293 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001294 "Camera %s: Error setting output stream deferred surface: %s (%d)",
1295 mCameraIdStr.string(), strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -07001296 }
1297
1298 return res;
1299}
1300
Igor Murashkine7ee7632013-06-11 18:10:18 -07001301status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001302 return BasicClient::dump(fd, args);
1303}
1304
1305status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001306 dprintf(fd, " CameraDeviceClient[%s] (%p) dump:\n",
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001307 mCameraIdStr.string(),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001308 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001309 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001310 dprintf(fd, " Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001311
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001312 dprintf(fd, " State:\n");
1313 dprintf(fd, " Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001314 if (mInputStream.configured) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001315 dprintf(fd, " Current input stream ID: %d\n", mInputStream.id);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001316 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001317 dprintf(fd, " No input stream configured.\n");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001318 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001319 if (!mStreamMap.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001320 dprintf(fd, " Current output stream/surface IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001321 for (size_t i = 0; i < mStreamMap.size(); i++) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001322 dprintf(fd, " Stream %d Surface %d\n",
Shuzhen Wang0129d522016-10-30 22:43:41 -07001323 mStreamMap.valueAt(i).streamId(),
1324 mStreamMap.valueAt(i).surfaceId());
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001325 }
Zhijun He5d677d12016-05-29 16:52:39 -07001326 } else if (!mDeferredStreams.isEmpty()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001327 dprintf(fd, " Current deferred surface output stream IDs:\n");
Zhijun He5d677d12016-05-29 16:52:39 -07001328 for (auto& streamId : mDeferredStreams) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001329 dprintf(fd, " Stream %d\n", streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001330 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001331 } else {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -08001332 dprintf(fd, " No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001333 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001334 // TODO: print dynamic/request section from most recent requests
1335 mFrameProcessor->dump(fd, args);
1336
1337 return dumpDevice(fd, args);
1338}
1339
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001340void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001341 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001342 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001343 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001344
1345 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001346 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001347 }
1348}
1349
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001350void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1351 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1352
1353 if (remoteCb != 0) {
1354 remoteCb->onRepeatingRequestError(lastFrameNumber);
1355 }
1356
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001357 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001358 mStreamingRequestId = REQUEST_ID_NONE;
1359}
1360
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001361void CameraDeviceClient::notifyIdle() {
1362 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001363 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001364
1365 if (remoteCb != 0) {
1366 remoteCb->onDeviceIdle();
1367 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001368 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001369}
1370
Jianing Weicb0652e2014-03-12 18:29:36 -07001371void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001372 nsecs_t timestamp) {
1373 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001374 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001375 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001376 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001377 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001378 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379}
1380
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001381void CameraDeviceClient::notifyPrepared(int streamId) {
1382 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001383 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001384 if (remoteCb != 0) {
1385 remoteCb->onPrepared(streamId);
1386 }
1387}
1388
Shuzhen Wang9d066012016-09-30 11:30:20 -07001389void CameraDeviceClient::notifyRequestQueueEmpty() {
1390 // Thread safe. Don't bother locking.
1391 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1392 if (remoteCb != 0) {
1393 remoteCb->onRequestQueueEmpty();
1394 }
1395}
1396
Igor Murashkine7ee7632013-06-11 18:10:18 -07001397void CameraDeviceClient::detachDevice() {
1398 if (mDevice == 0) return;
1399
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001400 ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001401
1402 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1403 FRAME_PROCESSOR_LISTENER_MAX_ID,
1404 /*listener*/this);
1405 mFrameProcessor->requestExit();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001406 ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001407 mFrameProcessor->join();
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -08001408 ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001409
1410 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1411 {
1412 mDevice->clearStreamingRequest();
1413
1414 status_t code;
1415 if ((code = mDevice->waitUntilDrained()) != OK) {
1416 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1417 code);
1418 }
1419 }
1420
1421 Camera2ClientBase::detachDevice();
1422}
1423
1424/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001425void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001426 ATRACE_CALL();
1427 ALOGV("%s", __FUNCTION__);
1428
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001429 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001430 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001431 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001432 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001433 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001434}
1435
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001436binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001437 if (mDisconnected) {
1438 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1439 "The camera device has been disconnected");
1440 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001441 status_t res = checkPid(checkLocation);
1442 return (res == OK) ? binder::Status::ok() :
1443 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1444 "Attempt to use camera from a different process than original client");
1445}
1446
Igor Murashkine7ee7632013-06-11 18:10:18 -07001447// TODO: move to Camera2ClientBase
1448bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1449
1450 const int pid = IPCThreadState::self()->getCallingPid();
1451 const int selfPid = getpid();
1452 camera_metadata_entry_t entry;
1453
1454 /**
1455 * Mixin default important security values
1456 * - android.led.transmit = defaulted ON
1457 */
1458 CameraMetadata staticInfo = mDevice->info();
1459 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1460 for(size_t i = 0; i < entry.count; ++i) {
1461 uint8_t led = entry.data.u8[i];
1462
1463 switch(led) {
1464 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1465 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1466 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1467 metadata.update(ANDROID_LED_TRANSMIT,
1468 &transmitDefault, 1);
1469 }
1470 break;
1471 }
1472 }
1473 }
1474
1475 // We can do anything!
1476 if (pid == selfPid) {
1477 return true;
1478 }
1479
1480 /**
1481 * Permission check special fields in the request
1482 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1483 */
1484 entry = metadata.find(ANDROID_LED_TRANSMIT);
1485 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1486 String16 permissionString =
1487 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1488 if (!checkCallingPermission(permissionString)) {
1489 const int uid = IPCThreadState::self()->getCallingUid();
1490 ALOGE("Permission Denial: "
1491 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1492 return false;
1493 }
1494 }
1495
1496 return true;
1497}
1498
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001499status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1500 ALOGV("%s: begin", __FUNCTION__);
1501
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001502 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001503 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001504}
1505
Igor Murashkine7ee7632013-06-11 18:10:18 -07001506} // namespace android