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];
38 unsigned char *p = buf + w;
40 struct writer_node *wn = &g->writer_nodes[i];
44 min_written = PARA_MIN(min_written, w);
47 if (!g->eof && (*loaded < wn->chunk_bytes + w))
49 bytes_to_write = PARA_MIN(wn->chunk_bytes,
51 ret = wn->writer->write(p, bytes_to_write, wn);
54 if (ret != bytes_to_write)
55 PARA_WARNING_LOG("short write: %d/%d\n", ret,
61 *loaded -= min_written;
66 memmove(buf, buf + min_written, *loaded);
67 FOR_EACH_WRITER_NODE(i, g)
68 g->written[i] -= min_written;
74 int wng_open(struct writer_node_group *g)
78 FOR_EACH_WRITER_NODE(i, g) {
79 struct writer_node *wn = &g->writer_nodes[i];
80 ret = wn->writer->open(wn);
83 wn->chunk_bytes = ret;
84 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
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 wn->writer->close(wn);
100 struct writer_node_group *wng_new(unsigned num_writers)
102 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
103 g->num_writers = num_writers;
104 g->writer_nodes = para_calloc(num_writers
105 * sizeof(struct writer_node));
106 g->written = para_calloc(num_writers * sizeof(size_t));
110 void wng_destroy(struct writer_node_group *g)
115 free(g->writer_nodes);
119 void init_supported_writers(void)
124 writers[i].init(&writers[i]);
127 int check_writer_arg(const char *arg)
129 int i, ret = -E_WRITE_COMMON_SYNTAX;
130 char *a = para_strdup(arg), *p = strchr(a, ':');
135 if (strcmp(writer_names[i], a))
145 struct writer_node_group *setup_default_wng(void)
147 struct writer_node_group *wng = wng_new(1);
148 enum writer_enum default_writer;
150 if (NUM_SUPPORTED_WRITERS == 1)
151 default_writer = FILE_WRITE;
154 wng->writer_nodes[0].writer = &writers[default_writer];
155 PARA_INFO_LOG("using default writer: %s\n",
156 writer_names[default_writer]);