houxiao
2017-07-13 b022b91c0c6fa807424b6c12cc92ac5946838083
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* ---------------------------------------------------------------------------
** This software is in the public domain, furnished "as is", without technical
** support, and with no warranty, express or implied, as to its usefulness for
** any purpose.
**
** V4l2Device.h
** 
** V4L2 wrapper 
**
** -------------------------------------------------------------------------*/
 
 
#ifndef V4L2_DEVICE
#define V4L2_DEVICE
 
#include <string>
#include <linux/videodev2.h>
 
#ifndef V4L2_PIX_FMT_VP8
#define V4L2_PIX_FMT_VP8  v4l2_fourcc('V', 'P', '8', '0')
#endif
#ifndef V4L2_PIX_FMT_VP9
#define V4L2_PIX_FMT_VP9  v4l2_fourcc('V', 'P', '9', '0')
#endif
 
// ---------------------------------
// V4L2 Device parameters
// ---------------------------------
struct V4L2DeviceParameters 
{
    V4L2DeviceParameters(const char* devname, unsigned int format, unsigned int width, unsigned int height, int fps, int verbose) : 
        m_devName(devname), m_format(format), m_width(width), m_height(height), m_fps(fps), m_verbose(verbose) {};
        
    std::string m_devName;
    unsigned int m_format;
    unsigned int m_width;
    unsigned int m_height;
    int m_fps;            
    int m_verbose;
};
 
// ---------------------------------
// V4L2 Device
// ---------------------------------
class V4l2Device
{        
    friend class V4l2Capture;
    friend class V4l2Output;
    
    protected:    
        void close();    
    
        int initdevice(const char *dev_name, unsigned int mandatoryCapabilities);
        int checkCapabilities(int fd, unsigned int mandatoryCapabilities);
        int configureFormat(int fd);
        int configureParam(int fd);
 
        virtual bool init(unsigned int mandatoryCapabilities);        
        virtual size_t writeInternal(char*, size_t) { return -1; };
        virtual size_t readInternal(char*, size_t)  { return -1; };        
    
    public:
        V4l2Device(const V4L2DeviceParameters&  params, v4l2_buf_type deviceType);        
        virtual ~V4l2Device();
    
        virtual bool isReady() { return (m_fd != -1); }
        virtual bool start()   { return true; }
        virtual bool stop()    { return true; }
    
        int getBufferSize() { return m_bufferSize; }
        int getFormat()     { return m_format;     }
        int getWidth()      { return m_width;      }
        int getHeight()     { return m_height;     }
        int getFd()         { return m_fd;         }
        void queryFormat();    
 
    protected:
        V4L2DeviceParameters m_params;
        int m_fd;
        v4l2_buf_type m_deviceType;    
    
        int m_bufferSize;
        int m_format;
        int m_width;
        int m_height;    
};
 
 
#endif