Fixing bug where "mid" wasn't preserved across re-offers.

Review URL: https://codereview.webrtc.org/1529673002

Cr-Commit-Position: refs/heads/master@{#11039}
diff --git a/talk/session/media/mediasession.cc b/talk/session/media/mediasession.cc
index 8db37d0..2dfaa1e 100644
--- a/talk/session/media/mediasession.cc
+++ b/talk/session/media/mediasession.cc
@@ -1556,9 +1556,14 @@
     const AudioCodecs& audio_codecs,
     StreamParamsVec* current_streams,
     SessionDescription* desc) const {
+  const ContentInfo* current_audio_content =
+      GetFirstAudioContent(current_description);
+  std::string content_name =
+      current_audio_content ? current_audio_content->name : CN_AUDIO;
+
   cricket::SecurePolicy sdes_policy =
-      IsDtlsActive(CN_AUDIO, current_description) ?
-          cricket::SEC_DISABLED : secure();
+      IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED
+                                                      : secure();
 
   scoped_ptr<AudioContentDescription> audio(new AudioContentDescription());
   std::vector<std::string> crypto_suites;
@@ -1594,8 +1599,8 @@
     }
   }
 
-  desc->AddContent(CN_AUDIO, NS_JINGLE_RTP, audio.release());
-  if (!AddTransportOffer(CN_AUDIO, options.transport_options,
+  desc->AddContent(content_name, NS_JINGLE_RTP, audio.release());
+  if (!AddTransportOffer(content_name, options.transport_options,
                          current_description, desc)) {
     return false;
   }
@@ -1610,9 +1615,14 @@
     const VideoCodecs& video_codecs,
     StreamParamsVec* current_streams,
     SessionDescription* desc) const {
+  const ContentInfo* current_video_content =
+      GetFirstVideoContent(current_description);
+  std::string content_name =
+      current_video_content ? current_video_content->name : CN_VIDEO;
+
   cricket::SecurePolicy sdes_policy =
-      IsDtlsActive(CN_VIDEO, current_description) ?
-          cricket::SEC_DISABLED : secure();
+      IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED
+                                                      : secure();
 
   scoped_ptr<VideoContentDescription> video(new VideoContentDescription());
   std::vector<std::string> crypto_suites;
@@ -1649,8 +1659,8 @@
     }
   }
 
-  desc->AddContent(CN_VIDEO, NS_JINGLE_RTP, video.release());
-  if (!AddTransportOffer(CN_VIDEO, options.transport_options,
+  desc->AddContent(content_name, NS_JINGLE_RTP, video.release());
+  if (!AddTransportOffer(content_name, options.transport_options,
                          current_description, desc)) {
     return false;
   }
@@ -1671,9 +1681,14 @@
 
   FilterDataCodecs(data_codecs, is_sctp);
 
+  const ContentInfo* current_data_content =
+      GetFirstDataContent(current_description);
+  std::string content_name =
+      current_data_content ? current_data_content->name : CN_DATA;
+
   cricket::SecurePolicy sdes_policy =
-      IsDtlsActive(CN_DATA, current_description) ?
-          cricket::SEC_DISABLED : secure();
+      IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED
+                                                      : secure();
   std::vector<std::string> crypto_suites;
   if (is_sctp) {
     // SDES doesn't make sense for SCTP, so we disable it, and we only
@@ -1703,13 +1718,13 @@
   }
 
   if (is_sctp) {
-    desc->AddContent(CN_DATA, NS_JINGLE_DRAFT_SCTP, data.release());
+    desc->AddContent(content_name, NS_JINGLE_DRAFT_SCTP, data.release());
   } else {
     data->set_bandwidth(options.data_bandwidth);
     SetMediaProtocol(secure_transport, data.get());
-    desc->AddContent(CN_DATA, NS_JINGLE_RTP, data.release());
+    desc->AddContent(content_name, NS_JINGLE_RTP, data.release());
   }
-  if (!AddTransportOffer(CN_DATA, options.transport_options,
+  if (!AddTransportOffer(content_name, options.transport_options,
                          current_description, desc)) {
     return false;
   }
diff --git a/talk/session/media/mediasession_unittest.cc b/talk/session/media/mediasession_unittest.cc
index 72aefc8..abb03e7 100644
--- a/talk/session/media/mediasession_unittest.cc
+++ b/talk/session/media/mediasession_unittest.cc
@@ -69,6 +69,9 @@
 using cricket::AudioContentDescription;
 using cricket::VideoContentDescription;
 using cricket::DataContentDescription;
+using cricket::GetFirstAudioContent;
+using cricket::GetFirstVideoContent;
+using cricket::GetFirstDataContent;
 using cricket::GetFirstAudioContentDescription;
 using cricket::GetFirstVideoContentDescription;
 using cricket::GetFirstDataContentDescription;
@@ -607,6 +610,7 @@
   ASSERT_CRYPTO(dcd, 1U, CS_AES_CM_128_HMAC_SHA1_80);
   EXPECT_EQ(std::string(cricket::kMediaProtocolSavpf), dcd->protocol());
 }
+
 // Create a RTP data offer, and ensure it matches what we expect.
 TEST_F(MediaSessionDescriptionFactoryTest, TestCreateRtpDataOffer) {
   MediaSessionOptions opts;
@@ -2313,3 +2317,30 @@
   audio_content = answer->GetContentByName("audio");
   EXPECT_TRUE(VerifyNoCNCodecs(audio_content));
 }
+
+// Test that the content name ("mid" in SDP) is unchanged when creating a
+// new offer.
+TEST_F(MediaSessionDescriptionFactoryTest,
+       TestContentNameNotChangedInSubsequentOffers) {
+  MediaSessionOptions opts;
+  opts.recv_audio = true;
+  opts.recv_video = true;
+  opts.data_channel_type = cricket::DCT_SCTP;
+  // Create offer and modify the default content names.
+  rtc::scoped_ptr<SessionDescription> offer(f1_.CreateOffer(opts, nullptr));
+  for (ContentInfo& content : offer->contents()) {
+    content.name.append("_modified");
+  }
+
+  rtc::scoped_ptr<SessionDescription> updated_offer(
+      f1_.CreateOffer(opts, offer.get()));
+  const ContentInfo* audio_content = GetFirstAudioContent(updated_offer.get());
+  const ContentInfo* video_content = GetFirstVideoContent(updated_offer.get());
+  const ContentInfo* data_content = GetFirstDataContent(updated_offer.get());
+  ASSERT_TRUE(audio_content != nullptr);
+  ASSERT_TRUE(video_content != nullptr);
+  ASSERT_TRUE(data_content != nullptr);
+  EXPECT_EQ("audio_modified", audio_content->name);
+  EXPECT_EQ("video_modified", video_content->name);
+  EXPECT_EQ("data_modified", data_content->name);
+}