blob: 3d85e90b0ed200ec3401d63076e7aa38af5b9c57 [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>
26
27#include "common/CameraDeviceBase.h"
28#include "api2/CameraDeviceClient.h"
29
30
Igor Murashkine7ee7632013-06-11 18:10:18 -070031
32namespace android {
33using namespace camera2;
34
35CameraDeviceClientBase::CameraDeviceClientBase(
36 const sp<CameraService>& cameraService,
37 const sp<ICameraDeviceCallbacks>& remoteCallback,
38 const String16& clientPackageName,
39 int cameraId,
40 int cameraFacing,
41 int clientPid,
42 uid_t clientUid,
43 int servicePid) :
44 BasicClient(cameraService, remoteCallback->asBinder(), clientPackageName,
45 cameraId, cameraFacing, clientPid, clientUid, servicePid),
46 mRemoteCallback(remoteCallback) {
47}
Igor Murashkine7ee7632013-06-11 18:10:18 -070048
49// Interface used by CameraService
50
51CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
52 const sp<ICameraDeviceCallbacks>& remoteCallback,
53 const String16& clientPackageName,
54 int cameraId,
55 int cameraFacing,
56 int clientPid,
57 uid_t clientUid,
58 int servicePid) :
59 Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
60 cameraId, cameraFacing, clientPid, clientUid, servicePid),
61 mRequestIdCounter(0) {
62
63 ATRACE_CALL();
64 ALOGI("CameraDeviceClient %d: Opened", cameraId);
65}
66
67status_t CameraDeviceClient::initialize(camera_module_t *module)
68{
69 ATRACE_CALL();
70 status_t res;
71
72 res = Camera2ClientBase::initialize(module);
73 if (res != OK) {
74 return res;
75 }
76
77 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070078 mFrameProcessor = new FrameProcessorBase(mDevice);
Igor Murashkine7ee7632013-06-11 18:10:18 -070079 threadName = String8::format("CDU-%d-FrameProc", mCameraId);
80 mFrameProcessor->run(threadName.string());
81
82 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
83 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -080084 /*listener*/this,
85 /*quirkSendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -070086
87 return OK;
88}
89
90CameraDeviceClient::~CameraDeviceClient() {
91}
92
93status_t CameraDeviceClient::submitRequest(sp<CaptureRequest> request,
Jianing Weicb0652e2014-03-12 18:29:36 -070094 bool streaming,
95 /*out*/
96 int64_t* lastFrameNumber) {
97 List<sp<CaptureRequest> > requestList;
98 requestList.push_back(request);
99 return submitRequestList(requestList, streaming, lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700100}
101
Jianing Wei90e59c92014-03-12 18:29:36 -0700102status_t CameraDeviceClient::submitRequestList(List<sp<CaptureRequest> > requests,
Jianing Weicb0652e2014-03-12 18:29:36 -0700103 bool streaming, int64_t* lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700104 ATRACE_CALL();
Jianing Weicb0652e2014-03-12 18:29:36 -0700105 ALOGV("%s-start of function. Request list size %d", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700106
107 status_t res;
108 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
109
110 Mutex::Autolock icl(mBinderSerializationLock);
111
112 if (!mDevice.get()) return DEAD_OBJECT;
113
114 if (requests.empty()) {
115 ALOGE("%s: Camera %d: Sent null request. Rejecting request.",
116 __FUNCTION__, mCameraId);
117 return BAD_VALUE;
118 }
119
120 List<const CameraMetadata> metadataRequestList;
121 int32_t requestId = mRequestIdCounter;
122 uint32_t loopCounter = 0;
123
124 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end(); ++it) {
125 sp<CaptureRequest> request = *it;
126 if (request == 0) {
127 ALOGE("%s: Camera %d: Sent null request.",
128 __FUNCTION__, mCameraId);
129 return BAD_VALUE;
130 }
131
132 CameraMetadata metadata(request->mMetadata);
133 if (metadata.isEmpty()) {
134 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.",
135 __FUNCTION__, mCameraId);
136 return BAD_VALUE;
137 } else if (request->mSurfaceList.isEmpty()) {
138 ALOGE("%s: Camera %d: Requests must have at least one surface target. "
139 "Rejecting request.", __FUNCTION__, mCameraId);
140 return BAD_VALUE;
141 }
142
143 if (!enforceRequestPermissions(metadata)) {
144 // Callee logs
145 return PERMISSION_DENIED;
146 }
147
148 /**
149 * Write in the output stream IDs which we calculate from
150 * the capture request's list of surface targets
151 */
152 Vector<int32_t> outputStreamIds;
153 outputStreamIds.setCapacity(request->mSurfaceList.size());
Jianing Weicb0652e2014-03-12 18:29:36 -0700154 for (size_t i = 0; i < request->mSurfaceList.size(); ++i) {
155 sp<Surface> surface = request->mSurfaceList[i];
Jianing Wei90e59c92014-03-12 18:29:36 -0700156 if (surface == 0) continue;
157
158 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
159 int idx = mStreamMap.indexOfKey(gbp->asBinder());
160
161 // Trying to submit request with surface that wasn't created
162 if (idx == NAME_NOT_FOUND) {
163 ALOGE("%s: Camera %d: Tried to submit a request with a surface that"
164 " we have not called createStream on",
165 __FUNCTION__, mCameraId);
166 return BAD_VALUE;
167 }
168
169 int streamId = mStreamMap.valueAt(idx);
170 outputStreamIds.push_back(streamId);
171 ALOGV("%s: Camera %d: Appending output stream %d to request",
172 __FUNCTION__, mCameraId, streamId);
173 }
174
175 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
176 outputStreamIds.size());
177
178 metadata.update(ANDROID_REQUEST_ID, &requestId, /*size*/1);
179 loopCounter++; // loopCounter starts from 1
180 ALOGV("%s: Camera %d: Creating request with ID %d (%d of %d)",
181 __FUNCTION__, mCameraId, requestId, loopCounter, requests.size());
182
183 metadataRequestList.push_back(metadata);
184 }
185 mRequestIdCounter++;
186
187 if (streaming) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700188 res = mDevice->setStreamingRequestList(metadataRequestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700189 if (res != OK) {
190 ALOGE("%s: Camera %d: Got error %d after trying to set streaming "
191 "request", __FUNCTION__, mCameraId, res);
192 } else {
193 mStreamingRequestList.push_back(requestId);
194 }
195 } else {
Jianing Weicb0652e2014-03-12 18:29:36 -0700196 res = mDevice->captureList(metadataRequestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700197 if (res != OK) {
198 ALOGE("%s: Camera %d: Got error %d after trying to set capture",
199 __FUNCTION__, mCameraId, res);
200 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700201 ALOGV("%s: requestId = %d ", __FUNCTION__, requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700202 }
203
204 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId);
205 if (res == OK) {
206 return requestId;
207 }
208
209 return res;
210}
211
Jianing Weicb0652e2014-03-12 18:29:36 -0700212status_t CameraDeviceClient::cancelRequest(int requestId, int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700213 ATRACE_CALL();
214 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
215
216 status_t res;
217
218 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
219
220 Mutex::Autolock icl(mBinderSerializationLock);
221
222 if (!mDevice.get()) return DEAD_OBJECT;
223
224 Vector<int>::iterator it, end;
225 for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end();
226 it != end; ++it) {
227 if (*it == requestId) {
228 break;
229 }
230 }
231
232 if (it == end) {
233 ALOGE("%s: Camera%d: Did not find request id %d in list of streaming "
234 "requests", __FUNCTION__, mCameraId, requestId);
235 return BAD_VALUE;
236 }
237
Jianing Weicb0652e2014-03-12 18:29:36 -0700238 res = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700239
240 if (res == OK) {
241 ALOGV("%s: Camera %d: Successfully cleared streaming request",
242 __FUNCTION__, mCameraId);
243 mStreamingRequestList.erase(it);
244 }
245
246 return res;
247}
248
249status_t CameraDeviceClient::deleteStream(int streamId) {
250 ATRACE_CALL();
251 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
252
253 status_t res;
254 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
255
256 Mutex::Autolock icl(mBinderSerializationLock);
257
258 if (!mDevice.get()) return DEAD_OBJECT;
259
260 // Guard against trying to delete non-created streams
261 ssize_t index = NAME_NOT_FOUND;
262 for (size_t i = 0; i < mStreamMap.size(); ++i) {
263 if (streamId == mStreamMap.valueAt(i)) {
264 index = i;
265 break;
266 }
267 }
268
269 if (index == NAME_NOT_FOUND) {
270 ALOGW("%s: Camera %d: Invalid stream ID (%d) specified, no stream "
271 "created yet", __FUNCTION__, mCameraId, streamId);
272 return BAD_VALUE;
273 }
274
275 // Also returns BAD_VALUE if stream ID was not valid
276 res = mDevice->deleteStream(streamId);
277
278 if (res == BAD_VALUE) {
279 ALOGE("%s: Camera %d: Unexpected BAD_VALUE when deleting stream, but we"
280 " already checked and the stream ID (%d) should be valid.",
281 __FUNCTION__, mCameraId, streamId);
282 } else if (res == OK) {
283 mStreamMap.removeItemsAt(index);
284
Igor Murashkine7ee7632013-06-11 18:10:18 -0700285 }
286
287 return res;
288}
289
290status_t CameraDeviceClient::createStream(int width, int height, int format,
291 const sp<IGraphicBufferProducer>& bufferProducer)
292{
293 ATRACE_CALL();
294 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
295
296 status_t res;
297 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
298
299 Mutex::Autolock icl(mBinderSerializationLock);
300
301 if (!mDevice.get()) return DEAD_OBJECT;
302
303 // Don't create multiple streams for the same target surface
304 {
305 ssize_t index = mStreamMap.indexOfKey(bufferProducer->asBinder());
306 if (index != NAME_NOT_FOUND) {
307 ALOGW("%s: Camera %d: Buffer producer already has a stream for it "
Colin Crosse5729fa2014-03-21 15:04:25 -0700308 "(ID %zd)",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700309 __FUNCTION__, mCameraId, index);
310 return ALREADY_EXISTS;
311 }
312 }
313
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700314 // HACK b/10949105
315 // Query consumer usage bits to set async operation mode for
316 // GLConsumer using controlledByApp parameter.
317 bool useAsync = false;
318 int32_t consumerUsage;
319 if ((res = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
320 &consumerUsage)) != OK) {
321 ALOGE("%s: Camera %d: Failed to query consumer usage", __FUNCTION__,
322 mCameraId);
323 return res;
324 }
325 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
326 ALOGW("%s: Camera %d: Forcing asynchronous mode for stream",
327 __FUNCTION__, mCameraId);
328 useAsync = true;
329 }
330
Igor Murashkine7ee7632013-06-11 18:10:18 -0700331 sp<IBinder> binder;
332 sp<ANativeWindow> anw;
333 if (bufferProducer != 0) {
334 binder = bufferProducer->asBinder();
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700335 anw = new Surface(bufferProducer, useAsync);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700336 }
337
338 // TODO: remove w,h,f since we are ignoring them
339
340 if ((res = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, &width)) != OK) {
341 ALOGE("%s: Camera %d: Failed to query Surface width", __FUNCTION__,
342 mCameraId);
343 return res;
344 }
345 if ((res = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, &height)) != OK) {
346 ALOGE("%s: Camera %d: Failed to query Surface height", __FUNCTION__,
347 mCameraId);
348 return res;
349 }
350 if ((res = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &format)) != OK) {
351 ALOGE("%s: Camera %d: Failed to query Surface format", __FUNCTION__,
352 mCameraId);
353 return res;
354 }
355
356 // FIXME: remove this override since the default format should be
357 // IMPLEMENTATION_DEFINED. b/9487482
Igor Murashkin15811012013-07-29 12:25:59 -0700358 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
359 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700360 ALOGW("%s: Camera %d: Overriding format 0x%x to IMPLEMENTATION_DEFINED",
361 __FUNCTION__, mCameraId, format);
362 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
363 }
364
365 // TODO: add startConfigure/stopConfigure call to CameraDeviceBase
366 // this will make it so Camera3Device doesn't call configure_streams
367 // after each call, but only once we are done with all.
368
369 int streamId = -1;
Eino-Ville Talvalac7ba4a52013-07-01 09:23:55 -0700370 if (format == HAL_PIXEL_FORMAT_BLOB) {
371 // JPEG buffers need to be sized for maximum possible compressed size
372 CameraMetadata staticInfo = mDevice->info();
373 camera_metadata_entry_t entry = staticInfo.find(ANDROID_JPEG_MAX_SIZE);
374 if (entry.count == 0) {
375 ALOGE("%s: Camera %d: Can't find maximum JPEG size in "
376 "static metadata!", __FUNCTION__, mCameraId);
377 return INVALID_OPERATION;
378 }
379 int32_t maxJpegSize = entry.data.i32[0];
380 res = mDevice->createStream(anw, width, height, format, maxJpegSize,
381 &streamId);
382 } else {
383 // All other streams are a known size
384 res = mDevice->createStream(anw, width, height, format, /*size*/0,
385 &streamId);
386 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700387
388 if (res == OK) {
389 mStreamMap.add(bufferProducer->asBinder(), streamId);
390
391 ALOGV("%s: Camera %d: Successfully created a new stream ID %d",
392 __FUNCTION__, mCameraId, streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700393
394 /**
395 * Set the stream transform flags to automatically
396 * rotate the camera stream for preview use cases.
397 */
398 int32_t transform = 0;
399 res = getRotationTransformLocked(&transform);
400
401 if (res != OK) {
402 // Error logged by getRotationTransformLocked.
403 return res;
404 }
405
406 res = mDevice->setStreamTransform(streamId, transform);
407 if (res != OK) {
408 ALOGE("%s: Failed to set stream transform (stream id %d)",
409 __FUNCTION__, streamId);
410 return res;
411 }
412
Igor Murashkine7ee7632013-06-11 18:10:18 -0700413 return streamId;
414 }
415
416 return res;
417}
418
419// Create a request object from a template.
420status_t CameraDeviceClient::createDefaultRequest(int templateId,
421 /*out*/
422 CameraMetadata* request)
423{
424 ATRACE_CALL();
425 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
426
427 status_t res;
428 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
429
430 Mutex::Autolock icl(mBinderSerializationLock);
431
432 if (!mDevice.get()) return DEAD_OBJECT;
433
434 CameraMetadata metadata;
435 if ( (res = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
436 request != NULL) {
437
438 request->swap(metadata);
439 }
440
441 return res;
442}
443
Igor Murashkin099b4572013-07-12 17:52:16 -0700444status_t CameraDeviceClient::getCameraInfo(/*out*/CameraMetadata* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700445{
446 ATRACE_CALL();
447 ALOGV("%s", __FUNCTION__);
448
449 status_t res = OK;
450
Igor Murashkine7ee7632013-06-11 18:10:18 -0700451 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
452
453 Mutex::Autolock icl(mBinderSerializationLock);
454
455 if (!mDevice.get()) return DEAD_OBJECT;
456
Igor Murashkin099b4572013-07-12 17:52:16 -0700457 if (info != NULL) {
458 *info = mDevice->info(); // static camera metadata
459 // TODO: merge with device-specific camera metadata
460 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700461
462 return res;
463}
464
Zhijun He2ab500c2013-07-23 08:02:53 -0700465status_t CameraDeviceClient::waitUntilIdle()
466{
467 ATRACE_CALL();
468 ALOGV("%s", __FUNCTION__);
469
470 status_t res = OK;
471 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
472
473 Mutex::Autolock icl(mBinderSerializationLock);
474
475 if (!mDevice.get()) return DEAD_OBJECT;
476
477 // FIXME: Also need check repeating burst.
478 if (!mStreamingRequestList.isEmpty()) {
479 ALOGE("%s: Camera %d: Try to waitUntilIdle when there are active streaming requests",
480 __FUNCTION__, mCameraId);
481 return INVALID_OPERATION;
482 }
483 res = mDevice->waitUntilDrained();
484 ALOGV("%s Done", __FUNCTION__);
485
486 return res;
487}
488
Jianing Weicb0652e2014-03-12 18:29:36 -0700489status_t CameraDeviceClient::flush(int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700490 ATRACE_CALL();
491 ALOGV("%s", __FUNCTION__);
492
493 status_t res = OK;
494 if ( (res = checkPid(__FUNCTION__) ) != OK) return res;
495
496 Mutex::Autolock icl(mBinderSerializationLock);
497
498 if (!mDevice.get()) return DEAD_OBJECT;
499
Jianing Weicb0652e2014-03-12 18:29:36 -0700500 return mDevice->flush(lastFrameNumber);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700501}
502
Igor Murashkine7ee7632013-06-11 18:10:18 -0700503status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
504 String8 result;
505 result.appendFormat("CameraDeviceClient[%d] (%p) PID: %d, dump:\n",
506 mCameraId,
507 getRemoteCallback()->asBinder().get(),
508 mClientPid);
509 result.append(" State: ");
510
511 // TODO: print dynamic/request section from most recent requests
512 mFrameProcessor->dump(fd, args);
513
514 return dumpDevice(fd, args);
515}
516
Jianing Weicb0652e2014-03-12 18:29:36 -0700517void CameraDeviceClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
518 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700519 // Thread safe. Don't bother locking.
520 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
521
522 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700523 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700524 }
525}
526
527void CameraDeviceClient::notifyIdle() {
528 // Thread safe. Don't bother locking.
529 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
530
531 if (remoteCb != 0) {
532 remoteCb->onDeviceIdle();
533 }
534}
535
Jianing Weicb0652e2014-03-12 18:29:36 -0700536void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700537 nsecs_t timestamp) {
538 // Thread safe. Don't bother locking.
539 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
540 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700541 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700542 }
543}
544
Igor Murashkine7ee7632013-06-11 18:10:18 -0700545// TODO: refactor the code below this with IProCameraUser.
546// it's 100% copy-pasted, so lets not change it right now to make it easier.
547
548void CameraDeviceClient::detachDevice() {
549 if (mDevice == 0) return;
550
551 ALOGV("Camera %d: Stopping processors", mCameraId);
552
553 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
554 FRAME_PROCESSOR_LISTENER_MAX_ID,
555 /*listener*/this);
556 mFrameProcessor->requestExit();
557 ALOGV("Camera %d: Waiting for threads", mCameraId);
558 mFrameProcessor->join();
559 ALOGV("Camera %d: Disconnecting device", mCameraId);
560
561 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
562 {
563 mDevice->clearStreamingRequest();
564
565 status_t code;
566 if ((code = mDevice->waitUntilDrained()) != OK) {
567 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
568 code);
569 }
570 }
571
572 Camera2ClientBase::detachDevice();
573}
574
575/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -0700576void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700577 ATRACE_CALL();
578 ALOGV("%s", __FUNCTION__);
579
Igor Murashkin4fb55c12013-08-29 17:43:01 -0700580 // Thread-safe. No lock necessary.
581 sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
582 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700583 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700584 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585}
586
587// TODO: move to Camera2ClientBase
588bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
589
590 const int pid = IPCThreadState::self()->getCallingPid();
591 const int selfPid = getpid();
592 camera_metadata_entry_t entry;
593
594 /**
595 * Mixin default important security values
596 * - android.led.transmit = defaulted ON
597 */
598 CameraMetadata staticInfo = mDevice->info();
599 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
600 for(size_t i = 0; i < entry.count; ++i) {
601 uint8_t led = entry.data.u8[i];
602
603 switch(led) {
604 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
605 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
606 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
607 metadata.update(ANDROID_LED_TRANSMIT,
608 &transmitDefault, 1);
609 }
610 break;
611 }
612 }
613 }
614
615 // We can do anything!
616 if (pid == selfPid) {
617 return true;
618 }
619
620 /**
621 * Permission check special fields in the request
622 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
623 */
624 entry = metadata.find(ANDROID_LED_TRANSMIT);
625 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
626 String16 permissionString =
627 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
628 if (!checkCallingPermission(permissionString)) {
629 const int uid = IPCThreadState::self()->getCallingUid();
630 ALOGE("Permission Denial: "
631 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
632 return false;
633 }
634 }
635
636 return true;
637}
638
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700639status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
640 ALOGV("%s: begin", __FUNCTION__);
641
642 if (transform == NULL) {
643 ALOGW("%s: null transform", __FUNCTION__);
644 return BAD_VALUE;
645 }
646
647 *transform = 0;
648
649 const CameraMetadata& staticInfo = mDevice->info();
650 camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_SENSOR_ORIENTATION);
651 if (entry.count == 0) {
652 ALOGE("%s: Camera %d: Can't find android.sensor.orientation in "
653 "static metadata!", __FUNCTION__, mCameraId);
654 return INVALID_OPERATION;
655 }
656
Zhijun Hef0b70262013-12-26 10:38:46 -0800657 camera_metadata_ro_entry_t entryFacing = staticInfo.find(ANDROID_LENS_FACING);
658 if (entry.count == 0) {
659 ALOGE("%s: Camera %d: Can't find android.lens.facing in "
660 "static metadata!", __FUNCTION__, mCameraId);
661 return INVALID_OPERATION;
662 }
663
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700664 int32_t& flags = *transform;
665
Zhijun Hef0b70262013-12-26 10:38:46 -0800666 bool mirror = (entryFacing.data.u8[0] == ANDROID_LENS_FACING_FRONT);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700667 int orientation = entry.data.i32[0];
Zhijun Hef0b70262013-12-26 10:38:46 -0800668 if (!mirror) {
669 switch (orientation) {
670 case 0:
671 flags = 0;
672 break;
673 case 90:
674 flags = NATIVE_WINDOW_TRANSFORM_ROT_90;
675 break;
676 case 180:
677 flags = NATIVE_WINDOW_TRANSFORM_ROT_180;
678 break;
679 case 270:
680 flags = NATIVE_WINDOW_TRANSFORM_ROT_270;
681 break;
682 default:
683 ALOGE("%s: Invalid HAL android.sensor.orientation value: %d",
684 __FUNCTION__, orientation);
685 return INVALID_OPERATION;
686 }
687 } else {
688 switch (orientation) {
689 case 0:
690 flags = HAL_TRANSFORM_FLIP_H;
691 break;
692 case 90:
693 flags = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
694 break;
695 case 180:
696 flags = HAL_TRANSFORM_FLIP_V;
697 break;
698 case 270:
699 flags = HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
700 break;
701 default:
702 ALOGE("%s: Invalid HAL android.sensor.orientation value: %d",
703 __FUNCTION__, orientation);
704 return INVALID_OPERATION;
705 }
706
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700707 }
708
709 /**
710 * This magic flag makes surfaceflinger un-rotate the buffers
711 * to counter the extra global device UI rotation whenever the user
712 * physically rotates the device.
713 *
714 * By doing this, the camera buffer always ends up aligned
715 * with the physical camera for a "see through" effect.
716 *
717 * In essence, the buffer only gets rotated during preview use-cases.
718 * The user is still responsible to re-create streams of the proper
719 * aspect ratio, or the preview will end up looking non-uniformly
720 * stretched.
721 */
722 flags |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
723
724 ALOGV("%s: final transform = 0x%x", __FUNCTION__, flags);
725
726 return OK;
727}
728
Igor Murashkine7ee7632013-06-11 18:10:18 -0700729} // namespace android