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