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 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 71 | const char kMediaProtocolRtpPrefix[] = "RTP/"; |
| 72 | |
| 73 | const char kMediaProtocolSctp[] = "SCTP"; |
| 74 | const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP"; |
| 75 | const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP"; |
| 76 | const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP"; |
| 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 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 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. |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [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 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 611 | bool IsRtpProtocol(const std::string& protocol) { |
| 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. |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +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, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +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, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 755 | MediaContentDescriptionImpl<C>* offer) { |
| 756 | offer->AddCodecs(codecs); |
| 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 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 764 | if (!AddStreamParams(media_description_options.sender_options, |
| 765 | session_options.rtcp_cname, ssrc_generator, |
| 766 | current_streams, offer)) { |
| 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, |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 802 | std::vector<C>* negotiated_codecs, |
| 803 | bool keep_offer_order) { |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 804 | for (const C& ours : local_codecs) { |
| 805 | C theirs; |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 806 | // Note that we intentionally only find one matching codec for each of our |
| 807 | // local codecs, in case the remote offer contains duplicate codecs. |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 808 | if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) { |
| 809 | C negotiated = ours; |
| 810 | negotiated.IntersectFeedbackParams(theirs); |
| 811 | if (IsRtxCodec(negotiated)) { |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 812 | const auto apt_it = |
| 813 | theirs.params.find(kCodecParamAssociatedPayloadType); |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 814 | // FindMatchingCodec shouldn't return something with no apt value. |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 815 | RTC_DCHECK(apt_it != theirs.params.end()); |
| 816 | negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 817 | } |
Niels Möller | 039743e | 2018-10-23 10:07:25 +0200 | [diff] [blame] | 818 | if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) { |
magjed | f823ede | 2016-11-12 09:53:04 -0800 | [diff] [blame] | 819 | webrtc::H264::GenerateProfileLevelIdForAnswer( |
| 820 | ours.params, theirs.params, &negotiated.params); |
| 821 | } |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 822 | negotiated.id = theirs.id; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 823 | negotiated.name = theirs.name; |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 824 | negotiated_codecs->push_back(std::move(negotiated)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 827 | if (keep_offer_order) { |
| 828 | // RFC3264: Although the answerer MAY list the formats in their desired |
| 829 | // order of preference, it is RECOMMENDED that unless there is a |
| 830 | // specific reason, the answerer list formats in the same relative order |
| 831 | // they were present in the offer. |
| 832 | // This can be skipped when the transceiver has any codec preferences. |
| 833 | std::unordered_map<int, int> payload_type_preferences; |
| 834 | int preference = static_cast<int>(offered_codecs.size() + 1); |
| 835 | for (const C& codec : offered_codecs) { |
| 836 | payload_type_preferences[codec.id] = preference--; |
| 837 | } |
| 838 | absl::c_sort(*negotiated_codecs, [&payload_type_preferences](const C& a, |
| 839 | const C& b) { |
| 840 | return payload_type_preferences[a.id] > payload_type_preferences[b.id]; |
| 841 | }); |
deadbeef | 67cf2c1 | 2016-04-13 10:07:16 -0700 | [diff] [blame] | 842 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 845 | // Finds a codec in |codecs2| that matches |codec_to_match|, which is |
| 846 | // a member of |codecs1|. If |codec_to_match| is an RTX codec, both |
| 847 | // the codecs themselves and their associated codecs must match. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 848 | template <class C> |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 849 | static bool FindMatchingCodec(const std::vector<C>& codecs1, |
| 850 | const std::vector<C>& codecs2, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 851 | const C& codec_to_match, |
| 852 | C* found_codec) { |
Taylor Brandstetter | 1c34974 | 2017-10-03 18:25:36 -0700 | [diff] [blame] | 853 | // |codec_to_match| should be a member of |codecs1|, in order to look up RTX |
| 854 | // codecs' associated codecs correctly. If not, that's a programming error. |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 855 | RTC_DCHECK(absl::c_any_of(codecs1, [&codec_to_match](const C& codec) { |
| 856 | return &codec == &codec_to_match; |
| 857 | })); |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 858 | for (const C& potential_match : codecs2) { |
| 859 | if (potential_match.Matches(codec_to_match)) { |
| 860 | if (IsRtxCodec(codec_to_match)) { |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 861 | int apt_value_1 = 0; |
| 862 | int apt_value_2 = 0; |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 863 | if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType, |
| 864 | &apt_value_1) || |
| 865 | !potential_match.GetParam(kCodecParamAssociatedPayloadType, |
| 866 | &apt_value_2)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 867 | RTC_LOG(LS_WARNING) << "RTX missing associated payload type."; |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 868 | continue; |
| 869 | } |
| 870 | if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2, |
| 871 | apt_value_2)) { |
| 872 | continue; |
| 873 | } |
| 874 | } |
| 875 | if (found_codec) { |
| 876 | *found_codec = potential_match; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 877 | } |
| 878 | return true; |
| 879 | } |
| 880 | } |
| 881 | return false; |
| 882 | } |
| 883 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 884 | // Find the codec in |codec_list| that |rtx_codec| is associated with. |
| 885 | template <class C> |
| 886 | static const C* GetAssociatedCodec(const std::vector<C>& codec_list, |
| 887 | const C& rtx_codec) { |
| 888 | std::string associated_pt_str; |
| 889 | if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 890 | &associated_pt_str)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 891 | RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name |
| 892 | << " is missing an associated payload type."; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 893 | return nullptr; |
| 894 | } |
| 895 | |
| 896 | int associated_pt; |
| 897 | if (!rtc::FromString(associated_pt_str, &associated_pt)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 898 | RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str |
| 899 | << " of RTX codec " << rtx_codec.name |
| 900 | << " to an integer."; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 901 | return nullptr; |
| 902 | } |
| 903 | |
| 904 | // Find the associated reference codec for the reference RTX codec. |
| 905 | const C* associated_codec = FindCodecById(codec_list, associated_pt); |
| 906 | if (!associated_codec) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 907 | RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type " |
| 908 | << associated_pt << " for RTX codec " << rtx_codec.name |
| 909 | << "."; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 910 | } |
| 911 | return associated_codec; |
| 912 | } |
| 913 | |
| 914 | // 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] | 915 | // already exist in |offered_codecs| and ensure the payload types don't |
| 916 | // collide. |
| 917 | template <class C> |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 918 | static void MergeCodecs(const std::vector<C>& reference_codecs, |
| 919 | std::vector<C>* offered_codecs, |
| 920 | UsedPayloadTypes* used_pltypes) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 921 | // Add all new codecs that are not RTX codecs. |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 922 | for (const C& reference_codec : reference_codecs) { |
| 923 | if (!IsRtxCodec(reference_codec) && |
| 924 | !FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
| 925 | reference_codec, nullptr)) { |
| 926 | C codec = reference_codec; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 927 | used_pltypes->FindAndSetIdUsed(&codec); |
| 928 | offered_codecs->push_back(codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
| 932 | // Add all new RTX codecs. |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 933 | for (const C& reference_codec : reference_codecs) { |
| 934 | if (IsRtxCodec(reference_codec) && |
| 935 | !FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
| 936 | reference_codec, nullptr)) { |
| 937 | C rtx_codec = reference_codec; |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 938 | const C* associated_codec = |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 939 | GetAssociatedCodec(reference_codecs, rtx_codec); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 940 | if (!associated_codec) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 941 | continue; |
| 942 | } |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 943 | // Find a codec in the offered list that matches the reference codec. |
| 944 | // Its payload type may be different than the reference codec. |
| 945 | C matching_codec; |
| 946 | if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs, |
magjed | b05fa24 | 2016-11-11 04:00:16 -0800 | [diff] [blame] | 947 | *associated_codec, &matching_codec)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 948 | RTC_LOG(LS_WARNING) |
| 949 | << "Couldn't find matching " << associated_codec->name << " codec."; |
Taylor Brandstetter | 6ec641b | 2016-03-04 16:47:56 -0800 | [diff] [blame] | 950 | continue; |
| 951 | } |
| 952 | |
| 953 | rtx_codec.params[kCodecParamAssociatedPayloadType] = |
| 954 | rtc::ToString(matching_codec.id); |
| 955 | used_pltypes->FindAndSetIdUsed(&rtx_codec); |
| 956 | offered_codecs->push_back(rtx_codec); |
| 957 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 961 | template <typename Codecs> |
| 962 | static Codecs MatchCodecPreference( |
| 963 | const std::vector<webrtc::RtpCodecCapability>& codec_preferences, |
| 964 | const Codecs& codecs) { |
| 965 | Codecs filtered_codecs; |
| 966 | std::set<std::string> kept_codecs_ids; |
| 967 | bool want_rtx = false; |
| 968 | |
| 969 | for (const auto& codec_preference : codec_preferences) { |
| 970 | auto found_codec = absl::c_find_if( |
| 971 | codecs, [&codec_preference](const typename Codecs::value_type& codec) { |
| 972 | webrtc::RtpCodecParameters codec_parameters = |
| 973 | codec.ToCodecParameters(); |
| 974 | return codec_parameters.name == codec_preference.name && |
| 975 | codec_parameters.kind == codec_preference.kind && |
| 976 | codec_parameters.num_channels == |
| 977 | codec_preference.num_channels && |
| 978 | codec_parameters.clock_rate == codec_preference.clock_rate && |
| 979 | codec_parameters.parameters == codec_preference.parameters; |
| 980 | }); |
| 981 | |
| 982 | if (found_codec != codecs.end()) { |
| 983 | filtered_codecs.push_back(*found_codec); |
| 984 | kept_codecs_ids.insert(std::to_string(found_codec->id)); |
| 985 | } else if (IsRtxCodec(codec_preference)) { |
| 986 | want_rtx = true; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | if (want_rtx) { |
| 991 | for (const auto& codec : codecs) { |
| 992 | if (IsRtxCodec(codec)) { |
| 993 | const auto apt = |
| 994 | codec.params.find(cricket::kCodecParamAssociatedPayloadType); |
| 995 | if (apt != codec.params.end() && |
| 996 | kept_codecs_ids.count(apt->second) > 0) { |
| 997 | filtered_codecs.push_back(codec); |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | return filtered_codecs; |
| 1004 | } |
| 1005 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1006 | static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions, |
| 1007 | const webrtc::RtpExtension& ext_to_match, |
| 1008 | webrtc::RtpExtension* found_extension) { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 1009 | auto it = absl::c_find_if( |
| 1010 | extensions, [&ext_to_match](const webrtc::RtpExtension& extension) { |
| 1011 | // We assume that all URIs are given in a canonical |
| 1012 | // format. |
| 1013 | return extension.uri == ext_to_match.uri && |
| 1014 | extension.encrypt == ext_to_match.encrypt; |
| 1015 | }); |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1016 | if (it == extensions.end()) { |
| 1017 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1018 | } |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1019 | if (found_extension) { |
| 1020 | *found_extension = *it; |
| 1021 | } |
| 1022 | return true; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1025 | static bool FindByUri(const RtpHeaderExtensions& extensions, |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 1026 | const webrtc::RtpExtension& ext_to_match, |
| 1027 | webrtc::RtpExtension* found_extension) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1028 | // We assume that all URIs are given in a canonical format. |
| 1029 | const webrtc::RtpExtension* found = |
| 1030 | webrtc::RtpExtension::FindHeaderExtensionByUri(extensions, |
| 1031 | ext_to_match.uri); |
| 1032 | if (!found) { |
| 1033 | return false; |
| 1034 | } |
| 1035 | if (found_extension) { |
| 1036 | *found_extension = *found; |
| 1037 | } |
| 1038 | return true; |
| 1039 | } |
| 1040 | |
| 1041 | static bool FindByUriWithEncryptionPreference( |
| 1042 | const RtpHeaderExtensions& extensions, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1043 | const webrtc::RtpExtension& ext_to_match, |
| 1044 | bool encryption_preference, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1045 | webrtc::RtpExtension* found_extension) { |
| 1046 | const webrtc::RtpExtension* unencrypted_extension = nullptr; |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1047 | for (const webrtc::RtpExtension& extension : extensions) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1048 | // We assume that all URIs are given in a canonical format. |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1049 | if (extension.uri == ext_to_match.uri) { |
| 1050 | if (!encryption_preference || extension.encrypt) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1051 | if (found_extension) { |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1052 | *found_extension = extension; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1055 | } |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1056 | unencrypted_extension = &extension; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1059 | if (unencrypted_extension) { |
| 1060 | if (found_extension) { |
| 1061 | *found_extension = *unencrypted_extension; |
| 1062 | } |
| 1063 | return true; |
| 1064 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1065 | return false; |
| 1066 | } |
| 1067 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1068 | // Adds all extensions from |reference_extensions| to |offered_extensions| that |
| 1069 | // don't already exist in |offered_extensions| and ensure the IDs don't |
| 1070 | // collide. If an extension is added, it's also added to |regular_extensions| or |
| 1071 | // |encrypted_extensions|, and if the extension is in |regular_extensions| or |
| 1072 | // |encrypted_extensions|, its ID is marked as used in |used_ids|. |
| 1073 | // |offered_extensions| is for either audio or video while |regular_extensions| |
| 1074 | // and |encrypted_extensions| are used for both audio and video. There could be |
| 1075 | // overlap between audio extensions and video extensions. |
| 1076 | static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions, |
| 1077 | RtpHeaderExtensions* offered_extensions, |
| 1078 | RtpHeaderExtensions* regular_extensions, |
| 1079 | RtpHeaderExtensions* encrypted_extensions, |
| 1080 | UsedRtpHeaderExtensionIds* used_ids) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 1081 | for (auto reference_extension : reference_extensions) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1082 | if (!FindByUriAndEncryption(*offered_extensions, reference_extension, |
| 1083 | nullptr)) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 1084 | webrtc::RtpExtension existing; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1085 | if (reference_extension.encrypt) { |
| 1086 | if (FindByUriAndEncryption(*encrypted_extensions, reference_extension, |
| 1087 | &existing)) { |
| 1088 | offered_extensions->push_back(existing); |
| 1089 | } else { |
| 1090 | used_ids->FindAndSetIdUsed(&reference_extension); |
| 1091 | encrypted_extensions->push_back(reference_extension); |
| 1092 | offered_extensions->push_back(reference_extension); |
| 1093 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 1094 | } else { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1095 | if (FindByUriAndEncryption(*regular_extensions, reference_extension, |
| 1096 | &existing)) { |
| 1097 | offered_extensions->push_back(existing); |
| 1098 | } else { |
| 1099 | used_ids->FindAndSetIdUsed(&reference_extension); |
| 1100 | regular_extensions->push_back(reference_extension); |
| 1101 | offered_extensions->push_back(reference_extension); |
| 1102 | } |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 1103 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1108 | static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions, |
| 1109 | RtpHeaderExtensions* all_extensions, |
| 1110 | UsedRtpHeaderExtensionIds* used_ids) { |
| 1111 | RtpHeaderExtensions encrypted_extensions; |
| 1112 | for (const webrtc::RtpExtension& extension : *extensions) { |
| 1113 | webrtc::RtpExtension existing; |
| 1114 | // Don't add encrypted extensions again that were already included in a |
| 1115 | // previous offer or regular extensions that are also included as encrypted |
| 1116 | // extensions. |
| 1117 | if (extension.encrypt || |
| 1118 | !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) || |
| 1119 | (FindByUriWithEncryptionPreference(*extensions, extension, true, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1120 | &existing) && |
| 1121 | existing.encrypt)) { |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1122 | continue; |
| 1123 | } |
| 1124 | |
| 1125 | if (FindByUri(*all_extensions, extension, &existing)) { |
| 1126 | encrypted_extensions.push_back(existing); |
| 1127 | } else { |
| 1128 | webrtc::RtpExtension encrypted(extension); |
| 1129 | encrypted.encrypt = true; |
| 1130 | used_ids->FindAndSetIdUsed(&encrypted); |
| 1131 | all_extensions->push_back(encrypted); |
| 1132 | encrypted_extensions.push_back(encrypted); |
| 1133 | } |
| 1134 | } |
| 1135 | extensions->insert(extensions->end(), encrypted_extensions.begin(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1136 | encrypted_extensions.end()); |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1139 | static void NegotiateRtpHeaderExtensions( |
| 1140 | const RtpHeaderExtensions& local_extensions, |
| 1141 | const RtpHeaderExtensions& offered_extensions, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1142 | bool enable_encrypted_rtp_header_extensions, |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1143 | RtpHeaderExtensions* negotiated_extensions) { |
| 1144 | // TransportSequenceNumberV2 is not offered by default. The special logic for |
| 1145 | // the TransportSequenceNumber extensions works as follows: |
| 1146 | // Offer Answer |
| 1147 | // V1 V1 if in local_extensions. |
| 1148 | // V1 and V2 V2 regardless of local_extensions. |
| 1149 | // V2 V2 regardless of local_extensions. |
| 1150 | const webrtc::RtpExtension* transport_sequence_number_v2_offer = |
| 1151 | webrtc::RtpExtension::FindHeaderExtensionByUri( |
| 1152 | offered_extensions, |
| 1153 | webrtc::RtpExtension::kTransportSequenceNumberV2Uri); |
| 1154 | |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1155 | for (const webrtc::RtpExtension& ours : local_extensions) { |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 1156 | webrtc::RtpExtension theirs; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1157 | if (FindByUriWithEncryptionPreference( |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1158 | offered_extensions, ours, enable_encrypted_rtp_header_extensions, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1159 | &theirs)) { |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1160 | if (transport_sequence_number_v2_offer && |
| 1161 | ours.uri == webrtc::RtpExtension::kTransportSequenceNumberUri) { |
| 1162 | // Don't respond to |
| 1163 | // http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01 |
| 1164 | // if we get an offer including |
Johannes Kron | 8cc711a | 2019-03-07 22:36:35 +0100 | [diff] [blame] | 1165 | // http://www.webrtc.org/experiments/rtp-hdrext/transport-wide-cc-02 |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1166 | continue; |
| 1167 | } else { |
| 1168 | // We respond with their RTP header extension id. |
| 1169 | negotiated_extensions->push_back(theirs); |
| 1170 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1171 | } |
| 1172 | } |
Johannes Kron | ce8e867 | 2019-02-22 13:06:44 +0100 | [diff] [blame] | 1173 | |
| 1174 | if (transport_sequence_number_v2_offer) { |
| 1175 | // Respond that we support kTransportSequenceNumberV2Uri. |
| 1176 | negotiated_extensions->push_back(*transport_sequence_number_v2_offer); |
| 1177 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | static void StripCNCodecs(AudioCodecs* audio_codecs) { |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1181 | audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(), |
| 1182 | [](const AudioCodec& codec) { |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 1183 | return absl::EqualsIgnoreCase( |
| 1184 | codec.name, kComfortNoiseCodecName); |
Steve Anton | 3a66edf | 2018-09-10 12:57:37 -0700 | [diff] [blame] | 1185 | }), |
| 1186 | audio_codecs->end()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1189 | // Create a media content to be answered for the given |sender_options| |
| 1190 | // according to the given session_options.rtcp_mux, session_options.streams, |
| 1191 | // codecs, crypto, and current_streams. If we don't currently have crypto (in |
| 1192 | // current_cryptos) and it is enabled (in secure_policy), crypto is created |
| 1193 | // (according to crypto_suites). The codecs, rtcp_mux, and crypto are all |
| 1194 | // negotiated with the offer. If the negotiation fails, this method returns |
| 1195 | // false. The created content is added to the offer. |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1196 | template <class C> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1197 | static bool CreateMediaContentAnswer( |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1198 | const MediaContentDescriptionImpl<C>* offer, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1199 | const MediaDescriptionOptions& media_description_options, |
| 1200 | const MediaSessionOptions& session_options, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1201 | const std::vector<C>& local_codecs, |
henrike@webrtc.org | b90991d | 2014-03-04 19:54:57 +0000 | [diff] [blame] | 1202 | const SecurePolicy& sdes_policy, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1203 | const CryptoParamsVec* current_cryptos, |
| 1204 | const RtpHeaderExtensions& local_rtp_extenstions, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1205 | UniqueRandomIdGenerator* ssrc_generator, |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1206 | bool enable_encrypted_rtp_header_extensions, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1207 | StreamParamsVec* current_streams, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1208 | bool bundle_enabled, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1209 | MediaContentDescriptionImpl<C>* answer) { |
| 1210 | std::vector<C> negotiated_codecs; |
| 1211 | NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs, |
| 1212 | media_description_options.codec_preferences.empty()); |
| 1213 | answer->AddCodecs(negotiated_codecs); |
| 1214 | answer->set_protocol(offer->protocol()); |
| 1215 | |
Johannes Kron | 9581bc4 | 2018-10-23 10:17:39 +0200 | [diff] [blame] | 1216 | answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1217 | RtpHeaderExtensions negotiated_rtp_extensions; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1218 | NegotiateRtpHeaderExtensions( |
| 1219 | local_rtp_extenstions, offer->rtp_header_extensions(), |
| 1220 | enable_encrypted_rtp_header_extensions, &negotiated_rtp_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1221 | answer->set_rtp_header_extensions(negotiated_rtp_extensions); |
| 1222 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1223 | answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux()); |
Taylor Brandstetter | 5f0b83b | 2016-03-18 15:02:07 -0700 | [diff] [blame] | 1224 | if (answer->type() == cricket::MEDIA_TYPE_VIDEO) { |
| 1225 | answer->set_rtcp_reduced_size(offer->rtcp_reduced_size()); |
| 1226 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (sdes_policy != SEC_DISABLED) { |
| 1229 | CryptoParams crypto; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1230 | if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options, |
| 1231 | &crypto)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1232 | if (current_cryptos) { |
| 1233 | FindMatchingCrypto(*current_cryptos, crypto, &crypto); |
| 1234 | } |
| 1235 | answer->AddCrypto(crypto); |
| 1236 | } |
| 1237 | } |
| 1238 | |
deadbeef | 7af91dd | 2016-12-13 11:29:11 -0800 | [diff] [blame] | 1239 | if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1240 | return false; |
| 1241 | } |
| 1242 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1243 | if (!AddStreamParams(media_description_options.sender_options, |
| 1244 | session_options.rtcp_cname, ssrc_generator, |
| 1245 | current_streams, answer)) { |
| 1246 | return false; // Something went seriously wrong. |
| 1247 | } |
| 1248 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1249 | AddSimulcastToMediaDescription(media_description_options, answer); |
| 1250 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 1251 | answer->set_direction(NegotiateRtpTransceiverDirection( |
| 1252 | offer->direction(), media_description_options.direction)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1253 | return true; |
| 1254 | } |
| 1255 | |
| 1256 | static bool IsMediaProtocolSupported(MediaType type, |
jiayl@webrtc.org | 8dcd43c | 2014-05-29 22:07:59 +0000 | [diff] [blame] | 1257 | const std::string& protocol, |
| 1258 | bool secure_transport) { |
zhihuang | cf5b37c | 2016-05-05 11:44:35 -0700 | [diff] [blame] | 1259 | // Since not all applications serialize and deserialize the media protocol, |
| 1260 | // we will have to accept |protocol| to be empty. |
| 1261 | if (protocol.empty()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1262 | return true; |
| 1263 | } |
jiayl@webrtc.org | 8dcd43c | 2014-05-29 22:07:59 +0000 | [diff] [blame] | 1264 | |
zhihuang | cf5b37c | 2016-05-05 11:44:35 -0700 | [diff] [blame] | 1265 | if (type == MEDIA_TYPE_DATA) { |
| 1266 | // Check for SCTP, but also for RTP for RTP-based data channels. |
| 1267 | // TODO(pthatcher): Remove RTP once RTP-based data channels are gone. |
| 1268 | if (secure_transport) { |
| 1269 | // Most likely scenarios first. |
| 1270 | return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) || |
| 1271 | IsPlainRtp(protocol); |
| 1272 | } else { |
| 1273 | return IsPlainSctp(protocol) || IsPlainRtp(protocol); |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | // Allow for non-DTLS RTP protocol even when using DTLS because that's what |
| 1278 | // JSEP specifies. |
| 1279 | if (secure_transport) { |
| 1280 | // Most likely scenarios first. |
| 1281 | return IsDtlsRtp(protocol) || IsPlainRtp(protocol); |
| 1282 | } else { |
| 1283 | return IsPlainRtp(protocol); |
| 1284 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | static void SetMediaProtocol(bool secure_transport, |
| 1288 | MediaContentDescription* desc) { |
deadbeef | f393829 | 2015-07-15 12:20:53 -0700 | [diff] [blame] | 1289 | if (!desc->cryptos().empty()) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1290 | desc->set_protocol(kMediaProtocolSavpf); |
deadbeef | f393829 | 2015-07-15 12:20:53 -0700 | [diff] [blame] | 1291 | else if (secure_transport) |
| 1292 | desc->set_protocol(kMediaProtocolDtlsSavpf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1293 | else |
| 1294 | desc->set_protocol(kMediaProtocolAvpf); |
| 1295 | } |
| 1296 | |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1297 | // Gets the TransportInfo of the given |content_name| from the |
| 1298 | // |current_description|. If doesn't exist, returns a new one. |
| 1299 | static const TransportDescription* GetTransportDescription( |
| 1300 | const std::string& content_name, |
| 1301 | const SessionDescription* current_description) { |
| 1302 | const TransportDescription* desc = NULL; |
| 1303 | if (current_description) { |
| 1304 | const TransportInfo* info = |
| 1305 | current_description->GetTransportInfoByName(content_name); |
| 1306 | if (info) { |
| 1307 | desc = &info->description; |
| 1308 | } |
| 1309 | } |
| 1310 | return desc; |
| 1311 | } |
| 1312 | |
| 1313 | // Gets the current DTLS state from the transport description. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1314 | static bool IsDtlsActive(const ContentInfo* content, |
| 1315 | const SessionDescription* current_description) { |
| 1316 | if (!content) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1317 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1318 | } |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1319 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1320 | size_t msection_index = content - ¤t_description->contents()[0]; |
| 1321 | |
| 1322 | if (current_description->transport_infos().size() <= msection_index) { |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1323 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1324 | } |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1325 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1326 | return current_description->transport_infos()[msection_index] |
| 1327 | .description.secure(); |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1330 | void MediaDescriptionOptions::AddAudioSender( |
| 1331 | const std::string& track_id, |
| 1332 | const std::vector<std::string>& stream_ids) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1333 | RTC_DCHECK(type == MEDIA_TYPE_AUDIO); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1334 | AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1337 | void MediaDescriptionOptions::AddVideoSender( |
| 1338 | const std::string& track_id, |
| 1339 | const std::vector<std::string>& stream_ids, |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1340 | const std::vector<RidDescription>& rids, |
| 1341 | const SimulcastLayerList& simulcast_layers, |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1342 | int num_sim_layers) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1343 | RTC_DCHECK(type == MEDIA_TYPE_VIDEO); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1344 | RTC_DCHECK(rids.empty() || num_sim_layers == 0) |
| 1345 | << "RIDs are the compliant way to indicate simulcast."; |
| 1346 | RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers)); |
| 1347 | AddSenderInternal(track_id, stream_ids, rids, simulcast_layers, |
| 1348 | num_sim_layers); |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1351 | void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id, |
| 1352 | const std::string& stream_id) { |
| 1353 | RTC_DCHECK(type == MEDIA_TYPE_DATA); |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1354 | // TODO(steveanton): Is it the case that RtpDataChannel will never have more |
| 1355 | // than one stream? |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1356 | AddSenderInternal(track_id, {stream_id}, {}, SimulcastLayerList(), 1); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1359 | void MediaDescriptionOptions::AddSenderInternal( |
| 1360 | const std::string& track_id, |
| 1361 | const std::vector<std::string>& stream_ids, |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1362 | const std::vector<RidDescription>& rids, |
| 1363 | const SimulcastLayerList& simulcast_layers, |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 1364 | int num_sim_layers) { |
| 1365 | // TODO(steveanton): Support any number of stream ids. |
| 1366 | RTC_CHECK(stream_ids.size() == 1U); |
Amit Hilbuch | c63ddb2 | 2019-01-02 10:13:58 -0800 | [diff] [blame] | 1367 | SenderOptions options; |
| 1368 | options.track_id = track_id; |
| 1369 | options.stream_ids = stream_ids; |
| 1370 | options.simulcast_layers = simulcast_layers; |
| 1371 | options.rids = rids; |
| 1372 | options.num_sim_layers = num_sim_layers; |
| 1373 | sender_options.push_back(options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1376 | bool MediaSessionOptions::HasMediaDescription(MediaType type) const { |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 1377 | return absl::c_any_of( |
| 1378 | media_description_options, |
| 1379 | [type](const MediaDescriptionOptions& t) { return t.type == type; }); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1382 | MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1383 | const TransportDescriptionFactory* transport_desc_factory, |
| 1384 | rtc::UniqueRandomIdGenerator* ssrc_generator) |
| 1385 | : ssrc_generator_(ssrc_generator), |
| 1386 | transport_desc_factory_(transport_desc_factory) { |
| 1387 | RTC_DCHECK(ssrc_generator_); |
| 1388 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1389 | |
| 1390 | MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( |
| 1391 | ChannelManager* channel_manager, |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 1392 | const TransportDescriptionFactory* transport_desc_factory, |
| 1393 | rtc::UniqueRandomIdGenerator* ssrc_generator) |
| 1394 | : MediaSessionDescriptionFactory(transport_desc_factory, ssrc_generator) { |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 1395 | channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_); |
| 1396 | channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1397 | channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_); |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 1398 | channel_manager->GetSupportedVideoCodecs(&video_codecs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1399 | channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_); |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1400 | channel_manager->GetSupportedDataCodecs(&data_codecs_); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1401 | ComputeAudioCodecsIntersectionAndUnion(); |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 1404 | const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs() |
| 1405 | const { |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1406 | return audio_sendrecv_codecs_; |
| 1407 | } |
| 1408 | |
| 1409 | const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const { |
| 1410 | return audio_send_codecs_; |
| 1411 | } |
| 1412 | |
| 1413 | const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const { |
| 1414 | return audio_recv_codecs_; |
| 1415 | } |
| 1416 | |
| 1417 | void MediaSessionDescriptionFactory::set_audio_codecs( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1418 | const AudioCodecs& send_codecs, |
| 1419 | const AudioCodecs& recv_codecs) { |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1420 | audio_send_codecs_ = send_codecs; |
| 1421 | audio_recv_codecs_ = recv_codecs; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1422 | ComputeAudioCodecsIntersectionAndUnion(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1425 | static void AddUnifiedPlanExtensions(RtpHeaderExtensions* extensions) { |
| 1426 | RTC_DCHECK(extensions); |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 1427 | |
| 1428 | rtc::UniqueNumberGenerator<int> unique_id_generator; |
| 1429 | unique_id_generator.AddKnownId(0); // The first valid RTP extension ID is 1. |
| 1430 | for (const webrtc::RtpExtension& extension : *extensions) { |
| 1431 | const bool collision_free = unique_id_generator.AddKnownId(extension.id); |
| 1432 | RTC_DCHECK(collision_free); |
| 1433 | } |
| 1434 | |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1435 | // Unified Plan also offers the MID and RID header extensions. |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 1436 | extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kMidUri, |
| 1437 | unique_id_generator())); |
| 1438 | extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kRidUri, |
| 1439 | unique_id_generator())); |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1440 | extensions->push_back(webrtc::RtpExtension( |
Elad Alon | 157540a | 2019-02-08 23:37:52 +0100 | [diff] [blame] | 1441 | webrtc::RtpExtension::kRepairedRidUri, unique_id_generator())); |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | RtpHeaderExtensions |
| 1445 | MediaSessionDescriptionFactory::audio_rtp_header_extensions() const { |
| 1446 | RtpHeaderExtensions extensions = audio_rtp_extensions_; |
| 1447 | if (is_unified_plan_) { |
| 1448 | AddUnifiedPlanExtensions(&extensions); |
| 1449 | } |
| 1450 | |
| 1451 | return extensions; |
| 1452 | } |
| 1453 | |
| 1454 | RtpHeaderExtensions |
| 1455 | MediaSessionDescriptionFactory::video_rtp_header_extensions() const { |
| 1456 | RtpHeaderExtensions extensions = video_rtp_extensions_; |
| 1457 | if (is_unified_plan_) { |
| 1458 | AddUnifiedPlanExtensions(&extensions); |
| 1459 | } |
| 1460 | |
| 1461 | return extensions; |
| 1462 | } |
| 1463 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1464 | std::unique_ptr<SessionDescription> MediaSessionDescriptionFactory::CreateOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1465 | const MediaSessionOptions& session_options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1466 | const SessionDescription* current_description) const { |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1467 | // Must have options for each existing section. |
| 1468 | if (current_description) { |
| 1469 | RTC_DCHECK_LE(current_description->contents().size(), |
| 1470 | session_options.media_description_options.size()); |
| 1471 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1472 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1473 | IceCredentialsIterator ice_credentials( |
| 1474 | session_options.pooled_ice_credentials); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1475 | |
| 1476 | std::vector<const ContentInfo*> current_active_contents; |
| 1477 | if (current_description) { |
| 1478 | current_active_contents = |
| 1479 | GetActiveContents(*current_description, session_options); |
| 1480 | } |
| 1481 | |
| 1482 | StreamParamsVec current_streams = |
| 1483 | GetCurrentStreamParams(current_active_contents); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1484 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1485 | AudioCodecs offer_audio_codecs; |
| 1486 | VideoCodecs offer_video_codecs; |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1487 | DataCodecs offer_data_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1488 | GetCodecsForOffer(current_active_contents, &offer_audio_codecs, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1489 | &offer_video_codecs, &offer_data_codecs); |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1490 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1491 | if (!session_options.vad_enabled) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1492 | // If application doesn't want CN codecs in offer. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1493 | StripCNCodecs(&offer_audio_codecs); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1494 | } |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1495 | FilterDataCodecs(&offer_data_codecs, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1496 | session_options.data_channel_type == DCT_SCTP); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1497 | |
| 1498 | RtpHeaderExtensions audio_rtp_extensions; |
| 1499 | RtpHeaderExtensions video_rtp_extensions; |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1500 | GetRtpHdrExtsToOffer(current_active_contents, &audio_rtp_extensions, |
| 1501 | &video_rtp_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1502 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1503 | auto offer = absl::make_unique<SessionDescription>(); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 1504 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1505 | // Iterate through the media description options, matching with existing media |
| 1506 | // descriptions in |current_description|. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1507 | size_t msection_index = 0; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1508 | for (const MediaDescriptionOptions& media_description_options : |
| 1509 | session_options.media_description_options) { |
| 1510 | const ContentInfo* current_content = nullptr; |
| 1511 | if (current_description && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1512 | msection_index < current_description->contents().size()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1513 | current_content = ¤t_description->contents()[msection_index]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1514 | // Media type must match unless this media section is being recycled. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1515 | RTC_DCHECK(current_content->name != media_description_options.mid || |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1516 | IsMediaContentOfType(current_content, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1517 | media_description_options.type)); |
| 1518 | } |
| 1519 | switch (media_description_options.type) { |
| 1520 | case MEDIA_TYPE_AUDIO: |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1521 | if (!AddAudioContentForOffer( |
| 1522 | media_description_options, session_options, current_content, |
| 1523 | current_description, audio_rtp_extensions, offer_audio_codecs, |
| 1524 | ¤t_streams, offer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1525 | return nullptr; |
| 1526 | } |
| 1527 | break; |
| 1528 | case MEDIA_TYPE_VIDEO: |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1529 | if (!AddVideoContentForOffer( |
| 1530 | media_description_options, session_options, current_content, |
| 1531 | current_description, video_rtp_extensions, offer_video_codecs, |
| 1532 | ¤t_streams, offer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1533 | return nullptr; |
| 1534 | } |
| 1535 | break; |
| 1536 | case MEDIA_TYPE_DATA: |
| 1537 | if (!AddDataContentForOffer(media_description_options, session_options, |
| 1538 | current_content, current_description, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1539 | offer_data_codecs, ¤t_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1540 | offer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1541 | return nullptr; |
| 1542 | } |
| 1543 | break; |
| 1544 | default: |
| 1545 | RTC_NOTREACHED(); |
| 1546 | } |
| 1547 | ++msection_index; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | // Bundle the contents together, if we've been asked to do so, and update any |
| 1551 | // parameters that need to be tweaked for BUNDLE. |
Steve Anton | 2bed397 | 2019-01-04 17:04:30 -0800 | [diff] [blame] | 1552 | if (session_options.bundle_enabled) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1553 | ContentGroup offer_bundle(GROUP_TYPE_BUNDLE); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1554 | for (const ContentInfo& content : offer->contents()) { |
Steve Anton | 2bed397 | 2019-01-04 17:04:30 -0800 | [diff] [blame] | 1555 | if (content.rejected) { |
| 1556 | continue; |
| 1557 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1558 | // TODO(deadbeef): There are conditions that make bundling two media |
| 1559 | // descriptions together illegal. For example, they use the same payload |
| 1560 | // type to represent different codecs, or same IDs for different header |
| 1561 | // extensions. We need to detect this and not try to bundle those media |
| 1562 | // descriptions together. |
| 1563 | offer_bundle.AddContentName(content.name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1564 | } |
Steve Anton | 2bed397 | 2019-01-04 17:04:30 -0800 | [diff] [blame] | 1565 | if (!offer_bundle.content_names().empty()) { |
| 1566 | offer->AddGroup(offer_bundle); |
| 1567 | if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) { |
| 1568 | RTC_LOG(LS_ERROR) |
| 1569 | << "CreateOffer failed to UpdateTransportInfoForBundle."; |
| 1570 | return nullptr; |
| 1571 | } |
| 1572 | if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) { |
| 1573 | RTC_LOG(LS_ERROR) |
| 1574 | << "CreateOffer failed to UpdateCryptoParamsForBundle."; |
| 1575 | return nullptr; |
| 1576 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1577 | } |
| 1578 | } |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1579 | |
| 1580 | // The following determines how to signal MSIDs to ensure compatibility with |
| 1581 | // older endpoints (in particular, older Plan B endpoints). |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1582 | if (is_unified_plan_) { |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1583 | // Be conservative and signal using both a=msid and a=ssrc lines. Unified |
| 1584 | // Plan answerers will look at a=msid and Plan B answerers will look at the |
| 1585 | // a=ssrc MSID line. |
| 1586 | offer->set_msid_signaling(cricket::kMsidSignalingMediaSection | |
| 1587 | cricket::kMsidSignalingSsrcAttribute); |
| 1588 | } else { |
| 1589 | // Plan B always signals MSID using a=ssrc lines. |
| 1590 | offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute); |
| 1591 | } |
| 1592 | |
Johannes Kron | 89f874e | 2018-11-12 10:25:48 +0100 | [diff] [blame] | 1593 | offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed); |
| 1594 | |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 11:14:05 -0800 | [diff] [blame] | 1595 | if (session_options.media_transport_settings.has_value()) { |
| 1596 | offer->AddMediaTransportSetting( |
| 1597 | session_options.media_transport_settings->transport_name, |
| 1598 | session_options.media_transport_settings->transport_setting); |
| 1599 | } |
| 1600 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1601 | return offer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1604 | std::unique_ptr<SessionDescription> |
| 1605 | MediaSessionDescriptionFactory::CreateAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1606 | const SessionDescription* offer, |
| 1607 | const MediaSessionOptions& session_options, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1608 | const SessionDescription* current_description) const { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1609 | if (!offer) { |
| 1610 | return nullptr; |
| 1611 | } |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1612 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1613 | // Must have options for exactly as many sections as in the offer. |
| 1614 | RTC_DCHECK_EQ(offer->contents().size(), |
| 1615 | session_options.media_description_options.size()); |
| 1616 | |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1617 | IceCredentialsIterator ice_credentials( |
| 1618 | session_options.pooled_ice_credentials); |
| 1619 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1620 | std::vector<const ContentInfo*> current_active_contents; |
| 1621 | if (current_description) { |
| 1622 | current_active_contents = |
| 1623 | GetActiveContents(*current_description, session_options); |
| 1624 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1625 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1626 | StreamParamsVec current_streams = |
| 1627 | GetCurrentStreamParams(current_active_contents); |
Johannes Kron | 0854eb6 | 2018-10-10 22:33:20 +0200 | [diff] [blame] | 1628 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1629 | // Get list of all possible codecs that respects existing payload type |
| 1630 | // mappings and uses a single payload type space. |
| 1631 | // |
| 1632 | // Note that these lists may be further filtered for each m= section; this |
| 1633 | // step is done just to establish the payload type mappings shared by all |
| 1634 | // sections. |
| 1635 | AudioCodecs answer_audio_codecs; |
| 1636 | VideoCodecs answer_video_codecs; |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1637 | DataCodecs answer_data_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1638 | GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1639 | &answer_video_codecs, &answer_data_codecs); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1640 | |
| 1641 | if (!session_options.vad_enabled) { |
| 1642 | // If application doesn't want CN codecs in answer. |
| 1643 | StripCNCodecs(&answer_audio_codecs); |
| 1644 | } |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1645 | FilterDataCodecs(&answer_data_codecs, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1646 | session_options.data_channel_type == DCT_SCTP); |
| 1647 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1648 | auto answer = absl::make_unique<SessionDescription>(); |
| 1649 | |
| 1650 | // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE |
| 1651 | // group in the answer with the appropriate content names. |
| 1652 | const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE); |
| 1653 | ContentGroup answer_bundle(GROUP_TYPE_BUNDLE); |
| 1654 | // Transport info shared by the bundle group. |
| 1655 | std::unique_ptr<TransportInfo> bundle_transport; |
| 1656 | |
| 1657 | answer->set_extmap_allow_mixed(offer->extmap_allow_mixed()); |
| 1658 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1659 | // Iterate through the media description options, matching with existing |
| 1660 | // media descriptions in |current_description|. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1661 | size_t msection_index = 0; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1662 | for (const MediaDescriptionOptions& media_description_options : |
| 1663 | session_options.media_description_options) { |
| 1664 | const ContentInfo* offer_content = &offer->contents()[msection_index]; |
| 1665 | // Media types and MIDs must match between the remote offer and the |
| 1666 | // MediaDescriptionOptions. |
| 1667 | RTC_DCHECK( |
| 1668 | IsMediaContentOfType(offer_content, media_description_options.type)); |
| 1669 | RTC_DCHECK(media_description_options.mid == offer_content->name); |
| 1670 | const ContentInfo* current_content = nullptr; |
| 1671 | if (current_description && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1672 | msection_index < current_description->contents().size()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1673 | current_content = ¤t_description->contents()[msection_index]; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1674 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1675 | switch (media_description_options.type) { |
| 1676 | case MEDIA_TYPE_AUDIO: |
| 1677 | if (!AddAudioContentForAnswer( |
| 1678 | media_description_options, session_options, offer_content, |
| 1679 | offer, current_content, current_description, |
| 1680 | bundle_transport.get(), answer_audio_codecs, ¤t_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1681 | answer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1682 | return nullptr; |
| 1683 | } |
| 1684 | break; |
| 1685 | case MEDIA_TYPE_VIDEO: |
| 1686 | if (!AddVideoContentForAnswer( |
| 1687 | media_description_options, session_options, offer_content, |
| 1688 | offer, current_content, current_description, |
| 1689 | bundle_transport.get(), answer_video_codecs, ¤t_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1690 | answer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1691 | return nullptr; |
| 1692 | } |
| 1693 | break; |
| 1694 | case MEDIA_TYPE_DATA: |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1695 | if (!AddDataContentForAnswer( |
| 1696 | media_description_options, session_options, offer_content, |
| 1697 | offer, current_content, current_description, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1698 | bundle_transport.get(), answer_data_codecs, ¤t_streams, |
| 1699 | answer.get(), &ice_credentials)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1700 | return nullptr; |
| 1701 | } |
| 1702 | break; |
| 1703 | default: |
| 1704 | RTC_NOTREACHED(); |
| 1705 | } |
| 1706 | ++msection_index; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1707 | // See if we can add the newly generated m= section to the BUNDLE group in |
| 1708 | // the answer. |
| 1709 | ContentInfo& added = answer->contents().back(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1710 | if (!added.rejected && session_options.bundle_enabled && offer_bundle && |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1711 | offer_bundle->HasContentName(added.name)) { |
| 1712 | answer_bundle.AddContentName(added.name); |
| 1713 | bundle_transport.reset( |
| 1714 | new TransportInfo(*answer->GetTransportInfoByName(added.name))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1715 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
Taylor Brandstetter | 0ab5651 | 2018-04-12 10:30:48 -0700 | [diff] [blame] | 1718 | // If a BUNDLE group was offered, put a BUNDLE group in the answer even if |
| 1719 | // it's empty. RFC5888 says: |
| 1720 | // |
| 1721 | // A SIP entity that receives an offer that contains an "a=group" line |
| 1722 | // with semantics that are understood MUST return an answer that |
| 1723 | // contains an "a=group" line with the same semantics. |
| 1724 | if (offer_bundle) { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1725 | answer->AddGroup(answer_bundle); |
Taylor Brandstetter | 0ab5651 | 2018-04-12 10:30:48 -0700 | [diff] [blame] | 1726 | } |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1727 | |
Taylor Brandstetter | 0ab5651 | 2018-04-12 10:30:48 -0700 | [diff] [blame] | 1728 | if (answer_bundle.FirstContentName()) { |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1729 | // Share the same ICE credentials and crypto params across all contents, |
| 1730 | // as BUNDLE requires. |
| 1731 | if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1732 | RTC_LOG(LS_ERROR) |
| 1733 | << "CreateAnswer failed to UpdateTransportInfoForBundle."; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1734 | return NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1735 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1736 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1737 | if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1738 | RTC_LOG(LS_ERROR) |
| 1739 | << "CreateAnswer failed to UpdateCryptoParamsForBundle."; |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 1740 | return NULL; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1744 | // The following determines how to signal MSIDs to ensure compatibility with |
| 1745 | // older endpoints (in particular, older Plan B endpoints). |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1746 | if (is_unified_plan_) { |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 1747 | // Unified Plan needs to look at what the offer included to find the most |
| 1748 | // compatible answer. |
| 1749 | if (offer->msid_signaling() == 0) { |
| 1750 | // We end up here in one of three cases: |
| 1751 | // 1. An empty offer. We'll reply with an empty answer so it doesn't |
| 1752 | // matter what we pick here. |
| 1753 | // 2. A data channel only offer. We won't add any MSIDs to the answer so |
| 1754 | // it also doesn't matter what we pick here. |
| 1755 | // 3. Media that's either sendonly or inactive from the remote endpoint. |
| 1756 | // We don't have any information to say whether the endpoint is Plan B |
| 1757 | // or Unified Plan, so be conservative and send both. |
| 1758 | answer->set_msid_signaling(cricket::kMsidSignalingMediaSection | |
| 1759 | cricket::kMsidSignalingSsrcAttribute); |
| 1760 | } else if (offer->msid_signaling() == |
| 1761 | (cricket::kMsidSignalingMediaSection | |
| 1762 | cricket::kMsidSignalingSsrcAttribute)) { |
| 1763 | // If both a=msid and a=ssrc MSID signaling methods were used, we're |
| 1764 | // probably talking to a Unified Plan endpoint so respond with just |
| 1765 | // a=msid. |
| 1766 | answer->set_msid_signaling(cricket::kMsidSignalingMediaSection); |
| 1767 | } else { |
| 1768 | // Otherwise, it's clear which method the offerer is using so repeat that |
| 1769 | // back to them. |
| 1770 | answer->set_msid_signaling(offer->msid_signaling()); |
| 1771 | } |
| 1772 | } else { |
| 1773 | // Plan B always signals MSID using a=ssrc lines. |
| 1774 | answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute); |
| 1775 | } |
| 1776 | |
Steve Anton | 6fe1fba | 2018-12-11 10:15:23 -0800 | [diff] [blame] | 1777 | return answer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1780 | const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer( |
| 1781 | const RtpTransceiverDirection& direction) const { |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1782 | switch (direction) { |
| 1783 | // If stream is inactive - generate list as if sendrecv. |
| 1784 | case RtpTransceiverDirection::kSendRecv: |
| 1785 | case RtpTransceiverDirection::kInactive: |
| 1786 | return audio_sendrecv_codecs_; |
| 1787 | case RtpTransceiverDirection::kSendOnly: |
| 1788 | return audio_send_codecs_; |
| 1789 | case RtpTransceiverDirection::kRecvOnly: |
| 1790 | return audio_recv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1791 | } |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1792 | RTC_NOTREACHED(); |
| 1793 | return audio_sendrecv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer( |
| 1797 | const RtpTransceiverDirection& offer, |
| 1798 | const RtpTransceiverDirection& answer) const { |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1799 | switch (answer) { |
| 1800 | // For inactive and sendrecv answers, generate lists as if we were to accept |
| 1801 | // the offer's direction. See RFC 3264 Section 6.1. |
| 1802 | case RtpTransceiverDirection::kSendRecv: |
| 1803 | case RtpTransceiverDirection::kInactive: |
| 1804 | return GetAudioCodecsForOffer( |
| 1805 | webrtc::RtpTransceiverDirectionReversed(offer)); |
| 1806 | case RtpTransceiverDirection::kSendOnly: |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1807 | return audio_send_codecs_; |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1808 | case RtpTransceiverDirection::kRecvOnly: |
| 1809 | return audio_recv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1810 | } |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 1811 | RTC_NOTREACHED(); |
| 1812 | return audio_sendrecv_codecs_; |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1815 | void MergeCodecsFromDescription( |
| 1816 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1817 | AudioCodecs* audio_codecs, |
| 1818 | VideoCodecs* video_codecs, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1819 | DataCodecs* data_codecs, |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1820 | UsedPayloadTypes* used_pltypes) { |
| 1821 | for (const ContentInfo* content : current_active_contents) { |
| 1822 | if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1823 | const AudioContentDescription* audio = |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1824 | content->media_description()->as_audio(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1825 | MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1826 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1827 | const VideoContentDescription* video = |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1828 | content->media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1829 | MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes); |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1830 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_DATA)) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1831 | const DataContentDescription* data = |
| 1832 | content->media_description()->as_data(); |
| 1833 | MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1834 | } |
| 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | // Getting codecs for an offer involves these steps: |
| 1839 | // |
| 1840 | // 1. Construct payload type -> codec mappings for current description. |
| 1841 | // 2. Add any reference codecs that weren't already present |
| 1842 | // 3. For each individual media description (m= section), filter codecs based |
| 1843 | // on the directional attribute (happens in another method). |
| 1844 | void MediaSessionDescriptionFactory::GetCodecsForOffer( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1845 | const std::vector<const ContentInfo*>& current_active_contents, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1846 | AudioCodecs* audio_codecs, |
| 1847 | VideoCodecs* video_codecs, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1848 | DataCodecs* data_codecs) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1849 | // First - get all codecs from the current description if the media type |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1850 | // is used. Add them to |used_pltypes| so the payload type is not reused if a |
| 1851 | // new media type is added. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1852 | UsedPayloadTypes used_pltypes; |
| 1853 | MergeCodecsFromDescription(current_active_contents, audio_codecs, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1854 | video_codecs, data_codecs, &used_pltypes); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1855 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1856 | // Add our codecs that are not in the current description. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1857 | MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes); |
| 1858 | MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes); |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1859 | MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | // Getting codecs for an answer involves these steps: |
| 1863 | // |
| 1864 | // 1. Construct payload type -> codec mappings for current description. |
| 1865 | // 2. Add any codecs from the offer that weren't already present. |
| 1866 | // 3. Add any remaining codecs that weren't already present. |
| 1867 | // 4. For each individual media description (m= section), filter codecs based |
| 1868 | // on the directional attribute (happens in another method). |
| 1869 | void MediaSessionDescriptionFactory::GetCodecsForAnswer( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1870 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1871 | const SessionDescription& remote_offer, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1872 | AudioCodecs* audio_codecs, |
| 1873 | VideoCodecs* video_codecs, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1874 | DataCodecs* data_codecs) const { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1875 | // First - get all codecs from the current description if the media type |
| 1876 | // is used. Add them to |used_pltypes| so the payload type is not reused if a |
| 1877 | // new media type is added. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1878 | UsedPayloadTypes used_pltypes; |
| 1879 | MergeCodecsFromDescription(current_active_contents, audio_codecs, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1880 | video_codecs, data_codecs, &used_pltypes); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1881 | |
| 1882 | // Second - filter out codecs that we don't support at all and should ignore. |
| 1883 | AudioCodecs filtered_offered_audio_codecs; |
| 1884 | VideoCodecs filtered_offered_video_codecs; |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1885 | DataCodecs filtered_offered_data_codecs; |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1886 | for (const ContentInfo& content : remote_offer.contents()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1887 | if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) { |
| 1888 | const AudioContentDescription* audio = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1889 | content.media_description()->as_audio(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1890 | for (const AudioCodec& offered_audio_codec : audio->codecs()) { |
| 1891 | if (!FindMatchingCodec<AudioCodec>(audio->codecs(), |
| 1892 | filtered_offered_audio_codecs, |
| 1893 | offered_audio_codec, nullptr) && |
| 1894 | FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_, |
| 1895 | offered_audio_codec, nullptr)) { |
| 1896 | filtered_offered_audio_codecs.push_back(offered_audio_codec); |
| 1897 | } |
| 1898 | } |
| 1899 | } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) { |
| 1900 | const VideoContentDescription* video = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1901 | content.media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1902 | for (const VideoCodec& offered_video_codec : video->codecs()) { |
| 1903 | if (!FindMatchingCodec<VideoCodec>(video->codecs(), |
| 1904 | filtered_offered_video_codecs, |
| 1905 | offered_video_codec, nullptr) && |
| 1906 | FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_, |
| 1907 | offered_video_codec, nullptr)) { |
| 1908 | filtered_offered_video_codecs.push_back(offered_video_codec); |
| 1909 | } |
| 1910 | } |
| 1911 | } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1912 | const DataContentDescription* data = |
| 1913 | content.media_description()->as_data(); |
| 1914 | for (const DataCodec& offered_data_codec : data->codecs()) { |
| 1915 | if (!FindMatchingCodec<DataCodec>(data->codecs(), |
| 1916 | filtered_offered_data_codecs, |
| 1917 | offered_data_codec, nullptr) && |
| 1918 | FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_, |
| 1919 | offered_data_codec, nullptr)) { |
| 1920 | filtered_offered_data_codecs.push_back(offered_data_codec); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1926 | // Add codecs that are not in the current description but were in |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1927 | // |remote_offer|. |
| 1928 | MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs, |
| 1929 | &used_pltypes); |
| 1930 | MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs, |
| 1931 | &used_pltypes); |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 1932 | MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1933 | &used_pltypes); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer( |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1937 | const std::vector<const ContentInfo*>& current_active_contents, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1938 | RtpHeaderExtensions* offer_audio_extensions, |
| 1939 | RtpHeaderExtensions* offer_video_extensions) const { |
henrike@webrtc.org | 79047f9 | 2014-03-06 23:46:59 +0000 | [diff] [blame] | 1940 | // All header extensions allocated from the same range to avoid potential |
| 1941 | // issues when using BUNDLE. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1942 | UsedRtpHeaderExtensionIds used_ids; |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1943 | RtpHeaderExtensions all_regular_extensions; |
| 1944 | RtpHeaderExtensions all_encrypted_extensions; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1945 | |
| 1946 | // First - get all extensions from the current description if the media type |
| 1947 | // is used. |
| 1948 | // Add them to |used_ids| so the local ids are not reused if a new media |
| 1949 | // type is added. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1950 | for (const ContentInfo* content : current_active_contents) { |
| 1951 | if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) { |
| 1952 | const AudioContentDescription* audio = |
| 1953 | content->media_description()->as_audio(); |
| 1954 | MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions, |
| 1955 | &all_regular_extensions, &all_encrypted_extensions, |
| 1956 | &used_ids); |
| 1957 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) { |
| 1958 | const VideoContentDescription* video = |
| 1959 | content->media_description()->as_video(); |
| 1960 | MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions, |
| 1961 | &all_regular_extensions, &all_encrypted_extensions, |
| 1962 | &used_ids); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1963 | } |
| 1964 | } |
| 1965 | |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1966 | // Add our default RTP header extensions that are not in the current |
| 1967 | // description. |
Steve Anton | 8f66ddb | 2018-12-10 16:08:05 -0800 | [diff] [blame] | 1968 | MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions, |
| 1969 | &all_regular_extensions, &all_encrypted_extensions, |
| 1970 | &used_ids); |
| 1971 | MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions, |
| 1972 | &all_regular_extensions, &all_encrypted_extensions, |
| 1973 | &used_ids); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1974 | |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1975 | // TODO(jbauch): Support adding encrypted header extensions to existing |
| 1976 | // sessions. |
Steve Anton | 5c72e71 | 2018-12-10 14:25:30 -0800 | [diff] [blame] | 1977 | if (enable_encrypted_rtp_header_extensions_ && |
| 1978 | current_active_contents.empty()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1979 | AddEncryptedVersionsOfHdrExts(offer_audio_extensions, |
| 1980 | &all_encrypted_extensions, &used_ids); |
| 1981 | AddEncryptedVersionsOfHdrExts(offer_video_extensions, |
| 1982 | &all_encrypted_extensions, &used_ids); |
jbauch | 5869f50 | 2017-06-29 12:31:36 -0700 | [diff] [blame] | 1983 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | bool MediaSessionDescriptionFactory::AddTransportOffer( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1987 | const std::string& content_name, |
| 1988 | const TransportOptions& transport_options, |
| 1989 | const SessionDescription* current_desc, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1990 | SessionDescription* offer_desc, |
| 1991 | IceCredentialsIterator* ice_credentials) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1992 | if (!transport_desc_factory_) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1993 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1994 | const TransportDescription* current_tdesc = |
| 1995 | GetTransportDescription(content_name, current_desc); |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 1996 | std::unique_ptr<TransportDescription> new_tdesc( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 1997 | transport_desc_factory_->CreateOffer(transport_options, current_tdesc, |
| 1998 | ice_credentials)); |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 1999 | if (!new_tdesc) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2000 | RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name=" |
| 2001 | << content_name; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2002 | } |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2003 | offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)); |
| 2004 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2007 | std::unique_ptr<TransportDescription> |
| 2008 | MediaSessionDescriptionFactory::CreateTransportAnswer( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2009 | const std::string& content_name, |
| 2010 | const SessionDescription* offer_desc, |
| 2011 | const TransportOptions& transport_options, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2012 | const SessionDescription* current_desc, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2013 | bool require_transport_attributes, |
| 2014 | IceCredentialsIterator* ice_credentials) const { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2015 | if (!transport_desc_factory_) |
| 2016 | return NULL; |
| 2017 | const TransportDescription* offer_tdesc = |
| 2018 | GetTransportDescription(content_name, offer_desc); |
| 2019 | const TransportDescription* current_tdesc = |
| 2020 | GetTransportDescription(content_name, current_desc); |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2021 | return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options, |
| 2022 | require_transport_attributes, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2023 | current_tdesc, ice_credentials); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | bool MediaSessionDescriptionFactory::AddTransportAnswer( |
| 2027 | const std::string& content_name, |
| 2028 | const TransportDescription& transport_desc, |
| 2029 | SessionDescription* answer_desc) const { |
Steve Anton | 06817cd | 2018-12-18 15:55:30 -0800 | [diff] [blame] | 2030 | answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2031 | return true; |
| 2032 | } |
| 2033 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2034 | // |audio_codecs| = set of all possible codecs that can be used, with correct |
| 2035 | // payload type mappings |
| 2036 | // |
| 2037 | // |supported_audio_codecs| = set of codecs that are supported for the direction |
| 2038 | // of this m= section |
| 2039 | // |
| 2040 | // acd->codecs() = set of previously negotiated codecs for this m= section |
| 2041 | // |
| 2042 | // The payload types should come from audio_codecs, but the order should come |
| 2043 | // from acd->codecs() and then supported_codecs, to ensure that re-offers don't |
| 2044 | // change existing codec priority, and that new codecs are added with the right |
| 2045 | // priority. |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2046 | bool MediaSessionDescriptionFactory::AddAudioContentForOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2047 | const MediaDescriptionOptions& media_description_options, |
| 2048 | const MediaSessionOptions& session_options, |
| 2049 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2050 | const SessionDescription* current_description, |
| 2051 | const RtpHeaderExtensions& audio_rtp_extensions, |
| 2052 | const AudioCodecs& audio_codecs, |
| 2053 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2054 | SessionDescription* desc, |
| 2055 | IceCredentialsIterator* ice_credentials) const { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2056 | // Filter audio_codecs (which includes all codecs, with correctly remapped |
| 2057 | // payload types) based on transceiver direction. |
| 2058 | const AudioCodecs& supported_audio_codecs = |
| 2059 | GetAudioCodecsForOffer(media_description_options.direction); |
| 2060 | |
| 2061 | AudioCodecs filtered_codecs; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2062 | |
| 2063 | if (!media_description_options.codec_preferences.empty()) { |
| 2064 | // Add the codecs from the current transceiver's codec preferences. |
| 2065 | // They override any existing codecs from previous negotiations. |
| 2066 | filtered_codecs = MatchCodecPreference( |
| 2067 | media_description_options.codec_preferences, supported_audio_codecs); |
| 2068 | } else { |
| 2069 | // Add the codecs from current content if it exists and is not rejected nor |
| 2070 | // recycled. |
| 2071 | if (current_content && !current_content->rejected && |
| 2072 | current_content->name == media_description_options.mid) { |
| 2073 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO)); |
| 2074 | const AudioContentDescription* acd = |
| 2075 | current_content->media_description()->as_audio(); |
| 2076 | for (const AudioCodec& codec : acd->codecs()) { |
| 2077 | if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec, |
| 2078 | nullptr)) { |
| 2079 | filtered_codecs.push_back(codec); |
| 2080 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2081 | } |
| 2082 | } |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2083 | // Add other supported audio codecs. |
| 2084 | AudioCodec found_codec; |
| 2085 | for (const AudioCodec& codec : supported_audio_codecs) { |
| 2086 | if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs, |
| 2087 | codec, &found_codec) && |
| 2088 | !FindMatchingCodec<AudioCodec>(supported_audio_codecs, |
| 2089 | filtered_codecs, codec, nullptr)) { |
| 2090 | // Use the |found_codec| from |audio_codecs| because it has the |
| 2091 | // correctly mapped payload type. |
| 2092 | filtered_codecs.push_back(found_codec); |
| 2093 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2094 | } |
| 2095 | } |
deadbeef | 44f0819 | 2015-12-15 16:20:09 -0800 | [diff] [blame] | 2096 | |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2097 | cricket::SecurePolicy sdes_policy = |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2098 | IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED |
| 2099 | : secure(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2100 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2101 | std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2102 | std::vector<std::string> crypto_suites; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2103 | GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options, |
| 2104 | &crypto_suites); |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2105 | if (!CreateMediaContentOffer(media_description_options, session_options, |
| 2106 | filtered_codecs, sdes_policy, |
| 2107 | GetCryptos(current_content), crypto_suites, |
| 2108 | audio_rtp_extensions, ssrc_generator_, |
| 2109 | current_streams, audio.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2110 | return false; |
| 2111 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2112 | |
| 2113 | bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); |
| 2114 | SetMediaProtocol(secure_transport, audio.get()); |
jiayl@webrtc.org | 7d4891d | 2014-09-09 21:43:15 +0000 | [diff] [blame] | 2115 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 2116 | audio->set_direction(media_description_options.direction); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 2117 | |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2118 | desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2119 | media_description_options.stopped, audio.release()); |
| 2120 | if (!AddTransportOffer(media_description_options.mid, |
| 2121 | media_description_options.transport_options, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2122 | current_description, desc, ice_credentials)) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2123 | return false; |
| 2124 | } |
| 2125 | |
| 2126 | return true; |
| 2127 | } |
| 2128 | |
| 2129 | bool MediaSessionDescriptionFactory::AddVideoContentForOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2130 | const MediaDescriptionOptions& media_description_options, |
| 2131 | const MediaSessionOptions& session_options, |
| 2132 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2133 | const SessionDescription* current_description, |
| 2134 | const RtpHeaderExtensions& video_rtp_extensions, |
| 2135 | const VideoCodecs& video_codecs, |
| 2136 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2137 | SessionDescription* desc, |
| 2138 | IceCredentialsIterator* ice_credentials) const { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2139 | cricket::SecurePolicy sdes_policy = |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2140 | IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED |
| 2141 | : secure(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2142 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2143 | std::unique_ptr<VideoContentDescription> video(new VideoContentDescription()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2144 | std::vector<std::string> crypto_suites; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2145 | GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options, |
| 2146 | &crypto_suites); |
| 2147 | |
| 2148 | VideoCodecs filtered_codecs; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2149 | |
| 2150 | if (!media_description_options.codec_preferences.empty()) { |
| 2151 | // Add the codecs from the current transceiver's codec preferences. |
| 2152 | // They override any existing codecs from previous negotiations. |
| 2153 | filtered_codecs = MatchCodecPreference( |
| 2154 | media_description_options.codec_preferences, video_codecs_); |
| 2155 | } else { |
| 2156 | // Add the codecs from current content if it exists and is not rejected nor |
| 2157 | // recycled. |
| 2158 | if (current_content && !current_content->rejected && |
| 2159 | current_content->name == media_description_options.mid) { |
| 2160 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO)); |
| 2161 | const VideoContentDescription* vcd = |
| 2162 | current_content->media_description()->as_video(); |
| 2163 | for (const VideoCodec& codec : vcd->codecs()) { |
| 2164 | if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec, |
| 2165 | nullptr)) { |
| 2166 | filtered_codecs.push_back(codec); |
| 2167 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2168 | } |
| 2169 | } |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2170 | // Add other supported video codecs. |
| 2171 | VideoCodec found_codec; |
| 2172 | for (const VideoCodec& codec : video_codecs_) { |
| 2173 | if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec, |
| 2174 | &found_codec) && |
| 2175 | !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec, |
| 2176 | nullptr)) { |
| 2177 | // Use the |found_codec| from |video_codecs| because it has the |
| 2178 | // correctly mapped payload type. |
| 2179 | filtered_codecs.push_back(found_codec); |
| 2180 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2184 | if (!CreateMediaContentOffer(media_description_options, session_options, |
| 2185 | filtered_codecs, sdes_policy, |
| 2186 | GetCryptos(current_content), crypto_suites, |
| 2187 | video_rtp_extensions, ssrc_generator_, |
| 2188 | current_streams, video.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2189 | return false; |
| 2190 | } |
| 2191 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2192 | video->set_bandwidth(kAutoBandwidth); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2193 | |
| 2194 | bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); |
| 2195 | SetMediaProtocol(secure_transport, video.get()); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 2196 | |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 2197 | video->set_direction(media_description_options.direction); |
jiayl@webrtc.org | 742922b | 2014-10-07 21:32:43 +0000 | [diff] [blame] | 2198 | |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2199 | desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2200 | media_description_options.stopped, video.release()); |
| 2201 | if (!AddTransportOffer(media_description_options.mid, |
| 2202 | media_description_options.transport_options, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2203 | current_description, desc, ice_credentials)) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2204 | return false; |
| 2205 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2206 | return true; |
| 2207 | } |
| 2208 | |
| 2209 | bool MediaSessionDescriptionFactory::AddDataContentForOffer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2210 | const MediaDescriptionOptions& media_description_options, |
| 2211 | const MediaSessionOptions& session_options, |
| 2212 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2213 | const SessionDescription* current_description, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2214 | const DataCodecs& data_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2215 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2216 | SessionDescription* desc, |
| 2217 | IceCredentialsIterator* ice_credentials) const { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2218 | bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); |
| 2219 | |
| 2220 | std::unique_ptr<DataContentDescription> data(new DataContentDescription()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2221 | bool is_sctp = (session_options.data_channel_type == DCT_SCTP); |
| 2222 | // If the DataChannel type is not specified, use the DataChannel type in |
| 2223 | // the current description. |
| 2224 | if (session_options.data_channel_type == DCT_NONE && current_content) { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2225 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA)); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2226 | is_sctp = (current_content->media_description()->protocol() == |
| 2227 | kMediaProtocolSctp); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2228 | } |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2229 | |
| 2230 | cricket::SecurePolicy sdes_policy = |
| 2231 | IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED |
| 2232 | : secure(); |
| 2233 | std::vector<std::string> crypto_suites; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2234 | if (is_sctp) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2235 | // SDES doesn't make sense for SCTP, so we disable it, and we only |
| 2236 | // get SDES crypto suites for RTP-based data channels. |
| 2237 | sdes_policy = cricket::SEC_DISABLED; |
| 2238 | // Unlike SetMediaProtocol below, we need to set the protocol |
| 2239 | // before we call CreateMediaContentOffer. Otherwise, |
| 2240 | // CreateMediaContentOffer won't know this is SCTP and will |
| 2241 | // generate SSRCs rather than SIDs. |
| 2242 | // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once |
| 2243 | // it's safe to do so. Older versions of webrtc would reject these |
| 2244 | // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706. |
| 2245 | data->set_protocol(secure_transport ? kMediaProtocolDtlsSctp |
| 2246 | : kMediaProtocolSctp); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2247 | } else { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2248 | GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options, |
| 2249 | &crypto_suites); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2250 | } |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2251 | |
| 2252 | // Even SCTP uses a "codec". |
| 2253 | if (!CreateMediaContentOffer( |
| 2254 | media_description_options, session_options, data_codecs, sdes_policy, |
| 2255 | GetCryptos(current_content), crypto_suites, RtpHeaderExtensions(), |
| 2256 | ssrc_generator_, current_streams, data.get())) { |
| 2257 | return false; |
| 2258 | } |
| 2259 | |
| 2260 | if (is_sctp) { |
| 2261 | desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp, |
| 2262 | data.release()); |
| 2263 | } else { |
| 2264 | data->set_bandwidth(kDataMaxBandwidth); |
| 2265 | SetMediaProtocol(secure_transport, data.get()); |
| 2266 | desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp, |
| 2267 | media_description_options.stopped, data.release()); |
| 2268 | } |
| 2269 | if (!AddTransportOffer(media_description_options.mid, |
| 2270 | media_description_options.transport_options, |
| 2271 | current_description, desc, ice_credentials)) { |
| 2272 | return false; |
| 2273 | } |
| 2274 | return true; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2277 | // |audio_codecs| = set of all possible codecs that can be used, with correct |
| 2278 | // payload type mappings |
| 2279 | // |
| 2280 | // |supported_audio_codecs| = set of codecs that are supported for the direction |
| 2281 | // of this m= section |
| 2282 | // |
| 2283 | // acd->codecs() = set of previously negotiated codecs for this m= section |
| 2284 | // |
| 2285 | // The payload types should come from audio_codecs, but the order should come |
| 2286 | // from acd->codecs() and then supported_codecs, to ensure that re-offers don't |
| 2287 | // change existing codec priority, and that new codecs are added with the right |
| 2288 | // priority. |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2289 | bool MediaSessionDescriptionFactory::AddAudioContentForAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2290 | const MediaDescriptionOptions& media_description_options, |
| 2291 | const MediaSessionOptions& session_options, |
| 2292 | const ContentInfo* offer_content, |
| 2293 | const SessionDescription* offer_description, |
| 2294 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2295 | const SessionDescription* current_description, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2296 | const TransportInfo* bundle_transport, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2297 | const AudioCodecs& audio_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2298 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2299 | SessionDescription* answer, |
| 2300 | IceCredentialsIterator* ice_credentials) const { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2301 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2302 | const AudioContentDescription* offer_audio_description = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2303 | offer_content->media_description()->as_audio(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2304 | |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2305 | std::unique_ptr<TransportDescription> audio_transport = CreateTransportAnswer( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2306 | media_description_options.mid, offer_description, |
| 2307 | media_description_options.transport_options, current_description, |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2308 | bundle_transport != nullptr, ice_credentials); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2309 | if (!audio_transport) { |
| 2310 | return false; |
| 2311 | } |
| 2312 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2313 | // Pick codecs based on the requested communications direction in the offer |
| 2314 | // and the selected direction in the answer. |
| 2315 | // Note these will be filtered one final time in CreateMediaContentAnswer. |
| 2316 | auto wants_rtd = media_description_options.direction; |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 2317 | auto offer_rtd = offer_audio_description->direction(); |
ossu | 075af92 | 2016-06-14 03:29:38 -0700 | [diff] [blame] | 2318 | auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2319 | AudioCodecs supported_audio_codecs = |
| 2320 | GetAudioCodecsForAnswer(offer_rtd, answer_rtd); |
| 2321 | |
| 2322 | AudioCodecs filtered_codecs; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2323 | |
| 2324 | if (!media_description_options.codec_preferences.empty()) { |
| 2325 | filtered_codecs = MatchCodecPreference( |
| 2326 | media_description_options.codec_preferences, supported_audio_codecs); |
| 2327 | } else { |
| 2328 | // Add the codecs from current content if it exists and is not rejected nor |
| 2329 | // recycled. |
| 2330 | if (current_content && !current_content->rejected && |
| 2331 | current_content->name == media_description_options.mid) { |
| 2332 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO)); |
| 2333 | const AudioContentDescription* acd = |
| 2334 | current_content->media_description()->as_audio(); |
| 2335 | for (const AudioCodec& codec : acd->codecs()) { |
| 2336 | if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec, |
| 2337 | nullptr)) { |
| 2338 | filtered_codecs.push_back(codec); |
| 2339 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2340 | } |
| 2341 | } |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2342 | // Add other supported audio codecs. |
| 2343 | for (const AudioCodec& codec : supported_audio_codecs) { |
| 2344 | if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs, |
| 2345 | codec, nullptr) && |
| 2346 | !FindMatchingCodec<AudioCodec>(supported_audio_codecs, |
| 2347 | filtered_codecs, codec, nullptr)) { |
| 2348 | // We should use the local codec with local parameters and the codec id |
| 2349 | // would be correctly mapped in |NegotiateCodecs|. |
| 2350 | filtered_codecs.push_back(codec); |
| 2351 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2352 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2355 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2356 | session_options.bundle_enabled; |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2357 | std::unique_ptr<AudioContentDescription> audio_answer( |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2358 | new AudioContentDescription()); |
| 2359 | // Do not require or create SDES cryptos if DTLS is used. |
| 2360 | cricket::SecurePolicy sdes_policy = |
| 2361 | audio_transport->secure() ? cricket::SEC_DISABLED : secure(); |
| 2362 | if (!CreateMediaContentAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2363 | offer_audio_description, media_description_options, session_options, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2364 | filtered_codecs, sdes_policy, GetCryptos(current_content), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2365 | audio_rtp_header_extensions(), ssrc_generator_, |
Steve Anton | 1b8773d | 2018-04-06 11:13:34 -0700 | [diff] [blame] | 2366 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2367 | bundle_enabled, audio_answer.get())) { |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2368 | return false; // Fails the session setup. |
| 2369 | } |
| 2370 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2371 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2372 | : audio_transport->secure(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2373 | bool rejected = media_description_options.stopped || |
| 2374 | offer_content->rejected || |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2375 | !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO, |
| 2376 | audio_answer->protocol(), secure); |
Zhi Huang | 3518e7b | 2018-01-30 13:20:35 -0800 | [diff] [blame] | 2377 | if (!AddTransportAnswer(media_description_options.mid, |
| 2378 | *(audio_transport.get()), answer)) { |
| 2379 | return false; |
| 2380 | } |
| 2381 | |
| 2382 | if (rejected) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2383 | RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid |
| 2384 | << "' being rejected in answer."; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2387 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2388 | rejected, audio_answer.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2389 | return true; |
| 2390 | } |
| 2391 | |
| 2392 | bool MediaSessionDescriptionFactory::AddVideoContentForAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2393 | const MediaDescriptionOptions& media_description_options, |
| 2394 | const MediaSessionOptions& session_options, |
| 2395 | const ContentInfo* offer_content, |
| 2396 | const SessionDescription* offer_description, |
| 2397 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2398 | const SessionDescription* current_description, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2399 | const TransportInfo* bundle_transport, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2400 | const VideoCodecs& video_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2401 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2402 | SessionDescription* answer, |
| 2403 | IceCredentialsIterator* ice_credentials) const { |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2404 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2405 | const VideoContentDescription* offer_video_description = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2406 | offer_content->media_description()->as_video(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2407 | |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2408 | std::unique_ptr<TransportDescription> video_transport = CreateTransportAnswer( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2409 | media_description_options.mid, offer_description, |
| 2410 | media_description_options.transport_options, current_description, |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2411 | bundle_transport != nullptr, ice_credentials); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2412 | if (!video_transport) { |
| 2413 | return false; |
| 2414 | } |
| 2415 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2416 | VideoCodecs filtered_codecs; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2417 | |
| 2418 | if (!media_description_options.codec_preferences.empty()) { |
| 2419 | filtered_codecs = MatchCodecPreference( |
| 2420 | media_description_options.codec_preferences, video_codecs_); |
| 2421 | } else { |
| 2422 | // Add the codecs from current content if it exists and is not rejected nor |
| 2423 | // recycled. |
| 2424 | if (current_content && !current_content->rejected && |
| 2425 | current_content->name == media_description_options.mid) { |
| 2426 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO)); |
| 2427 | const VideoContentDescription* vcd = |
| 2428 | current_content->media_description()->as_video(); |
| 2429 | for (const VideoCodec& codec : vcd->codecs()) { |
| 2430 | if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec, |
| 2431 | nullptr)) { |
| 2432 | filtered_codecs.push_back(codec); |
| 2433 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2434 | } |
| 2435 | } |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2436 | // Add other supported video codecs. |
| 2437 | for (const VideoCodec& codec : video_codecs_) { |
| 2438 | if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec, |
| 2439 | nullptr) && |
| 2440 | !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec, |
| 2441 | nullptr)) { |
| 2442 | // We should use the local codec with local parameters and the codec id |
| 2443 | // would be correctly mapped in |NegotiateCodecs|. |
| 2444 | filtered_codecs.push_back(codec); |
| 2445 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2450 | session_options.bundle_enabled; |
| 2451 | |
kwiberg | 3102294 | 2016-03-11 14:18:21 -0800 | [diff] [blame] | 2452 | std::unique_ptr<VideoContentDescription> video_answer( |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2453 | new VideoContentDescription()); |
| 2454 | // Do not require or create SDES cryptos if DTLS is used. |
| 2455 | cricket::SecurePolicy sdes_policy = |
| 2456 | video_transport->secure() ? cricket::SEC_DISABLED : secure(); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2457 | if (!CreateMediaContentAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2458 | offer_video_description, media_description_options, session_options, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2459 | filtered_codecs, sdes_policy, GetCryptos(current_content), |
Amit Hilbuch | bcd39d4 | 2019-01-25 17:13:56 -0800 | [diff] [blame] | 2460 | video_rtp_header_extensions(), ssrc_generator_, |
Steve Anton | 1b8773d | 2018-04-06 11:13:34 -0700 | [diff] [blame] | 2461 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2462 | bundle_enabled, video_answer.get())) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2463 | return false; // Failed the sessin setup. |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2464 | } |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2465 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2466 | : video_transport->secure(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2467 | bool rejected = media_description_options.stopped || |
| 2468 | offer_content->rejected || |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2469 | !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO, |
| 2470 | video_answer->protocol(), secure); |
Zhi Huang | 3518e7b | 2018-01-30 13:20:35 -0800 | [diff] [blame] | 2471 | if (!AddTransportAnswer(media_description_options.mid, |
| 2472 | *(video_transport.get()), answer)) { |
| 2473 | return false; |
| 2474 | } |
| 2475 | |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2476 | if (!rejected) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2477 | video_answer->set_bandwidth(kAutoBandwidth); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2478 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2479 | RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid |
| 2480 | << "' being rejected in answer."; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2481 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2482 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2483 | rejected, video_answer.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2484 | return true; |
| 2485 | } |
| 2486 | |
| 2487 | bool MediaSessionDescriptionFactory::AddDataContentForAnswer( |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2488 | const MediaDescriptionOptions& media_description_options, |
| 2489 | const MediaSessionOptions& session_options, |
| 2490 | const ContentInfo* offer_content, |
| 2491 | const SessionDescription* offer_description, |
| 2492 | const ContentInfo* current_content, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2493 | const SessionDescription* current_description, |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2494 | const TransportInfo* bundle_transport, |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2495 | const DataCodecs& data_codecs, |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2496 | StreamParamsVec* current_streams, |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2497 | SessionDescription* answer, |
| 2498 | IceCredentialsIterator* ice_credentials) const { |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2499 | std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer( |
Jonas Oreland | 1cd39fa | 2018-10-11 07:47:12 +0200 | [diff] [blame] | 2500 | media_description_options.mid, offer_description, |
| 2501 | media_description_options.transport_options, current_description, |
Steve Anton | 1a9d3c3 | 2018-12-10 17:18:54 -0800 | [diff] [blame] | 2502 | bundle_transport != nullptr, ice_credentials); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2503 | if (!data_transport) { |
| 2504 | return false; |
| 2505 | } |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2506 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2507 | std::unique_ptr<DataContentDescription> data_answer( |
| 2508 | new DataContentDescription()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2509 | // Do not require or create SDES cryptos if DTLS is used. |
| 2510 | cricket::SecurePolicy sdes_policy = |
| 2511 | data_transport->secure() ? cricket::SEC_DISABLED : secure(); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2512 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2513 | session_options.bundle_enabled; |
Taylor Brandstetter | 80cfb52 | 2017-10-12 20:37:38 -0700 | [diff] [blame] | 2514 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA)); |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2515 | const DataContentDescription* offer_data_description = |
| 2516 | offer_content->media_description()->as_data(); |
| 2517 | if (!CreateMediaContentAnswer( |
| 2518 | offer_data_description, media_description_options, session_options, |
| 2519 | data_codecs, sdes_policy, GetCryptos(current_content), |
| 2520 | RtpHeaderExtensions(), ssrc_generator_, |
| 2521 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2522 | bundle_enabled, data_answer.get())) { |
| 2523 | return false; // Fails the session setup. |
Harald Alvestrand | 14b2758 | 2019-05-04 11:37:04 +0200 | [diff] [blame] | 2524 | } |
Danil Chapovalov | c6d1d24 | 2019-04-23 09:48:11 +0000 | [diff] [blame] | 2525 | |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2526 | // Respond with sctpmap if the offer uses sctpmap. |
| 2527 | bool offer_uses_sctpmap = offer_data_description->use_sctpmap(); |
| 2528 | data_answer->set_use_sctpmap(offer_uses_sctpmap); |
| 2529 | |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2530 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2531 | : data_transport->secure(); |
| 2532 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2533 | bool rejected = session_options.data_channel_type == DCT_NONE || |
| 2534 | media_description_options.stopped || |
| 2535 | offer_content->rejected || |
deadbeef | b789253 | 2017-02-22 19:35:18 -0800 | [diff] [blame] | 2536 | !IsMediaProtocolSupported(MEDIA_TYPE_DATA, |
| 2537 | data_answer->protocol(), secure); |
Zhi Huang | 3518e7b | 2018-01-30 13:20:35 -0800 | [diff] [blame] | 2538 | if (!AddTransportAnswer(media_description_options.mid, |
| 2539 | *(data_transport.get()), answer)) { |
| 2540 | return false; |
| 2541 | } |
| 2542 | |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2543 | if (!rejected) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2544 | data_answer->set_bandwidth(kDataMaxBandwidth); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2545 | } else { |
| 2546 | // RFC 3264 |
| 2547 | // 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] | 2548 | RTC_LOG(LS_INFO) << "Data is not supported in the answer."; |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2549 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2550 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2551 | rejected, data_answer.release()); |
jiayl@webrtc.org | e7d47a1 | 2014-08-05 19:19:05 +0000 | [diff] [blame] | 2552 | return true; |
| 2553 | } |
| 2554 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2555 | void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() { |
| 2556 | audio_sendrecv_codecs_.clear(); |
| 2557 | all_audio_codecs_.clear(); |
| 2558 | // Compute the audio codecs union. |
| 2559 | for (const AudioCodec& send : audio_send_codecs_) { |
| 2560 | all_audio_codecs_.push_back(send); |
| 2561 | if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_, |
| 2562 | send, nullptr)) { |
| 2563 | // It doesn't make sense to have an RTX codec we support sending but not |
| 2564 | // receiving. |
| 2565 | RTC_DCHECK(!IsRtxCodec(send)); |
| 2566 | } |
| 2567 | } |
| 2568 | for (const AudioCodec& recv : audio_recv_codecs_) { |
| 2569 | if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_, |
| 2570 | recv, nullptr)) { |
| 2571 | all_audio_codecs_.push_back(recv); |
| 2572 | } |
| 2573 | } |
| 2574 | // Use NegotiateCodecs to merge our codec lists, since the operation is |
| 2575 | // essentially the same. Put send_codecs as the offered_codecs, which is the |
| 2576 | // order we'd like to follow. The reasoning is that encoding is usually more |
| 2577 | // expensive than decoding, and prioritizing a codec in the send list probably |
| 2578 | // means it's a codec we can handle efficiently. |
| 2579 | NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_, |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 2580 | &audio_sendrecv_codecs_, true); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2583 | bool IsMediaContent(const ContentInfo* content) { |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 2584 | return (content && (content->type == MediaProtocolType::kRtp || |
| 2585 | content->type == MediaProtocolType::kSctp)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | bool IsAudioContent(const ContentInfo* content) { |
| 2589 | return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO); |
| 2590 | } |
| 2591 | |
| 2592 | bool IsVideoContent(const ContentInfo* content) { |
| 2593 | return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO); |
| 2594 | } |
| 2595 | |
| 2596 | bool IsDataContent(const ContentInfo* content) { |
| 2597 | return IsMediaContentOfType(content, MEDIA_TYPE_DATA); |
| 2598 | } |
| 2599 | |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 2600 | const ContentInfo* GetFirstMediaContent(const ContentInfos& contents, |
| 2601 | MediaType media_type) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2602 | for (const ContentInfo& content : contents) { |
| 2603 | if (IsMediaContentOfType(&content, media_type)) { |
| 2604 | return &content; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2605 | } |
| 2606 | } |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 2607 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) { |
| 2611 | return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO); |
| 2612 | } |
| 2613 | |
| 2614 | const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) { |
| 2615 | return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO); |
| 2616 | } |
| 2617 | |
| 2618 | const ContentInfo* GetFirstDataContent(const ContentInfos& contents) { |
| 2619 | return GetFirstMediaContent(contents, MEDIA_TYPE_DATA); |
| 2620 | } |
| 2621 | |
Steve Anton | ad7bffc | 2018-01-22 10:21:56 -0800 | [diff] [blame] | 2622 | const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc, |
| 2623 | MediaType media_type) { |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 2624 | if (sdesc == nullptr) { |
| 2625 | return nullptr; |
| 2626 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2627 | |
| 2628 | return GetFirstMediaContent(sdesc->contents(), media_type); |
| 2629 | } |
| 2630 | |
| 2631 | const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) { |
| 2632 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO); |
| 2633 | } |
| 2634 | |
| 2635 | const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) { |
| 2636 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO); |
| 2637 | } |
| 2638 | |
| 2639 | const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) { |
| 2640 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA); |
| 2641 | } |
| 2642 | |
| 2643 | const MediaContentDescription* GetFirstMediaContentDescription( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 2644 | const SessionDescription* sdesc, |
| 2645 | MediaType media_type) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2646 | const ContentInfo* content = GetFirstMediaContent(sdesc, media_type); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2647 | return (content ? content->media_description() : nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | const AudioContentDescription* GetFirstAudioContentDescription( |
| 2651 | const SessionDescription* sdesc) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2652 | return static_cast<const AudioContentDescription*>( |
| 2653 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | const VideoContentDescription* GetFirstVideoContentDescription( |
| 2657 | const SessionDescription* sdesc) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2658 | return static_cast<const VideoContentDescription*>( |
| 2659 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | const DataContentDescription* GetFirstDataContentDescription( |
| 2663 | const SessionDescription* sdesc) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2664 | return static_cast<const DataContentDescription*>( |
| 2665 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2668 | // |
| 2669 | // Non-const versions of the above functions. |
| 2670 | // |
| 2671 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2672 | ContentInfo* GetFirstMediaContent(ContentInfos* contents, |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2673 | MediaType media_type) { |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2674 | for (ContentInfo& content : *contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2675 | if (IsMediaContentOfType(&content, media_type)) { |
| 2676 | return &content; |
| 2677 | } |
| 2678 | } |
| 2679 | return nullptr; |
| 2680 | } |
| 2681 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2682 | ContentInfo* GetFirstAudioContent(ContentInfos* contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2683 | return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO); |
| 2684 | } |
| 2685 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2686 | ContentInfo* GetFirstVideoContent(ContentInfos* contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2687 | return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO); |
| 2688 | } |
| 2689 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2690 | ContentInfo* GetFirstDataContent(ContentInfos* contents) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2691 | return GetFirstMediaContent(contents, MEDIA_TYPE_DATA); |
| 2692 | } |
| 2693 | |
Steve Anton | ad7bffc | 2018-01-22 10:21:56 -0800 | [diff] [blame] | 2694 | ContentInfo* GetFirstMediaContent(SessionDescription* sdesc, |
| 2695 | MediaType media_type) { |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2696 | if (sdesc == nullptr) { |
| 2697 | return nullptr; |
| 2698 | } |
| 2699 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 2700 | return GetFirstMediaContent(&sdesc->contents(), media_type); |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) { |
| 2704 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO); |
| 2705 | } |
| 2706 | |
| 2707 | ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) { |
| 2708 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO); |
| 2709 | } |
| 2710 | |
| 2711 | ContentInfo* GetFirstDataContent(SessionDescription* sdesc) { |
| 2712 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA); |
| 2713 | } |
| 2714 | |
| 2715 | MediaContentDescription* GetFirstMediaContentDescription( |
| 2716 | SessionDescription* sdesc, |
| 2717 | MediaType media_type) { |
| 2718 | ContentInfo* content = GetFirstMediaContent(sdesc, media_type); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 2719 | return (content ? content->media_description() : nullptr); |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | AudioContentDescription* GetFirstAudioContentDescription( |
| 2723 | SessionDescription* sdesc) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2724 | return static_cast<AudioContentDescription*>( |
| 2725 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO)); |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2726 | } |
| 2727 | |
| 2728 | VideoContentDescription* GetFirstVideoContentDescription( |
| 2729 | SessionDescription* sdesc) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2730 | return static_cast<VideoContentDescription*>( |
| 2731 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | DataContentDescription* GetFirstDataContentDescription( |
| 2735 | SessionDescription* sdesc) { |
Harald Alvestrand | 141c0ad | 2019-05-05 19:00:00 +0000 | [diff] [blame^] | 2736 | return static_cast<DataContentDescription*>( |
| 2737 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); |
Taylor Brandstetter | dc4eb8c | 2016-05-12 08:14:50 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2740 | } // namespace cricket |