vivalib library
Concurrent C++11 OpenCV library for Computer Vision applications
channel.h
1 /**************************************************************************************************
2  **************************************************************************************************
3 
4  BSD 3-Clause License (https://www.tldrlegal.com/l/bsd3)
5 
6  Copyright (c) 2015 Andrés Solís Montero <http://www.solism.ca>, All rights reserved.
7 
8 
9  Redistribution and use in source and binary forms, with or without modification,
10  are permitted provided that the following conditions are met:
11 
12  1. Redistributions of source code must retain the above copyright notice,
13  this list of conditions and the following disclaimer.
14  2. Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17  3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32  **************************************************************************************************
33  **************************************************************************************************/
34 
35 #ifndef __viva__channel__
36 #define __viva__channel__
37 
38 #include "opencv2/opencv.hpp"
39 #include <deque>
40 #include <condition_variable>
41 
42 using namespace std;
43 using namespace cv;
44 
45 namespace viva
46 {
47 
53  template <class Data>
55  {
56  private:
57  size_t _capacity;
58  bool _terminate;
59  float _fps;
60  std::deque<Data> _images;
61  std::mutex _access_queue;
62  std::mutex _access_termination;
63  std::mutex _access_fps;
64 
65 
66  std::condition_variable _consume;
67  std::condition_variable _produce;
68 
69  public:
70  void close();
71 
72  bool isOpen();
73 
74  void addData(Data &data);
75 
76  bool empty();
77 
78  bool getData(Data &data);
79 
80  float getFrequency();
81  void setFrequency(float frequency);
82  void setCapacity(size_t capacity);
83 
84  BufferedChannel(size_t capacity = 10):
85  _capacity(capacity), _terminate(false), _fps(0)
86  {
87 
88  }
89 
90  };
91 
92  template<class Data>
93  void BufferedChannel<Data>::setCapacity(size_t capacity)
94  {
95  std::lock_guard<std::mutex> guard(_access_queue);
96  _capacity = capacity;
97  }
98 
99 
100  template<class Data>
102  {
103  std::lock_guard<std::mutex> guard(_access_termination);
104  _terminate = true;
105  _consume.notify_all();
106  _produce.notify_all();
107  }
108  template<class Data>
110  {
111  std::lock_guard<std::mutex> guard(_access_termination);
112  return !_terminate;
113  }
114  template<class Data>
115  void BufferedChannel<Data>::addData(Data &data)
116  {
117 
118  std::unique_lock<std::mutex> guard(_access_queue);
119  _produce.wait(guard,[&] {
120  return !isOpen() || (_images.size() < _capacity);
121  });
122  if (!isOpen())
123  {
124  guard.unlock();
125  return;
126  }
127  _images.push_back(data);
128  //cout<< "Added Image, size: " << _images.size() << endl;
129  guard.unlock();
130  _consume.notify_one();
131  }
132  template<class Data>
134  {
135  std::lock_guard<std::mutex> guard(_access_queue);
136  return _images.empty();
137  }
138  template<class Data>
139  bool BufferedChannel<Data>::getData(Data &data)
140  {
141  std::unique_lock<std::mutex> guard(_access_queue);
142  _consume.wait(guard, [&] {
143  return (!isOpen() && _images.empty()) || !_images.empty();
144  });
145  if (_images.empty() && !isOpen())
146  {
147  guard.unlock();
148  return false;
149  }
150 
151  data = _images.front();
152  _images.pop_front();
153  guard.unlock();
154  _produce.notify_one();
155  return true;
156 
157  }
158 
159  template<class Data>
161  {
162  std::lock_guard<std::mutex> guard(_access_fps);
163  return _fps;
164  }
165  template<class Data>
166  void BufferedChannel<Data>::setFrequency(float frequency)
167  {
168  std::lock_guard<std::mutex> guard(_access_fps);
169  _fps = frequency;
170  }
171 
176 
177 
178 
179 }
180 
181 #endif
Definition: channel.h:45
Definition: channel.h:54