Prevent the frame decryptor being set if the channel is stopped.

This change deals with a race condition if the media channel has been stopped
and is in the process of changing while we get a call to set a FrameDecryptor
or FrameEncryptor.

Bug: webrtc:9926, webrtc:9932
Change-Id: Ie2da2fa1f31f5cb5eb0b481861a7008e635f562d
Reviewed-on: https://webrtc-review.googlesource.com/c/107986
Commit-Queue: Benjamin Wright <benwright@webrtc.org>
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25398}
diff --git a/pc/rtpreceiver.cc b/pc/rtpreceiver.cc
index d2d7206..acea930 100644
--- a/pc/rtpreceiver.cc
+++ b/pc/rtpreceiver.cc
@@ -50,8 +50,9 @@
     const absl::optional<uint32_t>& ssrc,
     rtc::Thread* worker_thread,
     rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,
-    cricket::MediaChannel* media_channel) {
-  if (media_channel && frame_decryptor && ssrc.has_value()) {
+    cricket::MediaChannel* media_channel,
+    bool stopped) {
+  if (media_channel && frame_decryptor && ssrc.has_value() && !stopped) {
     worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
       media_channel->SetFrameDecryptor(*ssrc, frame_decryptor);
     });
@@ -157,7 +158,7 @@
     rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
   frame_decryptor_ = std::move(frame_decryptor);
   // Special Case: Set the frame decryptor to any value on any existing channel.
-  if (media_channel_ && ssrc_.has_value()) {
+  if (media_channel_ && ssrc_.has_value() && !stopped_) {
     worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
       media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
     });
@@ -255,8 +256,8 @@
     RTC_NOTREACHED();
   }
   // Reattach the frame decryptor if we were reconfigured.
-  MaybeAttachFrameDecryptorToMediaChannel(ssrc_, worker_thread_,
-                                          frame_decryptor_, media_channel_);
+  MaybeAttachFrameDecryptorToMediaChannel(
+      ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
 }
 
 void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
@@ -351,7 +352,7 @@
     rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
   frame_decryptor_ = std::move(frame_decryptor);
   // Special Case: Set the frame decryptor to any value on any existing channel.
-  if (media_channel_ && ssrc_.has_value()) {
+  if (media_channel_ && ssrc_.has_value() && !stopped_) {
     worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
       media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
     });
@@ -393,8 +394,8 @@
   ssrc_ = ssrc;
   SetSink(source_->sink());
   // Attach any existing frame decryptor to the media channel.
-  MaybeAttachFrameDecryptorToMediaChannel(ssrc_, worker_thread_,
-                                          frame_decryptor_, media_channel_);
+  MaybeAttachFrameDecryptorToMediaChannel(
+      ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
 }
 
 void VideoRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {