Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion NetCord/Gateway/Voice/Streams/OpusDecodeStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public sealed class OpusDecodeStream : RewritingStream
/// <param name="next">The stream that this stream is writing to.</param>
/// <param name="format">The PCM format to decode to.</param>
/// <param name="channels">Number of channels to decode.</param>
public OpusDecodeStream(Stream next, PcmFormat format, VoiceChannels channels) : base(next)
/// <param name="leaveOpen">Whether to leave the next stream open when this stream is disposed.</param>
public OpusDecodeStream(Stream next, PcmFormat format, VoiceChannels channels, bool leaveOpen = false) : base(next, leaveOpen)
{
_decoder = new(channels);
_decode = format switch
Expand Down
9 changes: 5 additions & 4 deletions NetCord/Gateway/Voice/Streams/OpusEncodeStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ namespace NetCord.Gateway.Voice;
/// <param name="channels">Number of channels in input signal.</param>
/// <param name="application">Opus coding mode.</param>
/// <param name="configuration">The configuration of the stream.</param>
public sealed class OpusEncodeStream(Stream next, PcmFormat format, VoiceChannels channels, OpusApplication application, OpusEncodeStreamConfiguration? configuration = null) : RewritingStream(CreateNextStream(next, format, channels, application, configuration))
/// <param name="leaveOpen">Whether to leave the next stream open when this stream is disposed.</param>
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;
}
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions NetCord/Gateway/Voice/Streams/RewritingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
///
/// </summary>
/// <param name="next">The stream that this stream is writing to.</param>
public abstract class RewritingStream(Stream next) : Stream
/// <param name="leaveOpen">Whether to leave the next stream open when this stream is disposed.</param>
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;
Expand Down Expand Up @@ -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();
}
}
Loading