Fix audio lag if the audio player takes a while to start

This commit is contained in:
Cameron Gutman 2014-10-20 03:34:15 -04:00
parent f133f293b8
commit e56923a71d

View File

@ -35,9 +35,12 @@ struct AUDIO_BUFFER_QUEUE_ENTRY {
char data[0]; char data[0];
}; };
#define MAX_QUEUE_ENTRIES 10
static short decodedPcmBuffer[512]; static short decodedPcmBuffer[512];
static NSLock *audioLock; static NSLock *audioLock;
static struct AUDIO_BUFFER_QUEUE_ENTRY *audioBufferQueue; static struct AUDIO_BUFFER_QUEUE_ENTRY *audioBufferQueue;
static int audioBufferQueueLength;
static AudioComponentInstance audioUnit; static AudioComponentInstance audioUnit;
static VideoDecoderRenderer* renderer; static VideoDecoderRenderer* renderer;
@ -127,6 +130,19 @@ void ArDecodeAndPlaySample(char* sampleData, int sampleLength)
memcpy(newEntry->data, decodedPcmBuffer, decodedLength); memcpy(newEntry->data, decodedPcmBuffer, decodedLength);
[audioLock lock]; [audioLock lock];
if (audioBufferQueueLength > MAX_QUEUE_ENTRIES) {
NSLog(@"Audio player too slow. Dropping all decoded samples!");
// Clear all values from the buffer queue
struct AUDIO_BUFFER_QUEUE_ENTRY *entry;
while (audioBufferQueue != NULL) {
entry = audioBufferQueue;
audioBufferQueue = entry->next;
audioBufferQueueLength--;
free(entry);
}
}
if (audioBufferQueue == NULL) { if (audioBufferQueue == NULL) {
audioBufferQueue = newEntry; audioBufferQueue = newEntry;
} }
@ -137,6 +153,8 @@ void ArDecodeAndPlaySample(char* sampleData, int sampleLength)
} }
lastEntry->next = newEntry; lastEntry->next = newEntry;
} }
audioBufferQueueLength++;
[audioLock unlock]; [audioLock unlock];
} }
} }
@ -185,7 +203,7 @@ void ClDisplayTransientMessage(char* message)
streamConfig.width = width; streamConfig.width = width;
streamConfig.height = height; streamConfig.height = height;
streamConfig.fps = 30; streamConfig.fps = 60;
streamConfig.bitrate = 5000; streamConfig.bitrate = 5000;
streamConfig.packetSize = 1024; streamConfig.packetSize = 1024;
// FIXME: RI AES members // FIXME: RI AES members
@ -310,6 +328,7 @@ static OSStatus playbackCallback(void *inRefCon,
// Dequeue this entry temporarily // Dequeue this entry temporarily
audioEntry = audioBufferQueue; audioEntry = audioBufferQueue;
audioBufferQueue = audioBufferQueue->next; audioBufferQueue = audioBufferQueue->next;
audioBufferQueueLength--;
} }
[audioLock unlock]; [audioLock unlock];
} }
@ -330,6 +349,7 @@ static OSStatus playbackCallback(void *inRefCon,
[audioLock lock]; [audioLock lock];
audioEntry->next = audioBufferQueue; audioEntry->next = audioBufferQueue;
audioBufferQueue = audioEntry; audioBufferQueue = audioEntry;
audioBufferQueueLength++;
[audioLock unlock]; [audioLock unlock];
} }
else { else {