This repository was archived by the owner on Oct 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameSource.cpp
More file actions
85 lines (71 loc) · 2.22 KB
/
FrameSource.cpp
File metadata and controls
85 lines (71 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "FrameSource.hpp"
#include <iostream>
#include <boost/circular_buffer.hpp>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
namespace
{
class FrameSourceQueueImpl : public FrameSourceQueue
{
public:
FrameSourceQueueImpl(const std::shared_ptr<FrameSource>& frameSource, size_t size);
~FrameSourceQueueImpl();
cv::Mat getDisplayFrame() override;
void operator()();
private:
std::shared_ptr<FrameSource> frameSource_;
boost::circular_buffer<cv::Mat> buffer_;
boost::mutex mtx_;
std::unique_ptr<boost::thread> thread_;
volatile bool stop_;
};
FrameSourceQueueImpl::FrameSourceQueueImpl(const std::shared_ptr<FrameSource>& frameSource, size_t capacity) :
frameSource_(frameSource),
buffer_(capacity),
stop_(false)
{
thread_.reset(new boost::thread(boost::ref(*this)));
}
FrameSourceQueueImpl::~FrameSourceQueueImpl()
{
stop_ = true;
thread_->join();
}
cv::Mat FrameSourceQueueImpl::getDisplayFrame()
{
while (buffer_.empty())
{
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
if (stop_)
return cv::Mat();
}
boost::mutex::scoped_lock lock(mtx_);
return buffer_.front();
}
void FrameSourceQueueImpl::operator()()
{
try
{
for (;;)
{
cv::Mat frame = frameSource_->nextFrame().clone();
{
boost::mutex::scoped_lock lock(mtx_);
buffer_.push_back(frame);
}
if (stop_ || (boost::this_thread::interruption_enabled() && boost::this_thread::interruption_requested()))
break;
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
}
}
catch (const std::exception& e)
{
stop_ = true;
std::cerr << e.what() << std::endl;
}
}
}
std::shared_ptr<FrameSourceQueue> createFrameSourceQueue(const std::shared_ptr<FrameSource>& frameSource, size_t size)
{
return std::make_shared<FrameSourceQueueImpl>(frameSource, size);
}