Skip to content

Commit 717fbf6

Browse files
committed
Refactor LibCameraJNILoader to use PhotonJNICommon
1 parent 7d927ac commit 717fbf6

File tree

3 files changed

+24
-50
lines changed

3 files changed

+24
-50
lines changed

photon-core/src/main/java/org/photonvision/common/dataflow/websocket/UIPhotonConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static UIPhotonConfiguration programStateToUi(PhotonConfiguration c) {
5353
new UIGeneralSettings(
5454
PhotonVersion.versionString,
5555
// TODO add support for other types of GPU accel
56-
LibCameraJNILoader.isSupported() ? "Zerocopy Libcamera Working" : "",
56+
LibCameraJNILoader.getInstance().isSupported() ? "Zerocopy Libcamera Working" : "",
5757
MrCalJNILoader.getInstance().isLoaded(),
5858
c.neuralNetworkPropertyManager().getModels(),
5959
NeuralNetworkModelManager.getInstance().getSupportedBackends(),

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

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,37 @@
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 {
31-
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);
24+
/** Helper for extracting photon-libcamera-gl-driver shared library files. */
25+
public class LibCameraJNILoader extends PhotonJNICommon {
26+
private boolean libraryLoaded = false;
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

76-
public static boolean isSupported() {
50+
public boolean isSupported() {
7751
return libraryLoaded && LibCameraJNI.isSupported();
7852
}
7953
}

photon-core/src/main/java/org/photonvision/vision/processes/VisionSourceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ protected List<PVCameraInfo> getConnectedCameras() {
300300
.filter(c -> !(String.join("", c.otherPaths()).contains("csi-video")))
301301
.filter(c -> !c.name().equals("unicam"))
302302
.forEach(cameraInfos::add);
303-
if (LibCameraJNILoader.isSupported()) {
303+
if (LibCameraJNILoader.getInstance().isSupported()) {
304304
// find all CSI cameras (Raspberry Pi cameras)
305305
Stream.of(LibCameraJNI.getCameraNames())
306306
.map(

0 commit comments

Comments
 (0)