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 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
/* Ruby Video4Linux Extension Library
* Copyright (C) 2004 Paulo Matias
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ruby.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/videodev.h>
typedef enum {
IO_METHOD_READ,
IO_METHOD_MMAP,
} io_method;
typedef struct _v4ldata
{
struct video_capability vid_cap;
struct video_mbuf vid_buf;
struct video_mmap vid_mmap;
struct video_window vid_win;
struct video_channel vid_chan;
struct video_picture vid_pic;
struct video_tuner vid_tuner;
unsigned char *map;
int fd;
io_method io;
} V4ldata;
VALUE cV4l;
static void v4l_free(V4ldata *ptr)
{
if(!ptr) return;
if(ptr->io == IO_METHOD_MMAP)
munmap(ptr->map, ptr->vid_buf.size);
close(ptr->fd);
free(ptr);
}
static void v4l_alloc_map(V4ldata *ptr)
{
unsigned long imgsz = ptr->vid_win.width * ptr->vid_win.height * 3;
if(ptr->io != IO_METHOD_READ)
return;
if((unsigned char *)-1 != (unsigned char *)ptr->map)
free(ptr->map);
if(!(ptr->map = (unsigned char *)malloc(imgsz)))
rb_raise(rb_eException, "Out of memory!");
}
VALUE v4l_new(VALUE class, VALUE device)
{
VALUE argv[1], tdata;
V4ldata *ptr = ALLOC(V4ldata);
char *sdevice = STR2CSTR(device);
ptr->fd = open(sdevice, O_RDWR /* required */ | O_NONBLOCK, 0);
if(ptr->fd == -1)
rb_raise(rb_eException, "Couldn't open %s.", sdevice);
if(ioctl(ptr->fd, VIDIOCGCAP, &ptr->vid_cap) == -1)
{
perror("VIDIOCGCAP");
rb_raise(rb_eException, "VIDIOCGCAP");
}
if(!(ptr->vid_cap.type & VID_TYPE_CAPTURE))
rb_raise(rb_eException, "%s is no video capture device.", sdevice);
ptr->map = (unsigned char *)-1;
if(ioctl(ptr->fd, VIDIOCGMBUF, &ptr->vid_buf) == -1)
ptr->io = IO_METHOD_READ;
else
{
ptr->io = IO_METHOD_MMAP;
ptr->map = (unsigned char *)mmap(0, ptr->vid_buf.size, PROT_READ|PROT_WRITE, MAP_SHARED, ptr->fd, 0);
if((unsigned char *)-1 == (unsigned char *)ptr->map)
{
perror("mmap()");
rb_warn("mmap() failed: falling back to read() method");
ptr->io = IO_METHOD_READ;
}
}
if(ioctl(ptr->fd, VIDIOCGWIN, &ptr->vid_win) == -1)
{
perror("VIDIOCGWIN");
rb_raise(rb_eException, "VIDIOCGWIN");
}
v4l_alloc_map(ptr);
if(ioctl(ptr->fd, VIDIOCGCHAN, &(ptr->vid_chan)) == -1)
{
perror("VIDIOCGCHAN");
rb_raise(rb_eException, "VIDIOCGCHAN");
}
ptr->vid_chan.channel = 0;
ptr->vid_chan.norm = VIDEO_MODE_NTSC;
if(ioctl(ptr->fd, VIDIOCSCHAN, &(ptr->vid_chan)) == -1)
{
perror("VIDIOCSCHAN");
rb_raise(rb_eException, "VIDIOCSCHAN");
}
ptr->vid_mmap.format = VIDEO_PALETTE_RGB24;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
ptr->vid_pic.palette = VIDEO_PALETTE_RGB24;
if(ioctl(ptr->fd, VIDIOCSPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCSPICT");
rb_raise(rb_eException, "VIDIOCSPICT");
}
if(ioctl(ptr->fd, VIDIOCGTUNER, &(ptr->vid_tuner)) == -1)
perror("VIDIOCGTUNER");
ptr->vid_tuner.mode = VIDEO_MODE_NTSC;
if(ioctl(ptr->fd, VIDIOCSTUNER, &(ptr->vid_tuner)) == -1)
perror("VIDIOCSTUNER");
tdata = Data_Wrap_Struct(class, 0, v4l_free, ptr);
rb_obj_call_init(tdata, 0, argv);
return tdata;
}
VALUE v4l_get_name(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
return rb_str_new2(ptr->vid_cap.name);
}
VALUE v4l_get_width(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGWIN, &ptr->vid_win) == -1)
{
perror("VIDIOCGWIN");
rb_raise(rb_eException, "VIDIOCGWIN");
}
return INT2NUM(ptr->vid_win.width);
}
VALUE v4l_set_width(VALUE self, VALUE width)
{
V4ldata *ptr;
int iwidth = NUM2INT(width);
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(iwidth > ptr->vid_cap.maxwidth)
iwidth = ptr->vid_cap.maxwidth;
if(iwidth < ptr->vid_cap.minwidth)
iwidth = ptr->vid_cap.minwidth;
if(ioctl(ptr->fd, VIDIOCGWIN, &ptr->vid_win) == -1)
{
perror("VIDIOCGWIN");
rb_raise(rb_eException, "VIDIOCGWIN");
}
ptr->vid_win.width = iwidth;
if(ioctl(ptr->fd, VIDIOCSWIN, &ptr->vid_win) == -1) {
perror("VIDIOCSWIN");
rb_raise(rb_eException, "VIDIOCSWIN");
}
v4l_alloc_map(ptr);
return self;
}
VALUE v4l_get_height(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGWIN, &ptr->vid_win) == -1)
{
perror("VIDIOCGWIN");
rb_raise(rb_eException, "VIDIOCGWIN");
}
return INT2NUM(ptr->vid_win.height);
}
VALUE v4l_set_height(VALUE self, VALUE height)
{
V4ldata *ptr;
int iheight = NUM2INT(height);
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(iheight > ptr->vid_cap.maxheight)
iheight = ptr->vid_cap.maxheight;
if(iheight < ptr->vid_cap.minheight)
iheight = ptr->vid_cap.minheight;
if(ioctl(ptr->fd, VIDIOCGWIN, &ptr->vid_win) == -1)
{
perror("VIDIOCGWIN");
rb_raise(rb_eException, "VIDIOCGWIN");
}
ptr->vid_win.height = iheight;
if(ioctl(ptr->fd, VIDIOCSWIN, &ptr->vid_win) == -1) {
perror("VIDIOCSWIN");
rb_raise(rb_eException, "VIDIOCSWIN");
}
v4l_alloc_map(ptr);
return self;
}
VALUE v4l_set_size(VALUE self, VALUE width, VALUE height)
{
V4ldata *ptr;
int iwidth = NUM2INT(width);
int iheight = NUM2INT(height);
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(iwidth > ptr->vid_cap.maxwidth)
iwidth = ptr->vid_cap.maxwidth;
if(iwidth < ptr->vid_cap.minwidth)
iwidth = ptr->vid_cap.minwidth;
if(iheight > ptr->vid_cap.maxheight)
iheight = ptr->vid_cap.maxheight;
if(iheight < ptr->vid_cap.minheight)
iheight = ptr->vid_cap.minheight;
if(ioctl(ptr->fd, VIDIOCGWIN, &(ptr->vid_win)) == -1)
{
perror("VIDIOCGWIN");
rb_raise(rb_eException, "VIDIOCGWIN");
}
ptr->vid_win.width = iwidth;
ptr->vid_win.height = iheight;
if(ioctl(ptr->fd, VIDIOCSWIN, &(ptr->vid_win)) == -1) {
perror("VIDIOCSWIN");
rb_raise(rb_eException, "VIDIOCSWIN");
}
v4l_alloc_map(ptr);
return self;
}
VALUE v4l_get_channels(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
int i;
VALUE ary = rb_ary_new();
for(i = 0; i < ptr->vid_cap.channels; i++)
{
ptr->vid_chan.channel = i;
if(ioctl(ptr->fd, VIDIOCGCHAN, &(ptr->vid_chan)) == -1)
{
perror("VIDIOCGCHAN");
rb_raise(rb_eException, "VIDIOCGCHAN");
}
rb_ary_push(ary, rb_str_new2(ptr->vid_chan.name));
}
return ary;
}
VALUE v4l_get_channel(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
return INT2NUM(ptr->vid_chan.channel);
}
VALUE v4l_set_channel(VALUE self, VALUE channel)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
ptr->vid_chan.channel = NUM2INT(channel);
ptr->vid_chan.norm = VIDEO_MODE_NTSC;
if(ioctl(ptr->fd, VIDIOCGCHAN, &(ptr->vid_chan)) == -1)
{
perror("VIDIOCGCHAN");
rb_raise(rb_eException, "VIDIOCGCHAN");
}
if(ioctl(ptr->fd, VIDIOCSCHAN, &(ptr->vid_chan)) == -1)
{
perror("VIDIOCSCHAN");
rb_raise(rb_eException, "VIDIOCSCHAN");
}
return self;
}
VALUE v4l_get_brightness(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
return INT2NUM(ptr->vid_pic.brightness);
}
VALUE v4l_set_brightness(VALUE self, VALUE brightness)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
ptr->vid_pic.brightness = NUM2INT(brightness);
if(ioctl(ptr->fd, VIDIOCSPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCSPICT");
rb_raise(rb_eException, "VIDIOCSPICT");
}
return self;
}
VALUE v4l_get_contrast(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
return INT2NUM(ptr->vid_pic.contrast);
}
VALUE v4l_set_contrast(VALUE self, VALUE contrast)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
ptr->vid_pic.contrast = NUM2INT(contrast);
if(ioctl(ptr->fd, VIDIOCSPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCSPICT");
rb_raise(rb_eException, "VIDIOCSPICT");
}
return self;
}
VALUE v4l_get_hue(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
return INT2NUM(ptr->vid_pic.hue);
}
VALUE v4l_set_hue(VALUE self, VALUE hue)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
ptr->vid_pic.hue = NUM2INT(hue);
if(ioctl(ptr->fd, VIDIOCSPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCSPICT");
rb_raise(rb_eException, "VIDIOCSPICT");
}
return self;
}
VALUE v4l_get_colour(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
return INT2NUM(ptr->vid_pic.colour);
}
VALUE v4l_set_colour(VALUE self, VALUE colour)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(ioctl(ptr->fd, VIDIOCGPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCGPICT");
rb_raise(rb_eException, "VIDIOCGPICT");
}
ptr->vid_pic.colour = NUM2INT(colour);
if(ioctl(ptr->fd, VIDIOCSPICT, &(ptr->vid_pic)) == -1)
{
perror("VIDIOCSPICT");
rb_raise(rb_eException, "VIDIOCSPICT");
}
return self;
}
VALUE v4l_get_frame(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
unsigned long i, imgsz = ptr->vid_win.width * ptr->vid_win.height * 3;
unsigned char buf, *p = ptr->map;
switch(ptr->io)
{
case IO_METHOD_MMAP:
ptr->vid_mmap.frame = 0;
ptr->vid_mmap.width = ptr->vid_win.width;
ptr->vid_mmap.height = ptr->vid_win.height;
if(ioctl(ptr->fd, VIDIOCMCAPTURE, &ptr->vid_mmap) == -1)
{
perror("VIDIOCMCAPTURE");
rb_warn("VIDIOCMCAPTURE failed");
}
if(ioctl(ptr->fd, VIDIOCSYNC, &ptr->vid_mmap.frame) == -1) {
perror("VIDIOCSYNC");
rb_warn("VIDIOCSYNC failed");
}
break;
case IO_METHOD_READ:
while(read(ptr->fd, ptr->map, imgsz) <= 0);
break;
}
/* Converts from BGR to RGB */
i = ptr->vid_win.width * ptr->vid_win.height;
while(0 < i)
{
buf = p[2]; p[2] = p[0]; p[0] = buf;
p += 3;
i--;
}
return rb_str_new(ptr->map, imgsz);
}
VALUE v4l_capture(VALUE self)
{
if(!rb_block_given_p())
rb_raise(rb_eArgError, "must take a block");
while(1)
rb_yield(v4l_get_frame(self));
return self;
}
VALUE v4l_write_file(int argc, VALUE *argv, VALUE self)
{
FILE *fp;
V4ldata *ptr;
unsigned char *map;
Data_Get_Struct(self, V4ldata, ptr);
if(!ptr) return Qnil;
if(argc == 1)
map = ptr->map;
else if(argc == 2)
map = STR2CSTR(argv[1]);
else
rb_raise(rb_eArgError, "usage: write_file(path, [data])");
fp = fopen(STR2CSTR(argv[0]), "wb");
fprintf(fp, "P6\n %d %d\n 255\n", ptr->vid_win.width, ptr->vid_win.height);
fflush(fp);
fwrite(map, ptr->vid_win.width * ptr->vid_win.height, 3, fp);
fclose(fp);
return self;
}
VALUE v4l_close(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
v4l_free(ptr);
ptr = 0;
return self;
}
VALUE v4l_is_closed(VALUE self)
{
V4ldata *ptr;
Data_Get_Struct(self, V4ldata, ptr);
if(ptr)
return Qfalse;
return Qtrue;
}
void Init_V4l()
{
cV4l = rb_define_class("V4l", rb_cObject);
rb_define_singleton_method(cV4l, "new", v4l_new, 1);
rb_define_method(cV4l, "name", v4l_get_name, 0);
rb_define_method(cV4l, "width", v4l_get_width, 0);
rb_define_method(cV4l, "width=", v4l_set_width, 1);
rb_define_method(cV4l, "height", v4l_get_height, 0);
rb_define_method(cV4l, "height=", v4l_set_height, 1);
rb_define_method(cV4l, "set_size", v4l_set_size, 2);
rb_define_method(cV4l, "channels", v4l_get_channels, 0);
rb_define_method(cV4l, "channel", v4l_get_channel, 0);
rb_define_method(cV4l, "channel=", v4l_set_channel, 1);
rb_define_method(cV4l, "brightness", v4l_get_brightness, 0);
rb_define_method(cV4l, "brightness=", v4l_set_brightness, 1);
rb_define_method(cV4l, "contrast", v4l_get_contrast, 0);
rb_define_method(cV4l, "contrast=", v4l_set_contrast, 1);
rb_define_method(cV4l, "hue", v4l_get_hue, 0);
rb_define_method(cV4l, "hue=", v4l_set_hue, 1);
rb_define_method(cV4l, "colour", v4l_get_colour, 0);
rb_define_method(cV4l, "colour=", v4l_set_colour, 1);
rb_define_method(cV4l, "get_frame", v4l_get_frame, 0);
rb_define_method(cV4l, "capture", v4l_capture, 0);
rb_define_method(cV4l, "write_file", v4l_write_file, -1);
rb_define_method(cV4l, "close", v4l_close, 0);
rb_define_method(cV4l, "closed?", v4l_is_closed, 0);
}
|