blob: 4f7d4b2ab00121899bdfce72263d4ffcbacf7715 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/session/media/mediasession.h"
29
30#include <functional>
31#include <map>
32#include <set>
33#include <utility>
34
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/media/base/constants.h"
36#include "talk/media/base/cryptoparams.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/session/media/channelmanager.h"
38#include "talk/session/media/srtpfilter.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000039#include "webrtc/base/helpers.h"
40#include "webrtc/base/logging.h"
41#include "webrtc/base/scoped_ptr.h"
42#include "webrtc/base/stringutils.h"
pthatcher@webrtc.org5ad41782014-12-23 22:14:15 +000043#include "webrtc/p2p/base/constants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000045#ifdef HAVE_SCTP
46#include "talk/media/sctp/sctpdataengine.h"
47#else
wu@webrtc.org97077a32013-10-25 21:18:33 +000048static const uint32 kMaxSctpSid = 1023;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000049#endif
50
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051namespace {
52const char kInline[] = "inline:";
53}
54
55namespace cricket {
56
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000057using rtc::scoped_ptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
59// RTP Profile names
60// http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
61// RFC4585
62const char kMediaProtocolAvpf[] = "RTP/AVPF";
63// RFC5124
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +000064const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
65
66// This should be replaced by "UDP/TLS/RTP/SAVPF", but we need to support it for
67// now to be compatible with previous Chrome versions.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068const char kMediaProtocolSavpf[] = "RTP/SAVPF";
69
70const char kMediaProtocolRtpPrefix[] = "RTP/";
71
72const char kMediaProtocolSctp[] = "SCTP";
73const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP";
74
75static bool IsMediaContentOfType(const ContentInfo* content,
76 MediaType media_type) {
77 if (!IsMediaContent(content)) {
78 return false;
79 }
80
81 const MediaContentDescription* mdesc =
82 static_cast<const MediaContentDescription*>(content->description);
83 return mdesc && mdesc->type() == media_type;
84}
85
86static bool CreateCryptoParams(int tag, const std::string& cipher,
87 CryptoParams *out) {
88 std::string key;
89 key.reserve(SRTP_MASTER_KEY_BASE64_LEN);
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 if (!rtc::CreateRandomString(SRTP_MASTER_KEY_BASE64_LEN, &key)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 return false;
93 }
94 out->tag = tag;
95 out->cipher_suite = cipher;
96 out->key_params = kInline;
97 out->key_params += key;
98 return true;
99}
100
101#ifdef HAVE_SRTP
102static bool AddCryptoParams(const std::string& cipher_suite,
103 CryptoParamsVec *out) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000104 int size = static_cast<int>(out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 out->resize(size + 1);
107 return CreateCryptoParams(size, cipher_suite, &out->at(size));
108}
109
110void AddMediaCryptos(const CryptoParamsVec& cryptos,
111 MediaContentDescription* media) {
112 for (CryptoParamsVec::const_iterator crypto = cryptos.begin();
113 crypto != cryptos.end(); ++crypto) {
114 media->AddCrypto(*crypto);
115 }
116}
117
118bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
119 MediaContentDescription* media) {
120 CryptoParamsVec cryptos;
121 for (std::vector<std::string>::const_iterator it = crypto_suites.begin();
122 it != crypto_suites.end(); ++it) {
123 if (!AddCryptoParams(*it, &cryptos)) {
124 return false;
125 }
126 }
127 AddMediaCryptos(cryptos, media);
128 return true;
129}
130#endif
131
132const CryptoParamsVec* GetCryptos(const MediaContentDescription* media) {
133 if (!media) {
134 return NULL;
135 }
136 return &media->cryptos();
137}
138
139bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
140 const CryptoParams& crypto,
141 CryptoParams* out) {
142 for (CryptoParamsVec::const_iterator it = cryptos.begin();
143 it != cryptos.end(); ++it) {
144 if (crypto.Matches(*it)) {
145 *out = *it;
146 return true;
147 }
148 }
149 return false;
150}
151
152// For audio, HMAC 32 is prefered because of the low overhead.
153void GetSupportedAudioCryptoSuites(
154 std::vector<std::string>* crypto_suites) {
155#ifdef HAVE_SRTP
156 crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_32);
157 crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_80);
158#endif
159}
160
161void GetSupportedVideoCryptoSuites(
162 std::vector<std::string>* crypto_suites) {
163 GetSupportedDefaultCryptoSuites(crypto_suites);
164}
165
166void GetSupportedDataCryptoSuites(
167 std::vector<std::string>* crypto_suites) {
168 GetSupportedDefaultCryptoSuites(crypto_suites);
169}
170
171void GetSupportedDefaultCryptoSuites(
172 std::vector<std::string>* crypto_suites) {
173#ifdef HAVE_SRTP
174 crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_80);
175#endif
176}
177
178// For video support only 80-bit SHA1 HMAC. For audio 32-bit HMAC is
179// tolerated unless bundle is enabled because it is low overhead. Pick the
180// crypto in the list that is supported.
181static bool SelectCrypto(const MediaContentDescription* offer,
182 bool bundle,
183 CryptoParams *crypto) {
184 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
185 const CryptoParamsVec& cryptos = offer->cryptos();
186
187 for (CryptoParamsVec::const_iterator i = cryptos.begin();
188 i != cryptos.end(); ++i) {
189 if (CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite ||
190 (CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio && !bundle)) {
191 return CreateCryptoParams(i->tag, i->cipher_suite, crypto);
192 }
193 }
194 return false;
195}
196
197static const StreamParams* FindFirstStreamParamsByCname(
198 const StreamParamsVec& params_vec,
199 const std::string& cname) {
200 for (StreamParamsVec::const_iterator it = params_vec.begin();
201 it != params_vec.end(); ++it) {
202 if (cname == it->cname)
203 return &*it;
204 }
205 return NULL;
206}
207
208// Generates a new CNAME or the CNAME of an already existing StreamParams
209// if a StreamParams exist for another Stream in streams with sync_label
210// sync_label.
211static bool GenerateCname(const StreamParamsVec& params_vec,
212 const MediaSessionOptions::Streams& streams,
213 const std::string& synch_label,
214 std::string* cname) {
215 ASSERT(cname != NULL);
216 if (!cname)
217 return false;
218
219 // Check if a CNAME exist for any of the other synched streams.
220 for (MediaSessionOptions::Streams::const_iterator stream_it = streams.begin();
221 stream_it != streams.end() ; ++stream_it) {
222 if (synch_label != stream_it->sync_label)
223 continue;
224
225 StreamParams param;
226 // groupid is empty for StreamParams generated using
227 // MediaSessionDescriptionFactory.
228 if (GetStreamByIds(params_vec, "", stream_it->id,
229 &param)) {
230 *cname = param.cname;
231 return true;
232 }
233 }
234 // No other stream seems to exist that we should sync with.
235 // Generate a random string for the RTCP CNAME, as stated in RFC 6222.
236 // This string is only used for synchronization, and therefore is opaque.
237 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000238 if (!rtc::CreateRandomString(16, cname)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239 ASSERT(false);
240 return false;
241 }
242 } while (FindFirstStreamParamsByCname(params_vec, *cname));
243
244 return true;
245}
246
247// Generate random SSRC values that are not already present in |params_vec|.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000248// The generated values are added to |ssrcs|.
249// |num_ssrcs| is the number of the SSRC will be generated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250static void GenerateSsrcs(const StreamParamsVec& params_vec,
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000251 int num_ssrcs,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 std::vector<uint32>* ssrcs) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000253 for (int i = 0; i < num_ssrcs; i++) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254 uint32 candidate;
255 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000256 candidate = rtc::CreateRandomNonZeroId();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 } while (GetStreamBySsrc(params_vec, candidate, NULL) ||
258 std::count(ssrcs->begin(), ssrcs->end(), candidate) > 0);
259 ssrcs->push_back(candidate);
260 }
261}
262
263// Returns false if we exhaust the range of SIDs.
264static bool GenerateSctpSid(const StreamParamsVec& params_vec,
265 uint32* sid) {
266 if (params_vec.size() > kMaxSctpSid) {
267 LOG(LS_WARNING) <<
268 "Could not generate an SCTP SID: too many SCTP streams.";
269 return false;
270 }
271 while (true) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000272 uint32 candidate = rtc::CreateRandomNonZeroId() % kMaxSctpSid;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 if (!GetStreamBySsrc(params_vec, candidate, NULL)) {
274 *sid = candidate;
275 return true;
276 }
277 }
278}
279
280static bool GenerateSctpSids(const StreamParamsVec& params_vec,
281 std::vector<uint32>* sids) {
282 uint32 sid;
283 if (!GenerateSctpSid(params_vec, &sid)) {
284 LOG(LS_WARNING) << "Could not generated an SCTP SID.";
285 return false;
286 }
287 sids->push_back(sid);
288 return true;
289}
290
291// Finds all StreamParams of all media types and attach them to stream_params.
292static void GetCurrentStreamParams(const SessionDescription* sdesc,
293 StreamParamsVec* stream_params) {
294 if (!sdesc)
295 return;
296
297 const ContentInfos& contents = sdesc->contents();
298 for (ContentInfos::const_iterator content = contents.begin();
299 content != contents.end(); ++content) {
300 if (!IsMediaContent(&*content)) {
301 continue;
302 }
303 const MediaContentDescription* media =
304 static_cast<const MediaContentDescription*>(
305 content->description);
306 const StreamParamsVec& streams = media->streams();
307 for (StreamParamsVec::const_iterator it = streams.begin();
308 it != streams.end(); ++it) {
309 stream_params->push_back(*it);
310 }
311 }
312}
313
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000314// Filters the data codecs for the data channel type.
315void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
316 // Filter RTP codec for SCTP and vice versa.
317 int codec_id = sctp ? kGoogleRtpDataCodecId : kGoogleSctpDataCodecId;
318 for (std::vector<DataCodec>::iterator iter = codecs->begin();
319 iter != codecs->end();) {
320 if (iter->id == codec_id) {
321 iter = codecs->erase(iter);
322 } else {
323 ++iter;
324 }
325 }
326}
327
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328template <typename IdStruct>
329class UsedIds {
330 public:
331 UsedIds(int min_allowed_id, int max_allowed_id)
332 : min_allowed_id_(min_allowed_id),
333 max_allowed_id_(max_allowed_id),
334 next_id_(max_allowed_id) {
335 }
336
337 // Loops through all Id in |ids| and changes its id if it is
338 // already in use by another IdStruct. Call this methods with all Id
339 // in a session description to make sure no duplicate ids exists.
340 // Note that typename Id must be a type of IdStruct.
341 template <typename Id>
342 void FindAndSetIdUsed(std::vector<Id>* ids) {
343 for (typename std::vector<Id>::iterator it = ids->begin();
344 it != ids->end(); ++it) {
345 FindAndSetIdUsed(&*it);
346 }
347 }
348
349 // Finds and sets an unused id if the |idstruct| id is already in use.
350 void FindAndSetIdUsed(IdStruct* idstruct) {
351 const int original_id = idstruct->id;
352 int new_id = idstruct->id;
353
354 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
355 // If the original id is not in range - this is an id that can't be
356 // dynamically changed.
357 return;
358 }
359
360 if (IsIdUsed(original_id)) {
361 new_id = FindUnusedId();
362 LOG(LS_WARNING) << "Duplicate id found. Reassigning from " << original_id
363 << " to " << new_id;
364 idstruct->id = new_id;
365 }
366 SetIdUsed(new_id);
367 }
368
369 private:
370 // Returns the first unused id in reverse order.
371 // This hopefully reduce the risk of more collisions. We want to change the
372 // default ids as little as possible.
373 int FindUnusedId() {
374 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
375 --next_id_;
376 }
377 ASSERT(next_id_ >= min_allowed_id_);
378 return next_id_;
379 }
380
381 bool IsIdUsed(int new_id) {
382 return id_set_.find(new_id) != id_set_.end();
383 }
384
385 void SetIdUsed(int new_id) {
386 id_set_.insert(new_id);
387 }
388
389 const int min_allowed_id_;
390 const int max_allowed_id_;
391 int next_id_;
392 std::set<int> id_set_;
393};
394
395// Helper class used for finding duplicate RTP payload types among audio, video
396// and data codecs. When bundle is used the payload types may not collide.
397class UsedPayloadTypes : public UsedIds<Codec> {
398 public:
399 UsedPayloadTypes()
400 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {
401 }
402
403
404 private:
405 static const int kDynamicPayloadTypeMin = 96;
406 static const int kDynamicPayloadTypeMax = 127;
407};
408
409// Helper class used for finding duplicate RTP Header extension ids among
410// audio and video extensions.
411class UsedRtpHeaderExtensionIds : public UsedIds<RtpHeaderExtension> {
412 public:
413 UsedRtpHeaderExtensionIds()
414 : UsedIds<RtpHeaderExtension>(kLocalIdMin, kLocalIdMax) {
415 }
416
417 private:
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000418 // Min and Max local identifier for one-byte header extensions, per RFC5285.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 static const int kLocalIdMin = 1;
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000420 static const int kLocalIdMax = 14;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421};
422
423static bool IsSctp(const MediaContentDescription* desc) {
424 return ((desc->protocol() == kMediaProtocolSctp) ||
425 (desc->protocol() == kMediaProtocolDtlsSctp));
426}
427
428// Adds a StreamParams for each Stream in Streams with media type
429// media_type to content_description.
430// |current_params| - All currently known StreamParams of any media type.
431template <class C>
432static bool AddStreamParams(
433 MediaType media_type,
434 const MediaSessionOptions::Streams& streams,
435 StreamParamsVec* current_streams,
436 MediaContentDescriptionImpl<C>* content_description,
437 const bool add_legacy_stream) {
438 const bool include_rtx_stream =
439 ContainsRtxCodec(content_description->codecs());
440
441 if (streams.empty() && add_legacy_stream) {
442 // TODO(perkj): Remove this legacy stream when all apps use StreamParams.
443 std::vector<uint32> ssrcs;
444 if (IsSctp(content_description)) {
445 GenerateSctpSids(*current_streams, &ssrcs);
446 } else {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000447 int num_ssrcs = include_rtx_stream ? 2 : 1;
448 GenerateSsrcs(*current_streams, num_ssrcs, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
450 if (include_rtx_stream) {
451 content_description->AddLegacyStream(ssrcs[0], ssrcs[1]);
452 content_description->set_multistream(true);
453 } else {
454 content_description->AddLegacyStream(ssrcs[0]);
455 }
456 return true;
457 }
458
459 MediaSessionOptions::Streams::const_iterator stream_it;
460 for (stream_it = streams.begin();
461 stream_it != streams.end(); ++stream_it) {
462 if (stream_it->type != media_type)
463 continue; // Wrong media type.
464
465 StreamParams param;
466 // groupid is empty for StreamParams generated using
467 // MediaSessionDescriptionFactory.
468 if (!GetStreamByIds(*current_streams, "", stream_it->id,
469 &param)) {
470 // This is a new stream.
471 // Get a CNAME. Either new or same as one of the other synched streams.
472 std::string cname;
473 if (!GenerateCname(*current_streams, streams, stream_it->sync_label,
474 &cname)) {
475 return false;
476 }
477
478 std::vector<uint32> ssrcs;
479 if (IsSctp(content_description)) {
480 GenerateSctpSids(*current_streams, &ssrcs);
481 } else {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000482 GenerateSsrcs(*current_streams, stream_it->num_sim_layers, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 }
484 StreamParams stream_param;
485 stream_param.id = stream_it->id;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000486 // Add the generated ssrc.
487 for (size_t i = 0; i < ssrcs.size(); ++i) {
488 stream_param.ssrcs.push_back(ssrcs[i]);
489 }
490 if (stream_it->num_sim_layers > 1) {
491 SsrcGroup group(kSimSsrcGroupSemantics, stream_param.ssrcs);
492 stream_param.ssrc_groups.push_back(group);
493 }
494 // Generate an extra ssrc for include_rtx_stream case.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 if (include_rtx_stream) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000496 std::vector<uint32> rtx_ssrc;
497 GenerateSsrcs(*current_streams, 1, &rtx_ssrc);
498 stream_param.AddFidSsrc(ssrcs[0], rtx_ssrc[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 content_description->set_multistream(true);
500 }
501 stream_param.cname = cname;
502 stream_param.sync_label = stream_it->sync_label;
503 content_description->AddStream(stream_param);
504
505 // Store the new StreamParams in current_streams.
506 // This is necessary so that we can use the CNAME for other media types.
507 current_streams->push_back(stream_param);
508 } else {
509 content_description->AddStream(param);
510 }
511 }
512 return true;
513}
514
515// Updates the transport infos of the |sdesc| according to the given
516// |bundle_group|. The transport infos of the content names within the
517// |bundle_group| should be updated to use the ufrag and pwd of the first
518// content within the |bundle_group|.
519static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
520 SessionDescription* sdesc) {
521 // The bundle should not be empty.
522 if (!sdesc || !bundle_group.FirstContentName()) {
523 return false;
524 }
525
526 // We should definitely have a transport for the first content.
527 std::string selected_content_name = *bundle_group.FirstContentName();
528 const TransportInfo* selected_transport_info =
529 sdesc->GetTransportInfoByName(selected_content_name);
530 if (!selected_transport_info) {
531 return false;
532 }
533
534 // Set the other contents to use the same ICE credentials.
535 const std::string selected_ufrag =
536 selected_transport_info->description.ice_ufrag;
537 const std::string selected_pwd =
538 selected_transport_info->description.ice_pwd;
539 for (TransportInfos::iterator it =
540 sdesc->transport_infos().begin();
541 it != sdesc->transport_infos().end(); ++it) {
542 if (bundle_group.HasContentName(it->content_name) &&
543 it->content_name != selected_content_name) {
544 it->description.ice_ufrag = selected_ufrag;
545 it->description.ice_pwd = selected_pwd;
546 }
547 }
548 return true;
549}
550
551// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
552// sets it to |cryptos|.
553static bool GetCryptosByName(const SessionDescription* sdesc,
554 const std::string& content_name,
555 CryptoParamsVec* cryptos) {
556 if (!sdesc || !cryptos) {
557 return false;
558 }
559
560 const ContentInfo* content = sdesc->GetContentByName(content_name);
561 if (!IsMediaContent(content) || !content->description) {
562 return false;
563 }
564
565 const MediaContentDescription* media_desc =
566 static_cast<const MediaContentDescription*>(content->description);
567 *cryptos = media_desc->cryptos();
568 return true;
569}
570
571// Predicate function used by the remove_if.
572// Returns true if the |crypto|'s cipher_suite is not found in |filter|.
573static bool CryptoNotFound(const CryptoParams crypto,
574 const CryptoParamsVec* filter) {
575 if (filter == NULL) {
576 return true;
577 }
578 for (CryptoParamsVec::const_iterator it = filter->begin();
579 it != filter->end(); ++it) {
580 if (it->cipher_suite == crypto.cipher_suite) {
581 return false;
582 }
583 }
584 return true;
585}
586
587// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
588// which are not available in |filter|.
589static void PruneCryptos(const CryptoParamsVec& filter,
590 CryptoParamsVec* target_cryptos) {
591 if (!target_cryptos) {
592 return;
593 }
594 target_cryptos->erase(std::remove_if(target_cryptos->begin(),
595 target_cryptos->end(),
596 bind2nd(ptr_fun(CryptoNotFound),
597 &filter)),
598 target_cryptos->end());
599}
600
601static bool IsRtpContent(SessionDescription* sdesc,
602 const std::string& content_name) {
603 bool is_rtp = false;
604 ContentInfo* content = sdesc->GetContentByName(content_name);
605 if (IsMediaContent(content)) {
606 MediaContentDescription* media_desc =
607 static_cast<MediaContentDescription*>(content->description);
608 if (!media_desc) {
609 return false;
610 }
611 is_rtp = media_desc->protocol().empty() ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000612 rtc::starts_with(media_desc->protocol().data(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 kMediaProtocolRtpPrefix);
614 }
615 return is_rtp;
616}
617
618// Updates the crypto parameters of the |sdesc| according to the given
619// |bundle_group|. The crypto parameters of all the contents within the
620// |bundle_group| should be updated to use the common subset of the
621// available cryptos.
622static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
623 SessionDescription* sdesc) {
624 // The bundle should not be empty.
625 if (!sdesc || !bundle_group.FirstContentName()) {
626 return false;
627 }
628
wu@webrtc.org78187522013-10-07 23:32:02 +0000629 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 // Get the common cryptos.
631 const ContentNames& content_names = bundle_group.content_names();
632 CryptoParamsVec common_cryptos;
633 for (ContentNames::const_iterator it = content_names.begin();
634 it != content_names.end(); ++it) {
635 if (!IsRtpContent(sdesc, *it)) {
636 continue;
637 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000638 // The common cryptos are needed if any of the content does not have DTLS
639 // enabled.
640 if (!sdesc->GetTransportInfoByName(*it)->description.secure()) {
641 common_cryptos_needed = true;
642 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 if (it == content_names.begin()) {
644 // Initial the common_cryptos with the first content in the bundle group.
645 if (!GetCryptosByName(sdesc, *it, &common_cryptos)) {
646 return false;
647 }
648 if (common_cryptos.empty()) {
649 // If there's no crypto params, we should just return.
650 return true;
651 }
652 } else {
653 CryptoParamsVec cryptos;
654 if (!GetCryptosByName(sdesc, *it, &cryptos)) {
655 return false;
656 }
657 PruneCryptos(cryptos, &common_cryptos);
658 }
659 }
660
wu@webrtc.org78187522013-10-07 23:32:02 +0000661 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 return false;
663 }
664
665 // Update to use the common cryptos.
666 for (ContentNames::const_iterator it = content_names.begin();
667 it != content_names.end(); ++it) {
668 if (!IsRtpContent(sdesc, *it)) {
669 continue;
670 }
671 ContentInfo* content = sdesc->GetContentByName(*it);
672 if (IsMediaContent(content)) {
673 MediaContentDescription* media_desc =
674 static_cast<MediaContentDescription*>(content->description);
675 if (!media_desc) {
676 return false;
677 }
678 media_desc->set_cryptos(common_cryptos);
679 }
680 }
681 return true;
682}
683
684template <class C>
685static bool ContainsRtxCodec(const std::vector<C>& codecs) {
686 typename std::vector<C>::const_iterator it;
687 for (it = codecs.begin(); it != codecs.end(); ++it) {
688 if (IsRtxCodec(*it)) {
689 return true;
690 }
691 }
692 return false;
693}
694
695template <class C>
696static bool IsRtxCodec(const C& codec) {
697 return stricmp(codec.name.c_str(), kRtxCodecName) == 0;
698}
699
700// Create a media content to be offered in a session-initiate,
701// according to the given options.rtcp_mux, options.is_muc,
702// options.streams, codecs, secure_transport, crypto, and streams. If we don't
703// currently have crypto (in current_cryptos) and it is enabled (in
704// secure_policy), crypto is created (according to crypto_suites). If
705// add_legacy_stream is true, and current_streams is empty, a legacy
706// stream is created. The created content is added to the offer.
707template <class C>
708static bool CreateMediaContentOffer(
709 const MediaSessionOptions& options,
710 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000711 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 const CryptoParamsVec* current_cryptos,
713 const std::vector<std::string>& crypto_suites,
714 const RtpHeaderExtensions& rtp_extensions,
715 bool add_legacy_stream,
716 StreamParamsVec* current_streams,
717 MediaContentDescriptionImpl<C>* offer) {
718 offer->AddCodecs(codecs);
719 offer->SortCodecs();
720
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000721 if (secure_policy == SEC_REQUIRED) {
722 offer->set_crypto_required(CT_SDES);
723 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 offer->set_rtcp_mux(options.rtcp_mux_enabled);
725 offer->set_multistream(options.is_muc);
726 offer->set_rtp_header_extensions(rtp_extensions);
727
728 if (!AddStreamParams(
729 offer->type(), options.streams, current_streams,
730 offer, add_legacy_stream)) {
731 return false;
732 }
733
734#ifdef HAVE_SRTP
735 if (secure_policy != SEC_DISABLED) {
736 if (current_cryptos) {
737 AddMediaCryptos(*current_cryptos, offer);
738 }
739 if (offer->cryptos().empty()) {
740 if (!CreateMediaCryptos(crypto_suites, offer)) {
741 return false;
742 }
743 }
744 }
745#endif
746
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000747 if (offer->crypto_required() == CT_SDES && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 return false;
749 }
750 return true;
751}
752
753template <class C>
754static void NegotiateCodecs(const std::vector<C>& local_codecs,
755 const std::vector<C>& offered_codecs,
756 std::vector<C>* negotiated_codecs) {
757 typename std::vector<C>::const_iterator ours;
758 for (ours = local_codecs.begin();
759 ours != local_codecs.end(); ++ours) {
760 typename std::vector<C>::const_iterator theirs;
761 for (theirs = offered_codecs.begin();
762 theirs != offered_codecs.end(); ++theirs) {
763 if (ours->Matches(*theirs)) {
764 C negotiated = *ours;
765 negotiated.IntersectFeedbackParams(*theirs);
766 if (IsRtxCodec(negotiated)) {
767 // Only negotiate RTX if kCodecParamAssociatedPayloadType has been
768 // set.
769 std::string apt_value;
770 if (!theirs->GetParam(kCodecParamAssociatedPayloadType, &apt_value)) {
771 LOG(LS_WARNING) << "RTX missing associated payload type.";
772 continue;
773 }
774 negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_value);
775 }
776 negotiated.id = theirs->id;
wu@webrtc.orgff1b1bf2014-06-20 20:57:42 +0000777 // RFC3264: Although the answerer MAY list the formats in their desired
778 // order of preference, it is RECOMMENDED that unless there is a
779 // specific reason, the answerer list formats in the same relative order
780 // they were present in the offer.
781 negotiated.preference = theirs->preference;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 negotiated_codecs->push_back(negotiated);
783 }
784 }
785 }
786}
787
788template <class C>
789static bool FindMatchingCodec(const std::vector<C>& codecs,
790 const C& codec_to_match,
791 C* found_codec) {
792 for (typename std::vector<C>::const_iterator it = codecs.begin();
793 it != codecs.end(); ++it) {
794 if (it->Matches(codec_to_match)) {
795 if (found_codec != NULL) {
796 *found_codec= *it;
797 }
798 return true;
799 }
800 }
801 return false;
802}
803
804// Adds all codecs from |reference_codecs| to |offered_codecs| that dont'
805// already exist in |offered_codecs| and ensure the payload types don't
806// collide.
807template <class C>
808static void FindCodecsToOffer(
809 const std::vector<C>& reference_codecs,
810 std::vector<C>* offered_codecs,
811 UsedPayloadTypes* used_pltypes) {
812
813 typedef std::map<int, C> RtxCodecReferences;
814 RtxCodecReferences new_rtx_codecs;
815
816 // Find all new RTX codecs.
817 for (typename std::vector<C>::const_iterator it = reference_codecs.begin();
818 it != reference_codecs.end(); ++it) {
819 if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && IsRtxCodec(*it)) {
820 C rtx_codec = *it;
821 int referenced_pl_type =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000822 rtc::FromString<int>(0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 rtx_codec.params[kCodecParamAssociatedPayloadType]);
824 new_rtx_codecs.insert(std::pair<int, C>(referenced_pl_type,
825 rtx_codec));
826 }
827 }
828
829 // Add all new codecs that are not RTX codecs.
830 for (typename std::vector<C>::const_iterator it = reference_codecs.begin();
831 it != reference_codecs.end(); ++it) {
832 if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && !IsRtxCodec(*it)) {
833 C codec = *it;
834 int original_payload_id = codec.id;
835 used_pltypes->FindAndSetIdUsed(&codec);
836 offered_codecs->push_back(codec);
837
838 // If this codec is referenced by a new RTX codec, update the reference
839 // in the RTX codec with the new payload type.
840 typename RtxCodecReferences::iterator rtx_it =
841 new_rtx_codecs.find(original_payload_id);
842 if (rtx_it != new_rtx_codecs.end()) {
843 C& rtx_codec = rtx_it->second;
844 rtx_codec.params[kCodecParamAssociatedPayloadType] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000845 rtc::ToString(codec.id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 }
847 }
848 }
849
850 // Add all new RTX codecs.
851 for (typename RtxCodecReferences::iterator it = new_rtx_codecs.begin();
852 it != new_rtx_codecs.end(); ++it) {
853 C& rtx_codec = it->second;
854 used_pltypes->FindAndSetIdUsed(&rtx_codec);
855 offered_codecs->push_back(rtx_codec);
856 }
857}
858
859
860static bool FindByUri(const RtpHeaderExtensions& extensions,
861 const RtpHeaderExtension& ext_to_match,
862 RtpHeaderExtension* found_extension) {
863 for (RtpHeaderExtensions::const_iterator it = extensions.begin();
864 it != extensions.end(); ++it) {
865 // We assume that all URIs are given in a canonical format.
866 if (it->uri == ext_to_match.uri) {
867 if (found_extension != NULL) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000868 *found_extension = *it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 }
870 return true;
871 }
872 }
873 return false;
874}
875
876static void FindAndSetRtpHdrExtUsed(
877 const RtpHeaderExtensions& reference_extensions,
878 RtpHeaderExtensions* offered_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000879 const RtpHeaderExtensions& other_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 UsedRtpHeaderExtensionIds* used_extensions) {
881 for (RtpHeaderExtensions::const_iterator it = reference_extensions.begin();
882 it != reference_extensions.end(); ++it) {
883 if (!FindByUri(*offered_extensions, *it, NULL)) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000884 RtpHeaderExtension ext;
885 if (!FindByUri(other_extensions, *it, &ext)) {
886 ext = *it;
887 used_extensions->FindAndSetIdUsed(&ext);
888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 offered_extensions->push_back(ext);
890 }
891 }
892}
893
894static void NegotiateRtpHeaderExtensions(
895 const RtpHeaderExtensions& local_extensions,
896 const RtpHeaderExtensions& offered_extensions,
897 RtpHeaderExtensions* negotiated_extenstions) {
898 RtpHeaderExtensions::const_iterator ours;
899 for (ours = local_extensions.begin();
900 ours != local_extensions.end(); ++ours) {
901 RtpHeaderExtension theirs;
902 if (FindByUri(offered_extensions, *ours, &theirs)) {
903 // We respond with their RTP header extension id.
904 negotiated_extenstions->push_back(theirs);
905 }
906 }
907}
908
909static void StripCNCodecs(AudioCodecs* audio_codecs) {
910 AudioCodecs::iterator iter = audio_codecs->begin();
911 while (iter != audio_codecs->end()) {
912 if (stricmp(iter->name.c_str(), kComfortNoiseCodecName) == 0) {
913 iter = audio_codecs->erase(iter);
914 } else {
915 ++iter;
916 }
917 }
918}
919
920// Create a media content to be answered in a session-accept,
921// according to the given options.rtcp_mux, options.streams, codecs,
922// crypto, and streams. If we don't currently have crypto (in
923// current_cryptos) and it is enabled (in secure_policy), crypto is
924// created (according to crypto_suites). If add_legacy_stream is
925// true, and current_streams is empty, a legacy stream is created.
926// The codecs, rtcp_mux, and crypto are all negotiated with the offer
927// from the incoming session-initiate. If the negotiation fails, this
928// method returns false. The created content is added to the offer.
929template <class C>
930static bool CreateMediaContentAnswer(
931 const MediaContentDescriptionImpl<C>* offer,
932 const MediaSessionOptions& options,
933 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000934 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 const CryptoParamsVec* current_cryptos,
936 const RtpHeaderExtensions& local_rtp_extenstions,
937 StreamParamsVec* current_streams,
938 bool add_legacy_stream,
939 bool bundle_enabled,
940 MediaContentDescriptionImpl<C>* answer) {
941 std::vector<C> negotiated_codecs;
942 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
943 answer->AddCodecs(negotiated_codecs);
944 answer->SortCodecs();
945 answer->set_protocol(offer->protocol());
946 RtpHeaderExtensions negotiated_rtp_extensions;
947 NegotiateRtpHeaderExtensions(local_rtp_extenstions,
948 offer->rtp_header_extensions(),
949 &negotiated_rtp_extensions);
950 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
951
952 answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux());
953
954 if (sdes_policy != SEC_DISABLED) {
955 CryptoParams crypto;
956 if (SelectCrypto(offer, bundle_enabled, &crypto)) {
957 if (current_cryptos) {
958 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
959 }
960 answer->AddCrypto(crypto);
961 }
962 }
963
964 if (answer->cryptos().empty() &&
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000965 (offer->crypto_required() == CT_SDES || sdes_policy == SEC_REQUIRED)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 return false;
967 }
968
969 if (!AddStreamParams(
970 answer->type(), options.streams, current_streams,
971 answer, add_legacy_stream)) {
972 return false; // Something went seriously wrong.
973 }
974
975 // Make sure the answer media content direction is per default set as
976 // described in RFC3264 section 6.1.
977 switch (offer->direction()) {
978 case MD_INACTIVE:
979 answer->set_direction(MD_INACTIVE);
980 break;
981 case MD_SENDONLY:
982 answer->set_direction(MD_RECVONLY);
983 break;
984 case MD_RECVONLY:
985 answer->set_direction(MD_SENDONLY);
986 break;
987 case MD_SENDRECV:
988 answer->set_direction(MD_SENDRECV);
989 break;
990 default:
991 break;
992 }
993
994 return true;
995}
996
997static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +0000998 const std::string& protocol,
999 bool secure_transport) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 // Data channels can have a protocol of SCTP or SCTP/DTLS.
1001 if (type == MEDIA_TYPE_DATA &&
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001002 ((protocol == kMediaProtocolSctp && !secure_transport)||
1003 (protocol == kMediaProtocolDtlsSctp && secure_transport))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 return true;
1005 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001006
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 // Since not all applications serialize and deserialize the media protocol,
1008 // we will have to accept |protocol| to be empty.
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001009 return protocol == kMediaProtocolAvpf || protocol.empty() ||
1010 protocol == kMediaProtocolSavpf ||
1011 (protocol == kMediaProtocolDtlsSavpf && secure_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012}
1013
1014static void SetMediaProtocol(bool secure_transport,
1015 MediaContentDescription* desc) {
1016 if (!desc->cryptos().empty() || secure_transport)
1017 desc->set_protocol(kMediaProtocolSavpf);
1018 else
1019 desc->set_protocol(kMediaProtocolAvpf);
1020}
1021
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001022// Gets the TransportInfo of the given |content_name| from the
1023// |current_description|. If doesn't exist, returns a new one.
1024static const TransportDescription* GetTransportDescription(
1025 const std::string& content_name,
1026 const SessionDescription* current_description) {
1027 const TransportDescription* desc = NULL;
1028 if (current_description) {
1029 const TransportInfo* info =
1030 current_description->GetTransportInfoByName(content_name);
1031 if (info) {
1032 desc = &info->description;
1033 }
1034 }
1035 return desc;
1036}
1037
1038// Gets the current DTLS state from the transport description.
1039static bool IsDtlsActive(
1040 const std::string& content_name,
1041 const SessionDescription* current_description) {
1042 if (!current_description)
1043 return false;
1044
1045 const ContentInfo* content =
1046 current_description->GetContentByName(content_name);
1047 if (!content)
1048 return false;
1049
1050 const TransportDescription* current_tdesc =
1051 GetTransportDescription(content_name, current_description);
1052 if (!current_tdesc)
1053 return false;
1054
1055 return current_tdesc->secure();
1056}
1057
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001058std::string MediaTypeToString(MediaType type) {
1059 std::string type_str;
1060 switch (type) {
1061 case MEDIA_TYPE_AUDIO:
1062 type_str = "audio";
1063 break;
1064 case MEDIA_TYPE_VIDEO:
1065 type_str = "video";
1066 break;
1067 case MEDIA_TYPE_DATA:
1068 type_str = "data";
1069 break;
1070 default:
1071 ASSERT(false);
1072 break;
1073 }
1074 return type_str;
1075}
1076
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001077void MediaSessionOptions::AddSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 const std::string& id,
1079 const std::string& sync_label) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001080 AddSendStreamInternal(type, id, sync_label, 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001081}
1082
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001083void MediaSessionOptions::AddSendVideoStream(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001084 const std::string& id,
1085 const std::string& sync_label,
1086 int num_sim_layers) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001087 AddSendStreamInternal(MEDIA_TYPE_VIDEO, id, sync_label, num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001088}
1089
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001090void MediaSessionOptions::AddSendStreamInternal(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001091 MediaType type,
1092 const std::string& id,
1093 const std::string& sync_label,
1094 int num_sim_layers) {
1095 streams.push_back(Stream(type, id, sync_label, num_sim_layers));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 // If we haven't already set the data_channel_type, and we add a
1098 // stream, we assume it's an RTP data stream.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001099 if (type == MEDIA_TYPE_DATA && data_channel_type == DCT_NONE)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100 data_channel_type = DCT_RTP;
1101}
1102
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001103void MediaSessionOptions::RemoveSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 const std::string& id) {
1105 Streams::iterator stream_it = streams.begin();
1106 for (; stream_it != streams.end(); ++stream_it) {
1107 if (stream_it->type == type && stream_it->id == id) {
1108 streams.erase(stream_it);
1109 return;
1110 }
1111 }
1112 ASSERT(false);
1113}
1114
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001115bool MediaSessionOptions::HasSendMediaStream(MediaType type) const {
1116 Streams::const_iterator stream_it = streams.begin();
1117 for (; stream_it != streams.end(); ++stream_it) {
1118 if (stream_it->type == type) {
1119 return true;
1120 }
1121 }
1122 return false;
1123}
1124
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1126 const TransportDescriptionFactory* transport_desc_factory)
1127 : secure_(SEC_DISABLED),
1128 add_legacy_(true),
1129 transport_desc_factory_(transport_desc_factory) {
1130}
1131
1132MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1133 ChannelManager* channel_manager,
1134 const TransportDescriptionFactory* transport_desc_factory)
1135 : secure_(SEC_DISABLED),
1136 add_legacy_(true),
1137 transport_desc_factory_(transport_desc_factory) {
1138 channel_manager->GetSupportedAudioCodecs(&audio_codecs_);
1139 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
1140 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
1141 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1142 channel_manager->GetSupportedDataCodecs(&data_codecs_);
1143}
1144
1145SessionDescription* MediaSessionDescriptionFactory::CreateOffer(
1146 const MediaSessionOptions& options,
1147 const SessionDescription* current_description) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 scoped_ptr<SessionDescription> offer(new SessionDescription());
1149
1150 StreamParamsVec current_streams;
1151 GetCurrentStreamParams(current_description, &current_streams);
1152
1153 AudioCodecs audio_codecs;
1154 VideoCodecs video_codecs;
1155 DataCodecs data_codecs;
1156 GetCodecsToOffer(current_description, &audio_codecs, &video_codecs,
1157 &data_codecs);
1158
1159 if (!options.vad_enabled) {
1160 // If application doesn't want CN codecs in offer.
1161 StripCNCodecs(&audio_codecs);
1162 }
1163
1164 RtpHeaderExtensions audio_rtp_extensions;
1165 RtpHeaderExtensions video_rtp_extensions;
1166 GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions,
1167 &video_rtp_extensions);
1168
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001169 bool audio_added = false;
1170 bool video_added = false;
1171 bool data_added = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001172
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001173 // Iterate through the contents of |current_description| to maintain the order
1174 // of the m-lines in the new offer.
1175 if (current_description) {
1176 ContentInfos::const_iterator it = current_description->contents().begin();
1177 for (; it != current_description->contents().end(); ++it) {
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001178 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001179 if (!AddAudioContentForOffer(options, current_description,
1180 audio_rtp_extensions, audio_codecs,
1181 &current_streams, offer.get())) {
1182 return NULL;
1183 }
1184 audio_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001185 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001186 if (!AddVideoContentForOffer(options, current_description,
1187 video_rtp_extensions, video_codecs,
1188 &current_streams, offer.get())) {
1189 return NULL;
1190 }
1191 video_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001192 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_DATA)) {
tommi@webrtc.orgf15dee62014-10-27 22:15:04 +00001193 MediaSessionOptions options_copy(options);
1194 if (IsSctp(static_cast<const MediaContentDescription*>(
1195 it->description))) {
1196 options_copy.data_channel_type = DCT_SCTP;
1197 }
1198 if (!AddDataContentForOffer(options_copy, current_description,
1199 &data_codecs, &current_streams,
1200 offer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001201 return NULL;
1202 }
1203 data_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001204 } else {
1205 ASSERT(false);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001206 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 }
1208 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001209
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001210 // Append contents that are not in |current_description|.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001211 if (!audio_added && options.has_audio() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001212 !AddAudioContentForOffer(options, current_description,
1213 audio_rtp_extensions, audio_codecs,
1214 &current_streams, offer.get())) {
1215 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001217 if (!video_added && options.has_video() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001218 !AddVideoContentForOffer(options, current_description,
1219 video_rtp_extensions, video_codecs,
1220 &current_streams, offer.get())) {
1221 return NULL;
1222 }
1223 if (!data_added && options.has_data() &&
1224 !AddDataContentForOffer(options, current_description, &data_codecs,
1225 &current_streams, offer.get())) {
1226 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 }
1228
1229 // Bundle the contents together, if we've been asked to do so, and update any
1230 // parameters that need to be tweaked for BUNDLE.
1231 if (options.bundle_enabled) {
1232 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
1233 for (ContentInfos::const_iterator content = offer->contents().begin();
1234 content != offer->contents().end(); ++content) {
1235 offer_bundle.AddContentName(content->name);
1236 }
1237 offer->AddGroup(offer_bundle);
1238 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1239 LOG(LS_ERROR) << "CreateOffer failed to UpdateTransportInfoForBundle.";
1240 return NULL;
1241 }
1242 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1243 LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1244 return NULL;
1245 }
1246 }
1247
1248 return offer.release();
1249}
1250
1251SessionDescription* MediaSessionDescriptionFactory::CreateAnswer(
1252 const SessionDescription* offer, const MediaSessionOptions& options,
1253 const SessionDescription* current_description) const {
1254 // The answer contains the intersection of the codecs in the offer with the
1255 // codecs we support, ordered by our local preference. As indicated by
1256 // XEP-0167, we retain the same payload ids from the offer in the answer.
1257 scoped_ptr<SessionDescription> answer(new SessionDescription());
1258
1259 StreamParamsVec current_streams;
1260 GetCurrentStreamParams(current_description, &current_streams);
1261
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001262 if (offer) {
1263 ContentInfos::const_iterator it = offer->contents().begin();
1264 for (; it != offer->contents().end(); ++it) {
1265 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
1266 if (!AddAudioContentForAnswer(offer, options, current_description,
1267 &current_streams, answer.get())) {
1268 return NULL;
1269 }
1270 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
1271 if (!AddVideoContentForAnswer(offer, options, current_description,
1272 &current_streams, answer.get())) {
1273 return NULL;
1274 }
1275 } else {
1276 ASSERT(IsMediaContentOfType(&*it, MEDIA_TYPE_DATA));
1277 if (!AddDataContentForAnswer(offer, options, current_description,
1278 &current_streams, answer.get())) {
1279 return NULL;
1280 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 }
1284
1285 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1286 // group in the answer with the appropriate content names.
1287 if (offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled) {
1288 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1289 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1290 for (ContentInfos::const_iterator content = answer->contents().begin();
1291 content != answer->contents().end(); ++content) {
1292 if (!content->rejected && offer_bundle->HasContentName(content->name)) {
1293 answer_bundle.AddContentName(content->name);
1294 }
1295 }
1296 if (answer_bundle.FirstContentName()) {
1297 answer->AddGroup(answer_bundle);
1298
1299 // Share the same ICE credentials and crypto params across all contents,
1300 // as BUNDLE requires.
1301 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
1302 LOG(LS_ERROR) << "CreateAnswer failed to UpdateTransportInfoForBundle.";
1303 return NULL;
1304 }
1305
1306 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
1307 LOG(LS_ERROR) << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
1308 return NULL;
1309 }
1310 }
1311 }
1312
1313 return answer.release();
1314}
1315
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316void MediaSessionDescriptionFactory::GetCodecsToOffer(
1317 const SessionDescription* current_description,
1318 AudioCodecs* audio_codecs,
1319 VideoCodecs* video_codecs,
1320 DataCodecs* data_codecs) const {
1321 UsedPayloadTypes used_pltypes;
1322 audio_codecs->clear();
1323 video_codecs->clear();
1324 data_codecs->clear();
1325
1326
1327 // First - get all codecs from the current description if the media type
1328 // is used.
1329 // Add them to |used_pltypes| so the payloadtype is not reused if a new media
1330 // type is added.
1331 if (current_description) {
1332 const AudioContentDescription* audio =
1333 GetFirstAudioContentDescription(current_description);
1334 if (audio) {
1335 *audio_codecs = audio->codecs();
1336 used_pltypes.FindAndSetIdUsed<AudioCodec>(audio_codecs);
1337 }
1338 const VideoContentDescription* video =
1339 GetFirstVideoContentDescription(current_description);
1340 if (video) {
1341 *video_codecs = video->codecs();
1342 used_pltypes.FindAndSetIdUsed<VideoCodec>(video_codecs);
1343 }
1344 const DataContentDescription* data =
1345 GetFirstDataContentDescription(current_description);
1346 if (data) {
1347 *data_codecs = data->codecs();
1348 used_pltypes.FindAndSetIdUsed<DataCodec>(data_codecs);
1349 }
1350 }
1351
1352 // Add our codecs that are not in |current_description|.
1353 FindCodecsToOffer<AudioCodec>(audio_codecs_, audio_codecs, &used_pltypes);
1354 FindCodecsToOffer<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1355 FindCodecsToOffer<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1356}
1357
1358void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
1359 const SessionDescription* current_description,
1360 RtpHeaderExtensions* audio_extensions,
1361 RtpHeaderExtensions* video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001362 // All header extensions allocated from the same range to avoid potential
1363 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 UsedRtpHeaderExtensionIds used_ids;
1365 audio_extensions->clear();
1366 video_extensions->clear();
1367
1368 // First - get all extensions from the current description if the media type
1369 // is used.
1370 // Add them to |used_ids| so the local ids are not reused if a new media
1371 // type is added.
1372 if (current_description) {
1373 const AudioContentDescription* audio =
1374 GetFirstAudioContentDescription(current_description);
1375 if (audio) {
1376 *audio_extensions = audio->rtp_header_extensions();
1377 used_ids.FindAndSetIdUsed(audio_extensions);
1378 }
1379 const VideoContentDescription* video =
1380 GetFirstVideoContentDescription(current_description);
1381 if (video) {
1382 *video_extensions = video->rtp_header_extensions();
1383 used_ids.FindAndSetIdUsed(video_extensions);
1384 }
1385 }
1386
1387 // Add our default RTP header extensions that are not in
1388 // |current_description|.
1389 FindAndSetRtpHdrExtUsed(audio_rtp_header_extensions(), audio_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001390 *video_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 FindAndSetRtpHdrExtUsed(video_rtp_header_extensions(), video_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001392 *audio_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393}
1394
1395bool MediaSessionDescriptionFactory::AddTransportOffer(
1396 const std::string& content_name,
1397 const TransportOptions& transport_options,
1398 const SessionDescription* current_desc,
1399 SessionDescription* offer_desc) const {
1400 if (!transport_desc_factory_)
1401 return false;
1402 const TransportDescription* current_tdesc =
1403 GetTransportDescription(content_name, current_desc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001404 rtc::scoped_ptr<TransportDescription> new_tdesc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 transport_desc_factory_->CreateOffer(transport_options, current_tdesc));
1406 bool ret = (new_tdesc.get() != NULL &&
1407 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)));
1408 if (!ret) {
1409 LOG(LS_ERROR)
1410 << "Failed to AddTransportOffer, content name=" << content_name;
1411 }
1412 return ret;
1413}
1414
1415TransportDescription* MediaSessionDescriptionFactory::CreateTransportAnswer(
1416 const std::string& content_name,
1417 const SessionDescription* offer_desc,
1418 const TransportOptions& transport_options,
1419 const SessionDescription* current_desc) const {
1420 if (!transport_desc_factory_)
1421 return NULL;
1422 const TransportDescription* offer_tdesc =
1423 GetTransportDescription(content_name, offer_desc);
1424 const TransportDescription* current_tdesc =
1425 GetTransportDescription(content_name, current_desc);
1426 return
1427 transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1428 current_tdesc);
1429}
1430
1431bool MediaSessionDescriptionFactory::AddTransportAnswer(
1432 const std::string& content_name,
1433 const TransportDescription& transport_desc,
1434 SessionDescription* answer_desc) const {
1435 if (!answer_desc->AddTransportInfo(TransportInfo(content_name,
1436 transport_desc))) {
1437 LOG(LS_ERROR)
1438 << "Failed to AddTransportAnswer, content name=" << content_name;
1439 return false;
1440 }
1441 return true;
1442}
1443
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001444bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
1445 const MediaSessionOptions& options,
1446 const SessionDescription* current_description,
1447 const RtpHeaderExtensions& audio_rtp_extensions,
1448 const AudioCodecs& audio_codecs,
1449 StreamParamsVec* current_streams,
1450 SessionDescription* desc) const {
1451 cricket::SecurePolicy sdes_policy =
1452 IsDtlsActive(CN_AUDIO, current_description) ?
1453 cricket::SEC_DISABLED : secure();
1454
1455 scoped_ptr<AudioContentDescription> audio(new AudioContentDescription());
1456 std::vector<std::string> crypto_suites;
1457 GetSupportedAudioCryptoSuites(&crypto_suites);
1458 if (!CreateMediaContentOffer(
1459 options,
1460 audio_codecs,
1461 sdes_policy,
1462 GetCryptos(GetFirstAudioContentDescription(current_description)),
1463 crypto_suites,
1464 audio_rtp_extensions,
1465 add_legacy_,
1466 current_streams,
1467 audio.get())) {
1468 return false;
1469 }
1470 audio->set_lang(lang_);
1471
1472 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1473 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001474
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001475 if (!options.recv_audio) {
1476 audio->set_direction(MD_SENDONLY);
1477 }
1478
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001479 desc->AddContent(CN_AUDIO, NS_JINGLE_RTP, audio.release());
1480 if (!AddTransportOffer(CN_AUDIO, options.transport_options,
1481 current_description, desc)) {
1482 return false;
1483 }
1484
1485 return true;
1486}
1487
1488bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
1489 const MediaSessionOptions& options,
1490 const SessionDescription* current_description,
1491 const RtpHeaderExtensions& video_rtp_extensions,
1492 const VideoCodecs& video_codecs,
1493 StreamParamsVec* current_streams,
1494 SessionDescription* desc) const {
1495 cricket::SecurePolicy sdes_policy =
1496 IsDtlsActive(CN_VIDEO, current_description) ?
1497 cricket::SEC_DISABLED : secure();
1498
1499 scoped_ptr<VideoContentDescription> video(new VideoContentDescription());
1500 std::vector<std::string> crypto_suites;
1501 GetSupportedVideoCryptoSuites(&crypto_suites);
1502 if (!CreateMediaContentOffer(
1503 options,
1504 video_codecs,
1505 sdes_policy,
1506 GetCryptos(GetFirstVideoContentDescription(current_description)),
1507 crypto_suites,
1508 video_rtp_extensions,
1509 add_legacy_,
1510 current_streams,
1511 video.get())) {
1512 return false;
1513 }
1514
1515 video->set_bandwidth(options.video_bandwidth);
1516
1517 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1518 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001519
1520 if (!options.recv_video) {
1521 video->set_direction(MD_SENDONLY);
1522 }
1523
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001524 desc->AddContent(CN_VIDEO, NS_JINGLE_RTP, video.release());
1525 if (!AddTransportOffer(CN_VIDEO, options.transport_options,
1526 current_description, desc)) {
1527 return false;
1528 }
1529
1530 return true;
1531}
1532
1533bool MediaSessionDescriptionFactory::AddDataContentForOffer(
1534 const MediaSessionOptions& options,
1535 const SessionDescription* current_description,
1536 DataCodecs* data_codecs,
1537 StreamParamsVec* current_streams,
1538 SessionDescription* desc) const {
1539 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1540
1541 scoped_ptr<DataContentDescription> data(new DataContentDescription());
1542 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1543
1544 FilterDataCodecs(data_codecs, is_sctp);
1545
1546 cricket::SecurePolicy sdes_policy =
1547 IsDtlsActive(CN_DATA, current_description) ?
1548 cricket::SEC_DISABLED : secure();
1549 std::vector<std::string> crypto_suites;
1550 if (is_sctp) {
1551 // SDES doesn't make sense for SCTP, so we disable it, and we only
1552 // get SDES crypto suites for RTP-based data channels.
1553 sdes_policy = cricket::SEC_DISABLED;
1554 // Unlike SetMediaProtocol below, we need to set the protocol
1555 // before we call CreateMediaContentOffer. Otherwise,
1556 // CreateMediaContentOffer won't know this is SCTP and will
1557 // generate SSRCs rather than SIDs.
1558 data->set_protocol(
1559 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp);
1560 } else {
1561 GetSupportedDataCryptoSuites(&crypto_suites);
1562 }
1563
1564 if (!CreateMediaContentOffer(
1565 options,
1566 *data_codecs,
1567 sdes_policy,
1568 GetCryptos(GetFirstDataContentDescription(current_description)),
1569 crypto_suites,
1570 RtpHeaderExtensions(),
1571 add_legacy_,
1572 current_streams,
1573 data.get())) {
1574 return false;
1575 }
1576
1577 if (is_sctp) {
1578 desc->AddContent(CN_DATA, NS_JINGLE_DRAFT_SCTP, data.release());
1579 } else {
1580 data->set_bandwidth(options.data_bandwidth);
1581 SetMediaProtocol(secure_transport, data.get());
1582 desc->AddContent(CN_DATA, NS_JINGLE_RTP, data.release());
1583 }
1584 if (!AddTransportOffer(CN_DATA, options.transport_options,
1585 current_description, desc)) {
1586 return false;
1587 }
1588 return true;
1589}
1590
1591bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
1592 const SessionDescription* offer,
1593 const MediaSessionOptions& options,
1594 const SessionDescription* current_description,
1595 StreamParamsVec* current_streams,
1596 SessionDescription* answer) const {
1597 const ContentInfo* audio_content = GetFirstAudioContent(offer);
1598
1599 scoped_ptr<TransportDescription> audio_transport(
1600 CreateTransportAnswer(audio_content->name, offer,
1601 options.transport_options,
1602 current_description));
1603 if (!audio_transport) {
1604 return false;
1605 }
1606
1607 AudioCodecs audio_codecs = audio_codecs_;
1608 if (!options.vad_enabled) {
1609 StripCNCodecs(&audio_codecs);
1610 }
1611
1612 bool bundle_enabled =
1613 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1614 scoped_ptr<AudioContentDescription> audio_answer(
1615 new AudioContentDescription());
1616 // Do not require or create SDES cryptos if DTLS is used.
1617 cricket::SecurePolicy sdes_policy =
1618 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
1619 if (!CreateMediaContentAnswer(
1620 static_cast<const AudioContentDescription*>(
1621 audio_content->description),
1622 options,
1623 audio_codecs,
1624 sdes_policy,
1625 GetCryptos(GetFirstAudioContentDescription(current_description)),
1626 audio_rtp_extensions_,
1627 current_streams,
1628 add_legacy_,
1629 bundle_enabled,
1630 audio_answer.get())) {
1631 return false; // Fails the session setup.
1632 }
1633
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001634 bool rejected = !options.has_audio() || audio_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001635 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
1636 audio_answer->protocol(),
1637 audio_transport->secure());
1638 if (!rejected) {
1639 AddTransportAnswer(audio_content->name, *(audio_transport.get()), answer);
1640 } else {
1641 // RFC 3264
1642 // The answer MUST contain the same number of m-lines as the offer.
1643 LOG(LS_INFO) << "Audio is not supported in the answer.";
1644 }
1645
1646 answer->AddContent(audio_content->name, audio_content->type, rejected,
1647 audio_answer.release());
1648 return true;
1649}
1650
1651bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
1652 const SessionDescription* offer,
1653 const MediaSessionOptions& options,
1654 const SessionDescription* current_description,
1655 StreamParamsVec* current_streams,
1656 SessionDescription* answer) const {
1657 const ContentInfo* video_content = GetFirstVideoContent(offer);
1658 scoped_ptr<TransportDescription> video_transport(
1659 CreateTransportAnswer(video_content->name, offer,
1660 options.transport_options,
1661 current_description));
1662 if (!video_transport) {
1663 return false;
1664 }
1665
1666 scoped_ptr<VideoContentDescription> video_answer(
1667 new VideoContentDescription());
1668 // Do not require or create SDES cryptos if DTLS is used.
1669 cricket::SecurePolicy sdes_policy =
1670 video_transport->secure() ? cricket::SEC_DISABLED : secure();
1671 bool bundle_enabled =
1672 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1673 if (!CreateMediaContentAnswer(
1674 static_cast<const VideoContentDescription*>(
1675 video_content->description),
1676 options,
1677 video_codecs_,
1678 sdes_policy,
1679 GetCryptos(GetFirstVideoContentDescription(current_description)),
1680 video_rtp_extensions_,
1681 current_streams,
1682 add_legacy_,
1683 bundle_enabled,
1684 video_answer.get())) {
1685 return false;
1686 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001687 bool rejected = !options.has_video() || video_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001688 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
1689 video_answer->protocol(),
1690 video_transport->secure());
1691 if (!rejected) {
1692 if (!AddTransportAnswer(video_content->name, *(video_transport.get()),
1693 answer)) {
1694 return false;
1695 }
1696 video_answer->set_bandwidth(options.video_bandwidth);
1697 } else {
1698 // RFC 3264
1699 // The answer MUST contain the same number of m-lines as the offer.
1700 LOG(LS_INFO) << "Video is not supported in the answer.";
1701 }
1702 answer->AddContent(video_content->name, video_content->type, rejected,
1703 video_answer.release());
1704 return true;
1705}
1706
1707bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
1708 const SessionDescription* offer,
1709 const MediaSessionOptions& options,
1710 const SessionDescription* current_description,
1711 StreamParamsVec* current_streams,
1712 SessionDescription* answer) const {
1713 const ContentInfo* data_content = GetFirstDataContent(offer);
1714 scoped_ptr<TransportDescription> data_transport(
1715 CreateTransportAnswer(data_content->name, offer,
1716 options.transport_options,
1717 current_description));
1718 if (!data_transport) {
1719 return false;
1720 }
1721 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1722 std::vector<DataCodec> data_codecs(data_codecs_);
1723 FilterDataCodecs(&data_codecs, is_sctp);
1724
1725 scoped_ptr<DataContentDescription> data_answer(
1726 new DataContentDescription());
1727 // Do not require or create SDES cryptos if DTLS is used.
1728 cricket::SecurePolicy sdes_policy =
1729 data_transport->secure() ? cricket::SEC_DISABLED : secure();
1730 bool bundle_enabled =
1731 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1732 if (!CreateMediaContentAnswer(
1733 static_cast<const DataContentDescription*>(
1734 data_content->description),
1735 options,
1736 data_codecs_,
1737 sdes_policy,
1738 GetCryptos(GetFirstDataContentDescription(current_description)),
1739 RtpHeaderExtensions(),
1740 current_streams,
1741 add_legacy_,
1742 bundle_enabled,
1743 data_answer.get())) {
1744 return false; // Fails the session setup.
1745 }
1746
1747 bool rejected = !options.has_data() || data_content->rejected ||
1748 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
1749 data_answer->protocol(),
1750 data_transport->secure());
1751 if (!rejected) {
1752 data_answer->set_bandwidth(options.data_bandwidth);
1753 if (!AddTransportAnswer(data_content->name, *(data_transport.get()),
1754 answer)) {
1755 return false;
1756 }
1757 } else {
1758 // RFC 3264
1759 // The answer MUST contain the same number of m-lines as the offer.
1760 LOG(LS_INFO) << "Data is not supported in the answer.";
1761 }
1762 answer->AddContent(data_content->name, data_content->type, rejected,
1763 data_answer.release());
1764 return true;
1765}
1766
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767bool IsMediaContent(const ContentInfo* content) {
1768 return (content &&
1769 (content->type == NS_JINGLE_RTP ||
1770 content->type == NS_JINGLE_DRAFT_SCTP));
1771}
1772
1773bool IsAudioContent(const ContentInfo* content) {
1774 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
1775}
1776
1777bool IsVideoContent(const ContentInfo* content) {
1778 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
1779}
1780
1781bool IsDataContent(const ContentInfo* content) {
1782 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
1783}
1784
1785static const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
1786 MediaType media_type) {
1787 for (ContentInfos::const_iterator content = contents.begin();
1788 content != contents.end(); content++) {
1789 if (IsMediaContentOfType(&*content, media_type)) {
1790 return &*content;
1791 }
1792 }
1793 return NULL;
1794}
1795
1796const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
1797 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
1798}
1799
1800const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
1801 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
1802}
1803
1804const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
1805 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
1806}
1807
1808static const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
1809 MediaType media_type) {
1810 if (sdesc == NULL)
1811 return NULL;
1812
1813 return GetFirstMediaContent(sdesc->contents(), media_type);
1814}
1815
1816const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
1817 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
1818}
1819
1820const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
1821 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
1822}
1823
1824const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
1825 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
1826}
1827
1828const MediaContentDescription* GetFirstMediaContentDescription(
1829 const SessionDescription* sdesc, MediaType media_type) {
1830 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
1831 const ContentDescription* description = content ? content->description : NULL;
1832 return static_cast<const MediaContentDescription*>(description);
1833}
1834
1835const AudioContentDescription* GetFirstAudioContentDescription(
1836 const SessionDescription* sdesc) {
1837 return static_cast<const AudioContentDescription*>(
1838 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
1839}
1840
1841const VideoContentDescription* GetFirstVideoContentDescription(
1842 const SessionDescription* sdesc) {
1843 return static_cast<const VideoContentDescription*>(
1844 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
1845}
1846
1847const DataContentDescription* GetFirstDataContentDescription(
1848 const SessionDescription* sdesc) {
1849 return static_cast<const DataContentDescription*>(
1850 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
1851}
1852
1853bool GetMediaChannelNameFromComponent(
1854 int component, MediaType media_type, std::string* channel_name) {
1855 if (media_type == MEDIA_TYPE_AUDIO) {
1856 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1857 *channel_name = GICE_CHANNEL_NAME_RTP;
1858 return true;
1859 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1860 *channel_name = GICE_CHANNEL_NAME_RTCP;
1861 return true;
1862 }
1863 } else if (media_type == MEDIA_TYPE_VIDEO) {
1864 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1865 *channel_name = GICE_CHANNEL_NAME_VIDEO_RTP;
1866 return true;
1867 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1868 *channel_name = GICE_CHANNEL_NAME_VIDEO_RTCP;
1869 return true;
1870 }
1871 } else if (media_type == MEDIA_TYPE_DATA) {
1872 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1873 *channel_name = GICE_CHANNEL_NAME_DATA_RTP;
1874 return true;
1875 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1876 *channel_name = GICE_CHANNEL_NAME_DATA_RTCP;
1877 return true;
1878 }
1879 }
1880
1881 return false;
1882}
1883
1884bool GetMediaComponentFromChannelName(
1885 const std::string& channel_name, int* component) {
1886 if (channel_name == GICE_CHANNEL_NAME_RTP ||
1887 channel_name == GICE_CHANNEL_NAME_VIDEO_RTP ||
1888 channel_name == GICE_CHANNEL_NAME_DATA_RTP) {
1889 *component = ICE_CANDIDATE_COMPONENT_RTP;
1890 return true;
1891 } else if (channel_name == GICE_CHANNEL_NAME_RTCP ||
1892 channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP ||
1893 channel_name == GICE_CHANNEL_NAME_DATA_RTP) {
1894 *component = ICE_CANDIDATE_COMPONENT_RTCP;
1895 return true;
1896 }
1897
1898 return false;
1899}
1900
1901bool GetMediaTypeFromChannelName(
1902 const std::string& channel_name, MediaType* media_type) {
1903 if (channel_name == GICE_CHANNEL_NAME_RTP ||
1904 channel_name == GICE_CHANNEL_NAME_RTCP) {
1905 *media_type = MEDIA_TYPE_AUDIO;
1906 return true;
1907 } else if (channel_name == GICE_CHANNEL_NAME_VIDEO_RTP ||
1908 channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP) {
1909 *media_type = MEDIA_TYPE_VIDEO;
1910 return true;
1911 } else if (channel_name == GICE_CHANNEL_NAME_DATA_RTP ||
1912 channel_name == GICE_CHANNEL_NAME_DATA_RTCP) {
1913 *media_type = MEDIA_TYPE_DATA;
1914 return true;
1915 }
1916
1917 return false;
1918}
1919
1920} // namespace cricket