blob: b739e904fde6247be1a3ad8aced36d51aef36855 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/media_session.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Elad Alon157540a2019-02-08 23:37:52 +010013#include <algorithm>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <functional>
15#include <map>
kwiberg31022942016-03-11 14:18:21 -080016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <set>
deadbeef67cf2c12016-04-13 10:07:16 -070018#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <utility>
20
Steve Anton64b626b2019-01-28 17:25:26 -080021#include "absl/algorithm/container.h"
Steve Anton5c72e712018-12-10 14:25:30 -080022#include "absl/memory/memory.h"
Niels Möller2edab4c2018-10-22 09:48:08 +020023#include "absl/strings/match.h"
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020024#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "api/crypto_params.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "media/base/h264_profile_level_id.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "media/base/media_constants.h"
28#include "p2p/base/p2p_constants.h"
29#include "pc/channel_manager.h"
Harald Alvestrand26bf7c42019-04-23 05:20:17 +000030#include "pc/media_protocol_names.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "pc/rtp_media_utils.h"
32#include "pc/srtp_filter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/checks.h"
34#include "rtc_base/helpers.h"
35#include "rtc_base/logging.h"
Artem Titova76af0c2018-07-23 17:38:12 +020036#include "rtc_base/third_party/base64/base64.h"
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080037#include "rtc_base/unique_id_generator.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
39namespace {
Steve Anton1d03a752017-11-27 14:30:09 -080040
Amit Hilbuchdbb49df2019-01-23 14:54:24 -080041using rtc::UniqueRandomIdGenerator;
Steve Anton1d03a752017-11-27 14:30:09 -080042using webrtc::RtpTransceiverDirection;
43
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044const char kInline[] = "inline:";
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080045
Benjamin Wrighta54daf12018-10-11 15:33:17 -070046void GetSupportedSdesCryptoSuiteNames(
47 void (*func)(const webrtc::CryptoOptions&, std::vector<int>*),
48 const webrtc::CryptoOptions& crypto_options,
49 std::vector<std::string>* names) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080050 std::vector<int> crypto_suites;
jbauchcb560652016-08-04 05:20:32 -070051 func(crypto_options, &crypto_suites);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080052 for (const auto crypto : crypto_suites) {
53 names->push_back(rtc::SrtpCryptoSuiteToName(crypto));
54 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080055}
Elad Alon157540a2019-02-08 23:37:52 +010056
terelius8c011e52016-04-26 05:28:11 -070057} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59namespace cricket {
60
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061// RTP Profile names
62// http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
63// RFC4585
64const char kMediaProtocolAvpf[] = "RTP/AVPF";
65// RFC5124
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +000066const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
67
deadbeeff3938292015-07-15 12:20:53 -070068// We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
69// but we tolerate "RTP/SAVPF" in offers we receive, for compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070const char kMediaProtocolSavpf[] = "RTP/SAVPF";
71
deadbeef8b7e9ad2017-05-25 09:38:55 -070072// Note that the below functions support some protocol strings purely for
73// legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names
74// and Interoperability.
75
76static bool IsDtlsRtp(const std::string& protocol) {
77 // Most-likely values first.
78 return protocol == "UDP/TLS/RTP/SAVPF" || protocol == "TCP/TLS/RTP/SAVPF" ||
79 protocol == "UDP/TLS/RTP/SAVP" || protocol == "TCP/TLS/RTP/SAVP";
80}
81
82static bool IsPlainRtp(const std::string& protocol) {
83 // Most-likely values first.
84 return protocol == "RTP/SAVPF" || protocol == "RTP/AVPF" ||
85 protocol == "RTP/SAVP" || protocol == "RTP/AVP";
86}
87
Steve Anton1d03a752017-11-27 14:30:09 -080088static RtpTransceiverDirection NegotiateRtpTransceiverDirection(
89 RtpTransceiverDirection offer,
90 RtpTransceiverDirection wants) {
91 bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer);
92 bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer);
93 bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants);
94 bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants);
95 return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send,
96 offer_send && wants_recv);
ossu075af922016-06-14 03:29:38 -070097}
98
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099static bool IsMediaContentOfType(const ContentInfo* content,
100 MediaType media_type) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800101 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 return false;
103 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800104 return content->media_description()->type() == media_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105}
106
Yves Gerey665174f2018-06-19 15:03:05 +0200107static bool CreateCryptoParams(int tag,
108 const std::string& cipher,
Steve Anton3a66edf2018-09-10 12:57:37 -0700109 CryptoParams* crypto_out) {
jbauchcb560652016-08-04 05:20:32 -0700110 int key_len;
111 int salt_len;
Yves Gerey665174f2018-06-19 15:03:05 +0200112 if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher),
113 &key_len, &salt_len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 return false;
115 }
jbauchcb560652016-08-04 05:20:32 -0700116
117 int master_key_len = key_len + salt_len;
118 std::string master_key;
119 if (!rtc::CreateRandomData(master_key_len, &master_key)) {
120 return false;
121 }
122
kwiberg352444f2016-11-28 15:58:53 -0800123 RTC_CHECK_EQ(master_key_len, master_key.size());
jbauchcb560652016-08-04 05:20:32 -0700124 std::string key = rtc::Base64::Encode(master_key);
125
Steve Anton3a66edf2018-09-10 12:57:37 -0700126 crypto_out->tag = tag;
127 crypto_out->cipher_suite = cipher;
128 crypto_out->key_params = kInline;
129 crypto_out->key_params += key;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 return true;
131}
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133static bool AddCryptoParams(const std::string& cipher_suite,
Steve Anton3a66edf2018-09-10 12:57:37 -0700134 CryptoParamsVec* cryptos_out) {
135 int size = static_cast<int>(cryptos_out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
Steve Anton3a66edf2018-09-10 12:57:37 -0700137 cryptos_out->resize(size + 1);
138 return CreateCryptoParams(size, cipher_suite, &cryptos_out->at(size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139}
140
141void AddMediaCryptos(const CryptoParamsVec& cryptos,
142 MediaContentDescription* media) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700143 for (const CryptoParams& crypto : cryptos) {
144 media->AddCrypto(crypto);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145 }
146}
147
148bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
149 MediaContentDescription* media) {
150 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700151 for (const std::string& crypto_suite : crypto_suites) {
152 if (!AddCryptoParams(crypto_suite, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 return false;
154 }
155 }
156 AddMediaCryptos(cryptos, media);
157 return true;
158}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159
zhihuang1c378ed2017-08-17 14:10:50 -0700160const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800161 if (!content || !content->media_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700162 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800164 return &content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165}
166
167bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
168 const CryptoParams& crypto,
Steve Anton3a66edf2018-09-10 12:57:37 -0700169 CryptoParams* crypto_out) {
Steve Anton64b626b2019-01-28 17:25:26 -0800170 auto it = absl::c_find_if(
171 cryptos, [&crypto](const CryptoParams& c) { return crypto.Matches(c); });
Steve Anton3a66edf2018-09-10 12:57:37 -0700172 if (it == cryptos.end()) {
173 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700175 *crypto_out = *it;
176 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177}
178
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700179// For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the
180// low overhead.
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700181void GetSupportedAudioSdesCryptoSuites(
182 const webrtc::CryptoOptions& crypto_options,
183 std::vector<int>* crypto_suites) {
184 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700185 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
186 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
187 }
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700188 if (crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher) {
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700189 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32);
190 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800191 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192}
193
deadbeef7914b8c2017-04-21 03:23:33 -0700194void GetSupportedAudioSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700195 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800196 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700197 GetSupportedSdesCryptoSuiteNames(GetSupportedAudioSdesCryptoSuites,
198 crypto_options, crypto_suite_names);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199}
200
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700201void GetSupportedVideoSdesCryptoSuites(
202 const webrtc::CryptoOptions& crypto_options,
203 std::vector<int>* crypto_suites) {
204 if (crypto_options.srtp.enable_gcm_crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700205 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
206 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
207 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800208 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209}
210
deadbeef7914b8c2017-04-21 03:23:33 -0700211void GetSupportedVideoSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700212 const webrtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800213 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700214 GetSupportedSdesCryptoSuiteNames(GetSupportedVideoSdesCryptoSuites,
215 crypto_options, crypto_suite_names);
216}
217
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700218void GetSupportedDataSdesCryptoSuites(
219 const webrtc::CryptoOptions& crypto_options,
220 std::vector<int>* crypto_suites) {
221 if (crypto_options.srtp.enable_gcm_crypto_suites) {
deadbeef7914b8c2017-04-21 03:23:33 -0700222 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
223 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
224 }
225 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
226}
227
228void GetSupportedDataSdesCryptoSuiteNames(
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700229 const webrtc::CryptoOptions& crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -0700230 std::vector<std::string>* crypto_suite_names) {
231 GetSupportedSdesCryptoSuiteNames(GetSupportedDataSdesCryptoSuites,
232 crypto_options, crypto_suite_names);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800233}
234
jbauchcb560652016-08-04 05:20:32 -0700235// Support any GCM cipher (if enabled through options). For video support only
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700236// 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated (if enabled) unless
237// bundle is enabled because it is low overhead.
jbauchcb560652016-08-04 05:20:32 -0700238// Pick the crypto in the list that is supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239static bool SelectCrypto(const MediaContentDescription* offer,
240 bool bundle,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700241 const webrtc::CryptoOptions& crypto_options,
Steve Anton3a66edf2018-09-10 12:57:37 -0700242 CryptoParams* crypto_out) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000243 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
244 const CryptoParamsVec& cryptos = offer->cryptos();
245
Steve Anton3a66edf2018-09-10 12:57:37 -0700246 for (const CryptoParams& crypto : cryptos) {
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700247 if ((crypto_options.srtp.enable_gcm_crypto_suites &&
Steve Anton3a66edf2018-09-10 12:57:37 -0700248 rtc::IsGcmCryptoSuiteName(crypto.cipher_suite)) ||
249 rtc::CS_AES_CM_128_HMAC_SHA1_80 == crypto.cipher_suite ||
250 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == crypto.cipher_suite && audio &&
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700251 !bundle && crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher)) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700252 return CreateCryptoParams(crypto.tag, crypto.cipher_suite, crypto_out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 }
254 }
255 return false;
256}
257
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258// Finds all StreamParams of all media types and attach them to stream_params.
Steve Anton5c72e712018-12-10 14:25:30 -0800259static StreamParamsVec GetCurrentStreamParams(
260 const std::vector<const ContentInfo*>& active_local_contents) {
261 StreamParamsVec stream_params;
262 for (const ContentInfo* content : active_local_contents) {
263 for (const StreamParams& params : content->media_description()->streams()) {
264 stream_params.push_back(params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265 }
266 }
Steve Anton5c72e712018-12-10 14:25:30 -0800267 return stream_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268}
269
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000270// Filters the data codecs for the data channel type.
271void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
272 // Filter RTP codec for SCTP and vice versa.
solenberg9fa49752016-10-08 13:02:44 -0700273 const char* codec_name =
274 sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName;
Steve Anton3a66edf2018-09-10 12:57:37 -0700275 codecs->erase(std::remove_if(codecs->begin(), codecs->end(),
276 [&codec_name](const DataCodec& codec) {
Niels Möller039743e2018-10-23 10:07:25 +0200277 return absl::EqualsIgnoreCase(codec.name,
278 codec_name);
Steve Anton3a66edf2018-09-10 12:57:37 -0700279 }),
280 codecs->end());
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000281}
282
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283template <typename IdStruct>
284class UsedIds {
285 public:
286 UsedIds(int min_allowed_id, int max_allowed_id)
287 : min_allowed_id_(min_allowed_id),
288 max_allowed_id_(max_allowed_id),
Yves Gerey665174f2018-06-19 15:03:05 +0200289 next_id_(max_allowed_id) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290
291 // Loops through all Id in |ids| and changes its id if it is
292 // already in use by another IdStruct. Call this methods with all Id
293 // in a session description to make sure no duplicate ids exists.
294 // Note that typename Id must be a type of IdStruct.
295 template <typename Id>
296 void FindAndSetIdUsed(std::vector<Id>* ids) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700297 for (const Id& id : *ids) {
298 FindAndSetIdUsed(&id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 }
300 }
301
302 // Finds and sets an unused id if the |idstruct| id is already in use.
303 void FindAndSetIdUsed(IdStruct* idstruct) {
304 const int original_id = idstruct->id;
305 int new_id = idstruct->id;
306
307 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
308 // If the original id is not in range - this is an id that can't be
309 // dynamically changed.
310 return;
311 }
312
313 if (IsIdUsed(original_id)) {
314 new_id = FindUnusedId();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100315 RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from "
316 << original_id << " to " << new_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000317 idstruct->id = new_id;
318 }
319 SetIdUsed(new_id);
320 }
321
322 private:
323 // Returns the first unused id in reverse order.
324 // This hopefully reduce the risk of more collisions. We want to change the
325 // default ids as little as possible.
326 int FindUnusedId() {
327 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
328 --next_id_;
329 }
nisseede5da42017-01-12 05:15:36 -0800330 RTC_DCHECK(next_id_ >= min_allowed_id_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331 return next_id_;
332 }
333
Yves Gerey665174f2018-06-19 15:03:05 +0200334 bool IsIdUsed(int new_id) { return id_set_.find(new_id) != id_set_.end(); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000335
Yves Gerey665174f2018-06-19 15:03:05 +0200336 void SetIdUsed(int new_id) { id_set_.insert(new_id); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337
338 const int min_allowed_id_;
339 const int max_allowed_id_;
340 int next_id_;
341 std::set<int> id_set_;
342};
343
344// Helper class used for finding duplicate RTP payload types among audio, video
345// and data codecs. When bundle is used the payload types may not collide.
346class UsedPayloadTypes : public UsedIds<Codec> {
347 public:
348 UsedPayloadTypes()
Yves Gerey665174f2018-06-19 15:03:05 +0200349 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350
351 private:
352 static const int kDynamicPayloadTypeMin = 96;
353 static const int kDynamicPayloadTypeMax = 127;
354};
355
356// Helper class used for finding duplicate RTP Header extension ids among
Johannes Kron07ba2b92018-09-26 13:33:35 +0200357// audio and video extensions. Only applies to one-byte header extensions at the
358// moment. ids > 14 will always be reported as available.
359// TODO(kron): This class needs to be refactored when we start to send two-byte
360// header extensions.
isheriff6f8d6862016-05-26 11:24:55 -0700361class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 public:
363 UsedRtpHeaderExtensionIds()
Johannes Kron07ba2b92018-09-26 13:33:35 +0200364 : UsedIds<webrtc::RtpExtension>(
365 webrtc::RtpExtension::kMinId,
366 webrtc::RtpExtension::kOneByteHeaderExtensionMaxId) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367
368 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369};
370
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800371static StreamParams CreateStreamParamsForNewSenderWithSsrcs(
372 const SenderOptions& sender,
373 const std::string& rtcp_cname,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800374 bool include_rtx_streams,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800375 bool include_flexfec_stream,
376 UniqueRandomIdGenerator* ssrc_generator) {
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800377 StreamParams result;
378 result.id = sender.track_id;
379
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800380 // TODO(brandtr): Update when we support multistream protection.
381 if (include_flexfec_stream && sender.num_sim_layers > 1) {
382 include_flexfec_stream = false;
383 RTC_LOG(LS_WARNING)
384 << "Our FlexFEC implementation only supports protecting "
385 "a single media streams. This session has multiple "
386 "media streams however, so no FlexFEC SSRC will be generated.";
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800387 }
388
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800389 result.GenerateSsrcs(sender.num_sim_layers, include_rtx_streams,
390 include_flexfec_stream, ssrc_generator);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800391
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800392 result.cname = rtcp_cname;
393 result.set_stream_ids(sender.stream_ids);
394
395 return result;
396}
397
398static bool ValidateSimulcastLayers(
399 const std::vector<RidDescription>& rids,
400 const SimulcastLayerList& simulcast_layers) {
Steve Anton64b626b2019-01-28 17:25:26 -0800401 return absl::c_all_of(
402 simulcast_layers.GetAllLayers(), [&rids](const SimulcastLayer& layer) {
403 return absl::c_any_of(rids, [&layer](const RidDescription& rid) {
404 return rid.rid == layer.rid;
405 });
406 });
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800407}
408
409static StreamParams CreateStreamParamsForNewSenderWithRids(
410 const SenderOptions& sender,
411 const std::string& rtcp_cname) {
412 RTC_DCHECK(!sender.rids.empty());
413 RTC_DCHECK_EQ(sender.num_sim_layers, 0)
414 << "RIDs are the compliant way to indicate simulcast.";
415 RTC_DCHECK(ValidateSimulcastLayers(sender.rids, sender.simulcast_layers));
416 StreamParams result;
417 result.id = sender.track_id;
418 result.cname = rtcp_cname;
419 result.set_stream_ids(sender.stream_ids);
420
421 // More than one rid should be signaled.
422 if (sender.rids.size() > 1) {
423 result.set_rids(sender.rids);
424 }
425
426 return result;
427}
428
429// Adds SimulcastDescription if indicated by the media description options.
430// MediaContentDescription should already be set up with the send rids.
431static void AddSimulcastToMediaDescription(
432 const MediaDescriptionOptions& media_description_options,
433 MediaContentDescription* description) {
434 RTC_DCHECK(description);
435
436 // Check if we are using RIDs in this scenario.
Steve Anton64b626b2019-01-28 17:25:26 -0800437 if (absl::c_all_of(description->streams(), [](const StreamParams& params) {
438 return !params.has_rids();
439 })) {
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800440 return;
441 }
442
443 RTC_DCHECK_EQ(1, description->streams().size())
444 << "RIDs are only supported in Unified Plan semantics.";
445 RTC_DCHECK_EQ(1, media_description_options.sender_options.size());
446 RTC_DCHECK(description->type() == MediaType::MEDIA_TYPE_AUDIO ||
447 description->type() == MediaType::MEDIA_TYPE_VIDEO);
448
449 // One RID or less indicates that simulcast is not needed.
450 if (description->streams()[0].rids().size() <= 1) {
451 return;
452 }
453
Amit Hilbuchb7446ed2019-01-28 12:25:25 -0800454 // Only negotiate the send layers.
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800455 SimulcastDescription simulcast;
456 simulcast.send_layers() =
457 media_description_options.sender_options[0].simulcast_layers;
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800458 description->set_simulcast_description(simulcast);
459}
460
zhihuang1c378ed2017-08-17 14:10:50 -0700461// Adds a StreamParams for each SenderOptions in |sender_options| to
462// content_description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463// |current_params| - All currently known StreamParams of any media type.
464template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700465static bool AddStreamParams(
466 const std::vector<SenderOptions>& sender_options,
467 const std::string& rtcp_cname,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800468 UniqueRandomIdGenerator* ssrc_generator,
zhihuang1c378ed2017-08-17 14:10:50 -0700469 StreamParamsVec* current_streams,
470 MediaContentDescriptionImpl<C>* content_description) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700471 // SCTP streams are not negotiated using SDP/ContentDescriptions.
Harald Alvestrand26bf7c42019-04-23 05:20:17 +0000472 if (IsSctpProtocol(content_description->protocol())) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700473 return true;
474 }
475
Noah Richards2e7a0982015-05-18 14:02:54 -0700476 const bool include_rtx_streams =
477 ContainsRtxCodec(content_description->codecs());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478
brandtr03d5fb12016-11-22 03:37:59 -0800479 const bool include_flexfec_stream =
480 ContainsFlexfecCodec(content_description->codecs());
481
zhihuang1c378ed2017-08-17 14:10:50 -0700482 for (const SenderOptions& sender : sender_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 // groupid is empty for StreamParams generated using
484 // MediaSessionDescriptionFactory.
zhihuang1c378ed2017-08-17 14:10:50 -0700485 StreamParams* param =
486 GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000487 if (!param) {
zhihuang1c378ed2017-08-17 14:10:50 -0700488 // This is a new sender.
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800489 StreamParams stream_param =
490 sender.rids.empty()
491 ?
492 // Signal SSRCs and legacy simulcast (if requested).
493 CreateStreamParamsForNewSenderWithSsrcs(
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800494 sender, rtcp_cname, include_rtx_streams,
495 include_flexfec_stream, ssrc_generator)
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800496 :
497 // Signal RIDs and spec-compliant simulcast (if requested).
498 CreateStreamParamsForNewSenderWithRids(sender, rtcp_cname);
499
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 content_description->AddStream(stream_param);
501
502 // Store the new StreamParams in current_streams.
503 // This is necessary so that we can use the CNAME for other media types.
504 current_streams->push_back(stream_param);
505 } else {
deadbeef2f425aa2017-04-14 10:41:32 -0700506 // Use existing generated SSRCs/groups, but update the sync_label if
507 // necessary. This may be needed if a MediaStreamTrack was moved from one
508 // MediaStream to another.
Seth Hampson845e8782018-03-02 11:34:10 -0800509 param->set_stream_ids(sender.stream_ids);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000510 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
512 }
513 return true;
514}
515
516// Updates the transport infos of the |sdesc| according to the given
517// |bundle_group|. The transport infos of the content names within the
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800518// |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
519// first content within the |bundle_group|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
521 SessionDescription* sdesc) {
522 // The bundle should not be empty.
523 if (!sdesc || !bundle_group.FirstContentName()) {
524 return false;
525 }
526
527 // We should definitely have a transport for the first content.
jbauch083b73f2015-07-16 02:46:32 -0700528 const std::string& selected_content_name = *bundle_group.FirstContentName();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 const TransportInfo* selected_transport_info =
530 sdesc->GetTransportInfoByName(selected_content_name);
531 if (!selected_transport_info) {
532 return false;
533 }
534
535 // Set the other contents to use the same ICE credentials.
jbauch083b73f2015-07-16 02:46:32 -0700536 const std::string& selected_ufrag =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 selected_transport_info->description.ice_ufrag;
jbauch083b73f2015-07-16 02:46:32 -0700538 const std::string& selected_pwd =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 selected_transport_info->description.ice_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800540 ConnectionRole selected_connection_role =
541 selected_transport_info->description.connection_role;
Steve Anton3a66edf2018-09-10 12:57:37 -0700542 for (TransportInfo& transport_info : sdesc->transport_infos()) {
543 if (bundle_group.HasContentName(transport_info.content_name) &&
544 transport_info.content_name != selected_content_name) {
545 transport_info.description.ice_ufrag = selected_ufrag;
546 transport_info.description.ice_pwd = selected_pwd;
547 transport_info.description.connection_role = selected_connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 }
549 }
550 return true;
551}
552
553// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
554// sets it to |cryptos|.
555static bool GetCryptosByName(const SessionDescription* sdesc,
556 const std::string& content_name,
557 CryptoParamsVec* cryptos) {
558 if (!sdesc || !cryptos) {
559 return false;
560 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 const ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800562 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000563 return false;
564 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800565 *cryptos = content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 return true;
567}
568
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
570// which are not available in |filter|.
571static void PruneCryptos(const CryptoParamsVec& filter,
572 CryptoParamsVec* target_cryptos) {
573 if (!target_cryptos) {
574 return;
575 }
tzik21995802018-04-26 17:38:28 +0900576
577 target_cryptos->erase(
578 std::remove_if(target_cryptos->begin(), target_cryptos->end(),
579 // Returns true if the |crypto|'s cipher_suite is not
580 // found in |filter|.
581 [&filter](const CryptoParams& crypto) {
582 for (const CryptoParams& entry : filter) {
583 if (entry.cipher_suite == crypto.cipher_suite)
584 return false;
585 }
586 return true;
587 }),
588 target_cryptos->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589}
590
591static bool IsRtpContent(SessionDescription* sdesc,
592 const std::string& content_name) {
593 bool is_rtp = false;
594 ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800595 if (content && content->media_description()) {
596 is_rtp = IsRtpProtocol(content->media_description()->protocol());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 }
598 return is_rtp;
599}
600
601// Updates the crypto parameters of the |sdesc| according to the given
602// |bundle_group|. The crypto parameters of all the contents within the
603// |bundle_group| should be updated to use the common subset of the
604// available cryptos.
605static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
606 SessionDescription* sdesc) {
607 // The bundle should not be empty.
608 if (!sdesc || !bundle_group.FirstContentName()) {
609 return false;
610 }
611
wu@webrtc.org78187522013-10-07 23:32:02 +0000612 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 // Get the common cryptos.
614 const ContentNames& content_names = bundle_group.content_names();
615 CryptoParamsVec common_cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700616 bool first = true;
617 for (const std::string& content_name : content_names) {
618 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 continue;
620 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000621 // The common cryptos are needed if any of the content does not have DTLS
622 // enabled.
Steve Anton3a66edf2018-09-10 12:57:37 -0700623 if (!sdesc->GetTransportInfoByName(content_name)->description.secure()) {
wu@webrtc.org78187522013-10-07 23:32:02 +0000624 common_cryptos_needed = true;
625 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700626 if (first) {
627 first = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628 // Initial the common_cryptos with the first content in the bundle group.
Steve Anton3a66edf2018-09-10 12:57:37 -0700629 if (!GetCryptosByName(sdesc, content_name, &common_cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 return false;
631 }
632 if (common_cryptos.empty()) {
633 // If there's no crypto params, we should just return.
634 return true;
635 }
636 } else {
637 CryptoParamsVec cryptos;
Steve Anton3a66edf2018-09-10 12:57:37 -0700638 if (!GetCryptosByName(sdesc, content_name, &cryptos)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 return false;
640 }
641 PruneCryptos(cryptos, &common_cryptos);
642 }
643 }
644
wu@webrtc.org78187522013-10-07 23:32:02 +0000645 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 return false;
647 }
648
649 // Update to use the common cryptos.
Steve Anton3a66edf2018-09-10 12:57:37 -0700650 for (const std::string& content_name : content_names) {
651 if (!IsRtpContent(sdesc, content_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 continue;
653 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700654 ContentInfo* content = sdesc->GetContentByName(content_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 if (IsMediaContent(content)) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800656 MediaContentDescription* media_desc = content->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 if (!media_desc) {
658 return false;
659 }
660 media_desc->set_cryptos(common_cryptos);
661 }
662 }
663 return true;
664}
665
Steve Anton5c72e712018-12-10 14:25:30 -0800666static std::vector<const ContentInfo*> GetActiveContents(
667 const SessionDescription& description,
668 const MediaSessionOptions& session_options) {
669 std::vector<const ContentInfo*> active_contents;
670 for (size_t i = 0; i < description.contents().size(); ++i) {
671 RTC_DCHECK_LT(i, session_options.media_description_options.size());
672 const ContentInfo& content = description.contents()[i];
673 const MediaDescriptionOptions& media_options =
674 session_options.media_description_options[i];
675 if (!content.rejected && !media_options.stopped &&
676 content.name == media_options.mid) {
677 active_contents.push_back(&content);
678 }
679 }
680 return active_contents;
681}
682
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683template <class C>
684static bool ContainsRtxCodec(const std::vector<C>& codecs) {
brandtr03d5fb12016-11-22 03:37:59 -0800685 for (const auto& codec : codecs) {
686 if (IsRtxCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 return true;
688 }
689 }
690 return false;
691}
692
693template <class C>
694static bool IsRtxCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200695 return absl::EqualsIgnoreCase(codec.name, kRtxCodecName);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696}
697
brandtr03d5fb12016-11-22 03:37:59 -0800698template <class C>
699static bool ContainsFlexfecCodec(const std::vector<C>& codecs) {
700 for (const auto& codec : codecs) {
701 if (IsFlexfecCodec(codec)) {
702 return true;
703 }
704 }
705 return false;
706}
707
708template <class C>
709static bool IsFlexfecCodec(const C& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +0200710 return absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName);
brandtr03d5fb12016-11-22 03:37:59 -0800711}
712
zhihuang1c378ed2017-08-17 14:10:50 -0700713// Create a media content to be offered for the given |sender_options|,
714// according to the given options.rtcp_mux, session_options.is_muc, codecs,
715// secure_transport, crypto, and current_streams. If we don't currently have
716// crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is
717// created (according to crypto_suites). The created content is added to the
718// offer.
Harald Alvestrand26bf7c42019-04-23 05:20:17 +0000719static bool CreateContentOffer(
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800720 const MediaDescriptionOptions& media_description_options,
zhihuang1c378ed2017-08-17 14:10:50 -0700721 const MediaSessionOptions& session_options,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000722 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 const CryptoParamsVec* current_cryptos,
724 const std::vector<std::string>& crypto_suites,
725 const RtpHeaderExtensions& rtp_extensions,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -0800726 UniqueRandomIdGenerator* ssrc_generator,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 StreamParamsVec* current_streams,
Harald Alvestrand26bf7c42019-04-23 05:20:17 +0000728 MediaContentDescription* offer) {
zhihuang1c378ed2017-08-17 14:10:50 -0700729 offer->set_rtcp_mux(session_options.rtcp_mux_enabled);
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700730 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
731 offer->set_rtcp_reduced_size(true);
732 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 offer->set_rtp_header_extensions(rtp_extensions);
734
Amit Hilbuchc63ddb22019-01-02 10:13:58 -0800735 AddSimulcastToMediaDescription(media_description_options, offer);
736
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 if (secure_policy != SEC_DISABLED) {
738 if (current_cryptos) {
739 AddMediaCryptos(*current_cryptos, offer);
740 }
741 if (offer->cryptos().empty()) {
742 if (!CreateMediaCryptos(crypto_suites, offer)) {
743 return false;
744 }
745 }
746 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747
deadbeef7af91dd2016-12-13 11:29:11 -0800748 if (secure_policy == SEC_REQUIRED && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 return false;
750 }
751 return true;
752}
Harald Alvestrand26bf7c42019-04-23 05:20:17 +0000753template <class C>
754static bool CreateMediaContentOffer(
755 const MediaDescriptionOptions& media_description_options,
756 const MediaSessionOptions& session_options,
757 const std::vector<C>& codecs,
758 const SecurePolicy& secure_policy,
759 const CryptoParamsVec* current_cryptos,
760 const std::vector<std::string>& crypto_suites,
761 const RtpHeaderExtensions& rtp_extensions,
762 UniqueRandomIdGenerator* ssrc_generator,
763 StreamParamsVec* current_streams,
764 MediaContentDescriptionImpl<C>* offer) {
765 offer->AddCodecs(codecs);
766 if (!AddStreamParams(media_description_options.sender_options,
767 session_options.rtcp_cname, ssrc_generator,
768 current_streams, offer)) {
769 return false;
770 }
771
772 return CreateContentOffer(media_description_options, session_options,
773 secure_policy, current_cryptos, crypto_suites,
774 rtp_extensions, ssrc_generator, current_streams,
775 offer);
776}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777
778template <class C>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000779static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
magjedb05fa242016-11-11 04:00:16 -0800780 const int codec1_id,
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000781 const std::vector<C>& codecs2,
magjedb05fa242016-11-11 04:00:16 -0800782 const int codec2_id) {
783 const C* codec1 = FindCodecById(codecs1, codec1_id);
784 const C* codec2 = FindCodecById(codecs2, codec2_id);
785 return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2);
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000786}
787
788template <class C>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789static void NegotiateCodecs(const std::vector<C>& local_codecs,
790 const std::vector<C>& offered_codecs,
791 std::vector<C>* negotiated_codecs) {
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800792 for (const C& ours : local_codecs) {
793 C theirs;
deadbeef67cf2c12016-04-13 10:07:16 -0700794 // Note that we intentionally only find one matching codec for each of our
795 // local codecs, in case the remote offer contains duplicate codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800796 if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) {
797 C negotiated = ours;
798 negotiated.IntersectFeedbackParams(theirs);
799 if (IsRtxCodec(negotiated)) {
magjedb05fa242016-11-11 04:00:16 -0800800 const auto apt_it =
801 theirs.params.find(kCodecParamAssociatedPayloadType);
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800802 // FindMatchingCodec shouldn't return something with no apt value.
magjedb05fa242016-11-11 04:00:16 -0800803 RTC_DCHECK(apt_it != theirs.params.end());
804 negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 }
Niels Möller039743e2018-10-23 10:07:25 +0200806 if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) {
magjedf823ede2016-11-12 09:53:04 -0800807 webrtc::H264::GenerateProfileLevelIdForAnswer(
808 ours.params, theirs.params, &negotiated.params);
809 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800810 negotiated.id = theirs.id;
ossu075af922016-06-14 03:29:38 -0700811 negotiated.name = theirs.name;
magjedb05fa242016-11-11 04:00:16 -0800812 negotiated_codecs->push_back(std::move(negotiated));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 }
814 }
deadbeef67cf2c12016-04-13 10:07:16 -0700815 // RFC3264: Although the answerer MAY list the formats in their desired
816 // order of preference, it is RECOMMENDED that unless there is a
817 // specific reason, the answerer list formats in the same relative order
818 // they were present in the offer.
819 std::unordered_map<int, int> payload_type_preferences;
820 int preference = static_cast<int>(offered_codecs.size() + 1);
821 for (const C& codec : offered_codecs) {
822 payload_type_preferences[codec.id] = preference--;
823 }
Steve Anton64b626b2019-01-28 17:25:26 -0800824 absl::c_sort(
825 *negotiated_codecs, [&payload_type_preferences](const C& a, const C& b) {
826 return payload_type_preferences[a.id] > payload_type_preferences[b.id];
827 });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828}
829
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800830// Finds a codec in |codecs2| that matches |codec_to_match|, which is
831// a member of |codecs1|. If |codec_to_match| is an RTX codec, both
832// the codecs themselves and their associated codecs must match.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833template <class C>
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800834static bool FindMatchingCodec(const std::vector<C>& codecs1,
835 const std::vector<C>& codecs2,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 const C& codec_to_match,
837 C* found_codec) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -0700838 // |codec_to_match| should be a member of |codecs1|, in order to look up RTX
839 // codecs' associated codecs correctly. If not, that's a programming error.
Steve Anton64b626b2019-01-28 17:25:26 -0800840 RTC_DCHECK(absl::c_any_of(codecs1, [&codec_to_match](const C& codec) {
841 return &codec == &codec_to_match;
842 }));
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800843 for (const C& potential_match : codecs2) {
844 if (potential_match.Matches(codec_to_match)) {
845 if (IsRtxCodec(codec_to_match)) {
magjedb05fa242016-11-11 04:00:16 -0800846 int apt_value_1 = 0;
847 int apt_value_2 = 0;
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800848 if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType,
849 &apt_value_1) ||
850 !potential_match.GetParam(kCodecParamAssociatedPayloadType,
851 &apt_value_2)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100852 RTC_LOG(LS_WARNING) << "RTX missing associated payload type.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800853 continue;
854 }
855 if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2,
856 apt_value_2)) {
857 continue;
858 }
859 }
860 if (found_codec) {
861 *found_codec = potential_match;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863 return true;
864 }
865 }
866 return false;
867}
868
zhihuang1c378ed2017-08-17 14:10:50 -0700869// Find the codec in |codec_list| that |rtx_codec| is associated with.
870template <class C>
871static const C* GetAssociatedCodec(const std::vector<C>& codec_list,
872 const C& rtx_codec) {
873 std::string associated_pt_str;
874 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
875 &associated_pt_str)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100876 RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
877 << " is missing an associated payload type.";
zhihuang1c378ed2017-08-17 14:10:50 -0700878 return nullptr;
879 }
880
881 int associated_pt;
882 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100883 RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
884 << " of RTX codec " << rtx_codec.name
885 << " to an integer.";
zhihuang1c378ed2017-08-17 14:10:50 -0700886 return nullptr;
887 }
888
889 // Find the associated reference codec for the reference RTX codec.
890 const C* associated_codec = FindCodecById(codec_list, associated_pt);
891 if (!associated_codec) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100892 RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
893 << associated_pt << " for RTX codec " << rtx_codec.name
894 << ".";
zhihuang1c378ed2017-08-17 14:10:50 -0700895 }
896 return associated_codec;
897}
898
899// Adds all codecs from |reference_codecs| to |offered_codecs| that don't
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900// already exist in |offered_codecs| and ensure the payload types don't
901// collide.
902template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700903static void MergeCodecs(const std::vector<C>& reference_codecs,
904 std::vector<C>* offered_codecs,
905 UsedPayloadTypes* used_pltypes) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 // Add all new codecs that are not RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800907 for (const C& reference_codec : reference_codecs) {
908 if (!IsRtxCodec(reference_codec) &&
909 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
910 reference_codec, nullptr)) {
911 C codec = reference_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 used_pltypes->FindAndSetIdUsed(&codec);
913 offered_codecs->push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915 }
916
917 // Add all new RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800918 for (const C& reference_codec : reference_codecs) {
919 if (IsRtxCodec(reference_codec) &&
920 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
921 reference_codec, nullptr)) {
922 C rtx_codec = reference_codec;
olka3c747662017-08-17 06:50:32 -0700923 const C* associated_codec =
zhihuang1c378ed2017-08-17 14:10:50 -0700924 GetAssociatedCodec(reference_codecs, rtx_codec);
olka3c747662017-08-17 06:50:32 -0700925 if (!associated_codec) {
olka3c747662017-08-17 06:50:32 -0700926 continue;
927 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800928 // Find a codec in the offered list that matches the reference codec.
929 // Its payload type may be different than the reference codec.
930 C matching_codec;
931 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
magjedb05fa242016-11-11 04:00:16 -0800932 *associated_codec, &matching_codec)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100933 RTC_LOG(LS_WARNING)
934 << "Couldn't find matching " << associated_codec->name << " codec.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800935 continue;
936 }
937
938 rtx_codec.params[kCodecParamAssociatedPayloadType] =
939 rtc::ToString(matching_codec.id);
940 used_pltypes->FindAndSetIdUsed(&rtx_codec);
941 offered_codecs->push_back(rtx_codec);
942 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 }
944}
945
zhihuang1c378ed2017-08-17 14:10:50 -0700946static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
947 const webrtc::RtpExtension& ext_to_match,
948 webrtc::RtpExtension* found_extension) {
Steve Anton64b626b2019-01-28 17:25:26 -0800949 auto it = absl::c_find_if(
950 extensions, [&ext_to_match](const webrtc::RtpExtension& extension) {
951 // We assume that all URIs are given in a canonical
952 // format.
953 return extension.uri == ext_to_match.uri &&
954 extension.encrypt == ext_to_match.encrypt;
955 });
Steve Anton3a66edf2018-09-10 12:57:37 -0700956 if (it == extensions.end()) {
957 return false;
zhihuang1c378ed2017-08-17 14:10:50 -0700958 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700959 if (found_extension) {
960 *found_extension = *it;
961 }
962 return true;
zhihuang1c378ed2017-08-17 14:10:50 -0700963}
964
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965static bool FindByUri(const RtpHeaderExtensions& extensions,
isheriff6f8d6862016-05-26 11:24:55 -0700966 const webrtc::RtpExtension& ext_to_match,
967 webrtc::RtpExtension* found_extension) {
jbauch5869f502017-06-29 12:31:36 -0700968 // We assume that all URIs are given in a canonical format.
969 const webrtc::RtpExtension* found =
970 webrtc::RtpExtension::FindHeaderExtensionByUri(extensions,
971 ext_to_match.uri);
972 if (!found) {
973 return false;
974 }
975 if (found_extension) {
976 *found_extension = *found;
977 }
978 return true;
979}
980
981static bool FindByUriWithEncryptionPreference(
982 const RtpHeaderExtensions& extensions,
Yves Gerey665174f2018-06-19 15:03:05 +0200983 const webrtc::RtpExtension& ext_to_match,
984 bool encryption_preference,
jbauch5869f502017-06-29 12:31:36 -0700985 webrtc::RtpExtension* found_extension) {
986 const webrtc::RtpExtension* unencrypted_extension = nullptr;
Steve Anton3a66edf2018-09-10 12:57:37 -0700987 for (const webrtc::RtpExtension& extension : extensions) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 // We assume that all URIs are given in a canonical format.
Steve Anton3a66edf2018-09-10 12:57:37 -0700989 if (extension.uri == ext_to_match.uri) {
990 if (!encryption_preference || extension.encrypt) {
jbauch5869f502017-06-29 12:31:36 -0700991 if (found_extension) {
Steve Anton3a66edf2018-09-10 12:57:37 -0700992 *found_extension = extension;
jbauch5869f502017-06-29 12:31:36 -0700993 }
994 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 }
Steve Anton3a66edf2018-09-10 12:57:37 -0700996 unencrypted_extension = &extension;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 }
998 }
jbauch5869f502017-06-29 12:31:36 -0700999 if (unencrypted_extension) {
1000 if (found_extension) {
1001 *found_extension = *unencrypted_extension;
1002 }
1003 return true;
1004 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 return false;
1006}
1007
zhihuang1c378ed2017-08-17 14:10:50 -07001008// Adds all extensions from |reference_extensions| to |offered_extensions| that
1009// don't already exist in |offered_extensions| and ensure the IDs don't
1010// collide. If an extension is added, it's also added to |regular_extensions| or
1011// |encrypted_extensions|, and if the extension is in |regular_extensions| or
1012// |encrypted_extensions|, its ID is marked as used in |used_ids|.
1013// |offered_extensions| is for either audio or video while |regular_extensions|
1014// and |encrypted_extensions| are used for both audio and video. There could be
1015// overlap between audio extensions and video extensions.
1016static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions,
1017 RtpHeaderExtensions* offered_extensions,
1018 RtpHeaderExtensions* regular_extensions,
1019 RtpHeaderExtensions* encrypted_extensions,
1020 UsedRtpHeaderExtensionIds* used_ids) {
olka3c747662017-08-17 06:50:32 -07001021 for (auto reference_extension : reference_extensions) {
zhihuang1c378ed2017-08-17 14:10:50 -07001022 if (!FindByUriAndEncryption(*offered_extensions, reference_extension,
1023 nullptr)) {
olka3c747662017-08-17 06:50:32 -07001024 webrtc::RtpExtension existing;
zhihuang1c378ed2017-08-17 14:10:50 -07001025 if (reference_extension.encrypt) {
1026 if (FindByUriAndEncryption(*encrypted_extensions, reference_extension,
1027 &existing)) {
1028 offered_extensions->push_back(existing);
1029 } else {
1030 used_ids->FindAndSetIdUsed(&reference_extension);
1031 encrypted_extensions->push_back(reference_extension);
1032 offered_extensions->push_back(reference_extension);
1033 }
olka3c747662017-08-17 06:50:32 -07001034 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07001035 if (FindByUriAndEncryption(*regular_extensions, reference_extension,
1036 &existing)) {
1037 offered_extensions->push_back(existing);
1038 } else {
1039 used_ids->FindAndSetIdUsed(&reference_extension);
1040 regular_extensions->push_back(reference_extension);
1041 offered_extensions->push_back(reference_extension);
1042 }
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001043 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 }
1045 }
1046}
1047
jbauch5869f502017-06-29 12:31:36 -07001048static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions,
1049 RtpHeaderExtensions* all_extensions,
1050 UsedRtpHeaderExtensionIds* used_ids) {
1051 RtpHeaderExtensions encrypted_extensions;
1052 for (const webrtc::RtpExtension& extension : *extensions) {
1053 webrtc::RtpExtension existing;
1054 // Don't add encrypted extensions again that were already included in a
1055 // previous offer or regular extensions that are also included as encrypted
1056 // extensions.
1057 if (extension.encrypt ||
1058 !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) ||
1059 (FindByUriWithEncryptionPreference(*extensions, extension, true,
Yves Gerey665174f2018-06-19 15:03:05 +02001060 &existing) &&
1061 existing.encrypt)) {
jbauch5869f502017-06-29 12:31:36 -07001062 continue;
1063 }
1064
1065 if (FindByUri(*all_extensions, extension, &existing)) {
1066 encrypted_extensions.push_back(existing);
1067 } else {
1068 webrtc::RtpExtension encrypted(extension);
1069 encrypted.encrypt = true;
1070 used_ids->FindAndSetIdUsed(&encrypted);
1071 all_extensions->push_back(encrypted);
1072 encrypted_extensions.push_back(encrypted);
1073 }
1074 }
1075 extensions->insert(extensions->end(), encrypted_extensions.begin(),
Yves Gerey665174f2018-06-19 15:03:05 +02001076 encrypted_extensions.end());
jbauch5869f502017-06-29 12:31:36 -07001077}
1078
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079static void NegotiateRtpHeaderExtensions(
1080 const RtpHeaderExtensions& local_extensions,
1081 const RtpHeaderExtensions& offered_extensions,
jbauch5869f502017-06-29 12:31:36 -07001082 bool enable_encrypted_rtp_header_extensions,
Johannes Kronce8e8672019-02-22 13:06:44 +01001083 RtpHeaderExtensions* negotiated_extensions) {
1084 // TransportSequenceNumberV2 is not offered by default. The special logic for
1085 // the TransportSequenceNumber extensions works as follows:
1086 // Offer Answer
1087 // V1 V1 if in local_extensions.
1088 // V1 and V2 V2 regardless of local_extensions.
1089 // V2 V2 regardless of local_extensions.
1090 const webrtc::RtpExtension* transport_sequence_number_v2_offer =
1091 webrtc::RtpExtension::FindHeaderExtensionByUri(
1092 offered_extensions,
1093 webrtc::RtpExtension::kTransportSequenceNumberV2Uri);
1094
Steve Anton3a66edf2018-09-10 12:57:37 -07001095 for (const webrtc::RtpExtension& ours : local_extensions) {
isheriff6f8d6862016-05-26 11:24:55 -07001096 webrtc::RtpExtension theirs;
Yves Gerey665174f2018-06-19 15:03:05 +02001097 if (FindByUriWithEncryptionPreference(
Steve Anton3a66edf2018-09-10 12:57:37 -07001098 offered_extensions, ours, enable_encrypted_rtp_header_extensions,
Yves Gerey665174f2018-06-19 15:03:05 +02001099 &theirs)) {
Johannes Kronce8e8672019-02-22 13:06:44 +01001100 if (transport_sequence_number_v2_offer &&
1101 ours.uri == webrtc::RtpExtension::kTransportSequenceNumberUri) {
1102 // Don't respond to
1103 // http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
1104 // if we get an offer including
Johannes Kron8cc711a2019-03-07 22:36:35 +01001105 // http://www.webrtc.org/experiments/rtp-hdrext/transport-wide-cc-02
Johannes Kronce8e8672019-02-22 13:06:44 +01001106 continue;
1107 } else {
1108 // We respond with their RTP header extension id.
1109 negotiated_extensions->push_back(theirs);
1110 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 }
1112 }
Johannes Kronce8e8672019-02-22 13:06:44 +01001113
1114 if (transport_sequence_number_v2_offer) {
1115 // Respond that we support kTransportSequenceNumberV2Uri.
1116 negotiated_extensions->push_back(*transport_sequence_number_v2_offer);
1117 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118}
1119
1120static void StripCNCodecs(AudioCodecs* audio_codecs) {
Steve Anton3a66edf2018-09-10 12:57:37 -07001121 audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(),
1122 [](const AudioCodec& codec) {
Niels Möller2edab4c2018-10-22 09:48:08 +02001123 return absl::EqualsIgnoreCase(
1124 codec.name, kComfortNoiseCodecName);
Steve Anton3a66edf2018-09-10 12:57:37 -07001125 }),
1126 audio_codecs->end());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127}
1128
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00001129template <class C>
1130static bool SetCodecsInAnswer(
1131 const MediaContentDescriptionImpl<C>* offer,
1132 const std::vector<C>& local_codecs,
1133 const MediaDescriptionOptions& media_description_options,
1134 const MediaSessionOptions& session_options,
1135 UniqueRandomIdGenerator* ssrc_generator,
1136 StreamParamsVec* current_streams,
1137 MediaContentDescriptionImpl<C>* answer) {
1138 std::vector<C> negotiated_codecs;
1139 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
1140 answer->AddCodecs(negotiated_codecs);
1141 answer->set_protocol(offer->protocol());
1142 if (!AddStreamParams(media_description_options.sender_options,
1143 session_options.rtcp_cname, ssrc_generator,
1144 current_streams, answer)) {
1145 return false; // Something went seriously wrong.
1146 }
1147 return true;
1148}
1149
zhihuang1c378ed2017-08-17 14:10:50 -07001150// Create a media content to be answered for the given |sender_options|
1151// according to the given session_options.rtcp_mux, session_options.streams,
1152// codecs, crypto, and current_streams. If we don't currently have crypto (in
1153// current_cryptos) and it is enabled (in secure_policy), crypto is created
1154// (according to crypto_suites). The codecs, rtcp_mux, and crypto are all
1155// negotiated with the offer. If the negotiation fails, this method returns
1156// false. The created content is added to the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157static bool CreateMediaContentAnswer(
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00001158 const MediaContentDescription* offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001159 const MediaDescriptionOptions& media_description_options,
1160 const MediaSessionOptions& session_options,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001161 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 const CryptoParamsVec* current_cryptos,
1163 const RtpHeaderExtensions& local_rtp_extenstions,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001164 UniqueRandomIdGenerator* ssrc_generator,
jbauch5869f502017-06-29 12:31:36 -07001165 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 StreamParamsVec* current_streams,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 bool bundle_enabled,
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00001168 MediaContentDescription* answer) {
Johannes Kron9581bc42018-10-23 10:17:39 +02001169 answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 RtpHeaderExtensions negotiated_rtp_extensions;
Yves Gerey665174f2018-06-19 15:03:05 +02001171 NegotiateRtpHeaderExtensions(
1172 local_rtp_extenstions, offer->rtp_header_extensions(),
1173 enable_encrypted_rtp_header_extensions, &negotiated_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
1175
zhihuang1c378ed2017-08-17 14:10:50 -07001176 answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux());
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -07001177 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
1178 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
1179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180
1181 if (sdes_policy != SEC_DISABLED) {
1182 CryptoParams crypto;
zhihuang1c378ed2017-08-17 14:10:50 -07001183 if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options,
1184 &crypto)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 if (current_cryptos) {
1186 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
1187 }
1188 answer->AddCrypto(crypto);
1189 }
1190 }
1191
deadbeef7af91dd2016-12-13 11:29:11 -08001192 if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 return false;
1194 }
1195
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001196 AddSimulcastToMediaDescription(media_description_options, answer);
1197
Steve Anton4e70a722017-11-28 14:57:10 -08001198 answer->set_direction(NegotiateRtpTransceiverDirection(
1199 offer->direction(), media_description_options.direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 return true;
1201}
1202
1203static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001204 const std::string& protocol,
1205 bool secure_transport) {
zhihuangcf5b37c2016-05-05 11:44:35 -07001206 // Since not all applications serialize and deserialize the media protocol,
1207 // we will have to accept |protocol| to be empty.
1208 if (protocol.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 return true;
1210 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001211
zhihuangcf5b37c2016-05-05 11:44:35 -07001212 if (type == MEDIA_TYPE_DATA) {
1213 // Check for SCTP, but also for RTP for RTP-based data channels.
1214 // TODO(pthatcher): Remove RTP once RTP-based data channels are gone.
1215 if (secure_transport) {
1216 // Most likely scenarios first.
1217 return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) ||
1218 IsPlainRtp(protocol);
1219 } else {
1220 return IsPlainSctp(protocol) || IsPlainRtp(protocol);
1221 }
1222 }
1223
1224 // Allow for non-DTLS RTP protocol even when using DTLS because that's what
1225 // JSEP specifies.
1226 if (secure_transport) {
1227 // Most likely scenarios first.
1228 return IsDtlsRtp(protocol) || IsPlainRtp(protocol);
1229 } else {
1230 return IsPlainRtp(protocol);
1231 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232}
1233
1234static void SetMediaProtocol(bool secure_transport,
1235 MediaContentDescription* desc) {
deadbeeff3938292015-07-15 12:20:53 -07001236 if (!desc->cryptos().empty())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 desc->set_protocol(kMediaProtocolSavpf);
deadbeeff3938292015-07-15 12:20:53 -07001238 else if (secure_transport)
1239 desc->set_protocol(kMediaProtocolDtlsSavpf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 else
1241 desc->set_protocol(kMediaProtocolAvpf);
1242}
1243
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001244// Gets the TransportInfo of the given |content_name| from the
1245// |current_description|. If doesn't exist, returns a new one.
1246static const TransportDescription* GetTransportDescription(
1247 const std::string& content_name,
1248 const SessionDescription* current_description) {
1249 const TransportDescription* desc = NULL;
1250 if (current_description) {
1251 const TransportInfo* info =
1252 current_description->GetTransportInfoByName(content_name);
1253 if (info) {
1254 desc = &info->description;
1255 }
1256 }
1257 return desc;
1258}
1259
1260// Gets the current DTLS state from the transport description.
zhihuang1c378ed2017-08-17 14:10:50 -07001261static bool IsDtlsActive(const ContentInfo* content,
1262 const SessionDescription* current_description) {
1263 if (!content) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001264 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001265 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001266
zhihuang1c378ed2017-08-17 14:10:50 -07001267 size_t msection_index = content - &current_description->contents()[0];
1268
1269 if (current_description->transport_infos().size() <= msection_index) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001270 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001271 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001272
zhihuang1c378ed2017-08-17 14:10:50 -07001273 return current_description->transport_infos()[msection_index]
1274 .description.secure();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001275}
1276
Steve Anton8ffb9c32017-08-31 15:45:38 -07001277void MediaDescriptionOptions::AddAudioSender(
1278 const std::string& track_id,
1279 const std::vector<std::string>& stream_ids) {
zhihuang1c378ed2017-08-17 14:10:50 -07001280 RTC_DCHECK(type == MEDIA_TYPE_AUDIO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001281 AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001282}
1283
Steve Anton8ffb9c32017-08-31 15:45:38 -07001284void MediaDescriptionOptions::AddVideoSender(
1285 const std::string& track_id,
1286 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001287 const std::vector<RidDescription>& rids,
1288 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001289 int num_sim_layers) {
zhihuang1c378ed2017-08-17 14:10:50 -07001290 RTC_DCHECK(type == MEDIA_TYPE_VIDEO);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001291 RTC_DCHECK(rids.empty() || num_sim_layers == 0)
1292 << "RIDs are the compliant way to indicate simulcast.";
1293 RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers));
1294 AddSenderInternal(track_id, stream_ids, rids, simulcast_layers,
1295 num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001296}
1297
zhihuang1c378ed2017-08-17 14:10:50 -07001298void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id,
1299 const std::string& stream_id) {
1300 RTC_DCHECK(type == MEDIA_TYPE_DATA);
Steve Anton8ffb9c32017-08-31 15:45:38 -07001301 // TODO(steveanton): Is it the case that RtpDataChannel will never have more
1302 // than one stream?
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001303 AddSenderInternal(track_id, {stream_id}, {}, SimulcastLayerList(), 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304}
1305
Steve Anton8ffb9c32017-08-31 15:45:38 -07001306void MediaDescriptionOptions::AddSenderInternal(
1307 const std::string& track_id,
1308 const std::vector<std::string>& stream_ids,
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001309 const std::vector<RidDescription>& rids,
1310 const SimulcastLayerList& simulcast_layers,
Steve Anton8ffb9c32017-08-31 15:45:38 -07001311 int num_sim_layers) {
1312 // TODO(steveanton): Support any number of stream ids.
1313 RTC_CHECK(stream_ids.size() == 1U);
Amit Hilbuchc63ddb22019-01-02 10:13:58 -08001314 SenderOptions options;
1315 options.track_id = track_id;
1316 options.stream_ids = stream_ids;
1317 options.simulcast_layers = simulcast_layers;
1318 options.rids = rids;
1319 options.num_sim_layers = num_sim_layers;
1320 sender_options.push_back(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321}
1322
zhihuang1c378ed2017-08-17 14:10:50 -07001323bool MediaSessionOptions::HasMediaDescription(MediaType type) const {
Steve Anton64b626b2019-01-28 17:25:26 -08001324 return absl::c_any_of(
1325 media_description_options,
1326 [type](const MediaDescriptionOptions& t) { return t.type == type; });
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001327}
1328
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001330 const TransportDescriptionFactory* transport_desc_factory,
1331 rtc::UniqueRandomIdGenerator* ssrc_generator)
1332 : ssrc_generator_(ssrc_generator),
1333 transport_desc_factory_(transport_desc_factory) {
1334 RTC_DCHECK(ssrc_generator_);
1335}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336
1337MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1338 ChannelManager* channel_manager,
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08001339 const TransportDescriptionFactory* transport_desc_factory,
1340 rtc::UniqueRandomIdGenerator* ssrc_generator)
1341 : MediaSessionDescriptionFactory(transport_desc_factory, ssrc_generator) {
ossudedfd282016-06-14 07:12:39 -07001342 channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_);
1343 channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
magjed3cf8ece2016-11-10 03:36:53 -08001345 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1347 channel_manager->GetSupportedDataCodecs(&data_codecs_);
zhihuang1c378ed2017-08-17 14:10:50 -07001348 ComputeAudioCodecsIntersectionAndUnion();
ossu075af922016-06-14 03:29:38 -07001349}
1350
ossudedfd282016-06-14 07:12:39 -07001351const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs()
1352 const {
ossu075af922016-06-14 03:29:38 -07001353 return audio_sendrecv_codecs_;
1354}
1355
1356const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const {
1357 return audio_send_codecs_;
1358}
1359
1360const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const {
1361 return audio_recv_codecs_;
1362}
1363
1364void MediaSessionDescriptionFactory::set_audio_codecs(
Yves Gerey665174f2018-06-19 15:03:05 +02001365 const AudioCodecs& send_codecs,
1366 const AudioCodecs& recv_codecs) {
ossu075af922016-06-14 03:29:38 -07001367 audio_send_codecs_ = send_codecs;
1368 audio_recv_codecs_ = recv_codecs;
zhihuang1c378ed2017-08-17 14:10:50 -07001369 ComputeAudioCodecsIntersectionAndUnion();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370}
1371
Amit Hilbuch77938e62018-12-21 09:23:38 -08001372static void AddUnifiedPlanExtensions(RtpHeaderExtensions* extensions) {
1373 RTC_DCHECK(extensions);
Elad Alon157540a2019-02-08 23:37:52 +01001374
1375 rtc::UniqueNumberGenerator<int> unique_id_generator;
1376 unique_id_generator.AddKnownId(0); // The first valid RTP extension ID is 1.
1377 for (const webrtc::RtpExtension& extension : *extensions) {
1378 const bool collision_free = unique_id_generator.AddKnownId(extension.id);
1379 RTC_DCHECK(collision_free);
1380 }
1381
Amit Hilbuch77938e62018-12-21 09:23:38 -08001382 // Unified Plan also offers the MID and RID header extensions.
Elad Alon157540a2019-02-08 23:37:52 +01001383 extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kMidUri,
1384 unique_id_generator()));
1385 extensions->push_back(webrtc::RtpExtension(webrtc::RtpExtension::kRidUri,
1386 unique_id_generator()));
Amit Hilbuch77938e62018-12-21 09:23:38 -08001387 extensions->push_back(webrtc::RtpExtension(
Elad Alon157540a2019-02-08 23:37:52 +01001388 webrtc::RtpExtension::kRepairedRidUri, unique_id_generator()));
Amit Hilbuch77938e62018-12-21 09:23:38 -08001389}
1390
1391RtpHeaderExtensions
1392MediaSessionDescriptionFactory::audio_rtp_header_extensions() const {
1393 RtpHeaderExtensions extensions = audio_rtp_extensions_;
1394 if (is_unified_plan_) {
1395 AddUnifiedPlanExtensions(&extensions);
1396 }
1397
1398 return extensions;
1399}
1400
1401RtpHeaderExtensions
1402MediaSessionDescriptionFactory::video_rtp_header_extensions() const {
1403 RtpHeaderExtensions extensions = video_rtp_extensions_;
1404 if (is_unified_plan_) {
1405 AddUnifiedPlanExtensions(&extensions);
1406 }
1407
1408 return extensions;
1409}
1410
Steve Anton6fe1fba2018-12-11 10:15:23 -08001411std::unique_ptr<SessionDescription> MediaSessionDescriptionFactory::CreateOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001412 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413 const SessionDescription* current_description) const {
Steve Anton5c72e712018-12-10 14:25:30 -08001414 // Must have options for each existing section.
1415 if (current_description) {
1416 RTC_DCHECK_LE(current_description->contents().size(),
1417 session_options.media_description_options.size());
1418 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001420 IceCredentialsIterator ice_credentials(
1421 session_options.pooled_ice_credentials);
Steve Anton5c72e712018-12-10 14:25:30 -08001422
1423 std::vector<const ContentInfo*> current_active_contents;
1424 if (current_description) {
1425 current_active_contents =
1426 GetActiveContents(*current_description, session_options);
1427 }
1428
1429 StreamParamsVec current_streams =
1430 GetCurrentStreamParams(current_active_contents);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001431
zhihuang1c378ed2017-08-17 14:10:50 -07001432 AudioCodecs offer_audio_codecs;
1433 VideoCodecs offer_video_codecs;
1434 DataCodecs offer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001435 GetCodecsForOffer(current_active_contents, &offer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001436 &offer_video_codecs, &offer_data_codecs);
ossu075af922016-06-14 03:29:38 -07001437
zhihuang1c378ed2017-08-17 14:10:50 -07001438 if (!session_options.vad_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 // If application doesn't want CN codecs in offer.
zhihuang1c378ed2017-08-17 14:10:50 -07001440 StripCNCodecs(&offer_audio_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 }
zhihuang1c378ed2017-08-17 14:10:50 -07001442 FilterDataCodecs(&offer_data_codecs,
1443 session_options.data_channel_type == DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444
1445 RtpHeaderExtensions audio_rtp_extensions;
1446 RtpHeaderExtensions video_rtp_extensions;
Steve Anton8f66ddb2018-12-10 16:08:05 -08001447 GetRtpHdrExtsToOffer(current_active_contents, &audio_rtp_extensions,
1448 &video_rtp_extensions);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449
Steve Anton5c72e712018-12-10 14:25:30 -08001450 auto offer = absl::make_unique<SessionDescription>();
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001451
zhihuang1c378ed2017-08-17 14:10:50 -07001452 // Iterate through the media description options, matching with existing media
1453 // descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001454 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001455 for (const MediaDescriptionOptions& media_description_options :
1456 session_options.media_description_options) {
1457 const ContentInfo* current_content = nullptr;
1458 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001459 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001460 current_content = &current_description->contents()[msection_index];
Steve Antondcc3c022017-12-22 16:02:54 -08001461 // Media type must match unless this media section is being recycled.
Steve Anton5c72e712018-12-10 14:25:30 -08001462 RTC_DCHECK(current_content->name != media_description_options.mid ||
Steve Antondcc3c022017-12-22 16:02:54 -08001463 IsMediaContentOfType(current_content,
zhihuang1c378ed2017-08-17 14:10:50 -07001464 media_description_options.type));
1465 }
1466 switch (media_description_options.type) {
1467 case MEDIA_TYPE_AUDIO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001468 if (!AddAudioContentForOffer(
1469 media_description_options, session_options, current_content,
1470 current_description, audio_rtp_extensions, offer_audio_codecs,
1471 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001472 return nullptr;
1473 }
1474 break;
1475 case MEDIA_TYPE_VIDEO:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001476 if (!AddVideoContentForOffer(
1477 media_description_options, session_options, current_content,
1478 current_description, video_rtp_extensions, offer_video_codecs,
1479 &current_streams, offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001480 return nullptr;
1481 }
1482 break;
1483 case MEDIA_TYPE_DATA:
1484 if (!AddDataContentForOffer(media_description_options, session_options,
1485 current_content, current_description,
1486 offer_data_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001487 offer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001488 return nullptr;
1489 }
1490 break;
1491 default:
1492 RTC_NOTREACHED();
1493 }
1494 ++msection_index;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 }
1496
1497 // Bundle the contents together, if we've been asked to do so, and update any
1498 // parameters that need to be tweaked for BUNDLE.
Steve Anton2bed3972019-01-04 17:04:30 -08001499 if (session_options.bundle_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
zhihuang1c378ed2017-08-17 14:10:50 -07001501 for (const ContentInfo& content : offer->contents()) {
Steve Anton2bed3972019-01-04 17:04:30 -08001502 if (content.rejected) {
1503 continue;
1504 }
zhihuang1c378ed2017-08-17 14:10:50 -07001505 // TODO(deadbeef): There are conditions that make bundling two media
1506 // descriptions together illegal. For example, they use the same payload
1507 // type to represent different codecs, or same IDs for different header
1508 // extensions. We need to detect this and not try to bundle those media
1509 // descriptions together.
1510 offer_bundle.AddContentName(content.name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 }
Steve Anton2bed3972019-01-04 17:04:30 -08001512 if (!offer_bundle.content_names().empty()) {
1513 offer->AddGroup(offer_bundle);
1514 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1515 RTC_LOG(LS_ERROR)
1516 << "CreateOffer failed to UpdateTransportInfoForBundle.";
1517 return nullptr;
1518 }
1519 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1520 RTC_LOG(LS_ERROR)
1521 << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1522 return nullptr;
1523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 }
1525 }
Steve Antone831b8c2018-02-01 12:22:16 -08001526
1527 // The following determines how to signal MSIDs to ensure compatibility with
1528 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001529 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001530 // Be conservative and signal using both a=msid and a=ssrc lines. Unified
1531 // Plan answerers will look at a=msid and Plan B answerers will look at the
1532 // a=ssrc MSID line.
1533 offer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1534 cricket::kMsidSignalingSsrcAttribute);
1535 } else {
1536 // Plan B always signals MSID using a=ssrc lines.
1537 offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1538 }
1539
Johannes Kron89f874e2018-11-12 10:25:48 +01001540 offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed);
1541
Piotr (Peter) Slatalab1ae10b2019-03-01 11:14:05 -08001542 if (session_options.media_transport_settings.has_value()) {
1543 offer->AddMediaTransportSetting(
1544 session_options.media_transport_settings->transport_name,
1545 session_options.media_transport_settings->transport_setting);
1546 }
1547
Steve Anton6fe1fba2018-12-11 10:15:23 -08001548 return offer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549}
1550
Steve Anton6fe1fba2018-12-11 10:15:23 -08001551std::unique_ptr<SessionDescription>
1552MediaSessionDescriptionFactory::CreateAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07001553 const SessionDescription* offer,
1554 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 const SessionDescription* current_description) const {
deadbeefb7892532017-02-22 19:35:18 -08001556 if (!offer) {
1557 return nullptr;
1558 }
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001559
Steve Anton5c72e712018-12-10 14:25:30 -08001560 // Must have options for exactly as many sections as in the offer.
1561 RTC_DCHECK_EQ(offer->contents().size(),
1562 session_options.media_description_options.size());
1563
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001564 IceCredentialsIterator ice_credentials(
1565 session_options.pooled_ice_credentials);
1566
Steve Anton5c72e712018-12-10 14:25:30 -08001567 std::vector<const ContentInfo*> current_active_contents;
1568 if (current_description) {
1569 current_active_contents =
1570 GetActiveContents(*current_description, session_options);
1571 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572
Steve Anton5c72e712018-12-10 14:25:30 -08001573 StreamParamsVec current_streams =
1574 GetCurrentStreamParams(current_active_contents);
Johannes Kron0854eb62018-10-10 22:33:20 +02001575
zhihuang1c378ed2017-08-17 14:10:50 -07001576 // Get list of all possible codecs that respects existing payload type
1577 // mappings and uses a single payload type space.
1578 //
1579 // Note that these lists may be further filtered for each m= section; this
1580 // step is done just to establish the payload type mappings shared by all
1581 // sections.
1582 AudioCodecs answer_audio_codecs;
1583 VideoCodecs answer_video_codecs;
1584 DataCodecs answer_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001585 GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs,
zhihuang1c378ed2017-08-17 14:10:50 -07001586 &answer_video_codecs, &answer_data_codecs);
1587
1588 if (!session_options.vad_enabled) {
1589 // If application doesn't want CN codecs in answer.
1590 StripCNCodecs(&answer_audio_codecs);
1591 }
1592 FilterDataCodecs(&answer_data_codecs,
1593 session_options.data_channel_type == DCT_SCTP);
1594
Steve Anton5c72e712018-12-10 14:25:30 -08001595 auto answer = absl::make_unique<SessionDescription>();
1596
1597 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1598 // group in the answer with the appropriate content names.
1599 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1600 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1601 // Transport info shared by the bundle group.
1602 std::unique_ptr<TransportInfo> bundle_transport;
1603
1604 answer->set_extmap_allow_mixed(offer->extmap_allow_mixed());
1605
zhihuang1c378ed2017-08-17 14:10:50 -07001606 // Iterate through the media description options, matching with existing
1607 // media descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001608 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001609 for (const MediaDescriptionOptions& media_description_options :
1610 session_options.media_description_options) {
1611 const ContentInfo* offer_content = &offer->contents()[msection_index];
1612 // Media types and MIDs must match between the remote offer and the
1613 // MediaDescriptionOptions.
1614 RTC_DCHECK(
1615 IsMediaContentOfType(offer_content, media_description_options.type));
1616 RTC_DCHECK(media_description_options.mid == offer_content->name);
1617 const ContentInfo* current_content = nullptr;
1618 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001619 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001620 current_content = &current_description->contents()[msection_index];
deadbeefb7892532017-02-22 19:35:18 -08001621 }
zhihuang1c378ed2017-08-17 14:10:50 -07001622 switch (media_description_options.type) {
1623 case MEDIA_TYPE_AUDIO:
1624 if (!AddAudioContentForAnswer(
1625 media_description_options, session_options, offer_content,
1626 offer, current_content, current_description,
1627 bundle_transport.get(), answer_audio_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001628 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001629 return nullptr;
1630 }
1631 break;
1632 case MEDIA_TYPE_VIDEO:
1633 if (!AddVideoContentForAnswer(
1634 media_description_options, session_options, offer_content,
1635 offer, current_content, current_description,
1636 bundle_transport.get(), answer_video_codecs, &current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001637 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001638 return nullptr;
1639 }
1640 break;
1641 case MEDIA_TYPE_DATA:
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001642 if (!AddDataContentForAnswer(
1643 media_description_options, session_options, offer_content,
1644 offer, current_content, current_description,
1645 bundle_transport.get(), answer_data_codecs, &current_streams,
1646 answer.get(), &ice_credentials)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001647 return nullptr;
1648 }
1649 break;
1650 default:
1651 RTC_NOTREACHED();
1652 }
1653 ++msection_index;
deadbeefb7892532017-02-22 19:35:18 -08001654 // See if we can add the newly generated m= section to the BUNDLE group in
1655 // the answer.
1656 ContentInfo& added = answer->contents().back();
zhihuang1c378ed2017-08-17 14:10:50 -07001657 if (!added.rejected && session_options.bundle_enabled && offer_bundle &&
deadbeefb7892532017-02-22 19:35:18 -08001658 offer_bundle->HasContentName(added.name)) {
1659 answer_bundle.AddContentName(added.name);
1660 bundle_transport.reset(
1661 new TransportInfo(*answer->GetTransportInfoByName(added.name)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 }
1664
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001665 // If a BUNDLE group was offered, put a BUNDLE group in the answer even if
1666 // it's empty. RFC5888 says:
1667 //
1668 // A SIP entity that receives an offer that contains an "a=group" line
1669 // with semantics that are understood MUST return an answer that
1670 // contains an "a=group" line with the same semantics.
1671 if (offer_bundle) {
deadbeefb7892532017-02-22 19:35:18 -08001672 answer->AddGroup(answer_bundle);
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001673 }
deadbeefb7892532017-02-22 19:35:18 -08001674
Taylor Brandstetter0ab56512018-04-12 10:30:48 -07001675 if (answer_bundle.FirstContentName()) {
deadbeefb7892532017-02-22 19:35:18 -08001676 // Share the same ICE credentials and crypto params across all contents,
1677 // as BUNDLE requires.
1678 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001679 RTC_LOG(LS_ERROR)
1680 << "CreateAnswer failed to UpdateTransportInfoForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001681 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683
deadbeefb7892532017-02-22 19:35:18 -08001684 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001685 RTC_LOG(LS_ERROR)
1686 << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001687 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 }
1689 }
1690
Steve Antone831b8c2018-02-01 12:22:16 -08001691 // The following determines how to signal MSIDs to ensure compatibility with
1692 // older endpoints (in particular, older Plan B endpoints).
Steve Anton8f66ddb2018-12-10 16:08:05 -08001693 if (is_unified_plan_) {
Steve Antone831b8c2018-02-01 12:22:16 -08001694 // Unified Plan needs to look at what the offer included to find the most
1695 // compatible answer.
1696 if (offer->msid_signaling() == 0) {
1697 // We end up here in one of three cases:
1698 // 1. An empty offer. We'll reply with an empty answer so it doesn't
1699 // matter what we pick here.
1700 // 2. A data channel only offer. We won't add any MSIDs to the answer so
1701 // it also doesn't matter what we pick here.
1702 // 3. Media that's either sendonly or inactive from the remote endpoint.
1703 // We don't have any information to say whether the endpoint is Plan B
1704 // or Unified Plan, so be conservative and send both.
1705 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1706 cricket::kMsidSignalingSsrcAttribute);
1707 } else if (offer->msid_signaling() ==
1708 (cricket::kMsidSignalingMediaSection |
1709 cricket::kMsidSignalingSsrcAttribute)) {
1710 // If both a=msid and a=ssrc MSID signaling methods were used, we're
1711 // probably talking to a Unified Plan endpoint so respond with just
1712 // a=msid.
1713 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection);
1714 } else {
1715 // Otherwise, it's clear which method the offerer is using so repeat that
1716 // back to them.
1717 answer->set_msid_signaling(offer->msid_signaling());
1718 }
1719 } else {
1720 // Plan B always signals MSID using a=ssrc lines.
1721 answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1722 }
1723
Steve Anton6fe1fba2018-12-11 10:15:23 -08001724 return answer;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725}
1726
ossu075af922016-06-14 03:29:38 -07001727const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer(
1728 const RtpTransceiverDirection& direction) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001729 switch (direction) {
1730 // If stream is inactive - generate list as if sendrecv.
1731 case RtpTransceiverDirection::kSendRecv:
1732 case RtpTransceiverDirection::kInactive:
1733 return audio_sendrecv_codecs_;
1734 case RtpTransceiverDirection::kSendOnly:
1735 return audio_send_codecs_;
1736 case RtpTransceiverDirection::kRecvOnly:
1737 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001738 }
Steve Anton1d03a752017-11-27 14:30:09 -08001739 RTC_NOTREACHED();
1740 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001741}
1742
1743const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer(
1744 const RtpTransceiverDirection& offer,
1745 const RtpTransceiverDirection& answer) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001746 switch (answer) {
1747 // For inactive and sendrecv answers, generate lists as if we were to accept
1748 // the offer's direction. See RFC 3264 Section 6.1.
1749 case RtpTransceiverDirection::kSendRecv:
1750 case RtpTransceiverDirection::kInactive:
1751 return GetAudioCodecsForOffer(
1752 webrtc::RtpTransceiverDirectionReversed(offer));
1753 case RtpTransceiverDirection::kSendOnly:
ossu075af922016-06-14 03:29:38 -07001754 return audio_send_codecs_;
Steve Anton1d03a752017-11-27 14:30:09 -08001755 case RtpTransceiverDirection::kRecvOnly:
1756 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001757 }
Steve Anton1d03a752017-11-27 14:30:09 -08001758 RTC_NOTREACHED();
1759 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001760}
1761
Steve Anton5c72e712018-12-10 14:25:30 -08001762void MergeCodecsFromDescription(
1763 const std::vector<const ContentInfo*>& current_active_contents,
1764 AudioCodecs* audio_codecs,
1765 VideoCodecs* video_codecs,
1766 DataCodecs* data_codecs,
1767 UsedPayloadTypes* used_pltypes) {
1768 for (const ContentInfo* content : current_active_contents) {
1769 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001770 const AudioContentDescription* audio =
Steve Anton5c72e712018-12-10 14:25:30 -08001771 content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001772 MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001773 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001774 const VideoContentDescription* video =
Steve Anton5c72e712018-12-10 14:25:30 -08001775 content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001776 MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes);
Steve Anton5c72e712018-12-10 14:25:30 -08001777 } else if (IsMediaContentOfType(content, MEDIA_TYPE_DATA)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001778 const DataContentDescription* data =
Steve Anton5c72e712018-12-10 14:25:30 -08001779 content->media_description()->as_data();
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00001780 if (data) {
1781 // Only relevant for RTP datachannels
1782 MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes);
1783 }
zhihuang1c378ed2017-08-17 14:10:50 -07001784 }
1785 }
1786}
1787
1788// Getting codecs for an offer involves these steps:
1789//
1790// 1. Construct payload type -> codec mappings for current description.
1791// 2. Add any reference codecs that weren't already present
1792// 3. For each individual media description (m= section), filter codecs based
1793// on the directional attribute (happens in another method).
1794void MediaSessionDescriptionFactory::GetCodecsForOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001795 const std::vector<const ContentInfo*>& current_active_contents,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 AudioCodecs* audio_codecs,
1797 VideoCodecs* video_codecs,
1798 DataCodecs* data_codecs) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 // First - get all codecs from the current description if the media type
zhihuang1c378ed2017-08-17 14:10:50 -07001800 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1801 // new media type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001802 UsedPayloadTypes used_pltypes;
1803 MergeCodecsFromDescription(current_active_contents, audio_codecs,
1804 video_codecs, data_codecs, &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805
Steve Anton5c72e712018-12-10 14:25:30 -08001806 // Add our codecs that are not in the current description.
zhihuang1c378ed2017-08-17 14:10:50 -07001807 MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes);
1808 MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1809 MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1810}
1811
1812// Getting codecs for an answer involves these steps:
1813//
1814// 1. Construct payload type -> codec mappings for current description.
1815// 2. Add any codecs from the offer that weren't already present.
1816// 3. Add any remaining codecs that weren't already present.
1817// 4. For each individual media description (m= section), filter codecs based
1818// on the directional attribute (happens in another method).
1819void MediaSessionDescriptionFactory::GetCodecsForAnswer(
Steve Anton5c72e712018-12-10 14:25:30 -08001820 const std::vector<const ContentInfo*>& current_active_contents,
1821 const SessionDescription& remote_offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001822 AudioCodecs* audio_codecs,
1823 VideoCodecs* video_codecs,
1824 DataCodecs* data_codecs) const {
zhihuang1c378ed2017-08-17 14:10:50 -07001825 // First - get all codecs from the current description if the media type
1826 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1827 // new media type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001828 UsedPayloadTypes used_pltypes;
1829 MergeCodecsFromDescription(current_active_contents, audio_codecs,
1830 video_codecs, data_codecs, &used_pltypes);
zhihuang1c378ed2017-08-17 14:10:50 -07001831
1832 // Second - filter out codecs that we don't support at all and should ignore.
1833 AudioCodecs filtered_offered_audio_codecs;
1834 VideoCodecs filtered_offered_video_codecs;
1835 DataCodecs filtered_offered_data_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08001836 for (const ContentInfo& content : remote_offer.contents()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001837 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1838 const AudioContentDescription* audio =
Steve Antonb1c1de12017-12-21 15:14:30 -08001839 content.media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001840 for (const AudioCodec& offered_audio_codec : audio->codecs()) {
1841 if (!FindMatchingCodec<AudioCodec>(audio->codecs(),
1842 filtered_offered_audio_codecs,
1843 offered_audio_codec, nullptr) &&
1844 FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_,
1845 offered_audio_codec, nullptr)) {
1846 filtered_offered_audio_codecs.push_back(offered_audio_codec);
1847 }
1848 }
1849 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1850 const VideoContentDescription* video =
Steve Antonb1c1de12017-12-21 15:14:30 -08001851 content.media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001852 for (const VideoCodec& offered_video_codec : video->codecs()) {
1853 if (!FindMatchingCodec<VideoCodec>(video->codecs(),
1854 filtered_offered_video_codecs,
1855 offered_video_codec, nullptr) &&
1856 FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_,
1857 offered_video_codec, nullptr)) {
1858 filtered_offered_video_codecs.push_back(offered_video_codec);
1859 }
1860 }
1861 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1862 const DataContentDescription* data =
Steve Antonb1c1de12017-12-21 15:14:30 -08001863 content.media_description()->as_data();
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00001864 if (data) {
1865 // RTP data. This part is inactive for SCTP data.
1866 for (const DataCodec& offered_data_codec : data->codecs()) {
1867 if (!FindMatchingCodec<DataCodec>(data->codecs(),
1868 filtered_offered_data_codecs,
1869 offered_data_codec, nullptr) &&
1870 FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_,
1871 offered_data_codec, nullptr)) {
1872 filtered_offered_data_codecs.push_back(offered_data_codec);
1873 }
zhihuang1c378ed2017-08-17 14:10:50 -07001874 }
1875 }
1876 }
1877 }
1878
Steve Anton5c72e712018-12-10 14:25:30 -08001879 // Add codecs that are not in the current description but were in
zhihuang1c378ed2017-08-17 14:10:50 -07001880 // |remote_offer|.
1881 MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs,
1882 &used_pltypes);
1883 MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs,
1884 &used_pltypes);
1885 MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs,
1886 &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887}
1888
1889void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
Steve Anton5c72e712018-12-10 14:25:30 -08001890 const std::vector<const ContentInfo*>& current_active_contents,
zhihuang1c378ed2017-08-17 14:10:50 -07001891 RtpHeaderExtensions* offer_audio_extensions,
1892 RtpHeaderExtensions* offer_video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001893 // All header extensions allocated from the same range to avoid potential
1894 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 UsedRtpHeaderExtensionIds used_ids;
jbauch5869f502017-06-29 12:31:36 -07001896 RtpHeaderExtensions all_regular_extensions;
1897 RtpHeaderExtensions all_encrypted_extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898
1899 // First - get all extensions from the current description if the media type
1900 // is used.
1901 // Add them to |used_ids| so the local ids are not reused if a new media
1902 // type is added.
Steve Anton5c72e712018-12-10 14:25:30 -08001903 for (const ContentInfo* content : current_active_contents) {
1904 if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
1905 const AudioContentDescription* audio =
1906 content->media_description()->as_audio();
1907 MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions,
1908 &all_regular_extensions, &all_encrypted_extensions,
1909 &used_ids);
1910 } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
1911 const VideoContentDescription* video =
1912 content->media_description()->as_video();
1913 MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions,
1914 &all_regular_extensions, &all_encrypted_extensions,
1915 &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916 }
1917 }
1918
Steve Anton5c72e712018-12-10 14:25:30 -08001919 // Add our default RTP header extensions that are not in the current
1920 // description.
Steve Anton8f66ddb2018-12-10 16:08:05 -08001921 MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions,
1922 &all_regular_extensions, &all_encrypted_extensions,
1923 &used_ids);
1924 MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions,
1925 &all_regular_extensions, &all_encrypted_extensions,
1926 &used_ids);
zhihuang1c378ed2017-08-17 14:10:50 -07001927
jbauch5869f502017-06-29 12:31:36 -07001928 // TODO(jbauch): Support adding encrypted header extensions to existing
1929 // sessions.
Steve Anton5c72e712018-12-10 14:25:30 -08001930 if (enable_encrypted_rtp_header_extensions_ &&
1931 current_active_contents.empty()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001932 AddEncryptedVersionsOfHdrExts(offer_audio_extensions,
1933 &all_encrypted_extensions, &used_ids);
1934 AddEncryptedVersionsOfHdrExts(offer_video_extensions,
1935 &all_encrypted_extensions, &used_ids);
jbauch5869f502017-06-29 12:31:36 -07001936 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937}
1938
1939bool MediaSessionDescriptionFactory::AddTransportOffer(
Yves Gerey665174f2018-06-19 15:03:05 +02001940 const std::string& content_name,
1941 const TransportOptions& transport_options,
1942 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001943 SessionDescription* offer_desc,
1944 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 if (!transport_desc_factory_)
Yves Gerey665174f2018-06-19 15:03:05 +02001946 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 const TransportDescription* current_tdesc =
1948 GetTransportDescription(content_name, current_desc);
kwiberg31022942016-03-11 14:18:21 -08001949 std::unique_ptr<TransportDescription> new_tdesc(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001950 transport_desc_factory_->CreateOffer(transport_options, current_tdesc,
1951 ice_credentials));
Steve Anton06817cd2018-12-18 15:55:30 -08001952 if (!new_tdesc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001953 RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name="
1954 << content_name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 }
Steve Anton06817cd2018-12-18 15:55:30 -08001956 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc));
1957 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958}
1959
Steve Anton1a9d3c32018-12-10 17:18:54 -08001960std::unique_ptr<TransportDescription>
1961MediaSessionDescriptionFactory::CreateTransportAnswer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 const std::string& content_name,
1963 const SessionDescription* offer_desc,
1964 const TransportOptions& transport_options,
deadbeefb7892532017-02-22 19:35:18 -08001965 const SessionDescription* current_desc,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001966 bool require_transport_attributes,
1967 IceCredentialsIterator* ice_credentials) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 if (!transport_desc_factory_)
1969 return NULL;
1970 const TransportDescription* offer_tdesc =
1971 GetTransportDescription(content_name, offer_desc);
1972 const TransportDescription* current_tdesc =
1973 GetTransportDescription(content_name, current_desc);
deadbeefb7892532017-02-22 19:35:18 -08001974 return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1975 require_transport_attributes,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02001976 current_tdesc, ice_credentials);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977}
1978
1979bool MediaSessionDescriptionFactory::AddTransportAnswer(
1980 const std::string& content_name,
1981 const TransportDescription& transport_desc,
1982 SessionDescription* answer_desc) const {
Steve Anton06817cd2018-12-18 15:55:30 -08001983 answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 return true;
1985}
1986
zhihuang1c378ed2017-08-17 14:10:50 -07001987// |audio_codecs| = set of all possible codecs that can be used, with correct
1988// payload type mappings
1989//
1990// |supported_audio_codecs| = set of codecs that are supported for the direction
1991// of this m= section
1992//
1993// acd->codecs() = set of previously negotiated codecs for this m= section
1994//
1995// The payload types should come from audio_codecs, but the order should come
1996// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
1997// change existing codec priority, and that new codecs are added with the right
1998// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001999bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002000 const MediaDescriptionOptions& media_description_options,
2001 const MediaSessionOptions& session_options,
2002 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002003 const SessionDescription* current_description,
2004 const RtpHeaderExtensions& audio_rtp_extensions,
2005 const AudioCodecs& audio_codecs,
2006 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002007 SessionDescription* desc,
2008 IceCredentialsIterator* ice_credentials) const {
zhihuang1c378ed2017-08-17 14:10:50 -07002009 // Filter audio_codecs (which includes all codecs, with correctly remapped
2010 // payload types) based on transceiver direction.
2011 const AudioCodecs& supported_audio_codecs =
2012 GetAudioCodecsForOffer(media_description_options.direction);
2013
2014 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002015 // Add the codecs from current content if it exists and is not rejected nor
2016 // recycled.
2017 if (current_content && !current_content->rejected &&
2018 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002019 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002020 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002021 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002022 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002023 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2024 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002025 filtered_codecs.push_back(codec);
2026 }
2027 }
2028 }
2029 // Add other supported audio codecs.
2030 AudioCodec found_codec;
2031 for (const AudioCodec& codec : supported_audio_codecs) {
2032 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
2033 codec, &found_codec) &&
2034 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2035 codec, nullptr)) {
2036 // Use the |found_codec| from |audio_codecs| because it has the correctly
2037 // mapped payload type.
2038 filtered_codecs.push_back(found_codec);
2039 }
2040 }
deadbeef44f08192015-12-15 16:20:09 -08002041
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002042 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002043 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2044 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002045
kwiberg31022942016-03-11 14:18:21 -08002046 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002047 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002048 GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options,
2049 &crypto_suites);
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002050 if (!CreateMediaContentOffer(media_description_options, session_options,
2051 filtered_codecs, sdes_policy,
2052 GetCryptos(current_content), crypto_suites,
2053 audio_rtp_extensions, ssrc_generator_,
2054 current_streams, audio.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002055 return false;
2056 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002057
2058 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2059 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00002060
Steve Anton4e70a722017-11-28 14:57:10 -08002061 audio->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002062
Steve Anton5adfafd2017-12-20 16:34:00 -08002063 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002064 media_description_options.stopped, audio.release());
2065 if (!AddTransportOffer(media_description_options.mid,
2066 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002067 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002068 return false;
2069 }
2070
2071 return true;
2072}
2073
2074bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002075 const MediaDescriptionOptions& media_description_options,
2076 const MediaSessionOptions& session_options,
2077 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002078 const SessionDescription* current_description,
2079 const RtpHeaderExtensions& video_rtp_extensions,
2080 const VideoCodecs& video_codecs,
2081 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002082 SessionDescription* desc,
2083 IceCredentialsIterator* ice_credentials) const {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002084 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07002085 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2086 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002087
kwiberg31022942016-03-11 14:18:21 -08002088 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002089 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07002090 GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options,
2091 &crypto_suites);
2092
2093 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002094 // Add the codecs from current content if it exists and is not rejected nor
2095 // recycled.
2096 if (current_content && !current_content->rejected &&
2097 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002098 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002099 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002100 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002101 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002102 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002103 nullptr)) {
2104 filtered_codecs.push_back(codec);
2105 }
2106 }
2107 }
2108 // Add other supported video codecs.
2109 VideoCodec found_codec;
2110 for (const VideoCodec& codec : video_codecs_) {
2111 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
2112 &found_codec) &&
2113 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2114 nullptr)) {
2115 // Use the |found_codec| from |video_codecs| because it has the correctly
2116 // mapped payload type.
2117 filtered_codecs.push_back(found_codec);
2118 }
2119 }
2120
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002121 if (!CreateMediaContentOffer(media_description_options, session_options,
2122 filtered_codecs, sdes_policy,
2123 GetCryptos(current_content), crypto_suites,
2124 video_rtp_extensions, ssrc_generator_,
2125 current_streams, video.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002126 return false;
2127 }
2128
zhihuang1c378ed2017-08-17 14:10:50 -07002129 video->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002130
2131 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2132 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002133
Steve Anton4e70a722017-11-28 14:57:10 -08002134 video->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00002135
Steve Anton5adfafd2017-12-20 16:34:00 -08002136 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002137 media_description_options.stopped, video.release());
2138 if (!AddTransportOffer(media_description_options.mid,
2139 media_description_options.transport_options,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002140 current_description, desc, ice_credentials)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002141 return false;
2142 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002143 return true;
2144}
2145
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002146bool MediaSessionDescriptionFactory::AddSctpDataContentForOffer(
2147 const MediaDescriptionOptions& media_description_options,
2148 const MediaSessionOptions& session_options,
2149 const ContentInfo* current_content,
2150 const SessionDescription* current_description,
2151 StreamParamsVec* current_streams,
2152 SessionDescription* desc,
2153 IceCredentialsIterator* ice_credentials) const {
2154 std::unique_ptr<SctpDataContentDescription> data(
2155 new SctpDataContentDescription());
2156
2157 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2158
2159 cricket::SecurePolicy sdes_policy =
2160 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2161 : secure();
2162 std::vector<std::string> crypto_suites;
2163 // SDES doesn't make sense for SCTP, so we disable it, and we only
2164 // get SDES crypto suites for RTP-based data channels.
2165 sdes_policy = cricket::SEC_DISABLED;
2166 // Unlike SetMediaProtocol below, we need to set the protocol
2167 // before we call CreateMediaContentOffer. Otherwise,
2168 // CreateMediaContentOffer won't know this is SCTP and will
2169 // generate SSRCs rather than SIDs.
2170 // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once
2171 // it's safe to do so. Older versions of webrtc would reject these
2172 // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706.
2173 data->set_protocol(secure_transport ? kMediaProtocolDtlsSctp
2174 : kMediaProtocolSctp);
2175
2176 if (!CreateContentOffer(media_description_options, session_options,
2177 sdes_policy, GetCryptos(current_content),
2178 crypto_suites, RtpHeaderExtensions(), ssrc_generator_,
2179 current_streams, data.get())) {
2180 return false;
2181 }
2182
2183 desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp,
2184 data.release());
2185 if (!AddTransportOffer(media_description_options.mid,
2186 media_description_options.transport_options,
2187 current_description, desc, ice_credentials)) {
2188 return false;
2189 }
2190 return true;
2191}
2192
2193bool MediaSessionDescriptionFactory::AddRtpDataContentForOffer(
2194 const MediaDescriptionOptions& media_description_options,
2195 const MediaSessionOptions& session_options,
2196 const ContentInfo* current_content,
2197 const SessionDescription* current_description,
2198 const DataCodecs& data_codecs,
2199 StreamParamsVec* current_streams,
2200 SessionDescription* desc,
2201 IceCredentialsIterator* ice_credentials) const {
2202 std::unique_ptr<DataContentDescription> data(new DataContentDescription());
2203 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
2204
2205 cricket::SecurePolicy sdes_policy =
2206 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
2207 : secure();
2208 std::vector<std::string> crypto_suites;
2209 GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options,
2210 &crypto_suites);
2211 if (!CreateMediaContentOffer(
2212 media_description_options, session_options, data_codecs, sdes_policy,
2213 GetCryptos(current_content), crypto_suites, RtpHeaderExtensions(),
2214 ssrc_generator_, current_streams, data.get())) {
2215 return false;
2216 }
2217
2218 data->set_bandwidth(kDataMaxBandwidth);
2219 SetMediaProtocol(secure_transport, data.get());
2220 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
2221 media_description_options.stopped, data.release());
2222 if (!AddTransportOffer(media_description_options.mid,
2223 media_description_options.transport_options,
2224 current_description, desc, ice_credentials)) {
2225 return false;
2226 }
2227 return true;
2228}
2229
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002230bool MediaSessionDescriptionFactory::AddDataContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002231 const MediaDescriptionOptions& media_description_options,
2232 const MediaSessionOptions& session_options,
2233 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002234 const SessionDescription* current_description,
zhihuang1c378ed2017-08-17 14:10:50 -07002235 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002236 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002237 SessionDescription* desc,
2238 IceCredentialsIterator* ice_credentials) const {
zhihuang1c378ed2017-08-17 14:10:50 -07002239 bool is_sctp = (session_options.data_channel_type == DCT_SCTP);
2240 // If the DataChannel type is not specified, use the DataChannel type in
2241 // the current description.
2242 if (session_options.data_channel_type == DCT_NONE && current_content) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002243 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA));
Steve Antonb1c1de12017-12-21 15:14:30 -08002244 is_sctp = (current_content->media_description()->protocol() ==
2245 kMediaProtocolSctp);
zhihuang1c378ed2017-08-17 14:10:50 -07002246 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002247 if (is_sctp) {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002248 return AddSctpDataContentForOffer(
2249 media_description_options, session_options, current_content,
2250 current_description, current_streams, desc, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002251 } else {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002252 return AddRtpDataContentForOffer(media_description_options, session_options,
2253 current_content, current_description,
2254 data_codecs, current_streams, desc,
2255 ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002256 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002257}
2258
zhihuang1c378ed2017-08-17 14:10:50 -07002259// |audio_codecs| = set of all possible codecs that can be used, with correct
2260// payload type mappings
2261//
2262// |supported_audio_codecs| = set of codecs that are supported for the direction
2263// of this m= section
2264//
2265// acd->codecs() = set of previously negotiated codecs for this m= section
2266//
2267// The payload types should come from audio_codecs, but the order should come
2268// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
2269// change existing codec priority, and that new codecs are added with the right
2270// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002271bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002272 const MediaDescriptionOptions& media_description_options,
2273 const MediaSessionOptions& session_options,
2274 const ContentInfo* offer_content,
2275 const SessionDescription* offer_description,
2276 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002277 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002278 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002279 const AudioCodecs& audio_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002280 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002281 SessionDescription* answer,
2282 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002283 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002284 const AudioContentDescription* offer_audio_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002285 offer_content->media_description()->as_audio();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002286
Steve Anton1a9d3c32018-12-10 17:18:54 -08002287 std::unique_ptr<TransportDescription> audio_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002288 media_description_options.mid, offer_description,
2289 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002290 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002291 if (!audio_transport) {
2292 return false;
2293 }
2294
zhihuang1c378ed2017-08-17 14:10:50 -07002295 // Pick codecs based on the requested communications direction in the offer
2296 // and the selected direction in the answer.
2297 // Note these will be filtered one final time in CreateMediaContentAnswer.
2298 auto wants_rtd = media_description_options.direction;
Steve Anton4e70a722017-11-28 14:57:10 -08002299 auto offer_rtd = offer_audio_description->direction();
ossu075af922016-06-14 03:29:38 -07002300 auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd);
zhihuang1c378ed2017-08-17 14:10:50 -07002301 AudioCodecs supported_audio_codecs =
2302 GetAudioCodecsForAnswer(offer_rtd, answer_rtd);
2303
2304 AudioCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002305 // Add the codecs from current content if it exists and is not rejected nor
2306 // recycled.
2307 if (current_content && !current_content->rejected &&
2308 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002309 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002310 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002311 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002312 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002313 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2314 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002315 filtered_codecs.push_back(codec);
2316 }
2317 }
2318 }
2319 // Add other supported audio codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002320 for (const AudioCodec& codec : supported_audio_codecs) {
2321 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
Zhi Huang6f367472017-11-22 13:20:02 -08002322 codec, nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002323 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2324 codec, nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002325 // We should use the local codec with local parameters and the codec id
2326 // would be correctly mapped in |NegotiateCodecs|.
2327 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002328 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002329 }
2330
zhihuang1c378ed2017-08-17 14:10:50 -07002331 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2332 session_options.bundle_enabled;
kwiberg31022942016-03-11 14:18:21 -08002333 std::unique_ptr<AudioContentDescription> audio_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002334 new AudioContentDescription());
2335 // Do not require or create SDES cryptos if DTLS is used.
2336 cricket::SecurePolicy sdes_policy =
2337 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002338 if (!SetCodecsInAnswer(offer_audio_description, filtered_codecs,
2339 media_description_options, session_options,
2340 ssrc_generator_, current_streams,
2341 audio_answer.get())) {
2342 return false;
2343 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002344 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002345 offer_audio_description, media_description_options, session_options,
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002346 sdes_policy, GetCryptos(current_content),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002347 audio_rtp_header_extensions(), ssrc_generator_,
Steve Anton1b8773d2018-04-06 11:13:34 -07002348 enable_encrypted_rtp_header_extensions_, current_streams,
2349 bundle_enabled, audio_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002350 return false; // Fails the session setup.
2351 }
2352
deadbeefb7892532017-02-22 19:35:18 -08002353 bool secure = bundle_transport ? bundle_transport->description.secure()
2354 : audio_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002355 bool rejected = media_description_options.stopped ||
2356 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002357 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
2358 audio_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002359 if (!AddTransportAnswer(media_description_options.mid,
2360 *(audio_transport.get()), answer)) {
2361 return false;
2362 }
2363
2364 if (rejected) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002365 RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid
2366 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002367 }
2368
zhihuang1c378ed2017-08-17 14:10:50 -07002369 answer->AddContent(media_description_options.mid, offer_content->type,
2370 rejected, audio_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002371 return true;
2372}
2373
2374bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002375 const MediaDescriptionOptions& media_description_options,
2376 const MediaSessionOptions& session_options,
2377 const ContentInfo* offer_content,
2378 const SessionDescription* offer_description,
2379 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002380 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002381 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002382 const VideoCodecs& video_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002383 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002384 SessionDescription* answer,
2385 IceCredentialsIterator* ice_credentials) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002386 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002387 const VideoContentDescription* offer_video_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002388 offer_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002389
Steve Anton1a9d3c32018-12-10 17:18:54 -08002390 std::unique_ptr<TransportDescription> video_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002391 media_description_options.mid, offer_description,
2392 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002393 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002394 if (!video_transport) {
2395 return false;
2396 }
2397
zhihuang1c378ed2017-08-17 14:10:50 -07002398 VideoCodecs filtered_codecs;
Steve Anton5c72e712018-12-10 14:25:30 -08002399 // Add the codecs from current content if it exists and is not rejected nor
2400 // recycled.
2401 if (current_content && !current_content->rejected &&
2402 current_content->name == media_description_options.mid) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002403 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002404 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002405 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002406 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002407 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002408 nullptr)) {
2409 filtered_codecs.push_back(codec);
2410 }
2411 }
2412 }
2413 // Add other supported video codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002414 for (const VideoCodec& codec : video_codecs_) {
2415 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
Zhi Huang6f367472017-11-22 13:20:02 -08002416 nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002417 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2418 nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002419 // We should use the local codec with local parameters and the codec id
2420 // would be correctly mapped in |NegotiateCodecs|.
2421 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002422 }
2423 }
2424
2425 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2426 session_options.bundle_enabled;
2427
kwiberg31022942016-03-11 14:18:21 -08002428 std::unique_ptr<VideoContentDescription> video_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002429 new VideoContentDescription());
2430 // Do not require or create SDES cryptos if DTLS is used.
2431 cricket::SecurePolicy sdes_policy =
2432 video_transport->secure() ? cricket::SEC_DISABLED : secure();
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002433 if (!SetCodecsInAnswer(offer_video_description, filtered_codecs,
2434 media_description_options, session_options,
2435 ssrc_generator_, current_streams,
2436 video_answer.get())) {
2437 return false;
2438 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002439 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002440 offer_video_description, media_description_options, session_options,
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002441 sdes_policy, GetCryptos(current_content),
Amit Hilbuchbcd39d42019-01-25 17:13:56 -08002442 video_rtp_header_extensions(), ssrc_generator_,
Steve Anton1b8773d2018-04-06 11:13:34 -07002443 enable_encrypted_rtp_header_extensions_, current_streams,
2444 bundle_enabled, video_answer.get())) {
zhihuang1c378ed2017-08-17 14:10:50 -07002445 return false; // Failed the sessin setup.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002446 }
deadbeefb7892532017-02-22 19:35:18 -08002447 bool secure = bundle_transport ? bundle_transport->description.secure()
2448 : video_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002449 bool rejected = media_description_options.stopped ||
2450 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002451 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
2452 video_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002453 if (!AddTransportAnswer(media_description_options.mid,
2454 *(video_transport.get()), answer)) {
2455 return false;
2456 }
2457
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002458 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002459 video_answer->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002460 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002461 RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid
2462 << "' being rejected in 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, video_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002466 return true;
2467}
2468
2469bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002470 const MediaDescriptionOptions& media_description_options,
2471 const MediaSessionOptions& session_options,
2472 const ContentInfo* offer_content,
2473 const SessionDescription* offer_description,
2474 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002475 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002476 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002477 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002478 StreamParamsVec* current_streams,
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002479 SessionDescription* answer,
2480 IceCredentialsIterator* ice_credentials) const {
Steve Anton1a9d3c32018-12-10 17:18:54 -08002481 std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer(
Jonas Oreland1cd39fa2018-10-11 07:47:12 +02002482 media_description_options.mid, offer_description,
2483 media_description_options.transport_options, current_description,
Steve Anton1a9d3c32018-12-10 17:18:54 -08002484 bundle_transport != nullptr, ice_credentials);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002485 if (!data_transport) {
2486 return false;
2487 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002488
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002489 // Do not require or create SDES cryptos if DTLS is used.
2490 cricket::SecurePolicy sdes_policy =
2491 data_transport->secure() ? cricket::SEC_DISABLED : secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002492 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2493 session_options.bundle_enabled;
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002494 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA));
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002495 std::unique_ptr<MediaContentDescription> data_answer;
2496 if (offer_content->media_description()->as_sctp()) {
2497 // SCTP data content
2498 data_answer = absl::make_unique<SctpDataContentDescription>();
2499 const SctpDataContentDescription* offer_data_description =
2500 offer_content->media_description()->as_sctp();
2501 // Respond with the offerer's proto, whatever it is.
2502 data_answer->as_sctp()->set_protocol(offer_data_description->protocol());
2503 if (!CreateMediaContentAnswer(
2504 offer_data_description, media_description_options, session_options,
2505 sdes_policy, GetCryptos(current_content), RtpHeaderExtensions(),
2506 ssrc_generator_, enable_encrypted_rtp_header_extensions_,
2507 current_streams, bundle_enabled, data_answer.get())) {
2508 return false; // Fails the session setup.
2509 }
2510 // Respond with sctpmap if the offer uses sctpmap.
2511 bool offer_uses_sctpmap = offer_data_description->use_sctpmap();
2512 data_answer->as_sctp()->set_use_sctpmap(offer_uses_sctpmap);
2513 } else {
2514 // RTP offer
2515 data_answer = absl::make_unique<DataContentDescription>();
zstein4b2e0822017-02-17 19:48:38 -08002516
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002517 RTC_CHECK(offer_content->media_description()->as_data());
2518 const DataContentDescription* offer_data_description =
2519 offer_content->media_description()->as_data();
2520 if (!SetCodecsInAnswer(offer_data_description, data_codecs,
2521 media_description_options, session_options,
2522 ssrc_generator_, current_streams,
2523 data_answer->as_data())) {
2524 return false;
2525 }
2526 if (!CreateMediaContentAnswer(
2527 offer_data_description, media_description_options, session_options,
2528 sdes_policy, GetCryptos(current_content), RtpHeaderExtensions(),
2529 ssrc_generator_, enable_encrypted_rtp_header_extensions_,
2530 current_streams, bundle_enabled, data_answer.get())) {
2531 return false; // Fails the session setup.
2532 }
2533 }
Seth Hampson1859dc02019-04-16 20:55:57 +00002534
deadbeefb7892532017-02-22 19:35:18 -08002535 bool secure = bundle_transport ? bundle_transport->description.secure()
2536 : data_transport->secure();
2537
zhihuang1c378ed2017-08-17 14:10:50 -07002538 bool rejected = session_options.data_channel_type == DCT_NONE ||
2539 media_description_options.stopped ||
2540 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002541 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
2542 data_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002543 if (!AddTransportAnswer(media_description_options.mid,
2544 *(data_transport.get()), answer)) {
2545 return false;
2546 }
2547
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002548 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002549 data_answer->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002550 } else {
2551 // RFC 3264
2552 // The answer MUST contain the same number of m-lines as the offer.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002553 RTC_LOG(LS_INFO) << "Data is not supported in the answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002554 }
zhihuang1c378ed2017-08-17 14:10:50 -07002555 answer->AddContent(media_description_options.mid, offer_content->type,
2556 rejected, data_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002557 return true;
2558}
2559
zhihuang1c378ed2017-08-17 14:10:50 -07002560void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() {
2561 audio_sendrecv_codecs_.clear();
2562 all_audio_codecs_.clear();
2563 // Compute the audio codecs union.
2564 for (const AudioCodec& send : audio_send_codecs_) {
2565 all_audio_codecs_.push_back(send);
2566 if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_,
2567 send, nullptr)) {
2568 // It doesn't make sense to have an RTX codec we support sending but not
2569 // receiving.
2570 RTC_DCHECK(!IsRtxCodec(send));
2571 }
2572 }
2573 for (const AudioCodec& recv : audio_recv_codecs_) {
2574 if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_,
2575 recv, nullptr)) {
2576 all_audio_codecs_.push_back(recv);
2577 }
2578 }
2579 // Use NegotiateCodecs to merge our codec lists, since the operation is
2580 // essentially the same. Put send_codecs as the offered_codecs, which is the
2581 // order we'd like to follow. The reasoning is that encoding is usually more
2582 // expensive than decoding, and prioritizing a codec in the send list probably
2583 // means it's a codec we can handle efficiently.
2584 NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_,
2585 &audio_sendrecv_codecs_);
2586}
2587
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002588bool IsMediaContent(const ContentInfo* content) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002589 return (content && (content->type == MediaProtocolType::kRtp ||
2590 content->type == MediaProtocolType::kSctp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002591}
2592
2593bool IsAudioContent(const ContentInfo* content) {
2594 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
2595}
2596
2597bool IsVideoContent(const ContentInfo* content) {
2598 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
2599}
2600
2601bool IsDataContent(const ContentInfo* content) {
2602 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
2603}
2604
deadbeef0ed85b22016-02-23 17:24:52 -08002605const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
2606 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002607 for (const ContentInfo& content : contents) {
2608 if (IsMediaContentOfType(&content, media_type)) {
2609 return &content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610 }
2611 }
deadbeef0ed85b22016-02-23 17:24:52 -08002612 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002613}
2614
2615const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
2616 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2617}
2618
2619const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
2620 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2621}
2622
2623const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
2624 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2625}
2626
Steve Antonad7bffc2018-01-22 10:21:56 -08002627const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
2628 MediaType media_type) {
deadbeef0ed85b22016-02-23 17:24:52 -08002629 if (sdesc == nullptr) {
2630 return nullptr;
2631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632
2633 return GetFirstMediaContent(sdesc->contents(), media_type);
2634}
2635
2636const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
2637 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2638}
2639
2640const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
2641 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2642}
2643
2644const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
2645 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2646}
2647
2648const MediaContentDescription* GetFirstMediaContentDescription(
Yves Gerey665174f2018-06-19 15:03:05 +02002649 const SessionDescription* sdesc,
2650 MediaType media_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002651 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002652 return (content ? content->media_description() : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002653}
2654
2655const AudioContentDescription* GetFirstAudioContentDescription(
2656 const SessionDescription* sdesc) {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002657 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO);
2658 return desc ? desc->as_audio() : nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002659}
2660
2661const VideoContentDescription* GetFirstVideoContentDescription(
2662 const SessionDescription* sdesc) {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002663 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO);
2664 return desc ? desc->as_video() : nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002665}
2666
2667const DataContentDescription* GetFirstDataContentDescription(
2668 const SessionDescription* sdesc) {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002669 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
2670 return desc ? desc->as_data() : nullptr;
2671}
2672
2673const SctpDataContentDescription* GetFirstSctpDataContentDescription(
2674 const SessionDescription* sdesc) {
2675 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
2676 return desc ? desc->as_sctp() : nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002677}
2678
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002679//
2680// Non-const versions of the above functions.
2681//
2682
Steve Anton36b29d12017-10-30 09:57:42 -07002683ContentInfo* GetFirstMediaContent(ContentInfos* contents,
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002684 MediaType media_type) {
Steve Anton36b29d12017-10-30 09:57:42 -07002685 for (ContentInfo& content : *contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002686 if (IsMediaContentOfType(&content, media_type)) {
2687 return &content;
2688 }
2689 }
2690 return nullptr;
2691}
2692
Steve Anton36b29d12017-10-30 09:57:42 -07002693ContentInfo* GetFirstAudioContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002694 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2695}
2696
Steve Anton36b29d12017-10-30 09:57:42 -07002697ContentInfo* GetFirstVideoContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002698 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2699}
2700
Steve Anton36b29d12017-10-30 09:57:42 -07002701ContentInfo* GetFirstDataContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002702 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2703}
2704
Steve Antonad7bffc2018-01-22 10:21:56 -08002705ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
2706 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002707 if (sdesc == nullptr) {
2708 return nullptr;
2709 }
2710
Steve Anton36b29d12017-10-30 09:57:42 -07002711 return GetFirstMediaContent(&sdesc->contents(), media_type);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002712}
2713
2714ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) {
2715 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2716}
2717
2718ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) {
2719 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2720}
2721
2722ContentInfo* GetFirstDataContent(SessionDescription* sdesc) {
2723 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2724}
2725
2726MediaContentDescription* GetFirstMediaContentDescription(
2727 SessionDescription* sdesc,
2728 MediaType media_type) {
2729 ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002730 return (content ? content->media_description() : nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002731}
2732
2733AudioContentDescription* GetFirstAudioContentDescription(
2734 SessionDescription* sdesc) {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002735 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO);
2736 return desc ? desc->as_audio() : nullptr;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002737}
2738
2739VideoContentDescription* GetFirstVideoContentDescription(
2740 SessionDescription* sdesc) {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002741 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO);
2742 return desc ? desc->as_video() : nullptr;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002743}
2744
2745DataContentDescription* GetFirstDataContentDescription(
2746 SessionDescription* sdesc) {
Harald Alvestrand26bf7c42019-04-23 05:20:17 +00002747 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
2748 return desc ? desc->as_data() : nullptr;
2749}
2750
2751SctpDataContentDescription* GetFirstSctpDataContentDescription(
2752 SessionDescription* sdesc) {
2753 auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
2754 return desc ? desc->as_sctp() : nullptr;
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002755}
2756
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002757} // namespace cricket