[pulseaudio-discuss] [PATCH v5] filter-apply, ladspa-sink, virtual-surround-sink: filter-apply supports ladspa-sink and virtual-surround-sink properly

KimJeongYeon see2002 at gmail.com
Sun Apr 2 15:11:09 UTC 2017


Currently, filter-apply doesn't support ladspa-sink based filters, because:
* loading ladspa-sink failed due to some argument mismatched.
  (e.g 'master' used instead of 'sink_master')
* ladspa-sink required additional parameters by default.
  (e.g plugin/label/control/...)

Changes in v1:
* filter-apply able to load ladspa-sink.
* This patch introduces new sink-input(source-output) property to append extra parameters.
  #define PA_PROP_FILTER_APPLY_EXTRA_PARAMETERS  "filter.apply.extra.parameters"
  e.g) paplay file.wav --property=filter.apply=ladspa-sink \
                       --property=filter.apply.extra.parameters="plugin=ladspa label=ladspa_stereo control=0"
* ladspa-sink support autoloaded feature.

Changes in v2:
* Additionally, support virtual-surround-sink too.

Changes in v3:
* Currently, filter-apply manages filters using name of filter module and master sink.
* Even if a filter able to load various plugins, it cannot run as multiple instances.
* New 'group' of filter will provide to run multiple instances in this case.
* For example, a filter group 'group1' of module-ladspa-sink has opened 'ladspa1' plugin.
  Other group 'group2' able to open 'ladspa2' plugin in same time.
  e.g) paplay file1.wav --property=filter.apply=ladspa-sink \
                        --property=filter.apply.extra.group=group1 \
                        --property=filter.apply.extra.parameters="plugin=ladspa1 label=stereo control=0"
       paplay file2.wav --property=filter.apply=ladspa-sink \
                        --property=filter.apply.extra.group=group2 \
                        --property=filter.apply.extra.parameters="plugin=ladspa2 label=stereo control=0"

Changes in v4:
* Use pa_safe_streq() instead of strcmp().
* Fix typos.

Changes in v5:
* Remove unnecessary 'extra' word from module-filter-apply.c.
* Small refactoring of filter_compare().

Signed-off-by: KimJeongYeon <jeongyeon.kim at samsung.com>
---
 src/modules/module-filter-apply.c          | 72 ++++++++++++++++++++++++------
 src/modules/module-ladspa-sink.c           | 32 +++++++++++--
 src/modules/module-virtual-surround-sink.c | 31 +++++++++++--
 src/pulse/proplist.h                       |  8 +++-
 4 files changed, 122 insertions(+), 21 deletions(-)

diff --git a/src/modules/module-filter-apply.c b/src/modules/module-filter-apply.c
index 364d68b..8579723 100644
--- a/src/modules/module-filter-apply.c
+++ b/src/modules/module-filter-apply.c
@@ -56,6 +56,7 @@ static const char* const valid_modargs[] = {
 
 struct filter {
     char *name;
+    char *group;
     uint32_t module_index;
     pa_sink *sink;
     pa_sink *sink_master;
@@ -87,23 +88,24 @@ static unsigned filter_hash(const void *p) {
 
 static int filter_compare(const void *a, const void *b) {
     const struct filter *fa = a, *fb = b;
-    int r;
 
     if (fa->sink_master != fb->sink_master || fa->source_master != fb->source_master)
         return 1;
-    if ((r = strcmp(fa->name, fb->name)))
-        return r;
-
+    if (!pa_safe_streq(fa->name, fb->name))
+        return 1;
+    if (!pa_safe_streq(fa->group, fb->group))
+        return 1;
     return 0;
 }
 
-static struct filter *filter_new(const char *name, pa_sink *sink, pa_source *source) {
+static struct filter *filter_new(const char *name, const char *group, pa_sink *sink, pa_source *source) {
     struct filter *f;
 
     pa_assert(sink || source);
 
     f = pa_xnew(struct filter, 1);
     f->name = pa_xstrdup(name);
+    f->group = pa_xstrdup(group);
     f->sink_master = sink;
     f->source_master = source;
     f->module_index = PA_INVALID_INDEX;
@@ -117,6 +119,7 @@ static void filter_free(struct filter *f) {
     pa_assert(f);
 
     pa_xfree(f->name);
+    pa_xfree(f->group);
     pa_xfree(f);
 }
 
@@ -140,6 +143,38 @@ static const char* should_filter(pa_object *o, bool is_sink_input) {
     return NULL;
 }
 
+static const char* should_filter_parameters(pa_object *o, bool is_sink_input) {
+    const char *parameters;
+    pa_proplist *pl;
+
+    if (is_sink_input)
+        pl = PA_SINK_INPUT(o)->proplist;
+    else
+        pl = PA_SOURCE_OUTPUT(o)->proplist;
+
+    /* If the stream needs parameters, append them to module. */
+    if ((parameters = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY_EXTRA_PARAMETERS)) && !pa_streq(parameters, ""))
+        return parameters;
+
+    return NULL;
+}
+
+static const char* should_filter_group(pa_object *o, bool is_sink_input) {
+    const char *group;
+    pa_proplist *pl;
+
+    if (is_sink_input)
+        pl = PA_SINK_INPUT(o)->proplist;
+    else
+        pl = PA_SOURCE_OUTPUT(o)->proplist;
+
+    /* If the stream needs group of filters, allow multiple instances. */
+    if ((group = pa_proplist_gets(pl, PA_PROP_FILTER_APPLY_EXTRA_GROUP)) && !pa_streq(group, ""))
+        return group;
+
+    return NULL;
+}
+
 static bool should_group_filter(struct filter *filter) {
     return pa_streq(filter->name, "echo-cancel");
 }
@@ -252,7 +287,7 @@ static void housekeeping_time_callback(pa_mainloop_api*a, pa_time_event* e, cons
         if (nothing_attached(filter)) {
             uint32_t idx;
 
-            pa_log_debug("Detected filter %s as no longer used. Unloading.", filter->name);
+            pa_log_debug("Detected filter %s(%s) as no longer used. Unloading.", filter->name, filter->group ? filter->group : "no group");
             idx = filter->module_index;
             pa_hashmap_remove(u->filters, filter);
             filter_free(filter);
@@ -359,7 +394,7 @@ static void move_objects_for_filter(struct userdata *u, pa_object *o, struct fil
 
 /* Note that we assume a filter will provide at most one sink and at most one
  * source (and at least one of either). */
-static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name) {
+static void find_filters_for_module(struct userdata *u, pa_module *m, const char *name, const char *group) {
     uint32_t idx;
     pa_sink *sink;
     pa_source *source;
@@ -369,7 +404,7 @@ static void find_filters_for_module(struct userdata *u, pa_module *m, const char
         if (sink->module == m) {
             pa_assert(pa_sink_is_filter(sink));
 
-            fltr = filter_new(name, sink->input_to_master->sink, NULL);
+            fltr = filter_new(name, group, sink->input_to_master->sink, NULL);
             fltr->module_index = m->index;
             fltr->sink = sink;
 
@@ -382,7 +417,7 @@ static void find_filters_for_module(struct userdata *u, pa_module *m, const char
             pa_assert(pa_source_is_filter(source));
 
             if (!fltr) {
-                fltr = filter_new(name, NULL, source->output_from_master->source);
+                fltr = filter_new(name, group, NULL, source->output_from_master->source);
                 fltr->module_index = m->index;
                 fltr->source = source;
             } else {
@@ -437,6 +472,9 @@ static pa_hook_result_t process(struct userdata *u, pa_object *o, bool is_sink_i
 
     /* If the stream doesn't what any filter, then let it be. */
     if ((want = should_filter(o, is_sink_input))) {
+        const char *parameters;
+        const char *group;
+
         /* We need to ensure the SI is playing on a sink of this type
          * attached to the sink it's "officially" playing on */
 
@@ -449,7 +487,14 @@ static pa_hook_result_t process(struct userdata *u, pa_object *o, bool is_sink_i
             goto done;
         }
 
-        fltr = filter_new(want, sink, source);
+        /* Some filter modules might require parameters by default.
+         * (e.g. 'plugin', 'label', 'control' of module-ladspa-sink) */
+        parameters = should_filter_parameters(o, is_sink_input);
+
+        /* Some applications may want group of filters. (optional) */
+        group = should_filter_group(o, is_sink_input);
+
+        fltr = filter_new(want, group, sink, source);
 
         if (should_group_filter(fltr) && !find_paired_master(u, fltr, o, is_sink_input)) {
             pa_log_debug("Want group filtering but don't have enough streams.");
@@ -460,16 +505,17 @@ static pa_hook_result_t process(struct userdata *u, pa_object *o, bool is_sink_i
             char *args;
             pa_module *m;
 
-            args = pa_sprintf_malloc("autoloaded=1 %s%s %s%s",
+            args = pa_sprintf_malloc("autoloaded=1 %s%s %s%s %s",
                     fltr->sink_master ? "sink_master=" : "",
                     fltr->sink_master ? fltr->sink_master->name : "",
                     fltr->source_master ? "source_master=" : "",
-                    fltr->source_master ? fltr->source_master->name : "");
+                    fltr->source_master ? fltr->source_master->name : "",
+                    parameters ? parameters : "");
 
             pa_log_debug("Loading %s with arguments '%s'", module_name, args);
 
             if ((m = pa_module_load(u->core, module_name, args))) {
-                find_filters_for_module(u, m, want);
+                find_filters_for_module(u, m, want, group);
                 filter = pa_hashmap_get(u->filters, fltr);
                 done_something = true;
             }
diff --git a/src/modules/module-ladspa-sink.c b/src/modules/module-ladspa-sink.c
index 6dd2987..1c301c3 100644
--- a/src/modules/module-ladspa-sink.c
+++ b/src/modules/module-ladspa-sink.c
@@ -54,7 +54,7 @@ PA_MODULE_LOAD_ONCE(false);
 PA_MODULE_USAGE(
     _("sink_name=<name for the sink> "
       "sink_properties=<properties for the sink> "
-      "master=<name of sink to filter> "
+      "sink_master=<name of sink to filter> "
       "format=<sample format> "
       "rate=<sample rate> "
       "channels=<number of channels> "
@@ -63,9 +63,11 @@ PA_MODULE_USAGE(
       "label=<ladspa plugin label> "
       "control=<comma separated list of input control values> "
       "input_ladspaport_map=<comma separated list of input LADSPA port names> "
-      "output_ladspaport_map=<comma separated list of output LADSPA port names> "));
+      "output_ladspaport_map=<comma separated list of output LADSPA port names> "
+      "autoloaded=<set if this module is being loaded automatically> "));
 
 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
+#define DEFAULT_AUTOLOADED false
 
 /* PLEASE NOTICE: The PortAudio ports and the LADSPA ports are two different concepts.
 They are not related and where possible the names of the LADSPA port variables contains "ladspa" to avoid confusion */
@@ -99,12 +101,13 @@ struct userdata {
 #endif
 
     bool auto_desc;
+    bool autoloaded;
 };
 
 static const char* const valid_modargs[] = {
     "sink_name",
     "sink_properties",
-    "master",
+    "sink_master",
     "format",
     "rate",
     "channels",
@@ -114,6 +117,7 @@ static const char* const valid_modargs[] = {
     "control",
     "input_ladspaport_map",
     "output_ladspaport_map",
+    "autoloaded",
     NULL
 };
 
@@ -640,6 +644,19 @@ static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t s
 }
 
 /* Called from main context */
+static bool sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
+
+    if (u->autoloaded)
+        return false;
+
+    return u->sink != dest;
+}
+
+/* Called from main context */
 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
     struct userdata *u;
 
@@ -968,7 +985,7 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
+    if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink_master", NULL), PA_NAMEREG_SINK))) {
         pa_log("Master sink not found");
         goto fail;
     }
@@ -1231,6 +1248,12 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    u->autoloaded = DEFAULT_AUTOLOADED;
+    if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
+        pa_log("Failed to parse autoloaded value");
+        goto fail;
+    }
+
     if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
         const char *z;
 
@@ -1283,6 +1306,7 @@ int pa__init(pa_module*m) {
     u->sink_input->attach = sink_input_attach_cb;
     u->sink_input->detach = sink_input_detach_cb;
     u->sink_input->state_change = sink_input_state_change_cb;
+    u->sink_input->may_move_to = sink_input_may_move_to_cb;
     u->sink_input->moving = sink_input_moving_cb;
     u->sink_input->mute_changed = sink_input_mute_changed_cb;
     u->sink_input->userdata = u;
diff --git a/src/modules/module-virtual-surround-sink.c b/src/modules/module-virtual-surround-sink.c
index 4a53623..5b17902 100644
--- a/src/modules/module-virtual-surround-sink.c
+++ b/src/modules/module-virtual-surround-sink.c
@@ -50,7 +50,7 @@ PA_MODULE_LOAD_ONCE(false);
 PA_MODULE_USAGE(
         _("sink_name=<name for the sink> "
           "sink_properties=<properties for the sink> "
-          "master=<name of sink to filter> "
+          "sink_master=<name of sink to filter> "
           "format=<sample format> "
           "rate=<sample rate> "
           "channels=<number of channels> "
@@ -58,9 +58,11 @@ PA_MODULE_USAGE(
           "use_volume_sharing=<yes or no> "
           "force_flat_volume=<yes or no> "
           "hrir=/path/to/left_hrir.wav "
+          "autoloaded=<set if this module is being loaded automatically> "
         ));
 
 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
+#define DEFAULT_AUTOLOADED false
 
 struct userdata {
     pa_module *module;
@@ -87,12 +89,14 @@ struct userdata {
 
     float *input_buffer;
     int input_buffer_offset;
+
+    bool autoloaded;
 };
 
 static const char* const valid_modargs[] = {
     "sink_name",
     "sink_properties",
-    "master",
+    "sink_master",
     "format",
     "rate",
     "channels",
@@ -100,6 +104,7 @@ static const char* const valid_modargs[] = {
     "use_volume_sharing",
     "force_flat_volume",
     "hrir",
+    "autoloaded",
     NULL
 };
 
@@ -428,6 +433,19 @@ static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t s
 }
 
 /* Called from main context */
+static bool sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
+    struct userdata *u;
+
+    pa_sink_input_assert_ref(i);
+    pa_assert_se(u = i->userdata);
+
+    if (u->autoloaded)
+        return false;
+
+    return u->sink != dest;
+}
+
+/* Called from main context */
 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
     struct userdata *u;
 
@@ -591,7 +609,7 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
+    if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "sink_master", NULL), PA_NAMEREG_SINK))) {
         pa_log("Master sink not found");
         goto fail;
     }
@@ -672,6 +690,12 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    u->autoloaded = DEFAULT_AUTOLOADED;
+    if (pa_modargs_get_value_boolean(ma, "autoloaded", &u->autoloaded) < 0) {
+        pa_log("Failed to parse autoloaded value");
+        goto fail;
+    }
+
     if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
         const char *z;
 
@@ -731,6 +755,7 @@ int pa__init(pa_module*m) {
     u->sink_input->attach = sink_input_attach_cb;
     u->sink_input->detach = sink_input_detach_cb;
     u->sink_input->state_change = sink_input_state_change_cb;
+    u->sink_input->may_move_to = sink_input_may_move_to_cb;
     u->sink_input->moving = sink_input_moving_cb;
     u->sink_input->volume_changed = use_volume_sharing ? NULL : sink_input_volume_changed_cb;
     u->sink_input->mute_changed = sink_input_mute_changed_cb;
diff --git a/src/pulse/proplist.h b/src/pulse/proplist.h
index bc9e8f8..8c19312 100644
--- a/src/pulse/proplist.h
+++ b/src/pulse/proplist.h
@@ -69,7 +69,13 @@ PA_C_DECL_BEGIN
 /** For streams: the name of a filter that is desired, e.g.\ "echo-cancel" or "equalizer-sink". Differs from PA_PROP_FILTER_WANT in that it forces PulseAudio to apply the filter, regardless of whether PulseAudio thinks it makes sense to do so or not. If this is set, PA_PROP_FILTER_WANT is ignored. In other words, you almost certainly do not want to use this. \since 1.0 */
 #define PA_PROP_FILTER_APPLY                   "filter.apply"
 
-/** For streams: the name of a filter that should specifically suppressed (i.e.\ overrides PA_PROP_FILTER_WANT). Useful for the times that PA_PROP_FILTER_WANT is automatically added (e.g. echo-cancellation for phone streams when $VOIP_APP does its own, internal AEC) \since 1.0 */
+/** For streams: some modules require extra parameters, e.g.\ "plugin=ladspa label=ladspa_stereo control=0" in case of "ladspa-sink" filter */
+#define PA_PROP_FILTER_APPLY_EXTRA_PARAMETERS  "filter.apply.extra.parameters"
+
+/** For streams: some applications may want to classify sub filter groups and they can be used as multiple instances. (e.g. load multiple plugin instances of "ladspa-sink" filter) */
+#define PA_PROP_FILTER_APPLY_EXTRA_GROUP       "filter.apply.extra.group"
+
+/** For streams: the name of a filter that should specifically be suppressed (i.e.\ overrides PA_PROP_FILTER_WANT). Useful for the times that PA_PROP_FILTER_WANT is automatically added (e.g. echo-cancellation for phone streams when $VOIP_APP does it's own, internal AEC) \since 1.0 */
 #define PA_PROP_FILTER_SUPPRESS                "filter.suppress"
 
 /** For event sound streams: XDG event sound name. e.g.\ "message-new-email" (Event sound streams are those with media.role set to "event") */
-- 
2.7.4



More information about the pulseaudio-discuss mailing list