Skip to content

Commit

Permalink
subtitle: thread safe. async load in qml
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Sep 8, 2014
1 parent 656ed20 commit 8b66627
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion qml/QuickSubtitle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void QuickSubtitle::onPlayerSourceChanged()
path = QFileInfo(path).dir().absoluteFilePath(name);
qDebug() << "open subtitle:" << path;
setFileName(path);
load();
loadAsync();
}

void QuickSubtitle::onPlayerPositionChanged()
Expand Down
1 change: 1 addition & 0 deletions src/QtAV/Subtitle.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public slots:
* start to process the whole subtitle content in a thread
*/
void load();
void loadAsync();
void setTimestamp(qreal t);
signals:
void codecChanged();
Expand Down
42 changes: 36 additions & 6 deletions src/Subtitle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
#include <QtCore/QLinkedList>
#include <QtCore/QRegExp>
#include <QtCore/QRunnable>
#include <QtCore/QThreadPool>
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
#include <QtCore/QMutexLocker>

namespace QtAV {

Expand All @@ -48,6 +50,9 @@ class Subtitle::Private {
, t(0)
{}
void reset() {
QMutexLocker lock(&mutex);
Q_UNUSED(lock);
loaded = false;
processor = 0;
update_image = true;
t = 0;
Expand Down Expand Up @@ -93,6 +98,7 @@ class Subtitle::Private {
qreal t;
SubtitleFrame frame;
QLinkedList<SubtitleFrame>::iterator itf;
QMutex mutex;
};

Subtitle::Subtitle(QObject *parent) :
Expand Down Expand Up @@ -239,12 +245,16 @@ QStringList Subtitle::suffixes() const

void Subtitle::setTimestamp(qreal t)
{
priv->t = t;
if (!isLoaded())
return;
if (!priv->prepareCurrentFrame())
return;
priv->update_image = true;
{
QMutexLocker lock(&priv->mutex);
Q_UNUSED(lock);
priv->t = t;
if (!isLoaded())
return;
if (!priv->prepareCurrentFrame())
return;
priv->update_image = true;
}
emit contentChanged();
}

Expand All @@ -257,6 +267,7 @@ void Subtitle::load()
{
priv->reset();
emit contentChanged(); //notify user to update subtitle
// lock is not needed because it's not loaded now
if (!priv->url.isEmpty()) {
// need qt network module network
return;
Expand Down Expand Up @@ -293,15 +304,34 @@ void Subtitle::load()
}
}

void Subtitle::loadAsync()
{
class Loader : public QRunnable {
public:
Loader(Subtitle *sub) : m_sub(sub) {}
void run() {
if (m_sub)
m_sub->load();
}
private:
Subtitle *m_sub;
};
QThreadPool::globalInstance()->start(new Loader(this));
}

QString Subtitle::getText() const
{
QMutexLocker lock(&priv->mutex);
Q_UNUSED(lock);
if (!isLoaded())
return QString();
return priv->frame.text;
}

QImage Subtitle::getImage(int width, int height)
{
QMutexLocker lock(&priv->mutex);
Q_UNUSED(lock);
if (!isLoaded())
return QImage();
if (!priv->frame || width == 0 || height == 0)
Expand Down

0 comments on commit 8b66627

Please sign in to comment.