libcvd
uvcbuffer.h
1 //-*- c++ -*-
2 #ifndef CVD_VIDEO_UVCBUFFER_H
3 #define CVD_VIDEO_UVCBUFFER_H
4 
5 #include <cvd/byte.h>
6 #include <cvd/colourspaces.h>
7 #include <cvd/localvideoframe.h>
8 #include <cvd/rgb.h>
9 #include <cvd/videobuffer.h>
10 #include <libuvc/libuvc.h>
11 
12 namespace CVD
13 {
14 
15 namespace Exceptions
16 {
18  namespace UVCBuffer
19  {
22  {
23  using CVD::Exceptions::VideoBuffer::All::All;
24  };
25 
28  struct Init : public All
29  {
30  Init(std::string dev, std::string err);
31  };
32 
35  struct DeviceOpen : public All
36  {
37  DeviceOpen(std::string dev, std::string err);
38  };
39 
42  struct StreamOpen : public All
43  {
44  StreamOpen(std::string dev, std::string err);
45  };
46 
49  struct StreamStart : public All
50  {
51  StreamStart(std::string dev, std::string err);
52  };
53 
56  struct FindDevice : public All
57  {
58  FindDevice(std::string dev, std::string err);
59  };
60 
63  struct DeviceSetup : public All
64  {
65  DeviceSetup(std::string dev, std::string action);
66  };
69  struct PutFrame : public All
70  {
71  PutFrame(std::string dev, std::string msg);
72  };
75  struct GetFrame : public All
76  {
77  GetFrame(std::string dev, std::string msg);
78  };
79  }
80 }
81 
82 namespace UVC
83 {
84 #ifndef DOXYGEN_IGNORE_INTERNAL
85  template <class C>
86  struct format;
87 
88  //libUVC only supports a rather small subset of the possible
89  //types, but these seem to be the only common ones anyway.
90 
91  template <>
92  struct format<yuv422>
93  {
94  static const unsigned int fmt = UVC_FRAME_FORMAT_YUYV;
95  };
96 
97  template <>
98  struct format<Rgb<byte>>
99  {
100  static const unsigned int fmt = UVC_FRAME_FORMAT_RGB;
101  };
102 
103 #endif
104 
105  class RawUVCBuffer : public virtual RawVideoBuffer
106  {
107  public:
108  RawUVCBuffer(const std::string& dev, unsigned int fmt, ImageRef size, double frame_per_second, bool mjpeg, bool verbose);
109  ImageRef getSize();
110  void fill_frame(void*);
111  double getRate();
112  virtual ~RawUVCBuffer();
113  const std::string& device_name() const;
114 
115  RawUVCBuffer(const RawUVCBuffer&) = delete;
116  void operator=(const RawUVCBuffer&) = delete;
117 
118  private:
119  std::string dev_str;
120  uvc_context_t* ctx = 0;
121  uvc_device_t* dev = 0;
122  uvc_device_handle_t* devh = 0;
123  uvc_stream_ctrl_t ctrl = {};
124  uvc_stream_handle_t* strmh = 0;
125  ImageRef size;
126  int format;
127  bool capture_mjpeg;
128  double rate;
129  };
130 
131  class V4L1Client;
132 
133 };
134 
138 template <class T>
139 class UVCBuffer : public VideoBuffer<T>, public UVC::RawUVCBuffer
140 {
141  public:
142  UVCBuffer(const std::string& dev, ImageRef size, int frames_per_second = 0, bool mjpeg = 0, bool verbose = 0)
144  , RawUVCBuffer(dev, UVC::format<T>::fmt, size, frames_per_second, mjpeg, verbose)
145  {
146  }
147 
148  virtual ImageRef size()
149  {
150  return getSize();
151  }
152 
154  {
155 
156  Image<T> frame(size());
157  fill_frame(frame.data());
158  return new UVCFrame(0.0, std::move(frame));
159  }
160 
161  virtual void put_frame(VideoFrame<T>* f)
162  {
163  UVCFrame* vf = dynamic_cast<UVCFrame*>(f);
164  if(vf == 0)
165  throw Exceptions::UVCBuffer::PutFrame(device_name(), "Invalid VideoFrame");
166  delete vf;
167  }
168 
169  virtual bool frame_pending()
170  {
171  return true;
172  }
173 
174  virtual double frame_rate()
175  {
176  return getRate();
177  }
178 
179  int num_buffers()
180  {
181  return num_buffers();
182  }
183 
184  private:
185  struct UVCFrame : public LocalVideoFrame<T>
186  {
187  UVCFrame(double t, CVD::Image<T>&& local)
188  : LocalVideoFrame<T>(t, std::move(local))
189  {
190  }
191 
192  int id;
193  friend class UVCBuffer<T>;
194  };
195 
196  UVCBuffer(const UVCBuffer&) = delete;
197  void operator=(const UVCBuffer&) = delete;
198 };
199 
200 };
201 #endif
Base class which provides untyped access to video grabber objects.
Definition: videobuffer.h:39
A colour consisting of red, green and blue components.
Definition: rgb.h:25
Definition: uvcbuffer.h:86
All classes and functions are within the CVD namespace.
Definition: argb.h:6
Error opening the device.
Definition: uvcbuffer.h:28
virtual double frame_rate()
What is the (expected) frame rate of this video buffer, in frames per second?
Definition: uvcbuffer.h:174
Definition: uvcbuffer.h:105
Error opening the device.
Definition: uvcbuffer.h:35
Base class for objects which provide a typed video stream.
Definition: videobuffer.h:88
Error setting up the device.
Definition: uvcbuffer.h:63
virtual VideoFrame< T > * get_frame()
Returns the next frame from the buffer. This function blocks until a frame is ready.
Definition: uvcbuffer.h:153
Error in a get_frame() call.
Definition: uvcbuffer.h:75
The buffer has live semantics: frames are throttled by something externa, but VideoBuffer::frame_pend...
Definition: videobuffer.h:23
virtual ImageRef size()
The size of the VideoFrames returned by this buffer.
Definition: uvcbuffer.h:148
A live video buffer which uses the Video for Linux 2 (V4L2) API.
Definition: uvcbuffer.h:139
ConstPointerType data() const
Returns the raw image data.
Definition: image.h:535
Error in a put_frame() call.
Definition: uvcbuffer.h:69
Definition: image_ref.h:29
unsigned char byte
An 8-bit datatype.
Definition: byte.h:8
Base class for all VideoBuffer exceptions.
Definition: videobuffer.h:153
Definition: uvcbuffer.h:21
A frame from a VideoBuffer.
Definition: videoframe.h:35
virtual bool frame_pending()
Is there a frame waiting in the buffer? This function does not block.
Definition: uvcbuffer.h:169
A full image which manages its own data.
Definition: image.h:623
Error opening the device.
Definition: uvcbuffer.h:49
A frame from a LocalVideoBuffer, which manages its own data rather than wrapping data owned by the sy...
Definition: localvideoframe.h:30
Error opening the device.
Definition: uvcbuffer.h:42
virtual void put_frame(VideoFrame< T > *f)
Tell the buffer that you are finished with this frame.
Definition: uvcbuffer.h:161
Error opening the device.
Definition: uvcbuffer.h:56
A datatype to represent yuv422 (yuyv) data.
Definition: colourspaces.h:192