Fix the "Failed unprotect audio RTP packet" error when SCTP is bundled with audio.

BUG=3235
R=juberti@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@6356 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/talk/media/base/rtputils.cc b/talk/media/base/rtputils.cc
index 5215c3b..221d949 100644
--- a/talk/media/base/rtputils.cc
+++ b/talk/media/base/rtputils.cc
@@ -223,4 +223,15 @@
           SetRtpSsrc(data, len, header.ssrc));
 }
 
+bool IsRtpPacket(const void* data, size_t len) {
+  if (len < kMinRtpPacketLen)
+    return false;
+
+  int version = 0;
+  if (!GetRtpVersion(data, len, &version))
+    return false;
+
+  return version == kRtpVersion;
+}
+
 }  // namespace cricket
diff --git a/talk/media/base/rtputils.h b/talk/media/base/rtputils.h
index 6f76866..f653e42 100644
--- a/talk/media/base/rtputils.h
+++ b/talk/media/base/rtputils.h
@@ -74,6 +74,7 @@
 // Assumes version 2, no padding, no extensions, no csrcs.
 bool SetRtpHeader(void* data, size_t len, const RtpHeader& header);
 
+bool IsRtpPacket(const void* data, size_t len);
 }  // namespace cricket
 
 #endif  // TALK_MEDIA_BASE_RTPUTILS_H_
diff --git a/talk/p2p/base/dtlstransportchannel.cc b/talk/p2p/base/dtlstransportchannel.cc
index 416e6e9..ba8e6a7 100644
--- a/talk/p2p/base/dtlstransportchannel.cc
+++ b/talk/p2p/base/dtlstransportchannel.cc
@@ -34,6 +34,7 @@
 #include "talk/base/stream.h"
 #include "talk/base/sslstreamadapter.h"
 #include "talk/base/thread.h"
+#include "talk/media/base/rtputils.h"
 #include "talk/p2p/base/common.h"
 
 namespace cricket {
@@ -41,16 +42,11 @@
 // We don't pull the RTP constants from rtputils.h, to avoid a layer violation.
 static const size_t kDtlsRecordHeaderLen = 13;
 static const size_t kMaxDtlsPacketLen = 2048;
-static const size_t kMinRtpPacketLen = 12;
 
 static bool IsDtlsPacket(const char* data, size_t len) {
   const uint8* u = reinterpret_cast<const uint8*>(data);
   return (len >= kDtlsRecordHeaderLen && (u[0] > 19 && u[0] < 64));
 }
-static bool IsRtpPacket(const char* data, size_t len) {
-  const uint8* u = reinterpret_cast<const uint8*>(data);
-  return (len >= kMinRtpPacketLen && (u[0] & 0xC0) == 0x80);
-}
 
 talk_base::StreamResult StreamInterfaceChannel::Read(void* buffer,
                                                      size_t buffer_len,
diff --git a/talk/session/media/bundlefilter.cc b/talk/session/media/bundlefilter.cc
index 0d7927c..acac53e 100755
--- a/talk/session/media/bundlefilter.cc
+++ b/talk/session/media/bundlefilter.cc
@@ -47,6 +47,11 @@
   // |streams_| is empty, we will allow all rtcp packets pass through provided
   // that they are valid rtcp packets in case that they are for early media.
   if (!rtcp) {
+    // It may not be a RTP packet (e.g. SCTP).
+    if (!IsRtpPacket(data, len)) {
+      return false;
+    }
+
     int payload_type = 0;
     if (!GetRtpPayloadType(data, len, &payload_type)) {
       return false;
diff --git a/talk/session/media/bundlefilter_unittest.cc b/talk/session/media/bundlefilter_unittest.cc
index 0386666..a3e58c1 100755
--- a/talk/session/media/bundlefilter_unittest.cc
+++ b/talk/session/media/bundlefilter_unittest.cc
@@ -105,6 +105,15 @@
     0x81, 0xCE, 0x00, 0x0C, 0x00, 0x00, 0x11, 0x11, 0x00, 0x00, 0x11, 0x11,
 };
 
+// An SCTP packet.
+static const unsigned char kSctpPacket[] = {
+    0x00, 0x01, 0x00, 0x01,
+    0xff, 0xff, 0xff, 0xff,
+    0x00, 0x00, 0x00, 0x00,
+    0x03, 0x00, 0x00, 0x04,
+    0x00, 0x00, 0x00, 0x00,
+};
+
 TEST(BundleFilterTest, AddRemoveStreamTest) {
   cricket::BundleFilter bundle_filter;
   EXPECT_FALSE(bundle_filter.HasStreams());
@@ -194,3 +203,11 @@
       reinterpret_cast<const char*>(kRtcpPacketSrSsrc2),
       sizeof(kRtcpPacketSrSsrc2), true));
 }
+
+TEST(BundleFilterTest, InvalidRtpPacket) {
+  cricket::BundleFilter bundle_filter;
+  EXPECT_TRUE(bundle_filter.AddStream(StreamParams::CreateLegacy(kSsrc1)));
+  EXPECT_FALSE(bundle_filter.DemuxPacket(
+      reinterpret_cast<const char*>(kSctpPacket),
+      sizeof(kSctpPacket), false));
+}