9af8d682075ccf91de2bff3ff7625ab7d9f0a598
[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 /**
241  * Setup a writer node with the default writer.
242  *
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.
246  *
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
249  * parent.
250  *
251  * Finally, the new writer node's taks structure is initialized and registered
252  * to the paraslash scheduler.
253  *
254  * \return A pointer to the allocated writer node group.
255  */
256 struct writer_node *setup_writer_node(const char *arg, struct btr_node *parent)
257 {
258         struct writer_node *wn = para_calloc(sizeof(*wn));
259         struct writer *w;
260         char *name;
261
262         if (arg)
263                 wn->conf = check_writer_arg(arg, &wn->writer_num);
264         else {
265                 wn->writer_num = DEFAULT_WRITER;
266                 wn->conf = writers[DEFAULT_WRITER].parse_config("");
267         }
268         if (!wn->conf) {
269                 free(wn);
270                 return NULL;
271         }
272         w = writers + wn->writer_num;
273         name = make_message("%s writer", writer_names[wn->writer_num]);
274         wn->btrn = btr_new_node(name, parent, w->execute, wn);
275         strcpy(wn->task.status, name);
276         free(name);
277         w->open(wn);
278         wn->task.post_select = w->post_select_btr;
279         wn->task.pre_select = w->pre_select_btr;
280         register_task(&wn->task);
281         return wn;
282 }
283
284
285 /**
286  * Print the help text of all writers to stdout.
287  *
288  * \param detailed Whether to print the detailed help text.
289  */
290 void print_writer_helps(int detailed)
291 {
292         int i;
293
294         printf_or_die("\nAvailable writers: \n\t");
295         FOR_EACH_WRITER(i)
296                 printf_or_die("%s%s", i? " " : "", writer_names[i]);
297         printf_or_die("\n\n");
298         FOR_EACH_WRITER(i) {
299                 struct writer *w = writers + i;
300
301                 if (!w->help.short_help)
302                         continue;
303                 printf_or_die("Options for %s:\n", writer_names[i]);
304                 ggo_print_help(&w->help, detailed);
305         }
306 }
307
308 static int get_btr_value(struct btr_node *btrn, const char *key, int32_t *result)
309 {
310         char *buf = NULL;
311         int ret = btr_exec_up(btrn, key, &buf);
312
313         if (ret < 0)
314                 return ret;
315         ret = para_atoi32(buf, result);
316         free(buf);
317         return ret;
318 }
319
320 /*
321  * Ask parent btr nodes for the samplerate of the current stream.
322  */
323 int get_btr_samplerate(struct btr_node *btrn, int32_t *result)
324 {
325         return get_btr_value(btrn, "samplerate", result);
326 }
327
328 /*
329  * Ask parent btr nodes for the channel count of the current stream.
330  */
331 int get_btr_channels(struct btr_node *btrn, int32_t *result)
332 {
333         return get_btr_value(btrn, "channels", result);
334 }