CameraDriverV4L2.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_CameraDriverV4L2_h_
00003 #define INCLUDED_CameraDriverV4L2_h_
00004
00005 #include "local/DeviceDriver.h"
00006 #include "local/DataSource.h"
00007 #include "Shared/get_time.h"
00008 #include "IPC/CallbackThread.h"
00009
00010 #include <linux/types.h>
00011 #include <linux/videodev2.h>
00012
00013 #include <map>
00014 #include <vector>
00015
00016 #define NUM_BUFFERS 4
00017 #define MIN_BUFFERS 1
00018
00019
00020 class CameraDriverV4L2 : public virtual DeviceDriver, public virtual plist::PrimitiveListener, public virtual plist::CollectionListener, public DataSource
00021 {
00022 public:
00023 explicit CameraDriverV4L2(const std::string& name)
00024 : DeviceDriver(autoRegisterCameraDriverV4L2,name), DataSource(),
00025 path("/dev/video0"), resolution("640x480"),queryOptions(false),userOptions(),options(),
00026 thread(&CameraDriverV4L2::threadrun,*this),
00027 camfd(-1), v_cap(), v_fmt(), v_buf(), v_mem(0),
00028 streaming(false), downsample(true), frameCount(0), timestamp(0),
00029 captureBuf(), lock()
00030 {
00031 addEntry("Path",path,"Path to the video device, e.g. /dev/video0");
00032 addEntry("Resolution",resolution,"Image resolution of the final output image, e.g. 640x480."
00033 "If small enough, image will be captured at twice this resolution and downsampled,"
00034 "otherwise image will be captured at this resolution and upsampled.");
00035 addEntry("QueryOptions",queryOptions,"Query the options available for this camera.");
00036 addEntry("Options",userOptions,"Add entries to this collection to control camera-specific settings.\nSet QueryOptions to true to see list of available options.");
00037 userOptions.setLoadSavePolicy(SYNC,SYNC);
00038 }
00039
00040 ~CameraDriverV4L2() { deregisterSource(); }
00041
00042 virtual std::string getClassName() const { return autoRegisterCameraDriverV4L2; }
00043
00044 virtual void getImageSources(std::map<std::string,DataSource*>& sources) {
00045 sources.clear(); sources.insert(std::make_pair("Camera",this));
00046 }
00047
00048 virtual unsigned int nextTimestamp() { return get_time(); }
00049 virtual const std::string& nextName() { return instanceName; }
00050
00051 virtual bool advance();
00052 virtual void registerSource();
00053 virtual void deregisterSource();
00054
00055
00056 virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00057
00058
00059 virtual void plistCollectionEntryAdded(plist::Collection& col, plist::ObjectBase& obj);
00060
00061 virtual void plistCollectionEntryRemoved(plist::Collection& col, plist::ObjectBase& obj);
00062
00063 virtual void plistCollectionEntriesChanged(plist::Collection& col);
00064
00065 plist::Primitive<std::string> path;
00066 plist::Primitive<std::string> resolution;
00067 plist::Primitive<bool> queryOptions;
00068 plist::DictionaryOf< plist::Primitive<int> > userOptions;
00069 std::map<std::string, struct v4l2_queryctrl> options;
00070
00071 protected:
00072 void close_cam();
00073 void open_cam();
00074 bool setControl(const std::string& name, int value, bool verbose=false);
00075
00076 int video_enable();
00077 int video_disable();
00078 int select_format();
00079 int add_control(struct v4l2_queryctrl & queryctrl, bool verbose);
00080 int query_options(bool verbose);
00081 int dequeue_buffer();
00082 std::string v4l2_fourcc_inv(__u32 f);
00083
00084 void doFreeze();
00085 void doUnfreeze();
00086
00087 void downsample_yuyv();
00088 void upsample_yuyv();
00089
00090 void threadrun();
00091 CallbackThread thread;
00092
00093 int camfd;
00094
00095 struct v4l2_capability v_cap;
00096 struct v4l2_format v_fmt;
00097 struct v4l2_buffer v_buf;
00098
00099 struct MemoryMapping {
00100 void *start;
00101 size_t length;
00102 };
00103
00104
00105 std::vector<MemoryMapping> v_mem;
00106
00107 bool streaming;
00108 bool downsample;
00109
00110 unsigned int frameCount;
00111 unsigned int timestamp;
00112
00113 std::vector<unsigned char> captureBuf;
00114 Thread::Lock lock;
00115
00116 private:
00117
00118 static const std::string autoRegisterCameraDriverV4L2;
00119 };
00120
00121
00122
00123
00124
00125
00126
00127 #endif