iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
V4L2ImageSource.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies <voorhies@usc.edu>
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 #include <nrt/config.h>
37 #ifdef NRT_HAVE_V4L2
38 
39 #ifndef INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_V4L2IMAGESOURCE_H
40 #define INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_V4L2IMAGESOURCE_H
41 
45 #include <linux/videodev2.h>
46 
47 namespace nrt
48 {
49  namespace v4l2
50  {
51 
52  NRT_DECLARE_PARAMETER_WITH_CALLBACK(device, std::string, "Device file (in /dev) to open", "/dev/video0",
54 
55  NRT_DECLARE_PARAMETER(buffers, int, "Number of device buffers to request", 2,
57 
58  NRT_DECLARE_PARAMETER(channel, int, "Input channel (typically 0=TV, 1=Composite", 1,
60 
61  NRT_DECLARE_PARAMETER(dims, Dims<int32>, "Requested frame dimensions", Dims<int32>(320,240),
63 
64  NRT_DECLARE_PARAMETER(streaming, bool,
65  "Turn streaming mode on/off. Compared with single-frame mode, "
66  "streaming mode gives higher frame rates, but also requires that "
67  "the application be able to keep up with the frame rate; otherwise "
68  "the grabber will start to lose frames. In general, if the program "
69  "can operate at a consistently high frame rate, then streaming is "
70  "the way to go, but if the program requires lengthy per-frame "
71  "processing, or if the processing time varies widely from one "
72  "frame to the next, then single-frame mode is the way to go.",
74  }
75 
76  class V4L2ImageSource : public ImageSourceType,
77  public Parameter<v4l2::device, v4l2::buffers, v4l2::channel, v4l2::dims, v4l2::streaming>
78  {
79  struct ParamHelper : nrt::Component
80  {
81  ParamHelper(std::string const & instanceName);
82  void addControl(int device, v4l2_queryctrl ctrl);
83  std::map<unsigned int, ParameterDefBase const *> itsControlMap;
84  std::map<std::string, std::shared_ptr<ParameterBase> > itsControls;
85  };
86 
87  public:
88  V4L2ImageSource(std::string instanceID = "");
89 
90  ~V4L2ImageSource() {};
91 
92  void openDevice(std::string const & dev);
93 
94  void closeDevice();
95 
96  void preStart();
97  void postStop();
98 
99  //! Checks whether there is a new image available from in()
100  const bool ok() const { return itsOk; };
101 
102  //! Returns the latest image from the V4L2 device
103  GenericImage const in();
104 
105  //! Get the name associated with our --in type
106  static std::string const id() { return "V4L2|v4l2"; }
107 
108  //! Get a description to show for --help
109  static std::string const description()
110  { return "Grabs the images from a V4L2 camera"; }
111 
112  private:
113  std::shared_ptr<ParamHelper> itsParamHelper;
114 
115  //! Whether there is a new image
116  bool itsOk;
117 
118  int itsCurrentFrame;
119 
120  int itsFd;
121  byte **itsMmapBuf;
122  int *itsMmapBufSize;
123  Image<PixGray<byte>> itsReadBuf;
124  bool* itsGrabbing;
125 
126  bool itsStreamStarted;
127  bool itsCanRW;
128  bool itsCanMMap;
129 
130  void onParamChange(nrt::v4l2::device const & param, std::string const & dev);
131 
132  void addControl(const struct v4l2_queryctrl& ctrl);
133  GenericImage grabRaw();
134  GenericImage grabSingleRaw();
135  void restartStream();
136  };
137 }
138 #endif // INCLUDE_NRT_IMAGEPROC_IO_IMAGESOURCE_V4L2IMAGESOURCE_H
139 #endif // NRT_HAVE_V4L2