VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvcapture_v4l.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gvcapture_v4l.h
19 ///
20 
21 #ifndef GV_CAPTURE_V4L__H
22 #define GV_CAPTURE_V4L__H
23 
24 #include "gdef.h"
25 #include "gvcapture.h"
26 #include "gvcapture_v4l.h"
27 #include "gvcapturebuffer.h"
28 #include <string>
29 #include <vector>
30 
31 namespace Gv
32 {
33  class CaptureV4l ;
34  class CaptureRequeuer ;
35 }
36 
37 /// \class Gv::CaptureV4l
38 /// A video capture implementation for video-for-linux (v4l).
39 ///
41 {
42 public:
43  enum io_method
44  {
45  IO_METHOD_READ,
46  IO_METHOD_MMAP,
48  ///< TODO DMA method
49  } ;
50 
51 private:
52  typedef std::vector<shared_ptr<Gv::CaptureBuffer> > CaptureBuffers ;
53  typedef unsigned long size_type ; // __u32
54 
55 public:
56  CaptureV4l( const std::string & dev_name , const std::string & dev_config ) ;
57  ///< Constructor. Opens the video device, with optional device-specific
58  ///< configuration that can include "buffers=<n>", "nolibv4l", "mmap",
59  ///< "read", "userptr", and "fmt=<n>". The number of buffers should
60  ///< be small to reduce latency, or larger for better throughput.
61 
62  ~CaptureV4l() ;
63  ///< Destructor.
64 
65  virtual void start() override ;
66  ///< Override from Gv::Capture.
67 
68  virtual void stop() override ;
69  ///< Override from Gv::Capture.
70 
71  virtual bool simple() const override ;
72  ///< Override from Gv::Capture.
73 
74  virtual bool read( unsigned char * , size_t ) override ;
75  ///< Override from Gv::Capture.
76 
77  virtual bool read( CaptureCallback & ) override ;
78  ///< Override from Gv::Capture.
79 
80  virtual int fd() const override ;
81  ///< Override from Gv::Capture.
82 
83  virtual unsigned int dx() const override ;
84  ///< Override from Gv::Capture.
85 
86  virtual unsigned int dy() const override ;
87  ///< Override from Gv::Capture.
88 
89  virtual bool active() const override ;
90  ///< Override from Gv::Capture.
91 
92  virtual std::string info() const override ;
93  ///< Override from Gv::Capture.
94 
95 private:
96  friend class Gv::CaptureRequeuer ;
97  CaptureV4l( const CaptureV4l & ) ;
98  void operator=( const CaptureV4l & ) ;
99  //
100  static int open_device( const std::string & dev_name ) ;
101  static io_method check_device( int fd , const std::string & dev_config ) ;
102  CaptureBufferScale init_device( const std::string & dev_config ) ;
103  void set_simple( const std::string & dev_config ) ;
104  //
105  void create_buffers( size_type sizeimage , const std::string & dev_config ) ;
106  static void create_buffers_read( CaptureBuffers & buffers , size_type buffer_size , unsigned int nbuffers ) ;
107  static void create_buffers_mmap( CaptureBuffers & buffers , int fd , size_t , unsigned int nbuffers ) ;
108  static void create_buffers_userptr( CaptureBuffers & buffers , int fd , size_type buffer_size , unsigned int nbuffers ) ;
109  //
110  void add_buffers() ;
111  static void add_buffers_read() ;
112  static void add_buffers_mmap( CaptureBuffers & buffers , int fd ) ;
113  static void add_buffers_userptr( CaptureBuffers & buffers , int fd ) ;
114  //
115  static void start_capturing_read() ;
116  static void start_capturing_mmap( int fd ) ;
117  static void start_capturing_userptr( int fd ) ;
118  //
119  static void stop_capturing_read() ;
120  static void stop_capturing_mmap( int fd ) ;
121  static void stop_capturing_userptr( int fd ) ;
122  //
123  bool read_frame_read( CaptureCallback & ) ;
124  bool read_frame_read_simple( unsigned char * p , size_t n ) ;
125  bool read_frame_mmap( CaptureCallback & ) ;
126  bool read_frame_userptr( CaptureCallback & ) ;
127 
128 private:
129  int m_fd ;
130  io_method m_io ;
131  std::vector<shared_ptr<Gv::CaptureBuffer> > m_buffers ;
132  unsigned int m_dx ;
133  unsigned int m_dy ;
134  bool m_active ;
135  CaptureBufferFormat m_format ;
136  CaptureBufferScale m_buffer_scale ;
137  bool m_simple ;
138 } ;
139 
140 #endif
A video capture implementation for video-for-linux (v4l).
Definition: gvcapture_v4l.h:40
A RAII class to do ioctl(VIDIOC_QBUF) on a v4l buffer.
virtual unsigned int dy() const override
Override from Gv::Capture.
virtual bool simple() const override
Override from Gv::Capture.
virtual unsigned int dx() const override
Override from Gv::Capture.
~CaptureV4l()
Destructor.
A descriptor for a v4l pixel format.
virtual bool active() const override
Override from Gv::Capture.
CaptureV4l(const std::string &dev_name, const std::string &dev_config)
Constructor.
A structure holding capture buffer dimensions.
virtual bool read(unsigned char *, size_t) override
Override from Gv::Capture.
A callback interface for the Gv::Capture class.
Definition: gvcapture.h:99
A video capture abstract interface, exposing a file descriptor and a method to handle read events on ...
Definition: gvcapture.h:40
virtual std::string info() const override
Override from Gv::Capture.
virtual int fd() const override
Override from Gv::Capture.
virtual void stop() override
Override from Gv::Capture.
virtual void start() override
Override from Gv::Capture.