Skip to content

Commit

Permalink
Fix crash when more than one sound emitter use one sound source
Browse files Browse the repository at this point in the history
It crashed when one sound emitter was reading from sound source file and
other emitter was destroying the handle of that file.
  • Loading branch information
Xottab-DUTY committed Mar 13, 2024
1 parent c06415e commit 0c2542f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/xrSound/SoundRender_Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ bool ov_can_continue_read(long res)

void CSoundRender_Source::decompress(void* dest, u32 byte_offset, u32 size)
{
std::lock_guard guard{ read_lock };

if (!wave)
attach();

std::lock_guard guard{ read_lock };

// seek
const auto sample_offset = ogg_int64_t(byte_offset / m_wformat.nBlockAlign);
const u32 cur_pos = u32(ov_pcm_tell(&ovf));
Expand Down Expand Up @@ -145,21 +145,28 @@ constexpr ov_callbacks g_ov_callbacks =

void CSoundRender_Source::attach()
{
VERIFY(0 == wave);
if (wave)
return;
wave = FS.r_open(pname.c_str());
R_ASSERT3(wave && wave->length(), "Can't open wave file:", pname.c_str());
ov_open_callbacks(wave, &ovf, nullptr, 0, g_ov_callbacks);
std::lock_guard guard{ read_lock };
++refs;
VERIFY(refs > 0);
if (refs == 1)
{
wave = FS.r_open(pname.c_str());
R_ASSERT3(wave && wave->length(), "Can't open wave file:", pname.c_str());
ov_open_callbacks(wave, &ovf, nullptr, 0, g_ov_callbacks);
}
}

void CSoundRender_Source::detach()
{
if (wave)
std::lock_guard guard{ read_lock };
--refs;
if (refs == 0)
{
ov_clear(&ovf);
FS.r_close(wave);
}
if (refs < 0)
refs = 0;
}

bool CSoundRender_Source::LoadWave(pcstr pName, bool crashOnError)
Expand Down
1 change: 1 addition & 0 deletions src/xrSound/SoundRender_Source.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class XRSOUND_API CSoundRender_Source final : public CSound_source

OggVorbis_File ovf{};
IReader* wave{};
int refs{};
std::mutex read_lock;

float fTimeTotal;
Expand Down

0 comments on commit 0c2542f

Please sign in to comment.