]> git.tuebingen.mpg.de Git - paraslash.git/blob - write_common.c
trivial typo fix
[paraslash.git] / write_common.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /** \file write_common.c common functions of para_audiod and para_write */
20
21 #include "para.h"
22 #include "string.h"
23 #include "list.h"
24 #include "sched.h"
25 #include "write.h"
26 #include "error.h"
27
28 /** the array containing the names of all supported writers */
29 const char *writer_names[] ={WRITER_NAMES};
30
31 /** the array of supported writers */
32 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
33
34 static void wng_pre_select(__a_unused struct sched *s, struct task *t)
35 {
36         struct writer_node_group *g = t->private_data;
37         int i;
38
39         FOR_EACH_WRITER_NODE(i, g) {
40                 struct writer_node *wn = &g->writer_nodes[i];
41                 t->ret = wn->writer->pre_select(s, wn);
42                 if (t->ret < 0) {
43                         g->eof = 1;
44                         return;
45                 }
46         }
47 }
48
49 static void wng_post_select(struct sched *s, struct task *t)
50 {
51         struct writer_node_group *g = t->private_data;
52         int i;
53         size_t min_written = 0;
54
55         FOR_EACH_WRITER_NODE(i, g) {
56                 struct writer_node *wn = &g->writer_nodes[i];
57                 t->ret = wn->writer->post_select(s, wn);
58                 if (t->ret < 0) {
59                         g->eof = 1;
60                         return;
61                 }
62                 if (!i)
63                         min_written = wn->written;
64                 else
65                         min_written = PARA_MIN(min_written, wn->written);
66         }
67 //      PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
68         if (min_written) {
69                 *g->loaded -= min_written;
70                 FOR_EACH_WRITER_NODE(i, g)
71                         g->writer_nodes[i].written -= min_written;
72         }
73         if (!*g->loaded && *g->input_eof) {
74                 g->eof = 1;
75                 t->ret = -E_WNG_EOF;
76                 return;
77         }
78         t->ret = 1;
79         if (*g->loaded && min_written) {
80 //              PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
81                 memmove(g->buf, g->buf + min_written, *g->loaded);
82         }
83 }
84
85 /**
86  * call the open function of each writer in the group
87  *
88  * \param g the writer node group
89  *
90  * \return If at least one open function returned an error, all successful
91  * writer notes get closed and this error value is returned. Upon success, a
92  * task associated with \a g is registered to the scheduler and the function
93  * returnes a positive value.
94  * */
95 int wng_open(struct writer_node_group *g)
96 {
97         int i, ret = 1;
98
99         PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
100         register_task(&g->task);
101         FOR_EACH_WRITER_NODE(i, g) {
102                 struct writer_node *wn = &g->writer_nodes[i];
103                 wn->wng = g;
104                 ret = wn->writer->open(wn);
105                 if (ret < 0)
106                         goto err_out;
107                 wn->chunk_bytes = ret;
108                 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
109         }
110         sprintf(g->task.status, "%s", "writer node group");
111         g->eof = 0;
112         return 1;
113 err_out:
114         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
115         unregister_task(&g->task);
116         while (i > 0) {
117                 struct writer_node *wn = &g->writer_nodes[--i];
118                 wn->writer->close(wn);
119         }
120         g->num_writers = 0;
121         g->eof = 1;
122         return ret;
123 }
124
125
126 /**
127  * unregister a writer node group task
128  *
129  * \param g the group whose task is to be closed
130  */
131 void wng_unregister(struct writer_node_group *g)
132 {
133         unregister_task(&g->task);
134         g->eof = 1;
135 }
136
137 /**
138  * call the close function of each writer in the given group
139  *
140  * \param g the writer node group to close
141  *
142  * This function also frees all resources of the given group.
143  */
144 void wng_close(struct writer_node_group *g)
145 {
146         int i;
147
148         if (!g)
149                 return;
150         PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
151         FOR_EACH_WRITER_NODE(i, g) {
152                 struct writer_node *wn = &g->writer_nodes[i];
153                 wn->writer->close(wn);
154         }
155         free(g->writer_nodes);
156         free(g);
157 }
158
159 /**
160  * allocate and initialize a new writer_node_group struct
161  *
162  * \param num_writers the number of writer nodes for the new group
163  *
164  * \return Pointer to the new writer node group
165  */
166 struct writer_node_group *wng_new(unsigned num_writers)
167 {
168         struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
169         g->num_writers = num_writers;
170         g->writer_nodes = para_calloc(num_writers
171                 * sizeof(struct writer_node));
172         g->task.private_data = g;
173         g->task.post_select = wng_post_select;
174         g->task.pre_select = wng_pre_select;
175         return g;
176 }
177
178 /**
179  * call the init function of each supported paraslash writer
180  */
181 void init_supported_writers(void)
182 {
183         int i;
184
185         FOR_EACH_WRITER(i)
186                 writers[i].init(&writers[i]);
187 }
188 /**
189  * check if given string is a valid command line for any writer
190  *
191  * \param \wa string of the form writer_name:options
192  * \param writer_num contains the number of the writer upon success
193  *
194  * This function checks whether \a wa starts with the name of a supported
195  * paraslash writer, optinally followed by a colon and any options for that
196  * writer.  If a valid writer name was found and further are present, the
197  * remaining part of \a wa is passed to that writer's config parser.
198  *
199  * \return On success, a pointer to the gengetopt args info struct is returned
200  * and \a writer_num contains the number of the writer. Otherwise this function
201  * returns \p NULL.
202  */
203 void *check_writer_arg(const char *wa, int *writer_num)
204 {
205         int i;
206
207         *writer_num = -E_WRITE_COMMON_SYNTAX;
208         PARA_INFO_LOG("checking  %s\n", wa);
209         FOR_EACH_WRITER(i) {
210                 const char *name = writer_names[i];
211                 size_t len = strlen(name);
212                 char c;
213                 if (strlen(wa) < len)
214                         continue;
215                 if (strncmp(name, wa, len))
216                         continue;
217                 c = wa[len];
218                 if (c && c != ' ')
219                         continue;
220                 if (c && !writers[i].parse_config)
221                         return NULL;
222                 *writer_num = i;
223                 return writers[i].parse_config(c? wa + len + 1 : "");
224         }
225         PARA_ERROR_LOG("%s", "writer not found\n");
226         return NULL;
227 }
228
229 /**
230  * setup a writer node group with only one writer, the default writer
231  *
232  * The writer which is set up depends on the OS. It defaults to alsa for Linux,
233  * osx_write for OS X, file writer if neither of these is supported.
234  *
235  * \return pointer to the allocated writer node group
236  */
237 struct writer_node_group *setup_default_wng(void)
238 {
239         struct writer_node_group *wng = wng_new(1);
240         wng->writer_nodes[0].writer = &writers[DEFAULT_WRITER];
241         PARA_INFO_LOG("using default writer: %s %p\n",
242                 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
243         wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");
244         return wng;
245 }