Skip to content

Commit 807c849

Browse files
committed
Lock codecs
Supersedes #2880
1 parent 9eeef10 commit 807c849

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

opus-jna/src/main/java/net/dv8tion/jda/internal/audio/OpusJnaDecoder.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,32 @@ public boolean isInOrder(char newSeq)
4848
@Override
4949
public short[] decode(@Nullable AudioPacket decryptedPacket)
5050
{
51-
if (opusDecoder == null)
52-
throw new IllegalStateException("Decoder is closed.");
53-
5451
int result;
5552
ShortBuffer decoded = ShortBuffer.allocate(4096);
56-
if (decryptedPacket == null) //Flag for packet-loss
57-
{
58-
result = Opus.INSTANCE.opus_decode(opusDecoder, null, 0, decoded, OpusPacket.OPUS_FRAME_SIZE, 0);
59-
lastSeq = (char) -1;
60-
lastTimestamp = -1;
61-
}
62-
else
53+
synchronized (this)
6354
{
64-
this.lastSeq = decryptedPacket.getSequence();
65-
this.lastTimestamp = decryptedPacket.getTimestamp();
55+
if (opusDecoder == null)
56+
throw new IllegalStateException("Decoder is closed.");
57+
58+
if (decryptedPacket == null) //Flag for packet-loss
59+
{
60+
result = Opus.INSTANCE.opus_decode(opusDecoder, null, 0, decoded, OpusPacket.OPUS_FRAME_SIZE, 0);
61+
lastSeq = (char) -1;
62+
lastTimestamp = -1;
63+
}
64+
else
65+
{
66+
this.lastSeq = decryptedPacket.getSequence();
67+
this.lastTimestamp = decryptedPacket.getTimestamp();
6668

67-
ByteBuffer encodedAudio = decryptedPacket.getEncodedAudio();
68-
int length = encodedAudio.remaining();
69-
int offset = encodedAudio.arrayOffset() + encodedAudio.position();
70-
byte[] buf = new byte[length];
71-
byte[] data = encodedAudio.array();
72-
System.arraycopy(data, offset, buf, 0, length);
73-
result = Opus.INSTANCE.opus_decode(opusDecoder, buf, buf.length, decoded, OpusPacket.OPUS_FRAME_SIZE, 0);
69+
ByteBuffer encodedAudio = decryptedPacket.getEncodedAudio();
70+
int length = encodedAudio.remaining();
71+
int offset = encodedAudio.arrayOffset() + encodedAudio.position();
72+
byte[] buf = new byte[length];
73+
byte[] data = encodedAudio.array();
74+
System.arraycopy(data, offset, buf, 0, length);
75+
result = Opus.INSTANCE.opus_decode(opusDecoder, buf, buf.length, decoded, OpusPacket.OPUS_FRAME_SIZE, 0);
76+
}
7477
}
7578

7679
//If we get a result that is less than 0, then there was an error. Return null as a signifier.

opus-jna/src/main/java/net/dv8tion/jda/internal/audio/OpusJnaEncoder.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,29 @@ protected OpusJnaEncoder(PointerByReference opusEncoder)
4343
@Override
4444
public ByteBuffer encode(@Nonnull ByteBuffer rawAudio)
4545
{
46-
if (opusEncoder == null)
47-
throw new IllegalStateException("Encoder is closed.");
48-
49-
ShortBuffer nonEncodedBuffer = ShortBuffer.allocate(rawAudio.remaining() / 2);
46+
int result;
5047
ByteBuffer encoded = ByteBuffer.allocate(4096);
51-
for (int i = rawAudio.position(); i < rawAudio.limit(); i += 2)
48+
synchronized (this)
5249
{
53-
int firstByte = (0x000000FF & rawAudio.get(i)); //Promotes to int and handles the fact that it was unsigned.
54-
int secondByte = (0x000000FF & rawAudio.get(i + 1));
50+
if (opusEncoder == null)
51+
throw new IllegalStateException("Encoder is closed.");
52+
53+
ShortBuffer nonEncodedBuffer = ShortBuffer.allocate(rawAudio.remaining() / 2);
54+
for (int i = rawAudio.position(); i < rawAudio.limit(); i += 2)
55+
{
56+
int firstByte = (0x000000FF & rawAudio.get(i)); //Promotes to int and handles the fact that it was unsigned.
57+
int secondByte = (0x000000FF & rawAudio.get(i + 1));
58+
59+
//Combines the 2 bytes into a short. Opus deals with unsigned shorts, not bytes.
60+
short toShort = (short) ((firstByte << 8) | secondByte);
5561

56-
//Combines the 2 bytes into a short. Opus deals with unsigned shorts, not bytes.
57-
short toShort = (short) ((firstByte << 8) | secondByte);
62+
nonEncodedBuffer.put(toShort);
63+
}
64+
((Buffer) nonEncodedBuffer).flip();
5865

59-
nonEncodedBuffer.put(toShort);
66+
result = Opus.INSTANCE.opus_encode(opusEncoder, nonEncodedBuffer, OpusPacket.OPUS_FRAME_SIZE, encoded, encoded.capacity());
6067
}
61-
((Buffer) nonEncodedBuffer).flip();
6268

63-
int result = Opus.INSTANCE.opus_encode(opusEncoder, nonEncodedBuffer, OpusPacket.OPUS_FRAME_SIZE, encoded, encoded.capacity());
6469
if (result <= 0)
6570
{
6671
LOG.error("Received error code from opus_encode(...): {}", result);

0 commit comments

Comments
 (0)