From 8d5d003bfc10a87e51af9746e6957a02938d2a9e Mon Sep 17 00:00:00 2001 From: Kamil Karpacz Date: Fri, 16 Feb 2024 11:56:31 +0100 Subject: [PATCH] feat(windows): setVolume for audio track --- common/cpp/include/flutter_common.h | 8 ++++++ common/cpp/src/flutter_webrtc.cc | 42 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/common/cpp/include/flutter_common.h b/common/cpp/include/flutter_common.h index 8216181311..f82054d68d 100644 --- a/common/cpp/include/flutter_common.h +++ b/common/cpp/include/flutter_common.h @@ -13,6 +13,7 @@ #include #include #include +#include typedef flutter::EncodableValue EncodableValue; typedef flutter::EncodableMap EncodableMap; @@ -89,6 +90,13 @@ inline double findDouble(const EncodableMap& map, const std::string& key) { return 0.0; } +inline std::optional maybeFindDouble(const EncodableMap& map, const std::string& key) { + auto it = map.find(EncodableValue(key)); + if (it != map.end() && TypeIs(it->second)) + return GetValue(it->second); + return std::nullopt; +} + inline std::vector findVector(const EncodableMap& map, const std::string& key) { auto it = map.find(EncodableValue(key)); diff --git a/common/cpp/src/flutter_webrtc.cc b/common/cpp/src/flutter_webrtc.cc index f820ca0c8f..1c79fff765 100644 --- a/common/cpp/src/flutter_webrtc.cc +++ b/common/cpp/src/flutter_webrtc.cc @@ -488,6 +488,48 @@ void FlutterWebRTC::HandleMethodCall( const std::string track_id = findString(params, "trackId"); MediaStreamTrackSwitchCamera(track_id, std::move(result)); } else if (method_call.method_name().compare("setVolume") == 0) { + auto args = method_call.arguments(); + if (!args) { + result->Error("Bad Arguments", "setVolume() Null arguments received"); + return; + } + + const EncodableMap params = GetValue(*args); + const std::string trackId = findString(params, "trackId"); + const std::optional volume = maybeFindDouble(params, "volume"); + + if (trackId.empty()) { + result->Error("Bad Arguments", "setVolume() Empty track provided"); + return; + } + + if (!volume.has_value()) { + result->Error("Bad Arguments", "setVolume() No volume provided"); + return; + } + + if (volume.value() < 0) { + result->Error("Bad Arguments", "setVolume() Volume must be positive"); + return; + } + + RTCMediaTrack* track = MediaTrackForId(trackId); + if (nullptr == track) { + result->Error("setVolume", "setVolume() Unable to find provided track"); + return; + } + + std::string kind = track->kind().std_string(); + if (0 != kind.compare("audio")) { + result->Error("setVolume", + "setVolume() Only audio tracks can have volume set"); + return; + } + + auto audioTrack = static_cast(track); + audioTrack->SetVolume(volume.value()); + + result->Success(); } else if (method_call.method_name().compare("getLocalDescription") == 0) { if (!method_call.arguments()) { result->Error("Bad Arguments", "Null constraints arguments received");