2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write_common.c common functions of para_audiod and para_write */
17 #include "buffer_tree.h"
21 /** the array containing the names of all supported writers */
22 const char *writer_names[] ={WRITER_NAMES};
24 /** the array of supported writers */
25 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
27 static void wng_pre_select(struct sched *s, struct task *t)
29 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
32 FOR_EACH_WRITER_NODE(i, g) {
33 struct writer_node *wn = &g->writer_nodes[i];
34 struct writer *w = writers + wn->writer_num;
37 t->error = w->pre_select(s, wn);
42 * Force a minimal delay if something was written during the previous
43 * call to wng_post_select(). This is necessary because the filter
44 * chain might still have data for us which it couldn't convert during
45 * the previous run due to its buffer size constraints. In this case we
46 * do not want to wait until the next input data arrives as this could
47 * lead to buffer underruns.
49 if (g->last_written == 0)
51 s->timeout.tv_sec = 0;
52 s->timeout.tv_usec = 1;
55 static void wng_post_select(struct sched *s, struct task *t)
57 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
59 size_t min_written = 0, max_written = 0;
61 FOR_EACH_WRITER_NODE(i, g) {
62 struct writer_node *wn = &g->writer_nodes[i];
63 struct writer *w = writers + wn->writer_num;
64 t->error = w->post_select(s, wn);
68 min_written = wn->written;
70 min_written = PARA_MIN(min_written, wn->written);
71 max_written = PARA_MAX(max_written, wn->written);
73 g->last_written = max_written;
74 //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
76 *g->loaded -= min_written;
77 FOR_EACH_WRITER_NODE(i, g)
78 g->writer_nodes[i].written -= min_written;
80 if (!*g->loaded && *g->input_error) {
81 t->error = *g->input_error;
84 if (*g->loaded && min_written) {
85 // PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
86 memmove(*g->bufp, *g->bufp + min_written, *g->loaded);
91 * call the open function of each writer in the group
93 * \param g the writer node group
95 * \return If at least one open function returned an error, all successful
96 * writer notes get closed and this error value is returned. Upon success, a
97 * task associated with \a g is registered to the scheduler and the function
98 * returns a positive value.
100 int wng_open(struct writer_node_group *g)
104 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
105 FOR_EACH_WRITER_NODE(i, g) {
106 struct writer_node *wn = &g->writer_nodes[i];
107 struct writer *w = writers + wn->writer_num;
113 sprintf(g->task.status, "%s", "writer node group");
114 register_task(&g->task);
118 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
120 struct writer_node *wn = &g->writer_nodes[--i];
121 struct writer *w = writers + wn->writer_num;
124 free(g->writer_nodes);
126 g->task.error = -E_TASK_UNREGISTERED;
131 * call the close function of each writer in the given group
133 * \param g the writer node group to close
135 * This function also frees all resources of the given group.
137 void wng_close(struct writer_node_group *g)
143 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
144 FOR_EACH_WRITER_NODE(i, g) {
145 struct writer_node *wn = &g->writer_nodes[i];
146 struct writer *w = writers + wn->writer_num;
149 free(g->writer_nodes);
154 * allocate and initialize a new writer_node_group struct
156 * \param num_writers the number of writer nodes for the new group
158 * \return Pointer to the new writer node group
160 struct writer_node_group *wng_new(unsigned num_writers)
162 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
163 g->num_writers = num_writers;
164 g->writer_nodes = para_calloc(num_writers
165 * sizeof(struct writer_node));
166 g->task.post_select = wng_post_select;
167 g->task.pre_select = wng_pre_select;
172 * Call the init function of each supported paraslash writer.
174 void writer_init(void)
179 writers[i].init(&writers[i]);
182 * check if given string is a valid command line for any writer
184 * \param \wa string of the form writer_name:options
185 * \param writer_num contains the number of the writer upon success
187 * This function checks whether \a wa starts with the name of a supported
188 * paraslash writer, optionally followed by a colon and any options for that
189 * writer. If a valid writer name was found and further are present, the
190 * remaining part of \a wa is passed to that writer's config parser.
192 * \return On success, a pointer to the gengetopt args info struct is returned
193 * and \a writer_num contains the number of the writer. Otherwise this function
196 void *check_writer_arg(const char *wa, int *writer_num)
200 *writer_num = -E_WRITE_COMMON_SYNTAX;
201 PARA_INFO_LOG("checking %s\n", wa);
203 const char *name = writer_names[i];
204 size_t len = strlen(name);
206 if (strlen(wa) < len)
208 if (strncmp(name, wa, len))
213 if (c && !writers[i].parse_config)
216 return writers[i].parse_config(c? wa + len + 1 : "");
218 PARA_ERROR_LOG("writer not found\n");
223 * setup a writer node group with only one writer, the default writer
225 * The writer which is set up depends on the OS. It defaults to alsa for Linux,
226 * osx_write for OS X, file writer if neither of these is supported.
228 * \return pointer to the allocated writer node group
230 struct writer_node_group *setup_default_wng(void)
232 struct writer_node_group *wng = wng_new(1);
233 wng->writer_nodes[0].writer_num = DEFAULT_WRITER;
234 PARA_INFO_LOG("using default writer: %s %p\n",
235 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
236 wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");
241 * Setup a writer node with the default writer.
243 * If arg is \p NULL, the OS-dependent default writer is used with an empty
244 * configuration string. It defaults to alsa for Linux, osx for OS X, oss for
245 * *BSD and the file writer if neither of these is supported.
247 * Once the writer configuration has been retrieved, a writer node is created,
248 * its buffer tree node is added to the buffer tree as a child of the given
251 * Finally, the new writer node's taks structure is initialized and registered
252 * to the paraslash scheduler.
254 * \return A pointer to the allocated writer node group.
256 struct writer_node *setup_writer_node(const char *arg, struct btr_node *parent)
258 struct writer_node *wn = para_calloc(sizeof(*wn));
263 wn->conf = check_writer_arg(arg, &wn->writer_num);
265 wn->writer_num = DEFAULT_WRITER;
266 wn->conf = writers[DEFAULT_WRITER].parse_config("");
272 w = writers + wn->writer_num;
273 name = writer_names[wn->writer_num];
274 wn->btrn = btr_new_node(name, parent, w->execute, wn);
275 sprintf(wn->task.status, "%s", name);
277 wn->task.post_select = w->post_select_btr;
278 wn->task.pre_select = w->pre_select_btr;
279 register_task(&wn->task);
285 * Print the help text of all writers to stdout.
287 * \param detailed Whether to print the detailed help text.
289 void print_writer_helps(int detailed)
293 printf_or_die("\nAvailable writers: \n\t");
295 printf_or_die("%s%s", i? " " : "", writer_names[i]);
296 printf_or_die("\n\n");
298 struct writer *w = writers + i;
300 if (!w->help.short_help)
302 printf_or_die("Options for %s:\n", writer_names[i]);
303 ggo_print_help(&w->help, detailed);