src/core/android/SDL_android.cpp
changeset 5996 102a9ec1ea13
parent 5994 153d15ab3032
child 6044 35448a5ea044
     1.1 --- a/src/core/android/SDL_android.cpp	Sat Oct 15 14:25:00 2011 -0700
     1.2 +++ b/src/core/android/SDL_android.cpp	Sat Oct 15 23:50:06 2011 -0700
     1.3 @@ -29,6 +29,14 @@
     1.4  #include "../../video/android/SDL_androidtouch.h"
     1.5  #include "../../video/android/SDL_androidvideo.h"
     1.6  
     1.7 +#include <android/log.h>
     1.8 +#define LOG_TAG "SDL_android"
     1.9 +//#define LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
    1.10 +//#define LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
    1.11 +#define LOGI(...) do {} while (false)
    1.12 +#define LOGE(...) do {} while (false)
    1.13 +
    1.14 +
    1.15  /* Impelemented in audio/android/SDL_androidaudio.c */
    1.16  extern void Android_RunAudioThread();
    1.17  } // C
    1.18 @@ -45,6 +53,7 @@
    1.19  *******************************************************************************/
    1.20  static JNIEnv* mEnv = NULL;
    1.21  static JNIEnv* mAudioEnv = NULL;
    1.22 +static JavaVM* mJavaVM;
    1.23  
    1.24  // Main activity
    1.25  static jclass mActivityClass;
    1.26 @@ -68,6 +77,14 @@
    1.27  // Library init
    1.28  extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
    1.29  {
    1.30 +    JNIEnv *env;
    1.31 +    mJavaVM = vm;
    1.32 +    LOGI("JNI_OnLoad called");
    1.33 +    if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
    1.34 +        LOGE("Failed to get the environment using GetEnv()");
    1.35 +        return -1;
    1.36 +    }
    1.37 +
    1.38      return JNI_VERSION_1_4;
    1.39  }
    1.40  
    1.41 @@ -96,6 +113,7 @@
    1.42         !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
    1.43          __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly");
    1.44      }
    1.45 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init() finished!");
    1.46  }
    1.47  
    1.48  // Resize
    1.49 @@ -204,29 +222,48 @@
    1.50  {
    1.51      int audioBufferFrames;
    1.52  
    1.53 +    int status;
    1.54 +    JNIEnv *env;
    1.55 +    static bool isAttached = false;    
    1.56 +    status = mJavaVM->GetEnv((void **) &env, JNI_VERSION_1_4);
    1.57 +    if(status < 0) {
    1.58 +        LOGE("callback_handler: failed to get JNI environment, assuming native thread");
    1.59 +        status = mJavaVM->AttachCurrentThread(&env, NULL);
    1.60 +        if(status < 0) {
    1.61 +            LOGE("callback_handler: failed to attach current thread");
    1.62 +            return 0;
    1.63 +        }
    1.64 +        isAttached = true;
    1.65 +    }
    1.66 +
    1.67 +    
    1.68      __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device");
    1.69      audioBuffer16Bit = is16Bit;
    1.70      audioBufferStereo = channelCount > 1;
    1.71  
    1.72 -    audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
    1.73 +    audioBuffer = env->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames);
    1.74  
    1.75      if (audioBuffer == NULL) {
    1.76          __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!");
    1.77          return 0;
    1.78      }
    1.79 -    audioBuffer = mEnv->NewGlobalRef(audioBuffer);
    1.80 +    audioBuffer = env->NewGlobalRef(audioBuffer);
    1.81  
    1.82      jboolean isCopy = JNI_FALSE;
    1.83      if (audioBuffer16Bit) {
    1.84 -        audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
    1.85 -        audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer);
    1.86 +        audioBufferPinned = env->GetShortArrayElements((jshortArray)audioBuffer, &isCopy);
    1.87 +        audioBufferFrames = env->GetArrayLength((jshortArray)audioBuffer);
    1.88      } else {
    1.89 -        audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
    1.90 -        audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer);
    1.91 +        audioBufferPinned = env->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy);
    1.92 +        audioBufferFrames = env->GetArrayLength((jbyteArray)audioBuffer);
    1.93      }
    1.94      if (audioBufferStereo) {
    1.95          audioBufferFrames /= 2;
    1.96      }
    1.97 + 
    1.98 +    if (isAttached) {
    1.99 +        mJavaVM->DetachCurrentThread();
   1.100 +    }
   1.101  
   1.102      return audioBufferFrames;
   1.103  }
   1.104 @@ -251,13 +288,31 @@
   1.105  
   1.106  extern "C" void Android_JNI_CloseAudioDevice()
   1.107  {
   1.108 -    mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
   1.109 +    int status;
   1.110 +    JNIEnv *env;
   1.111 +    static bool isAttached = false;    
   1.112 +    status = mJavaVM->GetEnv((void **) &env, JNI_VERSION_1_4);
   1.113 +    if(status < 0) {
   1.114 +        LOGE("callback_handler: failed to get JNI environment, assuming native thread");
   1.115 +        status = mJavaVM->AttachCurrentThread(&env, NULL);
   1.116 +        if(status < 0) {
   1.117 +            LOGE("callback_handler: failed to attach current thread");
   1.118 +            return;
   1.119 +        }
   1.120 +        isAttached = true;
   1.121 +    }
   1.122 +
   1.123 +    env->CallStaticVoidMethod(mActivityClass, midAudioQuit); 
   1.124  
   1.125      if (audioBuffer) {
   1.126 -        mEnv->DeleteGlobalRef(audioBuffer);
   1.127 +        env->DeleteGlobalRef(audioBuffer);
   1.128          audioBuffer = NULL;
   1.129          audioBufferPinned = NULL;
   1.130      }
   1.131 +
   1.132 +    if (isAttached) {
   1.133 +        mJavaVM->DetachCurrentThread();
   1.134 +    }
   1.135  }
   1.136  
   1.137  // Test for an exception and call SDL_SetError with its detail if one occurs