string: constant value
[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 "para.h"
10 #include "string.h"
11 #include "list.h"
12 #include "sched.h"
13 #include "ggo.h"
14 #include "write.h"
15 #include "error.h"
16
17 /** the array containing the names of all supported writers */
18 const char *writer_names[] ={WRITER_NAMES};
19
20 /** the array of supported writers */
21 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
22
23 static void wng_pre_select(struct sched *s, struct task *t)
24 {
25         struct writer_node_group *g = container_of(t, struct writer_node_group, task);
26         int i;
27
28         FOR_EACH_WRITER_NODE(i, g) {
29                 struct writer_node *wn = &g->writer_nodes[i];
30                 struct writer *w = writers + wn->writer_num;
31                 if (!w->pre_select)
32                         continue;
33                 t->error = w->pre_select(s, wn);
34                 if (t->error < 0)
35                         return;
36         }
37         /*
38          * Force a minimal delay if something was written during the previous
39          * call to wng_post_select(). This is necessary because the filter
40          * chain might still have data for us which it couldn't convert during
41          * the previous run due to its buffer size constraints. In this case we
42          * do not want to wait until the next input data arrives as this could
43          * lead to buffer underruns.
44          */
45         if (g->last_written == 0)
46                 return;
47         s->timeout.tv_sec = 0;
48         s->timeout.tv_usec = 1;
49 }
50
51 static void wng_post_select(struct sched *s, struct task *t)
52 {
53         struct writer_node_group *g = container_of(t, struct writer_node_group, task);
54         int i;
55         size_t min_written = 0, max_written = 0;
56
57         FOR_EACH_WRITER_NODE(i, g) {
58                 struct writer_node *wn = &g->writer_nodes[i];
59                 struct writer *w = writers + wn->writer_num;
60                 t->error = w->post_select(s, wn);
61                 if (t->error < 0)
62                         return;
63                 if (!i)
64                         min_written = wn->written;
65                 else
66                         min_written = PARA_MIN(min_written, wn->written);
67                 max_written = PARA_MAX(max_written, wn->written);
68         }
69         g->last_written = max_written;
70         //PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
71         if (min_written) {
72                 *g->loaded -= min_written;
73                 FOR_EACH_WRITER_NODE(i, g)
74                         g->writer_nodes[i].written -= min_written;
75         }
76         if (!*g->loaded && *g->input_error) {
77                 t->error = *g->input_error;
78                 return;
79         }
80         if (*g->loaded && min_written) {
81 //              PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
82                 memmove(*g->bufp, *g->bufp + min_written, *g->loaded);
83         }
84 }
85
86 /**
87  * call the open function of each writer in the group
88  *
89  * \param g the writer node group
90  *
91  * \return If at least one open function returned an error, all successful
92  * writer notes get closed and this error value is returned. Upon success, a
93  * task associated with \a g is registered to the scheduler and the function
94  * returns a positive value.
95  * */
96 int wng_open(struct writer_node_group *g)
97 {
98         int i, ret = 1;
99
100         PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
101         FOR_EACH_WRITER_NODE(i, g) {
102                 struct writer_node *wn = &g->writer_nodes[i];
103                 struct writer *w = writers + wn->writer_num;
104                 wn->wng = g;
105                 ret = w->open(wn);
106                 if (ret < 0)
107                         goto err_out;
108         }
109         sprintf(g->task.status, "%s", "writer node group");
110         register_task(&g->task);
111         g->open = 1;
112         return 1;
113 err_out:
114         PARA_ERROR_LOG("%s\n", para_strerror(-ret));
115         while (i > 0) {
116                 struct writer_node *wn = &g->writer_nodes[--i];
117                 struct writer *w = writers + wn->writer_num;
118                 w->close(wn);
119         }
120         free(g->writer_nodes);
121         g->num_writers = 0;
122         g->task.error = -E_TASK_UNREGISTERED;
123         return ret;
124 }
125
126 /**
127  * call the close function of each writer in the given group
128  *
129  * \param g the writer node group to close
130  *
131  * This function also frees all resources of the given group.
132  */
133 void wng_close(struct writer_node_group *g)
134 {
135         int i;
136
137         if (!g || !g->open)
138                 return;
139         PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
140         FOR_EACH_WRITER_NODE(i, g) {
141                 struct writer_node *wn = &g->writer_nodes[i];
142                 struct writer *w = writers + wn->writer_num;
143                 w->close(wn);
144         }
145         free(g->writer_nodes);
146         free(g);
147 }
148
149 /**
150  * allocate and initialize a new writer_node_group struct
151  *
152  * \param num_writers the number of writer nodes for the new group
153  *
154  * \return Pointer to the new writer node group
155  */
156 struct writer_node_group *wng_new(unsigned num_writers)
157 {
158         struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
159         g->num_writers = num_writers;
160         g->writer_nodes = para_calloc(num_writers
161                 * sizeof(struct writer_node));
162         g->task.post_select = wng_post_select;
163         g->task.pre_select = wng_pre_select;
164         return g;
165 }
166
167 /**
168  * Call the init function of each supported paraslash writer.
169  */
170 void writer_init(void)
171 {
172         int i;
173
174         FOR_EACH_WRITER(i)
175                 writers[i].init(&writers[i]);
176 }
177 /**
178  * check if given string is a valid command line for any writer
179  *
180  * \param \wa string of the form writer_name:options
181  * \param writer_num contains the number of the writer upon success
182  *
183  * This function checks whether \a wa starts with the name of a supported
184  * paraslash writer, optionally followed by a colon and any options for that
185  * writer.  If a valid writer name was found and further are present, the
186  * remaining part of \a wa is passed to that writer's config parser.
187  *
188  * \return On success, a pointer to the gengetopt args info struct is returned
189  * and \a writer_num contains the number of the writer. Otherwise this function
190  * returns \p NULL.
191  */
192 void *check_writer_arg(const char *wa, int *writer_num)
193 {
194         int i;
195
196         *writer_num = -E_WRITE_COMMON_SYNTAX;
197         PARA_INFO_LOG("checking  %s\n", wa);
198         FOR_EACH_WRITER(i) {
199                 const char *name = writer_names[i];
200                 size_t len = strlen(name);
201                 char c;
202                 if (strlen(wa) < len)
203                         continue;
204                 if (strncmp(name, wa, len))
205                         continue;
206                 c = wa[len];
207                 if (c && c != ' ')
208                         continue;
209                 if (c && !writers[i].parse_config)
210                         return NULL;
211                 *writer_num = i;
212                 return writers[i].parse_config(c? wa + len + 1 : "");
213         }
214         PARA_ERROR_LOG("writer not found\n");
215         return NULL;
216 }
217
218 /**
219  * setup a writer node group with only one writer, the default writer
220  *
221  * The writer which is set up depends on the OS. It defaults to alsa for Linux,
222  * osx_write for OS X, file writer if neither of these is supported.
223  *
224  * \return pointer to the allocated writer node group
225  */
226 struct writer_node_group *setup_default_wng(void)
227 {
228         struct writer_node_group *wng = wng_new(1);
229         wng->writer_nodes[0].writer_num = DEFAULT_WRITER;
230         PARA_INFO_LOG("using default writer: %s %p\n",
231                 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
232         wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");
233         return wng;
234 }
235 /**
236  * Print the help text of all writers to stdout.
237  *
238  * \param detailed Whether to print the detailed help text.
239  */
240 void print_writer_helps(int detailed)
241 {
242         int i;
243
244         printf_or_die("\nAvailable writers: \n\t");
245         FOR_EACH_WRITER(i)
246                 printf_or_die("%s%s", i? " " : "", writer_names[i]);
247         printf_or_die("\n\n");
248         FOR_EACH_WRITER(i) {
249                 struct writer *w = writers + i;
250
251                 if (!w->help.short_help)
252                         continue;
253                 printf_or_die("Options for %s:\n", writer_names[i]);
254                 ggo_print_help(&w->help, detailed);
255         }
256 }