Fixed several audio stream issues

This commit is contained in:
RogueException
2017-04-04 00:47:34 -03:00
parent ac0a31c3be
commit c49118e25f
11 changed files with 89 additions and 98 deletions

View File

@@ -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);
}