| 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 | ** V4l2Device.h |
| 7 | ** |
| 8 | ** V4L2 wrapper |
| 9 | ** |
| 10 | ** -------------------------------------------------------------------------*/ |
| 11 | |
| 12 | |
| 13 | #ifndef V4L2_DEVICE |
| 14 | #define V4L2_DEVICE |
| 15 | |
| 16 | #include <string> |
| 17 | #include <list> |
| 18 | #include <linux/videodev2.h> |
| 19 | #include <fcntl.h> |
| 20 | |
| 21 | #ifndef V4L2_PIX_FMT_VP8 |
| 22 | #define V4L2_PIX_FMT_VP8 v4l2_fourcc('V', 'P', '8', '0') |
| 23 | #endif |
| 24 | #ifndef V4L2_PIX_FMT_VP9 |
| 25 | #define V4L2_PIX_FMT_VP9 v4l2_fourcc('V', 'P', '9', '0') |
| 26 | #endif |
| 27 | #ifndef V4L2_PIX_FMT_HEVC |
| 28 | #define V4L2_PIX_FMT_HEVC v4l2_fourcc('H', 'E', 'V', 'C') |
| 29 | #endif |
| 30 | |
| 31 | // --------------------------------- |
| 32 | // V4L2 Device parameters |
| 33 | // --------------------------------- |
| 34 | struct V4L2DeviceParameters |
| 35 | { |
| 36 | V4L2DeviceParameters(const char* devname, const std::list<unsigned int> & formatList, unsigned int width, unsigned int height, int fps, int verbose = 0, int openFlags = O_RDWR | O_NONBLOCK) : |
| 37 | m_devName(devname), m_formatList(formatList), m_width(width), m_height(height), m_fps(fps), m_verbose(verbose), m_openFlags(openFlags) {} |
| 38 | |
| 39 | V4L2DeviceParameters(const char* devname, unsigned int format, unsigned int width, unsigned int height, int fps, int verbose = 0, int openFlags = O_RDWR | O_NONBLOCK) : |
| 40 | m_devName(devname), m_width(width), m_height(height), m_fps(fps), m_verbose(verbose), m_openFlags(openFlags) { |
| 41 | if (format) { |
| 42 | m_formatList.push_back(format); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | std::string m_devName; |
| 47 | std::list<unsigned int> m_formatList; |
| 48 | unsigned int m_width; |
| 49 | unsigned int m_height; |
| 50 | int m_fps; |
| 51 | int m_verbose; |
| 52 | int m_openFlags; |
| 53 | }; |
| 54 | |
| 55 | // --------------------------------- |
| 56 | // V4L2 Device |
| 57 | // --------------------------------- |
| 58 | class V4l2Device |
| 59 | { |
| 60 | friend class V4l2Capture; |
| 61 | friend class V4l2Output; |
| 62 | |
| 63 | protected: |
| 64 | void close(); |
| 65 | |
| 66 | int initdevice(const char *dev_name, unsigned int mandatoryCapabilities); |
| 67 | int checkCapabilities(int fd, unsigned int mandatoryCapabilities); |
| 68 | int configureFormat(int fd); |
| 69 | int configureFormat(int fd, unsigned int format, unsigned int width, unsigned int height); |
| 70 | int configureParam(int fd); |
| 71 | |
| 72 | virtual bool init(unsigned int mandatoryCapabilities); |
| 73 | virtual size_t writeInternal(char*, size_t) { return -1; } |
| 74 | virtual bool startPartialWrite(void) { return false; } |
| 75 | virtual size_t writePartialInternal(char*, size_t) { return -1; } |
| 76 | virtual bool endPartialWrite(void) { return false; } |
| 77 | virtual size_t readInternal(char*, size_t) { return -1; } |
| 78 | |
| 79 | public: |
| 80 | V4l2Device(const V4L2DeviceParameters& params, v4l2_buf_type deviceType); |
| 81 | virtual ~V4l2Device(); |
| 82 | |
| 83 | virtual bool isReady() { return (m_fd != -1); } |
| 84 | virtual bool start() { return true; } |
| 85 | virtual bool stop() { return true; } |
| 86 | |
| 87 | unsigned int getBufferSize() { return m_bufferSize; } |
| 88 | unsigned int getFormat() { return m_format; } |
| 89 | unsigned int getWidth() { return m_width; } |
| 90 | unsigned int getHeight() { return m_height; } |
| 91 | int getFd() { return m_fd; } |
| 92 | void queryFormat(); |
| 93 | |
| 94 | protected: |
| 95 | V4L2DeviceParameters m_params; |
| 96 | int m_fd; |
| 97 | v4l2_buf_type m_deviceType; |
| 98 | |
| 99 | unsigned int m_bufferSize; |
| 100 | unsigned int m_format; |
| 101 | unsigned int m_width; |
| 102 | unsigned int m_height; |
| 103 | |
| 104 | struct v4l2_buffer m_partialWriteBuf; |
| 105 | bool m_partialWriteInProgress; |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | #endif |
| 110 | |