Start Android Audio Thread after audio buffer and AudioTrack are ready.
authorGabriel Jacobo <gabomdq@gmail.com>
Thu, 08 Aug 2013 21:25:09 -0300
changeset 7612627d571587d3
parent 7611 ed6f4b8557e4
child 7613 27e0ae109c71
Start Android Audio Thread after audio buffer and AudioTrack are ready.

Also, it starts the Audio Thread from the native side, putting the code in line
with other backends.
android-project/src/org/libsdl/app/SDLActivity.java
src/audio/android/SDL_androidaudio.c
src/audio/android/SDL_androidaudio.h
src/core/android/SDL_android.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Thu Aug 08 15:04:30 2013 -0700
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Thu Aug 08 21:25:09 2013 -0300
     1.3 @@ -252,8 +252,6 @@
     1.4                                              int action, float x, 
     1.5                                              float y, float p);
     1.6      public static native void onNativeAccel(float x, float y, float z);
     1.7 -    public static native void nativeRunAudioThread();
     1.8 -
     1.9  
    1.10      // Java functions called from C
    1.11  
    1.12 @@ -503,31 +501,15 @@
    1.13                  mAudioTrack = null;
    1.14                  return -1;
    1.15              }
    1.16 +            
    1.17 +            mAudioTrack.play();
    1.18          }
    1.19 -        
    1.20 -        audioStartThread();
    1.21 -        
    1.22 +       
    1.23          Log.v("SDL", "SDL audio: got " + ((mAudioTrack.getChannelCount() >= 2) ? "stereo" : "mono") + " " + ((mAudioTrack.getAudioFormat() == AudioFormat.ENCODING_PCM_16BIT) ? "16-bit" : "8-bit") + " " + (mAudioTrack.getSampleRate() / 1000f) + "kHz, " + desiredFrames + " frames buffer");
    1.24          
    1.25          return 0;
    1.26      }
    1.27      
    1.28 -    public static void audioStartThread() {
    1.29 -        if (mAudioThread == null) {
    1.30 -            mAudioThread = new Thread(new Runnable() {
    1.31 -                @Override
    1.32 -                public void run() {
    1.33 -                    mAudioTrack.play();
    1.34 -                    nativeRunAudioThread();
    1.35 -                }
    1.36 -            });
    1.37 -            
    1.38 -            // I'd take REALTIME if I could get it!
    1.39 -            mAudioThread.setPriority(Thread.MAX_PRIORITY);
    1.40 -            mAudioThread.start();
    1.41 -        }
    1.42 -    }
    1.43 -    
    1.44      public static void audioWriteShortBuffer(short[] buffer) {
    1.45          for (int i = 0; i < buffer.length; ) {
    1.46              int result = mAudioTrack.write(buffer, i, buffer.length - i);
    1.47 @@ -565,17 +547,6 @@
    1.48      }
    1.49  
    1.50      public static void audioQuit() {
    1.51 -        if (mAudioThread != null) {
    1.52 -            try {
    1.53 -                mAudioThread.join();
    1.54 -            } catch(Exception e) {
    1.55 -                Log.v("SDL", "Problem stopping audio thread: " + e);
    1.56 -            }
    1.57 -            mAudioThread = null;
    1.58 -
    1.59 -            //Log.v("SDL", "Finished waiting for audio thread");
    1.60 -        }
    1.61 -
    1.62          if (mAudioTrack != null) {
    1.63              mAudioTrack.stop();
    1.64              mAudioTrack = null;
    1.65 @@ -932,3 +903,4 @@
    1.66      public native void nativeSetComposingText(String text, int newCursorPosition);
    1.67  
    1.68  }
    1.69 +
     2.1 --- a/src/audio/android/SDL_androidaudio.c	Thu Aug 08 15:04:30 2013 -0700
     2.2 +++ b/src/audio/android/SDL_androidaudio.c	Thu Aug 08 21:25:09 2013 -0300
     2.3 @@ -50,7 +50,7 @@
     2.4  
     2.5      audioDevice = this;
     2.6  
     2.7 -    this->hidden = SDL_malloc(sizeof(*(this->hidden)));
     2.8 +    this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(sizeof(*(this->hidden)));
     2.9      if (!this->hidden) {
    2.10          return SDL_OutOfMemory();
    2.11      }
    2.12 @@ -91,6 +91,13 @@
    2.13          /* Init failed? */
    2.14          return SDL_SetError("Java-side initialization failed!");
    2.15      }
    2.16 +    
    2.17 +    /* Audio thread is started here, after audio buffers and Java's AudioTrack are in place and ready to go */
    2.18 +    this->thread = SDL_CreateThread(SDL_RunAudio, "AndroidAudioThread", this);
    2.19 +    if (this->thread == NULL) {
    2.20 +        AndroidAUD_CloseDevice(this);
    2.21 +        return SDL_SetError("Couldn't create audio thread");
    2.22 +    }
    2.23  
    2.24      return 0;
    2.25  }
    2.26 @@ -110,6 +117,10 @@
    2.27  static void
    2.28  AndroidAUD_CloseDevice(_THIS)
    2.29  {
    2.30 +    /* At this point SDL_CloseAudioDevice via close_audio_device took care of terminating the audio thread
    2.31 +       so it's safe to terminate the Java side buffer and AudioTrack
    2.32 +     */
    2.33 +   
    2.34      if (this->hidden != NULL) {
    2.35          SDL_free(this->hidden);
    2.36          this->hidden = NULL;
    2.37 @@ -143,13 +154,7 @@
    2.38      "android", "SDL Android audio driver", AndroidAUD_Init, 0
    2.39  };
    2.40  
    2.41 -/* Called by the Java code to start the audio processing on a thread */
    2.42 -void
    2.43 -Android_RunAudioThread()
    2.44 -{
    2.45 -    SDL_RunAudio(audioDevice);
    2.46 -}
    2.47 -
    2.48  #endif /* SDL_AUDIO_DRIVER_ANDROID */
    2.49  
    2.50  /* vi: set ts=4 sw=4 expandtab: */
    2.51 +
     3.1 --- a/src/audio/android/SDL_androidaudio.h	Thu Aug 08 15:04:30 2013 -0700
     3.2 +++ b/src/audio/android/SDL_androidaudio.h	Thu Aug 08 21:25:09 2013 -0300
     3.3 @@ -32,6 +32,8 @@
     3.4  {
     3.5  };
     3.6  
     3.7 +static void AndroidAUD_CloseDevice(_THIS);
     3.8 +
     3.9  #endif /* _SDL_androidaudio_h */
    3.10  
    3.11  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/core/android/SDL_android.c	Thu Aug 08 15:04:30 2013 -0700
     4.2 +++ b/src/core/android/SDL_android.c	Thu Aug 08 21:25:09 2013 -0300
     4.3 @@ -47,9 +47,6 @@
     4.4  /* Uncomment this to log messages entering and exiting methods in this file */
     4.5  //#define DEBUG_JNI
     4.6  
     4.7 -/* Implemented in audio/android/SDL_androidaudio.c */
     4.8 -extern void Android_RunAudioThread();
     4.9 -
    4.10  static void Android_JNI_ThreadDestroyed(void*);
    4.11  
    4.12  /*******************************************************************************
    4.13 @@ -245,15 +242,6 @@
    4.14      }
    4.15  }
    4.16  
    4.17 -void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread(
    4.18 -                                    JNIEnv* env, jclass cls)
    4.19 -{
    4.20 -    /* This is the audio thread, with a different environment */
    4.21 -    Android_JNI_SetupThread();
    4.22 -
    4.23 -    Android_RunAudioThread();
    4.24 -}
    4.25 -
    4.26  void Java_org_libsdl_app_SDLInputConnection_nativeCommitText(
    4.27                                      JNIEnv* env, jclass cls,
    4.28                                      jstring text, jint newCursorPosition)
    4.29 @@ -1374,3 +1362,4 @@
    4.30  #endif /* __ANDROID__ */
    4.31  
    4.32  /* vi: set ts=4 sw=4 expandtab: */
    4.33 +