1 /*
2  *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "pc/media_protocol_names.h"
12 
13 namespace cricket {
14 
15 // There are multiple variants of the RTP protocol stack, including
16 // UDP/TLS/RTP/SAVPF (WebRTC default), RTP/AVP, RTP/AVPF, RTP/SAVPF,
17 // TCP/DTLS/RTP/SAVPF and so on. We accept anything that has RTP/
18 // embedded in it somewhere as being an RTP protocol.
19 const char kMediaProtocolRtpPrefix[] = "RTP/";
20 
21 const char kMediaProtocolSctp[] = "SCTP";
22 const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
23 const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
24 const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
25 
IsDtlsSctp(const std::string & protocol)26 bool IsDtlsSctp(const std::string& protocol) {
27   return protocol == kMediaProtocolDtlsSctp ||
28          protocol == kMediaProtocolUdpDtlsSctp ||
29          protocol == kMediaProtocolTcpDtlsSctp;
30 }
31 
IsPlainSctp(const std::string & protocol)32 bool IsPlainSctp(const std::string& protocol) {
33   return protocol == kMediaProtocolSctp;
34 }
35 
IsRtpProtocol(const std::string & protocol)36 bool IsRtpProtocol(const std::string& protocol) {
37   if (protocol.empty()) {
38     return true;
39   }
40   size_t pos = protocol.find(cricket::kMediaProtocolRtpPrefix);
41   if (pos == std::string::npos) {
42     return false;
43   }
44   // RTP must be at the beginning of a string or not preceded by alpha
45   if (pos == 0 || !isalpha(protocol[pos - 1])) {
46     return true;
47   }
48   return false;
49 }
50 
IsSctpProtocol(const std::string & protocol)51 bool IsSctpProtocol(const std::string& protocol) {
52   return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
53 }
54 
55 }  // namespace cricket
56