| 1 | #ifndef CVD_VIDEOBUFFER_H |
| 2 | #define CVD_VIDEOBUFFER_H |
| 3 | |
| 4 | #include <cvd/exceptions.h> |
| 5 | #include <cvd/videoframe.h> |
| 6 | #include <memory> |
| 7 | |
| 8 | namespace CVD |
| 9 | { |
| 10 | |
| 11 | ///The semsntics of the videobuffer. See VideoFrame::type() |
| 12 | struct VideoBufferType |
| 13 | { |
| 14 | enum Type |
| 15 | { |
| 16 | ///The buffer does not have live semantics: frames |
| 17 | ///are not throttled by something external. |
| 18 | ///VideoBuffer::frame_pending() is true until the last frame has |
| 19 | ///been retrieved, after which is is set to false. |
| 20 | NotLive, |
| 21 | ///The buffer has live semantics: frames are throttled by |
| 22 | ///something externa, but VideoBuffer::frame_pending() always returns true. |
| 23 | Live, |
| 24 | ///The buffer is flushable: it is live and VideoBuffer::frame_pending() returns |
| 25 | ///an accurate result. |
| 26 | Flushable |
| 27 | }; |
| 28 | }; |
| 29 | |
| 30 | ///Base class which provides untyped access to video grabber objects. |
| 31 | ///This provides all of the functionality for which the type does not |
| 32 | ///need to be known (eg size, etc). |
| 33 | /// |
| 34 | ///This allows one to follow chains of video buffers which change types (for instance |
| 35 | ///<code>ColourspaceBuffer<Rgb<byte>, byte>) and get to the underlying video grabber. |
| 36 | ///This provides access to the grabber specific controls. |
| 37 | /// |
| 38 | ///See progs/video_play_source.cc for a very basic example. |
| 39 | class RawVideoBuffer |
| 40 | { |
| 41 | public: |
| 42 | /// Which video grabber provides the source images for this |
| 43 | /// video grabber. |
| 44 | virtual RawVideoBuffer* source_buffer() |
| 45 | { |
| 46 | return this; |
| 47 | } |
| 48 | |
| 49 | /// Follow the chain of video grabbers back as far as at will go. This will |
| 50 | /// usually yield the video grabber dealing with the hardware. |
| 51 | RawVideoBuffer* root_buffer() |
| 52 | { |
| 53 | RawVideoBuffer* b = this; |
| 54 | while(b->source_buffer() != b) |
| 55 | b = b->source_buffer(); |
| 56 | return b; |
| 57 | } |
| 58 | |
| 59 | virtual ~RawVideoBuffer() { } |
| 60 | |
| 61 | /// The size of the VideoFrames returned by this buffer |
| 62 | virtual ImageRef size() = 0; |
| 63 | |
| 64 | /// Is there a frame waiting in the buffer? This function does not block. |
| 65 | /// See is_live and is_flushable. |
| 66 | virtual bool frame_pending() = 0; |
| 67 | |
| 68 | /// What is the (expected) frame rate of this video buffer, in frames per second? |
| 69 | virtual double frame_rate() = 0; |
| 70 | /// Go to a particular point in the video buffer (only implemented in buffers of recorded video) |
| 71 | /// \param t The frame time in seconds |
| 72 | virtual void seek_to(double) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | /// Flush all old frames out of the video buffer, |
| 77 | /// on a flushable buffer, causing the next get_frame() |
| 78 | /// to sleep until a frame arrives. On a non-flushable |
| 79 | /// buffer, this does nothing. |
| 80 | virtual void flush() = 0; |
| 81 | }; |
| 82 | |
| 83 | /// Base class for objects which provide a typed video stream. A video |
| 84 | /// stream is a sequence of video frames (derived from VideoFrame). |
| 85 | /// @param T The pixel type of the video frames |
| 86 | /// @ingroup gVideoBuffer |
| 87 | template <class T> |
| 88 | class VideoBuffer : public virtual RawVideoBuffer |
| 89 | { |
| 90 | public: |
| 91 | ///Construct the buffer with the known semantics |
| 92 | VideoBuffer(VideoBufferType::Type _type) |
| 93 | : m_type(_type) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | virtual ~VideoBuffer() |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | /// Returns the next frame from the buffer. This function blocks until a frame is ready. |
| 102 | virtual VideoFrame<T>* get_frame() = 0; |
| 103 | |
| 104 | /// Tell the buffer that you are finished with this frame. Typically the VideoBuffer then destroys the frame. |
| 105 | /// \param f The frame that you are finished with. |
| 106 | virtual void put_frame(VideoFrame<T>* f) = 0; |
| 107 | |
| 108 | virtual void flush() |
| 109 | { |
| 110 | if(type() == VideoBufferType::Flushable) |
| 111 | while(frame_pending()) |
| 112 | put_frame(get_frame()); |
| 113 | } |
| 114 | |
| 115 | /// Returns the type of the video stream |
| 116 | /// |
| 117 | /// A video with live semantics has frames fed at |
| 118 | /// some externally controlled rate, such as from a |
| 119 | /// video camera. |
| 120 | /// |
| 121 | /// A stream with live semantics also may be flushable, in |
| 122 | /// that all current frames can be removed from the stream |
| 123 | /// while frame_pending() is 1, and then the next get_frame() |
| 124 | /// will sleep until a frame arrives. This ensures that the latency |
| 125 | /// is low by discarding any old frames. Buffers flushable in this |
| 126 | /// manner have a type of VideoBuffer::Type::Flushable. |
| 127 | /// |
| 128 | /// Some live streams are not flushable because it is not possible |
| 129 | /// to determine the state of frame_pending(). These have the type |
| 130 | /// VideoBuffer::Type::Live, and frame_pending() is always 1. |
| 131 | /// |
| 132 | /// Otherwise, streams have a type VideoBuffer::Type::NotLive, and |
| 133 | /// frame_pending is always 1 |
| 134 | /// |
| 135 | ///This should be in the base class |
| 136 | VideoBufferType::Type type() |
| 137 | { |
| 138 | return m_type; |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | VideoBufferType::Type m_type; |
| 143 | }; |
| 144 | |
| 145 | namespace Exceptions |
| 146 | { |
| 147 | /// %Exceptions specific to VideoBuffer |
| 148 | /// @ingroup gException |
| 149 | namespace VideoBuffer |
| 150 | { |
| 151 | /// Base class for all VideoBuffer exceptions |
| 152 | /// @ingroup gException |
| 153 | struct All : public CVD::Exceptions::All |
| 154 | { |
| 155 | using CVD::Exceptions::All::All; |
| 156 | }; |
| 157 | |
| 158 | /// The VideoBuffer was unable to successfully complete a VideoBuffer::put_frame() operation |
| 159 | /// @ingroup gException |
| 160 | struct BadPutFrame : public All |
| 161 | { |
| 162 | BadPutFrame(); |
| 163 | }; |
| 164 | |
| 165 | /// The videobuffer was unable to successfully initialize grabbing in the |
| 166 | /// specified colourspace. |
| 167 | /// @ingroup gException |
| 168 | struct BadColourSpace : public All |
| 169 | { |
| 170 | /// @param colourspace Specify the failed colourspace. |
| 171 | /// @param b Specify the failed buffer. |
| 172 | BadColourSpace(const std::string& colourspace, const std::string& b); |
| 173 | }; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | #endif |