diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index bce9e3b007..d166922e9d 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -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 ) diff --git a/apps/openmw/mwsound/ffmpegdecoder.cpp b/apps/openmw/mwsound/ffmpegdecoder.cpp index 406bd47d66..2383578bfb 100644 --- a/apps/openmw/mwsound/ffmpegdecoder.cpp +++ b/apps/openmw/mwsound/ffmpegdecoder.cpp @@ -15,6 +15,8 @@ #include #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 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(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)); diff --git a/apps/openmw/mwsound/ffmpegdecoder.hpp b/apps/openmw/mwsound/ffmpegdecoder.hpp index 4e0b0f0d52..848f423d9e 100644 --- a/apps/openmw/mwsound/ffmpegdecoder.hpp +++ b/apps/openmw/mwsound/ffmpegdecoder.hpp @@ -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; }; } diff --git a/apps/openmw/mwsound/headcache.cpp b/apps/openmw/mwsound/headcache.cpp new file mode 100644 index 0000000000..b1f21db73f --- /dev/null +++ b/apps/openmw/mwsound/headcache.cpp @@ -0,0 +1,254 @@ +#include "headcache.hpp" + +#include +#include +#include +#include +#include + +#include +#include + +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(pos), std::ios_base::beg, mode); + } + + private: + Files::IStreamPtr mInner; + std::streamoff mPos = 0; + std::streamoff mPrefixEnd = 0; + std::streamoff mSuffixStart = std::numeric_limits::max(); + std::streamoff mFileSize = 0; + }; + + class HeadBuf final : public std::streambuf + { + public: + HeadBuf(std::shared_ptr&& 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(mBuffer->mHead.size()); + if (mPos < headSize) + { + const std::streamsize take = std::min(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( + n - done, static_cast(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(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(pos), std::ios_base::beg, mode); + } + + private: + std::shared_ptr mBuffer; + const VFS::Manager& mVfs; + Files::IStreamPtr mInner; + std::streamoff mPos = 0; + }; + } + + Files::IStreamPtr makeHeadStream(std::shared_ptr&& buffer, const VFS::Manager& vfs) + { + return std::make_unique>(std::make_unique(std::move(buffer), vfs)); + } + + Files::IStreamPtr makeRecordingStream(Files::IStreamPtr&& impl) + { + return std::make_unique>(std::make_unique(std::move(impl))); + } + + HeadCache::HeadCache(const VFS::Manager& vfs, std::size_t maxBytes) + : mVfs(vfs) + , mMaxBytes(maxBytes) + { + } + + std::shared_ptr 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(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 head(static_cast(std::min(prefixEnd, fileSize))); + fresh->seekg(0); + fresh->read(head.data(), head.size()); + if (fresh->gcount() != static_cast(head.size())) + return; + + std::vector suffix; + std::streamoff suffixStart = fileSize; + if (recording->suffixStart() < fileSize) + { + suffixStart = recording->suffixStart(); + if (fileSize - suffixStart > sMaxHeadBytes) + return; + suffix.resize(static_cast(fileSize - suffixStart)); + fresh->clear(); + fresh->seekg(suffixStart); + fresh->read(suffix.data(), suffix.size()); + if (fresh->gcount() != static_cast(suffix.size())) + return; + } + + insert(name, std::move(head), std::move(suffix), suffixStart, fileSize); + } + + void HeadCache::insert(VFS::Path::NormalizedView name, std::vector&& head, std::vector&& 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( + VFS::Path::Normalized(name), std::move(head), std::move(suffix), suffixStart, fileSize)); + mEntries.emplace((*lruIt)->mName, lruIt); + mBytes += bytes; + } +} diff --git a/apps/openmw/mwsound/headcache.hpp b/apps/openmw/mwsound/headcache.hpp new file mode 100644 index 0000000000..15bc3a2c9b --- /dev/null +++ b/apps/openmw/mwsound/headcache.hpp @@ -0,0 +1,99 @@ +#ifndef GAME_SOUND_HEADCACHE_H +#define GAME_SOUND_HEADCACHE_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +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 mHead; + std::vector mSuffix; + std::streamoff mSuffixStart; + std::streamoff mFileSize; + + HeadBuffer(VFS::Path::Normalized&& name, std::vector&& head, std::vector&& 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&& 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 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>::iterator; + + void insert(VFS::Path::NormalizedView name, std::vector&& head, std::vector&& 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> mLru; + std::unordered_map> mEntries; + std::size_t mBytes = 0; + }; +} + +#endif diff --git a/apps/openmw/mwsound/soundmanagerimp.cpp b/apps/openmw/mwsound/soundmanagerimp.cpp index 8471dbe0be..1ec654fbfd 100644 --- a/apps/openmw/mwsound/soundmanagerimp.cpp +++ b/apps/openmw/mwsound/soundmanagerimp.cpp @@ -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 makeHeadCache(const VFS::Manager& vfs) + { + const std::size_t sizeMb = Settings::sound().mHeadCacheSize; + if (sizeMb == 0) + return nullptr; + return std::make_unique(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(*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(mVFS); + return std::make_shared(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(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); diff --git a/apps/openmw/mwsound/soundmanagerimp.hpp b/apps/openmw/mwsound/soundmanagerimp.hpp index 48ff293002..0093ef4eed 100644 --- a/apps/openmw/mwsound/soundmanagerimp.hpp +++ b/apps/openmw/mwsound/soundmanagerimp.hpp @@ -42,6 +42,7 @@ namespace MWSound class SoundBase; class Sound; class Stream; + class HeadCache; using SoundPtr = Misc::ObjectPtr; using StreamPtr = Misc::ObjectPtr; @@ -50,6 +51,8 @@ namespace MWSound { const VFS::Manager* mVFS; + std::unique_ptr mHeadCache; + std::unique_ptr 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); diff --git a/apps/openmw_tests/CMakeLists.txt b/apps/openmw_tests/CMakeLists.txt index 6c7b52d646..b763a26cb4 100644 --- a/apps/openmw_tests/CMakeLists.txt +++ b/apps/openmw_tests/CMakeLists.txt @@ -18,6 +18,8 @@ file(GLOB UNITTEST_SRC_FILES mwgui/weightedsearch.cpp mwscript/testscripts.cpp + + mwsound/testheadcache.cpp ) if (MSVC) diff --git a/apps/openmw_tests/mwsound/testheadcache.cpp b/apps/openmw_tests/mwsound/testheadcache.cpp new file mode 100644 index 0000000000..abffa5754a --- /dev/null +++ b/apps/openmw_tests/mwsound/testheadcache.cpp @@ -0,0 +1,260 @@ +#include "apps/openmw/mwsound/headcache.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +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(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('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(size)); + out.resize(static_cast(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 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 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 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 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 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 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); + } + } +} diff --git a/components/settings/categories/sound.hpp b/components/settings/categories/sound.hpp index 99e70fcdb2..33ad974a40 100644 --- a/components/settings/categories/sound.hpp +++ b/components/settings/categories/sound.hpp @@ -21,6 +21,7 @@ namespace Settings SettingValue mVoiceVolume{ mIndex, "Sound", "voice volume", makeClampSanitizerFloat(0, 1) }; SettingValue mBufferCacheMin{ mIndex, "Sound", "buffer cache min", makeMaxSanitizerInt(1) }; SettingValue mBufferCacheMax{ mIndex, "Sound", "buffer cache max", makeMaxSanitizerInt(1) }; + SettingValue mHeadCacheSize{ mIndex, "Sound", "head cache size", makeClampSanitizerSize(0, 4095) }; SettingValue mHrtfEnable{ mIndex, "Sound", "hrtf enable" }; SettingValue mHrtf{ mIndex, "Sound", "hrtf" }; SettingValue mCameraListener{ mIndex, "Sound", "camera listener" }; diff --git a/components/settings/sanitizerimpl.cpp b/components/settings/sanitizerimpl.cpp index bba08d86db..ca4a458e8b 100644 --- a/components/settings/sanitizerimpl.cpp +++ b/components/settings/sanitizerimpl.cpp @@ -188,6 +188,11 @@ namespace Settings return std::make_unique>(min, max); } + std::unique_ptr> makeClampSanitizerSize(std::size_t min, std::size_t max) + { + return std::make_unique>(min, max); + } + std::unique_ptr> makeClampStrictMaxSanitizerFloat(float min, float max) { return std::make_unique>(min, max); diff --git a/components/settings/sanitizerimpl.hpp b/components/settings/sanitizerimpl.hpp index c2d73d78a9..7913ee1df0 100644 --- a/components/settings/sanitizerimpl.hpp +++ b/components/settings/sanitizerimpl.hpp @@ -31,6 +31,8 @@ namespace Settings std::unique_ptr> makeClampSanitizerInt(int min, int max); + std::unique_ptr> makeClampSanitizerSize(std::size_t min, std::size_t max); + std::unique_ptr> makeClampStrictMaxSanitizerFloat(float min, float max); std::unique_ptr> makeEnumSanitizerInt(std::initializer_list values); diff --git a/docs/source/reference/modding/settings/sound.rst b/docs/source/reference/modding/settings/sound.rst index 4e73df989c..85d7e91c83 100644 --- a/docs/source/reference/modding/settings/sound.rst +++ b/docs/source/reference/modding/settings/sound.rst @@ -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 diff --git a/files/settings-default.cfg b/files/settings-default.cfg index 7643d18a84..bffa486097 100644 --- a/files/settings-default.cfg +++ b/files/settings-default.cfg @@ -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'.