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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* ---------------------------------------------------------------------------
** 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.cpp
** 
** -------------------------------------------------------------------------*/
 
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
 
// libv4l2
#include <linux/videodev2.h>
#include <linux/stat.h>
#include <unistd.h>
#include <fcntl.h>
 
#include "logger.h"
 
#include "V4l2Device.h"
 
// -----------------------------------------
//    V4L2Device
// -----------------------------------------
V4l2Device::V4l2Device(const V4L2DeviceParameters&  params, v4l2_buf_type deviceType) : m_params(params), m_fd(-1), m_deviceType(deviceType), m_bufferSize(0), m_format(0)
{
}
 
V4l2Device::~V4l2Device() 
{
    this->close();
}
 
void V4l2Device::close() 
{
    if (m_fd != -1)         
        ::close(m_fd);
    
    m_fd = -1;
}
 
// query current format
void V4l2Device::queryFormat()
{
    struct v4l2_format     fmt;
    memset(&fmt,0,sizeof(fmt));
    fmt.type  = m_deviceType;
    if (0 == ioctl(m_fd,VIDIOC_G_FMT,&fmt)) 
    {
        m_format     = fmt.fmt.pix.pixelformat;
        m_width      = fmt.fmt.pix.width;
        m_height     = fmt.fmt.pix.height;
        m_bufferSize = fmt.fmt.pix.sizeimage;
    }
}
 
// intialize the V4L2 connection
bool V4l2Device::init(unsigned int mandatoryCapabilities)
{
    struct stat sb;
    if ( (stat(m_params.m_devName.c_str(), &sb)==0) && ((sb.st_mode & S_IFMT) == S_IFCHR) )
    {
        if (initdevice(m_params.m_devName.c_str(), mandatoryCapabilities) == -1)
        {
            LOG(ERROR) << "Cannot init device:" << m_params.m_devName << LOG_ENDL;
        }
    }
    else
    {
        // open a normal file
        m_fd = open(m_params.m_devName.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
    }
    return (m_fd!=-1);
}
 
// intialize the V4L2 device
int V4l2Device::initdevice(const char *dev_name, unsigned int mandatoryCapabilities)
{
    m_fd = open(dev_name,  O_RDWR | O_NONBLOCK);
    if (m_fd < 0) 
    {
        LOG(ERROR) << "Cannot open device:" << m_params.m_devName << " " << strerror(errno) << LOG_ENDL;
        this->close();
        return -1;
    }
    if (checkCapabilities(m_fd,mandatoryCapabilities) !=0)
    {
        this->close();
        return -1;
    }    
    if (configureFormat(m_fd) !=0) 
    {
        this->close();
        return -1;
    }
    if (configureParam(m_fd) !=0)
    {
        this->close();
        return -1;
    }
    
    return m_fd;
}
 
// check needed V4L2 capabilities
int V4l2Device::checkCapabilities(int fd, unsigned int mandatoryCapabilities)
{
    struct v4l2_capability cap;
    memset(&(cap), 0, sizeof(cap));
    if (-1 == ioctl(fd, VIDIOC_QUERYCAP, &cap)) 
    {
        LOG(ERROR) << "Cannot get capabilities for device:" << m_params.m_devName << " " << strerror(errno) << LOG_ENDL;
        return -1;
    }
    LOG(NOTICE) << "driver:" << cap.driver << " " << std::hex << cap.capabilities << LOG_ENDL;
        
    if ((cap.capabilities & V4L2_CAP_READWRITE))    LOG(NOTICE) << m_params.m_devName << " support read/write" << LOG_ENDL;
    if ((cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) LOG(NOTICE) << m_params.m_devName << " support output" << LOG_ENDL;
    if ((cap.capabilities & V4L2_CAP_STREAMING))    LOG(NOTICE) << m_params.m_devName << " support streaming" << LOG_ENDL;
    if ((cap.capabilities & V4L2_CAP_TIMEPERFRAME)) LOG(NOTICE) << m_params.m_devName << " support timeperframe" << LOG_ENDL; 
    
    if ( (cap.capabilities & mandatoryCapabilities) != mandatoryCapabilities )
    {
        LOG(ERROR) << "Mandatory capability not available for device:" << m_params.m_devName << LOG_ENDL;
        return -1;
    }
    
    return 0;
}
 
std::string fourcc(unsigned int format)
{
    char formatArray[] = { (char)(format&0xff), (char)((format>>8)&0xff), (char)((format>>16)&0xff), (char)((format>>24)&0xff), 0 };
    return std::string(formatArray, strlen(formatArray));
}
 
// configure capture format 
int V4l2Device::configureFormat(int fd)
{
    
    if (m_params.m_format==0) 
    {
        this->queryFormat();
        m_params.m_format = m_format;
        m_params.m_width  = m_width;
        m_params.m_height = m_height;
    }        
        
    struct v4l2_format   fmt;            
    memset(&(fmt), 0, sizeof(fmt));
    fmt.type                = m_deviceType;
    fmt.fmt.pix.width       = m_params.m_width;
    fmt.fmt.pix.height      = m_params.m_height;
    fmt.fmt.pix.pixelformat = m_params.m_format;
    fmt.fmt.pix.field       = V4L2_FIELD_ANY;
    
    if (ioctl(fd, VIDIOC_S_FMT, &fmt) == -1)
    {
        LOG(ERROR) << "Cannot set format for device:" << m_params.m_devName << " " << strerror(errno) << LOG_ENDL;
        return -1;
    }            
    if (fmt.fmt.pix.pixelformat != m_params.m_format) 
    {
        LOG(ERROR) << "Cannot set pixelformat to:" << fourcc(m_params.m_format) << " format is:" << fourcc(fmt.fmt.pix.pixelformat) << LOG_ENDL;
        return -1;
    }
    if ((fmt.fmt.pix.width != m_params.m_width) || (fmt.fmt.pix.height != m_params.m_height))
    {
        LOG(WARN) << "Cannot set size width:" << fmt.fmt.pix.width << " height:" << fmt.fmt.pix.height << LOG_ENDL;
    }
    
    m_format     = fmt.fmt.pix.pixelformat;
    m_width      = fmt.fmt.pix.width;
    m_height     = fmt.fmt.pix.height;        
    m_bufferSize = fmt.fmt.pix.sizeimage;
    
    LOG(NOTICE) << m_params.m_devName << ":" << fourcc(m_format) << " size:" << m_params.m_width << "x" << m_params.m_height << " bufferSize:" << m_bufferSize << LOG_ENDL;
    
    return 0;
}
 
// configure capture FPS 
int V4l2Device::configureParam(int fd)
{
    if (m_params.m_fps!=0)
    {
        struct v4l2_streamparm   param;            
        memset(&(param), 0, sizeof(param));
        param.type = m_deviceType;
        param.parm.capture.timeperframe.numerator = 1;
        param.parm.capture.timeperframe.denominator = m_params.m_fps;
 
        if (ioctl(fd, VIDIOC_S_PARM, &param) == -1)
        {
            LOG(WARN) << "Cannot set param for device:" << m_params.m_devName << " " << strerror(errno) << LOG_ENDL;
        }
    
        LOG(NOTICE) << "fps:" << param.parm.capture.timeperframe.numerator << "/" << param.parm.capture.timeperframe.denominator << LOG_ENDL;
        LOG(NOTICE) << "nbBuffer:" << param.parm.capture.readbuffers << LOG_ENDL;
    }
    
    return 0;
}