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