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.

147 lines
5.1 KiB

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