Merge branch 't/audiod_exit_cleanup'
[paraslash.git] / filter_common.c
1 /*
2 * Copyright (C) 2005-2014 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file filter_common.c Common helper functions for filter input/output. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "fd.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "error.h"
20 #include "string.h"
21
22 /** The array of supported filters. */
23 struct filter filters[NUM_SUPPORTED_FILTERS] = {FILTER_ARRAY};
24
25 /**
26 * Call the init function of each supported filter.
27 * \sa filter::init
28 */
29 void filter_init(void)
30 {
31 int i;
32
33 FOR_EACH_SUPPORTED_FILTER(i)
34 filters[i].init(filters + i);
35 }
36
37 /*
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
40 */
41 static int parse_filter_args(int filter_num, char *options, void **conf)
42 {
43 struct filter *f = &filters[filter_num];
44 int ret, argc;
45 char **argv;
46
47 if (!f->parse_config)
48 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
49 argc = create_shifted_argv(options, " \t", &argv);
50 if (argc < 0)
51 return -E_BAD_FILTER_OPTIONS;
52 argv[0] = para_strdup(f->name);
53 ret = f->parse_config(argc, argv, conf);
54 free_argv(argv);
55 return ret < 0? ret : filter_num;
56 }
57
58 /**
59 * Check the filter command line options.
60 *
61 * \param fa The command line options.
62 * \param conf Points to the filter configuration upon successful return.
63 *
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.
66 *
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.
70 *
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.
73 *
74 * \sa filter::parse_config
75 */
76 int check_filter_arg(char *fa, void **conf)
77 {
78 int j;
79
80 *conf = NULL;
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);
85 char c;
86 if (strlen(fa) < len)
87 continue;
88 if (strncmp(name, fa, len))
89 continue;
90 c = fa[len];
91 if (c && c != ' ')
92 continue;
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);
97 }
98 return -E_UNSUPPORTED_FILTER;
99 }
100
101 /**
102 * Print help text of each filter to stdout.
103 *
104 * \param flags Passed to \ref ggo_print_help().
105 */
106 void print_filter_helps(unsigned flags)
107 {
108 int i, num = 0;
109
110 printf_or_die("\nAvailable filters: ");
111 FOR_EACH_SUPPORTED_FILTER(i) {
112 if (num > 50) {
113 printf_or_die("\n ");
114 num = 0;
115 }
116 num += printf_or_die("%s%s", i? " " : "", filters[i].name);
117 }
118 printf_or_die("\n");
119
120 FOR_EACH_SUPPORTED_FILTER(i) {
121 struct filter *f = filters + i;
122
123 if (!f->help.short_help)
124 continue;
125 printf_or_die("\nOptions for %s (%s):", f->name,
126 f->help.purpose);
127 ggo_print_help(&f->help, flags);
128 }
129 }
130
131 /**
132 * Set select timeout of the scheduler.
133 *
134 * \param s The scheduler.
135 * \param context Pointer to the filter node (task context).
136 *
137 * This looks at the status of the btr node of the filter. If data is available
138 * in the input queue of the filter, or if an error occurred, a minimal timeout
139 * for the next select call is requested from the scheduler. Otherwise the
140 * scheduler timeout is left unchanged.
141 */
142 void generic_filter_pre_select(struct sched *s, void *context)
143 {
144 struct filter_node *fn = context;
145
146 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
147 sched_min_delay(s);
148 }
149
150 #ifdef WORDS_BIGENDIAN
151 #define DECODER_SAMPLE_FORMAT SF_S16_BE
152 #else
153 #define DECODER_SAMPLE_FORMAT SF_S16_LE
154 #endif
155
156 /**
157 * Execute a btr command for a decoder.
158 *
159 * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
160 * channels count and sample format. This function is called by all decoders to
161 * answer these queries.
162 *
163 * \param cmd The command to be executed by the child node.
164 * \param sample_rate Known to the decoder.
165 * \param channels Known to the decoder.
166 * \param result Ascii representation on the answer is stored here.
167 *
168 * \return Standard.
169 */
170 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
171 char **result)
172 {
173 if (!strcmp(cmd, "sample_rate")) {
174 if (sample_rate == 0)
175 return -E_BTR_NAVAIL;
176 *result = make_message("%u", sample_rate);
177 return 1;
178 }
179 if (!strcmp(cmd, "channels")) {
180 if (channels == 0)
181 return -E_BTR_NAVAIL;
182 *result = make_message("%u", channels);
183 return 1;
184 }
185 if (!strcmp(cmd, "sample_format")) {
186 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
187 return 1;
188 }
189 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
190 }