Fix DCHECK crash when processing a remote answer

after the local offer stopped the only transceiver

BUG=None

Change-Id: I563207a26b6f0d8f41e5853521f05215b6a0eb09
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/319520
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#40722}
diff --git a/pc/sdp_offer_answer_unittest.cc b/pc/sdp_offer_answer_unittest.cc
index 03bea2a..14e9557 100644
--- a/pc/sdp_offer_answer_unittest.cc
+++ b/pc/sdp_offer_answer_unittest.cc
@@ -959,4 +959,38 @@
   EXPECT_TRUE(pc->SetRemoteDescription(std::move(offer)));
 }
 
+TEST_F(SdpOfferAnswerTest, RejectsAnswerWithInvalidTransport) {
+  auto pc1 = CreatePeerConnection();
+  pc1->AddAudioTrack("audio_track", {});
+  auto pc2 = CreatePeerConnection();
+  pc2->AddAudioTrack("anotheraudio_track", {});
+
+  auto initial_offer = pc1->CreateOfferAndSetAsLocal();
+  ASSERT_EQ(initial_offer->description()->contents().size(), 1u);
+  auto mid = initial_offer->description()->contents()[0].mid();
+
+  EXPECT_TRUE(pc2->SetRemoteDescription(std::move(initial_offer)));
+  auto initial_answer = pc2->CreateAnswerAndSetAsLocal();
+
+  std::string sdp;
+  initial_answer->ToString(&sdp);
+  EXPECT_TRUE(pc1->SetRemoteDescription(std::move(initial_answer)));
+
+  auto transceivers = pc1->pc()->GetTransceivers();
+  ASSERT_EQ(transceivers.size(), 1u);
+  // This stops the only transport.
+  transceivers[0]->StopStandard();
+
+  auto subsequent_offer = pc1->CreateOfferAndSetAsLocal();
+  // But the remote answers with a non-rejected m-line which is not valid.
+  auto bad_answer = CreateSessionDescription(
+      SdpType::kAnswer,
+      absl::StrReplaceAll(sdp, {{"a=group:BUNDLE " + mid + "\r\n", ""}}));
+
+  RTCError error;
+  pc1->SetRemoteDescription(std::move(bad_answer), &error);
+  EXPECT_FALSE(error.ok());
+  EXPECT_EQ(error.type(), RTCErrorType::INVALID_PARAMETER);
+}
+
 }  // namespace webrtc