blob: 99e7e4098de46d1a2abc3aebe8f57fa2247bf145 [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
deadbeef67cf2c12016-04-13 10:07:16 -070013#include <algorithm> // For std::find_if, std::sort.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <functional>
15#include <map>
kwiberg31022942016-03-11 14:18:21 -080016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <set>
deadbeef67cf2c12016-04-13 10:07:16 -070018#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <utility>
20
Steve Anton5c72e712018-12-10 14:25:30 -080021#include "absl/memory/memory.h"
Niels Möller2edab4c2018-10-22 09:48:08 +020022#include "absl/strings/match.h"
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020023#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "api/crypto_params.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "media/base/h264_profile_level_id.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "media/base/media_constants.h"
27#include "p2p/base/p2p_constants.h"
28#include "pc/channel_manager.h"
29#include "pc/rtp_media_utils.h"
30#include "pc/srtp_filter.h"
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080031#include "pc/unique_id_generator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/checks.h"
33#include "rtc_base/helpers.h"
34#include "rtc_base/logging.h"
Artem Titova76af0c2018-07-23 17:38:12 +020035#include "rtc_base/third_party/base64/base64.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37namespace {
Steve Anton1d03a752017-11-27 14:30:09 -080038
39using webrtc::RtpTransceiverDirection;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -080040using webrtc::UniqueRandomIdGenerator;
Steve Anton1d03a752017-11-27 14:30:09 -080041
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042const char kInline[] = "inline:";
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080043
Benjamin Wrighta54daf12018-10-11 15:33:17 -070044void GetSupportedSdesCryptoSuiteNames(
45 void (*func)(const webrtc::CryptoOptions&, std::vector<int>*),
46 const webrtc::CryptoOptions& crypto_options,
47 std::vector<std::string>* names) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080048 std::vector<int> crypto_suites;
jbauchcb560652016-08-04 05:20:32 -070049 func(crypto_options, &crypto_suites);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080050 for (const auto crypto : crypto_suites) {
51 names->push_back(rtc::SrtpCryptoSuiteToName(crypto));
52 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080053}
terelius8c011e52016-04-26 05:28:11 -070054} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055
56namespace cricket {
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// RTP Profile names
59// http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
60// RFC4585
61const char kMediaProtocolAvpf[] = "RTP/AVPF";
62// RFC5124
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +000063const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
64
deadbeeff3938292015-07-15 12:20:53 -070065// We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
66// but we tolerate "RTP/SAVPF" in offers we receive, for compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067const char kMediaProtocolSavpf[] = "RTP/SAVPF";
68
69const char kMediaProtocolRtpPrefix[] = "RTP/";
70
71const char kMediaProtocolSctp[] = "SCTP";
72const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
lally@webrtc.orgec97c652015-02-24 20:18:48 +000073const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
lally@webrtc.orga7470932015-02-24 20:19:21 +000074const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075
deadbeef8b7e9ad2017-05-25 09:38:55 -070076// Note that the below functions support some protocol strings purely for
77// legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names
78// and Interoperability.
79
80static bool IsDtlsRtp(const std::string& protocol) {
81 // Most-likely values first.
82 return protocol == "UDP/TLS/RTP/SAVPF" || protocol == "TCP/TLS/RTP/SAVPF" ||
83 protocol == "UDP/TLS/RTP/SAVP" || protocol == "TCP/TLS/RTP/SAVP";
84}
85
86static bool IsPlainRtp(const std::string& protocol) {
87 // Most-likely values first.
88 return protocol == "RTP/SAVPF" || protocol == "RTP/AVPF" ||
89 protocol == "RTP/SAVP" || protocol == "RTP/AVP";
90}
91
92static bool IsDtlsSctp(const std::string& protocol) {
93 return protocol == kMediaProtocolDtlsSctp ||
94 protocol == kMediaProtocolUdpDtlsSctp ||
95 protocol == kMediaProtocolTcpDtlsSctp;
96}
97
98static bool IsPlainSctp(const std::string& protocol) {
99 return protocol == kMediaProtocolSctp;
100}
101
102static bool IsSctp(const std::string& protocol) {
103 return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
104}
105
Steve Anton1d03a752017-11-27 14:30:09 -0800106static RtpTransceiverDirection NegotiateRtpTransceiverDirection(
107 RtpTransceiverDirection offer,
108 RtpTransceiverDirection wants) {
109 bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer);
110 bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer);
111 bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants);
112 bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants);
113 return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send,
114 offer_send && wants_recv);
ossu075af922016-06-14 03:29:38 -0700115}
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117static bool IsMediaContentOfType(const ContentInfo* content,
118 MediaType media_type) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800119 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120 return false;
121 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800122 return content->media_description()->type() == media_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123}
124
Yves Gerey665174f2018-06-19 15:03:05 +0200125static bool CreateCryptoParams(int tag,
126 const std::string& cipher,
Steve Anton3a66edf2018-09-10 12:57:37 -0700127 CryptoParams* crypto_out) {
jbauchcb560652016-08-04 05:20:32 -0700128 int key_len;
129 int salt_len;
Yves Gerey665174f2018-06-19 15:03:05 +0200130 if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher),
131 &key_len, &salt_len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 return false;
133 }
jbauchcb560652016-08-04 05:20:32 -0700134
135 int master_key_len = key_len + salt_len;
136 std::string master_key;
137 if (!rtc::CreateRandomData(master_key_len, &master_key)) {
138 return false;
139 }
140
kwiberg352444f2016-11-28 15:58:53 -0800141 RTC_CHECK_EQ(master_key_len, master_key.size());
jbauchcb560652016-08-04 05:20:32 -0700142 std::string key = rtc::Base64::Encode(master_key);
143
Steve Anton3a66edf2018-09-10 12:57:37 -0700144 crypto_out->tag = tag;
145 crypto_out->cipher_suite = cipher;
146 crypto_out->key_params = kInline;
147 crypto_out->key_params += key;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 return true;
149}
150
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151static bool AddCryptoParams(const std::string& cipher_suite,
Steve Anton3a66edf2018-09-10 12:57:37 -0700152 CryptoParamsVec* cryptos_out) {
153 int size = static_cast<int>(cryptos_out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154
Steve Anton3a66edf2018-09-10 12:57:37 -0700155 cryptos_out->resize(size + 1);
156 return CreateCryptoParams(size, cipher_suite, &cryptos_out->at(size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157}
158
159void AddMediaCryptos(const CryptoParamsVec& cryptos,
160 MediaContentDescription* media) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700161 for (const CryptoParams& crypto : cryptos) {
162 media->AddCrypto(crypto);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 }
164}
165
166bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
167 MediaContentDescription* media) {
168 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700169 for (const std::string& crypto_suite : crypto_suites) {
170 if (!AddCryptoParams(crypto_suite, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 return false;
172 }
173 }
174 AddMediaCryptos(cryptos, media);
175 return true;
176}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177
zhihuang1c378ed2017-08-17 14:10:50 -0700178const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800179 if (!content || !content->media_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700180 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800182 return &content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183}
184
185bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
186 const CryptoParams& crypto,
Steve Anton3a66edf2018-09-10 12:57:37 -0700187 CryptoParams* crypto_out) {
188 auto it = std::find_if(
189 cryptos.begin(), cryptos.end(),
190 [&crypto](const CryptoParams& c) { return crypto.Matches(c); });
191 if (it == cryptos.end()) {
192 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700194 *crypto_out = *it;
195 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196}
197
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700198// For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the
199// low overhead.
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700200void GetSupportedAudioSdesCryptoSuites(
201 const webrtc::CryptoOptions& crypto_options,
202 std::vector<int>* crypto_suites) {
203 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700204 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
205 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
206 }
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700207 if (crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher) {
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700208 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32);
209 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800210 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211}
212
deadbeef7914b8c2017-04-21 03:23:33 -0700213void GetSupportedAudioSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700214 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800215 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700216 GetSupportedSdesCryptoSuiteNames(GetSupportedAudioSdesCryptoSuites,
217 crypto_options, crypto_suite_names);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218}
219
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700220void GetSupportedVideoSdesCryptoSuites(
221 const webrtc::CryptoOptions& crypto_options,
222 std::vector<int>* crypto_suites) {
223 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700224 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
225 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
226 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800227 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228}
229
deadbeef7914b8c2017-04-21 03:23:33 -0700230void GetSupportedVideoSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700231 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800232 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700233 GetSupportedSdesCryptoSuiteNames(GetSupportedVideoSdesCryptoSuites,
234 crypto_options, crypto_suite_names);
235}
236
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700237void GetSupportedDataSdesCryptoSuites(
238 const webrtc::CryptoOptions& crypto_options,
239 std::vector<int>* crypto_suites) {
240 if (crypto_options.srtp.enable_gcm_crypto_suites) {
deadbeef7914b8c2017-04-21 03:23:33 -0700241 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
242 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
243 }
244 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
245}
246
247void GetSupportedDataSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700248 const webrtc::CryptoOptions& crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -0700249 std::vector<std::string>* crypto_suite_names) {
250 GetSupportedSdesCryptoSuiteNames(GetSupportedDataSdesCryptoSuites,
251 crypto_options, crypto_suite_names);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800252}
253
jbauchcb560652016-08-04 05:20:32 -0700254// Support any GCM cipher (if enabled through options). For video support only
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700255// 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated (if enabled) unless
256// bundle is enabled because it is low overhead.
jbauchcb560652016-08-04 05:20:32 -0700257// Pick the crypto in the list that is supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258static bool SelectCrypto(const MediaContentDescription* offer,
259 bool bundle,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700260 const webrtc::CryptoOptions& crypto_options,
Steve Anton3a66edf2018-09-10 12:57:37 -0700261 CryptoParams* crypto_out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
263 const CryptoParamsVec& cryptos = offer->cryptos();
264
Steve Anton3a66edf2018-09-10 12:57:37 -0700265 for (const CryptoParams& crypto : cryptos) {
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700266 if ((crypto_options.srtp.enable_gcm_crypto_suites &&
Steve Anton3a66edf2018-09-10 12:57:37 -0700267 rtc::IsGcmCryptoSuiteName(crypto.cipher_suite)) ||
268 rtc::CS_AES_CM_128_HMAC_SHA1_80 == crypto.cipher_suite ||
269 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == crypto.cipher_suite && audio &&
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700270 !bundle && crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher)) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700271 return CreateCryptoParams(crypto.tag, crypto.cipher_suite, crypto_out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 }
273 }
274 return false;
275}
276
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277// Finds all StreamParams of all media types and attach them to stream_params.
Steve Anton5c72e712018-12-10 14:25:30 -0800278static StreamParamsVec GetCurrentStreamParams(
279 const std::vector<const ContentInfo*>& active_local_contents) {
280 StreamParamsVec stream_params;
281 for (const ContentInfo* content : active_local_contents) {
282 for (const StreamParams& params : content->media_description()->streams()) {
283 stream_params.push_back(params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 }
285 }
Steve Anton5c72e712018-12-10 14:25:30 -0800286 return stream_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287}
288
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000289// Filters the data codecs for the data channel type.
290void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
291 // Filter RTP codec for SCTP and vice versa.
solenberg9fa49752016-10-08 13:02:44 -0700292 const char* codec_name =
293 sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName;
Steve Anton3a66edf2018-09-10 12:57:37 -0700294 codecs->erase(std::remove_if(codecs->begin(), codecs->end(),
295 [&codec_name](const DataCodec& codec) {
Niels Möller039743e2018-10-23 10:07:25 +0200296 return absl::EqualsIgnoreCase(codec.name,
297 codec_name);
Steve Anton3a66edf2018-09-10 12:57:37 -0700298 }),
299 codecs->end());
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000300}
301
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000302template <typename IdStruct>
303class UsedIds {
304 public:
305 UsedIds(int min_allowed_id, int max_allowed_id)
306 : min_allowed_id_(min_allowed_id),
307 max_allowed_id_(max_allowed_id),
Yves Gerey665174f2018-06-19 15:03:05 +0200308 next_id_(max_allowed_id) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309
310 // Loops through all Id in |ids| and changes its id if it is
311 // already in use by another IdStruct. Call this methods with all Id
312 // in a session description to make sure no duplicate ids exists.
313 // Note that typename Id must be a type of IdStruct.
314 template <typename Id>
315 void FindAndSetIdUsed(std::vector<Id>* ids) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700316 for (const Id& id : *ids) {
317 FindAndSetIdUsed(&id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318 }
319 }
320
321 // Finds and sets an unused id if the |idstruct| id is already in use.
322 void FindAndSetIdUsed(IdStruct* idstruct) {
323 const int original_id = idstruct->id;
324 int new_id = idstruct->id;
325
326 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
327 // If the original id is not in range - this is an id that can't be
328 // dynamically changed.
329 return;
330 }
331
332 if (IsIdUsed(original_id)) {
333 new_id = FindUnusedId();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100334 RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from "
335 << original_id << " to " << new_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336 idstruct->id = new_id;
337 }
338 SetIdUsed(new_id);
339 }
340
341 private:
342 // Returns the first unused id in reverse order.
343 // This hopefully reduce the risk of more collisions. We want to change the
344 // default ids as little as possible.
345 int FindUnusedId() {
346 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
347 --next_id_;
348 }
nisseede5da42017-01-12 05:15:36 -0800349 RTC_DCHECK(next_id_ >= min_allowed_id_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 return next_id_;
351 }
352
Yves Gerey665174f2018-06-19 15:03:05 +0200353 bool IsIdUsed(int new_id) { return id_set_.find(new_id) != id_set_.end(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354
Yves Gerey665174f2018-06-19 15:03:05 +0200355 void SetIdUsed(int new_id) { id_set_.insert(new_id); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356
357 const int min_allowed_id_;
358 const int max_allowed_id_;
359 int next_id_;
360 std::set<int> id_set_;
361};
362
363// Helper class used for finding duplicate RTP payload types among audio, video
364// and data codecs. When bundle is used the payload types may not collide.
365class UsedPayloadTypes : public UsedIds<Codec> {
366 public:
367 UsedPayloadTypes()
Yves Gerey665174f2018-06-19 15:03:05 +0200368 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369
370 private:
371 static const int kDynamicPayloadTypeMin = 96;
372 static const int kDynamicPayloadTypeMax = 127;
373};
374
375// Helper class used for finding duplicate RTP Header extension ids among
Johannes Kron07ba2b92018-09-26 13:33:35 +0200376// audio and video extensions. Only applies to one-byte header extensions at the
377// moment. ids > 14 will always be reported as available.
378// TODO(kron): This class needs to be refactored when we start to send two-byte
379// header extensions.
isheriff6f8d6862016-05-26 11:24:55 -0700380class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 public:
382 UsedRtpHeaderExtensionIds()
Johannes Kron07ba2b92018-09-26 13:33:35 +0200383 : UsedIds<webrtc::RtpExtension>(
384 webrtc::RtpExtension::kMinId,
385 webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386
387 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388};
389
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800390static StreamParams CreateStreamParamsForNewSenderWithSsrcs(
391 const SenderOptions& sender,
392 const std::string& rtcp_cname,
393 const StreamParamsVec& current_streams,
394 bool include_rtx_streams,
395 bool include_flexfec_stream) {
396 StreamParams result;
397 result.id = sender.track_id;
398
399 std::vector<uint32_t> known_ssrcs;
400 for (const StreamParams& params : current_streams) {
401 for (uint32_t ssrc : params.ssrcs) {
402 known_ssrcs.push_back(ssrc);
403 }
404 }
405
406 UniqueRandomIdGenerator ssrc_generator(known_ssrcs);
407 // We need to keep |primary_ssrcs| separate from |result.ssrcs| because
408 // iterators are invalidated when rtx and flexfec ssrcs are added to the list.
409 std::vector<uint32_t> primary_ssrcs;
410 for (int i = 0; i < sender.num_sim_layers; ++i) {
411 primary_ssrcs.push_back(ssrc_generator());
412 }
413 result.ssrcs = primary_ssrcs;
414
415 if (sender.num_sim_layers > 1) {
416 SsrcGroup group(kSimSsrcGroupSemantics, result.ssrcs);
417 result.ssrc_groups.push_back(group);
418 }
419 // Generate an RTX ssrc for every ssrc in the group.
420 if (include_rtx_streams) {
421 for (uint32_t ssrc : primary_ssrcs) {
422 result.AddFidSsrc(ssrc, ssrc_generator());
423 }
424 }
425 // Generate extra ssrc for include_flexfec_stream case.
426 if (include_flexfec_stream) {
427 // TODO(brandtr): Update when we support multistream protection.
428 if (result.ssrcs.size() == 1) {
429 for (uint32_t ssrc : primary_ssrcs) {
430 result.AddFecFrSsrc(ssrc, ssrc_generator());
431 }
432 } else if (!result.ssrcs.empty()) {
433 RTC_LOG(LS_WARNING)
434 << "Our FlexFEC implementation only supports protecting "
435 "a single media streams. This session has multiple "
436 "media streams however, so no FlexFEC SSRC will be generated.";
437 }
438 }
439 result.cname = rtcp_cname;
440 result.set_stream_ids(sender.stream_ids);
441
442 return result;
443}
444
445static bool ValidateSimulcastLayers(
446 const std::vector<RidDescription>& rids,
447 const SimulcastLayerList& simulcast_layers) {
448 std::vector<SimulcastLayer> all_layers = simulcast_layers.GetAllLayers();
449 return std::all_of(all_layers.begin(), all_layers.end(),
450 [&rids](const SimulcastLayer& layer) {
451 return std::find_if(rids.begin(), rids.end(),
452 [&layer](const RidDescription& rid) {
453 return rid.rid == layer.rid;
454 }) != rids.end();
455 });
456}
457
458static StreamParams CreateStreamParamsForNewSenderWithRids(
459 const SenderOptions& sender,
460 const std::string& rtcp_cname) {
461 RTC_DCHECK(!sender.rids.empty());
462 RTC_DCHECK_EQ(sender.num_sim_layers, 0)
463 << "RIDs are the compliant way to indicate simulcast.";
464 RTC_DCHECK(ValidateSimulcastLayers(sender.rids, sender.simulcast_layers));
465 StreamParams result;
466 result.id = sender.track_id;
467 result.cname = rtcp_cname;
468 result.set_stream_ids(sender.stream_ids);
469
470 // More than one rid should be signaled.
471 if (sender.rids.size() > 1) {
472 result.set_rids(sender.rids);
473 }
474
475 return result;
476}
477
478// Adds SimulcastDescription if indicated by the media description options.
479// MediaContentDescription should already be set up with the send rids.
480static void AddSimulcastToMediaDescription(
481 const MediaDescriptionOptions& media_description_options,
482 MediaContentDescription* description) {
483 RTC_DCHECK(description);
484
485 // Check if we are using RIDs in this scenario.
486 if (std::all_of(
487 description->streams().begin(), description->streams().end(),
488 [](const StreamParams& params) { return !params.has_rids(); })) {
489 return;
490 }
491
492 RTC_DCHECK_EQ(1, description->streams().size())
493 << "RIDs are only supported in Unified Plan semantics.";
494 RTC_DCHECK_EQ(1, media_description_options.sender_options.size());
495 RTC_DCHECK(description->type() == MediaType::MEDIA_TYPE_AUDIO ||
496 description->type() == MediaType::MEDIA_TYPE_VIDEO);
497
498 // One RID or less indicates that simulcast is not needed.
499 if (description->streams()[0].rids().size() <= 1) {
500 return;
501 }
502
503 RTC_DCHECK(!media_description_options.receive_rids.empty());
504 RTC_DCHECK(ValidateSimulcastLayers(
505 media_description_options.receive_rids,
506 media_description_options.receive_simulcast_layers));
507 StreamParams receive_stream;
508 receive_stream.set_rids(media_description_options.receive_rids);
509 description->set_receive_stream(receive_stream);
510
511 SimulcastDescription simulcast;
512 simulcast.send_layers() =
513 media_description_options.sender_options[0].simulcast_layers;
514 simulcast.receive_layers() =
515 media_description_options.receive_simulcast_layers;
516 description->set_simulcast_description(simulcast);
517}
518
zhihuang1c378ed2017-08-17 14:10:50 -0700519// Adds a StreamParams for each SenderOptions in |sender_options| to
520// content_description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521// |current_params| - All currently known StreamParams of any media type.
522template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700523static bool AddStreamParams(
524 const std::vector<SenderOptions>& sender_options,
525 const std::string& rtcp_cname,
526 StreamParamsVec* current_streams,
527 MediaContentDescriptionImpl<C>* content_description) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700528 // SCTP streams are not negotiated using SDP/ContentDescriptions.
deadbeef8b7e9ad2017-05-25 09:38:55 -0700529 if (IsSctp(content_description->protocol())) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700530 return true;
531 }
532
Noah Richards2e7a0982015-05-18 14:02:54 -0700533 const bool include_rtx_streams =
534 ContainsRtxCodec(content_description->codecs());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535
brandtr03d5fb12016-11-22 03:37:59 -0800536 const bool include_flexfec_stream =
537 ContainsFlexfecCodec(content_description->codecs());
538
zhihuang1c378ed2017-08-17 14:10:50 -0700539 for (const SenderOptions& sender : sender_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 // groupid is empty for StreamParams generated using
541 // MediaSessionDescriptionFactory.
zhihuang1c378ed2017-08-17 14:10:50 -0700542 StreamParams* param =
543 GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000544 if (!param) {
zhihuang1c378ed2017-08-17 14:10:50 -0700545 // This is a new sender.
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800546 StreamParams stream_param =
547 sender.rids.empty()
548 ?
549 // Signal SSRCs and legacy simulcast (if requested).
550 CreateStreamParamsForNewSenderWithSsrcs(
551 sender, rtcp_cname, *current_streams, include_rtx_streams,
552 include_flexfec_stream)
553 :
554 // Signal RIDs and spec-compliant simulcast (if requested).
555 CreateStreamParamsForNewSenderWithRids(sender, rtcp_cname);
556
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 content_description->AddStream(stream_param);
558
559 // Store the new StreamParams in current_streams.
560 // This is necessary so that we can use the CNAME for other media types.
561 current_streams->push_back(stream_param);
562 } else {
deadbeef2f425aa2017-04-14 10:41:32 -0700563 // Use existing generated SSRCs/groups, but update the sync_label if
564 // necessary. This may be needed if a MediaStreamTrack was moved from one
565 // MediaStream to another.
Seth Hampson845e8782018-03-02 11:34:10 -0800566 param->set_stream_ids(sender.stream_ids);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000567 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 }
569 }
570 return true;
571}
572
573// Updates the transport infos of the |sdesc| according to the given
574// |bundle_group|. The transport infos of the content names within the
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800575// |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
576// first content within the |bundle_group|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
578 SessionDescription* sdesc) {
579 // The bundle should not be empty.
580 if (!sdesc || !bundle_group.FirstContentName()) {
581 return false;
582 }
583
584 // We should definitely have a transport for the first content.
jbauch083b73f2015-07-16 02:46:32 -0700585 const std::string& selected_content_name = *bundle_group.FirstContentName();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 const TransportInfo* selected_transport_info =
587 sdesc->GetTransportInfoByName(selected_content_name);
588 if (!selected_transport_info) {
589 return false;
590 }
591
592 // Set the other contents to use the same ICE credentials.
jbauch083b73f2015-07-16 02:46:32 -0700593 const std::string& selected_ufrag =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 selected_transport_info->description.ice_ufrag;
jbauch083b73f2015-07-16 02:46:32 -0700595 const std::string& selected_pwd =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 selected_transport_info->description.ice_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800597 ConnectionRole selected_connection_role =
598 selected_transport_info->description.connection_role;
Steve Anton3a66edf2018-09-10 12:57:37 -0700599 for (TransportInfo& transport_info : sdesc->transport_infos()) {
600 if (bundle_group.HasContentName(transport_info.content_name) &&
601 transport_info.content_name != selected_content_name) {
602 transport_info.description.ice_ufrag = selected_ufrag;
603 transport_info.description.ice_pwd = selected_pwd;
604 transport_info.description.connection_role = selected_connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 }
606 }
607 return true;
608}
609
610// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
611// sets it to |cryptos|.
612static bool GetCryptosByName(const SessionDescription* sdesc,
613 const std::string& content_name,
614 CryptoParamsVec* cryptos) {
615 if (!sdesc || !cryptos) {
616 return false;
617 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 const ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800619 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 return false;
621 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800622 *cryptos = content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 return true;
624}
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
627// which are not available in |filter|.
628static void PruneCryptos(const CryptoParamsVec& filter,
629 CryptoParamsVec* target_cryptos) {
630 if (!target_cryptos) {
631 return;
632 }
tzik21995802018-04-26 17:38:28 +0900633
634 target_cryptos->erase(
635 std::remove_if(target_cryptos->begin(), target_cryptos->end(),
636 // Returns true if the |crypto|'s cipher_suite is not
637 // found in |filter|.
638 [&filter](const CryptoParams& crypto) {
639 for (const CryptoParams& entry : filter) {
640 if (entry.cipher_suite == crypto.cipher_suite)
641 return false;
642 }
643 return true;
644 }),
645 target_cryptos->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646}
647
Steve Antonfa2260d2017-12-28 16:38:23 -0800648bool IsRtpProtocol(const std::string& protocol) {
deadbeefb5cb19b2015-11-23 16:39:12 -0800649 return protocol.empty() ||
650 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
651}
652
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653static bool IsRtpContent(SessionDescription* sdesc,
654 const std::string& content_name) {
655 bool is_rtp = false;
656 ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800657 if (content && content->media_description()) {
658 is_rtp = IsRtpProtocol(content->media_description()->protocol());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 }
660 return is_rtp;
661}
662
663// Updates the crypto parameters of the |sdesc| according to the given
664// |bundle_group|. The crypto parameters of all the contents within the
665// |bundle_group| should be updated to use the common subset of the
666// available cryptos.
667static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
668 SessionDescription* sdesc) {
669 // The bundle should not be empty.
670 if (!sdesc || !bundle_group.FirstContentName()) {
671 return false;
672 }
673
wu@webrtc.org78187522013-10-07 23:32:02 +0000674 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 // Get the common cryptos.
676 const ContentNames& content_names = bundle_group.content_names();
677 CryptoParamsVec common_cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700678 bool first = true;
679 for (const std::string& content_name : content_names) {
680 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 continue;
682 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000683 // The common cryptos are needed if any of the content does not have DTLS
684 // enabled.
Steve Anton3a66edf2018-09-10 12:57:37 -0700685 if (!sdesc->GetTransportInfoByName(content_name)->description.secure()) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000686 common_cryptos_needed = true;
687 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700688 if (first) {
689 first = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 // Initial the common_cryptos with the first content in the bundle group.
Steve Anton3a66edf2018-09-10 12:57:37 -0700691 if (!GetCryptosByName(sdesc, content_name, &common_cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 return false;
693 }
694 if (common_cryptos.empty()) {
695 // If there's no crypto params, we should just return.
696 return true;
697 }
698 } else {
699 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700700 if (!GetCryptosByName(sdesc, content_name, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 return false;
702 }
703 PruneCryptos(cryptos, &common_cryptos);
704 }
705 }
706
wu@webrtc.org78187522013-10-07 23:32:02 +0000707 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 return false;
709 }
710
711 // Update to use the common cryptos.
Steve Anton3a66edf2018-09-10 12:57:37 -0700712 for (const std::string& content_name : content_names) {
713 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 continue;
715 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700716 ContentInfo* content = sdesc->GetContentByName(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 if (IsMediaContent(content)) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800718 MediaContentDescription* media_desc = content->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 if (!media_desc) {
720 return false;
721 }
722 media_desc->set_cryptos(common_cryptos);
723 }
724 }
725 return true;
726}
727
Steve Anton5c72e712018-12-10 14:25:30 -0800728static std::vector<const ContentInfo*> GetActiveContents(
729 const SessionDescription& description,
730 const MediaSessionOptions& session_options) {
731 std::vector<const ContentInfo*> active_contents;
732 for (size_t i = 0; i < description.contents().size(); ++i) {
733 RTC_DCHECK_LT(i, session_options.media_description_options.size());
734 const ContentInfo& content = description.contents()[i];
735 const MediaDescriptionOptions& media_options =
736 session_options.media_description_options[i];
737 if (!content.rejected && !media_options.stopped &&
738 content.name == media_options.mid) {
739 active_contents.push_back(&content);
740 }
741 }
742 return active_contents;
743}
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745template <class C>
746static bool ContainsRtxCodec(const std::vector<C>& codecs) {
brandtr03d5fb12016-11-22 03:37:59 -0800747 for (const auto& codec : codecs) {
748 if (IsRtxCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 return true;
750 }
751 }
752 return false;
753}
754
755template <class C>
756static bool IsRtxCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200757 return absl::EqualsIgnoreCase(codec.name, kRtxCodecName);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758}
759
brandtr03d5fb12016-11-22 03:37:59 -0800760template <class C>
761static bool ContainsFlexfecCodec(const std::vector<C>& codecs) {
762 for (const auto& codec : codecs) {
763 if (IsFlexfecCodec(codec)) {
764 return true;
765 }
766 }
767 return false;
768}
769
770template <class C>
771static bool IsFlexfecCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200772 return absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName);
brandtr03d5fb12016-11-22 03:37:59 -0800773}
774
zhihuang1c378ed2017-08-17 14:10:50 -0700775// Create a media content to be offered for the given |sender_options|,
776// according to the given options.rtcp_mux, session_options.is_muc, codecs,
777// secure_transport, crypto, and current_streams. If we don't currently have
778// crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is
779// created (according to crypto_suites). The created content is added to the
780// offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781template <class C>
782static bool CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800783 const MediaDescriptionOptions& media_description_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700784 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000786 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 const CryptoParamsVec* current_cryptos,
788 const std::vector<std::string>& crypto_suites,
789 const RtpHeaderExtensions& rtp_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 StreamParamsVec* current_streams,
791 MediaContentDescriptionImpl<C>* offer) {
792 offer->AddCodecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
zhihuang1c378ed2017-08-17 14:10:50 -0700794 offer->set_rtcp_mux(session_options.rtcp_mux_enabled);
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700795 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
796 offer->set_rtcp_reduced_size(true);
797 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 offer->set_rtp_header_extensions(rtp_extensions);
799
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800800 if (!AddStreamParams(media_description_options.sender_options,
801 session_options.rtcp_cname, current_streams, offer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 return false;
803 }
804
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800805 AddSimulcastToMediaDescription(media_description_options, offer);
806
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 if (secure_policy != SEC_DISABLED) {
808 if (current_cryptos) {
809 AddMediaCryptos(*current_cryptos, offer);
810 }
811 if (offer->cryptos().empty()) {
812 if (!CreateMediaCryptos(crypto_suites, offer)) {
813 return false;
814 }
815 }
816 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817
deadbeef7af91dd2016-12-13 11:29:11 -0800818 if (secure_policy == SEC_REQUIRED && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000819 return false;
820 }
821 return true;
822}
823
824template <class C>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000825static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
magjedb05fa242016-11-11 04:00:16 -0800826 const int codec1_id,
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000827 const std::vector<C>& codecs2,
magjedb05fa242016-11-11 04:00:16 -0800828 const int codec2_id) {
829 const C* codec1 = FindCodecById(codecs1, codec1_id);
830 const C* codec2 = FindCodecById(codecs2, codec2_id);
831 return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2);
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000832}
833
834template <class C>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835static void NegotiateCodecs(const std::vector<C>& local_codecs,
836 const std::vector<C>& offered_codecs,
837 std::vector<C>* negotiated_codecs) {
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800838 for (const C& ours : local_codecs) {
839 C theirs;
deadbeef67cf2c12016-04-13 10:07:16 -0700840 // Note that we intentionally only find one matching codec for each of our
841 // local codecs, in case the remote offer contains duplicate codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800842 if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) {
843 C negotiated = ours;
844 negotiated.IntersectFeedbackParams(theirs);
845 if (IsRtxCodec(negotiated)) {
magjedb05fa242016-11-11 04:00:16 -0800846 const auto apt_it =
847 theirs.params.find(kCodecParamAssociatedPayloadType);
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800848 // FindMatchingCodec shouldn't return something with no apt value.
magjedb05fa242016-11-11 04:00:16 -0800849 RTC_DCHECK(apt_it != theirs.params.end());
850 negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 }
Niels Möller039743e2018-10-23 10:07:25 +0200852 if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) {
magjedf823ede2016-11-12 09:53:04 -0800853 webrtc::H264::GenerateProfileLevelIdForAnswer(
854 ours.params, theirs.params, &negotiated.params);
855 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800856 negotiated.id = theirs.id;
ossu075af922016-06-14 03:29:38 -0700857 negotiated.name = theirs.name;
magjedb05fa242016-11-11 04:00:16 -0800858 negotiated_codecs->push_back(std::move(negotiated));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 }
860 }
deadbeef67cf2c12016-04-13 10:07:16 -0700861 // RFC3264: Although the answerer MAY list the formats in their desired
862 // order of preference, it is RECOMMENDED that unless there is a
863 // specific reason, the answerer list formats in the same relative order
864 // they were present in the offer.
865 std::unordered_map<int, int> payload_type_preferences;
866 int preference = static_cast<int>(offered_codecs.size() + 1);
867 for (const C& codec : offered_codecs) {
868 payload_type_preferences[codec.id] = preference--;
869 }
870 std::sort(negotiated_codecs->begin(), negotiated_codecs->end(),
871 [&payload_type_preferences](const C& a, const C& b) {
872 return payload_type_preferences[a.id] >
873 payload_type_preferences[b.id];
874 });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875}
876
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800877// Finds a codec in |codecs2| that matches |codec_to_match|, which is
878// a member of |codecs1|. If |codec_to_match| is an RTX codec, both
879// the codecs themselves and their associated codecs must match.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880template <class C>
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800881static bool FindMatchingCodec(const std::vector<C>& codecs1,
882 const std::vector<C>& codecs2,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 const C& codec_to_match,
884 C* found_codec) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -0700885 // |codec_to_match| should be a member of |codecs1|, in order to look up RTX
886 // codecs' associated codecs correctly. If not, that's a programming error.
887 RTC_DCHECK(std::find_if(codecs1.begin(), codecs1.end(),
888 [&codec_to_match](const C& codec) {
889 return &codec == &codec_to_match;
890 }) != codecs1.end());
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800891 for (const C& potential_match : codecs2) {
892 if (potential_match.Matches(codec_to_match)) {
893 if (IsRtxCodec(codec_to_match)) {
magjedb05fa242016-11-11 04:00:16 -0800894 int apt_value_1 = 0;
895 int apt_value_2 = 0;
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800896 if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType,
897 &apt_value_1) ||
898 !potential_match.GetParam(kCodecParamAssociatedPayloadType,
899 &apt_value_2)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100900 RTC_LOG(LS_WARNING) << "RTX missing associated payload type.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800901 continue;
902 }
903 if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2,
904 apt_value_2)) {
905 continue;
906 }
907 }
908 if (found_codec) {
909 *found_codec = potential_match;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
911 return true;
912 }
913 }
914 return false;
915}
916
zhihuang1c378ed2017-08-17 14:10:50 -0700917// Find the codec in |codec_list| that |rtx_codec| is associated with.
918template <class C>
919static const C* GetAssociatedCodec(const std::vector<C>& codec_list,
920 const C& rtx_codec) {
921 std::string associated_pt_str;
922 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
923 &associated_pt_str)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100924 RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
925 << " is missing an associated payload type.";
zhihuang1c378ed2017-08-17 14:10:50 -0700926 return nullptr;
927 }
928
929 int associated_pt;
930 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100931 RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
932 << " of RTX codec " << rtx_codec.name
933 << " to an integer.";
zhihuang1c378ed2017-08-17 14:10:50 -0700934 return nullptr;
935 }
936
937 // Find the associated reference codec for the reference RTX codec.
938 const C* associated_codec = FindCodecById(codec_list, associated_pt);
939 if (!associated_codec) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100940 RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
941 << associated_pt << " for RTX codec " << rtx_codec.name
942 << ".";
zhihuang1c378ed2017-08-17 14:10:50 -0700943 }
944 return associated_codec;
945}
946
947// Adds all codecs from |reference_codecs| to |offered_codecs| that don't
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948// already exist in |offered_codecs| and ensure the payload types don't
949// collide.
950template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700951static void MergeCodecs(const std::vector<C>& reference_codecs,
952 std::vector<C>* offered_codecs,
953 UsedPayloadTypes* used_pltypes) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 // Add all new codecs that are not RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800955 for (const C& reference_codec : reference_codecs) {
956 if (!IsRtxCodec(reference_codec) &&
957 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
958 reference_codec, nullptr)) {
959 C codec = reference_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 used_pltypes->FindAndSetIdUsed(&codec);
961 offered_codecs->push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 }
963 }
964
965 // Add all new RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800966 for (const C& reference_codec : reference_codecs) {
967 if (IsRtxCodec(reference_codec) &&
968 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
969 reference_codec, nullptr)) {
970 C rtx_codec = reference_codec;
olka3c747662017-08-17 06:50:32 -0700971 const C* associated_codec =
zhihuang1c378ed2017-08-17 14:10:50 -0700972 GetAssociatedCodec(reference_codecs, rtx_codec);
olka3c747662017-08-17 06:50:32 -0700973 if (!associated_codec) {
olka3c747662017-08-17 06:50:32 -0700974 continue;
975 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800976 // Find a codec in the offered list that matches the reference codec.
977 // Its payload type may be different than the reference codec.
978 C matching_codec;
979 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
magjedb05fa242016-11-11 04:00:16 -0800980 *associated_codec, &matching_codec)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100981 RTC_LOG(LS_WARNING)
982 << "Couldn't find matching " << associated_codec->name << " codec.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800983 continue;
984 }
985
986 rtx_codec.params[kCodecParamAssociatedPayloadType] =
987 rtc::ToString(matching_codec.id);
988 used_pltypes->FindAndSetIdUsed(&rtx_codec);
989 offered_codecs->push_back(rtx_codec);
990 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 }
992}
993
zhihuang1c378ed2017-08-17 14:10:50 -0700994static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
995 const webrtc::RtpExtension& ext_to_match,
996 webrtc::RtpExtension* found_extension) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700997 auto it =
998 std::find_if(extensions.begin(), extensions.end(),
999 [&ext_to_match](const webrtc::RtpExtension& extension) {
1000 // We assume that all URIs are given in a canonical
1001 // format.
1002 return extension.uri == ext_to_match.uri &&
1003 extension.encrypt == ext_to_match.encrypt;
1004 });
1005 if (it == extensions.end()) {
1006 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001007 }
Steve Anton3a66edf2018-09-10 12:57:37 -07001008 if (found_extension) {
1009 *found_extension = *it;
1010 }
1011 return true;
zhihuang1c378ed2017-08-17 14:10:50 -07001012}
1013
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014static bool FindByUri(const RtpHeaderExtensions& extensions,
isheriff6f8d6862016-05-26 11:24:55 -07001015 const webrtc::RtpExtension& ext_to_match,
1016 webrtc::RtpExtension* found_extension) {
jbauch5869f502017-06-29 12:31:36 -07001017 // We assume that all URIs are given in a canonical format.
1018 const webrtc::RtpExtension* found =
1019 webrtc::RtpExtension::FindHeaderExtensionByUri(extensions,
1020 ext_to_match.uri);
1021 if (!found) {
1022 return false;
1023 }
1024 if (found_extension) {
1025 *found_extension = *found;
1026 }
1027 return true;
1028}
1029
1030static bool FindByUriWithEncryptionPreference(
1031 const RtpHeaderExtensions& extensions,
Yves Gerey665174f2018-06-19 15:03:05 +02001032 const webrtc::RtpExtension& ext_to_match,
1033 bool encryption_preference,
jbauch5869f502017-06-29 12:31:36 -07001034 webrtc::RtpExtension* found_extension) {
1035 const webrtc::RtpExtension* unencrypted_extension = nullptr;
Steve Anton3a66edf2018-09-10 12:57:37 -07001036 for (const webrtc::RtpExtension& extension : extensions) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 // We assume that all URIs are given in a canonical format.
Steve Anton3a66edf2018-09-10 12:57:37 -07001038 if (extension.uri == ext_to_match.uri) {
1039 if (!encryption_preference || extension.encrypt) {
jbauch5869f502017-06-29 12:31:36 -07001040 if (found_extension) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001041 *found_extension = extension;
jbauch5869f502017-06-29 12:31:36 -07001042 }
1043 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 }
Steve Anton3a66edf2018-09-10 12:57:37 -07001045 unencrypted_extension = &extension;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 }
1047 }
jbauch5869f502017-06-29 12:31:36 -07001048 if (unencrypted_extension) {
1049 if (found_extension) {
1050 *found_extension = *unencrypted_extension;
1051 }
1052 return true;
1053 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 return false;
1055}
1056
zhihuang1c378ed2017-08-17 14:10:50 -07001057// Adds all extensions from |reference_extensions| to |offered_extensions| that
1058// don't already exist in |offered_extensions| and ensure the IDs don't
1059// collide. If an extension is added, it's also added to |regular_extensions| or
1060// |encrypted_extensions|, and if the extension is in |regular_extensions| or
1061// |encrypted_extensions|, its ID is marked as used in |used_ids|.
1062// |offered_extensions| is for either audio or video while |regular_extensions|
1063// and |encrypted_extensions| are used for both audio and video. There could be
1064// overlap between audio extensions and video extensions.
1065static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions,
1066 RtpHeaderExtensions* offered_extensions,
1067 RtpHeaderExtensions* regular_extensions,
1068 RtpHeaderExtensions* encrypted_extensions,
1069 UsedRtpHeaderExtensionIds* used_ids) {
olka3c747662017-08-17 06:50:32 -07001070 for (auto reference_extension : reference_extensions) {
zhihuang1c378ed2017-08-17 14:10:50 -07001071 if (!FindByUriAndEncryption(*offered_extensions, reference_extension,
1072 nullptr)) {
olka3c747662017-08-17 06:50:32 -07001073 webrtc::RtpExtension existing;
zhihuang1c378ed2017-08-17 14:10:50 -07001074 if (reference_extension.encrypt) {
1075 if (FindByUriAndEncryption(*encrypted_extensions, reference_extension,
1076 &existing)) {
1077 offered_extensions->push_back(existing);
1078 } else {
1079 used_ids->FindAndSetIdUsed(&reference_extension);
1080 encrypted_extensions->push_back(reference_extension);
1081 offered_extensions->push_back(reference_extension);
1082 }
olka3c747662017-08-17 06:50:32 -07001083 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07001084 if (FindByUriAndEncryption(*regular_extensions, reference_extension,
1085 &existing)) {
1086 offered_extensions->push_back(existing);
1087 } else {
1088 used_ids->FindAndSetIdUsed(&reference_extension);
1089 regular_extensions->push_back(reference_extension);
1090 offered_extensions->push_back(reference_extension);
1091 }
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001092 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 }
1094 }
1095}
1096
jbauch5869f502017-06-29 12:31:36 -07001097static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions,
1098 RtpHeaderExtensions* all_extensions,
1099 UsedRtpHeaderExtensionIds* used_ids) {
1100 RtpHeaderExtensions encrypted_extensions;
1101 for (const webrtc::RtpExtension& extension : *extensions) {
1102 webrtc::RtpExtension existing;
1103 // Don't add encrypted extensions again that were already included in a
1104 // previous offer or regular extensions that are also included as encrypted
1105 // extensions.
1106 if (extension.encrypt ||
1107 !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) ||
1108 (FindByUriWithEncryptionPreference(*extensions, extension, true,
Yves Gerey665174f2018-06-19 15:03:05 +02001109 &existing) &&
1110 existing.encrypt)) {
jbauch5869f502017-06-29 12:31:36 -07001111 continue;
1112 }
1113
1114 if (FindByUri(*all_extensions, extension, &existing)) {
1115 encrypted_extensions.push_back(existing);
1116 } else {
1117 webrtc::RtpExtension encrypted(extension);
1118 encrypted.encrypt = true;
1119 used_ids->FindAndSetIdUsed(&encrypted);
1120 all_extensions->push_back(encrypted);
1121 encrypted_extensions.push_back(encrypted);
1122 }
1123 }
1124 extensions->insert(extensions->end(), encrypted_extensions.begin(),
Yves Gerey665174f2018-06-19 15:03:05 +02001125 encrypted_extensions.end());
jbauch5869f502017-06-29 12:31:36 -07001126}
1127
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128static void NegotiateRtpHeaderExtensions(
1129 const RtpHeaderExtensions& local_extensions,
1130 const RtpHeaderExtensions& offered_extensions,
jbauch5869f502017-06-29 12:31:36 -07001131 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 RtpHeaderExtensions* negotiated_extenstions) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001133 for (const webrtc::RtpExtension& ours : local_extensions) {
isheriff6f8d6862016-05-26 11:24:55 -07001134 webrtc::RtpExtension theirs;
Yves Gerey665174f2018-06-19 15:03:05 +02001135 if (FindByUriWithEncryptionPreference(
Steve Anton3a66edf2018-09-10 12:57:37 -07001136 offered_extensions, ours, enable_encrypted_rtp_header_extensions,
Yves Gerey665174f2018-06-19 15:03:05 +02001137 &theirs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 // We respond with their RTP header extension id.
1139 negotiated_extenstions->push_back(theirs);
1140 }
1141 }
1142}
1143
1144static void StripCNCodecs(AudioCodecs* audio_codecs) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001145 audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(),
1146 [](const AudioCodec& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +02001147 return absl::EqualsIgnoreCase(
1148 codec.name, kComfortNoiseCodecName);
Steve Anton3a66edf2018-09-10 12:57:37 -07001149 }),
1150 audio_codecs->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151}
1152
zhihuang1c378ed2017-08-17 14:10:50 -07001153// Create a media content to be answered for the given |sender_options|
1154// according to the given session_options.rtcp_mux, session_options.streams,
1155// codecs, crypto, and current_streams. If we don't currently have crypto (in
1156// current_cryptos) and it is enabled (in secure_policy), crypto is created
1157// (according to crypto_suites). The codecs, rtcp_mux, and crypto are all
1158// negotiated with the offer. If the negotiation fails, this method returns
1159// false. The created content is added to the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001160template <class C>
1161static bool CreateMediaContentAnswer(
1162 const MediaContentDescriptionImpl<C>* offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001163 const MediaDescriptionOptions& media_description_options,
1164 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001166 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 const CryptoParamsVec* current_cryptos,
1168 const RtpHeaderExtensions& local_rtp_extenstions,
jbauch5869f502017-06-29 12:31:36 -07001169 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 StreamParamsVec* current_streams,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 bool bundle_enabled,
1172 MediaContentDescriptionImpl<C>* answer) {
1173 std::vector<C> negotiated_codecs;
1174 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
1175 answer->AddCodecs(negotiated_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 answer->set_protocol(offer->protocol());
Johannes Kron0854eb62018-10-10 22:33:20 +02001177
Johannes Kron9581bc42018-10-23 10:17:39 +02001178 answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 RtpHeaderExtensions negotiated_rtp_extensions;
Yves Gerey665174f2018-06-19 15:03:05 +02001180 NegotiateRtpHeaderExtensions(
1181 local_rtp_extenstions, offer->rtp_header_extensions(),
1182 enable_encrypted_rtp_header_extensions, &negotiated_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
1184
zhihuang1c378ed2017-08-17 14:10:50 -07001185 answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux());
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -07001186 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
1187 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
1188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189
1190 if (sdes_policy != SEC_DISABLED) {
1191 CryptoParams crypto;
zhihuang1c378ed2017-08-17 14:10:50 -07001192 if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options,
1193 &crypto)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 if (current_cryptos) {
1195 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
1196 }
1197 answer->AddCrypto(crypto);
1198 }
1199 }
1200
deadbeef7af91dd2016-12-13 11:29:11 -08001201 if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 return false;
1203 }
1204
zhihuang1c378ed2017-08-17 14:10:50 -07001205 if (!AddStreamParams(media_description_options.sender_options,
1206 session_options.rtcp_cname, current_streams, answer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 return false; // Something went seriously wrong.
1208 }
1209
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001210 AddSimulcastToMediaDescription(media_description_options, answer);
1211
Steve Anton4e70a722017-11-28 14:57:10 -08001212 answer->set_direction(NegotiateRtpTransceiverDirection(
1213 offer->direction(), media_description_options.direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 return true;
1215}
1216
1217static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001218 const std::string& protocol,
1219 bool secure_transport) {
zhihuangcf5b37c2016-05-05 11:44:35 -07001220 // Since not all applications serialize and deserialize the media protocol,
1221 // we will have to accept |protocol| to be empty.
1222 if (protocol.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 return true;
1224 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001225
zhihuangcf5b37c2016-05-05 11:44:35 -07001226 if (type == MEDIA_TYPE_DATA) {
1227 // Check for SCTP, but also for RTP for RTP-based data channels.
1228 // TODO(pthatcher): Remove RTP once RTP-based data channels are gone.
1229 if (secure_transport) {
1230 // Most likely scenarios first.
1231 return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) ||
1232 IsPlainRtp(protocol);
1233 } else {
1234 return IsPlainSctp(protocol) || IsPlainRtp(protocol);
1235 }
1236 }
1237
1238 // Allow for non-DTLS RTP protocol even when using DTLS because that's what
1239 // JSEP specifies.
1240 if (secure_transport) {
1241 // Most likely scenarios first.
1242 return IsDtlsRtp(protocol) || IsPlainRtp(protocol);
1243 } else {
1244 return IsPlainRtp(protocol);
1245 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246}
1247
1248static void SetMediaProtocol(bool secure_transport,
1249 MediaContentDescription* desc) {
deadbeeff3938292015-07-15 12:20:53 -07001250 if (!desc->cryptos().empty())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 desc->set_protocol(kMediaProtocolSavpf);
deadbeeff3938292015-07-15 12:20:53 -07001252 else if (secure_transport)
1253 desc->set_protocol(kMediaProtocolDtlsSavpf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 else
1255 desc->set_protocol(kMediaProtocolAvpf);
1256}
1257
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001258// Gets the TransportInfo of the given |content_name| from the
1259// |current_description|. If doesn't exist, returns a new one.
1260static const TransportDescription* GetTransportDescription(
1261 const std::string& content_name,
1262 const SessionDescription* current_description) {
1263 const TransportDescription* desc = NULL;
1264 if (current_description) {
1265 const TransportInfo* info =
1266 current_description->GetTransportInfoByName(content_name);
1267 if (info) {
1268 desc = &info->description;
1269 }
1270 }
1271 return desc;
1272}
1273
1274// Gets the current DTLS state from the transport description.
zhihuang1c378ed2017-08-17 14:10:50 -07001275static bool IsDtlsActive(const ContentInfo* content,
1276 const SessionDescription* current_description) {
1277 if (!content) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001278 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001279 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001280
zhihuang1c378ed2017-08-17 14:10:50 -07001281 size_t msection_index = content - &current_description->contents()[0];
1282
1283 if (current_description->transport_infos().size() <= msection_index) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001284 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001285 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001286
zhihuang1c378ed2017-08-17 14:10:50 -07001287 return current_description->transport_infos()[msection_index]
1288 .description.secure();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001289}
1290
Steve Anton8ffb9c32017-08-31 15:45:38 -07001291void MediaDescriptionOptions::AddAudioSender(
1292 const std::string& track_id,
1293 const std::vector<std::string>& stream_ids) {
zhihuang1c378ed2017-08-17 14:10:50 -07001294 RTC_DCHECK(type == MEDIA_TYPE_AUDIO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001295 AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001296}
1297
Steve Anton8ffb9c32017-08-31 15:45:38 -07001298void MediaDescriptionOptions::AddVideoSender(
1299 const std::string& track_id,
1300 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001301 const std::vector<RidDescription>& rids,
1302 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001303 int num_sim_layers) {
zhihuang1c378ed2017-08-17 14:10:50 -07001304 RTC_DCHECK(type == MEDIA_TYPE_VIDEO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001305 RTC_DCHECK(rids.empty() || num_sim_layers == 0)
1306 << "RIDs are the compliant way to indicate simulcast.";
1307 RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers));
1308 AddSenderInternal(track_id, stream_ids, rids, simulcast_layers,
1309 num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001310}
1311
zhihuang1c378ed2017-08-17 14:10:50 -07001312void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id,
1313 const std::string& stream_id) {
1314 RTC_DCHECK(type == MEDIA_TYPE_DATA);
Steve Anton8ffb9c32017-08-31 15:45:38 -07001315 // TODO(steveanton): Is it the case that RtpDataChannel will never have more
1316 // than one stream?
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001317 AddSenderInternal(track_id, {stream_id}, {}, SimulcastLayerList(), 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318}
1319
Steve Anton8ffb9c32017-08-31 15:45:38 -07001320void MediaDescriptionOptions::AddSenderInternal(
1321 const std::string& track_id,
1322 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001323 const std::vector<RidDescription>& rids,
1324 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001325 int num_sim_layers) {
1326 // TODO(steveanton): Support any number of stream ids.
1327 RTC_CHECK(stream_ids.size() == 1U);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001328 SenderOptions options;
1329 options.track_id = track_id;
1330 options.stream_ids = stream_ids;
1331 options.simulcast_layers = simulcast_layers;
1332 options.rids = rids;
1333 options.num_sim_layers = num_sim_layers;
1334 sender_options.push_back(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
1336
zhihuang1c378ed2017-08-17 14:10:50 -07001337bool MediaSessionOptions::HasMediaDescription(MediaType type) const {
1338 return std::find_if(media_description_options.begin(),
1339 media_description_options.end(),
1340 [type](const MediaDescriptionOptions& t) {
1341 return t.type == type;
1342 }) != media_description_options.end();
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001343}
1344
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1346 const TransportDescriptionFactory* transport_desc_factory)
zhihuang1c378ed2017-08-17 14:10:50 -07001347 : transport_desc_factory_(transport_desc_factory) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348
1349MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1350 ChannelManager* channel_manager,
1351 const TransportDescriptionFactory* transport_desc_factory)
zhihuang1c378ed2017-08-17 14:10:50 -07001352 : transport_desc_factory_(transport_desc_factory) {
ossudedfd282016-06-14 07:12:39 -07001353 channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_);
1354 channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
magjed3cf8ece2016-11-10 03:36:53 -08001356 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1358 channel_manager->GetSupportedDataCodecs(&data_codecs_);
zhihuang1c378ed2017-08-17 14:10:50 -07001359 ComputeAudioCodecsIntersectionAndUnion();
ossu075af922016-06-14 03:29:38 -07001360}
1361
ossudedfd282016-06-14 07:12:39 -07001362const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs()
1363 const {
ossu075af922016-06-14 03:29:38 -07001364 return audio_sendrecv_codecs_;
1365}
1366
1367const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const {
1368 return audio_send_codecs_;
1369}
1370
1371const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const {
1372 return audio_recv_codecs_;
1373}
1374
1375void MediaSessionDescriptionFactory::set_audio_codecs(
Yves Gerey665174f2018-06-19 15:03:05 +02001376 const AudioCodecs& send_codecs,
1377 const AudioCodecs& recv_codecs) {
ossu075af922016-06-14 03:29:38 -07001378 audio_send_codecs_ = send_codecs;
1379 audio_recv_codecs_ = recv_codecs;
zhihuang1c378ed2017-08-17 14:10:50 -07001380 ComputeAudioCodecsIntersectionAndUnion();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381}
1382
Amit Hilbuch77938e62018-12-21 09:23:38 -08001383static void AddUnifiedPlanExtensions(RtpHeaderExtensions* extensions) {
1384 RTC_DCHECK(extensions);
1385 // Unified Plan also offers the MID and RID header extensions.
1386 extensions->push_back(webrtc::RtpExtension(
1387 webrtc::RtpExtension::kMidUri, webrtc::RtpExtension::kMidDefaultId));
1388 extensions->push_back(webrtc::RtpExtension(
1389 webrtc::RtpExtension::kRidUri, webrtc::RtpExtension::kRidDefaultId));
1390 extensions->push_back(
1391 webrtc::RtpExtension(webrtc::RtpExtension::kRepairedRidUri,
1392 webrtc::RtpExtension::kRepairedRidDefaultId));
1393}
1394
1395RtpHeaderExtensions
1396MediaSessionDescriptionFactory::audio_rtp_header_extensions() const {
1397 RtpHeaderExtensions extensions = audio_rtp_extensions_;
1398 if (is_unified_plan_) {
1399 AddUnifiedPlanExtensions(&extensions);
1400 }
1401
1402 return extensions;
1403}
1404
1405RtpHeaderExtensions
1406MediaSessionDescriptionFactory::video_rtp_header_extensions() const {
1407 RtpHeaderExtensions extensions = video_rtp_extensions_;
1408 if (is_unified_plan_) {
1409 AddUnifiedPlanExtensions(&extensions);
1410 }
1411
1412 return extensions;
1413}
1414
Steve Anton6fe1fba2018-12-11 10:15:23 -08001415std::unique_ptr<SessionDescription> MediaSessionDescriptionFactory::CreateOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001416 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 const SessionDescription* current_description) const {
Steve Anton5c72e712018-12-10 14:25:30 -08001418 // Must have options for each existing section.
1419 if (current_description) {
1420 RTC_DCHECK_LE(current_description->contents().size(),
1421 session_options.media_description_options.size());
1422 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001424 IceCredentialsIterator ice_credentials(
1425 session_options.pooled_ice_credentials);
Steve Anton5c72e712018-12-10 14:25:30 -08001426
1427 std::vector<const ContentInfo*> current_active_contents;
1428 if (current_description) {
1429 current_active_contents =
1430 GetActiveContents(*current_description, session_options);
1431 }
1432
1433 StreamParamsVec current_streams =
1434 GetCurrentStreamParams(current_active_contents);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435
zhihuang1c378ed2017-08-17 14:10:50 -07001436 AudioCodecs offer_audio_codecs;
1437 VideoCodecs offer_video_codecs;
1438 DataCodecs offer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001439 GetCodecsForOffer(current_active_contents, &offer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001440 &offer_video_codecs, &offer_data_codecs);
ossu075af922016-06-14 03:29:38 -07001441
zhihuang1c378ed2017-08-17 14:10:50 -07001442 if (!session_options.vad_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443 // If application doesn't want CN codecs in offer.
zhihuang1c378ed2017-08-17 14:10:50 -07001444 StripCNCodecs(&offer_audio_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 }
zhihuang1c378ed2017-08-17 14:10:50 -07001446 FilterDataCodecs(&offer_data_codecs,
1447 session_options.data_channel_type == DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448
1449 RtpHeaderExtensions audio_rtp_extensions;
1450 RtpHeaderExtensions video_rtp_extensions;
Steve Anton8f66ddb2018-12-10 16:08:05 -08001451 GetRtpHdrExtsToOffer(current_active_contents, &audio_rtp_extensions,
1452 &video_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453
Steve Anton5c72e712018-12-10 14:25:30 -08001454 auto offer = absl::make_unique<SessionDescription>();
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001455
zhihuang1c378ed2017-08-17 14:10:50 -07001456 // Iterate through the media description options, matching with existing media
1457 // descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001458 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001459 for (const MediaDescriptionOptions& media_description_options :
1460 session_options.media_description_options) {
1461 const ContentInfo* current_content = nullptr;
1462 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001463 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001464 current_content = &current_description->contents()[msection_index];
Steve Antondcc3c022017-12-22 16:02:54 -08001465 // Media type must match unless this media section is being recycled.
Steve Anton5c72e712018-12-10 14:25:30 -08001466 RTC_DCHECK(current_content->name != media_description_options.mid ||
Steve Antondcc3c022017-12-22 16:02:54 -08001467 IsMediaContentOfType(current_content,
zhihuang1c378ed2017-08-17 14:10:50 -07001468 media_description_options.type));
1469 }
1470 switch (media_description_options.type) {
1471 case MEDIA_TYPE_AUDIO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001472 if (!AddAudioContentForOffer(
1473 media_description_options, session_options, current_content,
1474 current_description, audio_rtp_extensions, offer_audio_codecs,
1475 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001476 return nullptr;
1477 }
1478 break;
1479 case MEDIA_TYPE_VIDEO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001480 if (!AddVideoContentForOffer(
1481 media_description_options, session_options, current_content,
1482 current_description, video_rtp_extensions, offer_video_codecs,
1483 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001484 return nullptr;
1485 }
1486 break;
1487 case MEDIA_TYPE_DATA:
1488 if (!AddDataContentForOffer(media_description_options, session_options,
1489 current_content, current_description,
1490 offer_data_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001491 offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001492 return nullptr;
1493 }
1494 break;
1495 default:
1496 RTC_NOTREACHED();
1497 }
1498 ++msection_index;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 }
1500
1501 // Bundle the contents together, if we've been asked to do so, and update any
1502 // parameters that need to be tweaked for BUNDLE.
Steve Anton2bed3972019-01-04 17:04:30 -08001503 if (session_options.bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
zhihuang1c378ed2017-08-17 14:10:50 -07001505 for (const ContentInfo& content : offer->contents()) {
Steve Anton2bed3972019-01-04 17:04:30 -08001506 if (content.rejected) {
1507 continue;
1508 }
zhihuang1c378ed2017-08-17 14:10:50 -07001509 // TODO(deadbeef): There are conditions that make bundling two media
1510 // descriptions together illegal. For example, they use the same payload
1511 // type to represent different codecs, or same IDs for different header
1512 // extensions. We need to detect this and not try to bundle those media
1513 // descriptions together.
1514 offer_bundle.AddContentName(content.name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 }
Steve Anton2bed3972019-01-04 17:04:30 -08001516 if (!offer_bundle.content_names().empty()) {
1517 offer->AddGroup(offer_bundle);
1518 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1519 RTC_LOG(LS_ERROR)
1520 << "CreateOffer failed to UpdateTransportInfoForBundle.";
1521 return nullptr;
1522 }
1523 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1524 RTC_LOG(LS_ERROR)
1525 << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1526 return nullptr;
1527 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 }
1529 }
Steve Antone831b8c2018-02-01 12:22:16 -08001530
1531 // The following determines how to signal MSIDs to ensure compatibility with
1532 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001533 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001534 // Be conservative and signal using both a=msid and a=ssrc lines. Unified
1535 // Plan answerers will look at a=msid and Plan B answerers will look at the
1536 // a=ssrc MSID line.
1537 offer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1538 cricket::kMsidSignalingSsrcAttribute);
1539 } else {
1540 // Plan B always signals MSID using a=ssrc lines.
1541 offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1542 }
1543
Johannes Kron89f874e2018-11-12 10:25:48 +01001544 offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed);
1545
Steve Anton6fe1fba2018-12-11 10:15:23 -08001546 return offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547}
1548
Steve Anton6fe1fba2018-12-11 10:15:23 -08001549std::unique_ptr<SessionDescription>
1550MediaSessionDescriptionFactory::CreateAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07001551 const SessionDescription* offer,
1552 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 const SessionDescription* current_description) const {
deadbeefb7892532017-02-22 19:35:18 -08001554 if (!offer) {
1555 return nullptr;
1556 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001557
Steve Anton5c72e712018-12-10 14:25:30 -08001558 // Must have options for exactly as many sections as in the offer.
1559 RTC_DCHECK_EQ(offer->contents().size(),
1560 session_options.media_description_options.size());
1561
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001562 IceCredentialsIterator ice_credentials(
1563 session_options.pooled_ice_credentials);
1564
Steve Anton5c72e712018-12-10 14:25:30 -08001565 std::vector<const ContentInfo*> current_active_contents;
1566 if (current_description) {
1567 current_active_contents =
1568 GetActiveContents(*current_description, session_options);
1569 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570
Steve Anton5c72e712018-12-10 14:25:30 -08001571 StreamParamsVec current_streams =
1572 GetCurrentStreamParams(current_active_contents);
Johannes Kron0854eb62018-10-10 22:33:20 +02001573
zhihuang1c378ed2017-08-17 14:10:50 -07001574 // Get list of all possible codecs that respects existing payload type
1575 // mappings and uses a single payload type space.
1576 //
1577 // Note that these lists may be further filtered for each m= section; this
1578 // step is done just to establish the payload type mappings shared by all
1579 // sections.
1580 AudioCodecs answer_audio_codecs;
1581 VideoCodecs answer_video_codecs;
1582 DataCodecs answer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001583 GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001584 &answer_video_codecs, &answer_data_codecs);
1585
1586 if (!session_options.vad_enabled) {
1587 // If application doesn't want CN codecs in answer.
1588 StripCNCodecs(&answer_audio_codecs);
1589 }
1590 FilterDataCodecs(&answer_data_codecs,
1591 session_options.data_channel_type == DCT_SCTP);
1592
Steve Anton5c72e712018-12-10 14:25:30 -08001593 auto answer = absl::make_unique<SessionDescription>();
1594
1595 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1596 // group in the answer with the appropriate content names.
1597 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1598 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1599 // Transport info shared by the bundle group.
1600 std::unique_ptr<TransportInfo> bundle_transport;
1601
1602 answer->set_extmap_allow_mixed(offer->extmap_allow_mixed());
1603
zhihuang1c378ed2017-08-17 14:10:50 -07001604 // Iterate through the media description options, matching with existing
1605 // media descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001606 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001607 for (const MediaDescriptionOptions& media_description_options :
1608 session_options.media_description_options) {
1609 const ContentInfo* offer_content = &offer->contents()[msection_index];
1610 // Media types and MIDs must match between the remote offer and the
1611 // MediaDescriptionOptions.
1612 RTC_DCHECK(
1613 IsMediaContentOfType(offer_content, media_description_options.type));
1614 RTC_DCHECK(media_description_options.mid == offer_content->name);
1615 const ContentInfo* current_content = nullptr;
1616 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001617 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001618 current_content = &current_description->contents()[msection_index];
deadbeefb7892532017-02-22 19:35:18 -08001619 }
zhihuang1c378ed2017-08-17 14:10:50 -07001620 switch (media_description_options.type) {
1621 case MEDIA_TYPE_AUDIO:
1622 if (!AddAudioContentForAnswer(
1623 media_description_options, session_options, offer_content,
1624 offer, current_content, current_description,
1625 bundle_transport.get(), answer_audio_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001626 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001627 return nullptr;
1628 }
1629 break;
1630 case MEDIA_TYPE_VIDEO:
1631 if (!AddVideoContentForAnswer(
1632 media_description_options, session_options, offer_content,
1633 offer, current_content, current_description,
1634 bundle_transport.get(), answer_video_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001635 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001636 return nullptr;
1637 }
1638 break;
1639 case MEDIA_TYPE_DATA:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001640 if (!AddDataContentForAnswer(
1641 media_description_options, session_options, offer_content,
1642 offer, current_content, current_description,
1643 bundle_transport.get(), answer_data_codecs, &current_streams,
1644 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001645 return nullptr;
1646 }
1647 break;
1648 default:
1649 RTC_NOTREACHED();
1650 }
1651 ++msection_index;
deadbeefb7892532017-02-22 19:35:18 -08001652 // See if we can add the newly generated m= section to the BUNDLE group in
1653 // the answer.
1654 ContentInfo& added = answer->contents().back();
zhihuang1c378ed2017-08-17 14:10:50 -07001655 if (!added.rejected && session_options.bundle_enabled && offer_bundle &&
deadbeefb7892532017-02-22 19:35:18 -08001656 offer_bundle->HasContentName(added.name)) {
1657 answer_bundle.AddContentName(added.name);
1658 bundle_transport.reset(
1659 new TransportInfo(*answer->GetTransportInfoByName(added.name)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 }
1662
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001663 // If a BUNDLE group was offered, put a BUNDLE group in the answer even if
1664 // it's empty. RFC5888 says:
1665 //
1666 // A SIP entity that receives an offer that contains an "a=group" line
1667 // with semantics that are understood MUST return an answer that
1668 // contains an "a=group" line with the same semantics.
1669 if (offer_bundle) {
deadbeefb7892532017-02-22 19:35:18 -08001670 answer->AddGroup(answer_bundle);
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001671 }
deadbeefb7892532017-02-22 19:35:18 -08001672
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001673 if (answer_bundle.FirstContentName()) {
deadbeefb7892532017-02-22 19:35:18 -08001674 // Share the same ICE credentials and crypto params across all contents,
1675 // as BUNDLE requires.
1676 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001677 RTC_LOG(LS_ERROR)
1678 << "CreateAnswer failed to UpdateTransportInfoForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001679 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001680 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681
deadbeefb7892532017-02-22 19:35:18 -08001682 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001683 RTC_LOG(LS_ERROR)
1684 << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001685 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 }
1687 }
1688
Steve Antone831b8c2018-02-01 12:22:16 -08001689 // The following determines how to signal MSIDs to ensure compatibility with
1690 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001691 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001692 // Unified Plan needs to look at what the offer included to find the most
1693 // compatible answer.
1694 if (offer->msid_signaling() == 0) {
1695 // We end up here in one of three cases:
1696 // 1. An empty offer. We'll reply with an empty answer so it doesn't
1697 // matter what we pick here.
1698 // 2. A data channel only offer. We won't add any MSIDs to the answer so
1699 // it also doesn't matter what we pick here.
1700 // 3. Media that's either sendonly or inactive from the remote endpoint.
1701 // We don't have any information to say whether the endpoint is Plan B
1702 // or Unified Plan, so be conservative and send both.
1703 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1704 cricket::kMsidSignalingSsrcAttribute);
1705 } else if (offer->msid_signaling() ==
1706 (cricket::kMsidSignalingMediaSection |
1707 cricket::kMsidSignalingSsrcAttribute)) {
1708 // If both a=msid and a=ssrc MSID signaling methods were used, we're
1709 // probably talking to a Unified Plan endpoint so respond with just
1710 // a=msid.
1711 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection);
1712 } else {
1713 // Otherwise, it's clear which method the offerer is using so repeat that
1714 // back to them.
1715 answer->set_msid_signaling(offer->msid_signaling());
1716 }
1717 } else {
1718 // Plan B always signals MSID using a=ssrc lines.
1719 answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1720 }
1721
Steve Anton6fe1fba2018-12-11 10:15:23 -08001722 return answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
ossu075af922016-06-14 03:29:38 -07001725const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer(
1726 const RtpTransceiverDirection& direction) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001727 switch (direction) {
1728 // If stream is inactive - generate list as if sendrecv.
1729 case RtpTransceiverDirection::kSendRecv:
1730 case RtpTransceiverDirection::kInactive:
1731 return audio_sendrecv_codecs_;
1732 case RtpTransceiverDirection::kSendOnly:
1733 return audio_send_codecs_;
1734 case RtpTransceiverDirection::kRecvOnly:
1735 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001736 }
Steve Anton1d03a752017-11-27 14:30:09 -08001737 RTC_NOTREACHED();
1738 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001739}
1740
1741const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer(
1742 const RtpTransceiverDirection& offer,
1743 const RtpTransceiverDirection& answer) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001744 switch (answer) {
1745 // For inactive and sendrecv answers, generate lists as if we were to accept
1746 // the offer's direction. See RFC 3264 Section 6.1.
1747 case RtpTransceiverDirection::kSendRecv:
1748 case RtpTransceiverDirection::kInactive:
1749 return GetAudioCodecsForOffer(
1750 webrtc::RtpTransceiverDirectionReversed(offer));
1751 case RtpTransceiverDirection::kSendOnly:
ossu075af922016-06-14 03:29:38 -07001752 return audio_send_codecs_;
Steve Anton1d03a752017-11-27 14:30:09 -08001753 case RtpTransceiverDirection::kRecvOnly:
1754 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001755 }
Steve Anton1d03a752017-11-27 14:30:09 -08001756 RTC_NOTREACHED();
1757 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001758}
1759
Steve Anton5c72e712018-12-10 14:25:30 -08001760void MergeCodecsFromDescription(
1761 const std::vector<const ContentInfo*>& current_active_contents,
1762 AudioCodecs* audio_codecs,
1763 VideoCodecs* video_codecs,
1764 DataCodecs* data_codecs,
1765 UsedPayloadTypes* used_pltypes) {
1766 for (const ContentInfo* content : current_active_contents) {
1767 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001768 const AudioContentDescription* audio =
Steve Anton5c72e712018-12-10 14:25:30 -08001769 content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001770 MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001771 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001772 const VideoContentDescription* video =
Steve Anton5c72e712018-12-10 14:25:30 -08001773 content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001774 MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001775 } else if (IsMediaContentOfType(content, MEDIA_TYPE_DATA)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001776 const DataContentDescription* data =
Steve Anton5c72e712018-12-10 14:25:30 -08001777 content->media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001778 MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes);
1779 }
1780 }
1781}
1782
1783// Getting codecs for an offer involves these steps:
1784//
1785// 1. Construct payload type -> codec mappings for current description.
1786// 2. Add any reference codecs that weren't already present
1787// 3. For each individual media description (m= section), filter codecs based
1788// on the directional attribute (happens in another method).
1789void MediaSessionDescriptionFactory::GetCodecsForOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001790 const std::vector<const ContentInfo*>& current_active_contents,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 AudioCodecs* audio_codecs,
1792 VideoCodecs* video_codecs,
1793 DataCodecs* data_codecs) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 // First - get all codecs from the current description if the media type
zhihuang1c378ed2017-08-17 14:10:50 -07001795 // 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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800
Steve Anton5c72e712018-12-10 14:25:30 -08001801 // Add our codecs that are not in the current description.
zhihuang1c378ed2017-08-17 14:10:50 -07001802 MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes);
1803 MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1804 MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1805}
1806
1807// Getting codecs for an answer involves these steps:
1808//
1809// 1. Construct payload type -> codec mappings for current description.
1810// 2. Add any codecs from the offer that weren't already present.
1811// 3. Add any remaining codecs that weren't already present.
1812// 4. For each individual media description (m= section), filter codecs based
1813// on the directional attribute (happens in another method).
1814void MediaSessionDescriptionFactory::GetCodecsForAnswer(
Steve Anton5c72e712018-12-10 14:25:30 -08001815 const std::vector<const ContentInfo*>& current_active_contents,
1816 const SessionDescription& remote_offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001817 AudioCodecs* audio_codecs,
1818 VideoCodecs* video_codecs,
1819 DataCodecs* data_codecs) const {
zhihuang1c378ed2017-08-17 14:10:50 -07001820 // First - get all codecs from the current description if the media type
1821 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1822 // new media type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001823 UsedPayloadTypes used_pltypes;
1824 MergeCodecsFromDescription(current_active_contents, audio_codecs,
1825 video_codecs, data_codecs, &used_pltypes);
zhihuang1c378ed2017-08-17 14:10:50 -07001826
1827 // Second - filter out codecs that we don't support at all and should ignore.
1828 AudioCodecs filtered_offered_audio_codecs;
1829 VideoCodecs filtered_offered_video_codecs;
1830 DataCodecs filtered_offered_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001831 for (const ContentInfo& content : remote_offer.contents()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001832 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1833 const AudioContentDescription* audio =
Steve Antonb1c1de12017-12-21 15:14:30 -08001834 content.media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001835 for (const AudioCodec& offered_audio_codec : audio->codecs()) {
1836 if (!FindMatchingCodec<AudioCodec>(audio->codecs(),
1837 filtered_offered_audio_codecs,
1838 offered_audio_codec, nullptr) &&
1839 FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_,
1840 offered_audio_codec, nullptr)) {
1841 filtered_offered_audio_codecs.push_back(offered_audio_codec);
1842 }
1843 }
1844 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1845 const VideoContentDescription* video =
Steve Antonb1c1de12017-12-21 15:14:30 -08001846 content.media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001847 for (const VideoCodec& offered_video_codec : video->codecs()) {
1848 if (!FindMatchingCodec<VideoCodec>(video->codecs(),
1849 filtered_offered_video_codecs,
1850 offered_video_codec, nullptr) &&
1851 FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_,
1852 offered_video_codec, nullptr)) {
1853 filtered_offered_video_codecs.push_back(offered_video_codec);
1854 }
1855 }
1856 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1857 const DataContentDescription* data =
Steve Antonb1c1de12017-12-21 15:14:30 -08001858 content.media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001859 for (const DataCodec& offered_data_codec : data->codecs()) {
1860 if (!FindMatchingCodec<DataCodec>(data->codecs(),
1861 filtered_offered_data_codecs,
1862 offered_data_codec, nullptr) &&
1863 FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_,
1864 offered_data_codec, nullptr)) {
1865 filtered_offered_data_codecs.push_back(offered_data_codec);
1866 }
1867 }
1868 }
1869 }
1870
Steve Anton5c72e712018-12-10 14:25:30 -08001871 // Add codecs that are not in the current description but were in
zhihuang1c378ed2017-08-17 14:10:50 -07001872 // |remote_offer|.
1873 MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs,
1874 &used_pltypes);
1875 MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs,
1876 &used_pltypes);
1877 MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs,
1878 &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879}
1880
1881void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001882 const std::vector<const ContentInfo*>& current_active_contents,
zhihuang1c378ed2017-08-17 14:10:50 -07001883 RtpHeaderExtensions* offer_audio_extensions,
1884 RtpHeaderExtensions* offer_video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001885 // All header extensions allocated from the same range to avoid potential
1886 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 UsedRtpHeaderExtensionIds used_ids;
jbauch5869f502017-06-29 12:31:36 -07001888 RtpHeaderExtensions all_regular_extensions;
1889 RtpHeaderExtensions all_encrypted_extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890
1891 // First - get all extensions from the current description if the media type
1892 // is used.
1893 // Add them to |used_ids| so the local ids are not reused if a new media
1894 // type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001895 for (const ContentInfo* content : current_active_contents) {
1896 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
1897 const AudioContentDescription* audio =
1898 content->media_description()->as_audio();
1899 MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions,
1900 &all_regular_extensions, &all_encrypted_extensions,
1901 &used_ids);
1902 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
1903 const VideoContentDescription* video =
1904 content->media_description()->as_video();
1905 MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions,
1906 &all_regular_extensions, &all_encrypted_extensions,
1907 &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 }
1909 }
1910
Steve Anton5c72e712018-12-10 14:25:30 -08001911 // Add our default RTP header extensions that are not in the current
1912 // description.
Steve Anton8f66ddb2018-12-10 16:08:05 -08001913 MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions,
1914 &all_regular_extensions, &all_encrypted_extensions,
1915 &used_ids);
1916 MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions,
1917 &all_regular_extensions, &all_encrypted_extensions,
1918 &used_ids);
zhihuang1c378ed2017-08-17 14:10:50 -07001919
jbauch5869f502017-06-29 12:31:36 -07001920 // TODO(jbauch): Support adding encrypted header extensions to existing
1921 // sessions.
Steve Anton5c72e712018-12-10 14:25:30 -08001922 if (enable_encrypted_rtp_header_extensions_ &&
1923 current_active_contents.empty()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001924 AddEncryptedVersionsOfHdrExts(offer_audio_extensions,
1925 &all_encrypted_extensions, &used_ids);
1926 AddEncryptedVersionsOfHdrExts(offer_video_extensions,
1927 &all_encrypted_extensions, &used_ids);
jbauch5869f502017-06-29 12:31:36 -07001928 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929}
1930
1931bool MediaSessionDescriptionFactory::AddTransportOffer(
Yves Gerey665174f2018-06-19 15:03:05 +02001932 const std::string& content_name,
1933 const TransportOptions& transport_options,
1934 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001935 SessionDescription* offer_desc,
1936 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 if (!transport_desc_factory_)
Yves Gerey665174f2018-06-19 15:03:05 +02001938 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 const TransportDescription* current_tdesc =
1940 GetTransportDescription(content_name, current_desc);
kwiberg31022942016-03-11 14:18:21 -08001941 std::unique_ptr<TransportDescription> new_tdesc(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001942 transport_desc_factory_->CreateOffer(transport_options, current_tdesc,
1943 ice_credentials));
Steve Anton06817cd2018-12-18 15:55:30 -08001944 if (!new_tdesc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001945 RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name="
1946 << content_name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 }
Steve Anton06817cd2018-12-18 15:55:30 -08001948 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc));
1949 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950}
1951
Steve Anton1a9d3c32018-12-10 17:18:54 -08001952std::unique_ptr<TransportDescription>
1953MediaSessionDescriptionFactory::CreateTransportAnswer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 const std::string& content_name,
1955 const SessionDescription* offer_desc,
1956 const TransportOptions& transport_options,
deadbeefb7892532017-02-22 19:35:18 -08001957 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001958 bool require_transport_attributes,
1959 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 if (!transport_desc_factory_)
1961 return NULL;
1962 const TransportDescription* offer_tdesc =
1963 GetTransportDescription(content_name, offer_desc);
1964 const TransportDescription* current_tdesc =
1965 GetTransportDescription(content_name, current_desc);
deadbeefb7892532017-02-22 19:35:18 -08001966 return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1967 require_transport_attributes,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001968 current_tdesc, ice_credentials);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969}
1970
1971bool MediaSessionDescriptionFactory::AddTransportAnswer(
1972 const std::string& content_name,
1973 const TransportDescription& transport_desc,
1974 SessionDescription* answer_desc) const {
Steve Anton06817cd2018-12-18 15:55:30 -08001975 answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 return true;
1977}
1978
zhihuang1c378ed2017-08-17 14:10:50 -07001979// |audio_codecs| = set of all possible codecs that can be used, with correct
1980// payload type mappings
1981//
1982// |supported_audio_codecs| = set of codecs that are supported for the direction
1983// of this m= section
1984//
1985// acd->codecs() = set of previously negotiated codecs for this m= section
1986//
1987// The payload types should come from audio_codecs, but the order should come
1988// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
1989// change existing codec priority, and that new codecs are added with the right
1990// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001991bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001992 const MediaDescriptionOptions& media_description_options,
1993 const MediaSessionOptions& session_options,
1994 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001995 const SessionDescription* current_description,
1996 const RtpHeaderExtensions& audio_rtp_extensions,
1997 const AudioCodecs& audio_codecs,
1998 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001999 SessionDescription* desc,
2000 IceCredentialsIterator* ice_credentials) const {
zhihuang1c378ed2017-08-17 14:10:50 -07002001 // Filter audio_codecs (which includes all codecs, with correctly remapped
2002 // payload types) based on transceiver direction.
2003 const AudioCodecs& supported_audio_codecs =
2004 GetAudioCodecsForOffer(media_description_options.direction);
2005
2006 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002007 // Add the codecs from current content if it exists and is not rejected nor
2008 // recycled.
2009 if (current_content && !current_content->rejected &&
2010 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002011 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002012 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002013 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002014 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002015 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2016 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002017 filtered_codecs.push_back(codec);
2018 }
2019 }
2020 }
2021 // Add other supported audio codecs.
2022 AudioCodec found_codec;
2023 for (const AudioCodec& codec : supported_audio_codecs) {
2024 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
2025 codec, &found_codec) &&
2026 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2027 codec, nullptr)) {
2028 // Use the |found_codec| from |audio_codecs| because it has the correctly
2029 // mapped payload type.
2030 filtered_codecs.push_back(found_codec);
2031 }
2032 }
deadbeef44f08192015-12-15 16:20:09 -08002033
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002034 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002035 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2036 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002037
kwiberg31022942016-03-11 14:18:21 -08002038 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002039 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002040 GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options,
2041 &crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002042 if (!CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08002043 media_description_options, session_options, filtered_codecs,
2044 sdes_policy, GetCryptos(current_content), crypto_suites,
2045 audio_rtp_extensions, current_streams, audio.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002046 return false;
2047 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002048
2049 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2050 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002051
Steve Anton4e70a722017-11-28 14:57:10 -08002052 audio->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002053
Steve Anton5adfafd2017-12-20 16:34:00 -08002054 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002055 media_description_options.stopped, audio.release());
2056 if (!AddTransportOffer(media_description_options.mid,
2057 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002058 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002059 return false;
2060 }
2061
2062 return true;
2063}
2064
2065bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002066 const MediaDescriptionOptions& media_description_options,
2067 const MediaSessionOptions& session_options,
2068 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002069 const SessionDescription* current_description,
2070 const RtpHeaderExtensions& video_rtp_extensions,
2071 const VideoCodecs& video_codecs,
2072 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002073 SessionDescription* desc,
2074 IceCredentialsIterator* ice_credentials) const {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002075 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002076 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2077 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002078
kwiberg31022942016-03-11 14:18:21 -08002079 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002080 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002081 GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options,
2082 &crypto_suites);
2083
2084 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002085 // Add the codecs from current content if it exists and is not rejected nor
2086 // recycled.
2087 if (current_content && !current_content->rejected &&
2088 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002089 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002090 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002091 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002092 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002093 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002094 nullptr)) {
2095 filtered_codecs.push_back(codec);
2096 }
2097 }
2098 }
2099 // Add other supported video codecs.
2100 VideoCodec found_codec;
2101 for (const VideoCodec& codec : video_codecs_) {
2102 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
2103 &found_codec) &&
2104 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2105 nullptr)) {
2106 // Use the |found_codec| from |video_codecs| because it has the correctly
2107 // mapped payload type.
2108 filtered_codecs.push_back(found_codec);
2109 }
2110 }
2111
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002112 if (!CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08002113 media_description_options, session_options, filtered_codecs,
2114 sdes_policy, GetCryptos(current_content), crypto_suites,
2115 video_rtp_extensions, current_streams, video.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002116 return false;
2117 }
2118
zhihuang1c378ed2017-08-17 14:10:50 -07002119 video->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002120
2121 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2122 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002123
Steve Anton4e70a722017-11-28 14:57:10 -08002124 video->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002125
Steve Anton5adfafd2017-12-20 16:34:00 -08002126 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002127 media_description_options.stopped, video.release());
2128 if (!AddTransportOffer(media_description_options.mid,
2129 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002130 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002131 return false;
2132 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002133 return true;
2134}
2135
2136bool MediaSessionDescriptionFactory::AddDataContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002137 const MediaDescriptionOptions& media_description_options,
2138 const MediaSessionOptions& session_options,
2139 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002140 const SessionDescription* current_description,
zhihuang1c378ed2017-08-17 14:10:50 -07002141 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002142 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002143 SessionDescription* desc,
2144 IceCredentialsIterator* ice_credentials) const {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002145 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2146
kwiberg31022942016-03-11 14:18:21 -08002147 std::unique_ptr<DataContentDescription> data(new DataContentDescription());
zhihuang1c378ed2017-08-17 14:10:50 -07002148 bool is_sctp = (session_options.data_channel_type == DCT_SCTP);
2149 // If the DataChannel type is not specified, use the DataChannel type in
2150 // the current description.
2151 if (session_options.data_channel_type == DCT_NONE && current_content) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002152 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA));
Steve Antonb1c1de12017-12-21 15:14:30 -08002153 is_sctp = (current_content->media_description()->protocol() ==
2154 kMediaProtocolSctp);
zhihuang1c378ed2017-08-17 14:10:50 -07002155 }
deadbeef44f08192015-12-15 16:20:09 -08002156
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002157 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002158 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2159 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002160 std::vector<std::string> crypto_suites;
2161 if (is_sctp) {
2162 // SDES doesn't make sense for SCTP, so we disable it, and we only
2163 // get SDES crypto suites for RTP-based data channels.
2164 sdes_policy = cricket::SEC_DISABLED;
2165 // Unlike SetMediaProtocol below, we need to set the protocol
2166 // before we call CreateMediaContentOffer. Otherwise,
2167 // CreateMediaContentOffer won't know this is SCTP and will
2168 // generate SSRCs rather than SIDs.
deadbeef8b7e9ad2017-05-25 09:38:55 -07002169 // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once
2170 // it's safe to do so. Older versions of webrtc would reject these
2171 // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706.
Yves Gerey665174f2018-06-19 15:03:05 +02002172 data->set_protocol(secure_transport ? kMediaProtocolDtlsSctp
2173 : kMediaProtocolSctp);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002174 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002175 GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -07002176 &crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002177 }
2178
zhihuang1c378ed2017-08-17 14:10:50 -07002179 // Even SCTP uses a "codec".
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002180 if (!CreateMediaContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08002181 media_description_options, session_options, data_codecs, sdes_policy,
2182 GetCryptos(current_content), crypto_suites, RtpHeaderExtensions(),
2183 current_streams, data.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002184 return false;
2185 }
2186
2187 if (is_sctp) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002188 desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp,
zhihuang1c378ed2017-08-17 14:10:50 -07002189 data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002190 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002191 data->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002192 SetMediaProtocol(secure_transport, data.get());
Steve Anton5adfafd2017-12-20 16:34:00 -08002193 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002194 media_description_options.stopped, data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002195 }
zhihuang1c378ed2017-08-17 14:10:50 -07002196 if (!AddTransportOffer(media_description_options.mid,
2197 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002198 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002199 return false;
2200 }
2201 return true;
2202}
2203
zhihuang1c378ed2017-08-17 14:10:50 -07002204// |audio_codecs| = set of all possible codecs that can be used, with correct
2205// payload type mappings
2206//
2207// |supported_audio_codecs| = set of codecs that are supported for the direction
2208// of this m= section
2209//
2210// acd->codecs() = set of previously negotiated codecs for this m= section
2211//
2212// The payload types should come from audio_codecs, but the order should come
2213// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
2214// change existing codec priority, and that new codecs are added with the right
2215// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002216bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002217 const MediaDescriptionOptions& media_description_options,
2218 const MediaSessionOptions& session_options,
2219 const ContentInfo* offer_content,
2220 const SessionDescription* offer_description,
2221 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002222 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002223 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002224 const AudioCodecs& audio_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002225 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002226 SessionDescription* answer,
2227 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002228 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002229 const AudioContentDescription* offer_audio_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002230 offer_content->media_description()->as_audio();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002231
Steve Anton1a9d3c32018-12-10 17:18:54 -08002232 std::unique_ptr<TransportDescription> audio_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002233 media_description_options.mid, offer_description,
2234 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002235 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002236 if (!audio_transport) {
2237 return false;
2238 }
2239
zhihuang1c378ed2017-08-17 14:10:50 -07002240 // Pick codecs based on the requested communications direction in the offer
2241 // and the selected direction in the answer.
2242 // Note these will be filtered one final time in CreateMediaContentAnswer.
2243 auto wants_rtd = media_description_options.direction;
Steve Anton4e70a722017-11-28 14:57:10 -08002244 auto offer_rtd = offer_audio_description->direction();
ossu075af922016-06-14 03:29:38 -07002245 auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd);
zhihuang1c378ed2017-08-17 14:10:50 -07002246 AudioCodecs supported_audio_codecs =
2247 GetAudioCodecsForAnswer(offer_rtd, answer_rtd);
2248
2249 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002250 // Add the codecs from current content if it exists and is not rejected nor
2251 // recycled.
2252 if (current_content && !current_content->rejected &&
2253 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002254 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002255 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002256 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002257 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002258 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2259 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002260 filtered_codecs.push_back(codec);
2261 }
2262 }
2263 }
2264 // Add other supported audio codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002265 for (const AudioCodec& codec : supported_audio_codecs) {
2266 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
Zhi Huang6f367472017-11-22 13:20:02 -08002267 codec, nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002268 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2269 codec, nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002270 // We should use the local codec with local parameters and the codec id
2271 // would be correctly mapped in |NegotiateCodecs|.
2272 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002273 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002274 }
2275
zhihuang1c378ed2017-08-17 14:10:50 -07002276 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2277 session_options.bundle_enabled;
kwiberg31022942016-03-11 14:18:21 -08002278 std::unique_ptr<AudioContentDescription> audio_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002279 new AudioContentDescription());
2280 // Do not require or create SDES cryptos if DTLS is used.
2281 cricket::SecurePolicy sdes_policy =
2282 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
2283 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002284 offer_audio_description, media_description_options, session_options,
2285 filtered_codecs, sdes_policy, GetCryptos(current_content),
Steve Anton8f66ddb2018-12-10 16:08:05 -08002286 audio_rtp_header_extensions(),
Steve Anton1b8773d2018-04-06 11:13:34 -07002287 enable_encrypted_rtp_header_extensions_, current_streams,
2288 bundle_enabled, audio_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002289 return false; // Fails the session setup.
2290 }
2291
deadbeefb7892532017-02-22 19:35:18 -08002292 bool secure = bundle_transport ? bundle_transport->description.secure()
2293 : audio_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002294 bool rejected = media_description_options.stopped ||
2295 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002296 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
2297 audio_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002298 if (!AddTransportAnswer(media_description_options.mid,
2299 *(audio_transport.get()), answer)) {
2300 return false;
2301 }
2302
2303 if (rejected) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002304 RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid
2305 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002306 }
2307
zhihuang1c378ed2017-08-17 14:10:50 -07002308 answer->AddContent(media_description_options.mid, offer_content->type,
2309 rejected, audio_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002310 return true;
2311}
2312
2313bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002314 const MediaDescriptionOptions& media_description_options,
2315 const MediaSessionOptions& session_options,
2316 const ContentInfo* offer_content,
2317 const SessionDescription* offer_description,
2318 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002319 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002320 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002321 const VideoCodecs& video_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002322 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002323 SessionDescription* answer,
2324 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002325 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002326 const VideoContentDescription* offer_video_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002327 offer_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002328
Steve Anton1a9d3c32018-12-10 17:18:54 -08002329 std::unique_ptr<TransportDescription> video_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002330 media_description_options.mid, offer_description,
2331 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002332 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002333 if (!video_transport) {
2334 return false;
2335 }
2336
zhihuang1c378ed2017-08-17 14:10:50 -07002337 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002338 // Add the codecs from current content if it exists and is not rejected nor
2339 // recycled.
2340 if (current_content && !current_content->rejected &&
2341 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002342 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002343 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002344 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002345 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002346 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002347 nullptr)) {
2348 filtered_codecs.push_back(codec);
2349 }
2350 }
2351 }
2352 // Add other supported video codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002353 for (const VideoCodec& codec : video_codecs_) {
2354 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
Zhi Huang6f367472017-11-22 13:20:02 -08002355 nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002356 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2357 nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002358 // We should use the local codec with local parameters and the codec id
2359 // would be correctly mapped in |NegotiateCodecs|.
2360 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002361 }
2362 }
2363
2364 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2365 session_options.bundle_enabled;
2366
kwiberg31022942016-03-11 14:18:21 -08002367 std::unique_ptr<VideoContentDescription> video_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002368 new VideoContentDescription());
2369 // Do not require or create SDES cryptos if DTLS is used.
2370 cricket::SecurePolicy sdes_policy =
2371 video_transport->secure() ? cricket::SEC_DISABLED : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002372 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002373 offer_video_description, media_description_options, session_options,
2374 filtered_codecs, sdes_policy, GetCryptos(current_content),
Steve Anton8f66ddb2018-12-10 16:08:05 -08002375 video_rtp_header_extensions(),
Steve Anton1b8773d2018-04-06 11:13:34 -07002376 enable_encrypted_rtp_header_extensions_, current_streams,
2377 bundle_enabled, video_answer.get())) {
zhihuang1c378ed2017-08-17 14:10:50 -07002378 return false; // Failed the sessin setup.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002379 }
deadbeefb7892532017-02-22 19:35:18 -08002380 bool secure = bundle_transport ? bundle_transport->description.secure()
2381 : video_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002382 bool rejected = media_description_options.stopped ||
2383 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002384 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
2385 video_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002386 if (!AddTransportAnswer(media_description_options.mid,
2387 *(video_transport.get()), answer)) {
2388 return false;
2389 }
2390
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002391 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002392 video_answer->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002393 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002394 RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid
2395 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002396 }
zhihuang1c378ed2017-08-17 14:10:50 -07002397 answer->AddContent(media_description_options.mid, offer_content->type,
2398 rejected, video_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002399 return true;
2400}
2401
2402bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002403 const MediaDescriptionOptions& media_description_options,
2404 const MediaSessionOptions& session_options,
2405 const ContentInfo* offer_content,
2406 const SessionDescription* offer_description,
2407 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002408 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002409 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002410 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002411 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002412 SessionDescription* answer,
2413 IceCredentialsIterator* ice_credentials) const {
Steve Anton1a9d3c32018-12-10 17:18:54 -08002414 std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002415 media_description_options.mid, offer_description,
2416 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002417 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002418 if (!data_transport) {
2419 return false;
2420 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002421
kwiberg31022942016-03-11 14:18:21 -08002422 std::unique_ptr<DataContentDescription> data_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002423 new DataContentDescription());
2424 // Do not require or create SDES cryptos if DTLS is used.
2425 cricket::SecurePolicy sdes_policy =
2426 data_transport->secure() ? cricket::SEC_DISABLED : secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002427 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2428 session_options.bundle_enabled;
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002429 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA));
2430 const DataContentDescription* offer_data_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002431 offer_content->media_description()->as_data();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002432 if (!CreateMediaContentAnswer(
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002433 offer_data_description, media_description_options, session_options,
2434 data_codecs, sdes_policy, GetCryptos(current_content),
2435 RtpHeaderExtensions(), enable_encrypted_rtp_header_extensions_,
2436 current_streams, bundle_enabled, data_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002437 return false; // Fails the session setup.
2438 }
2439
zstein4b2e0822017-02-17 19:48:38 -08002440 // Respond with sctpmap if the offer uses sctpmap.
zstein4b2e0822017-02-17 19:48:38 -08002441 bool offer_uses_sctpmap = offer_data_description->use_sctpmap();
2442 data_answer->set_use_sctpmap(offer_uses_sctpmap);
2443
deadbeefb7892532017-02-22 19:35:18 -08002444 bool secure = bundle_transport ? bundle_transport->description.secure()
2445 : data_transport->secure();
2446
zhihuang1c378ed2017-08-17 14:10:50 -07002447 bool rejected = session_options.data_channel_type == DCT_NONE ||
2448 media_description_options.stopped ||
2449 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002450 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
2451 data_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002452 if (!AddTransportAnswer(media_description_options.mid,
2453 *(data_transport.get()), answer)) {
2454 return false;
2455 }
2456
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002457 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002458 data_answer->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002459 } else {
2460 // RFC 3264
2461 // The answer MUST contain the same number of m-lines as the offer.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002462 RTC_LOG(LS_INFO) << "Data is not supported in the answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002463 }
zhihuang1c378ed2017-08-17 14:10:50 -07002464 answer->AddContent(media_description_options.mid, offer_content->type,
2465 rejected, data_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002466 return true;
2467}
2468
zhihuang1c378ed2017-08-17 14:10:50 -07002469void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() {
2470 audio_sendrecv_codecs_.clear();
2471 all_audio_codecs_.clear();
2472 // Compute the audio codecs union.
2473 for (const AudioCodec& send : audio_send_codecs_) {
2474 all_audio_codecs_.push_back(send);
2475 if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_,
2476 send, nullptr)) {
2477 // It doesn't make sense to have an RTX codec we support sending but not
2478 // receiving.
2479 RTC_DCHECK(!IsRtxCodec(send));
2480 }
2481 }
2482 for (const AudioCodec& recv : audio_recv_codecs_) {
2483 if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_,
2484 recv, nullptr)) {
2485 all_audio_codecs_.push_back(recv);
2486 }
2487 }
2488 // Use NegotiateCodecs to merge our codec lists, since the operation is
2489 // essentially the same. Put send_codecs as the offered_codecs, which is the
2490 // order we'd like to follow. The reasoning is that encoding is usually more
2491 // expensive than decoding, and prioritizing a codec in the send list probably
2492 // means it's a codec we can handle efficiently.
2493 NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_,
2494 &audio_sendrecv_codecs_);
2495}
2496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497bool IsMediaContent(const ContentInfo* content) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002498 return (content && (content->type == MediaProtocolType::kRtp ||
2499 content->type == MediaProtocolType::kSctp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500}
2501
2502bool IsAudioContent(const ContentInfo* content) {
2503 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
2504}
2505
2506bool IsVideoContent(const ContentInfo* content) {
2507 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
2508}
2509
2510bool IsDataContent(const ContentInfo* content) {
2511 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
2512}
2513
deadbeef0ed85b22016-02-23 17:24:52 -08002514const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
2515 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002516 for (const ContentInfo& content : contents) {
2517 if (IsMediaContentOfType(&content, media_type)) {
2518 return &content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519 }
2520 }
deadbeef0ed85b22016-02-23 17:24:52 -08002521 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522}
2523
2524const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
2525 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2526}
2527
2528const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
2529 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2530}
2531
2532const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
2533 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2534}
2535
Steve Antonad7bffc2018-01-22 10:21:56 -08002536const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
2537 MediaType media_type) {
deadbeef0ed85b22016-02-23 17:24:52 -08002538 if (sdesc == nullptr) {
2539 return nullptr;
2540 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002541
2542 return GetFirstMediaContent(sdesc->contents(), media_type);
2543}
2544
2545const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
2546 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2547}
2548
2549const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
2550 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2551}
2552
2553const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
2554 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2555}
2556
2557const MediaContentDescription* GetFirstMediaContentDescription(
Yves Gerey665174f2018-06-19 15:03:05 +02002558 const SessionDescription* sdesc,
2559 MediaType media_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002560 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002561 return (content ? content->media_description() : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002562}
2563
2564const AudioContentDescription* GetFirstAudioContentDescription(
2565 const SessionDescription* sdesc) {
2566 return static_cast<const AudioContentDescription*>(
2567 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2568}
2569
2570const VideoContentDescription* GetFirstVideoContentDescription(
2571 const SessionDescription* sdesc) {
2572 return static_cast<const VideoContentDescription*>(
2573 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2574}
2575
2576const DataContentDescription* GetFirstDataContentDescription(
2577 const SessionDescription* sdesc) {
2578 return static_cast<const DataContentDescription*>(
2579 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2580}
2581
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002582//
2583// Non-const versions of the above functions.
2584//
2585
Steve Anton36b29d12017-10-30 09:57:42 -07002586ContentInfo* GetFirstMediaContent(ContentInfos* contents,
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002587 MediaType media_type) {
Steve Anton36b29d12017-10-30 09:57:42 -07002588 for (ContentInfo& content : *contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002589 if (IsMediaContentOfType(&content, media_type)) {
2590 return &content;
2591 }
2592 }
2593 return nullptr;
2594}
2595
Steve Anton36b29d12017-10-30 09:57:42 -07002596ContentInfo* GetFirstAudioContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002597 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2598}
2599
Steve Anton36b29d12017-10-30 09:57:42 -07002600ContentInfo* GetFirstVideoContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002601 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2602}
2603
Steve Anton36b29d12017-10-30 09:57:42 -07002604ContentInfo* GetFirstDataContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002605 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2606}
2607
Steve Antonad7bffc2018-01-22 10:21:56 -08002608ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
2609 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002610 if (sdesc == nullptr) {
2611 return nullptr;
2612 }
2613
Steve Anton36b29d12017-10-30 09:57:42 -07002614 return GetFirstMediaContent(&sdesc->contents(), media_type);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002615}
2616
2617ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) {
2618 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2619}
2620
2621ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) {
2622 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2623}
2624
2625ContentInfo* GetFirstDataContent(SessionDescription* sdesc) {
2626 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2627}
2628
2629MediaContentDescription* GetFirstMediaContentDescription(
2630 SessionDescription* sdesc,
2631 MediaType media_type) {
2632 ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002633 return (content ? content->media_description() : nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002634}
2635
2636AudioContentDescription* GetFirstAudioContentDescription(
2637 SessionDescription* sdesc) {
2638 return static_cast<AudioContentDescription*>(
2639 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2640}
2641
2642VideoContentDescription* GetFirstVideoContentDescription(
2643 SessionDescription* sdesc) {
2644 return static_cast<VideoContentDescription*>(
2645 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2646}
2647
2648DataContentDescription* GetFirstDataContentDescription(
2649 SessionDescription* sdesc) {
2650 return static_cast<DataContentDescription*>(
2651 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2652}
2653
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002654} // namespace cricket