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 */
26 const char *writer_names[] ={WRITER_NAMES};
27 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
29 int wng_write(struct writer_node_group *g, char *buf, size_t *loaded)
31 int ret, i, need_more_writes = 1;
32 size_t min_written = 0;
34 while (need_more_writes) {
36 FOR_EACH_WRITER_NODE(i, g) {
37 size_t w = g->written[i];
39 struct writer_node *wn = &g->writer_nodes[i];
43 min_written = PARA_MIN(min_written, w);
46 if (!g->eof && (*loaded < wn->chunk_bytes + w))
48 bytes_to_write = PARA_MIN(wn->chunk_bytes,
50 ret = wn->writer->write(buf + w, bytes_to_write, wn);
53 if (ret != bytes_to_write)
54 PARA_WARNING_LOG("short write: %d/%d\n", ret,
60 *loaded -= min_written;
65 memmove(buf, buf + min_written, *loaded);
66 FOR_EACH_WRITER_NODE(i, g)
67 g->written[i] -= min_written;
73 int wng_open(struct writer_node_group *g)
77 FOR_EACH_WRITER_NODE(i, g) {
78 struct writer_node *wn = &g->writer_nodes[i];
79 ret = wn->writer->open(wn);
82 wn->chunk_bytes = ret;
83 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
89 void wng_close(struct writer_node_group *g)
93 FOR_EACH_WRITER_NODE(i, g) {
94 struct writer_node *wn = &g->writer_nodes[i];
95 wn->writer->close(wn);
99 struct writer_node_group *wng_new(unsigned num_writers)
101 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
102 g->num_writers = num_writers;
103 g->writer_nodes = para_calloc(num_writers
104 * sizeof(struct writer_node));
105 g->written = para_calloc(num_writers * sizeof(size_t));
109 void wng_destroy(struct writer_node_group *g)
114 free(g->writer_nodes);
118 void init_supported_writers(void)
123 writers[i].init(&writers[i]);
126 int check_writer_arg(const char *arg)
128 int i, ret = -E_WRITE_COMMON_SYNTAX;
129 char *a = para_strdup(arg), *p = strchr(a, ':');
134 if (strcmp(writer_names[i], a))
144 struct writer_node_group *setup_default_wng(void)
146 struct writer_node_group *wng = wng_new(1);
147 enum writer_enum default_writer;
149 if (NUM_SUPPORTED_WRITERS == 1)
150 default_writer = FILE_WRITE;
153 wng->writer_nodes[0].writer = &writers[default_writer];
154 PARA_INFO_LOG("using default writer: %s\n",
155 writer_names[default_writer]);