2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write_common.c common functions of para_audiod and para_write */
12 #include "write_cmd.lsg.h"
17 #include "buffer_tree.h"
21 /** Loop over all writers. */
22 #define FOR_EACH_WRITER(i) for (i = 1; lls_cmd(i, write_cmd_suite); i++)
25 static inline bool writer_supported(int wid)
27 return lls_user_data(WRITE_CMD(wid));
30 /* simply return the first available writer */
31 static int default_writer_id(void)
36 if (writer_supported(i))
38 assert(0); /* the file writer should always be available */
42 * Return the writer structure from a writer ID.
44 * \param wid If non-positive, a pointer to the default writer is returned.
46 * \return Pointer to a (constant) struct writer.
48 const struct writer *writer_get(int wid)
51 wid = default_writer_id();
52 return lls_user_data(WRITE_CMD(wid));
56 * Return name of the writer identified by a writer ID.
58 * \param wid If non-positive, the name of the default writer is returned.
60 * \return The returned buffer must not be freed by the caller.
62 const char *writer_name(int wid)
65 wid = default_writer_id();
66 return lls_command_name(WRITE_CMD(wid));
70 * Check if the given string is a valid command line for any writer.
72 * \param wa String of the form writer_name options.
73 * \param lprp Contains the parsed command line on success.
75 * If wa is \p NULL, the (configuration-dependent) default writer is assumed.
76 * Otherwise, the function checks whether \a wa starts with the name of a
77 * supported writer. If a valid writer name was found, the rest of the command
78 * line is passed to the config parser of this writer.
80 * \return On success, the positive writer ID is returned. Otherwise the
81 * function prints an error message and calls exit().
83 int check_writer_arg_or_die(const char *wa, struct lls_parse_result **lprp)
85 int ret, writer_num, argc;
86 char **argv = NULL, *errctx = NULL;
87 const struct lls_command *cmd;
90 writer_num = default_writer_id();
91 cmd = WRITE_CMD(writer_num);
92 argv = para_malloc(2 * sizeof(char *));
94 argv[0] = para_strdup(lls_command_name(cmd));
98 ret = create_argv(wa, " \t\n", &argv);
102 ret = lls(lls_lookup_subcmd(argv[0], write_cmd_suite, &errctx));
106 cmd = WRITE_CMD(writer_num);
107 if (!writer_supported(writer_num)) {
108 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
109 errctx = make_message("%s writer is not supported",
110 lls_command_name(cmd));
114 ret = lls(lls_parse(argc, argv, cmd, lprp, &errctx));
123 PARA_ERROR_LOG("%s\n", errctx);
125 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
130 * Open a writer node and register the corresponding task.
132 * \param wn The writer node to open.
133 * \param parent The parent btr node (the source for the writer node).
134 * \param s The scheduler instance to register the task to.
136 void register_writer_node(struct writer_node *wn, struct btr_node *parent,
139 const struct writer *w = writer_get(wn->wid);
141 wn->btrn = btr_new_node(&(struct btr_node_description)
142 EMBRACE(.name = writer_name(wn->wid), .parent = parent,
143 .handler = w->execute, .context = wn));
144 wn->task = task_register(&(struct task_info) {
145 .name = writer_name(wn->wid),
146 .pre_select = w->pre_select,
147 .post_select = w->post_select,
153 * Print the help text of all writers to stdout.
155 * \param detailed Whether to print the short or the detailed help.
157 void print_writer_helps(bool detailed)
161 printf("\nAvailable writers: ");
163 if (!writer_supported(i))
165 printf("%s%s", i? " " : "", writer_name(i));
169 const struct lls_command *cmd = WRITE_CMD(i);
171 if (!writer_supported(i))
173 help = detailed? lls_long_help(cmd) : lls_short_help(cmd);
176 printf("%s\n", help);
181 static void get_btr_value(struct btr_node *btrn, const char *cmd,
185 int ret = btr_exec_up(btrn, cmd, &buf);
189 * This really should not happen. It means one of our parent
190 * nodes died unexpectedly. Proceed with fingers crossed.
192 PARA_CRIT_LOG("cmd %s: %s\n", cmd, para_strerror(-ret));
196 ret = para_atoi32(buf, result);
202 * Ask parent btr nodes for the sample rate of the current stream.
204 * \param btrn Where to start the search.
205 * \param result Filled in by this function.
207 * This function is assumed to succeed and terminates on errors.
209 void get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
211 get_btr_value(btrn, "sample_rate", result);
215 * Ask parent btr nodes for the channel count of the current stream.
217 * \param btrn See \ref get_btr_sample_rate.
218 * \param result See \ref get_btr_sample_rate.
220 void get_btr_channels(struct btr_node *btrn, int32_t *result)
222 get_btr_value(btrn, "channels", result);
226 * Ask parent btr nodes for the number of bits per sample and the byte sex.
228 * \param btrn See \ref get_btr_sample_rate.
229 * \param result Contains the sample format as an enum sample_format type.
231 void get_btr_sample_format(struct btr_node *btrn, int32_t *result)
233 get_btr_value(btrn, "sample_format", result);