/
AudioNatives.java
120 lines (111 loc) · 3.63 KB
/
AudioNatives.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
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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.api.audio;
import club.minnced.opus.util.OpusLibrary;
import net.dv8tion.jda.internal.utils.JDALogger;
import org.slf4j.Logger;
import java.io.IOException;
/**
* Controller used by JDA to ensure the native
* binaries for opus en-/decoding are available.
*
* @see <a href="https://github.com/discord-java/opus-java" target="_blank">opus-java source</a>
*/
public final class AudioNatives
{
private static final Logger LOG = JDALogger.getLog(AudioNatives.class);
private static boolean initialized;
private static boolean audioSupported;
private AudioNatives() {}
/**
* Whether the opus library is loaded or not.
* <br>This is initialized by the first call to {@link #ensureOpus()}.
*
* @return True, opus library is loaded.
*/
public static boolean isAudioSupported()
{
return audioSupported;
}
/**
* Whether this class was already initialized or not.
*
* @return True, if this class was already initialized.
*
* @see #ensureOpus()
*/
public static boolean isInitialized()
{
return initialized;
}
/**
* Checks whether the opus binary was loaded, if not it will be initialized here.
* <br>This is used by JDA to check at runtime whether the opus library is available or not.
*
* @return True, if the library could be loaded.
*/
public static synchronized boolean ensureOpus()
{
if (initialized)
return audioSupported;
initialized = true;
try
{
if (OpusLibrary.isInitialized())
return audioSupported = true;
audioSupported = OpusLibrary.loadFromJar();
}
catch (Throwable e)
{
handleException(e);
}
finally
{
if (audioSupported)
LOG.info("Audio System successfully setup!");
else
LOG.info("Audio System encountered problems while loading, thus, is disabled.");
}
return audioSupported;
}
private static void handleException(Throwable e)
{
if (e instanceof UnsupportedOperationException)
{
LOG.error("Sorry, JDA's audio system doesn't support this system.\n{}", e.getMessage());
}
else if (e instanceof NoClassDefFoundError)
{
LOG.error("Missing opus dependency, unable to initialize audio!");
}
else if (e instanceof IOException)
{
LOG.error("There was an IO Exception when setting up the temp files for audio.", e);
}
else if (e instanceof UnsatisfiedLinkError)
{
LOG.error("JDA encountered a problem when attempting to load the Native libraries. Contact a DEV.", e);
}
else if (e instanceof Error)
{
throw (Error) e;
}
else
{
LOG.error("An unknown exception occurred while attempting to setup JDA's audio system!", e);
}
}
}