Skip to content

Commit

Permalink
add buffering state in the queue
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Mar 17, 2015
1 parent 9d1bdb7 commit 5d2d12f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/PacketBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace QtAV {

PacketBuffer::PacketBuffer()
: m_mode(BufferTime)
, m_buffering(true) // in buffering state at the beginning
, m_buffer(0)
, m_value0(0)
, m_value1(0)
Expand Down Expand Up @@ -70,6 +71,11 @@ int PacketBuffer::buffered() const
return m_value1 - m_value0;
}

bool PacketBuffer::isBuffering() const
{
return m_buffering;
}

qreal PacketBuffer::bufferProgress() const
{
const qreal p = qreal(buffered())/qreal(bufferValue());
Expand All @@ -78,12 +84,12 @@ qreal PacketBuffer::bufferProgress() const

bool PacketBuffer::checkEnough() const
{
return m_value1 - m_value0 >= m_buffer;
return buffered() >= bufferValue();
}

bool PacketBuffer::checkFull() const
{
return m_value1 - m_value0 >= 2*m_buffer;
return buffered() >= 2*bufferValue();
}

void PacketBuffer::onPut(const Packet &p)
Expand All @@ -95,10 +101,19 @@ void PacketBuffer::onPut(const Packet &p)
} 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;
Expand Down
11 changes: 10 additions & 1 deletion src/PacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@

namespace QtAV {

/*
* take empty: start buffering, block at next take if still empty
* take enough: start to put more packets
* put enough: end buffering, end take block
* put full: stop putting more packets
*/
class PacketBuffer : BlockingQueue<Packet, QQueue>
{
public:
Expand Down Expand Up @@ -55,13 +61,15 @@ class PacketBuffer : BlockingQueue<Packet, QQueue>
* Current buffered value in the queue
*/
int buffered() const;
bool isBuffering() const;
/*!
* \brief bufferProgress
* How much of the data buffer is currently filled, from 0.0 (empty) to 1.0 (enough or full).
* bufferProgress() == 1
* bufferProgress() can be less than 1 if not isBuffering();
* \return Percent of buffered time, bytes or packets.
*/
qreal bufferProgress() const;
qreal bufferSpeed() const;

protected:
bool checkEnough() const Q_DECL_OVERRIDE;
Expand All @@ -70,6 +78,7 @@ class PacketBuffer : BlockingQueue<Packet, QQueue>
void onPut(const Packet &) Q_DECL_OVERRIDE;
private:
BufferMode m_mode;
bool m_buffering;
// bytes or count
quint32 m_buffer;
qint32 m_value0, m_value1;
Expand Down

0 comments on commit 5d2d12f

Please sign in to comment.