| 1 | /* --------------------------------------------------------------------------- |
| 2 | ** This software is in the public domain, furnished "as is", without technical |
| 3 | ** support, and with no warranty, express or implied, as to its usefulness for |
| 4 | ** any purpose. |
| 5 | ** |
| 6 | ** V4l2Capture.cpp |
| 7 | ** |
| 8 | ** V4L2 wrapper |
| 9 | ** |
| 10 | ** -------------------------------------------------------------------------*/ |
| 11 | |
| 12 | |
| 13 | // libv4l2 |
| 14 | #include <linux/videodev2.h> |
| 15 | |
| 16 | // project |
| 17 | #include "logger.h" |
| 18 | #include "V4l2Capture.h" |
| 19 | #include "V4l2MmapDevice.h" |
| 20 | #include "V4l2ReadWriteDevice.h" |
| 21 | |
| 22 | |
| 23 | // ----------------------------------------- |
| 24 | // create video capture interface |
| 25 | // ----------------------------------------- |
| 26 | V4l2Capture* V4l2Capture::create(const V4L2DeviceParameters & param, IoType iotype) |
| 27 | { |
| 28 | V4l2Capture* videoCapture = NULL; |
| 29 | V4l2Device* videoDevice = NULL; |
| 30 | int caps = V4L2_CAP_VIDEO_CAPTURE; |
| 31 | switch (iotype) |
| 32 | { |
| 33 | case IOTYPE_MMAP: |
| 34 | videoDevice = new V4l2MmapDevice(param, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 35 | caps |= V4L2_CAP_STREAMING; |
| 36 | break; |
| 37 | case IOTYPE_READWRITE: |
| 38 | videoDevice = new V4l2ReadWriteDevice(param, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 39 | caps |= V4L2_CAP_READWRITE; |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | if (videoDevice && !videoDevice->init(caps)) |
| 44 | { |
| 45 | delete videoDevice; |
| 46 | videoDevice=NULL; |
| 47 | } |
| 48 | |
| 49 | if (videoDevice) |
| 50 | { |
| 51 | videoCapture = new V4l2Capture(videoDevice); |
| 52 | } |
| 53 | return videoCapture; |
| 54 | } |
| 55 | |
| 56 | // ----------------------------------------- |
| 57 | // constructor |
| 58 | // ----------------------------------------- |
| 59 | V4l2Capture::V4l2Capture(V4l2Device* device) : V4l2Access(device) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | // ----------------------------------------- |
| 64 | // destructor |
| 65 | // ----------------------------------------- |
| 66 | V4l2Capture::~V4l2Capture() |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | // ----------------------------------------- |
| 71 | // check readability |
| 72 | // ----------------------------------------- |
| 73 | int V4l2Capture::isReadable(timeval* tv) |
| 74 | { |
| 75 | int fd = m_device->getFd(); |
| 76 | fd_set fdset; |
| 77 | FD_ZERO(&fdset); |
| 78 | FD_SET(fd, &fdset); |
| 79 | return select(fd+1, &fdset, NULL, NULL, tv); |
| 80 | } |
| 81 | |
| 82 | // ----------------------------------------- |
| 83 | // read from V4l2Device |
| 84 | // ----------------------------------------- |
| 85 | size_t V4l2Capture::read(char* buffer, size_t bufferSize) |
| 86 | { |
| 87 | return m_device->readInternal(buffer, bufferSize); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |