Skip to content

Commit

Permalink
Use the audio buffer mutex appropriately
Browse files Browse the repository at this point in the history
We need to use this any time we write to or access any of the
audio-buffer related variables. This also removes the need to use
'volatile', which cripples the compiler optimizations.
  • Loading branch information
toofishes committed Feb 24, 2012
1 parent 8b1728d commit cd95704
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions hairtunes.c
Expand Up @@ -106,14 +106,14 @@ typedef struct audio_buffer_entry { // decoded audio packets
int ready;
signed short *data;
} abuf_t;
volatile abuf_t audio_buffer[BUFFER_FRAMES];
static abuf_t audio_buffer[BUFFER_FRAMES];
#define BUFIDX(seqno) ((seq_t)(seqno) % BUFFER_FRAMES)

// mutex-protected variables
volatile seq_t ab_read, ab_write;
int ab_buffering = 1, ab_synced = 0;
pthread_mutex_t ab_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ab_buffer_ready = PTHREAD_COND_INITIALIZER;
static seq_t ab_read, ab_write;
static int ab_buffering = 1, ab_synced = 0;
static pthread_mutex_t ab_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t ab_buffer_ready = PTHREAD_COND_INITIALIZER;

static void die(char *why) {
fprintf(stderr, "FATAL: %s\n", why);
Expand Down Expand Up @@ -391,10 +391,12 @@ static void buffer_put_packet(seq_t seqno, char *data, int len) {
abuf->ready = 1;
}

pthread_mutex_lock(&ab_mutex);
if (ab_buffering && buf_fill >= buffer_start_fill) {
ab_buffering = 0;
pthread_cond_signal(&ab_buffer_ready);
}
pthread_mutex_unlock(&ab_mutex);
}

static int rtp_sockets[2]; // data, control
Expand Down Expand Up @@ -641,7 +643,7 @@ static void bf_est_update(short fill) {
static short *buffer_get_frame(void) {
short buf_fill;
seq_t read;
volatile abuf_t *abuf = 0;
abuf_t *abuf = 0;
unsigned short next;
int i;

Expand All @@ -656,9 +658,9 @@ static short *buffer_get_frame(void) {
pthread_cond_wait(&ab_buffer_ready, &ab_mutex);
ab_read++;
buf_fill = ab_write - ab_read;
bf_est_reset(buf_fill);
pthread_mutex_unlock(&ab_mutex);

bf_est_reset(buf_fill);
return 0;
}
if (buf_fill >= BUFFER_FRAMES) { // overrunning! uh-oh. restart at a sane distance
Expand All @@ -667,8 +669,6 @@ static short *buffer_get_frame(void) {
}
read = ab_read;
ab_read++;
pthread_mutex_unlock(&ab_mutex);

buf_fill = ab_write - ab_read;
bf_est_update(buf_fill);

Expand All @@ -683,12 +683,14 @@ static short *buffer_get_frame(void) {
}
}

volatile abuf_t *curframe = audio_buffer + BUFIDX(read);
abuf_t *curframe = audio_buffer + BUFIDX(read);
if (!curframe->ready) {
fprintf(stderr, "\nmissing frame.\n");
memset(curframe->data, 0, FRAME_BYTES);
}
curframe->ready = 0;
pthread_mutex_unlock(&ab_mutex);

return curframe->data;
}

Expand Down

0 comments on commit cd95704

Please sign in to comment.