Lower the buffer size of the Opus decoder based on the data we're actual receiving. Handle invocations of the decoder with null data for packet loss indication.

This commit is contained in:
Cameron Gutman 2013-11-12 02:04:08 -05:00
parent 77786f9693
commit 4eaf68ae58
4 changed files with 42 additions and 33 deletions

View File

@ -30,7 +30,7 @@ int nv_opus_get_channel_count(void) {
// This number assumes 2 channels at 48 KHz // This number assumes 2 channels at 48 KHz
int nv_opus_get_max_out_shorts(void) { int nv_opus_get_max_out_shorts(void) {
return 5760*2; return 512*nv_opus_get_channel_count();
} }
// The Opus stream is 48 KHz // The Opus stream is 48 KHz
@ -48,7 +48,7 @@ int nv_opus_decode(unsigned char* indata, int inlen, short* outpcmdata) {
// Decoding to 16-bit PCM with FEC off // Decoding to 16-bit PCM with FEC off
// Maximum length assuming 48KHz sample rate // Maximum length assuming 48KHz sample rate
err = opus_decode(decoder, indata, inlen, err = opus_decode(decoder, indata, inlen,
outpcmdata, 5760, 0); outpcmdata, 512, 0);
return err; return err;
} }

View File

@ -1,5 +1,6 @@
#include "nv_opus_dec.h" #include "nv_opus_dec.h"
#include <stdlib.h>
#include <jni.h> #include <jni.h>
// This function must be called before // This function must be called before
@ -48,13 +49,19 @@ Java_com_limelight_nvstream_av_audio_OpusDecoder_decode(
jbyte* jni_input_data; jbyte* jni_input_data;
jshort* jni_pcm_data; jshort* jni_pcm_data;
jni_input_data = (*env)->GetByteArrayElements(env, indata, 0);
jni_pcm_data = (*env)->GetShortArrayElements(env, outpcmdata, 0); jni_pcm_data = (*env)->GetShortArrayElements(env, outpcmdata, 0);
if (indata != NULL) {
jni_input_data = (*env)->GetByteArrayElements(env, indata, 0);
ret = nv_opus_decode(&jni_input_data[inoff], inlen, jni_pcm_data); ret = nv_opus_decode(&jni_input_data[inoff], inlen, jni_pcm_data);
// The input data isn't changed so it can be safely aborted
(*env)->ReleaseByteArrayElements(env, indata, jni_input_data, JNI_ABORT);
}
else {
ret = nv_opus_decode(NULL, 0, jni_pcm_data);
}
// The input data isn't changed so it can be safely aborted
(*env)->ReleaseByteArrayElements(env, indata, jni_input_data, JNI_ABORT);
(*env)->ReleaseShortArrayElements(env, outpcmdata, jni_pcm_data, 0); (*env)->ReleaseShortArrayElements(env, outpcmdata, jni_pcm_data, 0);
return ret; return ret;

Binary file not shown.

View File

@ -11,7 +11,7 @@ public class AvAudioDepacketizer {
private LinkedBlockingQueue<AvShortBufferDescriptor> decodedUnits = private LinkedBlockingQueue<AvShortBufferDescriptor> decodedUnits =
new LinkedBlockingQueue<AvShortBufferDescriptor>(15); new LinkedBlockingQueue<AvShortBufferDescriptor>(15);
private AvShortBufferPool pool = new AvShortBufferPool(512); private AvShortBufferPool pool = new AvShortBufferPool(OpusDecoder.getMaxOutputShorts());
// Sequencing state // Sequencing state
private short lastSequenceNumber; private short lastSequenceNumber;
@ -21,34 +21,11 @@ public class AvAudioDepacketizer {
pool.purge(); pool.purge();
} }
public void decodeInputData(AvRtpPacket packet) private void decodeData(byte[] data, int off, int len)
{ {
short seq = packet.getSequenceNumber();
if (packet.getPacketType() != 97) {
// Only type 97 is audio
return;
}
// Toss out the current NAL if we receive a packet that is
// out of sequence
if (lastSequenceNumber != 0 &&
(short)(lastSequenceNumber + 1) != seq)
{
System.out.println("Received OOS audio data (expected "+(lastSequenceNumber + 1)+", got "+seq+")");
// Tell the decoder about this packet loss
//OpusDecoder.decode(null, 0, 0, null);
}
lastSequenceNumber = seq;
// This is all the depacketizing we need to do
AvByteBufferDescriptor rtpPayload = packet.getNewPayloadDescriptor();
// Submit this data to the decoder // Submit this data to the decoder
short[] pcmData = pool.allocate(); short[] pcmData = pool.allocate();
int decodeLen = OpusDecoder.decode(rtpPayload.data, rtpPayload.offset, rtpPayload.length, pcmData); int decodeLen = OpusDecoder.decode(data, off, len, pcmData);
if (decodeLen > 0) { if (decodeLen > 0) {
// Return value of decode is frames decoded per channel // Return value of decode is frames decoded per channel
@ -66,6 +43,31 @@ public class AvAudioDepacketizer {
} }
} }
public void decodeInputData(AvRtpPacket packet)
{
short seq = packet.getSequenceNumber();
if (packet.getPacketType() != 97) {
// Only type 97 is audio
return;
}
// Toss out the current NAL if we receive a packet that is
// out of sequence
if (lastSequenceNumber != 0 &&
(short)(lastSequenceNumber + 1) != seq)
{
System.out.println("Received OOS audio data (expected "+(lastSequenceNumber + 1)+", got "+seq+")");
decodeData(null, 0, 0);
}
lastSequenceNumber = seq;
// This is all the depacketizing we need to do
AvByteBufferDescriptor rtpPayload = packet.getNewPayloadDescriptor();
decodeData(rtpPayload.data, rtpPayload.offset, rtpPayload.length);
}
public void releaseBuffer(AvShortBufferDescriptor decodedData) public void releaseBuffer(AvShortBufferDescriptor decodedData)
{ {
pool.free(decodedData.data); pool.free(decodedData.data);