Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Externalize opus binaries #659

Merged
merged 26 commits into from
Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d323a33
Moved opus out into external repository
MinnDevelopment Apr 6, 2018
027150f
opus submodule
MinnDevelopment Apr 7, 2018
31046fc
Handling for missing opus dependency
MinnDevelopment Apr 7, 2018
04b813c
Added information on how to exclude opus
MinnDevelopment Apr 7, 2018
3113032
Updated .gitattributes and submodule
MinnDevelopment Apr 7, 2018
fa3e52f
Fix indentation
MinnDevelopment Apr 8, 2018
91d72cf
Updated module
MinnDevelopment Apr 8, 2018
e04d19d
Merge remote-tracking branch 'origin/development' into patch-opus
MinnDevelopment May 21, 2018
f6cdc36
Move opus-java to bintray repository
MinnDevelopment May 21, 2018
5183cea
Update to 1.0.1
MinnDevelopment May 21, 2018
f4c413d
Fix javadoc
MinnDevelopment May 21, 2018
4fb22ad
Add support for optional opus binaries
MinnDevelopment May 21, 2018
179ed1a
Cleanup imports
MinnDevelopment May 21, 2018
e95991f
Add opus-java to dependencies list in README
MinnDevelopment May 22, 2018
7ad1622
Add support for using custom opus library paths
MinnDevelopment May 22, 2018
2982aba
Remove old opus-java module reference
MinnDevelopment May 22, 2018
6bfcd1c
Replace OpusLibrary with AudioNatives and move logic for loading
MinnDevelopment May 29, 2018
6634a53
Add loadFrom to AudioNatives class
MinnDevelopment May 29, 2018
3751763
Update version in README
MinnDevelopment May 30, 2018
15d34d9
Update documentation and allow loadFrom to be used after ensureOpus f…
MinnDevelopment May 30, 2018
3c1eb48
Update README and remove AudioNatives.loadFrom
MinnDevelopment Jun 2, 2018
5cad61a
Update opus-java to 1.0.2
MinnDevelopment Jun 2, 2018
964dc5f
Add artifact without opus natives
MinnDevelopment Jun 3, 2018
1127ea1
Add JNA to exclude path of noOpusJar
MinnDevelopment Jun 3, 2018
32c262a
Exclude opus classes from no-opus artifact
MinnDevelopment Jun 3, 2018
5116ddb
Switch to runtime configuration
MinnDevelopment Jun 3, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
root = true

[*]
charset = utf-8
end_of_line = crlf
insert_final_newline = true
indent_style = space
indent_size = 4

[gradlew]
end_of_line = lf

[*.{java, md, gradle, properties}]
charset = utf-8
indent_style = space
indent_size = 4
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Preserve gradlew's line ending
.gitattributes text eol=crlf

gradlew binary

.git* text eol=crlf
*.java text eol=crlf
*.gradle text eol=crlf
*.bat text eol=crlf
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ Be sure to replace the **VERSION** key below with the one of the versions shown

```

**Maven without Audio**
```xml
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>VERSION</version>
<exclusions>
<exclusion>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
</dependency>
```

**Gradle**
```gradle
dependencies {
Expand All @@ -183,7 +197,17 @@ repositories {
}
```

**Gradle without Audio**
```gradle
dependencies {
compile ('net.dv8tion:JDA:VERSION') {
exclude module: 'opus-java'
}
}
```

The builds are distributed using JCenter through Bintray [JDA JCenter Bintray](https://bintray.com/dv8fromtheworld/maven/JDA/)
<br>If you are not interested in audio, we recommend excluding the `opus-java` transitive dependency to achieve a smaller artifact without audio support.

### Logging Framework - SLF4J
JDA is using [SLF4J](https://www.slf4j.org/) to log its messages.
Expand Down Expand Up @@ -315,6 +339,10 @@ All dependencies are managed automatically by Gradle.
* Version: **1.7.25**
* [Website](https://www.slf4j.org/)
* [JCenter Repository](https://bintray.com/bintray/jcenter/org.slf4j%3Aslf4j-api/view)
* opus-java
* Version: **1.0.0**
* [GitHub](https://github.com/discord-java/opus-java)
* [JCenter Repository](https://bintray.com/minndevelopment/maven/opus-java)

## Related Projects

Expand Down
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dependencies {
compile 'net.sf.trove4j:trove4j:3.0.3'

//Native Library Support
compile 'club.minnced:opus-java:1.0.1'
compile 'net.java.dev.jna:jna:4.4.0'

//Web Connection Support
Expand Down Expand Up @@ -199,9 +200,7 @@ String getProjectProperty(String propertyName)
return property
}

task wrapper(type: Wrapper) {
gradleVersion = '4.2.1'
}
wrapper.gradleVersion = '4.6'

build {
dependsOn clean
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/net/dv8tion/jda/core/audio/AudioConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class AudioConnection
private volatile int silenceCounter = 0;
private boolean sentSilenceOnConnect = false;
private final byte[] silenceBytes = new byte[] {(byte)0xF8, (byte)0xFF, (byte)0xFE};
private static boolean printedError = false;

public AudioConnection(AudioWebSocket webSocket, VoiceChannel channel)
{
Expand Down Expand Up @@ -219,7 +220,7 @@ protected void updateUserSSRC(int ssrc, long userId)
ssrcMap.put(ssrc, userId);

//Only create a decoder if we are actively handling received audio.
if (receiveThread != null)
if (receiveThread != null && AudioNatives.ensureOpus())
opusDecoders.put(ssrc, new Decoder(ssrc));
}
}
Expand Down Expand Up @@ -262,9 +263,6 @@ private synchronized void setupSendSystem()
{
if (udpSocket != null && !udpSocket.isClosed() && sendHandler != null && sendSystem == null)
{
IntBuffer error = IntBuffer.allocate(4);
opusEncoder = Opus.INSTANCE.opus_encoder_create(OPUS_SAMPLE_RATE, OPUS_CHANNEL_COUNT, Opus.OPUS_APPLICATION_AUDIO, error);

IAudioSendFactory factory = ((JDAImpl) channel.getJDA()).getAudioSendFactory();
sendSystem = factory.createSendSystem(new PacketProvider());
sendSystem.setContextMap(contextMap);
Expand Down Expand Up @@ -360,7 +358,15 @@ private synchronized void setupReceiveThread()
}
if (decoder == null)
{
opusDecoders.put(ssrc, decoder = new Decoder(ssrc));
if (AudioNatives.ensureOpus())
{
opusDecoders.put(ssrc, decoder = new Decoder(ssrc));
}
else
{
LOG.error("Unable to decode audio due to missing opus binaries!");
break;
}
}
if (!decoder.isInOrder(decryptedPacket.getSequence()))
{
Expand Down Expand Up @@ -631,6 +637,18 @@ public DatagramPacket getNextPacket(boolean changeTalking)
{
if (!sendHandler.isOpus())
{
if (opusEncoder == null)
{
if (!AudioNatives.ensureOpus())
{
if (!printedError)
LOG.error("Unable to process PCM audio without opus binaries!");
printedError = true;
return null;
}
IntBuffer error = IntBuffer.allocate(4);
opusEncoder = Opus.INSTANCE.opus_encoder_create(OPUS_SAMPLE_RATE, OPUS_CHANNEL_COUNT, Opus.OPUS_APPLICATION_AUDIO, error);
}
rawAudio = encodeToOpus(rawAudio);
}
AudioPacket packet = new AudioPacket(seq, timestamp, webSocket.getSSRC(), rawAudio);
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/net/dv8tion/jda/core/audio/AudioNatives.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.audio;

import club.minnced.opus.util.OpusLibrary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public final class AudioNatives
{
private static final Logger LOG = LoggerFactory.getLogger(AudioNatives.class);
private static boolean initialized;
private static boolean audioSupported;

private AudioNatives() {}

public static synchronized boolean loadFrom(String absolutePath)
{
if (initialized)
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this return audioSupported?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensureOpus() returns audioSupported because it is used by JDA to check whether the library is available or not. loadFrom() returns true if the library is loaded from that method invokation and false if not (it was already loaded).

if (AudioNatives.loadFrom("/lib/.../libopus.so")) {
    log.info("Loaded custom library at /lib/.../libopus.so");
} else {
    log.info("Opus binaries were already loaded, skipping.");
}

initialized = true;
System.load(absolutePath);
System.setProperty("opus.lib", absolutePath);
return audioSupported = true;
}

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package net.dv8tion.jda.core.managers.impl;

import com.sun.jna.Platform;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.audio.AudioConnection;
Expand All @@ -34,20 +33,14 @@
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.NativeUtil;
import net.dv8tion.jda.core.utils.PermissionUtil;

import java.io.IOException;
import java.util.EnumSet;
import java.util.concurrent.locks.ReentrantLock;

public class AudioManagerImpl implements AudioManager
{
public static final ThreadGroup AUDIO_THREADS = new ThreadGroup("jda-audio");
//These values are set at the bottom of this file.
public static boolean AUDIO_SUPPORTED;
public static String OPUS_LIB_NAME;
protected static boolean initialized = false;

public final ReentrantLock CONNECTION_LOCK = new ReentrantLock();

Expand All @@ -71,7 +64,6 @@ public AudioManagerImpl(GuildImpl guild)
{
this.guild = guild;
this.api = this.guild.getJDA();
init(); //Just to make sure that the audio libs have been initialized.
}

public AudioConnection getAudioConnection()
Expand All @@ -84,8 +76,8 @@ 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 (!AUDIO_SUPPORTED)
// throw new UnsupportedOperationException("Sorry! Audio is disabled due to an internal JDA error! Contact Dev!");
if (!guild.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");
Expand Down Expand Up @@ -356,62 +348,4 @@ protected void updateVoiceState()
api.getClient().queueAudioConnect(channel);
}
}

//Load the Opus library.
public static synchronized boolean init()
{
if(initialized)
return AUDIO_SUPPORTED;
initialized = true;
String nativesRoot = null;
try
{
//The libraries that this is referencing are available in the src/main/resources/opus/ folder.
//Of course, when JDA is compiled that just becomes /opus/
nativesRoot = "/natives/" + Platform.RESOURCE_PREFIX + "/%s";
if (nativesRoot.contains("darwin")) //Mac
nativesRoot += ".dylib";
else if (nativesRoot.contains("win"))
nativesRoot += ".dll";
else if (nativesRoot.contains("linux"))
nativesRoot += ".so";
else
throw new UnsupportedOperationException();

NativeUtil.loadLibraryFromJar(String.format(nativesRoot, "libopus"));
}
catch (Throwable e)
{
if (e instanceof UnsupportedOperationException)
LOG.error("Sorry, JDA's audio system doesn't support this system.\n" +
"Supported Systems: Windows(x86, x64), Mac(x86, x64) and Linux(x86, x64)\n" +
"Operating system: " + Platform.RESOURCE_PREFIX);
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
{
LOG.error("An unknown error occurred while attempting to setup JDA's audio system!", e);
}

nativesRoot = null;
}
finally
{
OPUS_LIB_NAME = nativesRoot != null ? String.format(nativesRoot, "libopus") : null;
AUDIO_SUPPORTED = nativesRoot != null;

if (AUDIO_SUPPORTED)
LOG.info("Audio System successfully setup!");
else
LOG.info("Audio System encountered problems while loading, thus, is disabled.");
return AUDIO_SUPPORTED;
}

}
}
Loading