Commit b020885f authored by Geoffroy Lacarriere's avatar Geoffroy Lacarriere

Snapshot refactoring. Only one mediaplayer and vlc instance used.

parent 7e953224
......@@ -72,24 +72,24 @@ void InputMedia::unlock( LibVLCpp::Media::DataCtx* ctx )
ctx->mutex->unlock();
}
QPixmap* InputMedia::takeSnapshot( unsigned int width, unsigned int height )
{
// qint64 currentTime = m_vlcMediaPlayer->getTime();
// qint64 length = getLength();
// qDebug() << currentTime << length;
// m_vlcMediaPlayer->setTime(length / 2);
// qDebug() << "trying to take a snapshot";
QTemporaryFile tmp;
tmp.open();
char* tmpStr = const_cast<char*>(tmp.fileName().toStdString().c_str());
m_vlcMediaPlayer->takeSnapshot( tmpStr, width, height );
// qDebug() << "done snapshoting";
return new QPixmap( tmp.fileName() );
// qDebug() << "written to a QImage";
// m_vlcMediaPlayer->setTime(currentTime);
}
//QPixmap* InputMedia::takeSnapshot( unsigned int width, unsigned int height )
//{
//// qint64 currentTime = m_vlcMediaPlayer->getTime();
//// qint64 length = getLength();
//// qDebug() << currentTime << length;
//// m_vlcMediaPlayer->setTime(length / 2);
//
//
//// qDebug() << "trying to take a snapshot";
// QTemporaryFile tmp;
// tmp.open();
// char* tmpStr = const_cast<char*>(tmp.fileName().toStdString().c_str());
// m_vlcMediaPlayer->takeSnapshot( tmpStr, width, height );
//// qDebug() << "done snapshoting";
// return new QPixmap( tmp.fileName() );
//// qDebug() << "written to a QImage";
//// m_vlcMediaPlayer->setTime(currentTime);
//}
bool InputMedia::isSeekable()
{
......@@ -115,3 +115,10 @@ void InputMedia::stop()
{
Media::stop();
}
bool InputMedia::isReady()
{
if (m_vlcMediaPlayer->isPlaying() && m_vlcMediaPlayer->isSeekable())
return true;
return false;
}
......@@ -43,7 +43,7 @@ public:
static void lock( LibVLCpp::Media::DataCtx* dataCtx, void **pp_ret );
static void unlock( LibVLCpp::Media::DataCtx* dataCtx );
QPixmap* takeSnapshot( unsigned int width, unsigned int heigth );
//QPixmap* takeSnapshot( unsigned int width, unsigned int heigth );
/**
* Ask libvlc if the media can be seeked
......
......@@ -26,6 +26,25 @@
using namespace LibVLCpp;
Instance* Instance::m_singleton = NULL;
Instance* Instance::m_instance = NULL;
Instance::Instance()
{
char const *argv[] =
{
//"-vvvvv",
"--no-skip-frames",
"--no-audio",
//"--plugin-path", VLC_TREE "/modules",
//"--ignore-config", //Don't use VLC's config files
};
int argc = sizeof( argv ) / sizeof( *argv );
m_internalPtr = libvlc_new( argc, argv, m_ex );
m_ex.checkThrow();
}
Instance::Instance( int argc, const char** argv )
{
m_internalPtr = libvlc_new( argc, argv, m_ex );
......
......@@ -31,10 +31,45 @@ namespace LibVLCpp
{
class Instance : public Internal< libvlc_instance_t >
{
public:
private:
Instance();
Instance( int argc, const char** argv );
private:
Exception m_ex;
Exception m_ex;
static Instance* m_singleton;
static Instance* m_instance;
public:
static Instance* getNewInstance( int argc, const char** argv )
{
m_instance = new Instance( argc, argv );
return m_instance;
}
static Instance* getInstance()
{
if (NULL == m_singleton)
m_singleton = new Instance();
return m_singleton;
}
static void kill()
{
if ( m_singleton != NULL )
{
delete m_singleton;
m_singleton = NULL;
}
if ( m_instance != NULL )
{
delete m_instance;
m_instance = NULL;
}
}
};
}
......
......@@ -23,9 +23,28 @@
#include <QtDebug>
#include <cassert>
#include "VLCMediaPlayer.h"
#include "VLCInstance.h"
using namespace LibVLCpp;
MediaPlayer::MediaPlayer()
{
m_internalPtr = libvlc_media_player_new( LibVLCpp::Instance::getInstance()->getInternalPtr(), m_ex );
m_ex.checkThrow();
// Initialize the event manager
p_em = libvlc_media_player_event_manager( m_internalPtr, m_ex );
// Register the callback
libvlc_event_attach( p_em, libvlc_MediaPlayerSnapshotTaken, callbacks, this, m_ex );
libvlc_event_attach( p_em, libvlc_MediaPlayerTimeChanged, callbacks, this, m_ex );
libvlc_event_attach( p_em, libvlc_MediaPlayerPlaying, callbacks, this, m_ex );
libvlc_event_attach( p_em, libvlc_MediaPlayerPaused, callbacks, this, m_ex );
libvlc_event_attach( p_em, libvlc_MediaPlayerStopped, callbacks, this, m_ex );
libvlc_event_attach( p_em, libvlc_MediaPlayerEndReached, callbacks, this, m_ex );
libvlc_event_attach( p_em, libvlc_MediaPlayerPositionChanged, callbacks, this, m_ex );
}
MediaPlayer::MediaPlayer( Media* media, bool playStop /* = true*/ )
{
m_internalPtr = libvlc_media_player_new_from_media( media->getInternalPtr(), m_ex );
......@@ -194,3 +213,8 @@ void MediaPlayer::timeChangedFilter()
emit timeChanged();
lastTime = currentTime;
}
void MediaPlayer::setMedia( Media* media )
{
libvlc_media_player_set_media( m_internalPtr, media->getInternalPtr(), m_ex);
}
......@@ -38,6 +38,7 @@ namespace LibVLCpp
{
Q_OBJECT
public:
MediaPlayer();
MediaPlayer( Media* media, bool playStop = true );
void play();
void pause();
......@@ -52,6 +53,7 @@ namespace LibVLCpp
bool isSeekable();
void setDrawable( void* hwnd );
void setDrawable( uint32_t drawable );
void setMedia(Media* media);
private:
static void callbacks( const libvlc_event_t* event, void* self );
......
......@@ -25,7 +25,7 @@
Media::Media(LibVLCpp::Instance* instance, const QString& mrl )
: m_instance( NULL ), m_vlcMedia( NULL ), m_vlcMediaPlayer( NULL ),
m_mrl( mrl ), m_instanceOwned( false )
m_mrl( mrl ), m_snapshot( NULL ), m_instanceOwned( false )
{
if ( !instance )
{
......@@ -38,7 +38,7 @@ Media::Media(LibVLCpp::Instance* instance, const QString& mrl )
//"--ignore-config", //Don't use VLC's config files
};
int vlc_argc = sizeof( vlc_argv ) / sizeof( *vlc_argv );
instance = new LibVLCpp::Instance( vlc_argc, vlc_argv );
instance = LibVLCpp::Instance::getNewInstance( vlc_argc, vlc_argv );
m_instanceOwned = true;
}
m_instance = instance;
......@@ -73,15 +73,15 @@ void Media::loadMedia( const QString& mrl )
void Media::setupMedia()
{
if ( m_vlcMediaPlayer )
delete m_vlcMediaPlayer;
// if ( m_vlcMediaPlayer )
// delete m_vlcMediaPlayer;
//Flushing the args into the media :
QString param;
foreach ( param, m_parameters )
m_vlcMedia->addOption( param.toStdString().c_str() );
m_vlcMediaPlayer = new LibVLCpp::MediaPlayer( m_vlcMedia );
// m_vlcMediaPlayer = new LibVLCpp::MediaPlayer( m_vlcMedia );
}
void Media::play()
......@@ -144,3 +144,17 @@ void Media::setPosition( float pos )
{
m_vlcMediaPlayer->setPosition( pos );
}
void Media::setSnapshot( QPixmap* snapshot )
{
//TODO: check for mem leaks.
m_snapshot = snapshot;
}
const QPixmap& Media::getSnapshot() const
{
if ( m_snapshot )
return *m_snapshot;
//TODO: instanciate this as a static pixmap
return QPixmap( ":/images/images/vlmc.png" );
}
......@@ -47,6 +47,8 @@ public:
void setupMedia();
void setDrawable( WId handle );
LibVLCpp::MediaPlayer* mediaPlayer() { return m_vlcMediaPlayer; }
LibVLCpp::Media* getVLCMedia() { return m_vlcMedia; }
void setMediaPlayer( LibVLCpp::MediaPlayer* mediaPlayer ) { m_vlcMediaPlayer = mediaPlayer; }
/**
* Return the length (duration) of a Media
*/
......@@ -72,6 +74,9 @@ public:
*/
void setPosition( float pos );
void setSnapshot( QPixmap* snapshot );
const QPixmap& getSnapshot() const;
protected:
//Protected constructor so we can't use a Media without its sub-implementation
Media( LibVLCpp::Instance* instance, const QString& mrl );
......@@ -81,6 +86,8 @@ protected:
LibVLCpp::MediaPlayer* m_vlcMediaPlayer;
QString m_mrl;
QList<QString> m_parameters;
QPixmap* m_snapshot;
private:
bool m_instanceOwned;
};
......
#include "MetaDataManager.h"
MetaDataManager::MetaDataManager( LibraryWidget* libraryWidget ) : m_libraryWidget( libraryWidget ), m_renderWidget( NULL )
{
m_mediaPlayer = new LibVLCpp::MediaPlayer();
connect( m_libraryWidget, SIGNAL( listViewMediaAdded( ListViewMediaItem* ) ), this, SLOT( listViewMediaAdded( ListViewMediaItem* ) ) );
m_tmpSnapshotFilename = new char[512];
m_renderWidget = new QWidget();
}
MetaDataManager::~MetaDataManager()
{
if (m_mediaPlayer)
delete m_mediaPlayer;
if (m_renderWidget)
delete m_renderWidget;
delete[] m_tmpSnapshotFilename;
}
void MetaDataManager::listViewMediaAdded( ListViewMediaItem* item )
{
m_mediaList.append( item );
if ( !isRunning() )
start();
}
void MetaDataManager::run()
{
m_nextMedia = true;
// if ( !m_renderWidget )
m_mediaPlayer->setDrawable( m_renderWidget->winId() );
while ( !m_mediaList.isEmpty() )
{
if ( m_nextMedia )
{
m_nextMedia = false;
m_currentMediaItem = m_mediaList.front();
m_mediaList.pop_front();
m_currentMediaItem->getInputMedia()->setMediaPlayer( m_mediaPlayer );
m_mediaPlayer->setMedia( m_currentMediaItem->getInputMedia()->getVLCMedia() );
connect( m_mediaPlayer, SIGNAL( playing() ), this, SLOT( renderSnapshot() ) );
m_mediaPlayer->play();
}
usleep( 100 );
}
return;
}
void MetaDataManager::renderSnapshot()
{
m_currentMediaItem->getInputMedia()->setTime( m_currentMediaItem->getInputMedia()->getLength() / 3 );
QTemporaryFile tmp;
tmp.setAutoRemove( false );
tmp.open();
strncpy(m_tmpSnapshotFilename, tmp.fileName().toStdString().c_str(), tmp.fileName().length());
connect( m_mediaPlayer, SIGNAL( snapshotTaken() ), this, SLOT( setSnapshotInIcon() ) );
sleep(1);
//The slot should be triggered in this methode
m_mediaPlayer->takeSnapshot( m_tmpSnapshotFilename, 32, 32 );
//Snapshot slot should has been called (but maybe not in next version...)
}
void MetaDataManager::setSnapshotInIcon()
{
m_currentMediaItem->getInputMedia()->setSnapshot( new QPixmap( m_tmpSnapshotFilename ) );
m_currentMediaItem->setIcon( QIcon( m_currentMediaItem->getInputMedia()->getSnapshot() ) );
m_nextMedia = true;
disconnect( this, SLOT( setSnapshotInIcon() ) );
m_mediaPlayer->stop();
}
#ifndef METADATAMANAGER_H
#define METADATAMANAGER_H
#include <QList>
#include <QTemporaryFile>
#include <QThread>
#include "gui/LibraryWidget.h"
#include "VLCMediaPlayer.h"
class MetaDataManager : public QThread
{
Q_OBJECT
Q_DISABLE_COPY( MetaDataManager )
public:
MetaDataManager( LibraryWidget* libraryWidget );
~MetaDataManager();
private:
virtual void run();
private:
LibraryWidget* m_libraryWidget;
LibVLCpp::MediaPlayer* m_mediaPlayer;
QWidget* m_renderWidget;
// TODO: THREAD SAFING
QList<ListViewMediaItem*> m_mediaList;
// Thread component
bool m_nextMedia;
ListViewMediaItem* m_currentMediaItem;
char* m_tmpSnapshotFilename;
private slots:
void listViewMediaAdded( ListViewMediaItem* );
void renderSnapshot();
void setSnapshotInIcon();
};
#endif // METADATAMANAGER_H
......@@ -54,6 +54,7 @@ LibraryWidget::~LibraryWidget()
ListViewMediaItem* LibraryWidget::addMedia( QFileInfo* fileInfo, ListViewMediaItem::fType fileType )
{
ListViewMediaItem* item = new ListViewMediaItem( fileInfo, fileType );
emit listViewMediaAdded( item );
m_medias->append( item );
switch ( fileType )
{
......
......@@ -60,6 +60,9 @@ private:
private slots:
void on_pushButtonAddMedia_clicked();
void on_pushButtonRemoveMedia_clicked();
signals:
void listViewMediaAdded(ListViewMediaItem* item);
};
#endif /* !LIBRARYWIDGET_H */
......@@ -25,59 +25,24 @@
#include <QDebug>
ListViewMediaItem::ListViewMediaItem( QFileInfo* fInfo, ListViewMediaItem::fType fType, QListWidget* parent, int type ) :
QListWidgetItem( parent, type ), m_fileInfo( NULL ), m_currentMedia( NULL ), m_currentMediaSnapshot( NULL )
QListWidgetItem( parent, type ), m_fileInfo( NULL ), m_inputMedia( NULL )
{
m_fileInfo = fInfo;
m_fileType = fType;
setIcon( QIcon( ":/images/images/vlmc.png" ) );
setIcon( QIcon( ":/images/images/vlmc.png") );
setText( fInfo->baseName() );
m_renderWidget = new QWidget();
m_currentMedia = new InputMedia( "file://" + fInfo->absoluteFilePath() );
m_currentMedia->setupMedia();
m_currentMedia->setDrawable( m_renderWidget->winId() );
connect( m_currentMedia->mediaPlayer(), SIGNAL( playing() ), this, SLOT( setSnapshot() ) );
m_currentMedia->play();
m_inputMedia = new InputMedia( "file://" + fInfo->absoluteFilePath(), LibVLCpp::Instance::getInstance() );
m_inputMedia->setupMedia();
}
ListViewMediaItem::~ListViewMediaItem()
{
if ( m_fileInfo != NULL )
delete m_fileInfo;
if ( m_currentMedia->isPlaying() )
m_currentMedia->stop();
if ( m_renderWidget != NULL )
delete m_renderWidget;
if ( m_currentMedia != NULL )
delete m_currentMedia;
if ( m_currentMediaSnapshot != NULL )
delete m_currentMediaSnapshot;
}
void ListViewMediaItem::setSnapshot()
{
// qDebug() << "setSnapshot";
connect( m_currentMedia->mediaPlayer(), SIGNAL( timeChanged() ), this, SLOT( takeSnapshot() ) );
m_currentMedia->setTime( m_currentMedia->getLength() / 3 );
}
void ListViewMediaItem::takeSnapshot()
{
// TODO: Debug of the multiple snapshot
// TODO: Check for memory leak in the snapshot
m_currentMediaSnapshot = m_currentMedia->takeSnapshot( 32, 32 );
setIcon( QIcon( *m_currentMediaSnapshot ) );
m_currentMedia->stop();
disconnect( m_currentMedia->mediaPlayer(), SIGNAL( playing() ), this, SLOT( setSnapshot() ) );
disconnect( m_currentMedia->mediaPlayer(), SIGNAL( timeChanged() ), this, SLOT( takeSnapshot() ) );
}
const QPixmap* ListViewMediaItem::getSnapshot() const
{
return m_currentMediaSnapshot;
if ( m_inputMedia != NULL )
delete m_inputMedia;
}
......@@ -80,7 +80,12 @@ public:
* \brief Get the current media snapshot
* \return the current media snapshot as a QPixmap*, or NULL if there is no current media.
*/
const QPixmap* getSnapshot() const;
//const QPixmap* getSnapshot() const;
InputMedia* getInputMedia() { return m_inputMedia; }
//void setInputMedia( InputMedia* inputMedia ) { m_inputMedia = inputMedia; }
private:
/**
......@@ -96,7 +101,7 @@ private:
/**
* \Instance of the InputMedia
*/
InputMedia* m_currentMedia;
InputMedia* m_inputMedia;
/**
* \brief Instance of the temporary QWidget use for the snapshot
......@@ -107,11 +112,12 @@ private:
* \brief The current media snapshot
*/
//FIXME: this should probably be in the media itself
QPixmap* m_currentMediaSnapshot;
//QPixmap* m_currentMediaSnapshot;
private slots:
void setSnapshot();
void takeSnapshot();
public slots:
// void setSnapshot();
// void takeSnapshot();
// void seek();
};
#endif /* !LISTVIEWMEDIAITEM_H */
......@@ -42,6 +42,12 @@ MainWindow::MainWindow( QWidget *parent ) :
SLOT(transLateWidgetTitle() ) );
}
MainWindow::~MainWindow()
{
if (m_metaDataManager)
delete m_metaDataManager;
}
void MainWindow::changeEvent( QEvent *e )
{
switch ( e->type() )
......@@ -64,7 +70,8 @@ void MainWindow::m_initializeDockWidgets( void )
DockWidgetManager *dockManager = DockWidgetManager::instance();
dockManager->addDockedWidget( new LibraryWidget( this ),
LibraryWidget* libraryWidget = new LibraryWidget( this );
dockManager->addDockedWidget( libraryWidget,
tr( "Media Library" ),
Qt::AllDockWidgetAreas,
QDockWidget::AllDockWidgetFeatures,
......@@ -75,6 +82,8 @@ void MainWindow::m_initializeDockWidgets( void )
Qt::AllDockWidgetAreas,
QDockWidget::AllDockWidgetFeatures,
Qt::TopDockWidgetArea );
m_metaDataManager = new MetaDataManager( libraryWidget );
}
//Private slots definition
......
......@@ -29,6 +29,7 @@
#include "PreviewWidget.h"
#include "DockWidgetManager.h"
#include "Preferences.h"
#include "MetaDataManager.h"
class MainWindow : public QMainWindow
{
......@@ -37,6 +38,7 @@ class MainWindow : public QMainWindow
public:
explicit MainWindow( QWidget *parent = 0 );
~MainWindow();
protected:
virtual void changeEvent( QEvent *e );
......@@ -45,8 +47,9 @@ private:
void m_initializeDockWidgets( void );
Ui::MainWindow m_ui;
LibraryWidget* m_library;
Ui::MainWindow m_ui;
LibraryWidget* m_library;
MetaDataManager* m_metaDataManager;
private slots:
void on_actionQuit_triggered();
......
......@@ -68,11 +68,11 @@ void MediaListWidget::mouseMoveEvent( QMouseEvent* event )
mimeData->setText( ( ( ListViewMediaItem* )( currentItem() ) )->fileInfo()->absoluteFilePath() );
QDrag* drag = new QDrag( this );
drag->setMimeData( mimeData );
const QPixmap* dragPixmap = static_cast<ListViewMediaItem*>( currentItem() )->getSnapshot();
if ( dragPixmap != NULL )
const QPixmap& dragPixmap = static_cast<ListViewMediaItem*>( currentItem() )->getInputMedia()->getSnapshot();
if ( !dragPixmap.isNull() /*!= NULL */)
{
//TODO: creating a new pixmap maybe a little too much for this...
drag->setPixmap( dragPixmap->scaled( 100, 100, Qt::KeepAspectRatio ) );
drag->setPixmap( dragPixmap.scaled( 100, 100, Qt::KeepAspectRatio ) );
}
drag->exec( Qt::CopyAction | Qt::MoveAction, Qt::CopyAction );
}
......
......@@ -35,20 +35,24 @@ PreviewWidget::PreviewWidget( QWidget *parent ) :
m_ui->seekSlider->setSingleStep( 2 );
m_ui->seekSlider->setFocusPolicy( Qt::NoFocus );
char const *vlc_argv[] =
{
"-verbose", "3",
"--no-skip-frames",
//"--plugin-path", VLC_TREE "/modules",
//"--ignore-config", //Don't use VLC's config files
};
int vlc_argc = sizeof( vlc_argv ) / sizeof( *vlc_argv );
// char const *vlc_argv[] =
// {
// "-verbose", "3",
// "--no-skip-frames",
// //"--plugin-path", VLC_TREE "/modules",
// //"--ignore-config", //Don't use VLC's config files
// };
// int vlc_argc = sizeof( vlc_argv ) / sizeof( *vlc_argv );
setAcceptDrops(true);
m_currentInstance = new LibVLCpp::Instance( vlc_argc, vlc_argv );
//m_currentInstance = new LibVLCpp::Instance( vlc_argc, vlc_argv );
//m_currentInstance = LibVLCpp::Instance::getNewInstance( vlc_argc, vlc_argv );
m_currentInstance = LibVLCpp::Instance::getInstance();
connect( m_ui->seekSlider, SIGNAL( sliderPosChanged(int) ),
this, SLOT( seekSliderMoved(int) ) );
m_mediaPlayer = new LibVLCpp::MediaPlayer();
}
PreviewWidget::~PreviewWidget()
......@@ -87,6 +91,10 @@ void PreviewWidget::dropEvent( QDropEvent* event )
m_currentMedia = new OutputMedia( m_currentInstance );
m_currentMedia->loadMedia( "file://" + item->fileInfo()->absoluteFilePath() );
m_currentMedia->setupMedia();
m_mediaPlayer->setMedia( m_currentMedia->getVLCMedia() );
m_currentMedia->setMediaPlayer( m_mediaPlayer );
m_currentMedia->setDrawable( m_ui->clipRenderWidget->winId() );
//FIXME Connecting endReached to pause to change icon of playpause button
// this might not work as it works now later!
......
......@@ -56,6 +56,7 @@ private:
Ui::PreviewWidget* m_ui;
OutputMedia* m_currentMedia;
LibVLCpp::Instance* m_currentInstance;
LibVLCpp::MediaPlayer* m_mediaPlayer;
};
#endif // PREVIEWWIDGET_H
......@@ -31,7 +31,8 @@ SOURCES += src/main.cpp \
src/gui/About.cpp \
src/gui/Transcode.cpp \
src/InputMedia.cpp \
src/gui/Slider.cpp
src/gui/Slider.cpp \
src/MetaDataManager.cpp
HEADERS += src/gui/MainWindow.h \
src/gui/DockWidgetManager.h \
src/gui/LibraryWidget.h \
......@@ -54,7 +55,8 @@ HEADERS += src/gui/MainWindow.h \
src/gui/About.h \
src/gui/Transcode.h \
src/InputMedia.h \
src/gui/Slider.h
src/gui/Slider.h \
src/MetaDataManager.h
FORMS += src/gui/ui/MainWindow.ui \
src/gui/ui/PreviewWidget.ui \
src/gui/ui/Preferences.ui \
......
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