blob: 819525be1e5992a61c325ad01b2a7eba570cd8cb [file] [log] [blame]
Yin-Chia Yeh248ed702017-01-23 17:27:26 -08001/*
2 * Copyright (C) 2017 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 "CamDev@1.0-impl"
18#include <utils/Log.h>
19#include <hardware/camera.h>
20#include <hardware/gralloc1.h>
21#include <utils/Trace.h>
22
23#include "CameraDevice_1_0.h"
24
25namespace android {
26namespace hardware {
27namespace camera {
28namespace device {
29namespace V1_0 {
30namespace implementation {
31
32using ::android::hardware::graphics::allocator::V2_0::ProducerUsage;
33using ::android::hardware::graphics::common::V1_0::PixelFormat;
34
35HandleImporter& CameraDevice::sHandleImporter = HandleImporter::getInstance();
36
37Status CameraDevice::getHidlStatus(const int& status) {
38 switch (status) {
39 case 0: return Status::OK;
40 case -ENOSYS: return Status::OPERATION_NOT_SUPPORTED;
41 case -EBUSY : return Status::CAMERA_IN_USE;
42 case -EUSERS: return Status::MAX_CAMERAS_IN_USE;
43 case -ENODEV: return Status::INTERNAL_ERROR;
44 case -EINVAL: return Status::ILLEGAL_ARGUMENT;
45 default:
46 ALOGE("%s: unknown HAL status code %d", __FUNCTION__, status);
47 return Status::INTERNAL_ERROR;
48 }
49}
50
51status_t CameraDevice::getStatusT(const Status& s) {
52 switch(s) {
53 case Status::OK:
54 return OK;
55 case Status::ILLEGAL_ARGUMENT:
56 return BAD_VALUE;
57 case Status::CAMERA_IN_USE:
58 return -EBUSY;
59 case Status::MAX_CAMERAS_IN_USE:
60 return -EUSERS;
61 case Status::METHOD_NOT_SUPPORTED:
62 return UNKNOWN_TRANSACTION;
63 case Status::OPERATION_NOT_SUPPORTED:
64 return INVALID_OPERATION;
65 case Status::CAMERA_DISCONNECTED:
66 return DEAD_OBJECT;
67 case Status::INTERNAL_ERROR:
68 return INVALID_OPERATION;
69 }
70 ALOGW("Unexpected HAL status code %d", s);
71 return INVALID_OPERATION;
72}
73
74Status CameraDevice::initStatus() const {
75 Mutex::Autolock _l(mLock);
76 Status status = Status::OK;
77 if (mInitFail) {
78 status = Status::INTERNAL_ERROR;
79 } else if (mDisconnected) {
80 status = Status::CAMERA_DISCONNECTED;
81 }
82 return status;
83}
84
85CameraDevice::CameraDevice(
86 sp<CameraModule> module, const std::string& cameraId,
87 const SortedVector<std::pair<std::string, std::string>>& cameraDeviceNames) :
88 mModule(module),
89 mCameraId(cameraId),
90 mDisconnected(false),
91 mCameraDeviceNames(cameraDeviceNames) {
92 mCameraIdInt = atoi(mCameraId.c_str());
93 // Should not reach here as provider also validate ID
94 if (mCameraIdInt < 0 || mCameraIdInt >= module->getNumberOfCameras()) {
95 ALOGE("%s: Invalid camera id: %s", __FUNCTION__, mCameraId.c_str());
96 mInitFail = true;
97 }
98
99 mDeviceVersion = mModule->getDeviceVersion(mCameraIdInt);
100 if (mDeviceVersion != CAMERA_DEVICE_API_VERSION_1_0 && !mModule->isOpenLegacyDefined()) {
101 ALOGI("%s: Camera id %s does not support HAL1.0",
102 __FUNCTION__, mCameraId.c_str());
103 mInitFail = true;
104 }
105}
106
107CameraDevice::~CameraDevice() {
108 Mutex::Autolock _l(mLock);
109 if (mDevice != nullptr) {
110 ALOGW("%s: camera %s is deleted while open", __FUNCTION__, mCameraId.c_str());
111 close();
112 }
113 mHalPreviewWindow.cleanUpCirculatingBuffers();
114}
115
116
117void CameraDevice::setConnectionStatus(bool connected) {
118 Mutex::Autolock _l(mLock);
119 mDisconnected = !connected;
120 if (mDevice == nullptr) {
121 return;
122 }
123 if (!connected) {
124 ALOGW("%s: camera %s is disconneted. Closing", __FUNCTION__, mCameraId.c_str());
125 close();
126 }
127 return;
128}
129
130void CameraDevice::CameraPreviewWindow::cleanUpCirculatingBuffers() {
131 Mutex::Autolock _l(mLock);
132 for (auto pair : mCirculatingBuffers) {
133 sHandleImporter.freeBuffer(pair.second);
134 }
135 mCirculatingBuffers.clear();
136 mBufferIdMap.clear();
137}
138
139int CameraDevice::sDequeueBuffer(struct preview_stream_ops* w,
140 buffer_handle_t** buffer, int *stride) {
141 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
142 if (object->mPreviewCallback == nullptr) {
143 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
144 return INVALID_OPERATION;
145 }
146
147 if (buffer == nullptr || stride == nullptr) {
148 ALOGE("%s: buffer (%p) and stride (%p) must not be null!", __FUNCTION__, buffer, stride);
149 return BAD_VALUE;
150 }
151
152 Status s;
153 object->mPreviewCallback->dequeueBuffer(
154 [&](auto status, uint64_t bufferId, const auto& buf, uint32_t strd) {
155 s = status;
156 if (s == Status::OK) {
157 Mutex::Autolock _l(object->mLock);
158 if (object->mCirculatingBuffers.count(bufferId) == 0) {
159 buffer_handle_t importedBuf = buf.getNativeHandle();
160 sHandleImporter.importBuffer(importedBuf);
161 if (importedBuf == nullptr) {
162 ALOGE("%s: preview buffer import failed!", __FUNCTION__);
163 s = Status::INTERNAL_ERROR;
164 return;
165 } else {
166 object->mCirculatingBuffers[bufferId] = importedBuf;
167 object->mBufferIdMap[&(object->mCirculatingBuffers[bufferId])] = bufferId;
168 }
169 }
170 *buffer = &(object->mCirculatingBuffers[bufferId]);
171 *stride = strd;
172 }
173 });
174 return getStatusT(s);
175}
176
177int CameraDevice::sLockBuffer(struct preview_stream_ops*, buffer_handle_t*) {
178 // TODO: make sure lock_buffer is indeed a no-op (and will always be)
179 return 0;
180}
181
182int CameraDevice::sEnqueueBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer) {
183 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
184 if (object->mPreviewCallback == nullptr) {
185 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
186 return INVALID_OPERATION;
187 }
188 uint64_t bufferId = object->mBufferIdMap.at(buffer);
189 return getStatusT(object->mPreviewCallback->enqueueBuffer(bufferId));
190}
191
192int CameraDevice::sCancelBuffer(struct preview_stream_ops* w, buffer_handle_t* buffer) {
193 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
194 if (object->mPreviewCallback == nullptr) {
195 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
196 return INVALID_OPERATION;
197 }
198 uint64_t bufferId = object->mBufferIdMap.at(buffer);
199 return getStatusT(object->mPreviewCallback->cancelBuffer(bufferId));
200}
201
202int CameraDevice::sSetBufferCount(struct preview_stream_ops* w, int count) {
203 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
204 if (object->mPreviewCallback == nullptr) {
205 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
206 return INVALID_OPERATION;
207 }
208
209 object->cleanUpCirculatingBuffers();
210 return getStatusT(object->mPreviewCallback->setBufferCount(count));
211}
212
213int CameraDevice::sSetBuffersGeometry(struct preview_stream_ops* w,
214 int width, int height, int format) {
215 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
216 if (object->mPreviewCallback == nullptr) {
217 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
218 return INVALID_OPERATION;
219 }
220
221 object->cleanUpCirculatingBuffers();
222 return getStatusT(
223 object->mPreviewCallback->setBuffersGeometry(width, height, (PixelFormat) format));
224}
225
226int CameraDevice::sSetCrop(struct preview_stream_ops *w,
227 int left, int top, int right, int bottom) {
228 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
229 if (object->mPreviewCallback == nullptr) {
230 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
231 return INVALID_OPERATION;
232 }
233
234 return getStatusT(object->mPreviewCallback->setCrop(left, top, right, bottom));
235}
236
237int CameraDevice::sSetTimestamp(struct preview_stream_ops *w, int64_t timestamp) {
238 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
239 if (object->mPreviewCallback == nullptr) {
240 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
241 return INVALID_OPERATION;
242 }
243
244 return getStatusT(object->mPreviewCallback->setTimestamp(timestamp));
245}
246
247int CameraDevice::sSetUsage(struct preview_stream_ops* w, int usage) {
248 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
249 if (object->mPreviewCallback == nullptr) {
250 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
251 return INVALID_OPERATION;
252 }
253
254 object->cleanUpCirculatingBuffers();
255 return getStatusT(object->mPreviewCallback->setUsage((ProducerUsage) usage));
256}
257
258int CameraDevice::sSetSwapInterval(struct preview_stream_ops *w, int interval) {
259 CameraPreviewWindow* object = static_cast<CameraPreviewWindow*>(w);
260 if (object->mPreviewCallback == nullptr) {
261 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
262 return INVALID_OPERATION;
263 }
264
265 return getStatusT(object->mPreviewCallback->setSwapInterval(interval));
266}
267
268int CameraDevice::sGetMinUndequeuedBufferCount(
269 const struct preview_stream_ops *w,
270 int *count) {
271 const CameraPreviewWindow* object = static_cast<const CameraPreviewWindow*>(w);
272 if (object->mPreviewCallback == nullptr) {
273 ALOGE("%s: camera HAL calling preview ops while there is no preview window!", __FUNCTION__);
274 return INVALID_OPERATION;
275 }
276 if (count == nullptr) {
277 ALOGE("%s: count is null!", __FUNCTION__);
278 return BAD_VALUE;
279 }
280
281 Status s;
282 object->mPreviewCallback->getMinUndequeuedBufferCount(
283 [&](auto status, uint32_t cnt) {
284 s = status;
285 if (s == Status::OK) {
286 *count = cnt;
287 }
288 });
289 return getStatusT(s);
290}
291
292CameraDevice::CameraHeapMemory::CameraHeapMemory(int fd, size_t buf_size, uint_t num_buffers) :
293 mBufSize(buf_size),
294 mNumBufs(num_buffers) {
295 mHeap = new MemoryHeapBase(fd, buf_size * num_buffers);
296 commonInitialization();
297}
298
299CameraDevice::CameraHeapMemory::CameraHeapMemory(size_t buf_size, uint_t num_buffers) :
300 mBufSize(buf_size),
301 mNumBufs(num_buffers) {
302 mHeap = new MemoryHeapBase(buf_size * num_buffers);
303 commonInitialization();
304}
305
306void CameraDevice::CameraHeapMemory::commonInitialization() {
307 handle.data = mHeap->base();
308 handle.size = mBufSize * mNumBufs;
309 handle.handle = this;
310
311 mBuffers = new sp<MemoryBase>[mNumBufs];
312 for (uint_t i = 0; i < mNumBufs; i++) {
313 mBuffers[i] = new MemoryBase(mHeap, i * mBufSize, mBufSize);
314 }
315
316 handle.release = sPutMemory;
317}
318
319CameraDevice::CameraHeapMemory::~CameraHeapMemory() {
320 delete [] mBuffers;
321}
322
323// shared memory methods
324camera_memory_t* CameraDevice::sGetMemory(int fd, size_t buf_size, uint_t num_bufs, void *user) {
325 ALOGV("%s", __FUNCTION__);
326 CameraDevice* object = static_cast<CameraDevice*>(user);
327 if (object->mDeviceCallback == nullptr) {
328 ALOGE("%s: camera HAL request memory while camera is not opened!", __FUNCTION__);
329 return nullptr;
330 }
331
332 CameraHeapMemory* mem;
333 native_handle_t* handle = native_handle_create(1,0);
334
335 if (handle == nullptr) {
336 ALOGE("%s: native_handle_create failed!", __FUNCTION__);
337 return nullptr;
338 }
339
340 if (fd < 0) {
341 mem = new CameraHeapMemory(buf_size, num_bufs);
342 } else {
343 mem = new CameraHeapMemory(fd, buf_size, num_bufs);
344 }
345 handle->data[0] = mem->mHeap->getHeapID();
346 mem->incStrong(mem);
347
348 hidl_handle hidlHandle = handle;
349 MemoryId id = object->mDeviceCallback->registerMemory(hidlHandle, buf_size, num_bufs);
350 mem->handle.mId = id;
351 if (object->mMemoryMap.count(id) != 0) {
352 ALOGE("%s: duplicate MemoryId %d returned by client!", __FUNCTION__, id);
353 }
354 object->mMemoryMap[id] = mem;
355 mem->handle.mDevice = object;
356 native_handle_delete(handle);
357 return &mem->handle;
358}
359
360void CameraDevice::sPutMemory(camera_memory_t *data) {
361 if (!data)
362 return;
363
364 CameraHeapMemory* mem = static_cast<CameraHeapMemory *>(data->handle);
365 CameraDevice* device = mem->handle.mDevice;
366 if (device == nullptr) {
367 ALOGE("%s: camera HAL return memory for a null device!", __FUNCTION__);
368 }
369 if (device->mDeviceCallback == nullptr) {
370 ALOGE("%s: camera HAL return memory while camera is not opened!", __FUNCTION__);
371 }
372 device->mDeviceCallback->unregisterMemory(mem->handle.mId);
373 device->mMemoryMap.erase(mem->handle.mId);
374 mem->decStrong(mem);
375}
376
377// Callback forwarding methods
378void CameraDevice::sNotifyCb(int32_t msg_type, int32_t ext1, int32_t ext2, void *user) {
379 ALOGV("%s", __FUNCTION__);
380 CameraDevice* object = static_cast<CameraDevice*>(user);
381 if (object->mDeviceCallback != nullptr) {
382 object->mDeviceCallback->notifyCallback((NotifyCallbackMsg) msg_type, ext1, ext2);
383 }
384}
385
386void CameraDevice::sDataCb(int32_t msg_type, const camera_memory_t *data, unsigned int index,
387 camera_frame_metadata_t *metadata, void *user) {
388 ALOGV("%s", __FUNCTION__);
389 CameraDevice* object = static_cast<CameraDevice*>(user);
390 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory*>(data->handle));
391 if (index >= mem->mNumBufs) {
392 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
393 index, mem->mNumBufs);
394 return;
395 }
396 if (object->mDeviceCallback != nullptr) {
397 CameraFrameMetadata hidlMetadata;
398 if (metadata) {
399 hidlMetadata.faces.resize(metadata->number_of_faces);
400 for (size_t i = 0; i < hidlMetadata.faces.size(); i++) {
401 hidlMetadata.faces[i].score = metadata->faces[i].score;
402 hidlMetadata.faces[i].id = metadata->faces[i].id;
403 for (int k = 0; k < 4; k++) {
404 hidlMetadata.faces[i].rect[k] = metadata->faces[i].rect[k];
405 }
406 for (int k = 0; k < 2; k++) {
407 hidlMetadata.faces[i].leftEye[k] = metadata->faces[i].left_eye[k];
408 }
409 for (int k = 0; k < 2; k++) {
410 hidlMetadata.faces[i].rightEye[k] = metadata->faces[i].right_eye[k];
411 }
412 for (int k = 0; k < 2; k++) {
413 hidlMetadata.faces[i].mouth[k] = metadata->faces[i].mouth[k];
414 }
415 }
416 }
417 CameraHeapMemory* mem = static_cast<CameraHeapMemory *>(data->handle);
418 object->mDeviceCallback->dataCallback(
419 (DataCallbackMsg) msg_type, mem->handle.mId, index, hidlMetadata);
420 }
421}
422
423void CameraDevice::sDataCbTimestamp(nsecs_t timestamp, int32_t msg_type,
424 const camera_memory_t *data, unsigned index, void *user) {
425 ALOGV("%s", __FUNCTION__);
426 CameraDevice* object = static_cast<CameraDevice*>(user);
427 // Start refcounting the heap object from here on. When the clients
428 // drop all references, it will be destroyed (as well as the enclosed
429 // MemoryHeapBase.
430 sp<CameraHeapMemory> mem(static_cast<CameraHeapMemory*>(data->handle));
431 if (index >= mem->mNumBufs) {
432 ALOGE("%s: invalid buffer index %d, max allowed is %d", __FUNCTION__,
433 index, mem->mNumBufs);
434 return;
435 }
436
437 native_handle_t* handle = nullptr;
438 if (object->mMetadataMode) {
439 if (mem->mBufSize == sizeof(VideoNativeHandleMetadata)) {
440 VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*)
441 mem->mBuffers[index]->pointer();
442 if (md->eType == VideoNativeHandleMetadata::kMetadataBufferTypeNativeHandleSource) {
443 handle = md->pHandle;
444 }
445 }
446 }
447
448 if (object->mDeviceCallback != nullptr) {
449 if (handle == nullptr) {
450 object->mDeviceCallback->dataCallbackTimestamp(
451 (DataCallbackMsg) msg_type, mem->handle.mId, index, timestamp);
452 } else {
453 object->mDeviceCallback->handleCallbackTimestamp(
454 (DataCallbackMsg) msg_type, handle, mem->handle.mId, index, timestamp);
455 }
456 }
457}
458
459void CameraDevice::initHalPreviewWindow()
460{
461 mHalPreviewWindow.cancel_buffer = sCancelBuffer;
462 mHalPreviewWindow.lock_buffer = sLockBuffer;
463 mHalPreviewWindow.dequeue_buffer = sDequeueBuffer;
464 mHalPreviewWindow.enqueue_buffer = sEnqueueBuffer;
465 mHalPreviewWindow.set_buffer_count = sSetBufferCount;
466 mHalPreviewWindow.set_buffers_geometry = sSetBuffersGeometry;
467 mHalPreviewWindow.set_crop = sSetCrop;
468 mHalPreviewWindow.set_timestamp = sSetTimestamp;
469 mHalPreviewWindow.set_usage = sSetUsage;
470 mHalPreviewWindow.set_swap_interval = sSetSwapInterval;
471
472 mHalPreviewWindow.get_min_undequeued_buffer_count =
473 sGetMinUndequeuedBufferCount;
474}
475
476// Methods from ::android::hardware::camera::device::V1_0::ICameraDevice follow.
477Return<void> CameraDevice::getResourceCost(getResourceCost_cb _hidl_cb) {
478 Status status = initStatus();
479 CameraResourceCost resCost;
480 if (status == Status::OK) {
481 int cost = 100;
482 std::vector<std::string> conflicting_devices;
483 struct camera_info info;
484
485 // If using post-2.4 module version, query the cost + conflicting devices from the HAL
486 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4) {
487 int ret = mModule->getCameraInfo(mCameraIdInt, &info);
488 if (ret == OK) {
489 cost = info.resource_cost;
490 for (size_t i = 0; i < info.conflicting_devices_length; i++) {
491 std::string cameraId(info.conflicting_devices[i]);
492 for (const auto& pair : mCameraDeviceNames) {
493 if (cameraId == pair.first) {
494 conflicting_devices.push_back(pair.second);
495 }
496 }
497 }
498 } else {
499 status = Status::INTERNAL_ERROR;
500 }
501 }
502
503 if (status == Status::OK) {
504 resCost.resourceCost = cost;
505 resCost.conflictingDevices.resize(conflicting_devices.size());
506 for (size_t i = 0; i < conflicting_devices.size(); i++) {
507 resCost.conflictingDevices[i] = conflicting_devices[i];
508 ALOGV("CamDevice %s is conflicting with camDevice %s",
509 mCameraId.c_str(), resCost.conflictingDevices[i].c_str());
510 }
511 }
512 }
513 _hidl_cb(status, resCost);
514 return Void();
515}
516
517Return<void> CameraDevice::getCameraInfo(getCameraInfo_cb _hidl_cb) {
518 Status status = initStatus();
519 CameraInfo cameraInfo;
520 if (status == Status::OK) {
521 struct camera_info info;
522 int ret = mModule->getCameraInfo(mCameraIdInt, &info);
523 if (ret == OK) {
524 cameraInfo.facing = (CameraFacing) info.facing;
525 // Device 1.0 does not support external camera facing.
526 // The closest approximation would be front camera.
527 // TODO: figure out should we override here or let
528 // camera service handle it.
529 if (cameraInfo.facing == CameraFacing::EXTERNAL) {
530 cameraInfo.facing = CameraFacing::FRONT;
531 }
532 cameraInfo.orientation = info.orientation;
533 } else {
534 ALOGE("%s: get camera info failed!", __FUNCTION__);
535 status = Status::INTERNAL_ERROR;
536 }
537 }
538 _hidl_cb(status, cameraInfo);
539 return Void();
540}
541
542Return<Status> CameraDevice::setTorchMode(TorchMode mode) {
543 if (!mModule->isSetTorchModeSupported()) {
544 return Status::METHOD_NOT_SUPPORTED;
545 }
546
547 Status status = initStatus();
548 if (status == Status::OK) {
549 bool enable = (mode == TorchMode::ON) ? true : false;
550 status = getHidlStatus(mModule->setTorchMode(mCameraId.c_str(), enable));
551 }
552 return status;
553}
554
555Return<Status> CameraDevice::dumpState(const hidl_handle& handle) {
556 Mutex::Autolock _l(mLock);
557 if (handle.getNativeHandle() == nullptr) {
558 ALOGE("%s: handle must not be null", __FUNCTION__);
559 return Status::ILLEGAL_ARGUMENT;
560 }
561 if (handle->numFds != 1 || handle->numInts != 0) {
562 ALOGE("%s: handle must contain 1 FD and 0 integers! Got %d FDs and %d ints",
563 __FUNCTION__, handle->numFds, handle->numInts);
564 return Status::ILLEGAL_ARGUMENT;
565 }
566 int fd = handle->data[0];
567
568 if (mDevice != nullptr) {
569 if (mDevice->ops->dump) { // It's fine if the HAL doesn't implement dump()
570 return getHidlStatus(mDevice->ops->dump(mDevice, fd));
571 }
572 }
573 return Status::OK;
574}
575
576Return<Status> CameraDevice::open(const sp<ICameraDeviceCallback>& callback) {
577 ALOGI("Opening camera %s", mCameraId.c_str());
578 Mutex::Autolock _l(mLock);
579
580 camera_info info;
581 status_t res = mModule->getCameraInfo(mCameraIdInt, &info);
582 if (res != OK) {
583 ALOGE("Could not get camera info: %s: %d", mCameraId.c_str(), res);
584 return getHidlStatus(res);
585 }
586
587 int rc = OK;
588 if (mModule->getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_3 &&
589 info.device_version > CAMERA_DEVICE_API_VERSION_1_0) {
590 // Open higher version camera device as HAL1.0 device.
591 rc = mModule->openLegacy(mCameraId.c_str(),
592 CAMERA_DEVICE_API_VERSION_1_0,
593 (hw_device_t **)&mDevice);
594 } else {
595 rc = mModule->open(mCameraId.c_str(), (hw_device_t **)&mDevice);
596 }
597 if (rc != OK) {
598 mDevice = nullptr;
599 ALOGE("Could not open camera %s: %d", mCameraId.c_str(), rc);
600 return getHidlStatus(rc);
601 }
602
603 initHalPreviewWindow();
604 mDeviceCallback = callback;
605
606 if (mDevice->ops->set_callbacks) {
607 mDevice->ops->set_callbacks(mDevice,
608 sNotifyCb, sDataCb, sDataCbTimestamp, sGetMemory, this);
609 }
610
611 return getHidlStatus(rc);
612}
613
614Return<Status> CameraDevice::setPreviewWindow(const sp<ICameraDevicePreviewCallback>& window) {
615 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
616 Mutex::Autolock _l(mLock);
617 if (!mDevice) {
618 ALOGE("%s called while camera is not opened", __FUNCTION__);
619 return Status::OPERATION_NOT_SUPPORTED;
620 }
621
622 mHalPreviewWindow.mPreviewCallback = window;
623 if (mDevice->ops->set_preview_window) {
624 return getHidlStatus(mDevice->ops->set_preview_window(mDevice,
625 (window == nullptr) ? nullptr : &mHalPreviewWindow));
626 }
627 return Status::INTERNAL_ERROR; // HAL should provide set_preview_window
628}
629
630Return<void> CameraDevice::enableMsgType(uint32_t msgType) {
631 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
632 Mutex::Autolock _l(mLock);
633 if (!mDevice) {
634 ALOGE("%s called while camera is not opened", __FUNCTION__);
635 return Void();
636 }
637 if (mDevice->ops->enable_msg_type) {
638 mDevice->ops->enable_msg_type(mDevice, msgType);
639 }
640 return Void();
641}
642
643Return<void> CameraDevice::disableMsgType(uint32_t msgType) {
644 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
645 Mutex::Autolock _l(mLock);
646 if (!mDevice) {
647 ALOGE("%s called while camera is not opened", __FUNCTION__);
648 return Void();
649 }
650 if (mDevice->ops->disable_msg_type) {
651 mDevice->ops->disable_msg_type(mDevice, msgType);
652 }
653 return Void();
654}
655
656Return<bool> CameraDevice::msgTypeEnabled(uint32_t msgType) {
657 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
658 Mutex::Autolock _l(mLock);
659 if (!mDevice) {
660 ALOGE("%s called while camera is not opened", __FUNCTION__);
661 return false;
662 }
663 if (mDevice->ops->msg_type_enabled) {
664 return mDevice->ops->msg_type_enabled(mDevice, msgType);
665 }
666 return false;
667}
668
669Return<Status> CameraDevice::startPreview() {
670 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
671 Mutex::Autolock _l(mLock);
672 if (!mDevice) {
673 ALOGE("%s called while camera is not opened", __FUNCTION__);
674 return Status::OPERATION_NOT_SUPPORTED;
675 }
676 if (mDevice->ops->start_preview) {
677 return getHidlStatus(mDevice->ops->start_preview(mDevice));
678 }
679 return Status::INTERNAL_ERROR; // HAL should provide start_preview
680}
681
682Return<void> CameraDevice::stopPreview() {
683 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
684 Mutex::Autolock _l(mLock);
685 if (!mDevice) {
686 ALOGE("%s called while camera is not opened", __FUNCTION__);
687 return Void();
688 }
689 if (mDevice->ops->stop_preview) {
690 mDevice->ops->stop_preview(mDevice);
691 }
692 return Void();
693}
694
695Return<bool> CameraDevice::previewEnabled() {
696 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
697 Mutex::Autolock _l(mLock);
698 if (!mDevice) {
699 ALOGE("%s called while camera is not opened", __FUNCTION__);
700 return false;
701 }
702 if (mDevice->ops->preview_enabled) {
703 return mDevice->ops->preview_enabled(mDevice);
704 }
705 return false;
706}
707
708Return<Status> CameraDevice::storeMetaDataInBuffers(bool enable) {
709 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
710 Mutex::Autolock _l(mLock);
711 if (!mDevice) {
712 ALOGE("%s called while camera is not opened", __FUNCTION__);
713 return Status::OPERATION_NOT_SUPPORTED;
714 }
715 if (mDevice->ops->store_meta_data_in_buffers) {
716 status_t s = mDevice->ops->store_meta_data_in_buffers(mDevice, enable);
717 if (s == OK && enable) {
718 mMetadataMode = true;
719 }
720 return getHidlStatus(s);
721 }
722 return enable ? Status::ILLEGAL_ARGUMENT : Status::OK;
723}
724
725Return<Status> CameraDevice::startRecording() {
726 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
727 Mutex::Autolock _l(mLock);
728 if (!mDevice) {
729 ALOGE("%s called while camera is not opened", __FUNCTION__);
730 return Status::OPERATION_NOT_SUPPORTED;
731 }
732 if (mDevice->ops->start_recording) {
733 return getHidlStatus(mDevice->ops->start_recording(mDevice));
734 }
735 return Status::ILLEGAL_ARGUMENT;
736}
737
738Return<void> CameraDevice::stopRecording() {
739 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
740 Mutex::Autolock _l(mLock);
741 if (!mDevice) {
742 ALOGE("%s called while camera is not opened", __FUNCTION__);
743 return Void();
744 }
745 if (mDevice->ops->stop_recording) {
746 mDevice->ops->stop_recording(mDevice);
747 }
748 return Void();
749}
750
751Return<bool> CameraDevice::recordingEnabled() {
752 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
753 Mutex::Autolock _l(mLock);
754 if (!mDevice) {
755 ALOGE("%s called while camera is not opened", __FUNCTION__);
756 return false;
757 }
758 if (mDevice->ops->recording_enabled) {
759 return mDevice->ops->recording_enabled(mDevice);
760 }
761 return false;
762}
763
764void CameraDevice::releaseRecordingFrameLocked(
765 uint32_t memId, uint32_t bufferIndex, const native_handle_t* handle) {
766 if (!mDevice) {
767 ALOGE("%s called while camera is not opened", __FUNCTION__);
768 return;
769 }
770 if (mDevice->ops->release_recording_frame) {
771 CameraHeapMemory* camMemory = mMemoryMap.at(memId);
772 sp<MemoryHeapBase> heap = camMemory->mHeap;
773 if (bufferIndex >= camMemory->mNumBufs) {
774 ALOGE("%s: bufferIndex %d exceeds number of buffers %d",
775 __FUNCTION__, bufferIndex, camMemory->mNumBufs);
776 return;
777 }
778 sp<IMemory> mem = camMemory->mBuffers[bufferIndex];
779 // TODO: simplify below logic once we verify offset is indeed idx * mBufSize
780 // and heap == heap2
781 ssize_t offset;
782 size_t size;
783 sp<IMemoryHeap> heap2 = mem->getMemory(&offset, &size);
784 if ((size_t)offset != bufferIndex * camMemory->mBufSize) {
785 ALOGI("%s: unexpected offset %zd (was expecting %zu)",
786 __FUNCTION__, offset, bufferIndex * camMemory->mBufSize);
787 }
788 if (heap != heap2) {
789 ALOGE("%s: heap mismatch!", __FUNCTION__);
790 return;
791 }
792 void *data = ((uint8_t *)heap->base()) + offset;
793 if (handle) {
794 VideoNativeHandleMetadata* md = (VideoNativeHandleMetadata*) data;
795 if (md->eType == VideoNativeHandleMetadata::kMetadataBufferTypeNativeHandleSource) {
796 // Input handle will be closed by HIDL transport later, so clone it
797 // HAL implementation is responsible to close/delete the clone
798 native_handle_t* clone = native_handle_clone(handle);
799 if (!clone) {
800 ALOGE("%s: failed to clone buffer %p", __FUNCTION__, handle);
801 return;
802 }
803 md->pHandle = clone;
804 } else {
805 ALOGE("%s:Malform VideoNativeHandleMetadata at memId %d, bufferId %d",
806 __FUNCTION__, memId, bufferIndex);
807 return;
808 }
809 }
810 mDevice->ops->release_recording_frame(mDevice, data);
811 }
812}
813
814Return<void> CameraDevice::releaseRecordingFrame(uint32_t memId, uint32_t bufferIndex) {
815 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
816 Mutex::Autolock _l(mLock);
817 releaseRecordingFrameLocked(memId, bufferIndex, nullptr);
818 return Void();
819}
820
821Return<void> CameraDevice::releaseRecordingFrameHandle(
822 uint32_t memId, uint32_t bufferIndex, const hidl_handle& frame) {
823 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
824 Mutex::Autolock _l(mLock);
825 releaseRecordingFrameLocked(
826 memId, bufferIndex, frame.getNativeHandle());
827 return Void();
828}
829
830Return<Status> CameraDevice::autoFocus() {
831 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
832 Mutex::Autolock _l(mLock);
833 if (!mDevice) {
834 ALOGE("%s called while camera is not opened", __FUNCTION__);
835 return Status::OPERATION_NOT_SUPPORTED;
836 }
837 if (mDevice->ops->auto_focus) {
838 return getHidlStatus(mDevice->ops->auto_focus(mDevice));
839 }
840 return Status::ILLEGAL_ARGUMENT;
841}
842
843Return<Status> CameraDevice::cancelAutoFocus() {
844 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
845 Mutex::Autolock _l(mLock);
846 if (!mDevice) {
847 ALOGE("%s called while camera is not opened", __FUNCTION__);
848 return Status::OPERATION_NOT_SUPPORTED;
849 }
850 if (mDevice->ops->cancel_auto_focus) {
851 return getHidlStatus(mDevice->ops->cancel_auto_focus(mDevice));
852 }
853 return Status::ILLEGAL_ARGUMENT;
854}
855
856Return<Status> CameraDevice::takePicture() {
857 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
858 Mutex::Autolock _l(mLock);
859 if (!mDevice) {
860 ALOGE("%s called while camera is not opened", __FUNCTION__);
861 return Status::OPERATION_NOT_SUPPORTED;
862 }
863 if (mDevice->ops->take_picture) {
864 return getHidlStatus(mDevice->ops->take_picture(mDevice));
865 }
866 return Status::ILLEGAL_ARGUMENT;
867}
868
869Return<Status> CameraDevice::cancelPicture() {
870 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
871 Mutex::Autolock _l(mLock);
872 if (!mDevice) {
873 ALOGE("%s called while camera is not opened", __FUNCTION__);
874 return Status::OPERATION_NOT_SUPPORTED;
875 }
876 if (mDevice->ops->cancel_picture) {
877 return getHidlStatus(mDevice->ops->cancel_picture(mDevice));
878 }
879 return Status::ILLEGAL_ARGUMENT;
880}
881
882Return<Status> CameraDevice::setParameters(const hidl_string& params) {
883 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
884 Mutex::Autolock _l(mLock);
885 if (!mDevice) {
886 ALOGE("%s called while camera is not opened", __FUNCTION__);
887 return Status::OPERATION_NOT_SUPPORTED;
888 }
889 if (mDevice->ops->set_parameters) {
890 return getHidlStatus(mDevice->ops->set_parameters(mDevice, params.c_str()));
891 }
892 return Status::ILLEGAL_ARGUMENT;
893}
894
895Return<void> CameraDevice::getParameters(getParameters_cb _hidl_cb) {
896 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
897 Mutex::Autolock _l(mLock);
898 hidl_string outStr;
899 if (!mDevice) {
900 ALOGE("%s called while camera is not opened", __FUNCTION__);
901 _hidl_cb(outStr);
902 return Void();
903 }
904 if (mDevice->ops->get_parameters) {
905 char *temp = mDevice->ops->get_parameters(mDevice);
906 outStr = temp;
907 if (mDevice->ops->put_parameters) {
908 mDevice->ops->put_parameters(mDevice, temp);
909 } else {
910 free(temp);
911 }
912 }
913 _hidl_cb(outStr);
914 return Void();
915}
916
917Return<Status> CameraDevice::sendCommand(CommandType cmd, int32_t arg1, int32_t arg2) {
918 ALOGV("%s(%s)", __FUNCTION__, mCameraId.c_str());
919 Mutex::Autolock _l(mLock);
920 if (!mDevice) {
921 ALOGE("%s called while camera is not opened", __FUNCTION__);
922 return Status::OPERATION_NOT_SUPPORTED;
923 }
924 if (mDevice->ops->send_command) {
925 return getHidlStatus(mDevice->ops->send_command(mDevice, (int32_t) cmd, arg1, arg2));
926 }
927 return Status::ILLEGAL_ARGUMENT;
928}
929
930Return<void> CameraDevice::close() {
931 ALOGI("Closing camera %s", mCameraId.c_str());
932 Mutex::Autolock _l(mLock);
933 if(mDevice) {
934 int rc = mDevice->common.close(&mDevice->common);
935 if (rc != OK) {
936 ALOGE("Could not close camera %s: %d", mCameraId.c_str(), rc);
937 }
938 mDevice = nullptr;
939 }
940 return Void();
941}
942
943} // namespace implementation
944} // namespace V1_0
945} // namespace device
946} // namespace camera
947} // namespace hardware
948} // namespace android