Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_V4L.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __BIASVIDEOSOURCE_V4L_HH__
26 #define __BIASVIDEOSOURCE_V4L_HH__
27 
28 #include <bias_config.h>
29 
30 #ifndef BIAS_HAVE_V4L
31 # error Please recompile BIAS with BUILD_V4L to use VideoSource V4L.
32 #endif
33 #ifndef BIAS_HAVE_PTHREADS
34 # error BIAS_HAVE_PTHREADS not defined. But BIAS::VideoSource_V4L needs pthreads (on Unix and Win32). Please enable USE_PTHREADS in CMake configure step.
35 #endif
36 
37 #include <VideoSource/VideoSource_Base.hh>
38 
39 #ifdef BIAS_HAVE_LIBJPEG
40 # include <Base/Image/CompressJpeg.hh>
41 # include <Base/Image/ImageConvert.hh>
42 # include <Image/Camera.hh>
43 #endif
44 
45 #include <linux/videodev2.h>
46 #include <pthread.h>
47 #include <sys/time.h>
48 
49 
50 namespace BIAS
51 {
52 /** \class VideoSource_V4L
53  \ingroup g_videosource
54  \author evers, v4l2 by grest
55  \see ExampleV4L.cpp
56  \brief This class extends VideoSource for the use of Video4Linux
57  supported devices like framegrabber, USB cameras.
58 */
60  {
61 
62  public:
64  virtual ~VideoSource_V4L();
65 
66  int GetAllDevices(std::vector<std::string> &devices);
67 
69  int GetCapabilities(const char *device, VideoSourceCapabilities &caps);
70 
71 
72  /**\brief if more than one channel is present names can are returned here.
73  * the position in the vector corresponds to the channel number in SetChannel()
74  * \attention GetCapabilities() has to be called before */
75  void GetDeviceChannelNames(std::vector<std::string> &names);
76 
77  virtual int OpenDevice();
78  virtual int OpenDevice(const char *device);
79  virtual int OpenDevice(int device);
80  virtual int CloseDevice();
81 
82  /** \brief blocks until a new frame arrives from the cam,
83  if dont want blocking set waitForNew() to false
84  \param image[out]: the grabbed image*/
85  virtual int GrabSingle(BIAS::Camera <unsigned char> &image);
86 
87  int PreGrab();
88  int PostGrab();
89 
90  float GetGain();
91  int SetGain(float g);
92 
93  bool HasControlBrightness();
94  /* Value: 0<=g<=100 */
95  float GetBrightness();
96  /* Value: 0<=g<=100 */
97  int SetBrightness(float g);
98 
99  bool HasControlContrast();
100  /* Value: 0<=g<=100 */
101  float GetContrast();
102  /* Value: 0<=g<=100 */
103  int SetContrast(float g);
104 
105  bool HasControlShutter();
106  /* Value: 0<=g<=1.0 */
107  float GetShutter();
108  /* Value: 0<=g<=1.0 */
109  int SetShutter(float g);
110  void SetAutoShutter(bool b);
111  bool GetAutoShutter();
112 
113  bool HasControlGain();
114  void SetAutoGain(bool b);
115  bool GetAutoGain();
116 
117  bool HasControlWhiteBalance(){return false;};
118  void SetAutoBrightness(bool b){};
119  bool GetAutoBrightness(){return false;};
120 
121  /// Reset digital zoom, pan and tilt to defaults to ensure
122  /// compliance with calibration.
123  void ResetPanTiltZoom();
124 
125  //////////////////////////////////////////////////////////
126  protected:
127 
128 #ifdef BIAS_HAVE_LIBJPEG
131 #endif //BIAS_HAVE_LIBJPEG
132 
133  int fd;
134 
135  struct v4l2_requestbuffers reqBuf;
136  struct v4l2_capability capability2;
137  struct v4l2_format format2_;
138  struct v4l2_format formatRGB_;
139  struct v4l2_buffer buffer_;
140  unsigned int numBuffers_;
141 
142  struct v4l2_queryctrl ControlBrightness_;
143  struct v4l2_queryctrl ControlContrast_;
144  struct v4l2_queryctrl ControlGain_;
145  struct v4l2_queryctrl ControlShutter_;
146  struct v4l2_queryctrl ControlAutoGain_;
147  struct v4l2_queryctrl ControlZoom_;
148  struct v4l2_queryctrl ControlPanReset_;
149  struct v4l2_queryctrl ControlTiltReset_;
150 
151  bool newFrame_;
152  bool isJPEG_;
153  /**\cond HIDDEN_SYMBOLS */
154  struct buffer {
155  void * start;
156  size_t length;
157  };
158  /** \endcond */
159  std::vector<struct buffer> Buffers_;
160  unsigned ActiveFrame_;
162  std::vector<BIAS::Camera<unsigned char> > Frame_;
163 
164 
165  pthread_t ContinousThread_;
166  pthread_mutex_t ActiveFrameMutex_;
167 
168  struct timeval GrabTime_;
169 
170  static void *GrabContThread_(void * arg);
171  void FixIdentifierString_();
172 
173  std::vector<std::string> deviceChannelNames_;
174  std::map<std::string , std::string> ModelNameToDevice_;
175  v4l2_field interlaceMode_;
176 
177  private:
178  int xioctl_(int fd, int request, void *arg);
179  // initializes Buffers_ using mmap
180  int InitMMap_();
181  // initialses the device
182  int InitDevice_();
183  // queues all buffers ans starts the streaming by calling STREAMON
184  int StartCapturing_();
185  // stops streaming by calling STREAMOFF
186  int StopCapturing_();
187  // calls uninit
188  int UninitMMap_();
189  // alternately fills Frame_[0] and Frame_[1]
190  int ReadFrame_();
191  };
192 
193 } // namespace MIP
194 
195 
196 #endif
Wrapper for fast libjpeg methods.
Definition: CompressJpeg.hh:50
float GetBrightness()
Get brightness as value in interval [0, 100]. */.
float GetContrast()
Get contrast as value in interval [0, 100]. */.
pthread_mutex_t ActiveFrameMutex_
Defines a common interface to different devices.
void GetDeviceChannelNames(std::vector< std::string > &names)
if more than one channel is present names can are returned here.
struct v4l2_buffer buffer_
struct v4l2_requestbuffers reqBuf
struct timeval GrabTime_
int SetShutter(float g)
Set shutter (exposure time) in seconds.
std::vector< BIAS::Camera< unsigned char > > Frame_
struct v4l2_format formatRGB_
struct v4l2_queryctrl ControlTiltReset_
void ResetPanTiltZoom()
Reset digital zoom, pan and tilt to defaults to ensure compliance with calibration.
float GetShutter()
Get shutter (exposure time) in seconds.
virtual int OpenDevice()
selects the first available device to open (e.g.
float GetGain()
Get gain in dB.
void SetAutoBrightness(bool b)
struct v4l2_queryctrl ControlContrast_
struct v4l2_queryctrl ControlShutter_
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
struct v4l2_queryctrl ControlAutoGain_
virtual int GrabSingle(BIAS::Camera< unsigned char > &image)
blocks until a new frame arrives from the cam, if dont want blocking set waitForNew() to false ...
int SetBrightness(float g)
Set brightness as value in interval [0, 100]. */.
int SetGain(float g)
Set gain in dB.
struct v4l2_format format2_
int GetAllDevices(std::vector< std::string > &devices)
BIAS::JpegHandler Decompressor_
std::map< std::string, std::string > ModelNameToDevice_
struct v4l2_queryctrl ControlZoom_
struct v4l2_queryctrl ControlBrightness_
struct v4l2_queryctrl ControlGain_
int SetContrast(float g)
Set contrast as value in interval [0, 100]. */.
BIAS::Camera< unsigned char > jpegImg_
std::vector< std::string > deviceChannelNames_
Checks for VideoSource capabilities.
struct v4l2_queryctrl ControlPanReset_
struct v4l2_capability capability2
This class extends VideoSource for the use of Video4Linux supported devices like framegrabber, USB cameras.
int PostGrab()
Stop anything started in PreGrab()
int GetCapabilities(VideoSourceCapabilities &caps)
Use this method to learn something about the capabilities of the source (only useful for V4L sources...
std::vector< struct buffer > Buffers_
static void * GrabContThread_(void *arg)