Relanding: Adding support for Unified Plan offer/answer negotiation to the mediasession layer.

This layer takes in a simplified "options" struct and the current local description,
and generates a new offer/answer. Previously the options struct assumed there would
only be one media description per media type (audio/video), but it now supports
N number of audio/video descriptions.

The |add_legacy_stream| options is removed from the mediasession.cc/.h
in this CL.

The next step is to add the ability for PeerConnection/WebRtcSession to create
"options" to represent multiple RtpTransceivers, and apply the Unified Plan
descriptions correctly. Right now, only Plan B descriptions will be
generated in unit tests.

BUG=chromium:465349

Review-Url: https://codereview.webrtc.org/2991693002
Cr-Original-Commit-Position: refs/heads/master@{#19343}
Committed: https://chromium.googlesource.com/external/webrtc/+/a77e6bbd30276bdc5b30f2cbc1e92ca181ae76f0
Review-Url: https://codereview.webrtc.org/2991693002
Cr-Commit-Position: refs/heads/master@{#19394}
diff --git a/webrtc/pc/peerconnectioninterface_unittest.cc b/webrtc/pc/peerconnectioninterface_unittest.cc
index 0e52c20..892615a 100644
--- a/webrtc/pc/peerconnectioninterface_unittest.cc
+++ b/webrtc/pc/peerconnectioninterface_unittest.cc
@@ -1184,6 +1184,57 @@
     return audio_desc->streams()[0].cname;
   }
 
+  std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOptions(
+      const RTCOfferAnswerOptions& offer_answer_options) {
+    RTC_DCHECK(pc_);
+    rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer(
+        new rtc::RefCountedObject<MockCreateSessionDescriptionObserver>());
+    pc_->CreateOffer(observer, offer_answer_options);
+    EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
+    return observer->MoveDescription();
+  }
+
+  void CreateOfferWithOptionsAsRemoteDescription(
+      std::unique_ptr<SessionDescriptionInterface>* desc,
+      const RTCOfferAnswerOptions& offer_answer_options) {
+    *desc = CreateOfferWithOptions(offer_answer_options);
+    ASSERT_TRUE(desc != nullptr);
+    std::string sdp;
+    EXPECT_TRUE((*desc)->ToString(&sdp));
+    SessionDescriptionInterface* remote_offer =
+        webrtc::CreateSessionDescription(SessionDescriptionInterface::kOffer,
+                                         sdp, NULL);
+    EXPECT_TRUE(DoSetRemoteDescription(remote_offer));
+    EXPECT_EQ(PeerConnectionInterface::kHaveRemoteOffer, observer_.state_);
+  }
+
+  void CreateOfferWithOptionsAsLocalDescription(
+      std::unique_ptr<SessionDescriptionInterface>* desc,
+      const RTCOfferAnswerOptions& offer_answer_options) {
+    *desc = CreateOfferWithOptions(offer_answer_options);
+    ASSERT_TRUE(desc != nullptr);
+    std::string sdp;
+    EXPECT_TRUE((*desc)->ToString(&sdp));
+    SessionDescriptionInterface* new_offer = webrtc::CreateSessionDescription(
+        SessionDescriptionInterface::kOffer, sdp, NULL);
+
+    EXPECT_TRUE(DoSetLocalDescription(new_offer));
+    EXPECT_EQ(PeerConnectionInterface::kHaveLocalOffer, observer_.state_);
+  }
+
+  bool HasCNCodecs(const cricket::ContentInfo* content) {
+    const cricket::ContentDescription* description = content->description;
+    RTC_DCHECK(description);
+    const cricket::AudioContentDescription* audio_content_desc =
+        static_cast<const cricket::AudioContentDescription*>(description);
+    RTC_DCHECK(audio_content_desc);
+    for (size_t i = 0; i < audio_content_desc->codecs().size(); ++i) {
+      if (audio_content_desc->codecs()[i].name == "CN")
+        return true;
+    }
+    return false;
+  }
+
   std::unique_ptr<rtc::VirtualSocketServer> vss_;
   rtc::AutoSocketServerThread main_;
   cricket::FakePortAllocator* port_allocator_ = nullptr;
@@ -3494,6 +3545,224 @@
   EXPECT_TRUE(pc_->SetBitrate(bitrate).ok());
 }
 
+// The following tests verify that the offer can be created correctly.
+TEST_F(PeerConnectionInterfaceTest,
+       CreateOfferFailsWithInvalidOfferToReceiveAudio) {
+  RTCOfferAnswerOptions rtc_options;
+
+  // Setting offer_to_receive_audio to a value lower than kUndefined or greater
+  // than kMaxOfferToReceiveMedia should be treated as invalid.
+  rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
+  CreatePeerConnection();
+  EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
+
+  rtc_options.offer_to_receive_audio =
+      RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
+  EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
+}
+
+TEST_F(PeerConnectionInterfaceTest,
+       CreateOfferFailsWithInvalidOfferToReceiveVideo) {
+  RTCOfferAnswerOptions rtc_options;
+
+  // Setting offer_to_receive_video to a value lower than kUndefined or greater
+  // than kMaxOfferToReceiveMedia should be treated as invalid.
+  rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
+  CreatePeerConnection();
+  EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
+
+  rtc_options.offer_to_receive_video =
+      RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
+  EXPECT_FALSE(CreateOfferWithOptions(rtc_options));
+}
+
+// Test that the audio and video content will be added to an offer if both
+// |offer_to_receive_audio| and |offer_to_receive_video| options are 1.
+TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioVideoOptions) {
+  RTCOfferAnswerOptions rtc_options;
+  rtc_options.offer_to_receive_audio = 1;
+  rtc_options.offer_to_receive_video = 1;
+
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreatePeerConnection();
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
+  EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
+}
+
+// Test that only audio content will be added to the offer if only
+// |offer_to_receive_audio| options is 1.
+TEST_F(PeerConnectionInterfaceTest, CreateOfferWithAudioOnlyOptions) {
+  RTCOfferAnswerOptions rtc_options;
+  rtc_options.offer_to_receive_audio = 1;
+  rtc_options.offer_to_receive_video = 0;
+
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreatePeerConnection();
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
+  EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
+}
+
+// Test that only video content will be added if only |offer_to_receive_video|
+// options is 1.
+TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVideoOnlyOptions) {
+  RTCOfferAnswerOptions rtc_options;
+  rtc_options.offer_to_receive_audio = 0;
+  rtc_options.offer_to_receive_video = 1;
+
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreatePeerConnection();
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
+  EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
+}
+
+// Test that if |voice_activity_detection| is false, no CN codec is added to the
+// offer.
+TEST_F(PeerConnectionInterfaceTest, CreateOfferWithVADOptions) {
+  RTCOfferAnswerOptions rtc_options;
+  rtc_options.offer_to_receive_audio = 1;
+  rtc_options.offer_to_receive_video = 0;
+
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreatePeerConnection();
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  const cricket::ContentInfo* audio_content =
+      offer->description()->GetContentByName(cricket::CN_AUDIO);
+  ASSERT_TRUE(audio_content);
+  // |voice_activity_detection| is true by default.
+  EXPECT_TRUE(HasCNCodecs(audio_content));
+
+  rtc_options.voice_activity_detection = false;
+  CreatePeerConnection();
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  audio_content = offer->description()->GetContentByName(cricket::CN_AUDIO);
+  ASSERT_TRUE(audio_content);
+  EXPECT_FALSE(HasCNCodecs(audio_content));
+}
+
+// Test that no media content will be added to the offer if using default
+// RTCOfferAnswerOptions.
+TEST_F(PeerConnectionInterfaceTest, CreateOfferWithDefaultOfferAnswerOptions) {
+  RTCOfferAnswerOptions rtc_options;
+
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreatePeerConnection();
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  EXPECT_EQ(nullptr, GetFirstAudioContent(offer->description()));
+  EXPECT_EQ(nullptr, GetFirstVideoContent(offer->description()));
+}
+
+// Test that if |ice_restart| is true, the ufrag/pwd will change, otherwise
+// ufrag/pwd will be the same in the new offer.
+TEST_F(PeerConnectionInterfaceTest, CreateOfferWithIceRestart) {
+  RTCOfferAnswerOptions rtc_options;
+  rtc_options.ice_restart = false;
+  rtc_options.offer_to_receive_audio = 1;
+
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreatePeerConnection();
+  CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
+  auto ufrag1 = offer->description()
+                    ->GetTransportInfoByName(cricket::CN_AUDIO)
+                    ->description.ice_ufrag;
+  auto pwd1 = offer->description()
+                  ->GetTransportInfoByName(cricket::CN_AUDIO)
+                  ->description.ice_pwd;
+
+  // |ice_restart| is false, the ufrag/pwd shouldn't change.
+  CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
+  auto ufrag2 = offer->description()
+                    ->GetTransportInfoByName(cricket::CN_AUDIO)
+                    ->description.ice_ufrag;
+  auto pwd2 = offer->description()
+                  ->GetTransportInfoByName(cricket::CN_AUDIO)
+                  ->description.ice_pwd;
+
+  // |ice_restart| is true, the ufrag/pwd should change.
+  rtc_options.ice_restart = true;
+  CreateOfferWithOptionsAsLocalDescription(&offer, rtc_options);
+  auto ufrag3 = offer->description()
+                    ->GetTransportInfoByName(cricket::CN_AUDIO)
+                    ->description.ice_ufrag;
+  auto pwd3 = offer->description()
+                  ->GetTransportInfoByName(cricket::CN_AUDIO)
+                  ->description.ice_pwd;
+
+  EXPECT_EQ(ufrag1, ufrag2);
+  EXPECT_EQ(pwd1, pwd2);
+  EXPECT_NE(ufrag2, ufrag3);
+  EXPECT_NE(pwd2, pwd3);
+}
+
+// Test that if |use_rtp_mux| is true, the bundling will be enabled in the
+// offer; if it is false, there won't be any bundle group in the offer.
+TEST_F(PeerConnectionInterfaceTest, CreateOfferWithRtpMux) {
+  RTCOfferAnswerOptions rtc_options;
+  rtc_options.offer_to_receive_audio = 1;
+  rtc_options.offer_to_receive_video = 1;
+
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreatePeerConnection();
+
+  rtc_options.use_rtp_mux = true;
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
+  EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
+  EXPECT_TRUE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
+
+  rtc_options.use_rtp_mux = false;
+  offer = CreateOfferWithOptions(rtc_options);
+  ASSERT_TRUE(offer);
+  EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
+  EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
+  EXPECT_FALSE(offer->description()->HasGroup(cricket::GROUP_TYPE_BUNDLE));
+}
+
+// If SetMandatoryReceiveAudio(false) and SetMandatoryReceiveVideo(false) are
+// called for the answer constraints, but an audio and a video section were
+// offered, there will still be an audio and a video section in the answer.
+TEST_F(PeerConnectionInterfaceTest,
+       RejectAudioAndVideoInAnswerWithConstraints) {
+  // Offer both audio and video.
+  RTCOfferAnswerOptions rtc_offer_options;
+  rtc_offer_options.offer_to_receive_audio = 1;
+  rtc_offer_options.offer_to_receive_video = 1;
+
+  CreatePeerConnection();
+  std::unique_ptr<SessionDescriptionInterface> offer;
+  CreateOfferWithOptionsAsRemoteDescription(&offer, rtc_offer_options);
+  EXPECT_NE(nullptr, GetFirstAudioContent(offer->description()));
+  EXPECT_NE(nullptr, GetFirstVideoContent(offer->description()));
+
+  // Since an offer has been created with both audio and video,
+  // Answers will contain the media types that exist in the offer regardless of
+  // the value of |answer_options.has_audio| and |answer_options.has_video|.
+  FakeConstraints answer_c;
+  // Reject both audio and video.
+  answer_c.SetMandatoryReceiveAudio(false);
+  answer_c.SetMandatoryReceiveVideo(false);
+
+  std::unique_ptr<SessionDescriptionInterface> answer;
+  ASSERT_TRUE(DoCreateAnswer(&answer, &answer_c));
+  const cricket::ContentInfo* audio_content =
+      GetFirstAudioContent(answer->description());
+  const cricket::ContentInfo* video_content =
+      GetFirstVideoContent(answer->description());
+  ASSERT_NE(nullptr, audio_content);
+  ASSERT_NE(nullptr, video_content);
+  EXPECT_TRUE(audio_content->rejected);
+  EXPECT_TRUE(video_content->rejected);
+}
+
 class PeerConnectionMediaConfigTest : public testing::Test {
  protected:
   void SetUp() override {
@@ -3502,8 +3771,7 @@
   }
   const cricket::MediaConfig TestCreatePeerConnection(
       const PeerConnectionInterface::RTCConfiguration& config,
-      const MediaConstraintsInterface *constraints) {
-
+      const MediaConstraintsInterface* constraints) {
     rtc::scoped_refptr<PeerConnectionInterface> pc(pcf_->CreatePeerConnection(
         config, constraints, nullptr, nullptr, &observer_));
     EXPECT_TRUE(pc.get());
@@ -3585,177 +3853,6 @@
   EXPECT_TRUE(media_config.video.suspend_below_min_bitrate);
 }
 
-// The following tests verify that session options are created correctly.
-// TODO(deadbeef): Convert these tests to be more end-to-end. Instead of
-// "verify options are converted correctly", should be "pass options into
-// CreateOffer and verify the correct offer is produced."
-
-TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidAudioOption) {
-  RTCOfferAnswerOptions rtc_options;
-  rtc_options.offer_to_receive_audio = RTCOfferAnswerOptions::kUndefined - 1;
-
-  cricket::MediaSessionOptions options;
-  EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
-
-  rtc_options.offer_to_receive_audio =
-      RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
-  EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
-}
-
-TEST(CreateSessionOptionsTest, GetOptionsForOfferWithInvalidVideoOption) {
-  RTCOfferAnswerOptions rtc_options;
-  rtc_options.offer_to_receive_video = RTCOfferAnswerOptions::kUndefined - 1;
-
-  cricket::MediaSessionOptions options;
-  EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
-
-  rtc_options.offer_to_receive_video =
-      RTCOfferAnswerOptions::kMaxOfferToReceiveMedia + 1;
-  EXPECT_FALSE(ExtractMediaSessionOptions(rtc_options, true, &options));
-}
-
-// Test that a MediaSessionOptions is created for an offer if
-// OfferToReceiveAudio and OfferToReceiveVideo options are set.
-TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudioVideo) {
-  RTCOfferAnswerOptions rtc_options;
-  rtc_options.offer_to_receive_audio = 1;
-  rtc_options.offer_to_receive_video = 1;
-
-  cricket::MediaSessionOptions options;
-  EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
-  EXPECT_TRUE(options.has_audio());
-  EXPECT_TRUE(options.has_video());
-  EXPECT_TRUE(options.bundle_enabled);
-}
-
-// Test that a correct MediaSessionOptions is created for an offer if
-// OfferToReceiveAudio is set.
-TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithAudio) {
-  RTCOfferAnswerOptions rtc_options;
-  rtc_options.offer_to_receive_audio = 1;
-
-  cricket::MediaSessionOptions options;
-  EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
-  EXPECT_TRUE(options.has_audio());
-  EXPECT_FALSE(options.has_video());
-  EXPECT_TRUE(options.bundle_enabled);
-}
-
-// Test that a correct MediaSessionOptions is created for an offer if
-// the default OfferOptions are used.
-TEST(CreateSessionOptionsTest, GetDefaultMediaSessionOptionsForOffer) {
-  RTCOfferAnswerOptions rtc_options;
-
-  cricket::MediaSessionOptions options;
-  options.transport_options["audio"] = cricket::TransportOptions();
-  options.transport_options["video"] = cricket::TransportOptions();
-  EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
-  EXPECT_TRUE(options.has_audio());
-  EXPECT_FALSE(options.has_video());
-  EXPECT_TRUE(options.bundle_enabled);
-  EXPECT_TRUE(options.vad_enabled);
-  EXPECT_FALSE(options.transport_options["audio"].ice_restart);
-  EXPECT_FALSE(options.transport_options["video"].ice_restart);
-}
-
-// Test that a correct MediaSessionOptions is created for an offer if
-// OfferToReceiveVideo is set.
-TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithVideo) {
-  RTCOfferAnswerOptions rtc_options;
-  rtc_options.offer_to_receive_audio = 0;
-  rtc_options.offer_to_receive_video = 1;
-
-  cricket::MediaSessionOptions options;
-  EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
-  EXPECT_FALSE(options.has_audio());
-  EXPECT_TRUE(options.has_video());
-  EXPECT_TRUE(options.bundle_enabled);
-}
-
-// Test that a correct MediaSessionOptions is created for an offer if
-// UseRtpMux is set to false.
-TEST(CreateSessionOptionsTest,
-     GetMediaSessionOptionsForOfferWithBundleDisabled) {
-  RTCOfferAnswerOptions rtc_options;
-  rtc_options.offer_to_receive_audio = 1;
-  rtc_options.offer_to_receive_video = 1;
-  rtc_options.use_rtp_mux = false;
-
-  cricket::MediaSessionOptions options;
-  EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
-  EXPECT_TRUE(options.has_audio());
-  EXPECT_TRUE(options.has_video());
-  EXPECT_FALSE(options.bundle_enabled);
-}
-
-// Test that a correct MediaSessionOptions is created to restart ice if
-// IceRestart is set. It also tests that subsequent MediaSessionOptions don't
-// have |audio_transport_options.ice_restart| etc. set.
-TEST(CreateSessionOptionsTest, GetMediaSessionOptionsForOfferWithIceRestart) {
-  RTCOfferAnswerOptions rtc_options;
-  rtc_options.ice_restart = true;
-
-  cricket::MediaSessionOptions options;
-  options.transport_options["audio"] = cricket::TransportOptions();
-  options.transport_options["video"] = cricket::TransportOptions();
-  EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
-  EXPECT_TRUE(options.transport_options["audio"].ice_restart);
-  EXPECT_TRUE(options.transport_options["video"].ice_restart);
-
-  rtc_options = RTCOfferAnswerOptions();
-  EXPECT_TRUE(ExtractMediaSessionOptions(rtc_options, true, &options));
-  EXPECT_FALSE(options.transport_options["audio"].ice_restart);
-  EXPECT_FALSE(options.transport_options["video"].ice_restart);
-}
-
-// Test that the MediaConstraints in an answer don't affect if audio and video
-// is offered in an offer but that if kOfferToReceiveAudio or
-// kOfferToReceiveVideo constraints are true in an offer, the media type will be
-// included in subsequent answers.
-TEST(CreateSessionOptionsTest, MediaConstraintsInAnswer) {
-  FakeConstraints answer_c;
-  answer_c.SetMandatoryReceiveAudio(true);
-  answer_c.SetMandatoryReceiveVideo(true);
-
-  cricket::MediaSessionOptions answer_options;
-  EXPECT_TRUE(ParseConstraintsForAnswer(&answer_c, &answer_options));
-  EXPECT_TRUE(answer_options.has_audio());
-  EXPECT_TRUE(answer_options.has_video());
-
-  RTCOfferAnswerOptions rtc_offer_options;
-
-  cricket::MediaSessionOptions offer_options;
-  EXPECT_TRUE(
-      ExtractMediaSessionOptions(rtc_offer_options, false, &offer_options));
-  EXPECT_TRUE(offer_options.has_audio());
-  EXPECT_TRUE(offer_options.has_video());
-
-  RTCOfferAnswerOptions updated_rtc_offer_options;
-  updated_rtc_offer_options.offer_to_receive_audio = 1;
-  updated_rtc_offer_options.offer_to_receive_video = 1;
-
-  cricket::MediaSessionOptions updated_offer_options;
-  EXPECT_TRUE(ExtractMediaSessionOptions(updated_rtc_offer_options, false,
-                                         &updated_offer_options));
-  EXPECT_TRUE(updated_offer_options.has_audio());
-  EXPECT_TRUE(updated_offer_options.has_video());
-
-  // Since an offer has been created with both audio and video, subsequent
-  // offers and answers should contain both audio and video.
-  // Answers will only contain the media types that exist in the offer
-  // regardless of the value of |updated_answer_options.has_audio| and
-  // |updated_answer_options.has_video|.
-  FakeConstraints updated_answer_c;
-  answer_c.SetMandatoryReceiveAudio(false);
-  answer_c.SetMandatoryReceiveVideo(false);
-
-  cricket::MediaSessionOptions updated_answer_options;
-  EXPECT_TRUE(
-      ParseConstraintsForAnswer(&updated_answer_c, &updated_answer_options));
-  EXPECT_TRUE(updated_answer_options.has_audio());
-  EXPECT_TRUE(updated_answer_options.has_video());
-}
-
 // Tests a few random fields being different.
 TEST(RTCConfigurationTest, ComparisonOperators) {
   PeerConnectionInterface::RTCConfiguration a;