Detect and reject mismatched DataChannel types.

Test is in Chromium:
https://chromium-review.googlesource.com/c/chromium/src/+/1951011

Bug: chromium:1030628
Change-Id: I525d810b504f5b1e9dc05ad17da192f7fae5b07f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/161330
Reviewed-by: Guido Urdaneta <guidou@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30016}
diff --git a/pc/channel.cc b/pc/channel.cc
index fc5337a..285291f 100644
--- a/pc/channel.cc
+++ b/pc/channel.cc
@@ -1177,14 +1177,15 @@
 }
 
 bool RtpDataChannel::CheckDataChannelTypeFromContent(
-    const RtpDataContentDescription* content,
+    const MediaContentDescription* content,
     std::string* error_desc) {
-  bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
-                  (content->protocol() == kMediaProtocolDtlsSctp));
-  // It's been set before, but doesn't match.  That's bad.
-  if (is_sctp) {
-    SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
-                 error_desc);
+  if (!content->as_rtp_data()) {
+    if (content->as_sctp()) {
+      SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
+                   error_desc);
+    } else {
+      SafeSetError("Data channel is not RTP or SCTP.", error_desc);
+    }
     return false;
   }
   return true;
@@ -1203,11 +1204,10 @@
     return false;
   }
 
-  const RtpDataContentDescription* data = content->as_rtp_data();
-
-  if (!CheckDataChannelTypeFromContent(data, error_desc)) {
+  if (!CheckDataChannelTypeFromContent(content, error_desc)) {
     return false;
   }
+  const RtpDataContentDescription* data = content->as_rtp_data();
 
   RtpHeaderExtensions rtp_header_extensions =
       GetFilteredRtpHeaderExtensions(data->rtp_header_extensions());
@@ -1257,22 +1257,17 @@
     return false;
   }
 
-  const RtpDataContentDescription* data = content->as_rtp_data();
-
-  if (!data) {
-    RTC_LOG(LS_INFO) << "Accepting and ignoring non-RTP content description";
-    return true;
+  if (!CheckDataChannelTypeFromContent(content, error_desc)) {
+    return false;
   }
 
+  const RtpDataContentDescription* data = content->as_rtp_data();
+
   // If the remote data doesn't have codecs, it must be empty, so ignore it.
   if (!data->has_codecs()) {
     return true;
   }
 
-  if (!CheckDataChannelTypeFromContent(data, error_desc)) {
-    return false;
-  }
-
   RtpHeaderExtensions rtp_header_extensions =
       GetFilteredRtpHeaderExtensions(data->rtp_header_extensions());
 
diff --git a/pc/channel.h b/pc/channel.h
index f59a204..238a8e2 100644
--- a/pc/channel.h
+++ b/pc/channel.h
@@ -488,7 +488,7 @@
 
   // overrides from BaseChannel
   // Checks that data channel type is RTP.
-  bool CheckDataChannelTypeFromContent(const RtpDataContentDescription* content,
+  bool CheckDataChannelTypeFromContent(const MediaContentDescription* content,
                                        std::string* error_desc);
   bool SetLocalContent_w(const MediaContentDescription* content,
                          webrtc::SdpType type,