From 2292eec472b99c264e559e586b21d2e99b6e71a9 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 19 Dec 2013 20:54:53 -0500 Subject: [PATCH] Extract native libraries to the current folder on Windows, so prebuilt jars can be run normally --- src/com/limelight/Limelight.java | 50 +++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/src/com/limelight/Limelight.java b/src/com/limelight/Limelight.java index 6496ff5..a3520ce 100644 --- a/src/com/limelight/Limelight.java +++ b/src/com/limelight/Limelight.java @@ -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(); }