blob: b299944519376dcef804a1f206b3a09d2b4f15ce [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";
lally@webrtc.orgec97c652015-02-24 20:18:48 +000074const char kMediaProtocolUdpDtlsSctp[] = "UDP/DTLS/SCTP";
lally@webrtc.orga7470932015-02-24 20:19:21 +000075const char kMediaProtocolTcpDtlsSctp[] = "TCP/DTLS/SCTP";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77static bool IsMediaContentOfType(const ContentInfo* content,
78 MediaType media_type) {
79 if (!IsMediaContent(content)) {
80 return false;
81 }
82
83 const MediaContentDescription* mdesc =
84 static_cast<const MediaContentDescription*>(content->description);
85 return mdesc && mdesc->type() == media_type;
86}
87
88static bool CreateCryptoParams(int tag, const std::string& cipher,
89 CryptoParams *out) {
90 std::string key;
91 key.reserve(SRTP_MASTER_KEY_BASE64_LEN);
92
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093 if (!rtc::CreateRandomString(SRTP_MASTER_KEY_BASE64_LEN, &key)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 return false;
95 }
96 out->tag = tag;
97 out->cipher_suite = cipher;
98 out->key_params = kInline;
99 out->key_params += key;
100 return true;
101}
102
103#ifdef HAVE_SRTP
104static bool AddCryptoParams(const std::string& cipher_suite,
105 CryptoParamsVec *out) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000106 int size = static_cast<int>(out->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107
108 out->resize(size + 1);
109 return CreateCryptoParams(size, cipher_suite, &out->at(size));
110}
111
112void AddMediaCryptos(const CryptoParamsVec& cryptos,
113 MediaContentDescription* media) {
114 for (CryptoParamsVec::const_iterator crypto = cryptos.begin();
115 crypto != cryptos.end(); ++crypto) {
116 media->AddCrypto(*crypto);
117 }
118}
119
120bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
121 MediaContentDescription* media) {
122 CryptoParamsVec cryptos;
123 for (std::vector<std::string>::const_iterator it = crypto_suites.begin();
124 it != crypto_suites.end(); ++it) {
125 if (!AddCryptoParams(*it, &cryptos)) {
126 return false;
127 }
128 }
129 AddMediaCryptos(cryptos, media);
130 return true;
131}
132#endif
133
134const CryptoParamsVec* GetCryptos(const MediaContentDescription* media) {
135 if (!media) {
136 return NULL;
137 }
138 return &media->cryptos();
139}
140
141bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
142 const CryptoParams& crypto,
143 CryptoParams* out) {
144 for (CryptoParamsVec::const_iterator it = cryptos.begin();
145 it != cryptos.end(); ++it) {
146 if (crypto.Matches(*it)) {
147 *out = *it;
148 return true;
149 }
150 }
151 return false;
152}
153
154// For audio, HMAC 32 is prefered because of the low overhead.
155void GetSupportedAudioCryptoSuites(
156 std::vector<std::string>* crypto_suites) {
157#ifdef HAVE_SRTP
158 crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_32);
159 crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_80);
160#endif
161}
162
163void GetSupportedVideoCryptoSuites(
164 std::vector<std::string>* crypto_suites) {
165 GetSupportedDefaultCryptoSuites(crypto_suites);
166}
167
168void GetSupportedDataCryptoSuites(
169 std::vector<std::string>* crypto_suites) {
170 GetSupportedDefaultCryptoSuites(crypto_suites);
171}
172
173void GetSupportedDefaultCryptoSuites(
174 std::vector<std::string>* crypto_suites) {
175#ifdef HAVE_SRTP
176 crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_80);
177#endif
178}
179
180// For video support only 80-bit SHA1 HMAC. For audio 32-bit HMAC is
181// tolerated unless bundle is enabled because it is low overhead. Pick the
182// crypto in the list that is supported.
183static bool SelectCrypto(const MediaContentDescription* offer,
184 bool bundle,
185 CryptoParams *crypto) {
186 bool audio = offer->type() == MEDIA_TYPE_AUDIO;
187 const CryptoParamsVec& cryptos = offer->cryptos();
188
189 for (CryptoParamsVec::const_iterator i = cryptos.begin();
190 i != cryptos.end(); ++i) {
191 if (CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite ||
192 (CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio && !bundle)) {
193 return CreateCryptoParams(i->tag, i->cipher_suite, crypto);
194 }
195 }
196 return false;
197}
198
199static const StreamParams* FindFirstStreamParamsByCname(
200 const StreamParamsVec& params_vec,
201 const std::string& cname) {
202 for (StreamParamsVec::const_iterator it = params_vec.begin();
203 it != params_vec.end(); ++it) {
204 if (cname == it->cname)
205 return &*it;
206 }
207 return NULL;
208}
209
210// Generates a new CNAME or the CNAME of an already existing StreamParams
211// if a StreamParams exist for another Stream in streams with sync_label
212// sync_label.
213static bool GenerateCname(const StreamParamsVec& params_vec,
214 const MediaSessionOptions::Streams& streams,
215 const std::string& synch_label,
216 std::string* cname) {
217 ASSERT(cname != NULL);
218 if (!cname)
219 return false;
220
221 // Check if a CNAME exist for any of the other synched streams.
222 for (MediaSessionOptions::Streams::const_iterator stream_it = streams.begin();
223 stream_it != streams.end() ; ++stream_it) {
224 if (synch_label != stream_it->sync_label)
225 continue;
226
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 // groupid is empty for StreamParams generated using
228 // MediaSessionDescriptionFactory.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000229 const StreamParams* param = GetStreamByIds(params_vec, "", stream_it->id);
230 if (param) {
231 *cname = param->cname;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return true;
233 }
234 }
235 // No other stream seems to exist that we should sync with.
236 // Generate a random string for the RTCP CNAME, as stated in RFC 6222.
237 // This string is only used for synchronization, and therefore is opaque.
238 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000239 if (!rtc::CreateRandomString(16, cname)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 ASSERT(false);
241 return false;
242 }
243 } while (FindFirstStreamParamsByCname(params_vec, *cname));
244
245 return true;
246}
247
248// Generate random SSRC values that are not already present in |params_vec|.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000249// The generated values are added to |ssrcs|.
250// |num_ssrcs| is the number of the SSRC will be generated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251static void GenerateSsrcs(const StreamParamsVec& params_vec,
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000252 int num_ssrcs,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 std::vector<uint32>* ssrcs) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000254 for (int i = 0; i < num_ssrcs; i++) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000255 uint32 candidate;
256 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000257 candidate = rtc::CreateRandomNonZeroId();
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000258 } while (GetStreamBySsrc(params_vec, candidate) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000259 std::count(ssrcs->begin(), ssrcs->end(), candidate) > 0);
260 ssrcs->push_back(candidate);
261 }
262}
263
264// Returns false if we exhaust the range of SIDs.
265static bool GenerateSctpSid(const StreamParamsVec& params_vec,
266 uint32* sid) {
267 if (params_vec.size() > kMaxSctpSid) {
268 LOG(LS_WARNING) <<
269 "Could not generate an SCTP SID: too many SCTP streams.";
270 return false;
271 }
272 while (true) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000273 uint32 candidate = rtc::CreateRandomNonZeroId() % kMaxSctpSid;
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000274 if (!GetStreamBySsrc(params_vec, candidate)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275 *sid = candidate;
276 return true;
277 }
278 }
279}
280
281static bool GenerateSctpSids(const StreamParamsVec& params_vec,
282 std::vector<uint32>* sids) {
283 uint32 sid;
284 if (!GenerateSctpSid(params_vec, &sid)) {
285 LOG(LS_WARNING) << "Could not generated an SCTP SID.";
286 return false;
287 }
288 sids->push_back(sid);
289 return true;
290}
291
292// Finds all StreamParams of all media types and attach them to stream_params.
293static void GetCurrentStreamParams(const SessionDescription* sdesc,
294 StreamParamsVec* stream_params) {
295 if (!sdesc)
296 return;
297
298 const ContentInfos& contents = sdesc->contents();
299 for (ContentInfos::const_iterator content = contents.begin();
300 content != contents.end(); ++content) {
301 if (!IsMediaContent(&*content)) {
302 continue;
303 }
304 const MediaContentDescription* media =
305 static_cast<const MediaContentDescription*>(
306 content->description);
307 const StreamParamsVec& streams = media->streams();
308 for (StreamParamsVec::const_iterator it = streams.begin();
309 it != streams.end(); ++it) {
310 stream_params->push_back(*it);
311 }
312 }
313}
314
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000315// Filters the data codecs for the data channel type.
316void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
317 // Filter RTP codec for SCTP and vice versa.
318 int codec_id = sctp ? kGoogleRtpDataCodecId : kGoogleSctpDataCodecId;
319 for (std::vector<DataCodec>::iterator iter = codecs->begin();
320 iter != codecs->end();) {
321 if (iter->id == codec_id) {
322 iter = codecs->erase(iter);
323 } else {
324 ++iter;
325 }
326 }
327}
328
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329template <typename IdStruct>
330class UsedIds {
331 public:
332 UsedIds(int min_allowed_id, int max_allowed_id)
333 : min_allowed_id_(min_allowed_id),
334 max_allowed_id_(max_allowed_id),
335 next_id_(max_allowed_id) {
336 }
337
338 // Loops through all Id in |ids| and changes its id if it is
339 // already in use by another IdStruct. Call this methods with all Id
340 // in a session description to make sure no duplicate ids exists.
341 // Note that typename Id must be a type of IdStruct.
342 template <typename Id>
343 void FindAndSetIdUsed(std::vector<Id>* ids) {
344 for (typename std::vector<Id>::iterator it = ids->begin();
345 it != ids->end(); ++it) {
346 FindAndSetIdUsed(&*it);
347 }
348 }
349
350 // Finds and sets an unused id if the |idstruct| id is already in use.
351 void FindAndSetIdUsed(IdStruct* idstruct) {
352 const int original_id = idstruct->id;
353 int new_id = idstruct->id;
354
355 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
356 // If the original id is not in range - this is an id that can't be
357 // dynamically changed.
358 return;
359 }
360
361 if (IsIdUsed(original_id)) {
362 new_id = FindUnusedId();
363 LOG(LS_WARNING) << "Duplicate id found. Reassigning from " << original_id
364 << " to " << new_id;
365 idstruct->id = new_id;
366 }
367 SetIdUsed(new_id);
368 }
369
370 private:
371 // Returns the first unused id in reverse order.
372 // This hopefully reduce the risk of more collisions. We want to change the
373 // default ids as little as possible.
374 int FindUnusedId() {
375 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
376 --next_id_;
377 }
378 ASSERT(next_id_ >= min_allowed_id_);
379 return next_id_;
380 }
381
382 bool IsIdUsed(int new_id) {
383 return id_set_.find(new_id) != id_set_.end();
384 }
385
386 void SetIdUsed(int new_id) {
387 id_set_.insert(new_id);
388 }
389
390 const int min_allowed_id_;
391 const int max_allowed_id_;
392 int next_id_;
393 std::set<int> id_set_;
394};
395
396// Helper class used for finding duplicate RTP payload types among audio, video
397// and data codecs. When bundle is used the payload types may not collide.
398class UsedPayloadTypes : public UsedIds<Codec> {
399 public:
400 UsedPayloadTypes()
401 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {
402 }
403
404
405 private:
406 static const int kDynamicPayloadTypeMin = 96;
407 static const int kDynamicPayloadTypeMax = 127;
408};
409
410// Helper class used for finding duplicate RTP Header extension ids among
411// audio and video extensions.
412class UsedRtpHeaderExtensionIds : public UsedIds<RtpHeaderExtension> {
413 public:
414 UsedRtpHeaderExtensionIds()
415 : UsedIds<RtpHeaderExtension>(kLocalIdMin, kLocalIdMax) {
416 }
417
418 private:
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000419 // Min and Max local identifier for one-byte header extensions, per RFC5285.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420 static const int kLocalIdMin = 1;
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000421 static const int kLocalIdMax = 14;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422};
423
424static bool IsSctp(const MediaContentDescription* desc) {
425 return ((desc->protocol() == kMediaProtocolSctp) ||
426 (desc->protocol() == kMediaProtocolDtlsSctp));
427}
428
429// Adds a StreamParams for each Stream in Streams with media type
430// media_type to content_description.
431// |current_params| - All currently known StreamParams of any media type.
432template <class C>
433static bool AddStreamParams(
434 MediaType media_type,
435 const MediaSessionOptions::Streams& streams,
436 StreamParamsVec* current_streams,
437 MediaContentDescriptionImpl<C>* content_description,
438 const bool add_legacy_stream) {
Noah Richards2e7a0982015-05-18 14:02:54 -0700439 const bool include_rtx_streams =
440 ContainsRtxCodec(content_description->codecs());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441
442 if (streams.empty() && add_legacy_stream) {
443 // TODO(perkj): Remove this legacy stream when all apps use StreamParams.
444 std::vector<uint32> ssrcs;
445 if (IsSctp(content_description)) {
446 GenerateSctpSids(*current_streams, &ssrcs);
447 } else {
Noah Richards2e7a0982015-05-18 14:02:54 -0700448 int num_ssrcs = include_rtx_streams ? 2 : 1;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000449 GenerateSsrcs(*current_streams, num_ssrcs, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 }
Noah Richards2e7a0982015-05-18 14:02:54 -0700451 if (include_rtx_streams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 content_description->AddLegacyStream(ssrcs[0], ssrcs[1]);
453 content_description->set_multistream(true);
454 } else {
455 content_description->AddLegacyStream(ssrcs[0]);
456 }
457 return true;
458 }
459
460 MediaSessionOptions::Streams::const_iterator stream_it;
461 for (stream_it = streams.begin();
462 stream_it != streams.end(); ++stream_it) {
463 if (stream_it->type != media_type)
464 continue; // Wrong media type.
465
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000466 const StreamParams* param =
467 GetStreamByIds(*current_streams, "", stream_it->id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 // groupid is empty for StreamParams generated using
469 // MediaSessionDescriptionFactory.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000470 if (!param) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 // This is a new stream.
472 // Get a CNAME. Either new or same as one of the other synched streams.
473 std::string cname;
474 if (!GenerateCname(*current_streams, streams, stream_it->sync_label,
475 &cname)) {
476 return false;
477 }
478
479 std::vector<uint32> ssrcs;
480 if (IsSctp(content_description)) {
481 GenerateSctpSids(*current_streams, &ssrcs);
482 } else {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000483 GenerateSsrcs(*current_streams, stream_it->num_sim_layers, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 }
485 StreamParams stream_param;
486 stream_param.id = stream_it->id;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000487 // Add the generated ssrc.
488 for (size_t i = 0; i < ssrcs.size(); ++i) {
489 stream_param.ssrcs.push_back(ssrcs[i]);
490 }
491 if (stream_it->num_sim_layers > 1) {
492 SsrcGroup group(kSimSsrcGroupSemantics, stream_param.ssrcs);
493 stream_param.ssrc_groups.push_back(group);
494 }
Noah Richards2e7a0982015-05-18 14:02:54 -0700495 // Generate extra ssrcs for include_rtx_streams case.
496 if (include_rtx_streams) {
497 // Generate an RTX ssrc for every ssrc in the group.
498 std::vector<uint32> rtx_ssrcs;
499 GenerateSsrcs(*current_streams, static_cast<int>(ssrcs.size()),
500 &rtx_ssrcs);
501 for (size_t i = 0; i < ssrcs.size(); ++i) {
502 stream_param.AddFidSsrc(ssrcs[i], rtx_ssrcs[i]);
503 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 content_description->set_multistream(true);
505 }
506 stream_param.cname = cname;
507 stream_param.sync_label = stream_it->sync_label;
508 content_description->AddStream(stream_param);
509
510 // Store the new StreamParams in current_streams.
511 // This is necessary so that we can use the CNAME for other media types.
512 current_streams->push_back(stream_param);
513 } else {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000514 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 }
516 }
517 return true;
518}
519
520// Updates the transport infos of the |sdesc| according to the given
521// |bundle_group|. The transport infos of the content names within the
522// |bundle_group| should be updated to use the ufrag and pwd of the first
523// content within the |bundle_group|.
524static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
525 SessionDescription* sdesc) {
526 // The bundle should not be empty.
527 if (!sdesc || !bundle_group.FirstContentName()) {
528 return false;
529 }
530
531 // We should definitely have a transport for the first content.
532 std::string selected_content_name = *bundle_group.FirstContentName();
533 const TransportInfo* selected_transport_info =
534 sdesc->GetTransportInfoByName(selected_content_name);
535 if (!selected_transport_info) {
536 return false;
537 }
538
539 // Set the other contents to use the same ICE credentials.
540 const std::string selected_ufrag =
541 selected_transport_info->description.ice_ufrag;
542 const std::string selected_pwd =
543 selected_transport_info->description.ice_pwd;
544 for (TransportInfos::iterator it =
545 sdesc->transport_infos().begin();
546 it != sdesc->transport_infos().end(); ++it) {
547 if (bundle_group.HasContentName(it->content_name) &&
548 it->content_name != selected_content_name) {
549 it->description.ice_ufrag = selected_ufrag;
550 it->description.ice_pwd = selected_pwd;
551 }
552 }
553 return true;
554}
555
556// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
557// sets it to |cryptos|.
558static bool GetCryptosByName(const SessionDescription* sdesc,
559 const std::string& content_name,
560 CryptoParamsVec* cryptos) {
561 if (!sdesc || !cryptos) {
562 return false;
563 }
564
565 const ContentInfo* content = sdesc->GetContentByName(content_name);
566 if (!IsMediaContent(content) || !content->description) {
567 return false;
568 }
569
570 const MediaContentDescription* media_desc =
571 static_cast<const MediaContentDescription*>(content->description);
572 *cryptos = media_desc->cryptos();
573 return true;
574}
575
576// Predicate function used by the remove_if.
577// Returns true if the |crypto|'s cipher_suite is not found in |filter|.
578static bool CryptoNotFound(const CryptoParams crypto,
579 const CryptoParamsVec* filter) {
580 if (filter == NULL) {
581 return true;
582 }
583 for (CryptoParamsVec::const_iterator it = filter->begin();
584 it != filter->end(); ++it) {
585 if (it->cipher_suite == crypto.cipher_suite) {
586 return false;
587 }
588 }
589 return true;
590}
591
592// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
593// which are not available in |filter|.
594static void PruneCryptos(const CryptoParamsVec& filter,
595 CryptoParamsVec* target_cryptos) {
596 if (!target_cryptos) {
597 return;
598 }
599 target_cryptos->erase(std::remove_if(target_cryptos->begin(),
600 target_cryptos->end(),
601 bind2nd(ptr_fun(CryptoNotFound),
602 &filter)),
603 target_cryptos->end());
604}
605
606static bool IsRtpContent(SessionDescription* sdesc,
607 const std::string& content_name) {
608 bool is_rtp = false;
609 ContentInfo* content = sdesc->GetContentByName(content_name);
610 if (IsMediaContent(content)) {
611 MediaContentDescription* media_desc =
612 static_cast<MediaContentDescription*>(content->description);
613 if (!media_desc) {
614 return false;
615 }
616 is_rtp = media_desc->protocol().empty() ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000617 rtc::starts_with(media_desc->protocol().data(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 kMediaProtocolRtpPrefix);
619 }
620 return is_rtp;
621}
622
623// Updates the crypto parameters of the |sdesc| according to the given
624// |bundle_group|. The crypto parameters of all the contents within the
625// |bundle_group| should be updated to use the common subset of the
626// available cryptos.
627static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
628 SessionDescription* sdesc) {
629 // The bundle should not be empty.
630 if (!sdesc || !bundle_group.FirstContentName()) {
631 return false;
632 }
633
wu@webrtc.org78187522013-10-07 23:32:02 +0000634 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 // Get the common cryptos.
636 const ContentNames& content_names = bundle_group.content_names();
637 CryptoParamsVec common_cryptos;
638 for (ContentNames::const_iterator it = content_names.begin();
639 it != content_names.end(); ++it) {
640 if (!IsRtpContent(sdesc, *it)) {
641 continue;
642 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000643 // The common cryptos are needed if any of the content does not have DTLS
644 // enabled.
645 if (!sdesc->GetTransportInfoByName(*it)->description.secure()) {
646 common_cryptos_needed = true;
647 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 if (it == content_names.begin()) {
649 // Initial the common_cryptos with the first content in the bundle group.
650 if (!GetCryptosByName(sdesc, *it, &common_cryptos)) {
651 return false;
652 }
653 if (common_cryptos.empty()) {
654 // If there's no crypto params, we should just return.
655 return true;
656 }
657 } else {
658 CryptoParamsVec cryptos;
659 if (!GetCryptosByName(sdesc, *it, &cryptos)) {
660 return false;
661 }
662 PruneCryptos(cryptos, &common_cryptos);
663 }
664 }
665
wu@webrtc.org78187522013-10-07 23:32:02 +0000666 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 return false;
668 }
669
670 // Update to use the common cryptos.
671 for (ContentNames::const_iterator it = content_names.begin();
672 it != content_names.end(); ++it) {
673 if (!IsRtpContent(sdesc, *it)) {
674 continue;
675 }
676 ContentInfo* content = sdesc->GetContentByName(*it);
677 if (IsMediaContent(content)) {
678 MediaContentDescription* media_desc =
679 static_cast<MediaContentDescription*>(content->description);
680 if (!media_desc) {
681 return false;
682 }
683 media_desc->set_cryptos(common_cryptos);
684 }
685 }
686 return true;
687}
688
689template <class C>
690static bool ContainsRtxCodec(const std::vector<C>& codecs) {
691 typename std::vector<C>::const_iterator it;
692 for (it = codecs.begin(); it != codecs.end(); ++it) {
693 if (IsRtxCodec(*it)) {
694 return true;
695 }
696 }
697 return false;
698}
699
700template <class C>
701static bool IsRtxCodec(const C& codec) {
702 return stricmp(codec.name.c_str(), kRtxCodecName) == 0;
703}
704
705// Create a media content to be offered in a session-initiate,
706// according to the given options.rtcp_mux, options.is_muc,
707// options.streams, codecs, secure_transport, crypto, and streams. If we don't
708// currently have crypto (in current_cryptos) and it is enabled (in
709// secure_policy), crypto is created (according to crypto_suites). If
710// add_legacy_stream is true, and current_streams is empty, a legacy
711// stream is created. The created content is added to the offer.
712template <class C>
713static bool CreateMediaContentOffer(
714 const MediaSessionOptions& options,
715 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000716 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 const CryptoParamsVec* current_cryptos,
718 const std::vector<std::string>& crypto_suites,
719 const RtpHeaderExtensions& rtp_extensions,
720 bool add_legacy_stream,
721 StreamParamsVec* current_streams,
722 MediaContentDescriptionImpl<C>* offer) {
723 offer->AddCodecs(codecs);
724 offer->SortCodecs();
725
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000726 if (secure_policy == SEC_REQUIRED) {
727 offer->set_crypto_required(CT_SDES);
728 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 offer->set_rtcp_mux(options.rtcp_mux_enabled);
730 offer->set_multistream(options.is_muc);
731 offer->set_rtp_header_extensions(rtp_extensions);
732
733 if (!AddStreamParams(
734 offer->type(), options.streams, current_streams,
735 offer, add_legacy_stream)) {
736 return false;
737 }
738
739#ifdef HAVE_SRTP
740 if (secure_policy != SEC_DISABLED) {
741 if (current_cryptos) {
742 AddMediaCryptos(*current_cryptos, offer);
743 }
744 if (offer->cryptos().empty()) {
745 if (!CreateMediaCryptos(crypto_suites, offer)) {
746 return false;
747 }
748 }
749 }
750#endif
751
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000752 if (offer->crypto_required() == CT_SDES && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 return false;
754 }
755 return true;
756}
757
758template <class C>
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000759static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
760 const std::string& codec1_id_str,
761 const std::vector<C>& codecs2,
762 const std::string& codec2_id_str) {
763 int codec1_id;
764 int codec2_id;
765 C codec1;
766 C codec2;
767 if (!rtc::FromString(codec1_id_str, &codec1_id) ||
768 !rtc::FromString(codec2_id_str, &codec2_id) ||
769 !FindCodecById(codecs1, codec1_id, &codec1) ||
770 !FindCodecById(codecs2, codec2_id, &codec2)) {
771 return false;
772 }
773 return codec1.Matches(codec2);
774}
775
776template <class C>
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777static void NegotiateCodecs(const std::vector<C>& local_codecs,
778 const std::vector<C>& offered_codecs,
779 std::vector<C>* negotiated_codecs) {
780 typename std::vector<C>::const_iterator ours;
781 for (ours = local_codecs.begin();
782 ours != local_codecs.end(); ++ours) {
783 typename std::vector<C>::const_iterator theirs;
784 for (theirs = offered_codecs.begin();
785 theirs != offered_codecs.end(); ++theirs) {
786 if (ours->Matches(*theirs)) {
787 C negotiated = *ours;
788 negotiated.IntersectFeedbackParams(*theirs);
789 if (IsRtxCodec(negotiated)) {
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000790 std::string offered_apt_value;
791 std::string local_apt_value;
792 if (!ours->GetParam(kCodecParamAssociatedPayloadType,
793 &local_apt_value) ||
794 !theirs->GetParam(kCodecParamAssociatedPayloadType,
795 &offered_apt_value)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 LOG(LS_WARNING) << "RTX missing associated payload type.";
797 continue;
798 }
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000799 // Only negotiate RTX if kCodecParamAssociatedPayloadType has been
800 // set in local and remote codecs, and they match.
801 if (!ReferencedCodecsMatch(local_codecs, local_apt_value,
802 offered_codecs, offered_apt_value)) {
803 LOG(LS_WARNING) << "RTX associated codecs don't match.";
804 continue;
805 }
806 negotiated.SetParam(kCodecParamAssociatedPayloadType,
807 offered_apt_value);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 }
changbin.shao@webrtc.org2d25b442015-03-16 04:14:34 +0000809
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 negotiated.id = theirs->id;
wu@webrtc.orgff1b1bf2014-06-20 20:57:42 +0000811 // RFC3264: Although the answerer MAY list the formats in their desired
812 // order of preference, it is RECOMMENDED that unless there is a
813 // specific reason, the answerer list formats in the same relative order
814 // they were present in the offer.
815 negotiated.preference = theirs->preference;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 negotiated_codecs->push_back(negotiated);
817 }
818 }
819 }
820}
821
822template <class C>
823static bool FindMatchingCodec(const std::vector<C>& codecs,
824 const C& codec_to_match,
825 C* found_codec) {
826 for (typename std::vector<C>::const_iterator it = codecs.begin();
827 it != codecs.end(); ++it) {
828 if (it->Matches(codec_to_match)) {
829 if (found_codec != NULL) {
830 *found_codec= *it;
831 }
832 return true;
833 }
834 }
835 return false;
836}
837
838// Adds all codecs from |reference_codecs| to |offered_codecs| that dont'
839// already exist in |offered_codecs| and ensure the payload types don't
840// collide.
841template <class C>
842static void FindCodecsToOffer(
843 const std::vector<C>& reference_codecs,
844 std::vector<C>* offered_codecs,
845 UsedPayloadTypes* used_pltypes) {
846
847 typedef std::map<int, C> RtxCodecReferences;
848 RtxCodecReferences new_rtx_codecs;
849
850 // Find all new RTX codecs.
851 for (typename std::vector<C>::const_iterator it = reference_codecs.begin();
852 it != reference_codecs.end(); ++it) {
853 if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && IsRtxCodec(*it)) {
854 C rtx_codec = *it;
855 int referenced_pl_type =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000856 rtc::FromString<int>(0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 rtx_codec.params[kCodecParamAssociatedPayloadType]);
858 new_rtx_codecs.insert(std::pair<int, C>(referenced_pl_type,
859 rtx_codec));
860 }
861 }
862
863 // Add all new codecs that are not RTX codecs.
864 for (typename std::vector<C>::const_iterator it = reference_codecs.begin();
865 it != reference_codecs.end(); ++it) {
866 if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && !IsRtxCodec(*it)) {
867 C codec = *it;
868 int original_payload_id = codec.id;
869 used_pltypes->FindAndSetIdUsed(&codec);
870 offered_codecs->push_back(codec);
871
872 // If this codec is referenced by a new RTX codec, update the reference
873 // in the RTX codec with the new payload type.
874 typename RtxCodecReferences::iterator rtx_it =
875 new_rtx_codecs.find(original_payload_id);
876 if (rtx_it != new_rtx_codecs.end()) {
877 C& rtx_codec = rtx_it->second;
878 rtx_codec.params[kCodecParamAssociatedPayloadType] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000879 rtc::ToString(codec.id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881 }
882 }
883
884 // Add all new RTX codecs.
885 for (typename RtxCodecReferences::iterator it = new_rtx_codecs.begin();
886 it != new_rtx_codecs.end(); ++it) {
887 C& rtx_codec = it->second;
888 used_pltypes->FindAndSetIdUsed(&rtx_codec);
889 offered_codecs->push_back(rtx_codec);
890 }
891}
892
893
894static bool FindByUri(const RtpHeaderExtensions& extensions,
895 const RtpHeaderExtension& ext_to_match,
896 RtpHeaderExtension* found_extension) {
897 for (RtpHeaderExtensions::const_iterator it = extensions.begin();
898 it != extensions.end(); ++it) {
899 // We assume that all URIs are given in a canonical format.
900 if (it->uri == ext_to_match.uri) {
901 if (found_extension != NULL) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000902 *found_extension = *it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 }
904 return true;
905 }
906 }
907 return false;
908}
909
910static void FindAndSetRtpHdrExtUsed(
911 const RtpHeaderExtensions& reference_extensions,
912 RtpHeaderExtensions* offered_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000913 const RtpHeaderExtensions& other_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 UsedRtpHeaderExtensionIds* used_extensions) {
915 for (RtpHeaderExtensions::const_iterator it = reference_extensions.begin();
916 it != reference_extensions.end(); ++it) {
917 if (!FindByUri(*offered_extensions, *it, NULL)) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000918 RtpHeaderExtension ext;
919 if (!FindByUri(other_extensions, *it, &ext)) {
920 ext = *it;
921 used_extensions->FindAndSetIdUsed(&ext);
922 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 offered_extensions->push_back(ext);
924 }
925 }
926}
927
928static void NegotiateRtpHeaderExtensions(
929 const RtpHeaderExtensions& local_extensions,
930 const RtpHeaderExtensions& offered_extensions,
931 RtpHeaderExtensions* negotiated_extenstions) {
932 RtpHeaderExtensions::const_iterator ours;
933 for (ours = local_extensions.begin();
934 ours != local_extensions.end(); ++ours) {
935 RtpHeaderExtension theirs;
936 if (FindByUri(offered_extensions, *ours, &theirs)) {
937 // We respond with their RTP header extension id.
938 negotiated_extenstions->push_back(theirs);
939 }
940 }
941}
942
943static void StripCNCodecs(AudioCodecs* audio_codecs) {
944 AudioCodecs::iterator iter = audio_codecs->begin();
945 while (iter != audio_codecs->end()) {
946 if (stricmp(iter->name.c_str(), kComfortNoiseCodecName) == 0) {
947 iter = audio_codecs->erase(iter);
948 } else {
949 ++iter;
950 }
951 }
952}
953
954// Create a media content to be answered in a session-accept,
955// according to the given options.rtcp_mux, options.streams, codecs,
956// crypto, and streams. If we don't currently have crypto (in
957// current_cryptos) and it is enabled (in secure_policy), crypto is
958// created (according to crypto_suites). If add_legacy_stream is
959// true, and current_streams is empty, a legacy stream is created.
960// The codecs, rtcp_mux, and crypto are all negotiated with the offer
961// from the incoming session-initiate. If the negotiation fails, this
962// method returns false. The created content is added to the offer.
963template <class C>
964static bool CreateMediaContentAnswer(
965 const MediaContentDescriptionImpl<C>* offer,
966 const MediaSessionOptions& options,
967 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000968 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 const CryptoParamsVec* current_cryptos,
970 const RtpHeaderExtensions& local_rtp_extenstions,
971 StreamParamsVec* current_streams,
972 bool add_legacy_stream,
973 bool bundle_enabled,
974 MediaContentDescriptionImpl<C>* answer) {
975 std::vector<C> negotiated_codecs;
976 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
977 answer->AddCodecs(negotiated_codecs);
978 answer->SortCodecs();
979 answer->set_protocol(offer->protocol());
980 RtpHeaderExtensions negotiated_rtp_extensions;
981 NegotiateRtpHeaderExtensions(local_rtp_extenstions,
982 offer->rtp_header_extensions(),
983 &negotiated_rtp_extensions);
984 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
985
986 answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux());
987
988 if (sdes_policy != SEC_DISABLED) {
989 CryptoParams crypto;
990 if (SelectCrypto(offer, bundle_enabled, &crypto)) {
991 if (current_cryptos) {
992 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
993 }
994 answer->AddCrypto(crypto);
995 }
996 }
997
998 if (answer->cryptos().empty() &&
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000999 (offer->crypto_required() == CT_SDES || sdes_policy == SEC_REQUIRED)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 return false;
1001 }
1002
1003 if (!AddStreamParams(
1004 answer->type(), options.streams, current_streams,
1005 answer, add_legacy_stream)) {
1006 return false; // Something went seriously wrong.
1007 }
1008
1009 // Make sure the answer media content direction is per default set as
1010 // described in RFC3264 section 6.1.
1011 switch (offer->direction()) {
1012 case MD_INACTIVE:
1013 answer->set_direction(MD_INACTIVE);
1014 break;
1015 case MD_SENDONLY:
1016 answer->set_direction(MD_RECVONLY);
1017 break;
1018 case MD_RECVONLY:
1019 answer->set_direction(MD_SENDONLY);
1020 break;
1021 case MD_SENDRECV:
1022 answer->set_direction(MD_SENDRECV);
1023 break;
1024 default:
1025 break;
1026 }
1027
1028 return true;
1029}
1030
1031static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001032 const std::string& protocol,
1033 bool secure_transport) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 // Data channels can have a protocol of SCTP or SCTP/DTLS.
1035 if (type == MEDIA_TYPE_DATA &&
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001036 ((protocol == kMediaProtocolSctp && !secure_transport)||
1037 (protocol == kMediaProtocolDtlsSctp && secure_transport))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 return true;
1039 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001040
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 // Since not all applications serialize and deserialize the media protocol,
1042 // we will have to accept |protocol| to be empty.
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001043 return protocol == kMediaProtocolAvpf || protocol.empty() ||
1044 protocol == kMediaProtocolSavpf ||
1045 (protocol == kMediaProtocolDtlsSavpf && secure_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046}
1047
1048static void SetMediaProtocol(bool secure_transport,
1049 MediaContentDescription* desc) {
1050 if (!desc->cryptos().empty() || secure_transport)
1051 desc->set_protocol(kMediaProtocolSavpf);
1052 else
1053 desc->set_protocol(kMediaProtocolAvpf);
1054}
1055
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001056// Gets the TransportInfo of the given |content_name| from the
1057// |current_description|. If doesn't exist, returns a new one.
1058static const TransportDescription* GetTransportDescription(
1059 const std::string& content_name,
1060 const SessionDescription* current_description) {
1061 const TransportDescription* desc = NULL;
1062 if (current_description) {
1063 const TransportInfo* info =
1064 current_description->GetTransportInfoByName(content_name);
1065 if (info) {
1066 desc = &info->description;
1067 }
1068 }
1069 return desc;
1070}
1071
1072// Gets the current DTLS state from the transport description.
1073static bool IsDtlsActive(
1074 const std::string& content_name,
1075 const SessionDescription* current_description) {
1076 if (!current_description)
1077 return false;
1078
1079 const ContentInfo* content =
1080 current_description->GetContentByName(content_name);
1081 if (!content)
1082 return false;
1083
1084 const TransportDescription* current_tdesc =
1085 GetTransportDescription(content_name, current_description);
1086 if (!current_tdesc)
1087 return false;
1088
1089 return current_tdesc->secure();
1090}
1091
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001092std::string MediaTypeToString(MediaType type) {
1093 std::string type_str;
1094 switch (type) {
1095 case MEDIA_TYPE_AUDIO:
1096 type_str = "audio";
1097 break;
1098 case MEDIA_TYPE_VIDEO:
1099 type_str = "video";
1100 break;
1101 case MEDIA_TYPE_DATA:
1102 type_str = "data";
1103 break;
1104 default:
1105 ASSERT(false);
1106 break;
1107 }
1108 return type_str;
1109}
1110
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001111void MediaSessionOptions::AddSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 const std::string& id,
1113 const std::string& sync_label) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001114 AddSendStreamInternal(type, id, sync_label, 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001115}
1116
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001117void MediaSessionOptions::AddSendVideoStream(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001118 const std::string& id,
1119 const std::string& sync_label,
1120 int num_sim_layers) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001121 AddSendStreamInternal(MEDIA_TYPE_VIDEO, id, sync_label, num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001122}
1123
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001124void MediaSessionOptions::AddSendStreamInternal(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001125 MediaType type,
1126 const std::string& id,
1127 const std::string& sync_label,
1128 int num_sim_layers) {
1129 streams.push_back(Stream(type, id, sync_label, num_sim_layers));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 // If we haven't already set the data_channel_type, and we add a
1132 // stream, we assume it's an RTP data stream.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001133 if (type == MEDIA_TYPE_DATA && data_channel_type == DCT_NONE)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 data_channel_type = DCT_RTP;
1135}
1136
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001137void MediaSessionOptions::RemoveSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 const std::string& id) {
1139 Streams::iterator stream_it = streams.begin();
1140 for (; stream_it != streams.end(); ++stream_it) {
1141 if (stream_it->type == type && stream_it->id == id) {
1142 streams.erase(stream_it);
1143 return;
1144 }
1145 }
1146 ASSERT(false);
1147}
1148
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001149bool MediaSessionOptions::HasSendMediaStream(MediaType type) const {
1150 Streams::const_iterator stream_it = streams.begin();
1151 for (; stream_it != streams.end(); ++stream_it) {
1152 if (stream_it->type == type) {
1153 return true;
1154 }
1155 }
1156 return false;
1157}
1158
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1160 const TransportDescriptionFactory* transport_desc_factory)
1161 : secure_(SEC_DISABLED),
1162 add_legacy_(true),
1163 transport_desc_factory_(transport_desc_factory) {
1164}
1165
1166MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1167 ChannelManager* channel_manager,
1168 const TransportDescriptionFactory* transport_desc_factory)
1169 : secure_(SEC_DISABLED),
1170 add_legacy_(true),
1171 transport_desc_factory_(transport_desc_factory) {
1172 channel_manager->GetSupportedAudioCodecs(&audio_codecs_);
1173 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
1174 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
1175 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1176 channel_manager->GetSupportedDataCodecs(&data_codecs_);
1177}
1178
1179SessionDescription* MediaSessionDescriptionFactory::CreateOffer(
1180 const MediaSessionOptions& options,
1181 const SessionDescription* current_description) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 scoped_ptr<SessionDescription> offer(new SessionDescription());
1183
1184 StreamParamsVec current_streams;
1185 GetCurrentStreamParams(current_description, &current_streams);
1186
1187 AudioCodecs audio_codecs;
1188 VideoCodecs video_codecs;
1189 DataCodecs data_codecs;
1190 GetCodecsToOffer(current_description, &audio_codecs, &video_codecs,
1191 &data_codecs);
1192
1193 if (!options.vad_enabled) {
1194 // If application doesn't want CN codecs in offer.
1195 StripCNCodecs(&audio_codecs);
1196 }
1197
1198 RtpHeaderExtensions audio_rtp_extensions;
1199 RtpHeaderExtensions video_rtp_extensions;
1200 GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions,
1201 &video_rtp_extensions);
1202
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001203 bool audio_added = false;
1204 bool video_added = false;
1205 bool data_added = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001206
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001207 // Iterate through the contents of |current_description| to maintain the order
1208 // of the m-lines in the new offer.
1209 if (current_description) {
1210 ContentInfos::const_iterator it = current_description->contents().begin();
1211 for (; it != current_description->contents().end(); ++it) {
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001212 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001213 if (!AddAudioContentForOffer(options, current_description,
1214 audio_rtp_extensions, audio_codecs,
1215 &current_streams, offer.get())) {
1216 return NULL;
1217 }
1218 audio_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001219 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001220 if (!AddVideoContentForOffer(options, current_description,
1221 video_rtp_extensions, video_codecs,
1222 &current_streams, offer.get())) {
1223 return NULL;
1224 }
1225 video_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001226 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_DATA)) {
tommi@webrtc.orgf15dee62014-10-27 22:15:04 +00001227 MediaSessionOptions options_copy(options);
1228 if (IsSctp(static_cast<const MediaContentDescription*>(
1229 it->description))) {
1230 options_copy.data_channel_type = DCT_SCTP;
1231 }
1232 if (!AddDataContentForOffer(options_copy, current_description,
1233 &data_codecs, &current_streams,
1234 offer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001235 return NULL;
1236 }
1237 data_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001238 } else {
1239 ASSERT(false);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001240 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 }
1242 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001243
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001244 // Append contents that are not in |current_description|.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001245 if (!audio_added && options.has_audio() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001246 !AddAudioContentForOffer(options, current_description,
1247 audio_rtp_extensions, audio_codecs,
1248 &current_streams, offer.get())) {
1249 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001251 if (!video_added && options.has_video() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001252 !AddVideoContentForOffer(options, current_description,
1253 video_rtp_extensions, video_codecs,
1254 &current_streams, offer.get())) {
1255 return NULL;
1256 }
1257 if (!data_added && options.has_data() &&
1258 !AddDataContentForOffer(options, current_description, &data_codecs,
1259 &current_streams, offer.get())) {
1260 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 }
1262
1263 // Bundle the contents together, if we've been asked to do so, and update any
1264 // parameters that need to be tweaked for BUNDLE.
1265 if (options.bundle_enabled) {
1266 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
1267 for (ContentInfos::const_iterator content = offer->contents().begin();
1268 content != offer->contents().end(); ++content) {
1269 offer_bundle.AddContentName(content->name);
1270 }
1271 offer->AddGroup(offer_bundle);
1272 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1273 LOG(LS_ERROR) << "CreateOffer failed to UpdateTransportInfoForBundle.";
1274 return NULL;
1275 }
1276 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1277 LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1278 return NULL;
1279 }
1280 }
1281
1282 return offer.release();
1283}
1284
1285SessionDescription* MediaSessionDescriptionFactory::CreateAnswer(
1286 const SessionDescription* offer, const MediaSessionOptions& options,
1287 const SessionDescription* current_description) const {
1288 // The answer contains the intersection of the codecs in the offer with the
1289 // codecs we support, ordered by our local preference. As indicated by
1290 // XEP-0167, we retain the same payload ids from the offer in the answer.
1291 scoped_ptr<SessionDescription> answer(new SessionDescription());
1292
1293 StreamParamsVec current_streams;
1294 GetCurrentStreamParams(current_description, &current_streams);
1295
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001296 if (offer) {
1297 ContentInfos::const_iterator it = offer->contents().begin();
1298 for (; it != offer->contents().end(); ++it) {
1299 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
1300 if (!AddAudioContentForAnswer(offer, options, current_description,
1301 &current_streams, answer.get())) {
1302 return NULL;
1303 }
1304 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
1305 if (!AddVideoContentForAnswer(offer, options, current_description,
1306 &current_streams, answer.get())) {
1307 return NULL;
1308 }
1309 } else {
1310 ASSERT(IsMediaContentOfType(&*it, MEDIA_TYPE_DATA));
1311 if (!AddDataContentForAnswer(offer, options, current_description,
1312 &current_streams, answer.get())) {
1313 return NULL;
1314 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 }
1318
1319 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1320 // group in the answer with the appropriate content names.
1321 if (offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled) {
1322 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1323 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1324 for (ContentInfos::const_iterator content = answer->contents().begin();
1325 content != answer->contents().end(); ++content) {
1326 if (!content->rejected && offer_bundle->HasContentName(content->name)) {
1327 answer_bundle.AddContentName(content->name);
1328 }
1329 }
1330 if (answer_bundle.FirstContentName()) {
1331 answer->AddGroup(answer_bundle);
1332
1333 // Share the same ICE credentials and crypto params across all contents,
1334 // as BUNDLE requires.
1335 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
1336 LOG(LS_ERROR) << "CreateAnswer failed to UpdateTransportInfoForBundle.";
1337 return NULL;
1338 }
1339
1340 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
1341 LOG(LS_ERROR) << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
1342 return NULL;
1343 }
1344 }
1345 }
1346
1347 return answer.release();
1348}
1349
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350void MediaSessionDescriptionFactory::GetCodecsToOffer(
1351 const SessionDescription* current_description,
1352 AudioCodecs* audio_codecs,
1353 VideoCodecs* video_codecs,
1354 DataCodecs* data_codecs) const {
1355 UsedPayloadTypes used_pltypes;
1356 audio_codecs->clear();
1357 video_codecs->clear();
1358 data_codecs->clear();
1359
1360
1361 // First - get all codecs from the current description if the media type
1362 // is used.
1363 // Add them to |used_pltypes| so the payloadtype is not reused if a new media
1364 // type is added.
1365 if (current_description) {
1366 const AudioContentDescription* audio =
1367 GetFirstAudioContentDescription(current_description);
1368 if (audio) {
1369 *audio_codecs = audio->codecs();
1370 used_pltypes.FindAndSetIdUsed<AudioCodec>(audio_codecs);
1371 }
1372 const VideoContentDescription* video =
1373 GetFirstVideoContentDescription(current_description);
1374 if (video) {
1375 *video_codecs = video->codecs();
1376 used_pltypes.FindAndSetIdUsed<VideoCodec>(video_codecs);
1377 }
1378 const DataContentDescription* data =
1379 GetFirstDataContentDescription(current_description);
1380 if (data) {
1381 *data_codecs = data->codecs();
1382 used_pltypes.FindAndSetIdUsed<DataCodec>(data_codecs);
1383 }
1384 }
1385
1386 // Add our codecs that are not in |current_description|.
1387 FindCodecsToOffer<AudioCodec>(audio_codecs_, audio_codecs, &used_pltypes);
1388 FindCodecsToOffer<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1389 FindCodecsToOffer<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1390}
1391
1392void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
1393 const SessionDescription* current_description,
1394 RtpHeaderExtensions* audio_extensions,
1395 RtpHeaderExtensions* video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001396 // All header extensions allocated from the same range to avoid potential
1397 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398 UsedRtpHeaderExtensionIds used_ids;
1399 audio_extensions->clear();
1400 video_extensions->clear();
1401
1402 // First - get all extensions from the current description if the media type
1403 // is used.
1404 // Add them to |used_ids| so the local ids are not reused if a new media
1405 // type is added.
1406 if (current_description) {
1407 const AudioContentDescription* audio =
1408 GetFirstAudioContentDescription(current_description);
1409 if (audio) {
1410 *audio_extensions = audio->rtp_header_extensions();
1411 used_ids.FindAndSetIdUsed(audio_extensions);
1412 }
1413 const VideoContentDescription* video =
1414 GetFirstVideoContentDescription(current_description);
1415 if (video) {
1416 *video_extensions = video->rtp_header_extensions();
1417 used_ids.FindAndSetIdUsed(video_extensions);
1418 }
1419 }
1420
1421 // Add our default RTP header extensions that are not in
1422 // |current_description|.
1423 FindAndSetRtpHdrExtUsed(audio_rtp_header_extensions(), audio_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001424 *video_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 FindAndSetRtpHdrExtUsed(video_rtp_header_extensions(), video_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001426 *audio_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427}
1428
1429bool MediaSessionDescriptionFactory::AddTransportOffer(
1430 const std::string& content_name,
1431 const TransportOptions& transport_options,
1432 const SessionDescription* current_desc,
1433 SessionDescription* offer_desc) const {
1434 if (!transport_desc_factory_)
1435 return false;
1436 const TransportDescription* current_tdesc =
1437 GetTransportDescription(content_name, current_desc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001438 rtc::scoped_ptr<TransportDescription> new_tdesc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 transport_desc_factory_->CreateOffer(transport_options, current_tdesc));
1440 bool ret = (new_tdesc.get() != NULL &&
1441 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)));
1442 if (!ret) {
1443 LOG(LS_ERROR)
1444 << "Failed to AddTransportOffer, content name=" << content_name;
1445 }
1446 return ret;
1447}
1448
1449TransportDescription* MediaSessionDescriptionFactory::CreateTransportAnswer(
1450 const std::string& content_name,
1451 const SessionDescription* offer_desc,
1452 const TransportOptions& transport_options,
1453 const SessionDescription* current_desc) const {
1454 if (!transport_desc_factory_)
1455 return NULL;
1456 const TransportDescription* offer_tdesc =
1457 GetTransportDescription(content_name, offer_desc);
1458 const TransportDescription* current_tdesc =
1459 GetTransportDescription(content_name, current_desc);
1460 return
1461 transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1462 current_tdesc);
1463}
1464
1465bool MediaSessionDescriptionFactory::AddTransportAnswer(
1466 const std::string& content_name,
1467 const TransportDescription& transport_desc,
1468 SessionDescription* answer_desc) const {
1469 if (!answer_desc->AddTransportInfo(TransportInfo(content_name,
1470 transport_desc))) {
1471 LOG(LS_ERROR)
1472 << "Failed to AddTransportAnswer, content name=" << content_name;
1473 return false;
1474 }
1475 return true;
1476}
1477
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001478bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
1479 const MediaSessionOptions& options,
1480 const SessionDescription* current_description,
1481 const RtpHeaderExtensions& audio_rtp_extensions,
1482 const AudioCodecs& audio_codecs,
1483 StreamParamsVec* current_streams,
1484 SessionDescription* desc) const {
1485 cricket::SecurePolicy sdes_policy =
1486 IsDtlsActive(CN_AUDIO, current_description) ?
1487 cricket::SEC_DISABLED : secure();
1488
1489 scoped_ptr<AudioContentDescription> audio(new AudioContentDescription());
1490 std::vector<std::string> crypto_suites;
1491 GetSupportedAudioCryptoSuites(&crypto_suites);
1492 if (!CreateMediaContentOffer(
1493 options,
1494 audio_codecs,
1495 sdes_policy,
1496 GetCryptos(GetFirstAudioContentDescription(current_description)),
1497 crypto_suites,
1498 audio_rtp_extensions,
1499 add_legacy_,
1500 current_streams,
1501 audio.get())) {
1502 return false;
1503 }
1504 audio->set_lang(lang_);
1505
1506 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1507 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001508
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001509 if (!options.recv_audio) {
1510 audio->set_direction(MD_SENDONLY);
1511 }
1512
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001513 desc->AddContent(CN_AUDIO, NS_JINGLE_RTP, audio.release());
1514 if (!AddTransportOffer(CN_AUDIO, options.transport_options,
1515 current_description, desc)) {
1516 return false;
1517 }
1518
1519 return true;
1520}
1521
1522bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
1523 const MediaSessionOptions& options,
1524 const SessionDescription* current_description,
1525 const RtpHeaderExtensions& video_rtp_extensions,
1526 const VideoCodecs& video_codecs,
1527 StreamParamsVec* current_streams,
1528 SessionDescription* desc) const {
1529 cricket::SecurePolicy sdes_policy =
1530 IsDtlsActive(CN_VIDEO, current_description) ?
1531 cricket::SEC_DISABLED : secure();
1532
1533 scoped_ptr<VideoContentDescription> video(new VideoContentDescription());
1534 std::vector<std::string> crypto_suites;
1535 GetSupportedVideoCryptoSuites(&crypto_suites);
1536 if (!CreateMediaContentOffer(
1537 options,
1538 video_codecs,
1539 sdes_policy,
1540 GetCryptos(GetFirstVideoContentDescription(current_description)),
1541 crypto_suites,
1542 video_rtp_extensions,
1543 add_legacy_,
1544 current_streams,
1545 video.get())) {
1546 return false;
1547 }
1548
1549 video->set_bandwidth(options.video_bandwidth);
1550
1551 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1552 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001553
1554 if (!options.recv_video) {
1555 video->set_direction(MD_SENDONLY);
1556 }
1557
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001558 desc->AddContent(CN_VIDEO, NS_JINGLE_RTP, video.release());
1559 if (!AddTransportOffer(CN_VIDEO, options.transport_options,
1560 current_description, desc)) {
1561 return false;
1562 }
1563
1564 return true;
1565}
1566
1567bool MediaSessionDescriptionFactory::AddDataContentForOffer(
1568 const MediaSessionOptions& options,
1569 const SessionDescription* current_description,
1570 DataCodecs* data_codecs,
1571 StreamParamsVec* current_streams,
1572 SessionDescription* desc) const {
1573 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1574
1575 scoped_ptr<DataContentDescription> data(new DataContentDescription());
1576 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1577
1578 FilterDataCodecs(data_codecs, is_sctp);
1579
1580 cricket::SecurePolicy sdes_policy =
1581 IsDtlsActive(CN_DATA, current_description) ?
1582 cricket::SEC_DISABLED : secure();
1583 std::vector<std::string> crypto_suites;
1584 if (is_sctp) {
1585 // SDES doesn't make sense for SCTP, so we disable it, and we only
1586 // get SDES crypto suites for RTP-based data channels.
1587 sdes_policy = cricket::SEC_DISABLED;
1588 // Unlike SetMediaProtocol below, we need to set the protocol
1589 // before we call CreateMediaContentOffer. Otherwise,
1590 // CreateMediaContentOffer won't know this is SCTP and will
1591 // generate SSRCs rather than SIDs.
1592 data->set_protocol(
1593 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp);
1594 } else {
1595 GetSupportedDataCryptoSuites(&crypto_suites);
1596 }
1597
1598 if (!CreateMediaContentOffer(
1599 options,
1600 *data_codecs,
1601 sdes_policy,
1602 GetCryptos(GetFirstDataContentDescription(current_description)),
1603 crypto_suites,
1604 RtpHeaderExtensions(),
1605 add_legacy_,
1606 current_streams,
1607 data.get())) {
1608 return false;
1609 }
1610
1611 if (is_sctp) {
1612 desc->AddContent(CN_DATA, NS_JINGLE_DRAFT_SCTP, data.release());
1613 } else {
1614 data->set_bandwidth(options.data_bandwidth);
1615 SetMediaProtocol(secure_transport, data.get());
1616 desc->AddContent(CN_DATA, NS_JINGLE_RTP, data.release());
1617 }
1618 if (!AddTransportOffer(CN_DATA, options.transport_options,
1619 current_description, desc)) {
1620 return false;
1621 }
1622 return true;
1623}
1624
1625bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
1626 const SessionDescription* offer,
1627 const MediaSessionOptions& options,
1628 const SessionDescription* current_description,
1629 StreamParamsVec* current_streams,
1630 SessionDescription* answer) const {
1631 const ContentInfo* audio_content = GetFirstAudioContent(offer);
1632
1633 scoped_ptr<TransportDescription> audio_transport(
1634 CreateTransportAnswer(audio_content->name, offer,
1635 options.transport_options,
1636 current_description));
1637 if (!audio_transport) {
1638 return false;
1639 }
1640
1641 AudioCodecs audio_codecs = audio_codecs_;
1642 if (!options.vad_enabled) {
1643 StripCNCodecs(&audio_codecs);
1644 }
1645
1646 bool bundle_enabled =
1647 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1648 scoped_ptr<AudioContentDescription> audio_answer(
1649 new AudioContentDescription());
1650 // Do not require or create SDES cryptos if DTLS is used.
1651 cricket::SecurePolicy sdes_policy =
1652 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
1653 if (!CreateMediaContentAnswer(
1654 static_cast<const AudioContentDescription*>(
1655 audio_content->description),
1656 options,
1657 audio_codecs,
1658 sdes_policy,
1659 GetCryptos(GetFirstAudioContentDescription(current_description)),
1660 audio_rtp_extensions_,
1661 current_streams,
1662 add_legacy_,
1663 bundle_enabled,
1664 audio_answer.get())) {
1665 return false; // Fails the session setup.
1666 }
1667
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001668 bool rejected = !options.has_audio() || audio_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001669 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
1670 audio_answer->protocol(),
1671 audio_transport->secure());
1672 if (!rejected) {
1673 AddTransportAnswer(audio_content->name, *(audio_transport.get()), answer);
1674 } else {
1675 // RFC 3264
1676 // The answer MUST contain the same number of m-lines as the offer.
1677 LOG(LS_INFO) << "Audio is not supported in the answer.";
1678 }
1679
1680 answer->AddContent(audio_content->name, audio_content->type, rejected,
1681 audio_answer.release());
1682 return true;
1683}
1684
1685bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
1686 const SessionDescription* offer,
1687 const MediaSessionOptions& options,
1688 const SessionDescription* current_description,
1689 StreamParamsVec* current_streams,
1690 SessionDescription* answer) const {
1691 const ContentInfo* video_content = GetFirstVideoContent(offer);
1692 scoped_ptr<TransportDescription> video_transport(
1693 CreateTransportAnswer(video_content->name, offer,
1694 options.transport_options,
1695 current_description));
1696 if (!video_transport) {
1697 return false;
1698 }
1699
1700 scoped_ptr<VideoContentDescription> video_answer(
1701 new VideoContentDescription());
1702 // Do not require or create SDES cryptos if DTLS is used.
1703 cricket::SecurePolicy sdes_policy =
1704 video_transport->secure() ? cricket::SEC_DISABLED : secure();
1705 bool bundle_enabled =
1706 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1707 if (!CreateMediaContentAnswer(
1708 static_cast<const VideoContentDescription*>(
1709 video_content->description),
1710 options,
1711 video_codecs_,
1712 sdes_policy,
1713 GetCryptos(GetFirstVideoContentDescription(current_description)),
1714 video_rtp_extensions_,
1715 current_streams,
1716 add_legacy_,
1717 bundle_enabled,
1718 video_answer.get())) {
1719 return false;
1720 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001721 bool rejected = !options.has_video() || video_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001722 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
1723 video_answer->protocol(),
1724 video_transport->secure());
1725 if (!rejected) {
1726 if (!AddTransportAnswer(video_content->name, *(video_transport.get()),
1727 answer)) {
1728 return false;
1729 }
1730 video_answer->set_bandwidth(options.video_bandwidth);
1731 } else {
1732 // RFC 3264
1733 // The answer MUST contain the same number of m-lines as the offer.
1734 LOG(LS_INFO) << "Video is not supported in the answer.";
1735 }
1736 answer->AddContent(video_content->name, video_content->type, rejected,
1737 video_answer.release());
1738 return true;
1739}
1740
1741bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
1742 const SessionDescription* offer,
1743 const MediaSessionOptions& options,
1744 const SessionDescription* current_description,
1745 StreamParamsVec* current_streams,
1746 SessionDescription* answer) const {
1747 const ContentInfo* data_content = GetFirstDataContent(offer);
1748 scoped_ptr<TransportDescription> data_transport(
1749 CreateTransportAnswer(data_content->name, offer,
1750 options.transport_options,
1751 current_description));
1752 if (!data_transport) {
1753 return false;
1754 }
1755 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1756 std::vector<DataCodec> data_codecs(data_codecs_);
1757 FilterDataCodecs(&data_codecs, is_sctp);
1758
1759 scoped_ptr<DataContentDescription> data_answer(
1760 new DataContentDescription());
1761 // Do not require or create SDES cryptos if DTLS is used.
1762 cricket::SecurePolicy sdes_policy =
1763 data_transport->secure() ? cricket::SEC_DISABLED : secure();
1764 bool bundle_enabled =
1765 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1766 if (!CreateMediaContentAnswer(
1767 static_cast<const DataContentDescription*>(
1768 data_content->description),
1769 options,
1770 data_codecs_,
1771 sdes_policy,
1772 GetCryptos(GetFirstDataContentDescription(current_description)),
1773 RtpHeaderExtensions(),
1774 current_streams,
1775 add_legacy_,
1776 bundle_enabled,
1777 data_answer.get())) {
1778 return false; // Fails the session setup.
1779 }
1780
1781 bool rejected = !options.has_data() || data_content->rejected ||
1782 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
1783 data_answer->protocol(),
1784 data_transport->secure());
1785 if (!rejected) {
1786 data_answer->set_bandwidth(options.data_bandwidth);
1787 if (!AddTransportAnswer(data_content->name, *(data_transport.get()),
1788 answer)) {
1789 return false;
1790 }
1791 } else {
1792 // RFC 3264
1793 // The answer MUST contain the same number of m-lines as the offer.
1794 LOG(LS_INFO) << "Data is not supported in the answer.";
1795 }
1796 answer->AddContent(data_content->name, data_content->type, rejected,
1797 data_answer.release());
1798 return true;
1799}
1800
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801bool IsMediaContent(const ContentInfo* content) {
1802 return (content &&
1803 (content->type == NS_JINGLE_RTP ||
1804 content->type == NS_JINGLE_DRAFT_SCTP));
1805}
1806
1807bool IsAudioContent(const ContentInfo* content) {
1808 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
1809}
1810
1811bool IsVideoContent(const ContentInfo* content) {
1812 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
1813}
1814
1815bool IsDataContent(const ContentInfo* content) {
1816 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
1817}
1818
1819static const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
1820 MediaType media_type) {
1821 for (ContentInfos::const_iterator content = contents.begin();
1822 content != contents.end(); content++) {
1823 if (IsMediaContentOfType(&*content, media_type)) {
1824 return &*content;
1825 }
1826 }
1827 return NULL;
1828}
1829
1830const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
1831 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
1832}
1833
1834const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
1835 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
1836}
1837
1838const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
1839 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
1840}
1841
1842static const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
1843 MediaType media_type) {
1844 if (sdesc == NULL)
1845 return NULL;
1846
1847 return GetFirstMediaContent(sdesc->contents(), media_type);
1848}
1849
1850const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
1851 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
1852}
1853
1854const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
1855 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
1856}
1857
1858const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
1859 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
1860}
1861
1862const MediaContentDescription* GetFirstMediaContentDescription(
1863 const SessionDescription* sdesc, MediaType media_type) {
1864 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
1865 const ContentDescription* description = content ? content->description : NULL;
1866 return static_cast<const MediaContentDescription*>(description);
1867}
1868
1869const AudioContentDescription* GetFirstAudioContentDescription(
1870 const SessionDescription* sdesc) {
1871 return static_cast<const AudioContentDescription*>(
1872 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
1873}
1874
1875const VideoContentDescription* GetFirstVideoContentDescription(
1876 const SessionDescription* sdesc) {
1877 return static_cast<const VideoContentDescription*>(
1878 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
1879}
1880
1881const DataContentDescription* GetFirstDataContentDescription(
1882 const SessionDescription* sdesc) {
1883 return static_cast<const DataContentDescription*>(
1884 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
1885}
1886
1887bool GetMediaChannelNameFromComponent(
1888 int component, MediaType media_type, std::string* channel_name) {
1889 if (media_type == MEDIA_TYPE_AUDIO) {
1890 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1891 *channel_name = GICE_CHANNEL_NAME_RTP;
1892 return true;
1893 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1894 *channel_name = GICE_CHANNEL_NAME_RTCP;
1895 return true;
1896 }
1897 } else if (media_type == MEDIA_TYPE_VIDEO) {
1898 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1899 *channel_name = GICE_CHANNEL_NAME_VIDEO_RTP;
1900 return true;
1901 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1902 *channel_name = GICE_CHANNEL_NAME_VIDEO_RTCP;
1903 return true;
1904 }
1905 } else if (media_type == MEDIA_TYPE_DATA) {
1906 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1907 *channel_name = GICE_CHANNEL_NAME_DATA_RTP;
1908 return true;
1909 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1910 *channel_name = GICE_CHANNEL_NAME_DATA_RTCP;
1911 return true;
1912 }
1913 }
1914
1915 return false;
1916}
1917
1918bool GetMediaComponentFromChannelName(
1919 const std::string& channel_name, int* component) {
1920 if (channel_name == GICE_CHANNEL_NAME_RTP ||
1921 channel_name == GICE_CHANNEL_NAME_VIDEO_RTP ||
1922 channel_name == GICE_CHANNEL_NAME_DATA_RTP) {
1923 *component = ICE_CANDIDATE_COMPONENT_RTP;
1924 return true;
1925 } else if (channel_name == GICE_CHANNEL_NAME_RTCP ||
1926 channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP ||
1927 channel_name == GICE_CHANNEL_NAME_DATA_RTP) {
1928 *component = ICE_CANDIDATE_COMPONENT_RTCP;
1929 return true;
1930 }
1931
1932 return false;
1933}
1934
1935bool GetMediaTypeFromChannelName(
1936 const std::string& channel_name, MediaType* media_type) {
1937 if (channel_name == GICE_CHANNEL_NAME_RTP ||
1938 channel_name == GICE_CHANNEL_NAME_RTCP) {
1939 *media_type = MEDIA_TYPE_AUDIO;
1940 return true;
1941 } else if (channel_name == GICE_CHANNEL_NAME_VIDEO_RTP ||
1942 channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP) {
1943 *media_type = MEDIA_TYPE_VIDEO;
1944 return true;
1945 } else if (channel_name == GICE_CHANNEL_NAME_DATA_RTP ||
1946 channel_name == GICE_CHANNEL_NAME_DATA_RTCP) {
1947 *media_type = MEDIA_TYPE_DATA;
1948 return true;
1949 }
1950
1951 return false;
1952}
1953
1954} // namespace cricket