/
AudioManagerImpl.java
385 lines (332 loc) · 11.6 KB
/
AudioManagerImpl.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Copyright 2015-2018 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.managers.impl;
import net.dv8tion.jda.annotations.DeprecatedSince;
import net.dv8tion.jda.annotations.ForRemoval;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.audio.AudioConnection;
import net.dv8tion.jda.core.audio.AudioReceiveHandler;
import net.dv8tion.jda.core.audio.AudioSendHandler;
import net.dv8tion.jda.core.audio.SpeakingMode;
import net.dv8tion.jda.core.audio.hooks.ConnectionListener;
import net.dv8tion.jda.core.audio.hooks.ConnectionStatus;
import net.dv8tion.jda.core.audio.hooks.ListenerProxy;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.entities.impl.GuildImpl;
import net.dv8tion.jda.core.entities.impl.JDAImpl;
import net.dv8tion.jda.core.exceptions.GuildUnavailableException;
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.core.managers.AudioManager;
import net.dv8tion.jda.core.utils.Checks;
import net.dv8tion.jda.core.utils.MiscUtil;
import net.dv8tion.jda.core.utils.PermissionUtil;
import java.util.Collection;
import java.util.EnumSet;
import java.util.concurrent.locks.ReentrantLock;
public class AudioManagerImpl implements AudioManager
{
@Deprecated
@ForRemoval
@DeprecatedSince("3.8.1")
public static final ThreadGroup AUDIO_THREADS = new ThreadGroup("jda-audio");
public final ReentrantLock CONNECTION_LOCK = new ReentrantLock();
protected final ListenerProxy connectionListener = new ListenerProxy();
protected final GuildImpl guild;
protected VoiceChannel queuedAudioConnection = null;
protected AudioConnection audioConnection = null;
protected EnumSet<SpeakingMode> speakingModes = EnumSet.of(SpeakingMode.VOICE);
protected AudioSendHandler sendHandler;
protected AudioReceiveHandler receiveHandler;
protected long queueTimeout = 100;
protected boolean shouldReconnect = true;
protected boolean selfMuted = false;
protected boolean selfDeafened = false;
protected long timeout = DEFAULT_CONNECTION_TIMEOUT;
public AudioManagerImpl(GuildImpl guild)
{
this.guild = guild;
}
public AudioConnection getAudioConnection()
{
return audioConnection;
}
@Override
public void openAudioConnection(VoiceChannel channel)
{
Checks.notNull(channel, "Provided VoiceChannel");
// if (!AUDIO_SUPPORTED)
// throw new UnsupportedOperationException("Sorry! Audio is disabled due to an internal JDA error! Contact Dev!");
if (!getGuild().equals(channel.getGuild()))
throw new IllegalArgumentException("The provided VoiceChannel is not a part of the Guild that this AudioManager handles." +
"Please provide a VoiceChannel from the proper Guild");
if (!getGuild().isAvailable())
throw new GuildUnavailableException("Cannot open an Audio Connection with an unavailable guild. " +
"Please wait until this Guild is available to open a connection.");
final Member self = getGuild().getSelfMember();
//if (!self.hasPermission(channel, Permission.VOICE_CONNECT))
// throw new InsufficientPermissionException(Permission.VOICE_CONNECT);
if (audioConnection == null)
{
checkChannel(channel, self);
//Start establishing connection, joining provided channel
queuedAudioConnection = channel;
getJDA().getClient().queueAudioConnect(channel);
}
else
{
//Connection is already established, move to specified channel
//If we are already connected to this VoiceChannel, then do nothing.
if (channel.equals(audioConnection.getChannel()))
return;
checkChannel(channel, self);
getJDA().getClient().queueAudioConnect(channel);
audioConnection.setChannel(channel);
}
}
private void checkChannel(VoiceChannel channel, Member self)
{
EnumSet<Permission> perms = Permission.toEnumSet(PermissionUtil.getEffectivePermission(channel, self));
if (!perms.contains(Permission.VOICE_CONNECT))
throw new InsufficientPermissionException(Permission.VOICE_CONNECT);
final int userLimit = channel.getUserLimit(); // userLimit is 0 if no limit is set!
if (userLimit > 0 && !perms.contains(Permission.ADMINISTRATOR))
{
// Check if we can actually join this channel
// - If there is a userlimit
// - If that userlimit is reached
// - If we don't have voice move others permissions
// VOICE_MOVE_OTHERS allows access because you would be able to move people out to
// open up a slot anyway
if (userLimit <= channel.getMembers().size()
&& !perms.contains(Permission.VOICE_MOVE_OTHERS))
{
throw new InsufficientPermissionException(Permission.VOICE_MOVE_OTHERS,
"Unable to connect to VoiceChannel due to userlimit! Requires permission VOICE_MOVE_OTHERS to bypass");
}
}
}
@Override
public void closeAudioConnection()
{
closeAudioConnection(ConnectionStatus.NOT_CONNECTED);
}
public void closeAudioConnection(ConnectionStatus reason)
{
JDAImpl api = getJDA();
api.getAudioLifeCyclePool().execute(() -> MiscUtil.locked(CONNECTION_LOCK, () ->
{
api.setContext();
this.queuedAudioConnection = null;
if (audioConnection != null)
this.audioConnection.close(reason);
else
this.getJDA().getClient().queueAudioDisconnect(getGuild());
this.audioConnection = null;
}));
}
@Override
public void setSpeakingMode(Collection<SpeakingMode> mode)
{
Checks.notEmpty(mode, "Speaking Mode");
this.speakingModes = EnumSet.copyOf(mode);
if (audioConnection != null)
audioConnection.setSpeakingMode(this.speakingModes);
}
@Override
public EnumSet<SpeakingMode> getSpeakingMode()
{
return EnumSet.copyOf(this.speakingModes);
}
@Override
public JDAImpl getJDA()
{
return getGuild().getJDA();
}
@Override
public GuildImpl getGuild()
{
return guild;
}
@Override
public boolean isAttemptingToConnect()
{
return queuedAudioConnection != null;
}
@Override
public VoiceChannel getQueuedAudioConnection()
{
return queuedAudioConnection;
}
@Override
public VoiceChannel getConnectedChannel()
{
return audioConnection == null ? null : audioConnection.getChannel();
}
@Override
public boolean isConnected()
{
return audioConnection != null;
}
@Override
public void setConnectTimeout(long timeout)
{
this.timeout = timeout;
}
@Override
public long getConnectTimeout()
{
return timeout;
}
@Override
public void setSendingHandler(AudioSendHandler handler)
{
sendHandler = handler;
if (audioConnection != null)
audioConnection.setSendingHandler(handler);
}
@Override
public AudioSendHandler getSendingHandler()
{
return sendHandler;
}
@Override
public void setReceivingHandler(AudioReceiveHandler handler)
{
receiveHandler = handler;
if (audioConnection != null)
audioConnection.setReceivingHandler(handler);
}
@Override
public AudioReceiveHandler getReceiveHandler()
{
return receiveHandler;
}
@Override
public void setConnectionListener(ConnectionListener listener)
{
this.connectionListener.setListener(listener);
}
@Override
public ConnectionListener getConnectionListener()
{
return connectionListener.getListener();
}
@Override
public ConnectionStatus getConnectionStatus()
{
if (audioConnection != null)
return audioConnection.getConnectionStatus();
else
return ConnectionStatus.NOT_CONNECTED;
}
@Override
public void setAutoReconnect(boolean shouldReconnect)
{
this.shouldReconnect = shouldReconnect;
if (audioConnection != null)
audioConnection.setAutoReconnect(shouldReconnect);
}
@Override
public boolean isAutoReconnect()
{
return shouldReconnect;
}
@Override
public void setSelfMuted(boolean muted)
{
if (selfMuted != muted)
{
this.selfMuted = muted;
updateVoiceState();
}
}
@Override
public boolean isSelfMuted()
{
return selfMuted;
}
@Override
public void setSelfDeafened(boolean deafened)
{
if (selfDeafened != deafened)
{
this.selfDeafened = deafened;
updateVoiceState();
}
}
@Override
public boolean isSelfDeafened()
{
return selfDeafened;
}
public ConnectionListener getListenerProxy()
{
return connectionListener;
}
public void setAudioConnection(AudioConnection audioConnection)
{
this.audioConnection = audioConnection;
if (audioConnection == null)
return;
this.queuedAudioConnection = null;
audioConnection.setSendingHandler(sendHandler);
audioConnection.setReceivingHandler(receiveHandler);
audioConnection.setQueueTimeout(queueTimeout);
audioConnection.setSpeakingMode(speakingModes);
}
public void prepareForRegionChange()
{
VoiceChannel queuedChannel = audioConnection.getChannel();
closeAudioConnection(ConnectionStatus.AUDIO_REGION_CHANGE);
this.queuedAudioConnection = queuedChannel;
}
public void setQueuedAudioConnection(VoiceChannel channel)
{
queuedAudioConnection = channel;
}
public void setConnectedChannel(VoiceChannel channel)
{
if (audioConnection != null)
audioConnection.setChannel(channel);
}
public void setQueueTimeout(long queueTimeout)
{
this.queueTimeout = queueTimeout;
if (audioConnection != null)
audioConnection.setQueueTimeout(queueTimeout);
}
protected void updateVoiceState()
{
if (isConnected() || isAttemptingToConnect())
{
VoiceChannel channel = isConnected() ? getConnectedChannel() : getQueuedAudioConnection();
//This is technically equivalent to an audio open/move packet.
getJDA().getClient().queueAudioConnect(channel);
}
}
@Override
@SuppressWarnings("deprecation")
protected void finalize()
{
if (audioConnection != null)
{
LOG.warn("Finalized AudioManager with active audio connection. GuildId: {}", getGuild().getId());
audioConnection.close(ConnectionStatus.DISCONNECTED_REMOVED_FROM_GUILD);
}
audioConnection = null;
}
}