-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEncodingQueue.cpp
More file actions
96 lines (86 loc) · 1.99 KB
/
EncodingQueue.cpp
File metadata and controls
96 lines (86 loc) · 1.99 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
86
87
88
89
90
91
92
93
94
95
96
#include "EncodingQueue.h"
#include <utility>
#include <algorithm>
namespace vr_image_client
{
EncodingQueue::EncodingQueue(std::size_t max_size, std::size_t worker_count)
: max_size_(max_size), worker_count_(std::max<std::size_t>(1, worker_count))
{
}
EncodingQueue::~EncodingQueue()
{
Stop();
}
void EncodingQueue::Start()
{
std::lock_guard<std::mutex> lock(mutex_);
stop_requested_ = false;
if (!workers_.empty())
{
return;
}
workers_.reserve(worker_count_);
for (std::size_t i = 0; i < worker_count_; ++i)
{
workers_.emplace_back(&EncodingQueue::WorkerLoop, this);
}
}
void EncodingQueue::Stop()
{
{
std::lock_guard<std::mutex> lock(mutex_);
stop_requested_ = true;
}
cond_.notify_all();
for (auto &worker : workers_)
{
if (worker.joinable())
{
worker.join();
}
}
workers_.clear();
std::lock_guard<std::mutex> lock(mutex_);
queue_.clear();
}
void EncodingQueue::Enqueue(RawFrame frame, LogBuffer &logs)
{
{
std::lock_guard<std::mutex> lock(mutex_);
if (queue_.size() >= max_size_)
{
logs.Push("[VR_ImageClient] Encoding queue full; dropping frame.\n");
return;
}
queue_.push_back(std::move(frame));
}
cond_.notify_one();
}
void EncodingQueue::SetEncoder(std::function<void(RawFrame &&)> encoder)
{
encoder_ = std::move(encoder);
}
void EncodingQueue::WorkerLoop()
{
while (true)
{
RawFrame frame;
{
std::unique_lock<std::mutex> lock(mutex_);
cond_.wait(lock, [&] {
return stop_requested_ || !queue_.empty();
});
if (stop_requested_ && queue_.empty())
{
break;
}
frame = std::move(queue_.front());
queue_.pop_front();
}
if (encoder_)
{
encoder_(std::move(frame));
}
}
}
} // namespace vr_image_client