001/*----------------------------------------------------------------------------*/ 002/* Copyright (c) FIRST 2016. All Rights Reserved. */ 003/* Open Source Software - may be modified and shared by FRC teams. The code */ 004/* must be accompanied by the FIRST BSD license file in the root directory of */ 005/* the project. */ 006/*----------------------------------------------------------------------------*/ 007 008package edu.wpi.cscore; 009 010import java.io.File; 011import java.io.InputStream; 012import java.io.OutputStream; 013import java.io.FileOutputStream; 014import java.io.IOException; 015import java.nio.ByteBuffer; 016import java.util.function.Consumer; 017import org.opencv.core.Core; 018 019public class CameraServerJNI { 020 static boolean libraryLoaded = false; 021 static boolean cvLibraryLoaded = false; 022 static File jniLibrary = null; 023 static { 024 if (!libraryLoaded) { 025 try { 026 System.loadLibrary("cscore"); 027 } catch (UnsatisfiedLinkError e) { 028 try { 029 String osname = System.getProperty("os.name"); 030 String resname; 031 if (osname.startsWith("Windows")) 032 resname = "/Windows/" + System.getProperty("os.arch") + "/"; 033 else 034 resname = "/" + osname + "/" + System.getProperty("os.arch") + "/"; 035 System.out.println("platform: " + resname); 036 if (osname.startsWith("Windows")) 037 resname += "cscore.dll"; 038 else if (osname.startsWith("Mac")) 039 resname += "libcscore.dylib"; 040 else 041 resname += "libcscore.so"; 042 InputStream is = CameraServerJNI.class.getResourceAsStream(resname); 043 if (is != null) { 044 // create temporary file 045 if (System.getProperty("os.name").startsWith("Windows")) 046 jniLibrary = File.createTempFile("CameraServerJNI", ".dll"); 047 else if (System.getProperty("os.name").startsWith("Mac")) 048 jniLibrary = File.createTempFile("libCameraServerJNI", ".dylib"); 049 else 050 jniLibrary = File.createTempFile("libCameraServerJNI", ".so"); 051 // flag for delete on exit 052 jniLibrary.deleteOnExit(); 053 OutputStream os = new FileOutputStream(jniLibrary); 054 055 byte[] buffer = new byte[1024]; 056 int readBytes; 057 try { 058 while ((readBytes = is.read(buffer)) != -1) { 059 os.write(buffer, 0, readBytes); 060 } 061 } finally { 062 os.close(); 063 is.close(); 064 } 065 System.load(jniLibrary.getAbsolutePath()); 066 } else { 067 System.loadLibrary("cscore"); 068 } 069 } catch (IOException ex) { 070 ex.printStackTrace(); 071 System.exit(1); 072 } 073 } 074 libraryLoaded = true; 075 if (!cvLibraryLoaded) { 076 try { 077 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 078 } catch (UnsatisfiedLinkError ex) { 079 ex.printStackTrace(); 080 System.exit(1); 081 } 082 cvLibraryLoaded = true; 083 } 084 } 085 } 086 087 // 088 // Property Functions 089 // 090 public static native int getPropertyKind(int property); 091 public static native String getPropertyName(int property); 092 public static native int getProperty(int property); 093 public static native void setProperty(int property, int value); 094 public static native int getPropertyMin(int property); 095 public static native int getPropertyMax(int property); 096 public static native int getPropertyStep(int property); 097 public static native int getPropertyDefault(int property); 098 public static native String getStringProperty(int property); 099 public static native void setStringProperty(int property, String value); 100 public static native String[] getEnumPropertyChoices(int property); 101 102 // 103 // Source Creation Functions 104 // 105 public static native int createUsbCameraDev(String name, int dev); 106 public static native int createUsbCameraPath(String name, String path); 107 public static native int createHttpCamera(String name, String url, int kind); 108 public static native int createHttpCameraMulti(String name, String[] urls, int kind); 109 public static native int createCvSource(String name, int pixelFormat, int width, int height, int fps); 110 111 // 112 // Source Functions 113 // 114 public static native int getSourceKind(int source); 115 public static native String getSourceName(int source); 116 public static native String getSourceDescription(int source); 117 public static native long getSourceLastFrameTime(int source); 118 public static native boolean isSourceConnected(int source); 119 public static native int getSourceProperty(int source, String name); 120 public static native int[] enumerateSourceProperties(int source); 121 public static native VideoMode getSourceVideoMode(int source); 122 public static native boolean setSourceVideoMode(int source, int pixelFormat, int width, int height, int fps); 123 public static native boolean setSourcePixelFormat(int source, int pixelFormat); 124 public static native boolean setSourceResolution(int source, int width, int height); 125 public static native boolean setSourceFPS(int source, int fps); 126 public static native VideoMode[] enumerateSourceVideoModes(int source); 127 public static native int[] enumerateSourceSinks(int source); 128 public static native int copySource(int source); 129 public static native void releaseSource(int source); 130 131 // 132 // Camera Source Common Property Fuctions 133 // 134 public static native void setCameraBrightness(int source, int brightness); 135 public static native int getCameraBrightness(int source); 136 public static native void setCameraWhiteBalanceAuto(int source); 137 public static native void setCameraWhiteBalanceHoldCurrent(int source); 138 public static native void setCameraWhiteBalanceManual(int source, int value); 139 public static native void setCameraExposureAuto(int source); 140 public static native void setCameraExposureHoldCurrent(int source); 141 public static native void setCameraExposureManual(int source, int value); 142 143 // 144 // UsbCamera Source Functions 145 // 146 public static native String getUsbCameraPath(int source); 147 148 // 149 // HttpCamera Source Functions 150 // 151 public static native int getHttpCameraKind(int source); 152 public static native void setHttpCameraUrls(int source, String[] urls); 153 public static native String[] getHttpCameraUrls(int source); 154 155 // 156 // OpenCV Source Functions 157 // 158 public static native void putSourceFrame(int source, long imageNativeObj); 159 public static native void notifySourceError(int source, String msg); 160 public static native void setSourceConnected(int source, boolean connected); 161 public static native void setSourceDescription(int source, String description); 162 public static native int createSourceProperty(int source, String name, int kind, int minimum, int maximum, int step, int defaultValue, int value); 163 public static native void setSourceEnumPropertyChoices(int source, int property, String[] choices); 164 165 // 166 // Sink Creation Functions 167 // 168 public static native int createMjpegServer(String name, String listenAddress, int port); 169 public static native int createCvSink(String name); 170 //public static native int createCvSinkCallback(String name, 171 // void (*processFrame)(long time)); 172 173 // 174 // Sink Functions 175 // 176 public static native int getSinkKind(int sink); 177 public static native String getSinkName(int sink); 178 public static native String getSinkDescription(int sink); 179 public static native void setSinkSource(int sink, int source); 180 public static native int getSinkSourceProperty(int sink, String name); 181 public static native int getSinkSource(int sink); 182 public static native int copySink(int sink); 183 public static native void releaseSink(int sink); 184 185 // 186 // MjpegServer Sink Functions 187 // 188 public static native String getMjpegServerListenAddress(int sink); 189 public static native int getMjpegServerPort(int sink); 190 191 // 192 // OpenCV Sink Functions 193 // 194 public static native void setSinkDescription(int sink, String description); 195 public static native long grabSinkFrame(int sink, long imageNativeObj); 196 public static native String getSinkError(int sink); 197 public static native void setSinkEnabled(int sink, boolean enabled); 198 199 // 200 // Listener Functions 201 // 202 public static native int addListener(Consumer<VideoEvent> listener, 203 int eventMask, boolean immediateNotify); 204 205 public static native void removeListener(int handle); 206 207 // 208 // Logging Functions 209 // 210 @FunctionalInterface 211 public interface LoggerFunction { 212 void apply(int level, String file, int line, String msg); 213 } 214 public static native void setLogger(LoggerFunction func, int minLevel); 215 216 // 217 // Utility Functions 218 // 219 public static native UsbCameraInfo[] enumerateUsbCameras(); 220 221 public static native int[] enumerateSources(); 222 223 public static native int[] enumerateSinks(); 224 225 public static native String getHostname(); 226 227 public static native String[] getNetworkInterfaces(); 228}