mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-04-10 18:06:32 +00:00
Less audio stutter
This commit is contained in:
@@ -9,39 +9,40 @@ import com.limelight.nvstream.av.audio.AudioDecoderRenderer;
|
||||
*/
|
||||
public class AlsaAudioDecoderRenderer implements AudioDecoderRenderer {
|
||||
|
||||
private final static int CHANNEL_COUNT = 2;
|
||||
private final static int SAMPLE_RATE = 48000;
|
||||
|
||||
/* Number of 16 bits frames */
|
||||
private final static int FRAME_SIZE = 240;
|
||||
|
||||
private String device;
|
||||
private byte[] decodedData;
|
||||
|
||||
public AlsaAudioDecoderRenderer(String device) {
|
||||
this.device = device;
|
||||
this.decodedData = new byte[OpusDecoder.getMaxOutputShorts()*2];
|
||||
|
||||
int err;
|
||||
|
||||
err = OpusDecoder.init();
|
||||
if (err != 0) {
|
||||
throw new IllegalStateException("Opus decoder failed to initialize");
|
||||
}
|
||||
this.decodedData = new byte[FRAME_SIZE * CHANNEL_COUNT * 2];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean streamInitialize() {
|
||||
return AlsaAudio.init(OpusDecoder.getChannelCount(), OpusDecoder.getSampleRate(), device) == 0;
|
||||
return OpusDecoder.init(CHANNEL_COUNT, SAMPLE_RATE) == 0 && AlsaAudio.init(CHANNEL_COUNT, SAMPLE_RATE, device) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playAudio(byte[] bytes, int offset, int length) {
|
||||
int decodeLen = OpusDecoder.decode(bytes, offset, length, decodedData);
|
||||
int decodeLen = OpusDecoder.decode(bytes, offset, length, FRAME_SIZE, decodedData);
|
||||
|
||||
if (decodeLen > 0) {
|
||||
//Value of decode is frames (shorts) decoded per channel
|
||||
decodeLen *= 2*OpusDecoder.getChannelCount();
|
||||
decodeLen *= CHANNEL_COUNT * 2;
|
||||
int rc = AlsaAudio.play(decodedData, 0, decodeLen);
|
||||
|
||||
if (rc<0)
|
||||
LimeLog.warning("Alsa error from writei: "+rc);
|
||||
else if (rc!=length/4)
|
||||
else if (rc!=decodeLen/4)
|
||||
LimeLog.warning("Alsa short write, write "+rc+" frames");
|
||||
} else {
|
||||
LimeLog.warning("Opus error from decode: "+decodeLen);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,7 @@ public class OpusDecoder {
|
||||
System.loadLibrary("nv_opus_dec");
|
||||
}
|
||||
|
||||
public static native int init();
|
||||
public static native int init(int channelCoumt, int sampleRate);
|
||||
public static native void destroy();
|
||||
public static native int getChannelCount();
|
||||
public static native int getMaxOutputShorts();
|
||||
public static native int getSampleRate();
|
||||
public static native int decode(byte[] indata, int inoff, int inlen, byte[] outpcmdata);
|
||||
public static native int decode(byte[] indata, int inoff, int inlen, int frameSize, byte[] outpcmdata);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user