From 0610638110061ff05670a6c497b371bf2e1d4d0f Mon Sep 17 00:00:00 2001 From: Andre Date: Tue, 23 May 2006 20:27:23 +0200 Subject: [PATCH] Make the wng->eof a pointer. The receiver/stdin_reader already have a corresponding field. This patch also fixes a bug in the alsa_writer which was introduced by the previous patch. This bug caused a playback delay of one second. --- alsa_writer.c | 9 ++++++--- stdin.c | 18 +++++------------- stdin.h | 1 + write.c | 5 +++-- write.h | 2 +- write_common.c | 47 ++--------------------------------------------- 6 files changed, 18 insertions(+), 64 deletions(-) diff --git a/alsa_writer.c b/alsa_writer.c index 3a2226cf..ad65c92f 100644 --- a/alsa_writer.c +++ b/alsa_writer.c @@ -140,13 +140,13 @@ static void alsa_write_pre_select(struct sched *s, struct task *t) struct timeval diff; t->ret = 0; - if (wng->eof && *wng->loaded < pad->bytes_per_frame) + if (*wng->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) + if (tv_diff(&s->timeout, &diff, NULL) > 0) s->timeout = diff; } else { s->timeout.tv_sec = 0; @@ -164,8 +164,11 @@ static void alsa_write_post_select(struct sched *s, struct task *t) unsigned char *data = (unsigned char*)wng->buf; t->ret = 0; - if (!frames) + if (!frames) { + if (*wng->eof) + t->ret = *wng->loaded; return; + } if (tv_diff(&s->now, &pad->next_chunk, NULL) < 0) return; // PARA_INFO_LOG("%zd frames\n", frames); diff --git a/stdin.c b/stdin.c index cecdcb77..6b9c8098 100644 --- a/stdin.c +++ b/stdin.c @@ -27,22 +27,14 @@ void stdin_post_select(struct sched *s, struct task *t) ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded); if (ret < 0) t->ret = -E_STDIN_READ; - else { + else if (ret > 0) { sit->loaded += ret; t->ret = ret; - } + } else + t->ret = 0; + if (ret <= 0) + sit->eof = 1; sprintf(t->status, "%p stdin reader: loaded = %d, ret = %d", sit, sit->loaded, t->ret); } - -#if 0 -void stdin_init(struct stdin_task *sit) -{ - sit->task.private_data = sit; - sit->task.pre_select = stdin_pre_select; - sit->task.post_select = stdin_post_select; - sit->task.flags = POST_EOF_IS_ERROR; - sprintf(sit->task.status, "%p stdin reader: initialized", &sit->task); -} -#endif diff --git a/stdin.h b/stdin.h index 9213bf46..753cef56 100644 --- a/stdin.h +++ b/stdin.h @@ -3,6 +3,7 @@ struct stdin_task { size_t bufsize; size_t loaded; struct task task; + int eof; }; void stdin_pre_select(struct sched *s, struct task *t); diff --git a/write.c b/write.c index 508aee79..dffa5ecd 100644 --- a/write.c +++ b/write.c @@ -148,8 +148,10 @@ static struct writer_node_group *check_args(void) } ret = 1; out: - if (ret > 0) + if (ret > 0) { + wng->eof = &sit.eof; return wng; + } free(wng); return NULL; } @@ -194,7 +196,6 @@ static void stdin_error_handler(struct task *t) PARA_INFO_LOG("task %p, ret: %d\n", t, t->ret); if (t->ret < 0) PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret)); - wng->eof = 1; } int main(int argc, char *argv[]) diff --git a/write.h b/write.h index aece2313..e6e0a762 100644 --- a/write.h +++ b/write.h @@ -94,7 +94,7 @@ int *written; /** the maximum of the chunk_bytes values of the writer nodes in this group */ size_t max_chunk_bytes; /** non-zero if end of file was encountered */ -int eof; +int *eof; char *buf; size_t *loaded; struct task task; diff --git a/write_common.c b/write_common.c index 6ff778a9..2cb0eafd 100644 --- a/write_common.c +++ b/write_common.c @@ -28,50 +28,6 @@ const char *writer_names[] ={WRITER_NAMES}; struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY}; -int wng_write(struct writer_node_group *g, char *buf, size_t *loaded) -{ - int ret, i, need_more_writes = 1; - size_t min_written = 0; - - while (need_more_writes) { - need_more_writes = 0; - FOR_EACH_WRITER_NODE(i, g) { - size_t w = g->written[i]; - int bytes_to_write; - struct writer_node *wn = &g->writer_nodes[i]; - if (!i) - min_written = w; - else - min_written = PARA_MIN(min_written, w); - if (w == *loaded) - continue; - if (!g->eof && (*loaded < wn->chunk_bytes + w)) - continue; - bytes_to_write = PARA_MIN(wn->chunk_bytes, - *loaded - w); - ret = wn->writer->write(buf + w, bytes_to_write, wn); - if (ret < 0) - goto out; - if (ret != bytes_to_write) - PARA_WARNING_LOG("short write: %d/%d\n", ret, - bytes_to_write); - g->written[i] += ret; - need_more_writes = 1; - } - } - *loaded -= min_written; - ret = 0; - if (g->eof) - goto out; - if (*loaded) - memmove(buf, buf + min_written, *loaded); - FOR_EACH_WRITER_NODE(i, g) - g->written[i] -= min_written; - ret = 1; -out: - return ret; -} - static void wng_post_select(struct sched *s, struct task *t) { struct writer_node_group *g = t->private_data; @@ -89,7 +45,7 @@ static void wng_post_select(struct sched *s, struct task *t) min_written = PARA_MIN(min_written, t->ret); } *g->loaded -= min_written; - if (!*g->loaded && g->eof) + if (!*g->loaded && *g->eof) t->ret = 0; else t->ret = 1; @@ -114,6 +70,7 @@ int wng_open(struct writer_node_group *g) wn->task.pre_select = wn->writer->pre_select; wn->task.post_select = wn->writer->post_select; wn->task.private_data = wn; + wn->task.flags = POST_EOF_IS_ERROR; register_task(&wn->task); } register_task(&g->task); -- 2.39.5