From 1bcbb69e31090cf71e913419299b52dd4d094bef Mon Sep 17 00:00:00 2001 From: Arno Date: Fri, 29 Mar 2013 12:55:36 +0100 Subject: Make FrameCache threaded create snapshot pics in a separate thread. Also use the first frame available if the clip isn't long enough for the configured frame. --- smglobals.cpp | 126 ++-------------------------------------------------------- 1 file changed, 3 insertions(+), 123 deletions(-) (limited to 'smglobals.cpp') diff --git a/smglobals.cpp b/smglobals.cpp index 6ef4ceb..9240bf2 100644 --- a/smglobals.cpp +++ b/smglobals.cpp @@ -29,6 +29,7 @@ #include "pictureviewer2.h" #include "picfilesmodel.h" #include "configurationdialog.h" +#include "framecache.h" SmGlobals *SmGlobals::mInstance = 0; @@ -115,9 +116,9 @@ PictureViewer2 *SmGlobals::pictureViewer() { return mPictureViewer; } -SmGlobals::FrameCache *SmGlobals::frameCache() { +FrameCache *SmGlobals::frameCache() { if(!mFrameCache){ - mFrameCache = new SmGlobals::FrameCache; + mFrameCache = new FrameCache; } return mFrameCache; } @@ -170,124 +171,3 @@ SmGlobals::SmGlobals() : mPictureViewer(0), mFrameCache(0){ mIcons.insert("Clean tampon", ":/clean_tampon.png"); mDvdSize = Q_INT64_C(4707319808) - 20 * 1024 *1024; } - -//FrameCache -SmGlobals::FrameCache::FrameCache(QObject *parent) : QObject(parent), mMagic(0xDEADBEEF), mCacheSubDir(".frameCache"), mCacheFileName("cache") { - readConfig(); - readCache(); -} - -SmGlobals::FrameCache::~FrameCache(){ - QFile outfile(mCacheFile); - outfile.open(QIODevice::WriteOnly | QIODevice::Truncate); - QDataStream ds(&outfile); - ds << (qint32)mMagic; - for(QHash, QString>::const_iterator it = mFrameCache.constBegin(); it != mFrameCache.constEnd(); ++it){ - ds << it.key().first << it.key().second << it.value(); - } - outfile.close(); -} - -void SmGlobals::FrameCache::readConfig(){ - QSettings s; - QString archive = s.value("paths/archivedir").toString(); - if(archive.isEmpty()){ - return; - } - QDir archiveDir(archive); - if(!archiveDir.exists(mCacheSubDir)){ - archiveDir.mkdir(mCacheSubDir); - } - mCacheDir = QString("%1/%2").arg(archive).arg(mCacheSubDir); - mCacheFile = QString("%1/%2").arg(mCacheDir).arg(mCacheFileName); - mWhen = s.value("ui/grabframe", "00:00:00").toString(); - mFfMpegPath = s.value("paths/ffmpeg").toString(); -} - -const QPixmap SmGlobals::FrameCache::entry(const QString &sourcePath, const QString &when){ - const QPair source = prepFrame(sourcePath, when); - return QPixmap(mFrameCache.value(source)); -} - -const QString SmGlobals::FrameCache::entryPath(const QString &sourcePath, const QString &when){ - const QPair source = prepFrame(sourcePath, when); - return mFrameCache.value(source); -} - -const QPair SmGlobals::FrameCache::prepFrame(const QString &sourceFile, QString when){ - if(when.isEmpty()){ - when = mWhen; - } - const QString fileName = QFileInfo(sourceFile).fileName(); - const QPair source(fileName, when); - if(!mFrameCache.contains(source)){ - grabFrame(sourceFile, when); - } - return source; -} - -void SmGlobals::FrameCache::grabFrame(const QString &sourceFile, QString when){ - QFileInfo sourceInfo(sourceFile); - if(!sourceInfo.exists()){ - return; - } - QString tmpTemplate = QString("%1/%2_%3-XXXXXX.png").arg(mCacheDir).arg(sourceInfo.fileName()).arg(when); - QTemporaryFile tempFile(tmpTemplate); - tempFile.setAutoRemove(false); - if(tempFile.open()){ - QStringList ffMpegArgs = QStringList() << "-vframes" << "1" << "-ss" << when << "-i" << sourceFile << "-y" << tempFile.fileName(); - QProcess ffmpeg; - ffmpeg.start(mFfMpegPath, ffMpegArgs); - if(!ffmpeg.waitForStarted()){ - return; - } - ffmpeg.waitForFinished(); - QPair pair(sourceInfo.fileName(), when); - mFrameCache.insert(pair, tempFile.fileName()); - } -} - -void SmGlobals::FrameCache::readCache(){ - QFile cache(mCacheFile); - cache.open(QIODevice::ReadOnly); - QDataStream ds(&cache); - qint32 magic; - ds >> magic; - if(magic != mMagic){ - return; - } - while(!ds.atEnd()){ - QString source, pos, cacheFile; - ds >> source >> pos >> cacheFile; - QFileInfo fi(cacheFile); - if(fi.size() == 0){ - QFile::remove(fi.absoluteFilePath()); - continue; - } - QPair pair(source, pos); - mFrameCache.insert(pair, cacheFile); - } - cleanup(); -} - -/* cleanup function for fucked up framecache: - Key was full path until recently, not just the filename, so - duplicates showed up when hovering over the same in different - paths, eg, archive or filesystem. Should be removed in the future -*/ -void SmGlobals::FrameCache::cleanup(){ - QHash, QString> newFrameCache; - QHash, QString>::const_iterator it = mFrameCache.constBegin(); - while(it != mFrameCache.constEnd()){ - QPair key = it.key(); - QFileInfo fi(key.first); - QPair newEntry(fi.fileName(), key.second); - if(!newFrameCache.contains(newEntry)){ - newFrameCache.insert(newEntry, it.value()); - }else{ - QFile::remove(it.value()); - } - ++it; - } - mFrameCache = newFrameCache; -} -- cgit v1.2.3-70-g09d2