TL;DR; I'm having a problem with passing my FFMPEG raw data from C++ code to JAVA code, for displaying, through a thread.
There is a server set up that sends out encoded frames to its clients. Those encoded frames are encoded with some FFMPEG magic. When received on client side, the fore-mentioned frames are getting decoded into raw RGB data (as a unsigned char *). The problem now is that frames are being received in a "listener" of sorts. Just a thread running in the background polling the server and running specific onFrame
function once a new frame is available.
The current solution for displaying the frames in a video-format is to save each frame to internal storage in C++, and then have a FileObserver
on java side that displays an image as soon as it's written in the memory. Sadly, that approach yields a 6 FPS video on phone, for a 10 FPS video from Server.
I need a way of passing that unsigned char * (jbytearray) to my JAVA code so I can decode it and display it from RAM rather than Disk.
It's worth mentioning that onFrame
function cannot have JNIEnv*
&& jobject
inside it's arguments list (Library requirements).
What I have tried so far is making a native method in my MainActivity
through which I pass JNIEnv
and jobject
and assign those to global variables
JNIEnv* m_globalEnv = env;
jobject m_globalObject = thiz;
JavaVM m_jvm = 0;
jclass mainActivity = m_globalEnv->GetObjectClass(m_globalObject);
jmethodID testMethod = m_globalEnv->GetMethodID(mainClass, "testMethod", "(I)V");
m_globalEnv->GetJavaVM(&m_jvm);
After that, in my onFrame
I call
jvm->AttachCurrentThread(&m_globalEnv, NULL);
And then I try to call a JAVA method from somewhere inside the code (It's irrelevant where/when in the onFrame
I call it) by doing:
m_globalEnv->CallVoidMethod(m_globalObject, "testMethod", 5);
And then all crashes with either:
1- JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xffe8ea7c
2- JNI DETECTED ERROR IN APPLICATION: Thread is making JNI calls without being attached
.
.
.
EDIT 1
After Trying out the code from Michael's solution, I got the
java_vm_ext.cc:542] JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xc94f7f8c
error.
After running the app in debug mode to catch the error, I got to the jni.h
; Line of code that triggers the error is:
m_env->CallVoidMethod(m_globalObject, testMethod, 5);
(5 being the number I am trying to pass for testing purposes).
The line of code inside jni.h that is being highlighted by the debugger is inside of
void CallVoidMethod(jobject obj, jmethodID methodID, ...)
and it's
functions->CallVoidMethodV(this, obj, methodID, args);
which is defined on line 228:
void (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list);