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