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