Skip to content

Commit 0b6a951

Browse files
committed
Refactor LibCameraJNILoader to use PhotonJNICommon
1 parent e23df8c commit 0b6a951

File tree

1 file changed

+20
-46
lines changed

1 file changed

+20
-46
lines changed

photon-core/src/main/java/org/photonvision/raspi/LibCameraJNILoader.java

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,34 @@
1717

1818
package org.photonvision.raspi;
1919

20-
import java.io.File;
21-
import java.io.FileOutputStream;
2220
import java.io.IOException;
23-
import org.photonvision.common.logging.LogGroup;
24-
import org.photonvision.common.logging.Logger;
21+
import java.util.List;
22+
import org.photonvision.jni.PhotonJNICommon;
2523

26-
/**
27-
* Helper for extracting photon-libcamera-gl-driver shared library files. TODO: Refactor to use
28-
* PhotonJNICommon
29-
*/
30-
public class LibCameraJNILoader {
24+
/** Helper for extracting photon-libcamera-gl-driver shared library files. */
25+
public class LibCameraJNILoader extends PhotonJNICommon {
3126
private static boolean libraryLoaded = false;
32-
private static final Logger logger = new Logger(LibCameraJNILoader.class, LogGroup.Camera);
33-
34-
public static synchronized void forceLoad() throws IOException {
35-
if (libraryLoaded) return;
36-
37-
var libraryName = "photonlibcamera";
38-
39-
try {
40-
// We always extract the shared object (we could hash each so, but that's a lot of work)
41-
var arch_name = "linuxarm64";
42-
var nativeLibName = System.mapLibraryName(libraryName);
43-
var resourcePath = "/nativelibraries/" + arch_name + "/" + nativeLibName;
44-
var in = LibCameraJNILoader.class.getResourceAsStream(resourcePath);
27+
private static LibCameraJNILoader instance = null;
4528

46-
if (in == null) {
47-
logger.error("Failed to find internal native library at path " + resourcePath);
48-
libraryLoaded = false;
49-
return;
50-
}
29+
public static synchronized LibCameraJNILoader getInstance() {
30+
if (instance == null) instance = new LibCameraJNILoader();
5131

52-
// It's important that we don't mangle the names of these files on Windows at least
53-
File temp = new File(System.getProperty("java.io.tmpdir"), nativeLibName);
54-
FileOutputStream fos = new FileOutputStream(temp);
55-
56-
int read = -1;
57-
byte[] buffer = new byte[1024];
58-
while ((read = in.read(buffer)) != -1) {
59-
fos.write(buffer, 0, read);
60-
}
61-
fos.close();
62-
in.close();
32+
return instance;
33+
}
6334

64-
System.load(temp.getAbsolutePath());
35+
public static synchronized void forceLoad() throws IOException {
36+
forceLoad(
37+
LibCameraJNILoader.getInstance(), LibCameraJNILoader.class, List.of("photonlibcamera"));
38+
}
6539

66-
logger.info("Successfully loaded shared object " + temp.getName());
40+
@Override
41+
public boolean isLoaded() {
42+
return libraryLoaded;
43+
}
6744

68-
} catch (UnsatisfiedLinkError e) {
69-
logger.error("Couldn't load shared object " + libraryName, e);
70-
e.printStackTrace();
71-
// logger.error(System.getProperty("java.library.path"));
72-
}
73-
libraryLoaded = true;
45+
@Override
46+
public void setLoaded(boolean state) {
47+
libraryLoaded = state;
7448
}
7549

7650
public static boolean isSupported() {

0 commit comments

Comments
 (0)