Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 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 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 | [diff] [blame] | 13 | #include <ctype.h> |
| 14 | #include <stddef.h> |
| 15 | |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 16 | #include <string> |
| 17 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 18 | namespace cricket { |
| 19 | |
Harald Alvestrand | 1bffe9e | 2021-11-02 10:39:59 | [diff] [blame] | 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 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 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 | |
Harald Alvestrand | 1bffe9e | 2021-11-02 10:39:59 | [diff] [blame] | 30 | // Protocol names generated by WebRTC |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 31 | const char kMediaProtocolSctp[] = "SCTP"; |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 32 | const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP"; |
Harald Alvestrand | 1bffe9e | 2021-11-02 10:39:59 | [diff] [blame] | 33 | const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP"; |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 34 | const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP"; |
Harald Alvestrand | 1bffe9e | 2021-11-02 10:39:59 | [diff] [blame] | 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 |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 56 | |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 57 | bool IsDtlsSctp(absl::string_view protocol) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 58 | return protocol == kMediaProtocolDtlsSctp || |
| 59 | protocol == kMediaProtocolUdpDtlsSctp || |
| 60 | protocol == kMediaProtocolTcpDtlsSctp; |
| 61 | } |
| 62 | |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 63 | bool IsPlainSctp(absl::string_view protocol) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 64 | return protocol == kMediaProtocolSctp; |
| 65 | } |
| 66 | |
Harald Alvestrand | 1bffe9e | 2021-11-02 10:39:59 | [diff] [blame] | 67 | bool IsSctpProtocol(absl::string_view protocol) { |
| 68 | return IsPlainSctp(protocol) || IsDtlsSctp(protocol); |
| 69 | } |
| 70 | |
Niels Möller | d4aa3a3 | 2021-09-29 11:23:01 | [diff] [blame] | 71 | bool IsRtpProtocol(absl::string_view protocol) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 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 |
Niels Möller | e66b83f | 2022-05-30 10:57:41 | [diff] [blame] | 80 | if (pos == 0 || !isalpha(static_cast<unsigned char>(protocol[pos - 1]))) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
Harald Alvestrand | 1bffe9e | 2021-11-02 10:39:59 | [diff] [blame] | 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 | |
| 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 | |
| 99 | bool IsPlainRtp(absl::string_view protocol) { |
| 100 | // Most-likely values first. |
| 101 | return protocol == kMediaProtocolSavpf || protocol == kMediaProtocolAvpf || |
| 102 | protocol == kMediaProtocolSavp || protocol == kMediaProtocolAvp; |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace cricket |