forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacketBuffer.cpp
152 lines (130 loc) · 3.87 KB
/
PacketBuffer.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2015 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include "PacketBuffer.h"
namespace QtAV {
PacketBuffer::PacketBuffer()
: m_mode(BufferTime)
, m_buffering(true) // in buffering state at the beginning
, m_max(1.5)
, m_buffer(0)
, m_value0(0)
, m_value1(0)
{
}
PacketBuffer::~PacketBuffer()
{
}
void PacketBuffer::setBufferMode(BufferMode mode)
{
m_mode = mode;
if (queue.isEmpty()) {
m_value0 = m_value1 = 0;
return;
}
if (m_mode == BufferTime) {
m_value0 = qint64(queue[0].pts*1000.0);
} else {
m_value0 = 0;
}
}
BufferMode PacketBuffer::bufferMode() const
{
return m_mode;
}
void PacketBuffer::setBufferValue(qint64 value)
{
m_buffer = value;
}
qint64 PacketBuffer::bufferValue() const
{
return m_buffer;
}
void PacketBuffer::setBufferMax(qreal max)
{
if (max < 1.0) {
qWarning("max (%f) must >= 1.0", max);
return;
}
m_max = max;
}
qreal PacketBuffer::bufferMax() const
{
return m_max;
}
qint64 PacketBuffer::buffered() const
{
Q_ASSERT(m_value1 >= m_value0);
return m_value1 - m_value0;
}
bool PacketBuffer::isBuffering() const
{
return m_buffering;
}
qreal PacketBuffer::bufferProgress() const
{
const qreal p = qreal(buffered())/qreal(bufferValue());
return qMax<qreal>(qMin<qreal>(p, 1.0), 0.0);
}
bool PacketBuffer::checkEnough() const
{
return buffered() >= bufferValue();
}
bool PacketBuffer::checkFull() const
{
return buffered() >= qint64(qreal(bufferValue())*bufferMax());
}
void PacketBuffer::onPut(const Packet &p)
{
if (m_mode == BufferTime) {
m_value1 = qint64(p.pts*1000.0); // FIXME: what if no pts
m_value0 = qint64(queue[0].pts*1000.0); // must compute here because it is reset to 0 if take from empty
//if (isBuffering())
// qDebug("+buffering progress: %.1f%%=%.1f/%.1f~%.1fs %d-%d", bufferProgress()*100.0, (qreal)buffered()/1000.0, (qreal)bufferValue()/1000.0, qreal(bufferValue())*bufferMax()/1000.0, m_value1, m_value0);
} else if (m_mode == BufferBytes) {
m_value1 += p.data.size();
} else {
m_value1++;
}
// TODO: compute buffer speed (and auto set the best bufferValue)
if (!m_buffering)
return;
if (checkEnough()) {
m_buffering = false;
}
}
void PacketBuffer::onTake(const Packet &p)
{
if (checkEmpty()) {
m_buffering = true;
}
if (queue.isEmpty()) {
m_value0 = 0;
m_value1 = 0;
return;
}
if (m_mode == BufferTime) {
m_value0 = qint64(queue[0].pts*1000.0);
//if (isBuffering())
// qDebug("-buffering progress: %.1f=%.1f/%.1fs", bufferProgress(), (qreal)buffered()/1000.0, (qreal)bufferValue()/1000.0);
} else if (m_mode == BufferBytes) {
m_value1 -= p.data.size();
m_value1 = qMax<qint64>(0LL, m_value1);
} else {
m_value1--;
}
}
} //namespace QtAV