Cleaned up audio code

This commit is contained in:
RogueException
2017-02-26 10:57:28 -04:00
parent 2db60749ca
commit 8e0c65498b
21 changed files with 495 additions and 276 deletions

View File

@@ -1,33 +1,32 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Discord.Audio
namespace Discord.Audio.Streams
{
internal class RTPWriteStream : AudioOutStream
///<summary> Wraps data in an RTP frame </summary>
public class RTPWriteStream : AudioOutStream
{
private readonly IAudioTarget _target;
private readonly byte[] _nonce, _secretKey;
private readonly AudioOutStream _next;
private readonly byte[] _header;
private int _samplesPerFrame;
private uint _ssrc, _timestamp = 0;
protected readonly byte[] _buffer;
internal RTPWriteStream(IAudioTarget target, byte[] secretKey, int samplesPerFrame, uint ssrc)
internal RTPWriteStream(AudioOutStream next, int samplesPerFrame, uint ssrc, int bufferSize = 4000)
{
_target = target;
_secretKey = secretKey;
_next = next;
_samplesPerFrame = samplesPerFrame;
_ssrc = ssrc;
_buffer = new byte[4000];
_nonce = new byte[24];
_nonce[0] = 0x80;
_nonce[1] = 0x78;
_nonce[8] = (byte)(_ssrc >> 24);
_nonce[9] = (byte)(_ssrc >> 16);
_nonce[10] = (byte)(_ssrc >> 8);
_nonce[11] = (byte)(_ssrc >> 0);
_buffer = new byte[bufferSize];
_header = new byte[24];
_header[0] = 0x80;
_header[1] = 0x78;
_header[8] = (byte)(_ssrc >> 24);
_header[9] = (byte)(_ssrc >> 16);
_header[10] = (byte)(_ssrc >> 8);
_header[11] = (byte)(_ssrc >> 0);
}
public override void Write(byte[] buffer, int offset, int count)
@@ -39,48 +38,28 @@ namespace Discord.Audio
cancellationToken.ThrowIfCancellationRequested();
unchecked
{
if (_nonce[3]++ == byte.MaxValue)
_nonce[2]++;
if (_header[3]++ == byte.MaxValue)
_header[2]++;
_timestamp += (uint)_samplesPerFrame;
_nonce[4] = (byte)(_timestamp >> 24);
_nonce[5] = (byte)(_timestamp >> 16);
_nonce[6] = (byte)(_timestamp >> 8);
_nonce[7] = (byte)(_timestamp >> 0);
_header[4] = (byte)(_timestamp >> 24);
_header[5] = (byte)(_timestamp >> 16);
_header[6] = (byte)(_timestamp >> 8);
_header[7] = (byte)(_timestamp >> 0);
}
Buffer.BlockCopy(_header, 0, _buffer, 0, 12); //Copy RTP header from to the buffer
Buffer.BlockCopy(buffer, offset, _buffer, 12, count);
count = SecretBox.Encrypt(buffer, offset, count, _buffer, 12, _nonce, _secretKey);
Buffer.BlockCopy(_nonce, 0, _buffer, 0, 12); //Copy the RTP header from nonce to buffer
await _target.SendAsync(_buffer, count + 12).ConfigureAwait(false);
await _next.WriteAsync(_buffer, 0, count + 12).ConfigureAwait(false);
}
public override void Flush()
public override async Task FlushAsync(CancellationToken cancelToken)
{
FlushAsync(CancellationToken.None).GetAwaiter().GetResult();
}
public override async Task FlushAsync(CancellationToken cancellationToken)
{
await _target.FlushAsync(cancellationToken).ConfigureAwait(false);
}
public override void Clear()
{
ClearAsync(CancellationToken.None).GetAwaiter().GetResult();
await _next.FlushAsync(cancelToken).ConfigureAwait(false);
}
public override async Task ClearAsync(CancellationToken cancelToken)
{
await _target.ClearAsync(cancelToken).ConfigureAwait(false);
await _next.ClearAsync(cancelToken).ConfigureAwait(false);
}
public override long Length { get { throw new NotSupportedException(); } }
public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
public override void SetLength(long value) { throw new NotSupportedException(); }
public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
}
}