only disk cache around the playhead

This commit is contained in:
itsmattkc
2020-01-10 20:35:48 +11:00
parent ea42ca36f5
commit d32306f88b
16 changed files with 222 additions and 136 deletions
+9 -4
View File
@@ -115,13 +115,10 @@ void RenderBackend::InvalidateCache(const rational &start_range, const rational
<< "and"
<< end_range_adj.toDouble();
// Add the range to the list
cache_queue_.InsertTimeRange(TimeRange(start_range_adj, end_range_adj));
// Queue value update
QueueValueUpdate();
CacheNext();
InvalidateCacheInternal(start_range_adj, end_range_adj);
}
bool RenderBackend::Compile()
@@ -356,6 +353,14 @@ const QVector<QThread *> &RenderBackend::threads()
return threads_;
}
void RenderBackend::InvalidateCacheInternal(const rational &start_range, const rational &end_range)
{
// Add the range to the list
cache_queue_.InsertTimeRange(TimeRange(start_range, end_range));
CacheNext();
}
void RenderBackend::CacheIDChangedEvent(const QString &id)
{
Q_UNUSED(id)
+2
View File
@@ -63,6 +63,8 @@ protected:
*/
virtual bool GenerateCacheIDInternal(QCryptographicHash& hash) = 0;
virtual void InvalidateCacheInternal(const rational &start_range, const rational &end_range);
virtual void CacheIDChangedEvent(const QString& id);
void SetError(const QString& error);
+52 -5
View File
@@ -28,6 +28,8 @@
#include <QtMath>
#include "common/timecodefunctions.h"
#include "config/config.h"
#include "render/diskmanager.h"
#include "render/diskmanager.h"
#include "render/pixelservice.h"
#include "videorenderworker.h"
@@ -37,6 +39,7 @@ VideoRenderBackend::VideoRenderBackend(QObject *parent) :
operating_mode_(VideoRenderWorker::kHashRenderCache),
only_signal_last_frame_requested_(true)
{
connect(DiskManager::instance(), &DiskManager::DeletedFrame, this, &VideoRenderBackend::FrameRemovedFromDiskCache);
}
bool VideoRenderBackend::InitInternal()
@@ -142,10 +145,21 @@ void VideoRenderBackend::ConnectWorkerToThis(RenderWorker *processor)
video_processor->SetOperatingMode(operating_mode_);
connect(video_processor, &VideoRenderWorker::CompletedFrame, this, &VideoRenderBackend::ThreadCompletedFrame);
connect(video_processor, &VideoRenderWorker::HashAlreadyBeingCached, this, &VideoRenderBackend::ThreadSkippedFrame);
connect(video_processor, &VideoRenderWorker::CompletedDownload, this, &VideoRenderBackend::ThreadCompletedDownload);
connect(video_processor, &VideoRenderWorker::HashAlreadyExists, this, &VideoRenderBackend::ThreadHashAlreadyExists);
connect(video_processor, &VideoRenderWorker::CompletedFrame, this, &VideoRenderBackend::ThreadCompletedFrame, Qt::QueuedConnection);
connect(video_processor, &VideoRenderWorker::HashAlreadyBeingCached, this, &VideoRenderBackend::ThreadSkippedFrame, Qt::QueuedConnection);
connect(video_processor, &VideoRenderWorker::CompletedDownload, this, &VideoRenderBackend::ThreadCompletedDownload, Qt::QueuedConnection);
connect(video_processor, &VideoRenderWorker::HashAlreadyExists, this, &VideoRenderBackend::ThreadHashAlreadyExists, Qt::QueuedConnection);
}
void VideoRenderBackend::InvalidateCacheInternal(const rational &start_range, const rational &end_range)
{
TimeRange invalidated(start_range, end_range);
missing_cache_.InsertTimeRange(invalidated);
emit RangeInvalidated(invalidated);
Requeue();
}
VideoRenderFrameCache *VideoRenderBackend::frame_cache()
@@ -172,6 +186,8 @@ const char *VideoRenderBackend::GetCachedFrame(const rational &time)
return nullptr;
}
Requeue();
// Find frame in map
QByteArray frame_hash = frame_cache_.TimeToHash(time);
@@ -220,8 +236,13 @@ TimeRange VideoRenderBackend::PopNextFrameFromQueue()
snapped_in -= params_.time_base();
}
TimeRange frame_range(snapped_in, snapped_in + params_.time_base());
// Remove this particular frame from the queue
cache_queue_.RemoveTimeRange(TimeRange(snapped_in, snapped_in + params_.time_base()));
cache_queue_.RemoveTimeRange(frame_range);
// Remove this particular frame from missing frames
missing_cache_.RemoveTimeRange(frame_range);
// Return the snapped frame
return TimeRange(snapped_in, snapped_in);
@@ -293,6 +314,19 @@ void VideoRenderBackend::TruncateFrameCacheLength(const rational &length)
frame_cache_.Truncate(length);
}
void VideoRenderBackend::FrameRemovedFromDiskCache(const QByteArray &hash)
{
QList<rational> deleted_frames = frame_cache()->FramesWithHash(hash);
foreach (const rational& frame, deleted_frames) {
TimeRange invalidated(frame, frame+params_.time_base());
missing_cache_.InsertTimeRange(invalidated);
emit RangeInvalidated(invalidated);
}
}
bool VideoRenderBackend::TimeIsQueued(const TimeRange &time) const
{
return cache_queue_.ContainsTimeRange(time);
@@ -314,3 +348,16 @@ bool VideoRenderBackend::SetFrameHash(const NodeDependency &dep, const QByteArra
return false;
}
void VideoRenderBackend::Requeue()
{
cache_queue_.clear();
// Reset queue around the last time requested
TimeRange queueable_range(last_time_requested_ - Config::Current()["DiskCacheBehind"].value<rational>(),
last_time_requested_ + Config::Current()["DiskCacheAhead"].value<rational>());
cache_queue_ = missing_cache_.Intersects(queueable_range);
CacheNext();
}
+10
View File
@@ -103,12 +103,16 @@ protected:
virtual void EmitCachedFrameReady(const rational &time, const QVariant& value, qint64 job_time) = 0;
virtual void InvalidateCacheInternal(const rational &start_range, const rational &end_range) override;
VideoRenderWorker::OperatingMode operating_mode_;
signals:
void CachedFrameReady(const rational& time, QVariant value, qint64 job_time);
void CachedTimeReady(const rational& time, qint64 job_time);
void RangeInvalidated(const TimeRange& range);
private:
bool TimeIsQueued(const TimeRange &time) const;
@@ -116,12 +120,16 @@ private:
bool SetFrameHash(const NodeDependency& dep, const QByteArray& hash, const qint64& job_time);
void Requeue();
VideoRenderingParams params_;
QByteArray cache_frame_load_buffer_;
VideoRenderFrameCache frame_cache_;
TimeRangeList missing_cache_;
rational last_time_requested_;
bool only_signal_last_frame_requested_;
@@ -134,6 +142,8 @@ private slots:
void TruncateFrameCacheLength(const rational& length);
void FrameRemovedFromDiskCache(const QByteArray& hash);
};
#endif // VIDEORENDERERBACKEND_H
+14 -7
View File
@@ -4,6 +4,7 @@
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include "common/filefunctions.h"
#include "config/config.h"
@@ -19,8 +20,6 @@ DiskManager::DiskManager() :
if (cache_index_file.open(QFile::ReadOnly)) {
QDataStream ds(&cache_index_file);
ds >> consumption_;
while (!cache_index_file.atEnd()) {
HashTime h;
@@ -29,7 +28,10 @@ DiskManager::DiskManager() :
ds >> h.access_time;
ds >> h.file_size;
disk_data_.append(h);
if (QFileInfo::exists(h.file_name)) {
consumption_ += h.file_size;
disk_data_.append(h);
}
}
}
}
@@ -38,7 +40,7 @@ DiskManager::~DiskManager()
{
if (Config::Current()["ClearDiskCacheOnClose"].toBool()) {
// Clear all cache data
QDir(GetMediaCacheLocation()).removeRecursively();
ClearDiskCache();
} else {
// Save current cache index
QFile cache_index_file(QDir(GetMediaCacheLocation()).filePath("index"));
@@ -46,8 +48,6 @@ DiskManager::~DiskManager()
if (cache_index_file.open(QFile::WriteOnly)) {
QDataStream ds(&cache_index_file);
ds << consumption_;
foreach (const HashTime& h, disk_data_) {
ds << h.file_name;
ds << h.hash;
@@ -97,7 +97,7 @@ void DiskManager::CreatedFile(const QString &file_name, const QByteArray &hash)
{
qint64 file_size = QFile(file_name).size();
disk_data_.append({file_name, hash, QDateTime::currentMSecsSinceEpoch(), });
disk_data_.append({file_name, hash, QDateTime::currentMSecsSinceEpoch(), file_size});
consumption_ += file_size;
@@ -106,6 +106,13 @@ void DiskManager::CreatedFile(const QString &file_name, const QByteArray &hash)
}
}
bool DiskManager::ClearDiskCache()
{
disk_data_.clear();
return QDir(GetMediaCacheLocation()).removeRecursively();
}
void DiskManager::DeleteLeastRecent()
{
HashTime h = disk_data_.takeFirst();
+2
View File
@@ -17,6 +17,8 @@ public:
void CreatedFile(const QString& file_name, const QByteArray& hash);
bool ClearDiskCache();
signals:
void DeletedFrame(const QByteArray& hash);