Skip to content

Commit

Permalink
Make sure native library loads use correct class loader
Browse files Browse the repository at this point in the history
Merge of #9934

#minor-release

PiperOrigin-RevId: 429259055
  • Loading branch information
ojw28 authored and icbaker committed Feb 17, 2022
1 parent 5b8c4e0 commit 6310406
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public final class Gav1Library {
ExoPlayerLibraryInfo.registerModule("goog.exo.gav1");
}

private static final LibraryLoader LOADER = new LibraryLoader("gav1JNI");
private static final LibraryLoader LOADER =
new LibraryLoader("gav1JNI") {
@Override
protected void loadLibrary(String name) {
System.loadLibrary(name);
}
};

private Gav1Library() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ public final class FfmpegLibrary {

private static final String TAG = "FfmpegLibrary";

private static final LibraryLoader LOADER = new LibraryLoader("ffmpegJNI");
private static final LibraryLoader LOADER =
new LibraryLoader("ffmpegJNI") {
@Override
protected void loadLibrary(String name) {
System.loadLibrary(name);
}
};

private static @MonotonicNonNull String version;
private static int inputBufferPaddingSize = C.LENGTH_UNSET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public final class FlacLibrary {
ExoPlayerLibraryInfo.registerModule("goog.exo.flac");
}

private static final LibraryLoader LOADER = new LibraryLoader("flacJNI");
private static final LibraryLoader LOADER =
new LibraryLoader("flacJNI") {
@Override
protected void loadLibrary(String name) {
System.loadLibrary(name);
}
};

private FlacLibrary() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public final class OpusLibrary {
ExoPlayerLibraryInfo.registerModule("goog.exo.opus");
}

private static final LibraryLoader LOADER = new LibraryLoader("opusV2JNI");
private static final LibraryLoader LOADER =
new LibraryLoader("opusV2JNI") {
@Override
protected void loadLibrary(String name) {
System.loadLibrary(name);
}
};

private static @C.CryptoType int cryptoType = C.CRYPTO_TYPE_UNSUPPORTED;

private OpusLibrary() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@
@RunWith(AndroidJUnit4.class)
public final class OpusDecoderTest {

private static final LibraryLoader LOADER = new LibraryLoader("opusV2JNI");
private static final LibraryLoader LOADER =
new LibraryLoader("opusV2JNI") {
@Override
protected void loadLibrary(String name) {
System.loadLibrary(name);
}
};

private static final byte[] HEADER =
new byte[] {79, 112, 117, 115, 72, 101, 97, 100, 0, 2, 1, 56, 0, 0, -69, -128, 0, 0, 0};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ public final class VpxLibrary {
ExoPlayerLibraryInfo.registerModule("goog.exo.vpx");
}

private static final LibraryLoader LOADER = new LibraryLoader("vpx", "vpxV2JNI");
private static final LibraryLoader LOADER =
new LibraryLoader("vpx", "vpxV2JNI") {
@Override
protected void loadLibrary(String name) {
System.loadLibrary(name);
}
};

private static @C.CryptoType int cryptoType = C.CRYPTO_TYPE_UNSUPPORTED;

private VpxLibrary() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.util.Arrays;

/** Configurable loader for native libraries. */
public final class LibraryLoader {
public abstract class LibraryLoader {

private static final String TAG = "LibraryLoader";

Expand Down Expand Up @@ -48,7 +48,7 @@ public synchronized boolean isAvailable() {
loadAttempted = true;
try {
for (String lib : nativeLibraries) {
System.loadLibrary(lib);
loadLibrary(lib);
}
isAvailable = true;
} catch (UnsatisfiedLinkError exception) {
Expand All @@ -58,4 +58,17 @@ public synchronized boolean isAvailable() {
}
return isAvailable;
}

/**
* Should be implemented to call {@code System.loadLibrary(name)}.
*
* <p>It's necessary for each subclass to implement this method because {@link
* System#loadLibrary(String)} uses reflection to obtain the calling class, which is then used to
* obtain the class loader to use when loading the native library. If this class were to implement
* the method directly, and if a subclass were to have a different class loader, then loading of
* the native library would fail.
*
* @param name The name of the library to load.
*/
protected abstract void loadLibrary(String name);
}

0 comments on commit 6310406

Please sign in to comment.