Merge remote-tracking branch 'origin'

This commit is contained in:
Philip Rosedale 2013-02-07 15:29:49 -08:00
commit 9de62ea83b
3 changed files with 39 additions and 67 deletions

View file

@ -29,7 +29,7 @@ const short RING_BUFFER_FRAMES = 10;
const short RING_BUFFER_SIZE_SAMPLES = RING_BUFFER_FRAMES * BUFFER_LENGTH_SAMPLES; const short RING_BUFFER_SIZE_SAMPLES = RING_BUFFER_FRAMES * BUFFER_LENGTH_SAMPLES;
const int SAMPLE_RATE = 22050; const int SAMPLE_RATE = 22050;
const float JITTER_BUFFER_LENGTH_MSECS = 26.0; const float JITTER_BUFFER_LENGTH_MSECS = 10.0;
const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_LENGTH_MSECS * (SAMPLE_RATE / 1000.0); const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_LENGTH_MSECS * (SAMPLE_RATE / 1000.0);
const short NUM_AUDIO_SOURCES = 2; const short NUM_AUDIO_SOURCES = 2;
@ -105,31 +105,26 @@ int audioCallback (const void *inputBuffer,
if (ringBuffer->endOfLastWrite != NULL) { if (ringBuffer->endOfLastWrite != NULL) {
// play whatever we have in the audio buffer if (!ringBuffer->started && ringBuffer->diffLastWriteNextOutput() <= PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES) {
short silentTail = 0; printf("Held back\n");
} else if (ringBuffer->diffLastWriteNextOutput() < PACKET_LENGTH_SAMPLES) {
ringBuffer->started = false;
// if the end of the last write to the ring is in front of the current output pointer starve_counter++;
// AND the difference between the two is less than a full output buffer printf("Starved #%d\n", starve_counter);
// we need to add some silence after the audio data, to avoid replaying old data data->wasStarved = 10; // Frames to render the indication that the system was starved.
if ((ringBuffer->endOfLastWrite - ringBuffer->buffer) > (ringBuffer->nextOutput - ringBuffer->buffer) } else {
&& (ringBuffer->endOfLastWrite - ringBuffer->nextOutput) < BUFFER_LENGTH_SAMPLES) { ringBuffer->started = true;
silentTail = BUFFER_LENGTH_SAMPLES - (ringBuffer->endOfLastWrite - ringBuffer->nextOutput); // play whatever we have in the audio buffer
}
// no sample overlap, either a direct copy of the audio data, or a copy with some appended silence // no sample overlap, either a direct copy of the audio data, or a copy with some appended silence
memcpy(queueBuffer, ringBuffer->nextOutput, (BUFFER_LENGTH_SAMPLES - silentTail) * sizeof(int16_t)); memcpy(queueBuffer, ringBuffer->nextOutput, BUFFER_LENGTH_BYTES);
ringBuffer->nextOutput += BUFFER_LENGTH_SAMPLES; ringBuffer->nextOutput += BUFFER_LENGTH_SAMPLES;
if (ringBuffer->nextOutput == ringBuffer->buffer + RING_BUFFER_SIZE_SAMPLES) { if (ringBuffer->nextOutput == ringBuffer->buffer + RING_BUFFER_SIZE_SAMPLES) {
ringBuffer->nextOutput = ringBuffer->buffer; ringBuffer->nextOutput = ringBuffer->buffer;
} }
if (ringBuffer->diffLastWriteNextOutput() < BUFFER_LENGTH_SAMPLES) {
starve_counter++;
printf("Starved #%d\n", starve_counter);
data->wasStarved = 10; // Frames to render the indication that the system was starved.
ringBuffer->endOfLastWrite = NULL;
} }
} }
@ -255,53 +250,28 @@ void *receiveAudioViaUDP(void *args) {
AudioRingBuffer *ringBuffer = sharedAudioData->ringBuffer; AudioRingBuffer *ringBuffer = sharedAudioData->ringBuffer;
int16_t *copyToPointer; if (ringBuffer->endOfLastWrite == NULL) {
bool needsJitterBuffer = ringBuffer->endOfLastWrite == NULL; ringBuffer->endOfLastWrite = ringBuffer->buffer;
short bufferSampleOverlap = 0; } else if (ringBuffer->diffLastWriteNextOutput() > RING_BUFFER_SIZE_SAMPLES - PACKET_LENGTH_SAMPLES) {
std::cout << "NAB: " << ringBuffer->nextOutput - ringBuffer->buffer << "\n";
if (!needsJitterBuffer && ringBuffer->diffLastWriteNextOutput() > RING_BUFFER_SIZE_SAMPLES - PACKET_LENGTH_SAMPLES) { std::cout << "LAW: " << ringBuffer->endOfLastWrite - ringBuffer->buffer << "\n";
std::cout << "D: " << ringBuffer->diffLastWriteNextOutput() << "\n";
std::cout << "Full\n"; std::cout << "Full\n";
needsJitterBuffer = true;
}
if (needsJitterBuffer) { // reset us to started state
// we'll need a jitter buffer ringBuffer->endOfLastWrite = ringBuffer->buffer;
// reset the ring buffer and write
copyToPointer = ringBuffer->buffer;
//std::cout << "Writing jitter buffer\n";
} else {
copyToPointer = ringBuffer->endOfLastWrite;
// check for possibility of overlap
bufferSampleOverlap = ringBuffer->bufferOverlap(copyToPointer, PACKET_LENGTH_SAMPLES);
}
if (!bufferSampleOverlap) {
if (needsJitterBuffer) {
// we need to inject a jitter buffer
// add silence for jitter buffer and then the received packet
memset(copyToPointer, 0, JITTER_BUFFER_SAMPLES * sizeof(int16_t));
memcpy(copyToPointer + JITTER_BUFFER_SAMPLES, receivedData, PACKET_LENGTH_BYTES);
// the end of the write is the pointer to the buffer + packet + jitter buffer
ringBuffer->endOfLastWrite = ringBuffer->buffer + PACKET_LENGTH_SAMPLES + JITTER_BUFFER_SAMPLES;
ringBuffer->nextOutput = ringBuffer->buffer; ringBuffer->nextOutput = ringBuffer->buffer;
} else { ringBuffer->started = false;
// no jitter buffer, no overlap }
int16_t *copyToPointer = ringBuffer->endOfLastWrite;
// just copy the recieved data to the right spot and then add packet length to previous pointer // just copy the recieved data to the right spot and then add packet length to previous pointer
memcpy(copyToPointer, receivedData, PACKET_LENGTH_BYTES); memcpy(copyToPointer, receivedData, PACKET_LENGTH_BYTES);
ringBuffer->endOfLastWrite += PACKET_LENGTH_SAMPLES; ringBuffer->endOfLastWrite += PACKET_LENGTH_SAMPLES;
}
} else {
// no jitter buffer, but overlap
// copy to the end, and then from the begining to the overlap
memcpy(copyToPointer, receivedData, (PACKET_LENGTH_SAMPLES - bufferSampleOverlap) * sizeof(int16_t));
memcpy(ringBuffer->buffer, receivedData + (PACKET_LENGTH_SAMPLES - bufferSampleOverlap), bufferSampleOverlap * sizeof(int16_t));
// the end of the write is the amount of overlap if (ringBuffer->endOfLastWrite == ringBuffer->buffer + RING_BUFFER_SIZE_SAMPLES) {
ringBuffer->endOfLastWrite = ringBuffer->buffer + bufferSampleOverlap; ringBuffer->endOfLastWrite = ringBuffer->buffer;
} }
if (LOG_SAMPLE_DELAY) { if (LOG_SAMPLE_DELAY) {

View file

@ -10,6 +10,7 @@
AudioRingBuffer::AudioRingBuffer(short ringBufferSamples) { AudioRingBuffer::AudioRingBuffer(short ringBufferSamples) {
ringBufferLengthSamples = ringBufferSamples; ringBufferLengthSamples = ringBufferSamples;
started = false;
endOfLastWrite = NULL; endOfLastWrite = NULL;

View file

@ -17,6 +17,7 @@ class AudioRingBuffer {
int16_t *endOfLastWrite; int16_t *endOfLastWrite;
int16_t *buffer; int16_t *buffer;
short ringBufferLengthSamples; short ringBufferLengthSamples;
bool started;
short diffLastWriteNextOutput(); short diffLastWriteNextOutput();
short bufferOverlap(int16_t *pointer, short addedDistance); short bufferOverlap(int16_t *pointer, short addedDistance);