|  | 
| 17 | 17 | 
 | 
| 18 | 18 | package org.photonvision.raspi; | 
| 19 | 19 | 
 | 
| 20 |  | -import java.io.File; | 
| 21 |  | -import java.io.FileOutputStream; | 
| 22 | 20 | 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; | 
| 25 | 23 | 
 | 
| 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; | 
| 45 | 28 | 
 | 
| 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(); | 
| 51 | 31 | 
 | 
| 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 | +    } | 
| 63 | 34 | 
 | 
| 64 |  | -            System.load(temp.getAbsolutePath()); | 
|  | 35 | +    public static synchronized void forceLoad() throws IOException { | 
|  | 36 | +        forceLoad( | 
|  | 37 | +                LibCameraJNILoader.getInstance(), LibCameraJNILoader.class, List.of("photonlibcamera")); | 
|  | 38 | +    } | 
| 65 | 39 | 
 | 
| 66 |  | -            logger.info("Successfully loaded shared object " + temp.getName()); | 
|  | 40 | +    @Override | 
|  | 41 | +    public boolean isLoaded() { | 
|  | 42 | +        return libraryLoaded; | 
|  | 43 | +    } | 
| 67 | 44 | 
 | 
| 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; | 
| 74 | 48 |     } | 
| 75 | 49 | 
 | 
| 76 |  | -    public static boolean isSupported() { | 
|  | 50 | +    public boolean isSupported() { | 
| 77 | 51 |         return libraryLoaded && LibCameraJNI.isSupported(); | 
| 78 | 52 |     } | 
| 79 | 53 | } | 
0 commit comments