Merge branch 'audio-head-cache' into 'master'
Cache sound file heads so sound starts do not wait for storage See merge request OpenMW/openmw!5499
This commit is contained in:
@@ -71,7 +71,7 @@ add_openmw_dir (mwlua
|
||||
)
|
||||
|
||||
add_openmw_dir (mwsound
|
||||
soundmanagerimp openaloutput ffmpegdecoder sound soundbuffer sounddecoder soundoutput
|
||||
soundmanagerimp openaloutput ffmpegdecoder headcache sound soundbuffer sounddecoder soundoutput
|
||||
loudness movieaudiofactory alext efx efxpresets regionsoundselector watersoundupdater
|
||||
)
|
||||
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
#include <libavutil/channel_layout.h>
|
||||
#endif
|
||||
|
||||
#include "headcache.hpp"
|
||||
|
||||
namespace MWSound
|
||||
{
|
||||
void AVIOContextDeleter::operator()(AVIOContext* ptr) const
|
||||
@@ -319,7 +321,19 @@ namespace MWSound
|
||||
void FFmpegDecoder::open(VFS::Path::NormalizedView fname)
|
||||
{
|
||||
close();
|
||||
mDataStream = mResourceMgr->get(fname);
|
||||
bool cached = false;
|
||||
if (mHeadCache != nullptr)
|
||||
{
|
||||
if (std::shared_ptr<const HeadBuffer> buffer = mHeadCache->lookup(fname))
|
||||
{
|
||||
mDataStream = makeHeadStream(std::move(buffer), *mResourceMgr);
|
||||
cached = true;
|
||||
}
|
||||
else
|
||||
mDataStream = makeRecordingStream(mResourceMgr->get(fname));
|
||||
}
|
||||
else
|
||||
mDataStream = mResourceMgr->get(fname);
|
||||
|
||||
AVIOContextPtr ioCtx;
|
||||
AVFormatContextPtr formatCtxPtr;
|
||||
@@ -347,6 +361,10 @@ namespace MWSound
|
||||
if (!opened && !openContext(fname.value().data(), nullptr, false, ioCtx, formatCtxPtr, stream))
|
||||
throw std::runtime_error("Failed to open input");
|
||||
|
||||
// Opening is done, so the bytes it read are exactly the prefix the next open of this file needs.
|
||||
if (mHeadCache != nullptr && !cached)
|
||||
mHeadCache->insert(fname, *mDataStream);
|
||||
|
||||
const AVCodec* codec = avcodec_find_decoder((*stream)->codecpar->codec_id);
|
||||
if (codec == nullptr)
|
||||
throw std::runtime_error("No codec found for id " + std::to_string((*stream)->codecpar->codec_id));
|
||||
@@ -584,7 +602,7 @@ namespace MWSound
|
||||
return static_cast<std::size_t>(mNextPts * mCodecCtx->sample_rate) - delay;
|
||||
}
|
||||
|
||||
FFmpegDecoder::FFmpegDecoder(const VFS::Manager* vfs)
|
||||
FFmpegDecoder::FFmpegDecoder(const VFS::Manager* vfs, HeadCache* headCache)
|
||||
: SoundDecoder(vfs)
|
||||
, mStream(nullptr)
|
||||
, mFrameSize(0)
|
||||
@@ -600,6 +618,7 @@ namespace MWSound
|
||||
, mDataBuf(nullptr)
|
||||
, mFrameData(nullptr)
|
||||
, mDataBufLen(0)
|
||||
, mHeadCache(headCache)
|
||||
{
|
||||
memset(&mPacket, 0, sizeof(mPacket));
|
||||
|
||||
|
||||
@@ -35,6 +35,8 @@ extern "C"
|
||||
|
||||
namespace MWSound
|
||||
{
|
||||
class HeadCache;
|
||||
|
||||
struct AVIOContextDeleter
|
||||
{
|
||||
void operator()(AVIOContext* ptr) const;
|
||||
@@ -124,11 +126,14 @@ namespace MWSound
|
||||
FFmpegDecoder(const FFmpegDecoder& rhs);
|
||||
|
||||
public:
|
||||
explicit FFmpegDecoder(const VFS::Manager* vfs);
|
||||
explicit FFmpegDecoder(const VFS::Manager* vfs, HeadCache* headCache);
|
||||
|
||||
virtual ~FFmpegDecoder();
|
||||
|
||||
friend class SoundManager;
|
||||
|
||||
private:
|
||||
HeadCache* mHeadCache;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
#include "headcache.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <istream>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <components/files/streamwithbuffer.hpp>
|
||||
#include <components/vfs/manager.hpp>
|
||||
|
||||
namespace MWSound
|
||||
{
|
||||
namespace
|
||||
{
|
||||
// Both buffers implement only the block-read and seek subset of the
|
||||
// std::streambuf protocol that FFmpegDecoder uses (read, gcount,
|
||||
// seekg, tellg); single-character access is unsupported by design.
|
||||
|
||||
// Per-file ceiling. Measured stream init consumption tops out under
|
||||
// 100KB across vanilla and heavy audio mods once embedded pictures are
|
||||
// out of the way; files whose init reads more than this are not cached.
|
||||
constexpr std::streamoff sMaxHeadBytes = 256 * 1024;
|
||||
|
||||
// Reads in the lower half of the file extend the prefix; reads in the
|
||||
// upper half accumulate a suffix range, so both regions can be cached
|
||||
// and served next time.
|
||||
class RecordingBuf final : public std::streambuf
|
||||
{
|
||||
public:
|
||||
explicit RecordingBuf(Files::IStreamPtr inner)
|
||||
: mInner(std::move(inner))
|
||||
{
|
||||
}
|
||||
|
||||
std::streamoff prefixEnd() const { return mPrefixEnd; }
|
||||
std::streamoff suffixStart() const { return mSuffixStart; }
|
||||
|
||||
protected:
|
||||
std::streamsize xsgetn(char* s, std::streamsize n) override
|
||||
{
|
||||
mInner->clear();
|
||||
mInner->read(s, n);
|
||||
const std::streamsize got = mInner->gcount();
|
||||
const std::streamoff begin = mPos;
|
||||
mPos += got;
|
||||
if (mFileSize > 0 && begin >= mFileSize / 2)
|
||||
mSuffixStart = std::min(mSuffixStart, begin);
|
||||
else
|
||||
mPrefixEnd = std::max(mPrefixEnd, mPos);
|
||||
return got;
|
||||
}
|
||||
|
||||
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir dir, std::ios_base::openmode) override
|
||||
{
|
||||
mInner->clear();
|
||||
mInner->seekg(off, dir);
|
||||
if (!*mInner)
|
||||
return std::streampos(std::streamoff(-1));
|
||||
mPos = mInner->tellg();
|
||||
if (dir == std::ios_base::end)
|
||||
mFileSize = std::max(mFileSize, mPos - off);
|
||||
setg(nullptr, nullptr, nullptr);
|
||||
return mPos;
|
||||
}
|
||||
|
||||
std::streampos seekpos(std::streampos pos, std::ios_base::openmode mode) override
|
||||
{
|
||||
return seekoff(static_cast<std::streamoff>(pos), std::ios_base::beg, mode);
|
||||
}
|
||||
|
||||
private:
|
||||
Files::IStreamPtr mInner;
|
||||
std::streamoff mPos = 0;
|
||||
std::streamoff mPrefixEnd = 0;
|
||||
std::streamoff mSuffixStart = std::numeric_limits<std::streamoff>::max();
|
||||
std::streamoff mFileSize = 0;
|
||||
};
|
||||
|
||||
class HeadBuf final : public std::streambuf
|
||||
{
|
||||
public:
|
||||
HeadBuf(std::shared_ptr<const HeadBuffer>&& buffer, const VFS::Manager& vfs)
|
||||
: mBuffer(std::move(buffer))
|
||||
, mVfs(vfs)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
std::streamsize xsgetn(char* s, std::streamsize n) override
|
||||
{
|
||||
std::streamsize done = 0;
|
||||
while (done < n && mPos < mBuffer->mFileSize)
|
||||
{
|
||||
const std::streamoff headSize = static_cast<std::streamoff>(mBuffer->mHead.size());
|
||||
if (mPos < headSize)
|
||||
{
|
||||
const std::streamsize take = std::min<std::streamsize>(n - done, headSize - mPos);
|
||||
std::memcpy(s + done, mBuffer->mHead.data() + mPos, take);
|
||||
done += take;
|
||||
mPos += take;
|
||||
continue;
|
||||
}
|
||||
if (!mBuffer->mSuffix.empty() && mPos >= mBuffer->mSuffixStart)
|
||||
{
|
||||
const std::streamoff inSuffix = mPos - mBuffer->mSuffixStart;
|
||||
const std::streamsize take = std::min<std::streamsize>(
|
||||
n - done, static_cast<std::streamoff>(mBuffer->mSuffix.size()) - inSuffix);
|
||||
if (take <= 0)
|
||||
break;
|
||||
std::memcpy(s + done, mBuffer->mSuffix.data() + inSuffix, take);
|
||||
done += take;
|
||||
mPos += take;
|
||||
continue;
|
||||
}
|
||||
if (mInner == nullptr)
|
||||
mInner = mVfs.get(mBuffer->mName);
|
||||
mInner->clear();
|
||||
mInner->seekg(mPos);
|
||||
const std::streamsize want = !mBuffer->mSuffix.empty() && mPos < mBuffer->mSuffixStart
|
||||
? std::min<std::streamsize>(n - done, mBuffer->mSuffixStart - mPos)
|
||||
: n - done;
|
||||
mInner->read(s + done, want);
|
||||
const std::streamsize got = mInner->gcount();
|
||||
if (got <= 0)
|
||||
break;
|
||||
done += got;
|
||||
mPos += got;
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir dir, std::ios_base::openmode) override
|
||||
{
|
||||
std::streamoff base = 0;
|
||||
if (dir == std::ios_base::cur)
|
||||
base = mPos;
|
||||
else if (dir == std::ios_base::end)
|
||||
base = mBuffer->mFileSize;
|
||||
const std::streamoff target = base + off;
|
||||
// Reject seeks past the end like the constrained streams the
|
||||
// file is served from on a miss, so replays behave the same.
|
||||
if (target < 0 || target > mBuffer->mFileSize)
|
||||
return std::streampos(std::streamoff(-1));
|
||||
mPos = target;
|
||||
setg(nullptr, nullptr, nullptr);
|
||||
return mPos;
|
||||
}
|
||||
|
||||
std::streampos seekpos(std::streampos pos, std::ios_base::openmode mode) override
|
||||
{
|
||||
return seekoff(static_cast<std::streamoff>(pos), std::ios_base::beg, mode);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<const HeadBuffer> mBuffer;
|
||||
const VFS::Manager& mVfs;
|
||||
Files::IStreamPtr mInner;
|
||||
std::streamoff mPos = 0;
|
||||
};
|
||||
}
|
||||
|
||||
Files::IStreamPtr makeHeadStream(std::shared_ptr<const HeadBuffer>&& buffer, const VFS::Manager& vfs)
|
||||
{
|
||||
return std::make_unique<Files::StreamWithBuffer<HeadBuf>>(std::make_unique<HeadBuf>(std::move(buffer), vfs));
|
||||
}
|
||||
|
||||
Files::IStreamPtr makeRecordingStream(Files::IStreamPtr&& impl)
|
||||
{
|
||||
return std::make_unique<Files::StreamWithBuffer<RecordingBuf>>(std::make_unique<RecordingBuf>(std::move(impl)));
|
||||
}
|
||||
|
||||
HeadCache::HeadCache(const VFS::Manager& vfs, std::size_t maxBytes)
|
||||
: mVfs(vfs)
|
||||
, mMaxBytes(maxBytes)
|
||||
{
|
||||
}
|
||||
|
||||
std::shared_ptr<const HeadBuffer> HeadCache::lookup(VFS::Path::NormalizedView name)
|
||||
{
|
||||
const std::lock_guard lock(mMutex);
|
||||
const auto it = mEntries.find(name);
|
||||
if (it == mEntries.end())
|
||||
return nullptr;
|
||||
mLru.splice(mLru.begin(), mLru, it->second);
|
||||
return *it->second;
|
||||
}
|
||||
|
||||
void HeadCache::insert(VFS::Path::NormalizedView name, const std::istream& stream)
|
||||
{
|
||||
const auto* const recording = dynamic_cast<const RecordingBuf*>(stream.rdbuf());
|
||||
if (recording == nullptr)
|
||||
throw std::invalid_argument("HeadCache::insert: stream is not from makeRecordingStream");
|
||||
const std::streamoff prefixEnd = recording->prefixEnd();
|
||||
if (prefixEnd <= 0 || prefixEnd > sMaxHeadBytes)
|
||||
return;
|
||||
|
||||
// The file was just read by stream init, so this re-open and these
|
||||
// copies are served from the OS caches; storage is not touched again.
|
||||
Files::IStreamPtr fresh = mVfs.get(name);
|
||||
fresh->seekg(0, std::ios_base::end);
|
||||
const std::streamoff fileSize = fresh->tellg();
|
||||
if (fileSize <= 0)
|
||||
return;
|
||||
std::vector<char> head(static_cast<std::size_t>(std::min(prefixEnd, fileSize)));
|
||||
fresh->seekg(0);
|
||||
fresh->read(head.data(), head.size());
|
||||
if (fresh->gcount() != static_cast<std::streamsize>(head.size()))
|
||||
return;
|
||||
|
||||
std::vector<char> suffix;
|
||||
std::streamoff suffixStart = fileSize;
|
||||
if (recording->suffixStart() < fileSize)
|
||||
{
|
||||
suffixStart = recording->suffixStart();
|
||||
if (fileSize - suffixStart > sMaxHeadBytes)
|
||||
return;
|
||||
suffix.resize(static_cast<std::size_t>(fileSize - suffixStart));
|
||||
fresh->clear();
|
||||
fresh->seekg(suffixStart);
|
||||
fresh->read(suffix.data(), suffix.size());
|
||||
if (fresh->gcount() != static_cast<std::streamsize>(suffix.size()))
|
||||
return;
|
||||
}
|
||||
|
||||
insert(name, std::move(head), std::move(suffix), suffixStart, fileSize);
|
||||
}
|
||||
|
||||
void HeadCache::insert(VFS::Path::NormalizedView name, std::vector<char>&& head, std::vector<char>&& suffix,
|
||||
std::streamoff suffixStart, std::streamoff fileSize)
|
||||
{
|
||||
const std::size_t bytes = head.size() + suffix.size();
|
||||
// Nothing is allocated until every way out has been taken: an entry
|
||||
// bigger than the whole budget can never fit, and evicting for it
|
||||
// would empty the cache for no purpose.
|
||||
if (bytes > mMaxBytes)
|
||||
return;
|
||||
const std::lock_guard lock(mMutex);
|
||||
if (mEntries.contains(name))
|
||||
return;
|
||||
while (!mLru.empty() && mBytes + bytes > mMaxBytes)
|
||||
{
|
||||
const HeadBuffer& evicted = *mLru.back();
|
||||
mBytes -= evicted.mHead.size() + evicted.mSuffix.size();
|
||||
mEntries.erase(evicted.mName);
|
||||
mLru.pop_back();
|
||||
}
|
||||
const LruIt lruIt = mLru.insert(mLru.begin(),
|
||||
std::make_shared<const HeadBuffer>(
|
||||
VFS::Path::Normalized(name), std::move(head), std::move(suffix), suffixStart, fileSize));
|
||||
mEntries.emplace((*lruIt)->mName, lruIt);
|
||||
mBytes += bytes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
#ifndef GAME_SOUND_HEADCACHE_H
|
||||
#define GAME_SOUND_HEADCACHE_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <ios>
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <components/files/istreamptr.hpp>
|
||||
#include <components/vfs/pathutil.hpp>
|
||||
|
||||
namespace VFS
|
||||
{
|
||||
class Manager;
|
||||
}
|
||||
|
||||
namespace MWSound
|
||||
{
|
||||
// The bytes cached for one file: a prefix, plus a suffix when stream init
|
||||
// also read near the end (mp3 estimates duration from the tail). The name
|
||||
// travels with them because a stream serving the ranges opens the file
|
||||
// itself when something reads into the gap between them.
|
||||
struct HeadBuffer
|
||||
{
|
||||
VFS::Path::Normalized mName;
|
||||
std::vector<char> mHead;
|
||||
std::vector<char> mSuffix;
|
||||
std::streamoff mSuffixStart;
|
||||
std::streamoff mFileSize;
|
||||
|
||||
HeadBuffer(VFS::Path::Normalized&& name, std::vector<char>&& head, std::vector<char>&& suffix,
|
||||
std::streamoff suffixStart, std::streamoff fileSize)
|
||||
: mName(std::move(name))
|
||||
, mHead(std::move(head))
|
||||
, mSuffix(std::move(suffix))
|
||||
, mSuffixStart(suffixStart)
|
||||
, mFileSize(fileSize)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
// Serves the cached ranges from memory. The file is opened lazily on the
|
||||
// first read into the gap between them, which happens on the streaming
|
||||
// thread during a refill, or never when the ranges cover the whole file.
|
||||
Files::IStreamPtr makeHeadStream(std::shared_ptr<const HeadBuffer>&& buffer, const VFS::Manager& vfs);
|
||||
|
||||
// Passes through to the real stream and remembers which ranges stream init
|
||||
// read, which is what HeadCache::insert sizes an entry from.
|
||||
Files::IStreamPtr makeRecordingStream(Files::IStreamPtr&& impl);
|
||||
|
||||
// Keeps the first bytes of recently started sounds in memory so that a
|
||||
// sound start does not wait for storage on the thread starting it: stream
|
||||
// init is served from the cached head and the first disk read happens
|
||||
// later, during the streaming thread's refill (#4880). Storage waking from
|
||||
// a low power state costs 10-50ms on NVMe and seconds on spun-down drives,
|
||||
// paid by the first read after idle; opening a file costs nothing.
|
||||
//
|
||||
// Cached ranges are learned per file from what stream init consumed.
|
||||
// Ranges that would exceed a fixed ceiling are not cached. When the ranges
|
||||
// cover a whole file (most voice files), replays never touch the
|
||||
// filesystem.
|
||||
class HeadCache
|
||||
{
|
||||
public:
|
||||
// maxBytes: total budget for cached heads; entries are evicted least
|
||||
// recently used. A zero budget caches nothing.
|
||||
explicit HeadCache(const VFS::Manager& vfs, std::size_t maxBytes);
|
||||
|
||||
// The bytes cached for the file, or nullptr when it has none. Entries
|
||||
// are shared with the streams serving them, so eviction cannot
|
||||
// invalidate a stream in use.
|
||||
std::shared_ptr<const HeadBuffer> lookup(VFS::Path::NormalizedView name);
|
||||
|
||||
// Captures the head of a stream from makeRecordingStream, once stream
|
||||
// init has succeeded on it. Throws when handed any other stream.
|
||||
void insert(VFS::Path::NormalizedView name, const std::istream& stream);
|
||||
|
||||
private:
|
||||
using LruIt = std::list<std::shared_ptr<const HeadBuffer>>::iterator;
|
||||
|
||||
void insert(VFS::Path::NormalizedView name, std::vector<char>&& head, std::vector<char>&& suffix,
|
||||
std::streamoff suffixStart, std::streamoff fileSize);
|
||||
|
||||
const VFS::Manager& mVfs;
|
||||
const std::size_t mMaxBytes;
|
||||
std::mutex mMutex;
|
||||
// The buffer lives in the list so a lookup reaches it through the map
|
||||
// once; the map value is the list iterator that keeps it there.
|
||||
std::list<std::shared_ptr<const HeadBuffer>> mLru;
|
||||
std::unordered_map<VFS::Path::Normalized, LruIt, VFS::Path::Hash, std::equal_to<>> mEntries;
|
||||
std::size_t mBytes = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -27,6 +27,7 @@
|
||||
|
||||
#include "constants.hpp"
|
||||
#include "ffmpegdecoder.hpp"
|
||||
#include "headcache.hpp"
|
||||
#include "openaloutput.hpp"
|
||||
#include "sound.hpp"
|
||||
#include "soundbuffer.hpp"
|
||||
@@ -101,6 +102,14 @@ namespace MWSound
|
||||
|
||||
return volume;
|
||||
}
|
||||
|
||||
std::unique_ptr<HeadCache> makeHeadCache(const VFS::Manager& vfs)
|
||||
{
|
||||
const std::size_t sizeMb = Settings::sound().mHeadCacheSize;
|
||||
if (sizeMb == 0)
|
||||
return nullptr;
|
||||
return std::make_unique<HeadCache>(vfs, sizeMb * 1024 * 1024);
|
||||
}
|
||||
}
|
||||
|
||||
// For combining PlayMode and Type flags
|
||||
@@ -111,6 +120,7 @@ namespace MWSound
|
||||
|
||||
SoundManager::SoundManager(const VFS::Manager* vfs, bool useSound)
|
||||
: mVFS(vfs)
|
||||
, mHeadCache(makeHeadCache(*vfs))
|
||||
, mOutput(std::make_unique<OpenALOutput>(*this))
|
||||
, mWaterSoundUpdater(makeWaterSoundUpdaterSettings())
|
||||
, mSoundBuffers(*mOutput)
|
||||
@@ -170,14 +180,21 @@ namespace MWSound
|
||||
// Return a new decoder instance, used as needed by the output implementations
|
||||
DecoderPtr SoundManager::getDecoder()
|
||||
{
|
||||
return std::make_shared<FFmpegDecoder>(mVFS);
|
||||
return std::make_shared<FFmpegDecoder>(mVFS, nullptr);
|
||||
}
|
||||
|
||||
// Only streamed sounds take the head cache: a buffered sound is decoded whole at play time and
|
||||
// stays decoded in SoundBufferPool, so a cached head would only be read after the pool unloads it.
|
||||
DecoderPtr SoundManager::getStreamDecoder()
|
||||
{
|
||||
return std::make_shared<FFmpegDecoder>(mVFS, mHeadCache.get());
|
||||
}
|
||||
|
||||
DecoderPtr SoundManager::loadVoice(VFS::Path::NormalizedView voicefile)
|
||||
{
|
||||
try
|
||||
{
|
||||
DecoderPtr decoder = getDecoder();
|
||||
DecoderPtr decoder = getStreamDecoder();
|
||||
decoder->open(Misc::ResourceHelpers::correctSoundPath(voicefile, *decoder->mResourceMgr));
|
||||
return decoder;
|
||||
}
|
||||
@@ -264,7 +281,7 @@ namespace MWSound
|
||||
|
||||
Log(Debug::Info) << "Playing \"" << filename << "\"";
|
||||
|
||||
DecoderPtr decoder = getDecoder();
|
||||
DecoderPtr decoder = getStreamDecoder();
|
||||
try
|
||||
{
|
||||
decoder->open(filename);
|
||||
|
||||
@@ -42,6 +42,7 @@ namespace MWSound
|
||||
class SoundBase;
|
||||
class Sound;
|
||||
class Stream;
|
||||
class HeadCache;
|
||||
|
||||
using SoundPtr = Misc::ObjectPtr<Sound>;
|
||||
using StreamPtr = Misc::ObjectPtr<Stream>;
|
||||
@@ -50,6 +51,8 @@ namespace MWSound
|
||||
{
|
||||
const VFS::Manager* mVFS;
|
||||
|
||||
std::unique_ptr<HeadCache> mHeadCache;
|
||||
|
||||
std::unique_ptr<SoundOutput> mOutput;
|
||||
|
||||
WaterSoundUpdater mWaterSoundUpdater;
|
||||
@@ -153,6 +156,7 @@ namespace MWSound
|
||||
|
||||
protected:
|
||||
DecoderPtr getDecoder();
|
||||
DecoderPtr getStreamDecoder();
|
||||
friend class OpenALOutput;
|
||||
|
||||
void stopSound(SoundBuffer* sfx, const MWWorld::ConstPtr& ptr);
|
||||
|
||||
@@ -18,6 +18,8 @@ file(GLOB UNITTEST_SRC_FILES
|
||||
mwgui/weightedsearch.cpp
|
||||
|
||||
mwscript/testscripts.cpp
|
||||
|
||||
mwsound/testheadcache.cpp
|
||||
)
|
||||
|
||||
if (MSVC)
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
#include "apps/openmw/mwsound/headcache.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <ios>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <components/files/istreamptr.hpp>
|
||||
#include <components/testing/util.hpp>
|
||||
#include <components/vfs/file.hpp>
|
||||
#include <components/vfs/manager.hpp>
|
||||
#include <components/vfs/pathutil.hpp>
|
||||
|
||||
namespace MWSound
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr VFS::Path::NormalizedView sFile("sound/a.wav");
|
||||
constexpr VFS::Path::NormalizedView sOtherFile("sound/b.wav");
|
||||
|
||||
// Counts opens, so a read served from memory can be told from one that
|
||||
// reached the file.
|
||||
class CountingFile final : public VFS::File
|
||||
{
|
||||
public:
|
||||
explicit CountingFile(std::string content)
|
||||
: mContent(std::move(content))
|
||||
{
|
||||
}
|
||||
|
||||
Files::IStreamPtr open() override
|
||||
{
|
||||
++mOpens;
|
||||
return std::make_unique<std::stringstream>(mContent, std::ios_base::in);
|
||||
}
|
||||
|
||||
std::filesystem::file_time_type getLastModified() const override { return {}; }
|
||||
|
||||
std::string getStem() const override { return "TestFile"; }
|
||||
|
||||
int mOpens = 0;
|
||||
|
||||
private:
|
||||
const std::string mContent;
|
||||
};
|
||||
|
||||
std::string makeContent(std::size_t size)
|
||||
{
|
||||
std::string content(size, '\0');
|
||||
for (std::size_t i = 0; i < size; ++i)
|
||||
content[i] = static_cast<char>('a' + i % 26);
|
||||
return content;
|
||||
}
|
||||
|
||||
std::string read(std::istream& stream, std::size_t size)
|
||||
{
|
||||
std::string out(size, '\0');
|
||||
stream.read(out.data(), static_cast<std::streamsize>(size));
|
||||
out.resize(static_cast<std::size_t>(stream.gcount()));
|
||||
return out;
|
||||
}
|
||||
|
||||
// Fills the cache the way a first play does: stream init reads the
|
||||
// first bytes of the file, then the decoder hands the stream back.
|
||||
void warm(HeadCache& cache, VFS::Path::NormalizedView name, std::size_t initBytes, const VFS::Manager& vfs)
|
||||
{
|
||||
const Files::IStreamPtr stream = makeRecordingStream(vfs.get(name));
|
||||
read(*stream, initBytes);
|
||||
cache.insert(name, *stream);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldNotFindAnEntryForAFileNeverPlayed)
|
||||
{
|
||||
CountingFile file(makeContent(64));
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
|
||||
EXPECT_EQ(cache.lookup(sFile), nullptr);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldCacheTheHeadOfARecordingStream)
|
||||
{
|
||||
CountingFile file(makeContent(64));
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
const Files::IStreamPtr stream = makeRecordingStream(vfs->get(sFile));
|
||||
read(*stream, 16);
|
||||
|
||||
cache.insert(sFile, *stream);
|
||||
|
||||
EXPECT_NE(cache.lookup(sFile), nullptr);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldServeTheHeadOfAReplayFromMemory)
|
||||
{
|
||||
const std::string content = makeContent(64);
|
||||
CountingFile file(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
warm(cache, sFile, 16, *vfs);
|
||||
const int opens = file.mOpens;
|
||||
|
||||
std::shared_ptr<const HeadBuffer> buffer = cache.lookup(sFile);
|
||||
ASSERT_NE(buffer, nullptr);
|
||||
const Files::IStreamPtr stream = makeHeadStream(std::move(buffer), *vfs);
|
||||
|
||||
EXPECT_EQ(read(*stream, 16), content.substr(0, 16));
|
||||
EXPECT_EQ(file.mOpens, opens);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldNotOpenTheFileWhenTheRangesCoverIt)
|
||||
{
|
||||
const std::string content = makeContent(64);
|
||||
CountingFile file(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
warm(cache, sFile, 64, *vfs);
|
||||
const int opens = file.mOpens;
|
||||
|
||||
std::shared_ptr<const HeadBuffer> buffer = cache.lookup(sFile);
|
||||
ASSERT_NE(buffer, nullptr);
|
||||
const Files::IStreamPtr stream = makeHeadStream(std::move(buffer), *vfs);
|
||||
|
||||
EXPECT_EQ(read(*stream, 64), content);
|
||||
EXPECT_EQ(file.mOpens, opens);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldReadPastTheHeadFromTheFile)
|
||||
{
|
||||
const std::string content = makeContent(64);
|
||||
CountingFile file(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
warm(cache, sFile, 16, *vfs);
|
||||
const int opens = file.mOpens;
|
||||
|
||||
std::shared_ptr<const HeadBuffer> buffer = cache.lookup(sFile);
|
||||
ASSERT_NE(buffer, nullptr);
|
||||
const Files::IStreamPtr stream = makeHeadStream(std::move(buffer), *vfs);
|
||||
|
||||
EXPECT_EQ(read(*stream, 64), content);
|
||||
EXPECT_EQ(file.mOpens, opens + 1);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldCacheTheSuffixRangeReadByInit)
|
||||
{
|
||||
const std::string content = makeContent(64);
|
||||
CountingFile file(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
{
|
||||
const Files::IStreamPtr init = makeRecordingStream(vfs->get(sFile));
|
||||
read(*init, 8);
|
||||
init->seekg(-8, std::ios_base::end);
|
||||
read(*init, 8);
|
||||
cache.insert(sFile, *init);
|
||||
}
|
||||
const int opens = file.mOpens;
|
||||
|
||||
std::shared_ptr<const HeadBuffer> buffer = cache.lookup(sFile);
|
||||
ASSERT_NE(buffer, nullptr);
|
||||
const Files::IStreamPtr stream = makeHeadStream(std::move(buffer), *vfs);
|
||||
|
||||
EXPECT_EQ(read(*stream, 8), content.substr(0, 8));
|
||||
stream->seekg(-8, std::ios_base::end);
|
||||
EXPECT_EQ(read(*stream, 8), content.substr(56, 8));
|
||||
EXPECT_EQ(file.mOpens, opens);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldAnswerSeeksFromTheRecordedFileSize)
|
||||
{
|
||||
const std::string content = makeContent(64);
|
||||
CountingFile file(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
warm(cache, sFile, 64, *vfs);
|
||||
|
||||
std::shared_ptr<const HeadBuffer> buffer = cache.lookup(sFile);
|
||||
ASSERT_NE(buffer, nullptr);
|
||||
const Files::IStreamPtr stream = makeHeadStream(std::move(buffer), *vfs);
|
||||
|
||||
stream->seekg(0, std::ios_base::end);
|
||||
EXPECT_EQ(stream->tellg(), std::streampos(64));
|
||||
stream->seekg(65);
|
||||
EXPECT_TRUE(stream->fail());
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldEvictTheLeastRecentlyUsedEntry)
|
||||
{
|
||||
const std::string content = makeContent(32);
|
||||
CountingFile file(content);
|
||||
CountingFile otherFile(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file }, { sOtherFile, &otherFile } });
|
||||
HeadCache cache(*vfs, 40);
|
||||
warm(cache, sFile, 32, *vfs);
|
||||
|
||||
warm(cache, sOtherFile, 32, *vfs);
|
||||
|
||||
EXPECT_EQ(cache.lookup(sFile), nullptr);
|
||||
EXPECT_NE(cache.lookup(sOtherFile), nullptr);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldKeepServingAnOpenStreamAfterItsEntryIsEvicted)
|
||||
{
|
||||
const std::string content = makeContent(32);
|
||||
CountingFile file(content);
|
||||
CountingFile otherFile(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file }, { sOtherFile, &otherFile } });
|
||||
HeadCache cache(*vfs, 40);
|
||||
warm(cache, sFile, 32, *vfs);
|
||||
std::shared_ptr<const HeadBuffer> buffer = cache.lookup(sFile);
|
||||
ASSERT_NE(buffer, nullptr);
|
||||
const Files::IStreamPtr stream = makeHeadStream(std::move(buffer), *vfs);
|
||||
warm(cache, sOtherFile, 32, *vfs);
|
||||
const int opens = file.mOpens;
|
||||
|
||||
EXPECT_EQ(read(*stream, 32), content);
|
||||
EXPECT_EQ(file.mOpens, opens);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldCacheNothingWithAZeroBudget)
|
||||
{
|
||||
const std::string content = makeContent(32);
|
||||
CountingFile file(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 0);
|
||||
|
||||
warm(cache, sFile, 32, *vfs);
|
||||
|
||||
EXPECT_EQ(cache.lookup(sFile), nullptr);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldRejectAStreamItDidNotRecord)
|
||||
{
|
||||
CountingFile file(makeContent(64));
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024);
|
||||
const Files::IStreamPtr stream = vfs->get(sFile);
|
||||
|
||||
EXPECT_THROW(cache.insert(sFile, *stream), std::invalid_argument);
|
||||
}
|
||||
|
||||
TEST(MWSoundHeadCacheTest, shouldNotCacheAHeadOverTheCeiling)
|
||||
{
|
||||
const std::string content = makeContent(300 * 1024);
|
||||
CountingFile file(content);
|
||||
const auto vfs = TestingOpenMW::createTestVFS({ { sFile, &file } });
|
||||
HeadCache cache(*vfs, 1024 * 1024);
|
||||
|
||||
warm(cache, sFile, content.size(), *vfs);
|
||||
|
||||
EXPECT_EQ(cache.lookup(sFile), nullptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ namespace Settings
|
||||
SettingValue<float> mVoiceVolume{ mIndex, "Sound", "voice volume", makeClampSanitizerFloat(0, 1) };
|
||||
SettingValue<int> mBufferCacheMin{ mIndex, "Sound", "buffer cache min", makeMaxSanitizerInt(1) };
|
||||
SettingValue<int> mBufferCacheMax{ mIndex, "Sound", "buffer cache max", makeMaxSanitizerInt(1) };
|
||||
SettingValue<std::size_t> mHeadCacheSize{ mIndex, "Sound", "head cache size", makeClampSanitizerSize(0, 4095) };
|
||||
SettingValue<HrtfMode> mHrtfEnable{ mIndex, "Sound", "hrtf enable" };
|
||||
SettingValue<std::string> mHrtf{ mIndex, "Sound", "hrtf" };
|
||||
SettingValue<bool> mCameraListener{ mIndex, "Sound", "camera listener" };
|
||||
|
||||
@@ -188,6 +188,11 @@ namespace Settings
|
||||
return std::make_unique<Clamp<int>>(min, max);
|
||||
}
|
||||
|
||||
std::unique_ptr<Sanitizer<std::size_t>> makeClampSanitizerSize(std::size_t min, std::size_t max)
|
||||
{
|
||||
return std::make_unique<Clamp<std::size_t>>(min, max);
|
||||
}
|
||||
|
||||
std::unique_ptr<Sanitizer<float>> makeClampStrictMaxSanitizerFloat(float min, float max)
|
||||
{
|
||||
return std::make_unique<ClampStrictMax<float>>(min, max);
|
||||
|
||||
@@ -31,6 +31,8 @@ namespace Settings
|
||||
|
||||
std::unique_ptr<Sanitizer<int>> makeClampSanitizerInt(int min, int max);
|
||||
|
||||
std::unique_ptr<Sanitizer<std::size_t>> makeClampSanitizerSize(std::size_t min, std::size_t max);
|
||||
|
||||
std::unique_ptr<Sanitizer<float>> makeClampStrictMaxSanitizerFloat(float min, float max);
|
||||
|
||||
std::unique_ptr<Sanitizer<int>> makeEnumSanitizerInt(std::initializer_list<int> values);
|
||||
|
||||
@@ -83,6 +83,18 @@ Sound Settings
|
||||
This setting must be greater than or equal to the buffer cache min setting.
|
||||
|
||||
|
||||
.. omw-setting::
|
||||
:title: head cache size
|
||||
:type: int
|
||||
:range: 0 to 4095
|
||||
:default: 32
|
||||
|
||||
This setting determines the size of the sound head cache in megabytes.
|
||||
The cache keeps the beginning of recently played music and voice files in
|
||||
memory so that playing them again does not wait for storage. Set to 0 to
|
||||
disable.
|
||||
|
||||
|
||||
.. omw-setting::
|
||||
:title: hrtf enable
|
||||
:type: int
|
||||
|
||||
@@ -609,6 +609,11 @@ sfx volume = 1.0
|
||||
# Voice dialog volume.
|
||||
voice volume = 0.8
|
||||
|
||||
# Size of the sound head cache, in MB. Keeps the beginning of recently
|
||||
# started music and voice files in memory so a sound start does not wait for
|
||||
# storage to wake from a low power state. 0 disables the cache.
|
||||
head cache size = 32
|
||||
|
||||
# Minimum size to use for the sound buffer cache, in MB. When the cache is
|
||||
# filled, old buffers will be unloaded until it's using no more than this much
|
||||
# memory. Must be less than or equal to 'buffer cache max'.
|
||||
|
||||
Reference in New Issue
Block a user