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#include <sys/ioctl.h>
26
27#include <cstdlib>
28#include <cstring>
29#include <fcntl.h>
30
31#ifdef HAVE_V4L1_CAM
32# include <fvcams/v4l1.h>
33# include <linux/videodev.h>
34#endif
35
36#ifdef HAVE_V4L2_CAM
37# include <fvcams/v4l2.h>
38# include <linux/videodev2.h>
39#endif
40
41#include <core/exception.h>
42#include <core/exceptions/software.h>
43#include <fvutils/system/camargp.h>
44
45namespace firevision {
46
47/** @class V4LCamera <fvcams/v4l.h>
48 * General Video4Linux camera implementation.
49 * Maintains backwards compatibility.
50 * Chooses on the fly whether v4l1 or v4l2 is needed for a given device.
51 * @author Tobias Kellner
52 */
53
54/** Constructor.
55 * @param device_name device file name (e.g. /dev/video0)
56 */
57V4LCamera::V4LCamera(const char *device_name)
58{
59 _v4l_cam = NULL;
60 _device_name = strdup(device_name);
61}
62
63/** Constructor.
64 * Initialize camera with parameters from camera argument parser.
65 * Supported arguments:
66 * - device=DEV, device file, for example /dev/video0
67 * @param cap camera argument parser
68 */
70{
71 _v4l_cam = NULL;
72 if (cap->has("device"))
73 _device_name = strdup(cap->get("device").c_str());
74 else
75 throw fawkes::MissingParameterException("Missing device for V4lCamera");
76}
77
78/** Destructor. */
80{
81 free(_device_name);
82 if (_v4l_cam)
83 delete _v4l_cam;
84}
85
86void
88{
89 if (_v4l_cam)
90 delete _v4l_cam;
91
92 int dev = ::open(_device_name, O_RDWR);
93 if (dev < 0)
94 throw fawkes::Exception("V4LCam: Could not open device");
95
96#ifdef HAVE_V4L1_CAM
97 struct video_capability caps1;
98#endif
99#ifdef HAVE_V4L2_CAM
100 struct v4l2_capability caps2;
101#endif
102
103#ifdef HAVE_V4L2_CAM
104 if (ioctl(dev, VIDIOC_QUERYCAP, &caps2)) {
105#endif
106#ifdef HAVE_V4L1_CAM
107 if (ioctl(dev, VIDIOCGCAP, &caps1)) {
108#endif
109 throw fawkes::Exception("V4LCam: Device doesn't appear to be a v4l device");
110#ifdef HAVE_V4L1_CAM
111 }
112 _v4l_cam = new V4L1Camera(_device_name, dev);
113#endif
114#ifdef HAVE_V4L2_CAM
115 } else {
116 _v4l_cam = new V4L2Camera(_device_name, dev);
117 }
118#endif
119}
120
121void
123{
124 if (!_v4l_cam)
125 throw fawkes::Exception("V4LCam: Trying to start closed cam!");
126
127 _v4l_cam->start();
128}
129
130void
132{
133 if (!_v4l_cam)
134 throw fawkes::Exception("V4LCam: Trying to stop closed cam!");
135
136 _v4l_cam->stop();
137}
138
139void
141{
142 if (_v4l_cam)
143 _v4l_cam->close();
144}
145
146void
148{
149 if (_v4l_cam)
150 _v4l_cam->flush();
151}
152
153void
155{
156 if (_v4l_cam)
157 _v4l_cam->capture();
158}
159
160void
162{
163 if (_v4l_cam)
164 _v4l_cam->print_info();
165}
166
167bool
169{
170 return (_v4l_cam ? _v4l_cam->ready() : false);
171}
172
173unsigned char *
175{
176 return (_v4l_cam ? _v4l_cam->buffer() : NULL);
177}
178
179unsigned int
181{
182 return (_v4l_cam ? _v4l_cam->buffer_size() : 0);
183}
184
185void
187{
188 if (_v4l_cam)
189 _v4l_cam->dispose_buffer();
190}
191
192unsigned int
194{
195 if (!_v4l_cam)
196 throw fawkes::Exception("V4LCam::pixel_width(): Camera not opened");
197
198 return _v4l_cam->pixel_width();
199}
200
201unsigned int
203{
204 if (!_v4l_cam)
205 throw fawkes::Exception("V4LCam::pixel_height(): Camera not opened");
206
207 return _v4l_cam->pixel_height();
208}
209
210colorspace_t
212{
213 return (_v4l_cam ? _v4l_cam->colorspace() : CS_UNKNOWN);
214}
215
216void
218{
219 if (_v4l_cam)
220 _v4l_cam->set_image_number(n);
221}
222
223} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
Expected parameter is missing.
Definition: software.h:74
Camera argument parser.
Definition: camargp.h:36
bool has(std::string s) const
Check if an parameter was given.
Definition: camargp.cpp:145
std::string get(std::string s) const
Get the value of the given parameter.
Definition: camargp.cpp:156
virtual void stop()=0
Stop image transfer from the camera.
virtual void set_image_number(unsigned int n)=0
Set image number to retrieve.
virtual void close()=0
Close camera.
virtual unsigned int pixel_height()=0
Height of image in pixels.
virtual unsigned int buffer_size()=0
Size of buffer.
virtual void flush()=0
Flush image queue.
virtual void dispose_buffer()=0
Dispose current buffer.
virtual void print_info()=0
Print out camera information.
virtual unsigned int pixel_width()=0
Width of image in pixels.
virtual void capture()=0
Capture an image.
virtual colorspace_t colorspace()=0
Colorspace of returned image.
virtual unsigned char * buffer()=0
Get access to current image buffer.
virtual void start()=0
Start image transfer from the camera.
virtual bool ready()=0
Camera is ready for taking pictures.
Video4Linux 1 camera implementation.
Definition: v4l1.h:36
Video4Linux 2 camera access implementation.
Definition: v4l2.h:43
virtual unsigned int pixel_width()
Width of image in pixels.
Definition: v4l.cpp:193
virtual void set_image_number(unsigned int n)
Set image number to retrieve.
Definition: v4l.cpp:217
virtual void dispose_buffer()
Dispose current buffer.
Definition: v4l.cpp:186
virtual void flush()
Flush image queue.
Definition: v4l.cpp:147
virtual colorspace_t colorspace()
Colorspace of returned image.
Definition: v4l.cpp:211
virtual unsigned int buffer_size()
Size of buffer.
Definition: v4l.cpp:180
virtual void print_info()
Print out camera information.
Definition: v4l.cpp:161
virtual ~V4LCamera()
Destructor.
Definition: v4l.cpp:79
virtual void close()
Close camera.
Definition: v4l.cpp:140
virtual bool ready()
Camera is ready for taking pictures.
Definition: v4l.cpp:168
virtual unsigned char * buffer()
Get access to current image buffer.
Definition: v4l.cpp:174
virtual void start()
Start image transfer from the camera.
Definition: v4l.cpp:122
virtual unsigned int pixel_height()
Height of image in pixels.
Definition: v4l.cpp:202
V4LCamera(const char *device_name="/dev/video0")
Constructor.
Definition: v4l.cpp:57
virtual void open()
Open the camera.
Definition: v4l.cpp:87
virtual void capture()
Capture an image.
Definition: v4l.cpp:154
virtual void stop()
Stop image transfer from the camera.
Definition: v4l.cpp:131