Fixed several audio stream issues
This commit is contained in:
@@ -26,7 +26,7 @@ namespace Discord.Audio.Streams
|
||||
private static readonly byte[] _silenceFrame = new byte[0];
|
||||
|
||||
private readonly AudioClient _client;
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly AudioStream _next;
|
||||
private readonly CancellationTokenSource _cancelTokenSource;
|
||||
private readonly CancellationToken _cancelToken;
|
||||
private readonly Task _task;
|
||||
@@ -38,9 +38,9 @@ namespace Discord.Audio.Streams
|
||||
private bool _isPreloaded;
|
||||
private int _silenceFrames;
|
||||
|
||||
public BufferedWriteStream(AudioOutStream next, IAudioClient client, int samplesPerFrame, int bufferMillis, CancellationToken cancelToken, int maxFrameSize = 1500)
|
||||
public BufferedWriteStream(AudioStream next, IAudioClient client, int samplesPerFrame, int bufferMillis, CancellationToken cancelToken, int maxFrameSize = 1500)
|
||||
: this(next, client as AudioClient, samplesPerFrame, bufferMillis, cancelToken, null, maxFrameSize) { }
|
||||
internal BufferedWriteStream(AudioOutStream next, AudioClient client, int samplesPerFrame, int bufferMillis, CancellationToken cancelToken, Logger logger, int maxFrameSize = 1500)
|
||||
internal BufferedWriteStream(AudioStream next, AudioClient client, int samplesPerFrame, int bufferMillis, CancellationToken cancelToken, Logger logger, int maxFrameSize = 1500)
|
||||
{
|
||||
//maxFrameSize = 1275 was too limiting at 128kbps,2ch,60ms
|
||||
_next = next;
|
||||
|
||||
@@ -8,11 +8,11 @@ namespace Discord.Audio.Streams
|
||||
{
|
||||
public const int SampleRate = OpusEncodeStream.SampleRate;
|
||||
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly AudioStream _next;
|
||||
private readonly byte[] _buffer;
|
||||
private readonly OpusDecoder _decoder;
|
||||
|
||||
public OpusDecodeStream(AudioOutStream next, int channels = OpusConverter.MaxChannels, int bufferSize = 4000)
|
||||
public OpusDecodeStream(AudioStream next, int channels = OpusConverter.MaxChannels, int bufferSize = 4000)
|
||||
{
|
||||
_next = next;
|
||||
_buffer = new byte[bufferSize];
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Discord.Audio.Streams
|
||||
{
|
||||
public const int SampleRate = 48000;
|
||||
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly AudioStream _next;
|
||||
private readonly OpusEncoder _encoder;
|
||||
private readonly byte[] _buffer;
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Discord.Audio.Streams
|
||||
private byte[] _partialFrameBuffer;
|
||||
private int _partialFramePos;
|
||||
|
||||
public OpusEncodeStream(AudioOutStream next, int channels, int samplesPerFrame, int bitrate, AudioApplication application, int bufferSize = 4000)
|
||||
public OpusEncodeStream(AudioStream next, int channels, int samplesPerFrame, int bitrate, AudioApplication application, int bufferSize = 4000)
|
||||
{
|
||||
_next = next;
|
||||
_encoder = new OpusEncoder(SampleRate, channels, bitrate, application);
|
||||
|
||||
@@ -9,20 +9,19 @@ namespace Discord.Audio.Streams
|
||||
public class RTPReadStream : AudioOutStream
|
||||
{
|
||||
private readonly InputStream _queue;
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly byte[] _buffer, _nonce, _secretKey;
|
||||
private readonly AudioStream _next;
|
||||
private readonly byte[] _buffer, _nonce;
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => true;
|
||||
|
||||
public RTPReadStream(InputStream queue, byte[] secretKey, int bufferSize = 4000)
|
||||
: this(queue, null, secretKey, bufferSize) { }
|
||||
public RTPReadStream(InputStream queue, AudioOutStream next, byte[] secretKey, int bufferSize = 4000)
|
||||
public RTPReadStream(InputStream queue, int bufferSize = 4000)
|
||||
: this(queue, null, bufferSize) { }
|
||||
public RTPReadStream(InputStream queue, AudioStream next, int bufferSize = 4000)
|
||||
{
|
||||
_queue = queue;
|
||||
_next = next;
|
||||
_secretKey = secretKey;
|
||||
_buffer = new byte[bufferSize];
|
||||
_nonce = new byte[24];
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@ namespace Discord.Audio.Streams
|
||||
///<summary> Wraps data in an RTP frame </summary>
|
||||
public class RTPWriteStream : AudioOutStream
|
||||
{
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly AudioStream _next;
|
||||
private readonly byte[] _header;
|
||||
private int _samplesPerFrame;
|
||||
private uint _ssrc, _timestamp = 0;
|
||||
|
||||
protected readonly byte[] _buffer;
|
||||
|
||||
public RTPWriteStream(AudioOutStream next, int samplesPerFrame, uint ssrc, int bufferSize = 4000)
|
||||
public RTPWriteStream(AudioStream next, int samplesPerFrame, uint ssrc, int bufferSize = 4000)
|
||||
{
|
||||
_next = next;
|
||||
_samplesPerFrame = samplesPerFrame;
|
||||
|
||||
@@ -7,18 +7,18 @@ namespace Discord.Audio.Streams
|
||||
///<summary> Decrypts an RTP frame using libsodium </summary>
|
||||
public class SodiumDecryptStream : AudioOutStream
|
||||
{
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly byte[] _buffer, _nonce, _secretKey;
|
||||
private readonly AudioClient _client;
|
||||
private readonly AudioStream _next;
|
||||
private readonly byte[] _nonce;
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => true;
|
||||
|
||||
public SodiumDecryptStream(AudioOutStream next, byte[] secretKey, int bufferSize = 4000)
|
||||
public SodiumDecryptStream(AudioStream next, IAudioClient client)
|
||||
{
|
||||
_next = next;
|
||||
_secretKey = secretKey;
|
||||
_buffer = new byte[bufferSize];
|
||||
_client = (AudioClient)client;
|
||||
_nonce = new byte[24];
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ namespace Discord.Audio.Streams
|
||||
{
|
||||
cancelToken.ThrowIfCancellationRequested();
|
||||
|
||||
Buffer.BlockCopy(buffer, 0, _nonce, 0, 12); //Copy RTP header to nonce
|
||||
count = SecretBox.Decrypt(buffer, offset, count, _buffer, 0, _nonce, _secretKey);
|
||||
if (_client.SecretKey == null)
|
||||
return;
|
||||
|
||||
var newBuffer = new byte[count];
|
||||
Buffer.BlockCopy(_buffer, 0, newBuffer, 0, count);
|
||||
Buffer.BlockCopy(buffer, 0, _nonce, 0, 12); //Copy RTP header to nonce
|
||||
count = SecretBox.Decrypt(buffer, offset + 12, count - 12, buffer, offset + 12, _nonce, _client.SecretKey);
|
||||
await _next.WriteAsync(buffer, 0, count + 12, cancelToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,16 +7,14 @@ namespace Discord.Audio.Streams
|
||||
///<summary> Encrypts an RTP frame using libsodium </summary>
|
||||
public class SodiumEncryptStream : AudioOutStream
|
||||
{
|
||||
private readonly AudioOutStream _next;
|
||||
private readonly byte[] _nonce, _secretKey;
|
||||
private readonly AudioClient _client;
|
||||
private readonly AudioStream _next;
|
||||
private readonly byte[] _nonce;
|
||||
|
||||
//protected readonly byte[] _buffer;
|
||||
|
||||
public SodiumEncryptStream(AudioOutStream next, byte[] secretKey/*, int bufferSize = 4000*/)
|
||||
public SodiumEncryptStream(AudioStream next, IAudioClient client)
|
||||
{
|
||||
_next = next;
|
||||
_secretKey = secretKey;
|
||||
//_buffer = new byte[bufferSize]; //TODO: Can Sodium do an in-place encrypt?
|
||||
_client = (AudioClient)client;
|
||||
_nonce = new byte[24];
|
||||
}
|
||||
|
||||
@@ -24,8 +22,11 @@ namespace Discord.Audio.Streams
|
||||
{
|
||||
cancelToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (_client.SecretKey == null)
|
||||
return;
|
||||
|
||||
Buffer.BlockCopy(buffer, offset, _nonce, 0, 12); //Copy nonce from RTP header
|
||||
count = SecretBox.Encrypt(buffer, offset + 12, count - 12, buffer, 12, _nonce, _secretKey);
|
||||
count = SecretBox.Encrypt(buffer, offset + 12, count - 12, buffer, 12, _nonce, _client.SecretKey);
|
||||
await _next.WriteAsync(buffer, 0, count + 12, cancelToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user