Also fail CreateOffer and CreateAnswer if there is a session error

Bug: chromium:974509
Change-Id: I952047dcf1e0fe5f3655bd94ea4b47c76655d262
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/143843
Reviewed-by: Bjorn Mellem <mellem@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28375}
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index 2de3075..63d405b 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -1973,6 +1973,17 @@
     return;
   }
 
+  // If a session error has occurred the PeerConnection is in a possibly
+  // inconsistent state so fail right away.
+  if (session_error() != SessionError::kNone) {
+    std::string error_message = GetSessionErrorMsg();
+    RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message;
+    PostCreateSessionDescriptionFailure(
+        observer,
+        RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
+    return;
+  }
+
   if (!ValidateOfferAnswerOptions(options)) {
     std::string error = "CreateOffer called with invalid options.";
     RTC_LOG(LS_ERROR) << error;
@@ -2079,6 +2090,17 @@
     return;
   }
 
+  // If a session error has occurred the PeerConnection is in a possibly
+  // inconsistent state so fail right away.
+  if (session_error() != SessionError::kNone) {
+    std::string error_message = GetSessionErrorMsg();
+    RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message;
+    PostCreateSessionDescriptionFailure(
+        observer,
+        RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
+    return;
+  }
+
   if (!(signaling_state_ == kHaveRemoteOffer ||
         signaling_state_ == kHaveLocalPrAnswer)) {
     std::string error =
diff --git a/pc/peer_connection_crypto_unittest.cc b/pc/peer_connection_crypto_unittest.cc
index cb5c0e5..f32a124 100644
--- a/pc/peer_connection_crypto_unittest.cc
+++ b/pc/peer_connection_crypto_unittest.cc
@@ -720,7 +720,8 @@
   ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
 
   // Create an invalid answer with the other certificate's fingerprint.
-  auto invalid_answer = callee->CreateAnswer();
+  auto valid_answer = callee->CreateAnswer();
+  auto invalid_answer = CloneSessionDescription(valid_answer.get());
   auto* audio_content =
       cricket::GetFirstAudioContent(invalid_answer->description());
   ASSERT_TRUE(audio_content);
@@ -741,7 +742,7 @@
   ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error));
   EXPECT_PRED_FORMAT2(AssertStringContains, error,
                       "Session error code: ERROR_CONTENT.");
-  ASSERT_FALSE(callee->SetLocalDescription(callee->CreateAnswer(), &error));
+  ASSERT_FALSE(callee->SetLocalDescription(std::move(valid_answer), &error));
   EXPECT_PRED_FORMAT2(AssertStringContains, error,
                       "Session error code: ERROR_CONTENT.");
 }