24 #include <core/exception.h> 25 #include <core/exceptions/software.h> 26 #include <utils/system/console_colors.h> 33 #include <fvcams/firewire.h> 34 #include <fvcams/cam_exceptions.h> 35 #include <fvutils/system/camargp.h> 37 #include <dc1394/utils.h> 60 FirewireCamera::FirewireCamera(dc1394framerate_t framerate,
61 dc1394video_mode_t mode,
65 _started = _opened =
false;
66 _valid_frame_received =
false;
68 _auto_shutter =
false;
69 _auto_white_balance =
false;
71 _num_buffers = num_buffers;
73 _framerate = framerate;
74 _white_balance_ub = 0xFFFFFFFF;
75 _white_balance_vr = 0xFFFFFFFF;
76 _format7_mode_enabled =
false;
77 _format7_width = _format7_height = _format7_startx = _format7_starty = 0;
79 _model = strdup(
"any");
80 _do_set_shutter =
false;
81 _do_set_white_balance =
false;
82 _do_set_focus =
false;
89 if ((mode == DC1394_VIDEO_MODE_640x480_YUV422) && (framerate == DC1394_FRAMERATE_30)) {
91 _framerate = DC1394_FRAMERATE_15;
97 FirewireCamera::~FirewireCamera()
101 if ( _model != NULL ) {
111 FirewireCamera::open_device()
113 _dc1394 = dc1394_new();
114 dc1394camera_list_t *list;
116 if ( dc1394_camera_enumerate(_dc1394, &list) != DC1394_SUCCESS ) {
117 throw Exception(
"Could not enumerate cameras");
121 if ( strcmp(_model,
"any") == 0 ) {
123 _camera = dc1394_camera_new(_dc1394, list->ids[0].guid);
125 dc1394_free(_dc1394);
127 throw Exception(
"Could not create camera for first foiund camera");
131 for (
unsigned int i = 0; i < list->num; ++i) {
132 dc1394camera_t *tmpcam = dc1394_camera_new(_dc1394, list->ids[i].guid);
133 if ( strcmp(_model, tmpcam->model) == 0) {
138 dc1394_camera_free(tmpcam);
141 if ( _camera == NULL ) {
142 throw Exception(
"Could not find camera with model %s", _model);
149 _device_opened =
true;
153 FirewireCamera::open()
157 if (! _device_opened) {
163 if ( iso_mode_enabled() ) {
164 dc1394_video_set_transmission(_camera, DC1394_OFF);
168 dc1394_iso_release_bandwidth(_camera, INT_MAX);
169 for (
int channel = 0; channel < 64; ++channel) {
170 dc1394_iso_release_channel(_camera, channel);
175 if (_camera->bmode_capable > 0) {
176 dc1394_video_set_operation_mode(_camera, DC1394_OPERATION_MODE_1394B);
179 ((err = dc1394_video_set_iso_speed(_camera, _speed)) != DC1394_SUCCESS) ||
180 ((err = dc1394_video_set_mode(_camera, _mode)) != DC1394_SUCCESS) ||
181 ((err = dc1394_video_set_framerate(_camera, _framerate)) != DC1394_SUCCESS) ) {
182 throw Exception(
"Setting up the camera failed: %s", dc1394_error_get_string(err));
185 if (_format7_mode_enabled) {
186 if (_format7_bpp == 0) {
188 dc1394_format7_get_recommended_packet_size(_camera, _mode, &rps);
192 if ( ((err = dc1394_format7_set_image_size(_camera, _mode, _format7_width, _format7_height)) != DC1394_SUCCESS) ||
193 ((err = dc1394_format7_set_image_position(_camera, _mode, _format7_startx, _format7_starty)) != DC1394_SUCCESS) ||
194 ((err = dc1394_format7_set_color_coding(_camera, _mode, _format7_coding)) != DC1394_SUCCESS) ||
195 ((err = dc1394_format7_set_packet_size(_camera, _mode, _format7_bpp)) != DC1394_SUCCESS) ) {
196 throw Exception(
"Could not setup Format7 parameters: %s", dc1394_error_get_string(err));
200 set_auto_shutter(_auto_shutter);
201 if ( !_auto_shutter && _do_set_shutter ) {
202 set_shutter(_shutter);
205 set_auto_focus(_auto_focus);
206 if ( ! _auto_focus && _do_set_focus ) {
210 set_auto_white_balance(_auto_white_balance);
211 if ( ! _auto_white_balance &&
212 (_white_balance_ub != 0xFFFFFFFF) &&
213 (_white_balance_vr != 0xFFFFFFFF) &&
214 _do_set_white_balance ) {
215 set_white_balance(_white_balance_ub, _white_balance_vr);
227 FirewireCamera::start()
229 if (_started)
return;
232 throw Exception(
"FirewireCamera: Cannot start closed camera");
236 if ( (err = dc1394_capture_setup(_camera, _num_buffers, DC1394_CAPTURE_FLAGS_DEFAULT )) != DC1394_SUCCESS ) {
237 dc1394_capture_stop(_camera);
238 throw Exception(
"FirewireCamera: Could not setup capture (%s)", dc1394_error_get_string(err));
241 if ( (err = dc1394_video_set_transmission(_camera, DC1394_ON)) != DC1394_SUCCESS) {
243 dc1394_capture_stop(_camera);
244 throw Exception(
"FirewireCamera: Could not start ISO transmission (%s)", dc1394_error_get_string(err));
255 FirewireCamera::stop()
258 dc1394_video_set_transmission(_camera, DC1394_OFF);
259 dc1394_capture_stop(_camera);
270 FirewireCamera::iso_mode_enabled()
272 dc1394switch_t status;
273 if ( dc1394_video_get_transmission(_camera, &status) != DC1394_SUCCESS) {
274 throw Exception(
"Could not get transmission status");
276 return (status == DC1394_ON);
282 FirewireCamera::print_info()
285 dc1394_camera_print_info( _camera, stdout );
288 printf(
"Parameters:\n" 289 "valid frame received: %i\n" 291 "auto shutter: %i (shutter value: %u)\n" 292 "auto white balance: %i (white balance value %u/%u)\n" 293 "do set shutter: %i do set white balance: %i\n",
294 _valid_frame_received,_auto_focus,
295 _auto_shutter, _shutter,
296 _auto_white_balance, _white_balance_ub, _white_balance_vr,
297 _do_set_shutter =
false, _do_set_white_balance =
false 306 FirewireCamera::guid()
const 312 return _camera->guid;
320 FirewireCamera::model()
const 326 return _camera->model;
331 FirewireCamera::capture()
335 throw CaptureException(
"FirewireCamera(%s): cannot capture on closed camera", _model);
338 throw CaptureException(
"FirewireCamera(%s): cannot capture on stopped camera", _model);
341 if (! iso_mode_enabled()) {
342 throw CaptureException(
"FirewireCamera(%s): isochronous transfer not active", _model);
346 if (DC1394_SUCCESS != (err = dc1394_capture_dequeue(_camera, DC1394_CAPTURE_POLICY_WAIT, &_frame))) {
347 _valid_frame_received =
false;
349 _model, dc1394_error_get_string(err));
351 _valid_frame_received = (_frame != NULL);
357 FirewireCamera::flush()
367 FirewireCamera::buffer()
369 if ( _valid_frame_received ) {
370 return _frame->image;
378 FirewireCamera::buffer_size()
380 if ( _valid_frame_received ) {
381 return _frame->total_bytes;
388 FirewireCamera::close()
390 if ( _started ) stop();
392 if (_camera) dc1394_camera_free( _camera );
393 if (_dc1394) dc1394_free(_dc1394);
402 FirewireCamera::dispose_buffer()
404 if ( _valid_frame_received ) {
405 dc1394_capture_enqueue( _camera, _frame );
411 FirewireCamera::pixel_width()
414 if ( _valid_frame_received ) {
415 return _frame->size[0];
417 unsigned int width, height;
419 if ((err = dc1394_get_image_size_from_video_mode(_camera, _mode, &width, &height)) != DC1394_SUCCESS) {
420 throw Exception(
"FirewireCamera(%s): cannot get width (%s)", _model,
421 dc1394_error_get_string(err));
432 FirewireCamera::pixel_height()
435 if ( _valid_frame_received ) {
436 return _frame->size[1];
438 unsigned int width, height;
440 if ((err = dc1394_get_image_size_from_video_mode(_camera, _mode, &width, &height)) != DC1394_SUCCESS) {
441 throw Exception(
"FirewireCamera(%s): cannot get width (%s)", _model,
442 dc1394_error_get_string(err));
453 FirewireCamera::colorspace()
457 case DC1394_VIDEO_MODE_320x240_YUV422:
458 case DC1394_VIDEO_MODE_640x480_YUV422:
459 case DC1394_VIDEO_MODE_800x600_YUV422:
460 case DC1394_VIDEO_MODE_1024x768_YUV422:
461 case DC1394_VIDEO_MODE_1280x960_YUV422:
462 case DC1394_VIDEO_MODE_1600x1200_YUV422:
463 return YUV422_PACKED;
465 case DC1394_VIDEO_MODE_640x480_YUV411:
466 return YUV411_PACKED;
469 case DC1394_VIDEO_MODE_640x480_RGB8:
470 case DC1394_VIDEO_MODE_800x600_RGB8:
471 case DC1394_VIDEO_MODE_1024x768_RGB8:
472 case DC1394_VIDEO_MODE_1280x960_RGB8:
473 case DC1394_VIDEO_MODE_1600x1200_RGB8:
476 case DC1394_VIDEO_MODE_640x480_MONO8:
477 case DC1394_VIDEO_MODE_800x600_MONO8:
478 case DC1394_VIDEO_MODE_1024x768_MONO8:
479 case DC1394_VIDEO_MODE_1280x960_MONO8:
480 case DC1394_VIDEO_MODE_1600x1200_MONO8:
483 case DC1394_VIDEO_MODE_640x480_MONO16:
484 case DC1394_VIDEO_MODE_800x600_MONO16:
485 case DC1394_VIDEO_MODE_1024x768_MONO16:
486 case DC1394_VIDEO_MODE_1280x960_MONO16:
487 case DC1394_VIDEO_MODE_1600x1200_MONO16:
490 case DC1394_VIDEO_MODE_FORMAT7_0:
491 case DC1394_VIDEO_MODE_FORMAT7_1:
492 case DC1394_VIDEO_MODE_FORMAT7_2:
493 case DC1394_VIDEO_MODE_FORMAT7_3:
494 case DC1394_VIDEO_MODE_FORMAT7_4:
495 case DC1394_VIDEO_MODE_FORMAT7_5:
496 case DC1394_VIDEO_MODE_FORMAT7_6:
497 case DC1394_VIDEO_MODE_FORMAT7_7:
498 switch (_format7_coding) {
499 case DC1394_COLOR_CODING_MONO8:
501 case DC1394_COLOR_CODING_YUV411:
502 return YUV411_PACKED;
503 case DC1394_COLOR_CODING_YUV422:
504 return YUV422_PACKED;
505 case DC1394_COLOR_CODING_RGB8:
507 case DC1394_COLOR_CODING_MONO16:
509 case DC1394_COLOR_CODING_RAW8:
511 case DC1394_COLOR_CODING_RAW16:
525 FirewireCamera::ready()
532 FirewireCamera::set_image_number(
unsigned int n)
540 FirewireCamera::set_auto_focus(
bool enabled)
543 if ((err = dc1394_feature_set_mode(_camera, DC1394_FEATURE_FOCUS,
544 enabled ? DC1394_FEATURE_MODE_AUTO : DC1394_FEATURE_MODE_MANUAL))
546 _auto_focus = enabled;
548 throw Exception(
"FirewireCamera(%s): Setting auto focus failed (%s)", _model,
549 dc1394_error_get_string(err));
555 FirewireCamera::auto_focus()
562 FirewireCamera::focus()
564 unsigned int focus = 0;
565 if (dc1394_feature_get_value(_camera, DC1394_FEATURE_FOCUS, &focus) == DC1394_SUCCESS) {
575 FirewireCamera::set_focus(
unsigned int focus)
577 dc1394_feature_set_value(_camera, DC1394_FEATURE_FOCUS, focus);
582 FirewireCamera::focus_min()
584 unsigned int min = 0;
585 unsigned int max = 0;
586 if (dc1394_feature_get_boundaries(_camera, DC1394_FEATURE_FOCUS, &min, &max) == DC1394_SUCCESS) {
595 FirewireCamera::focus_max()
597 unsigned int max = 0;
598 unsigned int min = 0;
599 if (dc1394_feature_get_boundaries(_camera, DC1394_FEATURE_FOCUS, &min, &max) == DC1394_SUCCESS) {
611 FirewireCamera::set_auto_shutter(
bool enabled)
613 if (dc1394_feature_set_mode(_camera, DC1394_FEATURE_SHUTTER,
614 enabled ? DC1394_FEATURE_MODE_AUTO : DC1394_FEATURE_MODE_MANUAL)
616 _auto_shutter = enabled;
625 FirewireCamera::auto_shutter()
627 return _auto_shutter;
635 FirewireCamera::set_shutter(
unsigned int shutter)
637 if ( dc1394_feature_set_value(_camera, DC1394_FEATURE_SHUTTER, shutter) != DC1394_SUCCESS ) {
638 throw Exception(
"Failed to set shutter to %d", shutter);
647 FirewireCamera::shutter()
649 if ( dc1394_feature_get_value(_camera, DC1394_FEATURE_SHUTTER, &_shutter) != DC1394_SUCCESS ) {
650 throw Exception(
"Failed to retrieve shutter value");
661 FirewireCamera::set_auto_white_balance(
bool enabled)
663 if (dc1394_feature_set_mode(_camera, DC1394_FEATURE_WHITE_BALANCE,
664 enabled ? DC1394_FEATURE_MODE_AUTO : DC1394_FEATURE_MODE_MANUAL)
666 _auto_white_balance = enabled;
675 FirewireCamera::auto_white_balance()
677 return _auto_white_balance;
686 FirewireCamera::white_balance(
unsigned int *ub,
unsigned int *vr)
688 if ( dc1394_feature_whitebalance_get_value(_camera, &_white_balance_ub, &_white_balance_vr) != DC1394_SUCCESS ) {
689 throw Exception(
"Failed to retrieve white balance values");
692 *ub = _white_balance_ub;
693 *vr = _white_balance_vr;
702 FirewireCamera::set_white_balance(
unsigned int ub,
unsigned int vr)
704 if ( dc1394_feature_whitebalance_set_value(_camera, ub, vr) != DC1394_SUCCESS ) {
705 throw Exception(
"Failed to set white balance to ub=%d vr=%d", ub, vr);
713 FirewireCamera::set_gain(
unsigned int gain)
717 if ( dc1394_feature_get_boundaries(_camera, DC1394_FEATURE_GAIN, &min, &max) != DC1394_SUCCESS ) {
718 throw Exception(
"Failed to get boundaries for feature gain");
726 if ( dc1394_feature_set_mode( _camera, DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL ) != DC1394_SUCCESS ) {
727 throw Exception(
"Failed to set manual mode for feature gain");
729 if ( dc1394_feature_set_value( _camera, DC1394_FEATURE_GAIN, gain ) != DC1394_SUCCESS) {
730 throw Exception(
"Failed to set value for feature gain");
739 FirewireCamera::parse_set_focus(
const char *focus)
744 }
else if ( f ==
"manual" ) {
748 long int focus = strtol(f.c_str(), &endptr, 10);
749 if ( endptr[0] != 0 ) {
751 }
else if ( focus < 0 ) {
756 _do_set_focus =
true;
766 FirewireCamera::parse_set_white_balance(
const char *white_balance)
768 string w = white_balance;
770 _auto_white_balance =
true;
773 string::size_type commapos = w.find(
",", 0);
774 if ( commapos == string::npos ) {
775 throw Exception(
"Illegal white balance value, neither auto and no comma found");
777 string ub = w.substr(0, commapos);
778 string vr = w.substr(commapos + 1);
780 long int ub_i = strtol(ub.c_str(), &endptr, 10);
781 if ( endptr[0] != 0 ) {
783 "String to int conversion failed");
784 }
else if ( ub_i < 0 ) {
787 long int vr_i = strtol(vr.c_str(), &endptr, 10);
788 if ( endptr[0] != 0 ) {
790 "String to int conversion failed");
791 }
else if ( vr_i < 0 ) {
795 _auto_white_balance =
false;
796 _white_balance_ub = ub_i;
797 _white_balance_vr = vr_i;
798 _do_set_white_balance =
true;
808 FirewireCamera::parse_set_shutter(
const char *shutter)
812 _auto_shutter =
true;
815 long int tmp = strtol(s.c_str(), &endptr, 10);
816 if ( endptr[0] !=
'\0' ) {
818 "String to int conversion failed");
819 }
else if ( tmp < 0 ) {
822 _auto_shutter =
false;
824 _do_set_shutter =
true;
871 _started = _opened =
false;
872 _valid_frame_received =
false;
874 _auto_shutter =
false;
875 _auto_white_balance =
false;
876 _white_balance_ub = 0xFFFFFFFF;
877 _white_balance_vr = 0xFFFFFFFF;
878 _do_set_shutter =
false;
879 _do_set_white_balance =
false;
880 _do_set_focus =
false;
883 _mode = DC1394_VIDEO_MODE_640x480_YUV422;
884 _speed = DC1394_ISO_SPEED_400;
885 _framerate = DC1394_FRAMERATE_15;
888 _format7_mode_enabled =
false;
889 _format7_width = _format7_height = _format7_startx = _format7_starty = 0;
891 _model = strdup(cap->
cam_id().c_str());
897 if ( cap->
has(
"mode") ) {
898 string m = cap->
get(
"mode");
899 if ( m ==
"640x480_MONO16" ) {
900 _mode = DC1394_VIDEO_MODE_640x480_MONO16;
901 }
else if ( m ==
"FORMAT7_0" ) {
902 _mode = DC1394_VIDEO_MODE_FORMAT7_0;
903 _format7_mode_enabled =
true;
904 }
else if ( m ==
"FORMAT7_1" ) {
905 _mode = DC1394_VIDEO_MODE_FORMAT7_1;
906 _format7_mode_enabled =
true;
907 }
else if ( m ==
"FORMAT7_2" ) {
908 _mode = DC1394_VIDEO_MODE_FORMAT7_2;
909 _format7_mode_enabled =
true;
910 }
else if ( m ==
"FORMAT7_3" ) {
911 _mode = DC1394_VIDEO_MODE_FORMAT7_3;
912 _format7_mode_enabled =
true;
913 }
else if ( m ==
"FORMAT7_4" ) {
914 _mode = DC1394_VIDEO_MODE_FORMAT7_4;
915 _format7_mode_enabled =
true;
916 }
else if ( m ==
"FORMAT7_5" ) {
917 _mode = DC1394_VIDEO_MODE_FORMAT7_5;
918 _format7_mode_enabled =
true;
919 }
else if ( m ==
"FORMAT7_6" ) {
920 _mode = DC1394_VIDEO_MODE_FORMAT7_6;
921 _format7_mode_enabled =
true;
922 }
else if ( m ==
"FORMAT7_7" ) {
923 _mode = DC1394_VIDEO_MODE_FORMAT7_7;
924 _format7_mode_enabled =
true;
927 if ( cap->
has(
"coding") ) {
928 string c = cap->
get(
"coding");
929 if ( c ==
"YUV422" ) {
930 _format7_coding = DC1394_COLOR_CODING_YUV422;
931 }
else if ( c ==
"MONO8" ) {
932 _format7_coding = DC1394_COLOR_CODING_MONO8;
933 }
else if ( c ==
"MONO16" ) {
934 _format7_coding = DC1394_COLOR_CODING_MONO16;
935 }
else if ( c ==
"RAW16" ) {
936 _format7_coding = DC1394_COLOR_CODING_RAW16;
939 if ( cap->
has(
"isospeed") ) {
940 string s = cap->
get(
"isospeed");
942 _speed = DC1394_ISO_SPEED_400;
943 }
else if ( s ==
"800" ) {
944 _speed = DC1394_ISO_SPEED_800;
947 if ( cap->
has(
"framerate") ) {
948 string f = cap->
get(
"framerate");
949 if ( f ==
"1.875" ) {
950 _framerate = DC1394_FRAMERATE_1_875;
951 }
else if ( f ==
"3.75" ) {
952 _framerate = DC1394_FRAMERATE_3_75;
953 }
else if ( f ==
"7.5" ) {
954 _framerate = DC1394_FRAMERATE_7_5;
955 }
else if ( f ==
"15" ) {
956 _framerate = DC1394_FRAMERATE_15;
957 }
else if ( f ==
"30" ) {
958 _framerate = DC1394_FRAMERATE_30;
959 }
else if ( f ==
"60" ) {
960 _framerate = DC1394_FRAMERATE_60;
961 }
else if ( f ==
"120" ) {
962 _framerate = DC1394_FRAMERATE_120;
963 }
else if ( f ==
"240" ) {
964 _framerate = DC1394_FRAMERATE_240;
967 if ( cap->
has(
"focus") ) {
968 parse_set_focus(cap->
get(
"focus").c_str());
970 if ( cap->
has(
"nbufs") ) {
971 _num_buffers = atoi(cap->
get(
"nbufs").c_str());
973 if ( cap->
has(
"width") ) {
974 _format7_width = atoi(cap->
get(
"width").c_str());
976 if ( cap->
has(
"height") ) {
977 _format7_height = atoi(cap->
get(
"height").c_str());
979 if ( cap->
has(
"startx") ) {
980 _format7_startx = atoi(cap->
get(
"startx").c_str());
982 if ( cap->
has(
"starty") ) {
983 _format7_starty = atoi(cap->
get(
"starty").c_str());
985 if ( cap->
has(
"packetsize") ) {
986 string p = cap->
get(
"packetsize");
987 if ( p ==
"recommended" ) {
990 _format7_bpp = atoi(p.c_str());
993 if ( cap->
has(
"gain") ) {
994 string g = cap->
get(
"gain");
996 _gain = atoi(g.c_str());
1000 if ( cap->
has(
"white_balance") ) {
1001 parse_set_white_balance(cap->
get(
"white_balance").c_str());
1003 if ( cap->
has(
"shutter") ) {
1004 parse_set_shutter(cap->
get(
"shutter").c_str());
1013 FirewireCamera::print_available_fwcams()
1016 dc1394_t *dc1394 = dc1394_new();
1017 dc1394camera_list_t *list;
1019 if ( (err = dc1394_camera_enumerate(dc1394, &list)) != DC1394_SUCCESS ) {
1020 throw Exception(
"Could not enumerate cameras: %s", dc1394_error_get_string(err));
1023 if (list->num > 0) {
1024 for (
unsigned int i = 0; i < list->num; ++i) {
1025 dc1394camera_t *tmpcam = dc1394_camera_new(dc1394, list->ids[i].guid);
1026 dc1394_camera_print_info(tmpcam, stdout);
1027 dc1394_camera_free(tmpcam);
1030 printf(
"Could not find any cameras\n");
Fawkes library namespace.
Capturing a frame failed.
std::string cam_id() const
Get camera ID.
bool has(std::string s) const
Check if an parameter was given.
Base class for exceptions in Fawkes.
std::string get(std::string s) const
Get the value of the given parameter.