NEWS update.
[paraslash.git] / write_common.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write_common.c common functions of para_audiod and para_write */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "string.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "write.h"
17 #include "error.h"
18
19 /** the array containing the names of all supported writers */
20 const char *writer_names[] ={WRITER_NAMES};
21
22 /** the array of supported writers */
23 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
24
25 static void wng_pre_select(struct sched *s, struct task *t)
26 {
27 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
28 int i;
29
30 FOR_EACH_WRITER_NODE(i, g) {
31 struct writer_node *wn = &g->writer_nodes[i];
32 struct writer *w = writers + wn->writer_num;
33 if (!w->pre_select)
34 continue;
35 t->error = w->pre_select(s, wn);
36 if (t->error < 0)
37 return;
38 }
39 /*
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.
46 */
47 if (g->last_written == 0)
48 return;
49 s->timeout.tv_sec = 0;
50 s->timeout.tv_usec = 1;
51 }
52
53 static void wng_post_select(struct sched *s, struct task *t)
54 {
55 struct writer_node_group *g = container_of(t, struct writer_node_group, task);
56 int i;
57 size_t min_written = 0, max_written = 0;
58
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);
63 if (t->error < 0)
64 return;
65 if (!i)
66 min_written = wn->written;
67 else
68 min_written = PARA_MIN(min_written, wn->written);
69 max_written = PARA_MAX(max_written, wn->written);
70 }
71 g->last_written = max_written;
72 //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
73 if (min_written) {
74 *g->loaded -= min_written;
75 FOR_EACH_WRITER_NODE(i, g)
76 g->writer_nodes[i].written -= min_written;
77 }
78 if (!*g->loaded && *g->input_error) {
79 t->error = *g->input_error;
80 return;
81 }
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);
85 }
86 }
87
88 /**
89 * call the open function of each writer in the group
90 *
91 * \param g the writer node group
92 *
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.
97 * */
98 int wng_open(struct writer_node_group *g)
99 {
100 int i, ret = 1;
101
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;
106 wn->wng = g;
107 ret = w->open(wn);
108 if (ret < 0)
109 goto err_out;
110 }
111 sprintf(g->task.status, "%s", "writer node group");
112 register_task(&g->task);
113 g->open = 1;
114 return 1;
115 err_out:
116 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
117 while (i > 0) {
118 struct writer_node *wn = &g->writer_nodes[--i];
119 struct writer *w = writers + wn->writer_num;
120 w->close(wn);
121 }
122 free(g->writer_nodes);
123 g->num_writers = 0;
124 g->task.error = -E_TASK_UNREGISTERED;
125 return ret;
126 }
127
128 /**
129 * call the close function of each writer in the given group
130 *
131 * \param g the writer node group to close
132 *
133 * This function also frees all resources of the given group.
134 */
135 void wng_close(struct writer_node_group *g)
136 {
137 int i;
138
139 if (!g || !g->open)
140 return;
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;
145 w->close(wn);
146 }
147 free(g->writer_nodes);
148 free(g);
149 }
150
151 /**
152 * allocate and initialize a new writer_node_group struct
153 *
154 * \param num_writers the number of writer nodes for the new group
155 *
156 * \return Pointer to the new writer node group
157 */
158 struct writer_node_group *wng_new(unsigned num_writers)
159 {
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;
166 return g;
167 }
168
169 /**
170 * Call the init function of each supported paraslash writer.
171 */
172 void writer_init(void)
173 {
174 int i;
175
176 FOR_EACH_WRITER(i)
177 writers[i].init(&writers[i]);
178 }
179 /**
180 * check if given string is a valid command line for any writer
181 *
182 * \param \wa string of the form writer_name:options
183 * \param writer_num contains the number of the writer upon success
184 *
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.
189 *
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
192 * returns \p NULL.
193 */
194 void *check_writer_arg(const char *wa, int *writer_num)
195 {
196 int i;
197
198 *writer_num = -E_WRITE_COMMON_SYNTAX;
199 PARA_INFO_LOG("checking %s\n", wa);
200 FOR_EACH_WRITER(i) {
201 const char *name = writer_names[i];
202 size_t len = strlen(name);
203 char c;
204 if (strlen(wa) < len)
205 continue;
206 if (strncmp(name, wa, len))
207 continue;
208 c = wa[len];
209 if (c && c != ' ')
210 continue;
211 if (c && !writers[i].parse_config)
212 return NULL;
213 *writer_num = i;
214 return writers[i].parse_config(c? wa + len + 1 : "");
215 }
216 PARA_ERROR_LOG("writer not found\n");
217 return NULL;
218 }
219
220 /**
221 * setup a writer node group with only one writer, the default writer
222 *
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.
225 *
226 * \return pointer to the allocated writer node group
227 */
228 struct writer_node_group *setup_default_wng(void)
229 {
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("");
235 return wng;
236 }
237 /**
238 * Print the help text of all writers to stdout.
239 *
240 * \param detailed Whether to print the detailed help text.
241 */
242 void print_writer_helps(int detailed)
243 {
244 int i;
245
246 printf_or_die("\nAvailable writers: \n\t");
247 FOR_EACH_WRITER(i)
248 printf_or_die("%s%s", i? " " : "", writer_names[i]);
249 printf_or_die("\n\n");
250 FOR_EACH_WRITER(i) {
251 struct writer *w = writers + i;
252
253 if (!w->help.short_help)
254 continue;
255 printf_or_die("Options for %s:\n", writer_names[i]);
256 ggo_print_help(&w->help, detailed);
257 }
258 }