From 22a7b7dbba6a79d3f757540f780a87944f268bd0 Mon Sep 17 00:00:00 2001 From: RogueException Date: Sat, 8 Apr 2017 02:34:12 -0300 Subject: [PATCH] Support more incoming RTP packets types --- .../Audio/Streams/RTPReadStream.cs | 32 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Discord.Net.WebSocket/Audio/Streams/RTPReadStream.cs b/src/Discord.Net.WebSocket/Audio/Streams/RTPReadStream.cs index 72d9fc63b..292a9303a 100644 --- a/src/Discord.Net.WebSocket/Audio/Streams/RTPReadStream.cs +++ b/src/Discord.Net.WebSocket/Audio/Streams/RTPReadStream.cs @@ -29,8 +29,7 @@ namespace Discord.Audio.Streams { cancelToken.ThrowIfCancellationRequested(); - if (buffer[offset + 0] != 0x80 || buffer[offset + 1] != 0x78) - return; + int headerSize = GetHeaderSize(buffer, offset); ushort seq = (ushort)((buffer[offset + 2] << 8) | (buffer[offset + 3] << 0)); @@ -41,16 +40,21 @@ namespace Discord.Audio.Streams (buffer[offset + 7] << 0)); _queue.WriteHeader(seq, timestamp); - await (_next ?? _queue as Stream).WriteAsync(buffer, offset + 12, count - 12, cancelToken).ConfigureAwait(false); + await (_next ?? _queue as Stream).WriteAsync(buffer, offset + headerSize, count - headerSize, cancelToken).ConfigureAwait(false); } public static bool TryReadSsrc(byte[] buffer, int offset, out uint ssrc) { + ssrc = 0; if (buffer.Length - offset < 12) - { - ssrc = 0; return false; - } + + int version = (buffer[offset + 0] & 0b1100_0000) >> 6; + if (version != 2) + return false; + int type = (buffer[offset + 1] & 0b01111_1111); + if (type != 120) //Dynamic Discord type + return false; ssrc = (uint)((buffer[offset + 8] << 24) | (buffer[offset + 9] << 16) | @@ -58,5 +62,21 @@ namespace Discord.Audio.Streams (buffer[offset + 11] << 0)); return true; } + + public static int GetHeaderSize(byte[] buffer, int offset) + { + byte headerByte = buffer[offset]; + bool extension = (headerByte & 0b0001_0000) != 0; + int csics = (headerByte & 0b0000_1111) >> 4; + + if (!extension) + return 12 + csics * 4; + + int extensionOffset = offset + 12 + (csics * 4); + int extensionLength = + (buffer[extensionOffset + 2] << 8) | + (buffer[extensionOffset + 3]); + return extensionOffset + 4 + (extensionLength * 4); + } } }