001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.cscore;
006
007import edu.wpi.first.util.RuntimeLoader;
008import java.io.IOException;
009import java.util.concurrent.atomic.AtomicBoolean;
010import org.opencv.core.Core;
011
012public class CameraServerCvJNI {
013  static boolean libraryLoaded = false;
014
015  static RuntimeLoader<Core> loader = null;
016
017  public static class Helper {
018    private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true);
019
020    public static boolean getExtractOnStaticLoad() {
021      return extractOnStaticLoad.get();
022    }
023
024    public static void setExtractOnStaticLoad(boolean load) {
025      extractOnStaticLoad.set(load);
026    }
027  }
028
029  static {
030    String opencvName = Core.NATIVE_LIBRARY_NAME;
031    if (Helper.getExtractOnStaticLoad()) {
032      try {
033        CameraServerJNI.forceLoad();
034        loader =
035            new RuntimeLoader<>(opencvName, RuntimeLoader.getDefaultExtractionRoot(), Core.class);
036        loader.loadLibraryHashed();
037      } catch (IOException ex) {
038        ex.printStackTrace();
039        System.exit(1);
040      }
041      libraryLoaded = true;
042    }
043  }
044
045  /**
046   * Force load the library.
047   *
048   * @throws IOException if library load failed
049   */
050  public static synchronized void forceLoad() throws IOException {
051    if (libraryLoaded) {
052      return;
053    }
054    CameraServerJNI.forceLoad();
055    loader =
056        new RuntimeLoader<>(
057            Core.NATIVE_LIBRARY_NAME, RuntimeLoader.getDefaultExtractionRoot(), Core.class);
058    loader.loadLibrary();
059    libraryLoaded = true;
060  }
061
062  public static native int createCvSource(
063      String name, int pixelFormat, int width, int height, int fps);
064
065  public static native void putSourceFrame(int source, long imageNativeObj);
066
067  public static native int createCvSink(String name, int pixelFormat);
068
069  // public static native int createCvSinkCallback(String name,
070  //                            void (*processFrame)(long time));
071
072  public static native long grabSinkFrame(int sink, long imageNativeObj);
073
074  public static native long grabSinkFrameTimeout(int sink, long imageNativeObj, double timeout);
075}