3a5b85dde69978e749e87dafc535507d79509ce6
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file filter_chain.c common helper functions for filter input/output */
29 DEFINE_FILTER_ARRAY(filters
);
32 * call the init function of each supported filter
34 * \param all_filters the array of all supported filters
37 void filter_init(struct filter
*all_filters
)
41 for (f
= all_filters
; f
->name
; f
++)
46 * close and destroy a filter callback
48 * \param fcb the filter callback to close
50 * This removes \a fcb from the list of filter callbacks and calls
51 * the close callback associated with \a fcb.
53 static void close_filter_callback(struct filter_callback
*fcb
)
55 PARA_NOTICE_LOG("closing filter_callback %p, data: %p\n", fcb
, fcb
->data
);
61 * close all callbacks of a filter node
63 * \param fn the filter node which contains the filter callbacks to be closed
65 * Call close_filter_callback() for each entry in the filter callback list
68 static void close_callbacks(struct filter_node
*fn
)
70 struct filter_callback
*fcb
, *tmp
;
72 list_for_each_entry_safe(fcb
, tmp
, &fn
->callbacks
, node
) {
73 PARA_INFO_LOG("closing %s filter callback\n",
75 close_filter_callback(fcb
);
79 static void call_callbacks(struct filter_node
*fn
, char *inbuf
, size_t inlen
,
80 char *outbuf
, size_t outlen
)
82 struct filter_callback
*fcb
, *tmp
;
83 list_for_each_entry_safe(fcb
, tmp
, &fn
->callbacks
, node
) {
85 if (inlen
&& fcb
->input_cb
) {
86 ret
= fcb
->input_cb(inbuf
, inlen
, fcb
);
88 close_filter_callback(fcb
);
92 if (!outlen
|| !fcb
->output_cb
)
94 ret
= fcb
->output_cb(outbuf
, outlen
, fcb
);
96 close_filter_callback(fcb
);
101 * call the convert function of each filter
104 * \param t the task containing the filter chain
106 * This is the core function of the filter subsystem. It loops over the list of
107 * filter nodes determined by \a t and calls the filter's convert function if
108 * there is input available for the filter node in question. If the convert
109 * function consumed some or all of its input data, all registered input
110 * callbacks are called. Similarly, if a convert function produced output, all
111 * registerd output callbacks get called.
113 * \return The sum of output bytes produced by the convert functions on
114 * success, negative return value on errors (the return value is stored in
117 * \sa filter_node, filter#convert, filter_callback
119 void filter_pre_select(__a_unused
struct sched
*s
, struct task
*t
)
121 struct filter_chain
*fc
= t
->private_data
;
122 struct filter_node
*fn
;
125 int conv
, conv_total
= 0;
128 if (fc
->output_eof
&& *fc
->output_eof
)
132 loaded
= fc
->in_loaded
;
134 list_for_each_entry(fn
, &fc
->filters
, node
) {
135 if (*loaded
&& fn
->loaded
< fn
->bufsize
) {
136 size_t size
, old_fn_loaded
= fn
->loaded
;
137 // PARA_DEBUG_LOG("fc %p loaded: %zd, calling %s convert\n",
138 // fc, *loaded, fn->filter->name);
139 t
->ret
= fn
->filter
->convert(ib
, *loaded
, fn
);
143 call_callbacks(fn
, ib
, size
, fn
->buf
+ old_fn_loaded
,
144 fn
->loaded
- old_fn_loaded
);
147 if (*loaded
&& size
) {
148 // PARA_DEBUG_LOG("moving %zd bytes in input "
149 // "buffer for %s filter\n",
150 // *loaded, fn->filter->name);
151 memmove(ib
, ib
+ size
, *loaded
);
155 loaded
= &fn
->loaded
;
158 // PARA_DEBUG_LOG("eof (in/out/fc): %d/%d/%d out_loaded: %zd, "
159 // "conv: %d, conv_total: %d\n", *fc->input_eof,
160 // fc->output_eof? *fc->output_eof : -42,
161 // fc->eof, *fc->out_loaded, conv, conv_total);
169 if (*fc
->in_loaded
&& conv_total
)
177 * close all filter nodes and its callbacks
179 * \param fc the filter chain to close
181 * For each filter node determined by \a fc, call the close function of each
182 * registered filter callback as well as the close function of the
183 * corresponding filter. Free all resources and destroy all callback lists and
184 * the filter node list.
186 * \sa filter::close, filter_callback::close
188 void close_filters(struct filter_chain
*fc
)
190 struct filter_node
*fn
, *tmp
;
194 PARA_NOTICE_LOG("closing filter chain %p\n", fc
);
195 list_for_each_entry_safe(fn
, tmp
, &fc
->filters
, node
) {
197 PARA_INFO_LOG("closing %s filter\n", fn
->filter
->name
);
198 fn
->filter
->close(fn
);
205 * If the filter has a command line parser and options is not NULL, run it.
206 * Returns filter_num on success, negative on errors
208 static int parse_filter_args(int filter_num
, char *options
, void **conf
)
210 struct filter
*f
= &filters
[filter_num
];
214 // PARA_DEBUG_LOG("%s, options: %s, parser: %p\n", f->name,
215 // options? options : "(none)", f->parse_config);
216 if (!f
->parse_config
)
217 return strlen(options
)? -E_BAD_FILTER_OPTIONS
: filter_num
;
218 // PARA_DEBUG_LOG("options: %s\n", options);
219 argc
= split_args(options
, &argv
, " \t");
220 // PARA_DEBUG_LOG("argc = %d, argv[0]: %s\n", argc, argv[0]);
221 for (i
= argc
- 1; i
>= 0; i
--)
222 argv
[i
+ 1] = argv
[i
];
223 argv
[0] = para_strdup(f
->name
);
225 *conf
= f
->parse_config(argc
, argv
);
228 return *conf
? filter_num
: -E_BAD_FILTER_OPTIONS
;
232 * check the filter command line options
234 * \param fa the command line options
235 * \param conf points to the filter configuration upon successful return
237 * Check if \a fa starts with a the name of a supported filter, followed by
238 * a colon. If yes, call the command line parser of that filter.
240 * \return On success, the number of the filter is returned and \a conf
241 * is initialized to point to the filter configuration determined by \a fa.
242 * On errors, a negative value is returned.
244 * Note: If \a fa specifies a filter that has no command line parser success is
245 * returned, and \a conf is initialized to \p NULL.
247 * \sa filter::parse_config
249 int check_filter_arg(char *fa
, void **conf
)
254 // PARA_DEBUG_LOG("arg: %s\n", fa);
255 for (j
= 0; filters
[j
].name
; j
++) {
256 const char *name
= filters
[j
].name
;
257 size_t len
= strlen(name
);
259 if (strlen(fa
) < len
)
261 if (strncmp(name
, fa
, len
))
266 if (c
&& !filters
[j
].parse_config
)
267 return -E_BAD_FILTER_OPTIONS
;
268 return parse_filter_args(j
, c
? fa
+ len
+ 1 :
269 fa
+ strlen(fa
), conf
);
271 return -E_UNSUPPORTED_FILTER
;