1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
/**
* @file v4l2.c Video4Linux2 video-source
*
* Copyright (C) 2010 Creytiv.com
*/
#define _DEFAULT_SOURCE 1
#define _BSD_SOURCE 1
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#undef __STRICT_ANSI__ /* needed for RHEL4 kernel 2.6.9 */
#include <pthread.h>
#include <re.h>
#include <rem.h>
#include <baresip.h>
#if defined (OPENBSD) || defined (NETBSD)
#include <sys/videoio.h>
#else
#include <linux/videodev2.h>
#endif
#ifdef HAVE_LIBV4L2
#include <libv4l2.h>
#else
#define v4l2_open open
#define v4l2_read read
#define v4l2_ioctl ioctl
#define v4l2_mmap mmap
#define v4l2_munmap munmap
#define v4l2_close close
#endif
/**
* @defgroup v4l2 v4l2
*
* V4L2 (Video for Linux 2) video-source module
*/
struct buffer {
void *start;
size_t length;
};
struct vidsrc_st {
const struct vidsrc *vs; /* inheritance */
int fd;
pthread_t thread;
bool run;
struct vidsz sz;
u_int32_t pixfmt;
struct buffer *buffers;
unsigned int n_buffers;
vidsrc_frame_h *frameh;
void *arg;
};
static struct vidsrc *vidsrc;
static enum vidfmt match_fmt(u_int32_t fmt)
{
switch (fmt) {
case V4L2_PIX_FMT_YUV420: return VID_FMT_YUV420P;
case V4L2_PIX_FMT_YUYV: return VID_FMT_YUYV422;
case V4L2_PIX_FMT_UYVY: return VID_FMT_UYVY422;
case V4L2_PIX_FMT_RGB32: return VID_FMT_RGB32;
case V4L2_PIX_FMT_RGB565: return VID_FMT_RGB565;
case V4L2_PIX_FMT_RGB555: return VID_FMT_RGB555;
case V4L2_PIX_FMT_NV12: return VID_FMT_NV12;
case V4L2_PIX_FMT_NV21: return VID_FMT_NV21;
default: return VID_FMT_N;
}
}
static void print_video_input(struct vidsrc_st *st)
{
struct v4l2_input input;
memset(&input, 0, sizeof(input));
#ifndef OPENBSD
if (-1 == v4l2_ioctl(st->fd, VIDIOC_G_INPUT, &input.index)) {
warning("v4l2: VIDIOC_G_INPUT: %m\n", errno);
return;
}
#endif
if (-1 == v4l2_ioctl(st->fd, VIDIOC_ENUMINPUT, &input)) {
warning("v4l2: VIDIOC_ENUMINPUT: %m\n", errno);
return;
}
info("v4l2: Current input: \"%s\"\n", input.name);
}
static int xioctl(int fd, unsigned long int request, void *arg)
{
int r;
do {
r = v4l2_ioctl(fd, request, arg);
}
while (-1 == r && EINTR == errno);
return r;
}
static int init_mmap(struct vidsrc_st *st, const char *dev_name)
{
struct v4l2_requestbuffers req;
memset(&req, 0, sizeof(req));
req.count = 4;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(st->fd, VIDIOC_REQBUFS, &req)) {
if (EINVAL == errno) {
warning("v4l2: %s does not support "
"memory mapping\n", dev_name);
return errno;
}
else {
return errno;
}
}
if (req.count < 2) {
warning("v4l2: Insufficient buffer memory on %s\n", dev_name);
return ENOMEM;
}
st->buffers = mem_zalloc(req.count * sizeof(*st->buffers), NULL);
if (!st->buffers)
return ENOMEM;
for (st->n_buffers = 0; st->n_buffers<req.count; ++st->n_buffers) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = st->n_buffers;
if (-1 == xioctl(st->fd, VIDIOC_QUERYBUF, &buf)) {
warning("v4l2: VIDIOC_QUERYBUF\n");
return errno;
}
st->buffers[st->n_buffers].length = buf.length;
st->buffers[st->n_buffers].start =
v4l2_mmap(NULL /* start anywhere */,
buf.length,
PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */,
st->fd, buf.m.offset);
if (MAP_FAILED == st->buffers[st->n_buffers].start) {
warning("v4l2: mmap failed\n");
return ENODEV;
}
}
return 0;
}
static int v4l2_init_device(struct vidsrc_st *st, const char *dev_name,
int width, int height)
{
struct v4l2_capability cap;
struct v4l2_format fmt;
struct v4l2_fmtdesc fmts;
unsigned int min;
const char *pix;
int err;
if (-1 == xioctl(st->fd, VIDIOC_QUERYCAP, &cap)) {
if (EINVAL == errno) {
warning("v4l2: %s is no V4L2 device\n", dev_name);
return ENODEV;
}
else {
warning("v4l2: VIDIOC_QUERYCAP: %m\n", errno);
return errno;
}
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
warning("v4l2: %s is no video capture device\n", dev_name);
return ENODEV;
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
warning("v4l2: %s does not support streaming i/o\n",
dev_name);
return ENOSYS;
}
/* Negotiate video format */
memset(&fmts, 0, sizeof(fmts));
fmts.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
for (fmts.index=0; !v4l2_ioctl(st->fd, VIDIOC_ENUM_FMT, &fmts);
fmts.index++) {
if (match_fmt(fmts.pixelformat) != VID_FMT_N) {
st->pixfmt = fmts.pixelformat;
break;
}
}
if (!st->pixfmt) {
warning("v4l2: format negotiation failed: %m\n", errno);
return errno;
}
/* Select video input, video standard and tune here. */
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = width;
fmt.fmt.pix.height = height;
fmt.fmt.pix.pixelformat = st->pixfmt;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
if (-1 == xioctl(st->fd, VIDIOC_S_FMT, &fmt)) {
warning("v4l2: VIDIOC_S_FMT: %m\n", errno);
return errno;
}
/* Note VIDIOC_S_FMT may change width and height. */
/* Buggy driver paranoia. */
min = fmt.fmt.pix.width * 2;
if (fmt.fmt.pix.bytesperline < min)
fmt.fmt.pix.bytesperline = min;
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
if (fmt.fmt.pix.sizeimage < min)
fmt.fmt.pix.sizeimage = min;
st->sz.w = fmt.fmt.pix.width;
st->sz.h = fmt.fmt.pix.height;
err = init_mmap(st, dev_name);
if (err)
return err;
pix = (char *)&fmt.fmt.pix.pixelformat;
if (st->pixfmt != fmt.fmt.pix.pixelformat) {
warning("v4l2: %s: unexpectedly got %c%c%c%c\n", dev_name,
pix[0], pix[1], pix[2], pix[3]);
return ENODEV;
}
info("v4l2: %s: found valid V4L2 device (%u x %u) pixfmt=%c%c%c%c\n",
dev_name, fmt.fmt.pix.width, fmt.fmt.pix.height,
pix[0], pix[1], pix[2], pix[3]);
return 0;
}
static void stop_capturing(struct vidsrc_st *st)
{
enum v4l2_buf_type type;
if (st->fd < 0)
return;
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
xioctl(st->fd, VIDIOC_STREAMOFF, &type);
}
static void uninit_device(struct vidsrc_st *st)
{
unsigned int i;
for (i=0; i<st->n_buffers; ++i) {
v4l2_munmap(st->buffers[i].start, st->buffers[i].length);
}
st->buffers = mem_deref(st->buffers);
st->n_buffers = 0;
}
static int start_capturing(struct vidsrc_st *st)
{
unsigned int i;
enum v4l2_buf_type type;
for (i = 0; i < st->n_buffers; ++i) {
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl (st->fd, VIDIOC_QBUF, &buf))
return errno;
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl (st->fd, VIDIOC_STREAMON, &type))
return errno;
return 0;
}
static void call_frame_handler(struct vidsrc_st *st, uint8_t *buf)
{
struct vidframe frame;
vidframe_init_buf(&frame, match_fmt(st->pixfmt), &st->sz, buf);
st->frameh(&frame, st->arg);
}
static int read_frame(struct vidsrc_st *st)
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl (st->fd, VIDIOC_DQBUF, &buf)) {
switch (errno) {
case EAGAIN:
return 0;
case EIO:
/* Could ignore EIO, see spec. */
/* fall through */
default:
warning("v4l2: VIDIOC_DQBUF: %m\n", errno);
return errno;
}
}
if (buf.index >= st->n_buffers) {
warning("v4l2: index >= n_buffers\n");
}
call_frame_handler(st, st->buffers[buf.index].start);
if (-1 == xioctl (st->fd, VIDIOC_QBUF, &buf)) {
warning("v4l2: VIDIOC_QBUF\n");
return errno;
}
return 0;
}
static int vd_open(struct vidsrc_st *st, const char *device)
{
st->fd = v4l2_open(device, O_RDWR);
if (st->fd < 0) {
warning("v4l2: open %s: %m\n", device, errno);
return errno;
}
return 0;
}
static void destructor(void *arg)
{
struct vidsrc_st *st = arg;
if (st->run) {
st->run = false;
pthread_join(st->thread, NULL);
}
stop_capturing(st);
uninit_device(st);
if (st->fd >= 0)
v4l2_close(st->fd);
}
static void *read_thread(void *arg)
{
struct vidsrc_st *st = arg;
int err;
while (st->run) {
err = read_frame(st);
if (err) {
warning("v4l2: read_frame: %m\n", err);
}
}
return NULL;
}
static int alloc(struct vidsrc_st **stp, const struct vidsrc *vs,
struct media_ctx **ctx, struct vidsrc_prm *prm,
const struct vidsz *size, const char *fmt,
const char *dev, vidsrc_frame_h *frameh,
vidsrc_error_h *errorh, void *arg)
{
struct vidsrc_st *st;
int err;
(void)ctx;
(void)prm;
(void)fmt;
(void)errorh;
if (!stp || !size || !frameh)
return EINVAL;
if (!str_isset(dev))
dev = "/dev/video0";
st = mem_zalloc(sizeof(*st), destructor);
if (!st)
return ENOMEM;
st->vs = vs;
st->fd = -1;
st->sz = *size;
st->frameh = frameh;
st->arg = arg;
st->pixfmt = 0;
err = vd_open(st, dev);
if (err)
goto out;
err = v4l2_init_device(st, dev, size->w, size->h);
if (err)
goto out;
print_video_input(st);
err = start_capturing(st);
if (err)
goto out;
st->run = true;
err = pthread_create(&st->thread, NULL, read_thread, st);
if (err) {
st->run = false;
goto out;
}
out:
if (err)
mem_deref(st);
else
*stp = st;
return err;
}
static int v4l_init(void)
{
return vidsrc_register(&vidsrc, "v4l2", alloc, NULL);
}
static int v4l_close(void)
{
vidsrc = mem_deref(vidsrc);
return 0;
}
EXPORT_SYM const struct mod_export DECL_EXPORTS(v4l2) = {
"v4l2",
"vidsrc",
v4l_init,
v4l_close
};
|