From 576b60a942dadb00eb99ddcb02ac3df2176f359d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 25 Jan 2023 11:11:18 -0500
Subject: [PATCH] pulseaudio: avoid to change volumes previously set

Because PulseAudio got flat-volumes on by default, getting an
echo-cancelled stream will, without option, set the master and
echo-cancelled volume to 33%.
This will make the user adjust their microphone every time.
Moreover, the module seems to re-adjust automatically the volume
with some gain control options.
So, in this patch, we load the echo-cancel without volume sharing
and with options to avoid to change a volume previously set.

Sources:
https://wiki.archlinux.org/title/PulseAudio/Troubleshooting
https://askubuntu.com/questions/1015327/disable-volume-auto-adjust-in-pulseaudio-module-echo-cancel
https://askubuntu.com/questions/279407/how-to-disable-microphone-from-auto-adjusting-its-input-volume

Change-Id: I981d1fedf7424895e6f44e9906e385f9c21bddee
---
 src/media/audio/pulseaudio/audiostream.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/media/audio/pulseaudio/audiostream.cpp b/src/media/audio/pulseaudio/audiostream.cpp
index 1dca00a16b..dad3148915 100644
--- a/src/media/audio/pulseaudio/audiostream.cpp
+++ b/src/media/audio/pulseaudio/audiostream.cpp
@@ -59,6 +59,15 @@ AudioStream::AudioStream(pa_context* c,
     std::unique_ptr<pa_proplist, decltype(pa_proplist_free)&> pl(pa_proplist_new(),
                                                                  pa_proplist_free);
     pa_proplist_sets(pl.get(), PA_PROP_FILTER_WANT, "echo-cancel");
+    pa_proplist_sets(
+        pl.get(), "filter.apply.echo-cancel.parameters", // needs pulseaudio >= 11.0
+        "use_volume_sharing=0"  // share volume with master sink/source
+        " use_master_format=1"  // use format/rate/channels from master sink/source
+        " aec_args=\""
+            "digital_gain_control=1"
+            " analog_gain_control=0"
+            " experimental_agc=1"
+        "\"");
 
     audiostream_ = pa_stream_new_with_proplist(c,
                                                desc,
-- 
GitLab