Revamp API again and start implementing C and Java wrapper shells.

This commit is contained in:
Peter Johnson
2016-08-25 23:13:48 -07:00
parent c25c62e0af
commit aec16a934f
13 changed files with 1720 additions and 273 deletions

View File

@@ -30,11 +30,11 @@ public class CameraServerJNI {
if (is != null) {
// create temporary file
if (System.getProperty("os.name").startsWith("Windows"))
jniLibrary = File.createTempFile("NetworkTablesJNI", ".dll");
jniLibrary = File.createTempFile("CameraServerJNI", ".dll");
else if (System.getProperty("os.name").startsWith("Mac"))
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".dylib");
jniLibrary = File.createTempFile("libCameraServerJNI", ".dylib");
else
jniLibrary = File.createTempFile("libNetworkTablesJNI", ".so");
jniLibrary = File.createTempFile("libCameraServerJNI", ".so");
// flag for delete on exit
jniLibrary.deleteOnExit();
OutputStream os = new FileOutputStream(jniLibrary);
@@ -65,4 +65,113 @@ public class CameraServerJNI {
}
}
//
// Property Functions
//
public static native int getPropertyType(int property);
public static native boolean getBooleanProperty(int property);
public static native void setBooleanProperty(int property, boolean value);
public static native double getDoubleProperty(int property);
public static native void setDoubleProperty(int property, double value);
public static native double getDoublePropertyMin(int property);
public static native double getDoublePropertyMax(int property);
public static native String getStringProperty(int property);
public static native void setStringProperty(int property, String value);
public static native int getEnumProperty(int property);
public static native void setEnumProperty(int property, int value);
public static native String[] getEnumPropertyChoices(int property);
//
// Source/Sink Functions
//
public static native int getSourceProperty(int handle, String name);
//
// Source Creation Functions
//
public static native int createUSBSourceDev(String name, int dev);
public static native int createUSBSourcePath(String name, String path);
public static native int createHTTPSource(String name, String url);
public static native int createCvSource(String name, int numChannels);
//
// Source Functions
//
public static native String getSourceName(int source);
public static native String getSourceDescription(int source);
public static native long getSourceLastFrameTime(int source);
public static native int getSourceNumChannels(int source);
public static native boolean isSourceConnected(int source);
public static native int copySource(int source);
public static native void releaseSource(int source);
//
// OpenCV Source Functions
//
//public static native void putSourceImage(int source, int channel, CvMat image);
public static native void notifySourceFrame(int source);
//public static native void putSourceFrame(int source, CvMat image);
public static native void notifySourceError(int source, String msg);
public static native void setSourceConnected(int source, boolean connected);
public static native int createSourceProperty(int source, String name, int type);
//public static native int createSourcePropertyCallback(int source, String name,
// int type,
// void (*onChange)(String name,
// int property));
public static native void removeSourceProperty(int source, String name);
//
// Sink Creation Functions
//
public static native int createHTTPSink(String name, String listenAddress, int port);
public static native int createCvSink(String name);
//public static native int createCvSinkCallback(String name,
// void (*processFrame)(long time));
//
// Sink Functions
//
public static native String getSinkName(int sink);
public static native String getSinkDescription(int sink);
public static native void setSinkSource(int sink, int source);
public static native int getSinkSource(int sink);
public static native int copySink(int sink);
public static native void releaseSink(int sink);
//
// Server Sink (e.g. HTTP) Functions
//
public static native void setSinkSourceChannel(int sink, int channel);
//
// OpenCV Sink Functions
//
public static native long sinkWaitForFrame(int sink);
//public static native int getSinkImage(int sink, CvMat image);
//public static native int grabSinkFrame(int sink, CvMat image);
public static native String getSinkError(int sink);
public static native void setSinkEnabled(int sink, boolean enabled);
//
// Listener Functions
//
//public static native int AddSourceListener(void (*callback)(String name, int source,
// int event),
// int eventMask);
public static native void RemoveSourceListener(int handle);
//public static native int AddSinkListener(void (*callback)(String name, int sink, int event),
// int eventMask);
public static native void RemoveSinkListener(int handle);
//
// Utility Functions
//
public static native USBCameraInfo[] enumerateUSBCameras();
public static native int[] enumerateSources();
public static native int[] enumerateSinks();
}

View File

@@ -0,0 +1,61 @@
package edu.wpi.cameraserver;
/// A sink for user code to accept video frames as OpenCV images.
public class CvSink extends VideoSink {
/// Create a sink for accepting OpenCV images.
/// WaitForFrame() must be called on the created sink to get each new
/// image.
/// @param name Source name (arbitrary unique identifier)
public CvSink(String name) {
super(CameraServerJNI.createCvSink(name));
}
/// Create a sink for accepting OpenCV images in a separate thread.
/// A thread will be created that calls WaitForFrame() and calls the
/// processFrame() callback each time a new frame arrives.
/// @param name Source name (arbitrary unique identifier)
/// @param processFrame Frame processing function; will be called with a
/// time=0 if an error occurred. processFrame should call GetImage()
/// or GetError() as needed, but should not call (except in very
/// unusual circumstances) WaitForImage().
//public CvSink(llvm::StringRef name,
// std::function<void(uint64_t time)> processFrame) {
// super(CameraServerJNI.createCvSinkCallback(name, processFrame));
//}
/// Wait for the next frame. This is a blocking call.
/// @return Frame time, or 0 on error (call GetError() to obtain the error
/// message).
public long waitForFrame() {
return CameraServerJNI.sinkWaitForFrame(m_handle);
}
/// Get an OpenCV image from the specified channel.
/// @return False if image could not be obtained for some reason (e.g.
/// channel out of range)
//public boolean getImage(int channel, CvMat image) {
// return CameraServerJNI.getSinkImage(m_handle, channel, image);
//}
/// Wait for the next frame and get the image from channel 0. Equivalent
/// to calling WaitForFrame() followed by GetImage(0, image).
/// @return Frame time, or 0 on error (call GetError() to obtain the error
/// message);
//public long frameGrab(CvMat image) {
// return CameraServerJNI.grabSinkFrame(m_handle, image);
//}
/// Get error string. Call this if WaitForFrame() returns 0 to determine
/// what the error is.
public String getError() {
return CameraServerJNI.getSinkError(m_handle);
}
/// Enable or disable getting new frames.
/// Disabling will cause processFrame (for callback-based CvSinks) to not
/// be called and WaitForFrame() to not return. This can be used to save
/// processor resources when frames are not needed.
public void setEnabled(boolean enabled) {
CameraServerJNI.setSinkEnabled(m_handle, enabled);
}
}

View File

@@ -0,0 +1,68 @@
package edu.wpi.cameraserver;
/// A source that represents a video camera.
public class CvSource extends VideoSource {
/// Create an OpenCV source with a single channel.
/// @param name Source name (arbitrary unique identifier)
public CvSource(String name) {
super(CameraServerJNI.createCvSource(name, 1));
}
/// Create an OpenCV source.
/// @param name Source name (arbitrary unique identifier)
/// @param numChannels Number of channels
public CvSource(String name, int numChannels) {
super(CameraServerJNI.createCvSource(name, numChannels));
}
/// Put an OpenCV image onto the specified channel.
/// @param channel Channel number (range 0 to nChannels-1)
/// @param image OpenCV image
//public void putImage(int channel, Mat image);
/// Signal sinks connected to this source that all new channel images have
/// been put to the stream and are ready to be read.
public void notifyFrame() {
CameraServerJNI.notifySourceFrame(m_handle);
}
/// Put an OpenCV image onto channel 0 and notify sinks.
/// This is identical in behavior to calling PutImage(0, image) followed by
/// NotifyFrame().
/// @param image OpenCV image
//public void putFrame(Mat image);
/// Signal sinks that an error has occurred. This should be called instead
/// of NotifyFrame when an error occurs.
public void notifyError(String msg) {
CameraServerJNI.notifySourceError(m_handle, msg);
}
/// Set source connection status. Defaults to true.
/// @param connected True for connected, false for disconnected
public void setConnected(boolean connected) {
CameraServerJNI.setSourceConnected(m_handle, connected);
}
/// Create a property.
/// @param name Property name
/// @param type Property type
/// @return Property
//public VideoProperty createProperty(String name, VideoProperty.Type type);
/// Create a property with a change callback.
/// @param name Property name
/// @param type Property type
/// @param onChange Callback to call when the property value changes
/// @return Property
//public VideoProperty createProperty(
// String name, VideoProperty.Type type,
// std::function<void(String name, VideoProperty property)>
// onChange);
/// Remove a property.
/// @param name Property name
public void removeProperty(String name) {
CameraServerJNI.removeSourceProperty(m_handle, name);
}
}

View File

@@ -0,0 +1,11 @@
package edu.wpi.cameraserver;
/// A source that represents a MJPEG-over-HTTP (IP) camera.
public class HTTPCamera extends VideoSource {
/// Create a source for a MJPEG-over-HTTP (IP) camera.
/// @param name Source name (arbitrary unique identifier)
/// @param url Camera URL (e.g. "http://10.x.y.11/video/stream.mjpg")
public HTTPCamera(String name, String url) {
super(CameraServerJNI.createHTTPSource(name, url));
}
}

View File

@@ -0,0 +1,27 @@
package edu.wpi.cameraserver;
/// A sink that acts as a MJPEG-over-HTTP network server.
public class HTTPSink extends VideoSink {
/// Create a MJPEG-over-HTTP server sink.
/// @param name Sink name (arbitrary unique identifier)
/// @param listenAddress TCP listen address (empty string for all addresses)
/// @param port TCP port number
public HTTPSink(String name, String listenAddress, int port) {
super(CameraServerJNI.createHTTPSink(name, listenAddress, port));
}
/// Create a MJPEG-over-HTTP server sink.
/// @param name Sink name (arbitrary unique identifier)
/// @param port TCP port number
public HTTPSink(String name, int port) {
this(name, "", port);
}
/// Set what video channel should be served.
/// MJPEG-HTTP can only serve a single channel of video.
/// By default, channel 0 is served.
/// @param channel video channel to serve to clients
public void setSourceChannel(int channel) {
CameraServerJNI.setSinkSourceChannel(m_handle, channel);
}
}

View File

@@ -0,0 +1,24 @@
package edu.wpi.cameraserver;
/// A source that represents a USB camera.
public class USBCamera extends VideoSource {
/// Create a source for a USB camera based on device number.
/// @param name Source name (arbitrary unique identifier)
/// @param dev Device number (e.g. 0 for /dev/video0)
public USBCamera(String name, int dev) {
super(CameraServerJNI.createUSBSourceDev(name, dev));
}
/// Create a source for a USB camera based on device path.
/// @param name Source name (arbitrary unique identifier)
/// @param path Path to device (e.g. "/dev/video0" on Linux)
public USBCamera(String name, String path) {
super(CameraServerJNI.createUSBSourcePath(name, path));
}
/// Enumerate USB cameras on the local system.
/// @return Vector of USB camera information (one for each camera)
public static USBCameraInfo[] enumerateUSBCameras() {
return CameraServerJNI.enumerateUSBCameras();
}
}

View File

@@ -0,0 +1,21 @@
package edu.wpi.cameraserver;
/// USB camera information
public class USBCameraInfo {
public USBCameraInfo(int dev, String path, String name, int channels) {
this.dev = dev;
this.path = path;
this.name = name;
this.channels = channels;
}
/// Device number (e.g. N in '/dev/videoN' on Linux)
public int dev;
/// Path to device if available (e.g. '/dev/video0' on Linux)
public String path;
/// Vendor/model name of the camera as provided by the USB driver
public String name;
/// Number of channels the camera provides (usually 1, but some cameras such
/// as stereo or depth cameras may provide multiple channels).
public int channels;
}

View File

@@ -0,0 +1,68 @@
package edu.wpi.cameraserver;
/// A source for video that provides a sequence of frames. Each frame may
/// consist of multiple images (e.g. from a stereo or depth camera); these
/// are called channels.
public class VideoSink {
protected VideoSink(int handle) {
m_handle = handle;
}
public synchronized void free() {
if (m_handle != 0) {
CameraServerJNI.releaseSink(m_handle);
}
m_handle = 0;
}
public boolean isValid() {
return m_handle != 0;
}
/// Get the name of the sink. The name is an arbitrary identifier
/// provided when the sink is created, and should be unique.
public String getName() {
return CameraServerJNI.getSinkName(m_handle);
}
/// Get the sink description. This is sink-type specific.
public String getDescription() {
return CameraServerJNI.getSinkDescription(m_handle);
}
/// Configure which source should provide frames to this sink. Each sink
/// can accept frames from only a single source, but a single source can
/// provide frames to multiple clients.
/// @param source Source
public void setSource(VideoSource source) {
CameraServerJNI.setSinkSource(m_handle, source.m_handle);
}
/// Get the connected source.
/// @return Connected source; nullptr if no source connected.
public VideoSource getSource() {
// While VideoSource.free() will call releaseSource(), getSinkSource()
// increments the internal reference count so this is okay to do.
return new VideoSource(CameraServerJNI.getSinkSource(m_handle));
}
/// Get a property of the associated source.
/// @param name Property name
/// @return Property (type Property::kNone if no property with
/// the given name exists or no source connected)
//public VideoProperty getSourceProperty(String name) {
//}
/// Enumerate all existing sinks.
/// @return Vector of sinks.
public static VideoSink[] enumerateSinks() {
int[] handles = CameraServerJNI.enumerateSinks();
VideoSink[] rv = new VideoSink[handles.length];
for (int i=0; i<handles.length; i++) {
rv[i] = new VideoSink(handles[i]);
}
return rv;
}
protected int m_handle;
}

View File

@@ -0,0 +1,66 @@
package edu.wpi.cameraserver;
/// A source for video that provides a sequence of frames. Each frame may
/// consist of multiple images (e.g. from a stereo or depth camera); these
/// are called channels.
public class VideoSource {
protected VideoSource(int handle) {
m_handle = handle;
}
public synchronized void free() {
if (m_handle != 0) {
CameraServerJNI.releaseSource(m_handle);
}
m_handle = 0;
}
public boolean isValid() {
return m_handle != 0;
}
/// Get the name of the source. The name is an arbitrary identifier
/// provided when the source is created, and should be unique.
public String getName() {
return CameraServerJNI.getSourceName(m_handle);
}
/// Get the source description. This is source-type specific.
public String getDescription() {
return CameraServerJNI.getSourceDescription(m_handle);
}
/// Get the last time a frame was captured.
public long getLastFrameTime() {
return CameraServerJNI.getSourceLastFrameTime(m_handle);
}
/// Get the number of channels this source provides.
public int getNumChannels() {
return CameraServerJNI.getSourceNumChannels(m_handle);
}
/// Is the source currently connected to whatever is providing the images?
public boolean isConnected() {
return CameraServerJNI.isSourceConnected(m_handle);
}
/// Get a property.
/// @param name Property name
/// @return Property contents (of type Property::kNone if no property with
/// the given name exists)
//public VideoProperty getProperty(String name);
/// Enumerate all existing sources.
/// @return Vector of sources.
public static VideoSource[] enumerateSources() {
int[] handles = CameraServerJNI.enumerateSources();
VideoSource[] rv = new VideoSource[handles.length];
for (int i=0; i<handles.length; i++) {
rv[i] = new VideoSource(handles[i]);
}
return rv;
}
protected int m_handle;
}