Commit Diff
Commit:
0da0ad464c14fc59e00a2d2d904ebc97bedb212e
Date:
Wed Mar 9 11:24:36 2022
UTC
Message:
change play_*() so that they return an integer
this will allow to remove the `got_stop' hack in player.c
--- amused.h
+++ amused.h
@@ -139,9 +139,9 @@ void play_oggvorbis(int fd);
int play(const void *, size_t);
int player(int, int);
-void play_oggvorbis(int fd);
-void play_mp3(int fd);
-void play_flac(int fd);
-void play_opus(int fd);
+int play_oggvorbis(int fd);
+int play_mp3(int fd);
+int play_flac(int fd);
+int play_opus(int fd);
#endif
--- player_123.c
+++ player_123.c
@@ -48,13 +48,13 @@ void
return 1;
}
-void
+int
play_mp3(int fd)
{
static char buf[4096];
size_t len;
mpg123_handle *mh;
- int err;
+ int err, ret = -1;
if ((mh = mpg123_new(NULL, NULL)) == NULL)
fatal("mpg123_new");
@@ -62,7 +62,7 @@ play_mp3(int fd)
if (mpg123_open_fd(mh, fd) != MPG123_OK) {
log_warnx("mpg123_open_fd failed");
close(fd);
- return;
+ return -1;
}
if (!setup(mh))
@@ -72,14 +72,17 @@ play_mp3(int fd)
err = mpg123_read(mh, buf, sizeof(buf), &len);
switch (err) {
case MPG123_DONE:
+ ret = 0;
goto done;
case MPG123_NEW_FORMAT:
if (!setup(mh))
goto done;
break;
case MPG123_OK:
- if (!play(buf, len))
+ if (!play(buf, len)) {
+ ret = 1;
goto done;
+ }
break;
default:
log_warnx("error %d decoding mp3", err);
@@ -90,4 +93,5 @@ done:
done:
mpg123_delete(mh);
close(fd);
+ return ret;
}
--- player_flac.c
+++ player_flac.c
@@ -84,7 +84,7 @@ void
log_warnx("flac error: %s", FLAC__StreamDecoderErrorStatusString[status]);
}
-void
+int
play_flac(int fd)
{
FILE *f;
@@ -110,12 +110,16 @@ play_flac(int fd)
ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
s = FLAC__stream_decoder_get_state(decoder);
+
+ FLAC__stream_decoder_delete(decoder);
+ fclose(f);
+
if (!ok && s != FLAC__STREAM_DECODER_ABORTED) {
state = FLAC__StreamDecoderStateString[s];
log_warnx("decoding failed; state: %s", state);
+ return 1;
}
-
- FLAC__stream_decoder_delete(decoder);
-
- fclose(f);
+ if (!ok)
+ return -1;
+ return 0;
}
--- player_oggvorbis.c
+++ player_oggvorbis.c
@@ -39,20 +39,21 @@ void
#define nitems(x) (sizeof(x)/sizeof(x[0]))
#endif
-void
+int
play_oggvorbis(int fd)
{
static uint8_t pcmout[4096];
FILE *f;
OggVorbis_File vf;
vorbis_info *vi;
- int current_section, eof = 0;
+ int current_section, eof = 0, ret = 0;
if ((f = fdopen(fd, "r")) == NULL)
err(1, "fdopen");
if (ov_open_callbacks(f, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
log_warnx("input is not an Ogg bitstream");
+ ret = -1;
goto end;
}
@@ -74,8 +75,10 @@ play_oggvorbis(int fd)
eof = 1;
else if (ret > 0) {
/* TODO: deal with sample rate changes */
- if (!play(pcmout, ret))
+ if (!play(pcmout, ret)) {
+ ret = 1;
break;
+ }
}
}
@@ -83,4 +86,5 @@ end:
end:
fclose(f);
+ return ret;
}
--- player_opus.c
+++ player_opus.c
@@ -38,36 +38,37 @@ void
#define nitems(x) (sizeof(x)/sizeof(x[0]))
#endif
-void
+int
play_opus(int fd)
{
static uint16_t pcm[BUFSIZ];
static uint8_t out[BUFSIZ * 2];
OggOpusFile *of;
void *f;
- int ret;
+ int r, ret = 0;
OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
int i, li, prev_li = -1;
if ((f = op_fdopen(&cb, fd, "r")) == NULL)
err(1, "fdopen");
- of = op_open_callbacks(f, &cb, NULL, 0, &ret);
+ of = op_open_callbacks(f, &cb, NULL, 0, &r);
if (of == NULL) {
close(fd);
- return;
+ return -1;
}
for (;;) {
/* NB: will downmix multichannels files into two channels */
- ret = op_read_stereo(of, pcm, nitems(pcm));
- if (ret == OP_HOLE) /* corrupt file segment? */
+ r = op_read_stereo(of, pcm, nitems(pcm));
+ if (r == OP_HOLE) /* corrupt file segment? */
continue;
- if (ret < 0) {
+ if (r < 0) {
log_warnx("error %d decoding file", ret);
+ ret = -1;
break;
}
- if (ret == 0)
+ if (r == 0)
break; /* eof */
li = op_current_link(of);
@@ -75,7 +76,6 @@ play_opus(int fd)
const OpusHead *head;
prev_li = li;
-
head = op_head(of, li);
if (head->input_sample_rate &&
player_setup(16, head->input_sample_rate, 2) == -1)
@@ -87,9 +87,12 @@ play_opus(int fd)
out[2*i+1] = (pcm[i] >> 8) & 0xFF;
}
- if (!play(out, 4*ret))
+ if (!play(out, 4*ret)) {
+ ret = 1;
break;
+ }
}
op_free(of);
+ return ret;
}
Omar Polo