//---------------------------------------------------------------
  void
  Device1394::initDevice(int port)
  {
    MIRO_DBG(VIDEO, LL_DEBUG, "Device1394::initDevice()");

    handle_ = dc1394_create_handle(port);
    if (!handle_) {
      throw Miro::Exception("Device1394::initDevice: unable to create handle");
    }

    int num_nodes = raw1394_get_nodecount(handle_);
    int camera_count = 0;
    nodeid_t * camera_nodes = dc1394_get_camera_nodes(handle_, &camera_count, 1);

    MIRO_DBG_OSTR(VIDEO, LL_DEBUG,
		  "Device1394::initDevice: cameracount = " << camera_count);
    if (camera_count < 1)
      throw Miro::Exception("Device1394::initDevice: no camera found");

    p_camera_->node = camera_nodes[0];
    MIRO_DBG_OSTR(VIDEO, LL_DEBUG, 
		  "Device1394::initDevice: node = " << 
		  p_camera_->node << "/" << num_nodes);
    if (p_camera_->node == num_nodes - 1)
      throw Miro::Exception("Device1394::initDevice: camera is highest numbered node.");

    dc1394_stop_iso_transmission(handle_, p_camera_->node);

    if (dc1394_get_camera_feature_set(handle_, p_camera_->node, &features_) != DC1394_SUCCESS)
      throw Miro::Exception("Device1394::initDevice: unable to get camera features");
	
    if (Miro::Log::level() >= Miro::Log::LL_NOTICE)
      dc1394_print_feature_set(&features_);
  }
Esempio n. 2
0
int
main(int argc, char *argv[])
{
  XGCValues                 xgcv;
  long                      background = 0x010203;
  unsigned int              channel;
  unsigned int              speed;
  int                       i, p, cn;
  raw1394handle_t           raw_handle;
  struct raw1394_portinfo   ports[MAX_PORTS];

  get_options(argc, argv);
  /* process options */
  switch (fps) {
  case 1:
    fps = FRAMERATE_1_875;
    break;
  case 3:
    fps = FRAMERATE_3_75;
    break;
  case 15:
    fps = FRAMERATE_15;
    break;
  case 30:
    fps = FRAMERATE_30;
    break;
  case 60:
    fps = FRAMERATE_60;
    break;
  default:
    fps = FRAMERATE_7_5;
    break;
  }
  switch (res) {
  case 1:
    res = MODE_640x480_YUV411;
    device_width = 640;
    device_height = 480;
    format = XV_YUY2;
    break;
  case 2:
    res = MODE_640x480_RGB;
    device_width = 640;
    device_height = 480;
    format = XV_YUY2;
    break;
  default:
    res = MODE_320x240_YUV422;
    device_width = 320;
    device_height = 240;
    format = XV_UYVY;
    break;
  }

  /* get the number of ports (cards) */
  raw_handle = raw1394_new_handle();
  if (raw_handle == NULL) {
    perror("Unable to aquire a raw1394 handle\n");
    perror("did you load the drivers?\n");
    exit(-1);
  }

  numPorts = raw1394_get_port_info(raw_handle, ports, numPorts);
  raw1394_destroy_handle(raw_handle);
  if (verbose) printf("number of ports = %d\n", numPorts);

  /* get dc1394 handle to each port */
  for (p = 0; p < numPorts; p++) {
    int                       camCount;

    handles[p] = dc1394_create_handle(p);
    if (handles[p] == NULL) {
      perror("Unable to aquire a raw1394 handle\n");
      perror("did you load the drivers?\n");
      cleanup();
      exit(-1);
    }

    /* get the camera nodes and describe them as we find them */
    camera_nodes = dc1394_get_camera_nodes(handles[p], &camCount, verbose);

    /* setup cameras for capture */
    for (i = 0; i < camCount; i++) {
      cameras[numCameras].node = camera_nodes[i];

      if (dc1394_get_camera_feature_set
          (handles[p], cameras[numCameras].node,
           &features) != DC1394_SUCCESS) {
        printf("unable to get feature set\n");
      } else if (verbose) {
        dc1394_print_feature_set(&features);
      }

      if (dc1394_get_iso_channel_and_speed
          (handles[p], cameras[numCameras].node, &channel,
           &speed) != DC1394_SUCCESS) {
        printf("unable to get the iso channel number\n");
        cleanup();
        exit(-1);
      }

      if (dc1394_dma_setup_capture
          (handles[p], cameras[numCameras].node, i + 1 /*channel */ ,
           FORMAT_VGA_NONCOMPRESSED, res, SPEED_400, fps, NUM_BUFFERS,
           DROP_FRAMES, device_name,
           &cameras[numCameras]) != DC1394_SUCCESS) {
        fprintf(stderr,
                "unable to setup camera - check line %d of %s to make sure\n",
                __LINE__, __FILE__);
        perror("that the video mode, framerate and format are supported\n");
        printf("by your camera(s)\n");
        cleanup();
        exit(-1);
      }


      /*have the camera start sending us data */
      if (dc1394_start_iso_transmission
          (handles[p], cameras[numCameras].node) != DC1394_SUCCESS) {
        perror("unable to start camera iso transmission\n");
        cleanup();
        exit(-1);
      }
      numCameras++;
    }
  }

  fflush(stdout);
  if (numCameras < 1) {
    perror("no cameras found :(\n");
    cleanup();
    exit(-1);
  }


  //set_manual_exposure_gain(0, 440, 30);

  switch (format) {
  case XV_YV12:
    set_frame_length(device_width * device_height * 3 / 2, numCameras);
    break;
  case XV_YUY2:
  case XV_UYVY:
    set_frame_length(device_width * device_height * 2, numCameras);
    break;
  default:
    fprintf(stderr, "Unknown format set (internal error)\n");
    exit(255);
  }

  /* create OpenCV image wrappers */
  for (cn = 0; cn < MAX_CAMERAS; cn++) {
    if (cn < numCameras) {
      iplImages[cn] =
        cvCreateImage(cvSize(device_width, device_height),
		      IPL_DEPTH_8U, 3);
      readOnlyImg =
        cvCreateImageHeader(cvSize(device_width, device_height),
                            IPL_DEPTH_8U, 3);
    } else {
      iplImages[cn] = NULL;
    }
  }

  /* initialize handvu */
  hvInitialize(device_width, device_height);
  hvLoadConductor(string(conductor_fname));
  hvStartRecognition();
  hvSetOverlayLevel(3);
  if (async_processing) {
    hvAsyncSetup(num_async_bufs, displayCallback);
    hvAsyncGetImageBuffer(&m_async_image, &m_async_bufID);
    if (sync_display) async_display_image = cvCloneImage(iplImages[0]);
  }  

  /* make the window */
  display = XOpenDisplay(getenv("DISPLAY"));
  if (display == NULL) {
    fprintf(stderr, "Could not open display \"%s\"\n", getenv("DISPLAY"));
    cleanup();
    exit(-1);
  }

  QueryXv();

  if (adaptor < 0) {
    cleanup();
    exit(-1);
  }

  width = device_width;
  height = device_height * numCameras;

  window =
    XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0,
                        width, height, 0, WhitePixel(display,
                                                     DefaultScreen
                                                     (display)), background);

  XSelectInput(display, window, StructureNotifyMask | KeyPressMask);
  XMapWindow(display, window);
  connection = ConnectionNumber(display);

  gc = XCreateGC(display, window, 0, &xgcv);

  /* local main event loop */
  while (1) {

    if (async_processing) {
      // asynchronous processing in HandVu

      capture_frame();
      process_frame();
      if (sync_display) display_frame();
      handle_events();
      
    } else {
      // synchronous processing in HandVu
      capture_frame();
      process_frame();
      display_frame();
      handle_events();
    }

    /* XPending */

  }                             /* while not interrupted */

  exit(0);
}