Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static UIPhotonConfiguration programStateToUi(PhotonConfiguration c) {
new UIGeneralSettings(
PhotonVersion.versionString,
// TODO add support for other types of GPU accel
LibCameraJNILoader.isSupported() ? "Zerocopy Libcamera Working" : "",
LibCameraJNILoader.getInstance().isSupported() ? "Zerocopy Libcamera Working" : "",
MrCalJNILoader.getInstance().isLoaded(),
c.neuralNetworkPropertyManager().getModels(),
NeuralNetworkModelManager.getInstance().getSupportedBackends(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,37 @@

package org.photonvision.raspi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import java.util.List;
import org.photonvision.jni.PhotonJNICommon;

/**
* Helper for extracting photon-libcamera-gl-driver shared library files. TODO: Refactor to use
* PhotonJNICommon
*/
public class LibCameraJNILoader {
private static boolean libraryLoaded = false;
private static final Logger logger = new Logger(LibCameraJNILoader.class, LogGroup.Camera);

public static synchronized void forceLoad() throws IOException {
if (libraryLoaded) return;

var libraryName = "photonlibcamera";

try {
// We always extract the shared object (we could hash each so, but that's a lot of work)
var arch_name = "linuxarm64";
var nativeLibName = System.mapLibraryName(libraryName);
var resourcePath = "/nativelibraries/" + arch_name + "/" + nativeLibName;
var in = LibCameraJNILoader.class.getResourceAsStream(resourcePath);
/** Helper for extracting photon-libcamera-gl-driver shared library files. */
public class LibCameraJNILoader extends PhotonJNICommon {
private boolean libraryLoaded = false;
private static LibCameraJNILoader instance = null;

if (in == null) {
logger.error("Failed to find internal native library at path " + resourcePath);
libraryLoaded = false;
return;
}
public static synchronized LibCameraJNILoader getInstance() {
if (instance == null) instance = new LibCameraJNILoader();

// It's important that we don't mangle the names of these files on Windows at least
File temp = new File(System.getProperty("java.io.tmpdir"), nativeLibName);
FileOutputStream fos = new FileOutputStream(temp);

int read = -1;
byte[] buffer = new byte[1024];
while ((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
return instance;
}

System.load(temp.getAbsolutePath());
public static synchronized void forceLoad() throws IOException {
forceLoad(
LibCameraJNILoader.getInstance(), LibCameraJNILoader.class, List.of("photonlibcamera"));
}

logger.info("Successfully loaded shared object " + temp.getName());
@Override
public boolean isLoaded() {
return libraryLoaded;
}

} catch (UnsatisfiedLinkError e) {
logger.error("Couldn't load shared object " + libraryName, e);
e.printStackTrace();
// logger.error(System.getProperty("java.library.path"));
}
libraryLoaded = true;
@Override
public void setLoaded(boolean state) {
libraryLoaded = state;
}

public static boolean isSupported() {
public boolean isSupported() {
return libraryLoaded && LibCameraJNI.isSupported();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ protected List<PVCameraInfo> getConnectedCameras() {
.filter(c -> !(String.join("", c.otherPaths()).contains("csi-video")))
.filter(c -> !c.name().equals("unicam"))
.forEach(cameraInfos::add);
if (LibCameraJNILoader.isSupported()) {
if (LibCameraJNILoader.getInstance().isSupported()) {
// find all CSI cameras (Raspberry Pi cameras)
Stream.of(LibCameraJNI.getCameraNames())
.map(
Expand Down
Loading