Skip to content

Commit

Permalink
Make the play function return a status (ignored for the present). Cle…
Browse files Browse the repository at this point in the history
…an up audio_alsa by removing all the die calls and placing the alsa_mutex calls in pthread cleanup routines.
  • Loading branch information
mikebrady committed Jul 29, 2018
1 parent c91a991 commit ea20840
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 169 deletions.
2 changes: 1 addition & 1 deletion audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef struct {
void (*start)(int sample_rate, int sample_format);

// block of samples
void (*play)(void *buf, int samples);
int (*play)(void *buf, int samples);
void (*stop)(void);

// may be null if not implemented
Expand Down
342 changes: 182 additions & 160 deletions audio_alsa.c

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion audio_ao.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ static void deinit(void) {
static void start(__attribute__((unused)) int sample_rate,
__attribute__((unused)) int sample_format) {}

static void play(void *buf, int samples) { ao_play(dev, buf, samples * 4); }
static int play(void *buf, int samples) {
return ao_play(dev, buf, samples * 4);
}

static void stop(void) {}

Expand Down
2 changes: 1 addition & 1 deletion audio_dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static void start(int sample_rate, __attribute__((unused)) int sample_format) {
debug(1, "dummy audio output started at Fs=%d Hz\n", sample_rate);
}

static void play(__attribute__((unused)) void *buf, __attribute__((unused)) int samples) {}
static int play(__attribute__((unused)) void *buf, __attribute__((unused)) int samples) { return 0; }

static void stop(void) { debug(1, "dummy audio stopped\n"); }

Expand Down
3 changes: 2 additions & 1 deletion audio_pa.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static void start(__attribute__((unused)) int sample_rate,
pa_threaded_mainloop_unlock(mainloop);
}

static void play(void *buf, int samples) {
static int play(void *buf, int samples) {
// debug(1,"pa_play of %d samples.",samples);
// copy the samples into the queue
size_t bytes_to_transfer = samples * 2 * 2;
Expand All @@ -224,6 +224,7 @@ static void play(void *buf, int samples) {
pa_stream_cork(stream, 0, stream_success_cb, mainloop);
pa_threaded_mainloop_unlock(mainloop);
}
return 0;
}

int pa_delay(long *the_delay) {
Expand Down
3 changes: 2 additions & 1 deletion audio_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void start(__attribute__((unused)) int sample_rate,
}
}

static void play(void *buf, int samples) {
static int play(void *buf, int samples) {
// if the file is not open, try to open it.
char errorstring[1024];
if (fd == -1) {
Expand All @@ -73,6 +73,7 @@ static void play(void *buf, int samples) {
warn("Error %d opening the pipe named \"%s\": \"%s\".", errno, pipename, errorstring);
warned = 1;
}
return warned;
}

static void stop(void) {
Expand Down
5 changes: 3 additions & 2 deletions audio_sndio.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static int init(int, char **);
static void onmove_cb(void *, int);
static void deinit(void);
static void start(int, int);
static void play(void *, int);
static int play(void *, int);
static void stop(void);
static void onmove_cb(void *, int);
static int delay(long *);
Expand Down Expand Up @@ -222,12 +222,13 @@ static void start(__attribute__((unused)) int sample_rate,
pthread_mutex_unlock(&sndio_mutex);
}

static void play(void *buf, int frames) {
static int play(void *buf, int frames) {
if (frames > 0) {
pthread_mutex_lock(&sndio_mutex);
written += sio_write(hdl, buf, frames * framesize);
pthread_mutex_unlock(&sndio_mutex);
}
return 0;
}

static void stop() {
Expand Down
3 changes: 2 additions & 1 deletion audio_soundio.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static void start(int sample_rate, int sample_format) {
debug(1, "libsoundio output started\n");
}

static void play(void *buf, int samples) {
static int play(void *buf, int samples) {
// int err;
int free_bytes = soundio_ring_buffer_free_count(ring_buffer);
int written_bytes = 0;
Expand All @@ -187,6 +187,7 @@ static void play(void *buf, int samples) {
soundio_ring_buffer_advance_write_ptr(ring_buffer, write_bytes);
debug(3, "[<<---] Written to buffer : %d\n", written_bytes);
}
return 0;
}

static void parameters(audio_parameters *info) {
Expand Down
3 changes: 2 additions & 1 deletion audio_stdout.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void start(__attribute__((unused)) int sample_rate,
fd = STDOUT_FILENO;
}

static void play(void *buf, int samples) {
static int play(void *buf, int samples) {
char errorstring[1024];
int warned = 0;
int rc = write(fd, buf, samples * 4);
Expand All @@ -52,6 +52,7 @@ static void play(void *buf, int samples) {
warn("Error %d writing to stdout: \"%s\".", errno, errorstring);
warned = 1;
}
return rc;
}

static void stop(void) {
Expand Down
4 changes: 4 additions & 0 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,10 @@ int _debug_mutex_unlock(pthread_mutex_t *mutex, const char *filename, const int
return r;
}

void pthread_cleanup_debug_mutex_unlock(void *arg) {
pthread_mutex_unlock((pthread_mutex_t* )arg);
}

char *get_version_string() {
char *version_string = malloc(200);
if (version_string) {
Expand Down
5 changes: 5 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ int _debug_mutex_unlock(pthread_mutex_t *mutex, const char *filename, const int

#define debug_mutex_unlock(mu, d) _debug_mutex_unlock(mu, __FILE__, __LINE__, d)


void pthread_cleanup_debug_mutex_unlock(void *arg);

#define pthread_cleanup_debug_mutex_lock(mu, t, d) if (_debug_mutex_lock(mu, t, __FILE__, __LINE__, d) == 0) pthread_cleanup_push(pthread_cleanup_debug_mutex_unlock,(void *)mu)

char *get_version_string(); // mallocs a string space -- remember to free it afterwards

void sps_nanosleep(const time_t sec,
Expand Down

0 comments on commit ea20840

Please sign in to comment.