/
Decoder.java
104 lines (89 loc) · 3 KB
/
Decoder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter & Florian Spieß
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.core.audio;
import com.sun.jna.ptr.PointerByReference;
import tomp2p.opuswrapper.Opus;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
/**
* Class that wraps functionality around the Opus decoder.
*/
public class Decoder
{
protected int ssrc;
protected char lastSeq;
protected int lastTimestamp;
protected PointerByReference opusDecoder;
protected Decoder(int ssrc)
{
this.ssrc = ssrc;
this.lastSeq = (char) -1;
this.lastTimestamp = -1;
IntBuffer error = IntBuffer.allocate(4);
opusDecoder = Opus.INSTANCE.opus_decoder_create(AudioConnection.OPUS_SAMPLE_RATE,
AudioConnection.OPUS_CHANNEL_COUNT, error);
//TODO: check `error` for an error flag.
}
protected boolean isInOrder(char newSeq)
{
return lastSeq == -1 || newSeq > lastSeq || lastSeq - newSeq > 10;
}
protected boolean wasPacketLost(char newSeq)
{
return newSeq > lastSeq + 1;
}
protected short[] decodeFromOpus(AudioPacket decryptedPacket)
{
int result;
ShortBuffer decoded = ShortBuffer.allocate(4096);
if (decryptedPacket == null) //Flag for packet-loss
{
result = Opus.INSTANCE.opus_decode(opusDecoder, null, 0, decoded,
AudioConnection.OPUS_FRAME_SIZE, 0);
lastSeq = (char) -1;
lastTimestamp = -1;
}
else
{
char seq = decryptedPacket.getSequence();
this.lastSeq = seq;
this.lastTimestamp = decryptedPacket.getTimestamp();
byte[] encodedAudio = decryptedPacket.getEncodedAudio();
result = Opus.INSTANCE.opus_decode(opusDecoder, encodedAudio, encodedAudio.length, decoded,
AudioConnection.OPUS_FRAME_SIZE, 0);
}
//If we get a result that is less than 0, then there was an error. Return null as a signifier.
if (result < 0)
return null;
short[] audio = new short[result * 2];
decoded.get(audio);
return audio;
}
protected synchronized void close()
{
if (opusDecoder != null)
{
Opus.INSTANCE.opus_decoder_destroy(opusDecoder);
opusDecoder = null;
}
}
@Override
protected void finalize() throws Throwable
{
super.finalize();
close();
}
}