osx_write: kill superfluous semaphore code
[paraslash.git] / alsa_writer.c
index 7c6e8ca..7d7d36b 100644 (file)
@@ -49,6 +49,8 @@ struct private_alsa_data {
        struct timeval next_chunk;
        /** the return value of snd_pcm_hw_params_get_buffer_time_max() */
        unsigned buffer_time;
        struct timeval next_chunk;
        /** the return value of snd_pcm_hw_params_get_buffer_time_max() */
        unsigned buffer_time;
+       unsigned samplerate;
+       unsigned channels;
 };
 
 /*
 };
 
 /*
@@ -67,11 +69,21 @@ static int alsa_open(struct writer_node *w)
        snd_pcm_uframes_t period_size;
        struct private_alsa_data *pad = para_calloc(sizeof(struct private_alsa_data));
        struct alsa_write_args_info *conf = w->conf;
        snd_pcm_uframes_t period_size;
        struct private_alsa_data *pad = para_calloc(sizeof(struct private_alsa_data));
        struct alsa_write_args_info *conf = w->conf;
-
+       struct writer_node_group *wng = w->wng;
+
+       if (!conf->samplerate_given && wng->samplerate)
+               pad->samplerate = *wng->samplerate;
+       else
+               pad->samplerate = conf->samplerate_arg;
+       if (!conf->channels_given && wng->channels)
+               pad->channels = *wng->channels;
+       else
+               pad->channels = conf->channels_arg;
+       PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels, pad->samplerate);
        w->private_data = pad;
        snd_pcm_info_alloca(&info);
        err = snd_pcm_open(&pad->handle, conf->device_arg,
        w->private_data = pad;
        snd_pcm_info_alloca(&info);
        err = snd_pcm_open(&pad->handle, conf->device_arg,
-               SND_PCM_STREAM_PLAYBACK, 0);
+               SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
        if (err < 0)
                return -E_PCM_OPEN;
        if ((err = snd_pcm_info(pad->handle, info)) < 0)
        if (err < 0)
                return -E_PCM_OPEN;
        if ((err = snd_pcm_info(pad->handle, info)) < 0)
@@ -87,10 +99,10 @@ 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,
        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)
+                       pad->channels) < 0)
                return -E_CHANNEL_COUNT;
        if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
                return -E_CHANNEL_COUNT;
        if (snd_pcm_hw_params_set_rate_near(pad->handle, hwparams,
-                       (unsigned int*) &conf->sample_rate_arg, 0) < 0)
+                       &pad->samplerate, 0) < 0)
                return -E_SET_RATE;
        err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &pad->buffer_time, 0);
        if (err < 0 || !pad->buffer_time)
                return -E_SET_RATE;
        err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &pad->buffer_time, 0);
        if (err < 0 || !pad->buffer_time)
@@ -129,80 +141,70 @@ 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)
        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;
+               * pad->channels / 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;
 }
        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)
+
+static int alsa_write_pre_select(struct sched *s, struct writer_node *wn)
 {
 {
-       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;
 
        struct private_alsa_data *pad = wn->private_data;
        struct writer_node_group *wng = wn->wng;
        struct timeval diff;
 
-       t->ret = 0;
-       if (*wng->input_eof && *wng->loaded < pad->bytes_per_frame)
-               return;
-       t->ret = 1;
        if (*wng->loaded < pad->bytes_per_frame)
        if (*wng->loaded < pad->bytes_per_frame)
-               return;
-       if (tv_diff(&s->now, &pad->next_chunk, &diff) < 0) {
+               return 1;
+       if (tv_diff(now, &pad->next_chunk, &diff) < 0) {
                if (tv_diff(&s->timeout, &diff, NULL) > 0)
                        s->timeout = diff;
        } else {
                s->timeout.tv_sec = 0;
                if (tv_diff(&s->timeout, &diff, NULL) > 0)
                        s->timeout = diff;
        } else {
                s->timeout.tv_sec = 0;
-               s->timeout.tv_usec = 0;
+               s->timeout.tv_usec = 1;
        }
        }
+       return 1;
+//     PARA_INFO_LOG("timeout: %lu\n", tv2ms(&s->timeout));
 }
 
 }
 
-static void alsa_write_post_select(struct sched *s, struct task *t)
+static int alsa_write_post_select(__a_unused struct sched *s,
+               struct writer_node *wn)
 {
 {
-       struct writer_node *wn = t->private_data;
        struct private_alsa_data *pad = wn->private_data;
        struct writer_node_group *wng = wn->wng;
        struct private_alsa_data *pad = wn->private_data;
        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;
+       size_t frames = (*wng->loaded - wn->written) / pad->bytes_per_frame;
+       snd_pcm_sframes_t ret;
+       unsigned char *data = (unsigned char*)wng->buf + wn->written;
+       struct timeval tv;
 
 
-       t->ret = 0;
+//     PARA_INFO_LOG("%zd frames\n", frames);
        if (!frames) {
                if (*wng->input_eof)
        if (!frames) {
                if (*wng->input_eof)
-                       t->ret = *wng->loaded;
-               return;
+                       wn->written = *wng->loaded;
+               return 1;
        }
        }
-       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 (ret < 0) {
-                       t->ret = -E_ALSA_WRITE;
-                       return;
-               }
-               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);
+       if (tv_diff(now, &pad->next_chunk, NULL) < 0)
+               return 1;
+       ret = snd_pcm_writei(pad->handle, data, frames);
+       if (ret == -EPIPE) {
+               PARA_WARNING_LOG("%s", "EPIPE\n");
+               snd_pcm_prepare(pad->handle);
+               return 1;
+       }
+       if (ret < 0) {
+               PARA_WARNING_LOG("%s", "ALSA ERROR\n");
+               return -E_ALSA_WRITE;
+       }
+       ms2tv(pad->buffer_time / 4000, &tv);
+//     ms2tv(1, &tv);
+       tv_add(now, &tv, &pad->next_chunk);
+       wn->written += ret * pad->bytes_per_frame;
+       return 1;
 }
 
 static void alsa_close(struct writer_node *wn)
 {
        struct private_alsa_data *pad = wn->private_data;
 }
 
 static void alsa_close(struct writer_node *wn)
 {
        struct private_alsa_data *pad = wn->private_data;
+       PARA_INFO_LOG("closing writer node %p\n", wn);
        snd_pcm_drain(pad->handle);
        snd_pcm_close(pad->handle);
        snd_config_update_free_global();
        snd_pcm_drain(pad->handle);
        snd_pcm_close(pad->handle);
        snd_config_update_free_global();