Various audio fixes

This commit is contained in:
RogueException
2017-04-09 01:05:52 -03:00
parent aca8def0cb
commit 79fd14a95f
14 changed files with 111 additions and 46 deletions

View File

@@ -10,7 +10,10 @@ namespace Discord.Audio.Streams
private readonly AudioStream _next;
private readonly byte[] _header;
protected readonly byte[] _buffer;
private uint _ssrc, _timestamp = 0;
private uint _ssrc;
private ushort _nextSeq;
private uint _nextTimestamp;
private bool _hasHeader;
public RTPWriteStream(AudioStream next, uint ssrc, int bufferSize = 4000)
{
@@ -26,20 +29,30 @@ namespace Discord.Audio.Streams
_header[11] = (byte)(_ssrc >> 0);
}
public override void WriteHeader(ushort seq, uint timestamp, bool missed)
{
if (_hasHeader)
throw new InvalidOperationException("Header received with no payload");
_hasHeader = true;
_nextSeq = seq;
_nextTimestamp = timestamp;
}
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (!_hasHeader)
throw new InvalidOperationException("Received payload without an RTP header");
_hasHeader = false;
unchecked
{
if (_header[3]++ == byte.MaxValue)
_header[2]++;
_timestamp += (uint)OpusEncoder.FrameSamples;
_header[4] = (byte)(_timestamp >> 24);
_header[5] = (byte)(_timestamp >> 16);
_header[6] = (byte)(_timestamp >> 8);
_header[7] = (byte)(_timestamp >> 0);
_header[2] = (byte)(_nextSeq >> 8);
_header[3] = (byte)(_nextSeq >> 0);
_header[4] = (byte)(_nextTimestamp >> 24);
_header[5] = (byte)(_nextTimestamp >> 16);
_header[6] = (byte)(_nextTimestamp >> 8);
_header[7] = (byte)(_nextTimestamp >> 0);
}
Buffer.BlockCopy(_header, 0, _buffer, 0, 12); //Copy RTP header from to the buffer
Buffer.BlockCopy(buffer, offset, _buffer, 12, count);