[vlmc-devel] MetaDataWorker: Use vmem to compute media snapshot

Hugo Beauzée-Luyssen git at videolan.org
Sat Feb 15 23:54:48 CET 2014


vlmc | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Sun Feb 16 00:53:08 2014 +0200| [43d889bff862d545f2d3477e047d6398bd5f82ca] | committer: Hugo Beauzée-Luyssen

MetaDataWorker: Use vmem to compute media snapshot

> http://git.videolan.org/gitweb.cgi/vlmc.git/?a=commit;h=43d889bff862d545f2d3477e047d6398bd5f82ca
---

 src/Gui/media/GuiMedia.cpp      |   34 ++++++++--------------------------
 src/Gui/media/GuiMedia.h        |    6 +++---
 src/Metadata/MetaDataWorker.cpp |   33 ++++++++++++++++++++-------------
 src/Metadata/MetaDataWorker.h   |   11 ++++++++++-
 4 files changed, 41 insertions(+), 43 deletions(-)

diff --git a/src/Gui/media/GuiMedia.cpp b/src/Gui/media/GuiMedia.cpp
index 6f08112..ba370c7 100644
--- a/src/Gui/media/GuiMedia.cpp
+++ b/src/Gui/media/GuiMedia.cpp
@@ -25,45 +25,27 @@
 
 QPixmap*        GUIMedia::defaultSnapshot = NULL;
 
-GUIMedia::GUIMedia() :
-    m_snapshot( NULL )
+GUIMedia::GUIMedia()
 {
 }
 
-void GUIMedia::snapshotReady(const char *fileName)
+void GUIMedia::snapshotReady(const QImage* snapshot)
 {
-    QFile   tmp( fileName );
-
-    QPixmap* pixmap = new QPixmap( fileName );
-    if ( pixmap->isNull() )
-        delete pixmap;
-    else
-    {
-        setSnapshot( pixmap );
+    m_snapshot = QPixmap::fromImage( *snapshot );
+    if ( m_snapshot.isNull() == false )
         emit snapshotComputed( qobject_cast<const Media*>( this ) );
-    }
-    tmp.remove();
+    delete snapshot;
 }
 
 GUIMedia::~GUIMedia()
 {
-    if ( m_snapshot )
-        delete m_snapshot;
-}
-
-void
-GUIMedia::setSnapshot( QPixmap* snapshot )
-{
-    if ( m_snapshot != NULL )
-        delete m_snapshot;
-    m_snapshot = snapshot;
 }
 
 const QPixmap&
 GUIMedia::snapshot() const
 {
-    if ( m_snapshot != NULL )
-        return *m_snapshot;
+    if ( hasSnapshot() )
+        return m_snapshot;
     if ( GUIMedia::defaultSnapshot == NULL )
         GUIMedia::defaultSnapshot = new QPixmap( ":/images/vlmc" );
     return *GUIMedia::defaultSnapshot;
@@ -72,5 +54,5 @@ GUIMedia::snapshot() const
 bool
 GUIMedia::hasSnapshot() const
 {
-    return ( m_snapshot != NULL );
+    return !m_snapshot.isNull();
 }
diff --git a/src/Gui/media/GuiMedia.h b/src/Gui/media/GuiMedia.h
index 5034b34..9e38394 100644
--- a/src/Gui/media/GuiMedia.h
+++ b/src/Gui/media/GuiMedia.h
@@ -26,6 +26,7 @@
 #include <QObject>
 #include <QPixmap>
 
+class   QImage;
 class   QPixmap;
 class   Media;
 
@@ -35,7 +36,6 @@ class GUIMedia : public QObject
 
 public:
     ~GUIMedia();
-    void                        setSnapshot( QPixmap* snapshot );
     const QPixmap               &snapshot() const;
     bool                        hasSnapshot() const;
 
@@ -44,11 +44,11 @@ protected:
     GUIMedia();
 
     static QPixmap*             defaultSnapshot;
-    QPixmap*                    m_snapshot;
+    QPixmap                     m_snapshot;
 
 
 public slots:
-    void                        snapshotReady( const char *fileName );
+    void                        snapshotReady(const QImage *snapshot );
 
 signals:
     void                        snapshotComputed( const Media* );
diff --git a/src/Metadata/MetaDataWorker.cpp b/src/Metadata/MetaDataWorker.cpp
index 56a3660..e3b4f45 100644
--- a/src/Metadata/MetaDataWorker.cpp
+++ b/src/Metadata/MetaDataWorker.cpp
@@ -40,6 +40,7 @@
 MetaDataWorker::MetaDataWorker( LibVLCpp::MediaPlayer* mediaPlayer, Media* media ) :
         m_mediaPlayer( mediaPlayer ),
         m_media( media ),
+        m_snapshot( NULL ),
         m_audioBuffer( NULL )
 {
     connect( this, SIGNAL( finished() ), this, SLOT( deleteLater() ) );
@@ -66,12 +67,17 @@ MetaDataWorker::run()
 
     m_mediaPlayer->configureWaitForEvent( libvlc_MediaPlayerTimeChanged, cancel, &checkEvent );
 
-    m_media->vlcMedia()->addOption( ":vout=dummy" );
+    m_snapshot = new QImage( 320, 180, QImage::Format_RGB32 );
+
+    m_mediaPlayer->setupVmemCallbacks( vmemLock, NULL, NULL, this );
+    m_mediaPlayer->setupVmem( "RV32", m_snapshot->width(), m_snapshot->height(),
+                              m_snapshot->bytesPerLine() );
     // In VLC 2.x we can't set the volume before the playback has started
     // so just switch off the audio-output in any case.
     m_mediaPlayer->setAudioOutput( "dummy" );
     m_mediaPlayer->setMedia( m_media->vlcMedia() );
 
+
     m_mediaPlayer->play();
     LibVLCpp::MediaPlayer::EventWaitResult res = m_mediaPlayer->waitForEvent( 3000 );
     if ( res != LibVLCpp::MediaPlayer::Success )
@@ -149,6 +155,14 @@ MetaDataWorker::metaDataAvailable()
     emit computed();
 }
 
+void*
+MetaDataWorker::vmemLock(void *data, void **planes)
+{
+    MetaDataWorker* self = reinterpret_cast<MetaDataWorker*>( data );
+    *planes = self->m_snapshot->bits();
+    return self->m_snapshot;
+}
+
 #ifdef WITH_GUI
 void
 MetaDataWorker::computeSnapshot()
@@ -172,18 +186,11 @@ MetaDataWorker::computeSnapshot()
         emit failed( m_media );
         return ;
     }
-
-    QTemporaryFile tmp;
-    tmp.open();
-    // the snapshot file will be removed when processed by the media.
-    tmp.setAutoRemove( false );
-
-    // Although this function is synchrone, we have to be in the main thread to
-    // handle a QPixmap, hence the QueuedConnection
-    connect( m_mediaPlayer, SIGNAL( snapshotTaken( const char* ) ),
-             m_media, SLOT( snapshotReady( const char* ) ),
-             Qt::QueuedConnection );
-    m_mediaPlayer->takeSnapshot( tmp.fileName().toUtf8().constData(), 0, 0 );
+    m_mediaPlayer->stop();
+    // Since the snapshot is a QPixmap, we have to trigger this in the GUI thread
+    connect( this, SIGNAL( snapshotReady(const QImage*) ),
+             m_media, SLOT( snapshotReady(const QImage*) ), Qt::QueuedConnection );
+    emit snapshotReady( m_snapshot );
     emit computed();
 }
 #endif
diff --git a/src/Metadata/MetaDataWorker.h b/src/Metadata/MetaDataWorker.h
index bfc8598..b0eac0c 100644
--- a/src/Metadata/MetaDataWorker.h
+++ b/src/Metadata/MetaDataWorker.h
@@ -55,6 +55,9 @@ class MetaDataWorker : public QThread
 
     private:
         void                        metaDataAvailable();
+        static void                 vmemSetFormat( void* data, char* chroma, unsigned int* width,
+                                                   unsigned int* height, unsigned int* pitches, unsigned int* lines );
+        static void*                vmemLock( void* data, void **planes );
 //        static void                 lock( MetaDataWorker* metaDataWorker, quint8** pcm_buffer , unsigned int size );
 //        static void                 unlock( MetaDataWorker* metaDataWorker, quint8* pcm_buffer,
 //                                        unsigned int channels, unsigned int rate,
@@ -67,7 +70,7 @@ class MetaDataWorker : public QThread
     private:
         LibVLCpp::MediaPlayer*      m_mediaPlayer;
         Media*                      m_media;
-
+        QImage*                     m_snapshot;
         unsigned char*              m_audioBuffer;
 
 //    private slots:
@@ -75,6 +78,12 @@ class MetaDataWorker : public QThread
 
     signals:
         void    computed();
+        /**
+         * @brief snapshotReady Signals that a media now has a snapshot available
+         * @warning             This gives the ownership of the snapshot to the recipient.
+         *                      Memory will *NOT* be released by the MetaDataWorker
+         */
+        void    snapshotReady( const QImage* snapshot );
         void    failed( Media* media );
 };
 



More information about the Vlmc-devel mailing list