fix audio thread deadlocks

pull/47/head
cosmonaut 2023-03-03 23:56:24 -08:00
parent 52c4fa26c7
commit 8aa9ce550d
7 changed files with 111 additions and 76 deletions

View File

@ -28,7 +28,7 @@ namespace MoonWorks.Audio
}
private readonly List<WeakReference<AudioResource>> resources = new List<WeakReference<AudioResource>>();
private readonly List<WeakReference<StreamingSound>> streamingSounds = new List<WeakReference<StreamingSound>>();
private readonly List<StreamingSound> streamingSounds = new List<StreamingSound>();
private AudioTweenPool AudioTweenPool = new AudioTweenPool();
private readonly List<AudioTween> AudioTweens = new List<AudioTween>();
@ -39,7 +39,7 @@ namespace MoonWorks.Audio
private long previousTickTime;
private Thread Thread;
private AutoResetEvent WakeSignal;
private readonly object StateLock = new object();
internal readonly object StateLock = new object();
private bool IsDisposed;
@ -152,15 +152,7 @@ namespace MoonWorks.Audio
for (var i = streamingSounds.Count - 1; i >= 0; i--)
{
var weakReference = streamingSounds[i];
if (weakReference.TryGetTarget(out var streamingSound))
{
streamingSound.Update();
}
else
{
streamingSounds.RemoveAt(i);
}
streamingSounds[i].Update();
}
for (var i = AudioTweens.Count - 1; i >= 0; i--)
@ -262,7 +254,12 @@ namespace MoonWorks.Audio
private void AddDynamicSoundInstance(StreamingSound instance)
{
streamingSounds.Add(new WeakReference<StreamingSound>(instance));
streamingSounds.Add(instance);
}
private void RemoveDynamicSoundInstance(StreamingSound reference)
{
streamingSounds.Remove(reference);
}
internal void AddResourceReference(AudioResource resource, WeakReference<AudioResource> resourceReference)
@ -278,11 +275,16 @@ namespace MoonWorks.Audio
}
}
internal void RemoveResourceReference(WeakReference<AudioResource> resourceReference)
internal void RemoveResourceReference(AudioResource resource, WeakReference<AudioResource> resourceReference)
{
lock (StateLock)
{
resources.Remove(resourceReference);
if (resource is StreamingSound streamingSound)
{
RemoveDynamicSoundInstance(streamingSound);
}
}
}
@ -314,7 +316,6 @@ namespace MoonWorks.Audio
}
}
// TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
~AudioDevice()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method

View File

@ -28,7 +28,7 @@ namespace MoonWorks.Audio
if (selfReference != null)
{
Device.RemoveResourceReference(selfReference);
Device.RemoveResourceReference(this, selfReference);
selfReference = null;
}

View File

@ -91,6 +91,8 @@ namespace MoonWorks.Audio
internal unsafe void Update()
{
lock (StateLock)
{
if (!IsDisposed)
{
if (State != SoundState.Playing)
{
@ -100,6 +102,7 @@ namespace MoonWorks.Audio
QueueBuffers();
}
}
}
protected void QueueBuffers()
{
@ -181,6 +184,8 @@ namespace MoonWorks.Audio
protected unsafe override void Destroy()
{
lock (StateLock)
{
if (!IsDisposed)
{
StopImmediate();
@ -192,3 +197,4 @@ namespace MoonWorks.Audio
}
}
}
}

View File

@ -88,9 +88,14 @@ namespace MoonWorks.Audio
}
protected unsafe override void Destroy()
{
base.Destroy();
if (!IsDisposed)
{
FAudio.stb_vorbis_close(VorbisHandle);
NativeMemory.Free((void*) FileDataPtr);
}
}
}
}

View File

@ -32,6 +32,11 @@ namespace MoonWorks.Video
) {
var lengthInFloats = bufferLengthInBytes / sizeof(float);
// FIXME: this gets gnarly with theorafile being not thread safe
// is there some way we could just manually update in VideoPlayer
// instead of going through AudioDevice?
lock (Device.StateLock)
{
int samples = Theorafile.tf_readaudio(
VideoHandle,
(IntPtr) buffer,
@ -41,6 +46,7 @@ namespace MoonWorks.Video
filledLengthInBytes = samples * sizeof(float);
reachedEnd = Theorafile.tf_eos(VideoHandle) == 1;
}
}
protected override void OnReachedEnd() { }
}

View File

@ -27,7 +27,7 @@ namespace MoonWorks.Video
private int yWidth;
private int yHeight;
private bool disposed;
private bool IsDisposed;
public Video(string filename)
{
@ -89,7 +89,7 @@ namespace MoonWorks.Video
protected virtual void Dispose(bool disposing)
{
if (!disposed)
if (!IsDisposed)
{
if (disposing)
{
@ -100,7 +100,7 @@ namespace MoonWorks.Video
Theorafile.tf_close(ref Handle);
NativeMemory.Free(videoData);
disposed = true;
IsDisposed = true;
}
}

View File

@ -181,18 +181,22 @@ namespace MoonWorks.Video
lastTimestamp = 0;
timeElapsed = 0;
if (audioStream != null)
lock (AudioDevice.StateLock)
{
audioStream.StopImmediate();
audioStream.Dispose();
audioStream = null;
}
DestroyAudioStream();
Theorafile.tf_reset(Video.Handle);
}
State = VideoState.Stopped;
}
public void Unload()
{
Stop();
Video = null;
}
public void Render()
{
if (Video == null || State == VideoState.Stopped)
@ -200,6 +204,9 @@ namespace MoonWorks.Video
return;
}
// Theorafile is not thread safe so we have to do this. Fun!
lock (AudioDevice.StateLock)
{
timeElapsed += (timer.Elapsed.TotalMilliseconds - lastTimestamp) * PlaybackSpeed;
lastTimestamp = timer.Elapsed.TotalMilliseconds;
@ -210,7 +217,8 @@ namespace MoonWorks.Video
Video.Handle,
(IntPtr) yuvData,
thisFrame - currentFrame
) == 1 || currentFrame == -1) {
) == 1 || currentFrame == -1)
{
UpdateRenderTexture();
}
@ -223,12 +231,7 @@ namespace MoonWorks.Video
timer.Stop();
timer.Reset();
if (audioStream != null)
{
audioStream.Stop();
audioStream.Dispose();
audioStream = null;
}
DestroyAudioStream();
Theorafile.tf_reset(Video.Handle);
@ -245,6 +248,7 @@ namespace MoonWorks.Video
}
}
}
}
private void UpdateRenderTexture()
{
@ -306,14 +310,27 @@ namespace MoonWorks.Video
// Grab the first bit of audio. We're trying to start the decoding ASAP.
if (AudioDevice != null && Theorafile.tf_hasaudio(Video.Handle) == 1)
{
DestroyAudioStream();
int channels, sampleRate;
Theorafile.tf_audioinfo(Video.Handle, out channels, out sampleRate);
audioStream = new StreamingSoundTheora(AudioDevice, Video.Handle, channels, (uint) sampleRate);
}
currentFrame = -1;
}
private void DestroyAudioStream()
{
if (audioStream != null)
{
audioStream.StopImmediate();
audioStream.Dispose();
audioStream = null;
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)