8000 feat(windows): setVolume for audio track by kamilkarp · Pull Request #1526 · flutter-webrtc/flutter-webrtc · GitHub
[go: up one dir, main page]

Skip to content

feat(windows): setVolume for audio track #1526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions common/cpp/include/flutter_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <list>
#include <memory>
#include <string>
#include <optional>

typedef flutter::EncodableValue EncodableValue;
typedef flutter::EncodableMap EncodableMap;
Expand Down Expand Up @@ -89,6 +90,13 @@ inline double findDouble(const EncodableMap& map, const std::string& key) {
return 0.0;
}

inline std::optional<double> maybeFindDouble(const EncodableMap& map, const std::string& key) {
auto it = map.find(EncodableValue(key));
if (it != map.end() && TypeIs<double>(it->second))
return GetValue<double>(it->second);
return std::nullopt;
}

inline std::vector<uint8_t> findVector(const EncodableMap& map,
const std::string& key) {
auto it = map.find(EncodableValue(key));
Expand Down
42 changes: 42 additions & 0 deletions common/cpp/src/flutter_webrtc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<EncodableMap>(*args);
const std::string trackId = findString(params, "trackId");
const std::optional<double> 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<RTCAudioTrack*>(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");
Expand Down
0