2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file filter_common.c Common helper functions for filter input/output. */
10 #include <sys/types.h>
17 #include "buffer_tree.h"
22 /** Iterate over the array of supported filters. */
23 #define FOR_EACH_SUPPORTED_FILTER(j) for (j = 0; j < NUM_SUPPORTED_FILTERS; j++)
25 /** The array of supported filters. */
26 static struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
29 * Obtain a reference to a filter structure.
31 * \param filter_num Between zero and NUM_SUPPORTED_FILTERS, inclusively.
33 * \return Pointer to the filter identified by the given filter number.
35 * It is a fatal error if the given number is out of range. In this case
36 * the function aborts.
38 const struct filter *filter_get(int filter_num)
40 assert(filter_num >= 0);
41 assert(filter_num < NUM_SUPPORTED_FILTERS);
42 return filters + filter_num;
46 * Call the init function of each supported filter.
49 void filter_init(void)
53 FOR_EACH_SUPPORTED_FILTER(i)
54 filter_get(i)->init((struct filter *)filter_get(i));
58 * If the filter has a command line parser and options is not NULL, run it.
59 * Returns filter_num on success, negative on errors
61 static int parse_filter_args(int filter_num, const char *options, void **conf)
63 const struct filter *f = filter_get(filter_num);
68 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
69 argc = create_shifted_argv(options, " \t", &argv);
71 return -E_BAD_FILTER_OPTIONS;
72 argv[0] = para_strdup(f->name);
73 ret = f->parse_config(argc, argv, conf);
75 return ret < 0? ret : filter_num;
79 * Check the filter command line options.
81 * \param fa The filter argument.
82 * \param conf Points to the filter configuration upon successful return.
84 * Check if the given filter argument starts with the name of a supported
85 * filter, optionally followed by options for this filter. If yes, call the
86 * command line parser of that filter.
88 * \return On success, the number of the filter is returned and \a conf
89 * is initialized to point to the filter configuration determined by \a fa.
90 * On errors, a negative value is returned.
92 * Note: If \a fa specifies a filter that has no command line parser success is
93 * returned, and \a conf is initialized to \p NULL.
95 * \sa filter::parse_config
97 int check_filter_arg(const char *fa, void **conf)
102 // PARA_DEBUG_LOG("arg: %s\n", fa);
103 FOR_EACH_SUPPORTED_FILTER(j) {
104 const char *name = filter_get(j)->name;
105 size_t len = strlen(name);
107 if (strlen(fa) < len)
109 if (strncmp(name, fa, len))
114 if (c && !filter_get(j)->parse_config)
115 return -E_BAD_FILTER_OPTIONS;
116 return parse_filter_args(j, c? fa + len + 1 :
117 fa + strlen(fa), conf);
119 return -E_UNSUPPORTED_FILTER;
123 * Print help text of each filter to stdout.
125 * \param flags Passed to \ref ggo_print_help().
127 void print_filter_helps(unsigned flags)
131 printf_or_die("\nAvailable filters: ");
132 FOR_EACH_SUPPORTED_FILTER(i) {
134 printf_or_die("\n ");
137 num += printf_or_die("%s%s", i? " " : "", filter_get(i)->name);
141 FOR_EACH_SUPPORTED_FILTER(i) {
142 struct filter *f = (struct filter *)filter_get(i);
144 if (!f->help.short_help)
146 printf_or_die("\nOptions for %s (%s):", f->name,
148 ggo_print_help(&f->help, flags);
153 * Set select timeout of the scheduler.
155 * \param s The scheduler.
156 * \param context Pointer to the filter node (task context).
158 * This looks at the status of the btr node of the filter. If data is available
159 * in the input queue of the filter, or if an error occurred, a minimal timeout
160 * for the next select call is requested from the scheduler. Otherwise the
161 * scheduler timeout is left unchanged.
163 void generic_filter_pre_select(struct sched *s, void *context)
165 struct filter_node *fn = context;
167 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
171 #ifdef WORDS_BIGENDIAN
172 #define DECODER_SAMPLE_FORMAT SF_S16_BE
174 #define DECODER_SAMPLE_FORMAT SF_S16_LE
178 * Execute a btr command for a decoder.
180 * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
181 * channels count and sample format. This function is called by all decoders to
182 * answer these queries.
184 * \param cmd The command to be executed by the child node.
185 * \param sample_rate Known to the decoder.
186 * \param channels Known to the decoder.
187 * \param result Ascii representation on the answer is stored here.
191 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
194 if (!strcmp(cmd, "sample_rate")) {
195 if (sample_rate == 0)
196 return -E_BTR_NAVAIL;
197 *result = make_message("%u", sample_rate);
200 if (!strcmp(cmd, "channels")) {
202 return -E_BTR_NAVAIL;
203 *result = make_message("%u", channels);
206 if (!strcmp(cmd, "sample_format")) {
207 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
210 return -ERRNO_TO_PARA_ERROR(ENOTSUP);