From f9ae684475ea69724df5f77eec85b24d907e497e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Fri, 15 Mar 2024 14:53:20 -0700 Subject: [PATCH] Handler: allow spatial layers from 0 to N --- include/Producer.hpp | 2 +- src/Handler.cpp | 50 ++++--------------------------- test/src/mediasoupclient.test.cpp | 2 +- 3 files changed, 8 insertions(+), 46 deletions(-) diff --git a/include/Producer.hpp b/include/Producer.hpp index d8be084a..ce3bd5b8 100644 --- a/include/Producer.hpp +++ b/include/Producer.hpp @@ -87,7 +87,7 @@ namespace mediasoupclient // Paused flag. bool paused{ false }; // Video Max spatial layer. - uint8_t maxSpatialLayer{ 0 }; + uint8_t maxSpatialLayer{ (uint8_t) -1 }; // App custom data. nlohmann::json appData; }; diff --git a/src/Handler.cpp b/src/Handler.cpp index d2ff7210..b3f112fd 100644 --- a/src/Handler.cpp +++ b/src/Handler.cpp @@ -519,51 +519,13 @@ namespace mediasoupclient auto* transceiver = localIdIt->second; auto parameters = transceiver->sender()->GetParameters(); - bool hasLowEncoding{ false }; - bool hasMediumEncoding{ false }; - bool hasHighEncoding{ false }; - webrtc::RtpEncodingParameters* lowEncoding{ nullptr }; - webrtc::RtpEncodingParameters* mediumEncoding{ nullptr }; - webrtc::RtpEncodingParameters* highEncoding{ nullptr }; - - if (!parameters.encodings.empty()) + // Edit encodings. Activate only the layers below or equal to spatialLayer. + if (!parameters.encodings.empty() && (spatialLayer < parameters.encodings.size())) { - hasLowEncoding = true; - lowEncoding = ¶meters.encodings[0]; - } - - if (parameters.encodings.size() > 1) - { - hasMediumEncoding = true; - mediumEncoding = ¶meters.encodings[1]; - } - - if (parameters.encodings.size() > 2) - { - hasHighEncoding = true; - highEncoding = ¶meters.encodings[2]; - } - - // Edit encodings. - if (spatialLayer == 1u) - { - hasLowEncoding && (lowEncoding->active = true); - hasMediumEncoding && (mediumEncoding->active = false); - hasHighEncoding && (highEncoding->active = false); - } - - else if (spatialLayer == 2u) - { - hasLowEncoding && (lowEncoding->active = true); - hasMediumEncoding && (mediumEncoding->active = true); - hasHighEncoding && (highEncoding->active = false); - } - - else if (spatialLayer == 3u) - { - hasLowEncoding && (lowEncoding->active = true); - hasMediumEncoding && (mediumEncoding->active = true); - hasHighEncoding && (highEncoding->active = true); + for (int i = parameters.encodings.size() - 1; i >= 0; i--) + { + parameters.encodings[i].active = (i <= spatialLayer); + } } auto result = transceiver->sender()->SetParameters(parameters); diff --git a/test/src/mediasoupclient.test.cpp b/test/src/mediasoupclient.test.cpp index 7ea32375..9285eb8b 100644 --- a/test/src/mediasoupclient.test.cpp +++ b/test/src/mediasoupclient.test.cpp @@ -211,7 +211,7 @@ TEST_CASE("mediasoupclient", "[mediasoupclient]") REQUIRE(audioProducer->GetRtpSender() != nullptr); REQUIRE(audioProducer->GetTrack() == audioTrack); REQUIRE(audioProducer->IsPaused()); - REQUIRE(audioProducer->GetMaxSpatialLayer() == 0); + REQUIRE(audioProducer->GetMaxSpatialLayer() == (uint8_t) -1); REQUIRE(audioProducer->GetAppData() == appData); REQUIRE(audioProducer->GetRtpParameters()["codecs"].size() == 1);