hyperion.ng
V4L2Grabber.h
1 #pragma once
2 
3 // stl includes
4 #include <vector>
5 #include <map>
6 
7 // Qt includes
8 #include <QObject>
9 #include <QSocketNotifier>
10 #include <QRectF>
11 
12 // util includes
13 #include <utils/PixelFormat.h>
14 #include <hyperion/Grabber.h>
15 #include <grabber/VideoStandard.h>
16 #include <utils/Components.h>
17 
18 #ifdef HAVE_JPEG
19  #include <QImage>
20  #include <QColor>
21  #include <jpeglib.h>
22  #include <csetjmp>
23 #endif
24 
28 class V4L2Grabber : public Grabber
29 {
30  Q_OBJECT
31 
32 public:
33  V4L2Grabber(const QString & device,
34  VideoStandard videoStandard,
35  PixelFormat pixelFormat,
36  int pixelDecimation
37  );
38  virtual ~V4L2Grabber();
39 
40  QRectF getSignalDetectionOffset()
41  {
42  return QRectF(_x_frac_min, _y_frac_min, _x_frac_max, _y_frac_max);
43  }
44 
45  bool getSignalDetectionEnabled() { return _signalDetectionEnabled; }
46 
47  int grabFrame(Image<ColorRgb> &);
48 
52  virtual void setWidthHeight(){};
53 
58  virtual void setPixelDecimation(int pixelDecimation);
59 
63  virtual void setSignalThreshold(
64  double redSignalThreshold,
65  double greenSignalThreshold,
66  double blueSignalThreshold,
67  int noSignalCounterThreshold = 50);
68 
72  virtual void setSignalDetectionOffset(
73  double verticalMin,
74  double horizontalMin,
75  double verticalMax,
76  double horizontalMax);
80  virtual void setSignalDetectionEnable(bool enable);
81 
85  virtual void setDeviceVideoStandard(QString device, VideoStandard videoStandard);
86 
87 public slots:
88 
89  bool start();
90 
91  void stop();
92 
93  void componentStateChanged(const hyperion::Components component, bool enable);
94 
95 signals:
96  void newFrame(const Image<ColorRgb> & image);
97  void readError(const char* err);
98 
99 private slots:
100  int read_frame();
101 
102 private:
103  void getV4Ldevices();
104 
105  bool init();
106  void uninit();
107 
108  bool open_device();
109 
110  void close_device();
111 
112  void init_read(unsigned int buffer_size);
113 
114  void init_mmap();
115 
116  void init_userp(unsigned int buffer_size);
117 
118  void init_device(VideoStandard videoStandard, int input);
119 
120  void uninit_device();
121 
122  void start_capturing();
123 
124  void stop_capturing();
125 
126  bool process_image(const void *p, int size);
127 
128  void process_image(const uint8_t *p, int size);
129 
130  int xioctl(int request, void *arg);
131 
132  void throw_exception(const QString & error)
133  {
134  Error(_log, "Throws error: %s", QSTRING_CSTR(error));
135  }
136 
137  void throw_errno_exception(const QString & error)
138  {
139  Error(_log, "Throws error nr: %s", QSTRING_CSTR(QString(error + " error code " + QString::number(errno) + ", " + strerror(errno))));
140  }
141 
142 private:
143  enum io_method
144  {
145  IO_METHOD_READ,
146  IO_METHOD_MMAP,
147  IO_METHOD_USERPTR
148  };
149 
150  struct buffer
151  {
152  void *start;
153  size_t length;
154  };
155 
156 #ifdef HAVE_JPEG
157  struct errorManager
158  {
159  jpeg_error_mgr pub;
160  jmp_buf setjmp_buffer;
161  };
162 
163  static void errorHandler(j_common_ptr cInfo)
164  {
165  errorManager* mgr = reinterpret_cast<errorManager*>(cInfo->err);
166  longjmp(mgr->setjmp_buffer, 1);
167  }
168 
169  static void outputHandler(j_common_ptr cInfo)
170  {
171  // Suppress fprintf warnings.
172  }
173 
174  jpeg_decompress_struct* _decompress;
175  errorManager* _error;
176 #endif
177 
178 private:
179  QString _deviceName;
180  std::map<QString,QString> _v4lDevices;
181  int _input;
182  VideoStandard _videoStandard;
183  io_method _ioMethod;
184  int _fileDescriptor;
185  std::vector<buffer> _buffers;
186 
187  PixelFormat _pixelFormat;
188  int _pixelDecimation;
189  int _lineLength;
190  int _frameByteSize;
191 
192  // signal detection
193  int _noSignalCounterThreshold;
194  ColorRgb _noSignalThresholdColor;
195  bool _signalDetectionEnabled;
196  bool _noSignalDetected;
197  int _noSignalCounter;
198  double _x_frac_min;
199  double _y_frac_min;
200  double _x_frac_max;
201  double _y_frac_max;
202 
203  QSocketNotifier *_streamNotifier;
204 
205  bool _initialized;
206  bool _deviceAutoDiscoverEnabled;
207 };
virtual void setSignalThreshold(double redSignalThreshold, double greenSignalThreshold, double blueSignalThreshold, int noSignalCounterThreshold=50)
overwrite Grabber.h implementation
Definition: V4L2Grabber.cpp:186
virtual void setDeviceVideoStandard(QString device, VideoStandard videoStandard)
overwrite Grabber.h implementation
Definition: V4L2Grabber.cpp:1119
virtual void setSignalDetectionOffset(double verticalMin, double horizontalMin, double verticalMax, double horizontalMax)
overwrite Grabber.h implementation
Definition: V4L2Grabber.cpp:196
virtual void setPixelDecimation(int pixelDecimation)
set new PixelDecimation value to ImageResampler
Definition: V4L2Grabber.cpp:1109
Components
Enumeration of components in Hyperion.
Definition: Components.h:10
virtual void setSignalDetectionEnable(bool enable)
overwrite Grabber.h implementation
Definition: V4L2Grabber.cpp:1100
virtual void setWidthHeight()
overwrite Grabber.h implementation, as v4l doesn&#39;t use width/height
Definition: V4L2Grabber.h:52
The Grabber class is responsible to apply image resizes (with or without ImageResampler) Overwrite th...
Definition: Grabber.h:17
Logger * _log
logger instance
Definition: Grabber.h:119
Capture class for V4L2 devices.
Definition: V4L2Grabber.h:28
Plain-Old-Data structure containing the red-green-blue color specification.
Definition: ColorRgb.h:13