[vlmc-devel] ThumbnailWorker: Asynchronous

Yikai Lu git at videolan.org
Tue Aug 16 04:17:26 CEST 2016


vlmc | branch: master | Yikai Lu <luyikei.qmltu at gmail.com> | Mon Aug 15 21:16:37 2016 -0500| [0ea2713b06d989e4834bc0517a2c00b12b7c0122] | committer: Yikai Lu

ThumbnailWorker: Asynchronous

> https://code.videolan.org/videolan/vlmc/commit/0ea2713b06d989e4834bc0517a2c00b12b7c0122
---

 src/Workflow/MainWorkflow.cpp    | 16 +++++++++++-----
 src/Workflow/ThumbnailWorker.cpp | 19 ++++++++++++-------
 src/Workflow/ThumbnailWorker.h   | 11 +++++++++--
 3 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/src/Workflow/MainWorkflow.cpp b/src/Workflow/MainWorkflow.cpp
index 4e4ca0f..d265e38 100644
--- a/src/Workflow/MainWorkflow.cpp
+++ b/src/Workflow/MainWorkflow.cpp
@@ -302,12 +302,18 @@ MainWorkflow::addEffect( const QString &clipUuid, const QString &effectId )
 void
 MainWorkflow::takeThumbnail( const QString& uuid, quint32 pos )
 {
-    ThumbnailWorker worker;
-    connect( &worker, &ThumbnailWorker::imageReady, this, &MainWorkflow::thumbnailUpdated, Qt::DirectConnection );
     auto clip = m_sequenceWorkflow->clip( uuid );
-    worker.run( uuid, clip->media()->fileInfo()->absoluteFilePath(),
-                clip->begin() + pos, clip->input()->width(),
-                clip->input()->height() );
+    auto worker = new ThumbnailWorker( uuid, clip->media()->fileInfo()->absoluteFilePath(),
+                                       clip->begin() + pos, clip->input()->width(),
+                                       clip->input()->height() );
+    auto t = new QThread;
+    worker->moveToThread( t );
+    connect( t, &QThread::started, worker, &ThumbnailWorker::run );
+    connect( worker, &ThumbnailWorker::imageReady, this, &MainWorkflow::thumbnailUpdated, Qt::DirectConnection );
+    connect( worker, &ThumbnailWorker::imageReady, t, &QThread::quit );
+    connect( t, &QThread::finished, worker, &ThumbnailWorker::deleteLater );
+    connect( t, &QThread::finished, t, &QThread::deleteLater );
+    t->start();
 }
 
 bool
diff --git a/src/Workflow/ThumbnailWorker.cpp b/src/Workflow/ThumbnailWorker.cpp
index e454957..9635f4c 100644
--- a/src/Workflow/ThumbnailWorker.cpp
+++ b/src/Workflow/ThumbnailWorker.cpp
@@ -5,22 +5,27 @@
 
 #include "Backend/MLT/MLTInput.h"
 
-ThumbnailWorker::ThumbnailWorker( QObject* parent )
+ThumbnailWorker::ThumbnailWorker( const QString& uuid, const QString& filePath, qint64 pos, quint32 width, quint32 height, QObject* parent )
     : QObject( parent )
+    , m_uuid( uuid )
+    , m_filePath( filePath )
+    , m_pos( pos )
+    , m_width( width )
+    , m_height( height )
 {
 
 }
 
 void
-ThumbnailWorker::run( const QString& uuid, const QString& filePath, qint64 pos, quint32 width, quint32 height )
+ThumbnailWorker::run()
 {
-    Backend::MLT::MLTInput input( qPrintable( filePath ) );
-    input.setPosition( pos );
-    auto image = input.image( width, height );
-    QImage qImg( image, width, height,
+    Backend::MLT::MLTInput input( qPrintable( m_filePath ) );
+    input.setPosition( m_pos );
+    auto image = input.image( m_width, m_height );
+    QImage qImg( image, m_width, m_height,
                 QImage::Format_RGBA8888, []( void* buf ){ delete[] (uchar*) buf; } );
     auto qPix = QPixmap::fromImage( qImg );
 
     // Use Qt::DirectConnection or the pixmap will be deleted!
-    emit imageReady( uuid, pos, qPix );
+    emit imageReady( m_uuid, m_pos, qPix );
 }
diff --git a/src/Workflow/ThumbnailWorker.h b/src/Workflow/ThumbnailWorker.h
index e055734..7c32bc5 100644
--- a/src/Workflow/ThumbnailWorker.h
+++ b/src/Workflow/ThumbnailWorker.h
@@ -9,13 +9,20 @@ class ThumbnailWorker : public QObject
 {
     Q_OBJECT
 public:
-    explicit ThumbnailWorker( QObject* parent = 0 );
+    explicit ThumbnailWorker( const QString& uuid, const QString& filePath, qint64 pos, quint32 width, quint32 height , QObject* parent = 0 );
 
 signals:
     void    imageReady( const QString& uuid, qint64 pos, const QPixmap& pixmap );
 
 public slots:
-    void    run( const QString& uuid, const QString& filePath, qint64 pos, quint32 width, quint32 height );
+    void    run();
+
+private:
+    QString     m_uuid;
+    QString     m_filePath;
+    qint64      m_pos;
+    quint32     m_width;
+    quint32     m_height;
 };
 
 #endif // THUMBNAILWORKER_H



More information about the Vlmc-devel mailing list