diff --git a/NetCord/Gateway/Voice/Streams/OpusDecodeStream.cs b/NetCord/Gateway/Voice/Streams/OpusDecodeStream.cs
index 86bfcc12..45ce831a 100644
--- a/NetCord/Gateway/Voice/Streams/OpusDecodeStream.cs
+++ b/NetCord/Gateway/Voice/Streams/OpusDecodeStream.cs
@@ -18,7 +18,8 @@ public sealed class OpusDecodeStream : RewritingStream
/// The stream that this stream is writing to.
/// The PCM format to decode to.
/// Number of channels to decode.
- public OpusDecodeStream(Stream next, PcmFormat format, VoiceChannels channels) : base(next)
+ /// Whether to leave the next stream open when this stream is disposed.
+ public OpusDecodeStream(Stream next, PcmFormat format, VoiceChannels channels, bool leaveOpen = false) : base(next, leaveOpen)
{
_decoder = new(channels);
_decode = format switch
diff --git a/NetCord/Gateway/Voice/Streams/OpusEncodeStream.cs b/NetCord/Gateway/Voice/Streams/OpusEncodeStream.cs
index 495fa465..c132560f 100644
--- a/NetCord/Gateway/Voice/Streams/OpusEncodeStream.cs
+++ b/NetCord/Gateway/Voice/Streams/OpusEncodeStream.cs
@@ -11,14 +11,15 @@ namespace NetCord.Gateway.Voice;
/// Number of channels in input signal.
/// Opus coding mode.
/// The configuration of the stream.
-public sealed class OpusEncodeStream(Stream next, PcmFormat format, VoiceChannels channels, OpusApplication application, OpusEncodeStreamConfiguration? configuration = null) : RewritingStream(CreateNextStream(next, format, channels, application, configuration))
+/// Whether to leave the next stream open when this stream is disposed.
+public sealed class OpusEncodeStream(Stream next, PcmFormat format, VoiceChannels channels, OpusApplication application, OpusEncodeStreamConfiguration? configuration = null, bool leaveOpen = false) : RewritingStream(CreateNextStream(next, format, channels, application, configuration, leaveOpen))
{
- private static Stream CreateNextStream(Stream next, PcmFormat format, VoiceChannels channels, OpusApplication application, OpusEncodeStreamConfiguration? configuration)
+ private static Stream CreateNextStream(Stream next, PcmFormat format, VoiceChannels channels, OpusApplication application, OpusEncodeStreamConfiguration? configuration, bool leaveOpen)
{
var frameDuration = configuration?.FrameDuration ?? Opus.DefaultFrameDuration;
var segment = configuration?.Segment ?? true;
- OpusEncodeStreamInternal encodeStream = new(next, frameDuration, format, channels, application);
+ OpusEncodeStreamInternal encodeStream = new(next, frameDuration, format, channels, application, leaveOpen);
return segment ? new SegmentingStream(encodeStream, frameDuration, format, channels)
: encodeStream;
}
@@ -142,7 +143,7 @@ private sealed partial class OpusEncodeStreamInternal : RewritingStream
private readonly int _opusBufferSize;
private readonly int _pcmBufferSize;
- public OpusEncodeStreamInternal(Stream next, float frameDuration, PcmFormat format, VoiceChannels channels, OpusApplication application) : base(next)
+ public OpusEncodeStreamInternal(Stream next, float frameDuration, PcmFormat format, VoiceChannels channels, OpusApplication application, bool leaveOpen) : base(next, leaveOpen)
{
_encoder = new(channels, application);
_encode = format switch
diff --git a/NetCord/Gateway/Voice/Streams/RewritingStream.cs b/NetCord/Gateway/Voice/Streams/RewritingStream.cs
index ea0e18eb..6b05b339 100644
--- a/NetCord/Gateway/Voice/Streams/RewritingStream.cs
+++ b/NetCord/Gateway/Voice/Streams/RewritingStream.cs
@@ -4,9 +4,11 @@
///
///
/// The stream that this stream is writing to.
-public abstract class RewritingStream(Stream next) : Stream
+/// Whether to leave the next stream open when this stream is disposed.
+public abstract class RewritingStream(Stream next, bool leaveOpen = false) : Stream
{
private protected readonly Stream _next = next;
+ private readonly bool _leaveOpen = leaveOpen;
public override bool CanRead => false;
public override bool CanSeek => false;
@@ -40,7 +42,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
protected override void Dispose(bool disposing)
{
- if (disposing)
+ if (disposing && !_leaveOpen)
_next.Dispose();
}
}