alsa: Ignore errors from the btr query.
[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 #include <stdbool.h>
11
12 #include "para.h"
13 #include "string.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "write.h"
19 #include "error.h"
20
21 /** the array containing the names of all supported writers */
22 const char *writer_names[] ={WRITER_NAMES};
23
24 /** the array of supported writers */
25 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
26
27 static void wng_pre_select(struct sched *s, struct task *t)
28 {
29         struct writer_node_group *g = container_of(t, struct writer_node_group, task);
30         int i;
31
32         FOR_EACH_WRITER_NODE(i, g) {
33                 struct writer_node *wn = &g->writer_nodes[i];
34                 struct writer *w = writers + wn->writer_num;
35                 if (!w->pre_select)
36                         continue;
37                 t->error = w->pre_select(s, wn);
38                 if (t->error < 0)
39                         return;
40         }
41         /*
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.
48          */
49         if (g->last_written == 0)
50                 return;
51         s->timeout.tv_sec = 0;
52         s->timeout.tv_usec = 1;
53 }
54
55 static void wng_post_select(struct sched *s, struct task *t)
56 {
57         struct writer_node_group *g = container_of(t, struct writer_node_group, task);
58         int i;
59         size_t min_written = 0, max_written = 0;
60
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);
65                 if (t->error < 0)
66                         return;
67                 if (!i)
68                         min_written = wn->written;
69                 else
70                         min_written = PARA_MIN(min_written, wn->written);
71                 max_written = PARA_MAX(max_written, wn->written);
72         }
73         g->last_written = max_written;
74         //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
75         if (min_written) {
76                 *g->loaded -= min_written;
77                 FOR_EACH_WRITER_NODE(i, g)
78                         g->writer_nodes[i].written -= min_written;
79         }
80         if (!*g->loaded && *g->input_error) {
81                 t->error = *g->input_error;
82                 return;
83         }
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);
87         }
88 }
89
90 /**
91  * call the open function of each writer in the group
92  *
93  * \param g the writer node group
94  *
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.
99  * */
100 int wng_open(struct writer_node_group *g)
101 {
102         int i, ret = 1;
103
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;
108                 wn->wng = g;
109                 ret = w->open(wn);
110                 if (ret < 0)
111                         goto err_out;
112         }
113         sprintf(g->task.status, "%s", "writer node group");
114         register_task(&g->task);
115         g->open = 1;
116         return 1;
117 err_out:
118         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
119         while (i > 0) {
120                 struct writer_node *wn = &g->writer_nodes[--i];
121                 struct writer *w = writers + wn->writer_num;
122                 w->close(wn);
123         }
124         free(g->writer_nodes);
125         g->num_writers = 0;
126         g->task.error = -E_TASK_UNREGISTERED;
127         return ret;
128 }
129
130 /**
131  * call the close function of each writer in the given group
132  *
133  * \param g the writer node group to close
134  *
135  * This function also frees all resources of the given group.
136  */
137 void wng_close(struct writer_node_group *g)
138 {
139         int i;
140
141         if (!g || !g->open)
142                 return;
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;
147                 w->close(wn);
148         }
149         free(g->writer_nodes);
150         free(g);
151 }
152
153 /**
154  * allocate and initialize a new writer_node_group struct
155  *
156  * \param num_writers the number of writer nodes for the new group
157  *
158  * \return Pointer to the new writer node group
159  */
160 struct writer_node_group *wng_new(unsigned num_writers)
161 {
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;
168         return g;
169 }
170
171 /**
172  * Call the init function of each supported paraslash writer.
173  */
174 void writer_init(void)
175 {
176         int i;
177
178         FOR_EACH_WRITER(i)
179                 writers[i].init(&writers[i]);
180 }
181 /**
182  * check if given string is a valid command line for any writer
183  *
184  * \param \wa string of the form writer_name:options
185  * \param writer_num contains the number of the writer upon success
186  *
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.
191  *
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
194  * returns \p NULL.
195  */
196 void *check_writer_arg(const char *wa, int *writer_num)
197 {
198         int i;
199
200         *writer_num = -E_WRITE_COMMON_SYNTAX;
201         PARA_INFO_LOG("checking  %s\n", wa);
202         FOR_EACH_WRITER(i) {
203                 const char *name = writer_names[i];
204                 size_t len = strlen(name);
205                 char c;
206                 if (strlen(wa) < len)
207                         continue;
208                 if (strncmp(name, wa, len))
209                         continue;
210                 c = wa[len];
211                 if (c && c != ' ')
212                         continue;
213                 if (c && !writers[i].parse_config)
214                         return NULL;
215                 *writer_num = i;
216                 return writers[i].parse_config(c? wa + len + 1 : "");
217         }
218         PARA_ERROR_LOG("writer not found\n");
219         return NULL;
220 }
221
222 /**
223  * setup a writer node group with only one writer, the default writer
224  *
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.
227  *
228  * \return pointer to the allocated writer node group
229  */
230 struct writer_node_group *setup_default_wng(void)
231 {
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("");
237         return wng;
238 }
239
240 void register_writer_node(struct writer_node *wn, struct btr_node *parent)
241 {
242         struct writer *w = writers + wn->writer_num;
243         char *name = make_message("%s writer", writer_names[wn->writer_num]);
244         int ret;
245
246         wn->btrn = btr_new_node(name, parent, w->execute, wn);
247         strcpy(wn->task.status, name);
248         free(name);
249         ret = w->open(wn);
250         wn->task.post_select = w->post_select_btr;
251         wn->task.pre_select = w->pre_select_btr;
252         register_task(&wn->task);
253 }
254
255 /**
256  * Setup a writer node with the default writer.
257  *
258  * If arg is \p NULL, the OS-dependent default writer is used with an empty
259  * configuration string.  It defaults to alsa for Linux, osx for OS X, oss for
260  * *BSD and the file writer if neither of these is supported.
261  *
262  * Once the writer configuration has been retrieved, a writer node is created,
263  * its buffer tree node is added to the buffer tree as a child of the given
264  * parent.
265  *
266  * Finally, the new writer node's taks structure is initialized and registered
267  * to the paraslash scheduler.
268  *
269  * \return A pointer to the allocated writer node group.
270  */
271 int setup_writer_node(const char *arg, struct btr_node *parent,
272                 struct writer_node *wn)
273 {
274         if (arg)
275                 wn->conf = check_writer_arg(arg, &wn->writer_num);
276         else {
277                 wn->writer_num = DEFAULT_WRITER;
278                 wn->conf = writers[DEFAULT_WRITER].parse_config("");
279         }
280         if (!wn->conf)
281                 return -E_WRITE_COMMON_SYNTAX;
282         register_writer_node(wn, parent);
283         return 1;
284 }
285
286
287 /**
288  * Print the help text of all writers to stdout.
289  *
290  * \param detailed Whether to print the detailed help text.
291  */
292 void print_writer_helps(int detailed)
293 {
294         int i;
295
296         printf_or_die("\nAvailable writers: \n\t");
297         FOR_EACH_WRITER(i)
298                 printf_or_die("%s%s", i? " " : "", writer_names[i]);
299         printf_or_die("\n\n");
300         FOR_EACH_WRITER(i) {
301                 struct writer *w = writers + i;
302
303                 if (!w->help.short_help)
304                         continue;
305                 printf_or_die("Options for %s:\n", writer_names[i]);
306                 ggo_print_help(&w->help, detailed);
307         }
308 }
309
310 static int get_btr_value(struct btr_node *btrn, const char *key, int32_t *result)
311 {
312         char *buf = NULL;
313         int ret = btr_exec_up(btrn, key, &buf);
314
315         if (ret < 0)
316                 return ret;
317         ret = para_atoi32(buf, result);
318         free(buf);
319         return ret;
320 }
321
322 /*
323  * Ask parent btr nodes for the samplerate of the current stream.
324  */
325 int get_btr_samplerate(struct btr_node *btrn, int32_t *result)
326 {
327         return get_btr_value(btrn, "samplerate", result);
328 }
329
330 /*
331  * Ask parent btr nodes for the channel count of the current stream.
332  */
333 int get_btr_channels(struct btr_node *btrn, int32_t *result)
334 {
335         return get_btr_value(btrn, "channels", result);
336 }