| 1 |
|
| 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 |
{ |
| 17 |
|
| 18 |
namespace UVCBuffer |
| 19 |
{ |
| 20 |
|
| 21 |
struct All : public CVD::Exceptions::VideoBuffer::All |
| 22 |
{ |
| 23 |
using CVD::Exceptions::VideoBuffer::All::All; |
| 24 |
}; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
struct Init : public All |
| 29 |
{ |
| 30 |
Init(std::string dev, std::string err); |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
struct DeviceOpen : public All |
| 36 |
{ |
| 37 |
DeviceOpen(std::string dev, std::string err); |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
struct StreamOpen : public All |
| 43 |
{ |
| 44 |
StreamOpen(std::string dev, std::string err); |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
struct StreamStart : public All |
| 50 |
{ |
| 51 |
StreamStart(std::string dev, std::string err); |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
struct FindDevice : public All |
| 57 |
{ |
| 58 |
FindDevice(std::string dev, std::string err); |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
struct DeviceSetup : public All |
| 64 |
{ |
| 65 |
DeviceSetup(std::string dev, std::string action); |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
struct PutFrame : public All |
| 70 |
{ |
| 71 |
PutFrame(std::string dev, std::string msg); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 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) |
| 143 |
: VideoBuffer<T>(VideoBufferType::Live) |
| 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 |
|
| 153 |
virtual VideoFrame<T>* get_frame() |
| 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 |