]> git.tue.mpg.de Git - paraslash.git/commitdiff
switch to the new struct writer/writer_node abstraction
authorAndre <maan@p133.(none)>
Tue, 18 Apr 2006 01:00:53 +0000 (03:00 +0200)
committerAndre <maan@p133.(none)>
Tue, 18 Apr 2006 01:00:53 +0000 (03:00 +0200)
There's only one writer possible ATM, but it is designed to allow
more than one.

This patch also, adds a loglevel config option to para_play.

play.c
play.ggo

diff --git a/play.c b/play.c
index c9cc56abb5374831979cb510bcc89ed337425257..5d32df7cad66d05726c1cff1256171bf7e6b32c3 100644 (file)
--- a/play.c
+++ b/play.c
@@ -41,10 +41,11 @@ struct private_alsa_data {
 struct writer_node {
        struct writer *writer;
        void *private_data;
+       int chunk_bytes;
 };
 
 struct writer {
-       int (*open)(struct writer_node **);
+       int (*open)(struct writer_node *);
        int (*write)(char *data, size_t nbytes, struct writer_node *);
        void (*close)(struct writer_node *);
        void (*shutdown)(struct writer_node *);
@@ -53,7 +54,7 @@ struct writer {
 #define NUM_WRITERS 1
 static struct writer writers[NUM_WRITERS];
 #define FOR_EACH_WRITER(i) for (i = 0; i < NUM_WRITERS, i++)
-static struct writer_node **writer_nodes;
+static struct writer_node *writer_nodes;
 
 
 static unsigned char *audiobuf;
@@ -62,10 +63,12 @@ static struct gengetopt_args_info conf;
 
 INIT_PLAY_ERRLISTS;
 
-void para_log(__a_unused int ll, const char* fmt,...)
+void para_log(int ll, const char* fmt,...)
 {
        va_list argp;
 
+       if (ll < conf.loglevel_arg)
+               return;
        va_start(argp, fmt);
        vfprintf(stderr, fmt, argp);
        va_end(argp);
@@ -95,7 +98,7 @@ static int read_wav_header(void)
  *
  * Install PCM software and hardware configuration. Exit on errors.
  */
-static int alsa_open(void **private_data)
+static int alsa_open(struct writer_node *w)
 {
        snd_pcm_hw_params_t *hwparams;
        snd_pcm_sw_params_t *swparams;
@@ -107,7 +110,7 @@ static int alsa_open(void **private_data)
        snd_output_t *log;
        snd_pcm_uframes_t period_size;
        struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
-       *private_data = pad;
+       w->private_data = pad;
 
        snd_pcm_info_alloca(&info);
        if (snd_output_stdio_attach(&log, stderr, 0) < 0)
@@ -145,7 +148,8 @@ static int alsa_open(void **private_data)
                return -E_HW_PARAMS;
        snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
        snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
-       PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size, period_size);
+       PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
+               period_size);
        if (period_size == buffer_size)
                return -E_BAD_PERIOD;
        snd_pcm_sw_params_current(pad->handle, swparams);
@@ -181,9 +185,9 @@ static int alsa_open(void **private_data)
  *
  * \return Number of bytes written, -E_ALSA_WRITE on errors.
  */
-static int alsa_write(char *data, size_t nbytes, void *private_data)
+static int alsa_write(char *data, size_t nbytes, struct writer_node *wn)
 {
-       struct private_alsa_data *pad = private_data;
+       struct private_alsa_data *pad = wn->private_data;
        size_t frames = nbytes / pad->bytes_per_frame;
        unsigned char *d = data;
        snd_pcm_sframes_t r, result = 0;
@@ -208,9 +212,9 @@ static int alsa_write(char *data, size_t nbytes, void *private_data)
        return result * pad->bytes_per_frame;
 }
 
-static void alsa_close(void *private_data)
+static void alsa_close(struct writer_node *wn)
 {
-       struct private_alsa_data *pad = private_data;
+       struct private_alsa_data *pad = wn->private_data;
        snd_pcm_drain(pad->handle);
        snd_pcm_close(pad->handle);
        snd_config_update_free_global();
@@ -261,6 +265,22 @@ static void do_initial_delay(struct timeval *delay)
        while (start_time_in_future(delay));
 }
 
+static int read_stdin(char *buf, size_t bytes_to_load, size_t *loaded)
+{
+       ssize_t ret;
+
+       while (*loaded < bytes_to_load) {
+               ret = read(STDIN_FILENO, buf + *loaded, bytes_to_load - *loaded);
+               if (ret <= 0) {
+                       if (ret < 0)
+                               ret = -E_READ_STDIN;
+                       return ret;
+               }
+               *loaded += ret;
+       }
+       return 1;
+}
+
 /**
  * play raw pcm data
  * \param loaded number of bytes already loaded
@@ -273,51 +293,92 @@ static void do_initial_delay(struct timeval *delay)
  */
 static int play_pcm(size_t loaded)
 {
-       size_t bufsize, written = 0, prebuf_size;
-       unsigned char *p;
+       size_t bufsize, min_written = 0, prebuf_size, bytes_to_load;
        struct timeval delay;
-       void *private_data;
-       int chunk_bytes, ret = alsa_open(&private_data);
+       int i, max_chunk_bytes = 0, ret, not_yet_started = 1, need_more_writes;
+       struct writer_node *wn;
+       size_t *written = para_calloc(1 * sizeof(size_t));
 
-       if (ret < 0)
-               goto out;
-       chunk_bytes = ret;
-       bufsize = (conf.bufsize_arg * 1024 / chunk_bytes) * chunk_bytes;
+       for (i = 0; i < 1; i++) {
+               wn = &writer_nodes[i];
+               ret = wn->writer->open(wn);
+               if (ret < 0)
+                       goto out;
+               wn->chunk_bytes = ret;
+               max_chunk_bytes = PARA_MAX(max_chunk_bytes, ret);
+       }
+       PARA_INFO_LOG("max chunk_bytes: %d\n", max_chunk_bytes);
+       bufsize = (conf.bufsize_arg * 1024 / max_chunk_bytes) * max_chunk_bytes;
        audiobuf = para_realloc(audiobuf, bufsize);
        prebuf_size = conf.prebuffer_arg * bufsize / 100;
+       bytes_to_load =  PARA_MAX(prebuf_size, max_chunk_bytes);
+       ret = read_stdin(audiobuf, bytes_to_load, &loaded);
+       if (ret <= 0 || loaded < bytes_to_load) {
+               if (ret >= 0)
+                       ret = -E_PREMATURE_END;
+               goto out;
+       }
+       if (not_yet_started && start_time && start_time_in_future(&delay))
+               do_initial_delay(&delay);
+       not_yet_started = 0;
 again:
-       if (!written) {
-               if (loaded < prebuf_size)
-                       goto read;
-               if (start_time && start_time_in_future(&delay)) {
-                       do_initial_delay(&delay);
-                       start_time = NULL;
+       need_more_writes = 1;
+       while (need_more_writes) {
+               need_more_writes = 0;
+               for (i = 0; i < 1; i++) {
+                       unsigned char *p = audiobuf + written[i];
+                       wn = &writer_nodes[i];
+                       if (!i)
+                               min_written = written[i];
+                       else
+                               min_written = PARA_MIN(min_written, written[i]);
+                       if (loaded < wn->chunk_bytes + written[i])
+                               continue;
+                       ret = wn->writer->write(p, wn->chunk_bytes, wn);
+                       if (ret < 0)
+                               goto out;
+                       if (ret != wn->chunk_bytes)
+                               PARA_WARNING_LOG("short write: %d/%d", ret,
+                                       wn->chunk_bytes);
+                       written[i] += ret;
+                       need_more_writes = 1;
                }
        }
-       p = audiobuf;
-       while (loaded >= chunk_bytes) {
-               ret = alsa_write(p, chunk_bytes, private_data);
-               if (ret < 0)
+       loaded -= min_written;
+       if (loaded > 0) {
+               if (loaded >= bufsize) {
+                       ret = -E_PLAY_OVERRUN;
                        goto out;
-               p += ret;
-               written += ret;
-               loaded -= ret;
+               }
+               memmove(audiobuf, audiobuf + min_written, loaded);
        }
-       if (loaded && p != audiobuf)
-               memmove(audiobuf, p, loaded);
-read:
-       ret = read(STDIN_FILENO, audiobuf + loaded, bufsize - loaded);
-       if (ret < 0) {
-               ret = -E_READ_STDIN;
+       for (i = 0; i < 1; i++)
+               written[i] -= min_written;
+       bytes_to_load = PARA_MIN(max_chunk_bytes, bufsize);
+       ret = read_stdin(audiobuf, bytes_to_load, &loaded);
+       if (ret < 0)
                goto out;
-       }
-       if (ret) {
-               loaded += ret;
+       if (ret)
                goto again;
+       for (i = 0; i < 1; i++) {
+               unsigned char *p = audiobuf + written[i];
+               wn = &writer_nodes[i];
+               int bytes_to_write;
+               if (written[i] >= loaded)
+                       continue;
+               bytes_to_write = PARA_MIN(wn->chunk_bytes, loaded - written[i]);
+               ret = wn->writer->write(p, bytes_to_write, wn);
+               if (ret < 0)
+                       goto out;
+               written[i] += ret;
        }
-       ret = 1;
+       ret = 0;
 out:
-       alsa_close(private_data);
+       free(written);
+       for (i = 0; i < 1; i++) {
+               wn = &writer_nodes[i];
+               wn->writer->close(wn);
+       }
        return ret;
 }
 
@@ -340,11 +401,12 @@ static size_t check_wave(void)
 int main(int argc, char *argv[])
 {
        struct timeval tv;
-       int ret;
+       int ret = -E_PLAY_SYNTAX;
 
        cmdline_parser(argc, argv, &conf);
+       if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
+               goto out;
        if (conf.start_time_given) {
-               ret = -E_PLAY_SYNTAX;
                if (sscanf(conf.start_time_arg, "%lu:%lu",
                                &tv.tv_sec, &tv.tv_usec) != 2)
                        goto out;
@@ -354,6 +416,7 @@ int main(int argc, char *argv[])
        alsa_writer_init(&writers[0]);
        /* one for each given writer */
        writer_nodes = para_calloc(2 * sizeof(struct writer_node));
+       writer_nodes[0].writer = &writers[0]; /* alsa */
 
        audiobuf = para_malloc(WAV_HEADER_LEN);
        ret = read_wav_header();
@@ -362,6 +425,7 @@ int main(int argc, char *argv[])
        ret = play_pcm(check_wave());
 out:
        free(audiobuf);
+       free(writer_nodes);
        if (ret < 0)
                PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
        return ret;
index e4842499b716818f434b7e4cb616b230c59916aa..23cd64ee4499fbc1b8b5fcc353889f3d1d56f516 100644 (file)
--- a/play.ggo
+++ b/play.ggo
@@ -5,3 +5,12 @@ option "channels" c "number of channels (only neccessary for raw audio)" int typ
 option "sample_rate" s "force given sample rate (only neccessary for raw audio)" int typestr="num" default="44100" optional
 option "bufsize" b "input buffer size" int typestr="kilobytes" default="64" optional
 option "prebuffer" p "delay playback until buffer is filled" int typestr="percent" default="100" optional
+option "loglevel" l
+#~~~~~~~~~~~~~~~~~~
+
+"set loglevel (0-6)"
+
+       int typestr="level"
+       default="4"
+       optional
+