From addc7585b294e61e152f1148d02cbcc866b8e7fa Mon Sep 17 00:00:00 2001 From: Brian Fitzpatrick Date: Sun, 16 Nov 2014 21:04:12 -0600 Subject: [PATCH] Reverted to the version of audio.pipe.c prior to the last major change and used a different method to open the pipe for writing. --- audio_pipe.c | 90 +++++++++++++++------------------------------------- 1 file changed, 25 insertions(+), 65 deletions(-) diff --git a/audio_pipe.c b/audio_pipe.c index 759e73d01..4332ff2bb 100644 --- a/audio_pipe.c +++ b/audio_pipe.c @@ -28,98 +28,58 @@ #include #include #include -#include -#include -#include -#include #include "common.h" #include "audio.h" static int fd = -1; static char *pipename = NULL; static int Fs; -static long long starttime, samples_played; - -static void stop(void) { - close(fd); - fd = -1; -} static void start(int sample_rate) { - if (fd >= 0) - stop(); - - fd = open(pipename, O_WRONLY | O_NONBLOCK); - if ((fd < 0) && (errno != ENXIO)) { + int readfd; + + Fs = sample_rate; + + readfd = open(pipename, O_RDONLY | O_NONBLOCK); + if (readfd < 0) { + perror("open"); + die("could not open specified pipe for reading"); + } + fd = open(pipename, O_WRONLY); + if (fd < 0) { perror("open"); die("could not open specified pipe for writing"); } - - // The other end is ready, reopen with blocking - if (fd >= 0) { - close(fd); - fd = open(pipename, O_WRONLY); - } - - Fs = sample_rate; - starttime = 0; - samples_played = 0; -} - -// Wait procedure taken from audio_dummy.c -static void wait_samples(int samples) { - struct timeval tv; - - // this is all a bit expensive but it's long-term stable. - gettimeofday(&tv, NULL); - - long long nowtime = tv.tv_usec + 1e6*tv.tv_sec; - - if (!starttime) - starttime = nowtime; - - samples_played += samples; - - long long finishtime = starttime + samples_played * 1e6 / Fs; - - usleep(finishtime - nowtime); + close(readfd); } static void play(short buf[], int samples) { - if (fd < 0) { - wait_samples(samples); - - // check if the other end is ready every 5 seconds - if (samples_played > 5 * Fs) - start(Fs); - - return; - } + int rc; + + rc = write(fd, buf, samples*4); + if (rc <=0) usleep(1e6 * samples / Fs); +} - if (write(fd, buf, samples*4) < 0) - stop(); +static void stop(void) { + close(fd); } static int init(int argc, char **argv) { - struct stat sb; - if (argc != 1) die("bad argument(s) to pipe"); pipename = strdup(argv[0]); - if (stat(pipename, &sb) < 0) - die("could not stat() pipe"); - - if (!S_ISFIFO(sb.st_mode)) - die("not a pipe"); - + // test open pipe so we error on startup if it's going to fail + start(44100); + stop(); + return 0; } static void deinit(void) { - if (fd >= 0) - stop(); + if (fd > 0) + close(fd); if (pipename) free(pipename); }