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