Initialize OpenAL context in the render thread

Also remove useless AndroidAudioRenderer from jme3-ios
experimental
shadowislord 10 years ago
parent 62a235fbf7
commit 2ec7366fde
  1. 189
      jme3-core/src/main/java/com/jme3/audio/openal/ALAudioRenderer.java
  2. 24
      jme3-ios/src/main/java/com/jme3/audio/android/AndroidAudioRenderer.java

@ -87,67 +87,8 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
this.alc = alc; this.alc = alc;
this.efx = efx; this.efx = efx;
} }
public void initialize() { private void initOpenAL() {
if (!decoderThread.isAlive()) {
// Set high priority to avoid buffer starvation.
decoderThread.setDaemon(true);
decoderThread.setPriority(Thread.NORM_PRIORITY + 1);
decoderThread.start();
} else {
throw new IllegalStateException("Initialize already called");
}
}
private void checkDead() {
if (decoderThread.getState() == Thread.State.TERMINATED) {
throw new IllegalStateException("Decoding thread is terminated");
}
}
public void run() {
initInDecoderThread();
// Notify render thread that OAL context is available.
synchronized (threadLock) {
threadLock.set(true);
threadLock.notifyAll();
}
long updateRateNanos = (long) (UPDATE_RATE * 1000000000);
mainloop:
while (true) {
long startTime = System.nanoTime();
if (Thread.interrupted()) {
break;
}
synchronized (threadLock) {
updateInDecoderThread(UPDATE_RATE);
}
long endTime = System.nanoTime();
long diffTime = endTime - startTime;
if (diffTime < updateRateNanos) {
long desiredEndTime = startTime + updateRateNanos;
while (System.nanoTime() < desiredEndTime) {
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
break mainloop;
}
}
}
}
synchronized (threadLock) {
cleanupInDecoderThread();
}
}
public void initInDecoderThread() {
try { try {
if (!alc.isCreated()) { if (!alc.isCreated()) {
alc.createALC(); alc.createALC();
@ -228,8 +169,8 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
logger.log(Level.WARNING, "OpenAL EFX not available! Audio effects won't work."); logger.log(Level.WARNING, "OpenAL EFX not available! Audio effects won't work.");
} }
} }
public void cleanupInDecoderThread() { private void destroyOpenAL() {
if (audioDisabled) { if (audioDisabled) {
alc.destroyALC(); alc.destroyALC();
return; return;
@ -266,17 +207,73 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
alc.destroyALC(); alc.destroyALC();
} }
public void cleanup() { public void initialize() {
// kill audio thread
if (decoderThread.isAlive()) { if (decoderThread.isAlive()) {
decoderThread.interrupt(); throw new IllegalStateException("Initialize already called");
try { }
decoderThread.join();
} catch (InterruptedException ex) { // Initialize OpenAL context.
initOpenAL();
// Initialize decoder thread.
// Set high priority to avoid buffer starvation.
decoderThread.setDaemon(true);
decoderThread.setPriority(Thread.NORM_PRIORITY + 1);
decoderThread.start();
}
private void checkDead() {
if (decoderThread.getState() == Thread.State.TERMINATED) {
throw new IllegalStateException("Decoding thread is terminated");
}
}
public void run() {
long updateRateNanos = (long) (UPDATE_RATE * 1000000000);
mainloop:
while (true) {
long startTime = System.nanoTime();
if (Thread.interrupted()) {
break;
}
synchronized (threadLock) {
updateInDecoderThread(UPDATE_RATE);
}
long endTime = System.nanoTime();
long diffTime = endTime - startTime;
if (diffTime < updateRateNanos) {
long desiredEndTime = startTime + updateRateNanos;
while (System.nanoTime() < desiredEndTime) {
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
break mainloop;
}
}
} }
} }
} }
public void cleanup() {
// kill audio thread
if (!decoderThread.isAlive()) {
return;
}
decoderThread.interrupt();
try {
decoderThread.join();
} catch (InterruptedException ex) {
}
// destroy OpenAL context
destroyOpenAL();
}
private void updateFilter(Filter f) { private void updateFilter(Filter f) {
int id = f.getId(); int id = f.getId();
if (id == -1) { if (id == -1) {
@ -304,12 +301,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void updateSourceParam(AudioSource src, AudioParam param) { public void updateSourceParam(AudioSource src, AudioParam param) {
checkDead(); checkDead();
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }
@ -536,12 +527,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void updateListenerParam(Listener listener, ListenerParam param) { public void updateListenerParam(Listener listener, ListenerParam param) {
checkDead(); checkDead();
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }
@ -608,12 +593,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void setEnvironment(Environment env) { public void setEnvironment(Environment env) {
checkDead(); checkDead();
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled || !supportEfx) { if (audioDisabled || !supportEfx) {
return; return;
} }
@ -892,12 +871,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void setListener(Listener listener) { public void setListener(Listener listener) {
checkDead(); checkDead();
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }
@ -933,12 +906,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void playSourceInstance(AudioSource src) { public void playSourceInstance(AudioSource src) {
checkDead(); checkDead();
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }
@ -976,12 +943,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void playSource(AudioSource src) { public void playSource(AudioSource src) {
checkDead(); checkDead();
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }
@ -1018,12 +979,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void pauseSource(AudioSource src) { public void pauseSource(AudioSource src) {
checkDead(); checkDead();
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }
@ -1039,12 +994,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void stopSource(AudioSource src) { public void stopSource(AudioSource src) {
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }
@ -1140,12 +1089,6 @@ public class ALAudioRenderer implements AudioRenderer, Runnable {
public void deleteAudioData(AudioData ad) { public void deleteAudioData(AudioData ad) {
synchronized (threadLock) { synchronized (threadLock) {
while (!threadLock.get()) {
try {
threadLock.wait();
} catch (InterruptedException ex) {
}
}
if (audioDisabled) { if (audioDisabled) {
return; return;
} }

@ -1,24 +0,0 @@
package com.jme3.audio.android;
import com.jme3.audio.AudioRenderer;
/**
* Android specific AudioRenderer interface that supports pausing and resuming
* audio files when the app is minimized or placed in the background
*
* @author iwgeric
*/
public interface AndroidAudioRenderer extends AudioRenderer {
/**
* Pauses all Playing audio. To be used when the app is placed in the
* background.
*/
public void pauseAll();
/**
* Resumes all Paused audio. To be used when the app is brought back to
* the foreground.
*/
public void resumeAll();
}
Loading…
Cancel
Save