sad

simple audio daemon
git clone git://git.2f30.org/sad
Log | Files | Refs | LICENSE

commit a1e13e5a5040c97550dd37c0a43f06377b5bac32
parent 2211286fe6c0f9de3330abbfdb173bc02cdbab55
Author: sin <sin@2f30.org>
Date:   Wed, 24 Dec 2014 15:02:01 +0000

Add some error checking

Diffstat:
Mao.c | 13+++++++++++--
Mmp3.c | 22+++++++++++++---------
Msad.c | 26++++++++++++++++++--------
3 files changed, 42 insertions(+), 19 deletions(-)

diff --git a/ao.c b/ao.c @@ -1,5 +1,6 @@ #include <sys/select.h> +#include <err.h> #include <limits.h> #include <stdio.h> @@ -16,6 +17,7 @@ aoinit(void) { ao_initialize(); driver = ao_default_driver_id(); + return driver < 0 ? -1 : 0; } static int @@ -26,24 +28,31 @@ aoputfmt(long rate, int channels, int bits) format.bits = bits; format.byte_format = AO_FMT_NATIVE; format.matrix = 0; + return 0; } static int aoopen(void) { dev = ao_open_live(driver, &format, NULL); + return !dev ? -1 : 0; } static int aowrite(void *buf, size_t size) { - ao_play(dev, buf, size); + int r; + + r = ao_play(dev, buf, size); + if (!r) + return -1; + return r; } static int aoclose(void) { - ao_close(dev); + return !ao_close(dev) ? -1 : 0; } static void diff --git a/mp3.c b/mp3.c @@ -12,16 +12,17 @@ static mpg123_handle *handle; static int mp3init(void) { - int error; - mpg123_init(); - handle = mpg123_new(NULL, &error); + handle = mpg123_new(NULL, NULL); + if (!handle) + return -1; + return 0; } static int mp3open(const char *path) { - mpg123_open(handle, path); + mpg123_open(handle, path) != MPG123_OK ? -1 : 0; } static size_t @@ -35,8 +36,11 @@ mp3getfmt(long *rate, int *channels, int *bits) { long r; int c, e; + int ret; - mpg123_getformat(handle, &r, &c, &e); + ret = mpg123_getformat(handle, &r, &c, &e); + if (ret != MPG123_OK) + return -1; *rate = r; *channels = c; *bits = mpg123_encsize(e) * 8; @@ -50,15 +54,15 @@ mp3read(void *buf, size_t size) int r; r = mpg123_read(handle, buf, size, &done); - if (r == MPG123_OK) - return done; - return 0; + if (r != MPG123_OK) + return -1; + return done; } static int mp3close(void) { - mpg123_close(handle); + return mpg123_close(handle) != MPG123_OK ? -1 : 0; } static void diff --git a/sad.c b/sad.c @@ -78,10 +78,14 @@ doaudio(void) switch (s->state) { case READYTOPLAY: - curdecoder->open(s->path); - curdecoder->getfmt(&rate, &channels, &bits); - curoutput->putfmt(rate, channels, bits); - curoutput->open(); + if (curdecoder->open(s->path) < 0) + errx(1, "decoder: failed to open %s", s->path); + if (curdecoder->getfmt(&rate, &channels, &bits) < 0) + errx(1, "decoder: failed to get format"); + if (curoutput->putfmt(rate, channels, bits) < 0) + errx(1, "output: failed to put format"); + if (curoutput->open() < 0) + errx(1, "output: failed to open output device"); s->state = PLAYING; break; case PLAYING: @@ -90,8 +94,12 @@ doaudio(void) if (!buf) err(1, "malloc"); n = curdecoder->read(buf, bufsz); - if (n != 0) - curoutput->write(buf, n); + if (n < 0) + warnx("decoder: failed to decode buffer at %p of length %zu", + buf, bufsz); + else if (n > 0) + if (curoutput->write(buf, n) < 0) + warnx("output: failed to write buffer to output device"); free(buf); } } @@ -108,8 +116,10 @@ main(void) FD_SET(listenfd, &master); fdmax = listenfd; - curoutput->init(); - curdecoder->init(); + if (curoutput->init() < 0) + errx(1, "output: init failed"); + if (curdecoder->init() < 0) + errx(1, "decoder: init failed"); while (1) { rfds = master;