2 * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
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>
18 #include "buffer_tree.h"
23 /** The array of supported filters. */
24 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
27 * Call the init function of each supported filter.
30 void filter_init(void)
34 FOR_EACH_SUPPORTED_FILTER(i)
35 filters[i].init(filters + i);
39 * If the filter has a command line parser and options is not NULL, run it.
40 * Returns filter_num on success, negative on errors
42 static int parse_filter_args(int filter_num, char *options, void **conf)
44 struct filter *f = &filters[filter_num];
48 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
49 // options? options : "(none)", f->parse_config);
51 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
52 // PARA_DEBUG_LOG("options: %s\n", options);
53 argc = create_argv(options, " \t", &argv);
55 return -E_BAD_FILTER_OPTIONS;
56 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
57 for (i = argc - 1; i >= 0; i--)
58 argv[i + 1] = argv[i];
59 argv[0] = para_strdup(f->name);
61 ret = f->parse_config(argc, argv, conf);
63 argv[argc - 1] = NULL;
65 return ret < 0? ret : filter_num;
69 * Check the filter command line options.
71 * \param fa The command line options.
72 * \param conf Points to the filter configuration upon successful return.
74 * Check if \a fa starts with a the name of a supported filter, followed by
75 * a colon. If yes, call the command line parser of that filter.
77 * \return On success, the number of the filter is returned and \a conf
78 * is initialized to point to the filter configuration determined by \a fa.
79 * On errors, a negative value is returned.
81 * Note: If \a fa specifies a filter that has no command line parser success is
82 * returned, and \a conf is initialized to \p NULL.
84 * \sa filter::parse_config
86 int check_filter_arg(char *fa, void **conf)
91 // PARA_DEBUG_LOG("arg: %s\n", fa);
92 FOR_EACH_SUPPORTED_FILTER(j) {
93 const char *name = filters[j].name;
94 size_t len = strlen(name);
98 if (strncmp(name, fa, len))
103 if (c && !filters[j].parse_config)
104 return -E_BAD_FILTER_OPTIONS;
105 return parse_filter_args(j, c? fa + len + 1 :
106 fa + strlen(fa), conf);
108 return -E_UNSUPPORTED_FILTER;
111 void print_filter_helps(int detailed)
115 printf_or_die("\nAvailable filters: \n\t");
116 FOR_EACH_SUPPORTED_FILTER(i)
117 printf_or_die("%s%s", i? " " : "", filters[i].name);
118 printf_or_die("\n\n");
120 FOR_EACH_SUPPORTED_FILTER(i) {
121 struct filter *f = filters + i;
123 if (!f->help.short_help)
125 printf_or_die("Options for %s:\n", f->name);
126 ggo_print_help(&f->help, detailed);
130 void generic_filter_pre_select(struct sched *s, struct task *t)
132 struct filter_node *fn = container_of(t, struct filter_node, task);
135 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
139 #ifdef WORDS_BIGENDIAN
140 #define DECODER_SAMPLE_FORMAT SF_S16_BE
142 #define DECODER_SAMPLE_FORMAT SF_S16_LE
146 * Execute a btr command for a decoder.
148 * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
149 * channels count and sample format. This function is called by all decoders to
150 * answer these queries.
152 * \param cmd The command to be executed by the child node.
153 * \param sample_rate Known to the decoder.
154 * \param channels Known to the decoder.
155 * \param result Ascii representation on the answer is stored here.
157 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
160 if (!strcmp(cmd, "sample_rate")) {
161 if (sample_rate == 0)
162 return -E_BTR_NAVAIL;
163 *result = make_message("%u", sample_rate);
166 if (!strcmp(cmd, "channels")) {
168 return -E_BTR_NAVAIL;
169 *result = make_message("%u", channels);
172 if (!strcmp(cmd, "sample_format")) {
173 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
176 return -ERRNO_TO_PARA_ERROR(ENOTSUP);