henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/media_session.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 13 | #include <algorithm> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | #include <functional> |
| 15 | #include <map> |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 16 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | #include <set> |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 18 | #include <unordered_map> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 19 | #include <utility> |
| 20 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 21 | #include "absl/algorithm/container.h" |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 22 | #include "absl/memory/memory.h" |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 23 | #include "absl/strings/match.h" |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 24 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "api/crypto_params.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "media/base/h264_profile_level_id.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "media/base/media_constants.h" |
| 28 | #include "p2p/base/p2p_constants.h" |
| 29 | #include "pc/channel_manager.h" |
| 30 | #include "pc/rtp_media_utils.h" |
| 31 | #include "pc/srtp_filter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 32 | #include "rtc_base/checks.h" |
| 33 | #include "rtc_base/helpers.h" |
| 34 | #include "rtc_base/logging.h" |
Artem Titov | a76af0c | 2018-07-23 17:38:12 +0200 | [diff] [blame] | 35 | #include "rtc_base/third_party/base64/base64.h" |
Amit Hilbuch | dbb49df | 2019-01-23 14:54:24 -0800 | [diff] [blame] | 36 | #include "rtc_base/unique_id_generator.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
| 38 | namespace { |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 39 | |
Amit Hilbuch | dbb49df | 2019-01-23 14:54:24 -0800 | [diff] [blame] | 40 | using rtc::UniqueRandomIdGenerator; |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 41 | using webrtc::RtpTransceiverDirection; |
| 42 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | const char kInline[] = "inline:"; |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 44 | |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 45 | void GetSupportedSdesCryptoSuiteNames( |
| 46 | void (*func)(const webrtc::CryptoOptions&, std::vector<int>*), |
| 47 | const webrtc::CryptoOptions& crypto_options, |
| 48 | std::vector<std::string>* names) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 49 | std::vector<int> crypto_suites; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 50 | func(crypto_options, &crypto_suites); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 51 | for (const auto crypto : crypto_suites) { |
| 52 | names->push_back(rtc::SrtpCryptoSuiteToName(crypto)); |
| 53 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 54 | } |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 55 | |
terelius | 8c011e5 | 2016-04-26 05:28:11 -0700 | [diff] [blame] | 56 | } // namespace |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | |
| 58 | namespace cricket { |
| 59 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | // RTP Profile names |
| 61 | // http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml |
| 62 | // RFC4585 |
| 63 | const char kMediaProtocolAvpf[] = "RTP/AVPF"; |
| 64 | // RFC5124 |
jiayl@webrtc.org | 8dcd43c | 2014-05-29 22:07:59 +0000 | [diff] [blame] | 65 | const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF"; |
| 66 | |
deadbeef | f393829 | 2015-07-15 12:20:53 -0700 | [diff] [blame] | 67 | // We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP, |
| 68 | // but we tolerate "RTP/SAVPF" in offers we receive, for compatibility. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 69 | const char kMediaProtocolSavpf[] = "RTP/SAVPF"; |
| 70 | |
| 71 | const char kMediaProtocolRtpPrefix[] = "RTP/"; |
| 72 | |
| 73 | const char kMediaProtocolSctp[] = "SCTP"; |
| 74 | const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP"; |
lally@webrtc.org | ec97c65 | 2015-02-24 20:18:48 +0000 | [diff] [blame] | 75 | const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP"; |
lally@webrtc.org | a747093 | 2015-02-24 20:19:21 +0000 | [diff] [blame] | 76 | const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 77 | |
deadbeef | 8b7e9ad | 2017-05-25 09:38:55 -0700 | [diff] [blame] | 78 | // Note that the below functions support some protocol strings purely for |
| 79 | // legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names |
| 80 | // and Interoperability. |
| 81 | |
| 82 | static bool IsDtlsRtp(const std::string& protocol) { |
| 83 | // Most-likely values first. |
| 84 | return protocol == "UDP/TLS/RTP/SAVPF" || protocol == "TCP/TLS/RTP/SAVPF" || |
| 85 | protocol == "UDP/TLS/RTP/SAVP" || protocol == "TCP/TLS/RTP/SAVP"; |
| 86 | } |
| 87 | |
| 88 | static bool IsPlainRtp(const std::string& protocol) { |
| 89 | // Most-likely values first. |
| 90 | return protocol == "RTP/SAVPF" || protocol == "RTP/AVPF" || |
| 91 | protocol == "RTP/SAVP" || protocol == "RTP/AVP"; |
| 92 | } |
| 93 | |
| 94 | static bool IsDtlsSctp(const std::string& protocol) { |
| 95 | return protocol == kMediaProtocolDtlsSctp || |
| 96 | protocol == kMediaProtocolUdpDtlsSctp || |
| 97 | protocol == kMediaProtocolTcpDtlsSctp; |
| 98 | } |
| 99 | |
| 100 | static bool IsPlainSctp(const std::string& protocol) { |
| 101 | return protocol == kMediaProtocolSctp; |
| 102 | } |
| 103 | |
| 104 | static bool IsSctp(const std::string& protocol) { |
| 105 | return IsPlainSctp(protocol) || IsDtlsSctp(protocol); |
| 106 | } |
| 107 | |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 108 | static RtpTransceiverDirection NegotiateRtpTransceiverDirection( |
| 109 | RtpTransceiverDirection offer, |
| 110 | RtpTransceiverDirection wants) { |
| 111 | bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer); |
| 112 | bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer); |
| 113 | bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants); |
| 114 | bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants); |
| 115 | return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send, |
| 116 | offer_send && wants_recv); |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 117 | } |
| 118 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | static bool IsMediaContentOfType(const ContentInfo* content, |
| 120 | MediaType media_type) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 121 | if (!content || !content->media_description()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 124 | return content->media_description()->type() == media_type; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 127 | static bool CreateCryptoParams(int tag, |
| 128 | const std::string& cipher, |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 129 | CryptoParams* crypto_out) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 130 | int key_len; |
| 131 | int salt_len; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 132 | if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher), |
| 133 | &key_len, &salt_len)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | return false; |
| 135 | } |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 136 | |
| 137 | int master_key_len = key_len + salt_len; |
| 138 | std::string master_key; |
| 139 | if (!rtc::CreateRandomData(master_key_len, &master_key)) { |
| 140 | return false; |
| 141 | } |
| 142 | |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 143 | RTC_CHECK_EQ(master_key_len, master_key.size()); |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 144 | std::string key = rtc::Base64::Encode(master_key); |
| 145 | |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 146 | crypto_out->tag = tag; |
| 147 | crypto_out->cipher_suite = cipher; |
| 148 | crypto_out->key_params = kInline; |
| 149 | crypto_out->key_params += key; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | static bool AddCryptoParams(const std::string& cipher_suite, |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 154 | CryptoParamsVec* cryptos_out) { |
| 155 | int size = static_cast<int>(cryptos_out->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 156 | |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 157 | cryptos_out->resize(size + 1); |
| 158 | return CreateCryptoParams(size, cipher_suite, &cryptos_out->at(size)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void AddMediaCryptos(const CryptoParamsVec& cryptos, |
| 162 | MediaContentDescription* media) { |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 163 | for (const CryptoParams& crypto : cryptos) { |
| 164 | media->AddCrypto(crypto); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites, |
| 169 | MediaContentDescription* media) { |
| 170 | CryptoParamsVec cryptos; |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 171 | for (const std::string& crypto_suite : crypto_suites) { |
| 172 | if (!AddCryptoParams(crypto_suite, &cryptos)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | AddMediaCryptos(cryptos, media); |
| 177 | return true; |
| 178 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 179 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 180 | const CryptoParamsVec* GetCryptos(const ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 181 | if (!content || !content->media_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 182 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | } |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 184 | return &content->media_description()->cryptos(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | bool FindMatchingCrypto(const CryptoParamsVec& cryptos, |
| 188 | const CryptoParams& crypto, |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 189 | CryptoParams* crypto_out) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 190 | auto it = absl::c_find_if( |
| 191 | cryptos, [&crypto](const CryptoParams& c) { return crypto.Matches(c); }); |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 192 | if (it == cryptos.end()) { |
| 193 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | } |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 195 | *crypto_out = *it; |
| 196 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Taylor Brandstetter | 5e55fe8 | 2018-03-23 11:50:16 -0700 | [diff] [blame] | 199 | // For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the |
| 200 | // low overhead. |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 201 | void GetSupportedAudioSdesCryptoSuites( |
| 202 | const webrtc::CryptoOptions& crypto_options, |
| 203 | std::vector<int>* crypto_suites) { |
| 204 | if (crypto_options.srtp.enable_gcm_crypto_suites) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 205 | crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM); |
| 206 | crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM); |
| 207 | } |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 208 | if (crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher) { |
Taylor Brandstetter | 5e55fe8 | 2018-03-23 11:50:16 -0700 | [diff] [blame] | 209 | crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32); |
| 210 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 211 | crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | } |
| 213 | |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 214 | void GetSupportedAudioSdesCryptoSuiteNames( |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 215 | const webrtc::CryptoOptions& crypto_options, |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 216 | std::vector<std::string>* crypto_suite_names) { |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 217 | GetSupportedSdesCryptoSuiteNames(GetSupportedAudioSdesCryptoSuites, |
| 218 | crypto_options, crypto_suite_names); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 221 | void GetSupportedVideoSdesCryptoSuites( |
| 222 | const webrtc::CryptoOptions& crypto_options, |
| 223 | std::vector<int>* crypto_suites) { |
| 224 | if (crypto_options.srtp.enable_gcm_crypto_suites) { |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 225 | crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM); |
| 226 | crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM); |
| 227 | } |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 228 | crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 229 | } |
| 230 | |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 231 | void GetSupportedVideoSdesCryptoSuiteNames( |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 232 | const webrtc::CryptoOptions& crypto_options, |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 233 | std::vector<std::string>* crypto_suite_names) { |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 234 | GetSupportedSdesCryptoSuiteNames(GetSupportedVideoSdesCryptoSuites, |
| 235 | crypto_options, crypto_suite_names); |
| 236 | } |
| 237 | |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 238 | void GetSupportedDataSdesCryptoSuites( |
| 239 | const webrtc::CryptoOptions& crypto_options, |
| 240 | std::vector<int>* crypto_suites) { |
| 241 | if (crypto_options.srtp.enable_gcm_crypto_suites) { |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 242 | crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM); |
| 243 | crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM); |
| 244 | } |
| 245 | crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80); |
| 246 | } |
| 247 | |
| 248 | void GetSupportedDataSdesCryptoSuiteNames( |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 249 | const webrtc::CryptoOptions& crypto_options, |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 250 | std::vector<std::string>* crypto_suite_names) { |
| 251 | GetSupportedSdesCryptoSuiteNames(GetSupportedDataSdesCryptoSuites, |
| 252 | crypto_options, crypto_suite_names); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 253 | } |
| 254 | |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 255 | // Support any GCM cipher (if enabled through options). For video support only |
Taylor Brandstetter | 5e55fe8 | 2018-03-23 11:50:16 -0700 | [diff] [blame] | 256 | // 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated (if enabled) unless |
| 257 | // bundle is enabled because it is low overhead. |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 258 | // Pick the crypto in the list that is supported. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 259 | static bool SelectCrypto(const MediaContentDescription* offer, |
| 260 | bool bundle, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 261 | const webrtc::CryptoOptions& crypto_options, |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 262 | CryptoParams* crypto_out) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 263 | bool audio = offer->type() == MEDIA_TYPE_AUDIO; |
| 264 | const CryptoParamsVec& cryptos = offer->cryptos(); |
| 265 | |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 266 | for (const CryptoParams& crypto : cryptos) { |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 267 | if ((crypto_options.srtp.enable_gcm_crypto_suites && |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 268 | rtc::IsGcmCryptoSuiteName(crypto.cipher_suite)) || |
| 269 | rtc::CS_AES_CM_128_HMAC_SHA1_80 == crypto.cipher_suite || |
| 270 | (rtc::CS_AES_CM_128_HMAC_SHA1_32 == crypto.cipher_suite && audio && |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 271 | !bundle && crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher)) { |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 272 | return CreateCryptoParams(crypto.tag, crypto.cipher_suite, crypto_out); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 278 | // Finds all StreamParams of all media types and attach them to stream_params. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 279 | static StreamParamsVec GetCurrentStreamParams( |
| 280 | const std::vector<const ContentInfo*>& active_local_contents) { |
| 281 | StreamParamsVec stream_params; |
| 282 | for (const ContentInfo* content : active_local_contents) { |
| 283 | for (const StreamParams& params : content->media_description()->streams()) { |
| 284 | stream_params.push_back(params); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 287 | return stream_params; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 288 | } |
| 289 | |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 290 | // Filters the data codecs for the data channel type. |
| 291 | void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) { |
| 292 | // Filter RTP codec for SCTP and vice versa. |
solenberg | 9fa4975 | 2016-10-08 13:02:44 -0700 | [diff] [blame] | 293 | const char* codec_name = |
| 294 | sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName; |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 295 | codecs->erase(std::remove_if(codecs->begin(), codecs->end(), |
| 296 | [&codec_name](const DataCodec& codec) { |
Niels Möller | 039743e | 2018-10-23 10:07:25 +0200 | [diff] [blame] | 297 | return absl::EqualsIgnoreCase(codec.name, |
| 298 | codec_name); |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 299 | }), |
| 300 | codecs->end()); |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 301 | } |
| 302 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 303 | template <typename IdStruct> |
| 304 | class UsedIds { |
| 305 | public: |
| 306 | UsedIds(int min_allowed_id, int max_allowed_id) |
| 307 | : min_allowed_id_(min_allowed_id), |
| 308 | max_allowed_id_(max_allowed_id), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 309 | next_id_(max_allowed_id) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 310 | |
| 311 | // Loops through all Id in |ids| and changes its id if it is |
| 312 | // already in use by another IdStruct. Call this methods with all Id |
| 313 | // in a session description to make sure no duplicate ids exists. |
| 314 | // Note that typename Id must be a type of IdStruct. |
| 315 | template <typename Id> |
| 316 | void FindAndSetIdUsed(std::vector<Id>* ids) { |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 317 | for (const Id& id : *ids) { |
| 318 | FindAndSetIdUsed(&id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | // Finds and sets an unused id if the |idstruct| id is already in use. |
| 323 | void FindAndSetIdUsed(IdStruct* idstruct) { |
| 324 | const int original_id = idstruct->id; |
| 325 | int new_id = idstruct->id; |
| 326 | |
| 327 | if (original_id > max_allowed_id_ || original_id < min_allowed_id_) { |
| 328 | // If the original id is not in range - this is an id that can't be |
| 329 | // dynamically changed. |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (IsIdUsed(original_id)) { |
| 334 | new_id = FindUnusedId(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 335 | RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from " |
| 336 | << original_id << " to " << new_id; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 337 | idstruct->id = new_id; |
| 338 | } |
| 339 | SetIdUsed(new_id); |
| 340 | } |
| 341 | |
| 342 | private: |
| 343 | // Returns the first unused id in reverse order. |
| 344 | // This hopefully reduce the risk of more collisions. We want to change the |
| 345 | // default ids as little as possible. |
| 346 | int FindUnusedId() { |
| 347 | while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) { |
| 348 | --next_id_; |
| 349 | } |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 350 | RTC_DCHECK(next_id_ >= min_allowed_id_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 351 | return next_id_; |
| 352 | } |
| 353 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 354 | bool IsIdUsed(int new_id) { return id_set_.find(new_id) != id_set_.end(); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 355 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 356 | void SetIdUsed(int new_id) { id_set_.insert(new_id); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 357 | |
| 358 | const int min_allowed_id_; |
| 359 | const int max_allowed_id_; |
| 360 | int next_id_; |
| 361 | std::set<int> id_set_; |
| 362 | }; |
| 363 | |
| 364 | // Helper class used for finding duplicate RTP payload types among audio, video |
| 365 | // and data codecs. When bundle is used the payload types may not collide. |
| 366 | class UsedPayloadTypes : public UsedIds<Codec> { |
| 367 | public: |
| 368 | UsedPayloadTypes() |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 369 | : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 370 | |
| 371 | private: |
| 372 | static const int kDynamicPayloadTypeMin = 96; |
| 373 | static const int kDynamicPayloadTypeMax = 127; |
| 374 | }; |
| 375 | |
| 376 | // Helper class used for finding duplicate RTP Header extension ids among |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 377 | // audio and video extensions. Only applies to one-byte header extensions at the |
| 378 | // moment. ids > 14 will always be reported as available. |
| 379 | // TODO(kron): This class needs to be refactored when we start to send two-byte |
| 380 | // header extensions. |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 381 | class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 382 | public: |
| 383 | UsedRtpHeaderExtensionIds() |
Johannes Kron | 07ba2b9 | 2018-09-26 13:33:35 +0200 | [diff] [blame] | 384 | : UsedIds<webrtc::RtpExtension>( |
| 385 | webrtc::RtpExtension::kMinId, |
| 386 | webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 387 | |
| 388 | private: |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 389 | }; |
| 390 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 391 | static StreamParams CreateStreamParamsForNewSenderWithSsrcs( |
| 392 | const SenderOptions& sender, |
| 393 | const std::string& rtcp_cname, |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 394 | bool include_rtx_streams, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 395 | bool include_flexfec_stream, |
| 396 | UniqueRandomIdGenerator* ssrc_generator) { |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 397 | StreamParams result; |
| 398 | result.id = sender.track_id; |
| 399 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 400 | // TODO(brandtr): Update when we support multistream protection. |
| 401 | if (include_flexfec_stream && sender.num_sim_layers > 1) { |
| 402 | include_flexfec_stream = false; |
| 403 | RTC_LOG(LS_WARNING) |
| 404 | << "Our FlexFEC implementation only supports protecting " |
| 405 | "a single media streams. This session has multiple " |
| 406 | "media streams however, so no FlexFEC SSRC will be generated."; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 409 | result.GenerateSsrcs(sender.num_sim_layers, include_rtx_streams, |
| 410 | include_flexfec_stream, ssrc_generator); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 411 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 412 | result.cname = rtcp_cname; |
| 413 | result.set_stream_ids(sender.stream_ids); |
| 414 | |
| 415 | return result; |
| 416 | } |
| 417 | |
| 418 | static bool ValidateSimulcastLayers( |
| 419 | const std::vector<RidDescription>& rids, |
| 420 | const SimulcastLayerList& simulcast_layers) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 421 | return absl::c_all_of( |
| 422 | simulcast_layers.GetAllLayers(), [&rids](const SimulcastLayer& layer) { |
| 423 | return absl::c_any_of(rids, [&layer](const RidDescription& rid) { |
| 424 | return rid.rid == layer.rid; |
| 425 | }); |
| 426 | }); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static StreamParams CreateStreamParamsForNewSenderWithRids( |
| 430 | const SenderOptions& sender, |
| 431 | const std::string& rtcp_cname) { |
| 432 | RTC_DCHECK(!sender.rids.empty()); |
| 433 | RTC_DCHECK_EQ(sender.num_sim_layers, 0) |
| 434 | << "RIDs are the compliant way to indicate simulcast."; |
| 435 | RTC_DCHECK(ValidateSimulcastLayers(sender.rids, sender.simulcast_layers)); |
| 436 | StreamParams result; |
| 437 | result.id = sender.track_id; |
| 438 | result.cname = rtcp_cname; |
| 439 | result.set_stream_ids(sender.stream_ids); |
| 440 | |
| 441 | // More than one rid should be signaled. |
| 442 | if (sender.rids.size() > 1) { |
| 443 | result.set_rids(sender.rids); |
| 444 | } |
| 445 | |
| 446 | return result; |
| 447 | } |
| 448 | |
| 449 | // Adds SimulcastDescription if indicated by the media description options. |
| 450 | // MediaContentDescription should already be set up with the send rids. |
| 451 | static void AddSimulcastToMediaDescription( |
| 452 | const MediaDescriptionOptions& media_description_options, |
| 453 | MediaContentDescription* description) { |
| 454 | RTC_DCHECK(description); |
| 455 | |
| 456 | // Check if we are using RIDs in this scenario. |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 457 | if (absl::c_all_of(description->streams(), [](const StreamParams& params) { |
| 458 | return !params.has_rids(); |
| 459 | })) { |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
| 463 | RTC_DCHECK_EQ(1, description->streams().size()) |
| 464 | << "RIDs are only supported in Unified Plan semantics."; |
| 465 | RTC_DCHECK_EQ(1, media_description_options.sender_options.size()); |
| 466 | RTC_DCHECK(description->type() == MediaType::MEDIA_TYPE_AUDIO || |
| 467 | description->type() == MediaType::MEDIA_TYPE_VIDEO); |
| 468 | |
| 469 | // One RID or less indicates that simulcast is not needed. |
| 470 | if (description->streams()[0].rids().size() <= 1) { |
| 471 | return; |
| 472 | } |
| 473 | |
Amit Hilbuch | b7446ed | 2019-01-28 12:25:25 -0800 | [diff] [blame] | 474 | // Only negotiate the send layers. |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 475 | SimulcastDescription simulcast; |
| 476 | simulcast.send_layers() = |
| 477 | media_description_options.sender_options[0].simulcast_layers; |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 478 | description->set_simulcast_description(simulcast); |
| 479 | } |
| 480 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 481 | // Adds a StreamParams for each SenderOptions in |sender_options| to |
| 482 | // content_description. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 483 | // |current_params| - All currently known StreamParams of any media type. |
| 484 | template <class C> |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 485 | static bool AddStreamParams( |
| 486 | const std::vector<SenderOptions>& sender_options, |
| 487 | const std::string& rtcp_cname, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 488 | UniqueRandomIdGenerator* ssrc_generator, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 489 | StreamParamsVec* current_streams, |
| 490 | MediaContentDescriptionImpl<C>* content_description) { |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 491 | // SCTP streams are not negotiated using SDP/ContentDescriptions. |
deadbeef | 8b7e9ad | 2017-05-25 09:38:55 -0700 | [diff] [blame] | 492 | if (IsSctp(content_description->protocol())) { |
Taylor Brandstetter | 1d7a637 | 2016-08-24 13:15:27 -0700 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
Noah Richards | 2e7a098 | 2015-05-18 14:02:54 -0700 | [diff] [blame] | 496 | const bool include_rtx_streams = |
| 497 | ContainsRtxCodec(content_description->codecs()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 498 | |
brandtr | 03d5fb1 | 2016-11-22 03:37:59 -0800 | [diff] [blame] | 499 | const bool include_flexfec_stream = |
| 500 | ContainsFlexfecCodec(content_description->codecs()); |
| 501 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 502 | for (const SenderOptions& sender : sender_options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 503 | // groupid is empty for StreamParams generated using |
| 504 | // MediaSessionDescriptionFactory. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 505 | StreamParams* param = |
| 506 | GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id); |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 507 | if (!param) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 508 | // This is a new sender. |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 509 | StreamParams stream_param = |
| 510 | sender.rids.empty() |
| 511 | ? |
| 512 | // Signal SSRCs and legacy simulcast (if requested). |
| 513 | CreateStreamParamsForNewSenderWithSsrcs( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 514 | sender, rtcp_cname, include_rtx_streams, |
| 515 | include_flexfec_stream, ssrc_generator) |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 516 | : |
| 517 | // Signal RIDs and spec-compliant simulcast (if requested). |
| 518 | CreateStreamParamsForNewSenderWithRids(sender, rtcp_cname); |
| 519 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 520 | content_description->AddStream(stream_param); |
| 521 | |
| 522 | // Store the new StreamParams in current_streams. |
| 523 | // This is necessary so that we can use the CNAME for other media types. |
| 524 | current_streams->push_back(stream_param); |
| 525 | } else { |
deadbeef | 2f425aa | 2017-04-14 10:41:32 -0700 | [diff] [blame] | 526 | // Use existing generated SSRCs/groups, but update the sync_label if |
| 527 | // necessary. This may be needed if a MediaStreamTrack was moved from one |
| 528 | // MediaStream to another. |
Seth Hampson | 845e878 | 2018-03-02 11:34:10 -0800 | [diff] [blame] | 529 | param->set_stream_ids(sender.stream_ids); |
tommi@webrtc.org | 586f2ed | 2015-01-22 23:00:41 +0000 | [diff] [blame] | 530 | content_description->AddStream(*param); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | // Updates the transport infos of the |sdesc| according to the given |
| 537 | // |bundle_group|. The transport infos of the content names within the |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 538 | // |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the |
| 539 | // first content within the |bundle_group|. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 540 | static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group, |
| 541 | SessionDescription* sdesc) { |
| 542 | // The bundle should not be empty. |
| 543 | if (!sdesc || !bundle_group.FirstContentName()) { |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | // We should definitely have a transport for the first content. |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 548 | const std::string& selected_content_name = *bundle_group.FirstContentName(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 549 | const TransportInfo* selected_transport_info = |
| 550 | sdesc->GetTransportInfoByName(selected_content_name); |
| 551 | if (!selected_transport_info) { |
| 552 | return false; |
| 553 | } |
| 554 | |
| 555 | // Set the other contents to use the same ICE credentials. |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 556 | const std::string& selected_ufrag = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 557 | selected_transport_info->description.ice_ufrag; |
jbauch | 083b73f | 2015-07-16 02:46:32 -0700 | [diff] [blame] | 558 | const std::string& selected_pwd = |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 559 | selected_transport_info->description.ice_pwd; |
Taylor Brandstetter | f475d36 | 2016-01-08 15:35:57 -0800 | [diff] [blame] | 560 | ConnectionRole selected_connection_role = |
| 561 | selected_transport_info->description.connection_role; |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 562 | for (TransportInfo& transport_info : sdesc->transport_infos()) { |
| 563 | if (bundle_group.HasContentName(transport_info.content_name) && |
| 564 | transport_info.content_name != selected_content_name) { |
| 565 | transport_info.description.ice_ufrag = selected_ufrag; |
| 566 | transport_info.description.ice_pwd = selected_pwd; |
| 567 | transport_info.description.connection_role = selected_connection_role; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | return true; |
| 571 | } |
| 572 | |
| 573 | // Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and |
| 574 | // sets it to |cryptos|. |
| 575 | static bool GetCryptosByName(const SessionDescription* sdesc, |
| 576 | const std::string& content_name, |
| 577 | CryptoParamsVec* cryptos) { |
| 578 | if (!sdesc || !cryptos) { |
| 579 | return false; |
| 580 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 581 | const ContentInfo* content = sdesc->GetContentByName(content_name); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 582 | if (!content || !content->media_description()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 583 | return false; |
| 584 | } |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 585 | *cryptos = content->media_description()->cryptos(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 589 | // Prunes the |target_cryptos| by removing the crypto params (cipher_suite) |
| 590 | // which are not available in |filter|. |
| 591 | static void PruneCryptos(const CryptoParamsVec& filter, |
| 592 | CryptoParamsVec* target_cryptos) { |
| 593 | if (!target_cryptos) { |
| 594 | return; |
| 595 | } |
tzik | 2199580 | 2018-04-26 17:38:28 +0900 | [diff] [blame] | 596 | |
| 597 | target_cryptos->erase( |
| 598 | std::remove_if(target_cryptos->begin(), target_cryptos->end(), |
| 599 | // Returns true if the |crypto|'s cipher_suite is not |
| 600 | // found in |filter|. |
| 601 | [&filter](const CryptoParams& crypto) { |
| 602 | for (const CryptoParams& entry : filter) { |
| 603 | if (entry.cipher_suite == crypto.cipher_suite) |
| 604 | return false; |
| 605 | } |
| 606 | return true; |
| 607 | }), |
| 608 | target_cryptos->end()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 611 | bool IsRtpProtocol(const std::string& protocol) { |
deadbeef | b5cb19b | 2015-11-23 16:39:12 -0800 | [diff] [blame] | 612 | return protocol.empty() || |
| 613 | (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos); |
| 614 | } |
| 615 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 616 | static bool IsRtpContent(SessionDescription* sdesc, |
| 617 | const std::string& content_name) { |
| 618 | bool is_rtp = false; |
| 619 | ContentInfo* content = sdesc->GetContentByName(content_name); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 620 | if (content && content->media_description()) { |
| 621 | is_rtp = IsRtpProtocol(content->media_description()->protocol()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 622 | } |
| 623 | return is_rtp; |
| 624 | } |
| 625 | |
| 626 | // Updates the crypto parameters of the |sdesc| according to the given |
| 627 | // |bundle_group|. The crypto parameters of all the contents within the |
| 628 | // |bundle_group| should be updated to use the common subset of the |
| 629 | // available cryptos. |
| 630 | static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group, |
| 631 | SessionDescription* sdesc) { |
| 632 | // The bundle should not be empty. |
| 633 | if (!sdesc || !bundle_group.FirstContentName()) { |
| 634 | return false; |
| 635 | } |
| 636 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 637 | bool common_cryptos_needed = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 638 | // Get the common cryptos. |
| 639 | const ContentNames& content_names = bundle_group.content_names(); |
| 640 | CryptoParamsVec common_cryptos; |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 641 | bool first = true; |
| 642 | for (const std::string& content_name : content_names) { |
| 643 | if (!IsRtpContent(sdesc, content_name)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 644 | continue; |
| 645 | } |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 646 | // The common cryptos are needed if any of the content does not have DTLS |
| 647 | // enabled. |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 648 | if (!sdesc->GetTransportInfoByName(content_name)->description.secure()) { |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 649 | common_cryptos_needed = true; |
| 650 | } |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 651 | if (first) { |
| 652 | first = false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 653 | // Initial the common_cryptos with the first content in the bundle group. |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 654 | if (!GetCryptosByName(sdesc, content_name, &common_cryptos)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | if (common_cryptos.empty()) { |
| 658 | // If there's no crypto params, we should just return. |
| 659 | return true; |
| 660 | } |
| 661 | } else { |
| 662 | CryptoParamsVec cryptos; |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 663 | if (!GetCryptosByName(sdesc, content_name, &cryptos)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 664 | return false; |
| 665 | } |
| 666 | PruneCryptos(cryptos, &common_cryptos); |
| 667 | } |
| 668 | } |
| 669 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 670 | if (common_cryptos.empty() && common_cryptos_needed) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 671 | return false; |
| 672 | } |
| 673 | |
| 674 | // Update to use the common cryptos. |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 675 | for (const std::string& content_name : content_names) { |
| 676 | if (!IsRtpContent(sdesc, content_name)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 677 | continue; |
| 678 | } |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 679 | ContentInfo* content = sdesc->GetContentByName(content_name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 680 | if (IsMediaContent(content)) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 681 | MediaContentDescription* media_desc = content->media_description(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 682 | if (!media_desc) { |
| 683 | return false; |
| 684 | } |
| 685 | media_desc->set_cryptos(common_cryptos); |
| 686 | } |
| 687 | } |
| 688 | return true; |
| 689 | } |
| 690 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 691 | static std::vector<const ContentInfo*> GetActiveContents( |
| 692 | const SessionDescription& description, |
| 693 | const MediaSessionOptions& session_options) { |
| 694 | std::vector<const ContentInfo*> active_contents; |
| 695 | for (size_t i = 0; i < description.contents().size(); ++i) { |
| 696 | RTC_DCHECK_LT(i, session_options.media_description_options.size()); |
| 697 | const ContentInfo& content = description.contents()[i]; |
| 698 | const MediaDescriptionOptions& media_options = |
| 699 | session_options.media_description_options[i]; |
| 700 | if (!content.rejected && !media_options.stopped && |
| 701 | content.name == media_options.mid) { |
| 702 | active_contents.push_back(&content); |
| 703 | } |
| 704 | } |
| 705 | return active_contents; |
| 706 | } |
| 707 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 708 | template <class C> |
| 709 | static bool ContainsRtxCodec(const std::vector<C>& codecs) { |
brandtr | 03d5fb1 | 2016-11-22 03:37:59 -0800 | [diff] [blame] | 710 | for (const auto& codec : codecs) { |
| 711 | if (IsRtxCodec(codec)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 712 | return true; |
| 713 | } |
| 714 | } |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | template <class C> |
| 719 | static bool IsRtxCodec(const C& codec) { |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 720 | return absl::EqualsIgnoreCase(codec.name, kRtxCodecName); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 721 | } |
| 722 | |
brandtr | 03d5fb1 | 2016-11-22 03:37:59 -0800 | [diff] [blame] | 723 | template <class C> |
| 724 | static bool ContainsFlexfecCodec(const std::vector<C>& codecs) { |
| 725 | for (const auto& codec : codecs) { |
| 726 | if (IsFlexfecCodec(codec)) { |
| 727 | return true; |
| 728 | } |
| 729 | } |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | template <class C> |
| 734 | static bool IsFlexfecCodec(const C& codec) { |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 735 | return absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName); |
brandtr | 03d5fb1 | 2016-11-22 03:37:59 -0800 | [diff] [blame] | 736 | } |
| 737 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 738 | // Create a media content to be offered for the given |sender_options|, |
| 739 | // according to the given options.rtcp_mux, session_options.is_muc, codecs, |
| 740 | // secure_transport, crypto, and current_streams. If we don't currently have |
| 741 | // crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is |
| 742 | // created (according to crypto_suites). The created content is added to the |
| 743 | // offer. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 744 | template <class C> |
| 745 | static bool CreateMediaContentOffer( |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 746 | const MediaDescriptionOptions& media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 747 | const MediaSessionOptions& session_options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 748 | const std::vector<C>& codecs, |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 749 | const SecurePolicy& secure_policy, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 750 | const CryptoParamsVec* current_cryptos, |
| 751 | const std::vector<std::string>& crypto_suites, |
| 752 | const RtpHeaderExtensions& rtp_extensions, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 753 | UniqueRandomIdGenerator* ssrc_generator, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 754 | StreamParamsVec* current_streams, |
| 755 | MediaContentDescriptionImpl<C>* offer) { |
| 756 | offer->AddCodecs(codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 757 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 758 | offer->set_rtcp_mux(session_options.rtcp_mux_enabled); |
Taylor Brandstetter | 5f0b83b | 2016-03-18 15:02:07 -0700 | [diff] [blame] | 759 | if (offer->type() == cricket::MEDIA_TYPE_VIDEO) { |
| 760 | offer->set_rtcp_reduced_size(true); |
| 761 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 762 | offer->set_rtp_header_extensions(rtp_extensions); |
| 763 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 764 | if (!AddStreamParams(media_description_options.sender_options, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 765 | session_options.rtcp_cname, ssrc_generator, |
| 766 | current_streams, offer)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 767 | return false; |
| 768 | } |
| 769 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 770 | AddSimulcastToMediaDescription(media_description_options, offer); |
| 771 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 772 | if (secure_policy != SEC_DISABLED) { |
| 773 | if (current_cryptos) { |
| 774 | AddMediaCryptos(*current_cryptos, offer); |
| 775 | } |
| 776 | if (offer->cryptos().empty()) { |
| 777 | if (!CreateMediaCryptos(crypto_suites, offer)) { |
| 778 | return false; |
| 779 | } |
| 780 | } |
| 781 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 782 | |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 783 | if (secure_policy == SEC_REQUIRED && offer->cryptos().empty()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 784 | return false; |
| 785 | } |
| 786 | return true; |
| 787 | } |
| 788 | |
| 789 | template <class C> |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 790 | static bool ReferencedCodecsMatch(const std::vector<C>& codecs1, |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 791 | const int codec1_id, |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 792 | const std::vector<C>& codecs2, |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 793 | const int codec2_id) { |
| 794 | const C* codec1 = FindCodecById(codecs1, codec1_id); |
| 795 | const C* codec2 = FindCodecById(codecs2, codec2_id); |
| 796 | return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2); |
changbin.shao@webrtc.org | 2d25b44 | 2015-03-16 04:14:34 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | template <class C> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 800 | static void NegotiateCodecs(const std::vector<C>& local_codecs, |
| 801 | const std::vector<C>& offered_codecs, |
| 802 | std::vector<C>* negotiated_codecs) { |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 803 | for (const C& ours : local_codecs) { |
| 804 | C theirs; |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 805 | // Note that we intentionally only find one matching codec for each of our |
| 806 | // local codecs, in case the remote offer contains duplicate codecs. |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 807 | if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) { |
| 808 | C negotiated = ours; |
| 809 | negotiated.IntersectFeedbackParams(theirs); |
| 810 | if (IsRtxCodec(negotiated)) { |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 811 | const auto apt_it = |
| 812 | theirs.params.find(kCodecParamAssociatedPayloadType); |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 813 | // FindMatchingCodec shouldn't return something with no apt value. |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 814 | RTC_DCHECK(apt_it != theirs.params.end()); |
| 815 | negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 816 | } |
Niels Möller | 039743e | 2018-10-23 10:07:25 +0200 | [diff] [blame] | 817 | if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) { |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 818 | webrtc::H264::GenerateProfileLevelIdForAnswer( |
| 819 | ours.params, theirs.params, &negotiated.params); |
| 820 | } |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 821 | negotiated.id = theirs.id; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 822 | negotiated.name = theirs.name; |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 823 | negotiated_codecs->push_back(std::move(negotiated)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 826 | // RFC3264: Although the answerer MAY list the formats in their desired |
| 827 | // order of preference, it is RECOMMENDED that unless there is a |
| 828 | // specific reason, the answerer list formats in the same relative order |
| 829 | // they were present in the offer. |
| 830 | std::unordered_map<int, int> payload_type_preferences; |
| 831 | int preference = static_cast<int>(offered_codecs.size() + 1); |
| 832 | for (const C& codec : offered_codecs) { |
| 833 | payload_type_preferences[codec.id] = preference--; |
| 834 | } |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 835 | absl::c_sort( |
| 836 | *negotiated_codecs, [&payload_type_preferences](const C& a, const C& b) { |
| 837 | return payload_type_preferences[a.id] > payload_type_preferences[b.id]; |
| 838 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 841 | // Finds a codec in |codecs2| that matches |codec_to_match|, which is |
| 842 | // a member of |codecs1|. If |codec_to_match| is an RTX codec, both |
| 843 | // the codecs themselves and their associated codecs must match. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 844 | template <class C> |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 845 | static bool FindMatchingCodec(const std::vector<C>& codecs1, |
| 846 | const std::vector<C>& codecs2, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 847 | const C& codec_to_match, |
| 848 | C* found_codec) { |
Taylor Brandstetter | 1c34974 | 2017-10-03 18:25:36 -0700 | [diff] [blame] | 849 | // |codec_to_match| should be a member of |codecs1|, in order to look up RTX |
| 850 | // codecs' associated codecs correctly. If not, that's a programming error. |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 851 | RTC_DCHECK(absl::c_any_of(codecs1, [&codec_to_match](const C& codec) { |
| 852 | return &codec == &codec_to_match; |
| 853 | })); |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 854 | for (const C& potential_match : codecs2) { |
| 855 | if (potential_match.Matches(codec_to_match)) { |
| 856 | if (IsRtxCodec(codec_to_match)) { |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 857 | int apt_value_1 = 0; |
| 858 | int apt_value_2 = 0; |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 859 | if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType, |
| 860 | &apt_value_1) || |
| 861 | !potential_match.GetParam(kCodecParamAssociatedPayloadType, |
| 862 | &apt_value_2)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 863 | RTC_LOG(LS_WARNING) << "RTX missing associated payload type."; |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 864 | continue; |
| 865 | } |
| 866 | if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2, |
| 867 | apt_value_2)) { |
| 868 | continue; |
| 869 | } |
| 870 | } |
| 871 | if (found_codec) { |
| 872 | *found_codec = potential_match; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 873 | } |
| 874 | return true; |
| 875 | } |
| 876 | } |
| 877 | return false; |
| 878 | } |
| 879 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 880 | // Find the codec in |codec_list| that |rtx_codec| is associated with. |
| 881 | template <class C> |
| 882 | static const C* GetAssociatedCodec(const std::vector<C>& codec_list, |
| 883 | const C& rtx_codec) { |
| 884 | std::string associated_pt_str; |
| 885 | if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 886 | &associated_pt_str)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 887 | RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name |
| 888 | << " is missing an associated payload type."; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 889 | return nullptr; |
| 890 | } |
| 891 | |
| 892 | int associated_pt; |
| 893 | if (!rtc::FromString(associated_pt_str, &associated_pt)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 894 | RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str |
| 895 | << " of RTX codec " << rtx_codec.name |
| 896 | << " to an integer."; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 897 | return nullptr; |
| 898 | } |
| 899 | |
| 900 | // Find the associated reference codec for the reference RTX codec. |
| 901 | const C* associated_codec = FindCodecById(codec_list, associated_pt); |
| 902 | if (!associated_codec) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 903 | RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type " |
| 904 | << associated_pt << " for RTX codec " << rtx_codec.name |
| 905 | << "."; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 906 | } |
| 907 | return associated_codec; |
| 908 | } |
| 909 | |
| 910 | // Adds all codecs from |reference_codecs| to |offered_codecs| that don't |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 911 | // already exist in |offered_codecs| and ensure the payload types don't |
| 912 | // collide. |
| 913 | template <class C> |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 914 | static void MergeCodecs(const std::vector<C>& reference_codecs, |
| 915 | std::vector<C>* offered_codecs, |
| 916 | UsedPayloadTypes* used_pltypes) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 917 | // Add all new codecs that are not RTX codecs. |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 918 | for (const C& reference_codec : reference_codecs) { |
| 919 | if (!IsRtxCodec(reference_codec) && |
| 920 | !FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
| 921 | reference_codec, nullptr)) { |
| 922 | C codec = reference_codec; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 923 | used_pltypes->FindAndSetIdUsed(&codec); |
| 924 | offered_codecs->push_back(codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | // Add all new RTX codecs. |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 929 | for (const C& reference_codec : reference_codecs) { |
| 930 | if (IsRtxCodec(reference_codec) && |
| 931 | !FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
| 932 | reference_codec, nullptr)) { |
| 933 | C rtx_codec = reference_codec; |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 934 | const C* associated_codec = |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 935 | GetAssociatedCodec(reference_codecs, rtx_codec); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 936 | if (!associated_codec) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 937 | continue; |
| 938 | } |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 939 | // Find a codec in the offered list that matches the reference codec. |
| 940 | // Its payload type may be different than the reference codec. |
| 941 | C matching_codec; |
| 942 | if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 943 | *associated_codec, &matching_codec)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 944 | RTC_LOG(LS_WARNING) |
| 945 | << "Couldn't find matching " << associated_codec->name << " codec."; |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 946 | continue; |
| 947 | } |
| 948 | |
| 949 | rtx_codec.params[kCodecParamAssociatedPayloadType] = |
| 950 | rtc::ToString(matching_codec.id); |
| 951 | used_pltypes->FindAndSetIdUsed(&rtx_codec); |
| 952 | offered_codecs->push_back(rtx_codec); |
| 953 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 957 | static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions, |
| 958 | const webrtc::RtpExtension& ext_to_match, |
| 959 | webrtc::RtpExtension* found_extension) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 960 | auto it = absl::c_find_if( |
| 961 | extensions, [&ext_to_match](const webrtc::RtpExtension& extension) { |
| 962 | // We assume that all URIs are given in a canonical |
| 963 | // format. |
| 964 | return extension.uri == ext_to_match.uri && |
| 965 | extension.encrypt == ext_to_match.encrypt; |
| 966 | }); |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 967 | if (it == extensions.end()) { |
| 968 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 969 | } |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 970 | if (found_extension) { |
| 971 | *found_extension = *it; |
| 972 | } |
| 973 | return true; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 974 | } |
| 975 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 976 | static bool FindByUri(const RtpHeaderExtensions& extensions, |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 977 | const webrtc::RtpExtension& ext_to_match, |
| 978 | webrtc::RtpExtension* found_extension) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 979 | // We assume that all URIs are given in a canonical format. |
| 980 | const webrtc::RtpExtension* found = |
| 981 | webrtc::RtpExtension::FindHeaderExtensionByUri(extensions, |
| 982 | ext_to_match.uri); |
| 983 | if (!found) { |
| 984 | return false; |
| 985 | } |
| 986 | if (found_extension) { |
| 987 | *found_extension = *found; |
| 988 | } |
| 989 | return true; |
| 990 | } |
| 991 | |
| 992 | static bool FindByUriWithEncryptionPreference( |
| 993 | const RtpHeaderExtensions& extensions, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 994 | const webrtc::RtpExtension& ext_to_match, |
| 995 | bool encryption_preference, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 996 | webrtc::RtpExtension* found_extension) { |
| 997 | const webrtc::RtpExtension* unencrypted_extension = nullptr; |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 998 | for (const webrtc::RtpExtension& extension : extensions) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 999 | // We assume that all URIs are given in a canonical format. |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1000 | if (extension.uri == ext_to_match.uri) { |
| 1001 | if (!encryption_preference || extension.encrypt) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1002 | if (found_extension) { |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1003 | *found_extension = extension; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1004 | } |
| 1005 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1006 | } |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1007 | unencrypted_extension = &extension; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1010 | if (unencrypted_extension) { |
| 1011 | if (found_extension) { |
| 1012 | *found_extension = *unencrypted_extension; |
| 1013 | } |
| 1014 | return true; |
| 1015 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1016 | return false; |
| 1017 | } |
| 1018 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1019 | // Adds all extensions from |reference_extensions| to |offered_extensions| that |
| 1020 | // don't already exist in |offered_extensions| and ensure the IDs don't |
| 1021 | // collide. If an extension is added, it's also added to |regular_extensions| or |
| 1022 | // |encrypted_extensions|, and if the extension is in |regular_extensions| or |
| 1023 | // |encrypted_extensions|, its ID is marked as used in |used_ids|. |
| 1024 | // |offered_extensions| is for either audio or video while |regular_extensions| |
| 1025 | // and |encrypted_extensions| are used for both audio and video. There could be |
| 1026 | // overlap between audio extensions and video extensions. |
| 1027 | static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions, |
| 1028 | RtpHeaderExtensions* offered_extensions, |
| 1029 | RtpHeaderExtensions* regular_extensions, |
| 1030 | RtpHeaderExtensions* encrypted_extensions, |
| 1031 | UsedRtpHeaderExtensionIds* used_ids) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 1032 | for (auto reference_extension : reference_extensions) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1033 | if (!FindByUriAndEncryption(*offered_extensions, reference_extension, |
| 1034 | nullptr)) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 1035 | webrtc::RtpExtension existing; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1036 | if (reference_extension.encrypt) { |
| 1037 | if (FindByUriAndEncryption(*encrypted_extensions, reference_extension, |
| 1038 | &existing)) { |
| 1039 | offered_extensions->push_back(existing); |
| 1040 | } else { |
| 1041 | used_ids->FindAndSetIdUsed(&reference_extension); |
| 1042 | encrypted_extensions->push_back(reference_extension); |
| 1043 | offered_extensions->push_back(reference_extension); |
| 1044 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 1045 | } else { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1046 | if (FindByUriAndEncryption(*regular_extensions, reference_extension, |
| 1047 | &existing)) { |
| 1048 | offered_extensions->push_back(existing); |
| 1049 | } else { |
| 1050 | used_ids->FindAndSetIdUsed(&reference_extension); |
| 1051 | regular_extensions->push_back(reference_extension); |
| 1052 | offered_extensions->push_back(reference_extension); |
| 1053 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 1054 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1059 | static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions, |
| 1060 | RtpHeaderExtensions* all_extensions, |
| 1061 | UsedRtpHeaderExtensionIds* used_ids) { |
| 1062 | RtpHeaderExtensions encrypted_extensions; |
| 1063 | for (const webrtc::RtpExtension& extension : *extensions) { |
| 1064 | webrtc::RtpExtension existing; |
| 1065 | // Don't add encrypted extensions again that were already included in a |
| 1066 | // previous offer or regular extensions that are also included as encrypted |
| 1067 | // extensions. |
| 1068 | if (extension.encrypt || |
| 1069 | !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) || |
| 1070 | (FindByUriWithEncryptionPreference(*extensions, extension, true, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1071 | &existing) && |
| 1072 | existing.encrypt)) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
| 1075 | |
| 1076 | if (FindByUri(*all_extensions, extension, &existing)) { |
| 1077 | encrypted_extensions.push_back(existing); |
| 1078 | } else { |
| 1079 | webrtc::RtpExtension encrypted(extension); |
| 1080 | encrypted.encrypt = true; |
| 1081 | used_ids->FindAndSetIdUsed(&encrypted); |
| 1082 | all_extensions->push_back(encrypted); |
| 1083 | encrypted_extensions.push_back(encrypted); |
| 1084 | } |
| 1085 | } |
| 1086 | extensions->insert(extensions->end(), encrypted_extensions.begin(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1087 | encrypted_extensions.end()); |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1090 | static void NegotiateRtpHeaderExtensions( |
| 1091 | const RtpHeaderExtensions& local_extensions, |
| 1092 | const RtpHeaderExtensions& offered_extensions, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1093 | bool enable_encrypted_rtp_header_extensions, |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1094 | RtpHeaderExtensions* negotiated_extensions) { |
| 1095 | // TransportSequenceNumberV2 is not offered by default. The special logic for |
| 1096 | // the TransportSequenceNumber extensions works as follows: |
| 1097 | // Offer Answer |
| 1098 | // V1 V1 if in local_extensions. |
| 1099 | // V1 and V2 V2 regardless of local_extensions. |
| 1100 | // V2 V2 regardless of local_extensions. |
| 1101 | const webrtc::RtpExtension* transport_sequence_number_v2_offer = |
| 1102 | webrtc::RtpExtension::FindHeaderExtensionByUri( |
| 1103 | offered_extensions, |
| 1104 | webrtc::RtpExtension::kTransportSequenceNumberV2Uri); |
| 1105 | |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1106 | for (const webrtc::RtpExtension& ours : local_extensions) { |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 1107 | webrtc::RtpExtension theirs; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1108 | if (FindByUriWithEncryptionPreference( |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1109 | offered_extensions, ours, enable_encrypted_rtp_header_extensions, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1110 | &theirs)) { |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1111 | if (transport_sequence_number_v2_offer && |
| 1112 | ours.uri == webrtc::RtpExtension::kTransportSequenceNumberUri) { |
| 1113 | // Don't respond to |
| 1114 | // http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01 |
| 1115 | // if we get an offer including |
Johannes Kron | 8cc711a | 2019-03-07 22:36:35 +0100 | [diff] [blame^] | 1116 | // http://www.webrtc.org/experiments/rtp-hdrext/transport-wide-cc-02 |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1117 | continue; |
| 1118 | } else { |
| 1119 | // We respond with their RTP header extension id. |
| 1120 | negotiated_extensions->push_back(theirs); |
| 1121 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1122 | } |
| 1123 | } |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1124 | |
| 1125 | if (transport_sequence_number_v2_offer) { |
| 1126 | // Respond that we support kTransportSequenceNumberV2Uri. |
| 1127 | negotiated_extensions->push_back(*transport_sequence_number_v2_offer); |
| 1128 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static void StripCNCodecs(AudioCodecs* audio_codecs) { |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1132 | audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(), |
| 1133 | [](const AudioCodec& codec) { |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 1134 | return absl::EqualsIgnoreCase( |
| 1135 | codec.name, kComfortNoiseCodecName); |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1136 | }), |
| 1137 | audio_codecs->end()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1140 | // Create a media content to be answered for the given |sender_options| |
| 1141 | // according to the given session_options.rtcp_mux, session_options.streams, |
| 1142 | // codecs, crypto, and current_streams. If we don't currently have crypto (in |
| 1143 | // current_cryptos) and it is enabled (in secure_policy), crypto is created |
| 1144 | // (according to crypto_suites). The codecs, rtcp_mux, and crypto are all |
| 1145 | // negotiated with the offer. If the negotiation fails, this method returns |
| 1146 | // false. The created content is added to the offer. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1147 | template <class C> |
| 1148 | static bool CreateMediaContentAnswer( |
| 1149 | const MediaContentDescriptionImpl<C>* offer, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1150 | const MediaDescriptionOptions& media_description_options, |
| 1151 | const MediaSessionOptions& session_options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1152 | const std::vector<C>& local_codecs, |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 1153 | const SecurePolicy& sdes_policy, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1154 | const CryptoParamsVec* current_cryptos, |
| 1155 | const RtpHeaderExtensions& local_rtp_extenstions, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1156 | UniqueRandomIdGenerator* ssrc_generator, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1157 | bool enable_encrypted_rtp_header_extensions, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1158 | StreamParamsVec* current_streams, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1159 | bool bundle_enabled, |
| 1160 | MediaContentDescriptionImpl<C>* answer) { |
| 1161 | std::vector<C> negotiated_codecs; |
| 1162 | NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs); |
| 1163 | answer->AddCodecs(negotiated_codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1164 | answer->set_protocol(offer->protocol()); |
Johannes Kron | 0854eb6 | 2018-10-10 22:33:20 +0200 | [diff] [blame] | 1165 | |
Johannes Kron | 9581bc4 | 2018-10-23 10:17:39 +0200 | [diff] [blame] | 1166 | answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1167 | RtpHeaderExtensions negotiated_rtp_extensions; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1168 | NegotiateRtpHeaderExtensions( |
| 1169 | local_rtp_extenstions, offer->rtp_header_extensions(), |
| 1170 | enable_encrypted_rtp_header_extensions, &negotiated_rtp_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1171 | answer->set_rtp_header_extensions(negotiated_rtp_extensions); |
| 1172 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1173 | answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux()); |
Taylor Brandstetter | 5f0b83b | 2016-03-18 15:02:07 -0700 | [diff] [blame] | 1174 | if (answer->type() == cricket::MEDIA_TYPE_VIDEO) { |
| 1175 | answer->set_rtcp_reduced_size(offer->rtcp_reduced_size()); |
| 1176 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1177 | |
| 1178 | if (sdes_policy != SEC_DISABLED) { |
| 1179 | CryptoParams crypto; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1180 | if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options, |
| 1181 | &crypto)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1182 | if (current_cryptos) { |
| 1183 | FindMatchingCrypto(*current_cryptos, crypto, &crypto); |
| 1184 | } |
| 1185 | answer->AddCrypto(crypto); |
| 1186 | } |
| 1187 | } |
| 1188 | |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 1189 | if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1190 | return false; |
| 1191 | } |
| 1192 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1193 | if (!AddStreamParams(media_description_options.sender_options, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1194 | session_options.rtcp_cname, ssrc_generator, |
| 1195 | current_streams, answer)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1196 | return false; // Something went seriously wrong. |
| 1197 | } |
| 1198 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1199 | AddSimulcastToMediaDescription(media_description_options, answer); |
| 1200 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 1201 | answer->set_direction(NegotiateRtpTransceiverDirection( |
| 1202 | offer->direction(), media_description_options.direction)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | static bool IsMediaProtocolSupported(MediaType type, |
jiayl@webrtc.org | 8dcd43c | 2014-05-29 22:07:59 +0000 | [diff] [blame] | 1207 | const std::string& protocol, |
| 1208 | bool secure_transport) { |
zhihuang | cf5b37c | 2016-05-05 11:44:35 -0700 | [diff] [blame] | 1209 | // Since not all applications serialize and deserialize the media protocol, |
| 1210 | // we will have to accept |protocol| to be empty. |
| 1211 | if (protocol.empty()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1212 | return true; |
| 1213 | } |
jiayl@webrtc.org | 8dcd43c | 2014-05-29 22:07:59 +0000 | [diff] [blame] | 1214 | |
zhihuang | cf5b37c | 2016-05-05 11:44:35 -0700 | [diff] [blame] | 1215 | if (type == MEDIA_TYPE_DATA) { |
| 1216 | // Check for SCTP, but also for RTP for RTP-based data channels. |
| 1217 | // TODO(pthatcher): Remove RTP once RTP-based data channels are gone. |
| 1218 | if (secure_transport) { |
| 1219 | // Most likely scenarios first. |
| 1220 | return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) || |
| 1221 | IsPlainRtp(protocol); |
| 1222 | } else { |
| 1223 | return IsPlainSctp(protocol) || IsPlainRtp(protocol); |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | // Allow for non-DTLS RTP protocol even when using DTLS because that's what |
| 1228 | // JSEP specifies. |
| 1229 | if (secure_transport) { |
| 1230 | // Most likely scenarios first. |
| 1231 | return IsDtlsRtp(protocol) || IsPlainRtp(protocol); |
| 1232 | } else { |
| 1233 | return IsPlainRtp(protocol); |
| 1234 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | static void SetMediaProtocol(bool secure_transport, |
| 1238 | MediaContentDescription* desc) { |
deadbeef | f393829 | 2015-07-15 12:20:53 -0700 | [diff] [blame] | 1239 | if (!desc->cryptos().empty()) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1240 | desc->set_protocol(kMediaProtocolSavpf); |
deadbeef | f393829 | 2015-07-15 12:20:53 -0700 | [diff] [blame] | 1241 | else if (secure_transport) |
| 1242 | desc->set_protocol(kMediaProtocolDtlsSavpf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1243 | else |
| 1244 | desc->set_protocol(kMediaProtocolAvpf); |
| 1245 | } |
| 1246 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1247 | // Gets the TransportInfo of the given |content_name| from the |
| 1248 | // |current_description|. If doesn't exist, returns a new one. |
| 1249 | static const TransportDescription* GetTransportDescription( |
| 1250 | const std::string& content_name, |
| 1251 | const SessionDescription* current_description) { |
| 1252 | const TransportDescription* desc = NULL; |
| 1253 | if (current_description) { |
| 1254 | const TransportInfo* info = |
| 1255 | current_description->GetTransportInfoByName(content_name); |
| 1256 | if (info) { |
| 1257 | desc = &info->description; |
| 1258 | } |
| 1259 | } |
| 1260 | return desc; |
| 1261 | } |
| 1262 | |
| 1263 | // Gets the current DTLS state from the transport description. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1264 | static bool IsDtlsActive(const ContentInfo* content, |
| 1265 | const SessionDescription* current_description) { |
| 1266 | if (!content) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1267 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1268 | } |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1269 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1270 | size_t msection_index = content - ¤t_description->contents()[0]; |
| 1271 | |
| 1272 | if (current_description->transport_infos().size() <= msection_index) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1273 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1274 | } |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1275 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1276 | return current_description->transport_infos()[msection_index] |
| 1277 | .description.secure(); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1280 | void MediaDescriptionOptions::AddAudioSender( |
| 1281 | const std::string& track_id, |
| 1282 | const std::vector<std::string>& stream_ids) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1283 | RTC_DCHECK(type == MEDIA_TYPE_AUDIO); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1284 | AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1287 | void MediaDescriptionOptions::AddVideoSender( |
| 1288 | const std::string& track_id, |
| 1289 | const std::vector<std::string>& stream_ids, |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1290 | const std::vector<RidDescription>& rids, |
| 1291 | const SimulcastLayerList& simulcast_layers, |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1292 | int num_sim_layers) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1293 | RTC_DCHECK(type == MEDIA_TYPE_VIDEO); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1294 | RTC_DCHECK(rids.empty() || num_sim_layers == 0) |
| 1295 | << "RIDs are the compliant way to indicate simulcast."; |
| 1296 | RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers)); |
| 1297 | AddSenderInternal(track_id, stream_ids, rids, simulcast_layers, |
| 1298 | num_sim_layers); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1301 | void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id, |
| 1302 | const std::string& stream_id) { |
| 1303 | RTC_DCHECK(type == MEDIA_TYPE_DATA); |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1304 | // TODO(steveanton): Is it the case that RtpDataChannel will never have more |
| 1305 | // than one stream? |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1306 | AddSenderInternal(track_id, {stream_id}, {}, SimulcastLayerList(), 1); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1309 | void MediaDescriptionOptions::AddSenderInternal( |
| 1310 | const std::string& track_id, |
| 1311 | const std::vector<std::string>& stream_ids, |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1312 | const std::vector<RidDescription>& rids, |
| 1313 | const SimulcastLayerList& simulcast_layers, |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1314 | int num_sim_layers) { |
| 1315 | // TODO(steveanton): Support any number of stream ids. |
| 1316 | RTC_CHECK(stream_ids.size() == 1U); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1317 | SenderOptions options; |
| 1318 | options.track_id = track_id; |
| 1319 | options.stream_ids = stream_ids; |
| 1320 | options.simulcast_layers = simulcast_layers; |
| 1321 | options.rids = rids; |
| 1322 | options.num_sim_layers = num_sim_layers; |
| 1323 | sender_options.push_back(options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1326 | bool MediaSessionOptions::HasMediaDescription(MediaType type) const { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 1327 | return absl::c_any_of( |
| 1328 | media_description_options, |
| 1329 | [type](const MediaDescriptionOptions& t) { return t.type == type; }); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1332 | MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1333 | const TransportDescriptionFactory* transport_desc_factory, |
| 1334 | rtc::UniqueRandomIdGenerator* ssrc_generator) |
| 1335 | : ssrc_generator_(ssrc_generator), |
| 1336 | transport_desc_factory_(transport_desc_factory) { |
| 1337 | RTC_DCHECK(ssrc_generator_); |
| 1338 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1339 | |
| 1340 | MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( |
| 1341 | ChannelManager* channel_manager, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1342 | const TransportDescriptionFactory* transport_desc_factory, |
| 1343 | rtc::UniqueRandomIdGenerator* ssrc_generator) |
| 1344 | : MediaSessionDescriptionFactory(transport_desc_factory, ssrc_generator) { |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 1345 | channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_); |
| 1346 | channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1347 | channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_); |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 1348 | channel_manager->GetSupportedVideoCodecs(&video_codecs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1349 | channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_); |
| 1350 | channel_manager->GetSupportedDataCodecs(&data_codecs_); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1351 | ComputeAudioCodecsIntersectionAndUnion(); |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 1354 | const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs() |
| 1355 | const { |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1356 | return audio_sendrecv_codecs_; |
| 1357 | } |
| 1358 | |
| 1359 | const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const { |
| 1360 | return audio_send_codecs_; |
| 1361 | } |
| 1362 | |
| 1363 | const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const { |
| 1364 | return audio_recv_codecs_; |
| 1365 | } |
| 1366 | |
| 1367 | void MediaSessionDescriptionFactory::set_audio_codecs( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1368 | const AudioCodecs& send_codecs, |
| 1369 | const AudioCodecs& recv_codecs) { |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1370 | audio_send_codecs_ = send_codecs; |
| 1371 | audio_recv_codecs_ = recv_codecs; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1372 | ComputeAudioCodecsIntersectionAndUnion(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1375 | static void AddUnifiedPlanExtensions(RtpHeaderExtensions* extensions) { |
| 1376 | RTC_DCHECK(extensions); |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 1377 | |
| 1378 | rtc::UniqueNumberGenerator<int> unique_id_generator; |
| 1379 | unique_id_generator.AddKnownId(0); // The first valid RTP extension ID is 1. |
| 1380 | for (const webrtc::RtpExtension& extension : *extensions) { |
| 1381 | const bool collision_free = unique_id_generator.AddKnownId(extension.id); |
| 1382 | RTC_DCHECK(collision_free); |
| 1383 | } |
| 1384 | |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1385 | // Unified Plan also offers the MID and RID header extensions. |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 1386 | extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kMidUri, |
| 1387 | unique_id_generator())); |
| 1388 | extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kRidUri, |
| 1389 | unique_id_generator())); |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1390 | extensions->push_back(webrtc::RtpExtension( |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 1391 | webrtc::RtpExtension::kRepairedRidUri, unique_id_generator())); |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | RtpHeaderExtensions |
| 1395 | MediaSessionDescriptionFactory::audio_rtp_header_extensions() const { |
| 1396 | RtpHeaderExtensions extensions = audio_rtp_extensions_; |
| 1397 | if (is_unified_plan_) { |
| 1398 | AddUnifiedPlanExtensions(&extensions); |
| 1399 | } |
| 1400 | |
| 1401 | return extensions; |
| 1402 | } |
| 1403 | |
| 1404 | RtpHeaderExtensions |
| 1405 | MediaSessionDescriptionFactory::video_rtp_header_extensions() const { |
| 1406 | RtpHeaderExtensions extensions = video_rtp_extensions_; |
| 1407 | if (is_unified_plan_) { |
| 1408 | AddUnifiedPlanExtensions(&extensions); |
| 1409 | } |
| 1410 | |
| 1411 | return extensions; |
| 1412 | } |
| 1413 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1414 | std::unique_ptr<SessionDescription> MediaSessionDescriptionFactory::CreateOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1415 | const MediaSessionOptions& session_options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1416 | const SessionDescription* current_description) const { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1417 | // Must have options for each existing section. |
| 1418 | if (current_description) { |
| 1419 | RTC_DCHECK_LE(current_description->contents().size(), |
| 1420 | session_options.media_description_options.size()); |
| 1421 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1422 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1423 | IceCredentialsIterator ice_credentials( |
| 1424 | session_options.pooled_ice_credentials); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1425 | |
| 1426 | std::vector<const ContentInfo*> current_active_contents; |
| 1427 | if (current_description) { |
| 1428 | current_active_contents = |
| 1429 | GetActiveContents(*current_description, session_options); |
| 1430 | } |
| 1431 | |
| 1432 | StreamParamsVec current_streams = |
| 1433 | GetCurrentStreamParams(current_active_contents); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1434 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1435 | AudioCodecs offer_audio_codecs; |
| 1436 | VideoCodecs offer_video_codecs; |
| 1437 | DataCodecs offer_data_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1438 | GetCodecsForOffer(current_active_contents, &offer_audio_codecs, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1439 | &offer_video_codecs, &offer_data_codecs); |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1440 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1441 | if (!session_options.vad_enabled) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1442 | // If application doesn't want CN codecs in offer. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1443 | StripCNCodecs(&offer_audio_codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1444 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1445 | FilterDataCodecs(&offer_data_codecs, |
| 1446 | session_options.data_channel_type == DCT_SCTP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1447 | |
| 1448 | RtpHeaderExtensions audio_rtp_extensions; |
| 1449 | RtpHeaderExtensions video_rtp_extensions; |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1450 | GetRtpHdrExtsToOffer(current_active_contents, &audio_rtp_extensions, |
| 1451 | &video_rtp_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1452 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1453 | auto offer = absl::make_unique<SessionDescription>(); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 1454 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1455 | // Iterate through the media description options, matching with existing media |
| 1456 | // descriptions in |current_description|. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1457 | size_t msection_index = 0; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1458 | for (const MediaDescriptionOptions& media_description_options : |
| 1459 | session_options.media_description_options) { |
| 1460 | const ContentInfo* current_content = nullptr; |
| 1461 | if (current_description && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1462 | msection_index < current_description->contents().size()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1463 | current_content = ¤t_description->contents()[msection_index]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1464 | // Media type must match unless this media section is being recycled. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1465 | RTC_DCHECK(current_content->name != media_description_options.mid || |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1466 | IsMediaContentOfType(current_content, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1467 | media_description_options.type)); |
| 1468 | } |
| 1469 | switch (media_description_options.type) { |
| 1470 | case MEDIA_TYPE_AUDIO: |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1471 | if (!AddAudioContentForOffer( |
| 1472 | media_description_options, session_options, current_content, |
| 1473 | current_description, audio_rtp_extensions, offer_audio_codecs, |
| 1474 | ¤t_streams, offer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1475 | return nullptr; |
| 1476 | } |
| 1477 | break; |
| 1478 | case MEDIA_TYPE_VIDEO: |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1479 | if (!AddVideoContentForOffer( |
| 1480 | media_description_options, session_options, current_content, |
| 1481 | current_description, video_rtp_extensions, offer_video_codecs, |
| 1482 | ¤t_streams, offer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1483 | return nullptr; |
| 1484 | } |
| 1485 | break; |
| 1486 | case MEDIA_TYPE_DATA: |
| 1487 | if (!AddDataContentForOffer(media_description_options, session_options, |
| 1488 | current_content, current_description, |
| 1489 | offer_data_codecs, ¤t_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1490 | offer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1491 | return nullptr; |
| 1492 | } |
| 1493 | break; |
| 1494 | default: |
| 1495 | RTC_NOTREACHED(); |
| 1496 | } |
| 1497 | ++msection_index; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | // Bundle the contents together, if we've been asked to do so, and update any |
| 1501 | // parameters that need to be tweaked for BUNDLE. |
Steve Anton | 2bed397 | 2019-01-04 17:04:30 -0800 | [diff] [blame] | 1502 | if (session_options.bundle_enabled) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1503 | ContentGroup offer_bundle(GROUP_TYPE_BUNDLE); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1504 | for (const ContentInfo& content : offer->contents()) { |
Steve Anton | 2bed397 | 2019-01-04 17:04:30 -0800 | [diff] [blame] | 1505 | if (content.rejected) { |
| 1506 | continue; |
| 1507 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1508 | // TODO(deadbeef): There are conditions that make bundling two media |
| 1509 | // descriptions together illegal. For example, they use the same payload |
| 1510 | // type to represent different codecs, or same IDs for different header |
| 1511 | // extensions. We need to detect this and not try to bundle those media |
| 1512 | // descriptions together. |
| 1513 | offer_bundle.AddContentName(content.name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1514 | } |
Steve Anton | 2bed397 | 2019-01-04 17:04:30 -0800 | [diff] [blame] | 1515 | if (!offer_bundle.content_names().empty()) { |
| 1516 | offer->AddGroup(offer_bundle); |
| 1517 | if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) { |
| 1518 | RTC_LOG(LS_ERROR) |
| 1519 | << "CreateOffer failed to UpdateTransportInfoForBundle."; |
| 1520 | return nullptr; |
| 1521 | } |
| 1522 | if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) { |
| 1523 | RTC_LOG(LS_ERROR) |
| 1524 | << "CreateOffer failed to UpdateCryptoParamsForBundle."; |
| 1525 | return nullptr; |
| 1526 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1527 | } |
| 1528 | } |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1529 | |
| 1530 | // The following determines how to signal MSIDs to ensure compatibility with |
| 1531 | // older endpoints (in particular, older Plan B endpoints). |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1532 | if (is_unified_plan_) { |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1533 | // Be conservative and signal using both a=msid and a=ssrc lines. Unified |
| 1534 | // Plan answerers will look at a=msid and Plan B answerers will look at the |
| 1535 | // a=ssrc MSID line. |
| 1536 | offer->set_msid_signaling(cricket::kMsidSignalingMediaSection | |
| 1537 | cricket::kMsidSignalingSsrcAttribute); |
| 1538 | } else { |
| 1539 | // Plan B always signals MSID using a=ssrc lines. |
| 1540 | offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute); |
| 1541 | } |
| 1542 | |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 1543 | offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed); |
| 1544 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 1545 | if (session_options.media_transport_settings.has_value()) { |
| 1546 | offer->AddMediaTransportSetting( |
| 1547 | session_options.media_transport_settings->transport_name, |
| 1548 | session_options.media_transport_settings->transport_setting); |
| 1549 | } |
| 1550 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1551 | return offer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1554 | std::unique_ptr<SessionDescription> |
| 1555 | MediaSessionDescriptionFactory::CreateAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1556 | const SessionDescription* offer, |
| 1557 | const MediaSessionOptions& session_options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1558 | const SessionDescription* current_description) const { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1559 | if (!offer) { |
| 1560 | return nullptr; |
| 1561 | } |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1562 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1563 | // Must have options for exactly as many sections as in the offer. |
| 1564 | RTC_DCHECK_EQ(offer->contents().size(), |
| 1565 | session_options.media_description_options.size()); |
| 1566 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1567 | IceCredentialsIterator ice_credentials( |
| 1568 | session_options.pooled_ice_credentials); |
| 1569 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1570 | std::vector<const ContentInfo*> current_active_contents; |
| 1571 | if (current_description) { |
| 1572 | current_active_contents = |
| 1573 | GetActiveContents(*current_description, session_options); |
| 1574 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1575 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1576 | StreamParamsVec current_streams = |
| 1577 | GetCurrentStreamParams(current_active_contents); |
Johannes Kron | 0854eb6 | 2018-10-10 22:33:20 +0200 | [diff] [blame] | 1578 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1579 | // Get list of all possible codecs that respects existing payload type |
| 1580 | // mappings and uses a single payload type space. |
| 1581 | // |
| 1582 | // Note that these lists may be further filtered for each m= section; this |
| 1583 | // step is done just to establish the payload type mappings shared by all |
| 1584 | // sections. |
| 1585 | AudioCodecs answer_audio_codecs; |
| 1586 | VideoCodecs answer_video_codecs; |
| 1587 | DataCodecs answer_data_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1588 | GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1589 | &answer_video_codecs, &answer_data_codecs); |
| 1590 | |
| 1591 | if (!session_options.vad_enabled) { |
| 1592 | // If application doesn't want CN codecs in answer. |
| 1593 | StripCNCodecs(&answer_audio_codecs); |
| 1594 | } |
| 1595 | FilterDataCodecs(&answer_data_codecs, |
| 1596 | session_options.data_channel_type == DCT_SCTP); |
| 1597 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1598 | auto answer = absl::make_unique<SessionDescription>(); |
| 1599 | |
| 1600 | // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE |
| 1601 | // group in the answer with the appropriate content names. |
| 1602 | const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE); |
| 1603 | ContentGroup answer_bundle(GROUP_TYPE_BUNDLE); |
| 1604 | // Transport info shared by the bundle group. |
| 1605 | std::unique_ptr<TransportInfo> bundle_transport; |
| 1606 | |
| 1607 | answer->set_extmap_allow_mixed(offer->extmap_allow_mixed()); |
| 1608 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1609 | // Iterate through the media description options, matching with existing |
| 1610 | // media descriptions in |current_description|. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1611 | size_t msection_index = 0; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1612 | for (const MediaDescriptionOptions& media_description_options : |
| 1613 | session_options.media_description_options) { |
| 1614 | const ContentInfo* offer_content = &offer->contents()[msection_index]; |
| 1615 | // Media types and MIDs must match between the remote offer and the |
| 1616 | // MediaDescriptionOptions. |
| 1617 | RTC_DCHECK( |
| 1618 | IsMediaContentOfType(offer_content, media_description_options.type)); |
| 1619 | RTC_DCHECK(media_description_options.mid == offer_content->name); |
| 1620 | const ContentInfo* current_content = nullptr; |
| 1621 | if (current_description && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1622 | msection_index < current_description->contents().size()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1623 | current_content = ¤t_description->contents()[msection_index]; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1624 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1625 | switch (media_description_options.type) { |
| 1626 | case MEDIA_TYPE_AUDIO: |
| 1627 | if (!AddAudioContentForAnswer( |
| 1628 | media_description_options, session_options, offer_content, |
| 1629 | offer, current_content, current_description, |
| 1630 | bundle_transport.get(), answer_audio_codecs, ¤t_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1631 | answer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1632 | return nullptr; |
| 1633 | } |
| 1634 | break; |
| 1635 | case MEDIA_TYPE_VIDEO: |
| 1636 | if (!AddVideoContentForAnswer( |
| 1637 | media_description_options, session_options, offer_content, |
| 1638 | offer, current_content, current_description, |
| 1639 | bundle_transport.get(), answer_video_codecs, ¤t_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1640 | answer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1641 | return nullptr; |
| 1642 | } |
| 1643 | break; |
| 1644 | case MEDIA_TYPE_DATA: |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1645 | if (!AddDataContentForAnswer( |
| 1646 | media_description_options, session_options, offer_content, |
| 1647 | offer, current_content, current_description, |
| 1648 | bundle_transport.get(), answer_data_codecs, ¤t_streams, |
| 1649 | answer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1650 | return nullptr; |
| 1651 | } |
| 1652 | break; |
| 1653 | default: |
| 1654 | RTC_NOTREACHED(); |
| 1655 | } |
| 1656 | ++msection_index; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1657 | // See if we can add the newly generated m= section to the BUNDLE group in |
| 1658 | // the answer. |
| 1659 | ContentInfo& added = answer->contents().back(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1660 | if (!added.rejected && session_options.bundle_enabled && offer_bundle && |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1661 | offer_bundle->HasContentName(added.name)) { |
| 1662 | answer_bundle.AddContentName(added.name); |
| 1663 | bundle_transport.reset( |
| 1664 | new TransportInfo(*answer->GetTransportInfoByName(added.name))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1665 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Taylor Brandstetter | 0ab5651 | 2018-04-12 10:30:48 -0700 | [diff] [blame] | 1668 | // If a BUNDLE group was offered, put a BUNDLE group in the answer even if |
| 1669 | // it's empty. RFC5888 says: |
| 1670 | // |
| 1671 | // A SIP entity that receives an offer that contains an "a=group" line |
| 1672 | // with semantics that are understood MUST return an answer that |
| 1673 | // contains an "a=group" line with the same semantics. |
| 1674 | if (offer_bundle) { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1675 | answer->AddGroup(answer_bundle); |
Taylor Brandstetter | 0ab5651 | 2018-04-12 10:30:48 -0700 | [diff] [blame] | 1676 | } |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1677 | |
Taylor Brandstetter | 0ab5651 | 2018-04-12 10:30:48 -0700 | [diff] [blame] | 1678 | if (answer_bundle.FirstContentName()) { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1679 | // Share the same ICE credentials and crypto params across all contents, |
| 1680 | // as BUNDLE requires. |
| 1681 | if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1682 | RTC_LOG(LS_ERROR) |
| 1683 | << "CreateAnswer failed to UpdateTransportInfoForBundle."; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1684 | return NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1685 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1686 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1687 | if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1688 | RTC_LOG(LS_ERROR) |
| 1689 | << "CreateAnswer failed to UpdateCryptoParamsForBundle."; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1690 | return NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1694 | // The following determines how to signal MSIDs to ensure compatibility with |
| 1695 | // older endpoints (in particular, older Plan B endpoints). |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1696 | if (is_unified_plan_) { |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1697 | // Unified Plan needs to look at what the offer included to find the most |
| 1698 | // compatible answer. |
| 1699 | if (offer->msid_signaling() == 0) { |
| 1700 | // We end up here in one of three cases: |
| 1701 | // 1. An empty offer. We'll reply with an empty answer so it doesn't |
| 1702 | // matter what we pick here. |
| 1703 | // 2. A data channel only offer. We won't add any MSIDs to the answer so |
| 1704 | // it also doesn't matter what we pick here. |
| 1705 | // 3. Media that's either sendonly or inactive from the remote endpoint. |
| 1706 | // We don't have any information to say whether the endpoint is Plan B |
| 1707 | // or Unified Plan, so be conservative and send both. |
| 1708 | answer->set_msid_signaling(cricket::kMsidSignalingMediaSection | |
| 1709 | cricket::kMsidSignalingSsrcAttribute); |
| 1710 | } else if (offer->msid_signaling() == |
| 1711 | (cricket::kMsidSignalingMediaSection | |
| 1712 | cricket::kMsidSignalingSsrcAttribute)) { |
| 1713 | // If both a=msid and a=ssrc MSID signaling methods were used, we're |
| 1714 | // probably talking to a Unified Plan endpoint so respond with just |
| 1715 | // a=msid. |
| 1716 | answer->set_msid_signaling(cricket::kMsidSignalingMediaSection); |
| 1717 | } else { |
| 1718 | // Otherwise, it's clear which method the offerer is using so repeat that |
| 1719 | // back to them. |
| 1720 | answer->set_msid_signaling(offer->msid_signaling()); |
| 1721 | } |
| 1722 | } else { |
| 1723 | // Plan B always signals MSID using a=ssrc lines. |
| 1724 | answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute); |
| 1725 | } |
| 1726 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1727 | return answer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1730 | const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer( |
| 1731 | const RtpTransceiverDirection& direction) const { |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1732 | switch (direction) { |
| 1733 | // If stream is inactive - generate list as if sendrecv. |
| 1734 | case RtpTransceiverDirection::kSendRecv: |
| 1735 | case RtpTransceiverDirection::kInactive: |
| 1736 | return audio_sendrecv_codecs_; |
| 1737 | case RtpTransceiverDirection::kSendOnly: |
| 1738 | return audio_send_codecs_; |
| 1739 | case RtpTransceiverDirection::kRecvOnly: |
| 1740 | return audio_recv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1741 | } |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1742 | RTC_NOTREACHED(); |
| 1743 | return audio_sendrecv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer( |
| 1747 | const RtpTransceiverDirection& offer, |
| 1748 | const RtpTransceiverDirection& answer) const { |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1749 | switch (answer) { |
| 1750 | // For inactive and sendrecv answers, generate lists as if we were to accept |
| 1751 | // the offer's direction. See RFC 3264 Section 6.1. |
| 1752 | case RtpTransceiverDirection::kSendRecv: |
| 1753 | case RtpTransceiverDirection::kInactive: |
| 1754 | return GetAudioCodecsForOffer( |
| 1755 | webrtc::RtpTransceiverDirectionReversed(offer)); |
| 1756 | case RtpTransceiverDirection::kSendOnly: |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1757 | return audio_send_codecs_; |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1758 | case RtpTransceiverDirection::kRecvOnly: |
| 1759 | return audio_recv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1760 | } |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1761 | RTC_NOTREACHED(); |
| 1762 | return audio_sendrecv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1765 | void MergeCodecsFromDescription( |
| 1766 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1767 | AudioCodecs* audio_codecs, |
| 1768 | VideoCodecs* video_codecs, |
| 1769 | DataCodecs* data_codecs, |
| 1770 | UsedPayloadTypes* used_pltypes) { |
| 1771 | for (const ContentInfo* content : current_active_contents) { |
| 1772 | if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1773 | const AudioContentDescription* audio = |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1774 | content->media_description()->as_audio(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1775 | MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1776 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1777 | const VideoContentDescription* video = |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1778 | content->media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1779 | MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1780 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_DATA)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1781 | const DataContentDescription* data = |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1782 | content->media_description()->as_data(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1783 | MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes); |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | // Getting codecs for an offer involves these steps: |
| 1789 | // |
| 1790 | // 1. Construct payload type -> codec mappings for current description. |
| 1791 | // 2. Add any reference codecs that weren't already present |
| 1792 | // 3. For each individual media description (m= section), filter codecs based |
| 1793 | // on the directional attribute (happens in another method). |
| 1794 | void MediaSessionDescriptionFactory::GetCodecsForOffer( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1795 | const std::vector<const ContentInfo*>& current_active_contents, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1796 | AudioCodecs* audio_codecs, |
| 1797 | VideoCodecs* video_codecs, |
| 1798 | DataCodecs* data_codecs) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1799 | // First - get all codecs from the current description if the media type |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1800 | // is used. Add them to |used_pltypes| so the payload type is not reused if a |
| 1801 | // new media type is added. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1802 | UsedPayloadTypes used_pltypes; |
| 1803 | MergeCodecsFromDescription(current_active_contents, audio_codecs, |
| 1804 | video_codecs, data_codecs, &used_pltypes); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1805 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1806 | // Add our codecs that are not in the current description. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1807 | MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes); |
| 1808 | MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes); |
| 1809 | MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes); |
| 1810 | } |
| 1811 | |
| 1812 | // Getting codecs for an answer involves these steps: |
| 1813 | // |
| 1814 | // 1. Construct payload type -> codec mappings for current description. |
| 1815 | // 2. Add any codecs from the offer that weren't already present. |
| 1816 | // 3. Add any remaining codecs that weren't already present. |
| 1817 | // 4. For each individual media description (m= section), filter codecs based |
| 1818 | // on the directional attribute (happens in another method). |
| 1819 | void MediaSessionDescriptionFactory::GetCodecsForAnswer( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1820 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1821 | const SessionDescription& remote_offer, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1822 | AudioCodecs* audio_codecs, |
| 1823 | VideoCodecs* video_codecs, |
| 1824 | DataCodecs* data_codecs) const { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1825 | // First - get all codecs from the current description if the media type |
| 1826 | // is used. Add them to |used_pltypes| so the payload type is not reused if a |
| 1827 | // new media type is added. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1828 | UsedPayloadTypes used_pltypes; |
| 1829 | MergeCodecsFromDescription(current_active_contents, audio_codecs, |
| 1830 | video_codecs, data_codecs, &used_pltypes); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1831 | |
| 1832 | // Second - filter out codecs that we don't support at all and should ignore. |
| 1833 | AudioCodecs filtered_offered_audio_codecs; |
| 1834 | VideoCodecs filtered_offered_video_codecs; |
| 1835 | DataCodecs filtered_offered_data_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1836 | for (const ContentInfo& content : remote_offer.contents()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1837 | if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) { |
| 1838 | const AudioContentDescription* audio = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1839 | content.media_description()->as_audio(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1840 | for (const AudioCodec& offered_audio_codec : audio->codecs()) { |
| 1841 | if (!FindMatchingCodec<AudioCodec>(audio->codecs(), |
| 1842 | filtered_offered_audio_codecs, |
| 1843 | offered_audio_codec, nullptr) && |
| 1844 | FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_, |
| 1845 | offered_audio_codec, nullptr)) { |
| 1846 | filtered_offered_audio_codecs.push_back(offered_audio_codec); |
| 1847 | } |
| 1848 | } |
| 1849 | } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) { |
| 1850 | const VideoContentDescription* video = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1851 | content.media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1852 | for (const VideoCodec& offered_video_codec : video->codecs()) { |
| 1853 | if (!FindMatchingCodec<VideoCodec>(video->codecs(), |
| 1854 | filtered_offered_video_codecs, |
| 1855 | offered_video_codec, nullptr) && |
| 1856 | FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_, |
| 1857 | offered_video_codec, nullptr)) { |
| 1858 | filtered_offered_video_codecs.push_back(offered_video_codec); |
| 1859 | } |
| 1860 | } |
| 1861 | } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) { |
| 1862 | const DataContentDescription* data = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1863 | content.media_description()->as_data(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1864 | for (const DataCodec& offered_data_codec : data->codecs()) { |
| 1865 | if (!FindMatchingCodec<DataCodec>(data->codecs(), |
| 1866 | filtered_offered_data_codecs, |
| 1867 | offered_data_codec, nullptr) && |
| 1868 | FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_, |
| 1869 | offered_data_codec, nullptr)) { |
| 1870 | filtered_offered_data_codecs.push_back(offered_data_codec); |
| 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1876 | // Add codecs that are not in the current description but were in |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1877 | // |remote_offer|. |
| 1878 | MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs, |
| 1879 | &used_pltypes); |
| 1880 | MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs, |
| 1881 | &used_pltypes); |
| 1882 | MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs, |
| 1883 | &used_pltypes); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1887 | const std::vector<const ContentInfo*>& current_active_contents, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1888 | RtpHeaderExtensions* offer_audio_extensions, |
| 1889 | RtpHeaderExtensions* offer_video_extensions) const { |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 1890 | // All header extensions allocated from the same range to avoid potential |
| 1891 | // issues when using BUNDLE. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1892 | UsedRtpHeaderExtensionIds used_ids; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1893 | RtpHeaderExtensions all_regular_extensions; |
| 1894 | RtpHeaderExtensions all_encrypted_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1895 | |
| 1896 | // First - get all extensions from the current description if the media type |
| 1897 | // is used. |
| 1898 | // Add them to |used_ids| so the local ids are not reused if a new media |
| 1899 | // type is added. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1900 | for (const ContentInfo* content : current_active_contents) { |
| 1901 | if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) { |
| 1902 | const AudioContentDescription* audio = |
| 1903 | content->media_description()->as_audio(); |
| 1904 | MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions, |
| 1905 | &all_regular_extensions, &all_encrypted_extensions, |
| 1906 | &used_ids); |
| 1907 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) { |
| 1908 | const VideoContentDescription* video = |
| 1909 | content->media_description()->as_video(); |
| 1910 | MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions, |
| 1911 | &all_regular_extensions, &all_encrypted_extensions, |
| 1912 | &used_ids); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1916 | // Add our default RTP header extensions that are not in the current |
| 1917 | // description. |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1918 | MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions, |
| 1919 | &all_regular_extensions, &all_encrypted_extensions, |
| 1920 | &used_ids); |
| 1921 | MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions, |
| 1922 | &all_regular_extensions, &all_encrypted_extensions, |
| 1923 | &used_ids); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1924 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1925 | // TODO(jbauch): Support adding encrypted header extensions to existing |
| 1926 | // sessions. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1927 | if (enable_encrypted_rtp_header_extensions_ && |
| 1928 | current_active_contents.empty()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1929 | AddEncryptedVersionsOfHdrExts(offer_audio_extensions, |
| 1930 | &all_encrypted_extensions, &used_ids); |
| 1931 | AddEncryptedVersionsOfHdrExts(offer_video_extensions, |
| 1932 | &all_encrypted_extensions, &used_ids); |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1933 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | bool MediaSessionDescriptionFactory::AddTransportOffer( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1937 | const std::string& content_name, |
| 1938 | const TransportOptions& transport_options, |
| 1939 | const SessionDescription* current_desc, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1940 | SessionDescription* offer_desc, |
| 1941 | IceCredentialsIterator* ice_credentials) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1942 | if (!transport_desc_factory_) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1943 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1944 | const TransportDescription* current_tdesc = |
| 1945 | GetTransportDescription(content_name, current_desc); |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 1946 | std::unique_ptr<TransportDescription> new_tdesc( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1947 | transport_desc_factory_->CreateOffer(transport_options, current_tdesc, |
| 1948 | ice_credentials)); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 1949 | if (!new_tdesc) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1950 | RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name=" |
| 1951 | << content_name; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1952 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 1953 | offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)); |
| 1954 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 1957 | std::unique_ptr<TransportDescription> |
| 1958 | MediaSessionDescriptionFactory::CreateTransportAnswer( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1959 | const std::string& content_name, |
| 1960 | const SessionDescription* offer_desc, |
| 1961 | const TransportOptions& transport_options, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1962 | const SessionDescription* current_desc, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1963 | bool require_transport_attributes, |
| 1964 | IceCredentialsIterator* ice_credentials) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1965 | if (!transport_desc_factory_) |
| 1966 | return NULL; |
| 1967 | const TransportDescription* offer_tdesc = |
| 1968 | GetTransportDescription(content_name, offer_desc); |
| 1969 | const TransportDescription* current_tdesc = |
| 1970 | GetTransportDescription(content_name, current_desc); |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1971 | return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options, |
| 1972 | require_transport_attributes, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1973 | current_tdesc, ice_credentials); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | bool MediaSessionDescriptionFactory::AddTransportAnswer( |
| 1977 | const std::string& content_name, |
| 1978 | const TransportDescription& transport_desc, |
| 1979 | SessionDescription* answer_desc) const { |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 1980 | answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1981 | return true; |
| 1982 | } |
| 1983 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1984 | // |audio_codecs| = set of all possible codecs that can be used, with correct |
| 1985 | // payload type mappings |
| 1986 | // |
| 1987 | // |supported_audio_codecs| = set of codecs that are supported for the direction |
| 1988 | // of this m= section |
| 1989 | // |
| 1990 | // acd->codecs() = set of previously negotiated codecs for this m= section |
| 1991 | // |
| 1992 | // The payload types should come from audio_codecs, but the order should come |
| 1993 | // from acd->codecs() and then supported_codecs, to ensure that re-offers don't |
| 1994 | // change existing codec priority, and that new codecs are added with the right |
| 1995 | // priority. |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 1996 | bool MediaSessionDescriptionFactory::AddAudioContentForOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1997 | const MediaDescriptionOptions& media_description_options, |
| 1998 | const MediaSessionOptions& session_options, |
| 1999 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2000 | const SessionDescription* current_description, |
| 2001 | const RtpHeaderExtensions& audio_rtp_extensions, |
| 2002 | const AudioCodecs& audio_codecs, |
| 2003 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2004 | SessionDescription* desc, |
| 2005 | IceCredentialsIterator* ice_credentials) const { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2006 | // Filter audio_codecs (which includes all codecs, with correctly remapped |
| 2007 | // payload types) based on transceiver direction. |
| 2008 | const AudioCodecs& supported_audio_codecs = |
| 2009 | GetAudioCodecsForOffer(media_description_options.direction); |
| 2010 | |
| 2011 | AudioCodecs filtered_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 2012 | // Add the codecs from current content if it exists and is not rejected nor |
| 2013 | // recycled. |
| 2014 | if (current_content && !current_content->rejected && |
| 2015 | current_content->name == media_description_options.mid) { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2016 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2017 | const AudioContentDescription* acd = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2018 | current_content->media_description()->as_audio(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2019 | for (const AudioCodec& codec : acd->codecs()) { |
Taylor Brandstetter | 1c34974 | 2017-10-03 18:25:36 -0700 | [diff] [blame] | 2020 | if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec, |
| 2021 | nullptr)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2022 | filtered_codecs.push_back(codec); |
| 2023 | } |
| 2024 | } |
| 2025 | } |
| 2026 | // Add other supported audio codecs. |
| 2027 | AudioCodec found_codec; |
| 2028 | for (const AudioCodec& codec : supported_audio_codecs) { |
| 2029 | if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs, |
| 2030 | codec, &found_codec) && |
| 2031 | !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs, |
| 2032 | codec, nullptr)) { |
| 2033 | // Use the |found_codec| from |audio_codecs| because it has the correctly |
| 2034 | // mapped payload type. |
| 2035 | filtered_codecs.push_back(found_codec); |
| 2036 | } |
| 2037 | } |
deadbeef | 44f0819 | 2015-12-15 16:20:09 -0800 | [diff] [blame] | 2038 | |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2039 | cricket::SecurePolicy sdes_policy = |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2040 | IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED |
| 2041 | : secure(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2042 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2043 | std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2044 | std::vector<std::string> crypto_suites; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2045 | GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options, |
| 2046 | &crypto_suites); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2047 | if (!CreateMediaContentOffer(media_description_options, session_options, |
| 2048 | filtered_codecs, sdes_policy, |
| 2049 | GetCryptos(current_content), crypto_suites, |
| 2050 | audio_rtp_extensions, ssrc_generator_, |
| 2051 | current_streams, audio.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2052 | return false; |
| 2053 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2054 | |
| 2055 | bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); |
| 2056 | SetMediaProtocol(secure_transport, audio.get()); |
jiayl@webrtc.org | 7d4891d | 2014-09-09 21:43:15 +0000 | [diff] [blame] | 2057 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 2058 | audio->set_direction(media_description_options.direction); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 2059 | |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2060 | desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2061 | media_description_options.stopped, audio.release()); |
| 2062 | if (!AddTransportOffer(media_description_options.mid, |
| 2063 | media_description_options.transport_options, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2064 | current_description, desc, ice_credentials)) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2065 | return false; |
| 2066 | } |
| 2067 | |
| 2068 | return true; |
| 2069 | } |
| 2070 | |
| 2071 | bool MediaSessionDescriptionFactory::AddVideoContentForOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2072 | const MediaDescriptionOptions& media_description_options, |
| 2073 | const MediaSessionOptions& session_options, |
| 2074 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2075 | const SessionDescription* current_description, |
| 2076 | const RtpHeaderExtensions& video_rtp_extensions, |
| 2077 | const VideoCodecs& video_codecs, |
| 2078 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2079 | SessionDescription* desc, |
| 2080 | IceCredentialsIterator* ice_credentials) const { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2081 | cricket::SecurePolicy sdes_policy = |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2082 | IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED |
| 2083 | : secure(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2084 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2085 | std::unique_ptr<VideoContentDescription> video(new VideoContentDescription()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2086 | std::vector<std::string> crypto_suites; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2087 | GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options, |
| 2088 | &crypto_suites); |
| 2089 | |
| 2090 | VideoCodecs filtered_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 2091 | // Add the codecs from current content if it exists and is not rejected nor |
| 2092 | // recycled. |
| 2093 | if (current_content && !current_content->rejected && |
| 2094 | current_content->name == media_description_options.mid) { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2095 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2096 | const VideoContentDescription* vcd = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2097 | current_content->media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2098 | for (const VideoCodec& codec : vcd->codecs()) { |
Taylor Brandstetter | 1c34974 | 2017-10-03 18:25:36 -0700 | [diff] [blame] | 2099 | if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2100 | nullptr)) { |
| 2101 | filtered_codecs.push_back(codec); |
| 2102 | } |
| 2103 | } |
| 2104 | } |
| 2105 | // Add other supported video codecs. |
| 2106 | VideoCodec found_codec; |
| 2107 | for (const VideoCodec& codec : video_codecs_) { |
| 2108 | if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec, |
| 2109 | &found_codec) && |
| 2110 | !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec, |
| 2111 | nullptr)) { |
| 2112 | // Use the |found_codec| from |video_codecs| because it has the correctly |
| 2113 | // mapped payload type. |
| 2114 | filtered_codecs.push_back(found_codec); |
| 2115 | } |
| 2116 | } |
| 2117 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2118 | if (!CreateMediaContentOffer(media_description_options, session_options, |
| 2119 | filtered_codecs, sdes_policy, |
| 2120 | GetCryptos(current_content), crypto_suites, |
| 2121 | video_rtp_extensions, ssrc_generator_, |
| 2122 | current_streams, video.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2123 | return false; |
| 2124 | } |
| 2125 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2126 | video->set_bandwidth(kAutoBandwidth); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2127 | |
| 2128 | bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); |
| 2129 | SetMediaProtocol(secure_transport, video.get()); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 2130 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 2131 | video->set_direction(media_description_options.direction); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 2132 | |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2133 | desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2134 | media_description_options.stopped, video.release()); |
| 2135 | if (!AddTransportOffer(media_description_options.mid, |
| 2136 | media_description_options.transport_options, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2137 | current_description, desc, ice_credentials)) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2138 | return false; |
| 2139 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2140 | return true; |
| 2141 | } |
| 2142 | |
| 2143 | bool MediaSessionDescriptionFactory::AddDataContentForOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2144 | const MediaDescriptionOptions& media_description_options, |
| 2145 | const MediaSessionOptions& session_options, |
| 2146 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2147 | const SessionDescription* current_description, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2148 | const DataCodecs& data_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2149 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2150 | SessionDescription* desc, |
| 2151 | IceCredentialsIterator* ice_credentials) const { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2152 | bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); |
| 2153 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2154 | std::unique_ptr<DataContentDescription> data(new DataContentDescription()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2155 | bool is_sctp = (session_options.data_channel_type == DCT_SCTP); |
| 2156 | // If the DataChannel type is not specified, use the DataChannel type in |
| 2157 | // the current description. |
| 2158 | if (session_options.data_channel_type == DCT_NONE && current_content) { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2159 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA)); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2160 | is_sctp = (current_content->media_description()->protocol() == |
| 2161 | kMediaProtocolSctp); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2162 | } |
deadbeef | 44f0819 | 2015-12-15 16:20:09 -0800 | [diff] [blame] | 2163 | |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2164 | cricket::SecurePolicy sdes_policy = |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2165 | IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED |
| 2166 | : secure(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2167 | std::vector<std::string> crypto_suites; |
| 2168 | if (is_sctp) { |
| 2169 | // SDES doesn't make sense for SCTP, so we disable it, and we only |
| 2170 | // get SDES crypto suites for RTP-based data channels. |
| 2171 | sdes_policy = cricket::SEC_DISABLED; |
| 2172 | // Unlike SetMediaProtocol below, we need to set the protocol |
| 2173 | // before we call CreateMediaContentOffer. Otherwise, |
| 2174 | // CreateMediaContentOffer won't know this is SCTP and will |
| 2175 | // generate SSRCs rather than SIDs. |
deadbeef | 8b7e9ad | 2017-05-25 09:38:55 -0700 | [diff] [blame] | 2176 | // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once |
| 2177 | // it's safe to do so. Older versions of webrtc would reject these |
| 2178 | // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2179 | data->set_protocol(secure_transport ? kMediaProtocolDtlsSctp |
| 2180 | : kMediaProtocolSctp); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2181 | } else { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2182 | GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options, |
deadbeef | 7914b8c | 2017-04-21 03:23:33 -0700 | [diff] [blame] | 2183 | &crypto_suites); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2186 | // Even SCTP uses a "codec". |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2187 | if (!CreateMediaContentOffer( |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 2188 | media_description_options, session_options, data_codecs, sdes_policy, |
| 2189 | GetCryptos(current_content), crypto_suites, RtpHeaderExtensions(), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2190 | ssrc_generator_, current_streams, data.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2191 | return false; |
| 2192 | } |
| 2193 | |
| 2194 | if (is_sctp) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2195 | desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2196 | data.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2197 | } else { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2198 | data->set_bandwidth(kDataMaxBandwidth); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2199 | SetMediaProtocol(secure_transport, data.get()); |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2200 | desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2201 | media_description_options.stopped, data.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2202 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2203 | if (!AddTransportOffer(media_description_options.mid, |
| 2204 | media_description_options.transport_options, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2205 | current_description, desc, ice_credentials)) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2206 | return false; |
| 2207 | } |
| 2208 | return true; |
| 2209 | } |
| 2210 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2211 | // |audio_codecs| = set of all possible codecs that can be used, with correct |
| 2212 | // payload type mappings |
| 2213 | // |
| 2214 | // |supported_audio_codecs| = set of codecs that are supported for the direction |
| 2215 | // of this m= section |
| 2216 | // |
| 2217 | // acd->codecs() = set of previously negotiated codecs for this m= section |
| 2218 | // |
| 2219 | // The payload types should come from audio_codecs, but the order should come |
| 2220 | // from acd->codecs() and then supported_codecs, to ensure that re-offers don't |
| 2221 | // change existing codec priority, and that new codecs are added with the right |
| 2222 | // priority. |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2223 | bool MediaSessionDescriptionFactory::AddAudioContentForAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2224 | const MediaDescriptionOptions& media_description_options, |
| 2225 | const MediaSessionOptions& session_options, |
| 2226 | const ContentInfo* offer_content, |
| 2227 | const SessionDescription* offer_description, |
| 2228 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2229 | const SessionDescription* current_description, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2230 | const TransportInfo* bundle_transport, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2231 | const AudioCodecs& audio_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2232 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2233 | SessionDescription* answer, |
| 2234 | IceCredentialsIterator* ice_credentials) const { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2235 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2236 | const AudioContentDescription* offer_audio_description = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2237 | offer_content->media_description()->as_audio(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2238 | |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2239 | std::unique_ptr<TransportDescription> audio_transport = CreateTransportAnswer( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2240 | media_description_options.mid, offer_description, |
| 2241 | media_description_options.transport_options, current_description, |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2242 | bundle_transport != nullptr, ice_credentials); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2243 | if (!audio_transport) { |
| 2244 | return false; |
| 2245 | } |
| 2246 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2247 | // Pick codecs based on the requested communications direction in the offer |
| 2248 | // and the selected direction in the answer. |
| 2249 | // Note these will be filtered one final time in CreateMediaContentAnswer. |
| 2250 | auto wants_rtd = media_description_options.direction; |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 2251 | auto offer_rtd = offer_audio_description->direction(); |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 2252 | auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2253 | AudioCodecs supported_audio_codecs = |
| 2254 | GetAudioCodecsForAnswer(offer_rtd, answer_rtd); |
| 2255 | |
| 2256 | AudioCodecs filtered_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 2257 | // Add the codecs from current content if it exists and is not rejected nor |
| 2258 | // recycled. |
| 2259 | if (current_content && !current_content->rejected && |
| 2260 | current_content->name == media_description_options.mid) { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2261 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2262 | const AudioContentDescription* acd = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2263 | current_content->media_description()->as_audio(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2264 | for (const AudioCodec& codec : acd->codecs()) { |
Taylor Brandstetter | 1c34974 | 2017-10-03 18:25:36 -0700 | [diff] [blame] | 2265 | if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec, |
| 2266 | nullptr)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2267 | filtered_codecs.push_back(codec); |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | // Add other supported audio codecs. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2272 | for (const AudioCodec& codec : supported_audio_codecs) { |
| 2273 | if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs, |
Zhi Huang | 6f36747 | 2017-11-22 13:20:02 -0800 | [diff] [blame] | 2274 | codec, nullptr) && |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2275 | !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs, |
| 2276 | codec, nullptr)) { |
Zhi Huang | 6f36747 | 2017-11-22 13:20:02 -0800 | [diff] [blame] | 2277 | // We should use the local codec with local parameters and the codec id |
| 2278 | // would be correctly mapped in |NegotiateCodecs|. |
| 2279 | filtered_codecs.push_back(codec); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2280 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2283 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2284 | session_options.bundle_enabled; |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2285 | std::unique_ptr<AudioContentDescription> audio_answer( |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2286 | new AudioContentDescription()); |
| 2287 | // Do not require or create SDES cryptos if DTLS is used. |
| 2288 | cricket::SecurePolicy sdes_policy = |
| 2289 | audio_transport->secure() ? cricket::SEC_DISABLED : secure(); |
| 2290 | if (!CreateMediaContentAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2291 | offer_audio_description, media_description_options, session_options, |
| 2292 | filtered_codecs, sdes_policy, GetCryptos(current_content), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2293 | audio_rtp_header_extensions(), ssrc_generator_, |
Steve Anton | 1b8773d | 2018-04-06 11:13:34 -0700 | [diff] [blame] | 2294 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2295 | bundle_enabled, audio_answer.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2296 | return false; // Fails the session setup. |
| 2297 | } |
| 2298 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2299 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2300 | : audio_transport->secure(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2301 | bool rejected = media_description_options.stopped || |
| 2302 | offer_content->rejected || |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2303 | !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO, |
| 2304 | audio_answer->protocol(), secure); |
Zhi Huang | 3518e7b | 2018-01-30 13:20:35 -0800 | [diff] [blame] | 2305 | if (!AddTransportAnswer(media_description_options.mid, |
| 2306 | *(audio_transport.get()), answer)) { |
| 2307 | return false; |
| 2308 | } |
| 2309 | |
| 2310 | if (rejected) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2311 | RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid |
| 2312 | << "' being rejected in answer."; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2315 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2316 | rejected, audio_answer.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2317 | return true; |
| 2318 | } |
| 2319 | |
| 2320 | bool MediaSessionDescriptionFactory::AddVideoContentForAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2321 | const MediaDescriptionOptions& media_description_options, |
| 2322 | const MediaSessionOptions& session_options, |
| 2323 | const ContentInfo* offer_content, |
| 2324 | const SessionDescription* offer_description, |
| 2325 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2326 | const SessionDescription* current_description, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2327 | const TransportInfo* bundle_transport, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2328 | const VideoCodecs& video_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2329 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2330 | SessionDescription* answer, |
| 2331 | IceCredentialsIterator* ice_credentials) const { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2332 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2333 | const VideoContentDescription* offer_video_description = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2334 | offer_content->media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2335 | |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2336 | std::unique_ptr<TransportDescription> video_transport = CreateTransportAnswer( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2337 | media_description_options.mid, offer_description, |
| 2338 | media_description_options.transport_options, current_description, |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2339 | bundle_transport != nullptr, ice_credentials); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2340 | if (!video_transport) { |
| 2341 | return false; |
| 2342 | } |
| 2343 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2344 | VideoCodecs filtered_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 2345 | // Add the codecs from current content if it exists and is not rejected nor |
| 2346 | // recycled. |
| 2347 | if (current_content && !current_content->rejected && |
| 2348 | current_content->name == media_description_options.mid) { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2349 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2350 | const VideoContentDescription* vcd = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2351 | current_content->media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2352 | for (const VideoCodec& codec : vcd->codecs()) { |
Taylor Brandstetter | 1c34974 | 2017-10-03 18:25:36 -0700 | [diff] [blame] | 2353 | if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2354 | nullptr)) { |
| 2355 | filtered_codecs.push_back(codec); |
| 2356 | } |
| 2357 | } |
| 2358 | } |
| 2359 | // Add other supported video codecs. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2360 | for (const VideoCodec& codec : video_codecs_) { |
| 2361 | if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec, |
Zhi Huang | 6f36747 | 2017-11-22 13:20:02 -0800 | [diff] [blame] | 2362 | nullptr) && |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2363 | !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec, |
| 2364 | nullptr)) { |
Zhi Huang | 6f36747 | 2017-11-22 13:20:02 -0800 | [diff] [blame] | 2365 | // We should use the local codec with local parameters and the codec id |
| 2366 | // would be correctly mapped in |NegotiateCodecs|. |
| 2367 | filtered_codecs.push_back(codec); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2368 | } |
| 2369 | } |
| 2370 | |
| 2371 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2372 | session_options.bundle_enabled; |
| 2373 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2374 | std::unique_ptr<VideoContentDescription> video_answer( |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2375 | new VideoContentDescription()); |
| 2376 | // Do not require or create SDES cryptos if DTLS is used. |
| 2377 | cricket::SecurePolicy sdes_policy = |
| 2378 | video_transport->secure() ? cricket::SEC_DISABLED : secure(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2379 | if (!CreateMediaContentAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2380 | offer_video_description, media_description_options, session_options, |
| 2381 | filtered_codecs, sdes_policy, GetCryptos(current_content), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2382 | video_rtp_header_extensions(), ssrc_generator_, |
Steve Anton | 1b8773d | 2018-04-06 11:13:34 -0700 | [diff] [blame] | 2383 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2384 | bundle_enabled, video_answer.get())) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2385 | return false; // Failed the sessin setup. |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2386 | } |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2387 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2388 | : video_transport->secure(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2389 | bool rejected = media_description_options.stopped || |
| 2390 | offer_content->rejected || |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2391 | !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO, |
| 2392 | video_answer->protocol(), secure); |
Zhi Huang | 3518e7b | 2018-01-30 13:20:35 -0800 | [diff] [blame] | 2393 | if (!AddTransportAnswer(media_description_options.mid, |
| 2394 | *(video_transport.get()), answer)) { |
| 2395 | return false; |
| 2396 | } |
| 2397 | |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2398 | if (!rejected) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2399 | video_answer->set_bandwidth(kAutoBandwidth); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2400 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2401 | RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid |
| 2402 | << "' being rejected in answer."; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2403 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2404 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2405 | rejected, video_answer.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2406 | return true; |
| 2407 | } |
| 2408 | |
| 2409 | bool MediaSessionDescriptionFactory::AddDataContentForAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2410 | const MediaDescriptionOptions& media_description_options, |
| 2411 | const MediaSessionOptions& session_options, |
| 2412 | const ContentInfo* offer_content, |
| 2413 | const SessionDescription* offer_description, |
| 2414 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2415 | const SessionDescription* current_description, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2416 | const TransportInfo* bundle_transport, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2417 | const DataCodecs& data_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2418 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2419 | SessionDescription* answer, |
| 2420 | IceCredentialsIterator* ice_credentials) const { |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2421 | std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2422 | media_description_options.mid, offer_description, |
| 2423 | media_description_options.transport_options, current_description, |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2424 | bundle_transport != nullptr, ice_credentials); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2425 | if (!data_transport) { |
| 2426 | return false; |
| 2427 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2428 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2429 | std::unique_ptr<DataContentDescription> data_answer( |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2430 | new DataContentDescription()); |
| 2431 | // Do not require or create SDES cryptos if DTLS is used. |
| 2432 | cricket::SecurePolicy sdes_policy = |
| 2433 | data_transport->secure() ? cricket::SEC_DISABLED : secure(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2434 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2435 | session_options.bundle_enabled; |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2436 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA)); |
| 2437 | const DataContentDescription* offer_data_description = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2438 | offer_content->media_description()->as_data(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2439 | if (!CreateMediaContentAnswer( |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2440 | offer_data_description, media_description_options, session_options, |
| 2441 | data_codecs, sdes_policy, GetCryptos(current_content), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2442 | RtpHeaderExtensions(), ssrc_generator_, |
| 2443 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2444 | bundle_enabled, data_answer.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2445 | return false; // Fails the session setup. |
| 2446 | } |
| 2447 | |
zstein | 4b2e082 | 2017-02-17 19:48:38 -0800 | [diff] [blame] | 2448 | // Respond with sctpmap if the offer uses sctpmap. |
zstein | 4b2e082 | 2017-02-17 19:48:38 -0800 | [diff] [blame] | 2449 | bool offer_uses_sctpmap = offer_data_description->use_sctpmap(); |
| 2450 | data_answer->set_use_sctpmap(offer_uses_sctpmap); |
| 2451 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2452 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2453 | : data_transport->secure(); |
| 2454 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2455 | bool rejected = session_options.data_channel_type == DCT_NONE || |
| 2456 | media_description_options.stopped || |
| 2457 | offer_content->rejected || |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2458 | !IsMediaProtocolSupported(MEDIA_TYPE_DATA, |
| 2459 | data_answer->protocol(), secure); |
Zhi Huang | 3518e7b | 2018-01-30 13:20:35 -0800 | [diff] [blame] | 2460 | if (!AddTransportAnswer(media_description_options.mid, |
| 2461 | *(data_transport.get()), answer)) { |
| 2462 | return false; |
| 2463 | } |
| 2464 | |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2465 | if (!rejected) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2466 | data_answer->set_bandwidth(kDataMaxBandwidth); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2467 | } else { |
| 2468 | // RFC 3264 |
| 2469 | // The answer MUST contain the same number of m-lines as the offer. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2470 | RTC_LOG(LS_INFO) << "Data is not supported in the answer."; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2471 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2472 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2473 | rejected, data_answer.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2474 | return true; |
| 2475 | } |
| 2476 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2477 | void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() { |
| 2478 | audio_sendrecv_codecs_.clear(); |
| 2479 | all_audio_codecs_.clear(); |
| 2480 | // Compute the audio codecs union. |
| 2481 | for (const AudioCodec& send : audio_send_codecs_) { |
| 2482 | all_audio_codecs_.push_back(send); |
| 2483 | if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_, |
| 2484 | send, nullptr)) { |
| 2485 | // It doesn't make sense to have an RTX codec we support sending but not |
| 2486 | // receiving. |
| 2487 | RTC_DCHECK(!IsRtxCodec(send)); |
| 2488 | } |
| 2489 | } |
| 2490 | for (const AudioCodec& recv : audio_recv_codecs_) { |
| 2491 | if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_, |
| 2492 | recv, nullptr)) { |
| 2493 | all_audio_codecs_.push_back(recv); |
| 2494 | } |
| 2495 | } |
| 2496 | // Use NegotiateCodecs to merge our codec lists, since the operation is |
| 2497 | // essentially the same. Put send_codecs as the offered_codecs, which is the |
| 2498 | // order we'd like to follow. The reasoning is that encoding is usually more |
| 2499 | // expensive than decoding, and prioritizing a codec in the send list probably |
| 2500 | // means it's a codec we can handle efficiently. |
| 2501 | NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_, |
| 2502 | &audio_sendrecv_codecs_); |
| 2503 | } |
| 2504 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2505 | bool IsMediaContent(const ContentInfo* content) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2506 | return (content && (content->type == MediaProtocolType::kRtp || |
| 2507 | content->type == MediaProtocolType::kSctp)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | bool IsAudioContent(const ContentInfo* content) { |
| 2511 | return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO); |
| 2512 | } |
| 2513 | |
| 2514 | bool IsVideoContent(const ContentInfo* content) { |
| 2515 | return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO); |
| 2516 | } |
| 2517 | |
| 2518 | bool IsDataContent(const ContentInfo* content) { |
| 2519 | return IsMediaContentOfType(content, MEDIA_TYPE_DATA); |
| 2520 | } |
| 2521 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 2522 | const ContentInfo* GetFirstMediaContent(const ContentInfos& contents, |
| 2523 | MediaType media_type) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2524 | for (const ContentInfo& content : contents) { |
| 2525 | if (IsMediaContentOfType(&content, media_type)) { |
| 2526 | return &content; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2527 | } |
| 2528 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 2529 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) { |
| 2533 | return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO); |
| 2534 | } |
| 2535 | |
| 2536 | const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) { |
| 2537 | return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO); |
| 2538 | } |
| 2539 | |
| 2540 | const ContentInfo* GetFirstDataContent(const ContentInfos& contents) { |
| 2541 | return GetFirstMediaContent(contents, MEDIA_TYPE_DATA); |
| 2542 | } |
| 2543 | |
Steve Anton | ad7bffc | 2018-01-22 10:21:56 -0800 | [diff] [blame] | 2544 | const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc, |
| 2545 | MediaType media_type) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 2546 | if (sdesc == nullptr) { |
| 2547 | return nullptr; |
| 2548 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2549 | |
| 2550 | return GetFirstMediaContent(sdesc->contents(), media_type); |
| 2551 | } |
| 2552 | |
| 2553 | const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) { |
| 2554 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO); |
| 2555 | } |
| 2556 | |
| 2557 | const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) { |
| 2558 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO); |
| 2559 | } |
| 2560 | |
| 2561 | const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) { |
| 2562 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA); |
| 2563 | } |
| 2564 | |
| 2565 | const MediaContentDescription* GetFirstMediaContentDescription( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2566 | const SessionDescription* sdesc, |
| 2567 | MediaType media_type) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2568 | const ContentInfo* content = GetFirstMediaContent(sdesc, media_type); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2569 | return (content ? content->media_description() : nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | const AudioContentDescription* GetFirstAudioContentDescription( |
| 2573 | const SessionDescription* sdesc) { |
| 2574 | return static_cast<const AudioContentDescription*>( |
| 2575 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO)); |
| 2576 | } |
| 2577 | |
| 2578 | const VideoContentDescription* GetFirstVideoContentDescription( |
| 2579 | const SessionDescription* sdesc) { |
| 2580 | return static_cast<const VideoContentDescription*>( |
| 2581 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); |
| 2582 | } |
| 2583 | |
| 2584 | const DataContentDescription* GetFirstDataContentDescription( |
| 2585 | const SessionDescription* sdesc) { |
| 2586 | return static_cast<const DataContentDescription*>( |
| 2587 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); |
| 2588 | } |
| 2589 | |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2590 | // |
| 2591 | // Non-const versions of the above functions. |
| 2592 | // |
| 2593 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2594 | ContentInfo* GetFirstMediaContent(ContentInfos* contents, |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2595 | MediaType media_type) { |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2596 | for (ContentInfo& content : *contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2597 | if (IsMediaContentOfType(&content, media_type)) { |
| 2598 | return &content; |
| 2599 | } |
| 2600 | } |
| 2601 | return nullptr; |
| 2602 | } |
| 2603 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2604 | ContentInfo* GetFirstAudioContent(ContentInfos* contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2605 | return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO); |
| 2606 | } |
| 2607 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2608 | ContentInfo* GetFirstVideoContent(ContentInfos* contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2609 | return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO); |
| 2610 | } |
| 2611 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2612 | ContentInfo* GetFirstDataContent(ContentInfos* contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2613 | return GetFirstMediaContent(contents, MEDIA_TYPE_DATA); |
| 2614 | } |
| 2615 | |
Steve Anton | ad7bffc | 2018-01-22 10:21:56 -0800 | [diff] [blame] | 2616 | ContentInfo* GetFirstMediaContent(SessionDescription* sdesc, |
| 2617 | MediaType media_type) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2618 | if (sdesc == nullptr) { |
| 2619 | return nullptr; |
| 2620 | } |
| 2621 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2622 | return GetFirstMediaContent(&sdesc->contents(), media_type); |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) { |
| 2626 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO); |
| 2627 | } |
| 2628 | |
| 2629 | ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) { |
| 2630 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO); |
| 2631 | } |
| 2632 | |
| 2633 | ContentInfo* GetFirstDataContent(SessionDescription* sdesc) { |
| 2634 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA); |
| 2635 | } |
| 2636 | |
| 2637 | MediaContentDescription* GetFirstMediaContentDescription( |
| 2638 | SessionDescription* sdesc, |
| 2639 | MediaType media_type) { |
| 2640 | ContentInfo* content = GetFirstMediaContent(sdesc, media_type); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2641 | return (content ? content->media_description() : nullptr); |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | AudioContentDescription* GetFirstAudioContentDescription( |
| 2645 | SessionDescription* sdesc) { |
| 2646 | return static_cast<AudioContentDescription*>( |
| 2647 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO)); |
| 2648 | } |
| 2649 | |
| 2650 | VideoContentDescription* GetFirstVideoContentDescription( |
| 2651 | SessionDescription* sdesc) { |
| 2652 | return static_cast<VideoContentDescription*>( |
| 2653 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); |
| 2654 | } |
| 2655 | |
| 2656 | DataContentDescription* GetFirstDataContentDescription( |
| 2657 | SessionDescription* sdesc) { |
| 2658 | return static_cast<DataContentDescription*>( |
| 2659 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); |
| 2660 | } |
| 2661 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2662 | } // namespace cricket |