Browse Source

Android: Ensured that OpenSL audio callbacks can call into Java (even if this is not recommended)

tags/2021-05-28
hogliux 8 years ago
parent
commit
0b20507c8b
2 changed files with 25 additions and 11 deletions
  1. +13
    -0
      modules/juce_audio_devices/native/juce_android_OpenSL.cpp
  2. +12
    -11
      modules/juce_core/native/juce_android_SystemStats.cpp

+ 13
- 0
modules/juce_audio_devices/native/juce_android_OpenSL.cpp View File

@@ -261,6 +261,11 @@ struct BufferHelpers<float>
}
};
//==============================================================================
// need this from juce_core without asserting
struct JniEnvThreadHolder { static JNIEnv* getEnv(); };
extern JavaVM* androidJNIJavaVM;
//==============================================================================
class OpenSLAudioIODevice : public AudioIODevice
{
@@ -320,6 +325,14 @@ public:
void finished (SLAndroidSimpleBufferQueueItf)
{
if (JniEnvThreadHolder::getEnv() == nullptr)
{
JNIEnv* env;
androidJNIJavaVM->AttachCurrentThread (&env, nullptr);
setEnv (env);
}
--numBlocksOut;
owner.doSomeWorkOnAudioThread();
}


+ 12
- 11
modules/juce_core/native/juce_android_SystemStats.cpp View File

@@ -116,16 +116,7 @@ public:
return *instance;
}
static JNIEnv* getEnv()
{
JNIEnv* env = reinterpret_cast<JNIEnv*> (pthread_getspecific (getInstance().threadKey));
// You are trying to use a JUCE function on a thread that was not created by JUCE.
// You need to first call setEnv on this thread before using JUCE
jassert (env != nullptr);
return env;
}
static JNIEnv* getEnv() { return reinterpret_cast<JNIEnv*> (pthread_getspecific (getInstance().threadKey)); }
static void setEnv (JNIEnv* env)
{
@@ -167,7 +158,17 @@ private:
JniEnvThreadHolder* JniEnvThreadHolder::instance = nullptr;
//==============================================================================
JNIEnv* getEnv() noexcept { return JniEnvThreadHolder::getEnv(); }
JNIEnv* getEnv() noexcept
{
auto* env = JniEnvThreadHolder::getEnv();
// You are trying to use a JUCE function on a thread that was not created by JUCE.
// You need to first call setEnv on this thread before using JUCE
jassert (env != nullptr);
return env;
}
void setEnv (JNIEnv* env) noexcept { JniEnvThreadHolder::setEnv (env); }
extern "C" jint JNI_OnLoad (JavaVM* vm, void*)


Loading…
Cancel
Save