~phablet-team/aethercast/fix-for-microsoft-dongle

« back to all changes in this revision

Viewing changes to src/mcs/video/bufferqueue.cpp

Add hardware encoding and video streaming support.

The hardware encoding is currently only for Android 5.x based devices. On all others encoding will simply not work. The streaming part of aethercast (MPEGTS packetizing, RTP sending) as based on some code from Android.

Approved by PS Jenkins bot, Thomas Voß, Jim Hodapp.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical, Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 */
 
17
 
 
18
#include "mcs/video/bufferqueue.h"
 
19
 
 
20
namespace mcs {
 
21
namespace video {
 
22
 
 
23
BufferQueue::Ptr BufferQueue::Create(uint32_t max_size) {
 
24
    return std::shared_ptr<BufferQueue>(new BufferQueue(max_size));
 
25
}
 
26
 
 
27
BufferQueue::BufferQueue(uint32_t max_size) :
 
28
    max_size_(max_size) {
 
29
}
 
30
 
 
31
BufferQueue::~BufferQueue() {
 
32
}
 
33
 
 
34
void BufferQueue::Lock() {
 
35
    mutex_.lock();
 
36
}
 
37
 
 
38
void BufferQueue::PushUnlocked(const mcs::video::Buffer::Ptr &buffer) {
 
39
    queue_.push(buffer);
 
40
}
 
41
 
 
42
void BufferQueue::Unlock() {
 
43
    mutex_.unlock();
 
44
    lock_.notify_one();
 
45
}
 
46
 
 
47
mcs::video::Buffer::Ptr BufferQueue::Front() {
 
48
    std::unique_lock<std::mutex> l(mutex_);
 
49
    return queue_.front();
 
50
}
 
51
 
 
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();
 
57
    queue_.pop();
 
58
    return buffer;
 
59
}
 
60
 
 
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_)
 
64
        return;
 
65
    queue_.push(buffer);
 
66
    lock_.notify_one();
 
67
}
 
68
 
 
69
mcs::video::Buffer::Ptr BufferQueue::Pop() {
 
70
    std::unique_lock<std::mutex> l(mutex_);
 
71
    auto buffer = queue_.front();
 
72
    queue_.pop();
 
73
    lock_.notify_one();
 
74
    return buffer;
 
75
}
 
76
 
 
77
mcs::video::Buffer::Ptr BufferQueue::PopUnlocked() {
 
78
    if (queue_.size() == 0)
 
79
        return nullptr;
 
80
 
 
81
    auto buffer = queue_.front();
 
82
    queue_.pop();
 
83
    return buffer;
 
84
}
 
85
 
 
86
bool BufferQueue::WaitFor(const std::function<bool()> &pred, const std::chrono::milliseconds &timeout) {
 
87
    std::unique_lock<std::mutex> l(mutex_);
 
88
 
 
89
    if (timeout.count() >= 0) {
 
90
        auto now = std::chrono::system_clock::now();
 
91
        return lock_.wait_until(l, now + timeout, pred);
 
92
    }
 
93
 
 
94
    lock_.wait(l, pred);
 
95
    return true;
 
96
}
 
97
 
 
98
bool BufferQueue::WaitToBeFilled(const std::chrono::milliseconds &timeout) {
 
99
    if (IsFull())
 
100
        return true;
 
101
 
 
102
    return WaitFor([&]() { return !queue_.empty(); }, timeout);
 
103
}
 
104
 
 
105
bool BufferQueue::WaitForSlots(const std::chrono::milliseconds &timeout) {
 
106
    if (!IsLimited())
 
107
        return true;
 
108
 
 
109
    return WaitFor([&]() { return queue_.size() < max_size_; }, timeout);
 
110
}
 
111
 
 
112
bool BufferQueue::IsFull() {
 
113
    if (!IsLimited())
 
114
        return false;
 
115
 
 
116
    std::unique_lock<std::mutex> l(mutex_);
 
117
    return queue_.size() == max_size_;
 
118
}
 
119
 
 
120
bool BufferQueue::IsEmpty() {
 
121
    std::unique_lock<std::mutex> l(mutex_);
 
122
    return queue_.size() == 0;
 
123
}
 
124
 
 
125
int BufferQueue::Size() {
 
126
    std::unique_lock<std::mutex> l(mutex_);
 
127
    return queue_.size();
 
128
}
 
129
 
 
130
} // namespace video
 
131
} // namespace mcs