-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Decoder.java
139 lines (123 loc) · 4.17 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* 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 < Opus.OPUS_OK)
{
handleDecodeError(result);
return null;
}
short[] audio = new short[result * 2];
decoded.get(audio);
return audio;
}
private void handleDecodeError(int result)
{
StringBuilder b = new StringBuilder("Decoder failed to decode audio from user with code ");
switch (result)
{
case Opus.OPUS_BAD_ARG: //-1
b.append("OPUS_BAD_ARG");
break;
case Opus.OPUS_BUFFER_TOO_SMALL: //-2
b.append("OPUS_BUFFER_TOO_SMALL");
break;
case Opus.OPUS_INTERNAL_ERROR: //-3
b.append("OPUS_INTERNAL_ERROR");
break;
case Opus.OPUS_INVALID_PACKET: //-4
b.append("OPUS_INVALID_PACKET");
break;
case Opus.OPUS_UNIMPLEMENTED: //-5
b.append("OPUS_UNIMPLEMENTED");
break;
case Opus.OPUS_INVALID_STATE: //-6
b.append("OPUS_INVALID_STATE");
break;
case Opus.OPUS_ALLOC_FAIL: //-7
b.append("OPUS_ALLOC_FAIL");
break;
default:
b.append(result);
}
AudioConnection.LOG.debug(b.toString());
}
protected synchronized void close()
{
if (opusDecoder != null)
{
Opus.INSTANCE.opus_decoder_destroy(opusDecoder);
opusDecoder = null;
}
}
@Override
protected void finalize() throws Throwable
{
super.finalize();
close();
}
}