blob: 316cfdac5739c50422e36910bbe487c1b951fd9e [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,
49 int cameraId,
50 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,
70 int cameraId,
71 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(),
Igor Murashkine7ee7632013-06-11 18:10:18 -070078 mRequestIdCounter(0) {
79
80 ATRACE_CALL();
81 ALOGI("CameraDeviceClient %d: Opened", cameraId);
82}
83
Yin-Chia Yehe074a932015-01-30 10:29:02 -080084status_t CameraDeviceClient::initialize(CameraModule *module)
Igor Murashkine7ee7632013-06-11 18:10:18 -070085{
86 ATRACE_CALL();
87 status_t res;
88
89 res = Camera2ClientBase::initialize(module);
90 if (res != OK) {
91 return res;
92 }
93
94 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070095 mFrameProcessor = new FrameProcessorBase(mDevice);
Igor Murashkine7ee7632013-06-11 18:10:18 -070096 threadName = String8::format("CDU-%d-FrameProc", mCameraId);
97 mFrameProcessor->run(threadName.string());
98
99 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
100 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800101 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700102 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700103
104 return OK;
105}
106
107CameraDeviceClient::~CameraDeviceClient() {
108}
109
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800110binder::Status CameraDeviceClient::submitRequest(
111 const hardware::camera2::CaptureRequest& request,
112 bool streaming,
113 /*out*/
114 hardware::camera2::utils::SubmitInfo *submitInfo) {
115 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
116 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700117}
118
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800119binder::Status CameraDeviceClient::submitRequestList(
120 const std::vector<hardware::camera2::CaptureRequest>& requests,
121 bool streaming,
122 /*out*/
123 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700124 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700125 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700126
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800127 binder::Status res = binder::Status::ok();
128 status_t err;
129 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
130 return res;
131 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700132
133 Mutex::Autolock icl(mBinderSerializationLock);
134
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800135 if (!mDevice.get()) {
136 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
137 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700138
139 if (requests.empty()) {
140 ALOGE("%s: Camera %d: Sent null request. Rejecting request.",
141 __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800142 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700143 }
144
145 List<const CameraMetadata> metadataRequestList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800146 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700147 uint32_t loopCounter = 0;
148
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800149 for (auto&& request: requests) {
150 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700151 if (!mInputStream.configured) {
152 ALOGE("%s: Camera %d: no input stream is configured.", __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800153 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
154 "No input configured for camera %d but request is for reprocessing",
155 mCameraId);
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700156 } else if (streaming) {
157 ALOGE("%s: Camera %d: streaming reprocess requests not supported.", __FUNCTION__,
158 mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800159 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
160 "Repeating reprocess requests not supported");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700161 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700162 }
163
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800164 CameraMetadata metadata(request.mMetadata);
Jianing Wei90e59c92014-03-12 18:29:36 -0700165 if (metadata.isEmpty()) {
166 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.",
167 __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800168 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
169 "Request settings are empty");
170 } else if (request.mSurfaceList.isEmpty()) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700171 ALOGE("%s: Camera %d: Requests must have at least one surface target. "
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800172 "Rejecting request.", __FUNCTION__, mCameraId);
173 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
174 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700175 }
176
177 if (!enforceRequestPermissions(metadata)) {
178 // Callee logs
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800179 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
180 "Caller does not have permission to change restricted controls");
Jianing Wei90e59c92014-03-12 18:29:36 -0700181 }
182
183 /**
184 * Write in the output stream IDs which we calculate from
185 * the capture request's list of surface targets
186 */
187 Vector<int32_t> outputStreamIds;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800188 outputStreamIds.setCapacity(request.mSurfaceList.size());
189 for (sp<Surface> surface : request.mSurfaceList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700190 if (surface == 0) continue;
191
192 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Marco Nelissenf8880202014-11-14 07:58:25 -0800193 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
Jianing Wei90e59c92014-03-12 18:29:36 -0700194
195 // Trying to submit request with surface that wasn't created
196 if (idx == NAME_NOT_FOUND) {
197 ALOGE("%s: Camera %d: Tried to submit a request with a surface that"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800198 " we have not called createStream on",
199 __FUNCTION__, mCameraId);
200 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
201 "Request targets Surface that is not part of current capture session");
Jianing Wei90e59c92014-03-12 18:29:36 -0700202 }
203
204 int streamId = mStreamMap.valueAt(idx);
205 outputStreamIds.push_back(streamId);
206 ALOGV("%s: Camera %d: Appending output stream %d to request",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800207 __FUNCTION__, mCameraId, streamId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700208 }
209
210 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
211 outputStreamIds.size());
212
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800213 if (request.mIsReprocess) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700214 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
215 }
216
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800217 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700218 loopCounter++; // loopCounter starts from 1
Mark Salyzyn50468412014-06-18 16:33:43 -0700219 ALOGV("%s: Camera %d: Creating request with ID %d (%d of %zu)",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800220 __FUNCTION__, mCameraId, submitInfo->mRequestId, loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700221
222 metadataRequestList.push_back(metadata);
223 }
224 mRequestIdCounter++;
225
226 if (streaming) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800227 err = mDevice->setStreamingRequestList(metadataRequestList, &(submitInfo->mLastFrameNumber));
228 if (err != OK) {
229 String8 msg = String8::format(
230 "Camera %d: Got error %s (%d) after trying to set streaming request",
231 mCameraId, strerror(-err), err);
232 ALOGE("%s: %s", __FUNCTION__, msg.string());
233 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
234 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700235 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800236 mStreamingRequestList.push_back(submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700237 }
238 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800239 err = mDevice->captureList(metadataRequestList, &(submitInfo->mLastFrameNumber));
240 if (err != OK) {
241 String8 msg = String8::format(
242 "Camera %d: Got error %s (%d) after trying to submit capture request",
243 mCameraId, strerror(-err), err);
244 ALOGE("%s: %s", __FUNCTION__, msg.string());
245 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
246 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700247 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800248 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700249 }
250
251 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700252 return res;
253}
254
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800255binder::Status CameraDeviceClient::cancelRequest(
256 int requestId,
257 /*out*/
258 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700259 ATRACE_CALL();
260 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
261
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800262 status_t err;
263 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700264
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800265 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700266
267 Mutex::Autolock icl(mBinderSerializationLock);
268
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800269 if (!mDevice.get()) {
270 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
271 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700272
273 Vector<int>::iterator it, end;
274 for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end();
275 it != end; ++it) {
276 if (*it == requestId) {
277 break;
278 }
279 }
280
281 if (it == end) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800282 String8 msg = String8::format("Camera %d: Did not find request ID %d in list of "
283 "streaming requests", mCameraId, requestId);
284 ALOGE("%s: %s", __FUNCTION__, msg.string());
285 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700286 }
287
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800288 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700289
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800290 if (err == OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700291 ALOGV("%s: Camera %d: Successfully cleared streaming request",
292 __FUNCTION__, mCameraId);
293 mStreamingRequestList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800294 } else {
295 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
296 "Camera %d: Error clearing streaming request: %s (%d)",
297 mCameraId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700298 }
299
300 return res;
301}
302
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800303binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700304 // TODO: Implement this.
Zhijun He1fa89992015-06-01 15:44:31 -0700305 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800306 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700307}
308
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800309binder::Status CameraDeviceClient::endConfigure(bool isConstrainedHighSpeed) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700310 ALOGV("%s: ending configure (%d input stream, %zu output streams)",
311 __FUNCTION__, mInputStream.configured ? 1 : 0, mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700312
Zhijun He0effd522016-03-04 10:22:27 -0800313 binder::Status res;
314 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
315
316 Mutex::Autolock icl(mBinderSerializationLock);
317
318 if (!mDevice.get()) {
319 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
320 }
321
Zhijun He1fa89992015-06-01 15:44:31 -0700322 // Sanitize the high speed session against necessary capability bit.
323 if (isConstrainedHighSpeed) {
324 CameraMetadata staticInfo = mDevice->info();
325 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
326 bool isConstrainedHighSpeedSupported = false;
327 for(size_t i = 0; i < entry.count; ++i) {
328 uint8_t capability = entry.data.u8[i];
329 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
330 isConstrainedHighSpeedSupported = true;
331 break;
332 }
333 }
334 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800335 String8 msg = String8::format(
336 "Camera %d: Try to create a constrained high speed configuration on a device"
337 " that doesn't support it.", mCameraId);
338 ALOGE("%s: %s", __FUNCTION__, msg.string());
339 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
340 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700341 }
342 }
343
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800344 status_t err = mDevice->configureStreams(isConstrainedHighSpeed);
345 if (err != OK) {
346 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
347 "Camera %d: Error configuring streams: %s (%d)",
348 mCameraId, strerror(-err), err);
349 }
350
351 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700352}
353
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800354binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700355 ATRACE_CALL();
356 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
357
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800358 binder::Status res;
359 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700360
361 Mutex::Autolock icl(mBinderSerializationLock);
362
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800363 if (!mDevice.get()) {
364 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
365 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700366
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700367 bool isInput = false;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700368 ssize_t index = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700369
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700370 if (mInputStream.configured && mInputStream.id == streamId) {
371 isInput = true;
372 } else {
373 // Guard against trying to delete non-created streams
374 for (size_t i = 0; i < mStreamMap.size(); ++i) {
375 if (streamId == mStreamMap.valueAt(i)) {
376 index = i;
377 break;
378 }
379 }
380
381 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800382 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no such "
383 "stream created yet", mCameraId, streamId);
384 ALOGW("%s: %s", __FUNCTION__, msg.string());
385 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700386 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700387 }
388
389 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800390 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700391
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800392 if (err != OK) {
393 String8 msg = String8::format("Camera %d: Unexpected error %s (%d) when deleting stream %d",
394 mCameraId, strerror(-err), err, streamId);
395 ALOGE("%s: %s", __FUNCTION__, msg.string());
396 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
397 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700398 if (isInput) {
399 mInputStream.configured = false;
400 } else {
401 mStreamMap.removeItemsAt(index);
402 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700403 }
404
405 return res;
406}
407
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800408binder::Status CameraDeviceClient::createStream(
409 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
410 /*out*/
411 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700412 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700413
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800414 binder::Status res;
415 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700416
417 Mutex::Autolock icl(mBinderSerializationLock);
418
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700419 sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700420 if (bufferProducer == NULL) {
421 ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800422 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700423 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800424 if (!mDevice.get()) {
425 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
426 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700427
428 // Don't create multiple streams for the same target surface
429 {
Marco Nelissenf8880202014-11-14 07:58:25 -0800430 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
Igor Murashkine7ee7632013-06-11 18:10:18 -0700431 if (index != NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800432 String8 msg = String8::format("Camera %d: Surface already has a stream created for it "
433 "(ID %zd)", mCameraId, index);
434 ALOGW("%s: %s", __FUNCTION__, msg.string());
435 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700436 }
437 }
438
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800439 status_t err;
440
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700441 // HACK b/10949105
442 // Query consumer usage bits to set async operation mode for
443 // GLConsumer using controlledByApp parameter.
444 bool useAsync = false;
445 int32_t consumerUsage;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800446 if ((err = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700447 &consumerUsage)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800448 String8 msg = String8::format("Camera %d: Failed to query Surface consumer usage: %s (%d)",
449 mCameraId, strerror(-err), err);
450 ALOGE("%s: %s", __FUNCTION__, msg.string());
451 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700452 }
453 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
454 ALOGW("%s: Camera %d: Forcing asynchronous mode for stream",
455 __FUNCTION__, mCameraId);
456 useAsync = true;
457 }
458
Ruben Brunkbba75572014-11-20 17:29:50 -0800459 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
460 GRALLOC_USAGE_RENDERSCRIPT;
461 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
462 GraphicBuffer::USAGE_HW_TEXTURE |
463 GraphicBuffer::USAGE_HW_COMPOSER;
464 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
465 (consumerUsage & allowedFlags) != 0;
466
Marco Nelissenf8880202014-11-14 07:58:25 -0800467 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700468 sp<Surface> surface = new Surface(bufferProducer, useAsync);
469 ANativeWindow *anw = surface.get();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700470
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800471 int width, height, format;
472 android_dataspace dataSpace;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700473
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800474 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
475 String8 msg = String8::format("Camera %d: Failed to query Surface width: %s (%d)",
476 mCameraId, strerror(-err), err);
477 ALOGE("%s: %s", __FUNCTION__, msg.string());
478 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700479 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800480 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
481 String8 msg = String8::format("Camera %d: Failed to query Surface height: %s (%d)",
482 mCameraId, strerror(-err), err);
483 ALOGE("%s: %s", __FUNCTION__, msg.string());
484 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700485 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800486 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
487 String8 msg = String8::format("Camera %d: Failed to query Surface format: %s (%d)",
488 mCameraId, strerror(-err), err);
489 ALOGE("%s: %s", __FUNCTION__, msg.string());
490 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700491 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800492 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800493 reinterpret_cast<int*>(&dataSpace))) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800494 String8 msg = String8::format("Camera %d: Failed to query Surface dataspace: %s (%d)",
495 mCameraId, strerror(-err), err);
496 ALOGE("%s: %s", __FUNCTION__, msg.string());
497 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800498 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700499
500 // FIXME: remove this override since the default format should be
501 // IMPLEMENTATION_DEFINED. b/9487482
Igor Murashkin15811012013-07-29 12:25:59 -0700502 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
503 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
Ruben Brunkbba75572014-11-20 17:29:50 -0800504 ALOGW("%s: Camera %d: Overriding format %#x to IMPLEMENTATION_DEFINED",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700505 __FUNCTION__, mCameraId, format);
506 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
507 }
508
Ruben Brunkbba75572014-11-20 17:29:50 -0800509 // Round dimensions to the nearest dimensions available for this format
510 if (flexibleConsumer && !CameraDeviceClient::roundBufferDimensionNearest(width, height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800511 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800512 String8 msg = String8::format("Camera %d: No supported stream configurations with "
513 "format %#x defined, failed to create output stream", mCameraId, format);
514 ALOGE("%s: %s", __FUNCTION__, msg.string());
515 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkbba75572014-11-20 17:29:50 -0800516 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700517
Zhijun He125684a2015-12-26 15:07:30 -0800518 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800519 err = mDevice->createStream(surface, width, height, format, dataSpace,
520 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
521 &streamId, outputConfiguration.getSurfaceSetID());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700522
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800523 if (err != OK) {
524 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
525 "Camera %d: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
526 mCameraId, width, height, format, dataSpace, strerror(-err), err);
527 } else {
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800528 mStreamMap.add(binder, streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700529
530 ALOGV("%s: Camera %d: Successfully created a new stream ID %d",
531 __FUNCTION__, mCameraId, streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700532
533 /**
534 * Set the stream transform flags to automatically
535 * rotate the camera stream for preview use cases.
536 */
537 int32_t transform = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800538 err = getRotationTransformLocked(&transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700539
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800540 if (err != OK) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700541 // Error logged by getRotationTransformLocked.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800542 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
543 "Unable to calculate rotation transform for new stream");
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700544 }
545
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800546 err = mDevice->setStreamTransform(streamId, transform);
547 if (err != OK) {
548 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
549 streamId);
550 ALOGE("%s: %s", __FUNCTION__, msg.string());
551 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700552 }
553
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800554 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700555 }
556
557 return res;
558}
559
Ruben Brunkbba75572014-11-20 17:29:50 -0800560
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800561binder::Status CameraDeviceClient::createInputStream(
562 int width, int height, int format,
563 /*out*/
564 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700565
566 ATRACE_CALL();
567 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
568
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800569 binder::Status res;
570 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700571
572 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800573
574 if (!mDevice.get()) {
575 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
576 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700577
578 if (mInputStream.configured) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800579 String8 msg = String8::format("Camera %d: Already has an input stream "
580 "configured (ID %zd)", mCameraId, mInputStream.id);
581 ALOGE("%s: %s", __FUNCTION__, msg.string() );
582 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700583 }
584
585 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800586 status_t err = mDevice->createInputStream(width, height, format, &streamId);
587 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700588 mInputStream.configured = true;
589 mInputStream.width = width;
590 mInputStream.height = height;
591 mInputStream.format = format;
592 mInputStream.id = streamId;
593
594 ALOGV("%s: Camera %d: Successfully created a new input stream ID %d",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800595 __FUNCTION__, mCameraId, streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700596
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800597 *newStreamId = streamId;
598 } else {
599 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
600 "Camera %d: Error creating new input stream: %s (%d)", mCameraId,
601 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700602 }
603
604 return res;
605}
606
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800607binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700608
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800609 binder::Status res;
610 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
611
612 if (inputSurface == NULL) {
613 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700614 }
615
616 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800617 if (!mDevice.get()) {
618 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
619 }
620 sp<IGraphicBufferProducer> producer;
621 status_t err = mDevice->getInputBufferProducer(&producer);
622 if (err != OK) {
623 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
624 "Camera %d: Error getting input Surface: %s (%d)",
625 mCameraId, strerror(-err), err);
626 } else {
627 inputSurface->name = String16("CameraInput");
628 inputSurface->graphicBufferProducer = producer;
629 }
630 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700631}
632
Ruben Brunkbba75572014-11-20 17:29:50 -0800633bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800634 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800635 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
636
637 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800638 (dataSpace == HAL_DATASPACE_DEPTH) ?
639 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800640 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
641
642 int32_t bestWidth = -1;
643 int32_t bestHeight = -1;
644
645 // Iterate through listed stream configurations and find the one with the smallest euclidean
646 // distance from the given dimensions for the given format.
647 for (size_t i = 0; i < streamConfigs.count; i += 4) {
648 int32_t fmt = streamConfigs.data.i32[i];
649 int32_t w = streamConfigs.data.i32[i + 1];
650 int32_t h = streamConfigs.data.i32[i + 2];
651
652 // Ignore input/output type for now
653 if (fmt == format) {
654 if (w == width && h == height) {
655 bestWidth = width;
656 bestHeight = height;
657 break;
658 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
659 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
660 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
661 bestWidth = w;
662 bestHeight = h;
663 }
664 }
665 }
666
667 if (bestWidth == -1) {
668 // Return false if no configurations for this format were listed
669 return false;
670 }
671
672 // Set the outputs to the closet width/height
673 if (outWidth != NULL) {
674 *outWidth = bestWidth;
675 }
676 if (outHeight != NULL) {
677 *outHeight = bestHeight;
678 }
679
680 // Return true if at least one configuration for this format was listed
681 return true;
682}
683
684int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
685 int64_t d0 = x0 - x1;
686 int64_t d1 = y0 - y1;
687 return d0 * d0 + d1 * d1;
688}
689
Igor Murashkine7ee7632013-06-11 18:10:18 -0700690// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800691binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
692 /*out*/
693 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700694{
695 ATRACE_CALL();
696 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
697
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800698 binder::Status res;
699 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700700
701 Mutex::Autolock icl(mBinderSerializationLock);
702
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800703 if (!mDevice.get()) {
704 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
705 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700706
707 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800708 status_t err;
709 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700710 request != NULL) {
711
712 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800713 } else if (err == BAD_VALUE) {
714 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
715 "Camera %d: Template ID %d is invalid or not supported: %s (%d)",
716 mCameraId, templateId, strerror(-err), err);
717
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800718 } else {
719 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
720 "Camera %d: Error creating default request for template %d: %s (%d)",
721 mCameraId, templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700722 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700723 return res;
724}
725
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800726binder::Status CameraDeviceClient::getCameraInfo(
727 /*out*/
728 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700729{
730 ATRACE_CALL();
731 ALOGV("%s", __FUNCTION__);
732
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800733 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700734
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800735 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700736
737 Mutex::Autolock icl(mBinderSerializationLock);
738
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800739 if (!mDevice.get()) {
740 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
741 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700742
Igor Murashkin099b4572013-07-12 17:52:16 -0700743 if (info != NULL) {
744 *info = mDevice->info(); // static camera metadata
745 // TODO: merge with device-specific camera metadata
746 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700747
748 return res;
749}
750
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800751binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -0700752{
753 ATRACE_CALL();
754 ALOGV("%s", __FUNCTION__);
755
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800756 binder::Status res;
757 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -0700758
759 Mutex::Autolock icl(mBinderSerializationLock);
760
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800761 if (!mDevice.get()) {
762 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
763 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700764
765 // FIXME: Also need check repeating burst.
766 if (!mStreamingRequestList.isEmpty()) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800767 String8 msg = String8::format(
768 "Camera %d: Try to waitUntilIdle when there are active streaming requests",
769 mCameraId);
770 ALOGE("%s: %s", __FUNCTION__, msg.string());
771 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -0700772 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800773 status_t err = mDevice->waitUntilDrained();
774 if (err != OK) {
775 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
776 "Camera %d: Error waiting to drain: %s (%d)",
777 mCameraId, strerror(-err), err);
778 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700779 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -0700780 return res;
781}
782
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800783binder::Status CameraDeviceClient::flush(
784 /*out*/
785 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700786 ATRACE_CALL();
787 ALOGV("%s", __FUNCTION__);
788
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800789 binder::Status res;
790 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700791
792 Mutex::Autolock icl(mBinderSerializationLock);
793
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800794 if (!mDevice.get()) {
795 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
796 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700797
Jianing Wei3c76fa32014-04-21 11:34:34 -0700798 mStreamingRequestList.clear();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800799 status_t err = mDevice->flush(lastFrameNumber);
800 if (err != OK) {
801 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
802 "Camera %d: Error flushing device: %s (%d)", mCameraId, strerror(-err), err);
803 }
804 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700805}
806
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800807binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700808 ATRACE_CALL();
809 ALOGV("%s", __FUNCTION__);
810
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800811 binder::Status res;
812 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700813
814 Mutex::Autolock icl(mBinderSerializationLock);
815
816 // Guard against trying to prepare non-created streams
817 ssize_t index = NAME_NOT_FOUND;
818 for (size_t i = 0; i < mStreamMap.size(); ++i) {
819 if (streamId == mStreamMap.valueAt(i)) {
820 index = i;
821 break;
822 }
823 }
824
825 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800826 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
827 "with that ID exists", mCameraId, streamId);
828 ALOGW("%s: %s", __FUNCTION__, msg.string());
829 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700830 }
831
Eino-Ville Talvala261394e2015-05-13 14:28:38 -0700832 // Also returns BAD_VALUE if stream ID was not valid, or stream already
833 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800834 status_t err = mDevice->prepare(streamId);
835 if (err == BAD_VALUE) {
836 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
837 "Camera %d: Stream %d has already been used, and cannot be prepared",
838 mCameraId, streamId);
839 } else if (err != OK) {
840 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
841 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
842 strerror(-err), err);
843 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700844 return res;
845}
846
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800847binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -0700848 ATRACE_CALL();
849 ALOGV("%s", __FUNCTION__);
850
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800851 binder::Status res;
852 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700853
854 Mutex::Autolock icl(mBinderSerializationLock);
855
856 // Guard against trying to prepare non-created streams
857 ssize_t index = NAME_NOT_FOUND;
858 for (size_t i = 0; i < mStreamMap.size(); ++i) {
859 if (streamId == mStreamMap.valueAt(i)) {
860 index = i;
861 break;
862 }
863 }
864
865 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800866 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
867 "with that ID exists", mCameraId, streamId);
868 ALOGW("%s: %s", __FUNCTION__, msg.string());
869 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700870 }
871
872 if (maxCount <= 0) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800873 String8 msg = String8::format("Camera %d: maxCount (%d) must be greater than 0",
874 mCameraId, maxCount);
875 ALOGE("%s: %s", __FUNCTION__, msg.string());
876 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700877 }
878
879 // Also returns BAD_VALUE if stream ID was not valid, or stream already
880 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800881 status_t err = mDevice->prepare(maxCount, streamId);
882 if (err == BAD_VALUE) {
883 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
884 "Camera %d: Stream %d has already been used, and cannot be prepared",
885 mCameraId, streamId);
886 } else if (err != OK) {
887 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
888 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
889 strerror(-err), err);
890 }
Ruben Brunkc78ac262015-08-13 17:58:46 -0700891
892 return res;
893}
894
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800895binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700896 ATRACE_CALL();
897 ALOGV("%s", __FUNCTION__);
898
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800899 binder::Status res;
900 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700901
902 Mutex::Autolock icl(mBinderSerializationLock);
903
904 // Guard against trying to prepare non-created streams
905 ssize_t index = NAME_NOT_FOUND;
906 for (size_t i = 0; i < mStreamMap.size(); ++i) {
907 if (streamId == mStreamMap.valueAt(i)) {
908 index = i;
909 break;
910 }
911 }
912
913 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800914 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
915 "with that ID exists", mCameraId, streamId);
916 ALOGW("%s: %s", __FUNCTION__, msg.string());
917 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700918 }
919
920 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
921 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800922 status_t err = mDevice->tearDown(streamId);
923 if (err == BAD_VALUE) {
924 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
925 "Camera %d: Stream %d is still in use, cannot be torn down",
926 mCameraId, streamId);
927 } else if (err != OK) {
928 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
929 "Camera %d: Error tearing down stream %d: %s (%d)", mCameraId, streamId,
930 strerror(-err), err);
931 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700932
933 return res;
934}
935
Igor Murashkine7ee7632013-06-11 18:10:18 -0700936status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800937 return BasicClient::dump(fd, args);
938}
939
940status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700941 String8 result;
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700942 result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700943 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800944 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -0800945 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Ruben Brunkcc776712015-02-17 20:18:47 -0800946 result.appendFormat(" Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700947
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700948 result.append(" State:\n");
949 result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700950 if (mInputStream.configured) {
951 result.appendFormat(" Current input stream ID: %d\n",
952 mInputStream.id);
953 } else {
954 result.append(" No input stream configured.\n");
955 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700956 if (!mStreamMap.isEmpty()) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700957 result.append(" Current output stream IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700958 for (size_t i = 0; i < mStreamMap.size(); i++) {
959 result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i));
960 }
961 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700962 result.append(" No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700963 }
964 write(fd, result.string(), result.size());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700965 // TODO: print dynamic/request section from most recent requests
966 mFrameProcessor->dump(fd, args);
967
968 return dumpDevice(fd, args);
969}
970
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800971void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700972 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700973 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800974 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700975
976 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700977 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700978 }
979}
980
981void CameraDeviceClient::notifyIdle() {
982 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800983 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700984
985 if (remoteCb != 0) {
986 remoteCb->onDeviceIdle();
987 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700988 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700989}
990
Jianing Weicb0652e2014-03-12 18:29:36 -0700991void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700992 nsecs_t timestamp) {
993 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800994 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700995 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700996 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700997 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700998 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700999}
1000
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001001void CameraDeviceClient::notifyPrepared(int streamId) {
1002 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001003 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001004 if (remoteCb != 0) {
1005 remoteCb->onPrepared(streamId);
1006 }
1007}
1008
Igor Murashkine7ee7632013-06-11 18:10:18 -07001009void CameraDeviceClient::detachDevice() {
1010 if (mDevice == 0) return;
1011
1012 ALOGV("Camera %d: Stopping processors", mCameraId);
1013
1014 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1015 FRAME_PROCESSOR_LISTENER_MAX_ID,
1016 /*listener*/this);
1017 mFrameProcessor->requestExit();
1018 ALOGV("Camera %d: Waiting for threads", mCameraId);
1019 mFrameProcessor->join();
1020 ALOGV("Camera %d: Disconnecting device", mCameraId);
1021
1022 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1023 {
1024 mDevice->clearStreamingRequest();
1025
1026 status_t code;
1027 if ((code = mDevice->waitUntilDrained()) != OK) {
1028 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1029 code);
1030 }
1031 }
1032
1033 Camera2ClientBase::detachDevice();
1034}
1035
1036/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001037void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001038 ATRACE_CALL();
1039 ALOGV("%s", __FUNCTION__);
1040
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001041 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001042 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001043 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001044 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001045 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001046}
1047
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001048binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
1049 status_t res = checkPid(checkLocation);
1050 return (res == OK) ? binder::Status::ok() :
1051 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1052 "Attempt to use camera from a different process than original client");
1053}
1054
Igor Murashkine7ee7632013-06-11 18:10:18 -07001055// TODO: move to Camera2ClientBase
1056bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1057
1058 const int pid = IPCThreadState::self()->getCallingPid();
1059 const int selfPid = getpid();
1060 camera_metadata_entry_t entry;
1061
1062 /**
1063 * Mixin default important security values
1064 * - android.led.transmit = defaulted ON
1065 */
1066 CameraMetadata staticInfo = mDevice->info();
1067 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1068 for(size_t i = 0; i < entry.count; ++i) {
1069 uint8_t led = entry.data.u8[i];
1070
1071 switch(led) {
1072 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1073 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1074 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1075 metadata.update(ANDROID_LED_TRANSMIT,
1076 &transmitDefault, 1);
1077 }
1078 break;
1079 }
1080 }
1081 }
1082
1083 // We can do anything!
1084 if (pid == selfPid) {
1085 return true;
1086 }
1087
1088 /**
1089 * Permission check special fields in the request
1090 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1091 */
1092 entry = metadata.find(ANDROID_LED_TRANSMIT);
1093 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1094 String16 permissionString =
1095 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1096 if (!checkCallingPermission(permissionString)) {
1097 const int uid = IPCThreadState::self()->getCallingUid();
1098 ALOGE("Permission Denial: "
1099 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1100 return false;
1101 }
1102 }
1103
1104 return true;
1105}
1106
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001107status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1108 ALOGV("%s: begin", __FUNCTION__);
1109
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001110 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001111 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001112}
1113
Igor Murashkine7ee7632013-06-11 18:10:18 -07001114} // namespace android