blob: dde177908be73ed774955e8250cda6091d9967c1 [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
31
Igor Murashkine7ee7632013-06-11 18:10:18 -070032
33namespace android {
34using namespace camera2;
35
36CameraDeviceClientBase::CameraDeviceClientBase(
37 const sp<CameraService>& cameraService,
38 const sp<ICameraDeviceCallbacks>& remoteCallback,
39 const String16& clientPackageName,
40 int cameraId,
41 int cameraFacing,
42 int clientPid,
43 uid_t clientUid,
44 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080045 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080046 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080047 clientPackageName,
48 cameraId,
49 cameraFacing,
50 clientPid,
51 clientUid,
52 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070053 mRemoteCallback(remoteCallback) {
54}
Igor Murashkine7ee7632013-06-11 18:10:18 -070055
56// Interface used by CameraService
57
58CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
59 const sp<ICameraDeviceCallbacks>& remoteCallback,
60 const String16& clientPackageName,
61 int cameraId,
62 int cameraFacing,
63 int clientPid,
64 uid_t clientUid,
65 int servicePid) :
66 Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
67 cameraId, cameraFacing, clientPid, clientUid, servicePid),
68 mRequestIdCounter(0) {
69
70 ATRACE_CALL();
71 ALOGI("CameraDeviceClient %d: Opened", cameraId);
72}
73
Yin-Chia Yehe074a932015-01-30 10:29:02 -080074status_t CameraDeviceClient::initialize(CameraModule *module)
Igor Murashkine7ee7632013-06-11 18:10:18 -070075{
76 ATRACE_CALL();
77 status_t res;
78
79 res = Camera2ClientBase::initialize(module);
80 if (res != OK) {
81 return res;
82 }
83
84 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070085 mFrameProcessor = new FrameProcessorBase(mDevice);
Igor Murashkine7ee7632013-06-11 18:10:18 -070086 threadName = String8::format("CDU-%d-FrameProc", mCameraId);
87 mFrameProcessor->run(threadName.string());
88
89 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
90 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -080091 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -070092 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -070093
94 return OK;
95}
96
97CameraDeviceClient::~CameraDeviceClient() {
98}
99
100status_t CameraDeviceClient::submitRequest(sp<CaptureRequest> request,
Jianing Weicb0652e2014-03-12 18:29:36 -0700101 bool streaming,
102 /*out*/
103 int64_t* lastFrameNumber) {
104 List<sp<CaptureRequest> > requestList;
105 requestList.push_back(request);
106 return submitRequestList(requestList, streaming, lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700107}
108
Jianing Wei90e59c92014-03-12 18:29:36 -0700109status_t CameraDeviceClient::submitRequestList(List<sp<CaptureRequest> > requests,
Jianing Weicb0652e2014-03-12 18:29:36 -0700110 bool streaming, int64_t* lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700111 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700112 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700113
114 status_t res;
115 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
116
117 Mutex::Autolock icl(mBinderSerializationLock);
118
119 if (!mDevice.get()) return DEAD_OBJECT;
120
121 if (requests.empty()) {
122 ALOGE("%s: Camera %d: Sent null request. Rejecting request.",
123 __FUNCTION__, mCameraId);
124 return BAD_VALUE;
125 }
126
127 List<const CameraMetadata> metadataRequestList;
128 int32_t requestId = mRequestIdCounter;
129 uint32_t loopCounter = 0;
130
131 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); ++it) {
132 sp<CaptureRequest> request = *it;
133 if (request == 0) {
134 ALOGE("%s: Camera %d: Sent null request.",
135 __FUNCTION__, mCameraId);
136 return BAD_VALUE;
137 }
138
139 CameraMetadata metadata(request->mMetadata);
140 if (metadata.isEmpty()) {
141 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.",
142 __FUNCTION__, mCameraId);
143 return BAD_VALUE;
144 } else if (request->mSurfaceList.isEmpty()) {
145 ALOGE("%s: Camera %d: Requests must have at least one surface target. "
146 "Rejecting request.", __FUNCTION__, mCameraId);
147 return BAD_VALUE;
148 }
149
150 if (!enforceRequestPermissions(metadata)) {
151 // Callee logs
152 return PERMISSION_DENIED;
153 }
154
155 /**
156 * Write in the output stream IDs which we calculate from
157 * the capture request's list of surface targets
158 */
159 Vector<int32_t> outputStreamIds;
160 outputStreamIds.setCapacity(request->mSurfaceList.size());
Jianing Weicb0652e2014-03-12 18:29:36 -0700161 for (size_t i = 0; i < request->mSurfaceList.size(); ++i) {
162 sp<Surface> surface = request->mSurfaceList[i];
Jianing Wei90e59c92014-03-12 18:29:36 -0700163 if (surface == 0) continue;
164
165 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Marco Nelissenf8880202014-11-14 07:58:25 -0800166 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
Jianing Wei90e59c92014-03-12 18:29:36 -0700167
168 // Trying to submit request with surface that wasn't created
169 if (idx == NAME_NOT_FOUND) {
170 ALOGE("%s: Camera %d: Tried to submit a request with a surface that"
171 " we have not called createStream on",
172 __FUNCTION__, mCameraId);
173 return BAD_VALUE;
174 }
175
176 int streamId = mStreamMap.valueAt(idx);
177 outputStreamIds.push_back(streamId);
178 ALOGV("%s: Camera %d: Appending output stream %d to request",
179 __FUNCTION__, mCameraId, streamId);
180 }
181
182 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
183 outputStreamIds.size());
184
185 metadata.update(ANDROID_REQUEST_ID, &requestId, /*size*/1);
186 loopCounter++; // loopCounter starts from 1
Mark Salyzyn50468412014-06-18 16:33:43 -0700187 ALOGV("%s: Camera %d: Creating request with ID %d (%d of %zu)",
Jianing Wei90e59c92014-03-12 18:29:36 -0700188 __FUNCTION__, mCameraId, requestId, loopCounter, requests.size());
189
190 metadataRequestList.push_back(metadata);
191 }
192 mRequestIdCounter++;
193
194 if (streaming) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700195 res = mDevice->setStreamingRequestList(metadataRequestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700196 if (res != OK) {
197 ALOGE("%s: Camera %d: Got error %d after trying to set streaming "
198 "request", __FUNCTION__, mCameraId, res);
199 } else {
200 mStreamingRequestList.push_back(requestId);
201 }
202 } else {
Jianing Weicb0652e2014-03-12 18:29:36 -0700203 res = mDevice->captureList(metadataRequestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700204 if (res != OK) {
205 ALOGE("%s: Camera %d: Got error %d after trying to set capture",
206 __FUNCTION__, mCameraId, res);
207 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700208 ALOGV("%s: requestId = %d ", __FUNCTION__, requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700209 }
210
211 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId);
212 if (res == OK) {
213 return requestId;
214 }
215
216 return res;
217}
218
Jianing Weicb0652e2014-03-12 18:29:36 -0700219status_t CameraDeviceClient::cancelRequest(int requestId, int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700220 ATRACE_CALL();
221 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
222
223 status_t res;
224
225 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
226
227 Mutex::Autolock icl(mBinderSerializationLock);
228
229 if (!mDevice.get()) return DEAD_OBJECT;
230
231 Vector<int>::iterator it, end;
232 for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end();
233 it != end; ++it) {
234 if (*it == requestId) {
235 break;
236 }
237 }
238
239 if (it == end) {
240 ALOGE("%s: Camera%d: Did not find request id %d in list of streaming "
241 "requests", __FUNCTION__, mCameraId, requestId);
242 return BAD_VALUE;
243 }
244
Jianing Weicb0652e2014-03-12 18:29:36 -0700245 res = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700246
247 if (res == OK) {
248 ALOGV("%s: Camera %d: Successfully cleared streaming request",
249 __FUNCTION__, mCameraId);
250 mStreamingRequestList.erase(it);
251 }
252
253 return res;
254}
255
Ruben Brunkb2119af2014-05-09 19:57:56 -0700256status_t CameraDeviceClient::beginConfigure() {
257 // TODO: Implement this.
258 ALOGE("%s: Not implemented yet.", __FUNCTION__);
259 return OK;
260}
261
262status_t CameraDeviceClient::endConfigure() {
Igor Murashkine2d167e2014-08-19 16:19:59 -0700263 ALOGV("%s: ending configure (%zu streams)",
264 __FUNCTION__, mStreamMap.size());
265
266 status_t res;
267 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
268
269 Mutex::Autolock icl(mBinderSerializationLock);
270
271 if (!mDevice.get()) return DEAD_OBJECT;
272
273 return mDevice->configureStreams();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700274}
275
Igor Murashkine7ee7632013-06-11 18:10:18 -0700276status_t CameraDeviceClient::deleteStream(int streamId) {
277 ATRACE_CALL();
278 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
279
280 status_t res;
281 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
282
283 Mutex::Autolock icl(mBinderSerializationLock);
284
285 if (!mDevice.get()) return DEAD_OBJECT;
286
287 // Guard against trying to delete non-created streams
288 ssize_t index = NAME_NOT_FOUND;
289 for (size_t i = 0; i < mStreamMap.size(); ++i) {
290 if (streamId == mStreamMap.valueAt(i)) {
291 index = i;
292 break;
293 }
294 }
295
296 if (index == NAME_NOT_FOUND) {
297 ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream "
298 "created yet", __FUNCTION__, mCameraId, streamId);
299 return BAD_VALUE;
300 }
301
302 // Also returns BAD_VALUE if stream ID was not valid
303 res = mDevice->deleteStream(streamId);
304
305 if (res == BAD_VALUE) {
306 ALOGE("%s: Camera %d: Unexpected BAD_VALUE when deleting stream, but we"
307 " already checked and the stream ID (%d) should be valid.",
308 __FUNCTION__, mCameraId, streamId);
309 } else if (res == OK) {
310 mStreamMap.removeItemsAt(index);
311
Igor Murashkine7ee7632013-06-11 18:10:18 -0700312 }
313
314 return res;
315}
316
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800317status_t CameraDeviceClient::createStream(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700318 const sp<IGraphicBufferProducer>& bufferProducer)
319{
320 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700321
322 status_t res;
323 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
324
325 Mutex::Autolock icl(mBinderSerializationLock);
326
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700327 if (bufferProducer == NULL) {
328 ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
329 return BAD_VALUE;
330 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700331 if (!mDevice.get()) return DEAD_OBJECT;
332
333 // Don't create multiple streams for the same target surface
334 {
Marco Nelissenf8880202014-11-14 07:58:25 -0800335 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
Igor Murashkine7ee7632013-06-11 18:10:18 -0700336 if (index != NAME_NOT_FOUND) {
337 ALOGW("%s: Camera %d: Buffer producer already has a stream for it "
Colin Crosse5729fa2014-03-21 15:04:25 -0700338 "(ID %zd)",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700339 __FUNCTION__, mCameraId, index);
340 return ALREADY_EXISTS;
341 }
342 }
343
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700344 // HACK b/10949105
345 // Query consumer usage bits to set async operation mode for
346 // GLConsumer using controlledByApp parameter.
347 bool useAsync = false;
348 int32_t consumerUsage;
349 if ((res = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
350 &consumerUsage)) != OK) {
351 ALOGE("%s: Camera %d: Failed to query consumer usage", __FUNCTION__,
352 mCameraId);
353 return res;
354 }
355 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
356 ALOGW("%s: Camera %d: Forcing asynchronous mode for stream",
357 __FUNCTION__, mCameraId);
358 useAsync = true;
359 }
360
Ruben Brunkbba75572014-11-20 17:29:50 -0800361 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
362 GRALLOC_USAGE_RENDERSCRIPT;
363 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
364 GraphicBuffer::USAGE_HW_TEXTURE |
365 GraphicBuffer::USAGE_HW_COMPOSER;
366 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
367 (consumerUsage & allowedFlags) != 0;
368
Marco Nelissenf8880202014-11-14 07:58:25 -0800369 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800370 sp<ANativeWindow> anw = new Surface(bufferProducer, useAsync);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700371
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800372 int width, height, format;
373 android_dataspace dataSpace;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700374
375 if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
376 ALOGE("%s: Camera %d: Failed to query Surface width", __FUNCTION__,
377 mCameraId);
378 return res;
379 }
380 if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) {
381 ALOGE("%s: Camera %d: Failed to query Surface height", __FUNCTION__,
382 mCameraId);
383 return res;
384 }
385 if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &format)) != OK) {
386 ALOGE("%s: Camera %d: Failed to query Surface format", __FUNCTION__,
387 mCameraId);
388 return res;
389 }
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800390 if ((res = anw->query(anw.get(), NATIVE_WINDOW_DEFAULT_DATASPACE,
391 reinterpret_cast<int*>(&dataSpace))) != OK) {
392 ALOGE("%s: Camera %d: Failed to query Surface dataSpace", __FUNCTION__,
393 mCameraId);
394 return res;
395 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700396
397 // FIXME: remove this override since the default format should be
398 // IMPLEMENTATION_DEFINED. b/9487482
Igor Murashkin15811012013-07-29 12:25:59 -0700399 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
400 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
Ruben Brunkbba75572014-11-20 17:29:50 -0800401 ALOGW("%s: Camera %d: Overriding format %#x to IMPLEMENTATION_DEFINED",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700402 __FUNCTION__, mCameraId, format);
403 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
404 }
405
Ruben Brunkbba75572014-11-20 17:29:50 -0800406 // Round dimensions to the nearest dimensions available for this format
407 if (flexibleConsumer && !CameraDeviceClient::roundBufferDimensionNearest(width, height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800408 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
Ruben Brunkbba75572014-11-20 17:29:50 -0800409 ALOGE("%s: No stream configurations with the format %#x defined, failed to create stream.",
410 __FUNCTION__, format);
411 return BAD_VALUE;
412 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700413
414 int streamId = -1;
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800415 res = mDevice->createStream(anw, width, height, format, dataSpace,
416 &streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700417
418 if (res == OK) {
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800419 mStreamMap.add(binder, streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700420
421 ALOGV("%s: Camera %d: Successfully created a new stream ID %d",
422 __FUNCTION__, mCameraId, streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700423
424 /**
425 * Set the stream transform flags to automatically
426 * rotate the camera stream for preview use cases.
427 */
428 int32_t transform = 0;
429 res = getRotationTransformLocked(&transform);
430
431 if (res != OK) {
432 // Error logged by getRotationTransformLocked.
433 return res;
434 }
435
436 res = mDevice->setStreamTransform(streamId, transform);
437 if (res != OK) {
438 ALOGE("%s: Failed to set stream transform (stream id %d)",
439 __FUNCTION__, streamId);
440 return res;
441 }
442
Igor Murashkine7ee7632013-06-11 18:10:18 -0700443 return streamId;
444 }
445
446 return res;
447}
448
Ruben Brunkbba75572014-11-20 17:29:50 -0800449
450bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800451 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800452 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
453
454 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800455 (dataSpace == HAL_DATASPACE_DEPTH) ?
456 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800457 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
458
459 int32_t bestWidth = -1;
460 int32_t bestHeight = -1;
461
462 // Iterate through listed stream configurations and find the one with the smallest euclidean
463 // distance from the given dimensions for the given format.
464 for (size_t i = 0; i < streamConfigs.count; i += 4) {
465 int32_t fmt = streamConfigs.data.i32[i];
466 int32_t w = streamConfigs.data.i32[i + 1];
467 int32_t h = streamConfigs.data.i32[i + 2];
468
469 // Ignore input/output type for now
470 if (fmt == format) {
471 if (w == width && h == height) {
472 bestWidth = width;
473 bestHeight = height;
474 break;
475 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
476 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
477 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
478 bestWidth = w;
479 bestHeight = h;
480 }
481 }
482 }
483
484 if (bestWidth == -1) {
485 // Return false if no configurations for this format were listed
486 return false;
487 }
488
489 // Set the outputs to the closet width/height
490 if (outWidth != NULL) {
491 *outWidth = bestWidth;
492 }
493 if (outHeight != NULL) {
494 *outHeight = bestHeight;
495 }
496
497 // Return true if at least one configuration for this format was listed
498 return true;
499}
500
501int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
502 int64_t d0 = x0 - x1;
503 int64_t d1 = y0 - y1;
504 return d0 * d0 + d1 * d1;
505}
506
Igor Murashkine7ee7632013-06-11 18:10:18 -0700507// Create a request object from a template.
508status_t CameraDeviceClient::createDefaultRequest(int templateId,
509 /*out*/
510 CameraMetadata* request)
511{
512 ATRACE_CALL();
513 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
514
515 status_t res;
516 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
517
518 Mutex::Autolock icl(mBinderSerializationLock);
519
520 if (!mDevice.get()) return DEAD_OBJECT;
521
522 CameraMetadata metadata;
523 if ( (res = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
524 request != NULL) {
525
526 request->swap(metadata);
527 }
528
529 return res;
530}
531
Igor Murashkin099b4572013-07-12 17:52:16 -0700532status_t CameraDeviceClient::getCameraInfo(/*out*/CameraMetadata* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700533{
534 ATRACE_CALL();
535 ALOGV("%s", __FUNCTION__);
536
537 status_t res = OK;
538
Igor Murashkine7ee7632013-06-11 18:10:18 -0700539 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
540
541 Mutex::Autolock icl(mBinderSerializationLock);
542
543 if (!mDevice.get()) return DEAD_OBJECT;
544
Igor Murashkin099b4572013-07-12 17:52:16 -0700545 if (info != NULL) {
546 *info = mDevice->info(); // static camera metadata
547 // TODO: merge with device-specific camera metadata
548 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700549
550 return res;
551}
552
Zhijun He2ab500c2013-07-23 08:02:53 -0700553status_t CameraDeviceClient::waitUntilIdle()
554{
555 ATRACE_CALL();
556 ALOGV("%s", __FUNCTION__);
557
558 status_t res = OK;
559 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
560
561 Mutex::Autolock icl(mBinderSerializationLock);
562
563 if (!mDevice.get()) return DEAD_OBJECT;
564
565 // FIXME: Also need check repeating burst.
566 if (!mStreamingRequestList.isEmpty()) {
567 ALOGE("%s: Camera %d: Try to waitUntilIdle when there are active streaming requests",
568 __FUNCTION__, mCameraId);
569 return INVALID_OPERATION;
570 }
571 res = mDevice->waitUntilDrained();
572 ALOGV("%s Done", __FUNCTION__);
573
574 return res;
575}
576
Jianing Weicb0652e2014-03-12 18:29:36 -0700577status_t CameraDeviceClient::flush(int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700578 ATRACE_CALL();
579 ALOGV("%s", __FUNCTION__);
580
581 status_t res = OK;
582 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
583
584 Mutex::Autolock icl(mBinderSerializationLock);
585
586 if (!mDevice.get()) return DEAD_OBJECT;
587
Jianing Wei3c76fa32014-04-21 11:34:34 -0700588 mStreamingRequestList.clear();
Jianing Weicb0652e2014-03-12 18:29:36 -0700589 return mDevice->flush(lastFrameNumber);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700590}
591
Igor Murashkine7ee7632013-06-11 18:10:18 -0700592status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
593 String8 result;
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700594 result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700595 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800596 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -0800597 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700598 result.appendFormat(" Current client: %s (PID %d, UID %u)\n",
599 String8(mClientPackageName).string(),
600 mClientPid, mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700601
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700602 result.append(" State:\n");
603 result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter);
604 if (!mStreamMap.isEmpty()) {
605 result.append(" Current stream IDs:\n");
606 for (size_t i = 0; i < mStreamMap.size(); i++) {
607 result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i));
608 }
609 } else {
610 result.append(" No streams configured.\n");
611 }
612 write(fd, result.string(), result.size());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700613 // TODO: print dynamic/request section from most recent requests
614 mFrameProcessor->dump(fd, args);
615
616 return dumpDevice(fd, args);
617}
618
Jianing Weicb0652e2014-03-12 18:29:36 -0700619void CameraDeviceClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
620 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700621 // Thread safe. Don't bother locking.
622 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
623
624 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700625 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700626 }
627}
628
629void CameraDeviceClient::notifyIdle() {
630 // Thread safe. Don't bother locking.
631 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
632
633 if (remoteCb != 0) {
634 remoteCb->onDeviceIdle();
635 }
636}
637
Jianing Weicb0652e2014-03-12 18:29:36 -0700638void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700639 nsecs_t timestamp) {
640 // Thread safe. Don't bother locking.
641 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
642 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700643 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700644 }
645}
646
Igor Murashkine7ee7632013-06-11 18:10:18 -0700647// TODO: refactor the code below this with IProCameraUser.
648// it's 100% copy-pasted, so lets not change it right now to make it easier.
649
650void CameraDeviceClient::detachDevice() {
651 if (mDevice == 0) return;
652
653 ALOGV("Camera %d: Stopping processors", mCameraId);
654
655 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
656 FRAME_PROCESSOR_LISTENER_MAX_ID,
657 /*listener*/this);
658 mFrameProcessor->requestExit();
659 ALOGV("Camera %d: Waiting for threads", mCameraId);
660 mFrameProcessor->join();
661 ALOGV("Camera %d: Disconnecting device", mCameraId);
662
663 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
664 {
665 mDevice->clearStreamingRequest();
666
667 status_t code;
668 if ((code = mDevice->waitUntilDrained()) != OK) {
669 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
670 code);
671 }
672 }
673
674 Camera2ClientBase::detachDevice();
675}
676
677/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -0700678void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700679 ATRACE_CALL();
680 ALOGV("%s", __FUNCTION__);
681
Igor Murashkin4fb55c12013-08-29 17:43:01 -0700682 // Thread-safe. No lock necessary.
683 sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
684 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700685 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700686 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700687}
688
689// TODO: move to Camera2ClientBase
690bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
691
692 const int pid = IPCThreadState::self()->getCallingPid();
693 const int selfPid = getpid();
694 camera_metadata_entry_t entry;
695
696 /**
697 * Mixin default important security values
698 * - android.led.transmit = defaulted ON
699 */
700 CameraMetadata staticInfo = mDevice->info();
701 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
702 for(size_t i = 0; i < entry.count; ++i) {
703 uint8_t led = entry.data.u8[i];
704
705 switch(led) {
706 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
707 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
708 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
709 metadata.update(ANDROID_LED_TRANSMIT,
710 &transmitDefault, 1);
711 }
712 break;
713 }
714 }
715 }
716
717 // We can do anything!
718 if (pid == selfPid) {
719 return true;
720 }
721
722 /**
723 * Permission check special fields in the request
724 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
725 */
726 entry = metadata.find(ANDROID_LED_TRANSMIT);
727 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
728 String16 permissionString =
729 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
730 if (!checkCallingPermission(permissionString)) {
731 const int uid = IPCThreadState::self()->getCallingUid();
732 ALOGE("Permission Denial: "
733 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
734 return false;
735 }
736 }
737
738 return true;
739}
740
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700741status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
742 ALOGV("%s: begin", __FUNCTION__);
743
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700744 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -0700745 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700746}
747
Igor Murashkine7ee7632013-06-11 18:10:18 -0700748} // namespace android