FACT++  1.0
Bool_t MVideo::Init ( Int_t  channel)
private

Definition at line 500 of file MVideo.cc.

References buffer, fBuffers, fFileDesc, fPath, gLog, i, Ioctl(), and IsOpen().

Referenced by Open().

501 {
502  if (IsOpen())
503  {
504  gLog << warn << "WARNING - Device " << fPath << " already open." << endl;
505  return kTRUE;
506  }
507 
508  gLog << all << "Opening " << fPath << "... " << flush;
509  do
510  {
511  fFileDesc = open(fPath, O_RDWR|O_NONBLOCK, 0);
512  usleep(1);
513  }
514  while (errno==19 && fFileDesc==-1);
515 
516  if (fFileDesc == -1)
517  {
518  gLog << err << "ERROR: " << strerror(errno) << endl;
519  return kFALSE;
520  }
521 
522  gLog << "done (" << fFileDesc << ")." << endl;
523 
524  // Close device on exit
525  if (fcntl(fFileDesc, F_SETFD, FD_CLOEXEC)<0)
526  {
527  gLog << err << "ERROR - Call to fnctl (F_SETFD, FD_CLOEXEC) failed." << endl;
528  return kFALSE;
529  }
530 
531 
532 
533 /*
534  if (!Enumerate(fInputs, VIDIOC_ENUMINPUT))
535  {
536  gLog << err << "ERROR - Could not enumerate inputs." << endl;
537  return kFALSE;
538  }
539  PrintInputs();
540 
541  if (!Enumerate(fStandards, VIDIOC_ENUMSTD))
542  {
543  gLog << err << "ERROR - Could not enumerate inputs." << endl;
544  return kFALSE;
545  }
546  PrintStandards();
547  */
548 
549  int index = 3;
550  if (Ioctl(VIDIOC_S_INPUT, &index)==-1)
551  {
552  gLog << err << "ERROR - Could not set input." << endl;
553  return kFALSE;
554  }
555 
556  //check the input
557  if (Ioctl(VIDIOC_G_INPUT, &index))
558  {
559  gLog << err << "ERROR - Could not get input." << endl;
560  return kFALSE;
561  }
562 
563  v4l2_input input;
564  memset(&input, 0, sizeof (input));
565  input.index = index;
566  if (Ioctl(VIDIOC_ENUMINPUT, &input))
567  {
568  gLog << err << "ERROR - Could enum input." << endl;
569  return kFALSE;
570  }
571  gLog << "*** Input: " << input.name << " (" << input.index << ")" << endl;
572 
573  v4l2_std_id st = 4;//standard.id;
574  if (Ioctl (VIDIOC_S_STD, &st))
575  {
576  gLog << err << "ERROR - Could not set standard." << endl;
577  return kFALSE;
578  }
579 
580  v4l2_capability cap;
581  if (Ioctl(VIDIOC_QUERYCAP, &cap))
582  {
583  gLog << err << "ERROR - Could not get capabilities." << endl;
584  return kFALSE;
585  }
586 
587  if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
588  {
589  gLog << err << "ERROR - No capture capabaility." << endl;
590  return kFALSE;
591  }
592 
593  v4l2_cropcap cropcap;
594  cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
595 
596  if (Ioctl(VIDIOC_CROPCAP, &cropcap)==-1)
597  {
598  }
599 
600  v4l2_crop crop;
601  crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
602  crop.c = cropcap.defrect; /* reset to default */
603 
604  if (Ioctl(VIDIOC_S_CROP, &crop))
605  {
606  gLog << err << "Could not reset cropping." << endl;
607  return kFALSE;
608  }
609 
610  v4l2_format fmt;
611  fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
612  fmt.fmt.pix.width = 768;
613  fmt.fmt.pix.height = 576;
614  fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
615 
616  if (Ioctl(VIDIOC_S_FMT, &fmt)==-1)
617  {
618  gLog << err << "ERROR - Could not set format." << endl;
619  return kFALSE;
620  }
621  // The image format must be selected before buffers are allocated,
622  // with the VIDIOC_S_FMT ioctl. When no format is selected the driver
623  // may use the last, possibly by another application requested format.
624 
625  v4l2_requestbuffers reqbuf;
626  memset (&reqbuf, 0, sizeof (reqbuf));
627 
628  reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
629  reqbuf.memory = V4L2_MEMORY_MMAP;
630  reqbuf.count = 4;//125;
631 
632  if (Ioctl(VIDIOC_REQBUFS, &reqbuf)==-1)
633  {
634  gLog << err << "ERROR - Couldn't setup frame buffers." << endl;
635  return kFALSE;
636  }
637 
638  gLog << all << "Allocated " << reqbuf.count << " frame buffers." << endl;
639 
640  for (unsigned int i=0; i<reqbuf.count; i++)
641  {
642  v4l2_buffer buffer;
643  memset (&buffer, 0, sizeof (buffer));
644 
645  buffer.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
646  buffer.memory = V4L2_MEMORY_MMAP;
647  buffer.index = i;
648 
649  if (Ioctl(VIDIOC_QUERYBUF, &buffer))
650  {
651  gLog << err << "ERROR - Request of frame buffer " << i << " failed." << endl;
652  return kFALSE;
653  }
654 
655  void *ptr = mmap(NULL, buffer.length,
656  PROT_READ | PROT_WRITE,
657  MAP_SHARED,
658  fFileDesc, buffer.m.offset);
659 
660  if (MAP_FAILED == ptr)
661  {
662 
663  gLog << err << "ERROR - Could not allocate shared memory." << endl;
664  return kFALSE;
665  // If you do not exit here you should unmap() and free()
666  // the buffers mapped so far.
667  //perror ("mmap");
668  //exit (EXIT_FAILURE);
669  }
670 
671  fBuffers.push_back(make_pair(buffer, ptr));
672  }
673 
674  return kTRUE;
675 }
int fFileDesc
Definition: MVideo.h:54
TString fPath
Definition: MVideo.h:52
int i
Definition: db_dim_client.c:21
int Ioctl(int req, void *opt, bool allowirq=true, bool force=false) const
Definition: MVideo.cc:108
Bool_t IsOpen() const
Definition: MVideo.h:105
#define gLog
Definition: fits.h:36
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
std::vector< std::pair< v4l2_buffer, void * > > fBuffers
Definition: MVideo.h:69

+ Here is the call graph for this function:

+ Here is the caller graph for this function: