CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
httpcapture.h
1 /****************************************************************************
2  * Copyright (c) 2014, Joel Kivelä, Erkki Koskenkorva, Oskari Leppäaho,
3  * Mika Lehtinen and Petri Partanen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ****************************************************************************/
30 #ifndef HTTPCAPTURE_H
31 #define HTTPCAPTURE_H
32 
33 #include <condition_variable>
34 #include <mutex>
35 #include <string>
36 #include <thread>
37 #include <vector>
38 
39 #include <curl/curl.h>
40 
41 #include "captureinterface.h"
42 #include "common.h"
43 
44 namespace cavapa_gui
45 {
59 {
60 public:
61  HTTPCapture() {}
62  virtual ~HTTPCapture();
63 
68 
69  double getBarrelCorrection() override { return 0.0; }
70 
71  FrameCapture getBuffered() override;
72 
73  std::string getDescription() const override { return url; }
74 
79  int getDeviceID() const override { return UNKNOWN_DEVICE; }
80 
85  double getFramerate() const override { return 0.0; }
86 
87  int getMissedFrames() const override { return missed_frames; }
88 
89  FrameCapture getNext(bool skip, bool* error) override;
90 
91  std::string getPath() const override { return url; }
92 
98  cv::Size getResolution() const override;
99 
100  int getRetrievedFrames() const override { return retrieved_frames; }
101 
102  INTERFACE_TYPE getType() const override { return INTERFACE_TYPE::HTTP; }
103 
108  bool hasNew() override { return true; }
109 
110  bool isOpen() const override { return image_size_known && curl_open; }
111 
112  bool open(const std::string& path) override;
113 
114  void resetStats() override;
115 
116  void setBarrelCorrection(double amount) override { Q_UNUSED(amount); }
117 
122  bool setResolution(const cv::Size& new_size) override
123  { Q_UNUSED(new_size); return false; }
124 
125 private:
131  void retrieve();
132 
138  inline bool retrieve_next(std::unique_lock<std::mutex>& guard);
139 
148  static size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up);
149 
150  // Variables that can be used by the main thread.
151  std::condition_variable capture_next; // Conditional for next retrieve
152  bool curl_open = false; // Flag to indicate CURL state.
153  bool image_size_known = false; // Indicates if we know the true image size.
154  cv::Size image_size; // Size of the image
155  std::mutex lock;
156  int missed_frames = 0; // Missed frame count by getNext()
157  int retrieved_frames = 0; // Total frame count by getNext()
158  std::unique_ptr<std::thread> retriever = nullptr; // Retrieval thread
159  std::string url = ""; // Url for the HTTP source.
160 
161  // Variables that are protected by mutex.
162  FrameCapture buffer; // Buffered image.
163  bool capture = false; // If retrieval is in progress
164  CURL* curl = nullptr; // CURL library.
165  bool first_capture = true; // Indicates if first capture is happening.
166  bool read_error = false; // Indicates retrieval error
167  std::vector<char> temp_buffer;
168  bool terminate = false; // Terminate message for thread
169 };
170 } // namespace
171 
172 #endif // HTTPCAPTURE_H
void setBarrelCorrection(double amount) override
Sets the amount of the barrel correction to be applied.
Definition: httpcapture.h:116
cv::Size getResolution() const override
Retrieves the capture device resolution.
Definition: httpcapture.cpp:117
double getFramerate() const override
Retrieves the capture device framerate.
Definition: httpcapture.h:85
int getMissedFrames() const override
Retrieves the logged frame misses.
Definition: httpcapture.h:87
void resetStats() override
Resets the statistical counters.
Definition: httpcapture.cpp:168
bool isOpen() const override
Checks whether the capturing device is open or not.
Definition: httpcapture.h:110
double getBarrelCorrection() override
Retrieves the barrel correction applied to the source.
Definition: httpcapture.h:69
std::string getDescription() const override
Retrieves the description of the device.
Definition: httpcapture.h:73
Interface for the capture devices.
Definition: captureinterface.h:47
bool open(const std::string &path) override
Opens the video file or the stream.
Definition: httpcapture.cpp:122
DISALLOW_COPY_AND_ASSIGN(HTTPCapture)
Copy and assign of the class is not allowed.
int getRetrievedFrames() const override
Retrieves the total logged frames.
Definition: httpcapture.h:100
FrameCapture getBuffered() override
Retrieves the currently buffered frame.
Definition: httpcapture.cpp:64
bool hasNew() override
Checks whether the device has a new image to retrieve or not.
Definition: httpcapture.h:108
INTERFACE_TYPE getType() const override
Retrieves the type of the device.
Definition: httpcapture.h:102
The structure is used for storing a single source frame.
Definition: common.h:241
int getDeviceID() const override
Returns the ID number the device was initialized with.
Definition: httpcapture.h:79
bool setResolution(const cv::Size &new_size) override
Sets the capturing device resolution.
Definition: httpcapture.h:122
The Capture device for HTTP images.
Definition: httpcapture.h:58
std::string getPath() const override
Retrieves the path given during the initialization.
Definition: httpcapture.h:91
const int UNKNOWN_DEVICE
Used to indicate unknown hardware device.
Definition: common.h:224
FrameCapture getNext(bool skip, bool *error) override
Retrieves the next frame.
Definition: httpcapture.cpp:73
INTERFACE_TYPE
Used to define the type of source interface.
Definition: captureinterface.h:140