mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Implement listener interfaces.
This commit is contained in:
@@ -22,6 +22,29 @@ using namespace wpi::java;
|
||||
static JavaVM *jvm = nullptr;
|
||||
static jclass usbCameraInfoCls = nullptr;
|
||||
static jclass videoModeCls = nullptr;
|
||||
static jclass videoEventCls = nullptr;
|
||||
// Thread-attached environment for listener callbacks.
|
||||
static JNIEnv *listenerEnv = nullptr;
|
||||
|
||||
static void ListenerOnStart() {
|
||||
if (!jvm) return;
|
||||
JNIEnv *env;
|
||||
JavaVMAttachArgs args;
|
||||
args.version = JNI_VERSION_1_2;
|
||||
args.name = const_cast<char*>("CSListener");
|
||||
args.group = nullptr;
|
||||
if (jvm->AttachCurrentThreadAsDaemon(reinterpret_cast<void **>(&env),
|
||||
&args) != JNI_OK)
|
||||
return;
|
||||
if (!env || !env->functions) return;
|
||||
listenerEnv = env;
|
||||
}
|
||||
|
||||
static void ListenerOnExit() {
|
||||
listenerEnv = nullptr;
|
||||
if (!jvm) return;
|
||||
jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
@@ -47,6 +70,16 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
|
||||
if (!videoModeCls) return JNI_ERR;
|
||||
env->DeleteLocalRef(local);
|
||||
|
||||
local = env->FindClass("edu/wpi/cameraserver/VideoEvent");
|
||||
if (!local) return JNI_ERR;
|
||||
videoEventCls = static_cast<jclass>(env->NewGlobalRef(local));
|
||||
if (!videoEventCls) return JNI_ERR;
|
||||
env->DeleteLocalRef(local);
|
||||
|
||||
// Initial configuration of listener start/exit
|
||||
cs::SetListenerOnStart(ListenerOnStart);
|
||||
cs::SetListenerOnExit(ListenerOnExit);
|
||||
|
||||
return JNI_VERSION_1_6;
|
||||
}
|
||||
|
||||
@@ -57,11 +90,43 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
|
||||
// Delete global references
|
||||
if (usbCameraInfoCls) env->DeleteGlobalRef(usbCameraInfoCls);
|
||||
if (videoModeCls) env->DeleteGlobalRef(videoModeCls);
|
||||
if (videoEventCls) env->DeleteGlobalRef(videoEventCls);
|
||||
jvm = nullptr;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
//
|
||||
// Helper class to create and clean up a global reference
|
||||
//
|
||||
template <typename T>
|
||||
class JGlobal {
|
||||
public:
|
||||
JGlobal(JNIEnv *env, T obj)
|
||||
: m_obj(static_cast<T>(env->NewGlobalRef(obj))) {}
|
||||
~JGlobal() {
|
||||
if (!jvm || cs::NotifierDestroyed()) return;
|
||||
JNIEnv *env;
|
||||
bool attached = false;
|
||||
// don't attach and de-attach if already attached to a thread.
|
||||
if (jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) ==
|
||||
JNI_EDETACHED) {
|
||||
if (jvm->AttachCurrentThread(reinterpret_cast<void **>(&env), nullptr) !=
|
||||
JNI_OK)
|
||||
return;
|
||||
attached = true;
|
||||
}
|
||||
if (!env || !env->functions) return;
|
||||
env->DeleteGlobalRef(m_obj);
|
||||
if (attached) jvm->DetachCurrentThread();
|
||||
}
|
||||
operator T() { return m_obj; }
|
||||
T obj() { return m_obj; }
|
||||
|
||||
private:
|
||||
T m_obj;
|
||||
};
|
||||
|
||||
static void ReportError(JNIEnv *env, CS_Status status, bool do_throw = true) {
|
||||
// TODO
|
||||
}
|
||||
@@ -90,6 +155,29 @@ static jobject MakeJObject(JNIEnv *env, const cs::VideoMode &videoMode) {
|
||||
static_cast<jint>(videoMode.fps));
|
||||
}
|
||||
|
||||
static jobject MakeJObject(JNIEnv *env, const cs::RawEvent &event) {
|
||||
static jmethodID constructor =
|
||||
env->GetMethodID(videoEventCls, "<init>",
|
||||
"(IIILjava/lang/String;IIIIIIILjava/lang/String;)V");
|
||||
JLocal<jstring> name(env, MakeJString(env, event.name));
|
||||
JLocal<jstring> valueStr(env, MakeJString(env, event.valueStr));
|
||||
return env->NewObject(
|
||||
videoEventCls,
|
||||
constructor,
|
||||
static_cast<jint>(event.type),
|
||||
static_cast<jint>(event.sourceHandle),
|
||||
static_cast<jint>(event.sinkHandle),
|
||||
name.obj(),
|
||||
static_cast<jint>(event.mode.pixelFormat),
|
||||
static_cast<jint>(event.mode.width),
|
||||
static_cast<jint>(event.mode.height),
|
||||
static_cast<jint>(event.mode.fps),
|
||||
static_cast<jint>(event.propertyHandle),
|
||||
static_cast<jint>(event.propertyType),
|
||||
static_cast<jint>(event.value),
|
||||
valueStr.obj());
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
/*
|
||||
@@ -798,6 +886,57 @@ JNIEXPORT void JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_setSinkEnabled
|
||||
CheckStatus(env, status);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: addListener
|
||||
* Signature: (Ledu/wpi/cameraserver/CameraServerJNI/ConnectionListenerFunction;IZ)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_edu_wpi_cameraserver_CameraServerJNI_addListener
|
||||
(JNIEnv *envouter, jclass, jobject listener, jint eventMask, jboolean immediateNotify)
|
||||
{
|
||||
// the shared pointer to the weak global will keep it around until the
|
||||
// entry listener is destroyed
|
||||
auto listener_global =
|
||||
std::make_shared<JGlobal<jobject>>(envouter, listener);
|
||||
|
||||
// cls is a temporary here; cannot be used within callback functor
|
||||
jclass cls = envouter->GetObjectClass(listener);
|
||||
if (!cls) return 0;
|
||||
|
||||
// method ids, on the other hand, are safe to retain
|
||||
jmethodID mid = envouter->GetMethodID(cls, "apply",
|
||||
"(Ledu/wpi/cameraserver/VideoEvent;)V");
|
||||
if (!mid) return 0;
|
||||
|
||||
CS_Status status = 0;
|
||||
CS_Listener handle = cs::AddListener(
|
||||
[=](const cs::RawEvent &event) {
|
||||
JNIEnv *env = listenerEnv;
|
||||
if (!env || !env->functions) return;
|
||||
|
||||
// get the handler
|
||||
auto handler = listener_global->obj();
|
||||
|
||||
// convert into the appropriate Java type
|
||||
JLocal<jobject> jobj{env, MakeJObject(env, event)};
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
return;
|
||||
}
|
||||
if (!jobj) return;
|
||||
|
||||
env->CallVoidMethod(handler, mid, jobj.obj());
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
}
|
||||
},
|
||||
eventMask, immediateNotify != JNI_FALSE, &status);
|
||||
CheckStatus(envouter, status);
|
||||
return handle;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cameraserver_CameraServerJNI
|
||||
* Method: removeListener
|
||||
|
||||
@@ -121,9 +121,6 @@ public class CameraServerJNI {
|
||||
public static native void setSourceConnected(int source, boolean connected);
|
||||
public static native void setSourceDescription(int source, String description);
|
||||
public static native int createSourceProperty(int source, String name, int type, int minimum, int maximum, int step, int defaultValue, int value);
|
||||
//public static native int createSourcePropertyCallback(int source, String name,
|
||||
// int type, int minimum, int maximum, int step, int defaultValue, int value,
|
||||
// void (*onChange)(int property));
|
||||
public static native void setSourceEnumPropertyChoices(int source, int property, String[] choices);
|
||||
|
||||
//
|
||||
@@ -156,8 +153,8 @@ public class CameraServerJNI {
|
||||
//
|
||||
// Listener Functions
|
||||
//
|
||||
//public static native int addListener(void (*callback)(VideoEvent event),
|
||||
// int eventMask);
|
||||
public static native int addListener(VideoListenerFunction listener,
|
||||
int eventMask, boolean immediateNotify);
|
||||
|
||||
public static native void removeListener(int handle);
|
||||
|
||||
|
||||
83
java/src/edu/wpi/cameraserver/VideoEvent.java
Normal file
83
java/src/edu/wpi/cameraserver/VideoEvent.java
Normal file
@@ -0,0 +1,83 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cameraserver;
|
||||
|
||||
/// Video event
|
||||
public class VideoEvent {
|
||||
public enum Type {
|
||||
kSourceCreated(0x0001),
|
||||
kSourceDestroyed(0x0002),
|
||||
kSourceConnected(0x0004),
|
||||
kSourceDisconnected(0x0008),
|
||||
kSourceVideoModesUpdated(0x0010),
|
||||
kSourceVideoModeChanged(0x0020),
|
||||
kSinkCreated(0x0100),
|
||||
kSinkDestroyed(0x0200),
|
||||
kSinkEnabled(0x0400),
|
||||
kSinkDisabled(0x0800),
|
||||
kSourcePropertyCreated(0x1000),
|
||||
kSourcePropertyValueUpdated(0x2000),
|
||||
kSourcePropertyChoicesUpdated(0x4000);
|
||||
|
||||
private int value;
|
||||
|
||||
private Type(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
private static final Type[] m_typeValues = Type.values();
|
||||
|
||||
VideoEvent(int type, int source, int sink, String name, int pixelFormat,
|
||||
int width, int height, int fps, int property, int propertyType,
|
||||
int value, String valueStr) {
|
||||
this.type = m_typeValues[type];
|
||||
this.sourceHandle = source;
|
||||
this.sinkHandle = sink;
|
||||
this.name = name;
|
||||
this.mode = new VideoMode(pixelFormat, width, height, fps);
|
||||
this.propertyHandle = property;
|
||||
this.propertyType = VideoProperty.m_typeValues[propertyType];
|
||||
this.value = value;
|
||||
this.valueStr = valueStr;
|
||||
}
|
||||
|
||||
public Type type;
|
||||
|
||||
// Valid for kSource* and kSink* respectively
|
||||
private int sourceHandle;
|
||||
private int sinkHandle;
|
||||
|
||||
// Source/sink name
|
||||
public String name;
|
||||
|
||||
// Fields for kSourceVideoModeChanged event
|
||||
public VideoMode mode;
|
||||
|
||||
// Fields for kSourceProperty* events
|
||||
private int propertyHandle;
|
||||
public VideoProperty.Type propertyType;
|
||||
public int value;
|
||||
public String valueStr;
|
||||
|
||||
public VideoSource getSource() {
|
||||
return new VideoSource(CameraServerJNI.copySource(sourceHandle));
|
||||
}
|
||||
|
||||
public VideoSink getSink() {
|
||||
return new VideoSink(CameraServerJNI.copySink(sinkHandle));
|
||||
}
|
||||
|
||||
public VideoProperty getProperty() {
|
||||
return new VideoProperty(propertyHandle, propertyType);
|
||||
}
|
||||
|
||||
}
|
||||
35
java/src/edu/wpi/cameraserver/VideoListener.java
Normal file
35
java/src/edu/wpi/cameraserver/VideoListener.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cameraserver;
|
||||
|
||||
/// An event listener. This calls back to a desigated callback function when
|
||||
/// an event matching the specified mask is generated by the library.
|
||||
public class VideoListener {
|
||||
/// Create an event listener.
|
||||
/// @param listener Listener function
|
||||
/// @param eventMask Bitmask of VideoEvent.Type values
|
||||
/// @param immediateNotify Whether callback should be immediately called with
|
||||
/// a representative set of events for the current library state.
|
||||
public VideoListener(VideoListenerFunction listener, int eventMask,
|
||||
boolean immediateNotify) {
|
||||
m_handle = CameraServerJNI.addListener(listener, eventMask, immediateNotify);
|
||||
}
|
||||
|
||||
public synchronized void free() {
|
||||
if (m_handle != 0) {
|
||||
CameraServerJNI.removeListener(m_handle);
|
||||
}
|
||||
m_handle = 0;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return m_handle != 0;
|
||||
}
|
||||
|
||||
private int m_handle;
|
||||
}
|
||||
12
java/src/edu/wpi/cameraserver/VideoListenerFunction.java
Normal file
12
java/src/edu/wpi/cameraserver/VideoListenerFunction.java
Normal file
@@ -0,0 +1,12 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.cameraserver;
|
||||
|
||||
public interface VideoListenerFunction {
|
||||
void apply(VideoEvent event);
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class VideoProperty {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
private static final Type[] m_typeValues = Type.values();
|
||||
static final Type[] m_typeValues = Type.values();
|
||||
|
||||
public String getName() {
|
||||
return CameraServerJNI.getPropertyName(m_handle);
|
||||
@@ -94,6 +94,11 @@ public class VideoProperty {
|
||||
m_type = m_typeValues[CameraServerJNI.getPropertyType(handle)];
|
||||
}
|
||||
|
||||
VideoProperty(int handle, Type type) {
|
||||
m_handle = handle;
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
int m_handle;
|
||||
private Type m_type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user