Simplify audio renderer

This commit is contained in:
Iwan Timmer
2014-01-19 15:35:09 +01:00
parent 78452d7b82
commit c85e23eca2

View File

@@ -6,10 +6,8 @@ import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import com.limelight.nvstream.av.ByteBufferDescriptor;
import com.limelight.nvstream.av.audio.AudioRenderer;
import java.nio.ByteOrder;
import java.util.LinkedList;
/**
* Audio renderer implementation
@@ -19,15 +17,10 @@ import java.util.LinkedList;
public class JavaxAudioRenderer implements AudioRenderer {
private SourceDataLine soundLine;
private LinkedList<ByteBufferDescriptor> soundBuffer;
private byte[] lineBuffer;
private int channelCount;
private int sampleRate;
private boolean reallocateLines;
public static final int DEFAULT_BUFFER_SIZE = 0;
public static final int STARING_BUFFER_SIZE = 4096;
public static final int STAGING_BUFFERS = 3; // 3 complete frames of audio
public static final int DEFAULT_BUFFER_SIZE = 4096;
/**
* Takes some audio data and writes it out to the renderer.
@@ -37,53 +30,7 @@ public class JavaxAudioRenderer implements AudioRenderer {
*/
@Override
public void playDecodedAudio(byte[] pcmData, int offset, int length) {
if (soundLine != null) {
// Queue the decoded samples into the staging sound buffer
if (soundBuffer.size() > STAGING_BUFFERS) {
soundBuffer.removeFirst();
}
soundBuffer.addLast(new ByteBufferDescriptor(pcmData, offset, length));
int available = soundLine.available();
if (reallocateLines) {
// Kinda jank. If the queued is larger than available, we are going to have a delay
// so we increase the buffer size
int size = 0;
for (ByteBufferDescriptor desc : soundBuffer) {
size += desc.length;
}
if (available < size) {
System.out.println("buffer too full, buffer size: " + soundLine.getBufferSize());
int currentBuffer = soundLine.getBufferSize();
soundLine.close();
createSoundLine(currentBuffer*2);
if (soundLine != null) {
available = soundLine.available();
System.out.println("creating new line with buffer size: " + soundLine.getBufferSize());
}
else {
available = 0;
System.out.println("failed to create sound line");
}
}
}
// If there's space available in the sound line, pull some data out
// of the staging buffer and write it to the sound line
while (available > 0 && !soundBuffer.isEmpty()) {
ByteBufferDescriptor buff = soundBuffer.peek();
if (buff.length > available) {
break;
}
available -= soundLine.write(buff.data, buff.offset, buff.length);
soundBuffer.remove();
}
}
soundLine.write(pcmData, offset, length);
}
/**
@@ -119,8 +66,6 @@ public class JavaxAudioRenderer implements AudioRenderer {
}
soundLine.start();
lineBuffer = new byte[soundLine.getBufferSize()];
soundBuffer = new LinkedList<ByteBufferDescriptor>();
} catch (LineUnavailableException e) {
soundLine = null;
}
@@ -136,8 +81,7 @@ public class JavaxAudioRenderer implements AudioRenderer {
this.channelCount = channelCount;
this.sampleRate = sampleRate;
createSoundLine(STARING_BUFFER_SIZE);
reallocateLines = true;
createSoundLine(DEFAULT_BUFFER_SIZE);
}
}