Commit Diff


commit - acaf7eb251046be1f73077acf6519aa7496fa0a4
commit + 2139c525dbcd33e08c153dd8dbe75cd1fddf6b30
blob - 0fd2a5a1bab5c40838c3c37d0b1c2d17b95121b7
blob + 342b33930c82d67e0fa6c77211de1e43040f6244
--- amused.h
+++ amused.h
@@ -140,6 +140,7 @@ int	player_setup(int, int, int);
 void	player_senderr(void);
 void	player_sendeof(void);
 int	player_shouldstop(void);
+int	play(const void *, size_t);
 int	player(int, int);
 
 void	play_oggvorbis(int fd);
blob - 1332b0b1e42f2c3346f273bd0ae8df9849edf71e
blob + f2ca8cbd7e7d31f285a9aa277fa5ed395d38afad
--- player.c
+++ player.c
@@ -251,6 +251,15 @@ player_shouldstop(void)
 }
 
 int
+play(const void *buf, size_t len)
+{
+	if (player_shouldstop())
+		return 0;
+	sio_write(hdl, buf, len);
+	return 1;
+}
+
+int
 player(int debug, int verbose)
 {
 	int flags;
blob - 29d62dd0c22f2b5e00e67bfb032cd7d671bc64aa
blob + 87f426289cacd076eec3597a6547af82a72ac418
--- player_123.c
+++ player_123.c
@@ -70,9 +70,6 @@ play_mp3(int fd)
 		goto done;
 
 	for (;;) {
-		if (player_shouldstop())
-			break;
-
 		err = mpg123_read(mh, buf, sizeof(buf), &len);
 		switch (err) {
 		case MPG123_DONE:
@@ -82,7 +79,8 @@ play_mp3(int fd)
 				goto done;
 			break;
 		case MPG123_OK:
-			sio_write(hdl, buf, len);
+			if (!play(buf, len))
+				goto done;
 			break;
 		default:
 			log_warnx("error %d decoding mp3", err);
blob - 94d91e32a4f6dd6d63588d491b2f7d7bafa94fe2
blob + b097d0dcf104ebd3c6e2289e376ccae20e27655b
--- player_flac.c
+++ player_flac.c
@@ -41,13 +41,10 @@ writecb(const FLAC__StreamDecoder *decoder, const FLAC
 	int i;
 	size_t len;
 
-	if (player_shouldstop())
-		return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
-
 	for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
 		if (len+4 >= sizeof(buf)) {
-			sio_write(hdl, buf, len);
-			len = 0;
+			if (!play(buf, len))
+				goto quit;
 		}
 
 		buf[len++] = buffer[0][i] & 0xff;
@@ -57,10 +54,12 @@ writecb(const FLAC__StreamDecoder *decoder, const FLAC
 		buf[len++] = (buffer[1][i] >> 8) & 0xff;
 	}
 
-	if (len != 0)
-		sio_write(hdl, buf, len);
+	if (len != 0 && !play(buf, len))
+		goto quit;
 
 	return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
+quit:
+	return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
 }
 
 static void
blob - d6b64076b8d69170c9cf6c22d7cbb9ab80e72a7e
blob + b8511dc1eb177898f04336a7b86931cb82a8fc3c
--- player_oggvorbis.c
+++ player_oggvorbis.c
@@ -69,16 +69,14 @@ play_oggvorbis(int fd)
 	while (!eof) {
 		long ret;
 
-		if (player_shouldstop())
-			break;
-
 		ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
 		    &current_section);
 		if (ret == 0)
 			eof = 1;
 		else if (ret > 0) {
 			/* TODO: deal with sample rate changes */
-			sio_write(hdl, pcmout, ret);
+			if (!play(pcmout, ret))
+				break;
 		}
 	}
 
blob - 4a2f92ba4db4355e132cb2d1e3059028b9bb5611
blob + ed931ec0184aa63ca3fc91a8cc7dfae5af86c7ae
--- player_opus.c
+++ player_opus.c
@@ -60,9 +60,6 @@ play_opus(int fd)
 	}
 
 	for (;;) {
-		if (player_shouldstop())
-			break;
-
 		/* NB: will downmix multichannels files into two channels */
 		ret = op_read_stereo(of, pcm, nitems(pcm));
 		if (ret == OP_HOLE) /* corrupt file segment? */
@@ -90,7 +87,9 @@ play_opus(int fd)
 			out[2*i+0] = pcm[i] & 0xFF;
 			out[2*i+1] = (pcm[i] >> 8) & 0xFF;
 		}
-		sio_write(hdl, out, 4*ret);
+
+		if (!play(out, 4*ret))
+			break;
 	}
 
 	op_free(of);