Fix Audio Cleanup On Render Abort

This commit is contained in:
copecone committed 2026-10-13 15:37:16 +09:00
1 parent 7ba8944fbe
commit 8b9c009804
2 files changed
+28 -12

No files matched your search

+27 -12
View File
@@ -258,8 +258,10 @@ internal class RenderManager : MonoBehaviour
if (stage != RenderStage.None)
MarkFailed("the game exited or RenderManager was destroyed during rendering.");
ResetRenderState(success: false);
AudioSpectrumGenerator.Clear();
// Cancels the render thread's audio waits, then releases the clip store (decoder thread, readers, chunks).
AudioMixer?.Dispose(); AudioMixer = null;
ResetFrameHookState();
@@ -726,23 +728,35 @@ internal class RenderManager : MonoBehaviour
endSuccessful = !IsRenderAborted;
}
catch (OperationCanceledException) when (IsRenderAborted)
{
// Audio waits end with OperationCanceledException when the render is cancelled or has failed (OfflineAudioMixer.Cancel).
Melon<RenderMod>.Logger.Msg("Render thread stopped: the render was aborted.");
DisposeEncoderAfterStop(videoFFmpeg);
}
catch (Exception ex)
{
Melon<RenderMod>.Logger.Error($"FFmpeg rendering thread error: {ex}");
MarkFailed("encoder thread error. See the log for details.");
try
{
videoFFmpeg.Dispose();
}
catch (Exception disposeEx)
{
Melon<RenderMod>.Logger.Error($"Encoder dispose after thread error also failed: {disposeEx.Message}");
}
UnregisterEncoder(videoFFmpeg);
DisposeEncoderAfterStop(videoFFmpeg);
}
}
/// <summary>Releases the encoder after the render thread stopped early (abort or error) and removes it from the encoder list.</summary>
private void DisposeEncoderAfterStop(FFmpegNativeEncoder videoFFmpeg)
{
try
{
videoFFmpeg.Dispose();
}
catch (Exception disposeEx)
{
Melon<RenderMod>.Logger.Error($"Encoder dispose after the render thread stopped also failed: {disposeEx.Message}");
}
UnregisterEncoder(videoFFmpeg);
}
/// <summary>
/// Takes frames from ImageQueue in passCount order and sends each one to the encoder, followed by the audio
/// samples of that frame. Waits on FrameAvailableEvent (50ms) while no frame is ready.
@@ -873,12 +887,13 @@ internal class RenderManager : MonoBehaviour
private void StopFFmpeg(bool success)
{
endRequested = true;
if (success) AudioMixer?.Complete();
else AudioMixer?.Cancel();
if (!success)
{
MarkCancelled();
}
// The abort state is set first: the render thread reads it when its audio wait is cancelled.
if (success) AudioMixer?.Complete();
else AudioMixer?.Cancel();
FrameAvailableEvent.Set();
skipPressToStart = false;
_renderSetup = false;
+1
View File
@@ -171,6 +171,7 @@ internal static class AudioSpectrumGenerator
if (!_view.IsValid || !_view.Contains(frame))
{
source.Release(_view);
_view = default;
_view = _store!.Acquire(source, frame);
if (!_view.IsValid) return 0.0;
}