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
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
#include <hyperion/Hyperion.h>
|
2019-07-14 22:43:22 +02:00
|
|
|
#include <hyperion/HyperionIManager.h>
|
2019-04-28 19:53:45 +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))
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
#ifndef V4L2_CAP_META_CAPTURE
|
|
|
|
#define V4L2_CAP_META_CAPTURE 0x00800000 // Specified in kernel header v4.16. Required for backward compatibility.
|
|
|
|
#endif
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
V4L2Grabber::V4L2Grabber(const QString & device
|
2020-08-08 23:12:43 +02:00
|
|
|
, unsigned width
|
|
|
|
, unsigned height
|
|
|
|
, unsigned fps
|
|
|
|
, unsigned input
|
2016-12-16 19:48:43 +01:00
|
|
|
, VideoStandard videoStandard
|
|
|
|
, PixelFormat pixelFormat
|
2018-12-27 23:11:32 +01:00
|
|
|
, int pixelDecimation
|
2016-12-16 19:48:43 +01:00
|
|
|
)
|
2018-12-27 23:11:32 +01:00
|
|
|
: Grabber("V4L2:"+device)
|
2018-12-28 18:12:45 +01:00
|
|
|
, _deviceName()
|
2016-07-14 23:40:10 +02:00
|
|
|
, _videoStandard(videoStandard)
|
|
|
|
, _ioMethod(IO_METHOD_MMAP)
|
|
|
|
, _fileDescriptor(-1)
|
|
|
|
, _buffers()
|
|
|
|
, _pixelFormat(pixelFormat)
|
2018-12-28 18:12:45 +01:00
|
|
|
, _pixelDecimation(-1)
|
2016-07-14 23:40:10 +02:00
|
|
|
, _lineLength(-1)
|
|
|
|
, _frameByteSize(-1)
|
2018-12-28 18:12:45 +01:00
|
|
|
, _noSignalCounterThreshold(40)
|
2016-07-14 23:40:10 +02:00
|
|
|
, _noSignalThresholdColor(ColorRgb{0,0,0})
|
2017-03-15 20:33:11 +01:00
|
|
|
, _signalDetectionEnabled(true)
|
2020-07-20 20:06:41 +02:00
|
|
|
, _cecDetectionEnabled(true)
|
|
|
|
, _cecStandbyActivated(false)
|
2017-03-15 20:33:11 +01:00
|
|
|
, _noSignalDetected(false)
|
2017-08-01 17:29:47 +02:00
|
|
|
, _noSignalCounter(0)
|
2017-03-15 20:33:11 +01:00
|
|
|
, _x_frac_min(0.25)
|
|
|
|
, _y_frac_min(0.25)
|
|
|
|
, _x_frac_max(0.75)
|
|
|
|
, _y_frac_max(0.75)
|
2016-07-14 23:40:10 +02:00
|
|
|
, _streamNotifier(nullptr)
|
2016-08-12 09:39:41 +02:00
|
|
|
, _initialized(false)
|
|
|
|
, _deviceAutoDiscoverEnabled(false)
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
setPixelDecimation(pixelDecimation);
|
2016-07-14 23:40:10 +02:00
|
|
|
getV4Ldevices();
|
2019-02-25 21:10:44 +01:00
|
|
|
|
|
|
|
// init
|
2020-06-17 20:55:57 +02:00
|
|
|
setInput(input);
|
2020-03-27 23:13:58 +01:00
|
|
|
setWidthHeight(width, height);
|
|
|
|
setFramerate(fps);
|
2019-02-25 21:10:44 +01:00
|
|
|
setDeviceVideoStandard(device, videoStandard);
|
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)
|
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
Debug(_log,"uninit grabber: %s", QSTRING_CSTR(_deviceName));
|
2016-07-14 23:40:10 +02:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool V4L2Grabber::init()
|
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
if (!_initialized)
|
2016-07-14 23:40:10 +02:00
|
|
|
{
|
2016-07-20 16:04:56 +02:00
|
|
|
getV4Ldevices();
|
2017-03-04 22:17:42 +01:00
|
|
|
QString v4lDevices_str;
|
2017-10-04 21:23:45 +02:00
|
|
|
|
2016-07-24 15:18:34 +02:00
|
|
|
// show list only once
|
2020-07-12 09:19:59 +02:00
|
|
|
if (!_deviceName.startsWith("/dev/"))
|
2016-07-24 15:18:34 +02:00
|
|
|
{
|
|
|
|
for (auto& dev: _v4lDevices)
|
|
|
|
{
|
|
|
|
v4lDevices_str += "\t"+ dev.first + "\t" + dev.second + "\n";
|
|
|
|
}
|
2019-02-03 14:36:57 +01:00
|
|
|
if (!v4lDevices_str.isEmpty())
|
|
|
|
Info(_log, "available V4L2 devices:\n%s", QSTRING_CSTR(v4lDevices_str));
|
2016-07-24 15:18:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
if (_deviceName == "auto")
|
2016-07-14 23:40:10 +02:00
|
|
|
{
|
2016-08-12 09:39:41 +02:00
|
|
|
_deviceAutoDiscoverEnabled = true;
|
2016-07-24 15:18:34 +02:00
|
|
|
_deviceName = "unknown";
|
2016-08-12 09:39:41 +02:00
|
|
|
Info( _log, "search for usable video devices" );
|
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;
|
2020-04-17 16:59:20 +02:00
|
|
|
if (init())
|
2016-07-20 16:04:56 +02:00
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
Info(_log, "found usable v4l2 device: %s (%s)",QSTRING_CSTR(dev.first), QSTRING_CSTR(dev.second));
|
2016-08-12 09:39:41 +02:00
|
|
|
_deviceAutoDiscoverEnabled = false;
|
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
|
|
|
}
|
2020-04-17 16:59:20 +02:00
|
|
|
Info(_log, "no usable device found");
|
2016-07-14 23:40:10 +02:00
|
|
|
}
|
2020-04-17 16:59:20 +02:00
|
|
|
else if (!_deviceName.startsWith("/dev/"))
|
2016-07-24 15:18:34 +02:00
|
|
|
{
|
|
|
|
for (auto& dev: _v4lDevices)
|
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
if (_deviceName.toLower() == dev.second.toLower())
|
2016-07-24 15:18:34 +02:00
|
|
|
{
|
|
|
|
_deviceName = dev.first;
|
2017-03-04 22:17:42 +01:00
|
|
|
Info(_log, "found v4l2 device with configured name: %s (%s)", QSTRING_CSTR(dev.second), QSTRING_CSTR(dev.first) );
|
2016-07-24 15:18:34 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-20 16:04:56 +02:00
|
|
|
else
|
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
Info(_log, "%s v4l device: %s", (_deviceAutoDiscoverEnabled? "test" : "configured"), QSTRING_CSTR(_deviceName));
|
2016-07-20 16:04:56 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
bool opened = false;
|
|
|
|
try
|
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
// do not init with unknown device
|
2020-04-17 16:59:20 +02:00
|
|
|
if (_deviceName != "unknown")
|
2018-12-28 18:12:45 +01:00
|
|
|
{
|
2019-05-26 14:25:37 +02:00
|
|
|
if (open_device())
|
|
|
|
{
|
|
|
|
opened = true;
|
2020-06-17 20:55:57 +02:00
|
|
|
init_device(_videoStandard);
|
2019-05-26 14:25:37 +02:00
|
|
|
_initialized = true;
|
|
|
|
}
|
2018-12-28 18:12:45 +01:00
|
|
|
}
|
2016-07-14 23:40:10 +02:00
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
if (opened)
|
|
|
|
{
|
|
|
|
uninit_device();
|
|
|
|
close_device();
|
|
|
|
}
|
2016-08-12 09:39:41 +02:00
|
|
|
ErrorIf( !_deviceAutoDiscoverEnabled, _log, "V4l2 init failed (%s)", e.what());
|
2016-07-14 23:40:10 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-04 21:23:45 +02:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
return _initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::getV4Ldevices()
|
|
|
|
{
|
|
|
|
QDirIterator it("/sys/class/video4linux/", QDirIterator::NoIteratorFlags);
|
2020-04-17 16:59:20 +02:00
|
|
|
_deviceProperties.clear();
|
2016-07-14 23:40:10 +02:00
|
|
|
while(it.hasNext())
|
2017-10-04 21:23:45 +02:00
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
//_v4lDevices
|
|
|
|
QString dev = it.next();
|
|
|
|
if (it.fileName().startsWith("video"))
|
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
QString devName = "/dev/" + it.fileName();
|
|
|
|
int fd = open(QSTRING_CSTR(devName), O_RDWR | O_NONBLOCK, 0);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
throw_errno_exception("Cannot open '" + devName + "'");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct v4l2_capability cap;
|
|
|
|
CLEAR(cap);
|
|
|
|
|
|
|
|
if (xioctl(fd, VIDIOC_QUERYCAP, &cap) < 0)
|
|
|
|
{
|
|
|
|
throw_errno_exception("'" + devName + "' is no V4L2 device");
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap.device_caps & V4L2_CAP_META_CAPTURE) // this device has bit 23 set (and bit 1 reset), so it doesn't have capture.
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the current settings
|
|
|
|
struct v4l2_format fmt;
|
|
|
|
CLEAR(fmt);
|
|
|
|
|
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (xioctl(fd, VIDIOC_G_FMT, &fmt) < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
V4L2Grabber::DeviceProperties properties;
|
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
// collect available device inputs (index & name)
|
|
|
|
int inputIndex;
|
|
|
|
if (xioctl(fd, VIDIOC_G_INPUT, &inputIndex) == 0)
|
|
|
|
{
|
|
|
|
struct v4l2_input input;
|
|
|
|
CLEAR(input);
|
|
|
|
|
|
|
|
input.index = 0;
|
|
|
|
while (xioctl(fd, VIDIOC_ENUMINPUT, &input) >= 0)
|
|
|
|
{
|
|
|
|
properties.inputs.insert(QString((char*)input.name), input.index);
|
|
|
|
input.index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
// collect available device resolutions & frame rates
|
|
|
|
struct v4l2_frmsizeenum frmsizeenum;
|
|
|
|
CLEAR(frmsizeenum);
|
|
|
|
|
|
|
|
frmsizeenum.index = 0;
|
|
|
|
frmsizeenum.pixel_format = fmt.fmt.pix.pixelformat;
|
|
|
|
while (xioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsizeenum) >= 0)
|
|
|
|
{
|
|
|
|
switch (frmsizeenum.type)
|
|
|
|
{
|
|
|
|
case V4L2_FRMSIZE_TYPE_DISCRETE:
|
|
|
|
{
|
|
|
|
properties.resolutions << QString::number(frmsizeenum.discrete.width) + "x" + QString::number(frmsizeenum.discrete.height);
|
|
|
|
enumFrameIntervals(properties.framerates, fd, fmt.fmt.pix.pixelformat, frmsizeenum.discrete.width, frmsizeenum.discrete.height);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case V4L2_FRMSIZE_TYPE_CONTINUOUS:
|
|
|
|
case V4L2_FRMSIZE_TYPE_STEPWISE:
|
2020-06-17 20:59:26 +02:00
|
|
|
// We do not take care of V4L2_FRMSIZE_TYPE_CONTINUOUS or V4L2_FRMSIZE_TYPE_STEPWISE
|
|
|
|
break;
|
2020-04-17 16:59:20 +02:00
|
|
|
}
|
|
|
|
frmsizeenum.index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close(fd) < 0) continue;
|
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
QFile devNameFile(dev+"/name");
|
2020-04-17 16:59:20 +02:00
|
|
|
if (devNameFile.exists())
|
2016-07-14 23:40:10 +02:00
|
|
|
{
|
|
|
|
devNameFile.open(QFile::ReadOnly);
|
|
|
|
devName = devNameFile.readLine();
|
|
|
|
devName = devName.trimmed();
|
2020-04-17 16:59:20 +02:00
|
|
|
properties.name = devName;
|
2016-07-14 23:40:10 +02:00
|
|
|
devNameFile.close();
|
|
|
|
}
|
2017-03-04 22:17:42 +01:00
|
|
|
_v4lDevices.emplace("/dev/"+it.fileName(), devName);
|
2020-04-17 16:59:20 +02:00
|
|
|
_deviceProperties.insert("/dev/"+it.fileName(), properties);
|
2016-07-14 23:40:10 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
2014-03-04 22:04:15 +01:00
|
|
|
void V4L2Grabber::setSignalThreshold(double redSignalThreshold, double greenSignalThreshold, double blueSignalThreshold, int noSignalCounterThreshold)
|
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
_noSignalThresholdColor.red = uint8_t(255*redSignalThreshold);
|
2016-05-26 23:44:27 +02:00
|
|
|
_noSignalThresholdColor.green = uint8_t(255*greenSignalThreshold);
|
2017-03-04 22:17:42 +01:00
|
|
|
_noSignalThresholdColor.blue = uint8_t(255*blueSignalThreshold);
|
2017-08-04 12:01:45 +02:00
|
|
|
_noSignalCounterThreshold = qMax(1, noSignalCounterThreshold);
|
2014-03-22 15:41:52 +01:00
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
Info(_log, "Signal threshold set to: {%d, %d, %d}", _noSignalThresholdColor.red, _noSignalThresholdColor.green, _noSignalThresholdColor.blue );
|
2014-03-04 22:04:15 +01:00
|
|
|
}
|
|
|
|
|
2016-12-16 19:48:43 +01:00
|
|
|
void V4L2Grabber::setSignalDetectionOffset(double horizontalMin, double verticalMin, double horizontalMax, double verticalMax)
|
|
|
|
{
|
|
|
|
// rainbow 16 stripes 0.47 0.2 0.49 0.8
|
|
|
|
// unicolor: 0.25 0.25 0.75 0.75
|
|
|
|
|
|
|
|
_x_frac_min = horizontalMin;
|
|
|
|
_y_frac_min = verticalMin;
|
|
|
|
_x_frac_max = horizontalMax;
|
|
|
|
_y_frac_max = verticalMax;
|
|
|
|
|
|
|
|
Info(_log, "Signal detection area set to: %f,%f x %f,%f", _x_frac_min, _y_frac_min, _x_frac_max, _y_frac_max );
|
|
|
|
}
|
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
bool V4L2Grabber::start()
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-08-12 09:39:41 +02:00
|
|
|
try
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2016-08-12 09:39:41 +02:00
|
|
|
if (init() && _streamNotifier != nullptr && !_streamNotifier->isEnabled())
|
|
|
|
{
|
|
|
|
_streamNotifier->setEnabled(true);
|
|
|
|
start_capturing();
|
|
|
|
Info(_log, "Started");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
Error(_log, "start failed (%s)", e.what());
|
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);
|
2019-04-28 19:53:45 +02:00
|
|
|
uninit_device();
|
|
|
|
close_device();
|
|
|
|
_initialized = false;
|
2020-04-17 16:59:20 +02:00
|
|
|
_deviceProperties.clear();
|
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
|
|
|
}
|
|
|
|
|
2019-05-26 14:25:37 +02:00
|
|
|
bool V4L2Grabber::open_device()
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct stat st;
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
if (-1 == stat(QSTRING_CSTR(_deviceName), &st))
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_errno_exception("Cannot identify '" + _deviceName + "'");
|
2019-05-26 14:25:37 +02:00
|
|
|
return false;
|
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");
|
2019-05-26 14:25:37 +02:00
|
|
|
return false;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
_fileDescriptor = open(QSTRING_CSTR(_deviceName), O_RDWR | O_NONBLOCK, 0);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
if (-1 == _fileDescriptor)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_errno_exception("Cannot open '" + _deviceName + "'");
|
2019-05-26 14:25:37 +02:00
|
|
|
return false;
|
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);
|
2020-08-02 22:32:00 +02:00
|
|
|
connect(_streamNotifier, &QSocketNotifier::activated, this, &V4L2Grabber::read_frame);
|
2019-05-26 14:25:37 +02:00
|
|
|
return true;
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::close_device()
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
if (-1 == close(_fileDescriptor))
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
throw_errno_exception("close");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2020-07-12 09:19:59 +02:00
|
|
|
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
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (!_buffers[0].start)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("Out of memory");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
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;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
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");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
throw_errno_exception("VIDIOC_REQBUFS");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (req.count < 2)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("Insufficient buffer memory on " + _deviceName);
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_buffers.resize(req.count);
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
for (size_t n_buffers = 0; n_buffers < req.count; ++n_buffers)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct v4l2_buffer buf;
|
|
|
|
|
|
|
|
CLEAR(buf);
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
|
|
|
buf.index = n_buffers;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
if (-1 == xioctl(VIDIOC_QUERYBUF, &buf))
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
throw_errno_exception("VIDIOC_QUERYBUF");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
_buffers[n_buffers].length = buf.length;
|
2019-05-03 22:38:28 +02:00
|
|
|
_buffers[n_buffers].start = mmap(NULL /* start anywhere */,
|
|
|
|
buf.length,
|
|
|
|
PROT_READ | PROT_WRITE /* required */,
|
|
|
|
MAP_SHARED /* recommended */,
|
|
|
|
_fileDescriptor, buf.m.offset
|
|
|
|
);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
if (MAP_FAILED == _buffers[n_buffers].start)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
throw_errno_exception("mmap");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
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;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_REQBUFS, &req))
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
if (EINVAL == errno)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' does not support user pointer");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
throw_errno_exception("VIDIOC_REQBUFS");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffers.resize(4);
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
for (size_t n_buffers = 0; n_buffers < 4; ++n_buffers)
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
_buffers[n_buffers].length = buffer_size;
|
|
|
|
_buffers[n_buffers].start = malloc(buffer_size);
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (!_buffers[n_buffers].start)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("Out of memory");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
void V4L2Grabber::init_device(VideoStandard videoStandard)
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct v4l2_capability cap;
|
2020-04-17 16:59:20 +02:00
|
|
|
CLEAR(cap);
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_QUERYCAP, &cap))
|
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
if (EINVAL == errno)
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' is no V4L2 device");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
throw_errno_exception("VIDIOC_QUERYCAP");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
|
|
|
|
{
|
2016-07-14 23:40:10 +02:00
|
|
|
throw_exception("'" + _deviceName + "' is no video capture device");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (_ioMethod)
|
|
|
|
{
|
|
|
|
case IO_METHOD_READ:
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
if (!(cap.capabilities & V4L2_CAP_READWRITE))
|
|
|
|
{
|
|
|
|
throw_exception("'" + _deviceName + "' does not support read i/o");
|
|
|
|
return;
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
case IO_METHOD_USERPTR:
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
if (!(cap.capabilities & V4L2_CAP_STREAMING))
|
|
|
|
{
|
|
|
|
throw_exception("'" + _deviceName + "' does not support streaming i/o");
|
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (0 == xioctl(VIDIOC_CROPCAP, &cropcap))
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
struct v4l2_crop crop;
|
|
|
|
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
crop.c = cropcap.defrect; /* reset to default */
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_S_CROP, &crop))
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL: /* Cropping not supported. */
|
|
|
|
default: /* Errors ignored. */
|
|
|
|
break;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
/* Errors ignored. */
|
|
|
|
}
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
// set input if needed and supported
|
|
|
|
struct v4l2_input v4l2Input;
|
2020-04-17 16:59:20 +02:00
|
|
|
CLEAR(v4l2Input);
|
2020-06-17 20:55:57 +02:00
|
|
|
v4l2Input.index = _input;
|
2020-04-17 16:59:20 +02:00
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
if (_input >= 0 && 0 == xioctl(VIDIOC_ENUMINPUT, &v4l2Input))
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2020-06-17 20:55:57 +02:00
|
|
|
(-1 == xioctl(VIDIOC_S_INPUT, &_input))
|
|
|
|
? Debug(_log, "Input settings not supported.")
|
|
|
|
: Debug(_log, "Set device input to: %s", v4l2Input.name);
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
// set the video standard if needed and supported
|
2020-04-17 16:59:20 +02:00
|
|
|
struct v4l2_standard standard;
|
|
|
|
CLEAR(standard);
|
|
|
|
|
|
|
|
if (-1 != xioctl(VIDIOC_ENUMSTD, &standard))
|
2017-10-04 21:23:45 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (videoStandard)
|
2017-10-04 21:23:45 +02:00
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoStandard::PAL:
|
2019-05-03 22:38:28 +02:00
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
standard.id = V4L2_STD_PAL;
|
|
|
|
if (-1 == xioctl(VIDIOC_S_STD, &standard.id))
|
2019-05-03 22:38:28 +02:00
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_STD");
|
2019-08-16 20:05:30 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
2020-04-17 16:59:20 +02:00
|
|
|
Debug(_log, "Video standard=PAL");
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoStandard::NTSC:
|
2019-05-03 22:38:28 +02:00
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
standard.id = V4L2_STD_NTSC;
|
|
|
|
if (-1 == xioctl(VIDIOC_S_STD, &standard.id))
|
2019-05-03 22:38:28 +02:00
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_STD");
|
2019-08-16 20:05:30 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
2020-04-17 16:59:20 +02:00
|
|
|
Debug(_log, "Video standard=NTSC");
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoStandard::SECAM:
|
2019-05-03 22:38:28 +02:00
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
standard.id = V4L2_STD_SECAM;
|
|
|
|
if (-1 == xioctl(VIDIOC_S_STD, &standard.id))
|
2019-05-03 22:38:28 +02:00
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_STD");
|
2019-08-16 20:05:30 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
2020-04-17 16:59:20 +02:00
|
|
|
Debug(_log, "Video standard=SECAM");
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
case VideoStandard::NO_CHANGE:
|
2019-05-03 22:38:28 +02:00
|
|
|
default:
|
|
|
|
// No change to device settings
|
|
|
|
break;
|
2017-10-04 21:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// get the current settings
|
|
|
|
struct v4l2_format fmt;
|
|
|
|
CLEAR(fmt);
|
2020-04-17 16:59:20 +02:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (-1 == xioctl(VIDIOC_G_FMT, &fmt))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_G_FMT");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the requested pixel format
|
|
|
|
switch (_pixelFormat)
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::UYVY:
|
2019-04-28 19:53:45 +02:00
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::YUYV:
|
2019-04-28 19:53:45 +02:00
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::RGB32:
|
2019-04-28 19:53:45 +02:00
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32;
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::MJPEG:
|
2019-04-28 19:53:45 +02:00
|
|
|
{
|
|
|
|
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
|
|
|
|
fmt.fmt.pix.field = V4L2_FIELD_ANY;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2020-06-28 23:05:32 +02:00
|
|
|
case PixelFormat::NO_CHANGE:
|
2019-04-28 19:53:45 +02:00
|
|
|
default:
|
|
|
|
// No change to device settings
|
2019-05-03 22:38:28 +02:00
|
|
|
break;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
// set custom resolution for width and height if they are not zero
|
|
|
|
if(_width && _height)
|
2019-08-16 20:05:30 +02:00
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
fmt.fmt.pix.width = _width;
|
|
|
|
fmt.fmt.pix.height = _height;
|
2019-08-16 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
// set the settings
|
|
|
|
if (-1 == xioctl(VIDIOC_S_FMT, &fmt))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_S_FMT");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
// initialize current width and height
|
2016-05-26 23:44:27 +02:00
|
|
|
_width = fmt.fmt.pix.width;
|
|
|
|
_height = fmt.fmt.pix.height;
|
|
|
|
|
2016-07-12 15:13:06 +02:00
|
|
|
// display the used width and height
|
2019-08-16 20:05:30 +02:00
|
|
|
Debug(_log, "Set resolution to width=%d height=%d", _width, _height );
|
2016-07-12 15:13:06 +02:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
// Trying to set frame rate
|
|
|
|
struct v4l2_streamparm streamparms;
|
|
|
|
CLEAR(streamparms);
|
2020-04-17 16:59:20 +02:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
streamparms.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2020-04-17 16:59:20 +02:00
|
|
|
// Check that the driver knows about framerate get/set
|
|
|
|
if (xioctl(VIDIOC_G_PARM, &streamparms) >= 0)
|
2019-04-28 19:53:45 +02:00
|
|
|
{
|
2020-04-17 16:59:20 +02:00
|
|
|
// Check if the device is able to accept a capture framerate set.
|
2019-04-28 19:53:45 +02:00
|
|
|
if (streamparms.parm.capture.capability == V4L2_CAP_TIMEPERFRAME)
|
|
|
|
{
|
|
|
|
streamparms.parm.capture.timeperframe.numerator = 1;
|
2020-03-27 23:13:58 +01:00
|
|
|
streamparms.parm.capture.timeperframe.denominator = _fps;
|
2020-04-17 16:59:20 +02:00
|
|
|
(-1 == xioctl(VIDIOC_S_PARM, &streamparms))
|
|
|
|
? Debug(_log, "Frame rate settings not supported.")
|
|
|
|
: Debug(_log, "Set framerate to %d fps", streamparms.parm.capture.timeperframe.denominator);
|
2019-04-28 19:53:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the line length
|
|
|
|
_lineLength = fmt.fmt.pix.bytesperline;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
// check pixel format and frame size
|
|
|
|
switch (fmt.fmt.pix.pixelformat)
|
|
|
|
{
|
2019-04-28 19:53:45 +02:00
|
|
|
case V4L2_PIX_FMT_UYVY:
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
_pixelFormat = PixelFormat::UYVY;
|
2019-04-28 19:53:45 +02:00
|
|
|
_frameByteSize = _width * _height * 2;
|
|
|
|
Debug(_log, "Pixel format=UYVY");
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
case V4L2_PIX_FMT_YUYV:
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
_pixelFormat = PixelFormat::YUYV;
|
2019-04-28 19:53:45 +02:00
|
|
|
_frameByteSize = _width * _height * 2;
|
|
|
|
Debug(_log, "Pixel format=YUYV");
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
case V4L2_PIX_FMT_RGB32:
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
_pixelFormat = PixelFormat::RGB32;
|
2019-04-28 19:53:45 +02:00
|
|
|
_frameByteSize = _width * _height * 4;
|
|
|
|
Debug(_log, "Pixel format=RGB32");
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2019-04-28 19:53:45 +02:00
|
|
|
case V4L2_PIX_FMT_MJPEG:
|
|
|
|
{
|
2020-06-28 23:05:32 +02:00
|
|
|
_pixelFormat = PixelFormat::MJPEG;
|
2019-04-28 19:53:45 +02:00
|
|
|
Debug(_log, "Pixel format=MJPEG");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
2019-05-03 22:38:28 +02:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
default:
|
2020-03-27 23:13:58 +01:00
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2019-05-03 22:38:28 +02:00
|
|
|
throw_exception("Only pixel formats UYVY, YUYV, RGB32 and MJPEG are supported");
|
|
|
|
#else
|
2019-04-28 19:53:45 +02:00
|
|
|
throw_exception("Only pixel formats UYVY, YUYV, and RGB32 are supported");
|
2019-05-03 22:38:28 +02:00
|
|
|
#endif
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (_ioMethod)
|
|
|
|
{
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
init_read(fmt.fmt.pix.sizeimage);
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
init_mmap();
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
init_userp(fmt.fmt.pix.sizeimage);
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::uninit_device()
|
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (_ioMethod)
|
|
|
|
{
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
free(_buffers[0].start);
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
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");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i)
|
|
|
|
free(_buffers[i].start);
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_buffers.resize(0);
|
2014-01-04 13:24:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::start_capturing()
|
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (_ioMethod)
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_READ:
|
|
|
|
/* Nothing to do. */
|
|
|
|
break;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
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;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (-1 == xioctl(VIDIOC_STREAMON, &type))
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
throw_errno_exception("VIDIOC_STREAMON");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-05-03 22:38:28 +02:00
|
|
|
break;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_USERPTR:
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i)
|
|
|
|
{
|
|
|
|
struct v4l2_buffer buf;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
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;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
if (-1 == xioctl(VIDIOC_STREAMON, &type))
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
throw_errno_exception("VIDIOC_STREAMON");
|
2018-12-27 23:11:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-05-03 22:38:28 +02:00
|
|
|
break;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (_ioMethod)
|
|
|
|
{
|
|
|
|
case IO_METHOD_READ:
|
|
|
|
break; /* Nothing to do. */
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
case IO_METHOD_USERPTR:
|
|
|
|
{
|
|
|
|
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
2020-02-10 15:21:58 +01:00
|
|
|
ErrorIf((xioctl(VIDIOC_STREAMOFF, &type) == -1), _log, "VIDIOC_STREAMOFF error code %d, %s", errno, strerror(errno));
|
2019-05-03 22:38:28 +02:00
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
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-08-12 09:39:41 +02:00
|
|
|
try
|
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
struct v4l2_buffer buf;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (_ioMethod)
|
|
|
|
{
|
|
|
|
case IO_METHOD_READ:
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
int size;
|
|
|
|
if ((size = read(_fileDescriptor, _buffers[0].start, _buffers[0].length)) == -1)
|
2019-04-28 19:53:45 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case EIO: /* Could ignore EIO, see spec. */
|
|
|
|
default:
|
|
|
|
throw_errno_exception("read");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-28 19:53:45 +02:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
rc = process_image(_buffers[0].start, size);
|
|
|
|
}
|
2019-04-28 19:53:45 +02:00
|
|
|
break;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_MMAP:
|
|
|
|
{
|
|
|
|
CLEAR(buf);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_MMAP;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_DQBUF, &buf))
|
2019-04-28 19:53:45 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case EIO: /* Could ignore EIO, see spec. */
|
|
|
|
default:
|
2020-04-17 16:59:20 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
throw_errno_exception("VIDIOC_DQBUF");
|
2020-04-17 16:59:20 +02:00
|
|
|
stop();
|
|
|
|
getV4Ldevices();
|
|
|
|
}
|
2019-05-03 22:38:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
assert(buf.index < _buffers.size());
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
rc = process_image(_buffers[buf.index].start, buf.bytesused);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
2019-04-28 19:53:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2019-04-28 19:53:45 +02:00
|
|
|
break;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
case IO_METHOD_USERPTR:
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
CLEAR(buf);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
|
|
|
buf.memory = V4L2_MEMORY_USERPTR;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_DQBUF, &buf))
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EAGAIN:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case EIO: /* Could ignore EIO, see spec. */
|
|
|
|
default:
|
2020-04-17 16:59:20 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
throw_errno_exception("VIDIOC_DQBUF");
|
2020-04-17 16:59:20 +02:00
|
|
|
stop();
|
|
|
|
getV4Ldevices();
|
|
|
|
}
|
2019-05-03 22:38:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-04-28 19:53:45 +02:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
for (size_t i = 0; i < _buffers.size(); ++i)
|
2019-04-28 19:53:45 +02:00
|
|
|
{
|
2019-05-03 22:38:28 +02:00
|
|
|
if (buf.m.userptr == (unsigned long)_buffers[i].start && buf.length == _buffers[i].length)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2019-04-28 19:53:45 +02:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
rc = process_image((void *)buf.m.userptr, buf.bytesused);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if (-1 == xioctl(VIDIOC_QBUF, &buf))
|
|
|
|
{
|
|
|
|
throw_errno_exception("VIDIOC_QBUF");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-04-28 19:53:45 +02:00
|
|
|
}
|
|
|
|
break;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2016-08-12 09:39:41 +02:00
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
emit readError(e.what());
|
|
|
|
rc = false;
|
|
|
|
}
|
2017-10-04 21:23:45 +02: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
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
// We do want a new frame...
|
2020-03-27 23:13:58 +01:00
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2020-06-28 23:05:32 +02:00
|
|
|
if (size < _frameByteSize && _pixelFormat != PixelFormat::MJPEG)
|
2019-04-28 19:53:45 +02:00
|
|
|
#else
|
2020-04-17 16:59:20 +02:00
|
|
|
if (size < _frameByteSize)
|
2019-04-28 19:53:45 +02:00
|
|
|
#endif
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
Error(_log, "Frame too small: %d != %d", size, _frameByteSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-28 19:53:45 +02:00
|
|
|
process_image(reinterpret_cast<const uint8_t *>(p), size);
|
2018-12-27 23:11:32 +01:00
|
|
|
return true;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-01-11 20:43:55 +01:00
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
void V4L2Grabber::process_image(const uint8_t * data, int size)
|
2014-01-11 20:43:55 +01:00
|
|
|
{
|
2020-07-20 20:06:41 +02:00
|
|
|
if (_cecDetectionEnabled && _cecStandbyActivated)
|
|
|
|
return;
|
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
Image<ColorRgb> image(_width, _height);
|
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
/* ----------------------------------------------------------
|
|
|
|
* ----------- BEGIN of JPEG decoder related code -----------
|
|
|
|
* --------------------------------------------------------*/
|
|
|
|
|
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2020-06-28 23:05:32 +02:00
|
|
|
if (_pixelFormat == PixelFormat::MJPEG)
|
2019-04-28 19:53:45 +02:00
|
|
|
{
|
2020-03-27 23:13:58 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_JPEG
|
2019-04-28 19:53:45 +02:00
|
|
|
_decompress = new jpeg_decompress_struct;
|
|
|
|
_error = new errorManager;
|
|
|
|
|
|
|
|
_decompress->err = jpeg_std_error(&_error->pub);
|
|
|
|
_error->pub.error_exit = &errorHandler;
|
|
|
|
_error->pub.output_message = &outputHandler;
|
|
|
|
|
|
|
|
jpeg_create_decompress(_decompress);
|
|
|
|
|
|
|
|
if (setjmp(_error->setjmp_buffer))
|
|
|
|
{
|
|
|
|
jpeg_abort_decompress(_decompress);
|
|
|
|
jpeg_destroy_decompress(_decompress);
|
|
|
|
delete _decompress;
|
|
|
|
delete _error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_mem_src(_decompress, const_cast<uint8_t*>(data), size);
|
|
|
|
|
|
|
|
if (jpeg_read_header(_decompress, (bool) TRUE) != JPEG_HEADER_OK)
|
|
|
|
{
|
|
|
|
jpeg_abort_decompress(_decompress);
|
|
|
|
jpeg_destroy_decompress(_decompress);
|
|
|
|
delete _decompress;
|
|
|
|
delete _error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-03 17:54:43 +02:00
|
|
|
_decompress->scale_num = 1;
|
|
|
|
_decompress->scale_denom = 1;
|
|
|
|
_decompress->out_color_space = JCS_RGB;
|
|
|
|
_decompress->dct_method = JDCT_IFAST;
|
|
|
|
|
|
|
|
if (!jpeg_start_decompress(_decompress))
|
|
|
|
{
|
|
|
|
jpeg_abort_decompress(_decompress);
|
|
|
|
jpeg_destroy_decompress(_decompress);
|
|
|
|
delete _decompress;
|
|
|
|
delete _error;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_decompress->out_color_components != 3)
|
|
|
|
{
|
|
|
|
jpeg_abort_decompress(_decompress);
|
|
|
|
jpeg_destroy_decompress(_decompress);
|
|
|
|
delete _decompress;
|
|
|
|
delete _error;
|
|
|
|
return;
|
|
|
|
}
|
2019-04-28 19:53:45 +02:00
|
|
|
|
|
|
|
QImage imageFrame = QImage(_decompress->output_width, _decompress->output_height, QImage::Format_RGB888);
|
|
|
|
|
|
|
|
int y = 0;
|
|
|
|
while (_decompress->output_scanline < _decompress->output_height)
|
|
|
|
{
|
|
|
|
uchar *row = imageFrame.scanLine(_decompress->output_scanline);
|
|
|
|
jpeg_read_scanlines(_decompress, &row, 1);
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_finish_decompress(_decompress);
|
|
|
|
jpeg_destroy_decompress(_decompress);
|
|
|
|
delete _decompress;
|
|
|
|
delete _error;
|
|
|
|
|
2019-05-26 14:25:37 +02:00
|
|
|
if (imageFrame.isNull() || _error->pub.num_warnings > 0)
|
2019-04-28 19:53:45 +02:00
|
|
|
return;
|
2020-03-27 23:13:58 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TURBO_JPEG
|
|
|
|
_decompress = tjInitDecompress();
|
|
|
|
if (_decompress == nullptr)
|
|
|
|
return;
|
2019-04-28 19:53:45 +02:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
if (tjDecompressHeader2(_decompress, const_cast<uint8_t*>(data), size, &_width, &_height, &_subsamp) != 0)
|
|
|
|
{
|
|
|
|
tjDestroy(_decompress);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage imageFrame = QImage(_width, _height, QImage::Format_RGB888);
|
|
|
|
if (tjDecompress2(_decompress, const_cast<uint8_t*>(data), size, imageFrame.bits(), _width, 0, _height, TJPF_RGB, TJFLAG_FASTDCT | TJFLAG_FASTUPSAMPLE) != 0)
|
|
|
|
{
|
|
|
|
tjDestroy(_decompress);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tjDestroy(_decompress);
|
|
|
|
|
|
|
|
if (imageFrame.isNull())
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2019-04-28 19:53:45 +02:00
|
|
|
QRect rect(_cropLeft, _cropTop, imageFrame.width() - _cropLeft - _cropRight, imageFrame.height() - _cropTop - _cropBottom);
|
|
|
|
imageFrame = imageFrame.copy(rect);
|
|
|
|
imageFrame = imageFrame.scaled(imageFrame.width() / _pixelDecimation, imageFrame.height() / _pixelDecimation,Qt::KeepAspectRatio);
|
|
|
|
|
2019-05-03 17:54:43 +02:00
|
|
|
if ((image.width() != unsigned(imageFrame.width())) || (image.height() != unsigned(imageFrame.height())))
|
2019-04-28 19:53:45 +02:00
|
|
|
image.resize(imageFrame.width(), imageFrame.height());
|
|
|
|
|
|
|
|
for (int y=0; y<imageFrame.height(); ++y)
|
|
|
|
for (int x=0; x<imageFrame.width(); ++x)
|
|
|
|
{
|
2019-05-03 17:54:43 +02:00
|
|
|
QColor inPixel(imageFrame.pixel(x,y));
|
2019-04-28 19:53:45 +02:00
|
|
|
ColorRgb & outPixel = image(x,y);
|
2019-05-03 17:54:43 +02:00
|
|
|
outPixel.red = inPixel.red();
|
|
|
|
outPixel.green = inPixel.green();
|
|
|
|
outPixel.blue = inPixel.blue();
|
2019-04-28 19:53:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2020-03-27 23:13:58 +01:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------
|
|
|
|
* ------------ END of JPEG decoder related code ------------
|
|
|
|
* --------------------------------------------------------*/
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
_imageResampler.processImage(data, _width, _height, _lineLength, _pixelFormat, image);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
if (_signalDetectionEnabled)
|
|
|
|
{
|
|
|
|
// check signal (only in center of the resulting image, because some grabbers have noise values along the borders)
|
|
|
|
bool noSignal = true;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
// top left
|
|
|
|
unsigned xOffset = image.width() * _x_frac_min;
|
|
|
|
unsigned yOffset = image.height() * _y_frac_min;
|
2016-12-16 19:48:43 +01:00
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
// bottom right
|
|
|
|
unsigned xMax = image.width() * _x_frac_max;
|
|
|
|
unsigned yMax = image.height() * _y_frac_max;
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2017-10-04 21:23:45 +02:00
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
for (unsigned x = xOffset; noSignal && x < xMax; ++x)
|
2016-12-16 19:48:43 +01:00
|
|
|
{
|
2017-03-15 20:33:11 +01:00
|
|
|
for (unsigned y = yOffset; noSignal && y < yMax; ++y)
|
|
|
|
{
|
|
|
|
noSignal &= (ColorRgb&)image(x, y) <= _noSignalThresholdColor;
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
if (noSignal)
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2017-03-15 20:33:11 +01:00
|
|
|
++_noSignalCounter;
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2017-03-15 20:33:11 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_noSignalCounter >= _noSignalCounterThreshold)
|
|
|
|
{
|
|
|
|
_noSignalDetected = true;
|
|
|
|
Info(_log, "Signal detected");
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
_noSignalCounter = 0;
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
if ( _noSignalCounter < _noSignalCounterThreshold)
|
|
|
|
{
|
|
|
|
emit newFrame(image);
|
|
|
|
}
|
|
|
|
else if (_noSignalCounter == _noSignalCounterThreshold)
|
|
|
|
{
|
|
|
|
_noSignalDetected = false;
|
|
|
|
Info(_log, "Signal lost");
|
|
|
|
}
|
2016-05-26 23:44:27 +02:00
|
|
|
}
|
2017-03-15 20:33:11 +01:00
|
|
|
else
|
2016-05-26 23:44:27 +02:00
|
|
|
{
|
2017-03-15 20:33:11 +01:00
|
|
|
emit newFrame(image);
|
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
|
|
|
}
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
int V4L2Grabber::xioctl(int fileDescriptor, int request, void *arg)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
r = ioctl(fileDescriptor, request, arg);
|
|
|
|
}
|
|
|
|
while (r < 0 && errno == EINTR );
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void V4L2Grabber::enumFrameIntervals(QStringList &framerates, int fileDescriptor, int pixelformat, int width, int height)
|
|
|
|
{
|
|
|
|
// collect available frame rates
|
|
|
|
struct v4l2_frmivalenum frmivalenum;
|
|
|
|
CLEAR(frmivalenum);
|
|
|
|
|
|
|
|
frmivalenum.index = 0;
|
|
|
|
frmivalenum.pixel_format = pixelformat;
|
|
|
|
frmivalenum.width = width;
|
|
|
|
frmivalenum.height = height;
|
|
|
|
|
|
|
|
while (xioctl(fileDescriptor, VIDIOC_ENUM_FRAMEINTERVALS, &frmivalenum) >= 0)
|
|
|
|
{
|
|
|
|
int rate;
|
|
|
|
switch (frmivalenum.type)
|
|
|
|
{
|
|
|
|
case V4L2_FRMSIZE_TYPE_DISCRETE:
|
|
|
|
{
|
|
|
|
if (frmivalenum.discrete.numerator != 0)
|
|
|
|
{
|
|
|
|
rate = frmivalenum.discrete.denominator / frmivalenum.discrete.numerator;
|
|
|
|
if (!framerates.contains(QString::number(rate)))
|
|
|
|
framerates.append(QString::number(rate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case V4L2_FRMSIZE_TYPE_CONTINUOUS:
|
|
|
|
case V4L2_FRMSIZE_TYPE_STEPWISE:
|
|
|
|
{
|
|
|
|
if (frmivalenum.stepwise.min.denominator != 0)
|
|
|
|
{
|
|
|
|
rate = frmivalenum.stepwise.min.denominator / frmivalenum.stepwise.min.numerator;
|
|
|
|
if (!framerates.contains(QString::number(rate)))
|
|
|
|
framerates.append(QString::number(rate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
frmivalenum.index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
void V4L2Grabber::setSignalDetectionEnable(bool enable)
|
|
|
|
{
|
2019-05-03 17:54:43 +02:00
|
|
|
if (_signalDetectionEnabled != enable)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
|
|
|
_signalDetectionEnabled = enable;
|
|
|
|
Info(_log, "Signal detection is now %s", enable ? "enabled" : "disabled");
|
|
|
|
}
|
2017-03-15 20:33:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-20 20:06:41 +02:00
|
|
|
void V4L2Grabber::setCecDetectionEnable(bool enable)
|
|
|
|
{
|
|
|
|
if (_cecDetectionEnabled != enable)
|
|
|
|
{
|
|
|
|
_cecDetectionEnabled = enable;
|
|
|
|
Info(_log, QString("CEC detection is now %1").arg(enable ? "enabled" : "disabled").toLocal8Bit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
void V4L2Grabber::setPixelDecimation(int pixelDecimation)
|
|
|
|
{
|
2019-05-03 17:54:43 +02:00
|
|
|
if (_pixelDecimation != pixelDecimation)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
_pixelDecimation = pixelDecimation;
|
|
|
|
_imageResampler.setHorizontalPixelDecimation(pixelDecimation);
|
|
|
|
_imageResampler.setVerticalPixelDecimation(pixelDecimation);
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:12:45 +01:00
|
|
|
void V4L2Grabber::setDeviceVideoStandard(QString device, VideoStandard videoStandard)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2019-05-03 17:54:43 +02:00
|
|
|
if (_deviceName != device || _videoStandard != videoStandard)
|
2018-12-27 23:11:32 +01:00
|
|
|
{
|
2018-12-28 18:12:45 +01:00
|
|
|
// extract input of device
|
|
|
|
QChar input = device.at(device.size() - 1);
|
|
|
|
_input = input.isNumber() ? input.digitValue() : -1;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
bool started = _initialized;
|
2018-12-27 23:11:32 +01:00
|
|
|
uninit();
|
2018-12-28 18:12:45 +01:00
|
|
|
_deviceName = device;
|
|
|
|
_videoStandard = videoStandard;
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
if(started) start();
|
2018-12-27 23:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 23:13:58 +01:00
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
bool V4L2Grabber::setInput(int input)
|
2020-03-27 23:13:58 +01:00
|
|
|
{
|
2020-06-17 20:55:57 +02:00
|
|
|
if(Grabber::setInput(input))
|
2020-03-27 23:13:58 +01:00
|
|
|
{
|
|
|
|
bool started = _initialized;
|
|
|
|
uninit();
|
|
|
|
if(started) start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool V4L2Grabber::setWidthHeight(int width, int height)
|
|
|
|
{
|
|
|
|
if(Grabber::setWidthHeight(width,height))
|
|
|
|
{
|
|
|
|
bool started = _initialized;
|
|
|
|
uninit();
|
|
|
|
if(started) start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-17 16:59:20 +02:00
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
bool V4L2Grabber::setFramerate(int fps)
|
|
|
|
{
|
|
|
|
if(Grabber::setFramerate(fps))
|
|
|
|
{
|
|
|
|
bool started = _initialized;
|
|
|
|
uninit();
|
|
|
|
if(started) start();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QStringList V4L2Grabber::getV4L2devices() const
|
2020-04-17 16:59:20 +02:00
|
|
|
{
|
|
|
|
QStringList result = QStringList();
|
|
|
|
for (auto it = _deviceProperties.begin(); it != _deviceProperties.end(); ++it)
|
|
|
|
{
|
|
|
|
result << it.key();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QString V4L2Grabber::getV4L2deviceName(const QString& devicePath) const
|
2020-04-17 16:59:20 +02:00
|
|
|
{
|
|
|
|
return _deviceProperties.value(devicePath).name;
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QMultiMap<QString, int> V4L2Grabber::getV4L2deviceInputs(const QString& devicePath) const
|
2020-06-17 20:55:57 +02:00
|
|
|
{
|
|
|
|
return _deviceProperties.value(devicePath).inputs;
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QStringList V4L2Grabber::getResolutions(const QString& devicePath) const
|
2020-04-17 16:59:20 +02:00
|
|
|
{
|
|
|
|
return _deviceProperties.value(devicePath).resolutions;
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QStringList V4L2Grabber::getFramerates(const QString& devicePath) const
|
2020-04-17 16:59:20 +02:00
|
|
|
{
|
|
|
|
return _deviceProperties.value(devicePath).framerates;
|
|
|
|
}
|
2020-07-20 20:06:41 +02:00
|
|
|
|
|
|
|
void V4L2Grabber::handleCecEvent(CECEvent event)
|
|
|
|
{
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case CECEvent::On :
|
|
|
|
Debug(_log,"CEC on event received");
|
|
|
|
_cecStandbyActivated = false;
|
|
|
|
return;
|
|
|
|
case CECEvent::Off :
|
|
|
|
Debug(_log,"CEC off event received");
|
|
|
|
_cecStandbyActivated = true;
|
|
|
|
return;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|