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 */
19 /** the array containing the names of all supported writers */
20 const char *writer_names[] ={WRITER_NAMES};
22 /** the array of supported writers */
23 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
25 static void wng_pre_select(struct sched *s, struct task *t)
27 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
30 FOR_EACH_WRITER_NODE(i, g) {
31 struct writer_node *wn = &g->writer_nodes[i];
32 struct writer *w = writers + wn->writer_num;
35 t->error = w->pre_select(s, wn);
40 * Force a minimal delay if something was written during the previous
41 * call to wng_post_select(). This is necessary because the filter
42 * chain might still have data for us which it couldn't convert during
43 * the previous run due to its buffer size constraints. In this case we
44 * do not want to wait until the next input data arrives as this could
45 * lead to buffer underruns.
47 if (g->last_written == 0)
49 s->timeout.tv_sec = 0;
50 s->timeout.tv_usec = 1;
53 static void wng_post_select(struct sched *s, struct task *t)
55 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
57 size_t min_written = 0, max_written = 0;
59 FOR_EACH_WRITER_NODE(i, g) {
60 struct writer_node *wn = &g->writer_nodes[i];
61 struct writer *w = writers + wn->writer_num;
62 t->error = w->post_select(s, wn);
66 min_written = wn->written;
68 min_written = PARA_MIN(min_written, wn->written);
69 max_written = PARA_MAX(max_written, wn->written);
71 g->last_written = max_written;
72 //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
74 *g->loaded -= min_written;
75 FOR_EACH_WRITER_NODE(i, g)
76 g->writer_nodes[i].written -= min_written;
78 if (!*g->loaded && *g->input_error) {
79 t->error = *g->input_error;
82 if (*g->loaded && min_written) {
83 // PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
84 memmove(*g->bufp, *g->bufp + min_written, *g->loaded);
89 * call the open function of each writer in the group
91 * \param g the writer node group
93 * \return If at least one open function returned an error, all successful
94 * writer notes get closed and this error value is returned. Upon success, a
95 * task associated with \a g is registered to the scheduler and the function
96 * returns a positive value.
98 int wng_open(struct writer_node_group *g)
102 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
103 FOR_EACH_WRITER_NODE(i, g) {
104 struct writer_node *wn = &g->writer_nodes[i];
105 struct writer *w = writers + wn->writer_num;
111 sprintf(g->task.status, "%s", "writer node group");
112 register_task(&g->task);
116 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
118 struct writer_node *wn = &g->writer_nodes[--i];
119 struct writer *w = writers + wn->writer_num;
122 free(g->writer_nodes);
124 g->task.error = -E_TASK_UNREGISTERED;
129 * call the close function of each writer in the given group
131 * \param g the writer node group to close
133 * This function also frees all resources of the given group.
135 void wng_close(struct writer_node_group *g)
141 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
142 FOR_EACH_WRITER_NODE(i, g) {
143 struct writer_node *wn = &g->writer_nodes[i];
144 struct writer *w = writers + wn->writer_num;
147 free(g->writer_nodes);
152 * allocate and initialize a new writer_node_group struct
154 * \param num_writers the number of writer nodes for the new group
156 * \return Pointer to the new writer node group
158 struct writer_node_group *wng_new(unsigned num_writers)
160 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
161 g->num_writers = num_writers;
162 g->writer_nodes = para_calloc(num_writers
163 * sizeof(struct writer_node));
164 g->task.post_select = wng_post_select;
165 g->task.pre_select = wng_pre_select;
170 * Call the init function of each supported paraslash writer.
172 void writer_init(void)
177 writers[i].init(&writers[i]);
180 * check if given string is a valid command line for any writer
182 * \param \wa string of the form writer_name:options
183 * \param writer_num contains the number of the writer upon success
185 * This function checks whether \a wa starts with the name of a supported
186 * paraslash writer, optionally followed by a colon and any options for that
187 * writer. If a valid writer name was found and further are present, the
188 * remaining part of \a wa is passed to that writer's config parser.
190 * \return On success, a pointer to the gengetopt args info struct is returned
191 * and \a writer_num contains the number of the writer. Otherwise this function
194 void *check_writer_arg(const char *wa, int *writer_num)
198 *writer_num = -E_WRITE_COMMON_SYNTAX;
199 PARA_INFO_LOG("checking %s\n", wa);
201 const char *name = writer_names[i];
202 size_t len = strlen(name);
204 if (strlen(wa) < len)
206 if (strncmp(name, wa, len))
211 if (c && !writers[i].parse_config)
214 return writers[i].parse_config(c? wa + len + 1 : "");
216 PARA_ERROR_LOG("writer not found\n");
221 * setup a writer node group with only one writer, the default writer
223 * The writer which is set up depends on the OS. It defaults to alsa for Linux,
224 * osx_write for OS X, file writer if neither of these is supported.
226 * \return pointer to the allocated writer node group
228 struct writer_node_group *setup_default_wng(void)
230 struct writer_node_group *wng = wng_new(1);
231 wng->writer_nodes[0].writer_num = DEFAULT_WRITER;
232 PARA_INFO_LOG("using default writer: %s %p\n",
233 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
234 wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");
238 * Print the help text of all writers to stdout.
240 * \param detailed Whether to print the detailed help text.
242 void print_writer_helps(int detailed)
246 printf_or_die("\nAvailable writers: \n\t");
248 printf_or_die("%s%s", i? " " : "", writer_names[i]);
249 printf_or_die("\n\n");
251 struct writer *w = writers + i;
253 if (!w->help.short_help)
255 printf_or_die("Options for %s:\n", writer_names[i]);
256 ggo_print_help(&w->help, detailed);