title: How To Call JAVA Methods from inside of a Thread in JNI
body: <p>TL;DR; I'm having a problem with passing my FFMPEG raw data from C++ code to JAVA code, for displaying, through a thread.</p>
  <p>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 &quot;listener&quot; of sorts. Just a thread running in the background polling the server and running specific <code>onFrame</code> function once a new frame is available.</p>
  <p>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 <code>FileObserver</code> 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.</p>
  <p>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.</p>
  <p>It's worth mentioning that <code>onFrame</code> function cannot have <code>JNIEnv*</code> &amp;&amp; <code>jobject</code> inside it's arguments list (Library requirements).</p>
  <p>What I have tried so far is making a native method in my <code>MainActivity</code> through which I pass <code>JNIEnv</code> and <code>jobject</code> and assign those to global variables</p>
  <pre><code>JNIEnv* m_globalEnv = env;
  jobject m_globalObject = thiz;
  JavaVM m_jvm = 0;
  jclass mainActivity = m_globalEnv-&gt;GetObjectClass(m_globalObject);
  jmethodID testMethod = m_globalEnv-&gt;GetMethodID(mainClass, &quot;testMethod&quot;, &quot;(I)V&quot;);
  
  m_globalEnv-&gt;GetJavaVM(&amp;m_jvm);
  </code></pre>
  <p>After that, in my <code>onFrame</code> I call<br />
  <code>jvm-&gt;AttachCurrentThread(&amp;m_globalEnv, NULL);</code><br />
  And then I try to call a JAVA method from somewhere inside the code (It's irrelevant where/when in the <code>onFrame</code> I call it) by doing:</p>
  <pre><code>m_globalEnv-&gt;CallVoidMethod(m_globalObject, &quot;testMethod&quot;, 5);
  </code></pre>
  <p>And then all crashes with either:</p>
  <pre><code>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
  .
  .
  .
  </code></pre>
  <h1>EDIT 1</h1>
  <p>After Trying out the code from Michael's solution, I got the<br />
  <code>java_vm_ext.cc:542] JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0xc94f7f8c</code> error.
  After running the app in debug mode to catch the error, I got to the <code>jni.h</code>; Line of code that triggers the error is:
  <code>m_env-&gt;CallVoidMethod(m_globalObject, testMethod, 5);</code>
  (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<br />
  <code> void CallVoidMethod(jobject obj, jmethodID methodID, ...)</code><br />
  and it's
  <code>functions-&gt;CallVoidMethodV(this, obj, methodID, args);</code><br />
  which is defined  on line 228:
  <code>void (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list);</code></p>