blob: 5a48a62ba73f89e998e5c93f6bb186551ede0e95 [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 Wei3c76fa32014-04-21 11:34:34 -0700500 mStreamingRequestList.clear();
Jianing Weicb0652e2014-03-12 18:29:36 -0700501 return mDevice->flush(lastFrameNumber);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700502}
503
Igor Murashkine7ee7632013-06-11 18:10:18 -0700504status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
505 String8 result;
506 result.appendFormat("CameraDeviceClient[%d] (%p) PID: %d, dump:\n",
507 mCameraId,
508 getRemoteCallback()->asBinder().get(),
509 mClientPid);
510 result.append(" State: ");
511
512 // TODO: print dynamic/request section from most recent requests
513 mFrameProcessor->dump(fd, args);
514
515 return dumpDevice(fd, args);
516}
517
Jianing Weicb0652e2014-03-12 18:29:36 -0700518void CameraDeviceClient::notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
519 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700520 // Thread safe. Don't bother locking.
521 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
522
523 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700524 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700525 }
526}
527
528void CameraDeviceClient::notifyIdle() {
529 // Thread safe. Don't bother locking.
530 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
531
532 if (remoteCb != 0) {
533 remoteCb->onDeviceIdle();
534 }
535}
536
Jianing Weicb0652e2014-03-12 18:29:36 -0700537void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538 nsecs_t timestamp) {
539 // Thread safe. Don't bother locking.
540 sp<ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
541 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700542 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700543 }
544}
545
Igor Murashkine7ee7632013-06-11 18:10:18 -0700546// TODO: refactor the code below this with IProCameraUser.
547// it's 100% copy-pasted, so lets not change it right now to make it easier.
548
549void CameraDeviceClient::detachDevice() {
550 if (mDevice == 0) return;
551
552 ALOGV("Camera %d: Stopping processors", mCameraId);
553
554 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
555 FRAME_PROCESSOR_LISTENER_MAX_ID,
556 /*listener*/this);
557 mFrameProcessor->requestExit();
558 ALOGV("Camera %d: Waiting for threads", mCameraId);
559 mFrameProcessor->join();
560 ALOGV("Camera %d: Disconnecting device", mCameraId);
561
562 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
563 {
564 mDevice->clearStreamingRequest();
565
566 status_t code;
567 if ((code = mDevice->waitUntilDrained()) != OK) {
568 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
569 code);
570 }
571 }
572
573 Camera2ClientBase::detachDevice();
574}
575
576/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -0700577void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700578 ATRACE_CALL();
579 ALOGV("%s", __FUNCTION__);
580
Igor Murashkin4fb55c12013-08-29 17:43:01 -0700581 // Thread-safe. No lock necessary.
582 sp<ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
583 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700584 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700586}
587
588// TODO: move to Camera2ClientBase
589bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
590
591 const int pid = IPCThreadState::self()->getCallingPid();
592 const int selfPid = getpid();
593 camera_metadata_entry_t entry;
594
595 /**
596 * Mixin default important security values
597 * - android.led.transmit = defaulted ON
598 */
599 CameraMetadata staticInfo = mDevice->info();
600 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
601 for(size_t i = 0; i < entry.count; ++i) {
602 uint8_t led = entry.data.u8[i];
603
604 switch(led) {
605 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
606 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
607 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
608 metadata.update(ANDROID_LED_TRANSMIT,
609 &transmitDefault, 1);
610 }
611 break;
612 }
613 }
614 }
615
616 // We can do anything!
617 if (pid == selfPid) {
618 return true;
619 }
620
621 /**
622 * Permission check special fields in the request
623 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
624 */
625 entry = metadata.find(ANDROID_LED_TRANSMIT);
626 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
627 String16 permissionString =
628 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
629 if (!checkCallingPermission(permissionString)) {
630 const int uid = IPCThreadState::self()->getCallingUid();
631 ALOGE("Permission Denial: "
632 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
633 return false;
634 }
635 }
636
637 return true;
638}
639
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700640status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
641 ALOGV("%s: begin", __FUNCTION__);
642
643 if (transform == NULL) {
644 ALOGW("%s: null transform", __FUNCTION__);
645 return BAD_VALUE;
646 }
647
648 *transform = 0;
649
650 const CameraMetadata& staticInfo = mDevice->info();
651 camera_metadata_ro_entry_t entry = staticInfo.find(ANDROID_SENSOR_ORIENTATION);
652 if (entry.count == 0) {
653 ALOGE("%s: Camera %d: Can't find android.sensor.orientation in "
654 "static metadata!", __FUNCTION__, mCameraId);
655 return INVALID_OPERATION;
656 }
657
Zhijun Hef0b70262013-12-26 10:38:46 -0800658 camera_metadata_ro_entry_t entryFacing = staticInfo.find(ANDROID_LENS_FACING);
659 if (entry.count == 0) {
660 ALOGE("%s: Camera %d: Can't find android.lens.facing in "
661 "static metadata!", __FUNCTION__, mCameraId);
662 return INVALID_OPERATION;
663 }
664
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700665 int32_t& flags = *transform;
666
Zhijun Hef0b70262013-12-26 10:38:46 -0800667 bool mirror = (entryFacing.data.u8[0] == ANDROID_LENS_FACING_FRONT);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700668 int orientation = entry.data.i32[0];
Zhijun Hef0b70262013-12-26 10:38:46 -0800669 if (!mirror) {
670 switch (orientation) {
671 case 0:
672 flags = 0;
673 break;
674 case 90:
675 flags = NATIVE_WINDOW_TRANSFORM_ROT_90;
676 break;
677 case 180:
678 flags = NATIVE_WINDOW_TRANSFORM_ROT_180;
679 break;
680 case 270:
681 flags = NATIVE_WINDOW_TRANSFORM_ROT_270;
682 break;
683 default:
684 ALOGE("%s: Invalid HAL android.sensor.orientation value: %d",
685 __FUNCTION__, orientation);
686 return INVALID_OPERATION;
687 }
688 } else {
689 switch (orientation) {
690 case 0:
691 flags = HAL_TRANSFORM_FLIP_H;
692 break;
693 case 90:
694 flags = HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
695 break;
696 case 180:
697 flags = HAL_TRANSFORM_FLIP_V;
698 break;
699 case 270:
700 flags = HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
701 break;
702 default:
703 ALOGE("%s: Invalid HAL android.sensor.orientation value: %d",
704 __FUNCTION__, orientation);
705 return INVALID_OPERATION;
706 }
707
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700708 }
709
710 /**
711 * This magic flag makes surfaceflinger un-rotate the buffers
712 * to counter the extra global device UI rotation whenever the user
713 * physically rotates the device.
714 *
715 * By doing this, the camera buffer always ends up aligned
716 * with the physical camera for a "see through" effect.
717 *
718 * In essence, the buffer only gets rotated during preview use-cases.
719 * The user is still responsible to re-create streams of the proper
720 * aspect ratio, or the preview will end up looking non-uniformly
721 * stretched.
722 */
723 flags |= NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY;
724
725 ALOGV("%s: final transform = 0x%x", __FUNCTION__, flags);
726
727 return OK;
728}
729
Igor Murashkine7ee7632013-06-11 18:10:18 -0700730} // namespace android