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