1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.
*/
#include <QFile>
#include <QDataStream>
#include <QSettings>
#include <QDir>
#include <QSemaphore>
#include <QMutex>
#include <QProcess>
#include <QTemporaryFile>
#include <QPixmap>
#include "framecache.h"
FrameCache::FrameCache(QObject *parent) : QObject(parent){
mSemFree = new QSemaphore(128); // 128 queued requests for a frame, should be enough
mSemUsed = new QSemaphore;
mFrameCache = new QHash<QPair<QString, QString>, QString>();
mDataQueue = new QQueue<QPair<QString, QString> >();
mCacheMx = new QMutex;
mGenerator = new FrameCacheGenerator(this);
mGenerator->init(mSemFree, mSemUsed, mCacheMx, mDataQueue, mFrameCache);
mGenerator->readConfig();
rebuild();
mGenerator->start();
QSettings s;
mWhen = s.value("ui/grabframe", "00:00:00").toString();
}
FrameCache::~FrameCache(){
mGenerator->exit();
}
const QPixmap FrameCache::entry(const QString &sourcePath, const QString &when){
QFileInfo fi(sourcePath);
QString w = when.isEmpty() ? mWhen : when;
QPair<QString, QString> key = qMakePair<QString, QString>(fi.fileName(), w);
mCacheMx->lock();
QPixmap retval;
if(!mFrameCache->contains(key)){
QString nullPath = mFrameCache->value(qMakePair<QString, QString>(key.first, "00:00:00"));
if(!nullPath.isEmpty()){
mCacheMx->unlock();
return QPixmap(nullPath);
}
mCacheMx->unlock();
mSemFree->acquire();
mDataQueue->enqueue(qMakePair<QString, QString>(sourcePath, when));
mSemUsed->release();
return QPixmap();
}
retval = QPixmap(mFrameCache->value(key));
mCacheMx->unlock();
return retval;
}
const QString FrameCache::entryPath(const QString &sourcePath, const QString &when){
QFileInfo fi(sourcePath);
if(!fi.exists()){
return QString();
}
QPair<QString, QString> data = qMakePair<QString, QString>(fi.fileName(), when);
QMutexLocker l(mCacheMx);
return mFrameCache->value(data);
}
void FrameCache::rebuild(){
QMutexLocker l(mCacheMx);
QDir cdir(mGenerator->cacheDir());
QFileInfoList files = cdir.entryInfoList(QDir::Files);
mFrameCache->clear();
foreach(QFileInfo fi, files){
QString base = fi.fileName();
base.chop(fi.suffix().size() + 1); // basename doesn't work, b/c we have filenames w spaces
base.chop(7); //remove temp. extension
QString when = base.right(8);
base.chop(9); //remove when + _, leaving the orig. filename
QPair<QString, QString> key = qMakePair<QString, QString>(base, when);
if(mFrameCache->contains(key)){
QFile::remove(fi.absoluteFilePath());
}else{
mFrameCache->insert(key, fi.absoluteFilePath());
}
}
}
FrameCacheGenerator::FrameCacheGenerator(QObject *parent) : QThread(parent), mCacheSubDir(".frameCache"), mCacheFileName("cache") {}
void FrameCacheGenerator::init(QSemaphore *set, QSemaphore *get, QMutex *cachemx, QQueue<QPair<QString, QString> > *data, QHash<QPair<QString, QString>, QString> *cache){
mSemFree = set;
mSemUsed = get;
mDataQueue = data;
mCacheMx = cachemx;
mFrameCache = cache;
}
void FrameCacheGenerator::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);
mFfMpegPath = s.value("paths/ffmpeg").toString();
}
void FrameCacheGenerator::run(){
forever{
mSemUsed->acquire();
QPair<QString, QString> cur = mDataQueue->dequeue();
if(!grabFrame(cur.first, cur.second)){
grabFrame(cur.first, "00:00:00");
}
mSemFree->release();
}
}
bool FrameCacheGenerator::grabFrame(const QString &sourceFile, QString when){
QFileInfo sourceInfo(sourceFile);
if(!sourceInfo.exists()){
return false;
}
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 false;
}
ffmpeg.waitForFinished();
QFileInfo tfi(tempFile.fileName());
if(tfi.size() == 0){
tempFile.remove();
return false;
}
QPair<QString, QString> pair(sourceInfo.fileName(), when);
mCacheMx->lock();
mFrameCache->insert(pair, tempFile.fileName());
mCacheMx->unlock();
return true;
}
return false;
}
|