1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file write_common.c common functions of para_audiod and para_write */
8 #include "write_cmd.lsg.h"
13 #include "buffer_tree.h"
17 /** Loop over all writers. */
18 #define FOR_EACH_WRITER(i) for (i = 1; lls_cmd(i, write_cmd_suite); i++)
21 static inline bool writer_supported(int wid)
23 return lls_user_data(WRITE_CMD(wid));
26 /* simply return the first available writer */
27 static int default_writer_id(void)
32 if (writer_supported(i))
34 assert(0); /* the file writer should always be available */
38 * Return the writer structure from a writer ID.
40 * \param wid If non-positive, a pointer to the default writer is returned.
42 * \return Pointer to a (constant) struct writer.
44 const struct writer *writer_get(int wid)
47 wid = default_writer_id();
48 return lls_user_data(WRITE_CMD(wid));
52 * Return name of the writer identified by a writer ID.
54 * \param wid If non-positive, the name of the default writer is returned.
56 * \return The returned buffer must not be freed by the caller.
58 const char *writer_name(int wid)
61 wid = default_writer_id();
62 return lls_command_name(WRITE_CMD(wid));
66 * Check if the given string is a valid command line for any writer.
68 * \param wa String of the form writer_name options.
69 * \param lprp Contains the parsed command line on success.
71 * If wa is \p NULL, the (configuration-dependent) default writer is assumed.
72 * Otherwise, the function checks whether \a wa starts with the name of a
73 * supported writer. If a valid writer name was found, the rest of the command
74 * line is passed to the config parser of this writer.
76 * \return On success, the positive writer ID is returned. Otherwise the
77 * function prints an error message and calls exit().
79 int check_writer_arg_or_die(const char *wa, struct lls_parse_result **lprp)
81 int ret, writer_num, argc;
82 char **argv = NULL, *errctx = NULL;
83 const struct lls_command *cmd;
86 writer_num = default_writer_id();
87 cmd = WRITE_CMD(writer_num);
88 argv = alloc(2 * sizeof(char *));
90 argv[0] = para_strdup(lls_command_name(cmd));
94 ret = create_argv(wa, " \t\n", &argv);
98 ret = lls(lls_lookup_subcmd(argv[0], write_cmd_suite, &errctx));
102 cmd = WRITE_CMD(writer_num);
103 if (!writer_supported(writer_num)) {
104 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
105 errctx = make_message("%s writer is not supported",
106 lls_command_name(cmd));
110 ret = lls(lls_parse(argc, argv, cmd, lprp, &errctx));
119 PARA_ERROR_LOG("%s\n", errctx);
121 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
126 * Open a writer node and register the corresponding task.
128 * \param wn The writer node to open.
129 * \param parent The parent btr node (the source for the writer node).
130 * \param s The scheduler instance to register the task to.
132 void register_writer_node(struct writer_node *wn, struct btr_node *parent,
135 const struct writer *w = writer_get(wn->wid);
137 wn->btrn = btr_new_node(&(struct btr_node_description)
138 EMBRACE(.name = writer_name(wn->wid), .parent = parent,
139 .handler = w->execute, .context = wn));
140 wn->task = task_register(&(struct task_info) {
141 .name = writer_name(wn->wid),
142 .pre_monitor = w->pre_monitor,
143 .post_monitor = w->post_monitor,
149 * Print the help text of all writers to stdout.
151 * \param detailed Whether to print the short or the detailed help.
153 void print_writer_helps(bool detailed)
157 printf("\nAvailable writers: ");
159 if (!writer_supported(i))
161 printf("%s%s", i? " " : "", writer_name(i));
165 const struct lls_command *cmd = WRITE_CMD(i);
167 if (!writer_supported(i))
169 help = detailed? lls_long_help(cmd) : lls_short_help(cmd);
172 printf("%s\n", help);
177 static int get_btr_value(struct btr_node *btrn, const char *cmd,
181 int ret = btr_exec_up(btrn, cmd, &buf);
185 * Errors may happen when the decoder returns EOF before the writer had
186 * a chance to query the buffer tree for the channel count, sample rate
191 ret = para_atoi32(buf, result);
198 * Ask parent btr nodes for the sample rate of the current stream.
200 * \param btrn Where to start the search.
201 * \param result Filled in by this function.
205 int get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
207 return get_btr_value(btrn, "sample_rate", result);
211 * Ask parent btr nodes for the channel count of the current stream.
213 * \param btrn See \ref get_btr_sample_rate.
214 * \param result See \ref get_btr_sample_rate.
218 int get_btr_channels(struct btr_node *btrn, int32_t *result)
220 return get_btr_value(btrn, "channels", result);
224 * Ask parent btr nodes for the number of bits per sample and the byte sex.
226 * \param btrn See \ref get_btr_sample_rate.
227 * \param result Contains the sample format as an enum sample_format type.
231 int get_btr_sample_format(struct btr_node *btrn, int32_t *result)
233 return get_btr_value(btrn, "sample_format", result);