aacdec: detect buffer overrun and return an errror.
[paraslash.git] / alsa_writer.c
index 6a3d18a..7c6e8ca 100644 (file)
@@ -16,7 +16,7 @@
  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  */
 
-/** \file alsa_writer.c Data specific to the alsa writer */
+/** \file alsa_writer.c paraslash's alsa output plugin */
 
 /*
  * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
 #include "para.h"
 #include "fd.h"
 #include "string.h"
+#include "list.h"
+#include "sched.h"
 #include "write.h"
 
 #include <alsa/asoundlib.h>
 
-#include "write.cmdline.h"
+#include "alsa_write.cmdline.h"
 #include "error.h"
 
-extern struct gengetopt_args_info conf;
 
 #define FORMAT SND_PCM_FORMAT_S16_LE
+
+/** data specific to the alsa writer */
 struct private_alsa_data {
+       /** the alsa handle */
        snd_pcm_t *handle;
+       /** determined and set by alsa_open() */
        size_t bytes_per_frame;
+       /** don't write anything until this time */
+       struct timeval next_chunk;
+       /** the return value of snd_pcm_hw_params_get_buffer_time_max() */
+       unsigned buffer_time;
 };
 
 /*
@@ -53,18 +62,15 @@ static int alsa_open(struct writer_node *w)
        snd_pcm_sw_params_t *swparams;
        snd_pcm_uframes_t buffer_size, xfer_align, start_threshold,
                stop_threshold;
-       unsigned buffer_time = 0;
        int err;
        snd_pcm_info_t *info;
-       snd_output_t *log;
        snd_pcm_uframes_t period_size;
-       struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
-       w->private_data = pad;
+       struct private_alsa_data *pad = para_calloc(sizeof(struct private_alsa_data));
+       struct alsa_write_args_info *conf = w->conf;
 
+       w->private_data = pad;
        snd_pcm_info_alloca(&info);
-       if (snd_output_stdio_attach(&log, stderr, 0) < 0)
-               return -E_ALSA_LOG;
-       err = snd_pcm_open(&pad->handle, conf.device_arg,
+       err = snd_pcm_open(&pad->handle, conf->device_arg,
                SND_PCM_STREAM_PLAYBACK, 0);
        if (err < 0)
                return -E_PCM_OPEN;
@@ -81,23 +87,23 @@ static int alsa_open(struct writer_node *w)
        if (snd_pcm_hw_params_set_format(pad->handle, hwparams, FORMAT) < 0)
                return -E_SAMPLE_FORMAT;
        if (snd_pcm_hw_params_set_channels(pad->handle, hwparams,
-                       conf.channels_arg) < 0)
+                       conf->channels_arg) < 0)
                return -E_CHANNEL_COUNT;
        if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
-                       (unsigned int*) &conf.sample_rate_arg, 0) < 0)
+                       (unsigned int*) &conf->sample_rate_arg, 0) < 0)
                return -E_SET_RATE;
-       err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
-       if (err < 0 || !buffer_time)
+       err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &pad->buffer_time, 0);
+       if (err < 0 || !pad->buffer_time)
                return -E_GET_BUFFER_TIME;
-       PARA_DEBUG_LOG("buffer time: %d\n", buffer_time);
+       PARA_INFO_LOG("buffer time: %d\n", pad->buffer_time);
        if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
-                       &buffer_time, 0) < 0)
+                       &pad->buffer_time, 0) < 0)
                return -E_SET_BUFFER_TIME;
        if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
                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,
+       PARA_INFO_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
                period_size);
        if (period_size == buffer_size)
                return -E_BAD_PERIOD;
@@ -123,42 +129,75 @@ static int alsa_open(struct writer_node *w)
        if (snd_pcm_sw_params(pad->handle, swparams) < 0)
                return -E_SW_PARAMS;
        pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
-               * conf.channels_arg / 8;
+               * conf->channels_arg / 8;
+       if (snd_pcm_nonblock(pad->handle, 1))
+               PARA_ERROR_LOG("%s\n", "failed to set nonblock mode");
        return period_size * pad->bytes_per_frame;
 }
+static void alsa_write_pre_select(struct sched *s, struct task *t)
+{
+       struct writer_node *wn = t->private_data;
+       struct private_alsa_data *pad = wn->private_data;
+       struct writer_node_group *wng = wn->wng;
+       struct timeval diff;
 
-/**
- * push out pcm frames
- * \param data pointer do data to be written
- * \param nbytes number of bytes (not frames)
- *
- * \return Number of bytes written, -E_ALSA_WRITE on errors.
- */
-static int alsa_write(char *data, size_t nbytes, struct writer_node *wn)
+       t->ret = 0;
+       if (*wng->input_eof && *wng->loaded < pad->bytes_per_frame)
+               return;
+       t->ret = 1;
+       if (*wng->loaded < pad->bytes_per_frame)
+               return;
+       if (tv_diff(&s->now, &pad->next_chunk, &diff) < 0) {
+               if (tv_diff(&s->timeout, &diff, NULL) > 0)
+                       s->timeout = diff;
+       } else {
+               s->timeout.tv_sec = 0;
+               s->timeout.tv_usec = 0;
+       }
+}
+
+static void alsa_write_post_select(struct sched *s, struct task *t)
 {
+       struct writer_node *wn = t->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;
-
-       while (frames > 0) {
-               /* write interleaved frames */
-               r = snd_pcm_writei(pad->handle, d, frames);
-               if (r < 0)
-                       PARA_ERROR_LOG("write error: %s\n", snd_strerror(r));
-               if (r == -EAGAIN || (r >= 0 && r < frames))
-                       snd_pcm_wait(pad->handle, 1);
-               else if (r == -EPIPE)
+       struct writer_node_group *wng = wn->wng;
+       size_t frames = *wng->loaded / pad->bytes_per_frame;
+       snd_pcm_sframes_t ret, result = 0;
+       unsigned char *data = (unsigned char*)wng->buf;
+
+       t->ret = 0;
+       if (!frames) {
+               if (*wng->input_eof)
+                       t->ret = *wng->loaded;
+               return;
+       }
+       if (tv_diff(&s->now, &pad->next_chunk, NULL) < 0)
+               return;
+//     PARA_INFO_LOG("%zd frames\n", frames);
+//     while (frames > 0) {
+               ret = snd_pcm_writei(pad->handle, data, frames);
+               if (ret == -EAGAIN || (ret >= 0 && ret < frames)) {
+                       struct timeval tv;
+                       ms2tv(pad->buffer_time / 2000, &tv);
+                       PARA_DEBUG_LOG("EAGAIN. frames: %d, ret: %lu\n", frames, ret);
+                       tv_add(&s->now, &tv, &pad->next_chunk);
+               } else if (ret == -EPIPE) {
+                       PARA_WARNING_LOG("%s", "EPIPE\n");
                        snd_pcm_prepare(pad->handle);
-               else if (r < 0)
-                       return -E_ALSA_WRITE;
-               if (r > 0) {
-                       result += r;
-                       frames -= r;
-                       d += r * pad->bytes_per_frame;
+               } else if (ret < 0) {
+                       t->ret = -E_ALSA_WRITE;
+                       return;
                }
-       }
-       return result * pad->bytes_per_frame;
+               if (ret >= 0) {
+                       result += ret;
+                       frames -= ret;
+                       data += ret * pad->bytes_per_frame;
+               }
+//             if (ret == -EAGAIN)
+//                     break;
+//     }
+       t->ret = result * pad->bytes_per_frame;
+//     PARA_INFO_LOG("ret: %d, frames: %zd\n", t->ret, frames);
 }
 
 static void alsa_close(struct writer_node *wn)
@@ -170,10 +209,28 @@ static void alsa_close(struct writer_node *wn)
        free(pad);
 }
 
+__malloc void *alsa_parse_config(char *options)
+{
+       struct alsa_write_args_info *conf
+               = para_calloc(sizeof(struct alsa_write_args_info));
+       PARA_INFO_LOG("options: %s, %d\n", options, strcspn(options, " \t"));
+       int ret = alsa_cmdline_parser_string(options, conf, "alsa_write");
+       if (ret)
+               goto err_out;
+       PARA_INFO_LOG("help given: %d\n", conf->help_given);
+       return conf;
+err_out:
+       free(conf);
+       return NULL;
+}
+
+/** the init function of the alsa writer */
 void alsa_writer_init(struct writer *w)
 {
        w->open = alsa_open;
-       w->write = alsa_write;
        w->close = alsa_close;
+       w->pre_select = alsa_write_pre_select;
+       w->post_select = alsa_write_post_select;
+       w->parse_config = alsa_parse_config;
        w->shutdown = NULL; /* nothing to do */
 }