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



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

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

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



89
90
91
92
93
94
95
96
97
98
# File 'ext/v4l2/v4l2.c', line 89

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)


942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File 'ext/v4l2/v4l2.c', line 942

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  return (busy)? Qtrue: Qfalse;
}

#captureObject



908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'ext/v4l2/v4l2.c', line 908

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



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

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

  /*
   * do close
   */
  do {
    err = camera_check_ready(ptr, &ready);
    if (err) {
      rb_raise(rb_eRuntimeError, "camera_check_ready() failed.");
    }

    if (ready) {
      err = camera_stop(ptr);
      if (err) {
        rb_raise(rb_eRuntimeError, "camera_stop() failed.");
      }
    }

    err = camera_finalize(ptr);
    if (err) {
      rb_raise(rb_eRuntimeError, "finalize camera context failed.");
    }
  } while (0);

  return Qnil;
}

#controlsObject



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

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

  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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)


988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
# File 'ext/v4l2/v4l2.c', line 988

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  return (error)? Qtrue: Qfalse;
}

#format=(fmt) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'ext/v4l2/v4l2.c', line 627

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



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
572
573
574
# File 'ext/v4l2/v4l2.c', line 528

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

  int err;
  struct v4l2_frmsizeenum size;

  VALUE capa;
  VALUE list;
  VALUE rate;

  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

  ret = rb_ary_new();
  fmt = to_pixfmt(_fmt);

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

    if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
      capa = rb_obj_alloc(frame_cap_klass);
      list = get_framerate_list(ptr, fmt, &size.discrete);

      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, list);

      rb_ary_push(ret, capa);

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

  return ret;
}

#framerateObject



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'ext/v4l2/v4l2.c', line 739

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

  /*
   * create return object
   */
  ret = rb_rational_new(INT2FIX(ptr->framerate.num),
                        INT2FIX(ptr->framerate.denom));

  return ret;
}

#framerate=(val) ⇒ Object



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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
# File 'ext/v4l2/v4l2.c', line 759

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_eTypeError, "illeagal framerate value.");
  }

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'ext/v4l2/v4l2.c', line 610

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

  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  return INT2FIX(value);
}

#image_heightObject



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'ext/v4l2/v4l2.c', line 694

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'ext/v4l2/v4l2.c', line 717

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  return Qnil;
}

#image_widthObject



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'ext/v4l2/v4l2.c', line 649

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'ext/v4l2/v4l2.c', line 672

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  return Qnil;
}

#ready?Boolean

Returns:

  • (Boolean)


965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
# File 'ext/v4l2/v4l2.c', line 965

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  return (ready)? Qtrue: Qfalse;
}

#set_control(id, _val) ⇒ Object



576
577
578
579
580
581
582
583
584
585
586
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 576

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

  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'ext/v4l2/v4l2.c', line 853

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  /*
   *
   */
  if (rb_block_given_p()) {
    rb_protect(rb_yield, self, &state);
    camera_stop(ptr);
    if (state) rb_jump_tag(state);
  }

  return self;
}

#stateObject



806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'ext/v4l2/v4l2.c', line 806

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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



885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# File 'ext/v4l2/v4l2.c', line 885

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

  /*
   * strip object
   */
  TypedData_Get_Struct(self, camera_t, &camera_data_type, ptr);

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

  return self;
}

#support_formatsObject



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

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;

  TypedData_Get_Struct(self, camera_t, &camera_data_type, 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;
}