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

#ifndef __VIDEOV4L2SOURCE_H_INCL__
#define __VIDEOV4L2SOURCE_H_INCL__
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <asm/types.h> /* for videodev2.h */
#include <linux/videodev2.h>
#include <libv4l2.h>
#include <opencv2/opencv.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/opencv_modules.hpp> // Gaussian Blur
//#include <opencv2/videoio.hpp>
//#include <opencv2/highgui.hpp> // OpenCV window I/O
#include "../../../components/base/basicMediaSource.h"
#include "../../../components/base/basicSettings.h"
#include "../../../alg/generic/md5.h"
/* Defaults - If your board can do better, set it here. Set for the most common type inputs. */
#define DEFAULT_V4L_WIDTH 640
#define DEFAULT_V4L_HEIGHT 480
#define DEFAULT_V4L_FPS 30
#define MAX_CAMERAS 8
// default and maximum number of V4L buffers, not including last, 'special' buffer
#define MAX_V4L_BUFFERS 22
#define DEFAULT_V4L_BUFFERS 20
// if enabled, then bad JPEG warnings become errors and cause NULL returned instead of image
//#define V4L_ABORT_BADJPEG
/* Device Capture Objects */
/* V4L2 structure */
struct Buffer {
void * start;
size_t length;
// This is dequeued buffer. It used for to put it back in the queue.
// The buffer is valid only if capture->bufferIndex >= 0
v4l2_buffer buffer;
Buffer() : start(NULL), length(0)
{
buffer = v4l2_buffer();
}
};
/**
* TODO: Add class description
*
* @author Gagua
*/
class videoV4L2Source : public basicMediaSource {
private:
config_setting_t *m_pConfig;
basicLogCollection *m_pLog;
char m_pCookie[33];
int m_nDeviceHandle;
int8_t m_bBufferIndex;
bool m_bVirginity;
CSTR m_pDeviceName;
IplImage m_oFrame;
uint32_t m_nPixelFormat;
int m_nWidth, m_nHeight;
int m_nWidthSet, m_nHeightSet;
uint8_t m_nBufferCount;
uint32_t m_nFpsNumerator;
uint32_t m_nFpsDenominator;
float m_fFps;
bool m_bConvertToRGB;
bool m_bFrameAllocated;
bool m_bReturnFrame;
// To select a video input set cv::CAP_PROP_CHANNEL to channel number.
// If the new channel number is than 0, then a video input will not change
int32_t m_nChannelNumber;
// Normalize properties. If set parameters will be converted to/from [0,1) range.
// Enabled by default (as OpenCV 3.x does).
// Value is initialized from the environment variable `OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED`:
// To select real parameters mode after devise is open set cv::CAP_PROP_MODE to 0
// any other value revert the backward compatibility mode (with normalized properties).
// Range normalization affects the following parameters:
// cv::CAP_PROP_*: BRIGHTNESS,CONTRAST,SATURATION,HUE,GAIN,EXPOSURE,FOCUS,AUTOFOCUS,AUTO_EXPOSURE.
bool m_bNormalizePropRange;
/* V4L2 variables */
Buffer buffers[MAX_V4L_BUFFERS + 1];
v4l2_capability capability;
v4l2_input videoInput;
v4l2_format m_sFormat;
v4l2_requestbuffers m_sBufferReq;
v4l2_buf_type m_sBufferType;
timeval timestamp;
bool requestBuffers();
bool requestBuffers( uint8_t buffer_number);
bool createBuffers();
void releaseBuffers();
bool initCapture();
bool streaming(bool startStream);
bool setFps( float FramesPerSecond );
bool setFps( uint32_t denominator, uint32_t numerator = 1 );
bool tryIoctl(unsigned long ioctlCode, void *parameter) const;
bool controlInfo(int property_id, __u32 &v4l2id, cv::Range &range) const;
bool icvControl(__u32 v4l2id, int &value, bool isSet) const;
bool icvSetFrameSize(int _width, int _height);
bool v4l2_reset();
bool setVideoInputChannel();
bool try_palette_v4l2();
bool try_init_v4l2();
bool autosetup_capture_mode_v4l2();
void v4l2_create_frame();
bool read_frame_v4l2();
bool convertableToRgb() const;
void convertToRgb(const Buffer &currentBuffer);
void releaseFrame();
bool grabFrame();
void m_fThreadProc( void );
public:
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 );
bool _close( void );
bool _stop( void );
// Constructor
videoV4L2Source();
videoV4L2Source( CSTR URI );
videoV4L2Source( config_setting_t *CFG );
// Destructor
virtual ~videoV4L2Source();
};
#endif // __VIDEOAVLIBSOURCE_H_INCL__