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