blob: a9d1b95c22aee6aae9299315a9ecc05586242336 [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
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010011#include "webrtc/pc/mediasession.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -080013#include <algorithm> // For std::find_if.
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>
18#include <utility>
19
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000020#include "webrtc/base/helpers.h"
21#include "webrtc/base/logging.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000022#include "webrtc/base/stringutils.h"
kjellandera96e2d72016-02-04 23:52:28 -080023#include "webrtc/media/base/cryptoparams.h"
kjellanderf4752772016-03-02 05:42:30 -080024#include "webrtc/media/base/mediaconstants.h"
25#include "webrtc/p2p/base/p2pconstants.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010026#include "webrtc/pc/channelmanager.h"
27#include "webrtc/pc/srtpfilter.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000029#ifdef HAVE_SCTP
kjellandera96e2d72016-02-04 23:52:28 -080030#include "webrtc/media/sctp/sctpdataengine.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000031#else
Peter Boström0c4e06b2015-10-07 12:23:21 +020032static const uint32_t kMaxSctpSid = 1023;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000033#endif
34
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035namespace {
36const char kInline[] = "inline:";
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080037
38void GetSupportedCryptoSuiteNames(void (*func)(std::vector<int>*),
39 std::vector<std::string>* names) {
40#ifdef HAVE_SRTP
41 std::vector<int> crypto_suites;
42 func(&crypto_suites);
43 for (const auto crypto : crypto_suites) {
44 names->push_back(rtc::SrtpCryptoSuiteToName(crypto));
45 }
46#endif
47}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048}
49
50namespace cricket {
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052
53// RTP Profile names
54// http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
55// RFC4585
56const char kMediaProtocolAvpf[] = "RTP/AVPF";
57// RFC5124
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +000058const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
59
deadbeeff3938292015-07-15 12:20:53 -070060// We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
61// but we tolerate "RTP/SAVPF" in offers we receive, for compatibility.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062const char kMediaProtocolSavpf[] = "RTP/SAVPF";
63
64const char kMediaProtocolRtpPrefix[] = "RTP/";
65
66const char kMediaProtocolSctp[] = "SCTP";
67const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
lally@webrtc.orgec97c652015-02-24 20:18:48 +000068const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
lally@webrtc.orga7470932015-02-24 20:19:21 +000069const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070
71static bool IsMediaContentOfType(const ContentInfo* content,
72 MediaType media_type) {
73 if (!IsMediaContent(content)) {
74 return false;
75 }
76
77 const MediaContentDescription* mdesc =
78 static_cast<const MediaContentDescription*>(content->description);
79 return mdesc && mdesc->type() == media_type;
80}
81
82static bool CreateCryptoParams(int tag, const std::string& cipher,
83 CryptoParams *out) {
84 std::string key;
85 key.reserve(SRTP_MASTER_KEY_BASE64_LEN);
86
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000087 if (!rtc::CreateRandomString(SRTP_MASTER_KEY_BASE64_LEN, &key)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 return false;
89 }
90 out->tag = tag;
91 out->cipher_suite = cipher;
92 out->key_params = kInline;
93 out->key_params += key;
94 return true;
95}
96
97#ifdef HAVE_SRTP
98static bool AddCryptoParams(const std::string& cipher_suite,
99 CryptoParamsVec *out) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000100 int size = static_cast<int>(out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101
102 out->resize(size + 1);
103 return CreateCryptoParams(size, cipher_suite, &out->at(size));
104}
105
106void AddMediaCryptos(const CryptoParamsVec& cryptos,
107 MediaContentDescription* media) {
108 for (CryptoParamsVec::const_iterator crypto = cryptos.begin();
109 crypto != cryptos.end(); ++crypto) {
110 media->AddCrypto(*crypto);
111 }
112}
113
114bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
115 MediaContentDescription* media) {
116 CryptoParamsVec cryptos;
117 for (std::vector<std::string>::const_iterator it = crypto_suites.begin();
118 it != crypto_suites.end(); ++it) {
119 if (!AddCryptoParams(*it, &cryptos)) {
120 return false;
121 }
122 }
123 AddMediaCryptos(cryptos, media);
124 return true;
125}
126#endif
127
128const CryptoParamsVec* GetCryptos(const MediaContentDescription* media) {
129 if (!media) {
130 return NULL;
131 }
132 return &media->cryptos();
133}
134
135bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
136 const CryptoParams& crypto,
137 CryptoParams* out) {
138 for (CryptoParamsVec::const_iterator it = cryptos.begin();
139 it != cryptos.end(); ++it) {
140 if (crypto.Matches(*it)) {
141 *out = *it;
142 return true;
143 }
144 }
145 return false;
146}
147
148// For audio, HMAC 32 is prefered because of the low overhead.
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800149void GetSupportedAudioCryptoSuites(std::vector<int>* crypto_suites) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150#ifdef HAVE_SRTP
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800151 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32);
152 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153#endif
154}
155
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800156void GetSupportedAudioCryptoSuiteNames(
157 std::vector<std::string>* crypto_suite_names) {
158 GetSupportedCryptoSuiteNames(GetSupportedAudioCryptoSuites,
159 crypto_suite_names);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160}
161
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800162void GetSupportedVideoCryptoSuites(std::vector<int>* crypto_suites) {
163 GetDefaultSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164}
165
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800166void GetSupportedVideoCryptoSuiteNames(
167 std::vector<std::string>* crypto_suite_names) {
168 GetSupportedCryptoSuiteNames(GetSupportedVideoCryptoSuites,
169 crypto_suite_names);
170}
171
172void GetSupportedDataCryptoSuites(std::vector<int>* crypto_suites) {
173 GetDefaultSrtpCryptoSuites(crypto_suites);
174}
175
176void GetSupportedDataCryptoSuiteNames(
177 std::vector<std::string>* crypto_suite_names) {
178 GetSupportedCryptoSuiteNames(GetSupportedDataCryptoSuites,
179 crypto_suite_names);
180}
181
182void GetDefaultSrtpCryptoSuites(std::vector<int>* crypto_suites) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183#ifdef HAVE_SRTP
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800184 crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185#endif
186}
187
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800188void GetDefaultSrtpCryptoSuiteNames(
189 std::vector<std::string>* crypto_suite_names) {
190 GetSupportedCryptoSuiteNames(GetDefaultSrtpCryptoSuites, crypto_suite_names);
191}
192
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193// For video support only 80-bit SHA1 HMAC. For audio 32-bit HMAC is
194// tolerated unless bundle is enabled because it is low overhead. Pick the
195// crypto in the list that is supported.
196static bool SelectCrypto(const MediaContentDescription* offer,
197 bool bundle,
198 CryptoParams *crypto) {
199 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
200 const CryptoParamsVec& cryptos = offer->cryptos();
201
202 for (CryptoParamsVec::const_iterator i = cryptos.begin();
203 i != cryptos.end(); ++i) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700204 if (rtc::CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite ||
205 (rtc::CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio &&
206 !bundle)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207 return CreateCryptoParams(i->tag, i->cipher_suite, crypto);
208 }
209 }
210 return false;
211}
212
213static const StreamParams* FindFirstStreamParamsByCname(
214 const StreamParamsVec& params_vec,
215 const std::string& cname) {
216 for (StreamParamsVec::const_iterator it = params_vec.begin();
217 it != params_vec.end(); ++it) {
218 if (cname == it->cname)
219 return &*it;
220 }
221 return NULL;
222}
223
224// Generates a new CNAME or the CNAME of an already existing StreamParams
225// if a StreamParams exist for another Stream in streams with sync_label
226// sync_label.
227static bool GenerateCname(const StreamParamsVec& params_vec,
228 const MediaSessionOptions::Streams& streams,
229 const std::string& synch_label,
230 std::string* cname) {
231 ASSERT(cname != NULL);
232 if (!cname)
233 return false;
234
235 // Check if a CNAME exist for any of the other synched streams.
236 for (MediaSessionOptions::Streams::const_iterator stream_it = streams.begin();
237 stream_it != streams.end() ; ++stream_it) {
238 if (synch_label != stream_it->sync_label)
239 continue;
240
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 // groupid is empty for StreamParams generated using
242 // MediaSessionDescriptionFactory.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000243 const StreamParams* param = GetStreamByIds(params_vec, "", stream_it->id);
244 if (param) {
245 *cname = param->cname;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 return true;
247 }
248 }
249 // No other stream seems to exist that we should sync with.
250 // Generate a random string for the RTCP CNAME, as stated in RFC 6222.
251 // This string is only used for synchronization, and therefore is opaque.
252 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000253 if (!rtc::CreateRandomString(16, cname)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 ASSERT(false);
255 return false;
256 }
257 } while (FindFirstStreamParamsByCname(params_vec, *cname));
258
259 return true;
260}
261
262// Generate random SSRC values that are not already present in |params_vec|.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000263// The generated values are added to |ssrcs|.
264// |num_ssrcs| is the number of the SSRC will be generated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265static void GenerateSsrcs(const StreamParamsVec& params_vec,
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000266 int num_ssrcs,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200267 std::vector<uint32_t>* ssrcs) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000268 for (int i = 0; i < num_ssrcs; i++) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200269 uint32_t candidate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000271 candidate = rtc::CreateRandomNonZeroId();
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272 } while (GetStreamBySsrc(params_vec, candidate) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 std::count(ssrcs->begin(), ssrcs->end(), candidate) > 0);
274 ssrcs->push_back(candidate);
275 }
276}
277
278// Returns false if we exhaust the range of SIDs.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200279static bool GenerateSctpSid(const StreamParamsVec& params_vec, uint32_t* sid) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280 if (params_vec.size() > kMaxSctpSid) {
281 LOG(LS_WARNING) <<
282 "Could not generate an SCTP SID: too many SCTP streams.";
283 return false;
284 }
285 while (true) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200286 uint32_t candidate = rtc::CreateRandomNonZeroId() % kMaxSctpSid;
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000287 if (!GetStreamBySsrc(params_vec, candidate)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000288 *sid = candidate;
289 return true;
290 }
291 }
292}
293
294static bool GenerateSctpSids(const StreamParamsVec& params_vec,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200295 std::vector<uint32_t>* sids) {
296 uint32_t sid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297 if (!GenerateSctpSid(params_vec, &sid)) {
298 LOG(LS_WARNING) << "Could not generated an SCTP SID.";
299 return false;
300 }
301 sids->push_back(sid);
302 return true;
303}
304
305// Finds all StreamParams of all media types and attach them to stream_params.
306static void GetCurrentStreamParams(const SessionDescription* sdesc,
307 StreamParamsVec* stream_params) {
308 if (!sdesc)
309 return;
310
311 const ContentInfos& contents = sdesc->contents();
312 for (ContentInfos::const_iterator content = contents.begin();
313 content != contents.end(); ++content) {
314 if (!IsMediaContent(&*content)) {
315 continue;
316 }
317 const MediaContentDescription* media =
318 static_cast<const MediaContentDescription*>(
319 content->description);
320 const StreamParamsVec& streams = media->streams();
321 for (StreamParamsVec::const_iterator it = streams.begin();
322 it != streams.end(); ++it) {
323 stream_params->push_back(*it);
324 }
325 }
326}
327
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000328// Filters the data codecs for the data channel type.
329void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
330 // Filter RTP codec for SCTP and vice versa.
331 int codec_id = sctp ? kGoogleRtpDataCodecId : kGoogleSctpDataCodecId;
332 for (std::vector<DataCodec>::iterator iter = codecs->begin();
333 iter != codecs->end();) {
334 if (iter->id == codec_id) {
335 iter = codecs->erase(iter);
336 } else {
337 ++iter;
338 }
339 }
340}
341
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342template <typename IdStruct>
343class UsedIds {
344 public:
345 UsedIds(int min_allowed_id, int max_allowed_id)
346 : min_allowed_id_(min_allowed_id),
347 max_allowed_id_(max_allowed_id),
348 next_id_(max_allowed_id) {
349 }
350
351 // Loops through all Id in |ids| and changes its id if it is
352 // already in use by another IdStruct. Call this methods with all Id
353 // in a session description to make sure no duplicate ids exists.
354 // Note that typename Id must be a type of IdStruct.
355 template <typename Id>
356 void FindAndSetIdUsed(std::vector<Id>* ids) {
357 for (typename std::vector<Id>::iterator it = ids->begin();
358 it != ids->end(); ++it) {
359 FindAndSetIdUsed(&*it);
360 }
361 }
362
363 // Finds and sets an unused id if the |idstruct| id is already in use.
364 void FindAndSetIdUsed(IdStruct* idstruct) {
365 const int original_id = idstruct->id;
366 int new_id = idstruct->id;
367
368 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
369 // If the original id is not in range - this is an id that can't be
370 // dynamically changed.
371 return;
372 }
373
374 if (IsIdUsed(original_id)) {
375 new_id = FindUnusedId();
376 LOG(LS_WARNING) << "Duplicate id found. Reassigning from " << original_id
377 << " to " << new_id;
378 idstruct->id = new_id;
379 }
380 SetIdUsed(new_id);
381 }
382
383 private:
384 // Returns the first unused id in reverse order.
385 // This hopefully reduce the risk of more collisions. We want to change the
386 // default ids as little as possible.
387 int FindUnusedId() {
388 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
389 --next_id_;
390 }
391 ASSERT(next_id_ >= min_allowed_id_);
392 return next_id_;
393 }
394
395 bool IsIdUsed(int new_id) {
396 return id_set_.find(new_id) != id_set_.end();
397 }
398
399 void SetIdUsed(int new_id) {
400 id_set_.insert(new_id);
401 }
402
403 const int min_allowed_id_;
404 const int max_allowed_id_;
405 int next_id_;
406 std::set<int> id_set_;
407};
408
409// Helper class used for finding duplicate RTP payload types among audio, video
410// and data codecs. When bundle is used the payload types may not collide.
411class UsedPayloadTypes : public UsedIds<Codec> {
412 public:
413 UsedPayloadTypes()
414 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {
415 }
416
417
418 private:
419 static const int kDynamicPayloadTypeMin = 96;
420 static const int kDynamicPayloadTypeMax = 127;
421};
422
423// Helper class used for finding duplicate RTP Header extension ids among
424// audio and video extensions.
425class UsedRtpHeaderExtensionIds : public UsedIds<RtpHeaderExtension> {
426 public:
427 UsedRtpHeaderExtensionIds()
428 : UsedIds<RtpHeaderExtension>(kLocalIdMin, kLocalIdMax) {
429 }
430
431 private:
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000432 // Min and Max local identifier for one-byte header extensions, per RFC5285.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 static const int kLocalIdMin = 1;
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000434 static const int kLocalIdMax = 14;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435};
436
437static bool IsSctp(const MediaContentDescription* desc) {
438 return ((desc->protocol() == kMediaProtocolSctp) ||
439 (desc->protocol() == kMediaProtocolDtlsSctp));
440}
441
442// Adds a StreamParams for each Stream in Streams with media type
443// media_type to content_description.
444// |current_params| - All currently known StreamParams of any media type.
445template <class C>
446static bool AddStreamParams(
447 MediaType media_type,
448 const MediaSessionOptions::Streams& streams,
449 StreamParamsVec* current_streams,
450 MediaContentDescriptionImpl<C>* content_description,
451 const bool add_legacy_stream) {
Noah Richards2e7a0982015-05-18 14:02:54 -0700452 const bool include_rtx_streams =
453 ContainsRtxCodec(content_description->codecs());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
455 if (streams.empty() && add_legacy_stream) {
456 // TODO(perkj): Remove this legacy stream when all apps use StreamParams.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200457 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 if (IsSctp(content_description)) {
459 GenerateSctpSids(*current_streams, &ssrcs);
460 } else {
Noah Richards2e7a0982015-05-18 14:02:54 -0700461 int num_ssrcs = include_rtx_streams ? 2 : 1;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000462 GenerateSsrcs(*current_streams, num_ssrcs, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 }
Noah Richards2e7a0982015-05-18 14:02:54 -0700464 if (include_rtx_streams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 content_description->AddLegacyStream(ssrcs[0], ssrcs[1]);
466 content_description->set_multistream(true);
467 } else {
468 content_description->AddLegacyStream(ssrcs[0]);
469 }
470 return true;
471 }
472
473 MediaSessionOptions::Streams::const_iterator stream_it;
474 for (stream_it = streams.begin();
475 stream_it != streams.end(); ++stream_it) {
476 if (stream_it->type != media_type)
477 continue; // Wrong media type.
478
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000479 const StreamParams* param =
480 GetStreamByIds(*current_streams, "", stream_it->id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 // groupid is empty for StreamParams generated using
482 // MediaSessionDescriptionFactory.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000483 if (!param) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 // This is a new stream.
485 // Get a CNAME. Either new or same as one of the other synched streams.
486 std::string cname;
487 if (!GenerateCname(*current_streams, streams, stream_it->sync_label,
488 &cname)) {
489 return false;
490 }
491
Peter Boström0c4e06b2015-10-07 12:23:21 +0200492 std::vector<uint32_t> ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 if (IsSctp(content_description)) {
494 GenerateSctpSids(*current_streams, &ssrcs);
495 } else {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000496 GenerateSsrcs(*current_streams, stream_it->num_sim_layers, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498 StreamParams stream_param;
499 stream_param.id = stream_it->id;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000500 // Add the generated ssrc.
501 for (size_t i = 0; i < ssrcs.size(); ++i) {
502 stream_param.ssrcs.push_back(ssrcs[i]);
503 }
504 if (stream_it->num_sim_layers > 1) {
505 SsrcGroup group(kSimSsrcGroupSemantics, stream_param.ssrcs);
506 stream_param.ssrc_groups.push_back(group);
507 }
Noah Richards2e7a0982015-05-18 14:02:54 -0700508 // Generate extra ssrcs for include_rtx_streams case.
509 if (include_rtx_streams) {
510 // Generate an RTX ssrc for every ssrc in the group.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200511 std::vector<uint32_t> rtx_ssrcs;
Noah Richards2e7a0982015-05-18 14:02:54 -0700512 GenerateSsrcs(*current_streams, static_cast<int>(ssrcs.size()),
513 &rtx_ssrcs);
514 for (size_t i = 0; i < ssrcs.size(); ++i) {
515 stream_param.AddFidSsrc(ssrcs[i], rtx_ssrcs[i]);
516 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 content_description->set_multistream(true);
518 }
519 stream_param.cname = cname;
520 stream_param.sync_label = stream_it->sync_label;
521 content_description->AddStream(stream_param);
522
523 // Store the new StreamParams in current_streams.
524 // This is necessary so that we can use the CNAME for other media types.
525 current_streams->push_back(stream_param);
526 } else {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000527 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 }
529 }
530 return true;
531}
532
533// Updates the transport infos of the |sdesc| according to the given
534// |bundle_group|. The transport infos of the content names within the
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800535// |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
536// first content within the |bundle_group|.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
538 SessionDescription* sdesc) {
539 // The bundle should not be empty.
540 if (!sdesc || !bundle_group.FirstContentName()) {
541 return false;
542 }
543
544 // We should definitely have a transport for the first content.
jbauch083b73f2015-07-16 02:46:32 -0700545 const std::string& selected_content_name = *bundle_group.FirstContentName();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 const TransportInfo* selected_transport_info =
547 sdesc->GetTransportInfoByName(selected_content_name);
548 if (!selected_transport_info) {
549 return false;
550 }
551
552 // Set the other contents to use the same ICE credentials.
jbauch083b73f2015-07-16 02:46:32 -0700553 const std::string& selected_ufrag =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554 selected_transport_info->description.ice_ufrag;
jbauch083b73f2015-07-16 02:46:32 -0700555 const std::string& selected_pwd =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 selected_transport_info->description.ice_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800557 ConnectionRole selected_connection_role =
558 selected_transport_info->description.connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 for (TransportInfos::iterator it =
560 sdesc->transport_infos().begin();
561 it != sdesc->transport_infos().end(); ++it) {
562 if (bundle_group.HasContentName(it->content_name) &&
563 it->content_name != selected_content_name) {
564 it->description.ice_ufrag = selected_ufrag;
565 it->description.ice_pwd = selected_pwd;
Taylor Brandstetterf475d362016-01-08 15:35:57 -0800566 it->description.connection_role = selected_connection_role;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 }
568 }
569 return true;
570}
571
572// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
573// sets it to |cryptos|.
574static bool GetCryptosByName(const SessionDescription* sdesc,
575 const std::string& content_name,
576 CryptoParamsVec* cryptos) {
577 if (!sdesc || !cryptos) {
578 return false;
579 }
580
581 const ContentInfo* content = sdesc->GetContentByName(content_name);
582 if (!IsMediaContent(content) || !content->description) {
583 return false;
584 }
585
586 const MediaContentDescription* media_desc =
587 static_cast<const MediaContentDescription*>(content->description);
588 *cryptos = media_desc->cryptos();
589 return true;
590}
591
592// Predicate function used by the remove_if.
593// Returns true if the |crypto|'s cipher_suite is not found in |filter|.
594static bool CryptoNotFound(const CryptoParams crypto,
595 const CryptoParamsVec* filter) {
596 if (filter == NULL) {
597 return true;
598 }
599 for (CryptoParamsVec::const_iterator it = filter->begin();
600 it != filter->end(); ++it) {
601 if (it->cipher_suite == crypto.cipher_suite) {
602 return false;
603 }
604 }
605 return true;
606}
607
608// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
609// which are not available in |filter|.
610static void PruneCryptos(const CryptoParamsVec& filter,
611 CryptoParamsVec* target_cryptos) {
612 if (!target_cryptos) {
613 return;
614 }
615 target_cryptos->erase(std::remove_if(target_cryptos->begin(),
616 target_cryptos->end(),
617 bind2nd(ptr_fun(CryptoNotFound),
618 &filter)),
619 target_cryptos->end());
620}
621
deadbeefb5cb19b2015-11-23 16:39:12 -0800622static bool IsRtpProtocol(const std::string& protocol) {
623 return protocol.empty() ||
624 (protocol.find(cricket::kMediaProtocolRtpPrefix) != std::string::npos);
625}
626
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627static bool IsRtpContent(SessionDescription* sdesc,
628 const std::string& content_name) {
629 bool is_rtp = false;
630 ContentInfo* content = sdesc->GetContentByName(content_name);
631 if (IsMediaContent(content)) {
632 MediaContentDescription* media_desc =
633 static_cast<MediaContentDescription*>(content->description);
634 if (!media_desc) {
635 return false;
636 }
deadbeefb5cb19b2015-11-23 16:39:12 -0800637 is_rtp = IsRtpProtocol(media_desc->protocol());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 }
639 return is_rtp;
640}
641
642// Updates the crypto parameters of the |sdesc| according to the given
643// |bundle_group|. The crypto parameters of all the contents within the
644// |bundle_group| should be updated to use the common subset of the
645// available cryptos.
646static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
647 SessionDescription* sdesc) {
648 // The bundle should not be empty.
649 if (!sdesc || !bundle_group.FirstContentName()) {
650 return false;
651 }
652
wu@webrtc.org78187522013-10-07 23:32:02 +0000653 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 // Get the common cryptos.
655 const ContentNames& content_names = bundle_group.content_names();
656 CryptoParamsVec common_cryptos;
657 for (ContentNames::const_iterator it = content_names.begin();
658 it != content_names.end(); ++it) {
659 if (!IsRtpContent(sdesc, *it)) {
660 continue;
661 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000662 // The common cryptos are needed if any of the content does not have DTLS
663 // enabled.
664 if (!sdesc->GetTransportInfoByName(*it)->description.secure()) {
665 common_cryptos_needed = true;
666 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 if (it == content_names.begin()) {
668 // Initial the common_cryptos with the first content in the bundle group.
669 if (!GetCryptosByName(sdesc, *it, &common_cryptos)) {
670 return false;
671 }
672 if (common_cryptos.empty()) {
673 // If there's no crypto params, we should just return.
674 return true;
675 }
676 } else {
677 CryptoParamsVec cryptos;
678 if (!GetCryptosByName(sdesc, *it, &cryptos)) {
679 return false;
680 }
681 PruneCryptos(cryptos, &common_cryptos);
682 }
683 }
684
wu@webrtc.org78187522013-10-07 23:32:02 +0000685 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 return false;
687 }
688
689 // Update to use the common cryptos.
690 for (ContentNames::const_iterator it = content_names.begin();
691 it != content_names.end(); ++it) {
692 if (!IsRtpContent(sdesc, *it)) {
693 continue;
694 }
695 ContentInfo* content = sdesc->GetContentByName(*it);
696 if (IsMediaContent(content)) {
697 MediaContentDescription* media_desc =
698 static_cast<MediaContentDescription*>(content->description);
699 if (!media_desc) {
700 return false;
701 }
702 media_desc->set_cryptos(common_cryptos);
703 }
704 }
705 return true;
706}
707
708template <class C>
709static bool ContainsRtxCodec(const std::vector<C>& codecs) {
710 typename std::vector<C>::const_iterator it;
711 for (it = codecs.begin(); it != codecs.end(); ++it) {
712 if (IsRtxCodec(*it)) {
713 return true;
714 }
715 }
716 return false;
717}
718
719template <class C>
720static bool IsRtxCodec(const C& codec) {
721 return stricmp(codec.name.c_str(), kRtxCodecName) == 0;
722}
723
deadbeef0ed85b22016-02-23 17:24:52 -0800724static TransportOptions GetTransportOptions(const MediaSessionOptions& options,
725 const std::string& content_name) {
726 auto it = options.transport_options.find(content_name);
727 if (it == options.transport_options.end()) {
728 return TransportOptions();
729 }
730 return it->second;
731}
732
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733// Create a media content to be offered in a session-initiate,
734// according to the given options.rtcp_mux, options.is_muc,
735// options.streams, codecs, secure_transport, crypto, and streams. If we don't
736// currently have crypto (in current_cryptos) and it is enabled (in
737// secure_policy), crypto is created (according to crypto_suites). If
738// add_legacy_stream is true, and current_streams is empty, a legacy
739// stream is created. The created content is added to the offer.
740template <class C>
741static bool CreateMediaContentOffer(
742 const MediaSessionOptions& options,
743 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000744 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 const CryptoParamsVec* current_cryptos,
746 const std::vector<std::string>& crypto_suites,
747 const RtpHeaderExtensions& rtp_extensions,
748 bool add_legacy_stream,
749 StreamParamsVec* current_streams,
750 MediaContentDescriptionImpl<C>* offer) {
751 offer->AddCodecs(codecs);
752 offer->SortCodecs();
753
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000754 if (secure_policy == SEC_REQUIRED) {
755 offer->set_crypto_required(CT_SDES);
756 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 offer->set_rtcp_mux(options.rtcp_mux_enabled);
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -0700758 if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
759 offer->set_rtcp_reduced_size(true);
760 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 offer->set_multistream(options.is_muc);
762 offer->set_rtp_header_extensions(rtp_extensions);
763
764 if (!AddStreamParams(
765 offer->type(), options.streams, current_streams,
766 offer, add_legacy_stream)) {
767 return false;
768 }
769
770#ifdef HAVE_SRTP
771 if (secure_policy != SEC_DISABLED) {
772 if (current_cryptos) {
773 AddMediaCryptos(*current_cryptos, offer);
774 }
775 if (offer->cryptos().empty()) {
776 if (!CreateMediaCryptos(crypto_suites, offer)) {
777 return false;
778 }
779 }
780 }
781#endif
782
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000783 if (offer->crypto_required() == CT_SDES && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 return false;
785 }
786 return true;
787}
788
789template <class C>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000790static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
791 const std::string& codec1_id_str,
792 const std::vector<C>& codecs2,
793 const std::string& codec2_id_str) {
794 int codec1_id;
795 int codec2_id;
796 C codec1;
797 C codec2;
798 if (!rtc::FromString(codec1_id_str, &codec1_id) ||
799 !rtc::FromString(codec2_id_str, &codec2_id) ||
800 !FindCodecById(codecs1, codec1_id, &codec1) ||
801 !FindCodecById(codecs2, codec2_id, &codec2)) {
802 return false;
803 }
804 return codec1.Matches(codec2);
805}
806
807template <class C>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808static void NegotiateCodecs(const std::vector<C>& local_codecs,
809 const std::vector<C>& offered_codecs,
810 std::vector<C>* negotiated_codecs) {
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800811 for (const C& ours : local_codecs) {
812 C theirs;
813 if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) {
814 C negotiated = ours;
815 negotiated.IntersectFeedbackParams(theirs);
816 if (IsRtxCodec(negotiated)) {
817 std::string offered_apt_value;
818 theirs.GetParam(kCodecParamAssociatedPayloadType, &offered_apt_value);
819 // FindMatchingCodec shouldn't return something with no apt value.
820 RTC_DCHECK(!offered_apt_value.empty());
821 negotiated.SetParam(kCodecParamAssociatedPayloadType,
822 offered_apt_value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 }
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800824 negotiated.id = theirs.id;
825 // RFC3264: Although the answerer MAY list the formats in their desired
826 // order of preference, it is RECOMMENDED that unless there is a
827 // specific reason, the answerer list formats in the same relative order
828 // they were present in the offer.
829 negotiated.preference = theirs.preference;
830 negotiated_codecs->push_back(negotiated);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 }
832 }
833}
834
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800835// Finds a codec in |codecs2| that matches |codec_to_match|, which is
836// a member of |codecs1|. If |codec_to_match| is an RTX codec, both
837// the codecs themselves and their associated codecs must match.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838template <class C>
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800839static bool FindMatchingCodec(const std::vector<C>& codecs1,
840 const std::vector<C>& codecs2,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 const C& codec_to_match,
842 C* found_codec) {
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800843 for (const C& potential_match : codecs2) {
844 if (potential_match.Matches(codec_to_match)) {
845 if (IsRtxCodec(codec_to_match)) {
846 std::string apt_value_1;
847 std::string apt_value_2;
848 if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType,
849 &apt_value_1) ||
850 !potential_match.GetParam(kCodecParamAssociatedPayloadType,
851 &apt_value_2)) {
852 LOG(LS_WARNING) << "RTX missing associated payload type.";
853 continue;
854 }
855 if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2,
856 apt_value_2)) {
857 continue;
858 }
859 }
860 if (found_codec) {
861 *found_codec = potential_match;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 }
863 return true;
864 }
865 }
866 return false;
867}
868
869// Adds all codecs from |reference_codecs| to |offered_codecs| that dont'
870// already exist in |offered_codecs| and ensure the payload types don't
871// collide.
872template <class C>
873static void FindCodecsToOffer(
874 const std::vector<C>& reference_codecs,
875 std::vector<C>* offered_codecs,
876 UsedPayloadTypes* used_pltypes) {
877
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 // Add all new codecs that are not RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800879 for (const C& reference_codec : reference_codecs) {
880 if (!IsRtxCodec(reference_codec) &&
881 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
882 reference_codec, nullptr)) {
883 C codec = reference_codec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 used_pltypes->FindAndSetIdUsed(&codec);
885 offered_codecs->push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 }
887 }
888
889 // Add all new RTX codecs.
Taylor Brandstetter6ec641b2016-03-04 16:47:56 -0800890 for (const C& reference_codec : reference_codecs) {
891 if (IsRtxCodec(reference_codec) &&
892 !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
893 reference_codec, nullptr)) {
894 C rtx_codec = reference_codec;
895
896 std::string associated_pt_str;
897 if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
898 &associated_pt_str)) {
899 LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
900 << " is missing an associated payload type.";
901 continue;
902 }
903
904 int associated_pt;
905 if (!rtc::FromString(associated_pt_str, &associated_pt)) {
906 LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
907 << " of RTX codec " << rtx_codec.name
908 << " to an integer.";
909 continue;
910 }
911
912 // Find the associated reference codec for the reference RTX codec.
913 C associated_codec;
914 if (!FindCodecById(reference_codecs, associated_pt, &associated_codec)) {
915 LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
916 << associated_pt << " for RTX codec " << rtx_codec.name
917 << ".";
918 continue;
919 }
920
921 // Find a codec in the offered list that matches the reference codec.
922 // Its payload type may be different than the reference codec.
923 C matching_codec;
924 if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
925 associated_codec, &matching_codec)) {
926 LOG(LS_WARNING) << "Couldn't find matching " << associated_codec.name
927 << " codec.";
928 continue;
929 }
930
931 rtx_codec.params[kCodecParamAssociatedPayloadType] =
932 rtc::ToString(matching_codec.id);
933 used_pltypes->FindAndSetIdUsed(&rtx_codec);
934 offered_codecs->push_back(rtx_codec);
935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 }
937}
938
939
940static bool FindByUri(const RtpHeaderExtensions& extensions,
941 const RtpHeaderExtension& ext_to_match,
942 RtpHeaderExtension* found_extension) {
943 for (RtpHeaderExtensions::const_iterator it = extensions.begin();
944 it != extensions.end(); ++it) {
945 // We assume that all URIs are given in a canonical format.
946 if (it->uri == ext_to_match.uri) {
947 if (found_extension != NULL) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000948 *found_extension = *it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 }
950 return true;
951 }
952 }
953 return false;
954}
955
deadbeefa5b273a2015-08-20 17:30:13 -0700956// Iterates through |offered_extensions|, adding each one to |all_extensions|
957// and |used_ids|, and resolving ID conflicts. If an offered extension has the
958// same URI as one in |all_extensions|, it will re-use the same ID and won't be
959// treated as a conflict.
960static void FindAndSetRtpHdrExtUsed(RtpHeaderExtensions* offered_extensions,
961 RtpHeaderExtensions* all_extensions,
962 UsedRtpHeaderExtensionIds* used_ids) {
963 for (auto& extension : *offered_extensions) {
964 RtpHeaderExtension existing;
965 if (FindByUri(*all_extensions, extension, &existing)) {
966 extension.id = existing.id;
967 } else {
968 used_ids->FindAndSetIdUsed(&extension);
969 all_extensions->push_back(extension);
970 }
971 }
972}
973
974// Adds |reference_extensions| to |offered_extensions|, while updating
975// |all_extensions| and |used_ids|.
976static void FindRtpHdrExtsToOffer(
977 const RtpHeaderExtensions& reference_extensions,
978 RtpHeaderExtensions* offered_extensions,
979 RtpHeaderExtensions* all_extensions,
980 UsedRtpHeaderExtensionIds* used_ids) {
981 for (auto reference_extension : reference_extensions) {
982 if (!FindByUri(*offered_extensions, reference_extension, NULL)) {
983 RtpHeaderExtension existing;
984 if (FindByUri(*all_extensions, reference_extension, &existing)) {
985 offered_extensions->push_back(existing);
986 } else {
987 used_ids->FindAndSetIdUsed(&reference_extension);
988 all_extensions->push_back(reference_extension);
989 offered_extensions->push_back(reference_extension);
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000990 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 }
992 }
993}
994
995static void NegotiateRtpHeaderExtensions(
996 const RtpHeaderExtensions& local_extensions,
997 const RtpHeaderExtensions& offered_extensions,
998 RtpHeaderExtensions* negotiated_extenstions) {
999 RtpHeaderExtensions::const_iterator ours;
1000 for (ours = local_extensions.begin();
1001 ours != local_extensions.end(); ++ours) {
1002 RtpHeaderExtension theirs;
1003 if (FindByUri(offered_extensions, *ours, &theirs)) {
1004 // We respond with their RTP header extension id.
1005 negotiated_extenstions->push_back(theirs);
1006 }
1007 }
1008}
1009
1010static void StripCNCodecs(AudioCodecs* audio_codecs) {
1011 AudioCodecs::iterator iter = audio_codecs->begin();
1012 while (iter != audio_codecs->end()) {
1013 if (stricmp(iter->name.c_str(), kComfortNoiseCodecName) == 0) {
1014 iter = audio_codecs->erase(iter);
1015 } else {
1016 ++iter;
1017 }
1018 }
1019}
1020
1021// Create a media content to be answered in a session-accept,
1022// according to the given options.rtcp_mux, options.streams, codecs,
1023// crypto, and streams. If we don't currently have crypto (in
1024// current_cryptos) and it is enabled (in secure_policy), crypto is
1025// created (according to crypto_suites). If add_legacy_stream is
1026// true, and current_streams is empty, a legacy stream is created.
1027// The codecs, rtcp_mux, and crypto are all negotiated with the offer
1028// from the incoming session-initiate. If the negotiation fails, this
1029// method returns false. The created content is added to the offer.
1030template <class C>
1031static bool CreateMediaContentAnswer(
1032 const MediaContentDescriptionImpl<C>* offer,
1033 const MediaSessionOptions& options,
1034 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001035 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 const CryptoParamsVec* current_cryptos,
1037 const RtpHeaderExtensions& local_rtp_extenstions,
1038 StreamParamsVec* current_streams,
1039 bool add_legacy_stream,
1040 bool bundle_enabled,
1041 MediaContentDescriptionImpl<C>* answer) {
1042 std::vector<C> negotiated_codecs;
1043 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
1044 answer->AddCodecs(negotiated_codecs);
1045 answer->SortCodecs();
1046 answer->set_protocol(offer->protocol());
1047 RtpHeaderExtensions negotiated_rtp_extensions;
1048 NegotiateRtpHeaderExtensions(local_rtp_extenstions,
1049 offer->rtp_header_extensions(),
1050 &negotiated_rtp_extensions);
1051 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
1052
1053 answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux());
Taylor Brandstetter5f0b83b2016-03-18 15:02:07 -07001054 if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
1055 answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
1056 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057
1058 if (sdes_policy != SEC_DISABLED) {
1059 CryptoParams crypto;
1060 if (SelectCrypto(offer, bundle_enabled, &crypto)) {
1061 if (current_cryptos) {
1062 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
1063 }
1064 answer->AddCrypto(crypto);
1065 }
1066 }
1067
1068 if (answer->cryptos().empty() &&
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001069 (offer->crypto_required() == CT_SDES || sdes_policy == SEC_REQUIRED)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 return false;
1071 }
1072
1073 if (!AddStreamParams(
1074 answer->type(), options.streams, current_streams,
1075 answer, add_legacy_stream)) {
1076 return false; // Something went seriously wrong.
1077 }
1078
1079 // Make sure the answer media content direction is per default set as
1080 // described in RFC3264 section 6.1.
1081 switch (offer->direction()) {
1082 case MD_INACTIVE:
1083 answer->set_direction(MD_INACTIVE);
1084 break;
1085 case MD_SENDONLY:
1086 answer->set_direction(MD_RECVONLY);
1087 break;
1088 case MD_RECVONLY:
deadbeefb5cb19b2015-11-23 16:39:12 -08001089 answer->set_direction(IsRtpProtocol(answer->protocol()) &&
1090 answer->streams().empty()
1091 ? MD_INACTIVE
1092 : MD_SENDONLY);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 break;
1094 case MD_SENDRECV:
deadbeefb5cb19b2015-11-23 16:39:12 -08001095 answer->set_direction(IsRtpProtocol(answer->protocol()) &&
1096 answer->streams().empty()
1097 ? MD_RECVONLY
1098 : MD_SENDRECV);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 break;
1100 default:
deadbeefc80741f2015-10-22 13:14:45 -07001101 RTC_DCHECK(false && "MediaContentDescription has unexpected direction.");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 break;
1103 }
1104
1105 return true;
1106}
1107
1108static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001109 const std::string& protocol,
1110 bool secure_transport) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 // Data channels can have a protocol of SCTP or SCTP/DTLS.
1112 if (type == MEDIA_TYPE_DATA &&
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001113 ((protocol == kMediaProtocolSctp && !secure_transport)||
1114 (protocol == kMediaProtocolDtlsSctp && secure_transport))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 return true;
1116 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001117
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 // Since not all applications serialize and deserialize the media protocol,
1119 // we will have to accept |protocol| to be empty.
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001120 return protocol == kMediaProtocolAvpf || protocol.empty() ||
1121 protocol == kMediaProtocolSavpf ||
1122 (protocol == kMediaProtocolDtlsSavpf && secure_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123}
1124
1125static void SetMediaProtocol(bool secure_transport,
1126 MediaContentDescription* desc) {
deadbeeff3938292015-07-15 12:20:53 -07001127 if (!desc->cryptos().empty())
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 desc->set_protocol(kMediaProtocolSavpf);
deadbeeff3938292015-07-15 12:20:53 -07001129 else if (secure_transport)
1130 desc->set_protocol(kMediaProtocolDtlsSavpf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 else
1132 desc->set_protocol(kMediaProtocolAvpf);
1133}
1134
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001135// Gets the TransportInfo of the given |content_name| from the
1136// |current_description|. If doesn't exist, returns a new one.
1137static const TransportDescription* GetTransportDescription(
1138 const std::string& content_name,
1139 const SessionDescription* current_description) {
1140 const TransportDescription* desc = NULL;
1141 if (current_description) {
1142 const TransportInfo* info =
1143 current_description->GetTransportInfoByName(content_name);
1144 if (info) {
1145 desc = &info->description;
1146 }
1147 }
1148 return desc;
1149}
1150
1151// Gets the current DTLS state from the transport description.
1152static bool IsDtlsActive(
1153 const std::string& content_name,
1154 const SessionDescription* current_description) {
1155 if (!current_description)
1156 return false;
1157
1158 const ContentInfo* content =
1159 current_description->GetContentByName(content_name);
1160 if (!content)
1161 return false;
1162
1163 const TransportDescription* current_tdesc =
1164 GetTransportDescription(content_name, current_description);
1165 if (!current_tdesc)
1166 return false;
1167
1168 return current_tdesc->secure();
1169}
1170
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001171std::string MediaTypeToString(MediaType type) {
1172 std::string type_str;
1173 switch (type) {
1174 case MEDIA_TYPE_AUDIO:
1175 type_str = "audio";
1176 break;
1177 case MEDIA_TYPE_VIDEO:
1178 type_str = "video";
1179 break;
1180 case MEDIA_TYPE_DATA:
1181 type_str = "data";
1182 break;
1183 default:
1184 ASSERT(false);
1185 break;
1186 }
1187 return type_str;
1188}
1189
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001190void MediaSessionOptions::AddSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 const std::string& id,
1192 const std::string& sync_label) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001193 AddSendStreamInternal(type, id, sync_label, 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001194}
1195
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001196void MediaSessionOptions::AddSendVideoStream(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001197 const std::string& id,
1198 const std::string& sync_label,
1199 int num_sim_layers) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001200 AddSendStreamInternal(MEDIA_TYPE_VIDEO, id, sync_label, num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001201}
1202
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001203void MediaSessionOptions::AddSendStreamInternal(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001204 MediaType type,
1205 const std::string& id,
1206 const std::string& sync_label,
1207 int num_sim_layers) {
1208 streams.push_back(Stream(type, id, sync_label, num_sim_layers));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 // If we haven't already set the data_channel_type, and we add a
1211 // stream, we assume it's an RTP data stream.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001212 if (type == MEDIA_TYPE_DATA && data_channel_type == DCT_NONE)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 data_channel_type = DCT_RTP;
1214}
1215
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001216void MediaSessionOptions::RemoveSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 const std::string& id) {
1218 Streams::iterator stream_it = streams.begin();
1219 for (; stream_it != streams.end(); ++stream_it) {
1220 if (stream_it->type == type && stream_it->id == id) {
1221 streams.erase(stream_it);
1222 return;
1223 }
1224 }
1225 ASSERT(false);
1226}
1227
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001228bool MediaSessionOptions::HasSendMediaStream(MediaType type) const {
1229 Streams::const_iterator stream_it = streams.begin();
1230 for (; stream_it != streams.end(); ++stream_it) {
1231 if (stream_it->type == type) {
1232 return true;
1233 }
1234 }
1235 return false;
1236}
1237
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1239 const TransportDescriptionFactory* transport_desc_factory)
1240 : secure_(SEC_DISABLED),
1241 add_legacy_(true),
1242 transport_desc_factory_(transport_desc_factory) {
1243}
1244
1245MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1246 ChannelManager* channel_manager,
1247 const TransportDescriptionFactory* transport_desc_factory)
1248 : secure_(SEC_DISABLED),
1249 add_legacy_(true),
1250 transport_desc_factory_(transport_desc_factory) {
1251 channel_manager->GetSupportedAudioCodecs(&audio_codecs_);
1252 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
1253 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
1254 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1255 channel_manager->GetSupportedDataCodecs(&data_codecs_);
1256}
1257
1258SessionDescription* MediaSessionDescriptionFactory::CreateOffer(
1259 const MediaSessionOptions& options,
1260 const SessionDescription* current_description) const {
kwiberg31022942016-03-11 14:18:21 -08001261 std::unique_ptr<SessionDescription> offer(new SessionDescription());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262
1263 StreamParamsVec current_streams;
1264 GetCurrentStreamParams(current_description, &current_streams);
1265
1266 AudioCodecs audio_codecs;
1267 VideoCodecs video_codecs;
1268 DataCodecs data_codecs;
1269 GetCodecsToOffer(current_description, &audio_codecs, &video_codecs,
1270 &data_codecs);
1271
1272 if (!options.vad_enabled) {
1273 // If application doesn't want CN codecs in offer.
1274 StripCNCodecs(&audio_codecs);
1275 }
1276
1277 RtpHeaderExtensions audio_rtp_extensions;
1278 RtpHeaderExtensions video_rtp_extensions;
1279 GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions,
1280 &video_rtp_extensions);
1281
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001282 bool audio_added = false;
1283 bool video_added = false;
1284 bool data_added = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001285
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001286 // Iterate through the contents of |current_description| to maintain the order
1287 // of the m-lines in the new offer.
1288 if (current_description) {
1289 ContentInfos::const_iterator it = current_description->contents().begin();
1290 for (; it != current_description->contents().end(); ++it) {
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001291 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001292 if (!AddAudioContentForOffer(options, current_description,
1293 audio_rtp_extensions, audio_codecs,
1294 &current_streams, offer.get())) {
1295 return NULL;
1296 }
1297 audio_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001298 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001299 if (!AddVideoContentForOffer(options, current_description,
1300 video_rtp_extensions, video_codecs,
1301 &current_streams, offer.get())) {
1302 return NULL;
1303 }
1304 video_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001305 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_DATA)) {
tommi@webrtc.orgf15dee62014-10-27 22:15:04 +00001306 MediaSessionOptions options_copy(options);
1307 if (IsSctp(static_cast<const MediaContentDescription*>(
1308 it->description))) {
1309 options_copy.data_channel_type = DCT_SCTP;
1310 }
1311 if (!AddDataContentForOffer(options_copy, current_description,
1312 &data_codecs, &current_streams,
1313 offer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001314 return NULL;
1315 }
1316 data_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001317 } else {
1318 ASSERT(false);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001319 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 }
1321 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001322
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001323 // Append contents that are not in |current_description|.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001324 if (!audio_added && options.has_audio() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001325 !AddAudioContentForOffer(options, current_description,
1326 audio_rtp_extensions, audio_codecs,
1327 &current_streams, offer.get())) {
1328 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001330 if (!video_added && options.has_video() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001331 !AddVideoContentForOffer(options, current_description,
1332 video_rtp_extensions, video_codecs,
1333 &current_streams, offer.get())) {
1334 return NULL;
1335 }
1336 if (!data_added && options.has_data() &&
1337 !AddDataContentForOffer(options, current_description, &data_codecs,
1338 &current_streams, offer.get())) {
1339 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 }
1341
1342 // Bundle the contents together, if we've been asked to do so, and update any
1343 // parameters that need to be tweaked for BUNDLE.
1344 if (options.bundle_enabled) {
1345 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
1346 for (ContentInfos::const_iterator content = offer->contents().begin();
1347 content != offer->contents().end(); ++content) {
1348 offer_bundle.AddContentName(content->name);
1349 }
1350 offer->AddGroup(offer_bundle);
1351 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1352 LOG(LS_ERROR) << "CreateOffer failed to UpdateTransportInfoForBundle.";
1353 return NULL;
1354 }
1355 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1356 LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1357 return NULL;
1358 }
1359 }
1360
1361 return offer.release();
1362}
1363
1364SessionDescription* MediaSessionDescriptionFactory::CreateAnswer(
1365 const SessionDescription* offer, const MediaSessionOptions& options,
1366 const SessionDescription* current_description) const {
1367 // The answer contains the intersection of the codecs in the offer with the
1368 // codecs we support, ordered by our local preference. As indicated by
1369 // XEP-0167, we retain the same payload ids from the offer in the answer.
kwiberg31022942016-03-11 14:18:21 -08001370 std::unique_ptr<SessionDescription> answer(new SessionDescription());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371
1372 StreamParamsVec current_streams;
1373 GetCurrentStreamParams(current_description, &current_streams);
1374
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001375 if (offer) {
1376 ContentInfos::const_iterator it = offer->contents().begin();
1377 for (; it != offer->contents().end(); ++it) {
1378 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
1379 if (!AddAudioContentForAnswer(offer, options, current_description,
1380 &current_streams, answer.get())) {
1381 return NULL;
1382 }
1383 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
1384 if (!AddVideoContentForAnswer(offer, options, current_description,
1385 &current_streams, answer.get())) {
1386 return NULL;
1387 }
1388 } else {
1389 ASSERT(IsMediaContentOfType(&*it, MEDIA_TYPE_DATA));
1390 if (!AddDataContentForAnswer(offer, options, current_description,
1391 &current_streams, answer.get())) {
1392 return NULL;
1393 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 }
1397
1398 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1399 // group in the answer with the appropriate content names.
1400 if (offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled) {
1401 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1402 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1403 for (ContentInfos::const_iterator content = answer->contents().begin();
1404 content != answer->contents().end(); ++content) {
1405 if (!content->rejected && offer_bundle->HasContentName(content->name)) {
1406 answer_bundle.AddContentName(content->name);
1407 }
1408 }
1409 if (answer_bundle.FirstContentName()) {
1410 answer->AddGroup(answer_bundle);
1411
1412 // Share the same ICE credentials and crypto params across all contents,
1413 // as BUNDLE requires.
1414 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
1415 LOG(LS_ERROR) << "CreateAnswer failed to UpdateTransportInfoForBundle.";
1416 return NULL;
1417 }
1418
1419 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
1420 LOG(LS_ERROR) << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
1421 return NULL;
1422 }
1423 }
1424 }
1425
1426 return answer.release();
1427}
1428
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429void MediaSessionDescriptionFactory::GetCodecsToOffer(
1430 const SessionDescription* current_description,
1431 AudioCodecs* audio_codecs,
1432 VideoCodecs* video_codecs,
1433 DataCodecs* data_codecs) const {
1434 UsedPayloadTypes used_pltypes;
1435 audio_codecs->clear();
1436 video_codecs->clear();
1437 data_codecs->clear();
1438
1439
1440 // First - get all codecs from the current description if the media type
1441 // is used.
1442 // Add them to |used_pltypes| so the payloadtype is not reused if a new media
1443 // type is added.
1444 if (current_description) {
1445 const AudioContentDescription* audio =
1446 GetFirstAudioContentDescription(current_description);
1447 if (audio) {
1448 *audio_codecs = audio->codecs();
1449 used_pltypes.FindAndSetIdUsed<AudioCodec>(audio_codecs);
1450 }
1451 const VideoContentDescription* video =
1452 GetFirstVideoContentDescription(current_description);
1453 if (video) {
1454 *video_codecs = video->codecs();
1455 used_pltypes.FindAndSetIdUsed<VideoCodec>(video_codecs);
1456 }
1457 const DataContentDescription* data =
1458 GetFirstDataContentDescription(current_description);
1459 if (data) {
1460 *data_codecs = data->codecs();
1461 used_pltypes.FindAndSetIdUsed<DataCodec>(data_codecs);
1462 }
1463 }
1464
1465 // Add our codecs that are not in |current_description|.
1466 FindCodecsToOffer<AudioCodec>(audio_codecs_, audio_codecs, &used_pltypes);
1467 FindCodecsToOffer<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1468 FindCodecsToOffer<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1469}
1470
1471void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
1472 const SessionDescription* current_description,
1473 RtpHeaderExtensions* audio_extensions,
1474 RtpHeaderExtensions* video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001475 // All header extensions allocated from the same range to avoid potential
1476 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 UsedRtpHeaderExtensionIds used_ids;
deadbeefa5b273a2015-08-20 17:30:13 -07001478 RtpHeaderExtensions all_extensions;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 audio_extensions->clear();
1480 video_extensions->clear();
1481
1482 // First - get all extensions from the current description if the media type
1483 // is used.
1484 // Add them to |used_ids| so the local ids are not reused if a new media
1485 // type is added.
1486 if (current_description) {
1487 const AudioContentDescription* audio =
1488 GetFirstAudioContentDescription(current_description);
1489 if (audio) {
1490 *audio_extensions = audio->rtp_header_extensions();
deadbeefa5b273a2015-08-20 17:30:13 -07001491 FindAndSetRtpHdrExtUsed(audio_extensions, &all_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 }
1493 const VideoContentDescription* video =
1494 GetFirstVideoContentDescription(current_description);
1495 if (video) {
1496 *video_extensions = video->rtp_header_extensions();
deadbeefa5b273a2015-08-20 17:30:13 -07001497 FindAndSetRtpHdrExtUsed(video_extensions, &all_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 }
1499 }
1500
1501 // Add our default RTP header extensions that are not in
1502 // |current_description|.
deadbeefa5b273a2015-08-20 17:30:13 -07001503 FindRtpHdrExtsToOffer(audio_rtp_header_extensions(), audio_extensions,
1504 &all_extensions, &used_ids);
1505 FindRtpHdrExtsToOffer(video_rtp_header_extensions(), video_extensions,
1506 &all_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507}
1508
1509bool MediaSessionDescriptionFactory::AddTransportOffer(
1510 const std::string& content_name,
1511 const TransportOptions& transport_options,
1512 const SessionDescription* current_desc,
1513 SessionDescription* offer_desc) const {
1514 if (!transport_desc_factory_)
1515 return false;
1516 const TransportDescription* current_tdesc =
1517 GetTransportDescription(content_name, current_desc);
kwiberg31022942016-03-11 14:18:21 -08001518 std::unique_ptr<TransportDescription> new_tdesc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 transport_desc_factory_->CreateOffer(transport_options, current_tdesc));
1520 bool ret = (new_tdesc.get() != NULL &&
1521 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)));
1522 if (!ret) {
1523 LOG(LS_ERROR)
1524 << "Failed to AddTransportOffer, content name=" << content_name;
1525 }
1526 return ret;
1527}
1528
1529TransportDescription* MediaSessionDescriptionFactory::CreateTransportAnswer(
1530 const std::string& content_name,
1531 const SessionDescription* offer_desc,
1532 const TransportOptions& transport_options,
1533 const SessionDescription* current_desc) const {
1534 if (!transport_desc_factory_)
1535 return NULL;
1536 const TransportDescription* offer_tdesc =
1537 GetTransportDescription(content_name, offer_desc);
1538 const TransportDescription* current_tdesc =
1539 GetTransportDescription(content_name, current_desc);
1540 return
1541 transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1542 current_tdesc);
1543}
1544
1545bool MediaSessionDescriptionFactory::AddTransportAnswer(
1546 const std::string& content_name,
1547 const TransportDescription& transport_desc,
1548 SessionDescription* answer_desc) const {
1549 if (!answer_desc->AddTransportInfo(TransportInfo(content_name,
1550 transport_desc))) {
1551 LOG(LS_ERROR)
1552 << "Failed to AddTransportAnswer, content name=" << content_name;
1553 return false;
1554 }
1555 return true;
1556}
1557
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001558bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
1559 const MediaSessionOptions& options,
1560 const SessionDescription* current_description,
1561 const RtpHeaderExtensions& audio_rtp_extensions,
1562 const AudioCodecs& audio_codecs,
1563 StreamParamsVec* current_streams,
1564 SessionDescription* desc) const {
deadbeef44f08192015-12-15 16:20:09 -08001565 const ContentInfo* current_audio_content =
1566 GetFirstAudioContent(current_description);
1567 std::string content_name =
1568 current_audio_content ? current_audio_content->name : CN_AUDIO;
1569
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001570 cricket::SecurePolicy sdes_policy =
deadbeef44f08192015-12-15 16:20:09 -08001571 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED
1572 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001573
kwiberg31022942016-03-11 14:18:21 -08001574 std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001575 std::vector<std::string> crypto_suites;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001576 GetSupportedAudioCryptoSuiteNames(&crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001577 if (!CreateMediaContentOffer(
1578 options,
1579 audio_codecs,
1580 sdes_policy,
1581 GetCryptos(GetFirstAudioContentDescription(current_description)),
1582 crypto_suites,
1583 audio_rtp_extensions,
1584 add_legacy_,
1585 current_streams,
1586 audio.get())) {
1587 return false;
1588 }
1589 audio->set_lang(lang_);
1590
1591 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1592 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001593
deadbeefc80741f2015-10-22 13:14:45 -07001594 if (!audio->streams().empty()) {
1595 if (options.recv_audio) {
1596 audio->set_direction(MD_SENDRECV);
1597 } else {
1598 audio->set_direction(MD_SENDONLY);
1599 }
1600 } else {
1601 if (options.recv_audio) {
1602 audio->set_direction(MD_RECVONLY);
1603 } else {
1604 audio->set_direction(MD_INACTIVE);
1605 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001606 }
1607
deadbeef44f08192015-12-15 16:20:09 -08001608 desc->AddContent(content_name, NS_JINGLE_RTP, audio.release());
deadbeef0ed85b22016-02-23 17:24:52 -08001609 if (!AddTransportOffer(content_name,
1610 GetTransportOptions(options, content_name),
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001611 current_description, desc)) {
1612 return false;
1613 }
1614
1615 return true;
1616}
1617
1618bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
1619 const MediaSessionOptions& options,
1620 const SessionDescription* current_description,
1621 const RtpHeaderExtensions& video_rtp_extensions,
1622 const VideoCodecs& video_codecs,
1623 StreamParamsVec* current_streams,
1624 SessionDescription* desc) const {
deadbeef44f08192015-12-15 16:20:09 -08001625 const ContentInfo* current_video_content =
1626 GetFirstVideoContent(current_description);
1627 std::string content_name =
1628 current_video_content ? current_video_content->name : CN_VIDEO;
1629
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001630 cricket::SecurePolicy sdes_policy =
deadbeef44f08192015-12-15 16:20:09 -08001631 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED
1632 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001633
kwiberg31022942016-03-11 14:18:21 -08001634 std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001635 std::vector<std::string> crypto_suites;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001636 GetSupportedVideoCryptoSuiteNames(&crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001637 if (!CreateMediaContentOffer(
1638 options,
1639 video_codecs,
1640 sdes_policy,
1641 GetCryptos(GetFirstVideoContentDescription(current_description)),
1642 crypto_suites,
1643 video_rtp_extensions,
1644 add_legacy_,
1645 current_streams,
1646 video.get())) {
1647 return false;
1648 }
1649
1650 video->set_bandwidth(options.video_bandwidth);
1651
1652 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1653 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001654
deadbeefc80741f2015-10-22 13:14:45 -07001655 if (!video->streams().empty()) {
1656 if (options.recv_video) {
1657 video->set_direction(MD_SENDRECV);
1658 } else {
1659 video->set_direction(MD_SENDONLY);
1660 }
1661 } else {
1662 if (options.recv_video) {
1663 video->set_direction(MD_RECVONLY);
1664 } else {
1665 video->set_direction(MD_INACTIVE);
1666 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001667 }
1668
deadbeef44f08192015-12-15 16:20:09 -08001669 desc->AddContent(content_name, NS_JINGLE_RTP, video.release());
deadbeef0ed85b22016-02-23 17:24:52 -08001670 if (!AddTransportOffer(content_name,
1671 GetTransportOptions(options, content_name),
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001672 current_description, desc)) {
1673 return false;
1674 }
1675
1676 return true;
1677}
1678
1679bool MediaSessionDescriptionFactory::AddDataContentForOffer(
1680 const MediaSessionOptions& options,
1681 const SessionDescription* current_description,
1682 DataCodecs* data_codecs,
1683 StreamParamsVec* current_streams,
1684 SessionDescription* desc) const {
1685 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1686
kwiberg31022942016-03-11 14:18:21 -08001687 std::unique_ptr<DataContentDescription> data(new DataContentDescription());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001688 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1689
1690 FilterDataCodecs(data_codecs, is_sctp);
1691
deadbeef44f08192015-12-15 16:20:09 -08001692 const ContentInfo* current_data_content =
1693 GetFirstDataContent(current_description);
1694 std::string content_name =
1695 current_data_content ? current_data_content->name : CN_DATA;
1696
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001697 cricket::SecurePolicy sdes_policy =
deadbeef44f08192015-12-15 16:20:09 -08001698 IsDtlsActive(content_name, current_description) ? cricket::SEC_DISABLED
1699 : secure();
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001700 std::vector<std::string> crypto_suites;
1701 if (is_sctp) {
1702 // SDES doesn't make sense for SCTP, so we disable it, and we only
1703 // get SDES crypto suites for RTP-based data channels.
1704 sdes_policy = cricket::SEC_DISABLED;
1705 // Unlike SetMediaProtocol below, we need to set the protocol
1706 // before we call CreateMediaContentOffer. Otherwise,
1707 // CreateMediaContentOffer won't know this is SCTP and will
1708 // generate SSRCs rather than SIDs.
1709 data->set_protocol(
1710 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp);
1711 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001712 GetSupportedDataCryptoSuiteNames(&crypto_suites);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001713 }
1714
1715 if (!CreateMediaContentOffer(
1716 options,
1717 *data_codecs,
1718 sdes_policy,
1719 GetCryptos(GetFirstDataContentDescription(current_description)),
1720 crypto_suites,
1721 RtpHeaderExtensions(),
1722 add_legacy_,
1723 current_streams,
1724 data.get())) {
1725 return false;
1726 }
1727
1728 if (is_sctp) {
deadbeef44f08192015-12-15 16:20:09 -08001729 desc->AddContent(content_name, NS_JINGLE_DRAFT_SCTP, data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001730 } else {
1731 data->set_bandwidth(options.data_bandwidth);
1732 SetMediaProtocol(secure_transport, data.get());
deadbeef44f08192015-12-15 16:20:09 -08001733 desc->AddContent(content_name, NS_JINGLE_RTP, data.release());
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001734 }
deadbeef0ed85b22016-02-23 17:24:52 -08001735 if (!AddTransportOffer(content_name,
1736 GetTransportOptions(options, content_name),
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001737 current_description, desc)) {
1738 return false;
1739 }
1740 return true;
1741}
1742
1743bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
1744 const SessionDescription* offer,
1745 const MediaSessionOptions& options,
1746 const SessionDescription* current_description,
1747 StreamParamsVec* current_streams,
1748 SessionDescription* answer) const {
1749 const ContentInfo* audio_content = GetFirstAudioContent(offer);
1750
kwiberg31022942016-03-11 14:18:21 -08001751 std::unique_ptr<TransportDescription> audio_transport(CreateTransportAnswer(
deadbeef0ed85b22016-02-23 17:24:52 -08001752 audio_content->name, offer,
1753 GetTransportOptions(options, audio_content->name), current_description));
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001754 if (!audio_transport) {
1755 return false;
1756 }
1757
1758 AudioCodecs audio_codecs = audio_codecs_;
1759 if (!options.vad_enabled) {
1760 StripCNCodecs(&audio_codecs);
1761 }
1762
1763 bool bundle_enabled =
1764 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
kwiberg31022942016-03-11 14:18:21 -08001765 std::unique_ptr<AudioContentDescription> audio_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001766 new AudioContentDescription());
1767 // Do not require or create SDES cryptos if DTLS is used.
1768 cricket::SecurePolicy sdes_policy =
1769 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
1770 if (!CreateMediaContentAnswer(
1771 static_cast<const AudioContentDescription*>(
1772 audio_content->description),
1773 options,
1774 audio_codecs,
1775 sdes_policy,
1776 GetCryptos(GetFirstAudioContentDescription(current_description)),
1777 audio_rtp_extensions_,
1778 current_streams,
1779 add_legacy_,
1780 bundle_enabled,
1781 audio_answer.get())) {
1782 return false; // Fails the session setup.
1783 }
1784
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001785 bool rejected = !options.has_audio() || audio_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001786 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
1787 audio_answer->protocol(),
1788 audio_transport->secure());
1789 if (!rejected) {
1790 AddTransportAnswer(audio_content->name, *(audio_transport.get()), answer);
1791 } else {
1792 // RFC 3264
1793 // The answer MUST contain the same number of m-lines as the offer.
1794 LOG(LS_INFO) << "Audio is not supported in the answer.";
1795 }
1796
1797 answer->AddContent(audio_content->name, audio_content->type, rejected,
1798 audio_answer.release());
1799 return true;
1800}
1801
1802bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
1803 const SessionDescription* offer,
1804 const MediaSessionOptions& options,
1805 const SessionDescription* current_description,
1806 StreamParamsVec* current_streams,
1807 SessionDescription* answer) const {
1808 const ContentInfo* video_content = GetFirstVideoContent(offer);
kwiberg31022942016-03-11 14:18:21 -08001809 std::unique_ptr<TransportDescription> video_transport(CreateTransportAnswer(
deadbeef0ed85b22016-02-23 17:24:52 -08001810 video_content->name, offer,
1811 GetTransportOptions(options, video_content->name), current_description));
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001812 if (!video_transport) {
1813 return false;
1814 }
1815
kwiberg31022942016-03-11 14:18:21 -08001816 std::unique_ptr<VideoContentDescription> video_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001817 new VideoContentDescription());
1818 // Do not require or create SDES cryptos if DTLS is used.
1819 cricket::SecurePolicy sdes_policy =
1820 video_transport->secure() ? cricket::SEC_DISABLED : secure();
1821 bool bundle_enabled =
1822 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1823 if (!CreateMediaContentAnswer(
1824 static_cast<const VideoContentDescription*>(
1825 video_content->description),
1826 options,
1827 video_codecs_,
1828 sdes_policy,
1829 GetCryptos(GetFirstVideoContentDescription(current_description)),
1830 video_rtp_extensions_,
1831 current_streams,
1832 add_legacy_,
1833 bundle_enabled,
1834 video_answer.get())) {
1835 return false;
1836 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001837 bool rejected = !options.has_video() || video_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001838 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
1839 video_answer->protocol(),
1840 video_transport->secure());
1841 if (!rejected) {
1842 if (!AddTransportAnswer(video_content->name, *(video_transport.get()),
1843 answer)) {
1844 return false;
1845 }
1846 video_answer->set_bandwidth(options.video_bandwidth);
1847 } else {
1848 // RFC 3264
1849 // The answer MUST contain the same number of m-lines as the offer.
1850 LOG(LS_INFO) << "Video is not supported in the answer.";
1851 }
1852 answer->AddContent(video_content->name, video_content->type, rejected,
1853 video_answer.release());
1854 return true;
1855}
1856
1857bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
1858 const SessionDescription* offer,
1859 const MediaSessionOptions& options,
1860 const SessionDescription* current_description,
1861 StreamParamsVec* current_streams,
1862 SessionDescription* answer) const {
1863 const ContentInfo* data_content = GetFirstDataContent(offer);
kwiberg31022942016-03-11 14:18:21 -08001864 std::unique_ptr<TransportDescription> data_transport(CreateTransportAnswer(
deadbeef0ed85b22016-02-23 17:24:52 -08001865 data_content->name, offer,
1866 GetTransportOptions(options, data_content->name), current_description));
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001867 if (!data_transport) {
1868 return false;
1869 }
1870 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1871 std::vector<DataCodec> data_codecs(data_codecs_);
1872 FilterDataCodecs(&data_codecs, is_sctp);
1873
kwiberg31022942016-03-11 14:18:21 -08001874 std::unique_ptr<DataContentDescription> data_answer(
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001875 new DataContentDescription());
1876 // Do not require or create SDES cryptos if DTLS is used.
1877 cricket::SecurePolicy sdes_policy =
1878 data_transport->secure() ? cricket::SEC_DISABLED : secure();
1879 bool bundle_enabled =
1880 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1881 if (!CreateMediaContentAnswer(
1882 static_cast<const DataContentDescription*>(
1883 data_content->description),
1884 options,
1885 data_codecs_,
1886 sdes_policy,
1887 GetCryptos(GetFirstDataContentDescription(current_description)),
1888 RtpHeaderExtensions(),
1889 current_streams,
1890 add_legacy_,
1891 bundle_enabled,
1892 data_answer.get())) {
1893 return false; // Fails the session setup.
1894 }
1895
1896 bool rejected = !options.has_data() || data_content->rejected ||
1897 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
1898 data_answer->protocol(),
1899 data_transport->secure());
1900 if (!rejected) {
1901 data_answer->set_bandwidth(options.data_bandwidth);
1902 if (!AddTransportAnswer(data_content->name, *(data_transport.get()),
1903 answer)) {
1904 return false;
1905 }
1906 } else {
1907 // RFC 3264
1908 // The answer MUST contain the same number of m-lines as the offer.
1909 LOG(LS_INFO) << "Data is not supported in the answer.";
1910 }
1911 answer->AddContent(data_content->name, data_content->type, rejected,
1912 data_answer.release());
1913 return true;
1914}
1915
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916bool IsMediaContent(const ContentInfo* content) {
1917 return (content &&
1918 (content->type == NS_JINGLE_RTP ||
1919 content->type == NS_JINGLE_DRAFT_SCTP));
1920}
1921
1922bool IsAudioContent(const ContentInfo* content) {
1923 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
1924}
1925
1926bool IsVideoContent(const ContentInfo* content) {
1927 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
1928}
1929
1930bool IsDataContent(const ContentInfo* content) {
1931 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
1932}
1933
deadbeef0ed85b22016-02-23 17:24:52 -08001934const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
1935 MediaType media_type) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 for (ContentInfos::const_iterator content = contents.begin();
1937 content != contents.end(); content++) {
1938 if (IsMediaContentOfType(&*content, media_type)) {
1939 return &*content;
1940 }
1941 }
deadbeef0ed85b22016-02-23 17:24:52 -08001942 return nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943}
1944
1945const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
1946 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
1947}
1948
1949const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
1950 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
1951}
1952
1953const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
1954 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
1955}
1956
1957static const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
1958 MediaType media_type) {
deadbeef0ed85b22016-02-23 17:24:52 -08001959 if (sdesc == nullptr) {
1960 return nullptr;
1961 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962
1963 return GetFirstMediaContent(sdesc->contents(), media_type);
1964}
1965
1966const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
1967 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
1968}
1969
1970const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
1971 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
1972}
1973
1974const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
1975 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
1976}
1977
1978const MediaContentDescription* GetFirstMediaContentDescription(
1979 const SessionDescription* sdesc, MediaType media_type) {
1980 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
1981 const ContentDescription* description = content ? content->description : NULL;
1982 return static_cast<const MediaContentDescription*>(description);
1983}
1984
1985const AudioContentDescription* GetFirstAudioContentDescription(
1986 const SessionDescription* sdesc) {
1987 return static_cast<const AudioContentDescription*>(
1988 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
1989}
1990
1991const VideoContentDescription* GetFirstVideoContentDescription(
1992 const SessionDescription* sdesc) {
1993 return static_cast<const VideoContentDescription*>(
1994 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
1995}
1996
1997const DataContentDescription* GetFirstDataContentDescription(
1998 const SessionDescription* sdesc) {
1999 return static_cast<const DataContentDescription*>(
2000 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
2001}
2002
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003} // namespace cricket