2
* Copyright (C) 2016 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
18
#include "mcs/video/bufferqueue.h"
23
BufferQueue::Ptr BufferQueue::Create(uint32_t max_size) {
24
return std::shared_ptr<BufferQueue>(new BufferQueue(max_size));
27
BufferQueue::BufferQueue(uint32_t max_size) :
31
BufferQueue::~BufferQueue() {
34
void BufferQueue::Lock() {
38
void BufferQueue::PushUnlocked(const mcs::video::Buffer::Ptr &buffer) {
42
void BufferQueue::Unlock() {
47
mcs::video::Buffer::Ptr BufferQueue::Front() {
48
std::unique_lock<std::mutex> l(mutex_);
49
return queue_.front();
52
mcs::video::Buffer::Ptr BufferQueue::Next() {
53
// We will block here forever until we get a new buffer
54
WaitToBeFilled(std::chrono::milliseconds{-1});
55
std::unique_lock<std::mutex> l(mutex_);
56
auto buffer = queue_.front();
61
void BufferQueue::Push(const mcs::video::Buffer::Ptr &buffer) {
62
std::unique_lock<std::mutex> l(mutex_);
63
if (IsLimited() && queue_.size() >= max_size_)
69
mcs::video::Buffer::Ptr BufferQueue::Pop() {
70
std::unique_lock<std::mutex> l(mutex_);
71
auto buffer = queue_.front();
77
mcs::video::Buffer::Ptr BufferQueue::PopUnlocked() {
78
if (queue_.size() == 0)
81
auto buffer = queue_.front();
86
bool BufferQueue::WaitFor(const std::function<bool()> &pred, const std::chrono::milliseconds &timeout) {
87
std::unique_lock<std::mutex> l(mutex_);
89
if (timeout.count() >= 0) {
90
auto now = std::chrono::system_clock::now();
91
return lock_.wait_until(l, now + timeout, pred);
98
bool BufferQueue::WaitToBeFilled(const std::chrono::milliseconds &timeout) {
102
return WaitFor([&]() { return !queue_.empty(); }, timeout);
105
bool BufferQueue::WaitForSlots(const std::chrono::milliseconds &timeout) {
109
return WaitFor([&]() { return queue_.size() < max_size_; }, timeout);
112
bool BufferQueue::IsFull() {
116
std::unique_lock<std::mutex> l(mutex_);
117
return queue_.size() == max_size_;
120
bool BufferQueue::IsEmpty() {
121
std::unique_lock<std::mutex> l(mutex_);
122
return queue_.size() == 0;
125
int BufferQueue::Size() {
126
std::unique_lock<std::mutex> l(mutex_);
127
return queue_.size();