Skip to content

Commit

Permalink
use semphore in AVThread.waitForStarted
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed May 12, 2016
1 parent 25e0ff0 commit 8bc7603
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
14 changes: 13 additions & 1 deletion src/AVDemuxThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@

namespace QtAV {

class AutoSem {
QSemaphore *s;
public:
AutoSem(QSemaphore* sem) : s(sem) { s->release();}
~AutoSem() {
if (s->available() > 0)
s->release(s->available());
}
};

class QueueEmptyCall : public PacketBuffer::StateChangeCallback
{
public:
Expand Down Expand Up @@ -524,7 +534,9 @@ void AVDemuxThread::run()
}
qreal last_apts = 0;
qreal last_vpts = 0;
sem.release();

AutoSem as(&sem);
Q_UNUSED(as);
while (!end) {
processNextSeekTask();
//vthread maybe changed by AVPlayer.setPriority() from no dec case
Expand Down
6 changes: 4 additions & 2 deletions src/AVPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1286,12 +1286,10 @@ void AVPlayer::playInternal()
if (d->demuxer.audioCodecContext() && d->athread) {
qDebug("Starting audio thread...");
d->athread->start();
d->athread->waitForReady();
}
if (d->demuxer.videoCodecContext() && d->vthread) {
qDebug("Starting video thread...");
d->vthread->start();
d->vthread->waitForReady();
}
if (startPosition() > 0 && startPosition() < mediaStopPosition() && d->last_position <= 0) {
if (relativeTimeMode())
Expand All @@ -1302,6 +1300,10 @@ void AVPlayer::playInternal()
d->read_thread->setMediaEndAction(mediaEndAction());
d->read_thread->start();

if (d->demuxer.audioCodecContext() && d->athread)
d->athread->waitForStarted();
if (d->demuxer.videoCodecContext() && d->vthread)
d->vthread->waitForStarted();
/// demux thread not started, seek tasks will be cleared
d->read_thread->waitForStarted();
if (d->timer_id < 0) {
Expand Down
16 changes: 4 additions & 12 deletions src/AVThread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV
Expand All @@ -20,6 +20,7 @@
******************************************************************************/

#include "AVThread.h"
#include <limits>
#include "AVThread_p.h"
#include "QtAV/AVClock.h"
#include "QtAV/AVDecoder.h"
Expand All @@ -41,7 +42,6 @@ AVThreadPrivate::~AVThreadPrivate() {
next_pause = false;
cond.wakeAll();
}
ready_cond.wakeAll();
packets.setBlocking(true); //???
packets.clear();
QList<Filter*>::iterator it = filters.begin();
Expand Down Expand Up @@ -197,8 +197,6 @@ void AVThread::stop()
d.packets.setBlocking(false); //stop blocking take()
d.packets.clear();
pause(false);
QMutexLocker lock(&d.ready_mutex);
d.ready = false;
//terminate();
}

Expand Down Expand Up @@ -305,9 +303,6 @@ void AVThread::resetState()
d.stop = false;
d.packets.setBlocking(true);
d.packets.clear();
QMutexLocker lock(&d.ready_mutex);
d.ready = true;
d.ready_cond.wakeOne();
}

bool AVThread::tryPause(unsigned long timeout)
Expand Down Expand Up @@ -341,12 +336,9 @@ void AVThread::setStatistics(Statistics *statistics)
d.statistics = statistics;
}

void AVThread::waitForReady()
bool AVThread::waitForStarted(int msec)
{
QMutexLocker lock(&d_func().ready_mutex);
while (!d_func().ready) {
d_func().ready_cond.wait(&d_func().ready_mutex);
}
return d_func().sem.tryAcquire(1, msec > 0 ? msec : std::numeric_limits<int>::max());
}

void AVThread::waitAndCheck(ulong value, qreal pts)
Expand Down
4 changes: 2 additions & 2 deletions src/AVThread.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -67,7 +67,7 @@ class AVThread : public QThread

bool isPaused() const;

void waitForReady();
bool waitForStarted(int msec = -1);

bool installFilter(Filter *filter, int index = 0x7FFFFFFF, bool lock = true);
bool uninstallFilter(Filter *filter, bool lock = true);
Expand Down
15 changes: 11 additions & 4 deletions src/AVThread_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include <QtCore/QSemaphore>
#include <QtCore/QVariant>
#include <QtCore/QWaitCondition>
#include "PacketBuffer.h"
Expand All @@ -36,6 +37,15 @@ namespace QtAV {

const double kSyncThreshold = 0.2; // 200 ms

class AutoSem {
QSemaphore *s;
public:
AutoSem(QSemaphore* sem) : s(sem) { s->release();}
~AutoSem() {
if (s->available() > 0)
s->release(s->available());
}
};
class AVDecoder;
class AVOutput;
class AVClock;
Expand All @@ -54,7 +64,6 @@ class AVThreadPrivate : public DPtrPrivate<AVThread>
, outputSet(0)
, delay(0)
, statistics(0)
, ready(false)
, seek_requested(false)
, render_pts0(-1)
, drop_frame_seek(true)
Expand Down Expand Up @@ -82,9 +91,7 @@ class AVThreadPrivate : public DPtrPrivate<AVThread>
QList<Filter*> filters;
Statistics *statistics; //not obj. Statistics is unique for the player, which is in AVPlayer
BlockingQueue<QRunnable*> tasks;
QWaitCondition ready_cond;
QMutex ready_mutex;
bool ready;
QSemaphore sem;
bool seek_requested;
//only decode video without display or skip decode audio until pts reaches
qreal render_pts0;
Expand Down
4 changes: 3 additions & 1 deletion src/AudioThread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -75,6 +75,8 @@ void AudioThread::applyFilters(AudioFrame &frame)
void AudioThread::run()
{
DPTR_D(AudioThread);
AutoSem as(&d.sem);
Q_UNUSED(as);
//No decoder or output. No audio output is ok, just display picture
if (!d.dec || !d.dec->isAvailable() || !d.outputSet)
return;
Expand Down
4 changes: 2 additions & 2 deletions src/AudioThread.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2013 Wang Bin <[email protected]>
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down
2 changes: 2 additions & 0 deletions src/VideoThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ bool VideoThread::deliverVideoFrame(VideoFrame &frame)
void VideoThread::run()
{
DPTR_D(VideoThread);
AutoSem as(&d.sem);
Q_UNUSED(as);
if (!d.dec || !d.dec->isAvailable() || !d.outputSet)
return;
resetState();
Expand Down

0 comments on commit 8bc7603

Please sign in to comment.