00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __OPENKN_CONTROLLER__LIN_V4L2WEBCAM_HPP__
00027 #define __OPENKN_CONTROLLER__LIN_V4L2WEBCAM_HPP__
00028
00029
00030
00031
00032
00033 #include "CameraController.hpp"
00034 #include "webcamstools/uvcvideo.h"
00035
00036
00037
00038
00039 #include <linux/videodev2.h>
00040
00041
00042
00043
00044 namespace kn{
00045
00046 enum WebcamControl { WEBCAM_BRIGHTNESS = V4L2_CID_BRIGHTNESS,
00047 WEBCAM_CONTRAST = V4L2_CID_CONTRAST,
00048 WEBCAM_SATURATION = V4L2_CID_SATURATION,
00049 WEBCAM_GAIN = V4L2_CID_GAIN,
00050 WEBCAM_SHARPNESS = V4L2_CID_SHARPNESS,
00051 WEBCAM_GAMMA = V4L2_CID_GAMMA,
00052 WEBCAM_EXPOSURE = V4L2_CID_EXPOSURE_ABSOLUTE,
00053 WEBCAM_EXPOSURE_AUTO = V4L2_CID_EXPOSURE_AUTO,
00054 WEBCAM_WB_AUTO = V4L2_CID_WHITE_BALANCE_TEMPERATURE_AUTO,
00055 WEBCAM_WB = V4L2_CID_WHITE_BALANCE_TEMPERATURE};
00056
00057 enum WebcamToggleControl {WEBCAM_EXPOSURE_ON = 3, WEBCAM_EXPOSURE_OFF = 2, WEBCAM_WB_ON = 1, WEBCAM_WB_OFF = 0};
00058
00062 enum WebcamFormat{FMT_MJPEG,FMT_YUYV};
00063
00067 enum GrabMethod { READ_METHOD, MMAP_METHOD};
00068
00069
00076 struct V4L2WebcamParams : CameraParams
00077 {
00078
00082 WebcamFormat fmt;
00083
00087 GrabMethod grab;
00088
00092 unsigned int framerate;
00093
00097 bool force;
00098
00102 V4L2WebcamParams(const std::string& dn = "/dev/video0",
00103 const size_t& expectedwidth = 320,
00104 const size_t& expectedheight = 240,
00105 const WebcamFormat& wf = FMT_YUYV,
00106 const GrabMethod& gm = MMAP_METHOD,
00107 const unsigned int& fps = 30,
00108 const bool& f = false)
00109 :CameraParams(dn,expectedwidth,expectedheight),fmt(wf),grab(gm),framerate(fps),force(f) {}
00110
00111 };
00112
00113 static const V4L2WebcamParams defaultV4L2WebcamParams;
00114
00115
00116
00117
00118
00142 class V4L2Webcam : public CameraController {
00143
00144 private:
00145
00147
00148 struct V4L2_Buffer {
00149 void * start;
00150 size_t length;
00151 };
00152
00154
00158 struct v4l2_capability cap;
00159
00163 struct v4l2_format fmt;
00164
00168 struct v4l2_buffer buf;
00169
00173 struct v4l2_requestbuffers rb;
00174
00178 GrabMethod gb;
00179
00183 int filedescriptor;
00184
00185
00190 void** buffersmmap;
00191
00196 V4L2_Buffer* buffersread;
00197
00198
00202 unsigned char *tmpbuffer;
00203
00208 unsigned char *framebuffer;
00209
00213 unsigned char *outputbuffer;
00214
00218 bool isgrabbing;
00219
00223 bool isenable;
00224
00228 struct v4l2_control control_s;
00229
00233 struct v4l2_queryctrl queryctrl;
00234
00235 public:
00236
00244 V4L2Webcam(void):CameraController(){
00245 outputbuffer = 0;
00246 framebuffer = 0;
00247 tmpbuffer = 0;
00248 buffersread = 0;
00249 buffersmmap = 0;
00250 isenable = false;
00251 isgrabbing = false;
00252 filedescriptor = -1;
00253 };
00254
00262 ~V4L2Webcam(void){closeDevice();};
00263
00264 private:
00271 bool checkAvailability(std::string& message);
00272
00278 void initMMAP(void);
00279
00285 void initREAD(void);
00286
00296 bool isControlSupportedQuery(const WebcamControl& control, struct v4l2_queryctrl* queryctrl);
00297
00298
00299 public:
00300
00310 void openDevice(const V4L2WebcamParams& params);
00311
00312 void openDevice(void){openDevice(V4L2WebcamParams());}
00313
00321 void closeDevice(void);
00322
00330 void start(void);
00331
00335 void stop(void);
00336
00342 bool isStarted(void) const {return isgrabbing;}
00343
00349 bool isOpened(void) const {return isenable;}
00350
00363 unsigned char* getImage(void);
00364
00372 size_t width(void) const;
00373
00381 size_t height(void) const;
00382
00391 bool isControlSupported(const WebcamControl& control);
00392
00401 int getControlValue(const WebcamControl& control);
00402
00411 bool resetControlValue(const WebcamControl& control);
00412
00421 int getControlMinValue(const WebcamControl& control);
00422
00431 int getControlMaxValue(const WebcamControl& control);
00432
00441 int getControlStepValue(const WebcamControl& control);
00442
00451 bool setControlUpValue(const WebcamControl& control);
00452
00461 bool setControlDownValue(const WebcamControl& control);
00462
00472 bool setControlValue(const WebcamControl& control, const int& value);
00473
00483 bool toggleControlValue(const WebcamControl& control, const WebcamToggleControl& value);
00484 };
00485
00486
00487
00488
00489 }
00490
00491 #endif