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

@ -92,12 +92,15 @@ namespace MoonWorks.Audio
{
lock (StateLock)
{
if (State != SoundState.Playing)
if (!IsDisposed)
{
return;
}
if (State != SoundState.Playing)
{
return;
}
QueueBuffers();
QueueBuffers();
}
}
}
@ -182,11 +185,14 @@ namespace MoonWorks.Audio
{
lock (StateLock)
{
StopImmediate();
for (int i = 0; i < BUFFER_COUNT; i += 1)
if (!IsDisposed)
{
NativeMemory.Free((void*) buffers[i]);
StopImmediate();
for (int i = 0; i < BUFFER_COUNT; i += 1)
{
NativeMemory.Free((void*) buffers[i]);
}
}
}
}

View File

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

View File

@ -32,14 +32,20 @@ namespace MoonWorks.Video
) {
var lengthInFloats = bufferLengthInBytes / sizeof(float);
int samples = Theorafile.tf_readaudio(
VideoHandle,
(IntPtr) buffer,
lengthInFloats
);
// 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,
lengthInFloats
);
filledLengthInBytes = samples * sizeof(float);
reachedEnd = Theorafile.tf_eos(VideoHandle) == 1;
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);
}
Theorafile.tf_reset(Video.Handle);
State = VideoState.Stopped;
}
public void Unload()
{
Stop();
Video = null;
}
public void Render()
{
if (Video == null || State == VideoState.Stopped)
@ -200,48 +204,48 @@ namespace MoonWorks.Video
return;
}
timeElapsed += (timer.Elapsed.TotalMilliseconds - lastTimestamp) * PlaybackSpeed;
lastTimestamp = timer.Elapsed.TotalMilliseconds;
int thisFrame = ((int) (timeElapsed / (1000.0 / Video.FramesPerSecond)));
if (thisFrame > currentFrame)
// Theorafile is not thread safe so we have to do this. Fun!
lock (AudioDevice.StateLock)
{
if (Theorafile.tf_readvideo(
Video.Handle,
(IntPtr) yuvData,
thisFrame - currentFrame
) == 1 || currentFrame == -1) {
UpdateRenderTexture();
timeElapsed += (timer.Elapsed.TotalMilliseconds - lastTimestamp) * PlaybackSpeed;
lastTimestamp = timer.Elapsed.TotalMilliseconds;
int thisFrame = ((int) (timeElapsed / (1000.0 / Video.FramesPerSecond)));
if (thisFrame > currentFrame)
{
if (Theorafile.tf_readvideo(
Video.Handle,
(IntPtr) yuvData,
thisFrame - currentFrame
) == 1 || currentFrame == -1)
{
UpdateRenderTexture();
}
currentFrame = thisFrame;
}
currentFrame = thisFrame;
}
bool ended = Theorafile.tf_eos(Video.Handle) == 1;
if (ended)
{
timer.Stop();
timer.Reset();
if (audioStream != null)
bool ended = Theorafile.tf_eos(Video.Handle) == 1;
if (ended)
{
audioStream.Stop();
audioStream.Dispose();
audioStream = null;
}
timer.Stop();
timer.Reset();
Theorafile.tf_reset(Video.Handle);
DestroyAudioStream();
if (Loop)
{
// Start over!
InitializeTheoraStream();
Theorafile.tf_reset(Video.Handle);
timer.Start();
}
else
{
State = VideoState.Stopped;
if (Loop)
{
// Start over!
InitializeTheoraStream();
timer.Start();
}
else
{
State = VideoState.Stopped;
}
}
}
}
@ -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)