Fix audio clips streaming to be thread-safe when audio sources play

#786
This commit is contained in:
Wojtek Figat
2022-10-29 18:57:57 +02:00
parent 6379171fa6
commit 57e812d336
4 changed files with 10 additions and 5 deletions

View File

@@ -20,10 +20,11 @@ bool AudioClip::StreamingTask::Run()
{
AssetReference<AudioClip> ref = _asset.Get();
if (ref == nullptr)
{
return true;
}
ScopeLock lock(ref->Locker);
const auto& queue = ref->StreamingQueue;
if (queue.Count() == 0)
return false;
auto clip = ref.Get();
// Update the buffers
@@ -108,7 +109,6 @@ bool AudioClip::StreamingTask::Run()
for (int32 sourceIndex = 0; sourceIndex < Audio::Sources.Count(); sourceIndex++)
{
// TODO: collect refs to audio clip from sources and use faster iteration (but do it thread-safe)
const auto src = Audio::Sources[sourceIndex];
if (src->Clip == clip && src->GetState() == AudioSource::States::Playing)
{

View File

@@ -132,7 +132,7 @@ void AudioSource::Play()
Clip->RequestStreamingUpdate();
// If we are looping and streaming also update streaming buffers
if(_loop)
if (_loop)
RequestStreamingBuffersUpdate();
}
}
@@ -372,6 +372,7 @@ void AudioSource::Update()
if (!UseStreaming() || SourceIDs.IsEmpty())
return;
auto clip = Clip.Get();
clip->Locker.Lock();
// Handle streaming buffers queue submit (ensure that clip has loaded the first chunk with audio data)
if (_needToUpdateStreamingBuffers && clip->Buffers[_streamingFirstChunk] != AUDIO_BUFFER_ID_INVALID)
@@ -441,6 +442,8 @@ void AudioSource::Update()
clip->RequestStreamingUpdate();
}
}
clip->Locker.Unlock();
}
void AudioSource::OnEnable()

View File

@@ -139,10 +139,12 @@ namespace ALC
alSourcef(sourceID, AL_GAIN, source->GetVolume());
alSourcef(sourceID, AL_PITCH, source->GetPitch());
alSourcef(sourceID, AL_SEC_OFFSET, 0.0f);
alSourcef(sourceID, AL_REFERENCE_DISTANCE, source->GetMinDistance());
alSourcef(sourceID, AL_ROLLOFF_FACTOR, source->GetAttenuation());
alSourcei(sourceID, AL_LOOPING, loop);
alSourcei(sourceID, AL_SOURCE_RELATIVE, !is3D);
alSourcei(sourceID, AL_BUFFER, 0);
alSource3f(sourceID, AL_POSITION, FLAX_POS_TO_OAL(position));
alSource3f(sourceID, AL_VELOCITY, FLAX_POS_TO_OAL(velocity));
}