Less audio stutter

This commit is contained in:
Iwan Timmer
2015-03-29 22:54:25 +02:00
parent ba65e6252e
commit fe829b32fe
6 changed files with 68 additions and 88 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}