Skip to content

Commit

Permalink
fix buffering signal emit too late
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Apr 17, 2015
1 parent 4d23ed5 commit 4c15e89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
36 changes: 19 additions & 17 deletions src/AVDemuxThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 43,7 @@ class QueueEmptyCall : public PacketBuffer::StateChangeCallback
return;
if (mDemuxThread->isEnd())
return;
mDemuxThread->updateBufferState(); // ensure detect buffering immediately
AVThread *thread = mDemuxThread->videoThread();
//qDebug("try wake up video queue");
if (thread)
Expand All @@ -62,7 63,6 @@ AVDemuxThread::AVDemuxThread(QObject *parent) :
, user_paused(false)
, end(false)
, m_buffering(false)
, m_buffered(0)
, m_buffer(0)
, demuxer(0)
, audio_thread(0)
Expand All @@ -79,7 79,6 @@ AVDemuxThread::AVDemuxThread(AVDemuxer *dmx, QObject *parent) :
, paused(false)
, end(false)
, m_buffering(false)
, m_buffered(0)
, m_buffer(0)
, audio_thread(0)
, video_thread(0)
Expand Down Expand Up @@ -232,6 231,23 @@ PacketBuffer* AVDemuxThread::buffer()
return m_buffer;
}

void AVDemuxThread::updateBufferState()
{
if (!m_buffer)
return;
if (m_buffering) { // always report progress when buffering
Q_EMIT bufferProgressChanged(m_buffer->bufferProgress());
}
if (m_buffering == m_buffer->isBuffering())
return;
m_buffering = m_buffer->isBuffering();
Q_EMIT mediaStatusChanged(m_buffering ? QtAV::BufferingMedia : QtAV::BufferedMedia);
// state change to buffering, report progress immediately. otherwise we have to wait to read 1 packet.
if (m_buffering) {
Q_EMIT bufferProgressChanged(m_buffer->bufferProgress());
}
}

//No more data to put. So stop blocking the queue to take the reset elements
void AVDemuxThread::stop()
{
Expand Down Expand Up @@ -363,7 379,6 @@ void AVDemuxThread::onAVThreadQuit()
void AVDemuxThread::run()
{
m_buffering = false;
m_buffered = false;
end = false;
if (audio_thread && !audio_thread->isRunning())
audio_thread->start(QThread::HighPriority);
Expand Down Expand Up @@ -407,13 422,7 @@ void AVDemuxThread::run()
if (tryPause()) {
continue; //the queue is empty and will block
}
if (m_buffering != m_buffer->isBuffering()) {
m_buffering = m_buffer->isBuffering();
Q_EMIT mediaStatusChanged(m_buffering ? QtAV::BufferingMedia : QtAV::BufferedMedia);
// state change to buffering, report progress immediatly. otherwise we have to wait to read 1 packet.
if (m_buffering)
Q_EMIT bufferProgressChanged(m_buffer->bufferProgress());
}
updateBufferState();
QMutexLocker locker(&buffer_mutex);
Q_UNUSED(locker);
if (!demuxer->readFrame()) {
Expand Down Expand Up @@ -466,15 475,8 @@ void AVDemuxThread::run()
} else { //subtitle
continue;
}
if (m_buffering) {
if (m_buffered != m_buffer->buffered()) {
m_buffered = m_buffer->buffered();
Q_EMIT bufferProgressChanged(m_buffer->bufferProgress());
}
}
}
m_buffering = false;
m_buffered = false;
m_buffer = 0;
while (audio_thread && audio_thread->isRunning()) {
qDebug("waiting audio thread.......");
Expand Down
3 changes: 1 addition & 2 deletions src/AVDemuxThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 49,7 @@ class AVDemuxThread : public QThread
bool isPaused() const;
bool isEnd() const;
PacketBuffer* buffer();

void updateBufferState();
public slots:
void stop(); //TODO: remove it?
void pause(bool p);
Expand Down Expand Up @@ -84,7 84,6 @@ private slots:
bool user_paused;
volatile bool end;
bool m_buffering;
int m_buffered;
PacketBuffer *m_buffer;
AVDemuxer *demuxer;
AVThread *audio_thread, *video_thread;
Expand Down

0 comments on commit 4c15e89

Please sign in to comment.