libcvd
serverpushjpegbuffer.h
1 #ifndef CVD_INC_SERVERPUSHJPEGBUFFER_H
2 #define CVD_INC_SERVERPUSHJPEGBUFFER_H
3 #include <cvd/localvideobuffer.h>
4 #include <cvd/serverpushjpegframe.h>
5 #include <cvd/timer.h>
6 #include <iostream>
7 
8 namespace CVD
9 {
10 
36 template <class C>
38 {
39  public:
46  ServerPushJpegBuffer(std::istream& i, bool warnings_ = 0, int eat_frames = 0)
48  , is(i)
49  , warnings(warnings_)
50  {
51  std::string tmp;
52  //Eat the first 10 frames because the camera sometimes takes a while to
53  //crank out ones of the specified size
54 
55  for(int junk = 0; junk < eat_frames; junk++)
56  gimme_an_image(tmp);
57 
58  //Eat the first frame just to get the size
59  Image<C> c = gimme_an_image(tmp);
60  s = c.size();
61  }
62 
63  virtual ImageRef size()
64  {
65  return s;
66  }
67 
69  {
70  Image<C> c;
71  std::string data;
72 
73  loop:
74  c = gimme_an_image(data);
75 
76  if(c.size() != s)
77  {
78  if(warnings)
79  std::cerr << "ServerPushJpegBuffer: video frame is " << c.size() << " not " << s << std::endl;
80  goto loop;
81  }
82 
83  return new ServerPushJpegFrame<C>(get_time_of_day(), std::move(c), data);
84  }
85 
87  {
88  LocalVideoFrame<C>* g = dynamic_cast<LocalVideoFrame<C>*>(f);
89 
90  if(g == NULL)
92  else
93  delete g;
94  }
95 
97  {
98  return 1;
99  }
100 
101  void seek_to(double) {};
102 
104  double frame_rate()
105  {
106  return 30;
107  }
108 
109  private:
110  std::istream& is;
111  ImageRef s;
112  bool warnings;
113 
114  Image<C> gimme_an_image(std::string& data)
115  {
116 
117  std::string line;
118 
119  int length;
120  getline(is, line); //Get --ImageSeparator
121  getline(is, line); //Get Content-Type:
122  is >> line; //Get Content-Length:
123  is >> length; //Get the actual content length
124  getline(is, line); //Eat the rest of the line
125  getline(is, line); //Get the blank line
126 
127  data.resize(length);
128  is.read(&data[0], length);
129 
130  std::istringstream ss(data);
131 
132  Image<C> c = img_load(ss);
133 
134  is.get(); //Eat the \r\n after the JPEG
135  is.get();
136 
137  return c;
138  }
139 };
140 
141 }
142 #endif
All classes and functions are within the CVD namespace.
Definition: argb.h:6
ImageRef size() const
What is the size of this image?
Definition: image.h:557
LocalVideoFrame< C > * get_frame()
Returns the next frame from the buffer. This function blocks until a frame is ready.
Definition: serverpushjpegbuffer.h:68
bool frame_pending()
Is there a frame waiting in the buffer? This function does not block.
Definition: serverpushjpegbuffer.h:96
ServerPushJpegBuffer(std::istream &i, bool warnings_=0, int eat_frames=0)
Construct a ServerPushJpegBuffer from an istream.
Definition: serverpushjpegbuffer.h:46
virtual ImageRef size()
The size of the VideoFrames returned by this buffer.
Definition: serverpushjpegbuffer.h:63
double frame_rate()
This value is not currently correct.
Definition: serverpushjpegbuffer.h:104
The semsntics of the videobuffer. See VideoFrame::type()
Definition: videobuffer.h:12
Play a server push stream as a video stream.
Definition: serverpushjpegbuffer.h:37
void put_frame(VideoFrame< C > *f)
Tell the buffer that you are finished with this frame.
Definition: serverpushjpegbuffer.h:86
Base class for a VideoBuffer which manages its own memory for each VideoFrame that it provides...
Definition: localvideobuffer.h:17
Definition: image_ref.h:29
The VideoBuffer was unable to successfully complete a VideoBuffer::put_frame() operation.
Definition: videobuffer.h:160
double get_time_of_day()
Same as the system call gettimeofday, but returns seconds since the epoch as a double.
Definition: cvd_timer.cc:45
A frame from a VideoBuffer.
Definition: videoframe.h:35
Definition: serverpushjpegframe.h:12
A full image which manages its own data.
Definition: image.h:623
A frame from a LocalVideoBuffer, which manages its own data rather than wrapping data owned by the sy...
Definition: localvideoframe.h:30
void seek_to(double)
Go to a particular point in the video buffer (only implemented in buffers of recorded video) ...
Definition: serverpushjpegbuffer.h:101