Remove deactivated RTP modules from PacketRouter map.

Apart from making the map smaller, a purpose of this is guaranteeing
that if a module has been deactived it will not receive new packets
from the pacer, which will be needed for deferred sequencing.

Bug: webrtc:11340
Change-Id: I171a13413c5b8d3fa569c2d56bd9a54bff7c7976
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/208542
Commit-Queue: Erik Språng <sprang@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33335}
diff --git a/call/rtp_video_sender.cc b/call/rtp_video_sender.cc
index 8bba2be..732d17a 100644
--- a/call/rtp_video_sender.cc
+++ b/call/rtp_video_sender.cc
@@ -411,18 +411,6 @@
 
   // RTP/RTCP initialization.
 
-  // We add the highest spatial layer first to ensure it'll be prioritized
-  // when sending padding, with the hope that the packet rate will be smaller,
-  // and that it's more important to protect than the lower layers.
-
-  // TODO(nisse): Consider moving registration with PacketRouter last, after the
-  // modules are fully configured.
-  for (const RtpStreamSender& stream : rtp_streams_) {
-    constexpr bool remb_candidate = true;
-    transport->packet_router()->AddSendRtpModule(stream.rtp_rtcp.get(),
-                                                 remb_candidate);
-  }
-
   for (size_t i = 0; i < rtp_config_.extensions.size(); ++i) {
     const std::string& extension = rtp_config_.extensions[i].uri;
     int id = rtp_config_.extensions[i].id;
@@ -463,9 +451,8 @@
 }
 
 RtpVideoSender::~RtpVideoSender() {
-  for (const RtpStreamSender& stream : rtp_streams_) {
-    transport_->packet_router()->RemoveSendRtpModule(stream.rtp_rtcp.get());
-  }
+  SetActiveModulesLocked(
+      std::vector<bool>(rtp_streams_.size(), /*active=*/false));
   transport_->GetStreamFeedbackProvider()->DeRegisterStreamFeedbackObserver(
       this);
 }
@@ -509,10 +496,30 @@
     if (active_modules[i]) {
       active_ = true;
     }
+
+    const bool was_active = rtp_streams_[i].rtp_rtcp->SendingMedia();
+    const bool should_be_active = active_modules[i];
+
     // Sends a kRtcpByeCode when going from true to false.
     rtp_streams_[i].rtp_rtcp->SetSendingStatus(active_modules[i]);
+
+    if (was_active && !should_be_active) {
+      // Disabling media, remove from packet router map to reduce size and
+      // prevent any stray packets in the pacer from asynchronously arriving
+      // to a disabled module.
+      transport_->packet_router()->RemoveSendRtpModule(
+          rtp_streams_[i].rtp_rtcp.get());
+    }
+
     // If set to false this module won't send media.
     rtp_streams_[i].rtp_rtcp->SetSendingMediaStatus(active_modules[i]);
+
+    if (!was_active && should_be_active) {
+      // Turning on media, register with packet router.
+      transport_->packet_router()->AddSendRtpModule(
+          rtp_streams_[i].rtp_rtcp.get(),
+          /*remb_candidate=*/true);
+    }
   }
 }