2014-03-11 11:33:04 -07:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Leonhard Oelke <leonhard@in-verted.de>
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2014-05-25 02:04:17 -07:00
|
|
|
#include <stdlib.h>
|
2014-03-11 11:33:04 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <fcntl.h>
|
2014-05-25 02:04:17 -07:00
|
|
|
#include <dirent.h>
|
2014-03-11 11:33:04 -07:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
#include <linux/videodev2.h>
|
2014-07-23 13:34:32 -07:00
|
|
|
#include <libv4l2.h>
|
2014-03-11 11:33:04 -07:00
|
|
|
|
|
|
|
#include <util/threading.h>
|
|
|
|
#include <util/bmem.h>
|
2014-05-25 02:04:17 -07:00
|
|
|
#include <util/dstr.h>
|
2014-07-09 22:12:57 -07:00
|
|
|
#include <obs-module.h>
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-08-10 08:33:03 -07:00
|
|
|
#include "v4l2-helpers.h"
|
|
|
|
|
2014-03-11 11:33:04 -07:00
|
|
|
#define V4L2_DATA(voidptr) struct v4l2_data *data = voidptr;
|
|
|
|
|
|
|
|
#define timeval2ns(tv) \
|
|
|
|
(((uint64_t) tv.tv_sec * 1000000000) + ((uint64_t) tv.tv_usec * 1000))
|
|
|
|
|
2014-07-15 11:51:13 -07:00
|
|
|
#define blog(level, msg, ...) blog(level, "v4l2-input: " msg, ##__VA_ARGS__)
|
|
|
|
|
2014-07-26 07:50:06 -07:00
|
|
|
/**
|
|
|
|
* Data structure for the v4l2 source
|
|
|
|
*/
|
2014-03-11 11:33:04 -07:00
|
|
|
struct v4l2_data {
|
2014-09-16 12:23:26 -07:00
|
|
|
/* settings */
|
|
|
|
char *device_id;
|
|
|
|
int input;
|
|
|
|
int pixfmt;
|
|
|
|
int resolution;
|
|
|
|
int framerate;
|
|
|
|
|
|
|
|
/* internal data */
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_source_t *source;
|
2014-03-11 11:33:04 -07:00
|
|
|
pthread_t thread;
|
2014-09-25 17:44:05 -07:00
|
|
|
os_event_t *event;
|
2014-03-11 11:33:04 -07:00
|
|
|
|
|
|
|
int_fast32_t dev;
|
2014-09-15 15:05:11 -07:00
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
int linesize;
|
2014-08-10 08:33:03 -07:00
|
|
|
struct v4l2_buffer_data buffers;
|
2014-09-16 12:23:26 -07:00
|
|
|
|
|
|
|
/* statistics */
|
|
|
|
uint64_t frames;
|
2014-03-11 11:33:04 -07:00
|
|
|
};
|
|
|
|
|
2014-07-26 06:42:45 -07:00
|
|
|
/**
|
|
|
|
* Prepare the output frame structure for obs and compute plane offsets
|
|
|
|
*
|
|
|
|
* Basically all data apart from memory pointers and the timestamp is known
|
2014-08-02 01:04:45 -07:00
|
|
|
* before the capture starts. This function prepares the obs_source_frame
|
|
|
|
* struct with all the data that is already known.
|
2014-07-26 06:42:45 -07:00
|
|
|
*
|
|
|
|
* v4l2 uses a continuous memory segment for all planes so we simply compute
|
|
|
|
* offsets to add to the start address in order to give obs the correct data
|
|
|
|
* pointers for the individual planes.
|
|
|
|
*/
|
|
|
|
static void v4l2_prep_obs_frame(struct v4l2_data *data,
|
2014-08-02 01:04:45 -07:00
|
|
|
struct obs_source_frame *frame, size_t *plane_offsets)
|
2014-07-26 06:42:45 -07:00
|
|
|
{
|
2014-08-02 01:04:45 -07:00
|
|
|
memset(frame, 0, sizeof(struct obs_source_frame));
|
2014-07-26 06:42:45 -07:00
|
|
|
memset(plane_offsets, 0, sizeof(size_t) * MAX_AV_PLANES);
|
|
|
|
|
|
|
|
frame->width = data->width;
|
|
|
|
frame->height = data->height;
|
2014-07-26 07:50:06 -07:00
|
|
|
frame->format = v4l2_to_obs_video_format(data->pixfmt);
|
2014-07-26 06:42:45 -07:00
|
|
|
video_format_get_parameters(VIDEO_CS_DEFAULT, VIDEO_RANGE_PARTIAL,
|
|
|
|
frame->color_matrix, frame->color_range_min,
|
|
|
|
frame->color_range_max);
|
|
|
|
|
2014-07-26 07:50:06 -07:00
|
|
|
switch(data->pixfmt) {
|
2014-07-26 06:42:45 -07:00
|
|
|
case V4L2_PIX_FMT_NV12:
|
|
|
|
frame->linesize[0] = data->linesize;
|
|
|
|
frame->linesize[1] = data->linesize / 2;
|
|
|
|
plane_offsets[1] = data->linesize * data->height;
|
|
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_YVU420:
|
|
|
|
frame->linesize[0] = data->linesize;
|
|
|
|
frame->linesize[1] = data->linesize / 2;
|
|
|
|
frame->linesize[2] = data->linesize / 2;
|
|
|
|
plane_offsets[1] = data->linesize * data->height * 5 / 4;
|
|
|
|
plane_offsets[2] = data->linesize * data->height;
|
|
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_YUV420:
|
|
|
|
frame->linesize[0] = data->linesize;
|
|
|
|
frame->linesize[1] = data->linesize / 2;
|
|
|
|
frame->linesize[2] = data->linesize / 2;
|
|
|
|
plane_offsets[1] = data->linesize * data->height;
|
|
|
|
plane_offsets[2] = data->linesize * data->height * 5 / 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
frame->linesize[0] = data->linesize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 11:33:04 -07:00
|
|
|
/*
|
|
|
|
* Worker thread to get video data
|
|
|
|
*/
|
|
|
|
static void *v4l2_thread(void *vptr)
|
|
|
|
{
|
|
|
|
V4L2_DATA(vptr);
|
2014-07-26 06:42:45 -07:00
|
|
|
int r;
|
|
|
|
fd_set fds;
|
|
|
|
uint8_t *start;
|
|
|
|
struct timeval tv;
|
|
|
|
struct v4l2_buffer buf;
|
2014-08-02 01:04:45 -07:00
|
|
|
struct obs_source_frame out;
|
2014-07-26 06:42:45 -07:00
|
|
|
size_t plane_offsets[MAX_AV_PLANES];
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-08-10 08:33:03 -07:00
|
|
|
if (v4l2_start_capture(data->dev, &data->buffers) < 0)
|
2014-03-11 11:33:04 -07:00
|
|
|
goto exit;
|
|
|
|
|
2014-07-15 12:40:52 -07:00
|
|
|
data->frames = 0;
|
|
|
|
|
2014-07-26 06:42:45 -07:00
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(data->dev, &fds);
|
2014-07-24 15:17:28 -07:00
|
|
|
|
2014-07-26 06:42:45 -07:00
|
|
|
v4l2_prep_obs_frame(data, &out, plane_offsets);
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-07-26 06:42:45 -07:00
|
|
|
while (os_event_try(data->event) == EAGAIN) {
|
2014-03-11 11:33:04 -07:00
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
r = select(data->dev + 1, &fds, NULL, NULL, &tv);
|
|
|
|
if (r < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2014-07-15 11:51:13 -07:00
|
|
|
blog(LOG_DEBUG, "select failed");
|
2014-03-11 11:33:04 -07:00
|
|
|
break;
|
|
|
|
} else if (r == 0) {
|
2014-07-15 11:51:13 -07:00
|
|
|
blog(LOG_DEBUG, "select timeout");
|
2014-03-11 11:33:04 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
2014-07-23 13:34:32 -07:00
|
|
|
if (v4l2_ioctl(data->dev, VIDIOC_DQBUF, &buf) < 0) {
|
2014-03-11 11:33:04 -07:00
|
|
|
if (errno == EAGAIN)
|
|
|
|
continue;
|
2014-07-15 11:51:13 -07:00
|
|
|
blog(LOG_DEBUG, "failed to dequeue buffer");
|
2014-03-11 11:33:04 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.timestamp = timeval2ns(buf.timestamp);
|
2014-08-10 08:33:03 -07:00
|
|
|
start = (uint8_t *) data->buffers.info[buf.index].start;
|
2014-07-26 06:42:45 -07:00
|
|
|
for (uint_fast32_t i = 0; i < MAX_AV_PLANES; ++i)
|
|
|
|
out.data[i] = start + plane_offsets[i];
|
2014-03-11 11:33:04 -07:00
|
|
|
obs_source_output_video(data->source, &out);
|
|
|
|
|
2014-07-23 13:34:32 -07:00
|
|
|
if (v4l2_ioctl(data->dev, VIDIOC_QBUF, &buf) < 0) {
|
2014-07-15 11:51:13 -07:00
|
|
|
blog(LOG_DEBUG, "failed to enqueue buffer");
|
2014-03-11 11:33:04 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->frames++;
|
|
|
|
}
|
|
|
|
|
2014-07-26 07:50:06 -07:00
|
|
|
blog(LOG_INFO, "Stopped capture after %"PRIu64" frames", data->frames);
|
2014-07-15 12:40:52 -07:00
|
|
|
|
2014-03-11 11:33:04 -07:00
|
|
|
exit:
|
2014-08-10 08:33:03 -07:00
|
|
|
v4l2_stop_capture(data->dev);
|
2014-03-11 11:33:04 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-25 00:13:00 -07:00
|
|
|
static const char* v4l2_getname(void)
|
2014-03-11 11:33:04 -07:00
|
|
|
{
|
2014-07-09 22:12:57 -07:00
|
|
|
return obs_module_text("V4L2Input");
|
2014-03-11 11:33:04 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void v4l2_defaults(obs_data_t *settings)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
2014-09-15 14:59:05 -07:00
|
|
|
obs_data_set_default_int(settings, "input", -1);
|
|
|
|
obs_data_set_default_int(settings, "pixelformat", -1);
|
|
|
|
obs_data_set_default_int(settings, "resolution", -1);
|
|
|
|
obs_data_set_default_int(settings, "framerate", -1);
|
2014-05-25 02:04:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List available devices
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static void v4l2_device_list(obs_property_t *prop, obs_data_t *settings)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
2014-09-15 12:06:55 -07:00
|
|
|
UNUSED_PARAMETER(settings);
|
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
|
|
|
struct dstr device;
|
|
|
|
|
2014-07-15 11:29:14 -07:00
|
|
|
dirp = opendir("/sys/class/video4linux");
|
|
|
|
if (!dirp)
|
|
|
|
return;
|
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
obs_property_list_clear(prop);
|
2014-07-15 14:23:05 -07:00
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
dstr_init_copy(&device, "/dev/");
|
|
|
|
|
2014-07-15 11:29:14 -07:00
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
2014-09-15 11:35:07 -07:00
|
|
|
int fd;
|
|
|
|
uint32_t caps;
|
|
|
|
struct v4l2_capability video_cap;
|
|
|
|
|
2014-07-15 11:45:42 -07:00
|
|
|
if (dp->d_type == DT_DIR)
|
|
|
|
continue;
|
|
|
|
|
2014-07-15 11:29:14 -07:00
|
|
|
dstr_resize(&device, 5);
|
|
|
|
dstr_cat(&device, dp->d_name);
|
|
|
|
|
2014-07-23 13:34:32 -07:00
|
|
|
if ((fd = v4l2_open(device.array, O_RDWR | O_NONBLOCK)) == -1) {
|
2014-07-15 12:40:52 -07:00
|
|
|
blog(LOG_INFO, "Unable to open %s", device.array);
|
2014-07-15 11:29:14 -07:00
|
|
|
continue;
|
2014-07-15 12:40:52 -07:00
|
|
|
}
|
2014-07-15 11:29:14 -07:00
|
|
|
|
2014-07-23 13:34:32 -07:00
|
|
|
if (v4l2_ioctl(fd, VIDIOC_QUERYCAP, &video_cap) == -1) {
|
2014-07-15 12:40:52 -07:00
|
|
|
blog(LOG_INFO, "Failed to query capabilities for %s",
|
|
|
|
device.array);
|
2014-09-15 11:35:07 -07:00
|
|
|
close(fd);
|
|
|
|
continue;
|
2014-07-15 12:40:52 -07:00
|
|
|
}
|
2014-09-15 11:35:07 -07:00
|
|
|
|
|
|
|
caps = (video_cap.capabilities & V4L2_CAP_DEVICE_CAPS)
|
|
|
|
? video_cap.device_caps
|
|
|
|
: video_cap.capabilities;
|
|
|
|
|
|
|
|
if (!(caps & V4L2_CAP_VIDEO_CAPTURE)) {
|
2014-07-15 12:40:52 -07:00
|
|
|
blog(LOG_INFO, "%s seems to not support video capture",
|
|
|
|
device.array);
|
2014-09-15 11:35:07 -07:00
|
|
|
close(fd);
|
|
|
|
continue;
|
2014-05-25 02:04:17 -07:00
|
|
|
}
|
2014-07-15 11:45:42 -07:00
|
|
|
|
2014-09-15 11:35:07 -07:00
|
|
|
obs_property_list_add_string(prop, (char *) video_cap.card,
|
|
|
|
device.array);
|
|
|
|
blog(LOG_INFO, "Found device '%s' at %s", video_cap.card,
|
|
|
|
device.array);
|
|
|
|
|
2014-07-15 11:29:14 -07:00
|
|
|
close(fd);
|
2014-05-25 02:04:17 -07:00
|
|
|
}
|
2014-07-15 11:29:14 -07:00
|
|
|
|
|
|
|
closedir(dirp);
|
2014-05-25 02:04:17 -07:00
|
|
|
dstr_free(&device);
|
|
|
|
}
|
|
|
|
|
2014-08-05 13:03:59 -07:00
|
|
|
/*
|
|
|
|
* List inputs for device
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static void v4l2_input_list(int_fast32_t dev, obs_property_t *prop)
|
2014-08-05 13:03:59 -07:00
|
|
|
{
|
|
|
|
struct v4l2_input in;
|
|
|
|
memset(&in, 0, sizeof(in));
|
|
|
|
|
|
|
|
obs_property_list_clear(prop);
|
|
|
|
|
2014-09-15 14:59:05 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
|
|
|
|
|
2014-08-05 13:03:59 -07:00
|
|
|
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);
|
2014-09-15 14:59:41 -07:00
|
|
|
blog(LOG_INFO, "Found input '%s' (Index %d)", in.name,
|
|
|
|
in.index);
|
2014-08-05 13:03:59 -07:00
|
|
|
}
|
|
|
|
in.index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
/*
|
|
|
|
* List formats for device
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static void v4l2_format_list(int dev, obs_property_t *prop)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
|
|
|
struct v4l2_fmtdesc fmt;
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
fmt.index = 0;
|
2014-07-23 13:59:48 -07:00
|
|
|
struct dstr buffer;
|
|
|
|
dstr_init(&buffer);
|
2014-05-25 02:04:17 -07:00
|
|
|
|
|
|
|
obs_property_list_clear(prop);
|
|
|
|
|
2014-09-15 14:59:05 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
|
|
|
|
|
2014-07-23 13:34:32 -07:00
|
|
|
while (v4l2_ioctl(dev, VIDIOC_ENUM_FMT, &fmt) == 0) {
|
2014-07-23 13:59:48 -07:00
|
|
|
dstr_copy(&buffer, (char *) fmt.description);
|
|
|
|
if (fmt.flags & V4L2_FMT_FLAG_EMULATED)
|
|
|
|
dstr_cat(&buffer, " (Emulated)");
|
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
if (v4l2_to_obs_video_format(fmt.pixelformat)
|
|
|
|
!= VIDEO_FORMAT_NONE) {
|
2014-07-23 13:59:48 -07:00
|
|
|
obs_property_list_add_int(prop, buffer.array,
|
2014-05-25 02:04:17 -07:00
|
|
|
fmt.pixelformat);
|
2014-07-15 12:40:52 -07:00
|
|
|
blog(LOG_INFO, "Pixelformat: %s (available)",
|
2014-07-23 13:59:48 -07:00
|
|
|
buffer.array);
|
2014-07-15 12:40:52 -07:00
|
|
|
} else {
|
|
|
|
blog(LOG_INFO, "Pixelformat: %s (unavailable)",
|
2014-07-23 13:59:48 -07:00
|
|
|
buffer.array);
|
2014-05-25 02:04:17 -07:00
|
|
|
}
|
|
|
|
fmt.index++;
|
|
|
|
}
|
2014-07-23 13:59:48 -07:00
|
|
|
|
|
|
|
dstr_free(&buffer);
|
2014-05-25 02:04:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List resolutions for device and format
|
|
|
|
*/
|
|
|
|
static void v4l2_resolution_list(int dev, uint_fast32_t pixelformat,
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *prop)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
|
|
|
struct v4l2_frmsizeenum frmsize;
|
|
|
|
frmsize.pixel_format = pixelformat;
|
|
|
|
frmsize.index = 0;
|
|
|
|
struct dstr buffer;
|
|
|
|
dstr_init(&buffer);
|
|
|
|
|
|
|
|
obs_property_list_clear(prop);
|
|
|
|
|
2014-09-15 14:59:05 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
|
|
|
|
|
2014-07-23 13:34:32 -07:00
|
|
|
v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize);
|
2014-07-15 14:23:05 -07:00
|
|
|
|
|
|
|
switch(frmsize.type) {
|
|
|
|
case V4L2_FRMSIZE_TYPE_DISCRETE:
|
2014-07-23 13:34:32 -07:00
|
|
|
while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
|
2014-07-15 14:23:05 -07:00
|
|
|
dstr_printf(&buffer, "%dx%d", frmsize.discrete.width,
|
|
|
|
frmsize.discrete.height);
|
|
|
|
obs_property_list_add_int(prop, buffer.array,
|
2014-09-15 15:05:11 -07:00
|
|
|
v4l2_pack_tuple(frmsize.discrete.width,
|
2014-07-15 14:23:05 -07:00
|
|
|
frmsize.discrete.height));
|
|
|
|
frmsize.index++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-07-15 12:40:52 -07:00
|
|
|
blog(LOG_INFO, "Stepwise and Continuous framesizes "
|
2014-07-15 14:23:05 -07:00
|
|
|
"are currently hardcoded");
|
|
|
|
|
2014-08-10 08:33:03 -07:00
|
|
|
for (const int *packed = v4l2_framesizes; *packed; ++packed) {
|
2014-07-15 14:23:05 -07:00
|
|
|
int width;
|
|
|
|
int height;
|
2014-09-15 15:05:11 -07:00
|
|
|
v4l2_unpack_tuple(&width, &height, *packed);
|
2014-07-15 14:23:05 -07:00
|
|
|
dstr_printf(&buffer, "%dx%d", width, height);
|
2014-08-02 09:15:49 -07:00
|
|
|
obs_property_list_add_int(prop, buffer.array, *packed);
|
2014-07-15 14:23:05 -07:00
|
|
|
}
|
|
|
|
break;
|
2014-07-15 12:40:52 -07:00
|
|
|
}
|
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
dstr_free(&buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List framerates for device and resolution
|
|
|
|
*/
|
|
|
|
static void v4l2_framerate_list(int dev, uint_fast32_t pixelformat,
|
2014-09-25 17:44:05 -07:00
|
|
|
uint_fast32_t width, uint_fast32_t height, obs_property_t *prop)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
|
|
|
struct v4l2_frmivalenum frmival;
|
|
|
|
frmival.pixel_format = pixelformat;
|
|
|
|
frmival.width = width;
|
|
|
|
frmival.height = height;
|
|
|
|
frmival.index = 0;
|
|
|
|
struct dstr buffer;
|
|
|
|
dstr_init(&buffer);
|
|
|
|
|
|
|
|
obs_property_list_clear(prop);
|
|
|
|
|
2014-09-15 14:59:05 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("LeaveUnchanged"), -1);
|
|
|
|
|
2014-07-23 13:34:32 -07:00
|
|
|
v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS, &frmival);
|
2014-07-15 14:23:05 -07:00
|
|
|
|
|
|
|
switch(frmival.type) {
|
|
|
|
case V4L2_FRMIVAL_TYPE_DISCRETE:
|
2014-07-23 13:34:32 -07:00
|
|
|
while (v4l2_ioctl(dev, VIDIOC_ENUM_FRAMEINTERVALS,
|
|
|
|
&frmival) == 0) {
|
2014-07-15 14:23:05 -07:00
|
|
|
float fps = (float) frmival.discrete.denominator /
|
|
|
|
frmival.discrete.numerator;
|
2014-09-15 15:05:11 -07:00
|
|
|
int pack = v4l2_pack_tuple(frmival.discrete.numerator,
|
2014-07-15 14:23:05 -07:00
|
|
|
frmival.discrete.denominator);
|
|
|
|
dstr_printf(&buffer, "%.2f", fps);
|
|
|
|
obs_property_list_add_int(prop, buffer.array, pack);
|
|
|
|
frmival.index++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-07-15 12:40:52 -07:00
|
|
|
blog(LOG_INFO, "Stepwise and Continuous framerates "
|
2014-07-15 14:23:05 -07:00
|
|
|
"are currently hardcoded");
|
2014-08-02 09:15:49 -07:00
|
|
|
|
2014-08-10 08:33:03 -07:00
|
|
|
for (const int *packed = v4l2_framerates; *packed; ++packed) {
|
2014-07-15 14:23:05 -07:00
|
|
|
int num;
|
|
|
|
int denom;
|
2014-09-15 15:05:11 -07:00
|
|
|
v4l2_unpack_tuple(&num, &denom, *packed);
|
2014-07-15 14:23:05 -07:00
|
|
|
float fps = (float) denom / num;
|
|
|
|
dstr_printf(&buffer, "%.2f", fps);
|
2014-08-02 09:15:49 -07:00
|
|
|
obs_property_list_add_int(prop, buffer.array, *packed);
|
2014-07-15 14:23:05 -07:00
|
|
|
}
|
|
|
|
break;
|
2014-07-15 12:40:52 -07:00
|
|
|
}
|
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
dstr_free(&buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Device selected callback
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static bool device_selected(obs_properties_t *props, obs_property_t *p,
|
|
|
|
obs_data_t *settings)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(p);
|
2014-08-05 11:09:29 -07:00
|
|
|
int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
|
2014-05-25 02:04:17 -07:00
|
|
|
O_RDWR | O_NONBLOCK);
|
2014-07-11 08:48:12 -07:00
|
|
|
if (dev == -1)
|
|
|
|
return false;
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *prop = obs_properties_get(props, "input");
|
2014-08-05 13:03:59 -07:00
|
|
|
v4l2_input_list(dev, prop);
|
|
|
|
obs_property_modified(prop, settings);
|
|
|
|
v4l2_close(dev);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input selected callback
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static bool input_selected(obs_properties_t *props, obs_property_t *p,
|
|
|
|
obs_data_t *settings)
|
2014-08-05 13:03:59 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(p);
|
|
|
|
int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
|
|
|
|
O_RDWR | O_NONBLOCK);
|
|
|
|
if (dev == -1)
|
|
|
|
return false;
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *prop = obs_properties_get(props, "pixelformat");
|
2014-05-25 02:04:17 -07:00
|
|
|
v4l2_format_list(dev, prop);
|
|
|
|
obs_property_modified(prop, settings);
|
2014-07-23 13:34:32 -07:00
|
|
|
v4l2_close(dev);
|
2014-05-25 02:04:17 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Format selected callback
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static bool format_selected(obs_properties_t *props, obs_property_t *p,
|
|
|
|
obs_data_t *settings)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(p);
|
2014-08-05 11:09:29 -07:00
|
|
|
int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
|
2014-05-25 02:04:17 -07:00
|
|
|
O_RDWR | O_NONBLOCK);
|
2014-07-11 08:48:12 -07:00
|
|
|
if (dev == -1)
|
|
|
|
return false;
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *prop = obs_properties_get(props, "resolution");
|
2014-08-05 11:09:29 -07:00
|
|
|
v4l2_resolution_list(dev, obs_data_get_int(settings, "pixelformat"),
|
2014-05-25 02:04:17 -07:00
|
|
|
prop);
|
|
|
|
obs_property_modified(prop, settings);
|
2014-07-23 13:34:32 -07:00
|
|
|
v4l2_close(dev);
|
2014-05-25 02:04:17 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Resolution selected callback
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static bool resolution_selected(obs_properties_t *props, obs_property_t *p,
|
|
|
|
obs_data_t *settings)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(p);
|
|
|
|
int width, height;
|
2014-08-05 11:09:29 -07:00
|
|
|
int dev = v4l2_open(obs_data_get_string(settings, "device_id"),
|
2014-05-25 02:04:17 -07:00
|
|
|
O_RDWR | O_NONBLOCK);
|
2014-07-11 08:48:12 -07:00
|
|
|
if (dev == -1)
|
|
|
|
return false;
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *prop = obs_properties_get(props, "framerate");
|
2014-09-15 15:05:11 -07:00
|
|
|
v4l2_unpack_tuple(&width, &height, obs_data_get_int(settings,
|
2014-05-25 02:04:17 -07:00
|
|
|
"resolution"));
|
2014-08-05 11:09:29 -07:00
|
|
|
v4l2_framerate_list(dev, obs_data_get_int(settings, "pixelformat"),
|
2014-05-25 02:04:17 -07:00
|
|
|
width, height, prop);
|
|
|
|
obs_property_modified(prop, settings);
|
2014-07-23 13:34:32 -07:00
|
|
|
v4l2_close(dev);
|
2014-05-25 02:04:17 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static obs_properties_t *v4l2_properties(void)
|
2014-05-25 02:04:17 -07:00
|
|
|
{
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_properties_t *props = obs_properties_create();
|
2014-07-09 22:12:57 -07:00
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *device_list = obs_properties_add_list(props,
|
2014-07-09 22:12:57 -07:00
|
|
|
"device_id", obs_module_text("Device"),
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *input_list = obs_properties_add_list(props,
|
2014-08-05 13:03:59 -07:00
|
|
|
"input", obs_module_text("Input"),
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *format_list = obs_properties_add_list(props,
|
2014-07-09 22:12:57 -07:00
|
|
|
"pixelformat", obs_module_text("VideoFormat"),
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
obs_property_t *resolution_list = obs_properties_add_list(props,
|
2014-07-09 22:12:57 -07:00
|
|
|
"resolution", obs_module_text("Resolution"),
|
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
|
|
|
|
|
|
|
obs_properties_add_list(props,
|
|
|
|
"framerate", obs_module_text("FrameRate"),
|
2014-05-25 02:04:17 -07:00
|
|
|
OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
|
2014-07-09 22:12:57 -07:00
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
v4l2_device_list(device_list, NULL);
|
|
|
|
obs_property_set_modified_callback(device_list, device_selected);
|
2014-08-05 13:03:59 -07:00
|
|
|
obs_property_set_modified_callback(input_list, input_selected);
|
2014-05-25 02:04:17 -07:00
|
|
|
obs_property_set_modified_callback(format_list, format_selected);
|
|
|
|
obs_property_set_modified_callback(resolution_list,
|
|
|
|
resolution_selected);
|
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void v4l2_terminate(struct v4l2_data *data)
|
|
|
|
{
|
2014-03-11 11:33:04 -07:00
|
|
|
if (data->thread) {
|
|
|
|
os_event_signal(data->event);
|
|
|
|
pthread_join(data->thread, NULL);
|
2014-05-25 02:04:17 -07:00
|
|
|
os_event_destroy(data->event);
|
2014-08-11 14:27:46 -07:00
|
|
|
data->thread = 0;
|
2014-03-11 11:33:04 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:33:03 -07:00
|
|
|
v4l2_destroy_mmap(&data->buffers);
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
if (data->dev != -1) {
|
2014-07-23 13:34:32 -07:00
|
|
|
v4l2_close(data->dev);
|
2014-05-25 02:04:17 -07:00
|
|
|
data->dev = -1;
|
|
|
|
}
|
|
|
|
}
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
static void v4l2_destroy(void *vptr)
|
|
|
|
{
|
|
|
|
V4L2_DATA(vptr);
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
return;
|
|
|
|
|
|
|
|
v4l2_terminate(data);
|
|
|
|
|
2014-09-16 12:23:26 -07:00
|
|
|
if (data->device_id)
|
|
|
|
bfree(data->device_id);
|
2014-03-11 11:33:04 -07:00
|
|
|
bfree(data);
|
|
|
|
}
|
|
|
|
|
2014-07-26 08:26:43 -07:00
|
|
|
/**
|
|
|
|
* Initialize the v4l2 device
|
|
|
|
*
|
|
|
|
* This function:
|
|
|
|
* - tries to open the device
|
|
|
|
* - sets pixelformat and requested resolution
|
|
|
|
* - sets the requested framerate
|
|
|
|
* - maps the buffers
|
|
|
|
* - starts the capture thread
|
|
|
|
*/
|
2014-05-25 02:04:17 -07:00
|
|
|
static void v4l2_init(struct v4l2_data *data)
|
2014-03-11 11:33:04 -07:00
|
|
|
{
|
2014-07-26 07:50:06 -07:00
|
|
|
int fps_num, fps_denom;
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-09-16 12:23:26 -07:00
|
|
|
blog(LOG_INFO, "Start capture from %s", data->device_id);
|
|
|
|
data->dev = v4l2_open(data->device_id, O_RDWR | O_NONBLOCK);
|
2014-03-11 11:33:04 -07:00
|
|
|
if (data->dev == -1) {
|
2014-07-26 08:26:43 -07:00
|
|
|
blog(LOG_ERROR, "Unable to open device");
|
2014-03-11 11:33:04 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-08-05 13:03:59 -07:00
|
|
|
/* set input */
|
2014-09-16 12:23:26 -07:00
|
|
|
if (v4l2_set_input(data->dev, &data->input) < 0) {
|
|
|
|
blog(LOG_ERROR, "Unable to set input %d", data->input);
|
2014-08-05 13:03:59 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-09-16 12:23:26 -07:00
|
|
|
blog(LOG_INFO, "Input: %d", data->input);
|
2014-08-05 13:03:59 -07:00
|
|
|
|
2014-07-26 08:26:43 -07:00
|
|
|
/* set pixel format and resolution */
|
2014-09-16 12:23:26 -07:00
|
|
|
if (v4l2_set_format(data->dev, &data->resolution, &data->pixfmt,
|
2014-09-15 15:05:11 -07:00
|
|
|
&data->linesize) < 0) {
|
2014-07-26 08:26:43 -07:00
|
|
|
blog(LOG_ERROR, "Unable to set format");
|
2014-03-11 11:33:04 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-09-16 12:45:23 -07:00
|
|
|
if (v4l2_to_obs_video_format(data->pixfmt) == VIDEO_FORMAT_NONE) {
|
|
|
|
blog(LOG_ERROR, "Selected video format not supported");
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-09-16 12:23:26 -07:00
|
|
|
v4l2_unpack_tuple(&data->width, &data->height, data->resolution);
|
2014-09-15 15:05:11 -07:00
|
|
|
blog(LOG_INFO, "Resolution: %dx%d", data->width, data->height);
|
2014-09-16 12:23:26 -07:00
|
|
|
blog(LOG_INFO, "Pixelformat: %d", data->pixfmt);
|
2014-09-15 15:05:11 -07:00
|
|
|
blog(LOG_INFO, "Linesize: %d Bytes", data->linesize);
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-07-26 08:26:43 -07:00
|
|
|
/* set framerate */
|
2014-09-16 12:23:26 -07:00
|
|
|
if (v4l2_set_framerate(data->dev, &data->framerate) < 0) {
|
2014-07-26 08:26:43 -07:00
|
|
|
blog(LOG_ERROR, "Unable to set framerate");
|
2014-03-11 11:33:04 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-09-16 12:23:26 -07:00
|
|
|
v4l2_unpack_tuple(&fps_num, &fps_denom, data->framerate);
|
2014-09-16 11:35:09 -07:00
|
|
|
blog(LOG_INFO, "Framerate: %.2f fps", (float) fps_denom / fps_num);
|
2014-07-26 08:26:43 -07:00
|
|
|
|
|
|
|
/* map buffers */
|
2014-08-10 08:33:03 -07:00
|
|
|
if (v4l2_create_mmap(data->dev, &data->buffers) < 0) {
|
2014-07-26 08:26:43 -07:00
|
|
|
blog(LOG_ERROR, "Failed to map buffers");
|
2014-03-11 11:33:04 -07:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-07-26 08:26:43 -07:00
|
|
|
/* start the capture thread */
|
2014-03-11 11:33:04 -07:00
|
|
|
if (os_event_init(&data->event, OS_EVENT_TYPE_MANUAL) != 0)
|
|
|
|
goto fail;
|
|
|
|
if (pthread_create(&data->thread, NULL, v4l2_thread, data) != 0)
|
|
|
|
goto fail;
|
2014-05-25 02:04:17 -07:00
|
|
|
return;
|
2014-03-11 11:33:04 -07:00
|
|
|
fail:
|
2014-07-26 08:26:43 -07:00
|
|
|
blog(LOG_ERROR, "Initialization failed");
|
2014-05-25 02:04:17 -07:00
|
|
|
v4l2_terminate(data);
|
2014-03-11 11:33:04 -07:00
|
|
|
}
|
|
|
|
|
2014-09-16 11:46:04 -07:00
|
|
|
/**
|
|
|
|
* Update the settings for the v4l2 source
|
|
|
|
*
|
|
|
|
* Since there are very few settings that can be changed without restarting the
|
|
|
|
* stream we don't bother to even try. Whenever this is called the currently
|
|
|
|
* active stream (if exists) is stopped, the settings are updated and finally
|
|
|
|
* the new stream is started.
|
|
|
|
*/
|
2014-09-25 17:44:05 -07:00
|
|
|
static void v4l2_update(void *vptr, obs_data_t *settings)
|
2014-03-11 11:33:04 -07:00
|
|
|
{
|
|
|
|
V4L2_DATA(vptr);
|
2014-05-25 02:04:17 -07:00
|
|
|
|
2014-09-16 11:46:04 -07:00
|
|
|
v4l2_terminate(data);
|
2014-05-25 02:04:17 -07:00
|
|
|
|
2014-09-16 12:23:26 -07:00
|
|
|
if (data->device_id)
|
|
|
|
bfree(data->device_id);
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-09-16 12:23:26 -07:00
|
|
|
data->device_id = bstrdup(obs_data_get_string(settings, "device_id"));
|
|
|
|
data->input = obs_data_get_int(settings, "input");
|
|
|
|
data->pixfmt = obs_data_get_int(settings, "pixelformat");
|
|
|
|
data->resolution = obs_data_get_int(settings, "resolution");
|
|
|
|
data->framerate = obs_data_get_int(settings, "framerate");
|
2014-05-25 02:04:17 -07:00
|
|
|
|
2014-09-16 11:46:04 -07:00
|
|
|
v4l2_init(data);
|
2014-03-11 11:33:04 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static void *v4l2_create(obs_data_t *settings, obs_source_t *source)
|
2014-03-11 11:33:04 -07:00
|
|
|
{
|
2014-05-25 02:04:17 -07:00
|
|
|
struct v4l2_data *data = bzalloc(sizeof(struct v4l2_data));
|
|
|
|
data->dev = -1;
|
|
|
|
data->source = source;
|
|
|
|
|
|
|
|
v4l2_update(data, settings);
|
2014-03-11 11:33:04 -07:00
|
|
|
|
2014-05-25 02:04:17 -07:00
|
|
|
return data;
|
2014-03-11 11:33:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct obs_source_info v4l2_input = {
|
2014-08-04 21:27:52 -07:00
|
|
|
.id = "v4l2_input",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_ASYNC_VIDEO,
|
|
|
|
.get_name = v4l2_getname,
|
|
|
|
.create = v4l2_create,
|
|
|
|
.destroy = v4l2_destroy,
|
|
|
|
.update = v4l2_update,
|
|
|
|
.get_defaults = v4l2_defaults,
|
|
|
|
.get_properties = v4l2_properties
|
2014-03-11 11:33:04 -07:00
|
|
|
};
|