2014-01-04 13:24:05 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// stl includes
|
|
|
|
#include <vector>
|
2016-07-14 23:40:10 +02:00
|
|
|
#include <map>
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2014-02-19 21:52:37 +01:00
|
|
|
// Qt includes
|
|
|
|
#include <QObject>
|
|
|
|
#include <QSocketNotifier>
|
2016-12-16 19:48:43 +01:00
|
|
|
#include <QRectF>
|
2020-04-17 16:59:20 +02:00
|
|
|
#include <QMap>
|
2020-06-17 20:55:57 +02:00
|
|
|
#include <QMultiMap>
|
2014-02-19 21:52:37 +01:00
|
|
|
|
2014-01-12 20:04:22 +01:00
|
|
|
// util includes
|
2014-12-14 14:26:59 +01:00
|
|
|
#include <utils/PixelFormat.h>
|
2017-08-04 23:08:15 +02:00
|
|
|
#include <hyperion/Grabber.h>
|
2021-02-07 14:30:36 +01:00
|
|
|
#include <utils/VideoStandard.h>
|
2019-04-28 19:53:45 +02:00
|
|
|
#include <utils/Components.h>
|
2020-07-20 20:06:41 +02:00
|
|
|
#include <cec/CECEvent.h>
|
2019-04-28 19:53:45 +02:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
// general JPEG decoder includes
|
|
|
|
#ifdef HAVE_JPEG_DECODER
|
2019-04-28 19:53:45 +02:00
|
|
|
#include <QImage>
|
2019-05-03 17:54:43 +02:00
|
|
|
#include <QColor>
|
2020-03-27 23:13:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// System JPEG decoder
|
|
|
|
#ifdef HAVE_JPEG
|
2019-04-28 19:53:45 +02:00
|
|
|
#include <jpeglib.h>
|
|
|
|
#include <csetjmp>
|
|
|
|
#endif
|
2014-01-12 20:04:22 +01:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
// TurboJPEG decoder
|
|
|
|
#ifdef HAVE_TURBO_JPEG
|
|
|
|
#include <turbojpeg.h>
|
|
|
|
#endif
|
|
|
|
|
2021-02-05 21:52:02 +01:00
|
|
|
///
|
2014-01-04 13:24:05 +01:00
|
|
|
/// Capture class for V4L2 devices
|
|
|
|
///
|
2021-02-05 21:52:02 +01:00
|
|
|
|
2017-08-04 23:08:15 +02:00
|
|
|
class V4L2Grabber : public Grabber
|
2014-01-04 13:24:05 +01:00
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
Q_OBJECT
|
2014-01-25 17:35:06 +01:00
|
|
|
|
2014-01-11 20:43:55 +01:00
|
|
|
public:
|
2020-04-17 16:59:20 +02:00
|
|
|
struct DeviceProperties
|
|
|
|
{
|
2021-02-05 21:52:02 +01:00
|
|
|
QString name = QString();
|
|
|
|
struct InputProperties
|
|
|
|
{
|
|
|
|
QString inputName = QString();
|
2021-02-07 14:30:36 +01:00
|
|
|
QList<VideoStandard> standards = QList<VideoStandard>();
|
2021-02-05 21:52:02 +01:00
|
|
|
struct EncodingProperties
|
|
|
|
{
|
|
|
|
unsigned int width = 0;
|
|
|
|
unsigned int height = 0;
|
|
|
|
QList<int> framerates = QList<int>();
|
|
|
|
};
|
|
|
|
QMultiMap<PixelFormat, EncodingProperties> encodingFormats = QMultiMap<PixelFormat, EncodingProperties>();
|
|
|
|
};
|
|
|
|
QMap<int, InputProperties> inputs = QMap<int, InputProperties>();
|
2020-04-17 16:59:20 +02:00
|
|
|
};
|
|
|
|
|
2020-12-18 17:38:21 +01:00
|
|
|
V4L2Grabber(const QString & device, const unsigned width, const unsigned height, const unsigned fps, const unsigned input, VideoStandard videoStandard, PixelFormat pixelFormat, int pixelDecimation);
|
2020-04-17 16:59:20 +02:00
|
|
|
~V4L2Grabber() override;
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
QRectF getSignalDetectionOffset() const
|
2019-05-03 22:38:28 +02:00
|
|
|
{
|
|
|
|
return QRectF(_x_frac_min, _y_frac_min, _x_frac_max, _y_frac_max);
|
|
|
|
}
|
|
|
|
|
2020-08-08 23:12:43 +02:00
|
|
|
bool getSignalDetectionEnabled() const { return _signalDetectionEnabled; }
|
|
|
|
bool getCecDetectionEnabled() const { return _cecDetectionEnabled; }
|
2016-12-16 19:48:43 +01:00
|
|
|
|
2017-08-12 07:55:32 +02:00
|
|
|
int grabFrame(Image<ColorRgb> &);
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief set new PixelDecimation value to ImageResampler
|
|
|
|
/// @param pixelDecimation The new pixelDecimation value
|
|
|
|
///
|
2020-04-17 16:59:20 +02:00
|
|
|
void setPixelDecimation(int pixelDecimation) override;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2020-04-17 16:59:20 +02:00
|
|
|
void setSignalThreshold(
|
2016-12-16 19:48:43 +01:00
|
|
|
double redSignalThreshold,
|
2016-05-26 23:44:27 +02:00
|
|
|
double greenSignalThreshold,
|
|
|
|
double blueSignalThreshold,
|
2020-04-17 16:59:20 +02:00
|
|
|
int noSignalCounterThreshold = 50) override;
|
2014-03-04 22:04:15 +01:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2020-04-17 16:59:20 +02:00
|
|
|
void setSignalDetectionOffset(
|
2016-12-16 19:48:43 +01:00
|
|
|
double verticalMin,
|
|
|
|
double horizontalMin,
|
|
|
|
double verticalMax,
|
2020-04-17 16:59:20 +02:00
|
|
|
double horizontalMax) override;
|
2018-12-27 23:11:32 +01:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2020-04-17 16:59:20 +02:00
|
|
|
void setSignalDetectionEnable(bool enable) override;
|
|
|
|
|
2020-07-20 20:06:41 +02:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
|
|
|
void setCecDetectionEnable(bool enable) override;
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
|
|
|
void setDeviceVideoStandard(QString device, VideoStandard videoStandard) override;
|
2016-12-16 19:48:43 +01:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
2020-04-17 16:59:20 +02:00
|
|
|
///
|
2020-06-17 20:55:57 +02:00
|
|
|
bool setInput(int input) override;
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
2020-04-17 16:59:20 +02:00
|
|
|
///
|
|
|
|
bool setWidthHeight(int width, int height) override;
|
2020-03-27 23:13:58 +01:00
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
|
|
|
bool setFramerate(int fps) override;
|
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2021-02-05 21:52:02 +01:00
|
|
|
QStringList getDevices() const override;
|
2020-04-17 16:59:20 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2021-02-05 21:52:02 +01:00
|
|
|
QString getDeviceName(const QString& devicePath) const override;
|
2020-04-17 16:59:20 +02:00
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2021-02-05 21:52:02 +01:00
|
|
|
QMultiMap<QString, int> getDeviceInputs(const QString& devicePath) const override;
|
2020-06-17 20:55:57 +02:00
|
|
|
|
2021-02-07 14:30:36 +01:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
|
|
|
QList<VideoStandard> getAvailableDeviceStandards(const QString& devicePath, const int& deviceInput) const override;
|
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2021-02-05 21:52:02 +01:00
|
|
|
QStringList getAvailableEncodingFormats(const QString& devicePath, const int& deviceInput) const override;
|
2020-04-17 16:59:20 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2021-02-05 21:52:02 +01:00
|
|
|
QMultiMap<int, int> getAvailableDeviceResolutions(const QString& devicePath, const int& deviceInput, const PixelFormat& encFormat) const override;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @brief overwrite Grabber.h implementation
|
|
|
|
///
|
2021-02-07 11:12:28 +01:00
|
|
|
QIntList getAvailableDeviceFramerates(const QString& devicePath, const int& deviceInput, const PixelFormat& encFormat, const unsigned width, const unsigned height) const override;
|
2021-02-05 21:52:02 +01:00
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
|
2018-12-27 23:11:32 +01:00
|
|
|
public slots:
|
2017-03-15 20:33:11 +01:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
bool start();
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void stop();
|
2014-01-11 20:43:55 +01:00
|
|
|
|
2020-07-20 20:06:41 +02:00
|
|
|
void handleCecEvent(CECEvent event);
|
|
|
|
|
2014-02-19 21:52:37 +01:00
|
|
|
signals:
|
2016-05-26 23:44:27 +02:00
|
|
|
void newFrame(const Image<ColorRgb> & image);
|
2016-08-12 09:39:41 +02:00
|
|
|
void readError(const char* err);
|
2014-02-19 21:52:37 +01:00
|
|
|
|
|
|
|
private slots:
|
2016-05-26 23:44:27 +02:00
|
|
|
int read_frame();
|
2014-02-19 21:52:37 +01:00
|
|
|
|
2014-01-04 13:24:05 +01:00
|
|
|
private:
|
2016-07-14 23:40:10 +02:00
|
|
|
void getV4Ldevices();
|
2018-12-27 23:11:32 +01:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
bool init();
|
2020-08-08 23:12:43 +02:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
void uninit();
|
|
|
|
|
2019-05-26 14:25:37 +02:00
|
|
|
bool open_device();
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void close_device();
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void init_read(unsigned int buffer_size);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void init_mmap();
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void init_userp(unsigned int buffer_size);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2020-06-17 20:55:57 +02:00
|
|
|
void init_device(VideoStandard videoStandard);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void uninit_device();
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void start_capturing();
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
void stop_capturing();
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
bool process_image(const void *p, int size);
|
2014-01-11 20:43:55 +01:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
void process_image(const uint8_t *p, int size);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
int xioctl(int request, void *arg);
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2020-04-17 16:59:20 +02:00
|
|
|
int xioctl(int fileDescriptor, int request, void *arg);
|
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
void throw_exception(const QString & error)
|
|
|
|
{
|
|
|
|
Error(_log, "Throws error: %s", QSTRING_CSTR(error));
|
|
|
|
}
|
2014-01-26 12:35:31 +01:00
|
|
|
|
2019-05-03 22:38:28 +02:00
|
|
|
void throw_errno_exception(const QString & error)
|
|
|
|
{
|
|
|
|
Error(_log, "Throws error nr: %s", QSTRING_CSTR(QString(error + " error code " + QString::number(errno) + ", " + strerror(errno))));
|
|
|
|
}
|
2014-01-04 13:24:05 +01:00
|
|
|
|
|
|
|
private:
|
2019-04-28 19:53:45 +02:00
|
|
|
enum io_method
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
IO_METHOD_READ,
|
|
|
|
IO_METHOD_MMAP,
|
|
|
|
IO_METHOD_USERPTR
|
|
|
|
};
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
struct buffer
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
void *start;
|
|
|
|
size_t length;
|
|
|
|
};
|
2014-01-04 13:24:05 +01:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
#ifdef HAVE_JPEG
|
|
|
|
struct errorManager
|
|
|
|
{
|
|
|
|
jpeg_error_mgr pub;
|
|
|
|
jmp_buf setjmp_buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void errorHandler(j_common_ptr cInfo)
|
|
|
|
{
|
|
|
|
errorManager* mgr = reinterpret_cast<errorManager*>(cInfo->err);
|
|
|
|
longjmp(mgr->setjmp_buffer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void outputHandler(j_common_ptr cInfo)
|
|
|
|
{
|
|
|
|
// Suppress fprintf warnings.
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_decompress_struct* _decompress;
|
|
|
|
errorManager* _error;
|
|
|
|
#endif
|
|
|
|
|
2020-03-27 23:13:58 +01:00
|
|
|
#ifdef HAVE_TURBO_JPEG
|
|
|
|
tjhandle _decompress = nullptr;
|
|
|
|
int _subsamp;
|
|
|
|
#endif
|
|
|
|
|
2014-01-04 13:24:05 +01:00
|
|
|
private:
|
2017-03-04 22:17:42 +01:00
|
|
|
QString _deviceName;
|
2020-08-08 23:12:43 +02:00
|
|
|
std::map<QString, QString> _v4lDevices;
|
|
|
|
QMap<QString, V4L2Grabber::DeviceProperties> _deviceProperties;
|
|
|
|
|
|
|
|
VideoStandard _videoStandard;
|
|
|
|
io_method _ioMethod;
|
|
|
|
int _fileDescriptor;
|
|
|
|
std::vector<buffer> _buffers;
|
2014-12-14 14:26:59 +01:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
PixelFormat _pixelFormat;
|
2018-12-27 23:11:32 +01:00
|
|
|
int _pixelDecimation;
|
2017-08-04 23:08:15 +02:00
|
|
|
int _lineLength;
|
|
|
|
int _frameByteSize;
|
2014-12-14 14:26:59 +01:00
|
|
|
|
2017-03-15 20:33:11 +01:00
|
|
|
// signal detection
|
|
|
|
int _noSignalCounterThreshold;
|
2016-05-26 23:44:27 +02:00
|
|
|
ColorRgb _noSignalThresholdColor;
|
2017-03-15 20:33:11 +01:00
|
|
|
bool _signalDetectionEnabled;
|
2020-07-20 20:06:41 +02:00
|
|
|
bool _cecDetectionEnabled;
|
|
|
|
bool _cecStandbyActivated;
|
2017-03-15 20:33:11 +01:00
|
|
|
bool _noSignalDetected;
|
|
|
|
int _noSignalCounter;
|
|
|
|
double _x_frac_min;
|
|
|
|
double _y_frac_min;
|
|
|
|
double _x_frac_max;
|
|
|
|
double _y_frac_max;
|
2014-12-14 14:26:59 +01:00
|
|
|
|
2019-04-28 19:53:45 +02:00
|
|
|
QSocketNotifier *_streamNotifier;
|
2014-12-16 21:30:08 +01:00
|
|
|
|
2016-07-14 23:40:10 +02:00
|
|
|
bool _initialized;
|
2016-08-12 09:39:41 +02:00
|
|
|
bool _deviceAutoDiscoverEnabled;
|
2020-04-17 16:59:20 +02:00
|
|
|
|
|
|
|
protected:
|
2021-02-05 21:52:02 +01:00
|
|
|
void enumFrameIntervals(QList<int> &framerates, int fileDescriptor, int pixelformat, int width, int height);
|
2014-01-04 13:24:05 +01:00
|
|
|
};
|