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
/* ---------------------------------------------------------------------------
** 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.
**
** V4l2Capture.cpp
** 
** V4L2 wrapper 
**
** -------------------------------------------------------------------------*/
 
 
// libv4l2
#include <linux/videodev2.h>
 
// project
#include "logger.h"
#include "V4l2Capture.h"
#include "V4l2MmapDevice.h"
#include "V4l2ReadWriteDevice.h"
 
 
// -----------------------------------------
//    create video capture interface
// -----------------------------------------
V4l2Capture* V4l2Capture::create(const V4L2DeviceParameters & param, IoType iotype)
{
    V4l2Capture* videoCapture = NULL;
    V4l2Device* videoDevice = NULL; 
    int caps = V4L2_CAP_VIDEO_CAPTURE;
    switch (iotype)
    {
        case IOTYPE_MMAP: 
            videoDevice = new V4l2MmapDevice(param, V4L2_BUF_TYPE_VIDEO_CAPTURE); 
            caps |= V4L2_CAP_STREAMING;
        break;
        default:          
            videoDevice = new V4l2ReadWriteDevice(param, V4L2_BUF_TYPE_VIDEO_CAPTURE); 
            caps |= V4L2_CAP_READWRITE;
        break;
    }
    
    if (videoDevice &&  !videoDevice->init(caps))
    {
        delete videoDevice;
        videoDevice=NULL; 
    }
    
    if (videoDevice)
    {
        videoCapture = new V4l2Capture(videoDevice);
    }    
    return videoCapture;
}
 
// -----------------------------------------
//    constructor
// -----------------------------------------
V4l2Capture::V4l2Capture(V4l2Device* device) : V4l2Access(device)
{
}
 
// -----------------------------------------
//    check readability
// -----------------------------------------
int V4l2Capture::isReadable(timeval* tv)
{
    int fd = m_device->getFd();
    fd_set fdset;
    FD_ZERO(&fdset);    
    FD_SET(fd, &fdset);
    return select(fd+1, &fdset, NULL, NULL, tv);
}
 
// -----------------------------------------
//    read from V4l2Device
// -----------------------------------------
size_t V4l2Capture::read(char* buffer, size_t bufferSize)
{
    return m_device->readInternal(buffer, bufferSize);
}