Fawkes API  Fawkes Development Version
v4l.cpp
1 
2 /***************************************************************************
3  * v4l.cpp - General Video4Linux access
4  *
5  * Generated: Sat Jul 5 16:16:16 2008
6  * Copyright 2008 Tobias Kellner
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvcams/v4l.h>
25 
26 #include <cstdlib>
27 #include <cstring>
28 #include <fcntl.h>
29 #include <sys/ioctl.h>
30 
31 #ifdef HAVE_V4L1_CAM
32 #include <linux/videodev.h>
33 #include <fvcams/v4l1.h>
34 #endif
35 
36 #ifdef HAVE_V4L2_CAM
37 #include <linux/videodev2.h>
38 #include <fvcams/v4l2.h>
39 #endif
40 
41 #include <fvutils/system/camargp.h>
42 #include <core/exception.h>
43 #include <core/exceptions/software.h>
44 
45 namespace firevision {
46 #if 0 /* just to make Emacs auto-indent happy */
47 }
48 #endif
49 
50 /** @class V4LCamera <fvcams/v4l.h>
51  * General Video4Linux camera implementation.
52  * Maintains backwards compatibility.
53  * Chooses on the fly whether v4l1 or v4l2 is needed for a given device.
54  * @author Tobias Kellner
55  */
56 
57 /** Constructor.
58  * @param device_name device file name (e.g. /dev/video0)
59  */
60 V4LCamera::V4LCamera(const char *device_name)
61 {
62  _v4l_cam = NULL;
63  _device_name = strdup(device_name);
64 }
65 
66 
67 /** Constructor.
68  * Initialize camera with parameters from camera argument parser.
69  * Supported arguments:
70  * - device=DEV, device file, for example /dev/video0
71  * @param cap camera argument parser
72  */
74 {
75  _v4l_cam = NULL;
76  if (cap->has("device")) _device_name = strdup(cap->get("device").c_str());
77  else throw fawkes::MissingParameterException("Missing device for V4lCamera");
78 }
79 
80 /** Destructor. */
82 {
83  free(_device_name);
84  if (_v4l_cam) delete _v4l_cam;
85 }
86 
87 void
89 {
90  if (_v4l_cam) delete _v4l_cam;
91 
92  int dev = ::open(_device_name, O_RDWR);
93  if (dev < 0) throw fawkes::Exception("V4LCam: Could not open device");
94 
95 #ifdef HAVE_V4L1_CAM
96  struct video_capability caps1;
97 #endif
98 #ifdef HAVE_V4L2_CAM
99  struct v4l2_capability caps2;
100 #endif
101 
102 #ifdef HAVE_V4L2_CAM
103  if (ioctl(dev, VIDIOC_QUERYCAP, &caps2))
104  {
105 #endif
106 #ifdef HAVE_V4L1_CAM
107  if (ioctl(dev, VIDIOCGCAP, &caps1))
108  {
109 #endif
110  throw fawkes::Exception("V4LCam: Device doesn't appear to be a v4l device");
111 #ifdef HAVE_V4L1_CAM
112  }
113  _v4l_cam = new V4L1Camera(_device_name, dev);
114 #endif
115 #ifdef HAVE_V4L2_CAM
116  }
117  else
118  {
119  _v4l_cam = new V4L2Camera(_device_name, dev);
120  }
121 #endif
122 }
123 
124 
125 void
127 {
128  if (!_v4l_cam) throw fawkes::Exception("V4LCam: Trying to start closed cam!");
129 
130  _v4l_cam->start();
131 }
132 
133 void
135 {
136  if (!_v4l_cam) throw fawkes::Exception("V4LCam: Trying to stop closed cam!");
137 
138  _v4l_cam->stop();
139 }
140 
141 void
143 {
144  if (_v4l_cam) _v4l_cam->close();
145 }
146 
147 void
149 {
150  if (_v4l_cam) _v4l_cam->flush();
151 }
152 
153 void
155 {
156  if (_v4l_cam) _v4l_cam->capture();
157 }
158 
159 void
161 {
162  if (_v4l_cam) _v4l_cam->print_info();
163 }
164 
165 bool
167 {
168  return (_v4l_cam ? _v4l_cam->ready() : false);
169 }
170 
171 unsigned char*
173 {
174  return (_v4l_cam ? _v4l_cam->buffer() : NULL);
175 }
176 
177 unsigned int
179 {
180  return (_v4l_cam ? _v4l_cam->buffer_size() : 0);
181 }
182 
183 void
185 {
186  if (_v4l_cam) _v4l_cam->dispose_buffer();
187 }
188 
189 unsigned int
191 {
192  if (!_v4l_cam) throw fawkes::Exception("V4LCam::pixel_width(): Camera not opened");
193 
194  return _v4l_cam->pixel_width();
195 }
196 
197 unsigned int
199 {
200  if (!_v4l_cam) throw fawkes::Exception("V4LCam::pixel_height(): Camera not opened");
201 
202  return _v4l_cam->pixel_height();
203 }
204 
205 colorspace_t
207 {
208  return (_v4l_cam ? _v4l_cam->colorspace() : CS_UNKNOWN);
209 }
210 
211 void
213 {
214  if (_v4l_cam) _v4l_cam->set_image_number(n);
215 }
216 
217 } // end namespace firevision
virtual unsigned int buffer_size()=0
Size of buffer.
virtual void stop()=0
Stop image transfer from the camera.
virtual unsigned int pixel_width()=0
Width of image in pixels.
Video4Linux 2 camera access implementation.
Definition: v4l2.h:46
Camera argument parser.
Definition: camargp.h:38
virtual unsigned int buffer_size()
Size of buffer.
Definition: v4l.cpp:178
virtual unsigned int pixel_width()
Width of image in pixels.
Definition: v4l.cpp:190
virtual void open()
Open the camera.
Definition: v4l.cpp:88
virtual colorspace_t colorspace()=0
Colorspace of returned image.
virtual void dispose_buffer()
Dispose current buffer.
Definition: v4l.cpp:184
virtual void capture()
Capture an image.
Definition: v4l.cpp:154
virtual void print_info()=0
Print out camera information.
virtual bool ready()=0
Camera is ready for taking pictures.
virtual void stop()
Stop image transfer from the camera.
Definition: v4l.cpp:134
V4LCamera(const char *device_name="/dev/video0")
Constructor.
Definition: v4l.cpp:60
virtual colorspace_t colorspace()
Colorspace of returned image.
Definition: v4l.cpp:206
bool has(std::string s) const
Check if an parameter was given.
Definition: camargp.cpp:152
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void set_image_number(unsigned int n)
Set image number to retrieve.
Definition: v4l.cpp:212
virtual void capture()=0
Capture an image.
virtual void set_image_number(unsigned int n)=0
Set image number to retrieve.
virtual void flush()=0
Flush image queue.
virtual ~V4LCamera()
Destructor.
Definition: v4l.cpp:81
Video4Linux 1 camera implementation.
Definition: v4l1.h:37
virtual void print_info()
Print out camera information.
Definition: v4l.cpp:160
virtual unsigned int pixel_height()
Height of image in pixels.
Definition: v4l.cpp:198
virtual void close()
Close camera.
Definition: v4l.cpp:142
virtual void close()=0
Close camera.
virtual unsigned char * buffer()=0
Get access to current image buffer.
virtual void start()
Start image transfer from the camera.
Definition: v4l.cpp:126
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual void start()=0
Start image transfer from the camera.
virtual unsigned char * buffer()
Get access to current image buffer.
Definition: v4l.cpp:172
std::string get(std::string s) const
Get the value of the given parameter.
Definition: camargp.cpp:164
Expected parameter is missing.
Definition: software.h:76
virtual bool ready()
Camera is ready for taking pictures.
Definition: v4l.cpp:166
virtual void flush()
Flush image queue.
Definition: v4l.cpp:148
virtual void dispose_buffer()=0
Dispose current buffer.