From c49129bab5473664772a046ad7db536a619e5e0a Mon Sep 17 00:00:00 2001 From: David Amorim Date: Thu, 22 Aug 2024 17:09:23 +0100 Subject: [PATCH] Remove extension from received audio packets --- .../dv8tion/jda/internal/audio/AudioPacket.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/internal/audio/AudioPacket.java b/src/main/java/net/dv8tion/jda/internal/audio/AudioPacket.java index df9d79b51c..5f1cf4c7ed 100644 --- a/src/main/java/net/dv8tion/jda/internal/audio/AudioPacket.java +++ b/src/main/java/net/dv8tion/jda/internal/audio/AudioPacket.java @@ -84,7 +84,16 @@ public AudioPacket(byte[] rawPacket) for (int i = 0; i < cc; i++) this.csrc[i] = buffer.getInt(); - this.extension = this.hasExtension ? buffer.getInt() : 0; + // Extract extension length as described by https://datatracker.ietf.org/doc/html/rfc3550#section-5.3.1 + if (this.hasExtension) + { + buffer.position(buffer.position() + 2); + this.extension = buffer.getShort(); + } + else + { + this.extension = 0; + } this.headerLength = buffer.position(); this.encodedAudio = buffer; @@ -145,12 +154,14 @@ public static AudioPacket decryptAudioPacket(CryptoAdapter crypto, DatagramPacke return null; byte[] decryptedPayload = crypto.decrypt(encryptedPacket.encodedAudio); + int offset = 4 * encryptedPacket.extension; + return new AudioPacket( null, encryptedPacket.seq, encryptedPacket.timestamp, encryptedPacket.ssrc, - ByteBuffer.wrap(decryptedPayload) + ByteBuffer.wrap(decryptedPayload, offset, decryptedPayload.length - offset).slice() ); }