-
Notifications
You must be signed in to change notification settings - Fork 2
/
extern-jni.h
283 lines (236 loc) · 8.24 KB
/
extern-jni.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "audiotrack.h"
#include "componentmanager.h"
#include "eq.h"
#include "fir.h"
#include "player.h"
#include "preamp.h"
#include "threadpool.h"
#include <jni.h>
extern JavaVM *javaVM;
vrok::Player *pl;
vrok::DriverAudioTrack *out;
vrok::EffectSSEQ *pSSEQ;
vrok::EffectFIR *pFIR;
vrok::Component *current = nullptr;
ThreadPool *pool;
static jobject hookObject = NULL;
static jmethodID hookMethod = NULL;
static jclass hookClass = NULL;
static vrok::Player *plx = NULL;
void NextTrackCallback(void *user);
extern "C" {
void CreateContext() {
pl = new vrok::Player;
out = new vrok::DriverAudioTrack;
pSSEQ = new vrok::EffectSSEQ;
pFIR = new vrok::EffectFIR;
pool = new ThreadPool(4);
}
void SetCurrentComponent(const char *name) {
current = vrok::ComponentManager::GetSingleton()->GetComponent(std::string(name));
}
void SetPropertyFloat(const char *name, float val) {
vrok::PropertyBase *p = vrok::ComponentManager::GetSingleton()->GetProperty(current, std::string(name));
vrok::ComponentManager::GetSingleton()->SetProperty(current, p, &val);
}
vrok::Resource *CreateResource(const char *filename) {
vrok::Resource *res = new vrok::Resource();
res->_filename = std::string(filename);
return res;
}
void DestroyResource(vrok::Resource *resource) {
delete resource;
}
void RegisterSource(BufferGraph::Point *parent, BufferGraph::Point *source) {
parent->RegisterSource(source);
}
void RegisterSink(BufferGraph::Point *parent, BufferGraph::Point *sink) {
parent->RegisterSource(sink);
}
vrok::Player *CreatePlayer() {
DBG("player");
pFIR->RegisterSource(pl);
pFIR->RegisterSink(pSSEQ);
pSSEQ->RegisterSource(pFIR);
pSSEQ->RegisterSink(out);
pl->RegisterSink(pFIR);
out->RegisterSource(pSSEQ);
DBG("Reg");
out->Preallocate();
pl->Preallocate();
pSSEQ->Preallocate();
pFIR->Preallocate();
DBG("pre");
pool->RegisterWork(0, pl);
pool->RegisterWork(1, pSSEQ);
pool->RegisterWork(2, pFIR);
pool->RegisterWork(3, out);
DBG("reg");
pl->SetNextTrackCallback(NextTrackCallback, nullptr);
DBG("regxx");
pool->CreateThreads();
DBG("player done");
return pl;
}
void JoinPlayer() {
pool->JoinThreads();
}
void SubmitForPlayback(vrok::Resource *resource) {
pl->SubmitForPlayback(resource);
}
void SubmitForPlaybackNow(vrok::Resource *resource) {
pl->SubmitForPlaybackNow(resource);
}
void PausePlayer() {
pl->Pause();
}
void ResumePlayer() {
pl->Resume();
}
void DeleteContext() {
delete pl;
delete out;
delete pSSEQ;
delete pool;
delete pFIR;
}
void StopThreads() {
pool->StopThreads();
}
float GetSSEQPreamp() {
float val = 0;
vrok::PropertyBase *p = vrok::ComponentManager::GetSingleton()->GetProperty(pSSEQ, "preamp");
p->Get(&val);
return val;
}
float GetSSEQBand(int band) {
char bandname[32];
snprintf(bandname, 32, "band_%d", band);
std::string sband = std::string(bandname);
float val = 0;
DBG(bandname);
vrok::PropertyBase *p = vrok::ComponentManager::GetSingleton()->GetProperty(pSSEQ, sband);
p->Get(&val);
return val;
}
void SetSSEQPreamp(float val) {
vrok::PropertyBase *p = vrok::ComponentManager::GetSingleton()->GetProperty(pSSEQ, "preamp");
vrok::ComponentManager::GetSingleton()->SetProperty(pSSEQ, p, &val);
}
void SetSSEQBand(int band, float val) {
char bandname[32];
snprintf(bandname, 32, "band_%d", band);
std::string sband = std::string(bandname);
vrok::PropertyBase *p = vrok::ComponentManager::GetSingleton()->GetProperty(pSSEQ, sband);
vrok::ComponentManager::GetSingleton()->SetProperty(pSSEQ, p, &val);
}
}
void NextTrackCallback(void *user) {
if (hookObject) {
JNIEnv *g_env;
int getEnvStat = javaVM->GetEnv((void **)&g_env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) {
if (javaVM->AttachCurrentThread(&g_env, NULL) != 0) {
DBG("Failed to attach");
return;
}
} else if (getEnvStat == JNI_OK) {
//
} else if (getEnvStat == JNI_EVERSION) {
DBG("GetEnv: version not supported");
}
jstring str = (jstring)g_env->CallNonvirtualObjectMethod(hookObject, hookClass, hookMethod);
// strcpy(url, g_env->GetStringUTFChars( str , NULL ) );
DBG(g_env->GetStringUTFChars(str, NULL));
plx->SubmitForPlaybackNow(CreateResource(g_env->GetStringUTFChars(str, NULL)));
if (g_env->ExceptionCheck()) {
g_env->ExceptionDescribe();
}
javaVM->DetachCurrentThread();
} else {
DBG("Callback interface not initialized");
}
}
extern "C" {
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
javaVM = vm;
DBG("context");
CreateContext();
DBG("context done");
return JNI_VERSION_1_6;
}
JNIEXPORT void JNICALL JNI_OnUnLoad(JavaVM *vm, void *reserved) {
DeleteContext();
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_hookCallback(JNIEnv *env, jobject thiz) {
DBG("hook called");
hookObject = env->NewGlobalRef(thiz);
// save refs for callback
hookClass = (jclass)env->NewGlobalRef((jobject)env->GetObjectClass(hookObject));
if (hookClass == NULL) {
DBG("Failed to find class");
}
hookMethod = env->GetMethodID(hookClass, "nextTrack", "()Ljava/lang/String;");
// hookMethod = env->GetStaticMethodID(hookClass, "nextTrack", "()Ljava/lang/String;");
if (hookMethod == NULL) {
DBG("Unable to get method ref");
}
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_initialize(JNIEnv *env, jobject thiz,
jstring settingsPath) {
plx = CreatePlayer();
DBG("player done");
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_unhookCallback(JNIEnv *env, jobject thiz) {
env->DeleteGlobalRef(hookObject);
env->DeleteGlobalRef((jobject)hookClass);
hookMethod = NULL;
hookClass = NULL;
hookObject = NULL;
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_open(JNIEnv *env, jobject thiz, jstring path) {
const char *szPath = env->GetStringUTFChars(path, NULL);
plx->SubmitForPlaybackNow(CreateResource(szPath));
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_play(JNIEnv *env, jobject thiz) {
ResumePlayer();
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_pause(JNIEnv *env, jobject thiz) {
PausePlayer();
}
// Position is set in normalized range, 0..1
JNIEXPORT float JNICALL Java_com_mx_vrok_VrokService_getPosition(JNIEnv *env, jobject thiz) {
return 0;
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_setPosition(JNIEnv *env, jobject thiz, float pos) { }
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_setComponent(JNIEnv *env, jobject thiz, jstring name) {
const char *szComp = env->GetStringUTFChars(name, NULL);
SetCurrentComponent(szComp);
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_setPropertyFloat(JNIEnv *env, jobject thiz, jstring name,
float val) {
const char *szProp = env->GetStringUTFChars(name, NULL);
SetPropertyFloat(szProp, val);
}
JNIEXPORT bool JNICALL Java_com_mx_vrok_VrokService_getEqualizerAutoPreamp(JNIEnv *env, jobject thiz) { }
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_setEqualizerAutoPreamp(JNIEnv *env, jobject thiz,
bool value) { }
JNIEXPORT float JNICALL Java_com_mx_vrok_VrokService_getEqualizerPreamp(JNIEnv *env, jobject thiz) {
return GetSSEQPreamp();
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_setEqualizerPreamp(JNIEnv *env, jobject thiz,
float value) {
SetSSEQPreamp(value);
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_setEqualizerBand(JNIEnv *env, jobject thiz, int band,
float value) {
SetSSEQBand(band, value);
}
JNIEXPORT float JNICALL Java_com_mx_vrok_VrokService_getEqualizerBand(JNIEnv *env, jobject thiz, int band) {
return GetSSEQBand(band);
}
JNIEXPORT void JNICALL Java_com_mx_vrok_VrokService_shutdown(JNIEnv *env, jobject thiz) {
StopThreads();
exit(0);
}
}