blob: 19eb769dfccf645aa65118974f086b16cdc670dc [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/media_session.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Elad Alon157540a2019-02-08 23:37:52 +010013#include <algorithm>
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 Anton64b626b2019-01-28 17:25:26 -080021#include "absl/algorithm/container.h"
Steve Anton5c72e712018-12-10 14:25:30 -080022#include "absl/memory/memory.h"
Niels Möller2edab4c2018-10-22 09:48:08 +020023#include "absl/strings/match.h"
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020024#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/crypto_params.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/h264_profile_level_id.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "media/base/media_constants.h"
28#include "p2p/base/p2p_constants.h"
29#include "pc/channel_manager.h"
30#include "pc/rtp_media_utils.h"
31#include "pc/srtp_filter.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"
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080036#include "rtc_base/unique_id_generator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38namespace {
Steve Anton1d03a752017-11-27 14:30:09 -080039
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080040using rtc::UniqueRandomIdGenerator;
Steve Anton1d03a752017-11-27 14:30:09 -080041using webrtc::RtpTransceiverDirection;
42
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043const char kInline[] = "inline:";
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080044
Benjamin Wrighta54daf12018-10-11 15:33:17 -070045void GetSupportedSdesCryptoSuiteNames(
46 void (*func)(const webrtc::CryptoOptions&, std::vector<int>*),
47 const webrtc::CryptoOptions& crypto_options,
48 std::vector<std::string>* names) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080049 std::vector<int> crypto_suites;
jbauchcb560652016-08-04 05:20:32 -070050 func(crypto_options, &crypto_suites);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080051 for (const auto crypto : crypto_suites) {
52 names->push_back(rtc::SrtpCryptoSuiteToName(crypto));
53 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080054}
Elad Alon157540a2019-02-08 23:37:52 +010055
terelius8c011e52016-04-26 05:28:11 -070056} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
58namespace cricket {
59
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060// RTP Profile names
61// http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
62// RFC4585
63const char kMediaProtocolAvpf[] = "RTP/AVPF";
64// RFC5124
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +000065const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
66
deadbeeff3938292015-07-15 12:20:53 -070067// We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
68// but we tolerate "RTP/SAVPF" in offers we receive, for compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069const char kMediaProtocolSavpf[] = "RTP/SAVPF";
70
71const char kMediaProtocolRtpPrefix[] = "RTP/";
72
73const char kMediaProtocolSctp[] = "SCTP";
74const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
lally@webrtc.orgec97c652015-02-24 20:18:48 +000075const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
lally@webrtc.orga7470932015-02-24 20:19:21 +000076const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077
deadbeef8b7e9ad2017-05-25 09:38:55 -070078// Note that the below functions support some protocol strings purely for
79// legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names
80// and Interoperability.
81
82static bool IsDtlsRtp(const std::string& protocol) {
83 // Most-likely values first.
84 return protocol == "UDP/TLS/RTP/SAVPF" || protocol == "TCP/TLS/RTP/SAVPF" ||
85 protocol == "UDP/TLS/RTP/SAVP" || protocol == "TCP/TLS/RTP/SAVP";
86}
87
88static bool IsPlainRtp(const std::string& protocol) {
89 // Most-likely values first.
90 return protocol == "RTP/SAVPF" || protocol == "RTP/AVPF" ||
91 protocol == "RTP/SAVP" || protocol == "RTP/AVP";
92}
93
94static bool IsDtlsSctp(const std::string& protocol) {
95 return protocol == kMediaProtocolDtlsSctp ||
96 protocol == kMediaProtocolUdpDtlsSctp ||
97 protocol == kMediaProtocolTcpDtlsSctp;
98}
99
100static bool IsPlainSctp(const std::string& protocol) {
101 return protocol == kMediaProtocolSctp;
102}
103
104static bool IsSctp(const std::string& protocol) {
105 return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
106}
107
Steve Anton1d03a752017-11-27 14:30:09 -0800108static RtpTransceiverDirection NegotiateRtpTransceiverDirection(
109 RtpTransceiverDirection offer,
110 RtpTransceiverDirection wants) {
111 bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer);
112 bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer);
113 bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants);
114 bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants);
115 return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send,
116 offer_send && wants_recv);
ossu075af922016-06-14 03:29:38 -0700117}
118
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119static bool IsMediaContentOfType(const ContentInfo* content,
120 MediaType media_type) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800121 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122 return false;
123 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800124 return content->media_description()->type() == media_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125}
126
Yves Gerey665174f2018-06-19 15:03:05 +0200127static bool CreateCryptoParams(int tag,
128 const std::string& cipher,
Steve Anton3a66edf2018-09-10 12:57:37 -0700129 CryptoParams* crypto_out) {
jbauchcb560652016-08-04 05:20:32 -0700130 int key_len;
131 int salt_len;
Yves Gerey665174f2018-06-19 15:03:05 +0200132 if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher),
133 &key_len, &salt_len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 return false;
135 }
jbauchcb560652016-08-04 05:20:32 -0700136
137 int master_key_len = key_len + salt_len;
138 std::string master_key;
139 if (!rtc::CreateRandomData(master_key_len, &master_key)) {
140 return false;
141 }
142
kwiberg352444f2016-11-28 15:58:53 -0800143 RTC_CHECK_EQ(master_key_len, master_key.size());
jbauchcb560652016-08-04 05:20:32 -0700144 std::string key = rtc::Base64::Encode(master_key);
145
Steve Anton3a66edf2018-09-10 12:57:37 -0700146 crypto_out->tag = tag;
147 crypto_out->cipher_suite = cipher;
148 crypto_out->key_params = kInline;
149 crypto_out->key_params += key;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 return true;
151}
152
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153static bool AddCryptoParams(const std::string& cipher_suite,
Steve Anton3a66edf2018-09-10 12:57:37 -0700154 CryptoParamsVec* cryptos_out) {
155 int size = static_cast<int>(cryptos_out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156
Steve Anton3a66edf2018-09-10 12:57:37 -0700157 cryptos_out->resize(size + 1);
158 return CreateCryptoParams(size, cipher_suite, &cryptos_out->at(size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159}
160
161void AddMediaCryptos(const CryptoParamsVec& cryptos,
162 MediaContentDescription* media) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700163 for (const CryptoParams& crypto : cryptos) {
164 media->AddCrypto(crypto);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 }
166}
167
168bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
169 MediaContentDescription* media) {
170 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700171 for (const std::string& crypto_suite : crypto_suites) {
172 if (!AddCryptoParams(crypto_suite, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 return false;
174 }
175 }
176 AddMediaCryptos(cryptos, media);
177 return true;
178}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179
zhihuang1c378ed2017-08-17 14:10:50 -0700180const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800181 if (!content || !content->media_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700182 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800184 return &content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185}
186
187bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
188 const CryptoParams& crypto,
Steve Anton3a66edf2018-09-10 12:57:37 -0700189 CryptoParams* crypto_out) {
Steve Anton64b626b2019-01-28 17:25:26 -0800190 auto it = absl::c_find_if(
191 cryptos, [&crypto](const CryptoParams& c) { return crypto.Matches(c); });
Steve Anton3a66edf2018-09-10 12:57:37 -0700192 if (it == cryptos.end()) {
193 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700195 *crypto_out = *it;
196 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197}
198
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700199// For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the
200// low overhead.
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700201void GetSupportedAudioSdesCryptoSuites(
202 const webrtc::CryptoOptions& crypto_options,
203 std::vector<int>* crypto_suites) {
204 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700205 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
206 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
207 }
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700208 if (crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher) {
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700209 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32);
210 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800211 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212}
213
deadbeef7914b8c2017-04-21 03:23:33 -0700214void GetSupportedAudioSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700215 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800216 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700217 GetSupportedSdesCryptoSuiteNames(GetSupportedAudioSdesCryptoSuites,
218 crypto_options, crypto_suite_names);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219}
220
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700221void GetSupportedVideoSdesCryptoSuites(
222 const webrtc::CryptoOptions& crypto_options,
223 std::vector<int>* crypto_suites) {
224 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700225 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
226 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
227 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800228 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229}
230
deadbeef7914b8c2017-04-21 03:23:33 -0700231void GetSupportedVideoSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700232 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800233 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700234 GetSupportedSdesCryptoSuiteNames(GetSupportedVideoSdesCryptoSuites,
235 crypto_options, crypto_suite_names);
236}
237
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700238void GetSupportedDataSdesCryptoSuites(
239 const webrtc::CryptoOptions& crypto_options,
240 std::vector<int>* crypto_suites) {
241 if (crypto_options.srtp.enable_gcm_crypto_suites) {
deadbeef7914b8c2017-04-21 03:23:33 -0700242 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
243 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
244 }
245 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
246}
247
248void GetSupportedDataSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700249 const webrtc::CryptoOptions& crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -0700250 std::vector<std::string>* crypto_suite_names) {
251 GetSupportedSdesCryptoSuiteNames(GetSupportedDataSdesCryptoSuites,
252 crypto_options, crypto_suite_names);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800253}
254
jbauchcb560652016-08-04 05:20:32 -0700255// Support any GCM cipher (if enabled through options). For video support only
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700256// 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated (if enabled) unless
257// bundle is enabled because it is low overhead.
jbauchcb560652016-08-04 05:20:32 -0700258// Pick the crypto in the list that is supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259static bool SelectCrypto(const MediaContentDescription* offer,
260 bool bundle,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700261 const webrtc::CryptoOptions& crypto_options,
Steve Anton3a66edf2018-09-10 12:57:37 -0700262 CryptoParams* crypto_out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
264 const CryptoParamsVec& cryptos = offer->cryptos();
265
Steve Anton3a66edf2018-09-10 12:57:37 -0700266 for (const CryptoParams& crypto : cryptos) {
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700267 if ((crypto_options.srtp.enable_gcm_crypto_suites &&
Steve Anton3a66edf2018-09-10 12:57:37 -0700268 rtc::IsGcmCryptoSuiteName(crypto.cipher_suite)) ||
269 rtc::CS_AES_CM_128_HMAC_SHA1_80 == crypto.cipher_suite ||
270 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == crypto.cipher_suite && audio &&
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700271 !bundle && crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher)) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700272 return CreateCryptoParams(crypto.tag, crypto.cipher_suite, crypto_out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 }
274 }
275 return false;
276}
277
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278// Finds all StreamParams of all media types and attach them to stream_params.
Steve Anton5c72e712018-12-10 14:25:30 -0800279static StreamParamsVec GetCurrentStreamParams(
280 const std::vector<const ContentInfo*>& active_local_contents) {
281 StreamParamsVec stream_params;
282 for (const ContentInfo* content : active_local_contents) {
283 for (const StreamParams& params : content->media_description()->streams()) {
284 stream_params.push_back(params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 }
286 }
Steve Anton5c72e712018-12-10 14:25:30 -0800287 return stream_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288}
289
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000290// Filters the data codecs for the data channel type.
291void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
292 // Filter RTP codec for SCTP and vice versa.
solenberg9fa49752016-10-08 13:02:44 -0700293 const char* codec_name =
294 sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName;
Steve Anton3a66edf2018-09-10 12:57:37 -0700295 codecs->erase(std::remove_if(codecs->begin(), codecs->end(),
296 [&codec_name](const DataCodec& codec) {
Niels Möller039743e2018-10-23 10:07:25 +0200297 return absl::EqualsIgnoreCase(codec.name,
298 codec_name);
Steve Anton3a66edf2018-09-10 12:57:37 -0700299 }),
300 codecs->end());
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000301}
302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303template <typename IdStruct>
304class UsedIds {
305 public:
306 UsedIds(int min_allowed_id, int max_allowed_id)
307 : min_allowed_id_(min_allowed_id),
308 max_allowed_id_(max_allowed_id),
Yves Gerey665174f2018-06-19 15:03:05 +0200309 next_id_(max_allowed_id) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310
311 // Loops through all Id in |ids| and changes its id if it is
312 // already in use by another IdStruct. Call this methods with all Id
313 // in a session description to make sure no duplicate ids exists.
314 // Note that typename Id must be a type of IdStruct.
315 template <typename Id>
316 void FindAndSetIdUsed(std::vector<Id>* ids) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700317 for (const Id& id : *ids) {
318 FindAndSetIdUsed(&id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319 }
320 }
321
322 // Finds and sets an unused id if the |idstruct| id is already in use.
323 void FindAndSetIdUsed(IdStruct* idstruct) {
324 const int original_id = idstruct->id;
325 int new_id = idstruct->id;
326
327 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
328 // If the original id is not in range - this is an id that can't be
329 // dynamically changed.
330 return;
331 }
332
333 if (IsIdUsed(original_id)) {
334 new_id = FindUnusedId();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100335 RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from "
336 << original_id << " to " << new_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337 idstruct->id = new_id;
338 }
339 SetIdUsed(new_id);
340 }
341
342 private:
343 // Returns the first unused id in reverse order.
344 // This hopefully reduce the risk of more collisions. We want to change the
345 // default ids as little as possible.
346 int FindUnusedId() {
347 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
348 --next_id_;
349 }
nisseede5da42017-01-12 05:15:36 -0800350 RTC_DCHECK(next_id_ >= min_allowed_id_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 return next_id_;
352 }
353
Yves Gerey665174f2018-06-19 15:03:05 +0200354 bool IsIdUsed(int new_id) { return id_set_.find(new_id) != id_set_.end(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355
Yves Gerey665174f2018-06-19 15:03:05 +0200356 void SetIdUsed(int new_id) { id_set_.insert(new_id); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357
358 const int min_allowed_id_;
359 const int max_allowed_id_;
360 int next_id_;
361 std::set<int> id_set_;
362};
363
364// Helper class used for finding duplicate RTP payload types among audio, video
365// and data codecs. When bundle is used the payload types may not collide.
366class UsedPayloadTypes : public UsedIds<Codec> {
367 public:
368 UsedPayloadTypes()
Yves Gerey665174f2018-06-19 15:03:05 +0200369 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370
371 private:
372 static const int kDynamicPayloadTypeMin = 96;
373 static const int kDynamicPayloadTypeMax = 127;
374};
375
376// Helper class used for finding duplicate RTP Header extension ids among
Johannes Kron07ba2b92018-09-26 13:33:35 +0200377// audio and video extensions. Only applies to one-byte header extensions at the
378// moment. ids > 14 will always be reported as available.
379// TODO(kron): This class needs to be refactored when we start to send two-byte
380// header extensions.
isheriff6f8d6862016-05-26 11:24:55 -0700381class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382 public:
383 UsedRtpHeaderExtensionIds()
Johannes Kron07ba2b92018-09-26 13:33:35 +0200384 : UsedIds<webrtc::RtpExtension>(
385 webrtc::RtpExtension::kMinId,
386 webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387
388 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389};
390
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800391static StreamParams CreateStreamParamsForNewSenderWithSsrcs(
392 const SenderOptions& sender,
393 const std::string& rtcp_cname,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800394 bool include_rtx_streams,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800395 bool include_flexfec_stream,
396 UniqueRandomIdGenerator* ssrc_generator) {
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800397 StreamParams result;
398 result.id = sender.track_id;
399
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800400 // TODO(brandtr): Update when we support multistream protection.
401 if (include_flexfec_stream && sender.num_sim_layers > 1) {
402 include_flexfec_stream = false;
403 RTC_LOG(LS_WARNING)
404 << "Our FlexFEC implementation only supports protecting "
405 "a single media streams. This session has multiple "
406 "media streams however, so no FlexFEC SSRC will be generated.";
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800407 }
408
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800409 result.GenerateSsrcs(sender.num_sim_layers, include_rtx_streams,
410 include_flexfec_stream, ssrc_generator);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800411
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800412 result.cname = rtcp_cname;
413 result.set_stream_ids(sender.stream_ids);
414
415 return result;
416}
417
418static bool ValidateSimulcastLayers(
419 const std::vector<RidDescription>& rids,
420 const SimulcastLayerList& simulcast_layers) {
Steve Anton64b626b2019-01-28 17:25:26 -0800421 return absl::c_all_of(
422 simulcast_layers.GetAllLayers(), [&rids](const SimulcastLayer& layer) {
423 return absl::c_any_of(rids, [&layer](const RidDescription& rid) {
424 return rid.rid == layer.rid;
425 });
426 });
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800427}
428
429static StreamParams CreateStreamParamsForNewSenderWithRids(
430 const SenderOptions& sender,
431 const std::string& rtcp_cname) {
432 RTC_DCHECK(!sender.rids.empty());
433 RTC_DCHECK_EQ(sender.num_sim_layers, 0)
434 << "RIDs are the compliant way to indicate simulcast.";
435 RTC_DCHECK(ValidateSimulcastLayers(sender.rids, sender.simulcast_layers));
436 StreamParams result;
437 result.id = sender.track_id;
438 result.cname = rtcp_cname;
439 result.set_stream_ids(sender.stream_ids);
440
441 // More than one rid should be signaled.
442 if (sender.rids.size() > 1) {
443 result.set_rids(sender.rids);
444 }
445
446 return result;
447}
448
449// Adds SimulcastDescription if indicated by the media description options.
450// MediaContentDescription should already be set up with the send rids.
451static void AddSimulcastToMediaDescription(
452 const MediaDescriptionOptions& media_description_options,
453 MediaContentDescription* description) {
454 RTC_DCHECK(description);
455
456 // Check if we are using RIDs in this scenario.
Steve Anton64b626b2019-01-28 17:25:26 -0800457 if (absl::c_all_of(description->streams(), [](const StreamParams& params) {
458 return !params.has_rids();
459 })) {
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800460 return;
461 }
462
463 RTC_DCHECK_EQ(1, description->streams().size())
464 << "RIDs are only supported in Unified Plan semantics.";
465 RTC_DCHECK_EQ(1, media_description_options.sender_options.size());
466 RTC_DCHECK(description->type() == MediaType::MEDIA_TYPE_AUDIO ||
467 description->type() == MediaType::MEDIA_TYPE_VIDEO);
468
469 // One RID or less indicates that simulcast is not needed.
470 if (description->streams()[0].rids().size() <= 1) {
471 return;
472 }
473
Amit Hilbuchb7446ed2019-01-28 12:25:25 -0800474 // Only negotiate the send layers.
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800475 SimulcastDescription simulcast;
476 simulcast.send_layers() =
477 media_description_options.sender_options[0].simulcast_layers;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800478 description->set_simulcast_description(simulcast);
479}
480
zhihuang1c378ed2017-08-17 14:10:50 -0700481// Adds a StreamParams for each SenderOptions in |sender_options| to
482// content_description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483// |current_params| - All currently known StreamParams of any media type.
484template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700485static bool AddStreamParams(
486 const std::vector<SenderOptions>& sender_options,
487 const std::string& rtcp_cname,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800488 UniqueRandomIdGenerator* ssrc_generator,
zhihuang1c378ed2017-08-17 14:10:50 -0700489 StreamParamsVec* current_streams,
490 MediaContentDescriptionImpl<C>* content_description) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700491 // SCTP streams are not negotiated using SDP/ContentDescriptions.
deadbeef8b7e9ad2017-05-25 09:38:55 -0700492 if (IsSctp(content_description->protocol())) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700493 return true;
494 }
495
Noah Richards2e7a0982015-05-18 14:02:54 -0700496 const bool include_rtx_streams =
497 ContainsRtxCodec(content_description->codecs());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498
brandtr03d5fb12016-11-22 03:37:59 -0800499 const bool include_flexfec_stream =
500 ContainsFlexfecCodec(content_description->codecs());
501
zhihuang1c378ed2017-08-17 14:10:50 -0700502 for (const SenderOptions& sender : sender_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 // groupid is empty for StreamParams generated using
504 // MediaSessionDescriptionFactory.
zhihuang1c378ed2017-08-17 14:10:50 -0700505 StreamParams* param =
506 GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000507 if (!param) {
zhihuang1c378ed2017-08-17 14:10:50 -0700508 // This is a new sender.
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800509 StreamParams stream_param =
510 sender.rids.empty()
511 ?
512 // Signal SSRCs and legacy simulcast (if requested).
513 CreateStreamParamsForNewSenderWithSsrcs(
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800514 sender, rtcp_cname, include_rtx_streams,
515 include_flexfec_stream, ssrc_generator)
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800516 :
517 // Signal RIDs and spec-compliant simulcast (if requested).
518 CreateStreamParamsForNewSenderWithRids(sender, rtcp_cname);
519
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 content_description->AddStream(stream_param);
521
522 // Store the new StreamParams in current_streams.
523 // This is necessary so that we can use the CNAME for other media types.
524 current_streams->push_back(stream_param);
525 } else {
deadbeef2f425aa2017-04-14 10:41:32 -0700526 // Use existing generated SSRCs/groups, but update the sync_label if
527 // necessary. This may be needed if a MediaStreamTrack was moved from one
528 // MediaStream to another.
Seth Hampson845e8782018-03-02 11:34:10 -0800529 param->set_stream_ids(sender.stream_ids);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000530 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 }
532 }
533 return true;
534}
535
536// Updates the transport infos of the |sdesc| according to the given
537// |bundle_group|. The transport infos of the content names within the
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800538// |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
539// first content within the |bundle_group|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
541 SessionDescription* sdesc) {
542 // The bundle should not be empty.
543 if (!sdesc || !bundle_group.FirstContentName()) {
544 return false;
545 }
546
547 // We should definitely have a transport for the first content.
jbauch083b73f2015-07-16 02:46:32 -0700548 const std::string& selected_content_name = *bundle_group.FirstContentName();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 const TransportInfo* selected_transport_info =
550 sdesc->GetTransportInfoByName(selected_content_name);
551 if (!selected_transport_info) {
552 return false;
553 }
554
555 // Set the other contents to use the same ICE credentials.
jbauch083b73f2015-07-16 02:46:32 -0700556 const std::string& selected_ufrag =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 selected_transport_info->description.ice_ufrag;
jbauch083b73f2015-07-16 02:46:32 -0700558 const std::string& selected_pwd =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 selected_transport_info->description.ice_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800560 ConnectionRole selected_connection_role =
561 selected_transport_info->description.connection_role;
Steve Anton3a66edf2018-09-10 12:57:37 -0700562 for (TransportInfo& transport_info : sdesc->transport_infos()) {
563 if (bundle_group.HasContentName(transport_info.content_name) &&
564 transport_info.content_name != selected_content_name) {
565 transport_info.description.ice_ufrag = selected_ufrag;
566 transport_info.description.ice_pwd = selected_pwd;
567 transport_info.description.connection_role = selected_connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 }
569 }
570 return true;
571}
572
573// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
574// sets it to |cryptos|.
575static bool GetCryptosByName(const SessionDescription* sdesc,
576 const std::string& content_name,
577 CryptoParamsVec* cryptos) {
578 if (!sdesc || !cryptos) {
579 return false;
580 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 const ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800582 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 return false;
584 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800585 *cryptos = content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 return true;
587}
588
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
590// which are not available in |filter|.
591static void PruneCryptos(const CryptoParamsVec& filter,
592 CryptoParamsVec* target_cryptos) {
593 if (!target_cryptos) {
594 return;
595 }
tzik21995802018-04-26 17:38:28 +0900596
597 target_cryptos->erase(
598 std::remove_if(target_cryptos->begin(), target_cryptos->end(),
599 // Returns true if the |crypto|'s cipher_suite is not
600 // found in |filter|.
601 [&filter](const CryptoParams& crypto) {
602 for (const CryptoParams& entry : filter) {
603 if (entry.cipher_suite == crypto.cipher_suite)
604 return false;
605 }
606 return true;
607 }),
608 target_cryptos->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609}
610
Steve Antonfa2260d2017-12-28 16:38:23 -0800611bool IsRtpProtocol(const std::string& protocol) {
deadbeefb5cb19b2015-11-23 16:39:12 -0800612 return protocol.empty() ||
613 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
614}
615
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616static bool IsRtpContent(SessionDescription* sdesc,
617 const std::string& content_name) {
618 bool is_rtp = false;
619 ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800620 if (content && content->media_description()) {
621 is_rtp = IsRtpProtocol(content->media_description()->protocol());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 }
623 return is_rtp;
624}
625
626// Updates the crypto parameters of the |sdesc| according to the given
627// |bundle_group|. The crypto parameters of all the contents within the
628// |bundle_group| should be updated to use the common subset of the
629// available cryptos.
630static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
631 SessionDescription* sdesc) {
632 // The bundle should not be empty.
633 if (!sdesc || !bundle_group.FirstContentName()) {
634 return false;
635 }
636
wu@webrtc.org78187522013-10-07 23:32:02 +0000637 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 // Get the common cryptos.
639 const ContentNames& content_names = bundle_group.content_names();
640 CryptoParamsVec common_cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700641 bool first = true;
642 for (const std::string& content_name : content_names) {
643 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 continue;
645 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000646 // The common cryptos are needed if any of the content does not have DTLS
647 // enabled.
Steve Anton3a66edf2018-09-10 12:57:37 -0700648 if (!sdesc->GetTransportInfoByName(content_name)->description.secure()) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000649 common_cryptos_needed = true;
650 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700651 if (first) {
652 first = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 // Initial the common_cryptos with the first content in the bundle group.
Steve Anton3a66edf2018-09-10 12:57:37 -0700654 if (!GetCryptosByName(sdesc, content_name, &common_cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 return false;
656 }
657 if (common_cryptos.empty()) {
658 // If there's no crypto params, we should just return.
659 return true;
660 }
661 } else {
662 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700663 if (!GetCryptosByName(sdesc, content_name, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 return false;
665 }
666 PruneCryptos(cryptos, &common_cryptos);
667 }
668 }
669
wu@webrtc.org78187522013-10-07 23:32:02 +0000670 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 return false;
672 }
673
674 // Update to use the common cryptos.
Steve Anton3a66edf2018-09-10 12:57:37 -0700675 for (const std::string& content_name : content_names) {
676 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 continue;
678 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700679 ContentInfo* content = sdesc->GetContentByName(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 if (IsMediaContent(content)) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800681 MediaContentDescription* media_desc = content->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 if (!media_desc) {
683 return false;
684 }
685 media_desc->set_cryptos(common_cryptos);
686 }
687 }
688 return true;
689}
690
Steve Anton5c72e712018-12-10 14:25:30 -0800691static std::vector<const ContentInfo*> GetActiveContents(
692 const SessionDescription& description,
693 const MediaSessionOptions& session_options) {
694 std::vector<const ContentInfo*> active_contents;
695 for (size_t i = 0; i < description.contents().size(); ++i) {
696 RTC_DCHECK_LT(i, session_options.media_description_options.size());
697 const ContentInfo& content = description.contents()[i];
698 const MediaDescriptionOptions& media_options =
699 session_options.media_description_options[i];
700 if (!content.rejected && !media_options.stopped &&
701 content.name == media_options.mid) {
702 active_contents.push_back(&content);
703 }
704 }
705 return active_contents;
706}
707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708template <class C>
709static bool ContainsRtxCodec(const std::vector<C>& codecs) {
brandtr03d5fb12016-11-22 03:37:59 -0800710 for (const auto& codec : codecs) {
711 if (IsRtxCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 return true;
713 }
714 }
715 return false;
716}
717
718template <class C>
719static bool IsRtxCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200720 return absl::EqualsIgnoreCase(codec.name, kRtxCodecName);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721}
722
brandtr03d5fb12016-11-22 03:37:59 -0800723template <class C>
724static bool ContainsFlexfecCodec(const std::vector<C>& codecs) {
725 for (const auto& codec : codecs) {
726 if (IsFlexfecCodec(codec)) {
727 return true;
728 }
729 }
730 return false;
731}
732
733template <class C>
734static bool IsFlexfecCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200735 return absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName);
brandtr03d5fb12016-11-22 03:37:59 -0800736}
737
zhihuang1c378ed2017-08-17 14:10:50 -0700738// Create a media content to be offered for the given |sender_options|,
739// according to the given options.rtcp_mux, session_options.is_muc, codecs,
740// secure_transport, crypto, and current_streams. If we don't currently have
741// crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is
742// created (according to crypto_suites). The created content is added to the
743// offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744template <class C>
745static bool CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800746 const MediaDescriptionOptions& media_description_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700747 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000749 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 const CryptoParamsVec* current_cryptos,
751 const std::vector<std::string>& crypto_suites,
752 const RtpHeaderExtensions& rtp_extensions,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800753 UniqueRandomIdGenerator* ssrc_generator,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 StreamParamsVec* current_streams,
755 MediaContentDescriptionImpl<C>* offer) {
756 offer->AddCodecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757
zhihuang1c378ed2017-08-17 14:10:50 -0700758 offer->set_rtcp_mux(session_options.rtcp_mux_enabled);
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700759 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
760 offer->set_rtcp_reduced_size(true);
761 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 offer->set_rtp_header_extensions(rtp_extensions);
763
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800764 if (!AddStreamParams(media_description_options.sender_options,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800765 session_options.rtcp_cname, ssrc_generator,
766 current_streams, offer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 return false;
768 }
769
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800770 AddSimulcastToMediaDescription(media_description_options, offer);
771
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 if (secure_policy != SEC_DISABLED) {
773 if (current_cryptos) {
774 AddMediaCryptos(*current_cryptos, offer);
775 }
776 if (offer->cryptos().empty()) {
777 if (!CreateMediaCryptos(crypto_suites, offer)) {
778 return false;
779 }
780 }
781 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782
deadbeef7af91dd2016-12-13 11:29:11 -0800783 if (secure_policy == SEC_REQUIRED && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 return false;
785 }
786 return true;
787}
788
789template <class C>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000790static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
magjedb05fa242016-11-11 04:00:16 -0800791 const int codec1_id,
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000792 const std::vector<C>& codecs2,
magjedb05fa242016-11-11 04:00:16 -0800793 const int codec2_id) {
794 const C* codec1 = FindCodecById(codecs1, codec1_id);
795 const C* codec2 = FindCodecById(codecs2, codec2_id);
796 return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2);
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000797}
798
799template <class C>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800static void NegotiateCodecs(const std::vector<C>& local_codecs,
801 const std::vector<C>& offered_codecs,
802 std::vector<C>* negotiated_codecs) {
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800803 for (const C& ours : local_codecs) {
804 C theirs;
deadbeef67cf2c12016-04-13 10:07:16 -0700805 // Note that we intentionally only find one matching codec for each of our
806 // local codecs, in case the remote offer contains duplicate codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800807 if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) {
808 C negotiated = ours;
809 negotiated.IntersectFeedbackParams(theirs);
810 if (IsRtxCodec(negotiated)) {
magjedb05fa242016-11-11 04:00:16 -0800811 const auto apt_it =
812 theirs.params.find(kCodecParamAssociatedPayloadType);
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800813 // FindMatchingCodec shouldn't return something with no apt value.
magjedb05fa242016-11-11 04:00:16 -0800814 RTC_DCHECK(apt_it != theirs.params.end());
815 negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 }
Niels Möller039743e2018-10-23 10:07:25 +0200817 if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) {
magjedf823ede2016-11-12 09:53:04 -0800818 webrtc::H264::GenerateProfileLevelIdForAnswer(
819 ours.params, theirs.params, &negotiated.params);
820 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800821 negotiated.id = theirs.id;
ossu075af922016-06-14 03:29:38 -0700822 negotiated.name = theirs.name;
magjedb05fa242016-11-11 04:00:16 -0800823 negotiated_codecs->push_back(std::move(negotiated));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 }
825 }
deadbeef67cf2c12016-04-13 10:07:16 -0700826 // RFC3264: Although the answerer MAY list the formats in their desired
827 // order of preference, it is RECOMMENDED that unless there is a
828 // specific reason, the answerer list formats in the same relative order
829 // they were present in the offer.
830 std::unordered_map<int, int> payload_type_preferences;
831 int preference = static_cast<int>(offered_codecs.size() + 1);
832 for (const C& codec : offered_codecs) {
833 payload_type_preferences[codec.id] = preference--;
834 }
Steve Anton64b626b2019-01-28 17:25:26 -0800835 absl::c_sort(
836 *negotiated_codecs, [&payload_type_preferences](const C& a, const C& b) {
837 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
838 });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839}
840
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800841// Finds a codec in |codecs2| that matches |codec_to_match|, which is
842// a member of |codecs1|. If |codec_to_match| is an RTX codec, both
843// the codecs themselves and their associated codecs must match.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844template <class C>
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800845static bool FindMatchingCodec(const std::vector<C>& codecs1,
846 const std::vector<C>& codecs2,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 const C& codec_to_match,
848 C* found_codec) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -0700849 // |codec_to_match| should be a member of |codecs1|, in order to look up RTX
850 // codecs' associated codecs correctly. If not, that's a programming error.
Steve Anton64b626b2019-01-28 17:25:26 -0800851 RTC_DCHECK(absl::c_any_of(codecs1, [&codec_to_match](const C& codec) {
852 return &codec == &codec_to_match;
853 }));
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800854 for (const C& potential_match : codecs2) {
855 if (potential_match.Matches(codec_to_match)) {
856 if (IsRtxCodec(codec_to_match)) {
magjedb05fa242016-11-11 04:00:16 -0800857 int apt_value_1 = 0;
858 int apt_value_2 = 0;
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800859 if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType,
860 &apt_value_1) ||
861 !potential_match.GetParam(kCodecParamAssociatedPayloadType,
862 &apt_value_2)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100863 RTC_LOG(LS_WARNING) << "RTX missing associated payload type.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800864 continue;
865 }
866 if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2,
867 apt_value_2)) {
868 continue;
869 }
870 }
871 if (found_codec) {
872 *found_codec = potential_match;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 }
874 return true;
875 }
876 }
877 return false;
878}
879
zhihuang1c378ed2017-08-17 14:10:50 -0700880// Find the codec in |codec_list| that |rtx_codec| is associated with.
881template <class C>
882static const C* GetAssociatedCodec(const std::vector<C>& codec_list,
883 const C& rtx_codec) {
884 std::string associated_pt_str;
885 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
886 &associated_pt_str)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100887 RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
888 << " is missing an associated payload type.";
zhihuang1c378ed2017-08-17 14:10:50 -0700889 return nullptr;
890 }
891
892 int associated_pt;
893 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100894 RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
895 << " of RTX codec " << rtx_codec.name
896 << " to an integer.";
zhihuang1c378ed2017-08-17 14:10:50 -0700897 return nullptr;
898 }
899
900 // Find the associated reference codec for the reference RTX codec.
901 const C* associated_codec = FindCodecById(codec_list, associated_pt);
902 if (!associated_codec) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100903 RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
904 << associated_pt << " for RTX codec " << rtx_codec.name
905 << ".";
zhihuang1c378ed2017-08-17 14:10:50 -0700906 }
907 return associated_codec;
908}
909
910// Adds all codecs from |reference_codecs| to |offered_codecs| that don't
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911// already exist in |offered_codecs| and ensure the payload types don't
912// collide.
913template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700914static void MergeCodecs(const std::vector<C>& reference_codecs,
915 std::vector<C>* offered_codecs,
916 UsedPayloadTypes* used_pltypes) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 // Add all new codecs that are not RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800918 for (const C& reference_codec : reference_codecs) {
919 if (!IsRtxCodec(reference_codec) &&
920 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
921 reference_codec, nullptr)) {
922 C codec = reference_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 used_pltypes->FindAndSetIdUsed(&codec);
924 offered_codecs->push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926 }
927
928 // Add all new RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800929 for (const C& reference_codec : reference_codecs) {
930 if (IsRtxCodec(reference_codec) &&
931 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
932 reference_codec, nullptr)) {
933 C rtx_codec = reference_codec;
olka3c747662017-08-17 06:50:32 -0700934 const C* associated_codec =
zhihuang1c378ed2017-08-17 14:10:50 -0700935 GetAssociatedCodec(reference_codecs, rtx_codec);
olka3c747662017-08-17 06:50:32 -0700936 if (!associated_codec) {
olka3c747662017-08-17 06:50:32 -0700937 continue;
938 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800939 // Find a codec in the offered list that matches the reference codec.
940 // Its payload type may be different than the reference codec.
941 C matching_codec;
942 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
magjedb05fa242016-11-11 04:00:16 -0800943 *associated_codec, &matching_codec)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100944 RTC_LOG(LS_WARNING)
945 << "Couldn't find matching " << associated_codec->name << " codec.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800946 continue;
947 }
948
949 rtx_codec.params[kCodecParamAssociatedPayloadType] =
950 rtc::ToString(matching_codec.id);
951 used_pltypes->FindAndSetIdUsed(&rtx_codec);
952 offered_codecs->push_back(rtx_codec);
953 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 }
955}
956
zhihuang1c378ed2017-08-17 14:10:50 -0700957static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
958 const webrtc::RtpExtension& ext_to_match,
959 webrtc::RtpExtension* found_extension) {
Steve Anton64b626b2019-01-28 17:25:26 -0800960 auto it = absl::c_find_if(
961 extensions, [&ext_to_match](const webrtc::RtpExtension& extension) {
962 // We assume that all URIs are given in a canonical
963 // format.
964 return extension.uri == ext_to_match.uri &&
965 extension.encrypt == ext_to_match.encrypt;
966 });
Steve Anton3a66edf2018-09-10 12:57:37 -0700967 if (it == extensions.end()) {
968 return false;
zhihuang1c378ed2017-08-17 14:10:50 -0700969 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700970 if (found_extension) {
971 *found_extension = *it;
972 }
973 return true;
zhihuang1c378ed2017-08-17 14:10:50 -0700974}
975
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976static bool FindByUri(const RtpHeaderExtensions& extensions,
isheriff6f8d6862016-05-26 11:24:55 -0700977 const webrtc::RtpExtension& ext_to_match,
978 webrtc::RtpExtension* found_extension) {
jbauch5869f502017-06-29 12:31:36 -0700979 // We assume that all URIs are given in a canonical format.
980 const webrtc::RtpExtension* found =
981 webrtc::RtpExtension::FindHeaderExtensionByUri(extensions,
982 ext_to_match.uri);
983 if (!found) {
984 return false;
985 }
986 if (found_extension) {
987 *found_extension = *found;
988 }
989 return true;
990}
991
992static bool FindByUriWithEncryptionPreference(
993 const RtpHeaderExtensions& extensions,
Yves Gerey665174f2018-06-19 15:03:05 +0200994 const webrtc::RtpExtension& ext_to_match,
995 bool encryption_preference,
jbauch5869f502017-06-29 12:31:36 -0700996 webrtc::RtpExtension* found_extension) {
997 const webrtc::RtpExtension* unencrypted_extension = nullptr;
Steve Anton3a66edf2018-09-10 12:57:37 -0700998 for (const webrtc::RtpExtension& extension : extensions) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 // We assume that all URIs are given in a canonical format.
Steve Anton3a66edf2018-09-10 12:57:37 -07001000 if (extension.uri == ext_to_match.uri) {
1001 if (!encryption_preference || extension.encrypt) {
jbauch5869f502017-06-29 12:31:36 -07001002 if (found_extension) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001003 *found_extension = extension;
jbauch5869f502017-06-29 12:31:36 -07001004 }
1005 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 }
Steve Anton3a66edf2018-09-10 12:57:37 -07001007 unencrypted_extension = &extension;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 }
1009 }
jbauch5869f502017-06-29 12:31:36 -07001010 if (unencrypted_extension) {
1011 if (found_extension) {
1012 *found_extension = *unencrypted_extension;
1013 }
1014 return true;
1015 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 return false;
1017}
1018
zhihuang1c378ed2017-08-17 14:10:50 -07001019// Adds all extensions from |reference_extensions| to |offered_extensions| that
1020// don't already exist in |offered_extensions| and ensure the IDs don't
1021// collide. If an extension is added, it's also added to |regular_extensions| or
1022// |encrypted_extensions|, and if the extension is in |regular_extensions| or
1023// |encrypted_extensions|, its ID is marked as used in |used_ids|.
1024// |offered_extensions| is for either audio or video while |regular_extensions|
1025// and |encrypted_extensions| are used for both audio and video. There could be
1026// overlap between audio extensions and video extensions.
1027static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions,
1028 RtpHeaderExtensions* offered_extensions,
1029 RtpHeaderExtensions* regular_extensions,
1030 RtpHeaderExtensions* encrypted_extensions,
1031 UsedRtpHeaderExtensionIds* used_ids) {
olka3c747662017-08-17 06:50:32 -07001032 for (auto reference_extension : reference_extensions) {
zhihuang1c378ed2017-08-17 14:10:50 -07001033 if (!FindByUriAndEncryption(*offered_extensions, reference_extension,
1034 nullptr)) {
olka3c747662017-08-17 06:50:32 -07001035 webrtc::RtpExtension existing;
zhihuang1c378ed2017-08-17 14:10:50 -07001036 if (reference_extension.encrypt) {
1037 if (FindByUriAndEncryption(*encrypted_extensions, reference_extension,
1038 &existing)) {
1039 offered_extensions->push_back(existing);
1040 } else {
1041 used_ids->FindAndSetIdUsed(&reference_extension);
1042 encrypted_extensions->push_back(reference_extension);
1043 offered_extensions->push_back(reference_extension);
1044 }
olka3c747662017-08-17 06:50:32 -07001045 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07001046 if (FindByUriAndEncryption(*regular_extensions, reference_extension,
1047 &existing)) {
1048 offered_extensions->push_back(existing);
1049 } else {
1050 used_ids->FindAndSetIdUsed(&reference_extension);
1051 regular_extensions->push_back(reference_extension);
1052 offered_extensions->push_back(reference_extension);
1053 }
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001054 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 }
1056 }
1057}
1058
jbauch5869f502017-06-29 12:31:36 -07001059static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions,
1060 RtpHeaderExtensions* all_extensions,
1061 UsedRtpHeaderExtensionIds* used_ids) {
1062 RtpHeaderExtensions encrypted_extensions;
1063 for (const webrtc::RtpExtension& extension : *extensions) {
1064 webrtc::RtpExtension existing;
1065 // Don't add encrypted extensions again that were already included in a
1066 // previous offer or regular extensions that are also included as encrypted
1067 // extensions.
1068 if (extension.encrypt ||
1069 !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) ||
1070 (FindByUriWithEncryptionPreference(*extensions, extension, true,
Yves Gerey665174f2018-06-19 15:03:05 +02001071 &existing) &&
1072 existing.encrypt)) {
jbauch5869f502017-06-29 12:31:36 -07001073 continue;
1074 }
1075
1076 if (FindByUri(*all_extensions, extension, &existing)) {
1077 encrypted_extensions.push_back(existing);
1078 } else {
1079 webrtc::RtpExtension encrypted(extension);
1080 encrypted.encrypt = true;
1081 used_ids->FindAndSetIdUsed(&encrypted);
1082 all_extensions->push_back(encrypted);
1083 encrypted_extensions.push_back(encrypted);
1084 }
1085 }
1086 extensions->insert(extensions->end(), encrypted_extensions.begin(),
Yves Gerey665174f2018-06-19 15:03:05 +02001087 encrypted_extensions.end());
jbauch5869f502017-06-29 12:31:36 -07001088}
1089
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090static void NegotiateRtpHeaderExtensions(
1091 const RtpHeaderExtensions& local_extensions,
1092 const RtpHeaderExtensions& offered_extensions,
jbauch5869f502017-06-29 12:31:36 -07001093 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 RtpHeaderExtensions* negotiated_extenstions) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001095 for (const webrtc::RtpExtension& ours : local_extensions) {
isheriff6f8d6862016-05-26 11:24:55 -07001096 webrtc::RtpExtension theirs;
Yves Gerey665174f2018-06-19 15:03:05 +02001097 if (FindByUriWithEncryptionPreference(
Steve Anton3a66edf2018-09-10 12:57:37 -07001098 offered_extensions, ours, enable_encrypted_rtp_header_extensions,
Yves Gerey665174f2018-06-19 15:03:05 +02001099 &theirs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100 // We respond with their RTP header extension id.
1101 negotiated_extenstions->push_back(theirs);
1102 }
1103 }
1104}
1105
1106static void StripCNCodecs(AudioCodecs* audio_codecs) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001107 audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(),
1108 [](const AudioCodec& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +02001109 return absl::EqualsIgnoreCase(
1110 codec.name, kComfortNoiseCodecName);
Steve Anton3a66edf2018-09-10 12:57:37 -07001111 }),
1112 audio_codecs->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113}
1114
zhihuang1c378ed2017-08-17 14:10:50 -07001115// Create a media content to be answered for the given |sender_options|
1116// according to the given session_options.rtcp_mux, session_options.streams,
1117// codecs, crypto, and current_streams. If we don't currently have crypto (in
1118// current_cryptos) and it is enabled (in secure_policy), crypto is created
1119// (according to crypto_suites). The codecs, rtcp_mux, and crypto are all
1120// negotiated with the offer. If the negotiation fails, this method returns
1121// false. The created content is added to the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122template <class C>
1123static bool CreateMediaContentAnswer(
1124 const MediaContentDescriptionImpl<C>* offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001125 const MediaDescriptionOptions& media_description_options,
1126 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001128 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 const CryptoParamsVec* current_cryptos,
1130 const RtpHeaderExtensions& local_rtp_extenstions,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001131 UniqueRandomIdGenerator* ssrc_generator,
jbauch5869f502017-06-29 12:31:36 -07001132 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 StreamParamsVec* current_streams,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 bool bundle_enabled,
1135 MediaContentDescriptionImpl<C>* answer) {
1136 std::vector<C> negotiated_codecs;
1137 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
1138 answer->AddCodecs(negotiated_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 answer->set_protocol(offer->protocol());
Johannes Kron0854eb62018-10-10 22:33:20 +02001140
Johannes Kron9581bc42018-10-23 10:17:39 +02001141 answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 RtpHeaderExtensions negotiated_rtp_extensions;
Yves Gerey665174f2018-06-19 15:03:05 +02001143 NegotiateRtpHeaderExtensions(
1144 local_rtp_extenstions, offer->rtp_header_extensions(),
1145 enable_encrypted_rtp_header_extensions, &negotiated_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
1147
zhihuang1c378ed2017-08-17 14:10:50 -07001148 answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux());
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -07001149 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
1150 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
1151 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152
1153 if (sdes_policy != SEC_DISABLED) {
1154 CryptoParams crypto;
zhihuang1c378ed2017-08-17 14:10:50 -07001155 if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options,
1156 &crypto)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 if (current_cryptos) {
1158 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
1159 }
1160 answer->AddCrypto(crypto);
1161 }
1162 }
1163
deadbeef7af91dd2016-12-13 11:29:11 -08001164 if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 return false;
1166 }
1167
zhihuang1c378ed2017-08-17 14:10:50 -07001168 if (!AddStreamParams(media_description_options.sender_options,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001169 session_options.rtcp_cname, ssrc_generator,
1170 current_streams, answer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 return false; // Something went seriously wrong.
1172 }
1173
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001174 AddSimulcastToMediaDescription(media_description_options, answer);
1175
Steve Anton4e70a722017-11-28 14:57:10 -08001176 answer->set_direction(NegotiateRtpTransceiverDirection(
1177 offer->direction(), media_description_options.direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 return true;
1179}
1180
1181static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001182 const std::string& protocol,
1183 bool secure_transport) {
zhihuangcf5b37c2016-05-05 11:44:35 -07001184 // Since not all applications serialize and deserialize the media protocol,
1185 // we will have to accept |protocol| to be empty.
1186 if (protocol.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 return true;
1188 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001189
zhihuangcf5b37c2016-05-05 11:44:35 -07001190 if (type == MEDIA_TYPE_DATA) {
1191 // Check for SCTP, but also for RTP for RTP-based data channels.
1192 // TODO(pthatcher): Remove RTP once RTP-based data channels are gone.
1193 if (secure_transport) {
1194 // Most likely scenarios first.
1195 return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) ||
1196 IsPlainRtp(protocol);
1197 } else {
1198 return IsPlainSctp(protocol) || IsPlainRtp(protocol);
1199 }
1200 }
1201
1202 // Allow for non-DTLS RTP protocol even when using DTLS because that's what
1203 // JSEP specifies.
1204 if (secure_transport) {
1205 // Most likely scenarios first.
1206 return IsDtlsRtp(protocol) || IsPlainRtp(protocol);
1207 } else {
1208 return IsPlainRtp(protocol);
1209 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210}
1211
1212static void SetMediaProtocol(bool secure_transport,
1213 MediaContentDescription* desc) {
deadbeeff3938292015-07-15 12:20:53 -07001214 if (!desc->cryptos().empty())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 desc->set_protocol(kMediaProtocolSavpf);
deadbeeff3938292015-07-15 12:20:53 -07001216 else if (secure_transport)
1217 desc->set_protocol(kMediaProtocolDtlsSavpf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 else
1219 desc->set_protocol(kMediaProtocolAvpf);
1220}
1221
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001222// Gets the TransportInfo of the given |content_name| from the
1223// |current_description|. If doesn't exist, returns a new one.
1224static const TransportDescription* GetTransportDescription(
1225 const std::string& content_name,
1226 const SessionDescription* current_description) {
1227 const TransportDescription* desc = NULL;
1228 if (current_description) {
1229 const TransportInfo* info =
1230 current_description->GetTransportInfoByName(content_name);
1231 if (info) {
1232 desc = &info->description;
1233 }
1234 }
1235 return desc;
1236}
1237
1238// Gets the current DTLS state from the transport description.
zhihuang1c378ed2017-08-17 14:10:50 -07001239static bool IsDtlsActive(const ContentInfo* content,
1240 const SessionDescription* current_description) {
1241 if (!content) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001242 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001243 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001244
zhihuang1c378ed2017-08-17 14:10:50 -07001245 size_t msection_index = content - &current_description->contents()[0];
1246
1247 if (current_description->transport_infos().size() <= msection_index) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001248 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001249 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001250
zhihuang1c378ed2017-08-17 14:10:50 -07001251 return current_description->transport_infos()[msection_index]
1252 .description.secure();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001253}
1254
Steve Anton8ffb9c32017-08-31 15:45:38 -07001255void MediaDescriptionOptions::AddAudioSender(
1256 const std::string& track_id,
1257 const std::vector<std::string>& stream_ids) {
zhihuang1c378ed2017-08-17 14:10:50 -07001258 RTC_DCHECK(type == MEDIA_TYPE_AUDIO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001259 AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001260}
1261
Steve Anton8ffb9c32017-08-31 15:45:38 -07001262void MediaDescriptionOptions::AddVideoSender(
1263 const std::string& track_id,
1264 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001265 const std::vector<RidDescription>& rids,
1266 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001267 int num_sim_layers) {
zhihuang1c378ed2017-08-17 14:10:50 -07001268 RTC_DCHECK(type == MEDIA_TYPE_VIDEO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001269 RTC_DCHECK(rids.empty() || num_sim_layers == 0)
1270 << "RIDs are the compliant way to indicate simulcast.";
1271 RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers));
1272 AddSenderInternal(track_id, stream_ids, rids, simulcast_layers,
1273 num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001274}
1275
zhihuang1c378ed2017-08-17 14:10:50 -07001276void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id,
1277 const std::string& stream_id) {
1278 RTC_DCHECK(type == MEDIA_TYPE_DATA);
Steve Anton8ffb9c32017-08-31 15:45:38 -07001279 // TODO(steveanton): Is it the case that RtpDataChannel will never have more
1280 // than one stream?
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001281 AddSenderInternal(track_id, {stream_id}, {}, SimulcastLayerList(), 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282}
1283
Steve Anton8ffb9c32017-08-31 15:45:38 -07001284void MediaDescriptionOptions::AddSenderInternal(
1285 const std::string& track_id,
1286 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001287 const std::vector<RidDescription>& rids,
1288 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001289 int num_sim_layers) {
1290 // TODO(steveanton): Support any number of stream ids.
1291 RTC_CHECK(stream_ids.size() == 1U);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001292 SenderOptions options;
1293 options.track_id = track_id;
1294 options.stream_ids = stream_ids;
1295 options.simulcast_layers = simulcast_layers;
1296 options.rids = rids;
1297 options.num_sim_layers = num_sim_layers;
1298 sender_options.push_back(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299}
1300
zhihuang1c378ed2017-08-17 14:10:50 -07001301bool MediaSessionOptions::HasMediaDescription(MediaType type) const {
Steve Anton64b626b2019-01-28 17:25:26 -08001302 return absl::c_any_of(
1303 media_description_options,
1304 [type](const MediaDescriptionOptions& t) { return t.type == type; });
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001305}
1306
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001308 const TransportDescriptionFactory* transport_desc_factory,
1309 rtc::UniqueRandomIdGenerator* ssrc_generator)
1310 : ssrc_generator_(ssrc_generator),
1311 transport_desc_factory_(transport_desc_factory) {
1312 RTC_DCHECK(ssrc_generator_);
1313}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314
1315MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1316 ChannelManager* channel_manager,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001317 const TransportDescriptionFactory* transport_desc_factory,
1318 rtc::UniqueRandomIdGenerator* ssrc_generator)
1319 : MediaSessionDescriptionFactory(transport_desc_factory, ssrc_generator) {
ossudedfd282016-06-14 07:12:39 -07001320 channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_);
1321 channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
magjed3cf8ece2016-11-10 03:36:53 -08001323 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1325 channel_manager->GetSupportedDataCodecs(&data_codecs_);
zhihuang1c378ed2017-08-17 14:10:50 -07001326 ComputeAudioCodecsIntersectionAndUnion();
ossu075af922016-06-14 03:29:38 -07001327}
1328
ossudedfd282016-06-14 07:12:39 -07001329const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs()
1330 const {
ossu075af922016-06-14 03:29:38 -07001331 return audio_sendrecv_codecs_;
1332}
1333
1334const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const {
1335 return audio_send_codecs_;
1336}
1337
1338const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const {
1339 return audio_recv_codecs_;
1340}
1341
1342void MediaSessionDescriptionFactory::set_audio_codecs(
Yves Gerey665174f2018-06-19 15:03:05 +02001343 const AudioCodecs& send_codecs,
1344 const AudioCodecs& recv_codecs) {
ossu075af922016-06-14 03:29:38 -07001345 audio_send_codecs_ = send_codecs;
1346 audio_recv_codecs_ = recv_codecs;
zhihuang1c378ed2017-08-17 14:10:50 -07001347 ComputeAudioCodecsIntersectionAndUnion();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348}
1349
Amit Hilbuch77938e62018-12-21 09:23:38 -08001350static void AddUnifiedPlanExtensions(RtpHeaderExtensions* extensions) {
1351 RTC_DCHECK(extensions);
Elad Alon157540a2019-02-08 23:37:52 +01001352
1353 rtc::UniqueNumberGenerator<int> unique_id_generator;
1354 unique_id_generator.AddKnownId(0); // The first valid RTP extension ID is 1.
1355 for (const webrtc::RtpExtension& extension : *extensions) {
1356 const bool collision_free = unique_id_generator.AddKnownId(extension.id);
1357 RTC_DCHECK(collision_free);
1358 }
1359
Amit Hilbuch77938e62018-12-21 09:23:38 -08001360 // Unified Plan also offers the MID and RID header extensions.
Elad Alon157540a2019-02-08 23:37:52 +01001361 extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kMidUri,
1362 unique_id_generator()));
1363 extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kRidUri,
1364 unique_id_generator()));
Amit Hilbuch77938e62018-12-21 09:23:38 -08001365 extensions->push_back(webrtc::RtpExtension(
Elad Alon157540a2019-02-08 23:37:52 +01001366 webrtc::RtpExtension::kRepairedRidUri, unique_id_generator()));
Amit Hilbuch77938e62018-12-21 09:23:38 -08001367}
1368
1369RtpHeaderExtensions
1370MediaSessionDescriptionFactory::audio_rtp_header_extensions() const {
1371 RtpHeaderExtensions extensions = audio_rtp_extensions_;
1372 if (is_unified_plan_) {
1373 AddUnifiedPlanExtensions(&extensions);
1374 }
1375
1376 return extensions;
1377}
1378
1379RtpHeaderExtensions
1380MediaSessionDescriptionFactory::video_rtp_header_extensions() const {
1381 RtpHeaderExtensions extensions = video_rtp_extensions_;
1382 if (is_unified_plan_) {
1383 AddUnifiedPlanExtensions(&extensions);
1384 }
1385
1386 return extensions;
1387}
1388
Steve Anton6fe1fba2018-12-11 10:15:23 -08001389std::unique_ptr<SessionDescription> MediaSessionDescriptionFactory::CreateOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001390 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 const SessionDescription* current_description) const {
Steve Anton5c72e712018-12-10 14:25:30 -08001392 // Must have options for each existing section.
1393 if (current_description) {
1394 RTC_DCHECK_LE(current_description->contents().size(),
1395 session_options.media_description_options.size());
1396 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001398 IceCredentialsIterator ice_credentials(
1399 session_options.pooled_ice_credentials);
Steve Anton5c72e712018-12-10 14:25:30 -08001400
1401 std::vector<const ContentInfo*> current_active_contents;
1402 if (current_description) {
1403 current_active_contents =
1404 GetActiveContents(*current_description, session_options);
1405 }
1406
1407 StreamParamsVec current_streams =
1408 GetCurrentStreamParams(current_active_contents);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409
zhihuang1c378ed2017-08-17 14:10:50 -07001410 AudioCodecs offer_audio_codecs;
1411 VideoCodecs offer_video_codecs;
1412 DataCodecs offer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001413 GetCodecsForOffer(current_active_contents, &offer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001414 &offer_video_codecs, &offer_data_codecs);
ossu075af922016-06-14 03:29:38 -07001415
zhihuang1c378ed2017-08-17 14:10:50 -07001416 if (!session_options.vad_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 // If application doesn't want CN codecs in offer.
zhihuang1c378ed2017-08-17 14:10:50 -07001418 StripCNCodecs(&offer_audio_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 }
zhihuang1c378ed2017-08-17 14:10:50 -07001420 FilterDataCodecs(&offer_data_codecs,
1421 session_options.data_channel_type == DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422
1423 RtpHeaderExtensions audio_rtp_extensions;
1424 RtpHeaderExtensions video_rtp_extensions;
Steve Anton8f66ddb2018-12-10 16:08:05 -08001425 GetRtpHdrExtsToOffer(current_active_contents, &audio_rtp_extensions,
1426 &video_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427
Steve Anton5c72e712018-12-10 14:25:30 -08001428 auto offer = absl::make_unique<SessionDescription>();
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001429
zhihuang1c378ed2017-08-17 14:10:50 -07001430 // Iterate through the media description options, matching with existing media
1431 // descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001432 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001433 for (const MediaDescriptionOptions& media_description_options :
1434 session_options.media_description_options) {
1435 const ContentInfo* current_content = nullptr;
1436 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001437 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001438 current_content = &current_description->contents()[msection_index];
Steve Antondcc3c022017-12-22 16:02:54 -08001439 // Media type must match unless this media section is being recycled.
Steve Anton5c72e712018-12-10 14:25:30 -08001440 RTC_DCHECK(current_content->name != media_description_options.mid ||
Steve Antondcc3c022017-12-22 16:02:54 -08001441 IsMediaContentOfType(current_content,
zhihuang1c378ed2017-08-17 14:10:50 -07001442 media_description_options.type));
1443 }
1444 switch (media_description_options.type) {
1445 case MEDIA_TYPE_AUDIO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001446 if (!AddAudioContentForOffer(
1447 media_description_options, session_options, current_content,
1448 current_description, audio_rtp_extensions, offer_audio_codecs,
1449 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001450 return nullptr;
1451 }
1452 break;
1453 case MEDIA_TYPE_VIDEO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001454 if (!AddVideoContentForOffer(
1455 media_description_options, session_options, current_content,
1456 current_description, video_rtp_extensions, offer_video_codecs,
1457 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001458 return nullptr;
1459 }
1460 break;
1461 case MEDIA_TYPE_DATA:
1462 if (!AddDataContentForOffer(media_description_options, session_options,
1463 current_content, current_description,
1464 offer_data_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001465 offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001466 return nullptr;
1467 }
1468 break;
1469 default:
1470 RTC_NOTREACHED();
1471 }
1472 ++msection_index;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 }
1474
1475 // Bundle the contents together, if we've been asked to do so, and update any
1476 // parameters that need to be tweaked for BUNDLE.
Steve Anton2bed3972019-01-04 17:04:30 -08001477 if (session_options.bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
zhihuang1c378ed2017-08-17 14:10:50 -07001479 for (const ContentInfo& content : offer->contents()) {
Steve Anton2bed3972019-01-04 17:04:30 -08001480 if (content.rejected) {
1481 continue;
1482 }
zhihuang1c378ed2017-08-17 14:10:50 -07001483 // TODO(deadbeef): There are conditions that make bundling two media
1484 // descriptions together illegal. For example, they use the same payload
1485 // type to represent different codecs, or same IDs for different header
1486 // extensions. We need to detect this and not try to bundle those media
1487 // descriptions together.
1488 offer_bundle.AddContentName(content.name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489 }
Steve Anton2bed3972019-01-04 17:04:30 -08001490 if (!offer_bundle.content_names().empty()) {
1491 offer->AddGroup(offer_bundle);
1492 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1493 RTC_LOG(LS_ERROR)
1494 << "CreateOffer failed to UpdateTransportInfoForBundle.";
1495 return nullptr;
1496 }
1497 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1498 RTC_LOG(LS_ERROR)
1499 << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1500 return nullptr;
1501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 }
1503 }
Steve Antone831b8c2018-02-01 12:22:16 -08001504
1505 // The following determines how to signal MSIDs to ensure compatibility with
1506 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001507 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001508 // Be conservative and signal using both a=msid and a=ssrc lines. Unified
1509 // Plan answerers will look at a=msid and Plan B answerers will look at the
1510 // a=ssrc MSID line.
1511 offer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1512 cricket::kMsidSignalingSsrcAttribute);
1513 } else {
1514 // Plan B always signals MSID using a=ssrc lines.
1515 offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1516 }
1517
Johannes Kron89f874e2018-11-12 10:25:48 +01001518 offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed);
1519
Steve Anton6fe1fba2018-12-11 10:15:23 -08001520 return offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521}
1522
Steve Anton6fe1fba2018-12-11 10:15:23 -08001523std::unique_ptr<SessionDescription>
1524MediaSessionDescriptionFactory::CreateAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07001525 const SessionDescription* offer,
1526 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 const SessionDescription* current_description) const {
deadbeefb7892532017-02-22 19:35:18 -08001528 if (!offer) {
1529 return nullptr;
1530 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001531
Steve Anton5c72e712018-12-10 14:25:30 -08001532 // Must have options for exactly as many sections as in the offer.
1533 RTC_DCHECK_EQ(offer->contents().size(),
1534 session_options.media_description_options.size());
1535
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001536 IceCredentialsIterator ice_credentials(
1537 session_options.pooled_ice_credentials);
1538
Steve Anton5c72e712018-12-10 14:25:30 -08001539 std::vector<const ContentInfo*> current_active_contents;
1540 if (current_description) {
1541 current_active_contents =
1542 GetActiveContents(*current_description, session_options);
1543 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544
Steve Anton5c72e712018-12-10 14:25:30 -08001545 StreamParamsVec current_streams =
1546 GetCurrentStreamParams(current_active_contents);
Johannes Kron0854eb62018-10-10 22:33:20 +02001547
zhihuang1c378ed2017-08-17 14:10:50 -07001548 // Get list of all possible codecs that respects existing payload type
1549 // mappings and uses a single payload type space.
1550 //
1551 // Note that these lists may be further filtered for each m= section; this
1552 // step is done just to establish the payload type mappings shared by all
1553 // sections.
1554 AudioCodecs answer_audio_codecs;
1555 VideoCodecs answer_video_codecs;
1556 DataCodecs answer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001557 GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001558 &answer_video_codecs, &answer_data_codecs);
1559
1560 if (!session_options.vad_enabled) {
1561 // If application doesn't want CN codecs in answer.
1562 StripCNCodecs(&answer_audio_codecs);
1563 }
1564 FilterDataCodecs(&answer_data_codecs,
1565 session_options.data_channel_type == DCT_SCTP);
1566
Steve Anton5c72e712018-12-10 14:25:30 -08001567 auto answer = absl::make_unique<SessionDescription>();
1568
1569 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1570 // group in the answer with the appropriate content names.
1571 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1572 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1573 // Transport info shared by the bundle group.
1574 std::unique_ptr<TransportInfo> bundle_transport;
1575
1576 answer->set_extmap_allow_mixed(offer->extmap_allow_mixed());
1577
zhihuang1c378ed2017-08-17 14:10:50 -07001578 // Iterate through the media description options, matching with existing
1579 // media descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001580 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001581 for (const MediaDescriptionOptions& media_description_options :
1582 session_options.media_description_options) {
1583 const ContentInfo* offer_content = &offer->contents()[msection_index];
1584 // Media types and MIDs must match between the remote offer and the
1585 // MediaDescriptionOptions.
1586 RTC_DCHECK(
1587 IsMediaContentOfType(offer_content, media_description_options.type));
1588 RTC_DCHECK(media_description_options.mid == offer_content->name);
1589 const ContentInfo* current_content = nullptr;
1590 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001591 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001592 current_content = &current_description->contents()[msection_index];
deadbeefb7892532017-02-22 19:35:18 -08001593 }
zhihuang1c378ed2017-08-17 14:10:50 -07001594 switch (media_description_options.type) {
1595 case MEDIA_TYPE_AUDIO:
1596 if (!AddAudioContentForAnswer(
1597 media_description_options, session_options, offer_content,
1598 offer, current_content, current_description,
1599 bundle_transport.get(), answer_audio_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001600 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001601 return nullptr;
1602 }
1603 break;
1604 case MEDIA_TYPE_VIDEO:
1605 if (!AddVideoContentForAnswer(
1606 media_description_options, session_options, offer_content,
1607 offer, current_content, current_description,
1608 bundle_transport.get(), answer_video_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001609 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001610 return nullptr;
1611 }
1612 break;
1613 case MEDIA_TYPE_DATA:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001614 if (!AddDataContentForAnswer(
1615 media_description_options, session_options, offer_content,
1616 offer, current_content, current_description,
1617 bundle_transport.get(), answer_data_codecs, &current_streams,
1618 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001619 return nullptr;
1620 }
1621 break;
1622 default:
1623 RTC_NOTREACHED();
1624 }
1625 ++msection_index;
deadbeefb7892532017-02-22 19:35:18 -08001626 // See if we can add the newly generated m= section to the BUNDLE group in
1627 // the answer.
1628 ContentInfo& added = answer->contents().back();
zhihuang1c378ed2017-08-17 14:10:50 -07001629 if (!added.rejected && session_options.bundle_enabled && offer_bundle &&
deadbeefb7892532017-02-22 19:35:18 -08001630 offer_bundle->HasContentName(added.name)) {
1631 answer_bundle.AddContentName(added.name);
1632 bundle_transport.reset(
1633 new TransportInfo(*answer->GetTransportInfoByName(added.name)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 }
1636
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001637 // If a BUNDLE group was offered, put a BUNDLE group in the answer even if
1638 // it's empty. RFC5888 says:
1639 //
1640 // A SIP entity that receives an offer that contains an "a=group" line
1641 // with semantics that are understood MUST return an answer that
1642 // contains an "a=group" line with the same semantics.
1643 if (offer_bundle) {
deadbeefb7892532017-02-22 19:35:18 -08001644 answer->AddGroup(answer_bundle);
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001645 }
deadbeefb7892532017-02-22 19:35:18 -08001646
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001647 if (answer_bundle.FirstContentName()) {
deadbeefb7892532017-02-22 19:35:18 -08001648 // Share the same ICE credentials and crypto params across all contents,
1649 // as BUNDLE requires.
1650 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001651 RTC_LOG(LS_ERROR)
1652 << "CreateAnswer failed to UpdateTransportInfoForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001653 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655
deadbeefb7892532017-02-22 19:35:18 -08001656 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001657 RTC_LOG(LS_ERROR)
1658 << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001659 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 }
1661 }
1662
Steve Antone831b8c2018-02-01 12:22:16 -08001663 // The following determines how to signal MSIDs to ensure compatibility with
1664 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001665 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001666 // Unified Plan needs to look at what the offer included to find the most
1667 // compatible answer.
1668 if (offer->msid_signaling() == 0) {
1669 // We end up here in one of three cases:
1670 // 1. An empty offer. We'll reply with an empty answer so it doesn't
1671 // matter what we pick here.
1672 // 2. A data channel only offer. We won't add any MSIDs to the answer so
1673 // it also doesn't matter what we pick here.
1674 // 3. Media that's either sendonly or inactive from the remote endpoint.
1675 // We don't have any information to say whether the endpoint is Plan B
1676 // or Unified Plan, so be conservative and send both.
1677 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1678 cricket::kMsidSignalingSsrcAttribute);
1679 } else if (offer->msid_signaling() ==
1680 (cricket::kMsidSignalingMediaSection |
1681 cricket::kMsidSignalingSsrcAttribute)) {
1682 // If both a=msid and a=ssrc MSID signaling methods were used, we're
1683 // probably talking to a Unified Plan endpoint so respond with just
1684 // a=msid.
1685 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection);
1686 } else {
1687 // Otherwise, it's clear which method the offerer is using so repeat that
1688 // back to them.
1689 answer->set_msid_signaling(offer->msid_signaling());
1690 }
1691 } else {
1692 // Plan B always signals MSID using a=ssrc lines.
1693 answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1694 }
1695
Steve Anton6fe1fba2018-12-11 10:15:23 -08001696 return answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697}
1698
ossu075af922016-06-14 03:29:38 -07001699const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer(
1700 const RtpTransceiverDirection& direction) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001701 switch (direction) {
1702 // If stream is inactive - generate list as if sendrecv.
1703 case RtpTransceiverDirection::kSendRecv:
1704 case RtpTransceiverDirection::kInactive:
1705 return audio_sendrecv_codecs_;
1706 case RtpTransceiverDirection::kSendOnly:
1707 return audio_send_codecs_;
1708 case RtpTransceiverDirection::kRecvOnly:
1709 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001710 }
Steve Anton1d03a752017-11-27 14:30:09 -08001711 RTC_NOTREACHED();
1712 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001713}
1714
1715const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer(
1716 const RtpTransceiverDirection& offer,
1717 const RtpTransceiverDirection& answer) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001718 switch (answer) {
1719 // For inactive and sendrecv answers, generate lists as if we were to accept
1720 // the offer's direction. See RFC 3264 Section 6.1.
1721 case RtpTransceiverDirection::kSendRecv:
1722 case RtpTransceiverDirection::kInactive:
1723 return GetAudioCodecsForOffer(
1724 webrtc::RtpTransceiverDirectionReversed(offer));
1725 case RtpTransceiverDirection::kSendOnly:
ossu075af922016-06-14 03:29:38 -07001726 return audio_send_codecs_;
Steve Anton1d03a752017-11-27 14:30:09 -08001727 case RtpTransceiverDirection::kRecvOnly:
1728 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001729 }
Steve Anton1d03a752017-11-27 14:30:09 -08001730 RTC_NOTREACHED();
1731 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001732}
1733
Steve Anton5c72e712018-12-10 14:25:30 -08001734void MergeCodecsFromDescription(
1735 const std::vector<const ContentInfo*>& current_active_contents,
1736 AudioCodecs* audio_codecs,
1737 VideoCodecs* video_codecs,
1738 DataCodecs* data_codecs,
1739 UsedPayloadTypes* used_pltypes) {
1740 for (const ContentInfo* content : current_active_contents) {
1741 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001742 const AudioContentDescription* audio =
Steve Anton5c72e712018-12-10 14:25:30 -08001743 content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001744 MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001745 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001746 const VideoContentDescription* video =
Steve Anton5c72e712018-12-10 14:25:30 -08001747 content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001748 MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001749 } else if (IsMediaContentOfType(content, MEDIA_TYPE_DATA)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001750 const DataContentDescription* data =
Steve Anton5c72e712018-12-10 14:25:30 -08001751 content->media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001752 MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes);
1753 }
1754 }
1755}
1756
1757// Getting codecs for an offer involves these steps:
1758//
1759// 1. Construct payload type -> codec mappings for current description.
1760// 2. Add any reference codecs that weren't already present
1761// 3. For each individual media description (m= section), filter codecs based
1762// on the directional attribute (happens in another method).
1763void MediaSessionDescriptionFactory::GetCodecsForOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001764 const std::vector<const ContentInfo*>& current_active_contents,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 AudioCodecs* audio_codecs,
1766 VideoCodecs* video_codecs,
1767 DataCodecs* data_codecs) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 // First - get all codecs from the current description if the media type
zhihuang1c378ed2017-08-17 14:10:50 -07001769 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1770 // new media type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001771 UsedPayloadTypes used_pltypes;
1772 MergeCodecsFromDescription(current_active_contents, audio_codecs,
1773 video_codecs, data_codecs, &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774
Steve Anton5c72e712018-12-10 14:25:30 -08001775 // Add our codecs that are not in the current description.
zhihuang1c378ed2017-08-17 14:10:50 -07001776 MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes);
1777 MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1778 MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1779}
1780
1781// Getting codecs for an answer involves these steps:
1782//
1783// 1. Construct payload type -> codec mappings for current description.
1784// 2. Add any codecs from the offer that weren't already present.
1785// 3. Add any remaining codecs that weren't already present.
1786// 4. For each individual media description (m= section), filter codecs based
1787// on the directional attribute (happens in another method).
1788void MediaSessionDescriptionFactory::GetCodecsForAnswer(
Steve Anton5c72e712018-12-10 14:25:30 -08001789 const std::vector<const ContentInfo*>& current_active_contents,
1790 const SessionDescription& remote_offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001791 AudioCodecs* audio_codecs,
1792 VideoCodecs* video_codecs,
1793 DataCodecs* data_codecs) const {
zhihuang1c378ed2017-08-17 14:10:50 -07001794 // First - get all codecs from the current description if the media type
1795 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1796 // new media type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001797 UsedPayloadTypes used_pltypes;
1798 MergeCodecsFromDescription(current_active_contents, audio_codecs,
1799 video_codecs, data_codecs, &used_pltypes);
zhihuang1c378ed2017-08-17 14:10:50 -07001800
1801 // Second - filter out codecs that we don't support at all and should ignore.
1802 AudioCodecs filtered_offered_audio_codecs;
1803 VideoCodecs filtered_offered_video_codecs;
1804 DataCodecs filtered_offered_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001805 for (const ContentInfo& content : remote_offer.contents()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001806 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1807 const AudioContentDescription* audio =
Steve Antonb1c1de12017-12-21 15:14:30 -08001808 content.media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001809 for (const AudioCodec& offered_audio_codec : audio->codecs()) {
1810 if (!FindMatchingCodec<AudioCodec>(audio->codecs(),
1811 filtered_offered_audio_codecs,
1812 offered_audio_codec, nullptr) &&
1813 FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_,
1814 offered_audio_codec, nullptr)) {
1815 filtered_offered_audio_codecs.push_back(offered_audio_codec);
1816 }
1817 }
1818 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1819 const VideoContentDescription* video =
Steve Antonb1c1de12017-12-21 15:14:30 -08001820 content.media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001821 for (const VideoCodec& offered_video_codec : video->codecs()) {
1822 if (!FindMatchingCodec<VideoCodec>(video->codecs(),
1823 filtered_offered_video_codecs,
1824 offered_video_codec, nullptr) &&
1825 FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_,
1826 offered_video_codec, nullptr)) {
1827 filtered_offered_video_codecs.push_back(offered_video_codec);
1828 }
1829 }
1830 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1831 const DataContentDescription* data =
Steve Antonb1c1de12017-12-21 15:14:30 -08001832 content.media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001833 for (const DataCodec& offered_data_codec : data->codecs()) {
1834 if (!FindMatchingCodec<DataCodec>(data->codecs(),
1835 filtered_offered_data_codecs,
1836 offered_data_codec, nullptr) &&
1837 FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_,
1838 offered_data_codec, nullptr)) {
1839 filtered_offered_data_codecs.push_back(offered_data_codec);
1840 }
1841 }
1842 }
1843 }
1844
Steve Anton5c72e712018-12-10 14:25:30 -08001845 // Add codecs that are not in the current description but were in
zhihuang1c378ed2017-08-17 14:10:50 -07001846 // |remote_offer|.
1847 MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs,
1848 &used_pltypes);
1849 MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs,
1850 &used_pltypes);
1851 MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs,
1852 &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853}
1854
1855void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001856 const std::vector<const ContentInfo*>& current_active_contents,
zhihuang1c378ed2017-08-17 14:10:50 -07001857 RtpHeaderExtensions* offer_audio_extensions,
1858 RtpHeaderExtensions* offer_video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001859 // All header extensions allocated from the same range to avoid potential
1860 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 UsedRtpHeaderExtensionIds used_ids;
jbauch5869f502017-06-29 12:31:36 -07001862 RtpHeaderExtensions all_regular_extensions;
1863 RtpHeaderExtensions all_encrypted_extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864
1865 // First - get all extensions from the current description if the media type
1866 // is used.
1867 // Add them to |used_ids| so the local ids are not reused if a new media
1868 // type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001869 for (const ContentInfo* content : current_active_contents) {
1870 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
1871 const AudioContentDescription* audio =
1872 content->media_description()->as_audio();
1873 MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions,
1874 &all_regular_extensions, &all_encrypted_extensions,
1875 &used_ids);
1876 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
1877 const VideoContentDescription* video =
1878 content->media_description()->as_video();
1879 MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions,
1880 &all_regular_extensions, &all_encrypted_extensions,
1881 &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 }
1883 }
1884
Steve Anton5c72e712018-12-10 14:25:30 -08001885 // Add our default RTP header extensions that are not in the current
1886 // description.
Steve Anton8f66ddb2018-12-10 16:08:05 -08001887 MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions,
1888 &all_regular_extensions, &all_encrypted_extensions,
1889 &used_ids);
1890 MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions,
1891 &all_regular_extensions, &all_encrypted_extensions,
1892 &used_ids);
zhihuang1c378ed2017-08-17 14:10:50 -07001893
jbauch5869f502017-06-29 12:31:36 -07001894 // TODO(jbauch): Support adding encrypted header extensions to existing
1895 // sessions.
Steve Anton5c72e712018-12-10 14:25:30 -08001896 if (enable_encrypted_rtp_header_extensions_ &&
1897 current_active_contents.empty()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001898 AddEncryptedVersionsOfHdrExts(offer_audio_extensions,
1899 &all_encrypted_extensions, &used_ids);
1900 AddEncryptedVersionsOfHdrExts(offer_video_extensions,
1901 &all_encrypted_extensions, &used_ids);
jbauch5869f502017-06-29 12:31:36 -07001902 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903}
1904
1905bool MediaSessionDescriptionFactory::AddTransportOffer(
Yves Gerey665174f2018-06-19 15:03:05 +02001906 const std::string& content_name,
1907 const TransportOptions& transport_options,
1908 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001909 SessionDescription* offer_desc,
1910 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 if (!transport_desc_factory_)
Yves Gerey665174f2018-06-19 15:03:05 +02001912 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 const TransportDescription* current_tdesc =
1914 GetTransportDescription(content_name, current_desc);
kwiberg31022942016-03-11 14:18:21 -08001915 std::unique_ptr<TransportDescription> new_tdesc(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001916 transport_desc_factory_->CreateOffer(transport_options, current_tdesc,
1917 ice_credentials));
Steve Anton06817cd2018-12-18 15:55:30 -08001918 if (!new_tdesc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001919 RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name="
1920 << content_name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 }
Steve Anton06817cd2018-12-18 15:55:30 -08001922 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc));
1923 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924}
1925
Steve Anton1a9d3c32018-12-10 17:18:54 -08001926std::unique_ptr<TransportDescription>
1927MediaSessionDescriptionFactory::CreateTransportAnswer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928 const std::string& content_name,
1929 const SessionDescription* offer_desc,
1930 const TransportOptions& transport_options,
deadbeefb7892532017-02-22 19:35:18 -08001931 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001932 bool require_transport_attributes,
1933 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 if (!transport_desc_factory_)
1935 return NULL;
1936 const TransportDescription* offer_tdesc =
1937 GetTransportDescription(content_name, offer_desc);
1938 const TransportDescription* current_tdesc =
1939 GetTransportDescription(content_name, current_desc);
deadbeefb7892532017-02-22 19:35:18 -08001940 return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1941 require_transport_attributes,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001942 current_tdesc, ice_credentials);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943}
1944
1945bool MediaSessionDescriptionFactory::AddTransportAnswer(
1946 const std::string& content_name,
1947 const TransportDescription& transport_desc,
1948 SessionDescription* answer_desc) const {
Steve Anton06817cd2018-12-18 15:55:30 -08001949 answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 return true;
1951}
1952
zhihuang1c378ed2017-08-17 14:10:50 -07001953// |audio_codecs| = set of all possible codecs that can be used, with correct
1954// payload type mappings
1955//
1956// |supported_audio_codecs| = set of codecs that are supported for the direction
1957// of this m= section
1958//
1959// acd->codecs() = set of previously negotiated codecs for this m= section
1960//
1961// The payload types should come from audio_codecs, but the order should come
1962// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
1963// change existing codec priority, and that new codecs are added with the right
1964// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001965bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001966 const MediaDescriptionOptions& media_description_options,
1967 const MediaSessionOptions& session_options,
1968 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001969 const SessionDescription* current_description,
1970 const RtpHeaderExtensions& audio_rtp_extensions,
1971 const AudioCodecs& audio_codecs,
1972 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001973 SessionDescription* desc,
1974 IceCredentialsIterator* ice_credentials) const {
zhihuang1c378ed2017-08-17 14:10:50 -07001975 // Filter audio_codecs (which includes all codecs, with correctly remapped
1976 // payload types) based on transceiver direction.
1977 const AudioCodecs& supported_audio_codecs =
1978 GetAudioCodecsForOffer(media_description_options.direction);
1979
1980 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001981 // Add the codecs from current content if it exists and is not rejected nor
1982 // recycled.
1983 if (current_content && !current_content->rejected &&
1984 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07001985 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07001986 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08001987 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001988 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07001989 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
1990 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001991 filtered_codecs.push_back(codec);
1992 }
1993 }
1994 }
1995 // Add other supported audio codecs.
1996 AudioCodec found_codec;
1997 for (const AudioCodec& codec : supported_audio_codecs) {
1998 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
1999 codec, &found_codec) &&
2000 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2001 codec, nullptr)) {
2002 // Use the |found_codec| from |audio_codecs| because it has the correctly
2003 // mapped payload type.
2004 filtered_codecs.push_back(found_codec);
2005 }
2006 }
deadbeef44f08192015-12-15 16:20:09 -08002007
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002008 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002009 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2010 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002011
kwiberg31022942016-03-11 14:18:21 -08002012 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002013 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002014 GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options,
2015 &crypto_suites);
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002016 if (!CreateMediaContentOffer(media_description_options, session_options,
2017 filtered_codecs, sdes_policy,
2018 GetCryptos(current_content), crypto_suites,
2019 audio_rtp_extensions, ssrc_generator_,
2020 current_streams, audio.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002021 return false;
2022 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002023
2024 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2025 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002026
Steve Anton4e70a722017-11-28 14:57:10 -08002027 audio->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002028
Steve Anton5adfafd2017-12-20 16:34:00 -08002029 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002030 media_description_options.stopped, audio.release());
2031 if (!AddTransportOffer(media_description_options.mid,
2032 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002033 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002034 return false;
2035 }
2036
2037 return true;
2038}
2039
2040bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002041 const MediaDescriptionOptions& media_description_options,
2042 const MediaSessionOptions& session_options,
2043 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002044 const SessionDescription* current_description,
2045 const RtpHeaderExtensions& video_rtp_extensions,
2046 const VideoCodecs& video_codecs,
2047 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002048 SessionDescription* desc,
2049 IceCredentialsIterator* ice_credentials) const {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002050 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002051 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2052 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002053
kwiberg31022942016-03-11 14:18:21 -08002054 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002055 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002056 GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options,
2057 &crypto_suites);
2058
2059 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002060 // Add the codecs from current content if it exists and is not rejected nor
2061 // recycled.
2062 if (current_content && !current_content->rejected &&
2063 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002064 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002065 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002066 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002067 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002068 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002069 nullptr)) {
2070 filtered_codecs.push_back(codec);
2071 }
2072 }
2073 }
2074 // Add other supported video codecs.
2075 VideoCodec found_codec;
2076 for (const VideoCodec& codec : video_codecs_) {
2077 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
2078 &found_codec) &&
2079 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2080 nullptr)) {
2081 // Use the |found_codec| from |video_codecs| because it has the correctly
2082 // mapped payload type.
2083 filtered_codecs.push_back(found_codec);
2084 }
2085 }
2086
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002087 if (!CreateMediaContentOffer(media_description_options, session_options,
2088 filtered_codecs, sdes_policy,
2089 GetCryptos(current_content), crypto_suites,
2090 video_rtp_extensions, ssrc_generator_,
2091 current_streams, video.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002092 return false;
2093 }
2094
zhihuang1c378ed2017-08-17 14:10:50 -07002095 video->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002096
2097 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2098 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002099
Steve Anton4e70a722017-11-28 14:57:10 -08002100 video->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002101
Steve Anton5adfafd2017-12-20 16:34:00 -08002102 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002103 media_description_options.stopped, video.release());
2104 if (!AddTransportOffer(media_description_options.mid,
2105 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002106 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002107 return false;
2108 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002109 return true;
2110}
2111
2112bool MediaSessionDescriptionFactory::AddDataContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002113 const MediaDescriptionOptions& media_description_options,
2114 const MediaSessionOptions& session_options,
2115 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002116 const SessionDescription* current_description,
zhihuang1c378ed2017-08-17 14:10:50 -07002117 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002118 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002119 SessionDescription* desc,
2120 IceCredentialsIterator* ice_credentials) const {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002121 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2122
kwiberg31022942016-03-11 14:18:21 -08002123 std::unique_ptr<DataContentDescription> data(new DataContentDescription());
zhihuang1c378ed2017-08-17 14:10:50 -07002124 bool is_sctp = (session_options.data_channel_type == DCT_SCTP);
2125 // If the DataChannel type is not specified, use the DataChannel type in
2126 // the current description.
2127 if (session_options.data_channel_type == DCT_NONE && current_content) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002128 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA));
Steve Antonb1c1de12017-12-21 15:14:30 -08002129 is_sctp = (current_content->media_description()->protocol() ==
2130 kMediaProtocolSctp);
zhihuang1c378ed2017-08-17 14:10:50 -07002131 }
deadbeef44f08192015-12-15 16:20:09 -08002132
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002133 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002134 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2135 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002136 std::vector<std::string> crypto_suites;
2137 if (is_sctp) {
2138 // SDES doesn't make sense for SCTP, so we disable it, and we only
2139 // get SDES crypto suites for RTP-based data channels.
2140 sdes_policy = cricket::SEC_DISABLED;
2141 // Unlike SetMediaProtocol below, we need to set the protocol
2142 // before we call CreateMediaContentOffer. Otherwise,
2143 // CreateMediaContentOffer won't know this is SCTP and will
2144 // generate SSRCs rather than SIDs.
deadbeef8b7e9ad2017-05-25 09:38:55 -07002145 // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once
2146 // it's safe to do so. Older versions of webrtc would reject these
2147 // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706.
Yves Gerey665174f2018-06-19 15:03:05 +02002148 data->set_protocol(secure_transport ? kMediaProtocolDtlsSctp
2149 : kMediaProtocolSctp);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002150 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002151 GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -07002152 &crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002153 }
2154
zhihuang1c378ed2017-08-17 14:10:50 -07002155 // Even SCTP uses a "codec".
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002156 if (!CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08002157 media_description_options, session_options, data_codecs, sdes_policy,
2158 GetCryptos(current_content), crypto_suites, RtpHeaderExtensions(),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002159 ssrc_generator_, current_streams, data.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002160 return false;
2161 }
2162
2163 if (is_sctp) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002164 desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp,
zhihuang1c378ed2017-08-17 14:10:50 -07002165 data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002166 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002167 data->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002168 SetMediaProtocol(secure_transport, data.get());
Steve Anton5adfafd2017-12-20 16:34:00 -08002169 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002170 media_description_options.stopped, data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002171 }
zhihuang1c378ed2017-08-17 14:10:50 -07002172 if (!AddTransportOffer(media_description_options.mid,
2173 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002174 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002175 return false;
2176 }
2177 return true;
2178}
2179
zhihuang1c378ed2017-08-17 14:10:50 -07002180// |audio_codecs| = set of all possible codecs that can be used, with correct
2181// payload type mappings
2182//
2183// |supported_audio_codecs| = set of codecs that are supported for the direction
2184// of this m= section
2185//
2186// acd->codecs() = set of previously negotiated codecs for this m= section
2187//
2188// The payload types should come from audio_codecs, but the order should come
2189// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
2190// change existing codec priority, and that new codecs are added with the right
2191// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002192bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002193 const MediaDescriptionOptions& media_description_options,
2194 const MediaSessionOptions& session_options,
2195 const ContentInfo* offer_content,
2196 const SessionDescription* offer_description,
2197 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002198 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002199 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002200 const AudioCodecs& audio_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002201 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002202 SessionDescription* answer,
2203 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002204 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002205 const AudioContentDescription* offer_audio_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002206 offer_content->media_description()->as_audio();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002207
Steve Anton1a9d3c32018-12-10 17:18:54 -08002208 std::unique_ptr<TransportDescription> audio_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002209 media_description_options.mid, offer_description,
2210 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002211 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002212 if (!audio_transport) {
2213 return false;
2214 }
2215
zhihuang1c378ed2017-08-17 14:10:50 -07002216 // Pick codecs based on the requested communications direction in the offer
2217 // and the selected direction in the answer.
2218 // Note these will be filtered one final time in CreateMediaContentAnswer.
2219 auto wants_rtd = media_description_options.direction;
Steve Anton4e70a722017-11-28 14:57:10 -08002220 auto offer_rtd = offer_audio_description->direction();
ossu075af922016-06-14 03:29:38 -07002221 auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd);
zhihuang1c378ed2017-08-17 14:10:50 -07002222 AudioCodecs supported_audio_codecs =
2223 GetAudioCodecsForAnswer(offer_rtd, answer_rtd);
2224
2225 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002226 // Add the codecs from current content if it exists and is not rejected nor
2227 // recycled.
2228 if (current_content && !current_content->rejected &&
2229 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002230 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002231 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002232 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002233 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002234 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2235 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002236 filtered_codecs.push_back(codec);
2237 }
2238 }
2239 }
2240 // Add other supported audio codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002241 for (const AudioCodec& codec : supported_audio_codecs) {
2242 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
Zhi Huang6f367472017-11-22 13:20:02 -08002243 codec, nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002244 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2245 codec, nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002246 // We should use the local codec with local parameters and the codec id
2247 // would be correctly mapped in |NegotiateCodecs|.
2248 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002249 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002250 }
2251
zhihuang1c378ed2017-08-17 14:10:50 -07002252 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2253 session_options.bundle_enabled;
kwiberg31022942016-03-11 14:18:21 -08002254 std::unique_ptr<AudioContentDescription> audio_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002255 new AudioContentDescription());
2256 // Do not require or create SDES cryptos if DTLS is used.
2257 cricket::SecurePolicy sdes_policy =
2258 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
2259 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002260 offer_audio_description, media_description_options, session_options,
2261 filtered_codecs, sdes_policy, GetCryptos(current_content),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002262 audio_rtp_header_extensions(), ssrc_generator_,
Steve Anton1b8773d2018-04-06 11:13:34 -07002263 enable_encrypted_rtp_header_extensions_, current_streams,
2264 bundle_enabled, audio_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002265 return false; // Fails the session setup.
2266 }
2267
deadbeefb7892532017-02-22 19:35:18 -08002268 bool secure = bundle_transport ? bundle_transport->description.secure()
2269 : audio_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002270 bool rejected = media_description_options.stopped ||
2271 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002272 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
2273 audio_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002274 if (!AddTransportAnswer(media_description_options.mid,
2275 *(audio_transport.get()), answer)) {
2276 return false;
2277 }
2278
2279 if (rejected) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002280 RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid
2281 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002282 }
2283
zhihuang1c378ed2017-08-17 14:10:50 -07002284 answer->AddContent(media_description_options.mid, offer_content->type,
2285 rejected, audio_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002286 return true;
2287}
2288
2289bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002290 const MediaDescriptionOptions& media_description_options,
2291 const MediaSessionOptions& session_options,
2292 const ContentInfo* offer_content,
2293 const SessionDescription* offer_description,
2294 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002295 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002296 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002297 const VideoCodecs& video_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002298 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002299 SessionDescription* answer,
2300 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002301 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002302 const VideoContentDescription* offer_video_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002303 offer_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002304
Steve Anton1a9d3c32018-12-10 17:18:54 -08002305 std::unique_ptr<TransportDescription> video_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002306 media_description_options.mid, offer_description,
2307 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002308 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002309 if (!video_transport) {
2310 return false;
2311 }
2312
zhihuang1c378ed2017-08-17 14:10:50 -07002313 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002314 // Add the codecs from current content if it exists and is not rejected nor
2315 // recycled.
2316 if (current_content && !current_content->rejected &&
2317 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002318 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002319 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002320 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002321 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002322 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002323 nullptr)) {
2324 filtered_codecs.push_back(codec);
2325 }
2326 }
2327 }
2328 // Add other supported video codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002329 for (const VideoCodec& codec : video_codecs_) {
2330 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
Zhi Huang6f367472017-11-22 13:20:02 -08002331 nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002332 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2333 nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002334 // We should use the local codec with local parameters and the codec id
2335 // would be correctly mapped in |NegotiateCodecs|.
2336 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002337 }
2338 }
2339
2340 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2341 session_options.bundle_enabled;
2342
kwiberg31022942016-03-11 14:18:21 -08002343 std::unique_ptr<VideoContentDescription> video_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002344 new VideoContentDescription());
2345 // Do not require or create SDES cryptos if DTLS is used.
2346 cricket::SecurePolicy sdes_policy =
2347 video_transport->secure() ? cricket::SEC_DISABLED : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002348 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002349 offer_video_description, media_description_options, session_options,
2350 filtered_codecs, sdes_policy, GetCryptos(current_content),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002351 video_rtp_header_extensions(), ssrc_generator_,
Steve Anton1b8773d2018-04-06 11:13:34 -07002352 enable_encrypted_rtp_header_extensions_, current_streams,
2353 bundle_enabled, video_answer.get())) {
zhihuang1c378ed2017-08-17 14:10:50 -07002354 return false; // Failed the sessin setup.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002355 }
deadbeefb7892532017-02-22 19:35:18 -08002356 bool secure = bundle_transport ? bundle_transport->description.secure()
2357 : video_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002358 bool rejected = media_description_options.stopped ||
2359 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002360 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
2361 video_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002362 if (!AddTransportAnswer(media_description_options.mid,
2363 *(video_transport.get()), answer)) {
2364 return false;
2365 }
2366
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002367 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002368 video_answer->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002369 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002370 RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid
2371 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002372 }
zhihuang1c378ed2017-08-17 14:10:50 -07002373 answer->AddContent(media_description_options.mid, offer_content->type,
2374 rejected, video_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002375 return true;
2376}
2377
2378bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002379 const MediaDescriptionOptions& media_description_options,
2380 const MediaSessionOptions& session_options,
2381 const ContentInfo* offer_content,
2382 const SessionDescription* offer_description,
2383 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002384 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002385 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002386 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002387 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002388 SessionDescription* answer,
2389 IceCredentialsIterator* ice_credentials) const {
Steve Anton1a9d3c32018-12-10 17:18:54 -08002390 std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002391 media_description_options.mid, offer_description,
2392 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002393 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002394 if (!data_transport) {
2395 return false;
2396 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002397
kwiberg31022942016-03-11 14:18:21 -08002398 std::unique_ptr<DataContentDescription> data_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002399 new DataContentDescription());
2400 // Do not require or create SDES cryptos if DTLS is used.
2401 cricket::SecurePolicy sdes_policy =
2402 data_transport->secure() ? cricket::SEC_DISABLED : secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002403 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2404 session_options.bundle_enabled;
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002405 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA));
2406 const DataContentDescription* offer_data_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002407 offer_content->media_description()->as_data();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002408 if (!CreateMediaContentAnswer(
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002409 offer_data_description, media_description_options, session_options,
2410 data_codecs, sdes_policy, GetCryptos(current_content),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002411 RtpHeaderExtensions(), ssrc_generator_,
2412 enable_encrypted_rtp_header_extensions_, current_streams,
2413 bundle_enabled, data_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002414 return false; // Fails the session setup.
2415 }
2416
zstein4b2e0822017-02-17 19:48:38 -08002417 // Respond with sctpmap if the offer uses sctpmap.
zstein4b2e0822017-02-17 19:48:38 -08002418 bool offer_uses_sctpmap = offer_data_description->use_sctpmap();
2419 data_answer->set_use_sctpmap(offer_uses_sctpmap);
2420
deadbeefb7892532017-02-22 19:35:18 -08002421 bool secure = bundle_transport ? bundle_transport->description.secure()
2422 : data_transport->secure();
2423
zhihuang1c378ed2017-08-17 14:10:50 -07002424 bool rejected = session_options.data_channel_type == DCT_NONE ||
2425 media_description_options.stopped ||
2426 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002427 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
2428 data_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002429 if (!AddTransportAnswer(media_description_options.mid,
2430 *(data_transport.get()), answer)) {
2431 return false;
2432 }
2433
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002434 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002435 data_answer->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002436 } else {
2437 // RFC 3264
2438 // The answer MUST contain the same number of m-lines as the offer.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002439 RTC_LOG(LS_INFO) << "Data is not supported in the answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002440 }
zhihuang1c378ed2017-08-17 14:10:50 -07002441 answer->AddContent(media_description_options.mid, offer_content->type,
2442 rejected, data_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002443 return true;
2444}
2445
zhihuang1c378ed2017-08-17 14:10:50 -07002446void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() {
2447 audio_sendrecv_codecs_.clear();
2448 all_audio_codecs_.clear();
2449 // Compute the audio codecs union.
2450 for (const AudioCodec& send : audio_send_codecs_) {
2451 all_audio_codecs_.push_back(send);
2452 if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_,
2453 send, nullptr)) {
2454 // It doesn't make sense to have an RTX codec we support sending but not
2455 // receiving.
2456 RTC_DCHECK(!IsRtxCodec(send));
2457 }
2458 }
2459 for (const AudioCodec& recv : audio_recv_codecs_) {
2460 if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_,
2461 recv, nullptr)) {
2462 all_audio_codecs_.push_back(recv);
2463 }
2464 }
2465 // Use NegotiateCodecs to merge our codec lists, since the operation is
2466 // essentially the same. Put send_codecs as the offered_codecs, which is the
2467 // order we'd like to follow. The reasoning is that encoding is usually more
2468 // expensive than decoding, and prioritizing a codec in the send list probably
2469 // means it's a codec we can handle efficiently.
2470 NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_,
2471 &audio_sendrecv_codecs_);
2472}
2473
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474bool IsMediaContent(const ContentInfo* content) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002475 return (content && (content->type == MediaProtocolType::kRtp ||
2476 content->type == MediaProtocolType::kSctp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002477}
2478
2479bool IsAudioContent(const ContentInfo* content) {
2480 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
2481}
2482
2483bool IsVideoContent(const ContentInfo* content) {
2484 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
2485}
2486
2487bool IsDataContent(const ContentInfo* content) {
2488 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
2489}
2490
deadbeef0ed85b22016-02-23 17:24:52 -08002491const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
2492 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002493 for (const ContentInfo& content : contents) {
2494 if (IsMediaContentOfType(&content, media_type)) {
2495 return &content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 }
2497 }
deadbeef0ed85b22016-02-23 17:24:52 -08002498 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499}
2500
2501const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
2502 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2503}
2504
2505const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
2506 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2507}
2508
2509const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
2510 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2511}
2512
Steve Antonad7bffc2018-01-22 10:21:56 -08002513const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
2514 MediaType media_type) {
deadbeef0ed85b22016-02-23 17:24:52 -08002515 if (sdesc == nullptr) {
2516 return nullptr;
2517 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002518
2519 return GetFirstMediaContent(sdesc->contents(), media_type);
2520}
2521
2522const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
2523 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2524}
2525
2526const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
2527 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2528}
2529
2530const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
2531 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2532}
2533
2534const MediaContentDescription* GetFirstMediaContentDescription(
Yves Gerey665174f2018-06-19 15:03:05 +02002535 const SessionDescription* sdesc,
2536 MediaType media_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002537 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002538 return (content ? content->media_description() : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002539}
2540
2541const AudioContentDescription* GetFirstAudioContentDescription(
2542 const SessionDescription* sdesc) {
2543 return static_cast<const AudioContentDescription*>(
2544 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2545}
2546
2547const VideoContentDescription* GetFirstVideoContentDescription(
2548 const SessionDescription* sdesc) {
2549 return static_cast<const VideoContentDescription*>(
2550 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2551}
2552
2553const DataContentDescription* GetFirstDataContentDescription(
2554 const SessionDescription* sdesc) {
2555 return static_cast<const DataContentDescription*>(
2556 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2557}
2558
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002559//
2560// Non-const versions of the above functions.
2561//
2562
Steve Anton36b29d12017-10-30 09:57:42 -07002563ContentInfo* GetFirstMediaContent(ContentInfos* contents,
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002564 MediaType media_type) {
Steve Anton36b29d12017-10-30 09:57:42 -07002565 for (ContentInfo& content : *contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002566 if (IsMediaContentOfType(&content, media_type)) {
2567 return &content;
2568 }
2569 }
2570 return nullptr;
2571}
2572
Steve Anton36b29d12017-10-30 09:57:42 -07002573ContentInfo* GetFirstAudioContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002574 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2575}
2576
Steve Anton36b29d12017-10-30 09:57:42 -07002577ContentInfo* GetFirstVideoContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002578 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2579}
2580
Steve Anton36b29d12017-10-30 09:57:42 -07002581ContentInfo* GetFirstDataContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002582 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2583}
2584
Steve Antonad7bffc2018-01-22 10:21:56 -08002585ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
2586 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002587 if (sdesc == nullptr) {
2588 return nullptr;
2589 }
2590
Steve Anton36b29d12017-10-30 09:57:42 -07002591 return GetFirstMediaContent(&sdesc->contents(), media_type);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002592}
2593
2594ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) {
2595 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2596}
2597
2598ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) {
2599 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2600}
2601
2602ContentInfo* GetFirstDataContent(SessionDescription* sdesc) {
2603 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2604}
2605
2606MediaContentDescription* GetFirstMediaContentDescription(
2607 SessionDescription* sdesc,
2608 MediaType media_type) {
2609 ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002610 return (content ? content->media_description() : nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002611}
2612
2613AudioContentDescription* GetFirstAudioContentDescription(
2614 SessionDescription* sdesc) {
2615 return static_cast<AudioContentDescription*>(
2616 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2617}
2618
2619VideoContentDescription* GetFirstVideoContentDescription(
2620 SessionDescription* sdesc) {
2621 return static_cast<VideoContentDescription*>(
2622 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2623}
2624
2625DataContentDescription* GetFirstDataContentDescription(
2626 SessionDescription* sdesc) {
2627 return static_cast<DataContentDescription*>(
2628 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2629}
2630
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002631} // namespace cricket