v4l.cpp

00001
00002 /***************************************************************************
00003  *  v4l.cpp - General Video4Linux access
00004  *
00005  *  Generated: Sat Jul  5 16:16:16 2008
00006  *  Copyright  2008  Tobias Kellner
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <cams/v4l.h>
00025
00026 #include <cstdlib>
00027 #include <cstring>
00028 #include <fcntl.h>
00029 #include <sys/ioctl.h>
00030
00031 #ifdef HAVE_V4L1_CAM
00032 #include <linux/videodev.h>
00033 #include <cams/v4l1.h>
00034 #endif
00035 
00036 #ifdef HAVE_V4L2_CAM
00037 #include <linux/videodev2.h>
00038 #include <cams/v4l2.h>
00039 #endif
00040 
00041 #include <fvutils/system/camargp.h>
00042 #include <core/exception.h>
00043 #include <core/exceptions/software.h>
00044 
00045 /** @class V4LCamera <cams/v4l.h>
00046  * General Video4Linux camera implementation.
00047  * Maintains backwards compatibility.
00048  * Chooses on the fly whether v4l1 or v4l2 is needed for a given device.
00049  * @author Tobias Kellner
00050  */
00051 
00052 /** Constructor.
00053  * @param device_name device file name (e.g. /dev/video0)
00054  */
00055 V4LCamera::V4LCamera(const char *device_name)
00056 {
00057   _v4l_cam = NULL;
00058   _device_name = strdup(device_name);
00059 }
00060
00061 
00062 /** Constructor.
00063  * Initialize camera with parameters from camera argument parser.
00064  * Supported arguments:
00065  * - device=DEV, device file, for example /dev/video0
00066  * @param cap camera argument parser
00067  */
00068 V4LCamera::V4LCamera(const CameraArgumentParser *cap)
00069 {
00070   _v4l_cam = NULL;
00071   if (cap->has("device")) _device_name = strdup(cap->get("device").c_str());
00072   else throw fawkes::MissingParameterException("Missing device for V4lCamera");
00073 }
00074 
00075 /** Destructor. */
00076 V4LCamera::~V4LCamera()
00077 {
00078   free(_device_name);
00079   if (_v4l_cam) delete _v4l_cam;
00080 }
00081
00082 void
00083 V4LCamera::open()
00084 {
00085   if (_v4l_cam) delete _v4l_cam;
00086
00087   int dev = ::open(_device_name, O_RDWR);
00088   if (dev < 0) throw fawkes::Exception("V4LCam: Could not open device");
00089
00090 #ifdef HAVE_V4L1_CAM
00091   struct video_capability caps1;
00092 #endif
00093 #ifdef HAVE_V4L2_CAM
00094   struct v4l2_capability caps2;
00095 #endif
00096 
00097 #ifdef HAVE_V4L2_CAM
00098   if (ioctl(dev, VIDIOC_QUERYCAP, &caps2))
00099   {
00100 #endif
00101 #ifdef HAVE_V4L1_CAM
00102     if (ioctl(dev, VIDIOCGCAP, &caps1))
00103     {
00104 #endif
00105       throw fawkes::Exception("V4LCam: Device doesn't appear to be a v4l device");
00106 #ifdef HAVE_V4L1_CAM
00107     }
00108     _v4l_cam = new V4L1Camera(_device_name, dev);
00109 #endif
00110 #ifdef HAVE_V4L2_CAM
00111   }
00112   else
00113   {
00114     _v4l_cam = new V4L2Camera(_device_name, dev);
00115   }
00116 #endif
00117 }
00118
00119
00120 void
00121 V4LCamera::start()
00122 {
00123   if (!_v4l_cam) throw fawkes::Exception("V4LCam: Trying to start closed cam!");
00124
00125   _v4l_cam->start();
00126 }
00127
00128 void
00129 V4LCamera::stop()
00130 {
00131   if (!_v4l_cam) throw fawkes::Exception("V4LCam: Trying to stop closed cam!");
00132
00133   _v4l_cam->stop();
00134 }
00135
00136 void
00137 V4LCamera::close()
00138 {
00139   if (_v4l_cam) _v4l_cam->close();
00140 }
00141
00142 void
00143 V4LCamera::flush()
00144 {
00145   if (_v4l_cam) _v4l_cam->flush();
00146 }
00147
00148 void
00149 V4LCamera::capture()
00150 {
00151   if (_v4l_cam) _v4l_cam->capture();
00152 }
00153
00154 void
00155 V4LCamera::print_info()
00156 {
00157   if (_v4l_cam) _v4l_cam->print_info();
00158 }
00159
00160 bool
00161 V4LCamera::ready()
00162 {
00163   return (_v4l_cam ? _v4l_cam->ready() : false);
00164 }
00165
00166 unsigned char*
00167 V4LCamera::buffer()
00168 {
00169   return (_v4l_cam ? _v4l_cam->buffer() : NULL);
00170 }
00171
00172 unsigned int
00173 V4LCamera::buffer_size()
00174 {
00175   return (_v4l_cam ? _v4l_cam->buffer_size() : 0);
00176 }
00177
00178 void
00179 V4LCamera::dispose_buffer()
00180 {
00181   if (_v4l_cam) _v4l_cam->dispose_buffer();
00182 }
00183
00184 unsigned int
00185 V4LCamera::pixel_width()
00186 {
00187   if (!_v4l_cam) throw fawkes::Exception("V4LCam::pixel_width(): Camera not opened");
00188
00189   return _v4l_cam->pixel_width();
00190 }
00191
00192 unsigned int
00193 V4LCamera::pixel_height()
00194 {
00195   if (!_v4l_cam) throw fawkes::Exception("V4LCam::pixel_height(): Camera not opened");
00196
00197   return _v4l_cam->pixel_height();
00198 }
00199
00200 colorspace_t
00201 V4LCamera::colorspace()
00202 {
00203   return (_v4l_cam ? _v4l_cam->colorspace() : CS_UNKNOWN);
00204 }
00205
00206 void
00207 V4LCamera::set_image_number(unsigned int n)
00208 {
00209   if (_v4l_cam) _v4l_cam->set_image_number(n);
00210 }