Various audio fixes
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user