Fix documentation of pass_buffer_as_shm().
[paraslash.git] / filter_common.c
1 /*
2 * Copyright (C) 2005-2012 Andre Noll <maan@systemlinux.org>
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, i, argc = 2;
45 char **argv;
46
47 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
48 // options? options : "(none)", f->parse_config);
49 if (!f->parse_config)
50 return strlen(options)? -E_BAD_FILTER_OPTIONS : filter_num;
51 // PARA_DEBUG_LOG("options: %s\n", options);
52 argc = create_argv(options, " \t", &argv);
53 if (argc < 0)
54 return -E_BAD_FILTER_OPTIONS;
55 PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
56 for (i = argc - 1; i >= 0; i--)
57 argv[i + 1] = argv[i];
58 argv[0] = para_strdup(f->name);
59 argc++;
60 ret = f->parse_config(argc, argv, conf);
61 free(argv[argc - 1]);
62 argv[argc - 1] = NULL;
63 free_argv(argv);
64 return ret < 0? ret : filter_num;
65 }
66
67 /**
68 * Check the filter command line options.
69 *
70 * \param fa The command line options.
71 * \param conf Points to the filter configuration upon successful return.
72 *
73 * Check if \a fa starts with a the name of a supported filter, followed by
74 * a colon. If yes, call the command line parser of that filter.
75 *
76 * \return On success, the number of the filter is returned and \a conf
77 * is initialized to point to the filter configuration determined by \a fa.
78 * On errors, a negative value is returned.
79 *
80 * Note: If \a fa specifies a filter that has no command line parser success is
81 * returned, and \a conf is initialized to \p NULL.
82 *
83 * \sa filter::parse_config
84 */
85 int check_filter_arg(char *fa, void **conf)
86 {
87 int j;
88
89 *conf = NULL;
90 // PARA_DEBUG_LOG("arg: %s\n", fa);
91 FOR_EACH_SUPPORTED_FILTER(j) {
92 const char *name = filters[j].name;
93 size_t len = strlen(name);
94 char c;
95 if (strlen(fa) < len)
96 continue;
97 if (strncmp(name, fa, len))
98 continue;
99 c = fa[len];
100 if (c && c != ' ')
101 continue;
102 if (c && !filters[j].parse_config)
103 return -E_BAD_FILTER_OPTIONS;
104 return parse_filter_args(j, c? fa + len + 1 :
105 fa + strlen(fa), conf);
106 }
107 return -E_UNSUPPORTED_FILTER;
108 }
109
110 /**
111 * Print help text of each filter to stdout.
112 *
113 * \param detailed If non-zero, print detailed help.
114 */
115 void print_filter_helps(int detailed)
116 {
117 int i;
118
119 printf_or_die("\nAvailable filters: \n\t");
120 FOR_EACH_SUPPORTED_FILTER(i)
121 printf_or_die("%s%s", i? " " : "", filters[i].name);
122 printf_or_die("\n\n");
123
124 FOR_EACH_SUPPORTED_FILTER(i) {
125 struct filter *f = filters + i;
126
127 if (!f->help.short_help)
128 continue;
129 printf_or_die("Options for %s:\n", f->name);
130 ggo_print_help(&f->help, detailed);
131 }
132 }
133
134 /**
135 * Set select timeout of the the scheduler.
136 *
137 * \param s The scheduler.
138 * \param t The task struct of this filter.
139 *
140 * This looks at the status of the btr node of the filter. If data is available
141 * in the input queue of the filter, or if an error occured, a minimal timeout
142 * for the next select call is requested from the scheduler. Otherwise the
143 * scheduler timeout is left unchanged.
144 */
145 void generic_filter_pre_select(struct sched *s, struct task *t)
146 {
147 struct filter_node *fn = container_of(t, struct filter_node, task);
148
149 t->error = 0;
150 if (btr_node_status(fn->btrn, fn->min_iqs, BTR_NT_INTERNAL) != 0)
151 sched_min_delay(s);
152 }
153
154 #ifdef WORDS_BIGENDIAN
155 #define DECODER_SAMPLE_FORMAT SF_S16_BE
156 #else
157 #define DECODER_SAMPLE_FORMAT SF_S16_LE
158 #endif
159
160 /**
161 * Execute a btr command for a decoder.
162 *
163 * The buffer tree nodes of the writers ask the parent nodes about sample_rate,
164 * channels count and sample format. This function is called by all decoders to
165 * answer these queries.
166 *
167 * \param cmd The command to be executed by the child node.
168 * \param sample_rate Known to the decoder.
169 * \param channels Known to the decoder.
170 * \param result Ascii representation on the answer is stored here.
171 *
172 * \return Standard.
173 */
174 int decoder_execute(const char *cmd, unsigned sample_rate, unsigned channels,
175 char **result)
176 {
177 if (!strcmp(cmd, "sample_rate")) {
178 if (sample_rate == 0)
179 return -E_BTR_NAVAIL;
180 *result = make_message("%u", sample_rate);
181 return 1;
182 }
183 if (!strcmp(cmd, "channels")) {
184 if (channels == 0)
185 return -E_BTR_NAVAIL;
186 *result = make_message("%u", channels);
187 return 1;
188 }
189 if (!strcmp(cmd, "sample_format")) {
190 *result = make_message("%u", DECODER_SAMPLE_FORMAT);
191 return 1;
192 }
193 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
194 }