2 * Copyright (C) 2006-2008 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 */
16 /** the array containing the names of all supported writers */
17 const char *writer_names[] ={WRITER_NAMES};
19 /** the array of supported writers */
20 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
22 static void wng_pre_select(__a_unused struct sched *s, struct task *t)
24 struct writer_node_group *g = t->private_data;
27 FOR_EACH_WRITER_NODE(i, g) {
28 struct writer_node *wn = &g->writer_nodes[i];
29 t->ret = wn->writer->pre_select(s, wn);
37 static void wng_post_select(struct sched *s, struct task *t)
39 struct writer_node_group *g = t->private_data;
41 size_t min_written = 0;
43 FOR_EACH_WRITER_NODE(i, g) {
44 struct writer_node *wn = &g->writer_nodes[i];
45 t->ret = wn->writer->post_select(s, wn);
51 min_written = wn->written;
53 min_written = PARA_MIN(min_written, wn->written);
55 // PARA_INFO_LOG("loaded: %zd, min_written: %zd bytes\n", *g->loaded, min_written);
57 *g->loaded -= min_written;
58 FOR_EACH_WRITER_NODE(i, g)
59 g->writer_nodes[i].written -= min_written;
61 if (!*g->loaded && *g->input_error) {
62 g->error = *g->input_error;
67 if (*g->loaded && min_written) {
68 // PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
69 memmove(g->buf, g->buf + min_written, *g->loaded);
74 * call the open function of each writer in the group
76 * \param g the writer node group
78 * \return If at least one open function returned an error, all successful
79 * writer notes get closed and this error value is returned. Upon success, a
80 * task associated with \a g is registered to the scheduler and the function
81 * returns a positive value.
83 int wng_open(struct writer_node_group *g)
87 PARA_NOTICE_LOG("opening wng %p with %d writer(s)\n", g, g->num_writers);
88 register_task(&g->task);
89 FOR_EACH_WRITER_NODE(i, g) {
90 struct writer_node *wn = &g->writer_nodes[i];
92 ret = wn->writer->open(wn);
95 wn->chunk_bytes = ret;
96 g->max_chunk_bytes = PARA_MAX(g->max_chunk_bytes, ret);
98 sprintf(g->task.status, "%s", "writer node group");
102 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
103 unregister_task(&g->task);
105 struct writer_node *wn = &g->writer_nodes[--i];
106 wn->writer->close(wn);
115 * unregister a writer node group task
117 * \param g the group whose task is to be closed
119 void wng_unregister(struct writer_node_group *g)
121 unregister_task(&g->task);
125 * call the close function of each writer in the given group
127 * \param g the writer node group to close
129 * This function also frees all resources of the given group.
131 void wng_close(struct writer_node_group *g)
137 PARA_NOTICE_LOG("closing wng with %d writer(s)\n", g->num_writers);
138 FOR_EACH_WRITER_NODE(i, g) {
139 struct writer_node *wn = &g->writer_nodes[i];
140 wn->writer->close(wn);
142 free(g->writer_nodes);
147 * allocate and initialize a new writer_node_group struct
149 * \param num_writers the number of writer nodes for the new group
151 * \return Pointer to the new writer node group
153 struct writer_node_group *wng_new(unsigned num_writers)
155 struct writer_node_group *g = para_calloc(sizeof(struct writer_node_group));
156 g->num_writers = num_writers;
157 g->writer_nodes = para_calloc(num_writers
158 * sizeof(struct writer_node));
159 g->task.private_data = g;
160 g->task.post_select = wng_post_select;
161 g->task.pre_select = wng_pre_select;
166 * call the init function of each supported paraslash writer
168 void init_supported_writers(void)
173 writers[i].init(&writers[i]);
176 * check if given string is a valid command line for any writer
178 * \param \wa string of the form writer_name:options
179 * \param writer_num contains the number of the writer upon success
181 * This function checks whether \a wa starts with the name of a supported
182 * paraslash writer, optionally followed by a colon and any options for that
183 * writer. If a valid writer name was found and further are present, the
184 * remaining part of \a wa is passed to that writer's config parser.
186 * \return On success, a pointer to the gengetopt args info struct is returned
187 * and \a writer_num contains the number of the writer. Otherwise this function
190 void *check_writer_arg(const char *wa, int *writer_num)
194 *writer_num = -E_WRITE_COMMON_SYNTAX;
195 PARA_INFO_LOG("checking %s\n", wa);
197 const char *name = writer_names[i];
198 size_t len = strlen(name);
200 if (strlen(wa) < len)
202 if (strncmp(name, wa, len))
207 if (c && !writers[i].parse_config)
210 return writers[i].parse_config(c? wa + len + 1 : "");
212 PARA_ERROR_LOG("%s", "writer not found\n");
217 * setup a writer node group with only one writer, the default writer
219 * The writer which is set up depends on the OS. It defaults to alsa for Linux,
220 * osx_write for OS X, file writer if neither of these is supported.
222 * \return pointer to the allocated writer node group
224 struct writer_node_group *setup_default_wng(void)
226 struct writer_node_group *wng = wng_new(1);
227 wng->writer_nodes[0].writer = &writers[DEFAULT_WRITER];
228 PARA_INFO_LOG("using default writer: %s %p\n",
229 writer_names[DEFAULT_WRITER], writers[DEFAULT_WRITER].parse_config);
230 wng->writer_nodes[0].conf = writers[DEFAULT_WRITER].parse_config("");