sexta-feira, 18 de janeiro de 2013

The fifth element

Installing the fifth camera for testing, we had problems and this did not work at all.

Searching in Google, I remembered that the GV-800 card is a multichannel card with four Bt8xx chips, each one with four channels, that is, each chip can control up to four cameras.

The problem is that each chip can control only one camera at a time, and we can not read two or more of these four cameras simultaneously. So we must have some synchronization mechanism to control the capture from these cameras connected to the same chip (device).



So, a implemented a function that acquires a exclusive lock on the device file descriptor (/dev/video0, /dev/video1, etc).

void acquire_file_lock(int fd, int retry_ms)
{
  int rc;

  for (;;) {
    rc = flock(fd, LOCK_EX | LOCK_NB); // grab exclusive lock, fail if can't obtain.
    if (rc == -1) {
      // fail - sleep retry_ms (ms) and try again
      xsleep(0, retry_ms);
    } else {
      // success - return
      return;
    }
  }
}

In the main loop that reads the device, we acquire the lock, read the device and release the lock, ensuring the synchronization between the cameras connected to the same device, preventing concurrent accesses.

static void mainloop(void)
{
  ...

  while (1) {
    acquire_file_lock(fd, 10);
    init_device();

    // read the device - capture frame
    ...

    release_file_lock(fd);

    ...
  } // while (1)
}

This way, our fifth camera started to work ok.

Links:

Nenhum comentário:

Postar um comentário