core/vidl/vidl_v4l_istream.h
Go to the documentation of this file.
00001 // This is core/vidl/vidl_v4l_istream.h
00002 #ifndef vidl_v4l_istream_h_
00003 #define vidl_v4l_istream_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A class for input video streams from a video-4-linux device
00010 //
00011 // \author Paul Crane and Brendan McCane
00012 // \date 21 Feb 2006
00013 
00014 #include "vidl_istream.h"
00015 #include <vcl_string.h> // this is for the strings scattered about the place
00016 
00017 // this is linux specific
00018 using namespace std;
00019 
00020 extern "C" {
00021 #include <linux/videodev.h> // this is the video for linux stuff
00022 
00023 #include <sys/ioctl.h> // this is to communicate with the device
00024 #include <sys/mman.h>  // for mmap
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 
00028 #include <unistd.h> // for reading/writing to the camera
00029 #include <errno.h> // for errno
00030 #include <fcntl.h> // this is for the open, and O_RDWR
00031 #include <pthread.h> // for threaded approach to the asynch capture
00032 };
00033 
00034 #include "vidl_v4l_params.h"
00035 
00036 class vidl_v4l_istream:public vidl_istream
00037 {
00038   public:
00039     // Constructor
00040     vidl_v4l_istream():buf(NULL)
00041     {
00042         open("/dev/video0");
00043     }
00044 
00045     vidl_v4l_istream(const vcl_string &device_name):buf(NULL)
00046     {
00047         open(device_name);
00048     }
00049 
00050     vidl_v4l_istream(const vcl_string &device_name, const vidl_v4l_params p)
00051         :buf(NULL)
00052     {
00053         open(device_name);
00054         set_params(p);
00055     }
00056 
00057     // Destructor
00058     virtual ~vidl_v4l_istream();
00059 
00060     //: Return true if the stream is open for reading
00061     virtual bool is_open() const;
00062 
00063     //: Return true if the stream is in a valid state
00064     virtual bool is_valid() const;
00065 
00066     //: Return true if the stream support seeking
00067     virtual bool is_seekable() const {return false;}
00068 
00069     //: Return the number of frames if known
00070     //  returns -1 for non-seekable streams
00071     virtual int num_frames() const { return -1; }
00072 
00073     //: Return the width of each frame
00074     virtual unsigned int width() const;
00075 
00076     //: Return the height of each frame
00077     virtual unsigned int height() const;
00078 
00079     //: Return the pixel format
00080     virtual vidl_pixel_format format() const;
00081 
00082     //: Return the frame rate (FPS, 0.0 if unspecified)
00083     virtual double frame_rate() const;
00084 
00085     //: Return the duration in seconds (0.0 if unknown)
00086     virtual double duration() const { return 0.0; }
00087 
00088     //: Return the current frame number
00089     virtual unsigned int frame_number() const { return frame_number_; }
00090 
00091     //: Open
00092     bool open(const vcl_string &device_name);
00093 
00094     //: Close the stream
00095     virtual void close();
00096 
00097     //: set the params for the device
00098     bool set_params(const vidl_v4l_params &p);
00099     vidl_v4l_params get_params() { return params_; }
00100 
00101     //: Advance to the next frame (but don't acquire an image)
00102     virtual bool advance();
00103 
00104     //: Read the next frame from the stream (advance and acquire)
00105     virtual vidl_frame_sptr read_frame();
00106 
00107     //: Return the current frame in the stream
00108     virtual vidl_frame_sptr current_frame() { return cur_frame_; }
00109 
00110     //: Seek to the given frame number
00111     // \returns true if successful
00112     virtual bool seek_frame(unsigned int /*frame_nr*/) { return false; }
00113   private:
00114     struct video_capability vc;
00115     struct video_window vw;
00116     struct video_picture vp;
00117     struct video_mbuf vm;
00118     struct video_mmap mm;
00119     mutable vidl_frame_sptr cur_frame_;
00120     vidl_v4l_params defaults_;
00121     vidl_v4l_params params_;
00122     int fd_;
00123     unsigned int frame_number_;
00124     void *buf;
00125 };
00126 
00127 #endif