2014-01-04 13:24:05 +01:00
|
|
|
#include <iostream>
|
2014-01-26 12:35:31 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
2014-01-04 13:24:05 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2016-07-14 23:40:10 +02:00
|
|
|
#include <sstream>
|
2016-07-24 22:45:26 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2014-01-04 13:24:05 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/videodev2.h>
|
2016-07-24 22:45:26 +02:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
#include <QDirIterator>
|
|
|
|
#include <QFileInfo>
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2014-02-07 21:11:50 +01:00
|
|
|
#include "grabber/V4L2Grabber.h"
|
2014-01-04 13:24:05 +01:00
|
|
|
|
|
|
|
#define CLEAR(x) memset(&(x), 0, sizeof(x))
|
|
|
|
|
2014-03-04 22:04:15 +01:00
|
|
|
V4L2Grabber::V4L2Grabber(const std::string & device,
|
2016-05-26 23:44:27 +02:00
|
|
|
int input,
|
|
|
|
VideoStandard videoStandard,
|
|
|
|
PixelFormat pixelFormat,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int frameDecimation,
|
|
|
|
int horizontalPixelDecimation,
|
2016-07-14 23:40:10 +02:00
|
|
|
int verticalPixelDecimation)
|
|
|
|
: _deviceName(device)
|
|
|
|
, _input(input)
|
|
|
|
, _videoStandard(videoStandard)
|
|
|
|
, _ioMethod(IO_METHOD_MMAP)
|
|
|
|
, _fileDescriptor(-1)
|
|
|
|
, _buffers()
|
|
|
|
, _pixelFormat(pixelFormat)
|
|
|
|
, _width(width)
|
|
|
|
, _height(height)
|
|
|
|
, _lineLength(-1)
|
|
|
|
, _frameByteSize(-1)
|
|
|
|
, _frameDecimation(std::max(1, frameDecimation))
|
|
|
|
, _noSignalCounterThreshold(50)
|
|
|
|
, _noSignalThresholdColor(ColorRgb{0,0,0})
|
|
|
|
, _currentFrame(0)
|
|
|
|
, _noSignalCounter(0)
|
|
|
|
, _streamNotifier(nullptr)
|
|
|
|
, _imageResampler()
|
|
|
|
, _log(Logger::getInstance("V4L2GRABBER"))
|
|
|
|
,_initialized(false)
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
_imageResampler.setHorizontalPixelDecimation(std::max(1, horizontalPixelDecimation));
|
|
|
|
_imageResampler.setVerticalPixelDecimation(std::max(1, verticalPixelDecimation));
|
2014-12-16 21:30:08 +01:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
getV4Ldevices();
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
V4L2Grabber::~V4L2Grabber()
|
2016-07-14 23:40:10 +02:00
|
|
|
{
|
|
|
|
uninit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::uninit()
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
// stop if the grabber was not stopped
|
2016-07-14 23:40:10 +02:00
|
|
|
if (_initialized)
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
uninit_device();
|
|
|
|
close_device();
|
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool V4L2Grabber::init()
|
|
|
|
{
|
2016-07-20 16:04:56 +02:00
|
|
|
if (! _initialized)
|
2016-07-14 23:40:10 +02:00
|
|
|
{
|
2016-07-20 16:04:56 +02:00
|
|
|
getV4Ldevices();
|
2016-07-24 15:18:34 +02:00
|
|
|
std::string v4lDevices_str;
|
|
|
|
|
|
|
|
// show list only once
|
|
|
|
if ( ! QString(_deviceName.c_str()).startsWith("/dev/") )
|
|
|
|
{
|
|
|
|
for (auto& dev: _v4lDevices)
|
|
|
|
{
|
|
|
|
v4lDevices_str += "\t"+ dev.first + "\t" + dev.second + "\n";
|
|
|
|
}
|
|
|
|
Info(_log, "available V4L2 devices:\n%s", v4lDevices_str.c_str());
|
|
|
|
}
|
|
|
|
|
2016-07-20 16:04:56 +02:00
|
|
|
if ( _deviceName == "auto" )
|
2016-07-14 23:40:10 +02:00
|
|
|
{
|
2016-07-24 15:18:34 +02:00
|
|
|
_deviceName = "unknown";
|
2016-07-20 16:04:56 +02:00
|
|
|
for (auto& dev: _v4lDevices)
|
2016-07-14 23:40:10 +02:00
|
|
|
{
|
2016-07-20 16:04:56 +02:00
|
|
|
_deviceName = dev.first;
|
|
|
|
if ( init() )
|
|
|
|
{
|
|
|
|
Info(_log, "found usable v4l2 device: %s (%s)",dev.first.c_str(), dev.second.c_str());
|
2016-07-24 22:45:26 +02:00
|
|
|
return _initialized;
|
2016-07-20 16:04:56 +02:00
|
|
|
}
|
2016-07-14 23:40:10 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 15:18:34 +02:00
|
|
|
else if ( ! QString(_deviceName.c_str()).startsWith("/dev/") )
|
|
|
|
{
|
|
|
|
for (auto& dev: _v4lDevices)
|
|
|
|
{
|
|
|
|
if ( QString(_deviceName.c_str()).toLower() == QString(dev.second.c_str()).toLower() )
|
|
|
|
{
|
|
|
|
_deviceName = dev.first;
|
|
|
|
Info(_log, "found v4l2 device with configured name: %s (%s)", dev.second.c_str(), dev.first.c_str() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 16:04:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Info(_log, "configured v4l device: %s", _deviceName.c_str());
|
|
|
|
}
|
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
bool opened = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
open_device();
|
|
|
|
opened = true;
|
|
|
|
init_device(_videoStandard, _input);
|
|
|
|
_initialized = true;
|
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
if (opened)
|
|
|
|
{
|
|
|
|
uninit_device();
|
|
|
|
close_device();
|
|
|
|
}
|
|
|
|
Error( _log, "V4l2 init failed (%s)", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::getV4Ldevices()
|
|
|
|
{
|
|
|
|
QDirIterator it("/sys/class/video4linux/", QDirIterator::NoIteratorFlags);
|
|
|
|
while(it.hasNext())
|
|
|
|
{
|
|
|
|
//_v4lDevices
|
|
|
|
QString dev = it.next();
|
|
|
|
if (it.fileName().startsWith("video"))
|
|
|
|
{
|
|
|
|
QFile devNameFile(dev+"/name");
|
|
|
|
QString devName;
|
|
|
|
if ( devNameFile.exists())
|
|
|
|
{
|
|
|
|
devNameFile.open(QFile::ReadOnly);
|
|
|
|
devName = devNameFile.readLine();
|
|
|
|
devName = devName.trimmed();
|
|
|
|
devNameFile.close();
|
|
|
|
}
|
|
|
|
_v4lDevices.emplace("/dev/"+it.fileName().toStdString(), devName.toStdString());
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
2014-02-04 21:53:36 +01:00
|
|
|
void V4L2Grabber::setCropping(int cropLeft, int cropRight, int cropTop, int cropBottom)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
_imageResampler.setCropping(cropLeft, cropRight, cropTop, cropBottom);
|
2014-02-04 21:53:36 +01:00
|
|
|
}
|
|
|
|
|
2014-02-19 21:52:37 +01:00
|
|
|
void V4L2Grabber::set3D(VideoMode mode)
|
2014-02-04 21:53:36 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
_imageResampler.set3D(mode);
|
2014-02-04 21:53:36 +01:00
|
|
|
}
|
|
|
|
|
2014-03-04 22:04:15 +01:00
|
|
|
void V4L2Grabber::setSignalThreshold(double redSignalThreshold, double greenSignalThreshold, double blueSignalThreshold, int noSignalCounterThreshold)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
_noSignalThresholdColor.red = uint8_t(255*redSignalThreshold);
|
|
|
|
_noSignalThresholdColor.green = uint8_t(255*greenSignalThreshold);
|
|
|
|
_noSignalThresholdColor.blue = uint8_t(255*blueSignalThreshold);
|
|
|
|
_noSignalCounterThreshold = std::max(1, noSignalCounterThreshold);
|
2014-03-22 15:41:52 +01:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << _noSignalThresholdColor;
|
|
|
|
Info(_log, "Signal threshold set to: %s", ss.str().c_str() );
|
2014-03-04 22:04:15 +01:00
|
|
|
}
|
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
bool V4L2Grabber::start()
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
if (init() && _streamNotifier != nullptr && !_streamNotifier->isEnabled())
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
|
|
|
_streamNotifier->setEnabled(true);
|
|
|
|
start_capturing();
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Started");
|
2016-07-14 23:40:10 +02:00
|
|
|
return true;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2016-07-14 23:40:10 +02:00
|
|
|
|
|
|
|
return false;
|
2014-01-11 20:43:55 +01:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2014-01-11 20:43:55 +01:00
|
|
|
void V4L2Grabber::stop()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
if (_streamNotifier != nullptr && _streamNotifier->isEnabled())
|
|
|
|
{
|
|
|
|
stop_capturing();
|
|
|
|
_streamNotifier->setEnabled(false);
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Stopped");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::open_device()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (-1 == stat(_deviceName.c_str(), &st))
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_errno_exception("Cannot identify '" + _deviceName + "'");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISCHR(st.st_mode))
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' is no device");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_fileDescriptor = open(_deviceName.c_str(), O_RDWR /* required */ | O_NONBLOCK, 0);
|
|
|
|
|
|
|
|
if (-1 == _fileDescriptor)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_errno_exception("Cannot open '" + _deviceName + "'");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the notifier for when a new frame is available
|
|
|
|
_streamNotifier = new QSocketNotifier(_fileDescriptor, QSocketNotifier::Read);
|
|
|
|
_streamNotifier->setEnabled(false);
|
|
|
|
connect(_streamNotifier, SIGNAL(activated(int)), this, SLOT(read_frame()));
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::close_device()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
if (-1 == close(_fileDescriptor))
|
|
|
|
throw_errno_exception("close");
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
_fileDescriptor = -1;
|
2014-02-19 21:52:37 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
if (_streamNotifier != nullptr)
|
|
|
|
{
|
|
|
|
delete _streamNotifier;
|
|
|
|
_streamNotifier = nullptr;
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::init_read(unsigned int buffer_size)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
_buffers.resize(1);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
_buffers[0].length = buffer_size;
|
|
|
|
_buffers[0].start = malloc(buffer_size);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
if (!_buffers[0].start) {
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("Out of memory");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::init_mmap()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct v4l2_requestbuffers req;
|
|
|
|
|
|
|
|
CLEAR(req);
|
|
|
|
|
|
|
|
req.count = 4;
|
|
|
|
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
req.memory = V4L2_MEMORY_MMAP;
|
|
|
|
|
|
|
|
if (-1 == xioctl(VIDIOC_REQBUFS, &req)) {
|
|
|
|
if (EINVAL == errno) {
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' does not support memory mapping");
|
2016-05-26 23:44:27 +02:00
|
|
|
} else {
|
|
|
|
throw_errno_exception("VIDIOC_REQBUFS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.count < 2) {
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("Insufficient buffer memory on " + _deviceName);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_buffers.resize(req.count);
|
|
|
|
|
|
|
|
for (size_t n_buffers = 0; n_buffers < req.count; ++n_buffers) {
|
|
|
|
struct v4l2_buffer buf;
|
|
|
|
|
|
|
|
CLEAR(buf);
|
|
|
|
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
buf.index = n_buffers;
|
|
|
|
|
|
|
|
if (-1 == xioctl(VIDIOC_QUERYBUF, &buf))
|
|
|
|
throw_errno_exception("VIDIOC_QUERYBUF");
|
|
|
|
|
|
|
|
_buffers[n_buffers].length = buf.length;
|
|
|
|
_buffers[n_buffers].start =
|
|
|
|
mmap(NULL /* start anywhere */,
|
|
|
|
buf.length,
|
|
|
|
PROT_READ | PROT_WRITE /* required */,
|
|
|
|
MAP_SHARED /* recommended */,
|
|
|
|
_fileDescriptor, buf.m.offset);
|
|
|
|
|
|
|
|
if (MAP_FAILED == _buffers[n_buffers].start)
|
|
|
|
throw_errno_exception("mmap");
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::init_userp(unsigned int buffer_size)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct v4l2_requestbuffers req;
|
|
|
|
|
|
|
|
CLEAR(req);
|
|
|
|
|
|
|
|
req.count = 4;
|
|
|
|
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
req.memory = V4L2_MEMORY_USERPTR;
|
|
|
|
|
|
|
|
if (-1 == xioctl(VIDIOC_REQBUFS, &req)) {
|
|
|
|
if (EINVAL == errno)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' does not support user pointer");
|
2016-05-26 23:44:27 +02:00
|
|
|
} else {
|
|
|
|
throw_errno_exception("VIDIOC_REQBUFS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffers.resize(4);
|
|
|
|
|
|
|
|
for (size_t n_buffers = 0; n_buffers < 4; ++n_buffers) {
|
|
|
|
_buffers[n_buffers].length = buffer_size;
|
|
|
|
_buffers[n_buffers].start = malloc(buffer_size);
|
|
|
|
|
|
|
|
if (!_buffers[n_buffers].start) {
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("Out of memory");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-11 20:43:55 +01:00
|
|
|
void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct v4l2_capability cap;
|
|
|
|
if (-1 == xioctl(VIDIOC_QUERYCAP, &cap))
|
|
|
|
{
|
|
|
|
if (EINVAL == errno) {
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' is no V4L2 device");
|
2016-05-26 23:44:27 +02:00
|
|
|
} else {
|
|
|
|
throw_errno_exception("VIDIOC_QUERYCAP");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' is no video capture device");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (_ioMethod) {
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
if (!(cap.capabilities & V4L2_CAP_READWRITE))
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' does not support read i/o");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
if (!(cap.capabilities & V4L2_CAP_STREAMING))
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' does not support streaming i/o");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Select video input, video standard and tune here. */
|
|
|
|
|
|
|
|
struct v4l2_cropcap cropcap;
|
|
|
|
CLEAR(cropcap);
|
|
|
|
|
|
|
|
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
|
|
|
|
if (0 == xioctl(VIDIOC_CROPCAP, &cropcap)) {
|
|
|
|
struct v4l2_crop crop;
|
|
|
|
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
crop.c = cropcap.defrect; /* reset to default */
|
|
|
|
|
|
|
|
if (-1 == xioctl(VIDIOC_S_CROP, &crop)) {
|
|
|
|
switch (errno) {
|
|
|
|
case EINVAL:
|
|
|
|
/* Cropping not supported. */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Errors ignored. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Errors ignored. */
|
|
|
|
}
|
|
|
|
|
|
|
|
// set input if needed
|
|
|
|
if (input >= 0)
|
|
|
|
{
|
|
|
|
if (-1 == xioctl(VIDIOC_S_INPUT, &input))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_INPUT");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the video standard if needed
|
|
|
|
switch (videoStandard)
|
|
|
|
{
|
|
|
|
case VIDEOSTANDARD_PAL:
|
|
|
|
{
|
|
|
|
v4l2_std_id std_id = V4L2_STD_PAL;
|
|
|
|
if (-1 == xioctl(VIDIOC_S_STD, &std_id))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_STD");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VIDEOSTANDARD_NTSC:
|
|
|
|
{
|
|
|
|
v4l2_std_id std_id = V4L2_STD_NTSC;
|
|
|
|
if (-1 == xioctl(VIDIOC_S_STD, &std_id))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_STD");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VIDEOSTANDARD_NO_CHANGE:
|
|
|
|
default:
|
|
|
|
// No change to device settings
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the current settings
|
|
|
|
struct v4l2_format fmt;
|
|
|
|
CLEAR(fmt);
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (-1 == xioctl(VIDIOC_G_FMT, &fmt))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_G_FMT");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the requested pixel format
|
|
|
|
switch (_pixelFormat)
|
|
|
|
{
|
|
|
|
case PIXELFORMAT_UYVY:
|
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
|
|
|
|
break;
|
|
|
|
case PIXELFORMAT_YUYV:
|
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
|
|
|
|
break;
|
|
|
|
case PIXELFORMAT_RGB32:
|
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
|
|
|
|
break;
|
|
|
|
case PIXELFORMAT_NO_CHANGE:
|
|
|
|
default:
|
|
|
|
// No change to device settings
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the requested withd and height
|
|
|
|
if (_width > 0 || _height > 0)
|
|
|
|
{
|
|
|
|
if (_width > 0)
|
|
|
|
{
|
|
|
|
fmt.fmt.pix.width = _width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fmt.fmt.pix.height > 0)
|
|
|
|
{
|
|
|
|
fmt.fmt.pix.height = _height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the line length
|
|
|
|
_lineLength = fmt.fmt.pix.bytesperline;
|
|
|
|
|
|
|
|
// set the settings
|
|
|
|
if (-1 == xioctl(VIDIOC_S_FMT, &fmt))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_FMT");
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the format settings again
|
|
|
|
// (the size may not have been accepted without an error)
|
|
|
|
if (-1 == xioctl(VIDIOC_G_FMT, &fmt))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_G_FMT");
|
|
|
|
}
|
|
|
|
|
|
|
|
// store width & height
|
|
|
|
_width = fmt.fmt.pix.width;
|
|
|
|
_height = fmt.fmt.pix.height;
|
|
|
|
|
2016-07-12 15:13:06 +02:00
|
|
|
// display the used width and height
|
2016-07-14 23:40:10 +02:00
|
|
|
Info(_log, "width=%d height=%d", _width, _height );
|
2016-07-12 15:13:06 +02:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// check pixel format and frame size
|
|
|
|
switch (fmt.fmt.pix.pixelformat)
|
|
|
|
{
|
|
|
|
case V4L2_PIX_FMT_UYVY:
|
|
|
|
_pixelFormat = PIXELFORMAT_UYVY;
|
|
|
|
_frameByteSize = _width * _height * 2;
|
2016-07-12 00:53:48 +02:00
|
|
|
Debug(_log, "Pixel format=UYVY");
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_YUYV:
|
|
|
|
_pixelFormat = PIXELFORMAT_YUYV;
|
|
|
|
_frameByteSize = _width * _height * 2;
|
2016-07-12 00:53:48 +02:00
|
|
|
Debug(_log, "Pixel format=YUYV");
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
case V4L2_PIX_FMT_RGB32:
|
|
|
|
_pixelFormat = PIXELFORMAT_RGB32;
|
|
|
|
_frameByteSize = _width * _height * 4;
|
2016-07-12 00:53:48 +02:00
|
|
|
Debug(_log, "Pixel format=RGB32");
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("Only pixel formats UYVY, YUYV, and RGB32 are supported");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (_ioMethod) {
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
init_read(fmt.fmt.pix.sizeimage);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
init_mmap();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
init_userp(fmt.fmt.pix.sizeimage);
|
|
|
|
break;
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::uninit_device()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
switch (_ioMethod) {
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
free(_buffers[0].start);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i)
|
|
|
|
if (-1 == munmap(_buffers[i].start, _buffers[i].length))
|
|
|
|
throw_errno_exception("munmap");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i)
|
|
|
|
free(_buffers[i].start);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffers.resize(0);
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::start_capturing()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
switch (_ioMethod) {
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
/* Nothing to do. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i) {
|
|
|
|
struct v4l2_buffer buf;
|
|
|
|
|
|
|
|
CLEAR(buf);
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
buf.index = i;
|
|
|
|
|
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
|
|
|
}
|
|
|
|
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (-1 == xioctl(VIDIOC_STREAMON, &type))
|
|
|
|
throw_errno_exception("VIDIOC_STREAMON");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i) {
|
|
|
|
struct v4l2_buffer buf;
|
|
|
|
|
|
|
|
CLEAR(buf);
|
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_USERPTR;
|
|
|
|
buf.index = i;
|
|
|
|
buf.m.userptr = (unsigned long)_buffers[i].start;
|
|
|
|
buf.length = _buffers[i].length;
|
|
|
|
|
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
|
|
|
}
|
|
|
|
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (-1 == xioctl(VIDIOC_STREAMON, &type))
|
|
|
|
throw_errno_exception("VIDIOC_STREAMON");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::stop_capturing()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
enum v4l2_buf_type type;
|
|
|
|
|
|
|
|
switch (_ioMethod) {
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
/* Nothing to do. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (-1 == xioctl(VIDIOC_STREAMOFF, &type))
|
|
|
|
throw_errno_exception("VIDIOC_STREAMOFF");
|
|
|
|
break;
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int V4L2Grabber::read_frame()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
bool rc = false;
|
2014-01-25 17:35:06 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
struct v4l2_buffer buf;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
switch (_ioMethod) {
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
int size;
|
|
|
|
if ((size = read(_fileDescriptor, _buffers[0].start, _buffers[0].length)) == -1)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
return 0;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
case EIO:
|
|
|
|
/* Could ignore EIO, see spec. */
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
/* fall through */
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
default:
|
|
|
|
throw_errno_exception("read");
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
rc = process_image(_buffers[0].start, size);
|
|
|
|
break;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
CLEAR(buf);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_DQBUF, &buf))
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
return 0;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
case EIO:
|
|
|
|
/* Could ignore EIO, see spec. */
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
/* fall through */
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
default:
|
|
|
|
throw_errno_exception("VIDIOC_DQBUF");
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
assert(buf.index < _buffers.size());
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
rc = process_image(_buffers[buf.index].start, buf.bytesused);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
|
|
|
}
|
2014-01-11 20:43:55 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
CLEAR(buf);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_USERPTR;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_DQBUF, &buf))
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
return 0;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
case EIO:
|
|
|
|
/* Could ignore EIO, see spec. */
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
/* fall through */
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
default:
|
|
|
|
throw_errno_exception("VIDIOC_DQBUF");
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i)
|
|
|
|
{
|
|
|
|
if (buf.m.userptr == (unsigned long)_buffers[i].start && buf.length == _buffers[i].length)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
rc = process_image((void *)buf.m.userptr, buf.bytesused);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
return rc ? 1 : 0;
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-25 17:35:06 +01:00
|
|
|
bool V4L2Grabber::process_image(const void *p, int size)
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
if (++_currentFrame >= _frameDecimation)
|
|
|
|
{
|
|
|
|
// We do want a new frame...
|
|
|
|
|
|
|
|
if (size != _frameByteSize)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Error(_log, "Frame too small: %d != %d", size, _frameByteSize);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
process_image(reinterpret_cast<const uint8_t *>(p));
|
|
|
|
_currentFrame = 0; // restart counting
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-01-11 20:43:55 +01:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2014-01-11 20:43:55 +01:00
|
|
|
void V4L2Grabber::process_image(const uint8_t * data)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
Image<ColorRgb> image(0, 0);
|
|
|
|
_imageResampler.processImage(data, _width, _height, _lineLength, _pixelFormat, image);
|
|
|
|
|
|
|
|
// check signal (only in center of the resulting image, because some grabbers have noise values along the borders)
|
|
|
|
bool noSignal = true;
|
|
|
|
for (unsigned x = 0; noSignal && x < (image.width()>>1); ++x)
|
|
|
|
{
|
|
|
|
int xImage = (image.width()>>2) + x;
|
|
|
|
|
|
|
|
for (unsigned y = 0; noSignal && y < (image.height()>>1); ++y)
|
|
|
|
{
|
|
|
|
int yImage = (image.height()>>2) + y;
|
|
|
|
|
|
|
|
ColorRgb & rgb = image(xImage, yImage);
|
|
|
|
noSignal &= rgb <= _noSignalThresholdColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (noSignal)
|
|
|
|
{
|
|
|
|
++_noSignalCounter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_noSignalCounter >= _noSignalCounterThreshold)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Signal detected");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_noSignalCounter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_noSignalCounter < _noSignalCounterThreshold)
|
|
|
|
{
|
|
|
|
emit newFrame(image);
|
|
|
|
}
|
|
|
|
else if (_noSignalCounter == _noSignalCounterThreshold)
|
|
|
|
{
|
2016-07-12 00:53:48 +02:00
|
|
|
Info(_log, "Signal lost");
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int V4L2Grabber::xioctl(int request, void *arg)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
int r;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
r = ioctl(_fileDescriptor, request, arg);
|
|
|
|
}
|
|
|
|
while (-1 == r && EINTR == errno);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
return r;
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 12:35:31 +01:00
|
|
|
void V4L2Grabber::throw_exception(const std::string & error)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw std::runtime_error(error);
|
2014-01-26 12:35:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::throw_errno_exception(const std::string & error)
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
std::ostringstream oss;
|
2016-07-14 23:40:10 +02:00
|
|
|
oss << error << " error code " << errno << ", " << strerror(errno);
|
2016-05-26 23:44:27 +02:00
|
|
|
throw std::runtime_error(oss.str());
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|