Debian Codegraph @

cvd/serverpushjpegbuffer.h

raw symbols SCIP
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
11 /// Play a server push stream as a video stream. This is a standard used by a number of
12 /// HTTP based security cameras. The format is as follows:
13 /// @code
14 /// --ImageSeparator\n
15 /// Content-Type: image/jpeg\r\n
16 /// Content-Length: 123456789\r\n
17 /// \r\n
18 /// <123456789 bytes of JPEG go here>\r\n
19 /// @endcode
20 /// This exact format is from the InVision IQEye series of cameras. Other cameras have
21 /// a different image separator, and do not miss the carriage return.
22 ///
23 /// The buffer reads a number of frames from the stream on initiation (by default 10),
24 /// in order to flush the camera's inbuilt buffer. With some cameras, changing the video
25 /// size does not flush the buffer, so the first few frames will be of the incorrect size.
26 /// After flushing the buffer, the size of the first frame is taken to be the size of the
27 /// video stream. If spurious frames arrive of a different size later, these will be ignored.
28 ///
29 /// WARNING: error checking is currently very minimal. The result of failure will probably
30 /// result in an exception being thrown from the JPEG loader.
31 ///
32 /// @param T The pixel type of the frames to provide (usually <code>CVD::Rgb<CVD::byte></code>
33 /// or <code>CVD::byte</code>. If the image files are of a different type, they will be automatically
34 /// converted (see @link gImageIO Image loading and saving, and format conversion@endlink).
35 /// @ingroup gVideoBuffer
36 template <class C>
37 class ServerPushJpegBuffer : public LocalVideoBuffer<C>
38 {
39 public:
40 ///Construct a ServerPushJpegBuffer from an istream. The istream
41 ///
42 ///
43 ///@param i The stream to use for video.
44 ///@param warnings Whether to print warnings if mis-sized frames arrive.
45 ///@param eat_frames Number of frames to discard on initialization.
46 ServerPushJpegBuffer(std::istream& i, bool warnings_ = 0, int eat_frames = 0)
47 : LocalVideoBuffer<C>(VideoBufferType::Live)
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
68 LocalVideoFrame<C>* get_frame()
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
86 void put_frame(VideoFrame<C>* f)
87 {
88 LocalVideoFrame<C>* g = dynamic_cast<LocalVideoFrame<C>*>(f);
89
90 if(g == NULL)
91 throw CVD::Exceptions::VideoBuffer::BadPutFrame();
92 else
93 delete g;
94 }
95
96 bool frame_pending()
97 {
98 return 1;
99 }
100
101 void seek_to(double) {};
102
103 ///This value is not currently correct.
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