Don't reset streams for the FrameEncryptor / FrameDecryptor unless they changed.

This change prevents resets unless someone actually set a FrameEncryptor
/ FrameDecryptor.

Bug: webrtc:9795
Change-Id: I29910b9ecc2f6f8eea371c5961ac7e9780de65d2
Reviewed-on: https://webrtc-review.googlesource.com/c/104901
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Commit-Queue: Benjamin Wright <benwright@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25095}
diff --git a/pc/rtpreceiver.cc b/pc/rtpreceiver.cc
index d4a1f62..d2d7206 100644
--- a/pc/rtpreceiver.cc
+++ b/pc/rtpreceiver.cc
@@ -51,8 +51,8 @@
     rtc::Thread* worker_thread,
     rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,
     cricket::MediaChannel* media_channel) {
-  if (media_channel && ssrc.has_value()) {
-    return worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
+  if (media_channel && frame_decryptor && ssrc.has_value()) {
+    worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
       media_channel->SetFrameDecryptor(*ssrc, frame_decryptor);
     });
   }
@@ -156,9 +156,12 @@
 void AudioRtpReceiver::SetFrameDecryptor(
     rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
   frame_decryptor_ = std::move(frame_decryptor);
-  // Attach the frame decryptor to the media channel if it exists.
-  MaybeAttachFrameDecryptorToMediaChannel(ssrc_, worker_thread_,
-                                          frame_decryptor_, media_channel_);
+  // Special Case: Set the frame decryptor to any value on any existing channel.
+  if (media_channel_ && ssrc_.has_value()) {
+    worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
+      media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
+    });
+  }
 }
 
 rtc::scoped_refptr<FrameDecryptorInterface>
@@ -347,9 +350,12 @@
 void VideoRtpReceiver::SetFrameDecryptor(
     rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
   frame_decryptor_ = std::move(frame_decryptor);
-  // Attach the new frame decryptor the media channel if it exists yet.
-  MaybeAttachFrameDecryptorToMediaChannel(ssrc_, worker_thread_,
-                                          frame_decryptor_, media_channel_);
+  // Special Case: Set the frame decryptor to any value on any existing channel.
+  if (media_channel_ && ssrc_.has_value()) {
+    worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
+      media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
+    });
+  }
 }
 
 rtc::scoped_refptr<FrameDecryptorInterface>
diff --git a/pc/rtpsender.cc b/pc/rtpsender.cc
index 3bb90f2..7493542 100644
--- a/pc/rtpsender.cc
+++ b/pc/rtpsender.cc
@@ -68,13 +68,13 @@
 // correct worker thread only if both the media channel exists and a ssrc has
 // been allocated to the stream.
 void MaybeAttachFrameEncryptorToMediaChannel(
-    const absl::optional<uint32_t> ssrc,
+    const uint32_t ssrc,
     rtc::Thread* worker_thread,
     rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor,
     cricket::MediaChannel* media_channel) {
-  if (media_channel && ssrc.has_value()) {
-    return worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
-      media_channel->SetFrameEncryptor(*ssrc, frame_encryptor);
+  if (media_channel && frame_encryptor && ssrc) {
+    worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
+      media_channel->SetFrameEncryptor(ssrc, frame_encryptor);
     });
   }
 }
@@ -305,8 +305,12 @@
 void AudioRtpSender::SetFrameEncryptor(
     rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) {
   frame_encryptor_ = std::move(frame_encryptor);
-  MaybeAttachFrameEncryptorToMediaChannel(ssrc_, worker_thread_,
-                                          frame_encryptor_, media_channel_);
+  // Special Case: Set the frame encryptor to any value on any existing channel.
+  if (media_channel_ && ssrc_) {
+    worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
+      media_channel_->SetFrameEncryptor(ssrc_, frame_encryptor_);
+    });
+  }
 }
 
 rtc::scoped_refptr<FrameEncryptorInterface> AudioRtpSender::GetFrameEncryptor()
@@ -557,8 +561,12 @@
 void VideoRtpSender::SetFrameEncryptor(
     rtc::scoped_refptr<FrameEncryptorInterface> frame_encryptor) {
   frame_encryptor_ = std::move(frame_encryptor);
-  MaybeAttachFrameEncryptorToMediaChannel(ssrc_, worker_thread_,
-                                          frame_encryptor_, media_channel_);
+  // Special Case: Set the frame encryptor to any value on any existing channel.
+  if (media_channel_ && ssrc_) {
+    worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
+      media_channel_->SetFrameEncryptor(ssrc_, frame_encryptor_);
+    });
+  }
 }
 
 rtc::scoped_refptr<FrameEncryptorInterface> VideoRtpSender::GetFrameEncryptor()