Fix svace, coverity, lint accepted/tizen_7.0_unified tizen_7.0 accepted/tizen/7.0/unified/20231102.181244
authorKwanghoon Son <k.son@samsung.com>
Thu, 2 Nov 2023 05:55:35 +0000 (14:55 +0900)
committerKwanghoon Son <k.son@samsung.com>
Thu, 2 Nov 2023 05:58:59 +0000 (14:58 +0900)
[Version] 1.0.1

Change-Id: I63d1f9b785c62705409f66993b99ab87fe1804a1
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
execute_format.sh [new file with mode: 0755]
packaging/vision-source-v4l2.spec
src/vision_source_v4l2.cpp
test/test_vision_source_v4l2.cpp

diff --git a/execute_format.sh b/execute_format.sh
new file mode 100755 (executable)
index 0000000..bd0fd4b
--- /dev/null
@@ -0,0 +1 @@
+find . -iname *.h -o -iname *.hpp -o -iname *.cpp -o -iname *.c | xargs clang-format-9 -i
\ No newline at end of file
index 2ecbb7a..702e777 100644 (file)
@@ -3,7 +3,7 @@
 
 Name:        vision-source-v4l2
 Summary:     vision source-v4l2
-Version:     1.0.0
+Version:     1.0.1
 Release:     0
 Group:       Multimedia/Framework
 License:     Apache-2.0
index 606f4a0..8daf6c2 100644 (file)
@@ -62,8 +62,9 @@
                }                                                      \
        } while (0)
 
-#define VISION_SOURCE_NULL_ARG_CHECK(arg)                                             \
-       VISION_SOURCE_CHECK_CONDITION(arg != NULL, VISION_SOURCE_ERROR_INVALID_PARAMETER, \
+#define VISION_SOURCE_NULL_ARG_CHECK(arg)                                \
+       VISION_SOURCE_CHECK_CONDITION(arg != NULL,                           \
+                                                                 VISION_SOURCE_ERROR_INVALID_PARAMETER, \
                                                                  "VISION_SOURCE_ERROR_INVALID_PARAMETER")
 
 #define VISION_SOURCE_INITIAL_FD -1
@@ -71,7 +72,9 @@
 
 #define DEVICE_NODE_PATH_PREFIX "/dev/video"
 #define FOURCC_FORMAT "%c%c%c%c"
-#define FOURCC_CONVERT(fourcc) fourcc & 0xff, (fourcc >> 8) & 0xff, (fourcc >> 16) & 0xff, (fourcc >> 24) & 0xff
+#define FOURCC_CONVERT(fourcc)                                  \
+       fourcc & 0xff, (fourcc >> 8) & 0xff, (fourcc >> 16) & 0xff, \
+                       (fourcc >> 24) & 0xff
 
 #ifdef USE_DL
 #define vision_source_init vision_source_v4l2_init
@@ -90,7 +93,8 @@
 
 using namespace std;
 
-struct fmt_info {
+struct fmt_info
+{
        media_format_mimetype_e type;
        uint32_t width;
        uint32_t height;
@@ -98,16 +102,19 @@ struct fmt_info {
 
        bool operator==(const fmt_info &rhs)
        {
-               return (type == rhs.type) && (width == rhs.width) && (height == rhs.height) && (fps == rhs.fps);
+               return (type == rhs.type) && (width == rhs.width) &&
+                          (height == rhs.height) && (fps == rhs.fps);
        }
 };
 
-struct pkt_dispose_cb_data {
+struct pkt_dispose_cb_data
+{
        int fd;
        int index;
 };
 
-struct vision_source_v4l2_s {
+struct vision_source_v4l2_s
+{
        /* device */
        vision_source_device_info_s *dev_info {};
        media_format_h **fmt {};
@@ -152,12 +159,15 @@ static int __vision_source_get_format(uint32_t fourcc, int *pixel_format)
                return VISION_SOURCE_ERROR_NOT_SUPPORTED_FORMAT;
        }
 
-       LOGD("fourcc " FOURCC_FORMAT " -> %d", FOURCC_CONVERT(fourcc), *pixel_format);
+       LOGD("fourcc " FOURCC_FORMAT " -> %d", FOURCC_CONVERT(fourcc),
+                *pixel_format);
 
        return VISION_SOURCE_ERROR_NONE;
 }
 
-static int __vision_source_get_fourcc_plane_num(media_format_mimetype_e pixel_format, uint32_t *fourcc)
+static int
+__vision_source_get_fourcc_plane_num(media_format_mimetype_e pixel_format,
+                                                                        uint32_t *fourcc)
 {
        switch (pixel_format) {
        case MEDIA_FORMAT_RGB888:
@@ -177,12 +187,15 @@ static int __vision_source_get_fourcc_plane_num(media_format_mimetype_e pixel_fo
                return VISION_SOURCE_ERROR_INTERNAL;
        }
 
-       LOGD("format %d -> fourcc " FOURCC_FORMAT, pixel_format, FOURCC_CONVERT(*fourcc));
+       LOGD("format %d -> fourcc " FOURCC_FORMAT, pixel_format,
+                FOURCC_CONVERT(*fourcc));
 
        return VISION_SOURCE_ERROR_NONE;
 }
 
-static vector<uint32_t> __vision_source_get_fps_list(int device_fd, uint32_t pixel_format, int width, int height)
+static vector<uint32_t> __vision_source_get_fps_list(int device_fd,
+                                                                                                        uint32_t pixel_format,
+                                                                                                        int width, int height)
 {
        vector<uint32_t> fps_list;
        struct v4l2_frmivalenum ival;
@@ -203,7 +216,8 @@ static vector<uint32_t> __vision_source_get_fps_list(int device_fd, uint32_t pix
        }
 
        do {
-               LOGI("\t\t\t\tFramerate[%u/%u]", ival.discrete.denominator, ival.discrete.numerator);
+               LOGI("\t\t\t\tFramerate[%u/%u]", ival.discrete.denominator,
+                        ival.discrete.numerator);
                fps_list.push_back(ival.discrete.denominator);
                ival.index++;
        } while (v4l2_ioctl(device_fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
@@ -211,7 +225,8 @@ static vector<uint32_t> __vision_source_get_fps_list(int device_fd, uint32_t pix
        return fps_list;
 }
 
-static int __vision_source_v4l2_qbuf(int device_fd, int type, int memory, int index)
+static int __vision_source_v4l2_qbuf(int device_fd, int type, int memory,
+                                                                        int index)
 {
        struct v4l2_buffer v4l2_buf;
 
@@ -222,7 +237,8 @@ static int __vision_source_v4l2_qbuf(int device_fd, int type, int memory, int in
        v4l2_buf.memory = memory;
 
        if (v4l2_ioctl(device_fd, VIDIOC_QBUF, &v4l2_buf) < 0) {
-               LOGE("qbuf failed.  [i: %d, t: %d, m: %d] errno %d", index, type, memory, errno);
+               LOGE("qbuf failed.  [i: %d, t: %d, m: %d] errno %d", index, type,
+                        memory, errno);
                return VISION_SOURCE_ERROR_INTERNAL;
        }
 
@@ -231,7 +247,8 @@ static int __vision_source_v4l2_qbuf(int device_fd, int type, int memory, int in
        return VISION_SOURCE_ERROR_NONE;
 }
 
-static int __vision_source_get_valid_fmt(int device_fd, vector<fmt_info> &found_fmt)
+static int __vision_source_get_valid_fmt(int device_fd,
+                                                                                vector<fmt_info> &found_fmt)
 {
        int pixel_format = 0;
        int pixel_index = 0;
@@ -249,10 +266,13 @@ static int __vision_source_get_valid_fmt(int device_fd, vector<fmt_info> &found_
                        break;
                }
 
-               LOGD("\tformat[%d] " FOURCC_FORMAT " (emulated:%d)", pixel_index, FOURCC_CONVERT(v4l2_format.pixelformat),
+               LOGD("\tformat[%d] " FOURCC_FORMAT " (emulated:%d)", pixel_index,
+                        FOURCC_CONVERT(v4l2_format.pixelformat),
                         ((v4l2_format.flags & V4L2_FMT_FLAG_EMULATED) ? 1 : 0));
 
-               if (__vision_source_get_format(v4l2_format.pixelformat, &pixel_format) != VISION_SOURCE_ERROR_NONE) {
+               if (__vision_source_get_format(v4l2_format.pixelformat,
+                                                                          &pixel_format) !=
+                       VISION_SOURCE_ERROR_NONE) {
                        pixel_index++;
                        continue;
                }
@@ -267,7 +287,8 @@ static int __vision_source_get_valid_fmt(int device_fd, vector<fmt_info> &found_
                        v4l2_frame.index = resolution_index;
                        v4l2_frame.pixel_format = v4l2_format.pixelformat;
 
-                       if (v4l2_ioctl(device_fd, VIDIOC_ENUM_FRAMESIZES, &v4l2_frame) < 0) {
+                       if (v4l2_ioctl(device_fd, VIDIOC_ENUM_FRAMESIZES, &v4l2_frame) <
+                               0) {
                                LOGW("\t\tframe : end of enumeration ");
                                break;
                        }
@@ -283,7 +304,8 @@ static int __vision_source_get_valid_fmt(int device_fd, vector<fmt_info> &found_
 
                        LOGD("\t\tsize[%d] %ux%u", resolution_index, width, height);
 
-                       auto fps_list = __vision_source_get_fps_list(device_fd, v4l2_frame.pixel_format, width, height);
+                       auto fps_list = __vision_source_get_fps_list(
+                                       device_fd, v4l2_frame.pixel_format, width, height);
                        if (fps_list.empty()) {
                                return VISION_SOURCE_ERROR_NOT_SUPPORTED_FORMAT;
                        }
@@ -291,7 +313,8 @@ static int __vision_source_get_valid_fmt(int device_fd, vector<fmt_info> &found_
                        resolution_index++;
 
                        for (auto fps : fps_list) {
-                               fmt_info fmt { (media_format_mimetype_e) pixel_format, width, height, fps };
+                               fmt_info fmt { (media_format_mimetype_e) pixel_format, width,
+                                                          height, fps };
                                found_fmt.push_back(fmt);
                        }
                }
@@ -342,7 +365,8 @@ static int __dev_info_from_path(const char *path, vector<fmt_info> &fmt_list)
        else
                dev_caps = v4l2_cap.capabilities;
 
-       if (!(dev_caps & V4L2_CAP_VIDEO_CAPTURE) || (dev_caps & V4L2_CAP_VIDEO_OUTPUT)) {
+       if (!(dev_caps & V4L2_CAP_VIDEO_CAPTURE) ||
+               (dev_caps & V4L2_CAP_VIDEO_OUTPUT)) {
                LOGW("[%s] is not a capture device 0x%x", path, dev_caps);
                v4l2_close(device_fd);
                return ret;
@@ -397,20 +421,25 @@ static int __vision_source_list_devices(vision_source_v4l2_s *vs_handle)
 
        size_t dev_count = dev_name.size();
 
-       vs_handle->dev_info = (vision_source_device_info_s *) calloc(dev_count, sizeof(vision_source_device_info_s));
+       vs_handle->dev_info = (vision_source_device_info_s *) calloc(
+                       dev_count, sizeof(vision_source_device_info_s));
        if (!vs_handle->dev_info) {
                return VISION_SOURCE_ERROR_OUT_OF_MEMORY;
        }
 
-       vs_handle->fmt = (media_format_h **) calloc(dev_count, sizeof(media_format_h *));
+       vs_handle->fmt =
+                       (media_format_h **) calloc(dev_count, sizeof(media_format_h *));
        if (!vs_handle->fmt) {
                return VISION_SOURCE_ERROR_OUT_OF_MEMORY;
        }
 
        for (size_t i = 0; i < dev_count; i++) {
                vs_handle->dev_info[i].index = i;
-               strncpy(vs_handle->dev_info[i].name, dev_name[i].c_str(), DEVICE_NAME_LENGTH_MAX);
-               vs_handle->fmt[i] = (media_format_h *) calloc(dev_fmt_list[i].size(), sizeof(media_format_h));
+               strncpy(vs_handle->dev_info[i].name, dev_name[i].c_str(),
+                               DEVICE_NAME_LENGTH_MAX);
+               vs_handle->dev_info[i].name[DEVICE_NAME_LENGTH_MAX - 1] = '\0';
+               vs_handle->fmt[i] = (media_format_h *) calloc(dev_fmt_list[i].size(),
+                                                                                                         sizeof(media_format_h));
                if (!vs_handle->fmt[i]) {
                        return VISION_SOURCE_ERROR_OUT_OF_MEMORY;
                }
@@ -431,7 +460,8 @@ static int __vision_source_list_devices(vision_source_v4l2_s *vs_handle)
 
 static int __vision_source_v4l2_stream(int device_fd, int type, bool onoff)
 {
-       if (v4l2_ioctl(device_fd, onoff ? VIDIOC_STREAMON : VIDIOC_STREAMOFF, &type) < 0) {
+       if (v4l2_ioctl(device_fd, onoff ? VIDIOC_STREAMON : VIDIOC_STREAMOFF,
+                                  &type) < 0) {
                LOGE("stream %d failed. [t:%d] errno %d", onoff, type, errno);
                return VISION_SOURCE_ERROR_INTERNAL;
        }
@@ -441,7 +471,8 @@ static int __vision_source_v4l2_stream(int device_fd, int type, bool onoff)
        return VISION_SOURCE_ERROR_NONE;
 }
 
-static int __vision_source_v4l2_reqbufs(int device_fd, int type, int memory, uint32_t count, uint32_t *result_count)
+static int __vision_source_v4l2_reqbufs(int device_fd, int type, int memory,
+                                                                               uint32_t count, uint32_t *result_count)
 {
        struct v4l2_requestbuffers v4l2_reqbuf;
 
@@ -471,7 +502,8 @@ static int __vision_source_stop_stream(vision_source_v4l2_s *handle)
        LOGD("__vision_source_stop_stream");
 
        /* stream off */
-       int ret = __vision_source_v4l2_stream(handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, false);
+       int ret = __vision_source_v4l2_stream(handle->device_fd,
+                                                                                 V4L2_BUF_TYPE_VIDEO_CAPTURE, false);
 
        /* munmap */
        for (size_t i = 0; i < handle->mmap_addr.size(); i++) {
@@ -482,8 +514,9 @@ static int __vision_source_stop_stream(vision_source_v4l2_s *handle)
 
        /* reqbufs 0 */
        uint32_t buffer_count;
-       ret = __vision_source_v4l2_reqbufs(handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_MEMORY_MMAP, 0,
-                                                                          &buffer_count);
+       ret = __vision_source_v4l2_reqbufs(handle->device_fd,
+                                                                          V4L2_BUF_TYPE_VIDEO_CAPTURE,
+                                                                          V4L2_MEMORY_MMAP, 0, &buffer_count);
 
        LOGD("reqbufs 0 : 0x%x", ret);
 
@@ -535,7 +568,8 @@ static int __vision_source_set_stream(vision_source_v4l2_s *handle)
        return VISION_SOURCE_ERROR_NONE;
 }
 
-static int __vision_source_v4l2_dqbuf(int device_fd, int type, int memory, int *index, unsigned int *used_size)
+static int __vision_source_v4l2_dqbuf(int device_fd, int type, int memory,
+                                                                         int *index, unsigned int *used_size)
 {
        struct v4l2_buffer v4l2_buf;
        memset(&v4l2_buf, 0x0, sizeof(struct v4l2_buffer));
@@ -609,8 +643,9 @@ static int __vision_source_mmap(vision_source_v4l2_s *handle)
 
                handle->v4l2_buf.push_back(v4l2_buf);
 
-               void *data =
-                               v4l2_mmap(0, v4l2_buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, handle->device_fd, v4l2_buf.m.offset);
+               void *data = v4l2_mmap(0, v4l2_buf.length, PROT_READ | PROT_WRITE,
+                                                          MAP_SHARED, handle->device_fd,
+                                                          v4l2_buf.m.offset);
 
                if (data == MAP_FAILED) {
                        LOGE("[%d] mmap failed (errno %d)", i, errno);
@@ -619,22 +654,26 @@ static int __vision_source_mmap(vision_source_v4l2_s *handle)
 
                handle->mmap_addr.push_back(data);
 
-               if (__vision_source_v4l2_qbuf(handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_MEMORY_MMAP, i) !=
-                       VISION_SOURCE_ERROR_NONE) {
+               if (__vision_source_v4l2_qbuf(
+                                       handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE,
+                                       V4L2_MEMORY_MMAP, i) != VISION_SOURCE_ERROR_NONE) {
                        LOGE("[%d] qbuf failed (errno %d)", i, errno);
                        return VISION_SOURCE_ERROR_INTERNAL;
                }
        }
 
        /* stream on */
-       return __vision_source_v4l2_stream(handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, true);
+       return __vision_source_v4l2_stream(handle->device_fd,
+                                                                          V4L2_BUF_TYPE_VIDEO_CAPTURE, true);
 }
 
 static int __vision_source_start_stream(vision_source_v4l2_s *handle)
 {
        uint32_t buffer_count = 0;
        /* request buffer */
-       int ret = __vision_source_v4l2_reqbufs(handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_MEMORY_MMAP, BUFFER_MAX,
+       int ret = __vision_source_v4l2_reqbufs(handle->device_fd,
+                                                                                  V4L2_BUF_TYPE_VIDEO_CAPTURE,
+                                                                                  V4L2_MEMORY_MMAP, BUFFER_MAX,
                                                                                   &buffer_count);
        if (ret != VISION_SOURCE_ERROR_NONE) {
                return ret;
@@ -644,8 +683,9 @@ static int __vision_source_start_stream(vision_source_v4l2_s *handle)
 
        ret = __vision_source_mmap(handle);
        if (ret != VISION_SOURCE_ERROR_NONE)
-               __vision_source_v4l2_reqbufs(handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_MEMORY_MMAP, 0,
-                                                                        &buffer_count);
+               __vision_source_v4l2_reqbufs(handle->device_fd,
+                                                                        V4L2_BUF_TYPE_VIDEO_CAPTURE,
+                                                                        V4L2_MEMORY_MMAP, 0, &buffer_count);
 
        return ret;
 }
@@ -656,7 +696,7 @@ int vision_source_init(vision_source_h *handle)
 
        int ret = __vision_source_list_devices(v4l2_handle);
        if (ret != VISION_SOURCE_ERROR_NONE) {
-               free(v4l2_handle);
+               delete v4l2_handle;
                LOGE("get device info failed");
                return ret;
        }
@@ -694,7 +734,9 @@ int vision_source_exit(vision_source_h handle)
        return VISION_SOURCE_ERROR_NONE;
 }
 
-int vision_source_list_devices(vision_source_h handle, const vision_source_device_info_s **dev_list, int *dev_count)
+int vision_source_list_devices(vision_source_h handle,
+                                                          const vision_source_device_info_s **dev_list,
+                                                          int *dev_count)
 {
        vision_source_v4l2_s *v4l2_handle = (vision_source_v4l2_s *) handle;
 
@@ -704,7 +746,8 @@ int vision_source_list_devices(vision_source_h handle, const vision_source_devic
        return VISION_SOURCE_ERROR_NONE;
 }
 
-int vision_source_list_device_caps(vision_source_h handle, int dev_index, const media_format_h **fmt_list,
+int vision_source_list_device_caps(vision_source_h handle, int dev_index,
+                                                                  const media_format_h **fmt_list,
                                                                   int *fmt_count)
 {
        vision_source_v4l2_s *v4l2_handle = (vision_source_v4l2_s *) handle;
@@ -789,14 +832,16 @@ int vision_source_set_stream_format(vision_source_h handle, media_format_h fmt)
 {
        vision_source_v4l2_s *v4l2_handle = (vision_source_v4l2_s *) handle;
 
-       if (!v4l2_handle->fmt || v4l2_handle->status != VISION_SOURCE_STATUS_OPENDED) {
+       if (!v4l2_handle->fmt ||
+               v4l2_handle->status != VISION_SOURCE_STATUS_OPENDED) {
                LOGE("Invalid state");
                return VISION_SOURCE_ERROR_INVALID_OPERATION;
        }
 
        media_format_mimetype_e mimetype;
        int width, height, fps;
-       int ret = media_format_get_video_info(fmt, &mimetype, &width, &height, nullptr, nullptr);
+       int ret = media_format_get_video_info(fmt, &mimetype, &width, &height,
+                                                                                 nullptr, nullptr);
        if (ret != MEDIA_FORMAT_ERROR_NONE) {
                LOGE("media_format_get_video_info failed");
                return VISION_SOURCE_ERROR_INTERNAL;
@@ -809,7 +854,8 @@ int vision_source_set_stream_format(vision_source_h handle, media_format_h fmt)
 
        LOGI("Try set format width: %d, height: %d, fps: %d", width, height, fps);
 
-       fmt_info request { mimetype, (uint32_t) width, (uint32_t) height, (uint32_t) fps };
+       fmt_info request { mimetype, (uint32_t) width, (uint32_t) height,
+                                          (uint32_t) fps };
        vector<fmt_info> &vec = v4l2_handle->fmt_list[v4l2_handle->device_index];
        const auto pos = find(vec.begin(), vec.end(), request);
        if (pos == vec.end()) {
@@ -834,11 +880,13 @@ int vision_source_set_stream_format(vision_source_h handle, media_format_h fmt)
        return VISION_SOURCE_ERROR_NONE;
 }
 
-static void __vision_source_pkt_dispose_cb(media_packet_h packet, void *user_data)
+static void __vision_source_pkt_dispose_cb(media_packet_h packet,
+                                                                                  void *user_data)
 {
        pkt_dispose_cb_data *data = (pkt_dispose_cb_data *) user_data;
 
-       int ret = __vision_source_v4l2_qbuf(data->fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_MEMORY_MMAP, data->index);
+       int ret = __vision_source_v4l2_qbuf(data->fd, V4L2_BUF_TYPE_VIDEO_CAPTURE,
+                                                                               V4L2_MEMORY_MMAP, data->index);
        if (ret != VISION_SOURCE_ERROR_NONE)
                LOGE("qbuf failed");
 }
@@ -868,19 +916,22 @@ static void __fetch_buffer_and_callback(vision_source_v4l2_s *v4l2_handle)
                        LOGD("stop buffer handler thread");
                        break;
                }
-               ret = __vision_source_v4l2_dqbuf(v4l2_handle->device_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_MEMORY_MMAP, &index,
-                                                                                &byte_size);
+               ret = __vision_source_v4l2_dqbuf(v4l2_handle->device_fd,
+                                                                                V4L2_BUF_TYPE_VIDEO_CAPTURE,
+                                                                                V4L2_MEMORY_MMAP, &index, &byte_size);
                if (ret != VISION_SOURCE_ERROR_NONE) {
                        LOGE("dqbuf failed");
                        break;
                }
 
-               media_format_h fmt = v4l2_handle->fmt[v4l2_handle->device_index][v4l2_handle->fmt_index];
+               media_format_h fmt = v4l2_handle->fmt[v4l2_handle->device_index]
+                                                                                        [v4l2_handle->fmt_index];
 
                media_packet_h pkt;
-               ret = media_packet_new_from_external_memory(fmt, v4l2_handle->mmap_addr[index], byte_size,
-                                                                                                       __vision_source_pkt_dispose_cb, &v4l2_handle->pkt_data[index],
-                                                                                                       &pkt);
+               ret = media_packet_new_from_external_memory(
+                               fmt, v4l2_handle->mmap_addr[index], byte_size,
+                               __vision_source_pkt_dispose_cb, &v4l2_handle->pkt_data[index],
+                               &pkt);
                if (ret != MEDIA_PACKET_ERROR_NONE) {
                        LOGE("media_packet_new_from_external_memory failed");
                        continue;
@@ -916,7 +967,8 @@ int vision_source_start_stream(vision_source_h handle)
        }
 
        v4l2_handle->buffer_thread_run = true;
-       v4l2_handle->buffer_thread = thread(__fetch_buffer_and_callback, v4l2_handle);
+       v4l2_handle->buffer_thread =
+                       thread(__fetch_buffer_and_callback, v4l2_handle);
 
        v4l2_handle->status = VISION_SOURCE_STATUS_STARTED;
        LOGD("start preview done");
@@ -952,7 +1004,8 @@ int vision_source_stop_stream(vision_source_h handle)
 }
 
 /* TODO: run-time callback change*/
-int vision_source_set_stream_cb(vision_source_h handle, stream_cb callback, void *user_data)
+int vision_source_set_stream_cb(vision_source_h handle, stream_cb callback,
+                                                               void *user_data)
 {
        vision_source_v4l2_s *v4l2_handle = (vision_source_v4l2_s *) handle;
 
index 3a2f6d6..4462e48 100644 (file)
@@ -21,7 +21,8 @@ TEST_F(VisionV4L2, ListDevices)
 {
        const vision_source_device_info_s *dev_list;
        int dev_cnt;
-       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt),
+                         VISION_SOURCE_ERROR_NONE);
        EXPECT_GT(dev_cnt, 0);
 }
 
@@ -31,15 +32,19 @@ TEST_F(VisionV4L2, DevCap)
        const media_format_h *fmt_list;
        int fmt_count;
        int dev_cnt;
-       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt),
+                         VISION_SOURCE_ERROR_NONE);
        ASSERT_GT(dev_cnt, 0);
-       ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list, &fmt_count), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list,
+                                                                                        &fmt_count),
+                         VISION_SOURCE_ERROR_NONE);
        ASSERT_GT(fmt_count, 0);
 }
 
 TEST_F(VisionV4L2, OpenDev0)
 {
-       ASSERT_EQ(vision_source_open_device(ms_handle, 0), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_open_device(ms_handle, 0),
+                         VISION_SOURCE_ERROR_NONE);
        EXPECT_EQ(vision_source_close_device(ms_handle), VISION_SOURCE_ERROR_NONE);
 }
 
@@ -49,11 +54,15 @@ TEST_F(VisionV4L2, NotOpenDev)
        const media_format_h *fmt_list;
        int fmt_count;
        int dev_cnt;
-       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt),
+                         VISION_SOURCE_ERROR_NONE);
        ASSERT_GT(dev_cnt, 0);
-       ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list, &fmt_count), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list,
+                                                                                        &fmt_count),
+                         VISION_SOURCE_ERROR_NONE);
        ASSERT_GT(fmt_count, 2);
-       EXPECT_EQ(vision_source_set_stream_format(ms_handle, fmt_list[2]), VISION_SOURCE_ERROR_INVALID_OPERATION);
+       EXPECT_EQ(vision_source_set_stream_format(ms_handle, fmt_list[2]),
+                         VISION_SOURCE_ERROR_INVALID_OPERATION);
 }
 
 TEST_F(VisionV4L2, SetStream)
@@ -62,12 +71,17 @@ TEST_F(VisionV4L2, SetStream)
        const media_format_h *fmt_list;
        int fmt_count;
        int dev_cnt;
-       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt),
+                         VISION_SOURCE_ERROR_NONE);
        ASSERT_GT(dev_cnt, 0);
-       ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list, &fmt_count), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list,
+                                                                                        &fmt_count),
+                         VISION_SOURCE_ERROR_NONE);
        ASSERT_GT(fmt_count, 2);
-       ASSERT_EQ(vision_source_open_device(ms_handle, 0), VISION_SOURCE_ERROR_NONE);
-       EXPECT_EQ(vision_source_set_stream_format(ms_handle, fmt_list[2]), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_open_device(ms_handle, 0),
+                         VISION_SOURCE_ERROR_NONE);
+       EXPECT_EQ(vision_source_set_stream_format(ms_handle, fmt_list[2]),
+                         VISION_SOURCE_ERROR_NONE);
        EXPECT_EQ(vision_source_close_device(ms_handle), VISION_SOURCE_ERROR_NONE);
 }
 
@@ -82,18 +96,24 @@ protected:
                int fmt_count;
                int dev_cnt;
 
-               ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt), VISION_SOURCE_ERROR_NONE);
+               ASSERT_EQ(vision_source_list_devices(ms_handle, &dev_list, &dev_cnt),
+                                 VISION_SOURCE_ERROR_NONE);
                ASSERT_GT(dev_cnt, 0);
-               ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list, &fmt_count), VISION_SOURCE_ERROR_NONE);
+               ASSERT_EQ(vision_source_list_device_caps(ms_handle, 0, &fmt_list,
+                                                                                                &fmt_count),
+                                 VISION_SOURCE_ERROR_NONE);
                ASSERT_GT(fmt_count, 2);
 
-               ASSERT_EQ(vision_source_open_device(ms_handle, 0), VISION_SOURCE_ERROR_NONE);
-               ASSERT_EQ(vision_source_set_stream_format(ms_handle, fmt_list[2]), VISION_SOURCE_ERROR_NONE);
+               ASSERT_EQ(vision_source_open_device(ms_handle, 0),
+                                 VISION_SOURCE_ERROR_NONE);
+               ASSERT_EQ(vision_source_set_stream_format(ms_handle, fmt_list[2]),
+                                 VISION_SOURCE_ERROR_NONE);
        }
 
        void TearDown() override
        {
-               EXPECT_EQ(vision_source_close_device(ms_handle), VISION_SOURCE_ERROR_NONE);
+               EXPECT_EQ(vision_source_close_device(ms_handle),
+                                 VISION_SOURCE_ERROR_NONE);
                ASSERT_EQ(vision_source_exit(ms_handle), VISION_SOURCE_ERROR_NONE);
        }
        vision_source_h ms_handle;
@@ -101,7 +121,8 @@ protected:
 
 TEST_F(VisionV4L2FixedFormat, OnlyStopStream)
 {
-       EXPECT_EQ(vision_source_stop_stream(ms_handle), VISION_SOURCE_ERROR_INVALID_OPERATION);
+       EXPECT_EQ(vision_source_stop_stream(ms_handle),
+                         VISION_SOURCE_ERROR_INVALID_OPERATION);
 }
 
 TEST_F(VisionV4L2FixedFormat, StartStream)
@@ -120,7 +141,8 @@ static int test_cb(media_packet_h pkt, void *user_data)
 
        double delta_ms = (double) (end - *start) / CLOCKS_PER_SEC * 1000;
 
-       snprintf(filename, 127, "/opt/usr/home/owner/media/Images/out_%04u.data", (unsigned) delta_ms);
+       snprintf(filename, 127, "/opt/usr/home/owner/media/Images/out_%04u.data",
+                        (unsigned) delta_ms);
        file = fopen(filename, "w");
 
        void *data;
@@ -136,7 +158,8 @@ static int test_cb(media_packet_h pkt, void *user_data)
 TEST_F(VisionV4L2FixedFormat, StartWithCallback)
 {
        clock_t start = clock();
-       ASSERT_EQ(vision_source_set_stream_cb(ms_handle, test_cb, &start), VISION_SOURCE_ERROR_NONE);
+       ASSERT_EQ(vision_source_set_stream_cb(ms_handle, test_cb, &start),
+                         VISION_SOURCE_ERROR_NONE);
        ASSERT_EQ(vision_source_start_stream(ms_handle), VISION_SOURCE_ERROR_NONE);
        sleep(5);
        EXPECT_EQ(vision_source_stop_stream(ms_handle), VISION_SOURCE_ERROR_NONE);