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