From 5a5b02d79cec235b3c89aa48508f37db606a7fc4 Mon Sep 17 00:00:00 2001 From: Siddharth Date: Wed, 2 Oct 2013 19:13:44 +0530 Subject: [PATCH 1/4] fixed bugs in atomic_flag --- base/mutex.h | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/base/mutex.h b/base/mutex.h index c2b3343379..08a46db10d 100644 --- a/base/mutex.h +++ b/base/mutex.h @@ -12,6 +12,7 @@ #define NOMINMAX #include #include +#include // Zap stupid windows defines // Should move these somewhere clever. @@ -23,7 +24,63 @@ #include #include #include + +#ifdef BLACKBERRY +#include +#elif defined(__SYMBIAN32__) +#include #endif +#endif + +#include "base/basictypes.h" + +struct atomic_flag_init { + atomic_flag_init() : v(false) { + } + + bool v; +}; +static const atomic_flag_init NATIVE_ATOMIC_FLAG_INIT; + +class atomic_flag { +public: + atomic_flag() { + } + + atomic_flag(const atomic_flag_init &v) : value(0) { + } + + void clear() { +#if defined(_WIN32) + _WriteBarrier(); + value = 0; +#elif defined(BLACKBERRY) + atomic_set(&value, 0); +#elif defined(__SYMBIAN32__) + g_atomic_int_set(&value, 0); +#else + __sync_lock_release(&value); +#endif + } + + // Returns the previous value. + bool test_and_set() { +#if defined(_WIN32) + return InterlockedExchange(&value, 1) != 0; +#elif defined(BLACKBERRY) + return atomic_set_value(&value, 1) != 0; +#elif defined(__SYMBIAN32__) + return !g_atomic_int_compare_and_exchange(&value, 0, 1); +#else + return __sync_lock_test_and_set(&value, 1) != 0; +#endif + } + +private: + volatile unsigned int value; + + DISALLOW_COPY_AND_ASSIGN(atomic_flag); +}; class recursive_mutex { #ifdef _WIN32 From 36b82e6159243cbf74d63f0d80c342a7291418e3 Mon Sep 17 00:00:00 2001 From: Siddharth Date: Wed, 2 Oct 2013 20:17:44 +0530 Subject: [PATCH 2/4] fixed bugs in the atomic_flag class --- base/mutex.h | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/base/mutex.h b/base/mutex.h index d59cf45000..ae108ff12c 100644 --- a/base/mutex.h +++ b/base/mutex.h @@ -43,20 +43,11 @@ struct atomic_flag_init { static const atomic_flag_init NATIVE_ATOMIC_FLAG_INIT; class atomic_flag { -<<<<<<< HEAD public: - atomic_flag() { - } - atomic_flag(const atomic_flag_init &v) : value(0) { -======= - atomic_flag() { - } - - atomic_flag(atomic_flag_init &v) : value(0) { ->>>>>>> upstream/master - } + atomic_flag(const atomic_flag_init &v) : value(0) {} + void clear() { #if defined(_WIN32) _WriteBarrier(); From 6f95936da9665d7f8f4e5549ee800e751e888b50 Mon Sep 17 00:00:00 2001 From: Siddharth Date: Sun, 6 Oct 2013 18:09:42 +0530 Subject: [PATCH 3/4] ppsspp on Android gains and loses audio focus --- .../libnative/AudioFocusChangeListener.java | 25 +++++++++++++ .../libnative/NativeActivity.java | 37 +++++++++++++++---- .../libnative/NativeAudioPlayer.java | 23 ++++++++++-- 3 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 android/src/com/henrikrydgard/libnative/AudioFocusChangeListener.java diff --git a/android/src/com/henrikrydgard/libnative/AudioFocusChangeListener.java b/android/src/com/henrikrydgard/libnative/AudioFocusChangeListener.java new file mode 100644 index 0000000000..c3dbfb7d9f --- /dev/null +++ b/android/src/com/henrikrydgard/libnative/AudioFocusChangeListener.java @@ -0,0 +1,25 @@ +package com.henrikrydgard.libnative; +import android.media.AudioManager; +import android.media.AudioManager.OnAudioFocusChangeListener; + +public class AudioFocusChangeListener implements OnAudioFocusChangeListener{ + //not used right now, but we may need to use it sometime. So just store it + //for now. + private boolean hasAudioFocus = false; + + @Override + public void onAudioFocusChange(int focusChange) { + switch (focusChange){ + case AudioManager.AUDIOFOCUS_GAIN: + hasAudioFocus = true; + break; + + case AudioManager.AUDIOFOCUS_LOSS: + hasAudioFocus = false; + } + + // TODO Auto-generated method stub + + } + +} diff --git a/android/src/com/henrikrydgard/libnative/NativeActivity.java b/android/src/com/henrikrydgard/libnative/NativeActivity.java index 0afcd398b5..4f608ff7b8 100644 --- a/android/src/com/henrikrydgard/libnative/NativeActivity.java +++ b/android/src/com/henrikrydgard/libnative/NativeActivity.java @@ -106,12 +106,15 @@ public class NativeActivity extends Activity { private int optimalFramesPerBuffer; private int optimalSampleRate; + //audioFocusChangeListener to listen to changes in audio state + private AudioFocusChangeListener audioFocusChangeListener; + private AudioManager audioManager; + @TargetApi(17) private void detectOptimalAudioSettings() { - AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE); try { - optimalFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)); - optimalSampleRate = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)); + optimalFramesPerBuffer = Integer.parseInt(this.audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)); + optimalSampleRate = Integer.parseInt(this.audioManager.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE)); } catch (NumberFormatException e) { // Ignore, if we can't parse it it's bogus and zero is a fine value (means we couldn't detect it). } @@ -153,6 +156,12 @@ public class NativeActivity extends Activity { } public void Initialize() { + + //initialise audio classes. Do this here since detectOptimalAudioSettings() + //needs audioManager + this.audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); + this.audioFocusChangeListener = new AudioFocusChangeListener(); + if (Build.VERSION.SDK_INT >= 9) { // Native OpenSL is available. Let's use it! useOpenSL = true; @@ -214,7 +223,8 @@ public class NativeActivity extends Activity { } else { Log.i(TAG, "OpenGL ES 2.0 detected."); } - + + /* editText = new EditText(this); editText.setText("Hello world"); @@ -239,9 +249,13 @@ public class NativeActivity extends Activity { Window window = this.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setVolumeControlStream(AudioManager.STREAM_MUSIC); - - if (!useOpenSL) - audioPlayer = new NativeAudioPlayer(); + + + //intialize audio and tell PPSSPP to gain audio focus + if (!useOpenSL){ + audioPlayer = new NativeAudioPlayer(); + } + NativeAudioPlayer.gainAudioFocus(this.audioManager, this.audioFocusChangeListener); NativeApp.audioInit(); Point size = new Point(); @@ -277,6 +291,9 @@ public class NativeActivity extends Activity { NativeApp.audioShutdown(); audioPlayer = null; mGLSurfaceView = null; + audioFocusChangeListener = null; + audioManager = null; + } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @@ -294,6 +311,8 @@ public class NativeActivity extends Activity { protected void onPause() { super.onPause(); Log.i(TAG, "onPause"); + + NativeAudioPlayer.loseAudioFocus(this.audioManager, this.audioFocusChangeListener); if (audioPlayer != null) { audioPlayer.stop(); } @@ -313,7 +332,9 @@ public class NativeActivity extends Activity { } else { Log.e(TAG, "mGLSurfaceView really shouldn't be null in onResume"); } - if (audioPlayer != null) { + + NativeAudioPlayer.gainAudioFocus(this.audioManager, this.audioFocusChangeListener); + if (audioPlayer != null) { audioPlayer.play(); } NativeApp.resume(); diff --git a/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java b/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java index 2d98e25f08..34e7b94cdc 100644 --- a/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java +++ b/android/src/com/henrikrydgard/libnative/NativeAudioPlayer.java @@ -2,23 +2,24 @@ package com.henrikrydgard.libnative; import android.media.AudioFormat; import android.media.AudioManager; +import android.media.AudioManager.OnAudioFocusChangeListener; import android.media.AudioTrack; import android.util.Log; - +import com.henrikrydgard.libnative.AudioFocusChangeListener; public class NativeAudioPlayer { private String TAG = "NativeAudioPlayer"; private Thread thread; private boolean playing_; - public NativeAudioPlayer() { - } + // Calling stop() is allowed at any time, whether stopped or not. // If playing, blocks until not. public synchronized void stop() { if (thread != null) { waitUntilDone(); + } else { Log.e(TAG, "Was already stopped"); } @@ -32,7 +33,21 @@ public class NativeAudioPlayer { Log.e(TAG, "Was already playing"); } } - + + //keep this static so we can call this even if we don't + //instantiate NativeAudioPlayer + public static void gainAudioFocus(AudioManager audioManager,AudioFocusChangeListener focusChangeListener) { + audioManager.requestAudioFocus(focusChangeListener, + AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); + } + + //keep this static so we can call this even if we don't + //instantiate NativeAudioPlayer + public static void loseAudioFocus(AudioManager audioManager,AudioFocusChangeListener focusChangeListener){ + audioManager.abandonAudioFocus(focusChangeListener); + } + + private void playStreaming() { playing_ = true; thread = new Thread(new Runnable() { From 1a4ba9ef86228425e738c6c23aa926c44807b5ef Mon Sep 17 00:00:00 2001 From: Siddharth Date: Sun, 6 Oct 2013 18:19:09 +0530 Subject: [PATCH 4/4] made unwated changes to base/mutex. changing --- base/mutex.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/mutex.h b/base/mutex.h index 0c7d1c5eec..9b92e831e2 100644 --- a/base/mutex.h +++ b/base/mutex.h @@ -47,7 +47,7 @@ public: atomic_flag() { } - atomic_flag(atomic_flag_init &v) : value(0) { + atomic_flag(const atomic_flag_init &v) : value(0) { } void clear() {