2 * Copyright (C) 2005-2013 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>
17 #include "buffer_tree.h"
22 /** The array of supported filters. */
23 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
26 * Call the init function of each supported filter.
29 void filter_init(void)
33 FOR_EACH_SUPPORTED_FILTER(i)
34 filters[i].init(filters + i);
38 * If the filter has a command line parser and options is not NULL, run it.
39 * Returns filter_num on success, negative on errors
41 static int parse_filter_args(int filter_num, char *options, void **conf)
43 struct filter *f = &filters[filter_num];
48 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
49 argc = create_shifted_argv(options, " \t", &argv);
51 return -E_BAD_FILTER_OPTIONS;
52 argv[0] = para_strdup(f->name);
53 ret = f->parse_config(argc, argv, conf);
55 return ret < 0? ret : filter_num;
59 * Check the filter command line options.
61 * \param fa The command line options.
62 * \param conf Points to the filter configuration upon successful return.
64 * Check if \a fa starts with a the name of a supported filter, followed by
65 * a colon. If yes, call the command line parser of that filter.
67 * \return On success, the number of the filter is returned and \a conf
68 * is initialized to point to the filter configuration determined by \a fa.
69 * On errors, a negative value is returned.
71 * Note: If \a fa specifies a filter that has no command line parser success is
72 * returned, and \a conf is initialized to \p NULL.
74 * \sa filter::parse_config
76 int check_filter_arg(char *fa, void **conf)
81 // PARA_DEBUG_LOG("arg: %s\n", fa);
82 FOR_EACH_SUPPORTED_FILTER(j) {
83 const char *name = filters[j].name;
84 size_t len = strlen(name);
88 if (strncmp(name, fa, len))
93 if (c && !filters[j].parse_config)
94 return -E_BAD_FILTER_OPTIONS;
95 return parse_filter_args(j, c? fa + len + 1 :
96 fa + strlen(fa), conf);
98 return -E_UNSUPPORTED_FILTER;
102 * Print help text of each filter to stdout.
104 * \param detailed If non-zero, print detailed help.
106 void print_filter_helps(int detailed)
110 printf_or_die("\nAvailable filters: \n\t");
111 FOR_EACH_SUPPORTED_FILTER(i)
112 printf_or_die("%s%s", i? " " : "", filters[i].name);
113 printf_or_die("\n\n");
115 FOR_EACH_SUPPORTED_FILTER(i) {
116 struct filter *f = filters + i;
118 if (!f->help.short_help)
120 printf_or_die("Options for %s:\n", f->name);
121 ggo_print_help(&f->help, detailed);
126 * Set select timeout of the scheduler.
128 * \param s The scheduler.
129 * \param t The task struct of this filter.
131 * This looks at the status of the btr node of the filter. If data is available
132 * in the input queue of the filter, or if an error occurred, a minimal timeout
133 * for the next select call is requested from the scheduler. Otherwise the
134 * scheduler timeout is left unchanged.
136 void generic_filter_pre_select(struct sched *s, struct task *t)
138 struct filter_node *fn = container_of(t, struct filter_node, task);
141 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
145 #ifdef WORDS_BIGENDIAN
146 #define DECODER_SAMPLE_FORMAT SF_S16_BE
148 #define DECODER_SAMPLE_FORMAT SF_S16_LE
152 * Execute a btr command for a decoder.
154 * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
155 * channels count and sample format. This function is called by all decoders to
156 * answer these queries.
158 * \param cmd The command to be executed by the child node.
159 * \param sample_rate Known to the decoder.
160 * \param channels Known to the decoder.
161 * \param result Ascii representation on the answer is stored here.
165 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
168 if (!strcmp(cmd, "sample_rate")) {
169 if (sample_rate == 0)
170 return -E_BTR_NAVAIL;
171 *result = make_message("%u", sample_rate);
174 if (!strcmp(cmd, "channels")) {
176 return -E_BTR_NAVAIL;
177 *result = make_message("%u", channels);
180 if (!strcmp(cmd, "sample_format")) {
181 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
184 return -ERRNO_TO_PARA_ERROR(ENOTSUP);