From: Jeongmo Yang Date: Wed, 22 Jul 2020 00:37:08 +0000 (+0900) Subject: Revise code X-Git-Tag: accepted/tizen/6.0/unified/20201030.123220~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8906a575c14afae24c58bf6de6107c2d893d5819;p=platform%2Fadaptation%2Fcamera-hal-v4l2.git Revise code - Apply missed tizen coding rule - Add sub function for start/stop stream - Reduce code line to check invalid parameter - Remove unused code - Change the name of some preview related field in camera_hal_handle [Version] 0.0.11 [Profile] Common [Issue Type] Update Change-Id: Ic9991b248a22e52633a089c04d7388514933f42d Signed-off-by: Jeongmo Yang --- diff --git a/packaging/camera-hal-v4l2.spec b/packaging/camera-hal-v4l2.spec index d77166d..58bdca4 100644 --- a/packaging/camera-hal-v4l2.spec +++ b/packaging/camera-hal-v4l2.spec @@ -1,6 +1,6 @@ Name: camera-hal-v4l2 Summary: Tizen Camera Hal for V4L2 -Version: 0.0.10 +Version: 0.0.11 Release: 0 Group: Multimedia/Libraries License: Apache-2.0 diff --git a/src/tizen_camera_v4l2.c b/src/tizen_camera_v4l2.c index 6454cd6..2b4b06d 100644 --- a/src/tizen_camera_v4l2.c +++ b/src/tizen_camera_v4l2.c @@ -52,22 +52,23 @@ static camera_device_info_list_t *g_device_info_list; -static guint32 device_caps; +static guint32 g_device_caps; static GMutex g_device_info_lock; -static void _camera_hal_v4l2_destructor(void) __attribute__((destructor)); +static void __camera_hal_v4l2_destructor(void) __attribute__((destructor)); -static void _camera_hal_v4l2_destructor(void) +static void __camera_hal_v4l2_destructor(void) { LOGD("release device info list %p", g_device_info_list); g_free(g_device_info_list); + g_device_info_list = NULL; return; } -static int _camera_v4l2_wait_frame(int device_fd, int wait_time) +static int __camera_v4l2_wait_frame(int device_fd, int wait_time) { int ret = CAMERA_ERROR_NONE; fd_set fds; @@ -86,7 +87,7 @@ static int _camera_v4l2_wait_frame(int device_fd, int wait_time) timeout.tv_sec = wait_time; timeout.tv_usec = 0; - LOGD("select : %d sec", wait_time); + /*LOGD("select : %d sec", wait_time);*/ ret = select(device_fd + 1, &fds, NULL, NULL, &timeout); if (ret == -1) { @@ -103,13 +104,13 @@ static int _camera_v4l2_wait_frame(int device_fd, int wait_time) return CAMERA_ERROR_INTERNAL; } - LOGD("select done"); + /*LOGD("select done");*/ return CAMERA_ERROR_NONE; } -static int _camera_v4l2_g_ctrl(int device_fd, int cid, int *value) +static int __camera_v4l2_g_ctrl(int device_fd, int cid, int *value) { int ret = 0; struct v4l2_control ctrl; @@ -133,7 +134,7 @@ static int _camera_v4l2_g_ctrl(int device_fd, int cid, int *value) } -static int _camera_v4l2_s_ctrl(int device_fd, int cid, int value) +static int __camera_v4l2_s_ctrl(int device_fd, int cid, int value) { int ret = 0; struct v4l2_control ctrl; @@ -151,7 +152,7 @@ static int _camera_v4l2_s_ctrl(int device_fd, int cid, int value) } -static int _camera_v4l2_stream(int device_fd, int type, gboolean onoff) +static int __camera_v4l2_stream(int device_fd, int type, gboolean onoff) { if (device_fd < 0) { LOGE("invalid fd %d", device_fd); @@ -169,7 +170,7 @@ static int _camera_v4l2_stream(int device_fd, int type, gboolean onoff) } -static int _camera_v4l2_reqbufs(int device_fd, int type, int memory, int count, int *result_count) +static int __camera_v4l2_reqbufs(int device_fd, int type, int memory, uint32_t count, uint32_t *result_count) { struct v4l2_requestbuffers v4l2_reqbuf; @@ -203,7 +204,7 @@ static int _camera_v4l2_reqbufs(int device_fd, int type, int memory, int count, } -static int _camera_v4l2_qbuf(int device_fd, int type, int memory, int index) +static int __camera_v4l2_qbuf(int device_fd, int type, int memory, int index) { struct v4l2_buffer v4l2_buf; struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX]; @@ -229,14 +230,13 @@ static int _camera_v4l2_qbuf(int device_fd, int type, int memory, int index) return CAMERA_ERROR_INTERNAL; } - LOGD("QBUF done [i: %d, t: %d, m: %d]", - index, type, memory); + /*LOGD("QBUF done [i: %d, t: %d, m: %d]", index, type, memory);*/ return CAMERA_ERROR_NONE; } -static int _camera_v4l2_dqbuf(int device_fd, int type, int memory, int *index) +static int __camera_v4l2_dqbuf(int device_fd, int type, int memory, int *index) { int ret = CAMERA_ERROR_NONE; struct v4l2_buffer v4l2_buf; @@ -270,13 +270,13 @@ static int _camera_v4l2_dqbuf(int device_fd, int type, int memory, int *index) *index = v4l2_buf.index; - LOGD("dqbuf index %d", *index); + /*LOGD("dqbuf index %d", *index);*/ return CAMERA_ERROR_NONE; } -static int _camera_get_format(guint32 fourcc, int *pixel_format) +static int __camera_get_format(guint32 fourcc, int *pixel_format) { if (!pixel_format) { LOGE("NULL parameter"); @@ -322,7 +322,7 @@ static int _camera_get_format(guint32 fourcc, int *pixel_format) return CAMERA_ERROR_NONE; } -static int _camera_get_fourcc_plane_num(int pixel_format, guint32 *fourcc, uint8_t *plane_num) +static int __camera_get_fourcc_plane_num(int pixel_format, guint32 *fourcc, uint8_t *plane_num) { if (!fourcc || !plane_num) { LOGE("NULL parameter %p %p", fourcc, plane_num); @@ -374,7 +374,7 @@ static int _camera_get_fourcc_plane_num(int pixel_format, guint32 *fourcc, uint8 } -static int _camera_get_device_info(int device_index, int device_fd, camera_device_info_t *device_info, char *node_path) +static int __camera_get_device_info(int device_index, int device_fd, camera_device_info_t *device_info, char *node_path) { int i = 0; int j = 0; @@ -404,7 +404,7 @@ static int _camera_get_device_info(int device_index, int device_fd, camera_devic LOGD("\tformat[%d] "FOURCC_FORMAT, i, FOURCC_CONVERT(v4l2_format.pixelformat)); - if (_camera_get_format(v4l2_format.pixelformat, &camera_format) != CAMERA_ERROR_NONE) + if (__camera_get_format(v4l2_format.pixelformat, &camera_format) != CAMERA_ERROR_NONE) continue; device_info->format_list.formats[format_count] = camera_format; @@ -479,7 +479,7 @@ static int _camera_get_device_info(int device_index, int device_fd, camera_devic return CAMERA_ERROR_NONE; } -static int _camera_get_device_info_list(void) +static int __camera_get_device_info_list(void) { int i = 0; int ret = 0; @@ -548,18 +548,18 @@ static int _camera_get_device_info_list(void) } if (v4l2_cap.capabilities & V4L2_CAP_DEVICE_CAPS) - device_caps = v4l2_cap.device_caps; + g_device_caps = v4l2_cap.device_caps; else - device_caps = v4l2_cap.capabilities; + g_device_caps = v4l2_cap.capabilities; - if (!(device_caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) || - (device_caps & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE))) { - LOGW("[%s] is not a capture device 0x%x", glob_buf.gl_pathv[i], device_caps); + if (!(g_device_caps & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) || + (g_device_caps & (V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OUTPUT_MPLANE))) { + LOGW("[%s] is not a capture device 0x%x", glob_buf.gl_pathv[i], g_device_caps); close(device_fd); continue; } - ret = _camera_get_device_info(device_count, device_fd, + ret = __camera_get_device_info(device_count, device_fd, &device_info_list->device_info[device_count], glob_buf.gl_pathv[i]); close(device_fd); @@ -585,7 +585,142 @@ _GET_DEVICE_INFO_LIST_DONE: } -static void *_camera_preview_handler_func(gpointer data) +static int __camera_stop_stream(camera_hal_handle *handle, uint32_t buffer_count) +{ + int i = 0; + int ret = CAMERA_ERROR_NONE; + + if (!handle) { + LOGE("NULL handle"); + return CAMERA_ERROR_INVALID_PARAMETER; + } + + LOGD("buffer count[%d]", buffer_count); + + /* stream off */ + ret = __camera_v4l2_stream(handle->device_fd, handle->v4l2_type, FALSE); + + LOGD("stream off : 0x%x", ret); + + /* munmap */ + for (i = 0 ; i < buffer_count ; i++) { + if (handle->camera_buffers[i].planes[0].data != NULL) { + LOGW("munmap %p", handle->camera_buffers[i].planes[0].data); + + munmap(handle->camera_buffers[i].planes[0].data, handle->camera_buffers[i].planes[0].size); + + handle->camera_buffers[i].planes[0].data = 0; + handle->camera_buffers[i].planes[0].size = 0; + } else { + LOGW("NULL data [index %d]", i); + } + } + + /* reqbufs 0 */ + ret = __camera_v4l2_reqbufs(handle->device_fd, + handle->v4l2_type, V4L2_MEMORY_MMAP, 0, &buffer_count); + + LOGD("reqbufs 0 : 0x%x", ret); + + return ret; +} + + +static int __camera_start_stream(camera_hal_handle *handle, camera_format_t *format, + uint32_t request_buffer_count, uint32_t *result_buffer_count) +{ + int i = 0; + int ret = CAMERA_ERROR_NONE; + camera_buffer_t *buffer = NULL; + struct v4l2_buffer v4l2_buf; + struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX]; + + if (!handle || !format || !result_buffer_count) { + LOGE("NULL param %p %p", handle, result_buffer_count); + return CAMERA_ERROR_INTERNAL; + } + + /* request buffer */ + ret = __camera_v4l2_reqbufs(handle->device_fd, + handle->v4l2_type, V4L2_MEMORY_MMAP, request_buffer_count, result_buffer_count); + if (ret != CAMERA_ERROR_NONE) { + g_mutex_unlock(&handle->lock); + return ret; + } + + LOGD("buffer count : request %d -> result %d", + request_buffer_count, *result_buffer_count); + + /* query buffer, mmap and qbuf */ + for (i = 0 ; i < *result_buffer_count ; i++) { + memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer)); + memset(v4l2_planes, 0x0, sizeof(v4l2_planes)); + + v4l2_buf.type = handle->v4l2_type; + v4l2_buf.memory = V4L2_MEMORY_MMAP; + v4l2_buf.index = i; + v4l2_buf.m.planes = v4l2_planes; + v4l2_buf.length = handle->plane_num; + + LOGD("%d", i); + + if (ioctl(handle->device_fd, VIDIOC_QUERYBUF, &v4l2_buf) < 0) { + LOGE("[%d] query buf failed. errno %d", i, errno); + goto _START_STREAM_FAILED; + } + + LOGD("%d", i); + + buffer = &handle->camera_buffers[i]; + + LOGD("%d", i); + + buffer->index = i; + buffer->format = format->stream_format; + buffer->resolution = format->stream_resolution; + buffer->total_size = v4l2_buf.length; + buffer->num_planes = handle->plane_num; + buffer->planes[0].size = v4l2_buf.length; + buffer->planes[0].data = mmap(0, + v4l2_buf.length, + PROT_READ | PROT_WRITE, + MAP_SHARED, + handle->device_fd, + v4l2_buf.m.offset); + + LOGD("%d", i); + + if (buffer->planes[0].data == MAP_FAILED) { + LOGE("[%d] mmap failed (errno %d)", i, errno); + goto _START_STREAM_FAILED; + } + + LOGD("%d", i); + + if (__camera_v4l2_qbuf(handle->device_fd, handle->v4l2_type, V4L2_MEMORY_MMAP, i) != CAMERA_ERROR_NONE) { + LOGE("[%d] qbuf failed (errno %d)", i, errno); + goto _START_STREAM_FAILED; + } + + LOGD("%d", i); + } + + /* stream on */ + ret = __camera_v4l2_stream(handle->device_fd, handle->v4l2_type, TRUE); + if (ret != CAMERA_ERROR_NONE) { + LOGE("stream on failed"); + goto _START_STREAM_FAILED; + } + + return CAMERA_ERROR_NONE; + +_START_STREAM_FAILED: + __camera_stop_stream(handle, *result_buffer_count); + return ret; +} + + +static void *__camera_buffer_handler_func(gpointer data) { int index = 0; camera_hal_handle *handle = (camera_hal_handle *)data; @@ -600,10 +735,10 @@ static void *_camera_preview_handler_func(gpointer data) /* run buffer thread */ g_mutex_lock(&handle->preview_cb_lock); - while (handle->preview_cb_run) { + while (handle->buffer_thread_run) { g_mutex_unlock(&handle->preview_cb_lock); - if (_camera_v4l2_wait_frame(handle->device_fd, 5) != CAMERA_ERROR_NONE) { + if (__camera_v4l2_wait_frame(handle->device_fd, 5) != CAMERA_ERROR_NONE) { LOGE("frame wait failed"); g_mutex_lock(&handle->preview_cb_lock); break; @@ -611,24 +746,24 @@ static void *_camera_preview_handler_func(gpointer data) g_mutex_lock(&handle->preview_cb_lock); - if (handle->preview_cb_run == FALSE) { + if (handle->buffer_thread_run == FALSE) { LOGW("stop preview handler thread"); break; } - if (_camera_v4l2_dqbuf(handle->device_fd, handle->v4l2_type, V4L2_MEMORY_MMAP, &index) != CAMERA_ERROR_NONE) { + if (__camera_v4l2_dqbuf(handle->device_fd, handle->v4l2_type, V4L2_MEMORY_MMAP, &index) != CAMERA_ERROR_NONE) { LOGE("dqbuf failed"); break; } handle->live_buffer_num++; - LOGD("live buffer num %d", handle->live_buffer_num); + /*LOGD("live buffer num %d", handle->live_buffer_num);*/ g_mutex_unlock(&handle->preview_cb_lock); if (handle->preview_cb) { - ((camera_preview_frame_cb)handle->preview_cb)(&handle->preview_buffer[index], NULL, handle->preview_cb_data); + ((camera_preview_frame_cb)handle->preview_cb)(&handle->camera_buffers[index], NULL, handle->preview_cb_data); } else { LOGW("preview callback is NULL"); camera_release_preview_buffer((void *)handle, index); @@ -647,7 +782,7 @@ static void *_camera_preview_handler_func(gpointer data) } -static void _camera_message_release_func(gpointer data) +static void __camera_message_release_func(gpointer data) { camera_message_t *message = (camera_message_t *)data; @@ -720,7 +855,7 @@ static void *_camera_message_handler_func(gpointer data) } -void _camera_release_handle(camera_hal_handle *handle) +static void __camera_release_handle(camera_hal_handle *handle) { if (!handle) { LOGW("NULL handle"); @@ -733,7 +868,7 @@ void _camera_release_handle(camera_hal_handle *handle) g_cond_signal(&handle->message_cb_cond); g_mutex_unlock(&handle->message_cb_lock); g_thread_join(handle->message_thread); - g_queue_free_full(handle->message_list, (GDestroyNotify)_camera_message_release_func); + g_queue_free_full(handle->message_list, (GDestroyNotify)__camera_message_release_func); handle->message_list = NULL; } @@ -805,7 +940,7 @@ int camera_init(void **camera_handle) new_handle->device_fd = CAMERA_HAL_INITIAL_FD; new_handle->state = CAMERA_STATE_INITIALIZED; - ret = _camera_get_device_info_list(); + ret = __camera_get_device_info_list(); if (ret != CAMERA_ERROR_NONE) { LOGE("get device info failed"); goto _INIT_ERROR; @@ -818,22 +953,20 @@ int camera_init(void **camera_handle) return CAMERA_ERROR_NONE; _INIT_ERROR: - _camera_release_handle(new_handle); + __camera_release_handle(new_handle); return ret; } int camera_deinit(void *camera_handle) { - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { + if (!handle) { LOGE("NULL handle"); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); if (handle->state != CAMERA_STATE_INITIALIZED) { @@ -844,7 +977,7 @@ int camera_deinit(void *camera_handle) g_mutex_unlock(&handle->lock); - _camera_release_handle(handle); + __camera_release_handle(handle); return CAMERA_ERROR_NONE; } @@ -858,7 +991,7 @@ int camera_get_device_info_list(camera_device_info_list_t *device_info_list) return CAMERA_ERROR_INVALID_PARAMETER; } - ret = _camera_get_device_info_list(); + ret = __camera_get_device_info_list(); if (ret != CAMERA_ERROR_NONE) { LOGE("get device info failed"); return ret; @@ -874,15 +1007,13 @@ int camera_open_device(void *camera_handle, int device_index) int ret = CAMERA_ERROR_NONE; int device_fd = CAMERA_HAL_INITIAL_FD; char *node_path = NULL; - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { + if (!handle) { LOGE("NULL handle"); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); if (handle->state != CAMERA_STATE_INITIALIZED) { @@ -929,7 +1060,7 @@ int camera_open_device(void *camera_handle, int device_index) goto _OPEN_DEVICE_DONE; } - if (device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE) + if (g_device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE) handle->v4l2_type = V4L2_CAP_VIDEO_CAPTURE_MPLANE; else handle->v4l2_type = V4L2_CAP_VIDEO_CAPTURE; @@ -949,15 +1080,13 @@ _OPEN_DEVICE_DONE: int camera_close_device(void *camera_handle) { - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { + if (!handle) { LOGE("NULL handle"); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); if (handle->state != CAMERA_STATE_OPENED) { @@ -987,9 +1116,9 @@ int camera_close_device(void *camera_handle) int camera_add_message_callback(void *camera_handle, camera_message_cb callback, void *user_data, uint32_t *cb_id) { uint32_t i = 0; - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { + if (!handle) { LOGE("NULL handle"); return CAMERA_ERROR_INVALID_PARAMETER; } @@ -999,8 +1128,6 @@ int camera_add_message_callback(void *camera_handle, camera_message_cb callback, return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); if (handle->state != CAMERA_STATE_OPENED) { @@ -1029,9 +1156,9 @@ int camera_add_message_callback(void *camera_handle, camera_message_cb callback, int camera_remove_message_callback(void *camera_handle, uint32_t cb_id) { - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { + if (!handle) { LOGE("NULL handle"); return CAMERA_ERROR_INVALID_PARAMETER; } @@ -1041,8 +1168,6 @@ int camera_remove_message_callback(void *camera_handle, uint32_t cb_id) return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); if (handle->state != CAMERA_STATE_OPENED) { @@ -1075,18 +1200,13 @@ int camera_set_preview_stream_format(void *camera_handle, camera_format_t *forma int ret = CAMERA_ERROR_NONE; gboolean capability_check = FALSE; guint32 fourcc = 0; - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; camera_device_info_t *device_info = NULL; struct v4l2_format v4l2_fmt; struct v4l2_streamparm v4l2_parm; - if (!camera_handle) { - LOGE("NULL handle"); - return CAMERA_ERROR_INVALID_PARAMETER; - } - - if (!format) { - LOGE("NULL format"); + if (!handle || !format) { + LOGE("NULL param %p %p", handle, format); return CAMERA_ERROR_INVALID_PARAMETER; } @@ -1095,8 +1215,6 @@ int camera_set_preview_stream_format(void *camera_handle, camera_format_t *forma return CAMERA_ERROR_INTERNAL; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); if (handle->state != CAMERA_STATE_OPENED) { @@ -1139,7 +1257,7 @@ int camera_set_preview_stream_format(void *camera_handle, camera_format_t *forma } /* S_FMT */ - ret = _camera_get_fourcc_plane_num(format->stream_format, &fourcc, &handle->plane_num); + ret = __camera_get_fourcc_plane_num(format->stream_format, &fourcc, &handle->plane_num); if (ret != CAMERA_ERROR_NONE) { LOGE("get fourcc failed [format %d]", format->stream_format); g_mutex_unlock(&handle->lock); @@ -1220,20 +1338,13 @@ int camera_set_preview_stream_format(void *camera_handle, camera_format_t *forma int camera_get_preview_stream_format(void *camera_handle, camera_format_t *format) { - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { - LOGE("NULL handle"); + if (!handle || !format) { + LOGE("NULL param %p %p", handle, format); return CAMERA_ERROR_INVALID_PARAMETER; } - if (!format) { - LOGE("NULL format"); - return CAMERA_ERROR_INVALID_PARAMETER; - } - - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); memcpy(format, &handle->preview_format, sizeof(camera_format_t)); @@ -1249,27 +1360,14 @@ int camera_get_preview_stream_format(void *camera_handle, camera_format_t *forma int camera_start_preview(void *camera_handle, camera_preview_frame_cb callback, void *user_data) { - int i = 0; int ret = 0; - int result_count = 0; - camera_hal_handle *handle = NULL; - camera_buffer_t *buffer = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - struct v4l2_buffer v4l2_buf; - struct v4l2_plane v4l2_planes[V4L2_PLANES_MAX]; - - if (!camera_handle) { - LOGE("NULL handle"); - return CAMERA_ERROR_INVALID_PARAMETER; - } - - if (!callback) { - LOGE("NULL callback for preview"); + if (!handle || !callback) { + LOGE("NULL param %p %p", handle, callback); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); if (handle->state != CAMERA_STATE_OPENED) { @@ -1278,78 +1376,28 @@ int camera_start_preview(void *camera_handle, camera_preview_frame_cb callback, return CAMERA_ERROR_INVALID_STATE; } - /* request buffer */ - ret = _camera_v4l2_reqbufs(handle->device_fd, - handle->v4l2_type, V4L2_MEMORY_MMAP, PREVIEW_BUFFER_MAX, &result_count); + ret = __camera_start_stream(handle, &handle->preview_format, BUFFER_MAX, &handle->buffer_count); if (ret != CAMERA_ERROR_NONE) { + LOGE("__camera_start_stream failed[0x%x]", ret); g_mutex_unlock(&handle->lock); return ret; } - LOGD("REQUESTED buffer count %d", result_count); - - handle->preview_buffer_num = result_count; - - /* query buffer, mmap and qbuf */ - for (i = 0 ; i < handle->preview_buffer_num ; i++) { - memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer)); - memset(v4l2_planes, 0x0, sizeof(v4l2_planes)); - - v4l2_buf.type = handle->v4l2_type; - v4l2_buf.memory = V4L2_MEMORY_MMAP; - v4l2_buf.index = i; - v4l2_buf.m.planes = v4l2_planes; - v4l2_buf.length = handle->plane_num; - - if (ioctl(handle->device_fd, VIDIOC_QUERYBUF, &v4l2_buf) < 0) { - LOGE("[%d] query buf failed. errno %d", i, errno); - goto _START_PREVIEW_FAILED; - } - - buffer = &handle->preview_buffer[i]; - - buffer->index = i; - buffer->format = handle->preview_format.stream_format; - buffer->resolution = handle->preview_format.stream_resolution; - buffer->total_size = v4l2_buf.length; - buffer->num_planes = handle->plane_num; - buffer->planes[0].size = v4l2_buf.length; - buffer->planes[0].data = mmap(0, - v4l2_buf.length, - PROT_READ | PROT_WRITE, - MAP_SHARED, - handle->device_fd, - v4l2_buf.m.offset); - if (buffer->planes[0].data == MAP_FAILED) { - LOGE("[%d] mmap failed. errno %d", i, errno); - goto _START_PREVIEW_FAILED; - } - } + g_mutex_lock(&handle->preview_cb_lock); - for (i = 0 ; i < handle->preview_buffer_num ; i++) { - if (_camera_v4l2_qbuf(handle->device_fd, handle->v4l2_type, V4L2_MEMORY_MMAP, i) != CAMERA_ERROR_NONE) { - LOGE("qbuf failed"); - goto _START_PREVIEW_FAILED; - } - } + handle->buffer_thread_run = TRUE; - /* stream on */ - ret = _camera_v4l2_stream(handle->device_fd, handle->v4l2_type, TRUE); - if (ret != CAMERA_ERROR_NONE) { - LOGE("stream on failed"); - goto _START_PREVIEW_FAILED; - } + handle->buffer_thread = g_thread_try_new("camera_hal_buffer_thread", + __camera_buffer_handler_func, (gpointer)handle, NULL); + if (!handle->buffer_thread) { + LOGE("failed to create buffer handler thread"); + g_mutex_unlock(&handle->preview_cb_lock); - g_mutex_lock(&handle->preview_cb_lock); + __camera_stop_stream(handle, handle->buffer_count); - handle->preview_cb_run = TRUE; + g_mutex_unlock(&handle->lock); - handle->preview_thread = g_thread_try_new("camera_hal_preview_thread", - _camera_preview_handler_func, (gpointer)handle, NULL); - if (!handle->preview_thread) { - LOGE("failed to create preview callback thread"); - g_mutex_unlock(&handle->preview_cb_lock); - goto _START_PREVIEW_FAILED; + return ret; } handle->preview_cb = callback; @@ -1364,51 +1412,24 @@ int camera_start_preview(void *camera_handle, camera_preview_frame_cb callback, g_mutex_unlock(&handle->lock); return CAMERA_ERROR_NONE; - -_START_PREVIEW_FAILED: - /* stream off */ - if (ioctl(handle->device_fd, VIDIOC_STREAMOFF, &handle->v4l2_type) < 0) - LOGE("stream off failed. errno %d", errno); - - /* munmap */ - for (i = 0 ; i < handle->preview_buffer_num ; i++) { - buffer = &handle->preview_buffer[i]; - if (buffer->planes[0].data != NULL && - buffer->planes[0].data != MAP_FAILED) { - LOGW("munmap %p", buffer->planes[0].data); - munmap(buffer->planes[0].data, buffer->planes[0].size); - } - memset(buffer, 0x0, sizeof(camera_buffer_t)); - } - - /* reqbufs 0 */ - if (_camera_v4l2_reqbufs(handle->device_fd, - handle->v4l2_type, V4L2_MEMORY_MMAP, 0, &result_count) != CAMERA_ERROR_NONE) - LOGE("reqbufs 0 failed"); - - g_mutex_unlock(&handle->lock); - - return ret; } int camera_release_preview_buffer(void *camera_handle, int buffer_index) { int ret = CAMERA_ERROR_NONE; - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { + if (!handle) { LOGE("NULL handle"); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - - if (buffer_index >= handle->preview_buffer_num) { + if (buffer_index >= handle->buffer_count) { LOGE("invalid buffer index %d", buffer_index); return CAMERA_ERROR_INVALID_PARAMETER; } - ret = _camera_v4l2_qbuf(handle->device_fd, + ret = __camera_v4l2_qbuf(handle->device_fd, handle->v4l2_type, V4L2_MEMORY_MMAP, buffer_index); g_mutex_lock(&handle->preview_cb_lock); @@ -1428,21 +1449,18 @@ int camera_release_preview_buffer(void *camera_handle, int buffer_index) return ret; } + int camera_stop_preview(void *camera_handle) { int ret = CAMERA_ERROR_NONE; - int i = 0; - int result_count = 0; gint64 end_time; - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { + if (!handle) { LOGE("NULL handle"); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - LOGD("start"); g_mutex_lock(&handle->lock); @@ -1455,7 +1473,7 @@ int camera_stop_preview(void *camera_handle) g_mutex_lock(&handle->preview_cb_lock); - handle->preview_cb_run = FALSE; + handle->buffer_thread_run = FALSE; while (handle->live_buffer_num > 0) { LOGD("wait for live buffer [num %d]", handle->live_buffer_num); @@ -1470,38 +1488,15 @@ int camera_stop_preview(void *camera_handle) g_mutex_unlock(&handle->preview_cb_lock); - /* stream off */ - ret = _camera_v4l2_stream(handle->device_fd, handle->v4l2_type, FALSE); - - LOGD("stream off : 0x%x", ret); - - /* munmap */ - for (i = 0 ; i < handle->preview_buffer_num ; i++) { - if (handle->preview_buffer[i].planes[0].data != NULL) { - LOGW("munmap %p", handle->preview_buffer[i].planes[0].data); - - munmap(handle->preview_buffer[i].planes[0].data, handle->preview_buffer[i].planes[0].size); - - handle->preview_buffer[i].planes[0].data = 0; - handle->preview_buffer[i].planes[0].size = 0; - } else { - LOGW("NULL data [index %d]", i); - } - } - - /* reqbufs 0 */ - ret = _camera_v4l2_reqbufs(handle->device_fd, - handle->v4l2_type, V4L2_MEMORY_MMAP, 0, &result_count); - - LOGD("reqbufs 0 : 0x%x", ret); + ret = __camera_stop_stream(handle, handle->buffer_count); /* wait for preview thread exit */ - g_thread_join(handle->preview_thread); - handle->preview_thread = NULL; + g_thread_join(handle->buffer_thread); + handle->buffer_thread = NULL; handle->state = CAMERA_STATE_OPENED; - LOGD("stop preview done"); + LOGD("stop preview done [0x%x]", ret); g_mutex_unlock(&handle->lock); @@ -1515,7 +1510,7 @@ int camera_start_auto_focus(void *camera_handle) return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* auto focus is not supported */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1528,7 +1523,7 @@ int camera_stop_auto_focus(void *camera_handle) return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* auto focus is not supported */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1538,11 +1533,11 @@ int camera_stop_auto_focus(void *camera_handle) int camera_start_capture(void *camera_handle, camera_capture_cb callback, void *user_data) { if (!camera_handle || !callback) { - LOGE("NULL handle %p or callback %p", camera_handle, callback); + LOGE("NULL param %p %p", camera_handle, callback); return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* capture function is not supported */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1555,7 +1550,7 @@ int camera_stop_capture(void *camera_handle) return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* capture function is not supported */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1563,17 +1558,12 @@ int camera_stop_capture(void *camera_handle) int camera_set_video_stream_format(void *camera_handle, camera_format_t *format) { - if (!camera_handle) { - LOGE("NULL handle"); + if (!camera_handle || !format) { + LOGE("NULL param %p %p", camera_handle, format); return CAMERA_ERROR_INVALID_PARAMETER; } - if (!format) { - LOGE("NULL format"); - return CAMERA_ERROR_INVALID_PARAMETER; - } - - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* single stream device can not support video stream */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1581,17 +1571,12 @@ int camera_set_video_stream_format(void *camera_handle, camera_format_t *format) int camera_get_video_stream_format(void *camera_handle, camera_format_t *format) { - if (!camera_handle) { - LOGE("NULL handle"); - return CAMERA_ERROR_INVALID_PARAMETER; - } - - if (!format) { - LOGE("NULL format"); + if (!camera_handle || !format) { + LOGE("NULL param %p %p", camera_handle, format); return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* single stream device can not support video stream */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1599,17 +1584,12 @@ int camera_get_video_stream_format(void *camera_handle, camera_format_t *format) int camera_start_record(void *camera_handle, camera_video_frame_cb callback, void *user_data) { - if (!camera_handle) { - LOGE("NULL handle"); - return CAMERA_ERROR_INVALID_PARAMETER; - } - - if (!callback) { - LOGE("NULL callback for video"); + if (!camera_handle || !callback) { + LOGE("NULL param %p %p", camera_handle, callback); return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* single stream device can not support video stream */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1622,7 +1602,7 @@ int camera_release_video_buffer(void *camera_handle, int buffer_index) return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* single stream device can not support video stream */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1635,7 +1615,7 @@ int camera_stop_record(void *camera_handle) return CAMERA_ERROR_INVALID_PARAMETER; } - LOGD("NOT SUPPORTED"); + LOGE("NOT SUPPORTED"); /* single stream device can not support video stream */ return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; @@ -1647,19 +1627,13 @@ int camera_set_command(void *camera_handle, int64_t command, void *value) int cid = 0; int ctrl_ret = 0; int set_value = 0; - camera_hal_handle *handle = NULL; - - if (!camera_handle) { - LOGE("NULL handle"); - return CAMERA_ERROR_INVALID_PARAMETER; - } + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!value) { - LOGE("invalid pointer : value"); + if (!handle || !value) { + LOGE("NULL param %p %p", handle, value); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; set_value = *(int *)value; g_mutex_lock(&handle->lock); @@ -1700,13 +1674,22 @@ int camera_set_command(void *camera_handle, int64_t command, void *value) case CAMERA_COMMAND_TILT: cid = V4L2_CID_TILT_ABSOLUTE; break; + case CAMERA_COMMAND_FLIP: + g_mutex_unlock(&handle->lock); + if (set_value != CAMERA_FLIP_NONE) { + LOGE("NOT_SUPPORTED flip %d", set_value); + return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; + } else { + return CAMERA_ERROR_NONE; + } + break; default: LOGE("NOT_SUPPORTED %"PRId64, command); g_mutex_unlock(&handle->lock); return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; } - ctrl_ret = _camera_v4l2_s_ctrl(handle->device_fd, cid, set_value); + ctrl_ret = __camera_v4l2_s_ctrl(handle->device_fd, cid, set_value); if (ctrl_ret < 0) { switch (errno) { case EACCES: @@ -1743,20 +1726,13 @@ int camera_get_command(void *camera_handle, int64_t command, void **value) int ret = CAMERA_ERROR_NONE; int cid = 0; int ctrl_ret = 0; - camera_hal_handle *handle = NULL; - - if (!camera_handle) { - LOGE("NULL handle"); - return CAMERA_ERROR_INVALID_PARAMETER; - } + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!value) { - LOGE("invalid pointer for value"); + if (!handle || !value) { + LOGE("NULL param %p %p", handle, value); return CAMERA_ERROR_INVALID_PARAMETER; } - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); LOGD("get command %"PRId64" - state %d", command, handle->state); @@ -1780,7 +1756,7 @@ int camera_get_command(void *camera_handle, int64_t command, void **value) return CAMERA_ERROR_DEVICE_NOT_SUPPORTED; } - ctrl_ret = _camera_v4l2_g_ctrl(handle->device_fd, cid, (int *)value); + ctrl_ret = __camera_v4l2_g_ctrl(handle->device_fd, cid, (int *)value); if (ctrl_ret < 0) { switch (errno) { case EACCES: @@ -1814,20 +1790,13 @@ int camera_get_command(void *camera_handle, int64_t command, void **value) int camera_set_batch_command(void *camera_handle, camera_batch_command_control_t *batch_command, int64_t *error_command) { - camera_hal_handle *handle = NULL; + camera_hal_handle *handle = (camera_hal_handle *)camera_handle; - if (!camera_handle) { - LOGE("NULL handle"); + if (!handle || !batch_command) { + LOGE("NULL param %p %p", handle, batch_command); return CAMERA_ERROR_INVALID_PARAMETER; } - if (!batch_command) { - LOGE("invalid pointer for batch_command"); - return CAMERA_ERROR_INVALID_PARAMETER; - } - - handle = (camera_hal_handle *)camera_handle; - g_mutex_lock(&handle->lock); LOGD("set batch command - flag %"PRIx64", state %d", diff --git a/src/tizen_camera_v4l2_private.h b/src/tizen_camera_v4l2_private.h index e70ceac..2e4996f 100644 --- a/src/tizen_camera_v4l2_private.h +++ b/src/tizen_camera_v4l2_private.h @@ -28,8 +28,7 @@ #define CAMERA_HAL_INITIAL_INDEX -1 #define CAMERA_HAL_INITIAL_FD -1 #define MESSAGE_CALLBACK_MAX 10 -#define PREVIEW_BUFFER_MAX 4 -#define VIDEO_BUFFER_MAX 8 +#define BUFFER_MAX 4 #define V4L2_PLANES_MAX 4 typedef struct _camera_hal_handle { @@ -47,22 +46,17 @@ typedef struct _camera_hal_handle { int live_buffer_num; /* thread */ - GThread *preview_thread; + GThread *buffer_thread; GThread *message_thread; + gboolean buffer_thread_run; - /* preview callback */ + /* buffer handler */ camera_preview_frame_cb preview_cb; + camera_capture_cb capture_cb; void *preview_cb_data; - gboolean preview_cb_run; - uint32_t preview_buffer_num; - camera_buffer_t preview_buffer[PREVIEW_BUFFER_MAX]; - - /* video callback */ - camera_video_frame_cb video_cb; - void *video_cb_data; - gboolean video_cb_run; - uint32_t video_buffer_num; - camera_buffer_t video_buffer[VIDEO_BUFFER_MAX]; + void *capture_cb_data; + uint32_t buffer_count; + camera_buffer_t camera_buffers[BUFFER_MAX]; /* message callback */ camera_message_cb message_cb[MESSAGE_CALLBACK_MAX];