Files

118 lines
4.2 KiB
C++

#include "apps/openmw/mwsound/warmqueue.hpp"
#include "apps/openmw/mwsound/headcache.hpp"
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <thread>
#include <gtest/gtest.h>
#include <components/files/istreamptr.hpp>
#include <components/testing/util.hpp>
#include <components/vfs/manager.hpp>
#include <components/vfs/pathutil.hpp>
namespace MWSound
{
namespace
{
constexpr VFS::Path::NormalizedView sEffect("sound/fx/a.wav");
constexpr VFS::Path::NormalizedView sOtherEffect("sound/fx/b.wav");
constexpr VFS::Path::NormalizedView sStreamed("sound/vo/a.wav");
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;
}
void appendLe(std::string& out, std::uint32_t value, std::size_t bytes)
{
for (std::size_t i = 0; i < bytes; ++i)
out.push_back(static_cast<char>((value >> (8 * i)) & 0xff));
}
// Exercise full decoder initialization.
std::string makeWav(std::size_t samples)
{
std::string wav = "RIFF";
appendLe(wav, static_cast<std::uint32_t>(36 + samples * 2), 4);
wav += "WAVEfmt ";
appendLe(wav, 16, 4); // subchunk size
appendLe(wav, 1, 2); // PCM
appendLe(wav, 1, 2); // mono
appendLe(wav, 8000, 4); // sample rate
appendLe(wav, 16000, 4); // byte rate
appendLe(wav, 2, 2); // block align
appendLe(wav, 16, 2); // bits per sample
wav += "data";
appendLe(wav, static_cast<std::uint32_t>(samples * 2), 4);
for (std::size_t i = 0; i < samples; ++i)
appendLe(wav, static_cast<std::uint32_t>(i * 137), 2);
return wav;
}
// Wait for background completion.
template <class Predicate>
bool waitFor(Predicate predicate)
{
for (int i = 0; i < 500 && !predicate(); ++i)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return predicate();
}
TEST(MWSoundWarmQueueTest, warmsWholeEffect)
{
const std::string content = makeContent(64);
TestingOpenMW::VFSTestFile file(content);
const auto vfs = TestingOpenMW::createTestVFS({ { sEffect, &file } });
HeadCache cache(*vfs, 4 * 1024 * 1024);
{
WarmQueue queue(*vfs, cache);
queue.enqueue(VFS::Path::Normalized(sEffect));
EXPECT_TRUE(waitFor([&] { return cache.contains(sEffect); }));
}
const std::shared_ptr<const HeadBuffer> buffer = cache.lookup(sEffect);
ASSERT_NE(buffer, nullptr);
EXPECT_EQ(std::string(buffer->mHead.begin(), buffer->mHead.end()), content);
}
TEST(MWSoundWarmQueueTest, warmsStreamedSound)
{
TestingOpenMW::VFSTestFile file(makeWav(4000));
const auto vfs = TestingOpenMW::createTestVFS({ { sStreamed, &file } });
HeadCache cache(*vfs, 4 * 1024 * 1024);
WarmQueue queue(*vfs, cache);
queue.enqueueStreamed(VFS::Path::Normalized(sStreamed));
EXPECT_TRUE(waitFor([&] { return cache.contains(sStreamed); }));
}
TEST(MWSoundWarmQueueTest, prioritizesUrgentWarm)
{
const std::string content = makeContent(64);
TestingOpenMW::VFSTestFile file(content);
TestingOpenMW::VFSTestFile otherFile(content);
const auto vfs = TestingOpenMW::createTestVFS({ { sEffect, &file }, { sOtherEffect, &otherFile } });
// This budget starts full.
HeadCache cache(*vfs, 512 * 1024);
ASSERT_TRUE(cache.full());
WarmQueue queue(*vfs, cache);
queue.enqueue(VFS::Path::Normalized(sEffect));
queue.enqueue(VFS::Path::Normalized(sOtherEffect), true);
EXPECT_TRUE(waitFor([&] { return cache.contains(sOtherEffect); }));
EXPECT_FALSE(cache.contains(sEffect));
}
}
}