Skip to content

Commit 2fc0eec

Browse files
committed
Thread sync possibly working
1 parent 7f630e1 commit 2fc0eec

25 files changed

Lines changed: 466 additions & 112 deletions

JavaGUI/jni/TSDRLibraryNDK.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ struct java_context {
1919
jfieldID fid_height;
2020
jmethodID fixSize;
2121
jmethodID notifyCallbacks;
22+
jint * pixels;
23+
int pixelsize;
2224
} typedef java_context_t;
2325

2426
static JavaVM *jvm;
@@ -69,23 +71,14 @@ void error(JNIEnv * env, int exception_code, const char *inmsg, ...)
6971
(*env)->DeleteLocalRef(env, cls);
7072
}
7173

72-
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_init (JNIEnv * env, jobject obj) {
74+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_init (JNIEnv * env, jobject obj, jstring path) {
7375
(*env)->GetJavaVM(env, &jvm);
7476
javaversion = (*env)->GetVersion(env);
75-
}
76-
77-
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeLoadPlugin (JNIEnv * env, jobject obj, jstring path) {
7877
const char *npath = (*env)->GetStringUTFChars(env, path, 0);
7978
THROW(tsdr_loadplugin(&tsdr_instance, npath));
8079
(*env)->ReleaseStringUTFChars(env, path, npath);
8180
}
8281

83-
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_pluginParams (JNIEnv * env, jobject obj, jstring params) {
84-
const char *nparams = (*env)->GetStringUTFChars(env, params, 0);
85-
THROW(tsdr_pluginparams(&tsdr_instance, nparams));
86-
(*env)->ReleaseStringUTFChars(env, params, nparams);
87-
}
88-
8982
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_setSampleRate (JNIEnv * env, jobject obj, jlong rate) {
9083
THROW(tsdr_setsamplerate(&tsdr_instance, (uint32_t) rate));
9184
}
@@ -107,27 +100,27 @@ void read_async(float *buf, int width, int height, void *ctx) {
107100
if (i_width != width || i_height != height) {
108101
// fixSize(200, 200);
109102
(*env)->CallVoidMethod(env, context->obj, context->fixSize, width, height);
103+
104+
context->pixelsize = width * height;
105+
context->pixels = (jint *) realloc((void *) context->pixels, sizeof(jint) * context->pixelsize);
110106
}
111107

112-
jintArray pixels_obj = (*env)->GetObjectField(env, context->obj, context->fid_pixels);
113-
jint * pixels = (*env)->GetIntArrayElements(env,pixels_obj,0);
114-
jint * data = pixels;
108+
jint * data = context->pixels;
115109

116110
int i;
117-
const int size = width * height;
118-
for (i = 0; i < size; i++) {
111+
for (i = 0; i < context->pixelsize; i++) {
119112
const int col = (int) (*(buf++) * 255.0f);
120113
*(data++) = col | (col << 8) | (col << 16);
121114
}
122115

123116
// release elements
124-
(*env)->ReleaseIntArrayElements(env,pixels_obj,pixels,0);
117+
(*env)->SetIntArrayRegion(env, (*env)->GetObjectField(env, context->obj, context->fid_pixels), 0, context->pixelsize, context->pixels);
125118

126119
// notifyCallbacks();
127120
(*env)->CallVoidMethod(env, context->obj, context->notifyCallbacks);
128121
}
129122

130-
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeStart (JNIEnv * env, jobject obj) {
123+
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeStart (JNIEnv * env, jobject obj, jstring params) {
131124

132125
java_context_t * context = (java_context_t *) malloc(sizeof(java_context_t));
133126

@@ -140,11 +133,22 @@ JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_nativeStart (JNIEnv
140133
context->fixSize = (*env)->GetMethodID(env, context->cls, "fixSize", "(II)V");
141134
context->notifyCallbacks = (*env)->GetMethodID(env, context->cls, "notifyCallbacks", "()V");
142135

143-
THROW(tsdr_readasync(&tsdr_instance, read_async, (void *) context));
136+
jint i_width = (*env)->GetIntField(env, context->obj, context->fid_width);
137+
jint i_height = (*env)->GetIntField(env, context->obj, context->fid_height);
138+
139+
context->pixelsize = i_width * i_height;
140+
context->pixels = (jint *) malloc(sizeof(jint) * context->pixelsize);
141+
142+
const char *nparams = (*env)->GetStringUTFChars(env, params, 0);
144143

144+
THROW(tsdr_readasync(&tsdr_instance, read_async, (void *) context, nparams));
145+
146+
147+
(*env)->ReleaseStringUTFChars(env, params, nparams);
145148
(*env)->DeleteGlobalRef(env, context->obj);
146149
(*env)->DeleteGlobalRef(env, context->cls);
147150
free(context);
151+
free(context->pixels);
148152
}
149153

150154
JNIEXPORT void JNICALL Java_martin_tempest_core_TSDRLibrary_stop (JNIEnv * env, jobject obj) {

JavaGUI/jni/TSDRLibraryNDK.h

Lines changed: 3 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

JavaGUI/jni/include/TSDRLibrary.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
struct tsdr_lib {
77
void * plugin;
8+
void * mutex_sync_unload;
89
uint32_t samplerate;
910
int width;
1011
int height;
@@ -15,13 +16,12 @@
1516

1617
typedef void(*tsdr_readasync_function)(float *buf, int width, int height, void *ctx);
1718

18-
int tsdr_loadplugin(tsdr_lib_t * tsdr, char * filepath);
19-
int tsdr_pluginparams(tsdr_lib_t * tsdr, char * params);
19+
int tsdr_loadplugin(tsdr_lib_t * tsdr, const char * filepath);
2020
int tsdr_setsamplerate(tsdr_lib_t * tsdr, uint32_t rate);
2121
int tsdr_setbasefreq(tsdr_lib_t * tsdr, uint32_t freq);
2222
int tsdr_stop(tsdr_lib_t * tsdr);
2323
int tsdr_setgain(tsdr_lib_t * tsdr, float gain);
24-
int tsdr_readasync(tsdr_lib_t * tsdr, tsdr_readasync_function cb, void *ctx);
24+
int tsdr_readasync(tsdr_lib_t * tsdr, tsdr_readasync_function cb, void *, const char * params);
2525
int tsdr_unloadplugin(tsdr_lib_t * tsdr);
2626
int tsdr_setresolution(tsdr_lib_t * tsdr, int width, int height);
2727
int tsdr_setvfreq(tsdr_lib_t * tsdr, float freq);

JavaGUI/jni/makefile

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ endif
66
# Flags
77
CPPFLAGS=-O3
88

9+
# Define all of the C files here
10+
OBJS = TSDRLibraryNDK.o
11+
12+
# Define all of the dependencies here
13+
DEPS = TSDRLibraryNDK.h include/TSDRLibrary.h include/TSDRCodes.h
14+
915
# Headers
1016
INC = "$(JAVA_HOME)/include" "$(JAVA_HOME)/include/win32"
1117

@@ -44,32 +50,34 @@ endif
4450
# The folder that will contain the libraries
4551
OUTPUTFOLDER=../lib/$(OSNAME)/$(ARCHNAME)
4652

53+
# The folder that will contain the object files
54+
OBJFOLDER=obj
55+
56+
# The obj files reside in the bin/obj directory
57+
_OBJS = $(patsubst %,$(OBJFOLDER)/%,$(OBJS))
58+
4759
# We want to build the library
48-
all : $(OUTPUTFOLDER)/TSDRLibraryNDK$(EXT)
60+
all : rebuildtempestsdr library
4961

5062
# Linking
51-
$(OUTPUTFOLDER)/TSDRLibraryNDK$(EXT) : TSDRLibraryNDK.o rebuildtempestsdr $(OUTPUTFOLDER)
52-
gcc -L../../TempestSDR/bin/$(OSNAME)/$(ARCHNAME)/ -lTSDRLibrary -Wl,--add-stdcall-alias -shared -o $@ $<
53-
54-
# Create output dir
55-
$(OUTPUTFOLDER) :
56-
mkdir -p $(OUTPUTFOLDER)
63+
library : $(_OBJS)
64+
gcc -L../../TempestSDR/bin/$(OSNAME)/$(ARCHNAME)/ -lTSDRLibrary -Wl,--add-stdcall-alias -shared -o $(OUTPUTFOLDER)/TSDRLibraryNDK$(EXT) $<
5765

5866
# Rebuild the main library so that we have the most up-to-date version
5967
# The main library is assumed to be two levels below this jni folder
60-
rebuildtempestsdr : $(OUTPUTFOLDER)
68+
rebuildtempestsdr :
6169
@$(MAKE) -C ../../TempestSDR all
6270
@cp -f ../../TempestSDR/bin/$(OSNAME)/$(ARCHNAME)/TSDRLibrary$(EXT) $(OUTPUTFOLDER)/TSDRLibrary$(EXT)
71+
mkdir -p include
72+
@cp -f ../../TempestSDR/src/include/TSDRCodes.h include/
73+
@cp -f ../../TempestSDR/src/include/TSDRLibrary.h include/
74+
mkdir -p $(OUTPUTFOLDER)
75+
mkdir -p $(OBJFOLDER)
6376

6477
# Compile the sources to obj files
65-
TSDRLibraryNDK.o : TSDRLibraryNDK.c TSDRLibraryNDK.h include/TSDRLibrary.h include/TSDRCodes.h
78+
$(OBJFOLDER)/%.o : %.c $(DEPS)
6679
gcc $(foreach d, $(INC), -I$d) $(CFLAGS) -c $< -o $@
6780

68-
# Copy over a fresh set of library headers
69-
include/%.h : ../../TempestSDR/src/include/%.h
70-
mkdir -p include
71-
@cp -f $< $@
72-
7381
# Generate the java header based on the class
7482
TSDRLibraryNDK.h : ../bin/martin/tempest/core/TSDRLibrary.class
7583
javah -classpath ../bin -o TSDRLibraryNDK.h -jni martin.tempest.core.TSDRLibrary
@@ -81,5 +89,5 @@ TSDRLibraryNDK.h : ../bin/martin/tempest/core/TSDRLibrary.class
8189

8290
# Remove any compiled artifacts
8391
clean :
84-
rm -f TSDRLibraryNDK.h *.o ../TSDRLibraryNDK$(EXT) ../TSDRLibrary$(EXT) include/*.h
85-
rm -rf include/
92+
rm -f TSDRLibraryNDK.h ../TSDRLibraryNDK$(EXT) ../TSDRLibrary$(EXT)
93+
rm -rf $(OBJFOLDER)/

JavaGUI/jni/osdetect.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifdef OS_WINDOWS
2+
#define WINHEAD (1)
3+
#elif defined(_WIN64)
4+
#define WINHEAD (1)
5+
#elif defined(_WIN32)
6+
#define WINHEAD (1)
7+
#elif defined(_MSC_VER) // Microsoft compiler
8+
#define WINHEAD (1)
9+
#else
10+
#define WINHEAD (0)
11+
#endif

JavaGUI/jni/timer.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "timer.h"
2+
3+
void timer_tick(TickTockTimer_t * timer) {
4+
timer->started = 1;
5+
gettimeofday(&timer->start, NULL);
6+
}
7+
8+
float timer_tock(TickTockTimer_t * timer) {
9+
struct timeval now;
10+
struct timeval elapsed;
11+
12+
if (!timer->started) return -1;
13+
14+
gettimeofday(&now, NULL);
15+
timersub(&now, &timer->start, &elapsed);
16+
17+
return (float) elapsed.tv_sec + (float) (elapsed.tv_usec) * (float) 1e-6;
18+
}
19+
20+
float timer_ticktock(TickTockTimer_t * timer) {
21+
struct timeval now;
22+
struct timeval elapsed;
23+
24+
gettimeofday(&now, NULL);
25+
26+
if (!timer->started) {
27+
timer->start = now;
28+
timer->started = 1;
29+
return 0;
30+
} else {
31+
timersub(&now, &timer->start, &elapsed);
32+
timer->start = now;
33+
34+
return (float) elapsed.tv_sec + (float) (elapsed.tv_usec) * (float) 1e-6;;
35+
}
36+
}

JavaGUI/jni/timer.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef TIMR_M_H
2+
#define TIMR_M_H
3+
#include "osdetect.h"
4+
#include <sys/time.h>
5+
6+
typedef struct TickTockTimer TickTockTimer_t;
7+
8+
struct TickTockTimer
9+
{
10+
struct timeval start;
11+
int started;
12+
};
13+
14+
void timer_tick(TickTockTimer_t * timer);
15+
float timer_tock(TickTockTimer_t * timer);
16+
float timer_ticktock(TickTockTimer_t * timer);
17+
18+
#endif

JavaGUI/src/martin/tempest/Main.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,23 @@ public Main() throws TSDRException {
3333
frame.setSize(WIDTH,HEIGHT);
3434
frame.setVisible(true);
3535

36-
TSDRLibrary sdrlib = new TSDRLibrary(WIDTH, HEIGHT);
37-
sdrlib.loadSource(TSDRLibrary.getAllSources()[0]);
38-
sdrlib.registerFrameReadyCallback(this);
39-
sdrlib.startAsync();
36+
new Thread() {
37+
public void run() {
38+
try {
39+
TSDRLibrary sdrlib = new TSDRLibrary(TSDRLibrary.getAllSources()[0], WIDTH, HEIGHT);
40+
sdrlib.registerFrameReadyCallback(Main.this);
41+
sdrlib.startAsync();
42+
} catch (Throwable e) {e.printStackTrace();};
43+
44+
};
45+
}.start();
46+
4047
}
4148

4249
@Override
4350
public void onFrameReady(TSDRLibrary lib, BufferedImage frame) {
4451
viz.drawImage(frame);
52+
try { lib.setSampleRate(0); } catch (Exception e) {};
4553
}
4654

4755
@Override

0 commit comments

Comments
 (0)