Visual Servoing Platform  version 3.5.1 under development (2022-08-17)
vp1394TwoGrabber.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Firewire cameras video capture.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
44 #include <iostream>
45 
46 #include <visp3/core/vpConfig.h>
47 
48 /*
49  * Interface with libdc1394 2.x
50  */
51 #if defined(VISP_HAVE_DC1394)
52 #include <unistd.h>
53 
54 #include <visp3/core/vpFrameGrabberException.h>
55 #include <visp3/core/vpImageConvert.h>
56 #include <visp3/core/vpTime.h>
57 #include <visp3/sensor/vp1394TwoGrabber.h>
58 
59 const char *vp1394TwoGrabber::strVideoMode[DC1394_VIDEO_MODE_NUM] = {
60  "MODE_160x120_YUV444", "MODE_320x240_YUV422", "MODE_640x480_YUV411", "MODE_640x480_YUV422",
61  "MODE_640x480_RGB8", "MODE_640x480_MONO8", "MODE_640x480_MONO16", "MODE_800x600_YUV422",
62  "MODE_800x600_RGB8", "MODE_800x600_MONO8", "MODE_1024x768_YUV422", "MODE_1024x768_RGB8",
63  "MODE_1024x768_MONO8", "MODE_800x600_MONO16", "MODE_1024x768_MONO16", "MODE_1280x960_YUV422",
64  "MODE_1280x960_RGB8", "MODE_1280x960_MONO8", "MODE_1600x1200_YUV422", "MODE_1600x1200_RGB8",
65  "MODE_1600x1200_MONO8", "MODE_1280x960_MONO16", "MODE_1600x1200_MONO16", "MODE_EXIF",
66  "MODE_FORMAT7_0", "MODE_FORMAT7_1", "MODE_FORMAT7_2", "MODE_FORMAT7_3",
67  "MODE_FORMAT7_4", "MODE_FORMAT7_5", "MODE_FORMAT7_6", "MODE_FORMAT7_7"};
68 
69 const char *vp1394TwoGrabber::strFramerate[DC1394_FRAMERATE_NUM] = {
70  "FRAMERATE_1_875", "FRAMERATE_3_75", "FRAMERATE_7_5", "FRAMERATE_15",
71  "FRAMERATE_30", "FRAMERATE_60", "FRAMERATE_120", "FRAMERATE_240"};
72 
73 const char *vp1394TwoGrabber::strColorCoding[DC1394_COLOR_CODING_NUM] = {
74  "COLOR_CODING_MONO8", "COLOR_CODING_YUV411", "COLOR_CODING_YUV422", "COLOR_CODING_YUV444",
75  "COLOR_CODING_RGB8", "COLOR_CODING_MONO16", "COLOR_CODING_RGB16", "COLOR_CODING_MONO16S",
76  "COLOR_CODING_RGB16S", "COLOR_CODING_RAW8", "COLOR_CODING_RAW16",
77 };
78 
124  : camera(NULL), cameras(NULL), num_cameras(0), camera_id(0), verbose(false), camIsOpen(NULL),
125  num_buffers(4), // ring buffer size
126  isDataModified(NULL), initialShutterMode(NULL), dataCam(NULL)
127 #ifdef VISP_HAVE_DC1394_CAMERA_ENUMERATE // new API > libdc1394-2.0.0-rc7
128  ,
129  d(NULL), list(NULL)
130 #endif
131 {
132  // protected members
133  width = height = 0;
134 
135  // private members
136  init = false;
137 
138  reset = false;
139  initialize(reset);
140 
141  // open();
142 }
143 
154 {
155  /* if(num_cameras >= 1){
156  delete[] isDataModified;
157  delete[] initialShutterMode;
158  delete[] dataCam;
159  }*/
160  close();
161 }
162 
281 void vp1394TwoGrabber::setCamera(uint64_t cam_id)
282 {
283  // Suppose that if camera_id is a camera GUID, this value is greater
284  // than the number of cameras connected to the bus
285  if (cam_id >= num_cameras) {
286  // Check if camera_id is a camera guid
287  bool is_guid = false;
288  // check if the camera_id is a guid
289  for (unsigned int i = 0; i < num_cameras; i++) {
290  if (cameras[i]->guid == cam_id) {
291  this->camera_id = i;
292  is_guid = true;
293  break;
294  }
295  }
296  if (is_guid == false) {
297  std::cout << "Error: The camera with guid 0x" << std::hex << cam_id << " is not present" << std::endl;
298  std::cout << num_cameras << " camera(s) connected" << std::endl;
299  for (unsigned int i = 0; i < num_cameras; i++) {
300  std::cout << " - camera " << i << " with guid 0x" << std::hex << cameras[i]->guid << std::endl;
301  }
302  close();
303  throw(vpFrameGrabberException(vpFrameGrabberException::settingError, "The required camera is not present"));
304  }
305  } else {
306  this->camera_id = (unsigned int)cam_id; // The input cam_id is not a
307  // uint64_t guid, but the index of
308  // the camera
309  }
310 
311  // create a pointer to the working camera
312  camera = cameras[this->camera_id];
313 }
314 
329 void vp1394TwoGrabber::getCamera(uint64_t &cam_id)
330 {
331  if (num_cameras) {
332  cam_id = this->camera_id;
333  } else {
334  close();
335  vpERROR_TRACE("No cameras found");
337  }
338 }
339 
355 {
356  if (num_cameras) {
357  return this->camera_id;
358  } else {
359  close();
360  vpERROR_TRACE("No cameras found");
362  }
363 }
364 
372 void vp1394TwoGrabber::getNumCameras(unsigned int &ncameras) const
373 {
374  if (!num_cameras) {
375  vpCTRACE << "No camera found..." << std::endl;
376  ncameras = 0;
377  }
378 
379  ncameras = num_cameras;
380 }
381 
390 {
391  unsigned int ncameras = 0;
392  if (!num_cameras) {
393  vpCTRACE << "No camera found..." << std::endl;
394  ncameras = 0;
395  }
396 
397  ncameras = num_cameras;
398  return ncameras;
399 }
400 
446 {
447  open();
448  if (!num_cameras) {
449  close();
451  }
452  if (!isVideoModeSupported(videomode)) {
453  throw(vpFrameGrabberException(vpFrameGrabberException::settingError, "Video mode not supported by camera %d",
454  camera_id));
455  }
456  // Stop dma capture if started
457  setTransmission(DC1394_OFF);
458  setCapture(DC1394_OFF);
459 
460  if (dc1394_video_set_mode(camera, (dc1394video_mode_t)videomode) != DC1394_SUCCESS) {
461  close();
462  throw(vpFrameGrabberException(vpFrameGrabberException::settingError, "Can't set video mode"));
463  }
464 
465  setCapture(DC1394_ON);
466  setTransmission(DC1394_ON);
467 
468  // Updates image size from new video mode
469  if (dc1394_get_image_size_from_video_mode(camera, (dc1394video_mode_t)videomode, &this->width, &this->height) !=
470  DC1394_SUCCESS) {
471 
472  close();
473  throw(vpFrameGrabberException(vpFrameGrabberException::settingError, "Can't get image size"));
474  }
475 }
476 
494 {
495  if (!num_cameras) {
496  close();
497  vpERROR_TRACE("No camera found");
499  }
500 
501  dc1394video_mode_t _videomode;
502  if (dc1394_video_get_mode(camera, &_videomode) != DC1394_SUCCESS) {
503 
504  close();
505  vpERROR_TRACE("Can't get current video mode");
506  throw(vpFrameGrabberException(vpFrameGrabberException::settingError, "Can't get current video mode"));
507  }
508  videomode = (vp1394TwoVideoModeType)_videomode;
509 }
510 
528 uint32_t vp1394TwoGrabber::getVideoModeSupported(std::list<vp1394TwoVideoModeType> &videomodes)
529 {
530  // Refresh the list of supported modes
531  videomodes.clear();
532 
533  if (!num_cameras) {
534  close();
535  vpERROR_TRACE("No camera found");
537  }
538  dc1394video_modes_t _videomodes;
539 
540  // get video modes:
541  if (dc1394_video_get_supported_modes(camera, &_videomodes) != DC1394_SUCCESS) {
542 
543  close();
544  vpERROR_TRACE("Can't get video modes");
545  throw(vpFrameGrabberException(vpFrameGrabberException::settingError, "Can't get video modes"));
546  }
547 
548  // parse the video modes to add in the list
549  for (unsigned i = 0; i < _videomodes.num; i++) {
550  vp1394TwoVideoModeType _mode = (vp1394TwoVideoModeType)_videomodes.modes[i];
551  videomodes.push_back(_mode);
552  }
553 
554  // return the number of available video modes
555  return _videomodes.num;
556 }
573 {
574  if (!num_cameras) {
575  close();
576  vpERROR_TRACE("No camera found");
578  }
579  dc1394video_modes_t _videomodes;
580 
581  // get video modes:
582  if (dc1394_video_get_supported_modes(camera, &_videomodes) != DC1394_SUCCESS) {
583 
584  close();
585  vpERROR_TRACE("Can't get video modes");
586  throw(vpFrameGrabberException(vpFrameGrabberException::settingError, "Can't get video modes"));
587  }
588 
589  // parse the video modes to check with the desired
590  for (unsigned i = 0; i < _videomodes.num; i++) {
591  if ((vp1394TwoVideoModeType)_videomodes.modes[i] == videomode)
592  return true;
593  }
594  return false;
595 }
596 
610 {
611 
612  if (dc1394_is_video_mode_scalable((dc1394video_mode_t)videomode))
613  return true;
614 
615  return false;
616 }
617 
634 {
636  getColorCoding(coding);
637 
638  switch (coding) {
642  case vpCOLOR_CODING_RAW8:
644  return false;