Class: Video4Linux2::Camera

Inherits:
Object
  • Object
show all
Defined in:
ext/v4l2/v4l2.c

Defined Under Namespace

Classes: BooleanControl, Control, FormatDescription, FrameCapability, IntegerControl, MenuControl, MenuItem

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dev) ⇒ Object



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
# File 'ext/v4l2/v4l2.c', line 85

static VALUE
rb_camera_initialize(VALUE self, VALUE dev)
{
  int err;
  camera_t* ptr;

  /*
   * argument check
   */
  Check_Type(dev, T_STRING);

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * initialize struct
   */
  err = camera_initialize(ptr, RSTRING_PTR(dev));
  if (err) {
    rb_raise(rb_eRuntimeError, "initialize camera context failed.");
  }

  /*
   * set default instance variable
   */
  rb_ivar_set(self, id_iv_name,
              rb_enc_str_new_cstr(ptr->name, rb_utf8_encoding()));
  rb_ivar_set(self, id_iv_driver,
              rb_enc_str_new_cstr(ptr->driver, rb_utf8_encoding()));
  rb_ivar_set(self, id_iv_bus,
              rb_enc_str_new_cstr(ptr->bus, rb_utf8_encoding()));

  return Qtrue;
}

Class Method Details

.open(device) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'ext/v4l2/v4l2.c', line 74

static VALUE
rb_camera_open(VALUE self, VALUE device)
{
  VALUE ret;

  ret = rb_obj_alloc(camera_klass);
  rb_obj_call_init(ret, 1, &device);

  return ret;
}

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'ext/v4l2/v4l2.c', line 866

static VALUE
rb_camera_is_busy(VALUE self)
{
  camera_t* ptr;
  int err;
  int busy;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * do check
   */
  err = camera_check_busy(ptr, &busy);
  if (err) {
    rb_raise(rb_eRuntimeError, "check failed.");
  }

  return (busy)? Qtrue: Qfalse;
}

#captureObject



832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
# File 'ext/v4l2/v4l2.c', line 832

static VALUE
rb_camera_capture(VALUE self)
{
  VALUE ret;
  camera_t* ptr;
  size_t used;
  int err;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * allocate return value.
   */
  ret = rb_str_buf_new(ptr->image_size);
  rb_str_set_len(ret, ptr->image_size);

  /*
   * do capture
   */
  err = camera_get_image(ptr, RSTRING_PTR(ret), &used);
  if (err) {
    rb_raise(rb_eRuntimeError, "capture failed.");
  }

  if (ptr->image_size != used) {
    rb_str_set_len(ret, used);
  }

  return ret;
}

#closeObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'ext/v4l2/v4l2.c', line 122

static VALUE
rb_camera_close( VALUE self)
{
  int err;
  camera_t* ptr;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * convert
   */
  err = camera_finalize(ptr);
  if (err) {
    rb_raise(rb_eRuntimeError, "finalize camera context failed.");
  }

  return Qnil;
}

#controlsObject



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
# File 'ext/v4l2/v4l2.c', line 269

static VALUE
rb_camera_get_controls(VALUE self)
{
  VALUE ret;
  camera_t* ptr;
  int i;
  VALUE info;

  Data_Get_Struct(self, camera_t, ptr);

  ret = rb_ary_new();

  for (i = 0; i < 43; i++) {
    info = get_control_info(ptr, V4L2_CID_BASE + i);
    if (info != Qnil) rb_ary_push(ret, info);
  }

  for (i = 0; i < 30; i++) {
    info = get_control_info(ptr, V4L2_CID_CAMERA_CLASS_BASE + i);
    if (info != Qnil) rb_ary_push(ret, info);
  }

  for (i = 0; i < 20; i++) {
    info = get_control_info(ptr, V4L2_CID_JPEG_CLASS_BASE + i);
    if (info != Qnil) rb_ary_push(ret, info);
  }

  return ret;
}

#error?Boolean

Returns:

  • (Boolean)


889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'ext/v4l2/v4l2.c', line 889

static VALUE
rb_camera_is_error(VALUE self)
{
  camera_t* ptr;
  int err;
  int error;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * do check
   */
  err = camera_check_error(ptr, &error);
  if (err) {
    rb_raise(rb_eRuntimeError, "check failed.");
  }

  return (error)? Qtrue: Qfalse;
}

#format=(fmt) ⇒ Object



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'ext/v4l2/v4l2.c', line 560

static VALUE
rb_camera_set_format(VALUE self, VALUE fmt)
{
  camera_t* ptr;
  int err;

  /*
   * argument check
   */
  Check_Type(fmt, T_SYMBOL);

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * set parameter
   */
  err = camera_set_format(ptr, to_pixfmt(fmt));
  if (err) {
    rb_raise(rb_eRuntimeError, "set format failed.");
  }

  return Qnil;
}

#frame_capabilities(fmt) ⇒ Object



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
# File 'ext/v4l2/v4l2.c', line 465

static VALUE
rb_camera_get_frame_capabilities(VALUE self, VALUE fmt)
{
  VALUE ret;
  camera_t* ptr;
  int i;
  int j;

  int err;
  struct v4l2_frmsizeenum size;

  VALUE capa;
  VALUE list;
  VALUE rate;

  Check_Type(fmt, T_SYMBOL);

  Data_Get_Struct(self, camera_t, ptr);

  ret = rb_ary_new();

  for (i = 0; ;i++){
    err = camera_get_frame_size(ptr, to_pixfmt(fmt), i, &size);
    if (err) break;

    if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
      capa = rb_obj_alloc(frame_cap_klass);
      rb_ivar_set(capa, id_iv_width, INT2NUM(size.discrete.width));
      rb_ivar_set(capa, id_iv_height, INT2NUM(size.discrete.height));
      rb_ivar_set(capa, id_iv_rate, get_framerate_list(ptr, &size.discrete));

      rb_ary_push(ret, capa);

    } else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
      make_dummy_capabilities(ptr, ret,
                              size.stepwise.max_width,
                              size.stepwise.max_height);
      break;
    }
  }

  return ret;
}

#framerateObject



687
688
689
690
691
# File 'ext/v4l2/v4l2.c', line 687

static VALUE
rb_camera_get_framerate(VALUE self)
{
  return Qnil;
}

#framerate=(val) ⇒ Object



693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
# File 'ext/v4l2/v4l2.c', line 693

static VALUE
rb_camera_set_framerate(VALUE self, VALUE val)
{
  camera_t* ptr;
  int num;
  int denom;
  int err;

  /*
   * argument check
   */
  switch (TYPE(val)) {
  case T_FIXNUM:
    num   = 1;
    denom = FIX2INT(val);
    break;

  case T_FLOAT:
    num   = 1000;
    denom = (int)(NUM2DBL(val) * 1000.0);
    break;

  case T_RATIONAL:
    num   = FIX2INT(rb_rational_den(val));
    denom = FIX2INT(rb_rational_num(val));
    break;
  
  default:
    rb_raise(rb_eArgError, "illeagal framerate value.");
  }

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * set framerate
   */
  err = camera_set_framerate(ptr, num, denom);
  if (err) {
    rb_raise(rb_eRuntimeError, "set framerate failed.");
  }

  return Qnil;
}

#get_control(id) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'ext/v4l2/v4l2.c', line 543

static VALUE
rb_camera_get_control(VALUE self, VALUE id)
{
  camera_t* ptr;
  int err;
  int32_t value;

  Data_Get_Struct( self, camera_t, ptr);

  err = camera_get_control(ptr, FIX2INT(id), &value);
  if (err) {
      rb_raise(rb_eRuntimeError, "get control failed.");
  }

  return INT2FIX(value);
}

#image_heightObject



637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'ext/v4l2/v4l2.c', line 637

static VALUE
rb_camera_get_image_height(VALUE self)
{
  int ret;
  camera_t* ptr;
  int err;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * get parameter
   */
  err = camera_get_image_height(ptr, &ret);
  if (err) {
    rb_raise(rb_eRuntimeError, "get image height failed.");
  }

  return INT2FIX(ret);
}

#image_height=(val) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'ext/v4l2/v4l2.c', line 660

static VALUE
rb_camera_set_image_height(VALUE self, VALUE val)
{
  camera_t* ptr;
  int err;

  /*
   * argument check
   */
  Check_Type(val, T_FIXNUM);

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * set parameter
   */
  err = camera_set_image_height(ptr, FIX2INT(val));
  if (err) {
    rb_raise(rb_eRuntimeError, "set image height failed.");
  }

  return Qnil;
}

#image_widthObject



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'ext/v4l2/v4l2.c', line 587

static VALUE
rb_camera_get_image_width(VALUE self)
{
  int ret;
  camera_t* ptr;
  int err;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * get parameter
   */
  err = camera_get_image_width(ptr, &ret);
  if (err) {
    rb_raise(rb_eRuntimeError, "get image width failed.");
  }

  return INT2FIX(ret);
}

#image_width=(val) ⇒ Object



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'ext/v4l2/v4l2.c', line 610

static VALUE
rb_camera_set_image_width(VALUE self, VALUE val)
{
  camera_t* ptr;
  int err;

  /*
   * argument check
   */
  Check_Type(val, T_FIXNUM);

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * set parameter
   */
  err = camera_set_image_width(ptr, FIX2INT(val));
  if (err) {
    rb_raise(rb_eRuntimeError, "set image width failed.");
  }

  return Qnil;
}

#set_control(id, _val) ⇒ Object



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
# File 'ext/v4l2/v4l2.c', line 509

static VALUE
rb_camera_set_control(VALUE self, VALUE id, VALUE _val)
{
  camera_t* ptr;
  int err;
  int val;

  Data_Get_Struct(self, camera_t, ptr);

  switch (TYPE(_val)) {
  case T_TRUE:
    val = 1;
    break;

  case T_FALSE:
    val = 0;
    break;

  case T_FIXNUM:
    val = FIX2INT(_val);
    break;

  default:
    rb_raise(rb_eTypeError, "invalid type of control value");
  }

  err = camera_set_control(ptr, FIX2INT(id), val);
  if (err) {
    rb_raise(rb_eRuntimeError, "set control failed.");
  }

  return Qnil;
}

#startObject



787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'ext/v4l2/v4l2.c', line 787

static VALUE
rb_camera_start(VALUE self)
{
  int err;
  camera_t* ptr;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * convert
   */
  err = camera_start(ptr);
  if (err) {
    rb_raise(rb_eRuntimeError, "start capture failed.");
  }

  return Qnil;
}

#stateObject



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'ext/v4l2/v4l2.c', line 740

static VALUE
rb_camera_state( VALUE self)
{
  camera_t* ptr;
  const char* str;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * convert state code
   */
  switch (ptr->state) {
  case -1:
    str = "ERROR";
    break;

  case 0:
    str = "FINALIZED";
    break;

  case 1:
    str = "INITIALIZED";
    break;

  case 2:
    str = "PREAPARE";
    break;

  case 3:
    str = "READY";
    break;

  case 4:
    str = "ST_REQUESTED";
    break;

  default:
    str = "unknown";
    break;
  }

  return ID2SYM(rb_intern(str));
}

#stopObject



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'ext/v4l2/v4l2.c', line 809

static VALUE
rb_camera_stop(VALUE self)
{
  int err;
  camera_t* ptr;

  /*
   * strip object
   */
  Data_Get_Struct(self, camera_t, ptr);

  /*
   * convert
   */
  err = camera_stop(ptr);
  if (err) {
    rb_raise(rb_eRuntimeError, "stop capture failed.");
  }

  return Qnil;
}

#support_formatsObject



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
# File 'ext/v4l2/v4l2.c', line 363

static VALUE
rb_camera_get_support_formats(VALUE self)
{
  VALUE ret;
  camera_t* ptr;

  int i;
  int err;
  struct v4l2_fmtdesc desc;

  VALUE fmt;
  VALUE fcc;
  VALUE str;

  Data_Get_Struct(self, camera_t, ptr);

  ret = rb_ary_new();

  for (i = 0; ;i++) {
    err = camera_get_format_desc(ptr, i, &desc);
    if (err) break;

    fmt = rb_obj_alloc(fmt_desc_klass);
    fcc = rb_enc_sprintf(rb_utf8_encoding(),
                         "%c%c%c%c",
                         desc.pixelformat >>  0 & 0xff,
                         desc.pixelformat >>  8 & 0xff,
                         desc.pixelformat >> 16 & 0xff,
                         desc.pixelformat >> 24 & 0xff);

    str = rb_enc_str_new_cstr((const char*)desc.description,
                               rb_utf8_encoding());

    rb_ivar_set(fmt, id_iv_fcc, fcc);
    rb_ivar_set(fmt, id_iv_desc, str);
    
    rb_ary_push(ret, fmt);
  }

  return ret;
}