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 #include <ctype.h>
14 #include <stddef.h>
15
16 #include <string>
17
18 namespace cricket {
19
20 // The official registry of RTP parameters is at
21 // http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
22 // The UDP/DTLS and TCP/DTLS prefixes are not registered there.
23
24 // There are multiple variants of the RTP protocol stack, including
25 // UDP/TLS/RTP/SAVPF (WebRTC default), RTP/AVP, RTP/AVPF, RTP/SAVPF,
26 // TCP/DTLS/RTP/SAVPF and so on. We accept anything that has RTP/
27 // embedded in it somewhere as being an RTP protocol.
28 const char kMediaProtocolRtpPrefix[] = "RTP/";
29
30 // Protocol names generated by WebRTC
31 const char kMediaProtocolSctp[] = "SCTP";
32 const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
33 const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
34 const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
35 // RFC5124
36 const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
37 const char kMediaProtocolSavpf[] = "RTP/SAVPF";
38 const char kMediaProtocolAvpf[] = "RTP/AVPF";
39
40 namespace {
41
42 // Protocol names that we tolerate, but do not generate.
43 // We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
44 // but we tolerate "RTP/SAVPF" and "RTP/SAVP" and the "UDP/TLS" and "TCP/TLS"
45 // prefixes in offers we receive, for compatibility.
46 // RFC4585
47 const char kMediaProtocolSavp[] = "RTP/SAVP";
48 const char kMediaProtocolAvp[] = "RTP/AVP";
49
50 const char kMediaProtocolTcpTlsSavpf[] = "TCP/TLS/RTP/SAVPF";
51 const char kMediaProtocolUdpTlsSavpf[] = "UDP/TLS/RTP/SAVPF";
52 const char kMediaProtocolTcpTlsSavp[] = "TCP/TLS/RTP/SAVP";
53 const char kMediaProtocolUdpTlsSavp[] = "UDP/TLS/RTP/SAVP";
54
55 } // namespace
56
IsDtlsSctp(absl::string_view protocol)57 bool IsDtlsSctp(absl::string_view protocol) {
58 return protocol == kMediaProtocolDtlsSctp ||
59 protocol == kMediaProtocolUdpDtlsSctp ||
60 protocol == kMediaProtocolTcpDtlsSctp;
61 }
62
IsPlainSctp(absl::string_view protocol)63 bool IsPlainSctp(absl::string_view protocol) {
64 return protocol == kMediaProtocolSctp;
65 }
66
IsSctpProtocol(absl::string_view protocol)67 bool IsSctpProtocol(absl::string_view protocol) {
68 return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
69 }
70
IsRtpProtocol(absl::string_view protocol)71 bool IsRtpProtocol(absl::string_view protocol) {
72 if (protocol.empty()) {
73 return true;
74 }
75 size_t pos = protocol.find(cricket::kMediaProtocolRtpPrefix);
76 if (pos == std::string::npos) {
77 return false;
78 }
79 // RTP must be at the beginning of a string or not preceded by alpha
80 if (pos == 0 || !isalpha(static_cast<unsigned char>(protocol[pos - 1]))) {
81 return true;
82 }
83 return false;
84 }
85
86 // Note that the below functions support some protocol strings purely for
87 // legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names
88 // and Interoperability.
89
IsDtlsRtp(absl::string_view protocol)90 bool IsDtlsRtp(absl::string_view protocol) {
91 // Most-likely values first.
92 return protocol == kMediaProtocolDtlsSavpf ||
93 protocol == kMediaProtocolTcpTlsSavpf ||
94 protocol == kMediaProtocolUdpTlsSavpf ||
95 protocol == kMediaProtocolUdpTlsSavp ||
96 protocol == kMediaProtocolTcpTlsSavp;
97 }
98
IsPlainRtp(absl::string_view protocol)99 bool IsPlainRtp(absl::string_view protocol) {
100 // Most-likely values first.
101 return protocol == kMediaProtocolSavpf || protocol == kMediaProtocolAvpf ||
102 protocol == kMediaProtocolSavp || protocol == kMediaProtocolAvp;
103 }
104
105 } // namespace cricket
106