Drop audio samples if buffer space is not available to preserve real-time audio behavior

This commit is contained in:
Diego Waxemberg
2013-12-13 12:26:46 -05:00
parent 495258338a
commit 277fa53ec1

View File

@@ -20,7 +20,12 @@ public class JavaxAudioRenderer implements AudioRenderer {
if (soundLine != null) { if (soundLine != null) {
byte[] pcmDataBytes = new byte[length * 2]; byte[] pcmDataBytes = new byte[length * 2];
ByteBuffer.wrap(pcmDataBytes).asShortBuffer().put(pcmData, offset, length); ByteBuffer.wrap(pcmDataBytes).asShortBuffer().put(pcmData, offset, length);
soundLine.write(pcmDataBytes, 0, pcmDataBytes.length); if (soundLine.available() < length) {
soundLine.write(pcmDataBytes, 0, soundLine.available());
}
else {
soundLine.write(pcmDataBytes, 0, pcmDataBytes.length);
}
} }
} }
@@ -37,7 +42,7 @@ public class JavaxAudioRenderer implements AudioRenderer {
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, OpusDecoder.getMaxOutputShorts()); DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, OpusDecoder.getMaxOutputShorts());
try { try {
soundLine = (SourceDataLine) AudioSystem.getLine(info); soundLine = (SourceDataLine) AudioSystem.getLine(info);
soundLine.open(audioFormat, 131072); soundLine.open(audioFormat, OpusDecoder.getMaxOutputShorts()*4*2);
soundLine.start(); soundLine.start();
} catch (LineUnavailableException e) { } catch (LineUnavailableException e) {
soundLine = null; soundLine = null;