]> code.delx.au - pulseaudio/commitdiff
filters: Allow a filter to have both sink and source
authorArun Raghavan <arun.raghavan@collabora.co.uk>
Fri, 11 Nov 2011 09:38:24 +0000 (15:08 +0530)
committerArun Raghavan <arun.raghavan@collabora.co.uk>
Thu, 24 Nov 2011 07:05:48 +0000 (12:35 +0530)
This sets the base for better representing module-echo-cancel. This
patch itself should change no behaviour -- it only makes way for other
changes.

src/modules/module-filter-apply.c

index dd9a137b22fea7dd27162785b2562e0fa19dbd18..d1fb5df155f0356601fed3097abaded0d1b9f6ec 100644 (file)
@@ -57,9 +57,10 @@ static const char* const valid_modargs[] = {
 struct filter {
     char *name;
     uint32_t module_index;
-    pa_bool_t is_sink;
-    pa_object *parent_obj;      /* source or sink that the filter is connected to */
-    pa_object *obj;             /* source or sink of the filter */
+    pa_sink *sink;
+    pa_sink *sink_master;
+    pa_source *source;
+    pa_source *source_master;
 };
 
 struct userdata {
@@ -83,17 +84,19 @@ struct userdata {
 static unsigned filter_hash(const void *p) {
     const struct filter *f = p;
 
-    if (f->is_sink)
-        return (unsigned) (PA_SINK(f->parent_obj)->index + pa_idxset_string_hash_func(f->name));
+    if (f->sink_master && !f->source_master)
+        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
-        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) {
     const struct filter *fa = a, *fb = b;
     int r;
 
-    if (fa->parent_obj != fb->parent_obj)
+    if (fa->sink_master != fb->sink_master || fa->source_master != fb->source_master)
         return 1;
     if ((r = strcmp(fa->name, fb->name)))
         return r;
@@ -101,15 +104,19 @@ static int filter_compare(const void *a, const void *b) {
     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;
 
+    pa_assert(sink || source);
+
     f = pa_xnew(struct filter, 1);
     f->name = pa_xstrdup(name);
-    pa_assert_se(f->parent_obj = parent_obj);
-    f->is_sink = is_sink;
+    f->sink_master = sink;
+    f->source_master = source;
     f->module_index = PA_INVALID_INDEX;
-    f->obj = NULL;
+    f->sink = NULL;
+    f->source = NULL;
+
     return f;
 }
 
@@ -140,12 +147,15 @@ static const char* should_filter(pa_object *o, pa_bool_t is_sink_input) {
     return NULL;
 }
 
-static pa_bool_t nothing_attached(pa_object *obj, pa_bool_t is_sink)
-{
-    if (is_sink)
-        return pa_idxset_isempty(PA_SINK(obj)->inputs);
-    else
-        return pa_idxset_isempty(PA_SOURCE(obj)->outputs);
+static pa_bool_t nothing_attached(struct filter *f) {
+    pa_bool_t no_si = TRUE, no_so = TRUE;
+
+    if (f->sink)
+        no_si = pa_idxset_isempty(f->sink->inputs);
+    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) {
@@ -162,7 +172,7 @@ static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, cons
     u->housekeeping_time_event = NULL;
 
     PA_HASHMAP_FOREACH(filter, u->filters, state) {
-        if (filter->obj && nothing_attached(filter->obj, filter->is_sink)) {
+        if (nothing_attached(filter)) {
             uint32_t idx;
 
             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(filter);
 
-    pa_assert_se(parent = (restore ? filter->parent_obj : filter->obj));
-
     if (is_sink_input) {
         pl = PA_SINK_INPUT(o)->proplist;
+        pa_assert_se(parent = PA_OBJECT(restore ? filter->sink_master : filter->sink));
         name = PA_SINK(parent)->name;
     } else {
         pl = PA_SOURCE_OUTPUT(o)->proplist;
+        pa_assert_se(parent = PA_OBJECT(restore ? filter->source_master : filter->source));
         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) {
             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->obj = PA_OBJECT(sink);
+            fltr->sink = sink;
 
             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) {
             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->obj = PA_OBJECT(source);
+            fltr->source = source;
 
             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 */
     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;
     }
 
@@ -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) {
     const char *want;
     pa_bool_t done_something = FALSE;
-
-    pa_object *parent; /* source/sink of the given source-output/sink-input */
-    const char *parent_name;
+    pa_sink *sink = NULL;
+    pa_source *source = NULL;
+    const char *sink_name = NULL, *source_name = NULL;
     pa_module *module;
 
     if (is_sink_input) {
-        parent = PA_OBJECT(PA_SINK_INPUT(o)->sink);
-        parent_name = PA_SINK_INPUT(o)->sink->name;
-        module = PA_SINK_INPUT(o)->sink->module;
+        sink = PA_SINK_INPUT(o)->sink;
+        sink_name = sink->name;
+        module = sink->module;
     } else {
-        parent = PA_OBJECT(PA_SOURCE_OUTPUT(o)->source);
-        parent_name = PA_SOURCE_OUTPUT(o)->source->name;
-        module = PA_SOURCE_OUTPUT(o)->source->module;
+        source = PA_SOURCE_OUTPUT(o)->source;
+        source_name = source->name;
+        module = source->module;
     }
 
-    /* If there is no sink yet, we can't do much */
-    if (!parent)
+    /* If there is no sink/source yet, we can't do much */
+    if ((is_sink_input && !sink) || (!is_sink_input && !source))
         return PA_HOOK_OK;
 
     /* 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;
         }
 
-        fltr = filter_new(want, parent, is_sink_input);
+        fltr = filter_new(want, sink, source);
 
         if (!(filter = pa_hashmap_get(u->filters, fltr))) {
             char *args;
             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);
 
             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);
 
         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);
             return PA_HOOK_OK;
         }
         pa_xfree(module_name);
 
-        if (filter->obj) {
-            /* We can move the sink_input now as the know the destination.
-             * If this isn't true, we will do it later when the sink appears. */
+        /* We can move the stream now as we know the destination. 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);
             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?
          * This can happen if an input's proplist changes */
         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);
                 done_something = TRUE;
                 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,
      * we should remove it from our hashmap */
     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;
 
             /* 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
              * module-rescue-streams */
-            if (filter->obj == PA_OBJECT(sink)) {
+            if (filter->sink == sink) {
                 pa_sink_input *i;
 
                 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,
      * we should remove it from our hashmap */
     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;
 
             /* 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
              * module-rescue-streams */
-            if (filter->obj == PA_OBJECT(source)) {
+            if (filter->source == source) {
                 pa_source_output *o;
 
                 PA_IDXSET_FOREACH(o, source->outputs, idx)