2 * Copyright (C) 2006-2011 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 */
17 #include "buffer_tree.h"
21 /** the array containing the names of all supported writers */
22 const char *writer_names[] ={WRITER_NAMES};
24 /** the array of supported writers */
25 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
28 * Call the init function of each supported paraslash writer.
30 void writer_init(void)
35 writers[i].init(&writers[i]);
38 * Check if given string is a valid command line for any writer.
40 * \param \wa String of the form writer_name:options.
41 * \param writer_num Contains the number of the writer upon success.
43 * This function checks whether \a wa starts with the name of a supported
44 * paraslash writer, optionally followed by a colon and any options for that
45 * writer. If a valid writer name was found and further are present, the
46 * remaining part of \a wa is passed to that writer's config parser.
48 * \return On success, a pointer to the gengetopt args info struct is returned
49 * and \a writer_num contains the number of the writer. Otherwise this function
52 void *check_writer_arg(const char *wa, int *writer_num)
56 *writer_num = -E_WRITE_COMMON_SYNTAX;
57 PARA_INFO_LOG("checking %s\n", wa);
59 const char *name = writer_names[i];
60 size_t len = strlen(name);
64 if (strncmp(name, wa, len))
70 return writers[i].parse_config_or_die(c? wa + len + 1 : "");
72 PARA_ERROR_LOG("writer not found\n");
77 * Open a writer node and register the corresponding task.
79 * \param wn The writer node to open.
80 * \param parent The parent btr node (the source for the writer node).
82 * The configuration of the writer node stored in \p wn->conf must be
83 * initialized before this function may be called.
85 void register_writer_node(struct writer_node *wn, struct btr_node *parent)
87 struct writer *w = writers + wn->writer_num;
88 char *name = make_message("%s writer", writer_names[wn->writer_num]);
90 wn->btrn = btr_new_node(&(struct btr_node_description)
91 EMBRACE(.name = name, .parent = parent,
92 .handler = w->execute, .context = wn));
93 strcpy(wn->task.status, name);
95 wn->task.post_select = w->post_select;
96 wn->task.pre_select = w->pre_select;
97 register_task(&wn->task);
101 * Parse config and register a task for a writer node.
103 * \param arg Command line arguments.
104 * \param parent The new node will be a child of \a parent.
105 * \param wn The writer node.
107 * If arg is \p NULL, the OS-dependent default writer is used with no
108 * arguments. The default writers are alsa for Linux, osx for OS X, oss for
109 * *BSD, and the file writer if the default writer is not supported.
111 * Once the writer configuration has been retrieved from the ->parse_config
112 * callback a writer node is created, its buffer tree node is added to the
113 * buffer tree as a child of the given parent.
115 * Finally, the new writer node's task structure is initialized and registered
116 * to the paraslash scheduler.
120 int setup_writer_node(const char *arg, struct btr_node *parent,
121 struct writer_node *wn)
124 wn->conf = check_writer_arg(arg, &wn->writer_num);
126 wn->writer_num = DEFAULT_WRITER;
127 wn->conf = writers[DEFAULT_WRITER].parse_config_or_die("");
130 return -E_WRITE_COMMON_SYNTAX;
131 register_writer_node(wn, parent);
136 * Print the help text of all writers to stdout.
138 * \param detailed Whether to print the detailed help text.
140 void print_writer_helps(int detailed)
144 printf_or_die("\nAvailable writers: \n\t");
146 printf_or_die("%s%s", i? " " : "", writer_names[i]);
147 printf_or_die("\n\n");
149 struct writer *w = writers + i;
151 if (!w->help.short_help)
153 printf_or_die("Options for %s:\n", writer_names[i]);
154 ggo_print_help(&w->help, detailed);
158 static void get_btr_value(struct btr_node *btrn, const char *cmd,
162 int ret = btr_exec_up(btrn, cmd, &buf);
166 * This really should not happen. It means one of our parent
167 * nodes died unexpectedly. Proceed with fingers crossed.
169 PARA_CRIT_LOG("cmd %s: %s\n", cmd, para_strerror(-ret));
173 ret = para_atoi32(buf, result);
179 * Ask parent btr nodes for the sample rate of the current stream.
181 * \param btrn Where to start the search.
182 * \param result Filled in by this function.
184 * This function is assumed to succeed and terminates on errors.
186 void get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
188 get_btr_value(btrn, "sample_rate", result);
192 * Ask parent btr nodes for the channel count of the current stream.
194 * \param btrn See \ref get_btr_sample_rate.
195 * \param result See \ref get_btr_sample_rate.
197 void get_btr_channels(struct btr_node *btrn, int32_t *result)
199 get_btr_value(btrn, "channels", result);
203 * Ask parent btr nodes for the number of bits per sample and the byte sex.
205 * \param btrn See \ref get_btr_sample_rate.
206 * \param result Contains the sample format as an enum sample_format type.
208 void get_btr_sample_format(struct btr_node *btrn, int32_t *result)
210 get_btr_value(btrn, "sample_format", result);