introduce input_eof and ouput_eof
[paraslash.git] / alsa_writer.c
index 601353aaac84b7a8969d4bf9b5dc01d68242de19..f05dbbd0e3fdf3002de52e96c5ec63e95e1f41eb 100644 (file)
@@ -27,6 +27,8 @@
 #include "para.h"
 #include "fd.h"
 #include "string.h"
+#include "list.h"
+#include "sched.h"
 #include "write.h"
 
 #include <alsa/asoundlib.h>
@@ -44,6 +46,7 @@ struct private_alsa_data {
 snd_pcm_t *handle;
 /** determined and set by alsa_open() */
 size_t bytes_per_frame;
+struct timeval next_chunk;
 };
 
 /*
@@ -60,14 +63,11 @@ static int alsa_open(struct writer_node *w)
        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));
+       struct private_alsa_data *pad = para_calloc(sizeof(struct private_alsa_data));
        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,
                SND_PCM_STREAM_PLAYBACK, 0);
        if (err < 0)
@@ -128,41 +128,75 @@ static int alsa_open(struct writer_node *w)
                return -E_SW_PARAMS;
        pad->bytes_per_frame = snd_pcm_format_physical_width(FORMAT)
                * 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 = (unsigned char*)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 = {0, 1000 * 10};
+                       PARA_INFO_LOG("EAGAIN. frames: %d, ret: %lu\n", frames, ret);
+                       tv_add(&s->now, &tv, &pad->next_chunk);
+//                     snd_pcm_wait(pad->handle, 1);
+               } else if (ret == -EPIPE) {
+                       PARA_INFO_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) {
+                       PARA_INFO_LOG("ALSA ERR %d\n", frames);
+                       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)
@@ -178,7 +212,8 @@ static void alsa_close(struct writer_node *wn)
 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->shutdown = NULL; /* nothing to do */
 }