Commit 43d889bf authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

MetaDataWorker: Use vmem to compute media snapshot

parent 0d04715a
......@@ -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();
}
......@@ -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* );
......
......@@ -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
......
......@@ -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 );
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment