From 3031cb4b4cb595e90bea4986c6dc8cbb2352f993 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sat, 2 Aug 2014 18:15:49 +0200 Subject: [PATCH 1/6] Minor code refactoring in v4l2 plugin. --- plugins/linux-v4l2/v4l2-input.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/plugins/linux-v4l2/v4l2-input.c b/plugins/linux-v4l2/v4l2-input.c index f6334030b..bdfbf3a82 100644 --- a/plugins/linux-v4l2/v4l2-input.c +++ b/plugins/linux-v4l2/v4l2-input.c @@ -529,15 +529,12 @@ static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat, blog(LOG_INFO, "Stepwise and Continuous framesizes " "are currently hardcoded"); - for (uint_fast32_t i = 0; ; ++i) { - int packed = fixed_framesizes[i]; - if (!packed) - break; + for (const int *packed = fixed_framesizes; *packed; ++packed) { int width; int height; - unpack_tuple(&width, &height, packed); + unpack_tuple(&width, &height, *packed); dstr_printf(&buffer, "%dx%d", width, height); - obs_property_list_add_int(prop, buffer.array, packed); + obs_property_list_add_int(prop, buffer.array, *packed); } break; } @@ -579,16 +576,14 @@ static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat, default: blog(LOG_INFO, "Stepwise and Continuous framerates " "are currently hardcoded"); - for (uint_fast32_t i = 0; ; ++i) { - int packed = fixed_framerates[i]; - if (!packed) - break; + + for (const int *packed = fixed_framerates; *packed; ++packed) { int num; int denom; - unpack_tuple(&num, &denom, packed); + unpack_tuple(&num, &denom, *packed); float fps = (float) denom / num; dstr_printf(&buffer, "%.2f", fps); - obs_property_list_add_int(prop, buffer.array, packed); + obs_property_list_add_int(prop, buffer.array, *packed); } break; } From 07d4c5f0371e28694e9ff9861fa5458ed86daf4a Mon Sep 17 00:00:00 2001 From: fryshorts Date: Tue, 5 Aug 2014 22:03:59 +0200 Subject: [PATCH 2/6] Make device inputs selectable for v4l2 plugin. This adds an additional property to the plugin to select an input if the device has multiple ones. --- plugins/linux-v4l2/data/locale/en-US.ini | 1 + plugins/linux-v4l2/v4l2-input.c | 57 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/plugins/linux-v4l2/data/locale/en-US.ini b/plugins/linux-v4l2/data/locale/en-US.ini index e5c578845..d81320bd8 100644 --- a/plugins/linux-v4l2/data/locale/en-US.ini +++ b/plugins/linux-v4l2/data/locale/en-US.ini @@ -1,5 +1,6 @@ V4L2Input="Video Capture Device (V4L2)" Device="Device" +Input="Input" ImageFormat="Video Format" Resolution="Resolution" FrameRate="Frame Rate" diff --git a/plugins/linux-v4l2/v4l2-input.c b/plugins/linux-v4l2/v4l2-input.c index bdfbf3a82..f132b6676 100644 --- a/plugins/linux-v4l2/v4l2-input.c +++ b/plugins/linux-v4l2/v4l2-input.c @@ -65,6 +65,7 @@ struct v4l2_data { os_event_t event; char *set_device; + int_fast32_t set_input; int_fast32_t set_pixfmt; int_fast32_t set_res; int_fast32_t set_fps; @@ -399,6 +400,7 @@ static const char* v4l2_getname(void) static void v4l2_defaults(obs_data_t settings) { + obs_data_set_default_int(settings, "input", 0); obs_data_set_default_int(settings, "pixelformat", V4L2_PIX_FMT_YUYV); obs_data_set_default_int(settings, "resolution", pack_tuple(640, 480)); @@ -464,6 +466,26 @@ static void v4l2_device_list(obs_property_t prop, obs_data_t settings) dstr_free(&device); } +/* + * List inputs for device + */ +static void v4l2_input_list(int_fast32_t dev, obs_property_t prop) +{ + struct v4l2_input in; + memset(&in, 0, sizeof(in)); + + obs_property_list_clear(prop); + + while (v4l2_ioctl(dev, VIDIOC_ENUMINPUT, &in) == 0) { + if (in.type & V4L2_INPUT_TYPE_CAMERA) { + obs_property_list_add_int(prop, (char *) in.name, + in.index); + blog(LOG_INFO, "Found input '%s'", in.name); + } + in.index++; + } +} + /* * List formats for device */ @@ -603,6 +625,25 @@ static bool device_selected(obs_properties_t props, obs_property_t p, if (dev == -1) return false; + obs_property_t prop = obs_properties_get(props, "input"); + v4l2_input_list(dev, prop); + obs_property_modified(prop, settings); + v4l2_close(dev); + return true; +} + +/* + * Input selected callback + */ +static bool input_selected(obs_properties_t props, obs_property_t p, + obs_data_t settings) +{ + UNUSED_PARAMETER(p); + int dev = v4l2_open(obs_data_get_string(settings, "device_id"), + O_RDWR | O_NONBLOCK); + if (dev == -1) + return false; + obs_property_t prop = obs_properties_get(props, "pixelformat"); v4l2_format_list(dev, prop); obs_property_modified(prop, settings); @@ -662,6 +703,10 @@ static obs_properties_t v4l2_properties(void) "device_id", obs_module_text("Device"), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING); + obs_property_t input_list = obs_properties_add_list(props, + "input", obs_module_text("Input"), + OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT); + obs_property_t format_list = obs_properties_add_list(props, "pixelformat", obs_module_text("VideoFormat"), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT); @@ -676,6 +721,7 @@ static obs_properties_t v4l2_properties(void) v4l2_device_list(device_list, NULL); obs_property_set_modified_callback(device_list, device_selected); + obs_property_set_modified_callback(input_list, input_selected); obs_property_set_modified_callback(format_list, format_selected); obs_property_set_modified_callback(resolution_list, resolution_selected); @@ -738,6 +784,12 @@ static void v4l2_init(struct v4l2_data *data) goto fail; } + /* set input */ + if (v4l2_ioctl(data->dev, VIDIOC_S_INPUT, &data->set_input) < 0) { + blog(LOG_ERROR, "Unable to set input"); + goto fail; + } + /* set pixel format and resolution */ unpack_tuple(&width, &height, data->set_res); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; @@ -809,6 +861,11 @@ static void v4l2_update(void *vptr, obs_data_t settings) restart = true; } + if (data->set_input != obs_data_get_int(settings, "input")) { + data->set_input = obs_data_get_int(settings, "input"); + restart = true; + } + if (data->set_pixfmt != obs_data_get_int(settings, "pixelformat")) { data->set_pixfmt = obs_data_get_int(settings, "pixelformat"); restart = true; From 7e4972e747478084f4237d0fb2a2f16774cd2fad Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sun, 10 Aug 2014 14:53:44 +0200 Subject: [PATCH 3/6] Small cleanups in v4l2 plugin. --- plugins/linux-v4l2/v4l2-input.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/plugins/linux-v4l2/v4l2-input.c b/plugins/linux-v4l2/v4l2-input.c index f132b6676..9aca48c19 100644 --- a/plugins/linux-v4l2/v4l2-input.c +++ b/plugins/linux-v4l2/v4l2-input.c @@ -696,7 +696,6 @@ static bool resolution_selected(obs_properties_t props, obs_property_t p, static obs_properties_t v4l2_properties(void) { - /* TODO: locale */ obs_properties_t props = obs_properties_create(); obs_property_t device_list = obs_properties_add_list(props, @@ -889,8 +888,6 @@ static void v4l2_update(void *vptr, obs_data_t settings) static void *v4l2_create(obs_data_t settings, obs_source_t source) { - UNUSED_PARAMETER(settings); - struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data)); data->dev = -1; data->source = source; From 71200051b05296c3dcfceb281c7533ad07256565 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Sun, 10 Aug 2014 17:33:03 +0200 Subject: [PATCH 4/6] Move some functions to separate file for v4l2 plugin. This moves some functions that are generic to a separate source file. While doing so the api to those functions was improved to be more generic and not depend on knowledge about the internal structure of the plugin. --- plugins/linux-v4l2/CMakeLists.txt | 1 + plugins/linux-v4l2/v4l2-helpers.c | 123 ++++++++++++++++++ plugins/linux-v4l2/v4l2-helpers.h | 180 ++++++++++++++++++++++++++ plugins/linux-v4l2/v4l2-input.c | 202 ++---------------------------- 4 files changed, 314 insertions(+), 192 deletions(-) create mode 100644 plugins/linux-v4l2/v4l2-helpers.c create mode 100644 plugins/linux-v4l2/v4l2-helpers.h diff --git a/plugins/linux-v4l2/CMakeLists.txt b/plugins/linux-v4l2/CMakeLists.txt index 7b8b1c7af..ba84ab71d 100644 --- a/plugins/linux-v4l2/CMakeLists.txt +++ b/plugins/linux-v4l2/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/libobs") set(linux-v4l2_SOURCES linux-v4l2.c v4l2-input.c + v4l2-helpers.c ) add_library(linux-v4l2 MODULE diff --git a/plugins/linux-v4l2/v4l2-helpers.c b/plugins/linux-v4l2/v4l2-helpers.c new file mode 100644 index 000000000..165f26502 --- /dev/null +++ b/plugins/linux-v4l2/v4l2-helpers.c @@ -0,0 +1,123 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include + +#include + +#include "v4l2-helpers.h" + +#define blog(level, msg, ...) blog(level, "v4l2-helpers: " msg, ##__VA_ARGS__) + +int_fast32_t v4l2_start_capture(int_fast32_t dev, struct v4l2_buffer_data *buf) +{ + enum v4l2_buf_type type; + struct v4l2_buffer enq; + + memset(&enq, 0, sizeof(enq)); + enq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + enq.memory = V4L2_MEMORY_MMAP; + + for (enq.index = 0; enq.index < buf->count; ++enq.index) { + if (v4l2_ioctl(dev, VIDIOC_QBUF, &enq) < 0) { + blog(LOG_ERROR, "unable to queue buffer"); + return -1; + } + } + + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (v4l2_ioctl(dev, VIDIOC_STREAMON, &type) < 0) { + blog(LOG_ERROR, "unable to start stream"); + return -1; + } + + return 0; +} + +int_fast32_t v4l2_stop_capture(int_fast32_t dev) +{ + enum v4l2_buf_type type; + + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (v4l2_ioctl(dev, VIDIOC_STREAMOFF, &type) < 0) { + blog(LOG_ERROR, "unable to stop stream"); + return -1; + } + + return 0; +} + +int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf) +{ + struct v4l2_requestbuffers req; + struct v4l2_buffer map; + + memset(&req, 0, sizeof(req)); + req.count = 4; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + req.memory = V4L2_MEMORY_MMAP; + + if (v4l2_ioctl(dev, VIDIOC_REQBUFS, &req) < 0) { + blog(LOG_ERROR, "Request for buffers failed !"); + return -1; + } + + if (req.count < 2) { + blog(LOG_ERROR, "Device returned less than 2 buffers"); + return -1; + } + + buf->count = req.count; + buf->info = bzalloc(req.count * sizeof(struct v4l2_mmap_info)); + + memset(&map, 0, sizeof(map)); + map.type = req.type; + map.memory = req.memory; + + for (map.index = 0; map.index < req.count; ++map.index) { + if (v4l2_ioctl(dev, VIDIOC_QUERYBUF, &map) < 0) { + blog(LOG_ERROR, "Failed to query buffer details"); + return -1; + } + + buf->info[map.index].length = map.length; + buf->info[map.index].start = v4l2_mmap(NULL, map.length, + PROT_READ | PROT_WRITE, MAP_SHARED, + dev, map.m.offset); + + if (buf->info[map.index].start == MAP_FAILED) { + blog(LOG_ERROR, "mmap for buffer failed"); + return -1; + } + } + + return 0; +} + +int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf) +{ + for(uint_fast32_t i = 0; i < buf->count; ++i) { + if (buf->info[i].start != MAP_FAILED) + v4l2_munmap(buf->info[i].start, buf->info[i].length); + } + + buf->count = 0; + bfree(buf->info); + + return 0; +} + diff --git a/plugins/linux-v4l2/v4l2-helpers.h b/plugins/linux-v4l2/v4l2-helpers.h new file mode 100644 index 000000000..b53c9b746 --- /dev/null +++ b/plugins/linux-v4l2/v4l2-helpers.h @@ -0,0 +1,180 @@ +/* +Copyright (C) 2014 by Leonhard Oelke + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#pragma once + +#include +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Data structure for mapped buffers + */ +struct v4l2_mmap_info { + /** length of the mapped buffer */ + size_t length; + /** start address of the mapped buffer */ + void *start; +}; + +/** + * Data structure for buffer info + */ +struct v4l2_buffer_data { + /** number of mapped buffers */ + uint_fast32_t count; + /** memory info for mapped buffers */ + struct v4l2_mmap_info *info; +}; + +/** + * Convert v4l2 pixel format to obs video format + * + * @param format v4l2 format id + * + * @return obs video_format id + */ +static inline enum video_format v4l2_to_obs_video_format(uint_fast32_t format) +{ + switch (format) { + case V4L2_PIX_FMT_YVYU: return VIDEO_FORMAT_YVYU; + case V4L2_PIX_FMT_YUYV: return VIDEO_FORMAT_YUY2; + case V4L2_PIX_FMT_UYVY: return VIDEO_FORMAT_UYVY; + case V4L2_PIX_FMT_NV12: return VIDEO_FORMAT_NV12; + case V4L2_PIX_FMT_YUV420: return VIDEO_FORMAT_I420; + case V4L2_PIX_FMT_YVU420: return VIDEO_FORMAT_I420; + default: return VIDEO_FORMAT_NONE; + } +} + +/** + * Fixed framesizes for devices that don't support enumerating discrete values. + * + * The framesizes in this array are packed, the width encoded in the high word + * and the height in the low word. + * The array is terminated with a zero. + */ +static const int v4l2_framesizes[] = +{ + /* 4:3 */ + 160<<16 | 120, + 320<<16 | 240, + 480<<16 | 320, + 640<<16 | 480, + 800<<16 | 600, + 1024<<16 | 768, + 1280<<16 | 960, + 1440<<16 | 1050, + 1440<<16 | 1080, + 1600<<16 | 1200, + + /* 16:9 */ + 640<<16 | 360, + 960<<16 | 540, + 1280<<16 | 720, + 1600<<16 | 900, + 1920<<16 | 1080, + 1920<<16 | 1200, + + /* tv */ + 432<<16 | 520, + 480<<16 | 320, + 480<<16 | 530, + 486<<16 | 440, + 576<<16 | 310, + 576<<16 | 520, + 576<<16 | 570, + 720<<16 | 576, + 1024<<16 | 576, + + 0 +}; + +/** + * Fixed framerates for devices that don't support enumerating discrete values. + * + * The framerates in this array are packed, the numerator encoded in the high + * word and the denominator in the low word. + * The array is terminated with a zero. + */ +static const int v4l2_framerates[] = +{ + 1<<16 | 60, + 1<<16 | 50, + 1<<16 | 30, + 1<<16 | 25, + 1<<16 | 20, + 1<<16 | 15, + 1<<16 | 10, + 1<<16 | 5, + + 0 +}; + +/** + * Start the video capture on the device. + * + * This enqueues the memory mapped buffers and instructs the device to start + * the video stream. + * + * @param dev handle for the v4l2 device + * @param buf buffer data + * + * @return negative on failure + */ +int_fast32_t v4l2_start_capture(int_fast32_t dev, struct v4l2_buffer_data *buf); + +/** + * Stop the video capture on the device. + * + * @param dev handle for the v4l2 device + * + * @return negative on failure + */ +int_fast32_t v4l2_stop_capture(int_fast32_t dev); + +/** + * Create memory mapping for buffers + * + * This tries to map at least 2, preferably 4, buffers to application memory. + * + * @param dev handle for the v4l2 device + * @param buf buffer data + * + * @return negative on failure + */ +int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf); + +/** + * Destroy the memory mapping for buffers + * + * @param dev handle for the v4l2 device + * @param buf buffer data + * + * @return negative on failure + */ +int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf); + +#ifdef __cplusplus +} +#endif diff --git a/plugins/linux-v4l2/v4l2-input.c b/plugins/linux-v4l2/v4l2-input.c index 9aca48c19..61a4ce636 100644 --- a/plugins/linux-v4l2/v4l2-input.c +++ b/plugins/linux-v4l2/v4l2-input.c @@ -21,7 +21,6 @@ along with this program. If not, see . #include #include #include -#include #include #include #include @@ -35,6 +34,8 @@ along with this program. If not, see . #include #include +#include "v4l2-helpers.h" + #define V4L2_DATA(voidptr) struct v4l2_data *data = voidptr; #define timeval2ns(tv) \ @@ -42,11 +43,6 @@ along with this program. If not, see . #define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__) -struct v4l2_buffer_data { - size_t length; - void *start; -}; - /** * Data structure for the v4l2 source * @@ -79,23 +75,9 @@ struct v4l2_data { int_fast32_t pixfmt; uint_fast32_t linesize; - uint_fast32_t buf_count; - struct v4l2_buffer_data *buf; + struct v4l2_buffer_data buffers; }; -static enum video_format v4l2_to_obs_video_format(uint_fast32_t format) -{ - switch (format) { - case V4L2_PIX_FMT_YVYU: return VIDEO_FORMAT_YVYU; - case V4L2_PIX_FMT_YUYV: return VIDEO_FORMAT_YUY2; - case V4L2_PIX_FMT_UYVY: return VIDEO_FORMAT_UYVY; - case V4L2_PIX_FMT_NV12: return VIDEO_FORMAT_NV12; - case V4L2_PIX_FMT_YUV420: return VIDEO_FORMAT_I420; - case V4L2_PIX_FMT_YVU420: return VIDEO_FORMAT_I420; - default: return VIDEO_FORMAT_NONE; - } -} - /* * used to store framerate and resolution values */ @@ -110,169 +92,6 @@ static void unpack_tuple(int *a, int *b, int packed) *b = packed & 0xffff; } -/* fixed framesizes as fallback */ -static const int fixed_framesizes[] = -{ - /* 4:3 */ - 160<<16 | 120, - 320<<16 | 240, - 480<<16 | 320, - 640<<16 | 480, - 800<<16 | 600, - 1024<<16 | 768, - 1280<<16 | 960, - 1440<<16 | 1050, - 1440<<16 | 1080, - 1600<<16 | 1200, - - /* 16:9 */ - 640<<16 | 360, - 960<<16 | 540, - 1280<<16 | 720, - 1600<<16 | 900, - 1920<<16 | 1080, - 1920<<16 | 1200, - - /* tv */ - 432<<16 | 520, - 480<<16 | 320, - 480<<16 | 530, - 486<<16 | 440, - 576<<16 | 310, - 576<<16 | 520, - 576<<16 | 570, - 720<<16 | 576, - 1024<<16 | 576, - - 0 -}; - -/* fixed framerates as fallback */ -static const int fixed_framerates[] = -{ - 1<<16 | 60, - 1<<16 | 50, - 1<<16 | 30, - 1<<16 | 25, - 1<<16 | 20, - 1<<16 | 15, - 1<<16 | 10, - 1<<16 | 5, - - 0 -}; - -/* - * start capture - */ -static int_fast32_t v4l2_start_capture(struct v4l2_data *data) -{ - enum v4l2_buf_type type; - - for (uint_fast32_t i = 0; i < data->buf_count; ++i) { - struct v4l2_buffer buf; - - buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - buf.memory = V4L2_MEMORY_MMAP; - buf.index = i; - - if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) { - blog(LOG_ERROR, "unable to queue buffer"); - return -1; - } - } - - type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - if (v4l2_ioctl(data->dev, VIDIOC_STREAMON, &type) < 0) { - blog(LOG_ERROR, "unable to start stream"); - return -1; - } - - return 0; -} - -/* - * stop capture - */ -static int_fast32_t v4l2_stop_capture(struct v4l2_data *data) -{ - enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - - if (v4l2_ioctl(data->dev, VIDIOC_STREAMOFF, &type) < 0) { - blog(LOG_ERROR, "unable to stop stream"); - } - - return 0; -} - -/** - * Create memory mapping for buffers - * - * This tries to map at least 2, preferably 4, buffers to userspace. - * - * @return 0 on success, -1 on failure - */ -static int_fast32_t v4l2_create_mmap(struct v4l2_data *data) -{ - struct v4l2_requestbuffers req; - - req.count = 4; - req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - req.memory = V4L2_MEMORY_MMAP; - - if (v4l2_ioctl(data->dev, VIDIOC_REQBUFS, &req) < 0) { - blog(LOG_ERROR, "Request for buffers failed !"); - return -1; - } - - if (req.count < 2) { - blog(LOG_ERROR, "Device returned less than 2 buffers"); - return -1; - } - - data->buf_count = req.count; - data->buf = bzalloc(req.count * sizeof(struct v4l2_buffer_data)); - - for (uint_fast32_t i = 0; i < req.count; ++i) { - struct v4l2_buffer buf; - - buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - buf.memory = V4L2_MEMORY_MMAP; - buf.index = i; - - if (v4l2_ioctl(data->dev, VIDIOC_QUERYBUF, &buf) < 0) { - blog(LOG_ERROR, "Failed to query buffer details"); - return -1; - } - - data->buf[i].length = buf.length; - data->buf[i].start = v4l2_mmap(NULL, buf.length, - PROT_READ | PROT_WRITE, MAP_SHARED, - data->dev, buf.m.offset); - - if (data->buf[i].start == MAP_FAILED) { - blog(LOG_ERROR, "mmap for buffer failed"); - return -1; - } - } - - return 0; -} - -/** - * Destroy memory mapping for buffers - */ -static void v4l2_destroy_mmap(struct v4l2_data *data) -{ - for(uint_fast32_t i = 0; i < data->buf_count; ++i) { - if (data->buf[i].start != MAP_FAILED) - v4l2_munmap(data->buf[i].start, data->buf[i].length); - } - - data->buf_count = 0; - bfree(data->buf); -} - /** * Prepare the output frame structure for obs and compute plane offsets * @@ -337,7 +156,7 @@ static void *v4l2_thread(void *vptr) struct obs_source_frame out; size_t plane_offsets[MAX_AV_PLANES]; - if (v4l2_start_capture(data) < 0) + if (v4l2_start_capture(data->dev, &data->buffers) < 0) goto exit; data->frames = 0; @@ -373,7 +192,7 @@ static void *v4l2_thread(void *vptr) } out.timestamp = timeval2ns(buf.timestamp); - start = (uint8_t *) data->buf[buf.index].start; + start = (uint8_t *) data->buffers.info[buf.index].start; for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i) out.data[i] = start + plane_offsets[i]; obs_source_output_video(data->source, &out); @@ -389,7 +208,7 @@ static void *v4l2_thread(void *vptr) blog(LOG_INFO, "Stopped capture after %"PRIu64" frames", data->frames); exit: - v4l2_stop_capture(data); + v4l2_stop_capture(data->dev); return NULL; } @@ -551,7 +370,7 @@ static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat, blog(LOG_INFO, "Stepwise and Continuous framesizes " "are currently hardcoded"); - for (const int *packed = fixed_framesizes; *packed; ++packed) { + for (const int *packed = v4l2_framesizes; *packed; ++packed) { int width; int height; unpack_tuple(&width, &height, *packed); @@ -599,7 +418,7 @@ static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat, blog(LOG_INFO, "Stepwise and Continuous framerates " "are currently hardcoded"); - for (const int *packed = fixed_framerates; *packed; ++packed) { + for (const int *packed = v4l2_framerates; *packed; ++packed) { int num; int denom; unpack_tuple(&num, &denom, *packed); @@ -735,8 +554,7 @@ static void v4l2_terminate(struct v4l2_data *data) os_event_destroy(data->event); } - if (data->buf_count) - v4l2_destroy_mmap(data); + v4l2_destroy_mmap(&data->buffers); if (data->dev != -1) { v4l2_close(data->dev); @@ -825,7 +643,7 @@ static void v4l2_init(struct v4l2_data *data) dstr_free(&fps); /* map buffers */ - if (v4l2_create_mmap(data) < 0) { + if (v4l2_create_mmap(data->dev, &data->buffers) < 0) { blog(LOG_ERROR, "Failed to map buffers"); goto fail; } From d73d2b22f8f40cc247840ff5990c372fb90c275e Mon Sep 17 00:00:00 2001 From: fryshorts Date: Mon, 11 Aug 2014 23:27:46 +0200 Subject: [PATCH 5/6] Fix infinite loop in v4l2 plugin. When initialization of the device fails the plugin may hang trying to join a non-existing pthread. --- plugins/linux-v4l2/v4l2-input.c | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/linux-v4l2/v4l2-input.c b/plugins/linux-v4l2/v4l2-input.c index 61a4ce636..09f157d1f 100644 --- a/plugins/linux-v4l2/v4l2-input.c +++ b/plugins/linux-v4l2/v4l2-input.c @@ -552,6 +552,7 @@ static void v4l2_terminate(struct v4l2_data *data) os_event_signal(data->event); pthread_join(data->thread, NULL); os_event_destroy(data->event); + data->thread = 0; } v4l2_destroy_mmap(&data->buffers); From cd6d2eb9b6795cb12c875435c1de70173d888b15 Mon Sep 17 00:00:00 2001 From: fryshorts Date: Mon, 11 Aug 2014 23:35:13 +0200 Subject: [PATCH 6/6] Fix crash in v4l2 plugin. This adds checks to make sure munmap and bfree are only called when there was actually memory mapped and allocated. --- plugins/linux-v4l2/v4l2-helpers.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/linux-v4l2/v4l2-helpers.c b/plugins/linux-v4l2/v4l2-helpers.c index 165f26502..6497ff1cc 100644 --- a/plugins/linux-v4l2/v4l2-helpers.c +++ b/plugins/linux-v4l2/v4l2-helpers.c @@ -111,12 +111,14 @@ int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf) int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf) { for(uint_fast32_t i = 0; i < buf->count; ++i) { - if (buf->info[i].start != MAP_FAILED) + if (buf->info[i].start != MAP_FAILED && buf->info[i].start != 0) v4l2_munmap(buf->info[i].start, buf->info[i].length); } - buf->count = 0; - bfree(buf->info); + if (buf->count) { + bfree(buf->info); + buf->count = 0; + } return 0; }