Extract native libraries to the current folder on Windows, so prebuilt jars can be run normally

This commit is contained in:
Cameron Gutman
2013-12-19 20:54:53 -05:00
parent 149cabe686
commit 2292eec472

View File

@@ -1,6 +1,12 @@
package com.limelight;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.nio.file.Files;
import java.util.LinkedList;
import javax.swing.JFrame;
@@ -33,6 +39,42 @@ public class Limelight implements NvConnectionListener {
public Limelight(String host) {
this.host = host;
}
private static void extractNativeLibrary(String libraryName, String targetDirectory) throws IOException {
InputStream resource = new Object().getClass().getResourceAsStream("/binlib/"+libraryName);
if (resource == null) {
throw new FileNotFoundException("Unable to find native library in JAR: "+libraryName);
}
File destination = new File(targetDirectory+File.separatorChar+libraryName);
try {
Files.deleteIfExists(destination.toPath());
} catch (IOException e) {
// Try the copy anyway
}
Files.copy(resource, destination.toPath());
}
private static void prepareNativeLibraries() throws IOException {
if (!System.getProperty("os.name").contains("Windows")) {
// Nothing to do for platforms other than Windows
return;
}
// We need to extract nv_avc_dec's runtime dependencies manually
// because the current JRE extracts them with different file names
// so they don't load properly.
String nativeLibDir = ".";
extractNativeLibrary("avfilter-3.dll", nativeLibDir);
extractNativeLibrary("avformat-55.dll", nativeLibDir);
extractNativeLibrary("avutil-52.dll", nativeLibDir);
extractNativeLibrary("postproc-52.dll", nativeLibDir);
extractNativeLibrary("pthreadVC2.dll", nativeLibDir);
extractNativeLibrary("swresample-0.dll", nativeLibDir);
extractNativeLibrary("swscale-2.dll", nativeLibDir);
extractNativeLibrary("avcodec-55.dll", nativeLibDir);
}
private void startUp(boolean fullscreen) {
streamFrame = new StreamFrame();
@@ -42,7 +84,6 @@ public class Limelight implements NvConnectionListener {
VideoDecoderRenderer.FLAG_PREFER_QUALITY,
PlatformBinding.getAudioRenderer(),
PlatformBinding.getVideoDecoderRenderer());
}
private void startControllerListener() {
@@ -133,6 +174,13 @@ public class Limelight implements NvConnectionListener {
System.exit(2);
}
}
try {
prepareNativeLibraries();
} catch (IOException e) {
// This is expected to fail when not in a JAR
}
createFrame();
}