4 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
36 #include "hal_camera_v4l2_private.h"
41 #define LOG_TAG "CAMERA_HAL"
43 #define TEST_JPEG_PATH "/home/owner/media/Images/test.jpg"
44 #define DEVICE_NODE_PATH_MAX 16
45 #define DEVICE_NODE_PATH_PREFIX "/dev/video"
46 #define FOURCC_FORMAT "%c%c%c%c"
47 #define FOURCC_CONVERT(fourcc) \
49 (fourcc >> 8) & 0xff,\
50 (fourcc >> 16) & 0xff,\
54 static camera_device_info_list_s *g_device_info_list;
55 static guint32 g_device_caps;
56 static GMutex g_device_info_lock;
59 static void __camera_hal_v4l2_destructor(void) __attribute__((destructor));
60 static void __camera_send_message(hal_camera_handle *handle, camera_message_type_e type, int value);
63 static void __camera_hal_v4l2_destructor(void)
65 LOGD("release device info list %p", g_device_info_list);
67 g_free(g_device_info_list);
68 g_device_info_list = NULL;
74 static void __camera_send_message(hal_camera_handle *handle, camera_message_type_e type, int value)
76 camera_message_s *message = NULL;
83 message = g_new0(camera_message_s, 1);
88 case CAMERA_MESSAGE_TYPE_FOCUS_CHANGED:
89 message->focus_state = (camera_focus_state_e)value;
91 case CAMERA_MESSAGE_TYPE_CAPTURED:
93 case CAMERA_MESSAGE_TYPE_HDR_PROGRESS:
94 message->hdr_progress = (uint32_t)value;
96 case CAMERA_MESSAGE_TYPE_ERROR:
97 message->error_code = (camera_error_e)value;
100 LOGE("unknown type[%d]", type);
105 g_mutex_lock(&handle->msg_cb_lock);
107 LOGD("type[%d], value[0x%x]", type, value);
109 g_queue_push_tail(handle->msg_list, message);
110 g_cond_signal(&handle->msg_cb_cond);
112 g_mutex_unlock(&handle->msg_cb_lock);
116 static int __camera_v4l2_wait_frame(int device_fd, int wait_time)
118 int ret = CAMERA_ERROR_NONE;
120 struct timeval timeout;
123 LOGE("invalid fd %d", device_fd);
124 return CAMERA_ERROR_INVALID_PARAMETER;
128 FD_SET(device_fd, &fds);
130 memset(&timeout, 0x0, sizeof(struct timeval));
132 timeout.tv_sec = wait_time;
135 /*LOGD("select : %d sec", wait_time);*/
137 ret = select(device_fd + 1, &fds, NULL, NULL, &timeout);
139 if (EINTR == errno) {
140 LOGD("select error : EINTR");
141 return CAMERA_ERROR_NONE;
143 LOGE("select failed. errno %d", errno);
144 return CAMERA_ERROR_INTERNAL;
148 LOGE("select timeout.");
149 return CAMERA_ERROR_INTERNAL;
152 /*LOGD("select done");*/
154 return CAMERA_ERROR_NONE;
158 static int __camera_v4l2_g_ctrl(int device_fd, int cid, int *value)
161 struct v4l2_control ctrl;
165 return CAMERA_ERROR_INVALID_PARAMETER;
168 memset(&ctrl, 0x0, sizeof(struct v4l2_control));
172 ret = v4l2_ioctl(device_fd, VIDIOC_G_CTRL, &ctrl);
176 LOGD("G_CTRL id 0x%x, value %d, ret %d", cid, *value, ret);
182 static int __camera_v4l2_s_ctrl(int device_fd, int cid, int value)
185 struct v4l2_control ctrl;
187 memset(&ctrl, 0x0, sizeof(struct v4l2_control));
192 ret = v4l2_ioctl(device_fd, VIDIOC_S_CTRL, &ctrl);
194 LOGD("S_CTRL id 0x%x, value %d, ret %d", cid, value, ret);
200 static int __camera_v4l2_stream(int device_fd, int type, gboolean onoff)
203 LOGE("invalid fd %d", device_fd);
204 return CAMERA_ERROR_INVALID_PARAMETER;
207 if (v4l2_ioctl(device_fd, onoff ? VIDIOC_STREAMON : VIDIOC_STREAMOFF, &type) < 0) {
208 LOGE("stream %d failed. [t:%d] errno %d", onoff, type, errno);
209 return CAMERA_ERROR_INTERNAL;
212 LOGD("stream %d done [t:%d]", onoff, type);
214 return CAMERA_ERROR_NONE;
218 static int __camera_v4l2_reqbufs(int device_fd, int type, int memory, uint32_t count, uint32_t *result_count)
220 struct v4l2_requestbuffers v4l2_reqbuf;
223 LOGE("invalid fd %d", device_fd);
224 return CAMERA_ERROR_INVALID_PARAMETER;
228 LOGE("NULL parameter");
229 return CAMERA_ERROR_INVALID_PARAMETER;
232 memset(&v4l2_reqbuf, 0x0, sizeof(struct v4l2_requestbuffers));
234 v4l2_reqbuf.type = type;
235 v4l2_reqbuf.memory = memory;
236 v4l2_reqbuf.count = count;
238 if (v4l2_ioctl(device_fd, VIDIOC_REQBUFS, &v4l2_reqbuf) < 0) {
239 LOGE("REQBUFS[count %d] failed. errno %d", count, errno);
240 return CAMERA_ERROR_INTERNAL;
243 if (v4l2_reqbuf.count != count)
244 LOGW("different count [req:%d, result:%d]", count, v4l2_reqbuf.count);
246 *result_count = v4l2_reqbuf.count;
248 return CAMERA_ERROR_NONE;
252 static int __camera_v4l2_qbuf(int device_fd, int type, int memory, int index)
254 struct v4l2_buffer v4l2_buf;
255 struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX];
258 LOGE("invalid fd %d", device_fd);
259 return CAMERA_ERROR_INVALID_PARAMETER;
262 memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer));
263 memset(v4l2_planes, 0x0, sizeof(v4l2_planes));
265 v4l2_buf.index = index;
266 v4l2_buf.type = type;
267 v4l2_buf.memory = memory;
268 v4l2_buf.m.planes = v4l2_planes;
269 v4l2_buf.length = 460800;
270 v4l2_buf.bytesused = 460800;
272 if (v4l2_ioctl(device_fd, VIDIOC_QBUF, &v4l2_buf) < 0) {
273 LOGE("qbuf failed. [i: %d, t: %d, m: %d] errno %d",
274 index, type, memory, errno);
275 return CAMERA_ERROR_INTERNAL;
278 /*LOGD("QBUF done [i: %d, t: %d, m: %d]", index, type, memory);*/
280 return CAMERA_ERROR_NONE;
284 static int __camera_v4l2_dqbuf(int device_fd, int type, int memory, int *index)
286 int ret = CAMERA_ERROR_NONE;
287 struct v4l2_buffer v4l2_buf;
288 struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX];
291 LOGE("invalid fd %d", device_fd);
292 return CAMERA_ERROR_INVALID_PARAMETER;
296 LOGE("NULL parameter");
297 return CAMERA_ERROR_INVALID_PARAMETER;
300 memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer));
301 memset(v4l2_planes, 0x0, sizeof(v4l2_planes));
303 v4l2_buf.type = type;
304 v4l2_buf.memory = memory;
305 v4l2_buf.m.planes = v4l2_planes;
307 ret = v4l2_ioctl(device_fd, VIDIOC_DQBUF, &v4l2_buf);
309 LOGE("dqbuf failed. [t: %d, m: %d] errno %d",
310 type, memory, errno);
311 return CAMERA_ERROR_DEVICE_READ;
314 *index = v4l2_buf.index;
316 /*LOGD("dqbuf index %d", *index);*/
318 return CAMERA_ERROR_NONE;
322 static int __camera_get_format(guint32 fourcc, int *pixel_format)
325 LOGE("NULL parameter");
326 return CAMERA_ERROR_INVALID_PARAMETER;
330 case V4L2_PIX_FMT_NV12:
331 case V4L2_PIX_FMT_NV12M:
332 case V4L2_PIX_FMT_NV12MT:
333 *pixel_format = CAMERA_PIXEL_FORMAT_NV12;
335 case V4L2_PIX_FMT_NV21:
336 case V4L2_PIX_FMT_NV21M:
337 *pixel_format = CAMERA_PIXEL_FORMAT_NV21;
339 case V4L2_PIX_FMT_YUV420:
340 *pixel_format = CAMERA_PIXEL_FORMAT_I420;
342 case V4L2_PIX_FMT_YVU420:
343 *pixel_format = CAMERA_PIXEL_FORMAT_YV12;
345 case V4L2_PIX_FMT_YUYV:
346 *pixel_format = CAMERA_PIXEL_FORMAT_YUYV;
348 case V4L2_PIX_FMT_UYVY:
349 *pixel_format = CAMERA_PIXEL_FORMAT_UYVY;
351 case V4L2_PIX_FMT_JPEG:
352 *pixel_format = CAMERA_PIXEL_FORMAT_JPEG;
354 case V4L2_PIX_FMT_H264:
355 *pixel_format = CAMERA_PIXEL_FORMAT_H264;
357 case V4L2_PIX_FMT_MJPEG:
358 *pixel_format = CAMERA_PIXEL_FORMAT_MJPEG;
361 LOGE("unknown fourcc "FOURCC_FORMAT, FOURCC_CONVERT(fourcc));
362 return CAMERA_ERROR_INTERNAL;
365 LOGD("fourcc "FOURCC_FORMAT" -> %d",
366 FOURCC_CONVERT(fourcc), *pixel_format);
368 return CAMERA_ERROR_NONE;
372 static int __camera_get_fourcc_plane_num(int pixel_format, guint32 *fourcc, guint32 *plane_num)
374 if (!fourcc || !plane_num) {
375 LOGE("NULL parameter %p %p", fourcc, plane_num);
376 return CAMERA_ERROR_INVALID_PARAMETER;
379 switch (pixel_format) {
380 case CAMERA_PIXEL_FORMAT_NV12:
381 *fourcc = V4L2_PIX_FMT_NV12;
384 case CAMERA_PIXEL_FORMAT_NV21:
385 *fourcc = V4L2_PIX_FMT_NV21;
388 case CAMERA_PIXEL_FORMAT_I420:
389 *fourcc = V4L2_PIX_FMT_YUV420;
392 case CAMERA_PIXEL_FORMAT_YV12:
393 *fourcc = V4L2_PIX_FMT_YVU420;
396 case CAMERA_PIXEL_FORMAT_YUYV:
397 *fourcc = V4L2_PIX_FMT_YUYV;
400 case CAMERA_PIXEL_FORMAT_UYVY:
401 *fourcc = V4L2_PIX_FMT_UYVY;
404 case CAMERA_PIXEL_FORMAT_JPEG:
405 *fourcc = V4L2_PIX_FMT_JPEG;
408 case CAMERA_PIXEL_FORMAT_H264:
409 *fourcc = V4L2_PIX_FMT_H264;
412 case CAMERA_PIXEL_FORMAT_MJPEG:
413 *fourcc = V4L2_PIX_FMT_MJPEG;
417 LOGE("unknown format %d", pixel_format);
418 return CAMERA_ERROR_INTERNAL;
421 LOGD("format %d -> fourcc "FOURCC_FORMAT,
422 pixel_format, FOURCC_CONVERT(*fourcc));
424 return CAMERA_ERROR_NONE;
428 static void __camera_get_fps_list(int device_fd, guint32 pixel_format, int width, int height, camera_fps_list_s *fps_list)
430 uint32_t fps_count = 0;
431 struct v4l2_frmivalenum ival;
433 if (device_fd < 0 || !fps_list) {
434 LOGE("invalid param %d %p", device_fd, fps_list);
439 ival.pixel_format = pixel_format;
441 ival.height = height;
443 while (v4l2_ioctl(device_fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0) {
444 if (ival.type != V4L2_FRMIVAL_TYPE_DISCRETE) {
445 LOGE("NOT DISCRETE type[%u] for [%dx%d]", ival.type, width, height);
449 if (ival.index++ >= FPS_COUNT_MAX) {
450 LOGW("\t\t\t\tFramerate[i:%u][%u/%u] is available, but list is full[max:%d]",
451 ival.index - 1, ival.discrete.denominator, ival.discrete.numerator, FPS_COUNT_MAX);
455 LOGI("\t\t\t\tFramerate[%u/%u]", ival.discrete.denominator, ival.discrete.numerator);
456 fps_list->fps[fps_count++] = ival.discrete.denominator;
460 fps_list->count = fps_count;
464 static int __camera_get_device_info(int device_index, int device_fd, camera_device_info_s *device_info, char *node_path)
466 int format_index = 0;
467 int format_count = 0;
468 int resolution_index = 0;
469 int resolution_count = 0;
470 int camera_format = 0;
471 struct v4l2_fmtdesc v4l2_format;
472 struct v4l2_frmsizeenum v4l2_frame;
474 if (device_fd < 0 || !device_info || !node_path) {
475 LOGE("invalid param %d %p %p", device_fd, device_info, node_path);
476 return CAMERA_ERROR_INVALID_PARAMETER;
479 LOGD("Get Supported format, resolution and fps");
481 for (format_index = 0, format_count = 0 ; ; format_index++) {
482 memset(&v4l2_format, 0x0, sizeof(struct v4l2_fmtdesc));
484 v4l2_format.index = format_index;
485 v4l2_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
487 if (v4l2_ioctl(device_fd, VIDIOC_ENUM_FMT, &v4l2_format) < 0) {
488 LOGW("\tformat : end of enumeration");
492 LOGD("\tTry [%d] format "FOURCC_FORMAT" (emulated:%d)",
493 format_count, FOURCC_CONVERT(v4l2_format.pixelformat),
494 ((v4l2_format.flags & V4L2_FMT_FLAG_EMULATED) ? 1 : 0));
496 if (__camera_get_format(v4l2_format.pixelformat, &camera_format) != CAMERA_ERROR_NONE)
499 if (format_count + 1 >= CAMERA_PIXEL_FORMAT_MAX) {
500 LOGW("format list is full[max:%u], skip format[i:%u][%d]",
501 CAMERA_PIXEL_FORMAT_MAX, v4l2_format.index, camera_format);
505 device_info->format_list.formats[format_count++] = camera_format;
507 for (resolution_index = 0, resolution_count = 0 ; ; resolution_index++) {
508 memset(&v4l2_frame, 0x0, sizeof(struct v4l2_frmsizeenum));
510 v4l2_frame.index = resolution_index;
511 v4l2_frame.pixel_format = v4l2_format.pixelformat;
513 if (v4l2_ioctl(device_fd, VIDIOC_ENUM_FRAMESIZES, &v4l2_frame) < 0) {
514 LOGW("\t\tframesize : end of enumeration");
518 if (resolution_count + 1 >= RESOLUTION_COUNT_MAX) {
519 LOGW("resolution list is full, skip resolution[%ux%u]", v4l2_frame.discrete.width, v4l2_frame.discrete.height);
523 switch (v4l2_frame.type) {
524 case V4L2_FRMSIZE_TYPE_DISCRETE:
525 device_info->preview_list.resolutions[resolution_count].width = v4l2_frame.discrete.width;
526 device_info->preview_list.resolutions[resolution_count].height = v4l2_frame.discrete.height;
527 device_info->capture_list.resolutions[resolution_count].width = v4l2_frame.discrete.width;
528 device_info->capture_list.resolutions[resolution_count].height = v4l2_frame.discrete.height;
529 device_info->video_list.resolutions[resolution_count].width = v4l2_frame.discrete.width;
530 device_info->video_list.resolutions[resolution_count].height = v4l2_frame.discrete.height;
532 LOGD("\t\tsize[%d] %ux%u", resolution_count,
533 v4l2_frame.discrete.width,
534 v4l2_frame.discrete.height);
536 __camera_get_fps_list(device_fd,
537 v4l2_frame.pixel_format,
538 v4l2_frame.discrete.width,
539 v4l2_frame.discrete.height,
540 &device_info->preview_fps_list[resolution_count]);
542 memcpy(&device_info->video_fps_list[resolution_count], &device_info->preview_fps_list[resolution_count], sizeof(camera_fps_list_s));
546 case V4L2_FRMSIZE_TYPE_CONTINUOUS:
547 LOGW("\t\tsize[%d] %ux%u - %ux%u", resolution_count,
548 v4l2_frame.stepwise.min_width,
549 v4l2_frame.stepwise.min_height,
550 v4l2_frame.stepwise.max_width,
551 v4l2_frame.stepwise.max_height);
553 case V4L2_FRMSIZE_TYPE_STEPWISE:
554 LOGW("\t\tsize[%d] %ux%u - %ux%u (step %ux%u)", resolution_count,
555 v4l2_frame.stepwise.min_width,
556 v4l2_frame.stepwise.min_height,
557 v4l2_frame.stepwise.max_width,
558 v4l2_frame.stepwise.max_height,
559 v4l2_frame.stepwise.step_width,
560 v4l2_frame.stepwise.step_height);
563 LOGE("\t\tunknown frame type %d", v4l2_frame.type);
568 device_info->preview_list.count = resolution_count;
569 device_info->capture_list.count = resolution_count;
570 device_info->video_list.count = resolution_count;
572 LOGD("\t\tresolution count [%d]", resolution_count);
575 device_info->index = device_index;
576 device_info->format_list.count = format_count;
577 device_info->facing_direction = CAMERA_FACING_DIRECTION_EXTERNAL;
578 snprintf(device_info->name, sizeof(device_info->name), "V4L2_CAMERA");
579 snprintf(device_info->node_path, sizeof(device_info->node_path), "%s", node_path);
581 LOGD("\tformat count [%d]", format_count);
583 return CAMERA_ERROR_NONE;
587 static int __camera_get_device_info_list(void)
591 int device_count = 0;
592 int device_fd = CAMERA_HAL_INITIAL_FD;
594 int libv4l2_fd = CAMERA_HAL_INITIAL_FD;
595 #endif /* HAVE_LIBV4L2 */
597 struct v4l2_capability v4l2_cap;
598 camera_device_info_list_s *device_info_list = NULL;
600 g_mutex_lock(&g_device_info_lock);
602 if (g_device_info_list) {
603 LOGD("device info list is already existed");
604 ret = CAMERA_ERROR_NONE;
605 goto _GET_DEVICE_INFO_LIST_DONE;
608 device_info_list = g_new0(camera_device_info_list_s, 1);
609 if (!device_info_list) {
610 LOGE("failed to alloc device info structure");
611 ret = CAMERA_ERROR_OUT_OF_MEMORY;
612 goto _GET_DEVICE_INFO_LIST_DONE;
615 memset(&glob_buf, 0x0, sizeof(glob_t));
617 ret = glob(DEVICE_NODE_PATH_PREFIX"*", 0, 0, &glob_buf);
621 LOGE("out of memory");
622 ret = CAMERA_ERROR_OUT_OF_MEMORY;
623 goto _GET_DEVICE_INFO_LIST_DONE;
626 ret = CAMERA_ERROR_INTERNAL;
627 goto _GET_DEVICE_INFO_LIST_DONE;
629 LOGE("match not found");
630 ret = CAMERA_ERROR_INTERNAL;
631 goto _GET_DEVICE_INFO_LIST_DONE;
633 LOGE("unknown error : %d", ret);
634 ret = CAMERA_ERROR_INTERNAL;
635 goto _GET_DEVICE_INFO_LIST_DONE;
639 LOGD("device node count : %zu", glob_buf.gl_pathc);
641 for (i = 0 ; i < glob_buf.gl_pathc ; i++) {
642 LOGD("[%d] check device [%s]", i, glob_buf.gl_pathv[i]);
644 device_fd = open(glob_buf.gl_pathv[i], O_RDWR);
646 LOGE("open failed [%s] errno %d", glob_buf.gl_pathv[i], errno);
651 libv4l2_fd = v4l2_fd_open(device_fd, V4L2_ENABLE_ENUM_FMT_EMULATION);
653 LOGI("device_fd[%d], libv4l2_fd[%d]", device_fd, libv4l2_fd);
655 if (libv4l2_fd != CAMERA_HAL_INITIAL_FD)
656 device_fd = libv4l2_fd;
657 #endif /* HAVE_LIBV4L2 */
659 memset(&v4l2_cap, 0x0, sizeof(struct v4l2_capability));
661 if (v4l2_ioctl(device_fd, VIDIOC_QUERYCAP, &v4l2_cap) < 0) {
662 LOGE("querycap failed. errno %d", errno);
663 v4l2_close(device_fd);
667 if (v4l2_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
668 g_device_caps = v4l2_cap.device_caps;
670 g_device_caps = v4l2_cap.capabilities;
672 if (!(g_device_caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ||
673 (g_device_caps & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE))) {
674 LOGW("[%s] is not a capture device 0x%x", glob_buf.gl_pathv[i], g_device_caps);
675 v4l2_close(device_fd);
679 ret = __camera_get_device_info(device_count, device_fd,
680 &device_info_list->device_info[device_count], glob_buf.gl_pathv[i]);
682 v4l2_close(device_fd);
684 if (ret == CAMERA_ERROR_NONE)
688 device_info_list->count = device_count;
689 g_device_info_list = device_info_list;
691 LOGD("new g_device_info_list %p - device count %d",
692 g_device_info_list, device_count);
694 _GET_DEVICE_INFO_LIST_DONE:
695 g_mutex_unlock(&g_device_info_lock);
696 LOGD("ret 0x%x", ret);
698 if (ret != CAMERA_ERROR_NONE)
699 g_free(device_info_list);
705 static int __camera_stop_stream(hal_camera_handle *handle, uint32_t buffer_count)
708 int ret = CAMERA_ERROR_NONE;
712 return CAMERA_ERROR_INVALID_PARAMETER;
715 LOGD("buffer count[%d]", buffer_count);
718 ret = __camera_v4l2_stream(handle->device_fd, handle->buffer_type, FALSE);
720 LOGD("stream off : 0x%x", ret);
723 for (i = 0 ; i < buffer_count ; i++) {
724 if (handle->camera_buffers[i].planes[0].data != NULL) {
725 LOGW("munmap %p", handle->camera_buffers[i].planes[0].data);
727 v4l2_munmap(handle->camera_buffers[i].planes[0].data, handle->camera_buffers[i].planes[0].size);
729 handle->camera_buffers[i].planes[0].data = 0;
730 handle->camera_buffers[i].planes[0].size = 0;
732 LOGW("NULL data [index %d]", i);
737 ret = __camera_v4l2_reqbufs(handle->device_fd,
738 handle->buffer_type, V4L2_MEMORY_MMAP, 0, &buffer_count);
740 LOGD("reqbufs 0 : 0x%x", ret);
746 static int __camera_start_stream(hal_camera_handle *handle, camera_pixel_format_e pixel_format,
747 camera_resolution_s *resolution, uint32_t fps, uint32_t request_buffer_count)
750 int ret = CAMERA_ERROR_NONE;
751 camera_buffer_s *buffer = NULL;
752 struct v4l2_format v4l2_fmt;
753 struct v4l2_streamparm v4l2_parm;
754 struct v4l2_buffer v4l2_buf;
755 struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX];;
757 guint32 plane_num = 0;
759 if (!handle || !resolution) {
760 LOGE("NULL param %p %p", handle, resolution);
761 return CAMERA_ERROR_INTERNAL;
765 ret = __camera_get_fourcc_plane_num(pixel_format, &fourcc, &plane_num);
766 if (ret != CAMERA_ERROR_NONE) {
767 LOGE("get fourcc failed [format %d]", pixel_format);
771 memset(&v4l2_fmt, 0x0, sizeof(struct v4l2_format));
773 v4l2_fmt.type = handle->buffer_type;
774 if (V4L2_TYPE_IS_MULTIPLANAR(handle->buffer_type)) {
775 v4l2_fmt.fmt.pix_mp.width = resolution->width;
776 v4l2_fmt.fmt.pix_mp.height = resolution->height;
777 v4l2_fmt.fmt.pix_mp.pixelformat = fourcc;
778 v4l2_fmt.fmt.pix_mp.num_planes = plane_num;
780 v4l2_fmt.fmt.pix.width = resolution->width;
781 v4l2_fmt.fmt.pix.height = resolution->height;
782 v4l2_fmt.fmt.pix.pixelformat = fourcc;
783 v4l2_fmt.fmt.pix.bytesperline = resolution->width;
786 if (v4l2_ioctl(handle->device_fd, VIDIOC_S_FMT, &v4l2_fmt) < 0) {
787 LOGE("S_FMT failed. errno %d", errno);
788 return CAMERA_ERROR_INTERNAL;
791 if (V4L2_TYPE_IS_MULTIPLANAR(handle->buffer_type)) {
792 for (i = 0 ; i < v4l2_fmt.fmt.pix_mp.num_planes ; i++) {
793 LOGD("plane[%d] stride %u, sizeimage %u", i,
794 v4l2_fmt.fmt.pix_mp.plane_fmt[i].bytesperline,
795 v4l2_fmt.fmt.pix_mp.plane_fmt[i].sizeimage);
798 LOGD("stride %d, sizeimage %d",
799 v4l2_fmt.fmt.pix.bytesperline,
800 v4l2_fmt.fmt.pix.sizeimage);
804 memset(&v4l2_parm, 0x0, sizeof(struct v4l2_streamparm));
806 v4l2_parm.type = handle->buffer_type;
808 if (v4l2_ioctl(handle->device_fd, VIDIOC_G_PARM, &v4l2_parm) < 0) {
809 LOGE("G_PARM failed. errno %d", errno);
810 return CAMERA_ERROR_INTERNAL;
813 /* S_PARM to set fps */
814 v4l2_parm.parm.capture.timeperframe.numerator = 1;
815 v4l2_parm.parm.capture.timeperframe.denominator = fps;
817 if (v4l2_ioctl(handle->device_fd, VIDIOC_S_PARM, &v4l2_parm) < 0) {
818 LOGE("S_PARM failed. errno %d", errno);
819 return CAMERA_ERROR_INTERNAL;
823 ret = __camera_v4l2_reqbufs(handle->device_fd,
824 handle->buffer_type, V4L2_MEMORY_MMAP, request_buffer_count, &handle->buffer_count);
825 if (ret != CAMERA_ERROR_NONE) {
829 LOGD("buffer count : request %d -> result %d",
830 request_buffer_count, handle->buffer_count);
832 /* query buffer, mmap and qbuf */
833 for (i = 0 ; i < handle->buffer_count ; i++) {
834 memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer));
835 memset(v4l2_planes, 0x0, sizeof(v4l2_planes));
837 v4l2_buf.type = handle->buffer_type;
838 v4l2_buf.memory = V4L2_MEMORY_MMAP;
840 v4l2_buf.m.planes = v4l2_planes;
841 v4l2_buf.length = plane_num;
843 if (v4l2_ioctl(handle->device_fd, VIDIOC_QUERYBUF, &v4l2_buf) < 0) {
844 LOGE("[%d] query buf failed. errno %d", i, errno);
845 goto _START_STREAM_FAILED;
848 buffer = &handle->camera_buffers[i];
851 buffer->format = pixel_format;
852 buffer->resolution.width = resolution->width;
853 buffer->resolution.height = resolution->height;
854 buffer->total_size = v4l2_buf.length;
855 buffer->num_planes = plane_num;
856 buffer->planes[0].size = v4l2_buf.length;
857 buffer->planes[0].data = v4l2_mmap(0,
859 PROT_READ | PROT_WRITE,
864 if (buffer->planes[0].data == MAP_FAILED) {
865 LOGE("[%d] mmap failed (errno %d)", i, errno);
866 goto _START_STREAM_FAILED;
869 if (__camera_v4l2_qbuf(handle->device_fd, handle->buffer_type, V4L2_MEMORY_MMAP, i) != CAMERA_ERROR_NONE) {
870 LOGE("[%d] qbuf failed (errno %d)", i, errno);
871 goto _START_STREAM_FAILED;
876 ret = __camera_v4l2_stream(handle->device_fd, handle->buffer_type, TRUE);
877 if (ret != CAMERA_ERROR_NONE) {
878 LOGE("stream on failed");
879 goto _START_STREAM_FAILED;
882 return CAMERA_ERROR_NONE;
884 _START_STREAM_FAILED:
885 __camera_stop_stream(handle, handle->buffer_count);
890 static void __camera_do_capture(hal_camera_handle *handle)
892 int ret = CAMERA_ERROR_NONE;
893 int buffer_index = 0;
894 gint64 current_time = 0;
895 gint64 previous_time = 0;
896 gint64 interval_us = 0;
905 if (handle->capture_count > 1)
906 interval_us = (gint64)handle->capture_interval_ms * 1000;
908 /* restart stream for capture */
909 if (handle->capture_restart_stream) {
910 ret = __camera_stop_stream(handle, handle->buffer_count);
911 if (ret != CAMERA_ERROR_NONE) {
912 LOGE("stop stream failed for capture[0x%x]", ret);
916 ret = __camera_start_stream(handle,
917 handle->preview_format.capture_format,
918 &handle->preview_format.capture_resolution,
919 handle->preview_format.stream_fps,
921 if (ret != CAMERA_ERROR_NONE) {
922 LOGE("start stream failed for capture[0x%x]", ret);
928 /* get capture buffer */
929 ret = __camera_v4l2_wait_frame(handle->device_fd, 5);
930 if (ret != CAMERA_ERROR_NONE) {
931 LOGE("frame wait failed for capture[0x%x]", ret);
935 ret = __camera_v4l2_dqbuf(handle->device_fd,
936 handle->buffer_type, V4L2_MEMORY_MMAP, &buffer_index);
937 if (ret != CAMERA_ERROR_NONE) {
938 LOGE("dqbuf failed for capture[0x%x]", ret);
942 if (handle->captured_count > 0) {
943 g_mutex_lock(&handle->buffer_lock);
944 if (handle->state != CAMERA_STATE_CAPTURING) {
945 LOGW("stop continuous capture");
946 handle->captured_count = handle->capture_count;
947 g_mutex_unlock(&handle->buffer_lock);
950 g_mutex_unlock(&handle->buffer_lock);
953 if (handle->capture_count > 1) {
954 current_time = g_get_monotonic_time();
956 LOGI("time[prev:%"PRId64", cur:%"PRId64"] interval[%"PRId64" us]",
957 previous_time, current_time, interval_us);
959 if (current_time < previous_time + interval_us)
963 g_mutex_lock(&handle->buffer_lock);
964 handle->captured_count++;
965 g_mutex_unlock(&handle->buffer_lock);
967 LOGD("capture cb[%p], buffer index[%d],count[%d]",
968 handle->capture_cb, buffer_index, handle->captured_count);
970 if (handle->capture_cb) {
971 handle->capture_cb(&handle->camera_buffers[buffer_index],
972 NULL, NULL, handle->capture_cb_data);
974 LOGW("capture callback is NULL");
975 /* Need to post error? */
978 previous_time = current_time;
981 ret = __camera_v4l2_qbuf(handle->device_fd,
982 handle->buffer_type, V4L2_MEMORY_MMAP, buffer_index);
983 if (ret != CAMERA_ERROR_NONE)
984 LOGE("qbuf failed for capture[0x%x]", ret);
985 } while (handle->captured_count < handle->capture_count);
987 g_mutex_lock(&handle->buffer_lock);
989 if (handle->state == CAMERA_STATE_CAPTURING) {
990 LOGD("wait for capture stop signal");
991 g_cond_wait(&handle->buffer_cond, &handle->buffer_lock);
992 LOGD("signal received");
994 LOGD("The state is already changed.");
997 g_mutex_unlock(&handle->buffer_lock);
1000 /* restart stream for preview */
1001 if (handle->capture_restart_stream) {
1002 ret = __camera_stop_stream(handle, handle->buffer_count);
1003 if (ret != CAMERA_ERROR_NONE)
1004 LOGE("stop stream failed for preview[0x%x]", ret);
1006 ret = __camera_start_stream(handle,
1007 handle->preview_format.stream_format,
1008 &handle->preview_format.stream_resolution,
1009 handle->preview_format.stream_fps,
1011 if (ret != CAMERA_ERROR_NONE)
1012 LOGE("start stream failed for preview[0x%x]", ret);
1019 static void *__camera_buffer_handler_func(gpointer data)
1021 int error = CAMERA_ERROR_NONE;
1023 hal_camera_handle *handle = (hal_camera_handle *)data;
1026 LOGE("NULL handle for buffer handler");
1032 /* run buffer thread */
1033 g_mutex_lock(&handle->buffer_lock);
1035 while (handle->buffer_thread_run) {
1036 g_mutex_unlock(&handle->buffer_lock);
1038 if (__camera_v4l2_wait_frame(handle->device_fd, 5) != CAMERA_ERROR_NONE) {
1039 LOGE("frame wait failed");
1040 g_mutex_lock(&handle->buffer_lock);
1044 g_mutex_lock(&handle->buffer_lock);
1046 if (handle->buffer_thread_run == FALSE) {
1047 LOGW("stop buffer handler thread");
1051 error = __camera_v4l2_dqbuf(handle->device_fd, handle->buffer_type, V4L2_MEMORY_MMAP, &index);
1052 if (error != CAMERA_ERROR_NONE) {
1053 LOGE("dqbuf failed[0x%x]", error);
1057 handle->buffer_dequeued_count++;
1059 /*LOGD("dequeued buffer count %d", handle->buffer_dequeued_count);*/
1061 g_mutex_unlock(&handle->buffer_lock);
1063 if (handle->preview_cb) {
1064 handle->preview_cb(&handle->camera_buffers[index], NULL, handle->preview_cb_data);
1066 LOGW("preview callback is NULL");
1067 camera_v4l2_release_preview_buffer((void *)handle, index);
1070 g_mutex_lock(&handle->extra_preview_lock);
1072 if (handle->extra_preview_cb) {
1073 handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 0, handle->extra_preview_cb_data);
1074 handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 1, handle->extra_preview_cb_data);
1075 handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 2, handle->extra_preview_cb_data);
1076 handle->extra_preview_cb(&handle->camera_buffers[index], NULL, 3, handle->extra_preview_cb_data);
1079 g_mutex_unlock(&handle->extra_preview_lock);
1081 /* check capture request flag */
1082 if (handle->capture_request) {
1083 __camera_do_capture(handle);
1084 handle->capture_request = FALSE;
1089 g_mutex_lock(&handle->buffer_lock);
1092 g_mutex_unlock(&handle->buffer_lock);
1094 if (error != CAMERA_ERROR_NONE)
1095 __camera_send_message(handle, CAMERA_MESSAGE_TYPE_ERROR, error);
1103 static void __camera_message_release_func(gpointer data)
1105 camera_message_s *message = (camera_message_s *)data;
1108 LOGW("NULL message");
1112 LOGD("release message %p, type %d", message, message->type);
1120 static void *_camera_message_handler_func(gpointer data)
1123 camera_message_s *message = NULL;
1124 hal_camera_handle *handle = (hal_camera_handle *)data;
1127 LOGE("NULL handle for capture thread");
1131 LOGD("enter - message thread");
1133 g_mutex_lock(&handle->msg_cb_lock);
1135 while (handle->msg_cb_run) {
1136 if (g_queue_is_empty(handle->msg_list)) {
1137 LOGD("wait for message");
1138 g_cond_wait(&handle->msg_cb_cond, &handle->msg_cb_lock);
1139 LOGD("message signal received");
1142 if (!handle->msg_cb_run) {
1143 LOGW("break message thread");
1147 message = g_queue_pop_head(handle->msg_list);
1149 LOGW("NULL message");
1153 g_mutex_unlock(&handle->msg_cb_lock);
1155 for (i = 0 ; i < MESSAGE_CALLBACK_MAX ; i++) {
1156 if (handle->msg_cb[i]) {
1157 LOGD("call message callback[%d] type[%d]", i, message->type);
1158 handle->msg_cb[i](message, handle->msg_cb_data[i]);
1165 g_mutex_lock(&handle->msg_cb_lock);
1168 g_mutex_unlock(&handle->msg_cb_lock);
1170 LOGD("leave - message thread");
1176 static void __camera_release_handle(hal_camera_handle *handle)
1179 LOGW("NULL handle");
1183 if (handle->msg_thread) {
1184 g_mutex_lock(&handle->msg_cb_lock);
1185 handle->msg_cb_run = FALSE;
1186 g_cond_signal(&handle->msg_cb_cond);
1187 g_mutex_unlock(&handle->msg_cb_lock);
1188 g_thread_join(handle->msg_thread);
1189 g_queue_free_full(handle->msg_list, (GDestroyNotify)__camera_message_release_func);
1190 handle->msg_list = NULL;
1193 g_mutex_clear(&handle->lock);
1194 g_mutex_clear(&handle->buffer_lock);
1195 g_mutex_clear(&handle->msg_cb_lock);
1196 g_mutex_clear(&handle->extra_preview_lock);
1197 g_cond_clear(&handle->buffer_cond);
1198 g_cond_clear(&handle->msg_cb_cond);
1200 if (handle->bufmgr) {
1201 tbm_bufmgr_deinit(handle->bufmgr);
1202 handle->bufmgr = NULL;
1205 LOGD("camera HAL handle %p destroy", handle);
1213 int camera_v4l2_init(void **camera_handle)
1215 int ret = CAMERA_ERROR_NONE;
1216 hal_camera_handle *new_handle = NULL;
1217 tbm_bufmgr bufmgr = NULL;
1221 if (!camera_handle) {
1222 LOGE("NULL pointer for handle");
1223 return CAMERA_ERROR_INVALID_PARAMETER;
1226 bufmgr = tbm_bufmgr_init(-1);
1227 if (bufmgr == NULL) {
1228 LOGE("get tbm bufmgr failed");
1229 return CAMERA_ERROR_INTERNAL;
1232 new_handle = g_new0(hal_camera_handle, 1);
1234 LOGE("failed to alloc camera hal handle");
1235 tbm_bufmgr_deinit(bufmgr);
1236 return CAMERA_ERROR_OUT_OF_MEMORY;
1239 new_handle->bufmgr = bufmgr;
1241 g_mutex_init(&new_handle->lock);
1242 g_mutex_init(&new_handle->buffer_lock);
1243 g_mutex_init(&new_handle->msg_cb_lock);
1244 g_mutex_init(&new_handle->extra_preview_lock);
1245 g_cond_init(&new_handle->buffer_cond);
1246 g_cond_init(&new_handle->msg_cb_cond);
1248 /* message thread */
1249 new_handle->msg_list = g_queue_new();
1250 new_handle->msg_cb_run = TRUE;
1251 new_handle->msg_thread = g_thread_try_new("camera_hal_msg_thread",
1252 _camera_message_handler_func, (gpointer)new_handle, NULL);
1253 if (!new_handle->msg_thread) {
1254 LOGE("failed to create message thread");
1255 ret = CAMERA_ERROR_INTERNAL;
1259 new_handle->device_index = CAMERA_HAL_INITIAL_INDEX;
1260 new_handle->device_fd = CAMERA_HAL_INITIAL_FD;
1261 new_handle->state = CAMERA_STATE_INITIALIZED;
1263 ret = __camera_get_device_info_list();
1264 if (ret != CAMERA_ERROR_NONE) {
1265 LOGE("get device info failed");
1270 LOGI("libv4l2 ENABLED");
1271 #else /* HAVE_LIBV4L2 */
1272 LOGI("libv4l2 DISABLED");
1273 #endif /* HAVE_LIBV4L2 */
1275 *camera_handle = new_handle;
1277 LOGD("camera HAL handle %p", new_handle);
1279 return CAMERA_ERROR_NONE;
1282 __camera_release_handle(new_handle);
1288 int camera_v4l2_deinit(void *camera_handle)
1290 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1293 LOGE("NULL handle");
1294 return CAMERA_ERROR_INVALID_PARAMETER;
1297 g_mutex_lock(&handle->lock);
1299 if (handle->state != CAMERA_STATE_INITIALIZED) {
1300 LOGE("invalid state %d, can not destroy handle", handle->state);
1301 g_mutex_unlock(&handle->lock);
1302 return CAMERA_ERROR_INVALID_STATE;
1305 g_mutex_unlock(&handle->lock);
1307 __camera_release_handle(handle);
1309 return CAMERA_ERROR_NONE;
1313 int camera_v4l2_get_device_info_list(camera_device_info_list_s *device_info_list)
1317 if (!device_info_list) {
1318 LOGE("NULL pointer for device_info_list");
1319 return CAMERA_ERROR_INVALID_PARAMETER;
1322 ret = __camera_get_device_info_list();
1323 if (ret != CAMERA_ERROR_NONE) {
1324 LOGE("get device info failed");
1328 memcpy(device_info_list, g_device_info_list, sizeof(camera_device_info_list_s));
1330 return CAMERA_ERROR_NONE;
1334 int camera_v4l2_open_device(void *camera_handle, int device_index)
1336 int ret = CAMERA_ERROR_NONE;
1337 int device_fd = CAMERA_HAL_INITIAL_FD;
1339 int libv4l2_fd = CAMERA_HAL_INITIAL_FD;
1340 #endif /* HAVE_LIBV4L2 */
1341 char *node_path = NULL;
1342 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1345 LOGE("NULL handle");
1346 return CAMERA_ERROR_INVALID_PARAMETER;
1349 g_mutex_lock(&handle->lock);
1351 if (handle->state != CAMERA_STATE_INITIALIZED) {
1352 LOGE("invalid state %d", handle->state);
1353 ret = CAMERA_ERROR_INVALID_STATE;
1354 goto _OPEN_DEVICE_DONE;
1357 if (!g_device_info_list) {
1358 LOGE("NO DEVICE INFO");
1359 ret = CAMERA_ERROR_INTERNAL;
1360 goto _OPEN_DEVICE_DONE;
1363 if (device_index >= g_device_info_list->count) {
1364 LOGE("invalid index %d [info:%d]", device_index, g_device_info_list->count);
1365 ret = CAMERA_ERROR_INVALID_PARAMETER;
1366 goto _OPEN_DEVICE_DONE;
1369 node_path = g_device_info_list->device_info[device_index].node_path;
1371 device_fd = open(node_path, O_RDWR);
1372 if (device_fd < 0) {
1376 ret = CAMERA_ERROR_PERMISSION_DENIED;
1379 ret = CAMERA_ERROR_DEVICE_NOT_FOUND;
1382 ret = CAMERA_ERROR_DEVICE_BUSY;
1385 ret = CAMERA_ERROR_DEVICE_OPEN;
1389 LOGE("open [%s] failed 0x%x [errno %d]",
1390 node_path, ret, errno);
1392 goto _OPEN_DEVICE_DONE;
1395 if (g_device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
1396 handle->buffer_type = V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1398 handle->buffer_type = V4L2_CAP_VIDEO_CAPTURE;
1401 libv4l2_fd = v4l2_fd_open(device_fd, V4L2_ENABLE_ENUM_FMT_EMULATION);
1403 LOGI("device_fd[%d], libv4l2_fd[%d]", device_fd, libv4l2_fd);
1405 if (libv4l2_fd != CAMERA_HAL_INITIAL_FD)
1406 device_fd = libv4l2_fd;
1407 #endif /* HAVE_LIBV4L2 */
1409 handle->state = CAMERA_STATE_OPENED;
1410 handle->device_index = device_index;
1411 handle->device_fd = device_fd;
1413 LOGD("[%d] device[%s] opened [fd %d, type %d]",
1414 device_index, node_path, device_fd, handle->buffer_type);
1417 g_mutex_unlock(&handle->lock);
1423 int camera_v4l2_open_device_ext(void *camera_handle, const char *device_name)
1425 LOGE("NOT SUPPORTED");
1426 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1430 int camera_v4l2_close_device(void *camera_handle)
1432 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1435 LOGE("NULL handle");
1436 return CAMERA_ERROR_INVALID_PARAMETER;
1439 g_mutex_lock(&handle->lock);
1441 if (handle->state != CAMERA_STATE_OPENED) {
1442 LOGE("invalid state %d", handle->state);
1443 g_mutex_unlock(&handle->lock);
1444 return CAMERA_ERROR_INVALID_STATE;
1447 if (handle->device_fd >= 0) {
1448 LOGD("close fd %d", handle->device_fd);
1450 v4l2_close(handle->device_fd);
1451 handle->device_fd = CAMERA_HAL_INITIAL_FD;
1453 LOGW("invalid fd %d", handle->device_fd);
1456 handle->state = CAMERA_STATE_INITIALIZED;
1458 LOGD("device [%d] closed", handle->device_index);
1460 g_mutex_unlock(&handle->lock);
1462 return CAMERA_ERROR_NONE;
1466 int camera_v4l2_add_message_callback(void *camera_handle, hal_camera_message_cb callback, void *user_data, uint32_t *cb_id)
1469 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1472 LOGE("NULL handle");
1473 return CAMERA_ERROR_INVALID_PARAMETER;
1476 if (!callback || !cb_id) {
1477 LOGE("NULL pointer for callback %p or cb_id %p", callback, cb_id);
1478 return CAMERA_ERROR_INVALID_PARAMETER;
1481 g_mutex_lock(&handle->lock);
1483 for (i = 0 ; i < MESSAGE_CALLBACK_MAX ; i++) {
1484 if (handle->msg_cb[i] == NULL) {
1485 handle->msg_cb[i] = callback;
1486 handle->msg_cb_data[i] = user_data;
1488 LOGD("message cb [%p] added, user data %p - id %u", callback, user_data, i);
1489 g_mutex_unlock(&handle->lock);
1490 return CAMERA_ERROR_NONE;
1494 g_mutex_unlock(&handle->lock);
1496 LOGE("no available message cb slot");
1498 return CAMERA_ERROR_INTERNAL;
1502 int camera_v4l2_remove_message_callback(void *camera_handle, uint32_t cb_id)
1504 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1507 LOGE("NULL handle");
1508 return CAMERA_ERROR_INVALID_PARAMETER;
1511 if (cb_id >= MESSAGE_CALLBACK_MAX) {
1512 LOGE("invalid cb_id %u", cb_id);
1513 return CAMERA_ERROR_INVALID_PARAMETER;
1516 g_mutex_lock(&handle->lock);
1518 if (handle->msg_cb[cb_id]) {
1519 LOGD("remove message callback %p, user data %p - cb_id %u",
1520 handle->msg_cb[cb_id], handle->msg_cb_data[cb_id], cb_id);
1522 handle->msg_cb[cb_id] = NULL;
1523 handle->msg_cb_data[cb_id] = NULL;
1525 LOGE("already removed message cb");
1526 g_mutex_unlock(&handle->lock);
1527 return CAMERA_ERROR_INTERNAL;
1530 g_mutex_unlock(&handle->lock);
1532 return CAMERA_ERROR_NONE;
1536 int camera_v4l2_set_preview_stream_format(void *camera_handle, camera_format_s *format)
1540 int ret = CAMERA_ERROR_NONE;
1541 gboolean capability_check = FALSE;
1542 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1543 camera_device_info_s *device_info = NULL;
1545 if (!handle || !format) {
1546 LOGE("NULL param %p %p", handle, format);
1547 return CAMERA_ERROR_INVALID_PARAMETER;
1550 if (!g_device_info_list) {
1551 LOGE("no device info list");
1552 return CAMERA_ERROR_INTERNAL;
1555 g_mutex_lock(&handle->lock);
1557 if (handle->state != CAMERA_STATE_OPENED &&
1558 handle->state != CAMERA_STATE_PREVIEWING) {
1559 LOGE("invalid state %d", handle->state);
1560 g_mutex_unlock(&handle->lock);
1561 return CAMERA_ERROR_INVALID_STATE;
1564 /* check capability */
1565 device_info = &g_device_info_list->device_info[handle->device_index];
1568 for (i = 0 ; i < device_info->format_list.count ; i++) {
1569 if (format->stream_format == device_info->format_list.formats[i]) {
1570 LOGD("format matched %d, check resolution.", format->stream_format);
1573 for (j = 0 ; j < device_info->preview_list.count ; j++) {
1574 if (format->stream_resolution.width == device_info->preview_list.resolutions[j].width &&
1575 format->stream_resolution.height == device_info->preview_list.resolutions[j].height) {
1576 LOGD("resolution matched %dx%d",
1577 format->stream_resolution.width,
1578 format->stream_resolution.height);
1579 capability_check = TRUE;
1588 if (!capability_check) {
1589 LOGE("capability failed - %d, %dx%d",
1590 format->stream_format,
1591 format->stream_resolution.width,
1592 format->stream_resolution.height);
1593 g_mutex_unlock(&handle->lock);
1594 return CAMERA_ERROR_INVALID_PARAMETER;
1597 /* compare with current settings */
1598 if (handle->state == CAMERA_STATE_PREVIEWING) {
1599 if (handle->preview_format.stream_format == format->stream_format &&
1600 handle->preview_format.stream_resolution.width == format->stream_resolution.width &&
1601 handle->preview_format.stream_resolution.height == format->stream_resolution.height &&
1602 handle->preview_format.stream_fps == format->stream_fps &&
1603 handle->preview_format.stream_rotation == format->stream_rotation) {
1604 LOGD("no need to restart preview stream");
1605 goto _SET_PREVIEW_STREAM_FORMAT_DONE;
1608 LOGD("Preview setting is changed. Restart preview now.");
1610 /* stop preview stream to change it */
1611 ret = __camera_stop_stream(handle, handle->buffer_count);
1612 if (ret != CAMERA_ERROR_NONE) {
1613 LOGE("failed to stop stream");
1614 g_mutex_unlock(&handle->lock);
1618 /* restart preview stream to change it */
1619 ret = __camera_start_stream(handle,
1620 format->stream_format,
1621 &format->stream_resolution,
1624 if (ret != CAMERA_ERROR_NONE) {
1625 LOGE("failed to start stream");
1626 g_mutex_unlock(&handle->lock);
1631 _SET_PREVIEW_STREAM_FORMAT_DONE:
1632 /* set capture restart flag */
1633 if (format->stream_format == format->capture_format &&
1634 format->stream_resolution.width == format->capture_resolution.width &&
1635 format->stream_resolution.height == format->capture_resolution.height)
1636 handle->capture_restart_stream = FALSE;
1638 handle->capture_restart_stream = TRUE;
1640 memcpy(&handle->preview_format, format, sizeof(camera_format_s));
1642 LOGD("set format PREVIEW[%d:%dx%d,fps:%d], CAPTURE[%d:%dx%d,restart:%d]",
1643 format->stream_format,
1644 format->stream_resolution.width,
1645 format->stream_resolution.height,
1647 format->capture_format,
1648 format->capture_resolution.width,
1649 format->capture_resolution.height,
1650 handle->capture_restart_stream);
1652 g_mutex_unlock(&handle->lock);
1654 return CAMERA_ERROR_NONE;
1658 int camera_v4l2_get_preview_stream_format(void *camera_handle, camera_format_s *format)
1660 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1662 if (!handle || !format) {
1663 LOGE("NULL param %p %p", handle, format);
1664 return CAMERA_ERROR_INVALID_PARAMETER;
1667 g_mutex_lock(&handle->lock);
1669 memcpy(format, &handle->preview_format, sizeof(camera_format_s));
1671 LOGD("get stream format %d, %dx%d", format->stream_format,
1672 format->stream_resolution.width, format->stream_resolution.height);
1674 g_mutex_unlock(&handle->lock);
1676 return CAMERA_ERROR_NONE;
1680 int camera_v4l2_set_user_buffer_fd(void *camera_handle, int *fds, int number)
1682 LOGE("NOT SUPPORTED");
1683 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1687 int camera_v4l2_start_preview(void *camera_handle, hal_camera_preview_frame_cb callback, void *user_data)
1690 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1692 if (!handle || !callback) {
1693 LOGE("NULL param %p %p", handle, callback);
1694 return CAMERA_ERROR_INVALID_PARAMETER;
1697 g_mutex_lock(&handle->lock);
1699 if (handle->state != CAMERA_STATE_OPENED) {
1700 LOGE("invalid state %d", handle->state);
1701 g_mutex_unlock(&handle->lock);
1702 return CAMERA_ERROR_INVALID_STATE;
1705 ret = __camera_start_stream(handle,
1706 handle->preview_format.stream_format,
1707 &handle->preview_format.stream_resolution,
1708 handle->preview_format.stream_fps,
1710 if (ret != CAMERA_ERROR_NONE) {
1711 LOGE("__camera_start_stream failed[0x%x]", ret);
1712 g_mutex_unlock(&handle->lock);
1716 g_mutex_lock(&handle->buffer_lock);
1718 handle->buffer_thread_run = TRUE;
1720 handle->buffer_thread = g_thread_try_new("camera_hal_buffer_thread",
1721 __camera_buffer_handler_func, (gpointer)handle, NULL);
1722 if (!handle->buffer_thread) {
1723 LOGE("failed to create buffer handler thread");
1724 g_mutex_unlock(&handle->buffer_lock);
1726 __camera_stop_stream(handle, handle->buffer_count);
1728 g_mutex_unlock(&handle->lock);
1733 handle->preview_cb = callback;
1734 handle->preview_cb_data = user_data;
1736 g_mutex_unlock(&handle->buffer_lock);
1738 handle->state = CAMERA_STATE_PREVIEWING;
1740 LOGD("start preview done");
1742 g_mutex_unlock(&handle->lock);
1744 return CAMERA_ERROR_NONE;
1748 int camera_v4l2_release_preview_buffer(void *camera_handle, int buffer_index)
1750 int ret = CAMERA_ERROR_NONE;
1751 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1754 LOGE("NULL handle");
1755 return CAMERA_ERROR_INVALID_PARAMETER;
1758 if (buffer_index >= handle->buffer_count) {
1759 LOGE("invalid buffer index %d", buffer_index);
1760 return CAMERA_ERROR_INVALID_PARAMETER;
1763 ret = __camera_v4l2_qbuf(handle->device_fd,
1764 handle->buffer_type, V4L2_MEMORY_MMAP, buffer_index);
1766 g_mutex_lock(&handle->buffer_lock);
1768 if (ret == CAMERA_ERROR_NONE) {
1769 if (handle->buffer_dequeued_count > 0)
1770 handle->buffer_dequeued_count--;
1772 LOGW("invalid dequeued buffer count[%u]", handle->buffer_dequeued_count);
1774 /*LOGD("qbud done : index %d, dequeued buffer count %d",
1775 buffer_index, handle->buffer_dequeued_count);*/
1777 LOGE("qbuf failed [index %d]", buffer_index);
1780 g_cond_signal(&handle->buffer_cond);
1782 g_mutex_unlock(&handle->buffer_lock);
1788 int camera_v4l2_stop_preview(void *camera_handle)
1790 int ret = CAMERA_ERROR_NONE;
1792 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1795 LOGE("NULL handle");
1796 return CAMERA_ERROR_INVALID_PARAMETER;
1801 g_mutex_lock(&handle->lock);
1803 if (handle->state != CAMERA_STATE_PREVIEWING) {
1804 LOGE("invalid state %d", handle->state);
1805 g_mutex_unlock(&handle->lock);
1806 return CAMERA_ERROR_INVALID_STATE;
1809 g_mutex_lock(&handle->buffer_lock);
1811 handle->buffer_thread_run = FALSE;
1813 while (handle->buffer_dequeued_count > 0) {
1814 LOGD("wait for dequeued buffer [%d]", handle->buffer_dequeued_count);
1815 end_time = g_get_monotonic_time() + 3 * G_TIME_SPAN_SECOND;
1816 if (!g_cond_wait_until(&handle->buffer_cond, &handle->buffer_lock, end_time)) {
1817 LOGE("buffer wait failed");
1820 LOGD("signal received. check again...");
1824 g_mutex_unlock(&handle->buffer_lock);
1826 ret = __camera_stop_stream(handle, handle->buffer_count);
1828 /* wait for preview thread exit */
1829 g_thread_join(handle->buffer_thread);
1830 handle->buffer_thread = NULL;
1832 handle->state = CAMERA_STATE_OPENED;
1834 LOGD("stop preview done [0x%x]", ret);
1836 g_mutex_unlock(&handle->lock);
1838 return CAMERA_ERROR_NONE;
1842 int camera_v4l2_start_auto_focus(void *camera_handle)
1844 if (!camera_handle) {
1845 LOGE("NULL handle");
1846 return CAMERA_ERROR_INVALID_PARAMETER;
1849 LOGE("NOT SUPPORTED");
1851 /* auto focus is not supported */
1852 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1856 int camera_v4l2_stop_auto_focus(void *camera_handle)
1858 if (!camera_handle) {
1859 LOGE("NULL handle");
1860 return CAMERA_ERROR_INVALID_PARAMETER;
1863 LOGE("NOT SUPPORTED");
1865 /* auto focus is not supported */
1866 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1870 int camera_v4l2_start_capture(void *camera_handle, hal_camera_capture_cb callback, void *user_data)
1872 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1874 if (!handle || !callback) {
1875 LOGE("NULL param %p %p", camera_handle, callback);
1876 return CAMERA_ERROR_INVALID_PARAMETER;
1879 g_mutex_lock(&handle->lock);
1881 if (handle->state != CAMERA_STATE_PREVIEWING) {
1882 LOGE("invalid state %d", handle->state);
1883 g_mutex_unlock(&handle->lock);
1884 return CAMERA_ERROR_INVALID_STATE;
1887 /* set callback and user data */
1888 handle->capture_cb = callback;
1889 handle->capture_cb_data = user_data;
1891 /* reset captured count */
1892 handle->captured_count = 0;
1894 LOGD("start capture - count %u", handle->capture_count);
1896 /* set capture request flag */
1897 handle->capture_request = TRUE;
1899 handle->state = CAMERA_STATE_CAPTURING;
1901 g_mutex_unlock(&handle->lock);
1903 return CAMERA_ERROR_NONE;
1907 int camera_v4l2_stop_capture(void *camera_handle)
1909 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
1912 LOGE("NULL handle");
1913 return CAMERA_ERROR_INVALID_PARAMETER;
1916 g_mutex_lock(&handle->lock);
1918 if (handle->state != CAMERA_STATE_CAPTURING) {
1919 LOGE("invalid state %d", handle->state);
1920 g_mutex_unlock(&handle->lock);
1921 return CAMERA_ERROR_INVALID_STATE;
1924 g_mutex_lock(&handle->buffer_lock);
1926 if (handle->captured_count == 0) {
1927 LOGE("No captured image yet.");
1928 g_mutex_unlock(&handle->buffer_lock);
1929 g_mutex_unlock(&handle->lock);
1930 return CAMERA_ERROR_INTERNAL;
1933 LOGD("send signal to start preview after capture");
1935 g_cond_signal(&handle->buffer_cond);
1936 g_mutex_unlock(&handle->buffer_lock);
1938 handle->state = CAMERA_STATE_PREVIEWING;
1940 g_mutex_unlock(&handle->lock);
1942 return CAMERA_ERROR_NONE;
1946 int camera_v4l2_set_video_stream_format(void *camera_handle, camera_format_s *format)
1948 if (!camera_handle || !format) {
1949 LOGE("NULL param %p %p", camera_handle, format);
1950 return CAMERA_ERROR_INVALID_PARAMETER;
1953 LOGE("NOT SUPPORTED");
1955 /* single stream device can not support video stream */
1956 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1960 int camera_v4l2_get_video_stream_format(void *camera_handle, camera_format_s *format)
1962 if (!camera_handle || !format) {
1963 LOGE("NULL param %p %p", camera_handle, format);
1964 return CAMERA_ERROR_INVALID_PARAMETER;
1967 LOGE("NOT SUPPORTED");
1969 /* single stream device can not support video stream */
1970 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1974 int camera_v4l2_start_record(void *camera_handle, hal_camera_video_frame_cb callback, void *user_data)
1976 if (!camera_handle || !callback) {
1977 LOGE("NULL param %p %p", camera_handle, callback);
1978 return CAMERA_ERROR_INVALID_PARAMETER;
1981 LOGE("NOT SUPPORTED");
1983 /* single stream device can not support video stream */
1984 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
1988 int camera_v4l2_release_video_buffer(void *camera_handle, int buffer_index)
1990 if (!camera_handle) {
1991 LOGE("NULL handle");
1992 return CAMERA_ERROR_INVALID_PARAMETER;
1995 LOGE("NOT SUPPORTED");
1997 /* single stream device can not support video stream */
1998 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2002 int camera_v4l2_stop_record(void *camera_handle)
2004 if (!camera_handle) {
2005 LOGE("NULL handle");
2006 return CAMERA_ERROR_INVALID_PARAMETER;
2009 LOGE("NOT SUPPORTED");
2011 /* single stream device can not support video stream */
2012 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2016 int camera_v4l2_set_extra_preview_frame_cb(void *camera_handle, hal_camera_extra_preview_frame_cb callback, void *user_data)
2018 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2021 LOGE("NULL handle");
2022 return CAMERA_ERROR_INVALID_PARAMETER;
2025 g_mutex_lock(&handle->extra_preview_lock);
2027 handle->extra_preview_cb = callback;
2028 handle->extra_preview_cb_data = user_data;
2030 g_mutex_unlock(&handle->extra_preview_lock);
2034 return CAMERA_ERROR_NONE;
2038 int camera_v4l2_unset_extra_preview_frame_cb(void *camera_handle)
2040 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2043 LOGE("NULL handle");
2044 return CAMERA_ERROR_INVALID_PARAMETER;
2047 g_mutex_lock(&handle->extra_preview_lock);
2049 handle->extra_preview_cb = NULL;
2050 handle->extra_preview_cb_data = NULL;
2052 g_mutex_unlock(&handle->extra_preview_lock);
2056 return CAMERA_ERROR_NONE;
2060 int camera_v4l2_release_extra_preview_buffer(void *camera_handle, int stream_id, int buffer_index)
2062 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2065 LOGE("NULL handle");
2066 return CAMERA_ERROR_INVALID_PARAMETER;
2069 LOGI("done - stream_id[%d], index[%d]", stream_id, buffer_index);
2071 return CAMERA_ERROR_NONE;
2075 int camera_v4l2_set_extra_preview_stream_format(void *camera_handle, int stream_id, camera_format_s *format)
2077 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2080 LOGE("NULL handle");
2081 return CAMERA_ERROR_INVALID_PARAMETER;
2084 if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2085 LOGE("invalid stream_id[%d]", stream_id);
2086 return CAMERA_ERROR_INVALID_PARAMETER;
2089 LOGI("stream_id[%d], [%d,%dx%d,%d]",
2090 stream_id, format->stream_format,
2091 format->stream_resolution.width, format->stream_resolution.height,
2092 format->stream_fps);
2094 memcpy(&handle->extra_preview_format[stream_id], format, sizeof(camera_format_s));
2096 return CAMERA_ERROR_NONE;
2100 int camera_v4l2_get_extra_preview_stream_format(void *camera_handle, int stream_id, camera_format_s *format)
2102 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2104 if (!handle || !format) {
2105 LOGE("NULL param[%p,%p]", handle, format);
2106 return CAMERA_ERROR_INVALID_PARAMETER;
2109 if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2110 LOGE("invalid stream_id[%d]", stream_id);
2111 return CAMERA_ERROR_INVALID_PARAMETER;
2114 memcpy(format, &handle->extra_preview_format[stream_id], sizeof(camera_format_s));
2116 LOGI("stream_id[%d], [%d,%dx%d,%d]",
2117 stream_id, format->stream_format,
2118 format->stream_resolution.width, format->stream_resolution.height,
2119 format->stream_fps);
2121 return CAMERA_ERROR_NONE;
2125 int camera_v4l2_set_extra_preview_bitrate(void *camera_handle, int stream_id, int bitrate)
2127 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2130 LOGE("NULL handle");
2131 return CAMERA_ERROR_INVALID_PARAMETER;
2134 if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2135 LOGE("invalid stream_id[%d]", stream_id);
2136 return CAMERA_ERROR_INVALID_PARAMETER;
2139 LOGI("set bitrate[%d] for stream_id[%d]", bitrate, stream_id);
2141 handle->extra_preview_format[stream_id].stream_bitrate = (uint32_t)bitrate;
2143 return CAMERA_ERROR_NONE;
2147 int camera_v4l2_get_extra_preview_bitrate(void *camera_handle, int stream_id, int *bitrate)
2149 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2151 if (!handle || !bitrate) {
2152 LOGE("NULL param[%p,%p]", handle, bitrate);
2153 return CAMERA_ERROR_INVALID_PARAMETER;
2156 if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2157 LOGE("invalid stream_id[%d]", stream_id);
2158 return CAMERA_ERROR_INVALID_PARAMETER;
2161 *bitrate = (int)handle->extra_preview_format[stream_id].stream_bitrate;
2163 LOGI("get bitrate[%d] for stream_id[%d]", *bitrate, stream_id);
2165 return CAMERA_ERROR_NONE;
2169 int camera_v4l2_set_extra_preview_gop_interval(void *camera_handle, int stream_id, int interval)
2171 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2174 LOGE("NULL handle");
2175 return CAMERA_ERROR_INVALID_PARAMETER;
2178 if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2179 LOGE("invalid stream_id[%d]", stream_id);
2180 return CAMERA_ERROR_INVALID_PARAMETER;
2183 LOGI("set GOP interval[%d] for stream_id[%d]", interval, stream_id);
2185 handle->extra_preview_gop_interval[stream_id] = interval;
2187 return CAMERA_ERROR_NONE;
2191 int camera_v4l2_get_extra_preview_gop_interval(void *camera_handle, int stream_id, int *interval)
2193 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2195 if (!handle || !interval) {
2196 LOGE("NULL param[%p,%p]", handle, interval);
2197 return CAMERA_ERROR_INVALID_PARAMETER;
2200 if (stream_id < 0 || stream_id >= EXTRA_PREVIEW_STREAM_MAX) {
2201 LOGE("invalid stream_id[%d]", stream_id);
2202 return CAMERA_ERROR_INVALID_PARAMETER;
2205 *interval = (int)handle->extra_preview_gop_interval[stream_id];
2207 LOGI("get GOP interval[%d] for stream_id[%d]", *interval, stream_id);
2209 return CAMERA_ERROR_NONE;
2213 static int __set_command(hal_camera_handle *handle, int64_t command, void *value)
2219 if (!handle || !value) {
2220 LOGE("NULL param %p %p", handle, value);
2221 return CAMERA_ERROR_INVALID_PARAMETER;
2224 set_value = *(int *)value;
2226 if (handle->state < CAMERA_STATE_OPENED) {
2227 LOGE("invalid state %d", handle->state);
2228 return CAMERA_ERROR_INVALID_STATE;
2231 LOGD("set command %"PRIx64" - state %d", command, handle->state);
2234 case CAMERA_COMMAND_BRIGHTNESS:
2235 cid = V4L2_CID_BRIGHTNESS;
2237 case CAMERA_COMMAND_CONTRAST:
2238 cid = V4L2_CID_CONTRAST;
2240 case CAMERA_COMMAND_SATURATION:
2241 cid = V4L2_CID_SATURATION;
2243 case CAMERA_COMMAND_SHARPNESS:
2244 cid = V4L2_CID_SHARPNESS;
2246 case CAMERA_COMMAND_PTZ_TYPE:
2247 if (set_value != CAMERA_PTZ_TYPE_ELECTRONIC) {
2248 LOGE("not supported PTZ type %d", set_value);
2249 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2251 return CAMERA_ERROR_NONE;
2252 case CAMERA_COMMAND_PAN:
2253 cid = V4L2_CID_PAN_ABSOLUTE;
2255 case CAMERA_COMMAND_TILT:
2256 cid = V4L2_CID_TILT_ABSOLUTE;
2258 case CAMERA_COMMAND_FLIP:
2259 if (set_value != CAMERA_FLIP_NONE) {
2260 LOGE("NOT_SUPPORTED flip %d", set_value);
2261 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2263 return CAMERA_ERROR_NONE;
2264 case CAMERA_COMMAND_CAPTURE_COUNT:
2265 handle->capture_count = set_value;
2266 LOGI("capture count %u", handle->capture_count);
2267 return CAMERA_ERROR_NONE;
2268 case CAMERA_COMMAND_CAPTURE_INTERVAL:
2269 handle->capture_interval_ms = set_value;
2270 LOGI("capture interval %u ms", handle->capture_interval_ms);
2271 return CAMERA_ERROR_NONE;
2272 case CAMERA_COMMAND_FOCUS_MODE:
2273 LOGI("set focus mode [old:%d -> new:%d]", handle->focus_mode, set_value);
2274 handle->focus_mode = set_value;
2275 return CAMERA_ERROR_NONE;
2276 case CAMERA_COMMAND_FOCUS_RANGE:
2277 LOGI("set focus range [old:%d -> new:%d]", handle->focus_range, set_value);
2278 handle->focus_range = set_value;
2279 return CAMERA_ERROR_NONE;
2280 case CAMERA_COMMAND_FOCUS_LEVEL:
2281 LOGI("set focus level [old:%d -> new:%d]", handle->focus_level, set_value);
2282 handle->focus_level = set_value;
2283 return CAMERA_ERROR_NONE;
2285 LOGE("NOT_SUPPORTED command %"PRIx64, command);
2286 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2289 ctrl_ret = __camera_v4l2_s_ctrl(handle->device_fd, cid, set_value);
2294 LOGE("Permission denied %d", errno);
2295 return CAMERA_ERROR_PERMISSION_DENIED;
2297 LOGE("Invalid argument");
2298 return CAMERA_ERROR_INVALID_PARAMETER;
2300 LOGE("Device busy");
2301 return CAMERA_ERROR_DEVICE_BUSY;
2303 LOGE("Not supported");
2304 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2306 LOGE("Unknown errro %d", errno);
2307 return CAMERA_ERROR_INTERNAL;
2311 return CAMERA_ERROR_NONE;
2315 int camera_v4l2_set_command(void *camera_handle, int64_t command, void *value)
2317 int ret = CAMERA_ERROR_NONE;
2318 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2320 g_mutex_lock(&handle->lock);
2322 ret = __set_command(handle, command, value);
2324 g_mutex_unlock(&handle->lock);
2330 int camera_v4l2_get_command(void *camera_handle, int64_t command, void **value)
2332 int ret = CAMERA_ERROR_NONE;
2335 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2337 if (!handle || !value) {
2338 LOGE("NULL param %p %p", handle, value);
2339 return CAMERA_ERROR_INVALID_PARAMETER;
2342 g_mutex_lock(&handle->lock);
2344 LOGD("get command %"PRIx64" - state %d", command, handle->state);
2347 case CAMERA_COMMAND_BRIGHTNESS:
2348 cid = V4L2_CID_BRIGHTNESS;
2350 case CAMERA_COMMAND_CONTRAST:
2351 cid = V4L2_CID_CONTRAST;
2353 case CAMERA_COMMAND_SATURATION:
2354 cid = V4L2_CID_SATURATION;
2356 case CAMERA_COMMAND_SHARPNESS:
2357 cid = V4L2_CID_SHARPNESS;
2359 case CAMERA_COMMAND_FOCUS_MODE:
2360 **(int **)value = handle->focus_mode;
2361 LOGI("get focus mode %d", **(int **)value);
2362 goto _GET_COMMAND_DONE;
2363 case CAMERA_COMMAND_FOCUS_RANGE:
2364 **(int **)value = handle->focus_range;
2365 LOGI("get focus range %d", **(int **)value);
2366 goto _GET_COMMAND_DONE;
2367 case CAMERA_COMMAND_FOCUS_LEVEL:
2368 **(int **)value = handle->focus_level;
2369 LOGI("get focus level %d", **(int **)value);
2370 goto _GET_COMMAND_DONE;
2372 LOGE("NOT_SUPPORTED %"PRIx64, command);
2373 g_mutex_unlock(&handle->lock);
2374 return CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2377 ctrl_ret = __camera_v4l2_g_ctrl(handle->device_fd, cid, (int *)*value);
2382 LOGE("Permission denied %d", errno);
2383 ret = CAMERA_ERROR_PERMISSION_DENIED;
2386 LOGE("Invalid argument");
2387 ret = CAMERA_ERROR_INVALID_PARAMETER;
2390 LOGE("Device busy");
2391 ret = CAMERA_ERROR_DEVICE_BUSY;
2394 LOGE("Not supported");
2395 ret = CAMERA_ERROR_DEVICE_NOT_SUPPORTED;
2398 LOGE("Unknown errro %d", errno);
2399 ret = CAMERA_ERROR_INTERNAL;
2405 g_mutex_unlock(&handle->lock);
2411 static void __dump_batch_command(camera_batch_command_control_s *batch_command)
2413 if (!batch_command) {
2414 LOGE("NULL batch command");
2418 LOGI("[WHITE_BALANCE] %d", batch_command->white_balance);
2419 LOGI("[ISO] %d", batch_command->iso);
2420 LOGI("[CONTRAST] %d", batch_command->contrast);
2421 LOGI("[HUE] %d", batch_command->hue);
2422 LOGI("[SATURATION] %d", batch_command->saturation);
2423 LOGI("[SHARPNESS] %d", batch_command->sharpness);
2424 LOGI("[BRIGHTNESS] %d", batch_command->brightness);
2425 LOGI("[EFFECT] %d", batch_command->effect);
2426 LOGI("[SCENE_MODE] %d", batch_command->scene_mode);
2427 LOGI("[EXPOSURE_MODE] %d", batch_command->exposure_mode);
2428 LOGI("[EXPOSURE] %d", batch_command->exposure);
2429 LOGI("[ROTATION] %d", batch_command->rotation);
2430 LOGI("[FLIP] %d", batch_command->flip);
2431 LOGI("[FOCUS_MODE] %d", batch_command->focus_mode);
2432 LOGI("[FOCUS_RANGE] %d", batch_command->focus_range);
2433 LOGI("[FOCUS_AREA] %d,%d,%dx%d", batch_command->focus_area.x, batch_command->focus_area.y,
2434 batch_command->focus_area.width, batch_command->focus_area.height);
2435 LOGI("[FOCUS_LEVEL] %d", batch_command->focus_level);
2436 LOGI("[SHOT_MODE] %d", batch_command->shot_mode);
2437 LOGI("[ANTI_SHAKE] %d", batch_command->anti_shake);
2438 LOGI("[DIGITAL_ZOOM] %d", batch_command->digital_zoom);
2439 LOGI("[OPTICAL_ZOOM] %d", batch_command->optical_zoom);
2440 LOGI("[RECORDING_HINT] %d", batch_command->recording_hint);
2441 LOGI("[WDR] %d", batch_command->wdr);
2442 LOGI("[SHUTTER_SPEED] %d/%d", batch_command->shutter_speed.numerator, batch_command->shutter_speed.denominator);
2443 LOGI("[FLASH_MODE] %d", batch_command->flash_mode);
2444 LOGI("[FLASH_BRIGHTNESS] %d", batch_command->flash_brightness);
2445 LOGI("[FACE_DETECTION] %d", batch_command->face_detection);
2446 LOGI("[PTZ_TYPE] %d", batch_command->ptz_type);
2447 LOGI("[PAN] %d", batch_command->pan);
2448 LOGI("[TILT] %d", batch_command->tilt);
2449 LOGI("[BITRATE] %d", batch_command->bitrate);
2450 LOGI("[GOP_INTERVAL] %d", batch_command->gop_interval);
2451 LOGI("[CAPTURE_COUNT] %d", batch_command->capture_count);
2452 LOGI("[CAPTURE_INTERVAL] %d", batch_command->capture_interval);
2456 int camera_v4l2_set_batch_command(void *camera_handle, camera_batch_command_control_s *batch_command, int64_t *error_command)
2458 int ret = CAMERA_ERROR_NONE;
2460 int support_count = 0;
2461 hal_camera_handle *handle = (hal_camera_handle *)camera_handle;
2462 set_batch_table_s set_table[] = {
2463 {CAMERA_COMMAND_BRIGHTNESS, batch_command ? (void *)&batch_command->brightness : NULL},
2464 {CAMERA_COMMAND_CONTRAST, batch_command ? (void *)&batch_command->contrast : NULL},
2465 {CAMERA_COMMAND_SATURATION, batch_command ? (void *)&batch_command->saturation : NULL},
2466 {CAMERA_COMMAND_SHARPNESS, batch_command ? (void *)&batch_command->sharpness : NULL},
2467 {CAMERA_COMMAND_PTZ_TYPE, batch_command ? (void *)&batch_command->ptz_type : NULL},
2468 {CAMERA_COMMAND_PAN, batch_command ? (void *)&batch_command->pan : NULL},
2469 {CAMERA_COMMAND_TILT, batch_command ? (void *)&batch_command->tilt : NULL},
2470 {CAMERA_COMMAND_FLIP, batch_command ? (void *)&batch_command->flip : NULL},
2471 {CAMERA_COMMAND_CAPTURE_COUNT, batch_command ? (void *)&batch_command->capture_count : NULL},
2472 {CAMERA_COMMAND_CAPTURE_INTERVAL, batch_command ? (void *)&batch_command->capture_interval : NULL},
2473 {CAMERA_COMMAND_FOCUS_MODE, batch_command ? (void *)&batch_command->focus_mode : NULL},
2474 {CAMERA_COMMAND_FOCUS_RANGE, batch_command ? (void *)&batch_command->focus_range : NULL},
2475 {CAMERA_COMMAND_FOCUS_LEVEL, batch_command ? (void *)&batch_command->focus_level : NULL}
2478 if (!handle || !batch_command) {
2479 LOGE("NULL param %p %p", handle, batch_command);
2480 return CAMERA_ERROR_INVALID_PARAMETER;
2483 g_mutex_lock(&handle->lock);
2485 support_count = sizeof(set_table) / sizeof(set_batch_table_s);
2487 LOGD("set batch command - support count %d", support_count);
2489 __dump_batch_command(batch_command);
2491 for (i = 0 ; i < support_count ; i++) {
2492 if (!(batch_command->command_set_flag & set_table[i].command))
2495 ret = __set_command(handle, set_table[i].command, set_table[i].value);
2496 if (ret != CAMERA_ERROR_NONE) {
2497 LOGE("failed command %"PRIx64", ret 0x%x", set_table[i].command, ret);
2502 g_mutex_unlock(&handle->lock);
2508 static int camera_v4l2_backend_init(void **data)
2510 hal_backend_camera_funcs *funcs;
2512 funcs = calloc(1, sizeof(hal_backend_camera_funcs));
2514 return CAMERA_ERROR_OUT_OF_MEMORY;
2516 funcs->init = camera_v4l2_init;
2517 funcs->deinit = camera_v4l2_deinit;
2518 funcs->get_device_info_list = camera_v4l2_get_device_info_list;
2519 funcs->open_device = camera_v4l2_open_device;
2520 funcs->open_device_ext = camera_v4l2_open_device_ext;
2521 funcs->close_device = camera_v4l2_close_device;
2522 funcs->add_message_callback = camera_v4l2_add_message_callback;
2523 funcs->remove_message_callback = camera_v4l2_remove_message_callback;
2524 funcs->set_preview_stream_format = camera_v4l2_set_preview_stream_format;
2525 funcs->get_preview_stream_format = camera_v4l2_get_preview_stream_format;
2526 funcs->set_user_buffer_fd = camera_v4l2_set_user_buffer_fd;
2527 funcs->start_preview = camera_v4l2_start_preview;
2528 funcs->release_preview_buffer = camera_v4l2_release_preview_buffer;
2529 funcs->stop_preview = camera_v4l2_stop_preview;
2530 funcs->start_auto_focus = camera_v4l2_start_auto_focus;
2531 funcs->stop_auto_focus = camera_v4l2_stop_auto_focus;
2532 funcs->start_capture = camera_v4l2_start_capture;
2533 funcs->stop_capture = camera_v4l2_stop_capture;
2534 funcs->set_video_stream_format = camera_v4l2_set_video_stream_format;
2535 funcs->get_video_stream_format = camera_v4l2_get_video_stream_format;
2536 funcs->start_record = camera_v4l2_start_record;
2537 funcs->release_video_buffer = camera_v4l2_release_video_buffer;
2538 funcs->stop_record = camera_v4l2_stop_record;
2539 funcs->set_command = camera_v4l2_set_command;
2540 funcs->get_command = camera_v4l2_get_command;
2541 funcs->set_batch_command = camera_v4l2_set_batch_command;
2542 funcs->set_extra_preview_frame_cb = camera_v4l2_set_extra_preview_frame_cb;
2543 funcs->unset_extra_preview_frame_cb = camera_v4l2_unset_extra_preview_frame_cb;
2544 funcs->release_extra_preview_buffer = camera_v4l2_release_extra_preview_buffer;
2545 funcs->set_extra_preview_stream_format = camera_v4l2_set_extra_preview_stream_format;
2546 funcs->get_extra_preview_stream_format = camera_v4l2_get_extra_preview_stream_format;
2547 funcs->set_extra_preview_bitrate = camera_v4l2_set_extra_preview_bitrate;
2548 funcs->get_extra_preview_bitrate = camera_v4l2_get_extra_preview_bitrate;
2549 funcs->set_extra_preview_gop_interval = camera_v4l2_set_extra_preview_gop_interval;
2550 funcs->get_extra_preview_gop_interval = camera_v4l2_get_extra_preview_gop_interval;
2552 *data = (void *)funcs;
2558 static int camera_v4l2_backend_exit(void *data)
2569 hal_backend hal_backend_camera_data = {
2570 .name = "camera-v4l2",
2572 .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
2573 .init = camera_v4l2_backend_init,
2574 .exit = camera_v4l2_backend_exit,