Visual Servoing Platform  version 3.6.1 under development (2024-10-15)
vpV4l2Grabber.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Video for linux two framegrabber.
33  *
34 *****************************************************************************/
35 
41 #ifndef vpV4l2Grabber_hh
42 #define vpV4l2Grabber_hh
43 
44 #include <visp3/core/vpConfig.h>
45 
46 #ifdef VISP_HAVE_V4L2
47 
48 #include <libv4l2.h> // Video For Linux Two interface
49 #include <linux/videodev2.h> // Video For Linux Two interface
50 #include <string>
51 
52 #include <visp3/core/vpFrameGrabber.h>
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpRGBa.h>
55 #include <visp3/core/vpRect.h>
56 
57 BEGIN_VISP_NAMESPACE
131 class VISP_EXPORT vpV4l2Grabber : public vpFrameGrabber
132 {
133 public:
134  static const unsigned int DEFAULT_INPUT;
135  static const unsigned int DEFAULT_SCALE;
136  static const __u32 MAX_INPUTS;
137  static const __u32 MAX_NORM;
138  static const __u32 MAX_FORMAT;
139  static const unsigned int MAX_CTRL;
140  static const unsigned int MAX_BUFFERS;
141  static const unsigned int FRAME_SIZE;
142 
146  typedef enum
147  {
149  framerate_25fps
150  } vpV4l2FramerateType;
151 
155  typedef enum
156  {
158  V4L2_IMAGE_FORMAT
159  } vpV4l2FrameFormatType;
160 
164  typedef enum
165  {
171  V4L2_MAX_FORMAT
172  } vpV4l2PixelFormatType;
173 
174 #ifndef DOXYGEN_SHOULD_SKIP_THIS
175  struct ng_video_fmt
176  {
177  unsigned int pixelformat; /* VIDEO_* */
178  unsigned int width;
179  unsigned int height;
180  unsigned int bytesperline; /* zero for compressed formats */
181  };
182 
183  struct ng_video_buf
184  {
185  struct ng_video_fmt fmt;
186  size_t size;
187  unsigned char *data;
188  int refcount;
189  };
190 #endif
191 
192  // private:
193  //#ifndef DOXYGEN_SHOULD_SKIP_THIS
194  // vpV4l2Grabber(const vpV4l2Grabber &)
195  // : fd(-1), device(), cap(), streamparm(), inp(nullptr), std(nullptr),
196  // fmt(nullptr), ctl(nullptr),
197  // fmt_v4l2(), fmt_me(), reqbufs(), buf_v4l2(nullptr), buf_me(nullptr),
198  // queue(0), waiton_cpt(0), index_buffer(0), m_verbose(false),
199  // m_nbuffers(3), field(0), streaming(false),
200  // m_input(vpV4l2Grabber::DEFAULT_INPUT),
201  // m_framerate(vpV4l2Grabber::framerate_25fps),
202  // m_frameformat(vpV4l2Grabber::V4L2_FRAME_FORMAT),
203  // m_pixelformat(vpV4l2Grabber::V4L2_YUYV_FORMAT)
204  // {
205  // throw vpException(vpException::functionNotImplementedError,"Not
206  // implemented!");
207  // }
208  // vpV4l2Grabber &operator=(const vpV4l2Grabber &){
209  // throw vpException(vpException::functionNotImplementedError,"Not
210  // implemented!"); return *this;
211  // }
212  //#endif
213 
214 public:
215  vpV4l2Grabber();
216  VP_EXPLICIT vpV4l2Grabber(bool verbose);
217  vpV4l2Grabber(unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
218  vpV4l2Grabber(vpImage<unsigned char> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
219  vpV4l2Grabber(vpImage<vpRGBa> &I, unsigned input, unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
220  virtual ~vpV4l2Grabber();
221 
222 public:
223  void open(vpImage<unsigned char> &I);
224  void open(vpImage<vpRGBa> &I);
225 
227  void acquire(vpImage<unsigned char> &I, const vpRect &roi);
228  void acquire(vpImage<unsigned char> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
229  void acquire(vpImage<vpRGBa> &I);
230  void acquire(vpImage<vpRGBa> &I, const vpRect &roi);
231  void acquire(vpImage<vpRGBa> &I, struct timeval &timestamp, const vpRect &roi = vpRect());
232  bool getField();
233  vpV4l2FramerateType getFramerate();
241  inline vpV4l2PixelFormatType getPixelFormat() { return (this->m_pixelformat); }
242 
243  vpV4l2Grabber &operator>>(vpImage<unsigned char> &I);
244  vpV4l2Grabber &operator>>(vpImage<vpRGBa> &I);
245 
250  void setVerboseMode(bool verbose) { this->m_verbose = verbose; };
251  void setFramerate(vpV4l2FramerateType framerate);
252 
253  void setInput(unsigned input = vpV4l2Grabber::DEFAULT_INPUT);
254 
259  inline void setWidth(unsigned w) { this->width = w; }
264  inline void setHeight(unsigned h) { this->height = h; }
265 
266  void setScale(unsigned scale = vpV4l2Grabber::DEFAULT_SCALE);
267 
279  inline void setNBuffers(unsigned nbuffers) { this->m_nbuffers = nbuffers; }
280 
287  inline void setDevice(const std::string &devname) { device = devname; }
296  inline void setPixelFormat(vpV4l2PixelFormatType pixelformat)
297  {
298  this->m_pixelformat = pixelformat;
299  if (this->m_pixelformat >= V4L2_MAX_FORMAT)
300  this->m_pixelformat = V4L2_RGB24_FORMAT;
301  }
302 
303  void close();
304 
305 private:
306  void setFormat();
314  inline void setFrameFormat(vpV4l2FrameFormatType frameformat) { this->m_frameformat = frameformat; }
315  void open();
316  void getCapabilities();
317  void startStreaming();
318  void stopStreaming();
319  unsigned char *waiton(__u32 &index, struct timeval &timestamp);
320  int queueBuffer();
321  void queueAll();
322  void printBufInfo(struct v4l2_buffer buf);
323 
324  int fd;
325  std::string device;
326  /* device descriptions */
327  struct v4l2_capability cap;
328  struct v4l2_streamparm streamparm;
329  struct v4l2_input *inp; //[vpV4l2Grabber::MAX_INPUTS];
330  struct v4l2_standard *std; //[vpV4l2Grabber::MAX_NORM];
331  struct v4l2_fmtdesc *fmt; //[vpV4l2Grabber::MAX_FORMAT];
332  struct v4l2_queryctrl *ctl; //[vpV4l2Grabber::MAX_CTRL*2];
333 
334  /* capture */
335  struct v4l2_format fmt_v4l2;
336  struct ng_video_fmt fmt_me;
337  struct v4l2_requestbuffers reqbufs;
338  struct v4l2_buffer *buf_v4l2; //[vpV4l2Grabber::MAX_BUFFERS];
339  struct ng_video_buf *buf_me; //[vpV4l2Grabber::MAX_BUFFERS];
340  unsigned int queue;
341  unsigned int waiton_cpt;
342  __u32 index_buffer;
343 
344  bool m_verbose;
345  unsigned m_nbuffers;
346  unsigned int field;
347  bool streaming;
348 
349  unsigned m_input;
350  vpV4l2FramerateType m_framerate;
351  vpV4l2FrameFormatType m_frameformat;
352  vpV4l2PixelFormatType m_pixelformat;
353 };
354 END_VISP_NAMESPACE
355 #endif
356 #endif
Base class for all video devices. It is designed to provide a front end to video sources.
virtual void open(vpImage< unsigned char > &I)=0
virtual void acquire(vpImage< unsigned char > &I)=0
virtual void close()=0
Defines a rectangle in the plane.
Definition: vpRect.h:79
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
@ framerate_50fps
50 frames per second
void setWidth(unsigned w)
static const __u32 MAX_NORM
static const unsigned int MAX_CTRL
static const unsigned int MAX_BUFFERS
static const __u32 MAX_INPUTS
static const unsigned int DEFAULT_INPUT
void setVerboseMode(bool verbose)
vpV4l2PixelFormatType getPixelFormat()
static const unsigned int FRAME_SIZE
static const unsigned int DEFAULT_SCALE
static const __u32 MAX_FORMAT
void setHeight(unsigned h)
void setPixelFormat(vpV4l2PixelFormatType pixelformat)
void setNBuffers(unsigned nbuffers)
void setDevice(const std::string &devname)