Drop frames in RtpVideoSender::OnEncodedImage if stream disabled

Drops frames if the encoder has been configured with a new set of rtp
streams and a stray frame is returned from an encoder. This can happen with
hardware encoders that may deliver frames on a separate thread than were
they are configured.

This cl disable sending media on the RTP module a video layer is connected to and there by, old frames are dropped.

Bug: webrtc:1200, b/201798527
Change-Id: Id6bcfc3a846f6b8ed3b645cbbde571b819611a75
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/271122
Reviewed-by: Erik Språng <sprang@webrtc.org>
Commit-Queue: Per Kjellander <perkj@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37744}
diff --git a/call/rtp_video_sender.cc b/call/rtp_video_sender.cc
index 1f55eb8..a7e2f69 100644
--- a/call/rtp_video_sender.cc
+++ b/call/rtp_video_sender.cc
@@ -511,7 +511,7 @@
     }
 
     RtpRtcpInterface& rtp_module = *rtp_streams_[i].rtp_rtcp;
-    const bool was_active = rtp_module.SendingMedia();
+    const bool was_active = rtp_module.Sending();
     const bool should_be_active = active_modules[i];
 
     // Sends a kRtcpByeCode when going from true to false.
@@ -669,6 +669,14 @@
       stream_allocation.rtp_stream_index = i;
       rtp_streams_[i].sender_video->SetVideoLayersAllocation(
           std::move(stream_allocation));
+      // Only send video frames on the rtp module if the encoder is configured
+      // to send. This is to prevent stray frames to be sent after an encoder
+      // has been reconfigured.
+      rtp_streams_[i].rtp_rtcp->SetSendingMediaStatus(
+          absl::c_any_of(allocation.active_spatial_layers,
+                         [&i](const VideoLayersAllocation::SpatialLayer layer) {
+                           return layer.rtp_stream_index == static_cast<int>(i);
+                         }));
     }
   }
 }