Skip to content

Commit

Permalink
Libcamera JNI Maven Support (#1105)
Browse files Browse the repository at this point in the history
Download our libcamera JNI from Maven <3
  • Loading branch information
BytingBulldogs3539 authored Jan 2, 2024
1 parent e4f475a commit 341954c
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 215 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ allprojects {
mavenCentral()
mavenLocal()
maven { url = "https://maven.photonvision.org/repository/internal/" }
maven { url = "https://maven.photonvision.org/repository/snapshots/" }
}
wpilibRepositories.addAllReleaseRepositories(it)
wpilibRepositories.addAllDevelopmentRepositories(it)
Expand All @@ -26,6 +27,7 @@ ext {
openCVversion = "4.8.0-2"
joglVersion = "2.4.0-rc-20200307"
javalinVersion = "5.6.2"
photonGlDriverLibVersion = "dev-v2023.1.0-6-g5e6f7fa"
frcYear = "2024"

pubVersion = versionString
Expand Down
3 changes: 3 additions & 0 deletions photon-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ dependencies {
implementation 'org.zeroturnaround:zt-zip:1.14'

implementation "org.xerial:sqlite-jdbc:3.41.0.0"

implementation "org.photonvision:photon-libcamera-gl-driver-jni:$photonGlDriverLibVersion:linuxarm64"
implementation "org.photonvision:photon-libcamera-gl-driver-java:$photonGlDriverLibVersion"
}

task writeCurrentVersion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.networking.NetworkUtils;
import org.photonvision.common.util.SerializationUtils;
import org.photonvision.raspi.LibCameraJNI;
import org.photonvision.raspi.LibCameraJNILoader;
import org.photonvision.vision.processes.VisionModule;
import org.photonvision.vision.processes.VisionModuleManager;
import org.photonvision.vision.processes.VisionSource;
Expand Down Expand Up @@ -136,7 +136,7 @@ public Map<String, Object> toHashMap() {
generalSubmap.put("version", PhotonVersion.versionString);
generalSubmap.put(
"gpuAcceleration",
LibCameraJNI.isSupported()
LibCameraJNILoader.isSupported()
? "Zerocopy Libcamera Working"
: ""); // TODO add support for other types of GPU accel
generalSubmap.put("hardwareModel", hardwareConfig.deviceName);
Expand Down
210 changes: 0 additions & 210 deletions photon-core/src/main/java/org/photonvision/raspi/LibCameraJNI.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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;

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);

if (in == null) {
logger.error("Failed to find internal native library at path " + resourcePath);
libraryLoaded = false;
return;
}

// 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();

System.load(temp.getAbsolutePath());

logger.info("Successfully loaded shared object " + temp.getName());

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

public static boolean isSupported() {
return libraryLoaded && LibCameraJNI.isSupported();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.photonvision.common.logging.Logger;
import org.photonvision.common.util.TimedTaskManager;
import org.photonvision.raspi.LibCameraJNI;
import org.photonvision.raspi.LibCameraJNILoader;
import org.photonvision.vision.camera.CameraInfo;
import org.photonvision.vision.camera.CameraQuirk;
import org.photonvision.vision.camera.CameraType;
Expand Down Expand Up @@ -98,7 +99,7 @@ protected List<CameraInfo> getConnectedUSBCameras() {
*/
protected List<CameraInfo> getConnectedCSICameras() {
List<CameraInfo> cameraInfos = new ArrayList<CameraInfo>();
if (LibCameraJNI.isSupported())
if (LibCameraJNILoader.isSupported())
for (String path : LibCameraJNI.getCameraNames()) {
String name = LibCameraJNI.getSensorModel(path).getFriendlyName();
cameraInfos.add(
Expand Down
4 changes: 2 additions & 2 deletions photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.photonvision.common.networking.NetworkManager;
import org.photonvision.common.util.TestUtils;
import org.photonvision.common.util.numbers.IntegerCouple;
import org.photonvision.raspi.LibCameraJNI;
import org.photonvision.raspi.LibCameraJNILoader;
import org.photonvision.server.Server;
import org.photonvision.vision.camera.FileVisionSource;
import org.photonvision.vision.opencv.CVMat;
Expand Down Expand Up @@ -311,7 +311,7 @@ public static void main(String[] args) {

try {
if (Platform.isRaspberryPi()) {
LibCameraJNI.forceLoad();
LibCameraJNILoader.forceLoad();
}
} catch (IOException e) {
logger.error("Failed to load libcamera-JNI!", e);
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 341954c

Please sign in to comment.