blob: 63e44fd2e7554a510257f5a610e0b01cc8be8e50 [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(),
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();
82 ALOGI("CameraDeviceClient %d: Opened", cameraId);
83}
84
Yin-Chia Yehe074a932015-01-30 10:29:02 -080085status_t CameraDeviceClient::initialize(CameraModule *module)
Igor Murashkine7ee7632013-06-11 18:10:18 -070086{
87 ATRACE_CALL();
88 status_t res;
89
90 res = Camera2ClientBase::initialize(module);
91 if (res != OK) {
92 return res;
93 }
94
95 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070096 mFrameProcessor = new FrameProcessorBase(mDevice);
Igor Murashkine7ee7632013-06-11 18:10:18 -070097 threadName = String8::format("CDU-%d-FrameProc", mCameraId);
98 mFrameProcessor->run(threadName.string());
99
100 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
101 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800102 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700103 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700104
105 return OK;
106}
107
108CameraDeviceClient::~CameraDeviceClient() {
109}
110
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800111binder::Status CameraDeviceClient::submitRequest(
112 const hardware::camera2::CaptureRequest& request,
113 bool streaming,
114 /*out*/
115 hardware::camera2::utils::SubmitInfo *submitInfo) {
116 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
117 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700118}
119
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800120binder::Status CameraDeviceClient::submitRequestList(
121 const std::vector<hardware::camera2::CaptureRequest>& requests,
122 bool streaming,
123 /*out*/
124 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700125 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700126 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700127
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800128 binder::Status res = binder::Status::ok();
129 status_t err;
130 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
131 return res;
132 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700133
134 Mutex::Autolock icl(mBinderSerializationLock);
135
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800136 if (!mDevice.get()) {
137 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
138 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700139
140 if (requests.empty()) {
141 ALOGE("%s: Camera %d: Sent null request. Rejecting request.",
142 __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800143 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700144 }
145
146 List<const CameraMetadata> metadataRequestList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800147 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700148 uint32_t loopCounter = 0;
149
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800150 for (auto&& request: requests) {
151 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700152 if (!mInputStream.configured) {
153 ALOGE("%s: Camera %d: no input stream is configured.", __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800154 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
155 "No input configured for camera %d but request is for reprocessing",
156 mCameraId);
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700157 } else if (streaming) {
158 ALOGE("%s: Camera %d: streaming reprocess requests not supported.", __FUNCTION__,
159 mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800160 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
161 "Repeating reprocess requests not supported");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700162 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700163 }
164
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800165 CameraMetadata metadata(request.mMetadata);
Jianing Wei90e59c92014-03-12 18:29:36 -0700166 if (metadata.isEmpty()) {
167 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.",
168 __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800169 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
170 "Request settings are empty");
171 } else if (request.mSurfaceList.isEmpty()) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700172 ALOGE("%s: Camera %d: Requests must have at least one surface target. "
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800173 "Rejecting request.", __FUNCTION__, mCameraId);
174 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
175 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700176 }
177
178 if (!enforceRequestPermissions(metadata)) {
179 // Callee logs
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800180 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
181 "Caller does not have permission to change restricted controls");
Jianing Wei90e59c92014-03-12 18:29:36 -0700182 }
183
184 /**
185 * Write in the output stream IDs which we calculate from
186 * the capture request's list of surface targets
187 */
188 Vector<int32_t> outputStreamIds;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800189 outputStreamIds.setCapacity(request.mSurfaceList.size());
190 for (sp<Surface> surface : request.mSurfaceList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700191 if (surface == 0) continue;
192
193 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Marco Nelissenf8880202014-11-14 07:58:25 -0800194 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
Jianing Wei90e59c92014-03-12 18:29:36 -0700195
196 // Trying to submit request with surface that wasn't created
197 if (idx == NAME_NOT_FOUND) {
198 ALOGE("%s: Camera %d: Tried to submit a request with a surface that"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800199 " we have not called createStream on",
200 __FUNCTION__, mCameraId);
201 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
202 "Request targets Surface that is not part of current capture session");
Jianing Wei90e59c92014-03-12 18:29:36 -0700203 }
204
205 int streamId = mStreamMap.valueAt(idx);
206 outputStreamIds.push_back(streamId);
207 ALOGV("%s: Camera %d: Appending output stream %d to request",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800208 __FUNCTION__, mCameraId, streamId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700209 }
210
211 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
212 outputStreamIds.size());
213
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800214 if (request.mIsReprocess) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700215 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
216 }
217
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800218 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700219 loopCounter++; // loopCounter starts from 1
Mark Salyzyn50468412014-06-18 16:33:43 -0700220 ALOGV("%s: Camera %d: Creating request with ID %d (%d of %zu)",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800221 __FUNCTION__, mCameraId, submitInfo->mRequestId, loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700222
223 metadataRequestList.push_back(metadata);
224 }
225 mRequestIdCounter++;
226
227 if (streaming) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800228 err = mDevice->setStreamingRequestList(metadataRequestList, &(submitInfo->mLastFrameNumber));
229 if (err != OK) {
230 String8 msg = String8::format(
231 "Camera %d: Got error %s (%d) after trying to set streaming request",
232 mCameraId, strerror(-err), err);
233 ALOGE("%s: %s", __FUNCTION__, msg.string());
234 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
235 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700236 } else {
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700237 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700238 }
239 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800240 err = mDevice->captureList(metadataRequestList, &(submitInfo->mLastFrameNumber));
241 if (err != OK) {
242 String8 msg = String8::format(
243 "Camera %d: Got error %s (%d) after trying to submit capture request",
244 mCameraId, strerror(-err), err);
245 ALOGE("%s: %s", __FUNCTION__, msg.string());
246 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
247 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700248 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800249 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700250 }
251
252 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700253 return res;
254}
255
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800256binder::Status CameraDeviceClient::cancelRequest(
257 int requestId,
258 /*out*/
259 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700260 ATRACE_CALL();
261 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
262
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800263 status_t err;
264 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700265
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800266 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700267
268 Mutex::Autolock icl(mBinderSerializationLock);
269
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800270 if (!mDevice.get()) {
271 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
272 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700273
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700274 if (mStreamingRequestId != requestId) {
275 String8 msg = String8::format("Camera %d: Canceling request ID %d doesn't match "
276 "current request ID %d", mCameraId, requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800277 ALOGE("%s: %s", __FUNCTION__, msg.string());
278 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700279 }
280
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800281 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700282
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 if (err == OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700284 ALOGV("%s: Camera %d: Successfully cleared streaming request",
285 __FUNCTION__, mCameraId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700286 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800287 } else {
288 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
289 "Camera %d: Error clearing streaming request: %s (%d)",
290 mCameraId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700291 }
292
293 return res;
294}
295
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800296binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700297 // TODO: Implement this.
Zhijun He1fa89992015-06-01 15:44:31 -0700298 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800299 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700300}
301
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800302binder::Status CameraDeviceClient::endConfigure(bool isConstrainedHighSpeed) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700303 ALOGV("%s: ending configure (%d input stream, %zu output streams)",
304 __FUNCTION__, mInputStream.configured ? 1 : 0, mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700305
Zhijun He0effd522016-03-04 10:22:27 -0800306 binder::Status res;
307 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
308
309 Mutex::Autolock icl(mBinderSerializationLock);
310
311 if (!mDevice.get()) {
312 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
313 }
314
Zhijun He1fa89992015-06-01 15:44:31 -0700315 // Sanitize the high speed session against necessary capability bit.
316 if (isConstrainedHighSpeed) {
317 CameraMetadata staticInfo = mDevice->info();
318 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
319 bool isConstrainedHighSpeedSupported = false;
320 for(size_t i = 0; i < entry.count; ++i) {
321 uint8_t capability = entry.data.u8[i];
322 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
323 isConstrainedHighSpeedSupported = true;
324 break;
325 }
326 }
327 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800328 String8 msg = String8::format(
329 "Camera %d: Try to create a constrained high speed configuration on a device"
330 " that doesn't support it.", mCameraId);
331 ALOGE("%s: %s", __FUNCTION__, msg.string());
332 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
333 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700334 }
335 }
336
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800337 status_t err = mDevice->configureStreams(isConstrainedHighSpeed);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700338 if (err == BAD_VALUE) {
339 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
340 "Camera %d: Unsupported set of inputs/outputs provided",
341 mCameraId);
342 } else if (err != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800343 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
344 "Camera %d: Error configuring streams: %s (%d)",
345 mCameraId, strerror(-err), err);
346 }
347
348 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700349}
350
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800351binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700352 ATRACE_CALL();
353 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
354
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800355 binder::Status res;
356 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700357
358 Mutex::Autolock icl(mBinderSerializationLock);
359
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800360 if (!mDevice.get()) {
361 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
362 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700363
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700364 bool isInput = false;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700365 ssize_t index = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700366
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700367 if (mInputStream.configured && mInputStream.id == streamId) {
368 isInput = true;
369 } else {
370 // Guard against trying to delete non-created streams
371 for (size_t i = 0; i < mStreamMap.size(); ++i) {
372 if (streamId == mStreamMap.valueAt(i)) {
373 index = i;
374 break;
375 }
376 }
377
378 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800379 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no such "
380 "stream created yet", mCameraId, streamId);
381 ALOGW("%s: %s", __FUNCTION__, msg.string());
382 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700383 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700384 }
385
386 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800387 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700388
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800389 if (err != OK) {
390 String8 msg = String8::format("Camera %d: Unexpected error %s (%d) when deleting stream %d",
391 mCameraId, strerror(-err), err, streamId);
392 ALOGE("%s: %s", __FUNCTION__, msg.string());
393 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
394 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700395 if (isInput) {
396 mInputStream.configured = false;
397 } else {
398 mStreamMap.removeItemsAt(index);
399 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700400 }
401
402 return res;
403}
404
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800405binder::Status CameraDeviceClient::createStream(
406 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
407 /*out*/
408 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700409 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700410
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800411 binder::Status res;
412 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700413
414 Mutex::Autolock icl(mBinderSerializationLock);
415
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700416 sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700417 if (bufferProducer == NULL) {
418 ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800419 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700420 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800421 if (!mDevice.get()) {
422 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
423 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700424
425 // Don't create multiple streams for the same target surface
426 {
Marco Nelissenf8880202014-11-14 07:58:25 -0800427 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
Igor Murashkine7ee7632013-06-11 18:10:18 -0700428 if (index != NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800429 String8 msg = String8::format("Camera %d: Surface already has a stream created for it "
430 "(ID %zd)", mCameraId, index);
431 ALOGW("%s: %s", __FUNCTION__, msg.string());
432 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700433 }
434 }
435
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800436 status_t err;
437
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700438 // HACK b/10949105
439 // Query consumer usage bits to set async operation mode for
440 // GLConsumer using controlledByApp parameter.
441 bool useAsync = false;
442 int32_t consumerUsage;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800443 if ((err = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700444 &consumerUsage)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800445 String8 msg = String8::format("Camera %d: Failed to query Surface consumer usage: %s (%d)",
446 mCameraId, strerror(-err), err);
447 ALOGE("%s: %s", __FUNCTION__, msg.string());
448 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700449 }
450 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
451 ALOGW("%s: Camera %d: Forcing asynchronous mode for stream",
452 __FUNCTION__, mCameraId);
453 useAsync = true;
454 }
455
Ruben Brunkbba75572014-11-20 17:29:50 -0800456 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
457 GRALLOC_USAGE_RENDERSCRIPT;
458 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
459 GraphicBuffer::USAGE_HW_TEXTURE |
460 GraphicBuffer::USAGE_HW_COMPOSER;
461 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
462 (consumerUsage & allowedFlags) != 0;
463
Marco Nelissenf8880202014-11-14 07:58:25 -0800464 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700465 sp<Surface> surface = new Surface(bufferProducer, useAsync);
466 ANativeWindow *anw = surface.get();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700467
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800468 int width, height, format;
469 android_dataspace dataSpace;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700470
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800471 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
472 String8 msg = String8::format("Camera %d: Failed to query Surface width: %s (%d)",
473 mCameraId, strerror(-err), err);
474 ALOGE("%s: %s", __FUNCTION__, msg.string());
475 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700476 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800477 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
478 String8 msg = String8::format("Camera %d: Failed to query Surface height: %s (%d)",
479 mCameraId, strerror(-err), err);
480 ALOGE("%s: %s", __FUNCTION__, msg.string());
481 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700482 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800483 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
484 String8 msg = String8::format("Camera %d: Failed to query Surface format: %s (%d)",
485 mCameraId, strerror(-err), err);
486 ALOGE("%s: %s", __FUNCTION__, msg.string());
487 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700488 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800489 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800490 reinterpret_cast<int*>(&dataSpace))) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800491 String8 msg = String8::format("Camera %d: Failed to query Surface dataspace: %s (%d)",
492 mCameraId, strerror(-err), err);
493 ALOGE("%s: %s", __FUNCTION__, msg.string());
494 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800495 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700496
497 // FIXME: remove this override since the default format should be
498 // IMPLEMENTATION_DEFINED. b/9487482
Igor Murashkin15811012013-07-29 12:25:59 -0700499 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
500 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
Ruben Brunkbba75572014-11-20 17:29:50 -0800501 ALOGW("%s: Camera %d: Overriding format %#x to IMPLEMENTATION_DEFINED",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700502 __FUNCTION__, mCameraId, format);
503 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
504 }
505
Ruben Brunkbba75572014-11-20 17:29:50 -0800506 // Round dimensions to the nearest dimensions available for this format
507 if (flexibleConsumer && !CameraDeviceClient::roundBufferDimensionNearest(width, height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800508 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800509 String8 msg = String8::format("Camera %d: No supported stream configurations with "
510 "format %#x defined, failed to create output stream", mCameraId, format);
511 ALOGE("%s: %s", __FUNCTION__, msg.string());
512 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkbba75572014-11-20 17:29:50 -0800513 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700514
Zhijun He125684a2015-12-26 15:07:30 -0800515 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800516 err = mDevice->createStream(surface, width, height, format, dataSpace,
517 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
518 &streamId, outputConfiguration.getSurfaceSetID());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700519
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800520 if (err != OK) {
521 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
522 "Camera %d: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
523 mCameraId, width, height, format, dataSpace, strerror(-err), err);
524 } else {
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800525 mStreamMap.add(binder, streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700526
527 ALOGV("%s: Camera %d: Successfully created a new stream ID %d",
528 __FUNCTION__, mCameraId, streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700529
530 /**
531 * Set the stream transform flags to automatically
532 * rotate the camera stream for preview use cases.
533 */
534 int32_t transform = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800535 err = getRotationTransformLocked(&transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700536
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800537 if (err != OK) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700538 // Error logged by getRotationTransformLocked.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800539 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
540 "Unable to calculate rotation transform for new stream");
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700541 }
542
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800543 err = mDevice->setStreamTransform(streamId, transform);
544 if (err != OK) {
545 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
546 streamId);
547 ALOGE("%s: %s", __FUNCTION__, msg.string());
548 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700549 }
550
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700552 }
553
554 return res;
555}
556
Ruben Brunkbba75572014-11-20 17:29:50 -0800557
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800558binder::Status CameraDeviceClient::createInputStream(
559 int width, int height, int format,
560 /*out*/
561 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700562
563 ATRACE_CALL();
564 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
565
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800566 binder::Status res;
567 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700568
569 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800570
571 if (!mDevice.get()) {
572 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
573 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700574
575 if (mInputStream.configured) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800576 String8 msg = String8::format("Camera %d: Already has an input stream "
577 "configured (ID %zd)", mCameraId, mInputStream.id);
578 ALOGE("%s: %s", __FUNCTION__, msg.string() );
579 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700580 }
581
582 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800583 status_t err = mDevice->createInputStream(width, height, format, &streamId);
584 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700585 mInputStream.configured = true;
586 mInputStream.width = width;
587 mInputStream.height = height;
588 mInputStream.format = format;
589 mInputStream.id = streamId;
590
591 ALOGV("%s: Camera %d: Successfully created a new input stream ID %d",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800592 __FUNCTION__, mCameraId, streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700593
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800594 *newStreamId = streamId;
595 } else {
596 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
597 "Camera %d: Error creating new input stream: %s (%d)", mCameraId,
598 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700599 }
600
601 return res;
602}
603
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800604binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700605
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800606 binder::Status res;
607 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
608
609 if (inputSurface == NULL) {
610 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700611 }
612
613 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800614 if (!mDevice.get()) {
615 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
616 }
617 sp<IGraphicBufferProducer> producer;
618 status_t err = mDevice->getInputBufferProducer(&producer);
619 if (err != OK) {
620 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
621 "Camera %d: Error getting input Surface: %s (%d)",
622 mCameraId, strerror(-err), err);
623 } else {
624 inputSurface->name = String16("CameraInput");
625 inputSurface->graphicBufferProducer = producer;
626 }
627 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700628}
629
Ruben Brunkbba75572014-11-20 17:29:50 -0800630bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800631 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800632 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
633
634 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800635 (dataSpace == HAL_DATASPACE_DEPTH) ?
636 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800637 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
638
639 int32_t bestWidth = -1;
640 int32_t bestHeight = -1;
641
642 // Iterate through listed stream configurations and find the one with the smallest euclidean
643 // distance from the given dimensions for the given format.
644 for (size_t i = 0; i < streamConfigs.count; i += 4) {
645 int32_t fmt = streamConfigs.data.i32[i];
646 int32_t w = streamConfigs.data.i32[i + 1];
647 int32_t h = streamConfigs.data.i32[i + 2];
648
649 // Ignore input/output type for now
650 if (fmt == format) {
651 if (w == width && h == height) {
652 bestWidth = width;
653 bestHeight = height;
654 break;
655 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
656 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
657 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
658 bestWidth = w;
659 bestHeight = h;
660 }
661 }
662 }
663
664 if (bestWidth == -1) {
665 // Return false if no configurations for this format were listed
666 return false;
667 }
668
669 // Set the outputs to the closet width/height
670 if (outWidth != NULL) {
671 *outWidth = bestWidth;
672 }
673 if (outHeight != NULL) {
674 *outHeight = bestHeight;
675 }
676
677 // Return true if at least one configuration for this format was listed
678 return true;
679}
680
681int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
682 int64_t d0 = x0 - x1;
683 int64_t d1 = y0 - y1;
684 return d0 * d0 + d1 * d1;
685}
686
Igor Murashkine7ee7632013-06-11 18:10:18 -0700687// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800688binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
689 /*out*/
690 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700691{
692 ATRACE_CALL();
693 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
694
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800695 binder::Status res;
696 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700697
698 Mutex::Autolock icl(mBinderSerializationLock);
699
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800700 if (!mDevice.get()) {
701 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
702 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700703
704 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800705 status_t err;
706 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700707 request != NULL) {
708
709 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800710 } else if (err == BAD_VALUE) {
711 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
712 "Camera %d: Template ID %d is invalid or not supported: %s (%d)",
713 mCameraId, templateId, strerror(-err), err);
714
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800715 } else {
716 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
717 "Camera %d: Error creating default request for template %d: %s (%d)",
718 mCameraId, templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700719 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700720 return res;
721}
722
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800723binder::Status CameraDeviceClient::getCameraInfo(
724 /*out*/
725 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700726{
727 ATRACE_CALL();
728 ALOGV("%s", __FUNCTION__);
729
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800730 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700731
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800732 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700733
734 Mutex::Autolock icl(mBinderSerializationLock);
735
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800736 if (!mDevice.get()) {
737 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
738 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700739
Igor Murashkin099b4572013-07-12 17:52:16 -0700740 if (info != NULL) {
741 *info = mDevice->info(); // static camera metadata
742 // TODO: merge with device-specific camera metadata
743 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700744
745 return res;
746}
747
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800748binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -0700749{
750 ATRACE_CALL();
751 ALOGV("%s", __FUNCTION__);
752
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800753 binder::Status res;
754 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -0700755
756 Mutex::Autolock icl(mBinderSerializationLock);
757
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800758 if (!mDevice.get()) {
759 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
760 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700761
762 // FIXME: Also need check repeating burst.
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700763 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800764 String8 msg = String8::format(
765 "Camera %d: Try to waitUntilIdle when there are active streaming requests",
766 mCameraId);
767 ALOGE("%s: %s", __FUNCTION__, msg.string());
768 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -0700769 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800770 status_t err = mDevice->waitUntilDrained();
771 if (err != OK) {
772 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
773 "Camera %d: Error waiting to drain: %s (%d)",
774 mCameraId, strerror(-err), err);
775 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700776 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -0700777 return res;
778}
779
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800780binder::Status CameraDeviceClient::flush(
781 /*out*/
782 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700783 ATRACE_CALL();
784 ALOGV("%s", __FUNCTION__);
785
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800786 binder::Status res;
787 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700788
789 Mutex::Autolock icl(mBinderSerializationLock);
790
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800791 if (!mDevice.get()) {
792 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
793 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700794
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700795 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800796 status_t err = mDevice->flush(lastFrameNumber);
797 if (err != OK) {
798 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
799 "Camera %d: Error flushing device: %s (%d)", mCameraId, strerror(-err), err);
800 }
801 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700802}
803
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800804binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700805 ATRACE_CALL();
806 ALOGV("%s", __FUNCTION__);
807
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800808 binder::Status res;
809 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700810
811 Mutex::Autolock icl(mBinderSerializationLock);
812
813 // Guard against trying to prepare non-created streams
814 ssize_t index = NAME_NOT_FOUND;
815 for (size_t i = 0; i < mStreamMap.size(); ++i) {
816 if (streamId == mStreamMap.valueAt(i)) {
817 index = i;
818 break;
819 }
820 }
821
822 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800823 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
824 "with that ID exists", mCameraId, streamId);
825 ALOGW("%s: %s", __FUNCTION__, msg.string());
826 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700827 }
828
Eino-Ville Talvala261394e2015-05-13 14:28:38 -0700829 // Also returns BAD_VALUE if stream ID was not valid, or stream already
830 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800831 status_t err = mDevice->prepare(streamId);
832 if (err == BAD_VALUE) {
833 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
834 "Camera %d: Stream %d has already been used, and cannot be prepared",
835 mCameraId, streamId);
836 } else if (err != OK) {
837 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
838 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
839 strerror(-err), err);
840 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700841 return res;
842}
843
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800844binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -0700845 ATRACE_CALL();
846 ALOGV("%s", __FUNCTION__);
847
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800848 binder::Status res;
849 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700850
851 Mutex::Autolock icl(mBinderSerializationLock);
852
853 // Guard against trying to prepare non-created streams
854 ssize_t index = NAME_NOT_FOUND;
855 for (size_t i = 0; i < mStreamMap.size(); ++i) {
856 if (streamId == mStreamMap.valueAt(i)) {
857 index = i;
858 break;
859 }
860 }
861
862 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800863 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
864 "with that ID exists", mCameraId, streamId);
865 ALOGW("%s: %s", __FUNCTION__, msg.string());
866 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700867 }
868
869 if (maxCount <= 0) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800870 String8 msg = String8::format("Camera %d: maxCount (%d) must be greater than 0",
871 mCameraId, maxCount);
872 ALOGE("%s: %s", __FUNCTION__, msg.string());
873 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700874 }
875
876 // Also returns BAD_VALUE if stream ID was not valid, or stream already
877 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800878 status_t err = mDevice->prepare(maxCount, streamId);
879 if (err == BAD_VALUE) {
880 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
881 "Camera %d: Stream %d has already been used, and cannot be prepared",
882 mCameraId, streamId);
883 } else if (err != OK) {
884 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
885 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
886 strerror(-err), err);
887 }
Ruben Brunkc78ac262015-08-13 17:58:46 -0700888
889 return res;
890}
891
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800892binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700893 ATRACE_CALL();
894 ALOGV("%s", __FUNCTION__);
895
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800896 binder::Status res;
897 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700898
899 Mutex::Autolock icl(mBinderSerializationLock);
900
901 // Guard against trying to prepare non-created streams
902 ssize_t index = NAME_NOT_FOUND;
903 for (size_t i = 0; i < mStreamMap.size(); ++i) {
904 if (streamId == mStreamMap.valueAt(i)) {
905 index = i;
906 break;
907 }
908 }
909
910 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800911 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
912 "with that ID exists", mCameraId, streamId);
913 ALOGW("%s: %s", __FUNCTION__, msg.string());
914 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700915 }
916
917 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
918 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800919 status_t err = mDevice->tearDown(streamId);
920 if (err == BAD_VALUE) {
921 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
922 "Camera %d: Stream %d is still in use, cannot be torn down",
923 mCameraId, streamId);
924 } else if (err != OK) {
925 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
926 "Camera %d: Error tearing down stream %d: %s (%d)", mCameraId, streamId,
927 strerror(-err), err);
928 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700929
930 return res;
931}
932
Igor Murashkine7ee7632013-06-11 18:10:18 -0700933status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800934 return BasicClient::dump(fd, args);
935}
936
937status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700938 String8 result;
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700939 result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700940 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800941 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -0800942 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Ruben Brunkcc776712015-02-17 20:18:47 -0800943 result.appendFormat(" Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700944
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700945 result.append(" State:\n");
946 result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700947 if (mInputStream.configured) {
948 result.appendFormat(" Current input stream ID: %d\n",
949 mInputStream.id);
950 } else {
951 result.append(" No input stream configured.\n");
952 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700953 if (!mStreamMap.isEmpty()) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700954 result.append(" Current output stream IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700955 for (size_t i = 0; i < mStreamMap.size(); i++) {
956 result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i));
957 }
958 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700959 result.append(" No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700960 }
961 write(fd, result.string(), result.size());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700962 // TODO: print dynamic/request section from most recent requests
963 mFrameProcessor->dump(fd, args);
964
965 return dumpDevice(fd, args);
966}
967
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800968void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700969 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700970 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800971 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700972
973 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700974 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700975 }
976}
977
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700978void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
979 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
980
981 if (remoteCb != 0) {
982 remoteCb->onRepeatingRequestError(lastFrameNumber);
983 }
984
985 Mutex::Autolock icl(mBinderSerializationLock);
986 mStreamingRequestId = REQUEST_ID_NONE;
987}
988
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700989void CameraDeviceClient::notifyIdle() {
990 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800991 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700992
993 if (remoteCb != 0) {
994 remoteCb->onDeviceIdle();
995 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700996 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700997}
998
Jianing Weicb0652e2014-03-12 18:29:36 -0700999void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001000 nsecs_t timestamp) {
1001 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001002 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001003 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001004 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001005 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001006 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001007}
1008
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001009void CameraDeviceClient::notifyPrepared(int streamId) {
1010 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001011 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001012 if (remoteCb != 0) {
1013 remoteCb->onPrepared(streamId);
1014 }
1015}
1016
Igor Murashkine7ee7632013-06-11 18:10:18 -07001017void CameraDeviceClient::detachDevice() {
1018 if (mDevice == 0) return;
1019
1020 ALOGV("Camera %d: Stopping processors", mCameraId);
1021
1022 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1023 FRAME_PROCESSOR_LISTENER_MAX_ID,
1024 /*listener*/this);
1025 mFrameProcessor->requestExit();
1026 ALOGV("Camera %d: Waiting for threads", mCameraId);
1027 mFrameProcessor->join();
1028 ALOGV("Camera %d: Disconnecting device", mCameraId);
1029
1030 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1031 {
1032 mDevice->clearStreamingRequest();
1033
1034 status_t code;
1035 if ((code = mDevice->waitUntilDrained()) != OK) {
1036 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1037 code);
1038 }
1039 }
1040
1041 Camera2ClientBase::detachDevice();
1042}
1043
1044/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001045void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001046 ATRACE_CALL();
1047 ALOGV("%s", __FUNCTION__);
1048
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001049 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001050 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001051 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001052 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001053 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001054}
1055
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001056binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001057 if (mDisconnected) {
1058 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1059 "The camera device has been disconnected");
1060 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001061 status_t res = checkPid(checkLocation);
1062 return (res == OK) ? binder::Status::ok() :
1063 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1064 "Attempt to use camera from a different process than original client");
1065}
1066
Igor Murashkine7ee7632013-06-11 18:10:18 -07001067// TODO: move to Camera2ClientBase
1068bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1069
1070 const int pid = IPCThreadState::self()->getCallingPid();
1071 const int selfPid = getpid();
1072 camera_metadata_entry_t entry;
1073
1074 /**
1075 * Mixin default important security values
1076 * - android.led.transmit = defaulted ON
1077 */
1078 CameraMetadata staticInfo = mDevice->info();
1079 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1080 for(size_t i = 0; i < entry.count; ++i) {
1081 uint8_t led = entry.data.u8[i];
1082
1083 switch(led) {
1084 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1085 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1086 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1087 metadata.update(ANDROID_LED_TRANSMIT,
1088 &transmitDefault, 1);
1089 }
1090 break;
1091 }
1092 }
1093 }
1094
1095 // We can do anything!
1096 if (pid == selfPid) {
1097 return true;
1098 }
1099
1100 /**
1101 * Permission check special fields in the request
1102 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1103 */
1104 entry = metadata.find(ANDROID_LED_TRANSMIT);
1105 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1106 String16 permissionString =
1107 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1108 if (!checkCallingPermission(permissionString)) {
1109 const int uid = IPCThreadState::self()->getCallingUid();
1110 ALOGE("Permission Denial: "
1111 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1112 return false;
1113 }
1114 }
1115
1116 return true;
1117}
1118
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001119status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1120 ALOGV("%s: begin", __FUNCTION__);
1121
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001122 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001123 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001124}
1125
Igor Murashkine7ee7632013-06-11 18:10:18 -07001126} // namespace android