blob: 4f7fae10778b58a203f2a0775cc453a1948e1cc0 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
deadbeef67cf2c12016-04-13 10:07:16 -070013#include <algorithm> // For std::find_if, std::sort.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <functional>
15#include <map>
kwiberg31022942016-03-11 14:18:21 -080016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <set>
deadbeef67cf2c12016-04-13 10:07:16 -070018#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <utility>
20
Steve Anton5c72e712018-12-10 14:25:30 -080021#include "absl/memory/memory.h"
Niels Möller2edab4c2018-10-22 09:48:08 +020022#include "absl/strings/match.h"
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020023#include "absl/types/optional.h"
Patrik Höglund7aee3d52017-11-15 13:15:17 +010024#include "api/cryptoparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "media/base/h264_profile_level_id.h"
26#include "media/base/mediaconstants.h"
27#include "p2p/base/p2pconstants.h"
28#include "pc/channelmanager.h"
Steve Anton1d03a752017-11-27 14:30:09 -080029#include "pc/rtpmediautils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "pc/srtpfilter.h"
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080031#include "pc/unique_id_generator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/checks.h"
33#include "rtc_base/helpers.h"
34#include "rtc_base/logging.h"
Artem Titova76af0c2018-07-23 17:38:12 +020035#include "rtc_base/third_party/base64/base64.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37namespace {
Steve Anton1d03a752017-11-27 14:30:09 -080038
39using webrtc::RtpTransceiverDirection;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080040using webrtc::UniqueRandomIdGenerator;
Steve Anton1d03a752017-11-27 14:30:09 -080041
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042const char kInline[] = "inline:";
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080043
Benjamin Wrighta54daf12018-10-11 15:33:17 -070044void GetSupportedSdesCryptoSuiteNames(
45 void (*func)(const webrtc::CryptoOptions&, std::vector<int>*),
46 const webrtc::CryptoOptions& crypto_options,
47 std::vector<std::string>* names) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080048 std::vector<int> crypto_suites;
jbauchcb560652016-08-04 05:20:32 -070049 func(crypto_options, &crypto_suites);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080050 for (const auto crypto : crypto_suites) {
51 names->push_back(rtc::SrtpCryptoSuiteToName(crypto));
52 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080053}
terelius8c011e52016-04-26 05:28:11 -070054} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055
56namespace cricket {
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// RTP Profile names
59// http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
60// RFC4585
61const char kMediaProtocolAvpf[] = "RTP/AVPF";
62// RFC5124
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +000063const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
64
deadbeeff3938292015-07-15 12:20:53 -070065// We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
66// but we tolerate "RTP/SAVPF" in offers we receive, for compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067const char kMediaProtocolSavpf[] = "RTP/SAVPF";
68
69const char kMediaProtocolRtpPrefix[] = "RTP/";
70
71const char kMediaProtocolSctp[] = "SCTP";
72const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
lally@webrtc.orgec97c652015-02-24 20:18:48 +000073const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
lally@webrtc.orga7470932015-02-24 20:19:21 +000074const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
deadbeef8b7e9ad2017-05-25 09:38:55 -070076// Note that the below functions support some protocol strings purely for
77// legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names
78// and Interoperability.
79
80static bool IsDtlsRtp(const std::string& protocol) {
81 // Most-likely values first.
82 return protocol == "UDP/TLS/RTP/SAVPF" || protocol == "TCP/TLS/RTP/SAVPF" ||
83 protocol == "UDP/TLS/RTP/SAVP" || protocol == "TCP/TLS/RTP/SAVP";
84}
85
86static bool IsPlainRtp(const std::string& protocol) {
87 // Most-likely values first.
88 return protocol == "RTP/SAVPF" || protocol == "RTP/AVPF" ||
89 protocol == "RTP/SAVP" || protocol == "RTP/AVP";
90}
91
92static bool IsDtlsSctp(const std::string& protocol) {
93 return protocol == kMediaProtocolDtlsSctp ||
94 protocol == kMediaProtocolUdpDtlsSctp ||
95 protocol == kMediaProtocolTcpDtlsSctp;
96}
97
98static bool IsPlainSctp(const std::string& protocol) {
99 return protocol == kMediaProtocolSctp;
100}
101
102static bool IsSctp(const std::string& protocol) {
103 return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
104}
105
Steve Anton1d03a752017-11-27 14:30:09 -0800106static RtpTransceiverDirection NegotiateRtpTransceiverDirection(
107 RtpTransceiverDirection offer,
108 RtpTransceiverDirection wants) {
109 bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer);
110 bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer);
111 bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants);
112 bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants);
113 return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send,
114 offer_send && wants_recv);
ossu075af922016-06-14 03:29:38 -0700115}
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117static bool IsMediaContentOfType(const ContentInfo* content,
118 MediaType media_type) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800119 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 return false;
121 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800122 return content->media_description()->type() == media_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123}
124
Yves Gerey665174f2018-06-19 15:03:05 +0200125static bool CreateCryptoParams(int tag,
126 const std::string& cipher,
Steve Anton3a66edf2018-09-10 12:57:37 -0700127 CryptoParams* crypto_out) {
jbauchcb560652016-08-04 05:20:32 -0700128 int key_len;
129 int salt_len;
Yves Gerey665174f2018-06-19 15:03:05 +0200130 if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher),
131 &key_len, &salt_len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 return false;
133 }
jbauchcb560652016-08-04 05:20:32 -0700134
135 int master_key_len = key_len + salt_len;
136 std::string master_key;
137 if (!rtc::CreateRandomData(master_key_len, &master_key)) {
138 return false;
139 }
140
kwiberg352444f2016-11-28 15:58:53 -0800141 RTC_CHECK_EQ(master_key_len, master_key.size());
jbauchcb560652016-08-04 05:20:32 -0700142 std::string key = rtc::Base64::Encode(master_key);
143
Steve Anton3a66edf2018-09-10 12:57:37 -0700144 crypto_out->tag = tag;
145 crypto_out->cipher_suite = cipher;
146 crypto_out->key_params = kInline;
147 crypto_out->key_params += key;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 return true;
149}
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151static bool AddCryptoParams(const std::string& cipher_suite,
Steve Anton3a66edf2018-09-10 12:57:37 -0700152 CryptoParamsVec* cryptos_out) {
153 int size = static_cast<int>(cryptos_out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
Steve Anton3a66edf2018-09-10 12:57:37 -0700155 cryptos_out->resize(size + 1);
156 return CreateCryptoParams(size, cipher_suite, &cryptos_out->at(size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157}
158
159void AddMediaCryptos(const CryptoParamsVec& cryptos,
160 MediaContentDescription* media) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700161 for (const CryptoParams& crypto : cryptos) {
162 media->AddCrypto(crypto);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 }
164}
165
166bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
167 MediaContentDescription* media) {
168 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700169 for (const std::string& crypto_suite : crypto_suites) {
170 if (!AddCryptoParams(crypto_suite, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 return false;
172 }
173 }
174 AddMediaCryptos(cryptos, media);
175 return true;
176}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
zhihuang1c378ed2017-08-17 14:10:50 -0700178const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800179 if (!content || !content->media_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700180 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800182 return &content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183}
184
185bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
186 const CryptoParams& crypto,
Steve Anton3a66edf2018-09-10 12:57:37 -0700187 CryptoParams* crypto_out) {
188 auto it = std::find_if(
189 cryptos.begin(), cryptos.end(),
190 [&crypto](const CryptoParams& c) { return crypto.Matches(c); });
191 if (it == cryptos.end()) {
192 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700194 *crypto_out = *it;
195 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196}
197
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700198// For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the
199// low overhead.
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700200void GetSupportedAudioSdesCryptoSuites(
201 const webrtc::CryptoOptions& crypto_options,
202 std::vector<int>* crypto_suites) {
203 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700204 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
205 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
206 }
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700207 if (crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher) {
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700208 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32);
209 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800210 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211}
212
deadbeef7914b8c2017-04-21 03:23:33 -0700213void GetSupportedAudioSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700214 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800215 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700216 GetSupportedSdesCryptoSuiteNames(GetSupportedAudioSdesCryptoSuites,
217 crypto_options, crypto_suite_names);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218}
219
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700220void GetSupportedVideoSdesCryptoSuites(
221 const webrtc::CryptoOptions& crypto_options,
222 std::vector<int>* crypto_suites) {
223 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700224 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
225 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
226 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800227 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228}
229
deadbeef7914b8c2017-04-21 03:23:33 -0700230void GetSupportedVideoSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700231 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800232 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700233 GetSupportedSdesCryptoSuiteNames(GetSupportedVideoSdesCryptoSuites,
234 crypto_options, crypto_suite_names);
235}
236
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700237void GetSupportedDataSdesCryptoSuites(
238 const webrtc::CryptoOptions& crypto_options,
239 std::vector<int>* crypto_suites) {
240 if (crypto_options.srtp.enable_gcm_crypto_suites) {
deadbeef7914b8c2017-04-21 03:23:33 -0700241 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
242 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
243 }
244 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
245}
246
247void GetSupportedDataSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700248 const webrtc::CryptoOptions& crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -0700249 std::vector<std::string>* crypto_suite_names) {
250 GetSupportedSdesCryptoSuiteNames(GetSupportedDataSdesCryptoSuites,
251 crypto_options, crypto_suite_names);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800252}
253
jbauchcb560652016-08-04 05:20:32 -0700254// Support any GCM cipher (if enabled through options). For video support only
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700255// 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated (if enabled) unless
256// bundle is enabled because it is low overhead.
jbauchcb560652016-08-04 05:20:32 -0700257// Pick the crypto in the list that is supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258static bool SelectCrypto(const MediaContentDescription* offer,
259 bool bundle,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700260 const webrtc::CryptoOptions& crypto_options,
Steve Anton3a66edf2018-09-10 12:57:37 -0700261 CryptoParams* crypto_out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
263 const CryptoParamsVec& cryptos = offer->cryptos();
264
Steve Anton3a66edf2018-09-10 12:57:37 -0700265 for (const CryptoParams& crypto : cryptos) {
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700266 if ((crypto_options.srtp.enable_gcm_crypto_suites &&
Steve Anton3a66edf2018-09-10 12:57:37 -0700267 rtc::IsGcmCryptoSuiteName(crypto.cipher_suite)) ||
268 rtc::CS_AES_CM_128_HMAC_SHA1_80 == crypto.cipher_suite ||
269 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == crypto.cipher_suite && audio &&
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700270 !bundle && crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher)) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700271 return CreateCryptoParams(crypto.tag, crypto.cipher_suite, crypto_out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 }
273 }
274 return false;
275}
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277// Finds all StreamParams of all media types and attach them to stream_params.
Steve Anton5c72e712018-12-10 14:25:30 -0800278static StreamParamsVec GetCurrentStreamParams(
279 const std::vector<const ContentInfo*>& active_local_contents) {
280 StreamParamsVec stream_params;
281 for (const ContentInfo* content : active_local_contents) {
282 for (const StreamParams& params : content->media_description()->streams()) {
283 stream_params.push_back(params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 }
285 }
Steve Anton5c72e712018-12-10 14:25:30 -0800286 return stream_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287}
288
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000289// Filters the data codecs for the data channel type.
290void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
291 // Filter RTP codec for SCTP and vice versa.
solenberg9fa49752016-10-08 13:02:44 -0700292 const char* codec_name =
293 sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName;
Steve Anton3a66edf2018-09-10 12:57:37 -0700294 codecs->erase(std::remove_if(codecs->begin(), codecs->end(),
295 [&codec_name](const DataCodec& codec) {
Niels Möller039743e2018-10-23 10:07:25 +0200296 return absl::EqualsIgnoreCase(codec.name,
297 codec_name);
Steve Anton3a66edf2018-09-10 12:57:37 -0700298 }),
299 codecs->end());
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000300}
301
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302template <typename IdStruct>
303class UsedIds {
304 public:
305 UsedIds(int min_allowed_id, int max_allowed_id)
306 : min_allowed_id_(min_allowed_id),
307 max_allowed_id_(max_allowed_id),
Yves Gerey665174f2018-06-19 15:03:05 +0200308 next_id_(max_allowed_id) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309
310 // Loops through all Id in |ids| and changes its id if it is
311 // already in use by another IdStruct. Call this methods with all Id
312 // in a session description to make sure no duplicate ids exists.
313 // Note that typename Id must be a type of IdStruct.
314 template <typename Id>
315 void FindAndSetIdUsed(std::vector<Id>* ids) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700316 for (const Id& id : *ids) {
317 FindAndSetIdUsed(&id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 }
319 }
320
321 // Finds and sets an unused id if the |idstruct| id is already in use.
322 void FindAndSetIdUsed(IdStruct* idstruct) {
323 const int original_id = idstruct->id;
324 int new_id = idstruct->id;
325
326 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
327 // If the original id is not in range - this is an id that can't be
328 // dynamically changed.
329 return;
330 }
331
332 if (IsIdUsed(original_id)) {
333 new_id = FindUnusedId();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100334 RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from "
335 << original_id << " to " << new_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 idstruct->id = new_id;
337 }
338 SetIdUsed(new_id);
339 }
340
341 private:
342 // Returns the first unused id in reverse order.
343 // This hopefully reduce the risk of more collisions. We want to change the
344 // default ids as little as possible.
345 int FindUnusedId() {
346 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
347 --next_id_;
348 }
nisseede5da42017-01-12 05:15:36 -0800349 RTC_DCHECK(next_id_ >= min_allowed_id_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 return next_id_;
351 }
352
Yves Gerey665174f2018-06-19 15:03:05 +0200353 bool IsIdUsed(int new_id) { return id_set_.find(new_id) != id_set_.end(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354
Yves Gerey665174f2018-06-19 15:03:05 +0200355 void SetIdUsed(int new_id) { id_set_.insert(new_id); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356
357 const int min_allowed_id_;
358 const int max_allowed_id_;
359 int next_id_;
360 std::set<int> id_set_;
361};
362
363// Helper class used for finding duplicate RTP payload types among audio, video
364// and data codecs. When bundle is used the payload types may not collide.
365class UsedPayloadTypes : public UsedIds<Codec> {
366 public:
367 UsedPayloadTypes()
Yves Gerey665174f2018-06-19 15:03:05 +0200368 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369
370 private:
371 static const int kDynamicPayloadTypeMin = 96;
372 static const int kDynamicPayloadTypeMax = 127;
373};
374
375// Helper class used for finding duplicate RTP Header extension ids among
Johannes Kron07ba2b92018-09-26 13:33:35 +0200376// audio and video extensions. Only applies to one-byte header extensions at the
377// moment. ids > 14 will always be reported as available.
378// TODO(kron): This class needs to be refactored when we start to send two-byte
379// header extensions.
isheriff6f8d6862016-05-26 11:24:55 -0700380class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 public:
382 UsedRtpHeaderExtensionIds()
Johannes Kron07ba2b92018-09-26 13:33:35 +0200383 : UsedIds<webrtc::RtpExtension>(
384 webrtc::RtpExtension::kMinId,
385 webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386
387 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388};
389
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800390static StreamParams CreateStreamParamsForNewSenderWithSsrcs(
391 const SenderOptions& sender,
392 const std::string& rtcp_cname,
393 const StreamParamsVec& current_streams,
394 bool include_rtx_streams,
395 bool include_flexfec_stream) {
396 StreamParams result;
397 result.id = sender.track_id;
398
399 std::vector<uint32_t> known_ssrcs;
400 for (const StreamParams& params : current_streams) {
401 for (uint32_t ssrc : params.ssrcs) {
402 known_ssrcs.push_back(ssrc);
403 }
404 }
405
406 UniqueRandomIdGenerator ssrc_generator(known_ssrcs);
407 // We need to keep |primary_ssrcs| separate from |result.ssrcs| because
408 // iterators are invalidated when rtx and flexfec ssrcs are added to the list.
409 std::vector<uint32_t> primary_ssrcs;
410 for (int i = 0; i < sender.num_sim_layers; ++i) {
411 primary_ssrcs.push_back(ssrc_generator());
412 }
413 result.ssrcs = primary_ssrcs;
414
415 if (sender.num_sim_layers > 1) {
416 SsrcGroup group(kSimSsrcGroupSemantics, result.ssrcs);
417 result.ssrc_groups.push_back(group);
418 }
419 // Generate an RTX ssrc for every ssrc in the group.
420 if (include_rtx_streams) {
421 for (uint32_t ssrc : primary_ssrcs) {
422 result.AddFidSsrc(ssrc, ssrc_generator());
423 }
424 }
425 // Generate extra ssrc for include_flexfec_stream case.
426 if (include_flexfec_stream) {
427 // TODO(brandtr): Update when we support multistream protection.
428 if (result.ssrcs.size() == 1) {
429 for (uint32_t ssrc : primary_ssrcs) {
430 result.AddFecFrSsrc(ssrc, ssrc_generator());
431 }
432 } else if (!result.ssrcs.empty()) {
433 RTC_LOG(LS_WARNING)
434 << "Our FlexFEC implementation only supports protecting "
435 "a single media streams. This session has multiple "
436 "media streams however, so no FlexFEC SSRC will be generated.";
437 }
438 }
439 result.cname = rtcp_cname;
440 result.set_stream_ids(sender.stream_ids);
441
442 return result;
443}
444
445static bool ValidateSimulcastLayers(
446 const std::vector<RidDescription>& rids,
447 const SimulcastLayerList& simulcast_layers) {
448 std::vector<SimulcastLayer> all_layers = simulcast_layers.GetAllLayers();
449 return std::all_of(all_layers.begin(), all_layers.end(),
450 [&rids](const SimulcastLayer& layer) {
451 return std::find_if(rids.begin(), rids.end(),
452 [&layer](const RidDescription& rid) {
453 return rid.rid == layer.rid;
454 }) != rids.end();
455 });
456}
457
458static StreamParams CreateStreamParamsForNewSenderWithRids(
459 const SenderOptions& sender,
460 const std::string& rtcp_cname) {
461 RTC_DCHECK(!sender.rids.empty());
462 RTC_DCHECK_EQ(sender.num_sim_layers, 0)
463 << "RIDs are the compliant way to indicate simulcast.";
464 RTC_DCHECK(ValidateSimulcastLayers(sender.rids, sender.simulcast_layers));
465 StreamParams result;
466 result.id = sender.track_id;
467 result.cname = rtcp_cname;
468 result.set_stream_ids(sender.stream_ids);
469
470 // More than one rid should be signaled.
471 if (sender.rids.size() > 1) {
472 result.set_rids(sender.rids);
473 }
474
475 return result;
476}
477
478// Adds SimulcastDescription if indicated by the media description options.
479// MediaContentDescription should already be set up with the send rids.
480static void AddSimulcastToMediaDescription(
481 const MediaDescriptionOptions& media_description_options,
482 MediaContentDescription* description) {
483 RTC_DCHECK(description);
484
485 // Check if we are using RIDs in this scenario.
486 if (std::all_of(
487 description->streams().begin(), description->streams().end(),
488 [](const StreamParams& params) { return !params.has_rids(); })) {
489 return;
490 }
491
492 RTC_DCHECK_EQ(1, description->streams().size())
493 << "RIDs are only supported in Unified Plan semantics.";
494 RTC_DCHECK_EQ(1, media_description_options.sender_options.size());
495 RTC_DCHECK(description->type() == MediaType::MEDIA_TYPE_AUDIO ||
496 description->type() == MediaType::MEDIA_TYPE_VIDEO);
497
498 // One RID or less indicates that simulcast is not needed.
499 if (description->streams()[0].rids().size() <= 1) {
500 return;
501 }
502
503 RTC_DCHECK(!media_description_options.receive_rids.empty());
504 RTC_DCHECK(ValidateSimulcastLayers(
505 media_description_options.receive_rids,
506 media_description_options.receive_simulcast_layers));
507 StreamParams receive_stream;
508 receive_stream.set_rids(media_description_options.receive_rids);
509 description->set_receive_stream(receive_stream);
510
511 SimulcastDescription simulcast;
512 simulcast.send_layers() =
513 media_description_options.sender_options[0].simulcast_layers;
514 simulcast.receive_layers() =
515 media_description_options.receive_simulcast_layers;
516 description->set_simulcast_description(simulcast);
517}
518
zhihuang1c378ed2017-08-17 14:10:50 -0700519// Adds a StreamParams for each SenderOptions in |sender_options| to
520// content_description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521// |current_params| - All currently known StreamParams of any media type.
522template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700523static bool AddStreamParams(
524 const std::vector<SenderOptions>& sender_options,
525 const std::string& rtcp_cname,
526 StreamParamsVec* current_streams,
527 MediaContentDescriptionImpl<C>* content_description) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700528 // SCTP streams are not negotiated using SDP/ContentDescriptions.
deadbeef8b7e9ad2017-05-25 09:38:55 -0700529 if (IsSctp(content_description->protocol())) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700530 return true;
531 }
532
Noah Richards2e7a0982015-05-18 14:02:54 -0700533 const bool include_rtx_streams =
534 ContainsRtxCodec(content_description->codecs());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
brandtr03d5fb12016-11-22 03:37:59 -0800536 const bool include_flexfec_stream =
537 ContainsFlexfecCodec(content_description->codecs());
538
zhihuang1c378ed2017-08-17 14:10:50 -0700539 for (const SenderOptions& sender : sender_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 // groupid is empty for StreamParams generated using
541 // MediaSessionDescriptionFactory.
zhihuang1c378ed2017-08-17 14:10:50 -0700542 StreamParams* param =
543 GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000544 if (!param) {
zhihuang1c378ed2017-08-17 14:10:50 -0700545 // This is a new sender.
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800546 StreamParams stream_param =
547 sender.rids.empty()
548 ?
549 // Signal SSRCs and legacy simulcast (if requested).
550 CreateStreamParamsForNewSenderWithSsrcs(
551 sender, rtcp_cname, *current_streams, include_rtx_streams,
552 include_flexfec_stream)
553 :
554 // Signal RIDs and spec-compliant simulcast (if requested).
555 CreateStreamParamsForNewSenderWithRids(sender, rtcp_cname);
556
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 content_description->AddStream(stream_param);
558
559 // Store the new StreamParams in current_streams.
560 // This is necessary so that we can use the CNAME for other media types.
561 current_streams->push_back(stream_param);
562 } else {
deadbeef2f425aa2017-04-14 10:41:32 -0700563 // Use existing generated SSRCs/groups, but update the sync_label if
564 // necessary. This may be needed if a MediaStreamTrack was moved from one
565 // MediaStream to another.
Seth Hampson845e8782018-03-02 11:34:10 -0800566 param->set_stream_ids(sender.stream_ids);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000567 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 }
569 }
570 return true;
571}
572
573// Updates the transport infos of the |sdesc| according to the given
574// |bundle_group|. The transport infos of the content names within the
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800575// |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
576// first content within the |bundle_group|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
578 SessionDescription* sdesc) {
579 // The bundle should not be empty.
580 if (!sdesc || !bundle_group.FirstContentName()) {
581 return false;
582 }
583
584 // We should definitely have a transport for the first content.
jbauch083b73f2015-07-16 02:46:32 -0700585 const std::string& selected_content_name = *bundle_group.FirstContentName();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 const TransportInfo* selected_transport_info =
587 sdesc->GetTransportInfoByName(selected_content_name);
588 if (!selected_transport_info) {
589 return false;
590 }
591
592 // Set the other contents to use the same ICE credentials.
jbauch083b73f2015-07-16 02:46:32 -0700593 const std::string& selected_ufrag =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 selected_transport_info->description.ice_ufrag;
jbauch083b73f2015-07-16 02:46:32 -0700595 const std::string& selected_pwd =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 selected_transport_info->description.ice_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800597 ConnectionRole selected_connection_role =
598 selected_transport_info->description.connection_role;
Steve Anton3a66edf2018-09-10 12:57:37 -0700599 for (TransportInfo& transport_info : sdesc->transport_infos()) {
600 if (bundle_group.HasContentName(transport_info.content_name) &&
601 transport_info.content_name != selected_content_name) {
602 transport_info.description.ice_ufrag = selected_ufrag;
603 transport_info.description.ice_pwd = selected_pwd;
604 transport_info.description.connection_role = selected_connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
606 }
607 return true;
608}
609
610// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
611// sets it to |cryptos|.
612static bool GetCryptosByName(const SessionDescription* sdesc,
613 const std::string& content_name,
614 CryptoParamsVec* cryptos) {
615 if (!sdesc || !cryptos) {
616 return false;
617 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 const ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800619 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 return false;
621 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800622 *cryptos = content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 return true;
624}
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
627// which are not available in |filter|.
628static void PruneCryptos(const CryptoParamsVec& filter,
629 CryptoParamsVec* target_cryptos) {
630 if (!target_cryptos) {
631 return;
632 }
tzik21995802018-04-26 17:38:28 +0900633
634 target_cryptos->erase(
635 std::remove_if(target_cryptos->begin(), target_cryptos->end(),
636 // Returns true if the |crypto|'s cipher_suite is not
637 // found in |filter|.
638 [&filter](const CryptoParams& crypto) {
639 for (const CryptoParams& entry : filter) {
640 if (entry.cipher_suite == crypto.cipher_suite)
641 return false;
642 }
643 return true;
644 }),
645 target_cryptos->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646}
647
Steve Antonfa2260d2017-12-28 16:38:23 -0800648bool IsRtpProtocol(const std::string& protocol) {
deadbeefb5cb19b2015-11-23 16:39:12 -0800649 return protocol.empty() ||
650 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
651}
652
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653static bool IsRtpContent(SessionDescription* sdesc,
654 const std::string& content_name) {
655 bool is_rtp = false;
656 ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800657 if (content && content->media_description()) {
658 is_rtp = IsRtpProtocol(content->media_description()->protocol());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 }
660 return is_rtp;
661}
662
663// Updates the crypto parameters of the |sdesc| according to the given
664// |bundle_group|. The crypto parameters of all the contents within the
665// |bundle_group| should be updated to use the common subset of the
666// available cryptos.
667static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
668 SessionDescription* sdesc) {
669 // The bundle should not be empty.
670 if (!sdesc || !bundle_group.FirstContentName()) {
671 return false;
672 }
673
wu@webrtc.org78187522013-10-07 23:32:02 +0000674 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 // Get the common cryptos.
676 const ContentNames& content_names = bundle_group.content_names();
677 CryptoParamsVec common_cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700678 bool first = true;
679 for (const std::string& content_name : content_names) {
680 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 continue;
682 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000683 // The common cryptos are needed if any of the content does not have DTLS
684 // enabled.
Steve Anton3a66edf2018-09-10 12:57:37 -0700685 if (!sdesc->GetTransportInfoByName(content_name)->description.secure()) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000686 common_cryptos_needed = true;
687 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700688 if (first) {
689 first = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 // Initial the common_cryptos with the first content in the bundle group.
Steve Anton3a66edf2018-09-10 12:57:37 -0700691 if (!GetCryptosByName(sdesc, content_name, &common_cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 return false;
693 }
694 if (common_cryptos.empty()) {
695 // If there's no crypto params, we should just return.
696 return true;
697 }
698 } else {
699 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700700 if (!GetCryptosByName(sdesc, content_name, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 return false;
702 }
703 PruneCryptos(cryptos, &common_cryptos);
704 }
705 }
706
wu@webrtc.org78187522013-10-07 23:32:02 +0000707 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 return false;
709 }
710
711 // Update to use the common cryptos.
Steve Anton3a66edf2018-09-10 12:57:37 -0700712 for (const std::string& content_name : content_names) {
713 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 continue;
715 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700716 ContentInfo* content = sdesc->GetContentByName(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 if (IsMediaContent(content)) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800718 MediaContentDescription* media_desc = content->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 if (!media_desc) {
720 return false;
721 }
722 media_desc->set_cryptos(common_cryptos);
723 }
724 }
725 return true;
726}
727
Steve Anton5c72e712018-12-10 14:25:30 -0800728static std::vector<const ContentInfo*> GetActiveContents(
729 const SessionDescription& description,
730 const MediaSessionOptions& session_options) {
731 std::vector<const ContentInfo*> active_contents;
732 for (size_t i = 0; i < description.contents().size(); ++i) {
733 RTC_DCHECK_LT(i, session_options.media_description_options.size());
734 const ContentInfo& content = description.contents()[i];
735 const MediaDescriptionOptions& media_options =
736 session_options.media_description_options[i];
737 if (!content.rejected && !media_options.stopped &&
738 content.name == media_options.mid) {
739 active_contents.push_back(&content);
740 }
741 }
742 return active_contents;
743}
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745template <class C>
746static bool ContainsRtxCodec(const std::vector<C>& codecs) {
brandtr03d5fb12016-11-22 03:37:59 -0800747 for (const auto& codec : codecs) {
748 if (IsRtxCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 return true;
750 }
751 }
752 return false;
753}
754
755template <class C>
756static bool IsRtxCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200757 return absl::EqualsIgnoreCase(codec.name, kRtxCodecName);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758}
759
brandtr03d5fb12016-11-22 03:37:59 -0800760template <class C>
761static bool ContainsFlexfecCodec(const std::vector<C>& codecs) {
762 for (const auto& codec : codecs) {
763 if (IsFlexfecCodec(codec)) {
764 return true;
765 }
766 }
767 return false;
768}
769
770template <class C>
771static bool IsFlexfecCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200772 return absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName);
brandtr03d5fb12016-11-22 03:37:59 -0800773}
774
zhihuang1c378ed2017-08-17 14:10:50 -0700775// Create a media content to be offered for the given |sender_options|,
776// according to the given options.rtcp_mux, session_options.is_muc, codecs,
777// secure_transport, crypto, and current_streams. If we don't currently have
778// crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is
779// created (according to crypto_suites). The created content is added to the
780// offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781template <class C>
782static bool CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800783 const MediaDescriptionOptions& media_description_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700784 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000786 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 const CryptoParamsVec* current_cryptos,
788 const std::vector<std::string>& crypto_suites,
789 const RtpHeaderExtensions& rtp_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 StreamParamsVec* current_streams,
791 MediaContentDescriptionImpl<C>* offer) {
792 offer->AddCodecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
zhihuang1c378ed2017-08-17 14:10:50 -0700794 offer->set_rtcp_mux(session_options.rtcp_mux_enabled);
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700795 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
796 offer->set_rtcp_reduced_size(true);
797 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 offer->set_rtp_header_extensions(rtp_extensions);
799
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800800 if (!AddStreamParams(media_description_options.sender_options,
801 session_options.rtcp_cname, current_streams, offer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 return false;
803 }
804
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800805 AddSimulcastToMediaDescription(media_description_options, offer);
806
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 if (secure_policy != SEC_DISABLED) {
808 if (current_cryptos) {
809 AddMediaCryptos(*current_cryptos, offer);
810 }
811 if (offer->cryptos().empty()) {
812 if (!CreateMediaCryptos(crypto_suites, offer)) {
813 return false;
814 }
815 }
816 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817
deadbeef7af91dd2016-12-13 11:29:11 -0800818 if (secure_policy == SEC_REQUIRED && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 return false;
820 }
821 return true;
822}
823
824template <class C>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000825static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
magjedb05fa242016-11-11 04:00:16 -0800826 const int codec1_id,
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000827 const std::vector<C>& codecs2,
magjedb05fa242016-11-11 04:00:16 -0800828 const int codec2_id) {
829 const C* codec1 = FindCodecById(codecs1, codec1_id);
830 const C* codec2 = FindCodecById(codecs2, codec2_id);
831 return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2);
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000832}
833
834template <class C>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835static void NegotiateCodecs(const std::vector<C>& local_codecs,
836 const std::vector<C>& offered_codecs,
837 std::vector<C>* negotiated_codecs) {
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800838 for (const C& ours : local_codecs) {
839 C theirs;
deadbeef67cf2c12016-04-13 10:07:16 -0700840 // Note that we intentionally only find one matching codec for each of our
841 // local codecs, in case the remote offer contains duplicate codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800842 if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) {
843 C negotiated = ours;
844 negotiated.IntersectFeedbackParams(theirs);
845 if (IsRtxCodec(negotiated)) {
magjedb05fa242016-11-11 04:00:16 -0800846 const auto apt_it =
847 theirs.params.find(kCodecParamAssociatedPayloadType);
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800848 // FindMatchingCodec shouldn't return something with no apt value.
magjedb05fa242016-11-11 04:00:16 -0800849 RTC_DCHECK(apt_it != theirs.params.end());
850 negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 }
Niels Möller039743e2018-10-23 10:07:25 +0200852 if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) {
magjedf823ede2016-11-12 09:53:04 -0800853 webrtc::H264::GenerateProfileLevelIdForAnswer(
854 ours.params, theirs.params, &negotiated.params);
855 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800856 negotiated.id = theirs.id;
ossu075af922016-06-14 03:29:38 -0700857 negotiated.name = theirs.name;
magjedb05fa242016-11-11 04:00:16 -0800858 negotiated_codecs->push_back(std::move(negotiated));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860 }
deadbeef67cf2c12016-04-13 10:07:16 -0700861 // RFC3264: Although the answerer MAY list the formats in their desired
862 // order of preference, it is RECOMMENDED that unless there is a
863 // specific reason, the answerer list formats in the same relative order
864 // they were present in the offer.
865 std::unordered_map<int, int> payload_type_preferences;
866 int preference = static_cast<int>(offered_codecs.size() + 1);
867 for (const C& codec : offered_codecs) {
868 payload_type_preferences[codec.id] = preference--;
869 }
870 std::sort(negotiated_codecs->begin(), negotiated_codecs->end(),
871 [&payload_type_preferences](const C& a, const C& b) {
872 return payload_type_preferences[a.id] >
873 payload_type_preferences[b.id];
874 });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875}
876
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800877// Finds a codec in |codecs2| that matches |codec_to_match|, which is
878// a member of |codecs1|. If |codec_to_match| is an RTX codec, both
879// the codecs themselves and their associated codecs must match.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880template <class C>
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800881static bool FindMatchingCodec(const std::vector<C>& codecs1,
882 const std::vector<C>& codecs2,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 const C& codec_to_match,
884 C* found_codec) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -0700885 // |codec_to_match| should be a member of |codecs1|, in order to look up RTX
886 // codecs' associated codecs correctly. If not, that's a programming error.
887 RTC_DCHECK(std::find_if(codecs1.begin(), codecs1.end(),
888 [&codec_to_match](const C& codec) {
889 return &codec == &codec_to_match;
890 }) != codecs1.end());
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800891 for (const C& potential_match : codecs2) {
892 if (potential_match.Matches(codec_to_match)) {
893 if (IsRtxCodec(codec_to_match)) {
magjedb05fa242016-11-11 04:00:16 -0800894 int apt_value_1 = 0;
895 int apt_value_2 = 0;
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800896 if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType,
897 &apt_value_1) ||
898 !potential_match.GetParam(kCodecParamAssociatedPayloadType,
899 &apt_value_2)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100900 RTC_LOG(LS_WARNING) << "RTX missing associated payload type.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800901 continue;
902 }
903 if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2,
904 apt_value_2)) {
905 continue;
906 }
907 }
908 if (found_codec) {
909 *found_codec = potential_match;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
911 return true;
912 }
913 }
914 return false;
915}
916
zhihuang1c378ed2017-08-17 14:10:50 -0700917// Find the codec in |codec_list| that |rtx_codec| is associated with.
918template <class C>
919static const C* GetAssociatedCodec(const std::vector<C>& codec_list,
920 const C& rtx_codec) {
921 std::string associated_pt_str;
922 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
923 &associated_pt_str)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100924 RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
925 << " is missing an associated payload type.";
zhihuang1c378ed2017-08-17 14:10:50 -0700926 return nullptr;
927 }
928
929 int associated_pt;
930 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100931 RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
932 << " of RTX codec " << rtx_codec.name
933 << " to an integer.";
zhihuang1c378ed2017-08-17 14:10:50 -0700934 return nullptr;
935 }
936
937 // Find the associated reference codec for the reference RTX codec.
938 const C* associated_codec = FindCodecById(codec_list, associated_pt);
939 if (!associated_codec) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100940 RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
941 << associated_pt << " for RTX codec " << rtx_codec.name
942 << ".";
zhihuang1c378ed2017-08-17 14:10:50 -0700943 }
944 return associated_codec;
945}
946
947// Adds all codecs from |reference_codecs| to |offered_codecs| that don't
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948// already exist in |offered_codecs| and ensure the payload types don't
949// collide.
950template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700951static void MergeCodecs(const std::vector<C>& reference_codecs,
952 std::vector<C>* offered_codecs,
953 UsedPayloadTypes* used_pltypes) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 // Add all new codecs that are not RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800955 for (const C& reference_codec : reference_codecs) {
956 if (!IsRtxCodec(reference_codec) &&
957 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
958 reference_codec, nullptr)) {
959 C codec = reference_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 used_pltypes->FindAndSetIdUsed(&codec);
961 offered_codecs->push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 }
963 }
964
965 // Add all new RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800966 for (const C& reference_codec : reference_codecs) {
967 if (IsRtxCodec(reference_codec) &&
968 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
969 reference_codec, nullptr)) {
970 C rtx_codec = reference_codec;
olka3c747662017-08-17 06:50:32 -0700971 const C* associated_codec =
zhihuang1c378ed2017-08-17 14:10:50 -0700972 GetAssociatedCodec(reference_codecs, rtx_codec);
olka3c747662017-08-17 06:50:32 -0700973 if (!associated_codec) {
olka3c747662017-08-17 06:50:32 -0700974 continue;
975 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800976 // Find a codec in the offered list that matches the reference codec.
977 // Its payload type may be different than the reference codec.
978 C matching_codec;
979 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
magjedb05fa242016-11-11 04:00:16 -0800980 *associated_codec, &matching_codec)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100981 RTC_LOG(LS_WARNING)
982 << "Couldn't find matching " << associated_codec->name << " codec.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800983 continue;
984 }
985
986 rtx_codec.params[kCodecParamAssociatedPayloadType] =
987 rtc::ToString(matching_codec.id);
988 used_pltypes->FindAndSetIdUsed(&rtx_codec);
989 offered_codecs->push_back(rtx_codec);
990 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 }
992}
993
zhihuang1c378ed2017-08-17 14:10:50 -0700994static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
995 const webrtc::RtpExtension& ext_to_match,
996 webrtc::RtpExtension* found_extension) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700997 auto it =
998 std::find_if(extensions.begin(), extensions.end(),
999 [&ext_to_match](const webrtc::RtpExtension& extension) {
1000 // We assume that all URIs are given in a canonical
1001 // format.
1002 return extension.uri == ext_to_match.uri &&
1003 extension.encrypt == ext_to_match.encrypt;
1004 });
1005 if (it == extensions.end()) {
1006 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001007 }
Steve Anton3a66edf2018-09-10 12:57:37 -07001008 if (found_extension) {
1009 *found_extension = *it;
1010 }
1011 return true;
zhihuang1c378ed2017-08-17 14:10:50 -07001012}
1013
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014static bool FindByUri(const RtpHeaderExtensions& extensions,
isheriff6f8d6862016-05-26 11:24:55 -07001015 const webrtc::RtpExtension& ext_to_match,
1016 webrtc::RtpExtension* found_extension) {
jbauch5869f502017-06-29 12:31:36 -07001017 // We assume that all URIs are given in a canonical format.
1018 const webrtc::RtpExtension* found =
1019 webrtc::RtpExtension::FindHeaderExtensionByUri(extensions,
1020 ext_to_match.uri);
1021 if (!found) {
1022 return false;
1023 }
1024 if (found_extension) {
1025 *found_extension = *found;
1026 }
1027 return true;
1028}
1029
1030static bool FindByUriWithEncryptionPreference(
1031 const RtpHeaderExtensions& extensions,
Yves Gerey665174f2018-06-19 15:03:05 +02001032 const webrtc::RtpExtension& ext_to_match,
1033 bool encryption_preference,
jbauch5869f502017-06-29 12:31:36 -07001034 webrtc::RtpExtension* found_extension) {
1035 const webrtc::RtpExtension* unencrypted_extension = nullptr;
Steve Anton3a66edf2018-09-10 12:57:37 -07001036 for (const webrtc::RtpExtension& extension : extensions) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 // We assume that all URIs are given in a canonical format.
Steve Anton3a66edf2018-09-10 12:57:37 -07001038 if (extension.uri == ext_to_match.uri) {
1039 if (!encryption_preference || extension.encrypt) {
jbauch5869f502017-06-29 12:31:36 -07001040 if (found_extension) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001041 *found_extension = extension;
jbauch5869f502017-06-29 12:31:36 -07001042 }
1043 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 }
Steve Anton3a66edf2018-09-10 12:57:37 -07001045 unencrypted_extension = &extension;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 }
1047 }
jbauch5869f502017-06-29 12:31:36 -07001048 if (unencrypted_extension) {
1049 if (found_extension) {
1050 *found_extension = *unencrypted_extension;
1051 }
1052 return true;
1053 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 return false;
1055}
1056
zhihuang1c378ed2017-08-17 14:10:50 -07001057// Adds all extensions from |reference_extensions| to |offered_extensions| that
1058// don't already exist in |offered_extensions| and ensure the IDs don't
1059// collide. If an extension is added, it's also added to |regular_extensions| or
1060// |encrypted_extensions|, and if the extension is in |regular_extensions| or
1061// |encrypted_extensions|, its ID is marked as used in |used_ids|.
1062// |offered_extensions| is for either audio or video while |regular_extensions|
1063// and |encrypted_extensions| are used for both audio and video. There could be
1064// overlap between audio extensions and video extensions.
1065static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions,
1066 RtpHeaderExtensions* offered_extensions,
1067 RtpHeaderExtensions* regular_extensions,
1068 RtpHeaderExtensions* encrypted_extensions,
1069 UsedRtpHeaderExtensionIds* used_ids) {
olka3c747662017-08-17 06:50:32 -07001070 for (auto reference_extension : reference_extensions) {
zhihuang1c378ed2017-08-17 14:10:50 -07001071 if (!FindByUriAndEncryption(*offered_extensions, reference_extension,
1072 nullptr)) {
olka3c747662017-08-17 06:50:32 -07001073 webrtc::RtpExtension existing;
zhihuang1c378ed2017-08-17 14:10:50 -07001074 if (reference_extension.encrypt) {
1075 if (FindByUriAndEncryption(*encrypted_extensions, reference_extension,
1076 &existing)) {
1077 offered_extensions->push_back(existing);
1078 } else {
1079 used_ids->FindAndSetIdUsed(&reference_extension);
1080 encrypted_extensions->push_back(reference_extension);
1081 offered_extensions->push_back(reference_extension);
1082 }
olka3c747662017-08-17 06:50:32 -07001083 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07001084 if (FindByUriAndEncryption(*regular_extensions, reference_extension,
1085 &existing)) {
1086 offered_extensions->push_back(existing);
1087 } else {
1088 used_ids->FindAndSetIdUsed(&reference_extension);
1089 regular_extensions->push_back(reference_extension);
1090 offered_extensions->push_back(reference_extension);
1091 }
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001092 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 }
1094 }
1095}
1096
jbauch5869f502017-06-29 12:31:36 -07001097static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions,
1098 RtpHeaderExtensions* all_extensions,
1099 UsedRtpHeaderExtensionIds* used_ids) {
1100 RtpHeaderExtensions encrypted_extensions;
1101 for (const webrtc::RtpExtension& extension : *extensions) {
1102 webrtc::RtpExtension existing;
1103 // Don't add encrypted extensions again that were already included in a
1104 // previous offer or regular extensions that are also included as encrypted
1105 // extensions.
1106 if (extension.encrypt ||
1107 !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) ||
1108 (FindByUriWithEncryptionPreference(*extensions, extension, true,
Yves Gerey665174f2018-06-19 15:03:05 +02001109 &existing) &&
1110 existing.encrypt)) {
jbauch5869f502017-06-29 12:31:36 -07001111 continue;
1112 }
1113
1114 if (FindByUri(*all_extensions, extension, &existing)) {
1115 encrypted_extensions.push_back(existing);
1116 } else {
1117 webrtc::RtpExtension encrypted(extension);
1118 encrypted.encrypt = true;
1119 used_ids->FindAndSetIdUsed(&encrypted);
1120 all_extensions->push_back(encrypted);
1121 encrypted_extensions.push_back(encrypted);
1122 }
1123 }
1124 extensions->insert(extensions->end(), encrypted_extensions.begin(),
Yves Gerey665174f2018-06-19 15:03:05 +02001125 encrypted_extensions.end());
jbauch5869f502017-06-29 12:31:36 -07001126}
1127
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128static void NegotiateRtpHeaderExtensions(
1129 const RtpHeaderExtensions& local_extensions,
1130 const RtpHeaderExtensions& offered_extensions,
jbauch5869f502017-06-29 12:31:36 -07001131 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 RtpHeaderExtensions* negotiated_extenstions) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001133 for (const webrtc::RtpExtension& ours : local_extensions) {
isheriff6f8d6862016-05-26 11:24:55 -07001134 webrtc::RtpExtension theirs;
Yves Gerey665174f2018-06-19 15:03:05 +02001135 if (FindByUriWithEncryptionPreference(
Steve Anton3a66edf2018-09-10 12:57:37 -07001136 offered_extensions, ours, enable_encrypted_rtp_header_extensions,
Yves Gerey665174f2018-06-19 15:03:05 +02001137 &theirs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 // We respond with their RTP header extension id.
1139 negotiated_extenstions->push_back(theirs);
1140 }
1141 }
1142}
1143
1144static void StripCNCodecs(AudioCodecs* audio_codecs) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001145 audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(),
1146 [](const AudioCodec& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +02001147 return absl::EqualsIgnoreCase(
1148 codec.name, kComfortNoiseCodecName);
Steve Anton3a66edf2018-09-10 12:57:37 -07001149 }),
1150 audio_codecs->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151}
1152
zhihuang1c378ed2017-08-17 14:10:50 -07001153// Create a media content to be answered for the given |sender_options|
1154// according to the given session_options.rtcp_mux, session_options.streams,
1155// codecs, crypto, and current_streams. If we don't currently have crypto (in
1156// current_cryptos) and it is enabled (in secure_policy), crypto is created
1157// (according to crypto_suites). The codecs, rtcp_mux, and crypto are all
1158// negotiated with the offer. If the negotiation fails, this method returns
1159// false. The created content is added to the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001160template <class C>
1161static bool CreateMediaContentAnswer(
1162 const MediaContentDescriptionImpl<C>* offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001163 const MediaDescriptionOptions& media_description_options,
1164 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001166 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 const CryptoParamsVec* current_cryptos,
1168 const RtpHeaderExtensions& local_rtp_extenstions,
jbauch5869f502017-06-29 12:31:36 -07001169 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 StreamParamsVec* current_streams,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 bool bundle_enabled,
1172 MediaContentDescriptionImpl<C>* answer) {
1173 std::vector<C> negotiated_codecs;
1174 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
1175 answer->AddCodecs(negotiated_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 answer->set_protocol(offer->protocol());
Johannes Kron0854eb62018-10-10 22:33:20 +02001177
Johannes Kron9581bc42018-10-23 10:17:39 +02001178 answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 RtpHeaderExtensions negotiated_rtp_extensions;
Yves Gerey665174f2018-06-19 15:03:05 +02001180 NegotiateRtpHeaderExtensions(
1181 local_rtp_extenstions, offer->rtp_header_extensions(),
1182 enable_encrypted_rtp_header_extensions, &negotiated_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
1184
zhihuang1c378ed2017-08-17 14:10:50 -07001185 answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux());
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -07001186 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
1187 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
1188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189
1190 if (sdes_policy != SEC_DISABLED) {
1191 CryptoParams crypto;
zhihuang1c378ed2017-08-17 14:10:50 -07001192 if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options,
1193 &crypto)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 if (current_cryptos) {
1195 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
1196 }
1197 answer->AddCrypto(crypto);
1198 }
1199 }
1200
deadbeef7af91dd2016-12-13 11:29:11 -08001201 if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 return false;
1203 }
1204
zhihuang1c378ed2017-08-17 14:10:50 -07001205 if (!AddStreamParams(media_description_options.sender_options,
1206 session_options.rtcp_cname, current_streams, answer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 return false; // Something went seriously wrong.
1208 }
1209
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001210 AddSimulcastToMediaDescription(media_description_options, answer);
1211
Steve Anton4e70a722017-11-28 14:57:10 -08001212 answer->set_direction(NegotiateRtpTransceiverDirection(
1213 offer->direction(), media_description_options.direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 return true;
1215}
1216
1217static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001218 const std::string& protocol,
1219 bool secure_transport) {
zhihuangcf5b37c2016-05-05 11:44:35 -07001220 // Since not all applications serialize and deserialize the media protocol,
1221 // we will have to accept |protocol| to be empty.
1222 if (protocol.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 return true;
1224 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001225
zhihuangcf5b37c2016-05-05 11:44:35 -07001226 if (type == MEDIA_TYPE_DATA) {
1227 // Check for SCTP, but also for RTP for RTP-based data channels.
1228 // TODO(pthatcher): Remove RTP once RTP-based data channels are gone.
1229 if (secure_transport) {
1230 // Most likely scenarios first.
1231 return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) ||
1232 IsPlainRtp(protocol);
1233 } else {
1234 return IsPlainSctp(protocol) || IsPlainRtp(protocol);
1235 }
1236 }
1237
1238 // Allow for non-DTLS RTP protocol even when using DTLS because that's what
1239 // JSEP specifies.
1240 if (secure_transport) {
1241 // Most likely scenarios first.
1242 return IsDtlsRtp(protocol) || IsPlainRtp(protocol);
1243 } else {
1244 return IsPlainRtp(protocol);
1245 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246}
1247
1248static void SetMediaProtocol(bool secure_transport,
1249 MediaContentDescription* desc) {
deadbeeff3938292015-07-15 12:20:53 -07001250 if (!desc->cryptos().empty())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 desc->set_protocol(kMediaProtocolSavpf);
deadbeeff3938292015-07-15 12:20:53 -07001252 else if (secure_transport)
1253 desc->set_protocol(kMediaProtocolDtlsSavpf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 else
1255 desc->set_protocol(kMediaProtocolAvpf);
1256}
1257
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001258// Gets the TransportInfo of the given |content_name| from the
1259// |current_description|. If doesn't exist, returns a new one.
1260static const TransportDescription* GetTransportDescription(
1261 const std::string& content_name,
1262 const SessionDescription* current_description) {
1263 const TransportDescription* desc = NULL;
1264 if (current_description) {
1265 const TransportInfo* info =
1266 current_description->GetTransportInfoByName(content_name);
1267 if (info) {
1268 desc = &info->description;
1269 }
1270 }
1271 return desc;
1272}
1273
1274// Gets the current DTLS state from the transport description.
zhihuang1c378ed2017-08-17 14:10:50 -07001275static bool IsDtlsActive(const ContentInfo* content,
1276 const SessionDescription* current_description) {
1277 if (!content) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001278 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001279 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001280
zhihuang1c378ed2017-08-17 14:10:50 -07001281 size_t msection_index = content - &current_description->contents()[0];
1282
1283 if (current_description->transport_infos().size() <= msection_index) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001284 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001285 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001286
zhihuang1c378ed2017-08-17 14:10:50 -07001287 return current_description->transport_infos()[msection_index]
1288 .description.secure();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001289}
1290
Steve Anton8ffb9c32017-08-31 15:45:38 -07001291void MediaDescriptionOptions::AddAudioSender(
1292 const std::string& track_id,
1293 const std::vector<std::string>& stream_ids) {
zhihuang1c378ed2017-08-17 14:10:50 -07001294 RTC_DCHECK(type == MEDIA_TYPE_AUDIO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001295 AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001296}
1297
Steve Anton8ffb9c32017-08-31 15:45:38 -07001298void MediaDescriptionOptions::AddVideoSender(
1299 const std::string& track_id,
1300 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001301 const std::vector<RidDescription>& rids,
1302 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001303 int num_sim_layers) {
zhihuang1c378ed2017-08-17 14:10:50 -07001304 RTC_DCHECK(type == MEDIA_TYPE_VIDEO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001305 RTC_DCHECK(rids.empty() || num_sim_layers == 0)
1306 << "RIDs are the compliant way to indicate simulcast.";
1307 RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers));
1308 AddSenderInternal(track_id, stream_ids, rids, simulcast_layers,
1309 num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001310}
1311
zhihuang1c378ed2017-08-17 14:10:50 -07001312void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id,
1313 const std::string& stream_id) {
1314 RTC_DCHECK(type == MEDIA_TYPE_DATA);
Steve Anton8ffb9c32017-08-31 15:45:38 -07001315 // TODO(steveanton): Is it the case that RtpDataChannel will never have more
1316 // than one stream?
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001317 AddSenderInternal(track_id, {stream_id}, {}, SimulcastLayerList(), 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318}
1319
Steve Anton8ffb9c32017-08-31 15:45:38 -07001320void MediaDescriptionOptions::AddSenderInternal(
1321 const std::string& track_id,
1322 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001323 const std::vector<RidDescription>& rids,
1324 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001325 int num_sim_layers) {
1326 // TODO(steveanton): Support any number of stream ids.
1327 RTC_CHECK(stream_ids.size() == 1U);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001328 SenderOptions options;
1329 options.track_id = track_id;
1330 options.stream_ids = stream_ids;
1331 options.simulcast_layers = simulcast_layers;
1332 options.rids = rids;
1333 options.num_sim_layers = num_sim_layers;
1334 sender_options.push_back(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
1336
zhihuang1c378ed2017-08-17 14:10:50 -07001337bool MediaSessionOptions::HasMediaDescription(MediaType type) const {
1338 return std::find_if(media_description_options.begin(),
1339 media_description_options.end(),
1340 [type](const MediaDescriptionOptions& t) {
1341 return t.type == type;
1342 }) != media_description_options.end();
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001343}
1344
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1346 const TransportDescriptionFactory* transport_desc_factory)
zhihuang1c378ed2017-08-17 14:10:50 -07001347 : transport_desc_factory_(transport_desc_factory) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348
1349MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1350 ChannelManager* channel_manager,
1351 const TransportDescriptionFactory* transport_desc_factory)
zhihuang1c378ed2017-08-17 14:10:50 -07001352 : transport_desc_factory_(transport_desc_factory) {
ossudedfd282016-06-14 07:12:39 -07001353 channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_);
1354 channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
magjed3cf8ece2016-11-10 03:36:53 -08001356 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1358 channel_manager->GetSupportedDataCodecs(&data_codecs_);
zhihuang1c378ed2017-08-17 14:10:50 -07001359 ComputeAudioCodecsIntersectionAndUnion();
ossu075af922016-06-14 03:29:38 -07001360}
1361
ossudedfd282016-06-14 07:12:39 -07001362const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs()
1363 const {
ossu075af922016-06-14 03:29:38 -07001364 return audio_sendrecv_codecs_;
1365}
1366
1367const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const {
1368 return audio_send_codecs_;
1369}
1370
1371const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const {
1372 return audio_recv_codecs_;
1373}
1374
1375void MediaSessionDescriptionFactory::set_audio_codecs(
Yves Gerey665174f2018-06-19 15:03:05 +02001376 const AudioCodecs& send_codecs,
1377 const AudioCodecs& recv_codecs) {
ossu075af922016-06-14 03:29:38 -07001378 audio_send_codecs_ = send_codecs;
1379 audio_recv_codecs_ = recv_codecs;
zhihuang1c378ed2017-08-17 14:10:50 -07001380 ComputeAudioCodecsIntersectionAndUnion();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381}
1382
Amit Hilbuch77938e62018-12-21 09:23:38 -08001383static void AddUnifiedPlanExtensions(RtpHeaderExtensions* extensions) {
1384 RTC_DCHECK(extensions);
1385 // Unified Plan also offers the MID and RID header extensions.
1386 extensions->push_back(webrtc::RtpExtension(
1387 webrtc::RtpExtension::kMidUri, webrtc::RtpExtension::kMidDefaultId));
1388 extensions->push_back(webrtc::RtpExtension(
1389 webrtc::RtpExtension::kRidUri, webrtc::RtpExtension::kRidDefaultId));
1390 extensions->push_back(
1391 webrtc::RtpExtension(webrtc::RtpExtension::kRepairedRidUri,
1392 webrtc::RtpExtension::kRepairedRidDefaultId));
1393}
1394
1395RtpHeaderExtensions
1396MediaSessionDescriptionFactory::audio_rtp_header_extensions() const {
1397 RtpHeaderExtensions extensions = audio_rtp_extensions_;
1398 if (is_unified_plan_) {
1399 AddUnifiedPlanExtensions(&extensions);
1400 }
1401
1402 return extensions;
1403}
1404
1405RtpHeaderExtensions
1406MediaSessionDescriptionFactory::video_rtp_header_extensions() const {
1407 RtpHeaderExtensions extensions = video_rtp_extensions_;
1408 if (is_unified_plan_) {
1409 AddUnifiedPlanExtensions(&extensions);
1410 }
1411
1412 return extensions;
1413}
1414
Steve Anton6fe1fba2018-12-11 10:15:23 -08001415std::unique_ptr<SessionDescription> MediaSessionDescriptionFactory::CreateOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001416 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 const SessionDescription* current_description) const {
Steve Anton5c72e712018-12-10 14:25:30 -08001418 // Must have options for each existing section.
1419 if (current_description) {
1420 RTC_DCHECK_LE(current_description->contents().size(),
1421 session_options.media_description_options.size());
1422 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001424 IceCredentialsIterator ice_credentials(
1425 session_options.pooled_ice_credentials);
Steve Anton5c72e712018-12-10 14:25:30 -08001426
1427 std::vector<const ContentInfo*> current_active_contents;
1428 if (current_description) {
1429 current_active_contents =
1430 GetActiveContents(*current_description, session_options);
1431 }
1432
1433 StreamParamsVec current_streams =
1434 GetCurrentStreamParams(current_active_contents);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435
zhihuang1c378ed2017-08-17 14:10:50 -07001436 AudioCodecs offer_audio_codecs;
1437 VideoCodecs offer_video_codecs;
1438 DataCodecs offer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001439 GetCodecsForOffer(current_active_contents, &offer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001440 &offer_video_codecs, &offer_data_codecs);
ossu075af922016-06-14 03:29:38 -07001441
zhihuang1c378ed2017-08-17 14:10:50 -07001442 if (!session_options.vad_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443 // If application doesn't want CN codecs in offer.
zhihuang1c378ed2017-08-17 14:10:50 -07001444 StripCNCodecs(&offer_audio_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 }
zhihuang1c378ed2017-08-17 14:10:50 -07001446 FilterDataCodecs(&offer_data_codecs,
1447 session_options.data_channel_type == DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448
1449 RtpHeaderExtensions audio_rtp_extensions;
1450 RtpHeaderExtensions video_rtp_extensions;
Steve Anton8f66ddb2018-12-10 16:08:05 -08001451 GetRtpHdrExtsToOffer(current_active_contents, &audio_rtp_extensions,
1452 &video_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453
Steve Anton5c72e712018-12-10 14:25:30 -08001454 auto offer = absl::make_unique<SessionDescription>();
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001455
zhihuang1c378ed2017-08-17 14:10:50 -07001456 // Iterate through the media description options, matching with existing media
1457 // descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001458 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001459 for (const MediaDescriptionOptions& media_description_options :
1460 session_options.media_description_options) {
1461 const ContentInfo* current_content = nullptr;
1462 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001463 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001464 current_content = &current_description->contents()[msection_index];
Steve Antondcc3c022017-12-22 16:02:54 -08001465 // Media type must match unless this media section is being recycled.
Steve Anton5c72e712018-12-10 14:25:30 -08001466 RTC_DCHECK(current_content->name != media_description_options.mid ||
Steve Antondcc3c022017-12-22 16:02:54 -08001467 IsMediaContentOfType(current_content,
zhihuang1c378ed2017-08-17 14:10:50 -07001468 media_description_options.type));
1469 }
1470 switch (media_description_options.type) {
1471 case MEDIA_TYPE_AUDIO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001472 if (!AddAudioContentForOffer(
1473 media_description_options, session_options, current_content,
1474 current_description, audio_rtp_extensions, offer_audio_codecs,
1475 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001476 return nullptr;
1477 }
1478 break;
1479 case MEDIA_TYPE_VIDEO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001480 if (!AddVideoContentForOffer(
1481 media_description_options, session_options, current_content,
1482 current_description, video_rtp_extensions, offer_video_codecs,
1483 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001484 return nullptr;
1485 }
1486 break;
1487 case MEDIA_TYPE_DATA:
1488 if (!AddDataContentForOffer(media_description_options, session_options,
1489 current_content, current_description,
1490 offer_data_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001491 offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001492 return nullptr;
1493 }
1494 break;
1495 default:
1496 RTC_NOTREACHED();
1497 }
1498 ++msection_index;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 }
1500
1501 // Bundle the contents together, if we've been asked to do so, and update any
1502 // parameters that need to be tweaked for BUNDLE.
zhihuang1c378ed2017-08-17 14:10:50 -07001503 if (session_options.bundle_enabled && offer->contents().size() > 0u) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
zhihuang1c378ed2017-08-17 14:10:50 -07001505 for (const ContentInfo& content : offer->contents()) {
1506 // TODO(deadbeef): There are conditions that make bundling two media
1507 // descriptions together illegal. For example, they use the same payload
1508 // type to represent different codecs, or same IDs for different header
1509 // extensions. We need to detect this and not try to bundle those media
1510 // descriptions together.
1511 offer_bundle.AddContentName(content.name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 }
1513 offer->AddGroup(offer_bundle);
1514 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001515 RTC_LOG(LS_ERROR)
1516 << "CreateOffer failed to UpdateTransportInfoForBundle.";
zhihuang1c378ed2017-08-17 14:10:50 -07001517 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 }
1519 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001520 RTC_LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle.";
zhihuang1c378ed2017-08-17 14:10:50 -07001521 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 }
1523 }
Steve Antone831b8c2018-02-01 12:22:16 -08001524
1525 // The following determines how to signal MSIDs to ensure compatibility with
1526 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001527 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001528 // Be conservative and signal using both a=msid and a=ssrc lines. Unified
1529 // Plan answerers will look at a=msid and Plan B answerers will look at the
1530 // a=ssrc MSID line.
1531 offer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1532 cricket::kMsidSignalingSsrcAttribute);
1533 } else {
1534 // Plan B always signals MSID using a=ssrc lines.
1535 offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1536 }
1537
Johannes Kron89f874e2018-11-12 10:25:48 +01001538 offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed);
1539
Steve Anton6fe1fba2018-12-11 10:15:23 -08001540 return offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541}
1542
Steve Anton6fe1fba2018-12-11 10:15:23 -08001543std::unique_ptr<SessionDescription>
1544MediaSessionDescriptionFactory::CreateAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07001545 const SessionDescription* offer,
1546 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 const SessionDescription* current_description) const {
deadbeefb7892532017-02-22 19:35:18 -08001548 if (!offer) {
1549 return nullptr;
1550 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001551
Steve Anton5c72e712018-12-10 14:25:30 -08001552 // Must have options for exactly as many sections as in the offer.
1553 RTC_DCHECK_EQ(offer->contents().size(),
1554 session_options.media_description_options.size());
1555
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001556 IceCredentialsIterator ice_credentials(
1557 session_options.pooled_ice_credentials);
1558
Steve Anton5c72e712018-12-10 14:25:30 -08001559 std::vector<const ContentInfo*> current_active_contents;
1560 if (current_description) {
1561 current_active_contents =
1562 GetActiveContents(*current_description, session_options);
1563 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564
Steve Anton5c72e712018-12-10 14:25:30 -08001565 StreamParamsVec current_streams =
1566 GetCurrentStreamParams(current_active_contents);
Johannes Kron0854eb62018-10-10 22:33:20 +02001567
zhihuang1c378ed2017-08-17 14:10:50 -07001568 // Get list of all possible codecs that respects existing payload type
1569 // mappings and uses a single payload type space.
1570 //
1571 // Note that these lists may be further filtered for each m= section; this
1572 // step is done just to establish the payload type mappings shared by all
1573 // sections.
1574 AudioCodecs answer_audio_codecs;
1575 VideoCodecs answer_video_codecs;
1576 DataCodecs answer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001577 GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001578 &answer_video_codecs, &answer_data_codecs);
1579
1580 if (!session_options.vad_enabled) {
1581 // If application doesn't want CN codecs in answer.
1582 StripCNCodecs(&answer_audio_codecs);
1583 }
1584 FilterDataCodecs(&answer_data_codecs,
1585 session_options.data_channel_type == DCT_SCTP);
1586
Steve Anton5c72e712018-12-10 14:25:30 -08001587 auto answer = absl::make_unique<SessionDescription>();
1588
1589 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1590 // group in the answer with the appropriate content names.
1591 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1592 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1593 // Transport info shared by the bundle group.
1594 std::unique_ptr<TransportInfo> bundle_transport;
1595
1596 answer->set_extmap_allow_mixed(offer->extmap_allow_mixed());
1597
zhihuang1c378ed2017-08-17 14:10:50 -07001598 // Iterate through the media description options, matching with existing
1599 // media descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001600 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001601 for (const MediaDescriptionOptions& media_description_options :
1602 session_options.media_description_options) {
1603 const ContentInfo* offer_content = &offer->contents()[msection_index];
1604 // Media types and MIDs must match between the remote offer and the
1605 // MediaDescriptionOptions.
1606 RTC_DCHECK(
1607 IsMediaContentOfType(offer_content, media_description_options.type));
1608 RTC_DCHECK(media_description_options.mid == offer_content->name);
1609 const ContentInfo* current_content = nullptr;
1610 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001611 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001612 current_content = &current_description->contents()[msection_index];
deadbeefb7892532017-02-22 19:35:18 -08001613 }
zhihuang1c378ed2017-08-17 14:10:50 -07001614 switch (media_description_options.type) {
1615 case MEDIA_TYPE_AUDIO:
1616 if (!AddAudioContentForAnswer(
1617 media_description_options, session_options, offer_content,
1618 offer, current_content, current_description,
1619 bundle_transport.get(), answer_audio_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001620 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001621 return nullptr;
1622 }
1623 break;
1624 case MEDIA_TYPE_VIDEO:
1625 if (!AddVideoContentForAnswer(
1626 media_description_options, session_options, offer_content,
1627 offer, current_content, current_description,
1628 bundle_transport.get(), answer_video_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001629 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001630 return nullptr;
1631 }
1632 break;
1633 case MEDIA_TYPE_DATA:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001634 if (!AddDataContentForAnswer(
1635 media_description_options, session_options, offer_content,
1636 offer, current_content, current_description,
1637 bundle_transport.get(), answer_data_codecs, &current_streams,
1638 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001639 return nullptr;
1640 }
1641 break;
1642 default:
1643 RTC_NOTREACHED();
1644 }
1645 ++msection_index;
deadbeefb7892532017-02-22 19:35:18 -08001646 // See if we can add the newly generated m= section to the BUNDLE group in
1647 // the answer.
1648 ContentInfo& added = answer->contents().back();
zhihuang1c378ed2017-08-17 14:10:50 -07001649 if (!added.rejected && session_options.bundle_enabled && offer_bundle &&
deadbeefb7892532017-02-22 19:35:18 -08001650 offer_bundle->HasContentName(added.name)) {
1651 answer_bundle.AddContentName(added.name);
1652 bundle_transport.reset(
1653 new TransportInfo(*answer->GetTransportInfoByName(added.name)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 }
1656
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001657 // If a BUNDLE group was offered, put a BUNDLE group in the answer even if
1658 // it's empty. RFC5888 says:
1659 //
1660 // A SIP entity that receives an offer that contains an "a=group" line
1661 // with semantics that are understood MUST return an answer that
1662 // contains an "a=group" line with the same semantics.
1663 if (offer_bundle) {
deadbeefb7892532017-02-22 19:35:18 -08001664 answer->AddGroup(answer_bundle);
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001665 }
deadbeefb7892532017-02-22 19:35:18 -08001666
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001667 if (answer_bundle.FirstContentName()) {
deadbeefb7892532017-02-22 19:35:18 -08001668 // Share the same ICE credentials and crypto params across all contents,
1669 // as BUNDLE requires.
1670 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001671 RTC_LOG(LS_ERROR)
1672 << "CreateAnswer failed to UpdateTransportInfoForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001673 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675
deadbeefb7892532017-02-22 19:35:18 -08001676 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001677 RTC_LOG(LS_ERROR)
1678 << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001679 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 }
1681 }
1682
Steve Antone831b8c2018-02-01 12:22:16 -08001683 // The following determines how to signal MSIDs to ensure compatibility with
1684 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001685 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001686 // Unified Plan needs to look at what the offer included to find the most
1687 // compatible answer.
1688 if (offer->msid_signaling() == 0) {
1689 // We end up here in one of three cases:
1690 // 1. An empty offer. We'll reply with an empty answer so it doesn't
1691 // matter what we pick here.
1692 // 2. A data channel only offer. We won't add any MSIDs to the answer so
1693 // it also doesn't matter what we pick here.
1694 // 3. Media that's either sendonly or inactive from the remote endpoint.
1695 // We don't have any information to say whether the endpoint is Plan B
1696 // or Unified Plan, so be conservative and send both.
1697 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1698 cricket::kMsidSignalingSsrcAttribute);
1699 } else if (offer->msid_signaling() ==
1700 (cricket::kMsidSignalingMediaSection |
1701 cricket::kMsidSignalingSsrcAttribute)) {
1702 // If both a=msid and a=ssrc MSID signaling methods were used, we're
1703 // probably talking to a Unified Plan endpoint so respond with just
1704 // a=msid.
1705 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection);
1706 } else {
1707 // Otherwise, it's clear which method the offerer is using so repeat that
1708 // back to them.
1709 answer->set_msid_signaling(offer->msid_signaling());
1710 }
1711 } else {
1712 // Plan B always signals MSID using a=ssrc lines.
1713 answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1714 }
1715
Steve Anton6fe1fba2018-12-11 10:15:23 -08001716 return answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717}
1718
ossu075af922016-06-14 03:29:38 -07001719const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer(
1720 const RtpTransceiverDirection& direction) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001721 switch (direction) {
1722 // If stream is inactive - generate list as if sendrecv.
1723 case RtpTransceiverDirection::kSendRecv:
1724 case RtpTransceiverDirection::kInactive:
1725 return audio_sendrecv_codecs_;
1726 case RtpTransceiverDirection::kSendOnly:
1727 return audio_send_codecs_;
1728 case RtpTransceiverDirection::kRecvOnly:
1729 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001730 }
Steve Anton1d03a752017-11-27 14:30:09 -08001731 RTC_NOTREACHED();
1732 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001733}
1734
1735const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer(
1736 const RtpTransceiverDirection& offer,
1737 const RtpTransceiverDirection& answer) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001738 switch (answer) {
1739 // For inactive and sendrecv answers, generate lists as if we were to accept
1740 // the offer's direction. See RFC 3264 Section 6.1.
1741 case RtpTransceiverDirection::kSendRecv:
1742 case RtpTransceiverDirection::kInactive:
1743 return GetAudioCodecsForOffer(
1744 webrtc::RtpTransceiverDirectionReversed(offer));
1745 case RtpTransceiverDirection::kSendOnly:
ossu075af922016-06-14 03:29:38 -07001746 return audio_send_codecs_;
Steve Anton1d03a752017-11-27 14:30:09 -08001747 case RtpTransceiverDirection::kRecvOnly:
1748 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001749 }
Steve Anton1d03a752017-11-27 14:30:09 -08001750 RTC_NOTREACHED();
1751 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001752}
1753
Steve Anton5c72e712018-12-10 14:25:30 -08001754void MergeCodecsFromDescription(
1755 const std::vector<const ContentInfo*>& current_active_contents,
1756 AudioCodecs* audio_codecs,
1757 VideoCodecs* video_codecs,
1758 DataCodecs* data_codecs,
1759 UsedPayloadTypes* used_pltypes) {
1760 for (const ContentInfo* content : current_active_contents) {
1761 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001762 const AudioContentDescription* audio =
Steve Anton5c72e712018-12-10 14:25:30 -08001763 content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001764 MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001765 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001766 const VideoContentDescription* video =
Steve Anton5c72e712018-12-10 14:25:30 -08001767 content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001768 MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001769 } else if (IsMediaContentOfType(content, MEDIA_TYPE_DATA)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001770 const DataContentDescription* data =
Steve Anton5c72e712018-12-10 14:25:30 -08001771 content->media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001772 MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes);
1773 }
1774 }
1775}
1776
1777// Getting codecs for an offer involves these steps:
1778//
1779// 1. Construct payload type -> codec mappings for current description.
1780// 2. Add any reference codecs that weren't already present
1781// 3. For each individual media description (m= section), filter codecs based
1782// on the directional attribute (happens in another method).
1783void MediaSessionDescriptionFactory::GetCodecsForOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001784 const std::vector<const ContentInfo*>& current_active_contents,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 AudioCodecs* audio_codecs,
1786 VideoCodecs* video_codecs,
1787 DataCodecs* data_codecs) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 // First - get all codecs from the current description if the media type
zhihuang1c378ed2017-08-17 14:10:50 -07001789 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1790 // new media type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001791 UsedPayloadTypes used_pltypes;
1792 MergeCodecsFromDescription(current_active_contents, audio_codecs,
1793 video_codecs, data_codecs, &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794
Steve Anton5c72e712018-12-10 14:25:30 -08001795 // Add our codecs that are not in the current description.
zhihuang1c378ed2017-08-17 14:10:50 -07001796 MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes);
1797 MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1798 MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1799}
1800
1801// Getting codecs for an answer involves these steps:
1802//
1803// 1. Construct payload type -> codec mappings for current description.
1804// 2. Add any codecs from the offer that weren't already present.
1805// 3. Add any remaining codecs that weren't already present.
1806// 4. For each individual media description (m= section), filter codecs based
1807// on the directional attribute (happens in another method).
1808void MediaSessionDescriptionFactory::GetCodecsForAnswer(
Steve Anton5c72e712018-12-10 14:25:30 -08001809 const std::vector<const ContentInfo*>& current_active_contents,
1810 const SessionDescription& remote_offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001811 AudioCodecs* audio_codecs,
1812 VideoCodecs* video_codecs,
1813 DataCodecs* data_codecs) const {
zhihuang1c378ed2017-08-17 14:10:50 -07001814 // First - get all codecs from the current description if the media type
1815 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1816 // new media type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001817 UsedPayloadTypes used_pltypes;
1818 MergeCodecsFromDescription(current_active_contents, audio_codecs,
1819 video_codecs, data_codecs, &used_pltypes);
zhihuang1c378ed2017-08-17 14:10:50 -07001820
1821 // Second - filter out codecs that we don't support at all and should ignore.
1822 AudioCodecs filtered_offered_audio_codecs;
1823 VideoCodecs filtered_offered_video_codecs;
1824 DataCodecs filtered_offered_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001825 for (const ContentInfo& content : remote_offer.contents()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001826 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1827 const AudioContentDescription* audio =
Steve Antonb1c1de12017-12-21 15:14:30 -08001828 content.media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001829 for (const AudioCodec& offered_audio_codec : audio->codecs()) {
1830 if (!FindMatchingCodec<AudioCodec>(audio->codecs(),
1831 filtered_offered_audio_codecs,
1832 offered_audio_codec, nullptr) &&
1833 FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_,
1834 offered_audio_codec, nullptr)) {
1835 filtered_offered_audio_codecs.push_back(offered_audio_codec);
1836 }
1837 }
1838 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1839 const VideoContentDescription* video =
Steve Antonb1c1de12017-12-21 15:14:30 -08001840 content.media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001841 for (const VideoCodec& offered_video_codec : video->codecs()) {
1842 if (!FindMatchingCodec<VideoCodec>(video->codecs(),
1843 filtered_offered_video_codecs,
1844 offered_video_codec, nullptr) &&
1845 FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_,
1846 offered_video_codec, nullptr)) {
1847 filtered_offered_video_codecs.push_back(offered_video_codec);
1848 }
1849 }
1850 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1851 const DataContentDescription* data =
Steve Antonb1c1de12017-12-21 15:14:30 -08001852 content.media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001853 for (const DataCodec& offered_data_codec : data->codecs()) {
1854 if (!FindMatchingCodec<DataCodec>(data->codecs(),
1855 filtered_offered_data_codecs,
1856 offered_data_codec, nullptr) &&
1857 FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_,
1858 offered_data_codec, nullptr)) {
1859 filtered_offered_data_codecs.push_back(offered_data_codec);
1860 }
1861 }
1862 }
1863 }
1864
Steve Anton5c72e712018-12-10 14:25:30 -08001865 // Add codecs that are not in the current description but were in
zhihuang1c378ed2017-08-17 14:10:50 -07001866 // |remote_offer|.
1867 MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs,
1868 &used_pltypes);
1869 MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs,
1870 &used_pltypes);
1871 MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs,
1872 &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873}
1874
1875void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001876 const std::vector<const ContentInfo*>& current_active_contents,
zhihuang1c378ed2017-08-17 14:10:50 -07001877 RtpHeaderExtensions* offer_audio_extensions,
1878 RtpHeaderExtensions* offer_video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001879 // All header extensions allocated from the same range to avoid potential
1880 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 UsedRtpHeaderExtensionIds used_ids;
jbauch5869f502017-06-29 12:31:36 -07001882 RtpHeaderExtensions all_regular_extensions;
1883 RtpHeaderExtensions all_encrypted_extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884
1885 // First - get all extensions from the current description if the media type
1886 // is used.
1887 // Add them to |used_ids| so the local ids are not reused if a new media
1888 // type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001889 for (const ContentInfo* content : current_active_contents) {
1890 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
1891 const AudioContentDescription* audio =
1892 content->media_description()->as_audio();
1893 MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions,
1894 &all_regular_extensions, &all_encrypted_extensions,
1895 &used_ids);
1896 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
1897 const VideoContentDescription* video =
1898 content->media_description()->as_video();
1899 MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions,
1900 &all_regular_extensions, &all_encrypted_extensions,
1901 &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 }
1903 }
1904
Steve Anton5c72e712018-12-10 14:25:30 -08001905 // Add our default RTP header extensions that are not in the current
1906 // description.
Steve Anton8f66ddb2018-12-10 16:08:05 -08001907 MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions,
1908 &all_regular_extensions, &all_encrypted_extensions,
1909 &used_ids);
1910 MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions,
1911 &all_regular_extensions, &all_encrypted_extensions,
1912 &used_ids);
zhihuang1c378ed2017-08-17 14:10:50 -07001913
jbauch5869f502017-06-29 12:31:36 -07001914 // TODO(jbauch): Support adding encrypted header extensions to existing
1915 // sessions.
Steve Anton5c72e712018-12-10 14:25:30 -08001916 if (enable_encrypted_rtp_header_extensions_ &&
1917 current_active_contents.empty()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001918 AddEncryptedVersionsOfHdrExts(offer_audio_extensions,
1919 &all_encrypted_extensions, &used_ids);
1920 AddEncryptedVersionsOfHdrExts(offer_video_extensions,
1921 &all_encrypted_extensions, &used_ids);
jbauch5869f502017-06-29 12:31:36 -07001922 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923}
1924
1925bool MediaSessionDescriptionFactory::AddTransportOffer(
Yves Gerey665174f2018-06-19 15:03:05 +02001926 const std::string& content_name,
1927 const TransportOptions& transport_options,
1928 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001929 SessionDescription* offer_desc,
1930 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 if (!transport_desc_factory_)
Yves Gerey665174f2018-06-19 15:03:05 +02001932 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 const TransportDescription* current_tdesc =
1934 GetTransportDescription(content_name, current_desc);
kwiberg31022942016-03-11 14:18:21 -08001935 std::unique_ptr<TransportDescription> new_tdesc(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001936 transport_desc_factory_->CreateOffer(transport_options, current_tdesc,
1937 ice_credentials));
Steve Anton06817cd2018-12-18 15:55:30 -08001938 if (!new_tdesc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001939 RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name="
1940 << content_name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 }
Steve Anton06817cd2018-12-18 15:55:30 -08001942 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc));
1943 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944}
1945
Steve Anton1a9d3c32018-12-10 17:18:54 -08001946std::unique_ptr<TransportDescription>
1947MediaSessionDescriptionFactory::CreateTransportAnswer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 const std::string& content_name,
1949 const SessionDescription* offer_desc,
1950 const TransportOptions& transport_options,
deadbeefb7892532017-02-22 19:35:18 -08001951 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001952 bool require_transport_attributes,
1953 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 if (!transport_desc_factory_)
1955 return NULL;
1956 const TransportDescription* offer_tdesc =
1957 GetTransportDescription(content_name, offer_desc);
1958 const TransportDescription* current_tdesc =
1959 GetTransportDescription(content_name, current_desc);
deadbeefb7892532017-02-22 19:35:18 -08001960 return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1961 require_transport_attributes,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001962 current_tdesc, ice_credentials);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963}
1964
1965bool MediaSessionDescriptionFactory::AddTransportAnswer(
1966 const std::string& content_name,
1967 const TransportDescription& transport_desc,
1968 SessionDescription* answer_desc) const {
Steve Anton06817cd2018-12-18 15:55:30 -08001969 answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 return true;
1971}
1972
zhihuang1c378ed2017-08-17 14:10:50 -07001973// |audio_codecs| = set of all possible codecs that can be used, with correct
1974// payload type mappings
1975//
1976// |supported_audio_codecs| = set of codecs that are supported for the direction
1977// of this m= section
1978//
1979// acd->codecs() = set of previously negotiated codecs for this m= section
1980//
1981// The payload types should come from audio_codecs, but the order should come
1982// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
1983// change existing codec priority, and that new codecs are added with the right
1984// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001985bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001986 const MediaDescriptionOptions& media_description_options,
1987 const MediaSessionOptions& session_options,
1988 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001989 const SessionDescription* current_description,
1990 const RtpHeaderExtensions& audio_rtp_extensions,
1991 const AudioCodecs& audio_codecs,
1992 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001993 SessionDescription* desc,
1994 IceCredentialsIterator* ice_credentials) const {
zhihuang1c378ed2017-08-17 14:10:50 -07001995 // Filter audio_codecs (which includes all codecs, with correctly remapped
1996 // payload types) based on transceiver direction.
1997 const AudioCodecs& supported_audio_codecs =
1998 GetAudioCodecsForOffer(media_description_options.direction);
1999
2000 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002001 // Add the codecs from current content if it exists and is not rejected nor
2002 // recycled.
2003 if (current_content && !current_content->rejected &&
2004 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002005 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002006 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002007 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002008 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002009 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2010 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002011 filtered_codecs.push_back(codec);
2012 }
2013 }
2014 }
2015 // Add other supported audio codecs.
2016 AudioCodec found_codec;
2017 for (const AudioCodec& codec : supported_audio_codecs) {
2018 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
2019 codec, &found_codec) &&
2020 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2021 codec, nullptr)) {
2022 // Use the |found_codec| from |audio_codecs| because it has the correctly
2023 // mapped payload type.
2024 filtered_codecs.push_back(found_codec);
2025 }
2026 }
deadbeef44f08192015-12-15 16:20:09 -08002027
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002028 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002029 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2030 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002031
kwiberg31022942016-03-11 14:18:21 -08002032 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002033 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002034 GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options,
2035 &crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002036 if (!CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08002037 media_description_options, session_options, filtered_codecs,
2038 sdes_policy, GetCryptos(current_content), crypto_suites,
2039 audio_rtp_extensions, current_streams, audio.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002040 return false;
2041 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002042
2043 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2044 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002045
Steve Anton4e70a722017-11-28 14:57:10 -08002046 audio->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002047
Steve Anton5adfafd2017-12-20 16:34:00 -08002048 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002049 media_description_options.stopped, audio.release());
2050 if (!AddTransportOffer(media_description_options.mid,
2051 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002052 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002053 return false;
2054 }
2055
2056 return true;
2057}
2058
2059bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002060 const MediaDescriptionOptions& media_description_options,
2061 const MediaSessionOptions& session_options,
2062 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002063 const SessionDescription* current_description,
2064 const RtpHeaderExtensions& video_rtp_extensions,
2065 const VideoCodecs& video_codecs,
2066 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002067 SessionDescription* desc,
2068 IceCredentialsIterator* ice_credentials) const {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002069 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002070 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2071 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002072
kwiberg31022942016-03-11 14:18:21 -08002073 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002074 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002075 GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options,
2076 &crypto_suites);
2077
2078 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002079 // Add the codecs from current content if it exists and is not rejected nor
2080 // recycled.
2081 if (current_content && !current_content->rejected &&
2082 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002083 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002084 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002085 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002086 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002087 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002088 nullptr)) {
2089 filtered_codecs.push_back(codec);
2090 }
2091 }
2092 }
2093 // Add other supported video codecs.
2094 VideoCodec found_codec;
2095 for (const VideoCodec& codec : video_codecs_) {
2096 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
2097 &found_codec) &&
2098 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2099 nullptr)) {
2100 // Use the |found_codec| from |video_codecs| because it has the correctly
2101 // mapped payload type.
2102 filtered_codecs.push_back(found_codec);
2103 }
2104 }
2105
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002106 if (!CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08002107 media_description_options, session_options, filtered_codecs,
2108 sdes_policy, GetCryptos(current_content), crypto_suites,
2109 video_rtp_extensions, current_streams, video.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002110 return false;
2111 }
2112
zhihuang1c378ed2017-08-17 14:10:50 -07002113 video->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002114
2115 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2116 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002117
Steve Anton4e70a722017-11-28 14:57:10 -08002118 video->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002119
Steve Anton5adfafd2017-12-20 16:34:00 -08002120 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002121 media_description_options.stopped, video.release());
2122 if (!AddTransportOffer(media_description_options.mid,
2123 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002124 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002125 return false;
2126 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002127 return true;
2128}
2129
2130bool MediaSessionDescriptionFactory::AddDataContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002131 const MediaDescriptionOptions& media_description_options,
2132 const MediaSessionOptions& session_options,
2133 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002134 const SessionDescription* current_description,
zhihuang1c378ed2017-08-17 14:10:50 -07002135 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002136 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002137 SessionDescription* desc,
2138 IceCredentialsIterator* ice_credentials) const {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002139 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2140
kwiberg31022942016-03-11 14:18:21 -08002141 std::unique_ptr<DataContentDescription> data(new DataContentDescription());
zhihuang1c378ed2017-08-17 14:10:50 -07002142 bool is_sctp = (session_options.data_channel_type == DCT_SCTP);
2143 // If the DataChannel type is not specified, use the DataChannel type in
2144 // the current description.
2145 if (session_options.data_channel_type == DCT_NONE && current_content) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002146 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA));
Steve Antonb1c1de12017-12-21 15:14:30 -08002147 is_sctp = (current_content->media_description()->protocol() ==
2148 kMediaProtocolSctp);
zhihuang1c378ed2017-08-17 14:10:50 -07002149 }
deadbeef44f08192015-12-15 16:20:09 -08002150
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002151 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002152 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2153 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002154 std::vector<std::string> crypto_suites;
2155 if (is_sctp) {
2156 // SDES doesn't make sense for SCTP, so we disable it, and we only
2157 // get SDES crypto suites for RTP-based data channels.
2158 sdes_policy = cricket::SEC_DISABLED;
2159 // Unlike SetMediaProtocol below, we need to set the protocol
2160 // before we call CreateMediaContentOffer. Otherwise,
2161 // CreateMediaContentOffer won't know this is SCTP and will
2162 // generate SSRCs rather than SIDs.
deadbeef8b7e9ad2017-05-25 09:38:55 -07002163 // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once
2164 // it's safe to do so. Older versions of webrtc would reject these
2165 // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706.
Yves Gerey665174f2018-06-19 15:03:05 +02002166 data->set_protocol(secure_transport ? kMediaProtocolDtlsSctp
2167 : kMediaProtocolSctp);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002168 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002169 GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -07002170 &crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002171 }
2172
zhihuang1c378ed2017-08-17 14:10:50 -07002173 // Even SCTP uses a "codec".
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002174 if (!CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08002175 media_description_options, session_options, data_codecs, sdes_policy,
2176 GetCryptos(current_content), crypto_suites, RtpHeaderExtensions(),
2177 current_streams, data.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002178 return false;
2179 }
2180
2181 if (is_sctp) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002182 desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp,
zhihuang1c378ed2017-08-17 14:10:50 -07002183 data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002184 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002185 data->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002186 SetMediaProtocol(secure_transport, data.get());
Steve Anton5adfafd2017-12-20 16:34:00 -08002187 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002188 media_description_options.stopped, data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002189 }
zhihuang1c378ed2017-08-17 14:10:50 -07002190 if (!AddTransportOffer(media_description_options.mid,
2191 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002192 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002193 return false;
2194 }
2195 return true;
2196}
2197
zhihuang1c378ed2017-08-17 14:10:50 -07002198// |audio_codecs| = set of all possible codecs that can be used, with correct
2199// payload type mappings
2200//
2201// |supported_audio_codecs| = set of codecs that are supported for the direction
2202// of this m= section
2203//
2204// acd->codecs() = set of previously negotiated codecs for this m= section
2205//
2206// The payload types should come from audio_codecs, but the order should come
2207// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
2208// change existing codec priority, and that new codecs are added with the right
2209// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002210bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002211 const MediaDescriptionOptions& media_description_options,
2212 const MediaSessionOptions& session_options,
2213 const ContentInfo* offer_content,
2214 const SessionDescription* offer_description,
2215 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002216 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002217 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002218 const AudioCodecs& audio_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002219 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002220 SessionDescription* answer,
2221 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002222 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002223 const AudioContentDescription* offer_audio_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002224 offer_content->media_description()->as_audio();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002225
Steve Anton1a9d3c32018-12-10 17:18:54 -08002226 std::unique_ptr<TransportDescription> audio_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002227 media_description_options.mid, offer_description,
2228 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002229 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002230 if (!audio_transport) {
2231 return false;
2232 }
2233
zhihuang1c378ed2017-08-17 14:10:50 -07002234 // Pick codecs based on the requested communications direction in the offer
2235 // and the selected direction in the answer.
2236 // Note these will be filtered one final time in CreateMediaContentAnswer.
2237 auto wants_rtd = media_description_options.direction;
Steve Anton4e70a722017-11-28 14:57:10 -08002238 auto offer_rtd = offer_audio_description->direction();
ossu075af922016-06-14 03:29:38 -07002239 auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd);
zhihuang1c378ed2017-08-17 14:10:50 -07002240 AudioCodecs supported_audio_codecs =
2241 GetAudioCodecsForAnswer(offer_rtd, answer_rtd);
2242
2243 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002244 // Add the codecs from current content if it exists and is not rejected nor
2245 // recycled.
2246 if (current_content && !current_content->rejected &&
2247 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002248 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002249 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002250 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002251 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002252 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2253 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002254 filtered_codecs.push_back(codec);
2255 }
2256 }
2257 }
2258 // Add other supported audio codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002259 for (const AudioCodec& codec : supported_audio_codecs) {
2260 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
Zhi Huang6f367472017-11-22 13:20:02 -08002261 codec, nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002262 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2263 codec, nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002264 // We should use the local codec with local parameters and the codec id
2265 // would be correctly mapped in |NegotiateCodecs|.
2266 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002267 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002268 }
2269
zhihuang1c378ed2017-08-17 14:10:50 -07002270 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2271 session_options.bundle_enabled;
kwiberg31022942016-03-11 14:18:21 -08002272 std::unique_ptr<AudioContentDescription> audio_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002273 new AudioContentDescription());
2274 // Do not require or create SDES cryptos if DTLS is used.
2275 cricket::SecurePolicy sdes_policy =
2276 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
2277 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002278 offer_audio_description, media_description_options, session_options,
2279 filtered_codecs, sdes_policy, GetCryptos(current_content),
Steve Anton8f66ddb2018-12-10 16:08:05 -08002280 audio_rtp_header_extensions(),
Steve Anton1b8773d2018-04-06 11:13:34 -07002281 enable_encrypted_rtp_header_extensions_, current_streams,
2282 bundle_enabled, audio_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002283 return false; // Fails the session setup.
2284 }
2285
deadbeefb7892532017-02-22 19:35:18 -08002286 bool secure = bundle_transport ? bundle_transport->description.secure()
2287 : audio_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002288 bool rejected = media_description_options.stopped ||
2289 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002290 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
2291 audio_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002292 if (!AddTransportAnswer(media_description_options.mid,
2293 *(audio_transport.get()), answer)) {
2294 return false;
2295 }
2296
2297 if (rejected) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002298 RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid
2299 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002300 }
2301
zhihuang1c378ed2017-08-17 14:10:50 -07002302 answer->AddContent(media_description_options.mid, offer_content->type,
2303 rejected, audio_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002304 return true;
2305}
2306
2307bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002308 const MediaDescriptionOptions& media_description_options,
2309 const MediaSessionOptions& session_options,
2310 const ContentInfo* offer_content,
2311 const SessionDescription* offer_description,
2312 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002313 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002314 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002315 const VideoCodecs& video_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002316 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002317 SessionDescription* answer,
2318 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002319 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002320 const VideoContentDescription* offer_video_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002321 offer_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002322
Steve Anton1a9d3c32018-12-10 17:18:54 -08002323 std::unique_ptr<TransportDescription> video_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002324 media_description_options.mid, offer_description,
2325 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002326 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002327 if (!video_transport) {
2328 return false;
2329 }
2330
zhihuang1c378ed2017-08-17 14:10:50 -07002331 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002332 // Add the codecs from current content if it exists and is not rejected nor
2333 // recycled.
2334 if (current_content && !current_content->rejected &&
2335 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002336 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002337 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002338 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002339 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002340 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002341 nullptr)) {
2342 filtered_codecs.push_back(codec);
2343 }
2344 }
2345 }
2346 // Add other supported video codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002347 for (const VideoCodec& codec : video_codecs_) {
2348 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
Zhi Huang6f367472017-11-22 13:20:02 -08002349 nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002350 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2351 nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002352 // We should use the local codec with local parameters and the codec id
2353 // would be correctly mapped in |NegotiateCodecs|.
2354 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002355 }
2356 }
2357
2358 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2359 session_options.bundle_enabled;
2360
kwiberg31022942016-03-11 14:18:21 -08002361 std::unique_ptr<VideoContentDescription> video_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002362 new VideoContentDescription());
2363 // Do not require or create SDES cryptos if DTLS is used.
2364 cricket::SecurePolicy sdes_policy =
2365 video_transport->secure() ? cricket::SEC_DISABLED : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002366 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002367 offer_video_description, media_description_options, session_options,
2368 filtered_codecs, sdes_policy, GetCryptos(current_content),
Steve Anton8f66ddb2018-12-10 16:08:05 -08002369 video_rtp_header_extensions(),
Steve Anton1b8773d2018-04-06 11:13:34 -07002370 enable_encrypted_rtp_header_extensions_, current_streams,
2371 bundle_enabled, video_answer.get())) {
zhihuang1c378ed2017-08-17 14:10:50 -07002372 return false; // Failed the sessin setup.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002373 }
deadbeefb7892532017-02-22 19:35:18 -08002374 bool secure = bundle_transport ? bundle_transport->description.secure()
2375 : video_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002376 bool rejected = media_description_options.stopped ||
2377 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002378 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
2379 video_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002380 if (!AddTransportAnswer(media_description_options.mid,
2381 *(video_transport.get()), answer)) {
2382 return false;
2383 }
2384
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002385 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002386 video_answer->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002387 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002388 RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid
2389 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002390 }
zhihuang1c378ed2017-08-17 14:10:50 -07002391 answer->AddContent(media_description_options.mid, offer_content->type,
2392 rejected, video_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002393 return true;
2394}
2395
2396bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002397 const MediaDescriptionOptions& media_description_options,
2398 const MediaSessionOptions& session_options,
2399 const ContentInfo* offer_content,
2400 const SessionDescription* offer_description,
2401 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002402 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002403 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002404 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002405 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002406 SessionDescription* answer,
2407 IceCredentialsIterator* ice_credentials) const {
Steve Anton1a9d3c32018-12-10 17:18:54 -08002408 std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002409 media_description_options.mid, offer_description,
2410 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002411 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002412 if (!data_transport) {
2413 return false;
2414 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002415
kwiberg31022942016-03-11 14:18:21 -08002416 std::unique_ptr<DataContentDescription> data_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002417 new DataContentDescription());
2418 // Do not require or create SDES cryptos if DTLS is used.
2419 cricket::SecurePolicy sdes_policy =
2420 data_transport->secure() ? cricket::SEC_DISABLED : secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002421 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2422 session_options.bundle_enabled;
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002423 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA));
2424 const DataContentDescription* offer_data_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002425 offer_content->media_description()->as_data();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002426 if (!CreateMediaContentAnswer(
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002427 offer_data_description, media_description_options, session_options,
2428 data_codecs, sdes_policy, GetCryptos(current_content),
2429 RtpHeaderExtensions(), enable_encrypted_rtp_header_extensions_,
2430 current_streams, bundle_enabled, data_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002431 return false; // Fails the session setup.
2432 }
2433
zstein4b2e0822017-02-17 19:48:38 -08002434 // Respond with sctpmap if the offer uses sctpmap.
zstein4b2e0822017-02-17 19:48:38 -08002435 bool offer_uses_sctpmap = offer_data_description->use_sctpmap();
2436 data_answer->set_use_sctpmap(offer_uses_sctpmap);
2437
deadbeefb7892532017-02-22 19:35:18 -08002438 bool secure = bundle_transport ? bundle_transport->description.secure()
2439 : data_transport->secure();
2440
zhihuang1c378ed2017-08-17 14:10:50 -07002441 bool rejected = session_options.data_channel_type == DCT_NONE ||
2442 media_description_options.stopped ||
2443 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002444 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
2445 data_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002446 if (!AddTransportAnswer(media_description_options.mid,
2447 *(data_transport.get()), answer)) {
2448 return false;
2449 }
2450
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002451 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002452 data_answer->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002453 } else {
2454 // RFC 3264
2455 // The answer MUST contain the same number of m-lines as the offer.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002456 RTC_LOG(LS_INFO) << "Data is not supported in the answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002457 }
zhihuang1c378ed2017-08-17 14:10:50 -07002458 answer->AddContent(media_description_options.mid, offer_content->type,
2459 rejected, data_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002460 return true;
2461}
2462
zhihuang1c378ed2017-08-17 14:10:50 -07002463void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() {
2464 audio_sendrecv_codecs_.clear();
2465 all_audio_codecs_.clear();
2466 // Compute the audio codecs union.
2467 for (const AudioCodec& send : audio_send_codecs_) {
2468 all_audio_codecs_.push_back(send);
2469 if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_,
2470 send, nullptr)) {
2471 // It doesn't make sense to have an RTX codec we support sending but not
2472 // receiving.
2473 RTC_DCHECK(!IsRtxCodec(send));
2474 }
2475 }
2476 for (const AudioCodec& recv : audio_recv_codecs_) {
2477 if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_,
2478 recv, nullptr)) {
2479 all_audio_codecs_.push_back(recv);
2480 }
2481 }
2482 // Use NegotiateCodecs to merge our codec lists, since the operation is
2483 // essentially the same. Put send_codecs as the offered_codecs, which is the
2484 // order we'd like to follow. The reasoning is that encoding is usually more
2485 // expensive than decoding, and prioritizing a codec in the send list probably
2486 // means it's a codec we can handle efficiently.
2487 NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_,
2488 &audio_sendrecv_codecs_);
2489}
2490
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002491bool IsMediaContent(const ContentInfo* content) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002492 return (content && (content->type == MediaProtocolType::kRtp ||
2493 content->type == MediaProtocolType::kSctp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494}
2495
2496bool IsAudioContent(const ContentInfo* content) {
2497 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
2498}
2499
2500bool IsVideoContent(const ContentInfo* content) {
2501 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
2502}
2503
2504bool IsDataContent(const ContentInfo* content) {
2505 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
2506}
2507
deadbeef0ed85b22016-02-23 17:24:52 -08002508const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
2509 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002510 for (const ContentInfo& content : contents) {
2511 if (IsMediaContentOfType(&content, media_type)) {
2512 return &content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002513 }
2514 }
deadbeef0ed85b22016-02-23 17:24:52 -08002515 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002516}
2517
2518const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
2519 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2520}
2521
2522const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
2523 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2524}
2525
2526const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
2527 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2528}
2529
Steve Antonad7bffc2018-01-22 10:21:56 -08002530const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
2531 MediaType media_type) {
deadbeef0ed85b22016-02-23 17:24:52 -08002532 if (sdesc == nullptr) {
2533 return nullptr;
2534 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002535
2536 return GetFirstMediaContent(sdesc->contents(), media_type);
2537}
2538
2539const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
2540 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2541}
2542
2543const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
2544 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2545}
2546
2547const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
2548 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2549}
2550
2551const MediaContentDescription* GetFirstMediaContentDescription(
Yves Gerey665174f2018-06-19 15:03:05 +02002552 const SessionDescription* sdesc,
2553 MediaType media_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002554 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002555 return (content ? content->media_description() : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002556}
2557
2558const AudioContentDescription* GetFirstAudioContentDescription(
2559 const SessionDescription* sdesc) {
2560 return static_cast<const AudioContentDescription*>(
2561 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2562}
2563
2564const VideoContentDescription* GetFirstVideoContentDescription(
2565 const SessionDescription* sdesc) {
2566 return static_cast<const VideoContentDescription*>(
2567 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2568}
2569
2570const DataContentDescription* GetFirstDataContentDescription(
2571 const SessionDescription* sdesc) {
2572 return static_cast<const DataContentDescription*>(
2573 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2574}
2575
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002576//
2577// Non-const versions of the above functions.
2578//
2579
Steve Anton36b29d12017-10-30 09:57:42 -07002580ContentInfo* GetFirstMediaContent(ContentInfos* contents,
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002581 MediaType media_type) {
Steve Anton36b29d12017-10-30 09:57:42 -07002582 for (ContentInfo& content : *contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002583 if (IsMediaContentOfType(&content, media_type)) {
2584 return &content;
2585 }
2586 }
2587 return nullptr;
2588}
2589
Steve Anton36b29d12017-10-30 09:57:42 -07002590ContentInfo* GetFirstAudioContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002591 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2592}
2593
Steve Anton36b29d12017-10-30 09:57:42 -07002594ContentInfo* GetFirstVideoContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002595 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2596}
2597
Steve Anton36b29d12017-10-30 09:57:42 -07002598ContentInfo* GetFirstDataContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002599 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2600}
2601
Steve Antonad7bffc2018-01-22 10:21:56 -08002602ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
2603 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002604 if (sdesc == nullptr) {
2605 return nullptr;
2606 }
2607
Steve Anton36b29d12017-10-30 09:57:42 -07002608 return GetFirstMediaContent(&sdesc->contents(), media_type);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002609}
2610
2611ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) {
2612 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2613}
2614
2615ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) {
2616 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2617}
2618
2619ContentInfo* GetFirstDataContent(SessionDescription* sdesc) {
2620 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2621}
2622
2623MediaContentDescription* GetFirstMediaContentDescription(
2624 SessionDescription* sdesc,
2625 MediaType media_type) {
2626 ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002627 return (content ? content->media_description() : nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002628}
2629
2630AudioContentDescription* GetFirstAudioContentDescription(
2631 SessionDescription* sdesc) {
2632 return static_cast<AudioContentDescription*>(
2633 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2634}
2635
2636VideoContentDescription* GetFirstVideoContentDescription(
2637 SessionDescription* sdesc) {
2638 return static_cast<VideoContentDescription*>(
2639 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2640}
2641
2642DataContentDescription* GetFirstDataContentDescription(
2643 SessionDescription* sdesc) {
2644 return static_cast<DataContentDescription*>(
2645 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2646}
2647
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002648} // namespace cricket