blob: 474c0b4e4542c2d10c433b1ca5ba7d156c7911cd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
deadbeef67cf2c12016-04-13 10:07:16 -070013#include <algorithm> // For std::find_if, std::sort.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <functional>
15#include <map>
kwiberg31022942016-03-11 14:18:21 -080016#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include <set>
deadbeef67cf2c12016-04-13 10:07:16 -070018#include <unordered_map>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000019#include <utility>
20
Patrik Höglund7aee3d52017-11-15 13:15:17 +010021#include "api/cryptoparams.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/optional.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020023#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "media/base/h264_profile_level_id.h"
25#include "media/base/mediaconstants.h"
26#include "p2p/base/p2pconstants.h"
27#include "pc/channelmanager.h"
Steve Anton1d03a752017-11-27 14:30:09 -080028#include "pc/rtpmediautils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "pc/srtpfilter.h"
30#include "rtc_base/base64.h"
31#include "rtc_base/checks.h"
32#include "rtc_base/helpers.h"
33#include "rtc_base/logging.h"
34#include "rtc_base/stringutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035
36namespace {
Steve Anton1d03a752017-11-27 14:30:09 -080037
38using webrtc::RtpTransceiverDirection;
39
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040const char kInline[] = "inline:";
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080041
deadbeef7914b8c2017-04-21 03:23:33 -070042void GetSupportedSdesCryptoSuiteNames(void (*func)(const rtc::CryptoOptions&,
43 std::vector<int>*),
44 const rtc::CryptoOptions& crypto_options,
45 std::vector<std::string>* names) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080046 std::vector<int> crypto_suites;
jbauchcb560652016-08-04 05:20:32 -070047 func(crypto_options, &crypto_suites);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080048 for (const auto crypto : crypto_suites) {
49 names->push_back(rtc::SrtpCryptoSuiteToName(crypto));
50 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080051}
terelius8c011e52016-04-26 05:28:11 -070052} // namespace
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053
54namespace cricket {
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056// RTP Profile names
57// http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
58// RFC4585
59const char kMediaProtocolAvpf[] = "RTP/AVPF";
60// RFC5124
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +000061const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
62
deadbeeff3938292015-07-15 12:20:53 -070063// We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
64// but we tolerate "RTP/SAVPF" in offers we receive, for compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065const char kMediaProtocolSavpf[] = "RTP/SAVPF";
66
67const char kMediaProtocolRtpPrefix[] = "RTP/";
68
69const char kMediaProtocolSctp[] = "SCTP";
70const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
lally@webrtc.orgec97c652015-02-24 20:18:48 +000071const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
lally@webrtc.orga7470932015-02-24 20:19:21 +000072const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073
deadbeef8b7e9ad2017-05-25 09:38:55 -070074// Note that the below functions support some protocol strings purely for
75// legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names
76// and Interoperability.
77
78static bool IsDtlsRtp(const std::string& protocol) {
79 // Most-likely values first.
80 return protocol == "UDP/TLS/RTP/SAVPF" || protocol == "TCP/TLS/RTP/SAVPF" ||
81 protocol == "UDP/TLS/RTP/SAVP" || protocol == "TCP/TLS/RTP/SAVP";
82}
83
84static bool IsPlainRtp(const std::string& protocol) {
85 // Most-likely values first.
86 return protocol == "RTP/SAVPF" || protocol == "RTP/AVPF" ||
87 protocol == "RTP/SAVP" || protocol == "RTP/AVP";
88}
89
90static bool IsDtlsSctp(const std::string& protocol) {
91 return protocol == kMediaProtocolDtlsSctp ||
92 protocol == kMediaProtocolUdpDtlsSctp ||
93 protocol == kMediaProtocolTcpDtlsSctp;
94}
95
96static bool IsPlainSctp(const std::string& protocol) {
97 return protocol == kMediaProtocolSctp;
98}
99
100static bool IsSctp(const std::string& protocol) {
101 return IsPlainSctp(protocol) || IsDtlsSctp(protocol);
102}
103
Steve Anton1d03a752017-11-27 14:30:09 -0800104static RtpTransceiverDirection NegotiateRtpTransceiverDirection(
105 RtpTransceiverDirection offer,
106 RtpTransceiverDirection wants) {
107 bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer);
108 bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer);
109 bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants);
110 bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants);
111 return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send,
112 offer_send && wants_recv);
ossu075af922016-06-14 03:29:38 -0700113}
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115static bool IsMediaContentOfType(const ContentInfo* content,
116 MediaType media_type) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800117 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 return false;
119 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800120 return content->media_description()->type() == media_type;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121}
122
123static bool CreateCryptoParams(int tag, const std::string& cipher,
124 CryptoParams *out) {
jbauchcb560652016-08-04 05:20:32 -0700125 int key_len;
126 int salt_len;
127 if (!rtc::GetSrtpKeyAndSaltLengths(
128 rtc::SrtpCryptoSuiteFromName(cipher), &key_len, &salt_len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 return false;
130 }
jbauchcb560652016-08-04 05:20:32 -0700131
132 int master_key_len = key_len + salt_len;
133 std::string master_key;
134 if (!rtc::CreateRandomData(master_key_len, &master_key)) {
135 return false;
136 }
137
kwiberg352444f2016-11-28 15:58:53 -0800138 RTC_CHECK_EQ(master_key_len, master_key.size());
jbauchcb560652016-08-04 05:20:32 -0700139 std::string key = rtc::Base64::Encode(master_key);
140
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 out->tag = tag;
142 out->cipher_suite = cipher;
143 out->key_params = kInline;
144 out->key_params += key;
145 return true;
146}
147
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148static bool AddCryptoParams(const std::string& cipher_suite,
149 CryptoParamsVec *out) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000150 int size = static_cast<int>(out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151
152 out->resize(size + 1);
153 return CreateCryptoParams(size, cipher_suite, &out->at(size));
154}
155
156void AddMediaCryptos(const CryptoParamsVec& cryptos,
157 MediaContentDescription* media) {
158 for (CryptoParamsVec::const_iterator crypto = cryptos.begin();
159 crypto != cryptos.end(); ++crypto) {
160 media->AddCrypto(*crypto);
161 }
162}
163
164bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
165 MediaContentDescription* media) {
166 CryptoParamsVec cryptos;
167 for (std::vector<std::string>::const_iterator it = crypto_suites.begin();
168 it != crypto_suites.end(); ++it) {
169 if (!AddCryptoParams(*it, &cryptos)) {
170 return false;
171 }
172 }
173 AddMediaCryptos(cryptos, media);
174 return true;
175}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176
zhihuang1c378ed2017-08-17 14:10:50 -0700177const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800178 if (!content || !content->media_description()) {
zhihuang1c378ed2017-08-17 14:10:50 -0700179 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800181 return &content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182}
183
184bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
185 const CryptoParams& crypto,
186 CryptoParams* out) {
187 for (CryptoParamsVec::const_iterator it = cryptos.begin();
188 it != cryptos.end(); ++it) {
189 if (crypto.Matches(*it)) {
190 *out = *it;
191 return true;
192 }
193 }
194 return false;
195}
196
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700197// For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the
198// low overhead.
deadbeef7914b8c2017-04-21 03:23:33 -0700199void GetSupportedAudioSdesCryptoSuites(const rtc::CryptoOptions& crypto_options,
200 std::vector<int>* crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700201 if (crypto_options.enable_gcm_crypto_suites) {
202 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
203 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
204 }
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700205 if (crypto_options.enable_aes128_sha1_32_crypto_cipher) {
206 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32);
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 GetSupportedAudioSdesCryptoSuiteNames(
212 const rtc::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(GetSupportedAudioSdesCryptoSuites,
215 crypto_options, crypto_suite_names);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216}
217
deadbeef7914b8c2017-04-21 03:23:33 -0700218void GetSupportedVideoSdesCryptoSuites(const rtc::CryptoOptions& crypto_options,
219 std::vector<int>* crypto_suites) {
jbauchcb560652016-08-04 05:20:32 -0700220 if (crypto_options.enable_gcm_crypto_suites) {
221 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
222 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
223 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800224 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225}
226
deadbeef7914b8c2017-04-21 03:23:33 -0700227void GetSupportedVideoSdesCryptoSuiteNames(
228 const rtc::CryptoOptions& crypto_options,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800229 std::vector<std::string>* crypto_suite_names) {
deadbeef7914b8c2017-04-21 03:23:33 -0700230 GetSupportedSdesCryptoSuiteNames(GetSupportedVideoSdesCryptoSuites,
231 crypto_options, crypto_suite_names);
232}
233
234void GetSupportedDataSdesCryptoSuites(const rtc::CryptoOptions& crypto_options,
235 std::vector<int>* crypto_suites) {
236 if (crypto_options.enable_gcm_crypto_suites) {
237 crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
238 crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
239 }
240 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
241}
242
243void GetSupportedDataSdesCryptoSuiteNames(
244 const rtc::CryptoOptions& crypto_options,
245 std::vector<std::string>* crypto_suite_names) {
246 GetSupportedSdesCryptoSuiteNames(GetSupportedDataSdesCryptoSuites,
247 crypto_options, crypto_suite_names);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800248}
249
jbauchcb560652016-08-04 05:20:32 -0700250// Support any GCM cipher (if enabled through options). For video support only
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700251// 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated (if enabled) unless
252// bundle is enabled because it is low overhead.
jbauchcb560652016-08-04 05:20:32 -0700253// Pick the crypto in the list that is supported.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254static bool SelectCrypto(const MediaContentDescription* offer,
255 bool bundle,
jbauchcb560652016-08-04 05:20:32 -0700256 const rtc::CryptoOptions& crypto_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 CryptoParams *crypto) {
258 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
259 const CryptoParamsVec& cryptos = offer->cryptos();
260
261 for (CryptoParamsVec::const_iterator i = cryptos.begin();
262 i != cryptos.end(); ++i) {
jbauchcb560652016-08-04 05:20:32 -0700263 if ((crypto_options.enable_gcm_crypto_suites &&
264 rtc::IsGcmCryptoSuiteName(i->cipher_suite)) ||
265 rtc::CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite ||
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700266 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio &&
Taylor Brandstetter5e55fe82018-03-23 11:50:16 -0700267 !bundle && crypto_options.enable_aes128_sha1_32_crypto_cipher)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000268 return CreateCryptoParams(i->tag, i->cipher_suite, crypto);
269 }
270 }
271 return false;
272}
273
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000274// Generate random SSRC values that are not already present in |params_vec|.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000275// The generated values are added to |ssrcs|.
276// |num_ssrcs| is the number of the SSRC will be generated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000277static void GenerateSsrcs(const StreamParamsVec& params_vec,
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000278 int num_ssrcs,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200279 std::vector<uint32_t>* ssrcs) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000280 for (int i = 0; i < num_ssrcs; i++) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200281 uint32_t candidate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000283 candidate = rtc::CreateRandomNonZeroId();
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000284 } while (GetStreamBySsrc(params_vec, candidate) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 std::count(ssrcs->begin(), ssrcs->end(), candidate) > 0);
286 ssrcs->push_back(candidate);
287 }
288}
289
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000290// Finds all StreamParams of all media types and attach them to stream_params.
291static void GetCurrentStreamParams(const SessionDescription* sdesc,
292 StreamParamsVec* stream_params) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800293 RTC_DCHECK(stream_params);
294 if (!sdesc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 return;
Steve Antonb1c1de12017-12-21 15:14:30 -0800296 }
297 for (const ContentInfo& content : sdesc->contents()) {
298 if (!content.media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299 continue;
300 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800301 for (const StreamParams& params : content.media_description()->streams()) {
302 stream_params->push_back(params);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 }
304 }
305}
306
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000307// Filters the data codecs for the data channel type.
308void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
309 // Filter RTP codec for SCTP and vice versa.
solenberg9fa49752016-10-08 13:02:44 -0700310 const char* codec_name =
311 sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName;
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000312 for (std::vector<DataCodec>::iterator iter = codecs->begin();
313 iter != codecs->end();) {
solenberg9fa49752016-10-08 13:02:44 -0700314 if (CodecNamesEq(iter->name, codec_name)) {
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000315 iter = codecs->erase(iter);
316 } else {
317 ++iter;
318 }
319 }
320}
321
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322template <typename IdStruct>
323class UsedIds {
324 public:
325 UsedIds(int min_allowed_id, int max_allowed_id)
326 : min_allowed_id_(min_allowed_id),
327 max_allowed_id_(max_allowed_id),
328 next_id_(max_allowed_id) {
329 }
330
331 // Loops through all Id in |ids| and changes its id if it is
332 // already in use by another IdStruct. Call this methods with all Id
333 // in a session description to make sure no duplicate ids exists.
334 // Note that typename Id must be a type of IdStruct.
335 template <typename Id>
336 void FindAndSetIdUsed(std::vector<Id>* ids) {
337 for (typename std::vector<Id>::iterator it = ids->begin();
338 it != ids->end(); ++it) {
339 FindAndSetIdUsed(&*it);
340 }
341 }
342
343 // Finds and sets an unused id if the |idstruct| id is already in use.
344 void FindAndSetIdUsed(IdStruct* idstruct) {
345 const int original_id = idstruct->id;
346 int new_id = idstruct->id;
347
348 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
349 // If the original id is not in range - this is an id that can't be
350 // dynamically changed.
351 return;
352 }
353
354 if (IsIdUsed(original_id)) {
355 new_id = FindUnusedId();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100356 RTC_LOG(LS_WARNING) << "Duplicate id found. Reassigning from "
357 << original_id << " to " << new_id;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 idstruct->id = new_id;
359 }
360 SetIdUsed(new_id);
361 }
362
363 private:
364 // Returns the first unused id in reverse order.
365 // This hopefully reduce the risk of more collisions. We want to change the
366 // default ids as little as possible.
367 int FindUnusedId() {
368 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
369 --next_id_;
370 }
nisseede5da42017-01-12 05:15:36 -0800371 RTC_DCHECK(next_id_ >= min_allowed_id_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 return next_id_;
373 }
374
375 bool IsIdUsed(int new_id) {
376 return id_set_.find(new_id) != id_set_.end();
377 }
378
379 void SetIdUsed(int new_id) {
380 id_set_.insert(new_id);
381 }
382
383 const int min_allowed_id_;
384 const int max_allowed_id_;
385 int next_id_;
386 std::set<int> id_set_;
387};
388
389// Helper class used for finding duplicate RTP payload types among audio, video
390// and data codecs. When bundle is used the payload types may not collide.
391class UsedPayloadTypes : public UsedIds<Codec> {
392 public:
393 UsedPayloadTypes()
394 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {
395 }
396
397
398 private:
399 static const int kDynamicPayloadTypeMin = 96;
400 static const int kDynamicPayloadTypeMax = 127;
401};
402
403// Helper class used for finding duplicate RTP Header extension ids among
404// audio and video extensions.
isheriff6f8d6862016-05-26 11:24:55 -0700405class UsedRtpHeaderExtensionIds : public UsedIds<webrtc::RtpExtension> {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 public:
407 UsedRtpHeaderExtensionIds()
deadbeefe814a0d2017-02-25 18:15:09 -0800408 : UsedIds<webrtc::RtpExtension>(webrtc::RtpExtension::kMinId,
409 webrtc::RtpExtension::kMaxId) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410
411 private:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412};
413
zhihuang1c378ed2017-08-17 14:10:50 -0700414// Adds a StreamParams for each SenderOptions in |sender_options| to
415// content_description.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416// |current_params| - All currently known StreamParams of any media type.
417template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700418static bool AddStreamParams(
419 const std::vector<SenderOptions>& sender_options,
420 const std::string& rtcp_cname,
421 StreamParamsVec* current_streams,
422 MediaContentDescriptionImpl<C>* content_description) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700423 // SCTP streams are not negotiated using SDP/ContentDescriptions.
deadbeef8b7e9ad2017-05-25 09:38:55 -0700424 if (IsSctp(content_description->protocol())) {
Taylor Brandstetter1d7a6372016-08-24 13:15:27 -0700425 return true;
426 }
427
Noah Richards2e7a0982015-05-18 14:02:54 -0700428 const bool include_rtx_streams =
429 ContainsRtxCodec(content_description->codecs());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431
brandtr03d5fb12016-11-22 03:37:59 -0800432 const bool include_flexfec_stream =
433 ContainsFlexfecCodec(content_description->codecs());
434
zhihuang1c378ed2017-08-17 14:10:50 -0700435 for (const SenderOptions& sender : sender_options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 // groupid is empty for StreamParams generated using
437 // MediaSessionDescriptionFactory.
zhihuang1c378ed2017-08-17 14:10:50 -0700438 StreamParams* param =
439 GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000440 if (!param) {
zhihuang1c378ed2017-08-17 14:10:50 -0700441 // This is a new sender.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200442 std::vector<uint32_t> ssrcs;
zhihuang1c378ed2017-08-17 14:10:50 -0700443 GenerateSsrcs(*current_streams, sender.num_sim_layers, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 StreamParams stream_param;
zhihuang1c378ed2017-08-17 14:10:50 -0700445 stream_param.id = sender.track_id;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000446 // Add the generated ssrc.
447 for (size_t i = 0; i < ssrcs.size(); ++i) {
448 stream_param.ssrcs.push_back(ssrcs[i]);
449 }
zhihuang1c378ed2017-08-17 14:10:50 -0700450 if (sender.num_sim_layers > 1) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000451 SsrcGroup group(kSimSsrcGroupSemantics, stream_param.ssrcs);
452 stream_param.ssrc_groups.push_back(group);
453 }
Noah Richards2e7a0982015-05-18 14:02:54 -0700454 // Generate extra ssrcs for include_rtx_streams case.
455 if (include_rtx_streams) {
456 // Generate an RTX ssrc for every ssrc in the group.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200457 std::vector<uint32_t> rtx_ssrcs;
Noah Richards2e7a0982015-05-18 14:02:54 -0700458 GenerateSsrcs(*current_streams, static_cast<int>(ssrcs.size()),
459 &rtx_ssrcs);
460 for (size_t i = 0; i < ssrcs.size(); ++i) {
461 stream_param.AddFidSsrc(ssrcs[i], rtx_ssrcs[i]);
462 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 }
brandtr03d5fb12016-11-22 03:37:59 -0800464 // Generate extra ssrc for include_flexfec_stream case.
465 if (include_flexfec_stream) {
466 // TODO(brandtr): Update when we support multistream protection.
467 if (ssrcs.size() == 1) {
468 std::vector<uint32_t> flexfec_ssrcs;
469 GenerateSsrcs(*current_streams, 1, &flexfec_ssrcs);
470 stream_param.AddFecFrSsrc(ssrcs[0], flexfec_ssrcs[0]);
brandtr03d5fb12016-11-22 03:37:59 -0800471 } else if (!ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100472 RTC_LOG(LS_WARNING)
brandtr03d5fb12016-11-22 03:37:59 -0800473 << "Our FlexFEC implementation only supports protecting "
Jonas Olsson45cc8902018-02-13 10:37:07 +0100474 "a single media streams. This session has multiple "
475 "media streams however, so no FlexFEC SSRC will be generated.";
brandtr03d5fb12016-11-22 03:37:59 -0800476 }
477 }
zhihuang1c378ed2017-08-17 14:10:50 -0700478 stream_param.cname = rtcp_cname;
Seth Hampson845e8782018-03-02 11:34:10 -0800479 stream_param.set_stream_ids(sender.stream_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 content_description->AddStream(stream_param);
481
482 // Store the new StreamParams in current_streams.
483 // This is necessary so that we can use the CNAME for other media types.
484 current_streams->push_back(stream_param);
485 } else {
deadbeef2f425aa2017-04-14 10:41:32 -0700486 // Use existing generated SSRCs/groups, but update the sync_label if
487 // necessary. This may be needed if a MediaStreamTrack was moved from one
488 // MediaStream to another.
Seth Hampson845e8782018-03-02 11:34:10 -0800489 param->set_stream_ids(sender.stream_ids);
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000490 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 }
492 }
493 return true;
494}
495
496// Updates the transport infos of the |sdesc| according to the given
497// |bundle_group|. The transport infos of the content names within the
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800498// |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
499// first content within the |bundle_group|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
501 SessionDescription* sdesc) {
502 // The bundle should not be empty.
503 if (!sdesc || !bundle_group.FirstContentName()) {
504 return false;
505 }
506
507 // We should definitely have a transport for the first content.
jbauch083b73f2015-07-16 02:46:32 -0700508 const std::string& selected_content_name = *bundle_group.FirstContentName();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 const TransportInfo* selected_transport_info =
510 sdesc->GetTransportInfoByName(selected_content_name);
511 if (!selected_transport_info) {
512 return false;
513 }
514
515 // Set the other contents to use the same ICE credentials.
jbauch083b73f2015-07-16 02:46:32 -0700516 const std::string& selected_ufrag =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 selected_transport_info->description.ice_ufrag;
jbauch083b73f2015-07-16 02:46:32 -0700518 const std::string& selected_pwd =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 selected_transport_info->description.ice_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800520 ConnectionRole selected_connection_role =
521 selected_transport_info->description.connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 for (TransportInfos::iterator it =
523 sdesc->transport_infos().begin();
524 it != sdesc->transport_infos().end(); ++it) {
525 if (bundle_group.HasContentName(it->content_name) &&
526 it->content_name != selected_content_name) {
527 it->description.ice_ufrag = selected_ufrag;
528 it->description.ice_pwd = selected_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800529 it->description.connection_role = selected_connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 }
531 }
532 return true;
533}
534
535// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
536// sets it to |cryptos|.
537static bool GetCryptosByName(const SessionDescription* sdesc,
538 const std::string& content_name,
539 CryptoParamsVec* cryptos) {
540 if (!sdesc || !cryptos) {
541 return false;
542 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 const ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800544 if (!content || !content->media_description()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 return false;
546 }
Steve Antonb1c1de12017-12-21 15:14:30 -0800547 *cryptos = content->media_description()->cryptos();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 return true;
549}
550
551// Predicate function used by the remove_if.
552// Returns true if the |crypto|'s cipher_suite is not found in |filter|.
553static bool CryptoNotFound(const CryptoParams crypto,
554 const CryptoParamsVec* filter) {
555 if (filter == NULL) {
556 return true;
557 }
558 for (CryptoParamsVec::const_iterator it = filter->begin();
559 it != filter->end(); ++it) {
560 if (it->cipher_suite == crypto.cipher_suite) {
561 return false;
562 }
563 }
564 return true;
565}
566
567// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
568// which are not available in |filter|.
569static void PruneCryptos(const CryptoParamsVec& filter,
570 CryptoParamsVec* target_cryptos) {
571 if (!target_cryptos) {
572 return;
573 }
574 target_cryptos->erase(std::remove_if(target_cryptos->begin(),
575 target_cryptos->end(),
576 bind2nd(ptr_fun(CryptoNotFound),
577 &filter)),
578 target_cryptos->end());
579}
580
Steve Antonfa2260d2017-12-28 16:38:23 -0800581bool IsRtpProtocol(const std::string& protocol) {
deadbeefb5cb19b2015-11-23 16:39:12 -0800582 return protocol.empty() ||
583 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
584}
585
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586static bool IsRtpContent(SessionDescription* sdesc,
587 const std::string& content_name) {
588 bool is_rtp = false;
589 ContentInfo* content = sdesc->GetContentByName(content_name);
Steve Antonb1c1de12017-12-21 15:14:30 -0800590 if (content && content->media_description()) {
591 is_rtp = IsRtpProtocol(content->media_description()->protocol());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 }
593 return is_rtp;
594}
595
596// Updates the crypto parameters of the |sdesc| according to the given
597// |bundle_group|. The crypto parameters of all the contents within the
598// |bundle_group| should be updated to use the common subset of the
599// available cryptos.
600static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
601 SessionDescription* sdesc) {
602 // The bundle should not be empty.
603 if (!sdesc || !bundle_group.FirstContentName()) {
604 return false;
605 }
606
wu@webrtc.org78187522013-10-07 23:32:02 +0000607 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 // Get the common cryptos.
609 const ContentNames& content_names = bundle_group.content_names();
610 CryptoParamsVec common_cryptos;
611 for (ContentNames::const_iterator it = content_names.begin();
612 it != content_names.end(); ++it) {
613 if (!IsRtpContent(sdesc, *it)) {
614 continue;
615 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000616 // The common cryptos are needed if any of the content does not have DTLS
617 // enabled.
618 if (!sdesc->GetTransportInfoByName(*it)->description.secure()) {
619 common_cryptos_needed = true;
620 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 if (it == content_names.begin()) {
622 // Initial the common_cryptos with the first content in the bundle group.
623 if (!GetCryptosByName(sdesc, *it, &common_cryptos)) {
624 return false;
625 }
626 if (common_cryptos.empty()) {
627 // If there's no crypto params, we should just return.
628 return true;
629 }
630 } else {
631 CryptoParamsVec cryptos;
632 if (!GetCryptosByName(sdesc, *it, &cryptos)) {
633 return false;
634 }
635 PruneCryptos(cryptos, &common_cryptos);
636 }
637 }
638
wu@webrtc.org78187522013-10-07 23:32:02 +0000639 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 return false;
641 }
642
643 // Update to use the common cryptos.
644 for (ContentNames::const_iterator it = content_names.begin();
645 it != content_names.end(); ++it) {
646 if (!IsRtpContent(sdesc, *it)) {
647 continue;
648 }
649 ContentInfo* content = sdesc->GetContentByName(*it);
650 if (IsMediaContent(content)) {
Steve Antonb1c1de12017-12-21 15:14:30 -0800651 MediaContentDescription* media_desc = content->media_description();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 if (!media_desc) {
653 return false;
654 }
655 media_desc->set_cryptos(common_cryptos);
656 }
657 }
658 return true;
659}
660
661template <class C>
662static bool ContainsRtxCodec(const std::vector<C>& codecs) {
brandtr03d5fb12016-11-22 03:37:59 -0800663 for (const auto& codec : codecs) {
664 if (IsRtxCodec(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 return true;
666 }
667 }
668 return false;
669}
670
671template <class C>
672static bool IsRtxCodec(const C& codec) {
nisse21e4e0b2017-02-20 05:01:01 -0800673 return STR_CASE_CMP(codec.name.c_str(), kRtxCodecName) == 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674}
675
brandtr03d5fb12016-11-22 03:37:59 -0800676template <class C>
677static bool ContainsFlexfecCodec(const std::vector<C>& codecs) {
678 for (const auto& codec : codecs) {
679 if (IsFlexfecCodec(codec)) {
680 return true;
681 }
682 }
683 return false;
684}
685
686template <class C>
687static bool IsFlexfecCodec(const C& codec) {
nisse21e4e0b2017-02-20 05:01:01 -0800688 return STR_CASE_CMP(codec.name.c_str(), kFlexfecCodecName) == 0;
brandtr03d5fb12016-11-22 03:37:59 -0800689}
690
zhihuang1c378ed2017-08-17 14:10:50 -0700691// Create a media content to be offered for the given |sender_options|,
692// according to the given options.rtcp_mux, session_options.is_muc, codecs,
693// secure_transport, crypto, and current_streams. If we don't currently have
694// crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is
695// created (according to crypto_suites). The created content is added to the
696// offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697template <class C>
698static bool CreateMediaContentOffer(
zhihuang1c378ed2017-08-17 14:10:50 -0700699 const std::vector<SenderOptions>& sender_options,
700 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000702 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 const CryptoParamsVec* current_cryptos,
704 const std::vector<std::string>& crypto_suites,
705 const RtpHeaderExtensions& rtp_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 StreamParamsVec* current_streams,
707 MediaContentDescriptionImpl<C>* offer) {
708 offer->AddCodecs(codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709
zhihuang1c378ed2017-08-17 14:10:50 -0700710 offer->set_rtcp_mux(session_options.rtcp_mux_enabled);
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700711 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
712 offer->set_rtcp_reduced_size(true);
713 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 offer->set_rtp_header_extensions(rtp_extensions);
715
zhihuang1c378ed2017-08-17 14:10:50 -0700716 if (!AddStreamParams(sender_options, session_options.rtcp_cname,
717 current_streams, offer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 return false;
719 }
720
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 if (secure_policy != SEC_DISABLED) {
722 if (current_cryptos) {
723 AddMediaCryptos(*current_cryptos, offer);
724 }
725 if (offer->cryptos().empty()) {
726 if (!CreateMediaCryptos(crypto_suites, offer)) {
727 return false;
728 }
729 }
730 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731
deadbeef7af91dd2016-12-13 11:29:11 -0800732 if (secure_policy == SEC_REQUIRED && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 return false;
734 }
735 return true;
736}
737
738template <class C>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000739static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
magjedb05fa242016-11-11 04:00:16 -0800740 const int codec1_id,
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000741 const std::vector<C>& codecs2,
magjedb05fa242016-11-11 04:00:16 -0800742 const int codec2_id) {
743 const C* codec1 = FindCodecById(codecs1, codec1_id);
744 const C* codec2 = FindCodecById(codecs2, codec2_id);
745 return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2);
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000746}
747
748template <class C>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749static void NegotiateCodecs(const std::vector<C>& local_codecs,
750 const std::vector<C>& offered_codecs,
751 std::vector<C>* negotiated_codecs) {
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800752 for (const C& ours : local_codecs) {
753 C theirs;
deadbeef67cf2c12016-04-13 10:07:16 -0700754 // Note that we intentionally only find one matching codec for each of our
755 // local codecs, in case the remote offer contains duplicate codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800756 if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) {
757 C negotiated = ours;
758 negotiated.IntersectFeedbackParams(theirs);
759 if (IsRtxCodec(negotiated)) {
magjedb05fa242016-11-11 04:00:16 -0800760 const auto apt_it =
761 theirs.params.find(kCodecParamAssociatedPayloadType);
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800762 // FindMatchingCodec shouldn't return something with no apt value.
magjedb05fa242016-11-11 04:00:16 -0800763 RTC_DCHECK(apt_it != theirs.params.end());
764 negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 }
magjedf823ede2016-11-12 09:53:04 -0800766 if (CodecNamesEq(ours.name.c_str(), kH264CodecName)) {
767 webrtc::H264::GenerateProfileLevelIdForAnswer(
768 ours.params, theirs.params, &negotiated.params);
769 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800770 negotiated.id = theirs.id;
ossu075af922016-06-14 03:29:38 -0700771 negotiated.name = theirs.name;
magjedb05fa242016-11-11 04:00:16 -0800772 negotiated_codecs->push_back(std::move(negotiated));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 }
774 }
deadbeef67cf2c12016-04-13 10:07:16 -0700775 // RFC3264: Although the answerer MAY list the formats in their desired
776 // order of preference, it is RECOMMENDED that unless there is a
777 // specific reason, the answerer list formats in the same relative order
778 // they were present in the offer.
779 std::unordered_map<int, int> payload_type_preferences;
780 int preference = static_cast<int>(offered_codecs.size() + 1);
781 for (const C& codec : offered_codecs) {
782 payload_type_preferences[codec.id] = preference--;
783 }
784 std::sort(negotiated_codecs->begin(), negotiated_codecs->end(),
785 [&payload_type_preferences](const C& a, const C& b) {
786 return payload_type_preferences[a.id] >
787 payload_type_preferences[b.id];
788 });
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789}
790
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800791// Finds a codec in |codecs2| that matches |codec_to_match|, which is
792// a member of |codecs1|. If |codec_to_match| is an RTX codec, both
793// the codecs themselves and their associated codecs must match.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794template <class C>
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800795static bool FindMatchingCodec(const std::vector<C>& codecs1,
796 const std::vector<C>& codecs2,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 const C& codec_to_match,
798 C* found_codec) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -0700799 // |codec_to_match| should be a member of |codecs1|, in order to look up RTX
800 // codecs' associated codecs correctly. If not, that's a programming error.
801 RTC_DCHECK(std::find_if(codecs1.begin(), codecs1.end(),
802 [&codec_to_match](const C& codec) {
803 return &codec == &codec_to_match;
804 }) != codecs1.end());
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800805 for (const C& potential_match : codecs2) {
806 if (potential_match.Matches(codec_to_match)) {
807 if (IsRtxCodec(codec_to_match)) {
magjedb05fa242016-11-11 04:00:16 -0800808 int apt_value_1 = 0;
809 int apt_value_2 = 0;
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800810 if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType,
811 &apt_value_1) ||
812 !potential_match.GetParam(kCodecParamAssociatedPayloadType,
813 &apt_value_2)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100814 RTC_LOG(LS_WARNING) << "RTX missing associated payload type.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800815 continue;
816 }
817 if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2,
818 apt_value_2)) {
819 continue;
820 }
821 }
822 if (found_codec) {
823 *found_codec = potential_match;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 }
825 return true;
826 }
827 }
828 return false;
829}
830
zhihuang1c378ed2017-08-17 14:10:50 -0700831// Find the codec in |codec_list| that |rtx_codec| is associated with.
832template <class C>
833static const C* GetAssociatedCodec(const std::vector<C>& codec_list,
834 const C& rtx_codec) {
835 std::string associated_pt_str;
836 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
837 &associated_pt_str)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100838 RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
839 << " is missing an associated payload type.";
zhihuang1c378ed2017-08-17 14:10:50 -0700840 return nullptr;
841 }
842
843 int associated_pt;
844 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100845 RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
846 << " of RTX codec " << rtx_codec.name
847 << " to an integer.";
zhihuang1c378ed2017-08-17 14:10:50 -0700848 return nullptr;
849 }
850
851 // Find the associated reference codec for the reference RTX codec.
852 const C* associated_codec = FindCodecById(codec_list, associated_pt);
853 if (!associated_codec) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100854 RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
855 << associated_pt << " for RTX codec " << rtx_codec.name
856 << ".";
zhihuang1c378ed2017-08-17 14:10:50 -0700857 }
858 return associated_codec;
859}
860
861// Adds all codecs from |reference_codecs| to |offered_codecs| that don't
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862// already exist in |offered_codecs| and ensure the payload types don't
863// collide.
864template <class C>
zhihuang1c378ed2017-08-17 14:10:50 -0700865static void MergeCodecs(const std::vector<C>& reference_codecs,
866 std::vector<C>* offered_codecs,
867 UsedPayloadTypes* used_pltypes) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 // Add all new codecs that are not RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800869 for (const C& reference_codec : reference_codecs) {
870 if (!IsRtxCodec(reference_codec) &&
871 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
872 reference_codec, nullptr)) {
873 C codec = reference_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 used_pltypes->FindAndSetIdUsed(&codec);
875 offered_codecs->push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000876 }
877 }
878
879 // Add all new RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800880 for (const C& reference_codec : reference_codecs) {
881 if (IsRtxCodec(reference_codec) &&
882 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
883 reference_codec, nullptr)) {
884 C rtx_codec = reference_codec;
olka3c747662017-08-17 06:50:32 -0700885 const C* associated_codec =
zhihuang1c378ed2017-08-17 14:10:50 -0700886 GetAssociatedCodec(reference_codecs, rtx_codec);
olka3c747662017-08-17 06:50:32 -0700887 if (!associated_codec) {
olka3c747662017-08-17 06:50:32 -0700888 continue;
889 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800890 // Find a codec in the offered list that matches the reference codec.
891 // Its payload type may be different than the reference codec.
892 C matching_codec;
893 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
magjedb05fa242016-11-11 04:00:16 -0800894 *associated_codec, &matching_codec)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100895 RTC_LOG(LS_WARNING)
896 << "Couldn't find matching " << associated_codec->name << " codec.";
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800897 continue;
898 }
899
900 rtx_codec.params[kCodecParamAssociatedPayloadType] =
901 rtc::ToString(matching_codec.id);
902 used_pltypes->FindAndSetIdUsed(&rtx_codec);
903 offered_codecs->push_back(rtx_codec);
904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 }
906}
907
zhihuang1c378ed2017-08-17 14:10:50 -0700908static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
909 const webrtc::RtpExtension& ext_to_match,
910 webrtc::RtpExtension* found_extension) {
911 for (RtpHeaderExtensions::const_iterator it = extensions.begin();
912 it != extensions.end(); ++it) {
913 // We assume that all URIs are given in a canonical format.
914 if (it->uri == ext_to_match.uri && it->encrypt == ext_to_match.encrypt) {
915 if (found_extension) {
916 *found_extension = *it;
917 }
918 return true;
919 }
920 }
921 return false;
922}
923
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924static bool FindByUri(const RtpHeaderExtensions& extensions,
isheriff6f8d6862016-05-26 11:24:55 -0700925 const webrtc::RtpExtension& ext_to_match,
926 webrtc::RtpExtension* found_extension) {
jbauch5869f502017-06-29 12:31:36 -0700927 // We assume that all URIs are given in a canonical format.
928 const webrtc::RtpExtension* found =
929 webrtc::RtpExtension::FindHeaderExtensionByUri(extensions,
930 ext_to_match.uri);
931 if (!found) {
932 return false;
933 }
934 if (found_extension) {
935 *found_extension = *found;
936 }
937 return true;
938}
939
940static bool FindByUriWithEncryptionPreference(
941 const RtpHeaderExtensions& extensions,
942 const webrtc::RtpExtension& ext_to_match, bool encryption_preference,
943 webrtc::RtpExtension* found_extension) {
944 const webrtc::RtpExtension* unencrypted_extension = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 for (RtpHeaderExtensions::const_iterator it = extensions.begin();
946 it != extensions.end(); ++it) {
947 // We assume that all URIs are given in a canonical format.
948 if (it->uri == ext_to_match.uri) {
jbauch5869f502017-06-29 12:31:36 -0700949 if (!encryption_preference || it->encrypt) {
950 if (found_extension) {
951 *found_extension = *it;
952 }
953 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 }
jbauch5869f502017-06-29 12:31:36 -0700955 unencrypted_extension = &(*it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 }
957 }
jbauch5869f502017-06-29 12:31:36 -0700958 if (unencrypted_extension) {
959 if (found_extension) {
960 *found_extension = *unencrypted_extension;
961 }
962 return true;
963 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 return false;
965}
966
zhihuang1c378ed2017-08-17 14:10:50 -0700967// Adds all extensions from |reference_extensions| to |offered_extensions| that
968// don't already exist in |offered_extensions| and ensure the IDs don't
969// collide. If an extension is added, it's also added to |regular_extensions| or
970// |encrypted_extensions|, and if the extension is in |regular_extensions| or
971// |encrypted_extensions|, its ID is marked as used in |used_ids|.
972// |offered_extensions| is for either audio or video while |regular_extensions|
973// and |encrypted_extensions| are used for both audio and video. There could be
974// overlap between audio extensions and video extensions.
975static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions,
976 RtpHeaderExtensions* offered_extensions,
977 RtpHeaderExtensions* regular_extensions,
978 RtpHeaderExtensions* encrypted_extensions,
979 UsedRtpHeaderExtensionIds* used_ids) {
olka3c747662017-08-17 06:50:32 -0700980 for (auto reference_extension : reference_extensions) {
zhihuang1c378ed2017-08-17 14:10:50 -0700981 if (!FindByUriAndEncryption(*offered_extensions, reference_extension,
982 nullptr)) {
olka3c747662017-08-17 06:50:32 -0700983 webrtc::RtpExtension existing;
zhihuang1c378ed2017-08-17 14:10:50 -0700984 if (reference_extension.encrypt) {
985 if (FindByUriAndEncryption(*encrypted_extensions, reference_extension,
986 &existing)) {
987 offered_extensions->push_back(existing);
988 } else {
989 used_ids->FindAndSetIdUsed(&reference_extension);
990 encrypted_extensions->push_back(reference_extension);
991 offered_extensions->push_back(reference_extension);
992 }
olka3c747662017-08-17 06:50:32 -0700993 } else {
zhihuang1c378ed2017-08-17 14:10:50 -0700994 if (FindByUriAndEncryption(*regular_extensions, reference_extension,
995 &existing)) {
996 offered_extensions->push_back(existing);
997 } else {
998 used_ids->FindAndSetIdUsed(&reference_extension);
999 regular_extensions->push_back(reference_extension);
1000 offered_extensions->push_back(reference_extension);
1001 }
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 }
1004 }
1005}
1006
jbauch5869f502017-06-29 12:31:36 -07001007static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions,
1008 RtpHeaderExtensions* all_extensions,
1009 UsedRtpHeaderExtensionIds* used_ids) {
1010 RtpHeaderExtensions encrypted_extensions;
1011 for (const webrtc::RtpExtension& extension : *extensions) {
1012 webrtc::RtpExtension existing;
1013 // Don't add encrypted extensions again that were already included in a
1014 // previous offer or regular extensions that are also included as encrypted
1015 // extensions.
1016 if (extension.encrypt ||
1017 !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) ||
1018 (FindByUriWithEncryptionPreference(*extensions, extension, true,
1019 &existing) && existing.encrypt)) {
1020 continue;
1021 }
1022
1023 if (FindByUri(*all_extensions, extension, &existing)) {
1024 encrypted_extensions.push_back(existing);
1025 } else {
1026 webrtc::RtpExtension encrypted(extension);
1027 encrypted.encrypt = true;
1028 used_ids->FindAndSetIdUsed(&encrypted);
1029 all_extensions->push_back(encrypted);
1030 encrypted_extensions.push_back(encrypted);
1031 }
1032 }
1033 extensions->insert(extensions->end(), encrypted_extensions.begin(),
1034 encrypted_extensions.end());
1035}
1036
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037static void NegotiateRtpHeaderExtensions(
1038 const RtpHeaderExtensions& local_extensions,
1039 const RtpHeaderExtensions& offered_extensions,
jbauch5869f502017-06-29 12:31:36 -07001040 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 RtpHeaderExtensions* negotiated_extenstions) {
1042 RtpHeaderExtensions::const_iterator ours;
1043 for (ours = local_extensions.begin();
1044 ours != local_extensions.end(); ++ours) {
isheriff6f8d6862016-05-26 11:24:55 -07001045 webrtc::RtpExtension theirs;
jbauch5869f502017-06-29 12:31:36 -07001046 if (FindByUriWithEncryptionPreference(offered_extensions, *ours,
1047 enable_encrypted_rtp_header_extensions, &theirs)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 // We respond with their RTP header extension id.
1049 negotiated_extenstions->push_back(theirs);
1050 }
1051 }
1052}
1053
1054static void StripCNCodecs(AudioCodecs* audio_codecs) {
1055 AudioCodecs::iterator iter = audio_codecs->begin();
1056 while (iter != audio_codecs->end()) {
nisse21e4e0b2017-02-20 05:01:01 -08001057 if (STR_CASE_CMP(iter->name.c_str(), kComfortNoiseCodecName) == 0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 iter = audio_codecs->erase(iter);
1059 } else {
1060 ++iter;
1061 }
1062 }
1063}
1064
zhihuang1c378ed2017-08-17 14:10:50 -07001065// Create a media content to be answered for the given |sender_options|
1066// according to the given session_options.rtcp_mux, session_options.streams,
1067// codecs, crypto, and current_streams. If we don't currently have crypto (in
1068// current_cryptos) and it is enabled (in secure_policy), crypto is created
1069// (according to crypto_suites). The codecs, rtcp_mux, and crypto are all
1070// negotiated with the offer. If the negotiation fails, this method returns
1071// false. The created content is added to the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072template <class C>
1073static bool CreateMediaContentAnswer(
1074 const MediaContentDescriptionImpl<C>* offer,
zhihuang1c378ed2017-08-17 14:10:50 -07001075 const MediaDescriptionOptions& media_description_options,
1076 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001078 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 const CryptoParamsVec* current_cryptos,
1080 const RtpHeaderExtensions& local_rtp_extenstions,
jbauch5869f502017-06-29 12:31:36 -07001081 bool enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 StreamParamsVec* current_streams,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 bool bundle_enabled,
1084 MediaContentDescriptionImpl<C>* answer) {
1085 std::vector<C> negotiated_codecs;
1086 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
1087 answer->AddCodecs(negotiated_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 answer->set_protocol(offer->protocol());
1089 RtpHeaderExtensions negotiated_rtp_extensions;
1090 NegotiateRtpHeaderExtensions(local_rtp_extenstions,
1091 offer->rtp_header_extensions(),
jbauch5869f502017-06-29 12:31:36 -07001092 enable_encrypted_rtp_header_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 &negotiated_rtp_extensions);
1094 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
1095
zhihuang1c378ed2017-08-17 14:10:50 -07001096 answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux());
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -07001097 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
1098 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
1099 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100
1101 if (sdes_policy != SEC_DISABLED) {
1102 CryptoParams crypto;
zhihuang1c378ed2017-08-17 14:10:50 -07001103 if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options,
1104 &crypto)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 if (current_cryptos) {
1106 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
1107 }
1108 answer->AddCrypto(crypto);
1109 }
1110 }
1111
deadbeef7af91dd2016-12-13 11:29:11 -08001112 if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 return false;
1114 }
1115
zhihuang1c378ed2017-08-17 14:10:50 -07001116 if (!AddStreamParams(media_description_options.sender_options,
1117 session_options.rtcp_cname, current_streams, answer)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 return false; // Something went seriously wrong.
1119 }
1120
Steve Anton4e70a722017-11-28 14:57:10 -08001121 answer->set_direction(NegotiateRtpTransceiverDirection(
1122 offer->direction(), media_description_options.direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 return true;
1124}
1125
1126static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001127 const std::string& protocol,
1128 bool secure_transport) {
zhihuangcf5b37c2016-05-05 11:44:35 -07001129 // Since not all applications serialize and deserialize the media protocol,
1130 // we will have to accept |protocol| to be empty.
1131 if (protocol.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 return true;
1133 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001134
zhihuangcf5b37c2016-05-05 11:44:35 -07001135 if (type == MEDIA_TYPE_DATA) {
1136 // Check for SCTP, but also for RTP for RTP-based data channels.
1137 // TODO(pthatcher): Remove RTP once RTP-based data channels are gone.
1138 if (secure_transport) {
1139 // Most likely scenarios first.
1140 return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) ||
1141 IsPlainRtp(protocol);
1142 } else {
1143 return IsPlainSctp(protocol) || IsPlainRtp(protocol);
1144 }
1145 }
1146
1147 // Allow for non-DTLS RTP protocol even when using DTLS because that's what
1148 // JSEP specifies.
1149 if (secure_transport) {
1150 // Most likely scenarios first.
1151 return IsDtlsRtp(protocol) || IsPlainRtp(protocol);
1152 } else {
1153 return IsPlainRtp(protocol);
1154 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155}
1156
1157static void SetMediaProtocol(bool secure_transport,
1158 MediaContentDescription* desc) {
deadbeeff3938292015-07-15 12:20:53 -07001159 if (!desc->cryptos().empty())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001160 desc->set_protocol(kMediaProtocolSavpf);
deadbeeff3938292015-07-15 12:20:53 -07001161 else if (secure_transport)
1162 desc->set_protocol(kMediaProtocolDtlsSavpf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 else
1164 desc->set_protocol(kMediaProtocolAvpf);
1165}
1166
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001167// Gets the TransportInfo of the given |content_name| from the
1168// |current_description|. If doesn't exist, returns a new one.
1169static const TransportDescription* GetTransportDescription(
1170 const std::string& content_name,
1171 const SessionDescription* current_description) {
1172 const TransportDescription* desc = NULL;
1173 if (current_description) {
1174 const TransportInfo* info =
1175 current_description->GetTransportInfoByName(content_name);
1176 if (info) {
1177 desc = &info->description;
1178 }
1179 }
1180 return desc;
1181}
1182
1183// Gets the current DTLS state from the transport description.
zhihuang1c378ed2017-08-17 14:10:50 -07001184static bool IsDtlsActive(const ContentInfo* content,
1185 const SessionDescription* current_description) {
1186 if (!content) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001187 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001188 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001189
zhihuang1c378ed2017-08-17 14:10:50 -07001190 size_t msection_index = content - &current_description->contents()[0];
1191
1192 if (current_description->transport_infos().size() <= msection_index) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001193 return false;
zhihuang1c378ed2017-08-17 14:10:50 -07001194 }
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001195
zhihuang1c378ed2017-08-17 14:10:50 -07001196 return current_description->transport_infos()[msection_index]
1197 .description.secure();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001198}
1199
Steve Anton8ffb9c32017-08-31 15:45:38 -07001200void MediaDescriptionOptions::AddAudioSender(
1201 const std::string& track_id,
1202 const std::vector<std::string>& stream_ids) {
zhihuang1c378ed2017-08-17 14:10:50 -07001203 RTC_DCHECK(type == MEDIA_TYPE_AUDIO);
Steve Anton8ffb9c32017-08-31 15:45:38 -07001204 AddSenderInternal(track_id, stream_ids, 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001205}
1206
Steve Anton8ffb9c32017-08-31 15:45:38 -07001207void MediaDescriptionOptions::AddVideoSender(
1208 const std::string& track_id,
1209 const std::vector<std::string>& stream_ids,
1210 int num_sim_layers) {
zhihuang1c378ed2017-08-17 14:10:50 -07001211 RTC_DCHECK(type == MEDIA_TYPE_VIDEO);
Steve Anton8ffb9c32017-08-31 15:45:38 -07001212 AddSenderInternal(track_id, stream_ids, num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001213}
1214
zhihuang1c378ed2017-08-17 14:10:50 -07001215void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id,
1216 const std::string& stream_id) {
1217 RTC_DCHECK(type == MEDIA_TYPE_DATA);
Steve Anton8ffb9c32017-08-31 15:45:38 -07001218 // TODO(steveanton): Is it the case that RtpDataChannel will never have more
1219 // than one stream?
1220 AddSenderInternal(track_id, {stream_id}, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221}
1222
Steve Anton8ffb9c32017-08-31 15:45:38 -07001223void MediaDescriptionOptions::AddSenderInternal(
1224 const std::string& track_id,
1225 const std::vector<std::string>& stream_ids,
1226 int num_sim_layers) {
1227 // TODO(steveanton): Support any number of stream ids.
1228 RTC_CHECK(stream_ids.size() == 1U);
1229 sender_options.push_back(SenderOptions{track_id, stream_ids, num_sim_layers});
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230}
1231
zhihuang1c378ed2017-08-17 14:10:50 -07001232bool MediaSessionOptions::HasMediaDescription(MediaType type) const {
1233 return std::find_if(media_description_options.begin(),
1234 media_description_options.end(),
1235 [type](const MediaDescriptionOptions& t) {
1236 return t.type == type;
1237 }) != media_description_options.end();
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001238}
1239
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1241 const TransportDescriptionFactory* transport_desc_factory)
zhihuang1c378ed2017-08-17 14:10:50 -07001242 : transport_desc_factory_(transport_desc_factory) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243
1244MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1245 ChannelManager* channel_manager,
1246 const TransportDescriptionFactory* transport_desc_factory)
zhihuang1c378ed2017-08-17 14:10:50 -07001247 : transport_desc_factory_(transport_desc_factory) {
ossudedfd282016-06-14 07:12:39 -07001248 channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_);
1249 channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
magjed3cf8ece2016-11-10 03:36:53 -08001251 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1253 channel_manager->GetSupportedDataCodecs(&data_codecs_);
zhihuang1c378ed2017-08-17 14:10:50 -07001254 ComputeAudioCodecsIntersectionAndUnion();
ossu075af922016-06-14 03:29:38 -07001255}
1256
ossudedfd282016-06-14 07:12:39 -07001257const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs()
1258 const {
ossu075af922016-06-14 03:29:38 -07001259 return audio_sendrecv_codecs_;
1260}
1261
1262const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const {
1263 return audio_send_codecs_;
1264}
1265
1266const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const {
1267 return audio_recv_codecs_;
1268}
1269
1270void MediaSessionDescriptionFactory::set_audio_codecs(
1271 const AudioCodecs& send_codecs, const AudioCodecs& recv_codecs) {
1272 audio_send_codecs_ = send_codecs;
1273 audio_recv_codecs_ = recv_codecs;
zhihuang1c378ed2017-08-17 14:10:50 -07001274 ComputeAudioCodecsIntersectionAndUnion();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275}
1276
1277SessionDescription* MediaSessionDescriptionFactory::CreateOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001278 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 const SessionDescription* current_description) const {
kwiberg31022942016-03-11 14:18:21 -08001280 std::unique_ptr<SessionDescription> offer(new SessionDescription());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281
1282 StreamParamsVec current_streams;
1283 GetCurrentStreamParams(current_description, &current_streams);
1284
zhihuang1c378ed2017-08-17 14:10:50 -07001285 AudioCodecs offer_audio_codecs;
1286 VideoCodecs offer_video_codecs;
1287 DataCodecs offer_data_codecs;
1288 GetCodecsForOffer(current_description, &offer_audio_codecs,
1289 &offer_video_codecs, &offer_data_codecs);
ossu075af922016-06-14 03:29:38 -07001290
zhihuang1c378ed2017-08-17 14:10:50 -07001291 if (!session_options.vad_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 // If application doesn't want CN codecs in offer.
zhihuang1c378ed2017-08-17 14:10:50 -07001293 StripCNCodecs(&offer_audio_codecs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 }
zhihuang1c378ed2017-08-17 14:10:50 -07001295 FilterDataCodecs(&offer_data_codecs,
1296 session_options.data_channel_type == DCT_SCTP);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297
1298 RtpHeaderExtensions audio_rtp_extensions;
1299 RtpHeaderExtensions video_rtp_extensions;
1300 GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions,
1301 &video_rtp_extensions);
1302
zhihuang1c378ed2017-08-17 14:10:50 -07001303 // Must have options for each existing section.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001304 if (current_description) {
zhihuang1c378ed2017-08-17 14:10:50 -07001305 RTC_DCHECK(current_description->contents().size() <=
1306 session_options.media_description_options.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001308
zhihuang1c378ed2017-08-17 14:10:50 -07001309 // Iterate through the media description options, matching with existing media
1310 // descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001311 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001312 for (const MediaDescriptionOptions& media_description_options :
1313 session_options.media_description_options) {
1314 const ContentInfo* current_content = nullptr;
1315 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001316 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001317 current_content = &current_description->contents()[msection_index];
Steve Antondcc3c022017-12-22 16:02:54 -08001318 // Media type must match unless this media section is being recycled.
1319 RTC_DCHECK(current_content->rejected ||
1320 IsMediaContentOfType(current_content,
zhihuang1c378ed2017-08-17 14:10:50 -07001321 media_description_options.type));
1322 }
1323 switch (media_description_options.type) {
1324 case MEDIA_TYPE_AUDIO:
1325 if (!AddAudioContentForOffer(media_description_options, session_options,
1326 current_content, current_description,
1327 audio_rtp_extensions, offer_audio_codecs,
1328 &current_streams, offer.get())) {
1329 return nullptr;
1330 }
1331 break;
1332 case MEDIA_TYPE_VIDEO:
1333 if (!AddVideoContentForOffer(media_description_options, session_options,
1334 current_content, current_description,
1335 video_rtp_extensions, offer_video_codecs,
1336 &current_streams, offer.get())) {
1337 return nullptr;
1338 }
1339 break;
1340 case MEDIA_TYPE_DATA:
1341 if (!AddDataContentForOffer(media_description_options, session_options,
1342 current_content, current_description,
1343 offer_data_codecs, &current_streams,
1344 offer.get())) {
1345 return nullptr;
1346 }
1347 break;
1348 default:
1349 RTC_NOTREACHED();
1350 }
1351 ++msection_index;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 }
1353
1354 // Bundle the contents together, if we've been asked to do so, and update any
1355 // parameters that need to be tweaked for BUNDLE.
zhihuang1c378ed2017-08-17 14:10:50 -07001356 if (session_options.bundle_enabled && offer->contents().size() > 0u) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
zhihuang1c378ed2017-08-17 14:10:50 -07001358 for (const ContentInfo& content : offer->contents()) {
1359 // TODO(deadbeef): There are conditions that make bundling two media
1360 // descriptions together illegal. For example, they use the same payload
1361 // type to represent different codecs, or same IDs for different header
1362 // extensions. We need to detect this and not try to bundle those media
1363 // descriptions together.
1364 offer_bundle.AddContentName(content.name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 }
1366 offer->AddGroup(offer_bundle);
1367 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001368 RTC_LOG(LS_ERROR)
1369 << "CreateOffer failed to UpdateTransportInfoForBundle.";
zhihuang1c378ed2017-08-17 14:10:50 -07001370 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 }
1372 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001373 RTC_LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle.";
zhihuang1c378ed2017-08-17 14:10:50 -07001374 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375 }
1376 }
Steve Antone831b8c2018-02-01 12:22:16 -08001377
1378 // The following determines how to signal MSIDs to ensure compatibility with
1379 // older endpoints (in particular, older Plan B endpoints).
1380 if (session_options.is_unified_plan) {
1381 // Be conservative and signal using both a=msid and a=ssrc lines. Unified
1382 // Plan answerers will look at a=msid and Plan B answerers will look at the
1383 // a=ssrc MSID line.
1384 offer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1385 cricket::kMsidSignalingSsrcAttribute);
1386 } else {
1387 // Plan B always signals MSID using a=ssrc lines.
1388 offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1389 }
1390
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 return offer.release();
1392}
1393
1394SessionDescription* MediaSessionDescriptionFactory::CreateAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07001395 const SessionDescription* offer,
1396 const MediaSessionOptions& session_options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 const SessionDescription* current_description) const {
deadbeefb7892532017-02-22 19:35:18 -08001398 if (!offer) {
1399 return nullptr;
1400 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 // The answer contains the intersection of the codecs in the offer with the
deadbeef67cf2c12016-04-13 10:07:16 -07001402 // codecs we support. As indicated by XEP-0167, we retain the same payload ids
1403 // from the offer in the answer.
kwiberg31022942016-03-11 14:18:21 -08001404 std::unique_ptr<SessionDescription> answer(new SessionDescription());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405
1406 StreamParamsVec current_streams;
1407 GetCurrentStreamParams(current_description, &current_streams);
1408
deadbeefb7892532017-02-22 19:35:18 -08001409 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1410 // group in the answer with the appropriate content names.
1411 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1412 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1413 // Transport info shared by the bundle group.
1414 std::unique_ptr<TransportInfo> bundle_transport;
1415
zhihuang1c378ed2017-08-17 14:10:50 -07001416 // Get list of all possible codecs that respects existing payload type
1417 // mappings and uses a single payload type space.
1418 //
1419 // Note that these lists may be further filtered for each m= section; this
1420 // step is done just to establish the payload type mappings shared by all
1421 // sections.
1422 AudioCodecs answer_audio_codecs;
1423 VideoCodecs answer_video_codecs;
1424 DataCodecs answer_data_codecs;
1425 GetCodecsForAnswer(current_description, offer, &answer_audio_codecs,
1426 &answer_video_codecs, &answer_data_codecs);
1427
1428 if (!session_options.vad_enabled) {
1429 // If application doesn't want CN codecs in answer.
1430 StripCNCodecs(&answer_audio_codecs);
1431 }
1432 FilterDataCodecs(&answer_data_codecs,
1433 session_options.data_channel_type == DCT_SCTP);
1434
1435 // Must have options for exactly as many sections as in the offer.
1436 RTC_DCHECK(offer->contents().size() ==
1437 session_options.media_description_options.size());
1438 // Iterate through the media description options, matching with existing
1439 // media descriptions in |current_description|.
Steve Antondcc3c022017-12-22 16:02:54 -08001440 size_t msection_index = 0;
zhihuang1c378ed2017-08-17 14:10:50 -07001441 for (const MediaDescriptionOptions& media_description_options :
1442 session_options.media_description_options) {
1443 const ContentInfo* offer_content = &offer->contents()[msection_index];
1444 // Media types and MIDs must match between the remote offer and the
1445 // MediaDescriptionOptions.
1446 RTC_DCHECK(
1447 IsMediaContentOfType(offer_content, media_description_options.type));
1448 RTC_DCHECK(media_description_options.mid == offer_content->name);
1449 const ContentInfo* current_content = nullptr;
1450 if (current_description &&
Steve Antondcc3c022017-12-22 16:02:54 -08001451 msection_index < current_description->contents().size()) {
zhihuang1c378ed2017-08-17 14:10:50 -07001452 current_content = &current_description->contents()[msection_index];
deadbeefb7892532017-02-22 19:35:18 -08001453 }
zhihuang1c378ed2017-08-17 14:10:50 -07001454 switch (media_description_options.type) {
1455 case MEDIA_TYPE_AUDIO:
1456 if (!AddAudioContentForAnswer(
1457 media_description_options, session_options, offer_content,
1458 offer, current_content, current_description,
1459 bundle_transport.get(), answer_audio_codecs, &current_streams,
1460 answer.get())) {
1461 return nullptr;
1462 }
1463 break;
1464 case MEDIA_TYPE_VIDEO:
1465 if (!AddVideoContentForAnswer(
1466 media_description_options, session_options, offer_content,
1467 offer, current_content, current_description,
1468 bundle_transport.get(), answer_video_codecs, &current_streams,
1469 answer.get())) {
1470 return nullptr;
1471 }
1472 break;
1473 case MEDIA_TYPE_DATA:
1474 if (!AddDataContentForAnswer(media_description_options, session_options,
1475 offer_content, offer, current_content,
1476 current_description,
1477 bundle_transport.get(), answer_data_codecs,
1478 &current_streams, answer.get())) {
1479 return nullptr;
1480 }
1481 break;
1482 default:
1483 RTC_NOTREACHED();
1484 }
1485 ++msection_index;
deadbeefb7892532017-02-22 19:35:18 -08001486 // See if we can add the newly generated m= section to the BUNDLE group in
1487 // the answer.
1488 ContentInfo& added = answer->contents().back();
zhihuang1c378ed2017-08-17 14:10:50 -07001489 if (!added.rejected && session_options.bundle_enabled && offer_bundle &&
deadbeefb7892532017-02-22 19:35:18 -08001490 offer_bundle->HasContentName(added.name)) {
1491 answer_bundle.AddContentName(added.name);
1492 bundle_transport.reset(
1493 new TransportInfo(*answer->GetTransportInfoByName(added.name)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 }
1496
deadbeefb7892532017-02-22 19:35:18 -08001497 // Only put BUNDLE group in answer if nonempty.
1498 if (answer_bundle.FirstContentName()) {
1499 answer->AddGroup(answer_bundle);
1500
1501 // Share the same ICE credentials and crypto params across all contents,
1502 // as BUNDLE requires.
1503 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001504 RTC_LOG(LS_ERROR)
1505 << "CreateAnswer failed to UpdateTransportInfoForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001506 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508
deadbeefb7892532017-02-22 19:35:18 -08001509 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001510 RTC_LOG(LS_ERROR)
1511 << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
deadbeefb7892532017-02-22 19:35:18 -08001512 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 }
1514 }
1515
Steve Antone831b8c2018-02-01 12:22:16 -08001516 // The following determines how to signal MSIDs to ensure compatibility with
1517 // older endpoints (in particular, older Plan B endpoints).
1518 if (session_options.is_unified_plan) {
1519 // Unified Plan needs to look at what the offer included to find the most
1520 // compatible answer.
1521 if (offer->msid_signaling() == 0) {
1522 // We end up here in one of three cases:
1523 // 1. An empty offer. We'll reply with an empty answer so it doesn't
1524 // matter what we pick here.
1525 // 2. A data channel only offer. We won't add any MSIDs to the answer so
1526 // it also doesn't matter what we pick here.
1527 // 3. Media that's either sendonly or inactive from the remote endpoint.
1528 // We don't have any information to say whether the endpoint is Plan B
1529 // or Unified Plan, so be conservative and send both.
1530 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
1531 cricket::kMsidSignalingSsrcAttribute);
1532 } else if (offer->msid_signaling() ==
1533 (cricket::kMsidSignalingMediaSection |
1534 cricket::kMsidSignalingSsrcAttribute)) {
1535 // If both a=msid and a=ssrc MSID signaling methods were used, we're
1536 // probably talking to a Unified Plan endpoint so respond with just
1537 // a=msid.
1538 answer->set_msid_signaling(cricket::kMsidSignalingMediaSection);
1539 } else {
1540 // Otherwise, it's clear which method the offerer is using so repeat that
1541 // back to them.
1542 answer->set_msid_signaling(offer->msid_signaling());
1543 }
1544 } else {
1545 // Plan B always signals MSID using a=ssrc lines.
1546 answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
1547 }
1548
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 return answer.release();
1550}
1551
ossu075af922016-06-14 03:29:38 -07001552const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer(
1553 const RtpTransceiverDirection& direction) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001554 switch (direction) {
1555 // If stream is inactive - generate list as if sendrecv.
1556 case RtpTransceiverDirection::kSendRecv:
1557 case RtpTransceiverDirection::kInactive:
1558 return audio_sendrecv_codecs_;
1559 case RtpTransceiverDirection::kSendOnly:
1560 return audio_send_codecs_;
1561 case RtpTransceiverDirection::kRecvOnly:
1562 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001563 }
Steve Anton1d03a752017-11-27 14:30:09 -08001564 RTC_NOTREACHED();
1565 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001566}
1567
1568const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer(
1569 const RtpTransceiverDirection& offer,
1570 const RtpTransceiverDirection& answer) const {
Steve Anton1d03a752017-11-27 14:30:09 -08001571 switch (answer) {
1572 // For inactive and sendrecv answers, generate lists as if we were to accept
1573 // the offer's direction. See RFC 3264 Section 6.1.
1574 case RtpTransceiverDirection::kSendRecv:
1575 case RtpTransceiverDirection::kInactive:
1576 return GetAudioCodecsForOffer(
1577 webrtc::RtpTransceiverDirectionReversed(offer));
1578 case RtpTransceiverDirection::kSendOnly:
ossu075af922016-06-14 03:29:38 -07001579 return audio_send_codecs_;
Steve Anton1d03a752017-11-27 14:30:09 -08001580 case RtpTransceiverDirection::kRecvOnly:
1581 return audio_recv_codecs_;
ossu075af922016-06-14 03:29:38 -07001582 }
Steve Anton1d03a752017-11-27 14:30:09 -08001583 RTC_NOTREACHED();
1584 return audio_sendrecv_codecs_;
ossu075af922016-06-14 03:29:38 -07001585}
1586
zhihuang1c378ed2017-08-17 14:10:50 -07001587void MergeCodecsFromDescription(const SessionDescription* description,
1588 AudioCodecs* audio_codecs,
1589 VideoCodecs* video_codecs,
1590 DataCodecs* data_codecs,
1591 UsedPayloadTypes* used_pltypes) {
1592 RTC_DCHECK(description);
1593 for (const ContentInfo& content : description->contents()) {
1594 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1595 const AudioContentDescription* audio =
Steve Antonb1c1de12017-12-21 15:14:30 -08001596 content.media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001597 MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes);
1598 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1599 const VideoContentDescription* video =
Steve Antonb1c1de12017-12-21 15:14:30 -08001600 content.media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001601 MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes);
1602 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1603 const DataContentDescription* data =
Steve Antonb1c1de12017-12-21 15:14:30 -08001604 content.media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001605 MergeCodecs<DataCodec>(data->codecs(), data_codecs, used_pltypes);
1606 }
1607 }
1608}
1609
1610// Getting codecs for an offer involves these steps:
1611//
1612// 1. Construct payload type -> codec mappings for current description.
1613// 2. Add any reference codecs that weren't already present
1614// 3. For each individual media description (m= section), filter codecs based
1615// on the directional attribute (happens in another method).
1616void MediaSessionDescriptionFactory::GetCodecsForOffer(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 const SessionDescription* current_description,
1618 AudioCodecs* audio_codecs,
1619 VideoCodecs* video_codecs,
1620 DataCodecs* data_codecs) const {
1621 UsedPayloadTypes used_pltypes;
1622 audio_codecs->clear();
1623 video_codecs->clear();
1624 data_codecs->clear();
1625
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 // First - get all codecs from the current description if the media type
zhihuang1c378ed2017-08-17 14:10:50 -07001627 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1628 // new media type is added.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629 if (current_description) {
zhihuang1c378ed2017-08-17 14:10:50 -07001630 MergeCodecsFromDescription(current_description, audio_codecs, video_codecs,
1631 data_codecs, &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 }
1633
1634 // Add our codecs that are not in |current_description|.
zhihuang1c378ed2017-08-17 14:10:50 -07001635 MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes);
1636 MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1637 MergeCodecs<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1638}
1639
1640// Getting codecs for an answer involves these steps:
1641//
1642// 1. Construct payload type -> codec mappings for current description.
1643// 2. Add any codecs from the offer that weren't already present.
1644// 3. Add any remaining codecs that weren't already present.
1645// 4. For each individual media description (m= section), filter codecs based
1646// on the directional attribute (happens in another method).
1647void MediaSessionDescriptionFactory::GetCodecsForAnswer(
1648 const SessionDescription* current_description,
1649 const SessionDescription* remote_offer,
1650 AudioCodecs* audio_codecs,
1651 VideoCodecs* video_codecs,
1652 DataCodecs* data_codecs) const {
1653 UsedPayloadTypes used_pltypes;
1654 audio_codecs->clear();
1655 video_codecs->clear();
1656 data_codecs->clear();
1657
1658 // First - get all codecs from the current description if the media type
1659 // is used. Add them to |used_pltypes| so the payload type is not reused if a
1660 // new media type is added.
1661 if (current_description) {
1662 MergeCodecsFromDescription(current_description, audio_codecs, video_codecs,
1663 data_codecs, &used_pltypes);
1664 }
1665
1666 // Second - filter out codecs that we don't support at all and should ignore.
1667 AudioCodecs filtered_offered_audio_codecs;
1668 VideoCodecs filtered_offered_video_codecs;
1669 DataCodecs filtered_offered_data_codecs;
1670 for (const ContentInfo& content : remote_offer->contents()) {
1671 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1672 const AudioContentDescription* audio =
Steve Antonb1c1de12017-12-21 15:14:30 -08001673 content.media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001674 for (const AudioCodec& offered_audio_codec : audio->codecs()) {
1675 if (!FindMatchingCodec<AudioCodec>(audio->codecs(),
1676 filtered_offered_audio_codecs,
1677 offered_audio_codec, nullptr) &&
1678 FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_,
1679 offered_audio_codec, nullptr)) {
1680 filtered_offered_audio_codecs.push_back(offered_audio_codec);
1681 }
1682 }
1683 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1684 const VideoContentDescription* video =
Steve Antonb1c1de12017-12-21 15:14:30 -08001685 content.media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001686 for (const VideoCodec& offered_video_codec : video->codecs()) {
1687 if (!FindMatchingCodec<VideoCodec>(video->codecs(),
1688 filtered_offered_video_codecs,
1689 offered_video_codec, nullptr) &&
1690 FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_,
1691 offered_video_codec, nullptr)) {
1692 filtered_offered_video_codecs.push_back(offered_video_codec);
1693 }
1694 }
1695 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
1696 const DataContentDescription* data =
Steve Antonb1c1de12017-12-21 15:14:30 -08001697 content.media_description()->as_data();
zhihuang1c378ed2017-08-17 14:10:50 -07001698 for (const DataCodec& offered_data_codec : data->codecs()) {
1699 if (!FindMatchingCodec<DataCodec>(data->codecs(),
1700 filtered_offered_data_codecs,
1701 offered_data_codec, nullptr) &&
1702 FindMatchingCodec<DataCodec>(data->codecs(), data_codecs_,
1703 offered_data_codec, nullptr)) {
1704 filtered_offered_data_codecs.push_back(offered_data_codec);
1705 }
1706 }
1707 }
1708 }
1709
1710 // Add codecs that are not in |current_description| but were in
1711 // |remote_offer|.
1712 MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs,
1713 &used_pltypes);
1714 MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs,
1715 &used_pltypes);
1716 MergeCodecs<DataCodec>(filtered_offered_data_codecs, data_codecs,
1717 &used_pltypes);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718}
1719
1720void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
1721 const SessionDescription* current_description,
zhihuang1c378ed2017-08-17 14:10:50 -07001722 RtpHeaderExtensions* offer_audio_extensions,
1723 RtpHeaderExtensions* offer_video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001724 // All header extensions allocated from the same range to avoid potential
1725 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 UsedRtpHeaderExtensionIds used_ids;
jbauch5869f502017-06-29 12:31:36 -07001727 RtpHeaderExtensions all_regular_extensions;
1728 RtpHeaderExtensions all_encrypted_extensions;
zhihuang1c378ed2017-08-17 14:10:50 -07001729 offer_audio_extensions->clear();
1730 offer_video_extensions->clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731
1732 // First - get all extensions from the current description if the media type
1733 // is used.
1734 // Add them to |used_ids| so the local ids are not reused if a new media
1735 // type is added.
1736 if (current_description) {
zhihuang1c378ed2017-08-17 14:10:50 -07001737 for (const ContentInfo& content : current_description->contents()) {
1738 if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
1739 const AudioContentDescription* audio =
Steve Antonb1c1de12017-12-21 15:14:30 -08001740 content.media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001741 MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions,
1742 &all_regular_extensions, &all_encrypted_extensions,
1743 &used_ids);
1744 } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
1745 const VideoContentDescription* video =
Steve Antonb1c1de12017-12-21 15:14:30 -08001746 content.media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001747 MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions,
1748 &all_regular_extensions, &all_encrypted_extensions,
1749 &used_ids);
1750 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 }
1752 }
1753
1754 // Add our default RTP header extensions that are not in
1755 // |current_description|.
zhihuang1c378ed2017-08-17 14:10:50 -07001756 MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions,
1757 &all_regular_extensions, &all_encrypted_extensions,
1758 &used_ids);
1759 MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions,
1760 &all_regular_extensions, &all_encrypted_extensions,
1761 &used_ids);
1762
jbauch5869f502017-06-29 12:31:36 -07001763 // TODO(jbauch): Support adding encrypted header extensions to existing
1764 // sessions.
1765 if (enable_encrypted_rtp_header_extensions_ && !current_description) {
zhihuang1c378ed2017-08-17 14:10:50 -07001766 AddEncryptedVersionsOfHdrExts(offer_audio_extensions,
1767 &all_encrypted_extensions, &used_ids);
1768 AddEncryptedVersionsOfHdrExts(offer_video_extensions,
1769 &all_encrypted_extensions, &used_ids);
jbauch5869f502017-06-29 12:31:36 -07001770 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771}
1772
1773bool MediaSessionDescriptionFactory::AddTransportOffer(
1774 const std::string& content_name,
1775 const TransportOptions& transport_options,
1776 const SessionDescription* current_desc,
1777 SessionDescription* offer_desc) const {
1778 if (!transport_desc_factory_)
1779 return false;
1780 const TransportDescription* current_tdesc =
1781 GetTransportDescription(content_name, current_desc);
kwiberg31022942016-03-11 14:18:21 -08001782 std::unique_ptr<TransportDescription> new_tdesc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 transport_desc_factory_->CreateOffer(transport_options, current_tdesc));
1784 bool ret = (new_tdesc.get() != NULL &&
1785 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)));
1786 if (!ret) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001787 RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name="
1788 << content_name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 }
1790 return ret;
1791}
1792
1793TransportDescription* MediaSessionDescriptionFactory::CreateTransportAnswer(
1794 const std::string& content_name,
1795 const SessionDescription* offer_desc,
1796 const TransportOptions& transport_options,
deadbeefb7892532017-02-22 19:35:18 -08001797 const SessionDescription* current_desc,
1798 bool require_transport_attributes) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 if (!transport_desc_factory_)
1800 return NULL;
1801 const TransportDescription* offer_tdesc =
1802 GetTransportDescription(content_name, offer_desc);
1803 const TransportDescription* current_tdesc =
1804 GetTransportDescription(content_name, current_desc);
deadbeefb7892532017-02-22 19:35:18 -08001805 return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1806 require_transport_attributes,
1807 current_tdesc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808}
1809
1810bool MediaSessionDescriptionFactory::AddTransportAnswer(
1811 const std::string& content_name,
1812 const TransportDescription& transport_desc,
1813 SessionDescription* answer_desc) const {
1814 if (!answer_desc->AddTransportInfo(TransportInfo(content_name,
1815 transport_desc))) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001816 RTC_LOG(LS_ERROR) << "Failed to AddTransportAnswer, content name="
1817 << content_name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818 return false;
1819 }
1820 return true;
1821}
1822
zhihuang1c378ed2017-08-17 14:10:50 -07001823// |audio_codecs| = set of all possible codecs that can be used, with correct
1824// payload type mappings
1825//
1826// |supported_audio_codecs| = set of codecs that are supported for the direction
1827// of this m= section
1828//
1829// acd->codecs() = set of previously negotiated codecs for this m= section
1830//
1831// The payload types should come from audio_codecs, but the order should come
1832// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
1833// change existing codec priority, and that new codecs are added with the right
1834// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001835bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001836 const MediaDescriptionOptions& media_description_options,
1837 const MediaSessionOptions& session_options,
1838 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001839 const SessionDescription* current_description,
1840 const RtpHeaderExtensions& audio_rtp_extensions,
1841 const AudioCodecs& audio_codecs,
1842 StreamParamsVec* current_streams,
1843 SessionDescription* desc) const {
zhihuang1c378ed2017-08-17 14:10:50 -07001844 // Filter audio_codecs (which includes all codecs, with correctly remapped
1845 // payload types) based on transceiver direction.
1846 const AudioCodecs& supported_audio_codecs =
1847 GetAudioCodecsForOffer(media_description_options.direction);
1848
1849 AudioCodecs filtered_codecs;
Steve Antondcc3c022017-12-22 16:02:54 -08001850 // Add the codecs from current content if it exists and is not being recycled.
1851 if (current_content && !current_content->rejected) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07001852 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07001853 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08001854 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07001855 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07001856 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
1857 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07001858 filtered_codecs.push_back(codec);
1859 }
1860 }
1861 }
1862 // Add other supported audio codecs.
1863 AudioCodec found_codec;
1864 for (const AudioCodec& codec : supported_audio_codecs) {
1865 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
1866 codec, &found_codec) &&
1867 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
1868 codec, nullptr)) {
1869 // Use the |found_codec| from |audio_codecs| because it has the correctly
1870 // mapped payload type.
1871 filtered_codecs.push_back(found_codec);
1872 }
1873 }
deadbeef44f08192015-12-15 16:20:09 -08001874
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001875 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07001876 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
1877 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001878
kwiberg31022942016-03-11 14:18:21 -08001879 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001880 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07001881 GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options,
1882 &crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001883 if (!CreateMediaContentOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001884 media_description_options.sender_options, session_options,
1885 filtered_codecs, sdes_policy, GetCryptos(current_content),
1886 crypto_suites, audio_rtp_extensions, current_streams, audio.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001887 return false;
1888 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001889
1890 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1891 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001892
Steve Anton4e70a722017-11-28 14:57:10 -08001893 audio->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001894
Steve Anton5adfafd2017-12-20 16:34:00 -08001895 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07001896 media_description_options.stopped, audio.release());
1897 if (!AddTransportOffer(media_description_options.mid,
1898 media_description_options.transport_options,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001899 current_description, desc)) {
1900 return false;
1901 }
1902
1903 return true;
1904}
1905
1906bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001907 const MediaDescriptionOptions& media_description_options,
1908 const MediaSessionOptions& session_options,
1909 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001910 const SessionDescription* current_description,
1911 const RtpHeaderExtensions& video_rtp_extensions,
1912 const VideoCodecs& video_codecs,
1913 StreamParamsVec* current_streams,
1914 SessionDescription* desc) const {
1915 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07001916 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
1917 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001918
kwiberg31022942016-03-11 14:18:21 -08001919 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001920 std::vector<std::string> crypto_suites;
zhihuang1c378ed2017-08-17 14:10:50 -07001921 GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options,
1922 &crypto_suites);
1923
1924 VideoCodecs filtered_codecs;
Steve Antondcc3c022017-12-22 16:02:54 -08001925 // Add the codecs from current content if it exists and is not being recycled.
1926 if (current_content && !current_content->rejected) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07001927 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07001928 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08001929 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07001930 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07001931 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07001932 nullptr)) {
1933 filtered_codecs.push_back(codec);
1934 }
1935 }
1936 }
1937 // Add other supported video codecs.
1938 VideoCodec found_codec;
1939 for (const VideoCodec& codec : video_codecs_) {
1940 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
1941 &found_codec) &&
1942 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
1943 nullptr)) {
1944 // Use the |found_codec| from |video_codecs| because it has the correctly
1945 // mapped payload type.
1946 filtered_codecs.push_back(found_codec);
1947 }
1948 }
1949
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001950 if (!CreateMediaContentOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001951 media_description_options.sender_options, session_options,
1952 filtered_codecs, sdes_policy, GetCryptos(current_content),
1953 crypto_suites, video_rtp_extensions, current_streams, video.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001954 return false;
1955 }
1956
zhihuang1c378ed2017-08-17 14:10:50 -07001957 video->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001958
1959 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1960 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001961
Steve Anton4e70a722017-11-28 14:57:10 -08001962 video->set_direction(media_description_options.direction);
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001963
Steve Anton5adfafd2017-12-20 16:34:00 -08001964 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07001965 media_description_options.stopped, video.release());
1966 if (!AddTransportOffer(media_description_options.mid,
1967 media_description_options.transport_options,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001968 current_description, desc)) {
1969 return false;
1970 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001971 return true;
1972}
1973
1974bool MediaSessionDescriptionFactory::AddDataContentForOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07001975 const MediaDescriptionOptions& media_description_options,
1976 const MediaSessionOptions& session_options,
1977 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001978 const SessionDescription* current_description,
zhihuang1c378ed2017-08-17 14:10:50 -07001979 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001980 StreamParamsVec* current_streams,
1981 SessionDescription* desc) const {
1982 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1983
kwiberg31022942016-03-11 14:18:21 -08001984 std::unique_ptr<DataContentDescription> data(new DataContentDescription());
zhihuang1c378ed2017-08-17 14:10:50 -07001985 bool is_sctp = (session_options.data_channel_type == DCT_SCTP);
1986 // If the DataChannel type is not specified, use the DataChannel type in
1987 // the current description.
1988 if (session_options.data_channel_type == DCT_NONE && current_content) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07001989 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA));
Steve Antonb1c1de12017-12-21 15:14:30 -08001990 is_sctp = (current_content->media_description()->protocol() ==
1991 kMediaProtocolSctp);
zhihuang1c378ed2017-08-17 14:10:50 -07001992 }
deadbeef44f08192015-12-15 16:20:09 -08001993
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001994 cricket::SecurePolicy sdes_policy =
zhihuang1c378ed2017-08-17 14:10:50 -07001995 IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
1996 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001997 std::vector<std::string> crypto_suites;
1998 if (is_sctp) {
1999 // SDES doesn't make sense for SCTP, so we disable it, and we only
2000 // get SDES crypto suites for RTP-based data channels.
2001 sdes_policy = cricket::SEC_DISABLED;
2002 // Unlike SetMediaProtocol below, we need to set the protocol
2003 // before we call CreateMediaContentOffer. Otherwise,
2004 // CreateMediaContentOffer won't know this is SCTP and will
2005 // generate SSRCs rather than SIDs.
deadbeef8b7e9ad2017-05-25 09:38:55 -07002006 // TODO(deadbeef): Offer kMediaProtocolUdpDtlsSctp (or TcpDtlsSctp), once
2007 // it's safe to do so. Older versions of webrtc would reject these
2008 // protocols; see https://bugs.chromium.org/p/webrtc/issues/detail?id=7706.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002009 data->set_protocol(
2010 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp);
2011 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002012 GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options,
deadbeef7914b8c2017-04-21 03:23:33 -07002013 &crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002014 }
2015
zhihuang1c378ed2017-08-17 14:10:50 -07002016 // Even SCTP uses a "codec".
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002017 if (!CreateMediaContentOffer(
zhihuang1c378ed2017-08-17 14:10:50 -07002018 media_description_options.sender_options, session_options,
2019 data_codecs, sdes_policy, GetCryptos(current_content), crypto_suites,
2020 RtpHeaderExtensions(), current_streams, data.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002021 return false;
2022 }
2023
2024 if (is_sctp) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002025 desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp,
zhihuang1c378ed2017-08-17 14:10:50 -07002026 data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002027 } else {
zhihuang1c378ed2017-08-17 14:10:50 -07002028 data->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002029 SetMediaProtocol(secure_transport, data.get());
Steve Anton5adfafd2017-12-20 16:34:00 -08002030 desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
zhihuang1c378ed2017-08-17 14:10:50 -07002031 media_description_options.stopped, data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002032 }
zhihuang1c378ed2017-08-17 14:10:50 -07002033 if (!AddTransportOffer(media_description_options.mid,
2034 media_description_options.transport_options,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002035 current_description, desc)) {
2036 return false;
2037 }
2038 return true;
2039}
2040
zhihuang1c378ed2017-08-17 14:10:50 -07002041// |audio_codecs| = set of all possible codecs that can be used, with correct
2042// payload type mappings
2043//
2044// |supported_audio_codecs| = set of codecs that are supported for the direction
2045// of this m= section
2046//
2047// acd->codecs() = set of previously negotiated codecs for this m= section
2048//
2049// The payload types should come from audio_codecs, but the order should come
2050// from acd->codecs() and then supported_codecs, to ensure that re-offers don't
2051// change existing codec priority, and that new codecs are added with the right
2052// priority.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002053bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002054 const MediaDescriptionOptions& media_description_options,
2055 const MediaSessionOptions& session_options,
2056 const ContentInfo* offer_content,
2057 const SessionDescription* offer_description,
2058 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002059 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002060 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002061 const AudioCodecs& audio_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002062 StreamParamsVec* current_streams,
2063 SessionDescription* answer) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002064 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002065 const AudioContentDescription* offer_audio_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002066 offer_content->media_description()->as_audio();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002067
deadbeefb7892532017-02-22 19:35:18 -08002068 std::unique_ptr<TransportDescription> audio_transport(
zhihuang1c378ed2017-08-17 14:10:50 -07002069 CreateTransportAnswer(media_description_options.mid, offer_description,
2070 media_description_options.transport_options,
deadbeefb7892532017-02-22 19:35:18 -08002071 current_description, bundle_transport != nullptr));
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002072 if (!audio_transport) {
2073 return false;
2074 }
2075
zhihuang1c378ed2017-08-17 14:10:50 -07002076 // Pick codecs based on the requested communications direction in the offer
2077 // and the selected direction in the answer.
2078 // Note these will be filtered one final time in CreateMediaContentAnswer.
2079 auto wants_rtd = media_description_options.direction;
Steve Anton4e70a722017-11-28 14:57:10 -08002080 auto offer_rtd = offer_audio_description->direction();
ossu075af922016-06-14 03:29:38 -07002081 auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd);
zhihuang1c378ed2017-08-17 14:10:50 -07002082 AudioCodecs supported_audio_codecs =
2083 GetAudioCodecsForAnswer(offer_rtd, answer_rtd);
2084
2085 AudioCodecs filtered_codecs;
Steve Antondcc3c022017-12-22 16:02:54 -08002086 // Add the codecs from current content if it exists and is not being recycled.
2087 if (current_content && !current_content->rejected) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002088 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
zhihuang1c378ed2017-08-17 14:10:50 -07002089 const AudioContentDescription* acd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002090 current_content->media_description()->as_audio();
zhihuang1c378ed2017-08-17 14:10:50 -07002091 for (const AudioCodec& codec : acd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002092 if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
2093 nullptr)) {
zhihuang1c378ed2017-08-17 14:10:50 -07002094 filtered_codecs.push_back(codec);
2095 }
2096 }
2097 }
2098 // Add other supported audio codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002099 for (const AudioCodec& codec : supported_audio_codecs) {
2100 if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
Zhi Huang6f367472017-11-22 13:20:02 -08002101 codec, nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002102 !FindMatchingCodec<AudioCodec>(supported_audio_codecs, filtered_codecs,
2103 codec, nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002104 // We should use the local codec with local parameters and the codec id
2105 // would be correctly mapped in |NegotiateCodecs|.
2106 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002107 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002108 }
2109
zhihuang1c378ed2017-08-17 14:10:50 -07002110 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2111 session_options.bundle_enabled;
kwiberg31022942016-03-11 14:18:21 -08002112 std::unique_ptr<AudioContentDescription> audio_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002113 new AudioContentDescription());
2114 // Do not require or create SDES cryptos if DTLS is used.
2115 cricket::SecurePolicy sdes_policy =
2116 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
2117 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002118 offer_audio_description, media_description_options, session_options,
2119 filtered_codecs, sdes_policy, GetCryptos(current_content),
2120 audio_rtp_extensions_, enable_encrypted_rtp_header_extensions_,
2121 current_streams, bundle_enabled, audio_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002122 return false; // Fails the session setup.
2123 }
2124
deadbeefb7892532017-02-22 19:35:18 -08002125 bool secure = bundle_transport ? bundle_transport->description.secure()
2126 : audio_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002127 bool rejected = media_description_options.stopped ||
2128 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002129 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
2130 audio_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002131 if (!AddTransportAnswer(media_description_options.mid,
2132 *(audio_transport.get()), answer)) {
2133 return false;
2134 }
2135
2136 if (rejected) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002137 RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid
2138 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002139 }
2140
zhihuang1c378ed2017-08-17 14:10:50 -07002141 answer->AddContent(media_description_options.mid, offer_content->type,
2142 rejected, audio_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002143 return true;
2144}
2145
2146bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002147 const MediaDescriptionOptions& media_description_options,
2148 const MediaSessionOptions& session_options,
2149 const ContentInfo* offer_content,
2150 const SessionDescription* offer_description,
2151 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002152 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002153 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002154 const VideoCodecs& video_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002155 StreamParamsVec* current_streams,
2156 SessionDescription* answer) const {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002157 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002158 const VideoContentDescription* offer_video_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002159 offer_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002160
deadbeefb7892532017-02-22 19:35:18 -08002161 std::unique_ptr<TransportDescription> video_transport(
zhihuang1c378ed2017-08-17 14:10:50 -07002162 CreateTransportAnswer(media_description_options.mid, offer_description,
2163 media_description_options.transport_options,
deadbeefb7892532017-02-22 19:35:18 -08002164 current_description, bundle_transport != nullptr));
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002165 if (!video_transport) {
2166 return false;
2167 }
2168
zhihuang1c378ed2017-08-17 14:10:50 -07002169 VideoCodecs filtered_codecs;
Steve Antondcc3c022017-12-22 16:02:54 -08002170 // Add the codecs from current content if it exists and is not being recycled.
2171 if (current_content && !current_content->rejected) {
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002172 RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
zhihuang1c378ed2017-08-17 14:10:50 -07002173 const VideoContentDescription* vcd =
Steve Antonb1c1de12017-12-21 15:14:30 -08002174 current_content->media_description()->as_video();
zhihuang1c378ed2017-08-17 14:10:50 -07002175 for (const VideoCodec& codec : vcd->codecs()) {
Taylor Brandstetter1c349742017-10-03 18:25:36 -07002176 if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
zhihuang1c378ed2017-08-17 14:10:50 -07002177 nullptr)) {
2178 filtered_codecs.push_back(codec);
2179 }
2180 }
2181 }
2182 // Add other supported video codecs.
zhihuang1c378ed2017-08-17 14:10:50 -07002183 for (const VideoCodec& codec : video_codecs_) {
2184 if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
Zhi Huang6f367472017-11-22 13:20:02 -08002185 nullptr) &&
zhihuang1c378ed2017-08-17 14:10:50 -07002186 !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
2187 nullptr)) {
Zhi Huang6f367472017-11-22 13:20:02 -08002188 // We should use the local codec with local parameters and the codec id
2189 // would be correctly mapped in |NegotiateCodecs|.
2190 filtered_codecs.push_back(codec);
zhihuang1c378ed2017-08-17 14:10:50 -07002191 }
2192 }
2193
2194 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2195 session_options.bundle_enabled;
2196
kwiberg31022942016-03-11 14:18:21 -08002197 std::unique_ptr<VideoContentDescription> video_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002198 new VideoContentDescription());
2199 // Do not require or create SDES cryptos if DTLS is used.
2200 cricket::SecurePolicy sdes_policy =
2201 video_transport->secure() ? cricket::SEC_DISABLED : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002202 if (!CreateMediaContentAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002203 offer_video_description, media_description_options, session_options,
2204 filtered_codecs, sdes_policy, GetCryptos(current_content),
2205 video_rtp_extensions_, enable_encrypted_rtp_header_extensions_,
2206 current_streams, bundle_enabled, video_answer.get())) {
2207 return false; // Failed the sessin setup.
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002208 }
deadbeefb7892532017-02-22 19:35:18 -08002209 bool secure = bundle_transport ? bundle_transport->description.secure()
2210 : video_transport->secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002211 bool rejected = media_description_options.stopped ||
2212 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002213 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
2214 video_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002215 if (!AddTransportAnswer(media_description_options.mid,
2216 *(video_transport.get()), answer)) {
2217 return false;
2218 }
2219
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002220 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002221 video_answer->set_bandwidth(kAutoBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002222 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002223 RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid
2224 << "' being rejected in answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002225 }
zhihuang1c378ed2017-08-17 14:10:50 -07002226 answer->AddContent(media_description_options.mid, offer_content->type,
2227 rejected, video_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002228 return true;
2229}
2230
2231bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
zhihuang1c378ed2017-08-17 14:10:50 -07002232 const MediaDescriptionOptions& media_description_options,
2233 const MediaSessionOptions& session_options,
2234 const ContentInfo* offer_content,
2235 const SessionDescription* offer_description,
2236 const ContentInfo* current_content,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002237 const SessionDescription* current_description,
deadbeefb7892532017-02-22 19:35:18 -08002238 const TransportInfo* bundle_transport,
zhihuang1c378ed2017-08-17 14:10:50 -07002239 const DataCodecs& data_codecs,
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002240 StreamParamsVec* current_streams,
2241 SessionDescription* answer) const {
deadbeefb7892532017-02-22 19:35:18 -08002242 std::unique_ptr<TransportDescription> data_transport(
zhihuang1c378ed2017-08-17 14:10:50 -07002243 CreateTransportAnswer(media_description_options.mid, offer_description,
2244 media_description_options.transport_options,
deadbeefb7892532017-02-22 19:35:18 -08002245 current_description, bundle_transport != nullptr));
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002246 if (!data_transport) {
2247 return false;
2248 }
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002249
kwiberg31022942016-03-11 14:18:21 -08002250 std::unique_ptr<DataContentDescription> data_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002251 new DataContentDescription());
2252 // Do not require or create SDES cryptos if DTLS is used.
2253 cricket::SecurePolicy sdes_policy =
2254 data_transport->secure() ? cricket::SEC_DISABLED : secure();
zhihuang1c378ed2017-08-17 14:10:50 -07002255 bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
2256 session_options.bundle_enabled;
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002257 RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA));
2258 const DataContentDescription* offer_data_description =
Steve Antonb1c1de12017-12-21 15:14:30 -08002259 offer_content->media_description()->as_data();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002260 if (!CreateMediaContentAnswer(
Taylor Brandstetter80cfb522017-10-12 20:37:38 -07002261 offer_data_description, media_description_options, session_options,
2262 data_codecs, sdes_policy, GetCryptos(current_content),
2263 RtpHeaderExtensions(), enable_encrypted_rtp_header_extensions_,
2264 current_streams, bundle_enabled, data_answer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002265 return false; // Fails the session setup.
2266 }
2267
zstein4b2e0822017-02-17 19:48:38 -08002268 // Respond with sctpmap if the offer uses sctpmap.
zstein4b2e0822017-02-17 19:48:38 -08002269 bool offer_uses_sctpmap = offer_data_description->use_sctpmap();
2270 data_answer->set_use_sctpmap(offer_uses_sctpmap);
2271
deadbeefb7892532017-02-22 19:35:18 -08002272 bool secure = bundle_transport ? bundle_transport->description.secure()
2273 : data_transport->secure();
2274
zhihuang1c378ed2017-08-17 14:10:50 -07002275 bool rejected = session_options.data_channel_type == DCT_NONE ||
2276 media_description_options.stopped ||
2277 offer_content->rejected ||
deadbeefb7892532017-02-22 19:35:18 -08002278 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
2279 data_answer->protocol(), secure);
Zhi Huang3518e7b2018-01-30 13:20:35 -08002280 if (!AddTransportAnswer(media_description_options.mid,
2281 *(data_transport.get()), answer)) {
2282 return false;
2283 }
2284
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002285 if (!rejected) {
zhihuang1c378ed2017-08-17 14:10:50 -07002286 data_answer->set_bandwidth(kDataMaxBandwidth);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002287 } else {
2288 // RFC 3264
2289 // The answer MUST contain the same number of m-lines as the offer.
Mirko Bonadei675513b2017-11-09 11:09:25 +01002290 RTC_LOG(LS_INFO) << "Data is not supported in the answer.";
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002291 }
zhihuang1c378ed2017-08-17 14:10:50 -07002292 answer->AddContent(media_description_options.mid, offer_content->type,
2293 rejected, data_answer.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00002294 return true;
2295}
2296
zhihuang1c378ed2017-08-17 14:10:50 -07002297void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() {
2298 audio_sendrecv_codecs_.clear();
2299 all_audio_codecs_.clear();
2300 // Compute the audio codecs union.
2301 for (const AudioCodec& send : audio_send_codecs_) {
2302 all_audio_codecs_.push_back(send);
2303 if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_,
2304 send, nullptr)) {
2305 // It doesn't make sense to have an RTX codec we support sending but not
2306 // receiving.
2307 RTC_DCHECK(!IsRtxCodec(send));
2308 }
2309 }
2310 for (const AudioCodec& recv : audio_recv_codecs_) {
2311 if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_,
2312 recv, nullptr)) {
2313 all_audio_codecs_.push_back(recv);
2314 }
2315 }
2316 // Use NegotiateCodecs to merge our codec lists, since the operation is
2317 // essentially the same. Put send_codecs as the offered_codecs, which is the
2318 // order we'd like to follow. The reasoning is that encoding is usually more
2319 // expensive than decoding, and prioritizing a codec in the send list probably
2320 // means it's a codec we can handle efficiently.
2321 NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_,
2322 &audio_sendrecv_codecs_);
2323}
2324
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325bool IsMediaContent(const ContentInfo* content) {
Steve Anton5adfafd2017-12-20 16:34:00 -08002326 return (content && (content->type == MediaProtocolType::kRtp ||
2327 content->type == MediaProtocolType::kSctp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328}
2329
2330bool IsAudioContent(const ContentInfo* content) {
2331 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
2332}
2333
2334bool IsVideoContent(const ContentInfo* content) {
2335 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
2336}
2337
2338bool IsDataContent(const ContentInfo* content) {
2339 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
2340}
2341
deadbeef0ed85b22016-02-23 17:24:52 -08002342const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
2343 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002344 for (const ContentInfo& content : contents) {
2345 if (IsMediaContentOfType(&content, media_type)) {
2346 return &content;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 }
2348 }
deadbeef0ed85b22016-02-23 17:24:52 -08002349 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350}
2351
2352const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
2353 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2354}
2355
2356const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
2357 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2358}
2359
2360const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
2361 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2362}
2363
Steve Antonad7bffc2018-01-22 10:21:56 -08002364const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
2365 MediaType media_type) {
deadbeef0ed85b22016-02-23 17:24:52 -08002366 if (sdesc == nullptr) {
2367 return nullptr;
2368 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369
2370 return GetFirstMediaContent(sdesc->contents(), media_type);
2371}
2372
2373const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
2374 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2375}
2376
2377const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
2378 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2379}
2380
2381const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
2382 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2383}
2384
2385const MediaContentDescription* GetFirstMediaContentDescription(
2386 const SessionDescription* sdesc, MediaType media_type) {
2387 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002388 return (content ? content->media_description() : nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002389}
2390
2391const AudioContentDescription* GetFirstAudioContentDescription(
2392 const SessionDescription* sdesc) {
2393 return static_cast<const AudioContentDescription*>(
2394 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2395}
2396
2397const VideoContentDescription* GetFirstVideoContentDescription(
2398 const SessionDescription* sdesc) {
2399 return static_cast<const VideoContentDescription*>(
2400 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2401}
2402
2403const DataContentDescription* GetFirstDataContentDescription(
2404 const SessionDescription* sdesc) {
2405 return static_cast<const DataContentDescription*>(
2406 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2407}
2408
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002409//
2410// Non-const versions of the above functions.
2411//
2412
Steve Anton36b29d12017-10-30 09:57:42 -07002413ContentInfo* GetFirstMediaContent(ContentInfos* contents,
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002414 MediaType media_type) {
Steve Anton36b29d12017-10-30 09:57:42 -07002415 for (ContentInfo& content : *contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002416 if (IsMediaContentOfType(&content, media_type)) {
2417 return &content;
2418 }
2419 }
2420 return nullptr;
2421}
2422
Steve Anton36b29d12017-10-30 09:57:42 -07002423ContentInfo* GetFirstAudioContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002424 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
2425}
2426
Steve Anton36b29d12017-10-30 09:57:42 -07002427ContentInfo* GetFirstVideoContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002428 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
2429}
2430
Steve Anton36b29d12017-10-30 09:57:42 -07002431ContentInfo* GetFirstDataContent(ContentInfos* contents) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002432 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
2433}
2434
Steve Antonad7bffc2018-01-22 10:21:56 -08002435ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
2436 MediaType media_type) {
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002437 if (sdesc == nullptr) {
2438 return nullptr;
2439 }
2440
Steve Anton36b29d12017-10-30 09:57:42 -07002441 return GetFirstMediaContent(&sdesc->contents(), media_type);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002442}
2443
2444ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) {
2445 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
2446}
2447
2448ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) {
2449 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
2450}
2451
2452ContentInfo* GetFirstDataContent(SessionDescription* sdesc) {
2453 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
2454}
2455
2456MediaContentDescription* GetFirstMediaContentDescription(
2457 SessionDescription* sdesc,
2458 MediaType media_type) {
2459 ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
Steve Antonb1c1de12017-12-21 15:14:30 -08002460 return (content ? content->media_description() : nullptr);
Taylor Brandstetterdc4eb8c2016-05-12 08:14:50 -07002461}
2462
2463AudioContentDescription* GetFirstAudioContentDescription(
2464 SessionDescription* sdesc) {
2465 return static_cast<AudioContentDescription*>(
2466 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
2467}
2468
2469VideoContentDescription* GetFirstVideoContentDescription(
2470 SessionDescription* sdesc) {
2471 return static_cast<VideoContentDescription*>(
2472 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
2473}
2474
2475DataContentDescription* GetFirstDataContentDescription(
2476 SessionDescription* sdesc) {
2477 return static_cast<DataContentDescription*>(
2478 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2479}
2480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481} // namespace cricket