filters: Allow a filter to have both sink and source

This sets the base for better representing module-echo-cancel. This
patch itself should change no behaviour -- it only makes way for other
changes.
This commit is contained in:
Arun Raghavan 2011-11-11 15:08:24 +05:30
parent aaf0f5bd6e
commit c30a68ebbd

View file

@ -57,9 +57,10 @@ static const char* const valid_modargs[] = {
struct filter { struct filter {
char *name; char *name;
uint32_t module_index; uint32_t module_index;
pa_bool_t is_sink; pa_sink *sink;
pa_object *parent_obj; /* source or sink that the filter is connected to */ pa_sink *sink_master;
pa_object *obj; /* source or sink of the filter */ pa_source *source;
pa_source *source_master;
}; };
struct userdata { struct userdata {
@ -83,17 +84,19 @@ struct userdata {
static unsigned filter_hash(const void *p) { static unsigned filter_hash(const void *p) {
const struct filter *f = p; const struct filter *f = p;
if (f->is_sink) if (f->sink_master && !f->source_master)
return (unsigned) (PA_SINK(f->parent_obj)->index + pa_idxset_string_hash_func(f->name)); return (unsigned) (f->sink_master->index + pa_idxset_string_hash_func(f->name));
else if (!f->sink_master && f->source_master)
return (unsigned) ((f->source_master->index << 16) + pa_idxset_string_hash_func(f->name));
else else
return (unsigned) ((PA_SOURCE(f->parent_obj)->index << 16) + pa_idxset_string_hash_func(f->name)); pa_assert_not_reached();
} }
static int filter_compare(const void *a, const void *b) { static int filter_compare(const void *a, const void *b) {
const struct filter *fa = a, *fb = b; const struct filter *fa = a, *fb = b;
int r; int r;
if (fa->parent_obj != fb->parent_obj) if (fa->sink_master != fb->sink_master || fa->source_master != fb->source_master)
return 1; return 1;
if ((r = strcmp(fa->name, fb->name))) if ((r = strcmp(fa->name, fb->name)))
return r; return r;
@ -101,15 +104,19 @@ static int filter_compare(const void *a, const void *b) {
return 0; return 0;
} }
static struct filter *filter_new(const char *name, pa_object* parent_obj, pa_bool_t is_sink) { static struct filter *filter_new(const char *name, pa_sink *sink, pa_source *source) {
struct filter *f; struct filter *f;
pa_assert(sink || source);
f = pa_xnew(struct filter, 1); f = pa_xnew(struct filter, 1);
f->name = pa_xstrdup(name); f->name = pa_xstrdup(name);
pa_assert_se(f->parent_obj = parent_obj); f->sink_master = sink;
f->is_sink = is_sink; f->source_master = source;
f->module_index = PA_INVALID_INDEX; f->module_index = PA_INVALID_INDEX;
f->obj = NULL; f->sink = NULL;
f->source = NULL;
return f; return f;
} }
@ -140,12 +147,15 @@ static const char* should_filter(pa_object *o, pa_bool_t is_sink_input) {
return NULL; return NULL;
} }
static pa_bool_t nothing_attached(pa_object *obj, pa_bool_t is_sink) static pa_bool_t nothing_attached(struct filter *f) {
{ pa_bool_t no_si = TRUE, no_so = TRUE;
if (is_sink)
return pa_idxset_isempty(PA_SINK(obj)->inputs); if (f->sink)
else no_si = pa_idxset_isempty(f->sink->inputs);
return pa_idxset_isempty(PA_SOURCE(obj)->outputs); else if (f->source)
no_so = pa_idxset_isempty(f->source->outputs);
return no_si && no_so;
} }
static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) { static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
@ -162,7 +172,7 @@ static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, cons
u->housekeeping_time_event = NULL; u->housekeeping_time_event = NULL;
PA_HASHMAP_FOREACH(filter, u->filters, state) { PA_HASHMAP_FOREACH(filter, u->filters, state) {
if (filter->obj && nothing_attached(filter->obj, filter->is_sink)) { if (nothing_attached(filter)) {
uint32_t idx; uint32_t idx;
pa_log_debug("Detected filter %s as no longer used. Unloading.", filter->name); pa_log_debug("Detected filter %s as no longer used. Unloading.", filter->name);
@ -203,13 +213,13 @@ static void move_object_for_filter(pa_object *o, struct filter* filter, pa_bool_
pa_assert(o); pa_assert(o);
pa_assert(filter); pa_assert(filter);
pa_assert_se(parent = (restore ? filter->parent_obj : filter->obj));
if (is_sink_input) { if (is_sink_input) {
pl = PA_SINK_INPUT(o)->proplist; pl = PA_SINK_INPUT(o)->proplist;
pa_assert_se(parent = PA_OBJECT(restore ? filter->sink_master : filter->sink));
name = PA_SINK(parent)->name; name = PA_SINK(parent)->name;
} else { } else {
pl = PA_SOURCE_OUTPUT(o)->proplist; pl = PA_SOURCE_OUTPUT(o)->proplist;
pa_assert_se(parent = PA_OBJECT(restore ? filter->source_master : filter->source));
name = PA_SOURCE(parent)->name; name = PA_SOURCE(parent)->name;
} }
@ -235,9 +245,9 @@ static void find_filters_for_module(struct userdata *u, pa_module *m, const char
if (sink->module == m) { if (sink->module == m) {
pa_assert(sink->input_to_master != NULL); pa_assert(sink->input_to_master != NULL);
fltr = filter_new(name, PA_OBJECT(sink->input_to_master->sink), TRUE); fltr = filter_new(name, sink->input_to_master->sink, NULL);
fltr->module_index = m->index; fltr->module_index = m->index;
fltr->obj = PA_OBJECT(sink); fltr->sink = sink;
pa_hashmap_put(u->filters, fltr, fltr); pa_hashmap_put(u->filters, fltr, fltr);
} }
@ -247,9 +257,9 @@ static void find_filters_for_module(struct userdata *u, pa_module *m, const char
if (source->module == m && !source->monitor_of) { if (source->module == m && !source->monitor_of) {
pa_assert(source->output_from_master != NULL); pa_assert(source->output_from_master != NULL);
fltr = filter_new(name, PA_OBJECT(source->output_from_master->source), FALSE); fltr = filter_new(name, NULL, source->output_from_master->source);
fltr->module_index = m->index; fltr->module_index = m->index;
fltr->obj = PA_OBJECT(source); fltr->source = source;
pa_hashmap_put(u->filters, fltr, fltr); pa_hashmap_put(u->filters, fltr, fltr);
} }
@ -262,7 +272,7 @@ static pa_bool_t can_unload_module(struct userdata *u, uint32_t idx) {
/* Check if any other struct filters point to the same module */ /* Check if any other struct filters point to the same module */
PA_HASHMAP_FOREACH(filter, u->filters, state) { PA_HASHMAP_FOREACH(filter, u->filters, state) {
if (filter->module_index == idx && !nothing_attached(filter->obj, pa_sink_isinstance(filter->obj))) if (filter->module_index == idx && !nothing_attached(filter))
return FALSE; return FALSE;
} }
@ -272,23 +282,23 @@ static pa_bool_t can_unload_module(struct userdata *u, uint32_t idx) {
static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) { static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_sink_input) {
const char *want; const char *want;
pa_bool_t done_something = FALSE; pa_bool_t done_something = FALSE;
pa_sink *sink = NULL;
pa_object *parent; /* source/sink of the given source-output/sink-input */ pa_source *source = NULL;
const char *parent_name; const char *sink_name = NULL, *source_name = NULL;
pa_module *module; pa_module *module;
if (is_sink_input) { if (is_sink_input) {
parent = PA_OBJECT(PA_SINK_INPUT(o)->sink); sink = PA_SINK_INPUT(o)->sink;
parent_name = PA_SINK_INPUT(o)->sink->name; sink_name = sink->name;
module = PA_SINK_INPUT(o)->sink->module; module = sink->module;
} else { } else {
parent = PA_OBJECT(PA_SOURCE_OUTPUT(o)->source); source = PA_SOURCE_OUTPUT(o)->source;
parent_name = PA_SOURCE_OUTPUT(o)->source->name; source_name = source->name;
module = PA_SOURCE_OUTPUT(o)->source->module; module = source->module;
} }
/* If there is no sink yet, we can't do much */ /* If there is no sink/source yet, we can't do much */
if (!parent) if ((is_sink_input && !sink) || (!is_sink_input && !source))
return PA_HOOK_OK; return PA_HOOK_OK;
/* If the stream doesn't what any filter, then let it be. */ /* If the stream doesn't what any filter, then let it be. */
@ -309,13 +319,14 @@ static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_s
return PA_HOOK_OK; return PA_HOOK_OK;
} }
fltr = filter_new(want, parent, is_sink_input); fltr = filter_new(want, sink, source);
if (!(filter = pa_hashmap_get(u->filters, fltr))) { if (!(filter = pa_hashmap_get(u->filters, fltr))) {
char *args; char *args;
pa_module *m; pa_module *m;
args = pa_sprintf_malloc("autoloaded=1 %s_master=%s", is_sink_input ? "sink" : "source", parent_name); args = pa_sprintf_malloc("autoloaded=1 %s_master=%s", is_sink_input ? "sink" : "source",
is_sink_input ? sink_name : source_name);
pa_log_debug("Loading %s with arguments '%s'", module_name, args); pa_log_debug("Loading %s with arguments '%s'", module_name, args);
if ((m = pa_module_load(u->core, module_name, args))) { if ((m = pa_module_load(u->core, module_name, args))) {
@ -329,15 +340,15 @@ static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_s
pa_xfree(fltr); pa_xfree(fltr);
if (!filter) { if (!filter) {
pa_log("Unable to load %s for <%s>", module_name, parent_name); pa_log("Unable to load %s for <%s>", module_name, is_sink_input ? sink_name : source_name);
pa_xfree(module_name); pa_xfree(module_name);
return PA_HOOK_OK; return PA_HOOK_OK;
} }
pa_xfree(module_name); pa_xfree(module_name);
if (filter->obj) { /* We can move the stream now as we know the destination. If this
/* We can move the sink_input now as the know the destination. * isn't true, we will do it later when the sink appears. */
* If this isn't true, we will do it later when the sink appears. */ if ((is_sink_input && filter->sink) || (!is_sink_input && filter->source)) {
move_object_for_filter(o, filter, FALSE, is_sink_input); move_object_for_filter(o, filter, FALSE, is_sink_input);
done_something = TRUE; done_something = TRUE;
} }
@ -348,7 +359,7 @@ static pa_hook_result_t process(struct userdata *u, pa_object *o, pa_bool_t is_s
/* We do not want to filter... but are we already filtered? /* We do not want to filter... but are we already filtered?
* This can happen if an input's proplist changes */ * This can happen if an input's proplist changes */
PA_HASHMAP_FOREACH(filter, u->filters, state) { PA_HASHMAP_FOREACH(filter, u->filters, state) {
if (parent == filter->obj) { if ((is_sink_input && sink == filter->sink) || (!is_sink_input && source == filter->source)) {
move_object_for_filter(o, filter, TRUE, is_sink_input); move_object_for_filter(o, filter, TRUE, is_sink_input);
done_something = TRUE; done_something = TRUE;
break; break;
@ -409,13 +420,13 @@ static pa_hook_result_t sink_unlink_cb(pa_core *core, pa_sink *sink, struct user
/* If either the parent or the sink we've loaded disappears, /* If either the parent or the sink we've loaded disappears,
* we should remove it from our hashmap */ * we should remove it from our hashmap */
PA_HASHMAP_FOREACH(filter, u->filters, state) { PA_HASHMAP_FOREACH(filter, u->filters, state) {
if (filter->parent_obj == PA_OBJECT(sink) || filter->obj == PA_OBJECT(sink)) { if (filter->sink_master == sink || filter->sink == sink) {
uint32_t idx; uint32_t idx;
/* Attempt to rescue any streams to the parent sink as this is likely /* Attempt to rescue any streams to the parent sink as this is likely
* the best course of action (as opposed to a generic rescue via * the best course of action (as opposed to a generic rescue via
* module-rescue-streams */ * module-rescue-streams */
if (filter->obj == PA_OBJECT(sink)) { if (filter->sink == sink) {
pa_sink_input *i; pa_sink_input *i;
PA_IDXSET_FOREACH(i, sink->inputs, idx) PA_IDXSET_FOREACH(i, sink->inputs, idx)
@ -481,13 +492,13 @@ static pa_hook_result_t source_unlink_cb(pa_core *core, pa_source *source, struc
/* If either the parent or the source we've loaded disappears, /* If either the parent or the source we've loaded disappears,
* we should remove it from our hashmap */ * we should remove it from our hashmap */
PA_HASHMAP_FOREACH(filter, u->filters, state) { PA_HASHMAP_FOREACH(filter, u->filters, state) {
if (filter->parent_obj == PA_OBJECT(source) || filter->obj == PA_OBJECT(source)) { if (filter->source_master == source || filter->source == source) {
uint32_t idx; uint32_t idx;
/* Attempt to rescue any streams to the parent source as this is likely /* Attempt to rescue any streams to the parent source as this is likely
* the best course of action (as opposed to a generic rescue via * the best course of action (as opposed to a generic rescue via
* module-rescue-streams */ * module-rescue-streams */
if (filter->obj == PA_OBJECT(source)) { if (filter->source == source) {
pa_source_output *o; pa_source_output *o;
PA_IDXSET_FOREACH(o, source->outputs, idx) PA_IDXSET_FOREACH(o, source->outputs, idx)