android/testproject/jni/app-android.cpp
changeset 4724 d86332c0fb9b
parent 4723 74da47b2f5b7
child 4726 9076cdb027af
     1.1 --- a/android/testproject/jni/app-android.cpp	Tue Jul 27 21:58:18 2010 +0200
     1.2 +++ b/android/testproject/jni/app-android.cpp	Sat Aug 14 12:35:21 2010 +1200
     1.3 @@ -26,6 +26,7 @@
     1.4  }
     1.5  
     1.6  JNIEnv* mEnv = NULL;
     1.7 +JNIEnv* mAudioThreadEnv = NULL; //See the note below for why this is necessary
     1.8  JavaVM* mVM = NULL;
     1.9  
    1.10  //Main activity
    1.11 @@ -35,6 +36,7 @@
    1.12  jmethodID midCreateGLContext;
    1.13  jmethodID midFlipBuffers;
    1.14  jmethodID midEnableFeature;
    1.15 +jmethodID midUpdateAudio;
    1.16  
    1.17  extern "C" int SDL_main();
    1.18  extern "C" int Android_OnKeyDown(int keycode);
    1.19 @@ -54,6 +56,7 @@
    1.20  //Accelerometer data storage
    1.21  float fLastAccelerometer[3];
    1.22  
    1.23 +
    1.24  /*******************************************************************************
    1.25                   Functions called by JNI
    1.26  *******************************************************************************/	
    1.27 @@ -77,8 +80,10 @@
    1.28      midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
    1.29      midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
    1.30      midEnableFeature = mEnv->GetStaticMethodID(cls,"enableFeature","(II)V");
    1.31 +    midUpdateAudio = mEnv->GetStaticMethodID(cls,"updateAudio","([B)V");
    1.32  
    1.33 -    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature){
    1.34 +    if(!midCreateGLContext || !midFlipBuffers || !midEnableFeature ||
    1.35 +        !midUpdateAudio){
    1.36          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
    1.37      }else{
    1.38          __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
    1.39 @@ -200,3 +205,32 @@
    1.40                                  featureid, (int)enabled); 
    1.41  }
    1.42  
    1.43 +extern "C" void Android_UpdateAudioBuffer(unsigned char *buf, int len){
    1.44 +
    1.45 +    //Annoyingly we can't just call into Java from any thread. Because the audio
    1.46 +    //callback is dispatched from the SDL audio thread (that wasn't made from
    1.47 +    //java, we have to do some magic here to let the JVM know about the thread.
    1.48 +    //Because everything it touches on the Java side is static anyway, it's 
    1.49 +    //not a big deal, just annoying.
    1.50 +    if(!mAudioThreadEnv){
    1.51 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Need to set up audio thread env\n");
    1.52 +
    1.53 +        mJVM->AttachCurrentThread(&mAudioThreadEnv, NULL);
    1.54 +
    1.55 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: ok\n");
    1.56 +    }
    1.57 +    
    1.58 +    jbyteArray arr = mAudioThreadEnv->NewByteArray(len);
    1.59 +
    1.60 +    //blah. We probably should rework this so we avoid the copy. 
    1.61 +    mAudioThreadEnv->SetByteArrayRegion(arr, 0, len, (jbyte *)buf);
    1.62 +    
    1.63 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: copied\n");
    1.64 +
    1.65 +    mAudioThreadEnv->CallStaticVoidMethod(  mActivityInstance, 
    1.66 +                                            midUpdateAudio, arr );
    1.67 +
    1.68 +    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: invoked\n");
    1.69 +    
    1.70 +}
    1.71 +