2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file write_common.c common functions of para_audiod and para_write */
28 const char *writer_names[] ={WRITER_NAMES};
29 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
31 static void wng_post_select(struct sched *s, struct task *t)
33 struct writer_node_group *g = t->private_data;
35 size_t min_written = 0;
37 FOR_EACH_WRITER_NODE(i, g) {
38 struct writer_node *wn = &g->writer_nodes[i];
39 t->ret = wn->task.ret;
45 min_written = PARA_MIN(min_written, t->ret);
47 *g->loaded -= min_written;
48 if (!*g->loaded && *g->eof)
52 if (*g->loaded && min_written)
53 memmove(g->buf, g->buf + min_written, *g->loaded);
56 int wng_open(struct writer_node_group *g)
60 FOR_EACH_WRITER_NODE(i, g) {
61 struct writer_node *wn = &g->writer_nodes[i];
62 ret = wn->writer->open(wn);
65 wn->chunk_bytes = ret;
66 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
68 PARA_DEBUG_LOG("pre_select: %p\n", &wn->writer->pre_select);
69 PARA_DEBUG_LOG("post_select: %p\n", &wn->writer->post_select);
70 wn->task.pre_select = wn->writer->pre_select;
71 wn->task.post_select = wn->writer->post_select;
72 wn->task.private_data = wn;
73 wn->task.flags = POST_EOF_IS_ERROR;
74 register_task(&wn->task);
76 register_task(&g->task);
81 void wng_destroy(struct writer_node_group *g)
86 free(g->writer_nodes);
90 void wng_close(struct writer_node_group *g)
94 FOR_EACH_WRITER_NODE(i, g) {
95 struct writer_node *wn = &g->writer_nodes[i];
96 unregister_task(&wn->task);
97 wn->writer->close(wn);
101 static void wng_error_handler(struct task *t)
103 struct writer_node_group *g = t->private_data;
105 PARA_INFO_LOG("%p: ret = %d\n", t, t->ret);
111 struct writer_node_group *wng_new(unsigned num_writers)
113 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
114 g->num_writers = num_writers;
115 g->writer_nodes = para_calloc(num_writers
116 * sizeof(struct writer_node));
117 g->written = para_calloc(num_writers * sizeof(size_t));
118 g->task.private_data = g;
119 g->task.post_select = wng_post_select;
120 g->task.error_handler = wng_error_handler;
121 g->task.flags = POST_ADD_TAIL | POST_EOF_IS_ERROR;
125 void init_supported_writers(void)
130 writers[i].init(&writers[i]);
133 int check_writer_arg(const char *arg)
135 int i, ret = -E_WRITE_COMMON_SYNTAX;
136 char *a = para_strdup(arg), *p = strchr(a, ':');
141 if (strcmp(writer_names[i], a))
151 struct writer_node_group *setup_default_wng(void)
153 struct writer_node_group *wng = wng_new(1);
154 enum writer_enum default_writer;
156 if (NUM_SUPPORTED_WRITERS == 1)
157 default_writer = FILE_WRITE;
160 wng->writer_nodes[0].writer = &writers[default_writer];
161 PARA_INFO_LOG("using default writer: %s\n",
162 writer_names[default_writer]);