You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __VIDEOV4L2SOURCE_H_INCL__
  2. #define __VIDEOV4L2SOURCE_H_INCL__
  3. #include <asm/types.h> /* for videodev2.h */
  4. #include <assert.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <libv4l2.h>
  8. #include <linux/videodev2.h>
  9. #include <opencv2/opencv.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
  10. #include <opencv2/opencv_modules.hpp> // Gaussian Blur
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. // #include <opencv2/videoio.hpp>
  19. // #include <opencv2/highgui.hpp> // OpenCV window I/O
  20. #include "../../../alg/generic/md5.h"
  21. #include "../../../components/base/basicMediaSource.h"
  22. #include "../../../components/base/basicSettings.h"
  23. /* Defaults - If your board can do better, set it here. Set for the most common type inputs. */
  24. #define DEFAULT_V4L_WIDTH 640
  25. #define DEFAULT_V4L_HEIGHT 480
  26. #define DEFAULT_V4L_FPS 30
  27. #define MAX_CAMERAS 8
  28. // default and maximum number of V4L buffers, not including last, 'special' buffer
  29. #define MAX_V4L_BUFFERS 22
  30. #define DEFAULT_V4L_BUFFERS 20
  31. // if enabled, then bad JPEG warnings become errors and cause NULL returned instead of image
  32. // #define V4L_ABORT_BADJPEG
  33. /* Device Capture Objects */
  34. /* V4L2 structure */
  35. struct Buffer
  36. {
  37. void * start;
  38. size_t length;
  39. // This is dequeued buffer. It used for to put it back in the queue.
  40. // The buffer is valid only if capture->bufferIndex >= 0
  41. v4l2_buffer buffer;
  42. Buffer() :
  43. start(NULL),
  44. length(0)
  45. {
  46. buffer = v4l2_buffer();
  47. }
  48. };
  49. /**
  50. * TODO: Add class description
  51. *
  52. * @author Gagua
  53. */
  54. class videoV4L2Source : public basicMediaSource
  55. {
  56. private:
  57. config_setting_t * m_pConfig;
  58. basicLogCollection * m_pLog;
  59. char m_pCookie[33];
  60. int m_nDeviceHandle;
  61. int8_t m_bBufferIndex;
  62. bool m_bVirginity;
  63. CSTR m_pDeviceName;
  64. IplImage m_oFrame;
  65. uint32_t m_nPixelFormat;
  66. int m_nWidth, m_nHeight;
  67. int m_nWidthSet, m_nHeightSet;
  68. uint8_t m_nBufferCount;
  69. uint32_t m_nFpsNumerator;
  70. uint32_t m_nFpsDenominator;
  71. float m_fFps;
  72. bool m_bConvertToRGB;
  73. bool m_bFrameAllocated;
  74. bool m_bReturnFrame;
  75. // To select a video input set cv::CAP_PROP_CHANNEL to channel number.
  76. // If the new channel number is than 0, then a video input will not change
  77. int32_t m_nChannelNumber;
  78. // Normalize properties. If set parameters will be converted to/from [0,1) range.
  79. // Enabled by default (as OpenCV 3.x does).
  80. // Value is initialized from the environment variable `OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED`:
  81. // To select real parameters mode after devise is open set cv::CAP_PROP_MODE to 0
  82. // any other value revert the backward compatibility mode (with normalized properties).
  83. // Range normalization affects the following parameters:
  84. // cv::CAP_PROP_*: BRIGHTNESS,CONTRAST,SATURATION,HUE,GAIN,EXPOSURE,FOCUS,AUTOFOCUS,AUTO_EXPOSURE.
  85. bool m_bNormalizePropRange;
  86. /* V4L2 variables */
  87. Buffer buffers[MAX_V4L_BUFFERS + 1];
  88. v4l2_capability capability;
  89. v4l2_input videoInput;
  90. v4l2_format m_sFormat;
  91. v4l2_requestbuffers m_sBufferReq;
  92. v4l2_buf_type m_sBufferType;
  93. timeval timestamp;
  94. bool requestBuffers();
  95. bool requestBuffers(uint8_t buffer_number);
  96. bool createBuffers();
  97. void releaseBuffers();
  98. bool initCapture();
  99. bool streaming(bool startStream);
  100. bool setFps(float FramesPerSecond);
  101. bool setFps(uint32_t denominator, uint32_t numerator = 1);
  102. bool tryIoctl(unsigned long ioctlCode, void * parameter) const;
  103. bool controlInfo(int property_id, __u32 & v4l2id, cv::Range & range) const;
  104. bool icvControl(__u32 v4l2id, int & value, bool isSet) const;
  105. bool icvSetFrameSize(int _width, int _height);
  106. bool v4l2_reset();
  107. bool setVideoInputChannel();
  108. bool try_palette_v4l2();
  109. bool try_init_v4l2();
  110. bool autosetup_capture_mode_v4l2();
  111. void v4l2_create_frame();
  112. bool read_frame_v4l2();
  113. bool convertableToRgb() const;
  114. void convertToRgb(const Buffer & currentBuffer);
  115. void releaseFrame();
  116. bool grabFrame();
  117. void m_fThreadProc(void);
  118. public:
  119. bool _open(CSTR URI, bool ShowDeviceMenu = false, float framerate = -1.f, int inputPin = -1, int width = -1, int height = -1, bool scaled = false, int s_width = -1, int s_height = -1);
  120. bool _close(void);
  121. bool _stop(void);
  122. // Constructor
  123. videoV4L2Source();
  124. videoV4L2Source(CSTR URI);
  125. videoV4L2Source(config_setting_t * CFG);
  126. // Destructor
  127. virtual ~videoV4L2Source();
  128. };
  129. #endif // __VIDEOAVLIBSOURCE_H_INCL__