From b2bf8244dd4829a4b47aa712160345f1db02928f Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Tue, 15 Jun 2021 12:32:47 +0400
Subject: [PATCH] Add "Enable noise suppression" setting to group calls.

---
 Telegram/Resources/langs/lang.strings              |  1 +
 .../SourceFiles/calls/group/calls_group_call.cpp   |  8 ++++++++
 .../SourceFiles/calls/group/calls_group_call.h     |  1 +
 .../calls/group/calls_group_settings.cpp           | 14 ++++++++++++++
 Telegram/SourceFiles/core/core_settings.cpp        | 11 ++++++++++-
 Telegram/SourceFiles/core/core_settings.h          |  7 +++++++
 6 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings
index fd56b4bcc..3b2170c77 100644
--- a/Telegram/Resources/langs/lang.strings
+++ b/Telegram/Resources/langs/lang.strings
@@ -2057,6 +2057,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 "lng_group_call_ptt_delay_s" = "{amount}s";
 "lng_group_call_ptt_delay" = "Push to Talk release delay: {delay}";
 "lng_group_call_share" = "Share Invite Link";
+"lng_group_call_noise_suppression" = "Enable Noise Suppression";
 "lng_group_call_share_speaker" = "Users with this link can speak";
 "lng_group_call_copy_speaker_link" = "Copy Speaker Link";
 "lng_group_call_copy_listener_link" = "Copy Listener Link";
diff --git a/Telegram/SourceFiles/calls/group/calls_group_call.cpp b/Telegram/SourceFiles/calls/group/calls_group_call.cpp
index 6a1f5f1bd..dfb86b422 100644
--- a/Telegram/SourceFiles/calls/group/calls_group_call.cpp
+++ b/Telegram/SourceFiles/calls/group/calls_group_call.cpp
@@ -1606,6 +1606,12 @@ void GroupCall::toggleScheduleStartSubscribed(bool subscribed) {
 	}).send();
 }
 
+void GroupCall::setNoiseSuppression(bool enabled) {
+	if (_instance) {
+		_instance->setIsNoiseSuppressionEnabled(enabled);
+	}
+}
+
 void GroupCall::addVideoOutput(
 		const std::string &endpoint,
 		not_null<Webrtc::VideoTrack*> track) {
@@ -2171,6 +2177,8 @@ bool GroupCall::tryCreateController() {
 			return result;
 		},
 		.videoContentType = tgcalls::VideoContentType::Generic,
+		.initialEnableNoiseSuppression
+			= settings.groupCallNoiseSuppression(),
 		.requestMediaChannelDescriptions = [=, call = base::make_weak(this)](
 			const std::vector<uint32_t> &ssrcs,
 			std::function<void(
diff --git a/Telegram/SourceFiles/calls/group/calls_group_call.h b/Telegram/SourceFiles/calls/group/calls_group_call.h
index 9d5ae376c..2d66d25f4 100644
--- a/Telegram/SourceFiles/calls/group/calls_group_call.h
+++ b/Telegram/SourceFiles/calls/group/calls_group_call.h
@@ -235,6 +235,7 @@ public:
 	}
 	void startScheduledNow();
 	void toggleScheduleStartSubscribed(bool subscribed);
+	void setNoiseSuppression(bool enabled);
 
 	bool emitShareScreenError();
 	bool emitShareCameraError();
diff --git a/Telegram/SourceFiles/calls/group/calls_group_settings.cpp b/Telegram/SourceFiles/calls/group/calls_group_settings.cpp
index cbea713c7..2253df03d 100644
--- a/Telegram/SourceFiles/calls/group/calls_group_settings.cpp
+++ b/Telegram/SourceFiles/calls/group/calls_group_settings.cpp
@@ -306,6 +306,20 @@ void SettingsBox(
 	//AddDivider(layout);
 	//AddSkip(layout);
 
+	AddButton(
+		layout,
+		tr::lng_group_call_noise_suppression(),
+		st::groupCallSettingsButton
+	)->toggleOn(rpl::single(
+		settings.groupCallNoiseSuppression()
+	))->toggledChanges(
+	) | rpl::start_with_next([=](bool enabled) {
+		Core::App().settings().setGroupCallNoiseSuppression(enabled);
+		call->setNoiseSuppression(enabled);
+		Core::App().saveSettingsDelayed();
+	}, layout->lifetime());
+
+
 	using GlobalShortcut = base::GlobalShortcut;
 	struct PushToTalkState {
 		rpl::variable<QString> recordText = tr::lng_group_call_ptt_shortcut();
diff --git a/Telegram/SourceFiles/core/core_settings.cpp b/Telegram/SourceFiles/core/core_settings.cpp
index 0fa64b74e..ccf1fd6d9 100644
--- a/Telegram/SourceFiles/core/core_settings.cpp
+++ b/Telegram/SourceFiles/core/core_settings.cpp
@@ -195,7 +195,9 @@ QByteArray Settings::serialize() const {
 		for (const auto &[id, variant] : _emojiVariants) {
 			stream << id << quint8(variant);
 		}
-		stream << qint32(_disableOpenGL ? 1 : 0);
+		stream
+			<< qint32(_disableOpenGL ? 1 : 0)
+			<< qint32(_groupCallNoiseSuppression ? 1 : 0);
 	}
 	return result;
 }
@@ -272,6 +274,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
 	std::vector<RecentEmojiId> recentEmojiPreload;
 	base::flat_map<QString, uint8> emojiVariants;
 	qint32 disableOpenGL = _disableOpenGL ? 1 : 0;
+	qint32 groupCallNoiseSuppression = _groupCallNoiseSuppression ? 1 : 0;
 
 	stream >> themesAccentColors;
 	if (!stream.atEnd()) {
@@ -403,6 +406,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
 	if (!stream.atEnd()) {
 		stream >> disableOpenGL;
 	}
+	if (!stream.atEnd()) {
+		stream >> groupCallNoiseSuppression;
+	}
 	if (stream.status() != QDataStream::Ok) {
 		LOG(("App Error: "
 			"Bad data for Core::Settings::constructFromSerialized()"));
@@ -513,6 +519,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
 		Ui::GL::ForceDisable(_disableOpenGL
 			|| Ui::Integration::Instance().openglLastCheckFailed());
 	}
+	_groupCallNoiseSuppression = (groupCallNoiseSuppression == 1);
 }
 
 bool Settings::chatWide() const {
@@ -741,6 +748,8 @@ void Settings::resetOnLastLogout() {
 	_groupCallPushToTalkShortcut = QByteArray();
 	_groupCallPushToTalkDelay = 20;
 
+	_groupCallNoiseSuppression = true;
+
 	//_themesAccentColors = Window::Theme::AccentColors();
 
 	_lastSeenWarningSeen = false;
diff --git a/Telegram/SourceFiles/core/core_settings.h b/Telegram/SourceFiles/core/core_settings.h
index 3c0edd409..a4ebd35cf 100644
--- a/Telegram/SourceFiles/core/core_settings.h
+++ b/Telegram/SourceFiles/core/core_settings.h
@@ -265,6 +265,12 @@ public:
 	void setGroupCallPushToTalkDelay(crl::time delay) {
 		_groupCallPushToTalkDelay = delay;
 	}
+	[[nodiscard]] bool groupCallNoiseSuppression() const {
+		return _groupCallNoiseSuppression;
+	}
+	void setGroupCallNoiseSuppression(bool value) {
+		_groupCallNoiseSuppression = value;
+	}
 	[[nodiscard]] Window::Theme::AccentColors &themesAccentColors() {
 		return _themesAccentColors;
 	}
@@ -594,6 +600,7 @@ private:
 	bool _callAudioDuckingEnabled = true;
 	bool _disableCalls = false;
 	bool _groupCallPushToTalk = false;
+	bool _groupCallNoiseSuppression = true;
 	QByteArray _groupCallPushToTalkShortcut;
 	crl::time _groupCallPushToTalkDelay = 20;
 	Window::Theme::AccentColors _themesAccentColors;