8000 feat(windows): setVolume for audio track (#1526) · baihua666/flutter-webrtc@21d57df · GitHub
[go: up one dir, main page]

Skip to content

Commit 21d57df

Browse files
authored
feat(windows): setVolume for audio track (flutter-webrtc#1526)
1 parent a28b84b commit 21d57df

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

common/cpp/include/flutter_common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <list>
1414
#include <memory>
1515
#include <string>
16+
#include <optional>
1617

1718
typedef flutter::EncodableValue EncodableValue;
1819
typedef flutter::EncodableMap EncodableMap;
@@ -89,6 +90,13 @@ inline double findDouble(const EncodableMap& map, const std::string& key) {
8990
return 0.0;
9091
}
9192

93+
inline std::optional<double> maybeFindDouble(const EncodableMap& map, const std::string& key) {
94+
auto it = map.find(EncodableValue(key));
95+
if (it != map.end() && TypeIs<double>(it->second))
96+
return GetValue<double>(it->second);
97+
return std::nullopt;
98+
}
99+
92100
inline std::vector<uint8_t> findVector(const EncodableMap& map,
93101
const std::string& key) {
94102
auto it = map.find(EncodableValue(key));

common/cpp/src/flutter_webrtc.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,48 @@ void FlutterWebRTC::HandleMethodCall(
488488
const std::string track_id = findString(params, "trackId");
489489
MediaStreamTrackSwitchCamera(track_id, std::move(result));
490490
} else if (method_call.method_name().compare("setVolume") == 0) {
491+
auto args = method_call.arguments();
492+
if (!args) {
493+
result->Error("Bad Arguments", "setVolume() Null arguments received");
494+
return;
495+
}
496+
497+
const EncodableMap params = GetValue<EncodableMap>(*args);
498+
const std::string trackId = findString(params, "trackId");
499+
const std::optional<double> volume = maybeFindDouble(params, "volume");
500+
501+
if (trackId.empty()) {
502+
result->Error("Bad Arguments", "setVolume() Empty track provided");
503+
return;
504+
}
505+
506+
if (!volume.has_value()) {
507+
result->Error("Bad Arguments", "setVolume() No volume provided");
508+
return;
509+
}
510+
511+
if (volume.value() < 0) {
512+
result->Error("Bad Arguments", "setVolume() Volume must be positive");
513+
return;
514+
}
515+
516+
RTCMediaTrack* track = MediaTrackForId(trackId);
517+
if (nullptr == track) {
518+
result->Error("setVolume", "setVolume() Unable to find provided track");
519+
return;
520+
}
521+
522+
std::string kind = track->kind().std_string();
523+
if (0 != kind.compare("audio")) {
524+
result->Error("setVolume",
525+
"setVolume() Only audio tracks can have volume set");
526+
return;
527+
}
528+
529+
auto audioTrack = static_cast<RTCAudioTrack*>(track);
530+
audioTrack->SetVolume(volume.value());
531+
532+
result->Success();
491533
} else if (method_call.method_name().compare("getLocalDescription") == 0) {
492534
if (!method_call.arguments()) {
493535
result->Error("Bad Arguments", "Null constraints arguments received");

0 commit comments

Comments
 (0)
0