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_pre_select(__a_unused struct sched *s, struct task *t)
33 struct writer_node_group *g = t->private_data;
36 FOR_EACH_WRITER_NODE(i, g) {
37 struct writer_node *wn = &g->writer_nodes[i];
38 t->ret = wn->writer->pre_select(s, wn);
46 static void wng_post_select(struct sched *s, struct task *t)
48 struct writer_node_group *g = t->private_data;
50 size_t min_written = 0;
52 FOR_EACH_WRITER_NODE(i, g) {
53 struct writer_node *wn = &g->writer_nodes[i];
54 t->ret = wn->writer->post_select(s, wn);
62 min_written = PARA_MIN(min_written, t->ret);
64 *g->loaded -= min_written;
65 if (!*g->loaded && *g->input_eof) {
70 if (*g->loaded && min_written)
71 memmove(g->buf, g->buf + min_written, *g->loaded);
74 int wng_open(struct writer_node_group *g)
78 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
79 register_task(&g->task);
80 FOR_EACH_WRITER_NODE(i, g) {
81 struct writer_node *wn = &g->writer_nodes[i];
83 ret = wn->writer->open(wn);
86 wn->chunk_bytes = ret;
87 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
89 sprintf(g->task.status, "%s", "writer node group");
93 unregister_task(&g->task);
95 struct writer_node *wn = &g->writer_nodes[--i];
96 wn->writer->close(wn);
103 void wng_unregister(struct writer_node_group *g)
105 unregister_task(&g->task);
109 void wng_close(struct writer_node_group *g)
115 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
116 FOR_EACH_WRITER_NODE(i, g) {
117 struct writer_node *wn = &g->writer_nodes[i];
118 wn->writer->close(wn);
121 free(g->writer_nodes);
125 struct writer_node_group *wng_new(unsigned num_writers)
127 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
128 g->num_writers = num_writers;
129 g->writer_nodes = para_calloc(num_writers
130 * sizeof(struct writer_node));
131 g->written = para_calloc(num_writers * sizeof(size_t));
132 g->task.private_data = g;
133 g->task.post_select = wng_post_select;
134 g->task.pre_select = wng_pre_select;
138 void init_supported_writers(void)
143 writers[i].init(&writers[i]);
146 void *check_writer_arg(char *wa, int *writer_num)
150 *writer_num = -E_WRITE_COMMON_SYNTAX;
151 PARA_INFO_LOG("checking %s\n", wa);
153 const char *name = writer_names[i];
154 size_t len = strlen(name);
156 if (strlen(wa) < len)
158 if (strncmp(name, wa, len))
163 if (c && !writers[i].parse_config)
166 return writers[i].parse_config(c? wa + len + 1 : "");
168 PARA_ERROR_LOG("%s", "writer not found\n");
172 struct writer_node_group *setup_default_wng(void)
174 struct writer_node_group *wng = wng_new(1);
175 enum writer_enum default_writer;
177 if (NUM_SUPPORTED_WRITERS == 1)
178 default_writer = FILE_WRITE;
181 wng->writer_nodes[0].writer = &writers[default_writer];
182 PARA_INFO_LOG("using default writer: %s %p\n",
183 writer_names[default_writer], writers[default_writer].parse_config);
184 wng->writer_nodes[0].conf = writers[default_writer].parse_config("");