Check if a datachannel in the current local description is an sctp channel before assuming rtp.
When generating an offer from a local description when 'sctp' is not explicitly set in the
media session options, we were generating an offer with an RTP datachannel even though the
channel in the local description was already sctp.

R=pthatcher@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/28819004

git-svn-id: http://webrtc.googlecode.com/svn/trunk/talk@7539 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/session/media/mediasession.cc b/session/media/mediasession.cc
index 54eeffa..79fc151 100644
--- a/session/media/mediasession.cc
+++ b/session/media/mediasession.cc
@@ -1191,8 +1191,14 @@
         }
         video_added = true;
       } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_DATA)) {
-        if (!AddDataContentForOffer(options, current_description, &data_codecs,
-                                    &current_streams, offer.get())) {
+        MediaSessionOptions options_copy(options);
+        if (IsSctp(static_cast<const MediaContentDescription*>(
+                it->description))) {
+          options_copy.data_channel_type = DCT_SCTP;
+        }
+        if (!AddDataContentForOffer(options_copy, current_description,
+                                    &data_codecs, &current_streams,
+                                    offer.get())) {
           return NULL;
         }
         data_added = true;
diff --git a/session/media/mediasession_unittest.cc b/session/media/mediasession_unittest.cc
index ef155f0..e3c678e 100644
--- a/session/media/mediasession_unittest.cc
+++ b/session/media/mediasession_unittest.cc
@@ -628,6 +628,33 @@
   EXPECT_TRUE(offer->GetContentByName("data") != NULL);
 }
 
+// Test creating an sctp data channel from an already generated offer.
+TEST_F(MediaSessionDescriptionFactoryTest, TestCreateImplicitSctpDataOffer) {
+  MediaSessionOptions opts;
+  opts.recv_audio = false;
+  opts.bundle_enabled = true;
+  opts.data_channel_type = cricket::DCT_SCTP;
+  f1_.set_secure(SEC_ENABLED);
+  rtc::scoped_ptr<SessionDescription> offer1(f1_.CreateOffer(opts, NULL));
+  ASSERT_TRUE(offer1.get() != NULL);
+  const ContentInfo* data = offer1->GetContentByName("data");
+  ASSERT_TRUE(data != NULL);
+  const MediaContentDescription* mdesc =
+      static_cast<const MediaContentDescription*>(data->description);
+  ASSERT_EQ(cricket::kMediaProtocolSctp, mdesc->protocol());
+
+  // Now set data_channel_type to 'none' (default) and make sure that the
+  // datachannel type that gets generated from the previous offer, is of the
+  // same type.
+  opts.data_channel_type = cricket::DCT_NONE;
+  rtc::scoped_ptr<SessionDescription> offer2(
+      f1_.CreateOffer(opts, offer1.get()));
+  data = offer2->GetContentByName("data");
+  ASSERT_TRUE(data != NULL);
+  mdesc = static_cast<const MediaContentDescription*>(data->description);
+  EXPECT_EQ(cricket::kMediaProtocolSctp, mdesc->protocol());
+}
+
 // Create an audio, video offer without legacy StreamParams.
 TEST_F(MediaSessionDescriptionFactoryTest,
        TestCreateOfferWithoutLegacyStreams) {