JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
VideoBuffers.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
20 #include <jevois/Core/VideoBuf.H>
21 #include <vector>
22 #include <memory>
23 #include <linux/videodev2.h>
24 
25 namespace jevois
26 {
27  //! Collection of buffers for V4L2 video frames (Camera or Gadget) with hooks to the MMAP'd areas
28  /*! Both the Camera and the Gadget use a VideoBuffers object to manage their video buffers. VideoBuffer is just a
29  vector of VideoBuf objects, with added functions to queue a buffer (send it to the kernel driver, either so that
30  it will be filed with a new camera frame, or so that its contents will be streamed out over the USB link), and to
31  dequeue a buffer (get the next grabbed camera frame, or the next empty USB buffer to be filled by user
32  code).
33 
34  Note that this class provides no threading safety and must be protected externally in case of multithreaded
35  access.
36 
37  VideoBuffers is mainly for internal use by Camera and Gadget. Machine vision programmers should just use Module
38  and the InputFrame and OutputFrame wrappers instead.
39 
40  \ingroup core */
42  {
43  public:
44  //! Construct and allocate MMAP'd video buffers
45  /*! Type is the buffer type, typically V4L2_BUF_TYPE_VIDEO_CAPTURE or V4L2_BUF_TYPE_VIDEO_OUTPUT.
46  \note name is only for debug messages, so we can differentiate camera from USB buffers. */
47  VideoBuffers(char const * name, int const fd, v4l2_buf_type type, size_t const num = 4);
48 
49  //! Free the MMAP'd memory area
50  ~VideoBuffers();
51 
52  //! Get the number of buffers allocated
53  size_t size() const;
54 
55  //! Get the number of buffers queued, this is always in [0 .. size()[
56  size_t nqueued() const;
57 
58  //! Get one buffer, by index [0 .. size()[
59  std::shared_ptr<VideoBuf> get(size_t const index) const;
60 
61  //! Queue one buffer to V4L2, by index [0 .. size()[
62  /*! Beware that this throws if used on an output device as we would not know what value to use for bytesused (how
63  much data there is in the buffer; this is necessary to support streaming out MJPG images whose number of bytes
64  used is variable). Use qbuf(v4l2_buffer) instead on those, with typically a buffer obtained from dqbuf(), and
65  the number of bytes used set in the bytesused field on that buffer when it contains an MJPG image. */
66  void qbuf(size_t const index);
67 
68  //! Queue one buffer to V4L2, by v4l2_buffer
69  /*! The caller is responsible for setting all the fields in the v4l2_buf, including an index that corresponds to a
70  valid previously requested and not already queued buffer. Typically, this is used by output devices which
71  first get the buffer via dqbuf(), then stuff the pixel data into it, and then send it back to qbuf(). */
72  void qbuf(struct v4l2_buffer & buf);
73 
74  //! Queue all buffers, typically used when starting streaming on capture devices
75  void qbufall();
76 
77  //! Dequeue the next captured/displayed buffer, blocks until one is available
78  void dqbuf(struct v4l2_buffer & buf);
79 
80  //! Dequeue all buffers, typically used when stopping a stream, not that this may take some time
81  void dqbufall();
82 
83  private:
84  int const itsFd;
85  std::string const itsName;
86  v4l2_buf_type const itsType;
87  std::vector<std::shared_ptr<VideoBuf> > itsBuffers;
88  size_t itsNqueued;
89  };
90 
91 } // namespace jevois
92 
void dqbuf(struct v4l2_buffer &buf)
Dequeue the next captured/displayed buffer, blocks until one is available.
Definition: VideoBuffers.C:133
VideoBuffers(char const *name, int const fd, v4l2_buf_type type, size_t const num=4)
Construct and allocate MMAP'd video buffers.
Definition: VideoBuffers.C:30
void dqbufall()
Dequeue all buffers, typically used when stopping a stream, not that this may take some time...
Definition: VideoBuffers.C:147
size_t nqueued() const
Get the number of buffers queued, this is always in [0 .. size()[.
Definition: VideoBuffers.C:86
Collection of buffers for V4L2 video frames (Camera or Gadget) with hooks to the MMAP'd areas...
Definition: VideoBuffers.H:41
size_t size() const
Get the number of buffers allocated.
Definition: VideoBuffers.C:80
void qbuf(size_t const index)
Queue one buffer to V4L2, by index [0 .. size()[.
Definition: VideoBuffers.C:100
~VideoBuffers()
Free the MMAP'd memory area.
Definition: VideoBuffers.C:56
void qbufall()
Queue all buffers, typically used when starting streaming on capture devices.
Definition: VideoBuffers.C:127