blob: b4f8e21b858a23f5d21c7fbc12d33c8ff286d04f [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 {
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700237 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700238 mStreamingRequestId = submitInfo->mRequestId;
Jianing Wei90e59c92014-03-12 18:29:36 -0700239 }
240 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800241 err = mDevice->captureList(metadataRequestList, &(submitInfo->mLastFrameNumber));
242 if (err != OK) {
243 String8 msg = String8::format(
244 "Camera %d: Got error %s (%d) after trying to submit capture request",
245 mCameraId, strerror(-err), err);
246 ALOGE("%s: %s", __FUNCTION__, msg.string());
247 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
248 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700249 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800250 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700251 }
252
253 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700254 return res;
255}
256
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800257binder::Status CameraDeviceClient::cancelRequest(
258 int requestId,
259 /*out*/
260 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700261 ATRACE_CALL();
262 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
263
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800264 status_t err;
265 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700266
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800267 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700268
269 Mutex::Autolock icl(mBinderSerializationLock);
270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800271 if (!mDevice.get()) {
272 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
273 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700274
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700275 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700276 if (mStreamingRequestId != requestId) {
277 String8 msg = String8::format("Camera %d: Canceling request ID %d doesn't match "
278 "current request ID %d", mCameraId, requestId, mStreamingRequestId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800279 ALOGE("%s: %s", __FUNCTION__, msg.string());
280 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700281 }
282
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800283 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700284
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800285 if (err == OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700286 ALOGV("%s: Camera %d: Successfully cleared streaming request",
287 __FUNCTION__, mCameraId);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700288 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800289 } else {
290 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
291 "Camera %d: Error clearing streaming request: %s (%d)",
292 mCameraId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700293 }
294
295 return res;
296}
297
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800298binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700299 // TODO: Implement this.
Zhijun He1fa89992015-06-01 15:44:31 -0700300 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800301 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700302}
303
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800304binder::Status CameraDeviceClient::endConfigure(bool isConstrainedHighSpeed) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700305 ALOGV("%s: ending configure (%d input stream, %zu output streams)",
306 __FUNCTION__, mInputStream.configured ? 1 : 0, mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700307
Zhijun He0effd522016-03-04 10:22:27 -0800308 binder::Status res;
309 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
310
311 Mutex::Autolock icl(mBinderSerializationLock);
312
313 if (!mDevice.get()) {
314 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
315 }
316
Zhijun He1fa89992015-06-01 15:44:31 -0700317 // Sanitize the high speed session against necessary capability bit.
318 if (isConstrainedHighSpeed) {
319 CameraMetadata staticInfo = mDevice->info();
320 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
321 bool isConstrainedHighSpeedSupported = false;
322 for(size_t i = 0; i < entry.count; ++i) {
323 uint8_t capability = entry.data.u8[i];
324 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
325 isConstrainedHighSpeedSupported = true;
326 break;
327 }
328 }
329 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800330 String8 msg = String8::format(
331 "Camera %d: Try to create a constrained high speed configuration on a device"
332 " that doesn't support it.", mCameraId);
333 ALOGE("%s: %s", __FUNCTION__, msg.string());
334 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
335 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700336 }
337 }
338
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800339 status_t err = mDevice->configureStreams(isConstrainedHighSpeed);
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700340 if (err == BAD_VALUE) {
Zhijun He5d677d12016-05-29 16:52:39 -0700341 String8 msg = String8::format("Camera %d: Unsupported set of inputs/outputs provided",
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700342 mCameraId);
Zhijun He5d677d12016-05-29 16:52:39 -0700343 ALOGE("%s: %s", __FUNCTION__, msg.string());
344 res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalaace80582016-03-18 13:27:35 -0700345 } else if (err != OK) {
Zhijun He5d677d12016-05-29 16:52:39 -0700346 String8 msg = String8::format("Camera %d: Error configuring streams: %s (%d)",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800347 mCameraId, strerror(-err), err);
Zhijun He5d677d12016-05-29 16:52:39 -0700348 ALOGE("%s: %s", __FUNCTION__, msg.string());
349 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800350 }
351
352 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700353}
354
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800355binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700356 ATRACE_CALL();
357 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
358
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800359 binder::Status res;
360 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700361
362 Mutex::Autolock icl(mBinderSerializationLock);
363
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800364 if (!mDevice.get()) {
365 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
366 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700367
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700368 bool isInput = false;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700369 ssize_t index = NAME_NOT_FOUND;
Zhijun He5d677d12016-05-29 16:52:39 -0700370 ssize_t dIndex = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700371
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700372 if (mInputStream.configured && mInputStream.id == streamId) {
373 isInput = true;
374 } else {
375 // Guard against trying to delete non-created streams
376 for (size_t i = 0; i < mStreamMap.size(); ++i) {
377 if (streamId == mStreamMap.valueAt(i)) {
378 index = i;
379 break;
380 }
381 }
382
383 if (index == NAME_NOT_FOUND) {
Zhijun He5d677d12016-05-29 16:52:39 -0700384 // See if this stream is one of the deferred streams.
385 for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
386 if (streamId == mDeferredStreams[i]) {
387 dIndex = i;
388 break;
389 }
390 }
391 if (dIndex == NAME_NOT_FOUND) {
392 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no such"
393 " stream created yet", mCameraId, streamId);
394 ALOGW("%s: %s", __FUNCTION__, msg.string());
395 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
396 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700397 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700398 }
399
400 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800401 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700402
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800403 if (err != OK) {
404 String8 msg = String8::format("Camera %d: Unexpected error %s (%d) when deleting stream %d",
405 mCameraId, strerror(-err), err, streamId);
406 ALOGE("%s: %s", __FUNCTION__, msg.string());
407 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
408 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700409 if (isInput) {
410 mInputStream.configured = false;
Zhijun He5d677d12016-05-29 16:52:39 -0700411 } else if (index != NAME_NOT_FOUND) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700412 mStreamMap.removeItemsAt(index);
Zhijun He5d677d12016-05-29 16:52:39 -0700413 } else {
414 mDeferredStreams.removeItemsAt(dIndex);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700415 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700416 }
417
418 return res;
419}
420
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800421binder::Status CameraDeviceClient::createStream(
422 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
423 /*out*/
424 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700425 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700426
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800427 binder::Status res;
428 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700429
430 Mutex::Autolock icl(mBinderSerializationLock);
431
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700432 sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
Zhijun He5d677d12016-05-29 16:52:39 -0700433 bool deferredConsumer = bufferProducer == NULL;
434 int surfaceType = outputConfiguration.getSurfaceType();
435 bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
436 (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
437 if (deferredConsumer && !validSurfaceType) {
438 ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
439 __FUNCTION__, bufferProducer.get(), surfaceType);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800440 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700441 }
Zhijun He5d677d12016-05-29 16:52:39 -0700442
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800443 if (!mDevice.get()) {
444 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
445 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700446
Zhijun He5d677d12016-05-29 16:52:39 -0700447 int width, height, format;
448 int32_t consumerUsage;
449 android_dataspace dataSpace;
450 status_t err;
451
452 // Create stream for deferred surface case.
453 if (deferredConsumer) {
454 return createDeferredSurfaceStreamLocked(outputConfiguration, newStreamId);
455 }
456
Igor Murashkine7ee7632013-06-11 18:10:18 -0700457 // Don't create multiple streams for the same target surface
458 {
Marco Nelissenf8880202014-11-14 07:58:25 -0800459 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
Igor Murashkine7ee7632013-06-11 18:10:18 -0700460 if (index != NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800461 String8 msg = String8::format("Camera %d: Surface already has a stream created for it "
462 "(ID %zd)", mCameraId, index);
463 ALOGW("%s: %s", __FUNCTION__, msg.string());
464 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700465 }
466 }
467
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700468 // HACK b/10949105
469 // Query consumer usage bits to set async operation mode for
470 // GLConsumer using controlledByApp parameter.
471 bool useAsync = false;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800472 if ((err = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700473 &consumerUsage)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800474 String8 msg = String8::format("Camera %d: Failed to query Surface consumer usage: %s (%d)",
475 mCameraId, strerror(-err), err);
476 ALOGE("%s: %s", __FUNCTION__, msg.string());
477 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700478 }
479 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
Zhijun He5d677d12016-05-29 16:52:39 -0700480 ALOGW("%s: Camera %d with consumer usage flag: 0x%x: Forcing asynchronous mode for stream",
481 __FUNCTION__, mCameraId, consumerUsage);
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700482 useAsync = true;
483 }
484
Ruben Brunkbba75572014-11-20 17:29:50 -0800485 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
486 GRALLOC_USAGE_RENDERSCRIPT;
487 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
488 GraphicBuffer::USAGE_HW_TEXTURE |
489 GraphicBuffer::USAGE_HW_COMPOSER;
490 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
491 (consumerUsage & allowedFlags) != 0;
492
Marco Nelissenf8880202014-11-14 07:58:25 -0800493 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700494 sp<Surface> surface = new Surface(bufferProducer, useAsync);
495 ANativeWindow *anw = surface.get();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700496
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800497 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
498 String8 msg = String8::format("Camera %d: Failed to query Surface width: %s (%d)",
499 mCameraId, strerror(-err), err);
500 ALOGE("%s: %s", __FUNCTION__, msg.string());
501 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700502 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800503 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
504 String8 msg = String8::format("Camera %d: Failed to query Surface height: %s (%d)",
505 mCameraId, strerror(-err), err);
506 ALOGE("%s: %s", __FUNCTION__, msg.string());
507 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700508 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800509 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
510 String8 msg = String8::format("Camera %d: Failed to query Surface format: %s (%d)",
511 mCameraId, strerror(-err), err);
512 ALOGE("%s: %s", __FUNCTION__, msg.string());
513 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700514 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800515 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800516 reinterpret_cast<int*>(&dataSpace))) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800517 String8 msg = String8::format("Camera %d: Failed to query Surface dataspace: %s (%d)",
518 mCameraId, strerror(-err), err);
519 ALOGE("%s: %s", __FUNCTION__, msg.string());
520 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800521 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700522
523 // FIXME: remove this override since the default format should be
524 // IMPLEMENTATION_DEFINED. b/9487482
Igor Murashkin15811012013-07-29 12:25:59 -0700525 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
526 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
Ruben Brunkbba75572014-11-20 17:29:50 -0800527 ALOGW("%s: Camera %d: Overriding format %#x to IMPLEMENTATION_DEFINED",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700528 __FUNCTION__, mCameraId, format);
529 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
530 }
531
Ruben Brunkbba75572014-11-20 17:29:50 -0800532 // Round dimensions to the nearest dimensions available for this format
Eman Copty6d7af0e2016-06-17 20:46:40 -0700533 if (flexibleConsumer && isPublicFormat(format) &&
534 !CameraDeviceClient::roundBufferDimensionNearest(width, height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800535 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800536 String8 msg = String8::format("Camera %d: No supported stream configurations with "
537 "format %#x defined, failed to create output stream", mCameraId, format);
538 ALOGE("%s: %s", __FUNCTION__, msg.string());
539 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkbba75572014-11-20 17:29:50 -0800540 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700541
Zhijun He125684a2015-12-26 15:07:30 -0800542 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800543 err = mDevice->createStream(surface, width, height, format, dataSpace,
544 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
545 &streamId, outputConfiguration.getSurfaceSetID());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700546
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800547 if (err != OK) {
548 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
549 "Camera %d: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
550 mCameraId, width, height, format, dataSpace, strerror(-err), err);
551 } else {
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800552 mStreamMap.add(binder, streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700553
Zhijun He5d677d12016-05-29 16:52:39 -0700554 ALOGV("%s: Camera %d: Successfully created a new stream ID %d for output surface"
555 " (%d x %d) with format 0x%x.",
556 __FUNCTION__, mCameraId, streamId, width, height, format);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700557
Zhijun He5d677d12016-05-29 16:52:39 -0700558 // Set transform flags to ensure preview to be rotated correctly.
559 res = setStreamTransformLocked(streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700560
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800561 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700562 }
563
564 return res;
565}
566
Zhijun He5d677d12016-05-29 16:52:39 -0700567binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
568 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
569 /*out*/
570 int* newStreamId) {
571 int width, height, format, surfaceType;
572 int32_t consumerUsage;
573 android_dataspace dataSpace;
574 status_t err;
575 binder::Status res;
576
577 if (!mDevice.get()) {
578 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
579 }
580
581 // Infer the surface info for deferred surface stream creation.
582 width = outputConfiguration.getWidth();
583 height = outputConfiguration.getHeight();
584 surfaceType = outputConfiguration.getSurfaceType();
585 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
586 dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
587 // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
588 consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
589 if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
590 consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
591 }
592 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
593 err = mDevice->createStream(/*surface*/nullptr, width, height, format, dataSpace,
594 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
595 &streamId, outputConfiguration.getSurfaceSetID(), consumerUsage);
596
597 if (err != OK) {
598 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
599 "Camera %d: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
600 mCameraId, width, height, format, dataSpace, strerror(-err), err);
601 } else {
602 // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
603 // a separate list to track. Once the deferred surface is set, this id will be
604 // relocated to mStreamMap.
605 mDeferredStreams.push_back(streamId);
606
607 ALOGV("%s: Camera %d: Successfully created a new stream ID %d for a deferred surface"
608 " (%d x %d) stream with format 0x%x.",
609 __FUNCTION__, mCameraId, streamId, width, height, format);
610
611 // Set transform flags to ensure preview to be rotated correctly.
612 res = setStreamTransformLocked(streamId);
613
614 *newStreamId = streamId;
615 }
616 return res;
617}
618
619binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
620 int32_t transform = 0;
621 status_t err;
622 binder::Status res;
623
624 if (!mDevice.get()) {
625 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
626 }
627
628 err = getRotationTransformLocked(&transform);
629 if (err != OK) {
630 // Error logged by getRotationTransformLocked.
631 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
632 "Unable to calculate rotation transform for new stream");
633 }
634
635 err = mDevice->setStreamTransform(streamId, transform);
636 if (err != OK) {
637 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
638 streamId);
639 ALOGE("%s: %s", __FUNCTION__, msg.string());
640 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
641 }
642
643 return res;
644}
Ruben Brunkbba75572014-11-20 17:29:50 -0800645
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800646binder::Status CameraDeviceClient::createInputStream(
647 int width, int height, int format,
648 /*out*/
649 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700650
651 ATRACE_CALL();
652 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
653
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800654 binder::Status res;
655 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700656
657 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800658
659 if (!mDevice.get()) {
660 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
661 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700662
663 if (mInputStream.configured) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800664 String8 msg = String8::format("Camera %d: Already has an input stream "
665 "configured (ID %zd)", mCameraId, mInputStream.id);
666 ALOGE("%s: %s", __FUNCTION__, msg.string() );
667 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700668 }
669
670 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800671 status_t err = mDevice->createInputStream(width, height, format, &streamId);
672 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700673 mInputStream.configured = true;
674 mInputStream.width = width;
675 mInputStream.height = height;
676 mInputStream.format = format;
677 mInputStream.id = streamId;
678
679 ALOGV("%s: Camera %d: Successfully created a new input stream ID %d",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800680 __FUNCTION__, mCameraId, streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700681
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800682 *newStreamId = streamId;
683 } else {
684 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
685 "Camera %d: Error creating new input stream: %s (%d)", mCameraId,
686 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700687 }
688
689 return res;
690}
691
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800692binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700693
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800694 binder::Status res;
695 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
696
697 if (inputSurface == NULL) {
698 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700699 }
700
701 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800702 if (!mDevice.get()) {
703 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
704 }
705 sp<IGraphicBufferProducer> producer;
706 status_t err = mDevice->getInputBufferProducer(&producer);
707 if (err != OK) {
708 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
709 "Camera %d: Error getting input Surface: %s (%d)",
710 mCameraId, strerror(-err), err);
711 } else {
712 inputSurface->name = String16("CameraInput");
713 inputSurface->graphicBufferProducer = producer;
714 }
715 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700716}
717
Eman Copty6d7af0e2016-06-17 20:46:40 -0700718bool CameraDeviceClient::isPublicFormat(int32_t format)
719{
720 switch(format) {
721 case HAL_PIXEL_FORMAT_RGBA_8888:
722 case HAL_PIXEL_FORMAT_RGBX_8888:
723 case HAL_PIXEL_FORMAT_RGB_888:
724 case HAL_PIXEL_FORMAT_RGB_565:
725 case HAL_PIXEL_FORMAT_BGRA_8888:
726 case HAL_PIXEL_FORMAT_YV12:
727 case HAL_PIXEL_FORMAT_Y8:
728 case HAL_PIXEL_FORMAT_Y16:
729 case HAL_PIXEL_FORMAT_RAW16:
730 case HAL_PIXEL_FORMAT_RAW10:
731 case HAL_PIXEL_FORMAT_RAW12:
732 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
733 case HAL_PIXEL_FORMAT_BLOB:
734 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
735 case HAL_PIXEL_FORMAT_YCbCr_420_888:
736 case HAL_PIXEL_FORMAT_YCbCr_422_888:
737 case HAL_PIXEL_FORMAT_YCbCr_444_888:
738 case HAL_PIXEL_FORMAT_FLEX_RGB_888:
739 case HAL_PIXEL_FORMAT_FLEX_RGBA_8888:
740 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
741 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
742 case HAL_PIXEL_FORMAT_YCbCr_422_I:
743 return true;
744 default:
745 return false;
746 }
747}
748
Ruben Brunkbba75572014-11-20 17:29:50 -0800749bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800750 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800751 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
752
753 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800754 (dataSpace == HAL_DATASPACE_DEPTH) ?
755 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800756 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
757
758 int32_t bestWidth = -1;
759 int32_t bestHeight = -1;
760
761 // Iterate through listed stream configurations and find the one with the smallest euclidean
762 // distance from the given dimensions for the given format.
763 for (size_t i = 0; i < streamConfigs.count; i += 4) {
764 int32_t fmt = streamConfigs.data.i32[i];
765 int32_t w = streamConfigs.data.i32[i + 1];
766 int32_t h = streamConfigs.data.i32[i + 2];
767
768 // Ignore input/output type for now
769 if (fmt == format) {
770 if (w == width && h == height) {
771 bestWidth = width;
772 bestHeight = height;
773 break;
774 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
775 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
776 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
777 bestWidth = w;
778 bestHeight = h;
779 }
780 }
781 }
782
783 if (bestWidth == -1) {
784 // Return false if no configurations for this format were listed
785 return false;
786 }
787
788 // Set the outputs to the closet width/height
789 if (outWidth != NULL) {
790 *outWidth = bestWidth;
791 }
792 if (outHeight != NULL) {
793 *outHeight = bestHeight;
794 }
795
796 // Return true if at least one configuration for this format was listed
797 return true;
798}
799
800int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
801 int64_t d0 = x0 - x1;
802 int64_t d1 = y0 - y1;
803 return d0 * d0 + d1 * d1;
804}
805
Igor Murashkine7ee7632013-06-11 18:10:18 -0700806// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800807binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
808 /*out*/
809 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700810{
811 ATRACE_CALL();
812 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
813
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800814 binder::Status res;
815 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700816
817 Mutex::Autolock icl(mBinderSerializationLock);
818
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800819 if (!mDevice.get()) {
820 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
821 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700822
823 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800824 status_t err;
825 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700826 request != NULL) {
827
828 request->swap(metadata);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -0800829 } else if (err == BAD_VALUE) {
830 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
831 "Camera %d: Template ID %d is invalid or not supported: %s (%d)",
832 mCameraId, templateId, strerror(-err), err);
833
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800834 } else {
835 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
836 "Camera %d: Error creating default request for template %d: %s (%d)",
837 mCameraId, templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700838 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700839 return res;
840}
841
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800842binder::Status CameraDeviceClient::getCameraInfo(
843 /*out*/
844 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700845{
846 ATRACE_CALL();
847 ALOGV("%s", __FUNCTION__);
848
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800849 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700850
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800851 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700852
853 Mutex::Autolock icl(mBinderSerializationLock);
854
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800855 if (!mDevice.get()) {
856 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
857 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700858
Igor Murashkin099b4572013-07-12 17:52:16 -0700859 if (info != NULL) {
860 *info = mDevice->info(); // static camera metadata
861 // TODO: merge with device-specific camera metadata
862 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700863
864 return res;
865}
866
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800867binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -0700868{
869 ATRACE_CALL();
870 ALOGV("%s", __FUNCTION__);
871
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800872 binder::Status res;
873 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -0700874
875 Mutex::Autolock icl(mBinderSerializationLock);
876
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800877 if (!mDevice.get()) {
878 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
879 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700880
881 // FIXME: Also need check repeating burst.
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700882 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700883 if (mStreamingRequestId != REQUEST_ID_NONE) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800884 String8 msg = String8::format(
885 "Camera %d: Try to waitUntilIdle when there are active streaming requests",
886 mCameraId);
887 ALOGE("%s: %s", __FUNCTION__, msg.string());
888 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -0700889 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800890 status_t err = mDevice->waitUntilDrained();
891 if (err != OK) {
892 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
893 "Camera %d: Error waiting to drain: %s (%d)",
894 mCameraId, strerror(-err), err);
895 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700896 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -0700897 return res;
898}
899
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800900binder::Status CameraDeviceClient::flush(
901 /*out*/
902 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700903 ATRACE_CALL();
904 ALOGV("%s", __FUNCTION__);
905
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800906 binder::Status res;
907 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700908
909 Mutex::Autolock icl(mBinderSerializationLock);
910
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800911 if (!mDevice.get()) {
912 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
913 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700914
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700915 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700916 mStreamingRequestId = REQUEST_ID_NONE;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800917 status_t err = mDevice->flush(lastFrameNumber);
918 if (err != OK) {
919 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
920 "Camera %d: Error flushing device: %s (%d)", mCameraId, strerror(-err), err);
921 }
922 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700923}
924
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800925binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700926 ATRACE_CALL();
927 ALOGV("%s", __FUNCTION__);
928
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800929 binder::Status res;
930 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700931
932 Mutex::Autolock icl(mBinderSerializationLock);
933
934 // Guard against trying to prepare non-created streams
935 ssize_t index = NAME_NOT_FOUND;
936 for (size_t i = 0; i < mStreamMap.size(); ++i) {
937 if (streamId == mStreamMap.valueAt(i)) {
938 index = i;
939 break;
940 }
941 }
942
943 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800944 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
945 "with that ID exists", mCameraId, streamId);
946 ALOGW("%s: %s", __FUNCTION__, msg.string());
947 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700948 }
949
Eino-Ville Talvala261394e2015-05-13 14:28:38 -0700950 // Also returns BAD_VALUE if stream ID was not valid, or stream already
951 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800952 status_t err = mDevice->prepare(streamId);
953 if (err == BAD_VALUE) {
954 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
955 "Camera %d: Stream %d has already been used, and cannot be prepared",
956 mCameraId, streamId);
957 } else if (err != OK) {
958 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
959 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
960 strerror(-err), err);
961 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700962 return res;
963}
964
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800965binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -0700966 ATRACE_CALL();
967 ALOGV("%s", __FUNCTION__);
968
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969 binder::Status res;
970 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700971
972 Mutex::Autolock icl(mBinderSerializationLock);
973
974 // Guard against trying to prepare non-created streams
975 ssize_t index = NAME_NOT_FOUND;
976 for (size_t i = 0; i < mStreamMap.size(); ++i) {
977 if (streamId == mStreamMap.valueAt(i)) {
978 index = i;
979 break;
980 }
981 }
982
983 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800984 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
985 "with that ID exists", mCameraId, streamId);
986 ALOGW("%s: %s", __FUNCTION__, msg.string());
987 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700988 }
989
990 if (maxCount <= 0) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800991 String8 msg = String8::format("Camera %d: maxCount (%d) must be greater than 0",
992 mCameraId, maxCount);
993 ALOGE("%s: %s", __FUNCTION__, msg.string());
994 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700995 }
996
997 // Also returns BAD_VALUE if stream ID was not valid, or stream already
998 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800999 status_t err = mDevice->prepare(maxCount, streamId);
1000 if (err == BAD_VALUE) {
1001 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1002 "Camera %d: Stream %d has already been used, and cannot be prepared",
1003 mCameraId, streamId);
1004 } else if (err != OK) {
1005 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1006 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
1007 strerror(-err), err);
1008 }
Ruben Brunkc78ac262015-08-13 17:58:46 -07001009
1010 return res;
1011}
1012
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001013binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001014 ATRACE_CALL();
1015 ALOGV("%s", __FUNCTION__);
1016
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001017 binder::Status res;
1018 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001019
1020 Mutex::Autolock icl(mBinderSerializationLock);
1021
1022 // Guard against trying to prepare non-created streams
1023 ssize_t index = NAME_NOT_FOUND;
1024 for (size_t i = 0; i < mStreamMap.size(); ++i) {
1025 if (streamId == mStreamMap.valueAt(i)) {
1026 index = i;
1027 break;
1028 }
1029 }
1030
1031 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001032 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
1033 "with that ID exists", mCameraId, streamId);
1034 ALOGW("%s: %s", __FUNCTION__, msg.string());
1035 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001036 }
1037
1038 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
1039 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001040 status_t err = mDevice->tearDown(streamId);
1041 if (err == BAD_VALUE) {
1042 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1043 "Camera %d: Stream %d is still in use, cannot be torn down",
1044 mCameraId, streamId);
1045 } else if (err != OK) {
1046 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1047 "Camera %d: Error tearing down stream %d: %s (%d)", mCameraId, streamId,
1048 strerror(-err), err);
1049 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001050
1051 return res;
1052}
1053
Zhijun He5d677d12016-05-29 16:52:39 -07001054binder::Status CameraDeviceClient::setDeferredConfiguration(int32_t streamId,
1055 const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
1056 ATRACE_CALL();
1057
1058 binder::Status res;
1059 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
1060
1061 Mutex::Autolock icl(mBinderSerializationLock);
1062
1063 sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
1064
1065 // Client code should guarantee that the surface is from SurfaceView or SurfaceTexture.
1066 if (bufferProducer == NULL) {
1067 ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
1068 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
1069 }
1070 // Check if this stram id is one of the deferred streams
1071 ssize_t index = NAME_NOT_FOUND;
1072 for (size_t i = 0; i < mDeferredStreams.size(); i++) {
1073 if (streamId == mDeferredStreams[i]) {
1074 index = i;
1075 break;
1076 }
1077 }
1078 if (index == NAME_NOT_FOUND) {
1079 String8 msg = String8::format("Camera %d: deferred surface is set to a unknown stream"
1080 "(ID %d)", mCameraId, streamId);
1081 ALOGW("%s: %s", __FUNCTION__, msg.string());
1082 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
1083 }
1084
1085 if (!mDevice.get()) {
1086 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
1087 }
1088
1089 // Don't create multiple streams for the same target surface
1090 {
1091 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
1092 if (index != NAME_NOT_FOUND) {
1093 String8 msg = String8::format("Camera %d: Surface already has a stream created "
1094 " for it (ID %zd)", mCameraId, index);
1095 ALOGW("%s: %s", __FUNCTION__, msg.string());
1096 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
1097 }
1098 }
1099
1100 status_t err;
1101
1102 // Always set to async, as we know the deferred surface is for preview streaming.
1103 sp<Surface> consumerSurface = new Surface(bufferProducer, /*useAsync*/true);
1104
1105 // Finish the deferred stream configuration with the surface.
1106 err = mDevice->setConsumerSurface(streamId, consumerSurface);
1107 if (err == OK) {
1108 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
1109 mStreamMap.add(binder, streamId);
1110 mDeferredStreams.removeItemsAt(index);
1111 } else if (err == NO_INIT) {
1112 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
1113 "Camera %d: Deferred surface is invalid: %s (%d)",
1114 mCameraId, strerror(-err), err);
1115 } else {
1116 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
1117 "Camera %d: Error setting output stream deferred surface: %s (%d)",
1118 mCameraId, strerror(-err), err);
1119 }
1120
1121 return res;
1122}
1123
Igor Murashkine7ee7632013-06-11 18:10:18 -07001124status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -08001125 return BasicClient::dump(fd, args);
1126}
1127
1128status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001129 String8 result;
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001130 result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n",
Igor Murashkine7ee7632013-06-11 18:10:18 -07001131 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -08001132 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -08001133 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Ruben Brunkcc776712015-02-17 20:18:47 -08001134 result.appendFormat(" Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001135
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001136 result.append(" State:\n");
1137 result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001138 if (mInputStream.configured) {
1139 result.appendFormat(" Current input stream ID: %d\n",
1140 mInputStream.id);
1141 } else {
1142 result.append(" No input stream configured.\n");
1143 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001144 if (!mStreamMap.isEmpty()) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001145 result.append(" Current output stream IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001146 for (size_t i = 0; i < mStreamMap.size(); i++) {
1147 result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i));
1148 }
Zhijun He5d677d12016-05-29 16:52:39 -07001149 } else if (!mDeferredStreams.isEmpty()) {
1150 result.append(" Current deferred surface output stream IDs:\n");
1151 for (auto& streamId : mDeferredStreams) {
1152 result.appendFormat(" Stream %d\n", streamId);
1153 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001154 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001155 result.append(" No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -07001156 }
1157 write(fd, result.string(), result.size());
Igor Murashkine7ee7632013-06-11 18:10:18 -07001158 // TODO: print dynamic/request section from most recent requests
1159 mFrameProcessor->dump(fd, args);
1160
1161 return dumpDevice(fd, args);
1162}
1163
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001164void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -07001165 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001166 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001167 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168
1169 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001170 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001171 }
1172}
1173
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001174void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
1175 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
1176
1177 if (remoteCb != 0) {
1178 remoteCb->onRepeatingRequestError(lastFrameNumber);
1179 }
1180
Shuzhen Wangc9ca6782016-04-26 13:40:31 -07001181 Mutex::Autolock idLock(mStreamingRequestIdLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07001182 mStreamingRequestId = REQUEST_ID_NONE;
1183}
1184
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001185void CameraDeviceClient::notifyIdle() {
1186 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001187 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001188
1189 if (remoteCb != 0) {
1190 remoteCb->onDeviceIdle();
1191 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001192 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001193}
1194
Jianing Weicb0652e2014-03-12 18:29:36 -07001195void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001196 nsecs_t timestamp) {
1197 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001198 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001200 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001201 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -07001202 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001203}
1204
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001205void CameraDeviceClient::notifyPrepared(int streamId) {
1206 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001207 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001208 if (remoteCb != 0) {
1209 remoteCb->onPrepared(streamId);
1210 }
1211}
1212
Igor Murashkine7ee7632013-06-11 18:10:18 -07001213void CameraDeviceClient::detachDevice() {
1214 if (mDevice == 0) return;
1215
1216 ALOGV("Camera %d: Stopping processors", mCameraId);
1217
1218 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1219 FRAME_PROCESSOR_LISTENER_MAX_ID,
1220 /*listener*/this);
1221 mFrameProcessor->requestExit();
1222 ALOGV("Camera %d: Waiting for threads", mCameraId);
1223 mFrameProcessor->join();
1224 ALOGV("Camera %d: Disconnecting device", mCameraId);
1225
1226 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1227 {
1228 mDevice->clearStreamingRequest();
1229
1230 status_t code;
1231 if ((code = mDevice->waitUntilDrained()) != OK) {
1232 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1233 code);
1234 }
1235 }
1236
1237 Camera2ClientBase::detachDevice();
1238}
1239
1240/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001241void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001242 ATRACE_CALL();
1243 ALOGV("%s", __FUNCTION__);
1244
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001245 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001246 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001247 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001248 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001249 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001250}
1251
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001252binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
Eino-Ville Talvala6192b892016-04-04 12:31:18 -07001253 if (mDisconnected) {
1254 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
1255 "The camera device has been disconnected");
1256 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001257 status_t res = checkPid(checkLocation);
1258 return (res == OK) ? binder::Status::ok() :
1259 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1260 "Attempt to use camera from a different process than original client");
1261}
1262
Igor Murashkine7ee7632013-06-11 18:10:18 -07001263// TODO: move to Camera2ClientBase
1264bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1265
1266 const int pid = IPCThreadState::self()->getCallingPid();
1267 const int selfPid = getpid();
1268 camera_metadata_entry_t entry;
1269
1270 /**
1271 * Mixin default important security values
1272 * - android.led.transmit = defaulted ON
1273 */
1274 CameraMetadata staticInfo = mDevice->info();
1275 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1276 for(size_t i = 0; i < entry.count; ++i) {
1277 uint8_t led = entry.data.u8[i];
1278
1279 switch(led) {
1280 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1281 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1282 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1283 metadata.update(ANDROID_LED_TRANSMIT,
1284 &transmitDefault, 1);
1285 }
1286 break;
1287 }
1288 }
1289 }
1290
1291 // We can do anything!
1292 if (pid == selfPid) {
1293 return true;
1294 }
1295
1296 /**
1297 * Permission check special fields in the request
1298 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1299 */
1300 entry = metadata.find(ANDROID_LED_TRANSMIT);
1301 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1302 String16 permissionString =
1303 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1304 if (!checkCallingPermission(permissionString)) {
1305 const int uid = IPCThreadState::self()->getCallingUid();
1306 ALOGE("Permission Denial: "
1307 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1308 return false;
1309 }
1310 }
1311
1312 return true;
1313}
1314
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001315status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1316 ALOGV("%s: begin", __FUNCTION__);
1317
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001318 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001319 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001320}
1321
Igor Murashkine7ee7632013-06-11 18:10:18 -07001322} // namespace android