mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-06-16 22:01:11 +00:00
Simplify audio renderer
This commit is contained in:
@@ -6,10 +6,8 @@ import javax.sound.sampled.DataLine;
|
|||||||
import javax.sound.sampled.LineUnavailableException;
|
import javax.sound.sampled.LineUnavailableException;
|
||||||
import javax.sound.sampled.SourceDataLine;
|
import javax.sound.sampled.SourceDataLine;
|
||||||
|
|
||||||
import com.limelight.nvstream.av.ByteBufferDescriptor;
|
|
||||||
import com.limelight.nvstream.av.audio.AudioRenderer;
|
import com.limelight.nvstream.av.audio.AudioRenderer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Audio renderer implementation
|
* Audio renderer implementation
|
||||||
@@ -19,15 +17,10 @@ import java.util.LinkedList;
|
|||||||
public class JavaxAudioRenderer implements AudioRenderer {
|
public class JavaxAudioRenderer implements AudioRenderer {
|
||||||
|
|
||||||
private SourceDataLine soundLine;
|
private SourceDataLine soundLine;
|
||||||
private LinkedList<ByteBufferDescriptor> soundBuffer;
|
|
||||||
private byte[] lineBuffer;
|
|
||||||
private int channelCount;
|
private int channelCount;
|
||||||
private int sampleRate;
|
private int sampleRate;
|
||||||
private boolean reallocateLines;
|
|
||||||
|
|
||||||
public static final int DEFAULT_BUFFER_SIZE = 0;
|
public static final int DEFAULT_BUFFER_SIZE = 4096;
|
||||||
public static final int STARING_BUFFER_SIZE = 4096;
|
|
||||||
public static final int STAGING_BUFFERS = 3; // 3 complete frames of audio
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes some audio data and writes it out to the renderer.
|
* Takes some audio data and writes it out to the renderer.
|
||||||
@@ -37,53 +30,7 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void playDecodedAudio(byte[] pcmData, int offset, int length) {
|
public void playDecodedAudio(byte[] pcmData, int offset, int length) {
|
||||||
if (soundLine != null) {
|
soundLine.write(pcmData, offset, length);
|
||||||
// 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,8 +66,6 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
soundLine.start();
|
soundLine.start();
|
||||||
lineBuffer = new byte[soundLine.getBufferSize()];
|
|
||||||
soundBuffer = new LinkedList<ByteBufferDescriptor>();
|
|
||||||
} catch (LineUnavailableException e) {
|
} catch (LineUnavailableException e) {
|
||||||
soundLine = null;
|
soundLine = null;
|
||||||
}
|
}
|
||||||
@@ -136,8 +81,7 @@ public class JavaxAudioRenderer implements AudioRenderer {
|
|||||||
this.channelCount = channelCount;
|
this.channelCount = channelCount;
|
||||||
this.sampleRate = sampleRate;
|
this.sampleRate = sampleRate;
|
||||||
|
|
||||||
createSoundLine(STARING_BUFFER_SIZE);
|
createSoundLine(DEFAULT_BUFFER_SIZE);
|
||||||
reallocateLines = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user