Start up streams on the sound queue thread and handle OpenAL barfing on startup

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@648 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Charlie Groves
2008-08-26 20:23:02 +00:00
parent fff73d94cc
commit e4cdad7741
@@ -25,6 +25,7 @@ import com.google.common.collect.Maps;
import com.samskivert.util.BasicRunQueue; import com.samskivert.util.BasicRunQueue;
import com.samskivert.util.RandomUtil; import com.samskivert.util.RandomUtil;
import com.samskivert.util.ResultListener;
import com.samskivert.util.RunQueue; import com.samskivert.util.RunQueue;
import com.threerings.media.FrameManager; import com.threerings.media.FrameManager;
@@ -43,8 +44,12 @@ public class OpenALSoundPlayer extends SoundPlayer
public OpenALSoundPlayer (SoundLoader loader) public OpenALSoundPlayer (SoundLoader loader)
{ {
_loader = loader; _loader = loader;
_alSoundManager = new MediaALSoundManager(); try {
_group = _alSoundManager.createGroup(this, SOURCE_COUNT); _alSoundManager = new MediaALSoundManager();
_group = _alSoundManager.createGroup(this, SOURCE_COUNT);
} catch (Throwable t) {
log.warning("Unable to initialize OpenAL", "cause", t);
}
_ticker.start(); _ticker.start();
} }
@@ -155,49 +160,53 @@ public class OpenALSoundPlayer extends SoundPlayer
/** /**
* Streams ogg files from the given bundle and path. * Streams ogg files from the given bundle and path.
*/ */
public Stream stream (final String bundle, final String path, final boolean loop) public void stream (final String bundle, final String path, final boolean loop,
final ResultListener<Stream> listener)
throws IOException throws IOException
{ {
if (!path.endsWith(".ogg")) { if (!path.endsWith(".ogg")) {
log.warning("Unknown file type for streaming", "bundle", bundle, "path", path); log.warning("Unknown file type for streaming", "bundle", bundle, "path", path);
return null; return;
} }
InputStream rsrc = _loader.getSound(bundle, path); InputStream rsrc = _loader.getSound(bundle, path);
final StreamDecoder dec = new OggStreamDecoder(); final StreamDecoder dec = new OggStreamDecoder();
dec.init(rsrc); dec.init(rsrc);
Stream s = new Stream(_alSoundManager) { getSoundQueue().postRunnable(new Runnable(){
public void run () {
Stream s = new Stream(_alSoundManager) {
@Override @Override
protected void update (float time) { protected void update (float time) {
super.update(time); super.update(time);
if (_state != AL10.AL_PLAYING) { if (_state != AL10.AL_PLAYING) {
return; return;
} }
setGain(_clipVol); setGain(_clipVol);
} }
@Override @Override
protected int getFormat () { protected int getFormat () {
return dec.getFormat(); return dec.getFormat();
} }
@Override @Override
protected int getFrequency () { protected int getFrequency () {
return dec.getFrequency(); return dec.getFrequency();
} }
@Override @Override
protected int populateBuffer (ByteBuffer buf) throws IOException { protected int populateBuffer (ByteBuffer buf) throws IOException {
int read = dec.read(buf); int read = dec.read(buf);
if(buf.hasRemaining() && loop) { if(buf.hasRemaining() && loop) {
dec.init(_loader.getSound(bundle, path)); dec.init(_loader.getSound(bundle, path));
read = Math.max(0, read); read = Math.max(0, read);
read += dec.read(buf); read += dec.read(buf);
} }
return read; return read;
}}; }};
s.setGain(_clipVol); s.setGain(_clipVol);
return s; listener.requestCompleted(s);
}});
} }
@Override @Override
@@ -245,11 +254,14 @@ public class OpenALSoundPlayer extends SoundPlayer
@Override @Override
public void shutdown () public void shutdown ()
{ {
_group.dispose(); getSoundQueue().postRunnable(new Runnable(){
_locked.clear(); public void run () {
for (Stream stream : _alSoundManager.getStreams()) { _group.dispose();
stream.dispose(); _locked.clear();
} for (Stream stream : _alSoundManager.getStreams()) {
stream.dispose();
}
}});
} }
/** /**
@@ -308,6 +320,12 @@ public class OpenALSoundPlayer extends SoundPlayer
} else { } else {
r = _queue.get(STREAM_UPDATE_INTERVAL - elapsed); r = _queue.get(STREAM_UPDATE_INTERVAL - elapsed);
} }
if (_alSoundManager == null) {
// We weren't able to initialize the sound system, and we logged it earlier, so
// just empty the queue and bail without running the code that needs the sound
// manager.
return;
}
if (r != null) { if (r != null) {
try { try {
r.run(); r.run();
@@ -359,9 +377,9 @@ public class OpenALSoundPlayer extends SoundPlayer
protected Map<String, ClipBuffer> _locked = Maps.newHashMap(); protected Map<String, ClipBuffer> _locked = Maps.newHashMap();
protected final SoundLoader _loader; protected SoundLoader _loader;
protected final SoundGroup _group; protected SoundGroup _group;
protected final SoundManager _alSoundManager; protected SoundManager _alSoundManager;
/** Number of sounds that can be played simultaneously. */ /** Number of sounds that can be played simultaneously. */
protected final int SOURCE_COUNT = 10; protected final int SOURCE_COUNT = 10;