blob: 41a5e1d6e875835af1ccc260221a068e5b586d98 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 // groupid is empty for StreamParams generated using
226 // MediaSessionDescriptionFactory.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000227 const StreamParams* param = GetStreamByIds(params_vec, "", stream_it->id);
228 if (param) {
229 *cname = param->cname;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 return true;
231 }
232 }
233 // No other stream seems to exist that we should sync with.
234 // Generate a random string for the RTCP CNAME, as stated in RFC 6222.
235 // This string is only used for synchronization, and therefore is opaque.
236 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000237 if (!rtc::CreateRandomString(16, cname)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 ASSERT(false);
239 return false;
240 }
241 } while (FindFirstStreamParamsByCname(params_vec, *cname));
242
243 return true;
244}
245
246// Generate random SSRC values that are not already present in |params_vec|.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000247// The generated values are added to |ssrcs|.
248// |num_ssrcs| is the number of the SSRC will be generated.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249static void GenerateSsrcs(const StreamParamsVec& params_vec,
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000250 int num_ssrcs,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251 std::vector<uint32>* ssrcs) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000252 for (int i = 0; i < num_ssrcs; i++) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 uint32 candidate;
254 do {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000255 candidate = rtc::CreateRandomNonZeroId();
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000256 } while (GetStreamBySsrc(params_vec, candidate) ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257 std::count(ssrcs->begin(), ssrcs->end(), candidate) > 0);
258 ssrcs->push_back(candidate);
259 }
260}
261
262// Returns false if we exhaust the range of SIDs.
263static bool GenerateSctpSid(const StreamParamsVec& params_vec,
264 uint32* sid) {
265 if (params_vec.size() > kMaxSctpSid) {
266 LOG(LS_WARNING) <<
267 "Could not generate an SCTP SID: too many SCTP streams.";
268 return false;
269 }
270 while (true) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000271 uint32 candidate = rtc::CreateRandomNonZeroId() % kMaxSctpSid;
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000272 if (!GetStreamBySsrc(params_vec, candidate)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273 *sid = candidate;
274 return true;
275 }
276 }
277}
278
279static bool GenerateSctpSids(const StreamParamsVec& params_vec,
280 std::vector<uint32>* sids) {
281 uint32 sid;
282 if (!GenerateSctpSid(params_vec, &sid)) {
283 LOG(LS_WARNING) << "Could not generated an SCTP SID.";
284 return false;
285 }
286 sids->push_back(sid);
287 return true;
288}
289
290// Finds all StreamParams of all media types and attach them to stream_params.
291static void GetCurrentStreamParams(const SessionDescription* sdesc,
292 StreamParamsVec* stream_params) {
293 if (!sdesc)
294 return;
295
296 const ContentInfos& contents = sdesc->contents();
297 for (ContentInfos::const_iterator content = contents.begin();
298 content != contents.end(); ++content) {
299 if (!IsMediaContent(&*content)) {
300 continue;
301 }
302 const MediaContentDescription* media =
303 static_cast<const MediaContentDescription*>(
304 content->description);
305 const StreamParamsVec& streams = media->streams();
306 for (StreamParamsVec::const_iterator it = streams.begin();
307 it != streams.end(); ++it) {
308 stream_params->push_back(*it);
309 }
310 }
311}
312
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +0000313// Filters the data codecs for the data channel type.
314void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
315 // Filter RTP codec for SCTP and vice versa.
316 int codec_id = sctp ? kGoogleRtpDataCodecId : kGoogleSctpDataCodecId;
317 for (std::vector<DataCodec>::iterator iter = codecs->begin();
318 iter != codecs->end();) {
319 if (iter->id == codec_id) {
320 iter = codecs->erase(iter);
321 } else {
322 ++iter;
323 }
324 }
325}
326
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327template <typename IdStruct>
328class UsedIds {
329 public:
330 UsedIds(int min_allowed_id, int max_allowed_id)
331 : min_allowed_id_(min_allowed_id),
332 max_allowed_id_(max_allowed_id),
333 next_id_(max_allowed_id) {
334 }
335
336 // Loops through all Id in |ids| and changes its id if it is
337 // already in use by another IdStruct. Call this methods with all Id
338 // in a session description to make sure no duplicate ids exists.
339 // Note that typename Id must be a type of IdStruct.
340 template <typename Id>
341 void FindAndSetIdUsed(std::vector<Id>* ids) {
342 for (typename std::vector<Id>::iterator it = ids->begin();
343 it != ids->end(); ++it) {
344 FindAndSetIdUsed(&*it);
345 }
346 }
347
348 // Finds and sets an unused id if the |idstruct| id is already in use.
349 void FindAndSetIdUsed(IdStruct* idstruct) {
350 const int original_id = idstruct->id;
351 int new_id = idstruct->id;
352
353 if (original_id > max_allowed_id_ || original_id < min_allowed_id_) {
354 // If the original id is not in range - this is an id that can't be
355 // dynamically changed.
356 return;
357 }
358
359 if (IsIdUsed(original_id)) {
360 new_id = FindUnusedId();
361 LOG(LS_WARNING) << "Duplicate id found. Reassigning from " << original_id
362 << " to " << new_id;
363 idstruct->id = new_id;
364 }
365 SetIdUsed(new_id);
366 }
367
368 private:
369 // Returns the first unused id in reverse order.
370 // This hopefully reduce the risk of more collisions. We want to change the
371 // default ids as little as possible.
372 int FindUnusedId() {
373 while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) {
374 --next_id_;
375 }
376 ASSERT(next_id_ >= min_allowed_id_);
377 return next_id_;
378 }
379
380 bool IsIdUsed(int new_id) {
381 return id_set_.find(new_id) != id_set_.end();
382 }
383
384 void SetIdUsed(int new_id) {
385 id_set_.insert(new_id);
386 }
387
388 const int min_allowed_id_;
389 const int max_allowed_id_;
390 int next_id_;
391 std::set<int> id_set_;
392};
393
394// Helper class used for finding duplicate RTP payload types among audio, video
395// and data codecs. When bundle is used the payload types may not collide.
396class UsedPayloadTypes : public UsedIds<Codec> {
397 public:
398 UsedPayloadTypes()
399 : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) {
400 }
401
402
403 private:
404 static const int kDynamicPayloadTypeMin = 96;
405 static const int kDynamicPayloadTypeMax = 127;
406};
407
408// Helper class used for finding duplicate RTP Header extension ids among
409// audio and video extensions.
410class UsedRtpHeaderExtensionIds : public UsedIds<RtpHeaderExtension> {
411 public:
412 UsedRtpHeaderExtensionIds()
413 : UsedIds<RtpHeaderExtension>(kLocalIdMin, kLocalIdMax) {
414 }
415
416 private:
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000417 // Min and Max local identifier for one-byte header extensions, per RFC5285.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 static const int kLocalIdMin = 1;
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000419 static const int kLocalIdMax = 14;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420};
421
422static bool IsSctp(const MediaContentDescription* desc) {
423 return ((desc->protocol() == kMediaProtocolSctp) ||
424 (desc->protocol() == kMediaProtocolDtlsSctp));
425}
426
427// Adds a StreamParams for each Stream in Streams with media type
428// media_type to content_description.
429// |current_params| - All currently known StreamParams of any media type.
430template <class C>
431static bool AddStreamParams(
432 MediaType media_type,
433 const MediaSessionOptions::Streams& streams,
434 StreamParamsVec* current_streams,
435 MediaContentDescriptionImpl<C>* content_description,
436 const bool add_legacy_stream) {
437 const bool include_rtx_stream =
438 ContainsRtxCodec(content_description->codecs());
439
440 if (streams.empty() && add_legacy_stream) {
441 // TODO(perkj): Remove this legacy stream when all apps use StreamParams.
442 std::vector<uint32> ssrcs;
443 if (IsSctp(content_description)) {
444 GenerateSctpSids(*current_streams, &ssrcs);
445 } else {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000446 int num_ssrcs = include_rtx_stream ? 2 : 1;
447 GenerateSsrcs(*current_streams, num_ssrcs, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 }
449 if (include_rtx_stream) {
450 content_description->AddLegacyStream(ssrcs[0], ssrcs[1]);
451 content_description->set_multistream(true);
452 } else {
453 content_description->AddLegacyStream(ssrcs[0]);
454 }
455 return true;
456 }
457
458 MediaSessionOptions::Streams::const_iterator stream_it;
459 for (stream_it = streams.begin();
460 stream_it != streams.end(); ++stream_it) {
461 if (stream_it->type != media_type)
462 continue; // Wrong media type.
463
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000464 const StreamParams* param =
465 GetStreamByIds(*current_streams, "", stream_it->id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 // groupid is empty for StreamParams generated using
467 // MediaSessionDescriptionFactory.
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000468 if (!param) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 // This is a new stream.
470 // Get a CNAME. Either new or same as one of the other synched streams.
471 std::string cname;
472 if (!GenerateCname(*current_streams, streams, stream_it->sync_label,
473 &cname)) {
474 return false;
475 }
476
477 std::vector<uint32> ssrcs;
478 if (IsSctp(content_description)) {
479 GenerateSctpSids(*current_streams, &ssrcs);
480 } else {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000481 GenerateSsrcs(*current_streams, stream_it->num_sim_layers, &ssrcs);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 }
483 StreamParams stream_param;
484 stream_param.id = stream_it->id;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000485 // Add the generated ssrc.
486 for (size_t i = 0; i < ssrcs.size(); ++i) {
487 stream_param.ssrcs.push_back(ssrcs[i]);
488 }
489 if (stream_it->num_sim_layers > 1) {
490 SsrcGroup group(kSimSsrcGroupSemantics, stream_param.ssrcs);
491 stream_param.ssrc_groups.push_back(group);
492 }
493 // Generate an extra ssrc for include_rtx_stream case.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 if (include_rtx_stream) {
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000495 std::vector<uint32> rtx_ssrc;
496 GenerateSsrcs(*current_streams, 1, &rtx_ssrc);
497 stream_param.AddFidSsrc(ssrcs[0], rtx_ssrc[0]);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 content_description->set_multistream(true);
499 }
500 stream_param.cname = cname;
501 stream_param.sync_label = stream_it->sync_label;
502 content_description->AddStream(stream_param);
503
504 // Store the new StreamParams in current_streams.
505 // This is necessary so that we can use the CNAME for other media types.
506 current_streams->push_back(stream_param);
507 } else {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +0000508 content_description->AddStream(*param);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 }
510 }
511 return true;
512}
513
514// Updates the transport infos of the |sdesc| according to the given
515// |bundle_group|. The transport infos of the content names within the
516// |bundle_group| should be updated to use the ufrag and pwd of the first
517// content within the |bundle_group|.
518static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
519 SessionDescription* sdesc) {
520 // The bundle should not be empty.
521 if (!sdesc || !bundle_group.FirstContentName()) {
522 return false;
523 }
524
525 // We should definitely have a transport for the first content.
526 std::string selected_content_name = *bundle_group.FirstContentName();
527 const TransportInfo* selected_transport_info =
528 sdesc->GetTransportInfoByName(selected_content_name);
529 if (!selected_transport_info) {
530 return false;
531 }
532
533 // Set the other contents to use the same ICE credentials.
534 const std::string selected_ufrag =
535 selected_transport_info->description.ice_ufrag;
536 const std::string selected_pwd =
537 selected_transport_info->description.ice_pwd;
538 for (TransportInfos::iterator it =
539 sdesc->transport_infos().begin();
540 it != sdesc->transport_infos().end(); ++it) {
541 if (bundle_group.HasContentName(it->content_name) &&
542 it->content_name != selected_content_name) {
543 it->description.ice_ufrag = selected_ufrag;
544 it->description.ice_pwd = selected_pwd;
545 }
546 }
547 return true;
548}
549
550// Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
551// sets it to |cryptos|.
552static bool GetCryptosByName(const SessionDescription* sdesc,
553 const std::string& content_name,
554 CryptoParamsVec* cryptos) {
555 if (!sdesc || !cryptos) {
556 return false;
557 }
558
559 const ContentInfo* content = sdesc->GetContentByName(content_name);
560 if (!IsMediaContent(content) || !content->description) {
561 return false;
562 }
563
564 const MediaContentDescription* media_desc =
565 static_cast<const MediaContentDescription*>(content->description);
566 *cryptos = media_desc->cryptos();
567 return true;
568}
569
570// Predicate function used by the remove_if.
571// Returns true if the |crypto|'s cipher_suite is not found in |filter|.
572static bool CryptoNotFound(const CryptoParams crypto,
573 const CryptoParamsVec* filter) {
574 if (filter == NULL) {
575 return true;
576 }
577 for (CryptoParamsVec::const_iterator it = filter->begin();
578 it != filter->end(); ++it) {
579 if (it->cipher_suite == crypto.cipher_suite) {
580 return false;
581 }
582 }
583 return true;
584}
585
586// Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
587// which are not available in |filter|.
588static void PruneCryptos(const CryptoParamsVec& filter,
589 CryptoParamsVec* target_cryptos) {
590 if (!target_cryptos) {
591 return;
592 }
593 target_cryptos->erase(std::remove_if(target_cryptos->begin(),
594 target_cryptos->end(),
595 bind2nd(ptr_fun(CryptoNotFound),
596 &filter)),
597 target_cryptos->end());
598}
599
600static bool IsRtpContent(SessionDescription* sdesc,
601 const std::string& content_name) {
602 bool is_rtp = false;
603 ContentInfo* content = sdesc->GetContentByName(content_name);
604 if (IsMediaContent(content)) {
605 MediaContentDescription* media_desc =
606 static_cast<MediaContentDescription*>(content->description);
607 if (!media_desc) {
608 return false;
609 }
610 is_rtp = media_desc->protocol().empty() ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611 rtc::starts_with(media_desc->protocol().data(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 kMediaProtocolRtpPrefix);
613 }
614 return is_rtp;
615}
616
617// Updates the crypto parameters of the |sdesc| according to the given
618// |bundle_group|. The crypto parameters of all the contents within the
619// |bundle_group| should be updated to use the common subset of the
620// available cryptos.
621static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
622 SessionDescription* sdesc) {
623 // The bundle should not be empty.
624 if (!sdesc || !bundle_group.FirstContentName()) {
625 return false;
626 }
627
wu@webrtc.org78187522013-10-07 23:32:02 +0000628 bool common_cryptos_needed = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 // Get the common cryptos.
630 const ContentNames& content_names = bundle_group.content_names();
631 CryptoParamsVec common_cryptos;
632 for (ContentNames::const_iterator it = content_names.begin();
633 it != content_names.end(); ++it) {
634 if (!IsRtpContent(sdesc, *it)) {
635 continue;
636 }
wu@webrtc.org78187522013-10-07 23:32:02 +0000637 // The common cryptos are needed if any of the content does not have DTLS
638 // enabled.
639 if (!sdesc->GetTransportInfoByName(*it)->description.secure()) {
640 common_cryptos_needed = true;
641 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 if (it == content_names.begin()) {
643 // Initial the common_cryptos with the first content in the bundle group.
644 if (!GetCryptosByName(sdesc, *it, &common_cryptos)) {
645 return false;
646 }
647 if (common_cryptos.empty()) {
648 // If there's no crypto params, we should just return.
649 return true;
650 }
651 } else {
652 CryptoParamsVec cryptos;
653 if (!GetCryptosByName(sdesc, *it, &cryptos)) {
654 return false;
655 }
656 PruneCryptos(cryptos, &common_cryptos);
657 }
658 }
659
wu@webrtc.org78187522013-10-07 23:32:02 +0000660 if (common_cryptos.empty() && common_cryptos_needed) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 return false;
662 }
663
664 // Update to use the common cryptos.
665 for (ContentNames::const_iterator it = content_names.begin();
666 it != content_names.end(); ++it) {
667 if (!IsRtpContent(sdesc, *it)) {
668 continue;
669 }
670 ContentInfo* content = sdesc->GetContentByName(*it);
671 if (IsMediaContent(content)) {
672 MediaContentDescription* media_desc =
673 static_cast<MediaContentDescription*>(content->description);
674 if (!media_desc) {
675 return false;
676 }
677 media_desc->set_cryptos(common_cryptos);
678 }
679 }
680 return true;
681}
682
683template <class C>
684static bool ContainsRtxCodec(const std::vector<C>& codecs) {
685 typename std::vector<C>::const_iterator it;
686 for (it = codecs.begin(); it != codecs.end(); ++it) {
687 if (IsRtxCodec(*it)) {
688 return true;
689 }
690 }
691 return false;
692}
693
694template <class C>
695static bool IsRtxCodec(const C& codec) {
696 return stricmp(codec.name.c_str(), kRtxCodecName) == 0;
697}
698
699// Create a media content to be offered in a session-initiate,
700// according to the given options.rtcp_mux, options.is_muc,
701// options.streams, codecs, secure_transport, crypto, and streams. If we don't
702// currently have crypto (in current_cryptos) and it is enabled (in
703// secure_policy), crypto is created (according to crypto_suites). If
704// add_legacy_stream is true, and current_streams is empty, a legacy
705// stream is created. The created content is added to the offer.
706template <class C>
707static bool CreateMediaContentOffer(
708 const MediaSessionOptions& options,
709 const std::vector<C>& codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000710 const SecurePolicy& secure_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 const CryptoParamsVec* current_cryptos,
712 const std::vector<std::string>& crypto_suites,
713 const RtpHeaderExtensions& rtp_extensions,
714 bool add_legacy_stream,
715 StreamParamsVec* current_streams,
716 MediaContentDescriptionImpl<C>* offer) {
717 offer->AddCodecs(codecs);
718 offer->SortCodecs();
719
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000720 if (secure_policy == SEC_REQUIRED) {
721 offer->set_crypto_required(CT_SDES);
722 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 offer->set_rtcp_mux(options.rtcp_mux_enabled);
724 offer->set_multistream(options.is_muc);
725 offer->set_rtp_header_extensions(rtp_extensions);
726
727 if (!AddStreamParams(
728 offer->type(), options.streams, current_streams,
729 offer, add_legacy_stream)) {
730 return false;
731 }
732
733#ifdef HAVE_SRTP
734 if (secure_policy != SEC_DISABLED) {
735 if (current_cryptos) {
736 AddMediaCryptos(*current_cryptos, offer);
737 }
738 if (offer->cryptos().empty()) {
739 if (!CreateMediaCryptos(crypto_suites, offer)) {
740 return false;
741 }
742 }
743 }
744#endif
745
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000746 if (offer->crypto_required() == CT_SDES && offer->cryptos().empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 return false;
748 }
749 return true;
750}
751
752template <class C>
753static void NegotiateCodecs(const std::vector<C>& local_codecs,
754 const std::vector<C>& offered_codecs,
755 std::vector<C>* negotiated_codecs) {
756 typename std::vector<C>::const_iterator ours;
757 for (ours = local_codecs.begin();
758 ours != local_codecs.end(); ++ours) {
759 typename std::vector<C>::const_iterator theirs;
760 for (theirs = offered_codecs.begin();
761 theirs != offered_codecs.end(); ++theirs) {
762 if (ours->Matches(*theirs)) {
763 C negotiated = *ours;
764 negotiated.IntersectFeedbackParams(*theirs);
765 if (IsRtxCodec(negotiated)) {
766 // Only negotiate RTX if kCodecParamAssociatedPayloadType has been
767 // set.
768 std::string apt_value;
769 if (!theirs->GetParam(kCodecParamAssociatedPayloadType, &apt_value)) {
770 LOG(LS_WARNING) << "RTX missing associated payload type.";
771 continue;
772 }
773 negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_value);
774 }
775 negotiated.id = theirs->id;
wu@webrtc.orgff1b1bf2014-06-20 20:57:42 +0000776 // RFC3264: Although the answerer MAY list the formats in their desired
777 // order of preference, it is RECOMMENDED that unless there is a
778 // specific reason, the answerer list formats in the same relative order
779 // they were present in the offer.
780 negotiated.preference = theirs->preference;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 negotiated_codecs->push_back(negotiated);
782 }
783 }
784 }
785}
786
787template <class C>
788static bool FindMatchingCodec(const std::vector<C>& codecs,
789 const C& codec_to_match,
790 C* found_codec) {
791 for (typename std::vector<C>::const_iterator it = codecs.begin();
792 it != codecs.end(); ++it) {
793 if (it->Matches(codec_to_match)) {
794 if (found_codec != NULL) {
795 *found_codec= *it;
796 }
797 return true;
798 }
799 }
800 return false;
801}
802
803// Adds all codecs from |reference_codecs| to |offered_codecs| that dont'
804// already exist in |offered_codecs| and ensure the payload types don't
805// collide.
806template <class C>
807static void FindCodecsToOffer(
808 const std::vector<C>& reference_codecs,
809 std::vector<C>* offered_codecs,
810 UsedPayloadTypes* used_pltypes) {
811
812 typedef std::map<int, C> RtxCodecReferences;
813 RtxCodecReferences new_rtx_codecs;
814
815 // Find all new RTX codecs.
816 for (typename std::vector<C>::const_iterator it = reference_codecs.begin();
817 it != reference_codecs.end(); ++it) {
818 if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && IsRtxCodec(*it)) {
819 C rtx_codec = *it;
820 int referenced_pl_type =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000821 rtc::FromString<int>(0,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 rtx_codec.params[kCodecParamAssociatedPayloadType]);
823 new_rtx_codecs.insert(std::pair<int, C>(referenced_pl_type,
824 rtx_codec));
825 }
826 }
827
828 // Add all new codecs that are not RTX codecs.
829 for (typename std::vector<C>::const_iterator it = reference_codecs.begin();
830 it != reference_codecs.end(); ++it) {
831 if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && !IsRtxCodec(*it)) {
832 C codec = *it;
833 int original_payload_id = codec.id;
834 used_pltypes->FindAndSetIdUsed(&codec);
835 offered_codecs->push_back(codec);
836
837 // If this codec is referenced by a new RTX codec, update the reference
838 // in the RTX codec with the new payload type.
839 typename RtxCodecReferences::iterator rtx_it =
840 new_rtx_codecs.find(original_payload_id);
841 if (rtx_it != new_rtx_codecs.end()) {
842 C& rtx_codec = rtx_it->second;
843 rtx_codec.params[kCodecParamAssociatedPayloadType] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000844 rtc::ToString(codec.id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 }
846 }
847 }
848
849 // Add all new RTX codecs.
850 for (typename RtxCodecReferences::iterator it = new_rtx_codecs.begin();
851 it != new_rtx_codecs.end(); ++it) {
852 C& rtx_codec = it->second;
853 used_pltypes->FindAndSetIdUsed(&rtx_codec);
854 offered_codecs->push_back(rtx_codec);
855 }
856}
857
858
859static bool FindByUri(const RtpHeaderExtensions& extensions,
860 const RtpHeaderExtension& ext_to_match,
861 RtpHeaderExtension* found_extension) {
862 for (RtpHeaderExtensions::const_iterator it = extensions.begin();
863 it != extensions.end(); ++it) {
864 // We assume that all URIs are given in a canonical format.
865 if (it->uri == ext_to_match.uri) {
866 if (found_extension != NULL) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000867 *found_extension = *it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 }
869 return true;
870 }
871 }
872 return false;
873}
874
875static void FindAndSetRtpHdrExtUsed(
876 const RtpHeaderExtensions& reference_extensions,
877 RtpHeaderExtensions* offered_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000878 const RtpHeaderExtensions& other_extensions,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 UsedRtpHeaderExtensionIds* used_extensions) {
880 for (RtpHeaderExtensions::const_iterator it = reference_extensions.begin();
881 it != reference_extensions.end(); ++it) {
882 if (!FindByUri(*offered_extensions, *it, NULL)) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +0000883 RtpHeaderExtension ext;
884 if (!FindByUri(other_extensions, *it, &ext)) {
885 ext = *it;
886 used_extensions->FindAndSetIdUsed(&ext);
887 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 offered_extensions->push_back(ext);
889 }
890 }
891}
892
893static void NegotiateRtpHeaderExtensions(
894 const RtpHeaderExtensions& local_extensions,
895 const RtpHeaderExtensions& offered_extensions,
896 RtpHeaderExtensions* negotiated_extenstions) {
897 RtpHeaderExtensions::const_iterator ours;
898 for (ours = local_extensions.begin();
899 ours != local_extensions.end(); ++ours) {
900 RtpHeaderExtension theirs;
901 if (FindByUri(offered_extensions, *ours, &theirs)) {
902 // We respond with their RTP header extension id.
903 negotiated_extenstions->push_back(theirs);
904 }
905 }
906}
907
908static void StripCNCodecs(AudioCodecs* audio_codecs) {
909 AudioCodecs::iterator iter = audio_codecs->begin();
910 while (iter != audio_codecs->end()) {
911 if (stricmp(iter->name.c_str(), kComfortNoiseCodecName) == 0) {
912 iter = audio_codecs->erase(iter);
913 } else {
914 ++iter;
915 }
916 }
917}
918
919// Create a media content to be answered in a session-accept,
920// according to the given options.rtcp_mux, options.streams, codecs,
921// crypto, and streams. If we don't currently have crypto (in
922// current_cryptos) and it is enabled (in secure_policy), crypto is
923// created (according to crypto_suites). If add_legacy_stream is
924// true, and current_streams is empty, a legacy stream is created.
925// The codecs, rtcp_mux, and crypto are all negotiated with the offer
926// from the incoming session-initiate. If the negotiation fails, this
927// method returns false. The created content is added to the offer.
928template <class C>
929static bool CreateMediaContentAnswer(
930 const MediaContentDescriptionImpl<C>* offer,
931 const MediaSessionOptions& options,
932 const std::vector<C>& local_codecs,
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000933 const SecurePolicy& sdes_policy,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 const CryptoParamsVec* current_cryptos,
935 const RtpHeaderExtensions& local_rtp_extenstions,
936 StreamParamsVec* current_streams,
937 bool add_legacy_stream,
938 bool bundle_enabled,
939 MediaContentDescriptionImpl<C>* answer) {
940 std::vector<C> negotiated_codecs;
941 NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs);
942 answer->AddCodecs(negotiated_codecs);
943 answer->SortCodecs();
944 answer->set_protocol(offer->protocol());
945 RtpHeaderExtensions negotiated_rtp_extensions;
946 NegotiateRtpHeaderExtensions(local_rtp_extenstions,
947 offer->rtp_header_extensions(),
948 &negotiated_rtp_extensions);
949 answer->set_rtp_header_extensions(negotiated_rtp_extensions);
950
951 answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux());
952
953 if (sdes_policy != SEC_DISABLED) {
954 CryptoParams crypto;
955 if (SelectCrypto(offer, bundle_enabled, &crypto)) {
956 if (current_cryptos) {
957 FindMatchingCrypto(*current_cryptos, crypto, &crypto);
958 }
959 answer->AddCrypto(crypto);
960 }
961 }
962
963 if (answer->cryptos().empty() &&
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +0000964 (offer->crypto_required() == CT_SDES || sdes_policy == SEC_REQUIRED)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 return false;
966 }
967
968 if (!AddStreamParams(
969 answer->type(), options.streams, current_streams,
970 answer, add_legacy_stream)) {
971 return false; // Something went seriously wrong.
972 }
973
974 // Make sure the answer media content direction is per default set as
975 // described in RFC3264 section 6.1.
976 switch (offer->direction()) {
977 case MD_INACTIVE:
978 answer->set_direction(MD_INACTIVE);
979 break;
980 case MD_SENDONLY:
981 answer->set_direction(MD_RECVONLY);
982 break;
983 case MD_RECVONLY:
984 answer->set_direction(MD_SENDONLY);
985 break;
986 case MD_SENDRECV:
987 answer->set_direction(MD_SENDRECV);
988 break;
989 default:
990 break;
991 }
992
993 return true;
994}
995
996static bool IsMediaProtocolSupported(MediaType type,
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +0000997 const std::string& protocol,
998 bool secure_transport) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 // Data channels can have a protocol of SCTP or SCTP/DTLS.
1000 if (type == MEDIA_TYPE_DATA &&
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001001 ((protocol == kMediaProtocolSctp && !secure_transport)||
1002 (protocol == kMediaProtocolDtlsSctp && secure_transport))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 return true;
1004 }
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001005
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 // Since not all applications serialize and deserialize the media protocol,
1007 // we will have to accept |protocol| to be empty.
jiayl@webrtc.org8dcd43c2014-05-29 22:07:59 +00001008 return protocol == kMediaProtocolAvpf || protocol.empty() ||
1009 protocol == kMediaProtocolSavpf ||
1010 (protocol == kMediaProtocolDtlsSavpf && secure_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011}
1012
1013static void SetMediaProtocol(bool secure_transport,
1014 MediaContentDescription* desc) {
1015 if (!desc->cryptos().empty() || secure_transport)
1016 desc->set_protocol(kMediaProtocolSavpf);
1017 else
1018 desc->set_protocol(kMediaProtocolAvpf);
1019}
1020
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001021// Gets the TransportInfo of the given |content_name| from the
1022// |current_description|. If doesn't exist, returns a new one.
1023static const TransportDescription* GetTransportDescription(
1024 const std::string& content_name,
1025 const SessionDescription* current_description) {
1026 const TransportDescription* desc = NULL;
1027 if (current_description) {
1028 const TransportInfo* info =
1029 current_description->GetTransportInfoByName(content_name);
1030 if (info) {
1031 desc = &info->description;
1032 }
1033 }
1034 return desc;
1035}
1036
1037// Gets the current DTLS state from the transport description.
1038static bool IsDtlsActive(
1039 const std::string& content_name,
1040 const SessionDescription* current_description) {
1041 if (!current_description)
1042 return false;
1043
1044 const ContentInfo* content =
1045 current_description->GetContentByName(content_name);
1046 if (!content)
1047 return false;
1048
1049 const TransportDescription* current_tdesc =
1050 GetTransportDescription(content_name, current_description);
1051 if (!current_tdesc)
1052 return false;
1053
1054 return current_tdesc->secure();
1055}
1056
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001057std::string MediaTypeToString(MediaType type) {
1058 std::string type_str;
1059 switch (type) {
1060 case MEDIA_TYPE_AUDIO:
1061 type_str = "audio";
1062 break;
1063 case MEDIA_TYPE_VIDEO:
1064 type_str = "video";
1065 break;
1066 case MEDIA_TYPE_DATA:
1067 type_str = "data";
1068 break;
1069 default:
1070 ASSERT(false);
1071 break;
1072 }
1073 return type_str;
1074}
1075
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001076void MediaSessionOptions::AddSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 const std::string& id,
1078 const std::string& sync_label) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001079 AddSendStreamInternal(type, id, sync_label, 1);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001080}
1081
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001082void MediaSessionOptions::AddSendVideoStream(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001083 const std::string& id,
1084 const std::string& sync_label,
1085 int num_sim_layers) {
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001086 AddSendStreamInternal(MEDIA_TYPE_VIDEO, id, sync_label, num_sim_layers);
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001087}
1088
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001089void MediaSessionOptions::AddSendStreamInternal(
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001090 MediaType type,
1091 const std::string& id,
1092 const std::string& sync_label,
1093 int num_sim_layers) {
1094 streams.push_back(Stream(type, id, sync_label, num_sim_layers));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 // If we haven't already set the data_channel_type, and we add a
1097 // stream, we assume it's an RTP data stream.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001098 if (type == MEDIA_TYPE_DATA && data_channel_type == DCT_NONE)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 data_channel_type = DCT_RTP;
1100}
1101
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001102void MediaSessionOptions::RemoveSendStream(MediaType type,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 const std::string& id) {
1104 Streams::iterator stream_it = streams.begin();
1105 for (; stream_it != streams.end(); ++stream_it) {
1106 if (stream_it->type == type && stream_it->id == id) {
1107 streams.erase(stream_it);
1108 return;
1109 }
1110 }
1111 ASSERT(false);
1112}
1113
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001114bool MediaSessionOptions::HasSendMediaStream(MediaType type) const {
1115 Streams::const_iterator stream_it = streams.begin();
1116 for (; stream_it != streams.end(); ++stream_it) {
1117 if (stream_it->type == type) {
1118 return true;
1119 }
1120 }
1121 return false;
1122}
1123
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1125 const TransportDescriptionFactory* transport_desc_factory)
1126 : secure_(SEC_DISABLED),
1127 add_legacy_(true),
1128 transport_desc_factory_(transport_desc_factory) {
1129}
1130
1131MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
1132 ChannelManager* channel_manager,
1133 const TransportDescriptionFactory* transport_desc_factory)
1134 : secure_(SEC_DISABLED),
1135 add_legacy_(true),
1136 transport_desc_factory_(transport_desc_factory) {
1137 channel_manager->GetSupportedAudioCodecs(&audio_codecs_);
1138 channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
1139 channel_manager->GetSupportedVideoCodecs(&video_codecs_);
1140 channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
1141 channel_manager->GetSupportedDataCodecs(&data_codecs_);
1142}
1143
1144SessionDescription* MediaSessionDescriptionFactory::CreateOffer(
1145 const MediaSessionOptions& options,
1146 const SessionDescription* current_description) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 scoped_ptr<SessionDescription> offer(new SessionDescription());
1148
1149 StreamParamsVec current_streams;
1150 GetCurrentStreamParams(current_description, &current_streams);
1151
1152 AudioCodecs audio_codecs;
1153 VideoCodecs video_codecs;
1154 DataCodecs data_codecs;
1155 GetCodecsToOffer(current_description, &audio_codecs, &video_codecs,
1156 &data_codecs);
1157
1158 if (!options.vad_enabled) {
1159 // If application doesn't want CN codecs in offer.
1160 StripCNCodecs(&audio_codecs);
1161 }
1162
1163 RtpHeaderExtensions audio_rtp_extensions;
1164 RtpHeaderExtensions video_rtp_extensions;
1165 GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions,
1166 &video_rtp_extensions);
1167
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001168 bool audio_added = false;
1169 bool video_added = false;
1170 bool data_added = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001171
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001172 // Iterate through the contents of |current_description| to maintain the order
1173 // of the m-lines in the new offer.
1174 if (current_description) {
1175 ContentInfos::const_iterator it = current_description->contents().begin();
1176 for (; it != current_description->contents().end(); ++it) {
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001177 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001178 if (!AddAudioContentForOffer(options, current_description,
1179 audio_rtp_extensions, audio_codecs,
1180 &current_streams, offer.get())) {
1181 return NULL;
1182 }
1183 audio_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001184 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001185 if (!AddVideoContentForOffer(options, current_description,
1186 video_rtp_extensions, video_codecs,
1187 &current_streams, offer.get())) {
1188 return NULL;
1189 }
1190 video_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001191 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_DATA)) {
tommi@webrtc.orgf15dee62014-10-27 22:15:04 +00001192 MediaSessionOptions options_copy(options);
1193 if (IsSctp(static_cast<const MediaContentDescription*>(
1194 it->description))) {
1195 options_copy.data_channel_type = DCT_SCTP;
1196 }
1197 if (!AddDataContentForOffer(options_copy, current_description,
1198 &data_codecs, &current_streams,
1199 offer.get())) {
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001200 return NULL;
1201 }
1202 data_added = true;
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001203 } else {
1204 ASSERT(false);
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 }
1207 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001208
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001209 // Append contents that are not in |current_description|.
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001210 if (!audio_added && options.has_audio() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001211 !AddAudioContentForOffer(options, current_description,
1212 audio_rtp_extensions, audio_codecs,
1213 &current_streams, offer.get())) {
1214 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001216 if (!video_added && options.has_video() &&
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001217 !AddVideoContentForOffer(options, current_description,
1218 video_rtp_extensions, video_codecs,
1219 &current_streams, offer.get())) {
1220 return NULL;
1221 }
1222 if (!data_added && options.has_data() &&
1223 !AddDataContentForOffer(options, current_description, &data_codecs,
1224 &current_streams, offer.get())) {
1225 return NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 }
1227
1228 // Bundle the contents together, if we've been asked to do so, and update any
1229 // parameters that need to be tweaked for BUNDLE.
1230 if (options.bundle_enabled) {
1231 ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
1232 for (ContentInfos::const_iterator content = offer->contents().begin();
1233 content != offer->contents().end(); ++content) {
1234 offer_bundle.AddContentName(content->name);
1235 }
1236 offer->AddGroup(offer_bundle);
1237 if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
1238 LOG(LS_ERROR) << "CreateOffer failed to UpdateTransportInfoForBundle.";
1239 return NULL;
1240 }
1241 if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
1242 LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle.";
1243 return NULL;
1244 }
1245 }
1246
1247 return offer.release();
1248}
1249
1250SessionDescription* MediaSessionDescriptionFactory::CreateAnswer(
1251 const SessionDescription* offer, const MediaSessionOptions& options,
1252 const SessionDescription* current_description) const {
1253 // The answer contains the intersection of the codecs in the offer with the
1254 // codecs we support, ordered by our local preference. As indicated by
1255 // XEP-0167, we retain the same payload ids from the offer in the answer.
1256 scoped_ptr<SessionDescription> answer(new SessionDescription());
1257
1258 StreamParamsVec current_streams;
1259 GetCurrentStreamParams(current_description, &current_streams);
1260
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001261 if (offer) {
1262 ContentInfos::const_iterator it = offer->contents().begin();
1263 for (; it != offer->contents().end(); ++it) {
1264 if (IsMediaContentOfType(&*it, MEDIA_TYPE_AUDIO)) {
1265 if (!AddAudioContentForAnswer(offer, options, current_description,
1266 &current_streams, answer.get())) {
1267 return NULL;
1268 }
1269 } else if (IsMediaContentOfType(&*it, MEDIA_TYPE_VIDEO)) {
1270 if (!AddVideoContentForAnswer(offer, options, current_description,
1271 &current_streams, answer.get())) {
1272 return NULL;
1273 }
1274 } else {
1275 ASSERT(IsMediaContentOfType(&*it, MEDIA_TYPE_DATA));
1276 if (!AddDataContentForAnswer(offer, options, current_description,
1277 &current_streams, answer.get())) {
1278 return NULL;
1279 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 }
1283
1284 // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
1285 // group in the answer with the appropriate content names.
1286 if (offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled) {
1287 const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
1288 ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
1289 for (ContentInfos::const_iterator content = answer->contents().begin();
1290 content != answer->contents().end(); ++content) {
1291 if (!content->rejected && offer_bundle->HasContentName(content->name)) {
1292 answer_bundle.AddContentName(content->name);
1293 }
1294 }
1295 if (answer_bundle.FirstContentName()) {
1296 answer->AddGroup(answer_bundle);
1297
1298 // Share the same ICE credentials and crypto params across all contents,
1299 // as BUNDLE requires.
1300 if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
1301 LOG(LS_ERROR) << "CreateAnswer failed to UpdateTransportInfoForBundle.";
1302 return NULL;
1303 }
1304
1305 if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
1306 LOG(LS_ERROR) << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
1307 return NULL;
1308 }
1309 }
1310 }
1311
1312 return answer.release();
1313}
1314
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315void MediaSessionDescriptionFactory::GetCodecsToOffer(
1316 const SessionDescription* current_description,
1317 AudioCodecs* audio_codecs,
1318 VideoCodecs* video_codecs,
1319 DataCodecs* data_codecs) const {
1320 UsedPayloadTypes used_pltypes;
1321 audio_codecs->clear();
1322 video_codecs->clear();
1323 data_codecs->clear();
1324
1325
1326 // First - get all codecs from the current description if the media type
1327 // is used.
1328 // Add them to |used_pltypes| so the payloadtype is not reused if a new media
1329 // type is added.
1330 if (current_description) {
1331 const AudioContentDescription* audio =
1332 GetFirstAudioContentDescription(current_description);
1333 if (audio) {
1334 *audio_codecs = audio->codecs();
1335 used_pltypes.FindAndSetIdUsed<AudioCodec>(audio_codecs);
1336 }
1337 const VideoContentDescription* video =
1338 GetFirstVideoContentDescription(current_description);
1339 if (video) {
1340 *video_codecs = video->codecs();
1341 used_pltypes.FindAndSetIdUsed<VideoCodec>(video_codecs);
1342 }
1343 const DataContentDescription* data =
1344 GetFirstDataContentDescription(current_description);
1345 if (data) {
1346 *data_codecs = data->codecs();
1347 used_pltypes.FindAndSetIdUsed<DataCodec>(data_codecs);
1348 }
1349 }
1350
1351 // Add our codecs that are not in |current_description|.
1352 FindCodecsToOffer<AudioCodec>(audio_codecs_, audio_codecs, &used_pltypes);
1353 FindCodecsToOffer<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
1354 FindCodecsToOffer<DataCodec>(data_codecs_, data_codecs, &used_pltypes);
1355}
1356
1357void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
1358 const SessionDescription* current_description,
1359 RtpHeaderExtensions* audio_extensions,
1360 RtpHeaderExtensions* video_extensions) const {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001361 // All header extensions allocated from the same range to avoid potential
1362 // issues when using BUNDLE.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 UsedRtpHeaderExtensionIds used_ids;
1364 audio_extensions->clear();
1365 video_extensions->clear();
1366
1367 // First - get all extensions from the current description if the media type
1368 // is used.
1369 // Add them to |used_ids| so the local ids are not reused if a new media
1370 // type is added.
1371 if (current_description) {
1372 const AudioContentDescription* audio =
1373 GetFirstAudioContentDescription(current_description);
1374 if (audio) {
1375 *audio_extensions = audio->rtp_header_extensions();
1376 used_ids.FindAndSetIdUsed(audio_extensions);
1377 }
1378 const VideoContentDescription* video =
1379 GetFirstVideoContentDescription(current_description);
1380 if (video) {
1381 *video_extensions = video->rtp_header_extensions();
1382 used_ids.FindAndSetIdUsed(video_extensions);
1383 }
1384 }
1385
1386 // Add our default RTP header extensions that are not in
1387 // |current_description|.
1388 FindAndSetRtpHdrExtUsed(audio_rtp_header_extensions(), audio_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001389 *video_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 FindAndSetRtpHdrExtUsed(video_rtp_header_extensions(), video_extensions,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001391 *audio_extensions, &used_ids);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392}
1393
1394bool MediaSessionDescriptionFactory::AddTransportOffer(
1395 const std::string& content_name,
1396 const TransportOptions& transport_options,
1397 const SessionDescription* current_desc,
1398 SessionDescription* offer_desc) const {
1399 if (!transport_desc_factory_)
1400 return false;
1401 const TransportDescription* current_tdesc =
1402 GetTransportDescription(content_name, current_desc);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001403 rtc::scoped_ptr<TransportDescription> new_tdesc(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 transport_desc_factory_->CreateOffer(transport_options, current_tdesc));
1405 bool ret = (new_tdesc.get() != NULL &&
1406 offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)));
1407 if (!ret) {
1408 LOG(LS_ERROR)
1409 << "Failed to AddTransportOffer, content name=" << content_name;
1410 }
1411 return ret;
1412}
1413
1414TransportDescription* MediaSessionDescriptionFactory::CreateTransportAnswer(
1415 const std::string& content_name,
1416 const SessionDescription* offer_desc,
1417 const TransportOptions& transport_options,
1418 const SessionDescription* current_desc) const {
1419 if (!transport_desc_factory_)
1420 return NULL;
1421 const TransportDescription* offer_tdesc =
1422 GetTransportDescription(content_name, offer_desc);
1423 const TransportDescription* current_tdesc =
1424 GetTransportDescription(content_name, current_desc);
1425 return
1426 transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
1427 current_tdesc);
1428}
1429
1430bool MediaSessionDescriptionFactory::AddTransportAnswer(
1431 const std::string& content_name,
1432 const TransportDescription& transport_desc,
1433 SessionDescription* answer_desc) const {
1434 if (!answer_desc->AddTransportInfo(TransportInfo(content_name,
1435 transport_desc))) {
1436 LOG(LS_ERROR)
1437 << "Failed to AddTransportAnswer, content name=" << content_name;
1438 return false;
1439 }
1440 return true;
1441}
1442
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001443bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
1444 const MediaSessionOptions& options,
1445 const SessionDescription* current_description,
1446 const RtpHeaderExtensions& audio_rtp_extensions,
1447 const AudioCodecs& audio_codecs,
1448 StreamParamsVec* current_streams,
1449 SessionDescription* desc) const {
1450 cricket::SecurePolicy sdes_policy =
1451 IsDtlsActive(CN_AUDIO, current_description) ?
1452 cricket::SEC_DISABLED : secure();
1453
1454 scoped_ptr<AudioContentDescription> audio(new AudioContentDescription());
1455 std::vector<std::string> crypto_suites;
1456 GetSupportedAudioCryptoSuites(&crypto_suites);
1457 if (!CreateMediaContentOffer(
1458 options,
1459 audio_codecs,
1460 sdes_policy,
1461 GetCryptos(GetFirstAudioContentDescription(current_description)),
1462 crypto_suites,
1463 audio_rtp_extensions,
1464 add_legacy_,
1465 current_streams,
1466 audio.get())) {
1467 return false;
1468 }
1469 audio->set_lang(lang_);
1470
1471 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1472 SetMediaProtocol(secure_transport, audio.get());
jiayl@webrtc.org7d4891d2014-09-09 21:43:15 +00001473
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001474 if (!options.recv_audio) {
1475 audio->set_direction(MD_SENDONLY);
1476 }
1477
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001478 desc->AddContent(CN_AUDIO, NS_JINGLE_RTP, audio.release());
1479 if (!AddTransportOffer(CN_AUDIO, options.transport_options,
1480 current_description, desc)) {
1481 return false;
1482 }
1483
1484 return true;
1485}
1486
1487bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
1488 const MediaSessionOptions& options,
1489 const SessionDescription* current_description,
1490 const RtpHeaderExtensions& video_rtp_extensions,
1491 const VideoCodecs& video_codecs,
1492 StreamParamsVec* current_streams,
1493 SessionDescription* desc) const {
1494 cricket::SecurePolicy sdes_policy =
1495 IsDtlsActive(CN_VIDEO, current_description) ?
1496 cricket::SEC_DISABLED : secure();
1497
1498 scoped_ptr<VideoContentDescription> video(new VideoContentDescription());
1499 std::vector<std::string> crypto_suites;
1500 GetSupportedVideoCryptoSuites(&crypto_suites);
1501 if (!CreateMediaContentOffer(
1502 options,
1503 video_codecs,
1504 sdes_policy,
1505 GetCryptos(GetFirstVideoContentDescription(current_description)),
1506 crypto_suites,
1507 video_rtp_extensions,
1508 add_legacy_,
1509 current_streams,
1510 video.get())) {
1511 return false;
1512 }
1513
1514 video->set_bandwidth(options.video_bandwidth);
1515
1516 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1517 SetMediaProtocol(secure_transport, video.get());
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001518
1519 if (!options.recv_video) {
1520 video->set_direction(MD_SENDONLY);
1521 }
1522
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001523 desc->AddContent(CN_VIDEO, NS_JINGLE_RTP, video.release());
1524 if (!AddTransportOffer(CN_VIDEO, options.transport_options,
1525 current_description, desc)) {
1526 return false;
1527 }
1528
1529 return true;
1530}
1531
1532bool MediaSessionDescriptionFactory::AddDataContentForOffer(
1533 const MediaSessionOptions& options,
1534 const SessionDescription* current_description,
1535 DataCodecs* data_codecs,
1536 StreamParamsVec* current_streams,
1537 SessionDescription* desc) const {
1538 bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
1539
1540 scoped_ptr<DataContentDescription> data(new DataContentDescription());
1541 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1542
1543 FilterDataCodecs(data_codecs, is_sctp);
1544
1545 cricket::SecurePolicy sdes_policy =
1546 IsDtlsActive(CN_DATA, current_description) ?
1547 cricket::SEC_DISABLED : secure();
1548 std::vector<std::string> crypto_suites;
1549 if (is_sctp) {
1550 // SDES doesn't make sense for SCTP, so we disable it, and we only
1551 // get SDES crypto suites for RTP-based data channels.
1552 sdes_policy = cricket::SEC_DISABLED;
1553 // Unlike SetMediaProtocol below, we need to set the protocol
1554 // before we call CreateMediaContentOffer. Otherwise,
1555 // CreateMediaContentOffer won't know this is SCTP and will
1556 // generate SSRCs rather than SIDs.
1557 data->set_protocol(
1558 secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp);
1559 } else {
1560 GetSupportedDataCryptoSuites(&crypto_suites);
1561 }
1562
1563 if (!CreateMediaContentOffer(
1564 options,
1565 *data_codecs,
1566 sdes_policy,
1567 GetCryptos(GetFirstDataContentDescription(current_description)),
1568 crypto_suites,
1569 RtpHeaderExtensions(),
1570 add_legacy_,
1571 current_streams,
1572 data.get())) {
1573 return false;
1574 }
1575
1576 if (is_sctp) {
1577 desc->AddContent(CN_DATA, NS_JINGLE_DRAFT_SCTP, data.release());
1578 } else {
1579 data->set_bandwidth(options.data_bandwidth);
1580 SetMediaProtocol(secure_transport, data.get());
1581 desc->AddContent(CN_DATA, NS_JINGLE_RTP, data.release());
1582 }
1583 if (!AddTransportOffer(CN_DATA, options.transport_options,
1584 current_description, desc)) {
1585 return false;
1586 }
1587 return true;
1588}
1589
1590bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
1591 const SessionDescription* offer,
1592 const MediaSessionOptions& options,
1593 const SessionDescription* current_description,
1594 StreamParamsVec* current_streams,
1595 SessionDescription* answer) const {
1596 const ContentInfo* audio_content = GetFirstAudioContent(offer);
1597
1598 scoped_ptr<TransportDescription> audio_transport(
1599 CreateTransportAnswer(audio_content->name, offer,
1600 options.transport_options,
1601 current_description));
1602 if (!audio_transport) {
1603 return false;
1604 }
1605
1606 AudioCodecs audio_codecs = audio_codecs_;
1607 if (!options.vad_enabled) {
1608 StripCNCodecs(&audio_codecs);
1609 }
1610
1611 bool bundle_enabled =
1612 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1613 scoped_ptr<AudioContentDescription> audio_answer(
1614 new AudioContentDescription());
1615 // Do not require or create SDES cryptos if DTLS is used.
1616 cricket::SecurePolicy sdes_policy =
1617 audio_transport->secure() ? cricket::SEC_DISABLED : secure();
1618 if (!CreateMediaContentAnswer(
1619 static_cast<const AudioContentDescription*>(
1620 audio_content->description),
1621 options,
1622 audio_codecs,
1623 sdes_policy,
1624 GetCryptos(GetFirstAudioContentDescription(current_description)),
1625 audio_rtp_extensions_,
1626 current_streams,
1627 add_legacy_,
1628 bundle_enabled,
1629 audio_answer.get())) {
1630 return false; // Fails the session setup.
1631 }
1632
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001633 bool rejected = !options.has_audio() || audio_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001634 !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
1635 audio_answer->protocol(),
1636 audio_transport->secure());
1637 if (!rejected) {
1638 AddTransportAnswer(audio_content->name, *(audio_transport.get()), answer);
1639 } else {
1640 // RFC 3264
1641 // The answer MUST contain the same number of m-lines as the offer.
1642 LOG(LS_INFO) << "Audio is not supported in the answer.";
1643 }
1644
1645 answer->AddContent(audio_content->name, audio_content->type, rejected,
1646 audio_answer.release());
1647 return true;
1648}
1649
1650bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
1651 const SessionDescription* offer,
1652 const MediaSessionOptions& options,
1653 const SessionDescription* current_description,
1654 StreamParamsVec* current_streams,
1655 SessionDescription* answer) const {
1656 const ContentInfo* video_content = GetFirstVideoContent(offer);
1657 scoped_ptr<TransportDescription> video_transport(
1658 CreateTransportAnswer(video_content->name, offer,
1659 options.transport_options,
1660 current_description));
1661 if (!video_transport) {
1662 return false;
1663 }
1664
1665 scoped_ptr<VideoContentDescription> video_answer(
1666 new VideoContentDescription());
1667 // Do not require or create SDES cryptos if DTLS is used.
1668 cricket::SecurePolicy sdes_policy =
1669 video_transport->secure() ? cricket::SEC_DISABLED : secure();
1670 bool bundle_enabled =
1671 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1672 if (!CreateMediaContentAnswer(
1673 static_cast<const VideoContentDescription*>(
1674 video_content->description),
1675 options,
1676 video_codecs_,
1677 sdes_policy,
1678 GetCryptos(GetFirstVideoContentDescription(current_description)),
1679 video_rtp_extensions_,
1680 current_streams,
1681 add_legacy_,
1682 bundle_enabled,
1683 video_answer.get())) {
1684 return false;
1685 }
jiayl@webrtc.org742922b2014-10-07 21:32:43 +00001686 bool rejected = !options.has_video() || video_content->rejected ||
jiayl@webrtc.orge7d47a12014-08-05 19:19:05 +00001687 !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
1688 video_answer->protocol(),
1689 video_transport->secure());
1690 if (!rejected) {
1691 if (!AddTransportAnswer(video_content->name, *(video_transport.get()),
1692 answer)) {
1693 return false;
1694 }
1695 video_answer->set_bandwidth(options.video_bandwidth);
1696 } else {
1697 // RFC 3264
1698 // The answer MUST contain the same number of m-lines as the offer.
1699 LOG(LS_INFO) << "Video is not supported in the answer.";
1700 }
1701 answer->AddContent(video_content->name, video_content->type, rejected,
1702 video_answer.release());
1703 return true;
1704}
1705
1706bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
1707 const SessionDescription* offer,
1708 const MediaSessionOptions& options,
1709 const SessionDescription* current_description,
1710 StreamParamsVec* current_streams,
1711 SessionDescription* answer) const {
1712 const ContentInfo* data_content = GetFirstDataContent(offer);
1713 scoped_ptr<TransportDescription> data_transport(
1714 CreateTransportAnswer(data_content->name, offer,
1715 options.transport_options,
1716 current_description));
1717 if (!data_transport) {
1718 return false;
1719 }
1720 bool is_sctp = (options.data_channel_type == DCT_SCTP);
1721 std::vector<DataCodec> data_codecs(data_codecs_);
1722 FilterDataCodecs(&data_codecs, is_sctp);
1723
1724 scoped_ptr<DataContentDescription> data_answer(
1725 new DataContentDescription());
1726 // Do not require or create SDES cryptos if DTLS is used.
1727 cricket::SecurePolicy sdes_policy =
1728 data_transport->secure() ? cricket::SEC_DISABLED : secure();
1729 bool bundle_enabled =
1730 offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled;
1731 if (!CreateMediaContentAnswer(
1732 static_cast<const DataContentDescription*>(
1733 data_content->description),
1734 options,
1735 data_codecs_,
1736 sdes_policy,
1737 GetCryptos(GetFirstDataContentDescription(current_description)),
1738 RtpHeaderExtensions(),
1739 current_streams,
1740 add_legacy_,
1741 bundle_enabled,
1742 data_answer.get())) {
1743 return false; // Fails the session setup.
1744 }
1745
1746 bool rejected = !options.has_data() || data_content->rejected ||
1747 !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
1748 data_answer->protocol(),
1749 data_transport->secure());
1750 if (!rejected) {
1751 data_answer->set_bandwidth(options.data_bandwidth);
1752 if (!AddTransportAnswer(data_content->name, *(data_transport.get()),
1753 answer)) {
1754 return false;
1755 }
1756 } else {
1757 // RFC 3264
1758 // The answer MUST contain the same number of m-lines as the offer.
1759 LOG(LS_INFO) << "Data is not supported in the answer.";
1760 }
1761 answer->AddContent(data_content->name, data_content->type, rejected,
1762 data_answer.release());
1763 return true;
1764}
1765
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766bool IsMediaContent(const ContentInfo* content) {
1767 return (content &&
1768 (content->type == NS_JINGLE_RTP ||
1769 content->type == NS_JINGLE_DRAFT_SCTP));
1770}
1771
1772bool IsAudioContent(const ContentInfo* content) {
1773 return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
1774}
1775
1776bool IsVideoContent(const ContentInfo* content) {
1777 return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
1778}
1779
1780bool IsDataContent(const ContentInfo* content) {
1781 return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
1782}
1783
1784static const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
1785 MediaType media_type) {
1786 for (ContentInfos::const_iterator content = contents.begin();
1787 content != contents.end(); content++) {
1788 if (IsMediaContentOfType(&*content, media_type)) {
1789 return &*content;
1790 }
1791 }
1792 return NULL;
1793}
1794
1795const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
1796 return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
1797}
1798
1799const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
1800 return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
1801}
1802
1803const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
1804 return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
1805}
1806
1807static const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
1808 MediaType media_type) {
1809 if (sdesc == NULL)
1810 return NULL;
1811
1812 return GetFirstMediaContent(sdesc->contents(), media_type);
1813}
1814
1815const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
1816 return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
1817}
1818
1819const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
1820 return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
1821}
1822
1823const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
1824 return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
1825}
1826
1827const MediaContentDescription* GetFirstMediaContentDescription(
1828 const SessionDescription* sdesc, MediaType media_type) {
1829 const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
1830 const ContentDescription* description = content ? content->description : NULL;
1831 return static_cast<const MediaContentDescription*>(description);
1832}
1833
1834const AudioContentDescription* GetFirstAudioContentDescription(
1835 const SessionDescription* sdesc) {
1836 return static_cast<const AudioContentDescription*>(
1837 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO));
1838}
1839
1840const VideoContentDescription* GetFirstVideoContentDescription(
1841 const SessionDescription* sdesc) {
1842 return static_cast<const VideoContentDescription*>(
1843 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO));
1844}
1845
1846const DataContentDescription* GetFirstDataContentDescription(
1847 const SessionDescription* sdesc) {
1848 return static_cast<const DataContentDescription*>(
1849 GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA));
1850}
1851
1852bool GetMediaChannelNameFromComponent(
1853 int component, MediaType media_type, std::string* channel_name) {
1854 if (media_type == MEDIA_TYPE_AUDIO) {
1855 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1856 *channel_name = GICE_CHANNEL_NAME_RTP;
1857 return true;
1858 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1859 *channel_name = GICE_CHANNEL_NAME_RTCP;
1860 return true;
1861 }
1862 } else if (media_type == MEDIA_TYPE_VIDEO) {
1863 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1864 *channel_name = GICE_CHANNEL_NAME_VIDEO_RTP;
1865 return true;
1866 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1867 *channel_name = GICE_CHANNEL_NAME_VIDEO_RTCP;
1868 return true;
1869 }
1870 } else if (media_type == MEDIA_TYPE_DATA) {
1871 if (component == ICE_CANDIDATE_COMPONENT_RTP) {
1872 *channel_name = GICE_CHANNEL_NAME_DATA_RTP;
1873 return true;
1874 } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) {
1875 *channel_name = GICE_CHANNEL_NAME_DATA_RTCP;
1876 return true;
1877 }
1878 }
1879
1880 return false;
1881}
1882
1883bool GetMediaComponentFromChannelName(
1884 const std::string& channel_name, int* component) {
1885 if (channel_name == GICE_CHANNEL_NAME_RTP ||
1886 channel_name == GICE_CHANNEL_NAME_VIDEO_RTP ||
1887 channel_name == GICE_CHANNEL_NAME_DATA_RTP) {
1888 *component = ICE_CANDIDATE_COMPONENT_RTP;
1889 return true;
1890 } else if (channel_name == GICE_CHANNEL_NAME_RTCP ||
1891 channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP ||
1892 channel_name == GICE_CHANNEL_NAME_DATA_RTP) {
1893 *component = ICE_CANDIDATE_COMPONENT_RTCP;
1894 return true;
1895 }
1896
1897 return false;
1898}
1899
1900bool GetMediaTypeFromChannelName(
1901 const std::string& channel_name, MediaType* media_type) {
1902 if (channel_name == GICE_CHANNEL_NAME_RTP ||
1903 channel_name == GICE_CHANNEL_NAME_RTCP) {
1904 *media_type = MEDIA_TYPE_AUDIO;
1905 return true;
1906 } else if (channel_name == GICE_CHANNEL_NAME_VIDEO_RTP ||
1907 channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP) {
1908 *media_type = MEDIA_TYPE_VIDEO;
1909 return true;
1910 } else if (channel_name == GICE_CHANNEL_NAME_DATA_RTP ||
1911 channel_name == GICE_CHANNEL_NAME_DATA_RTCP) {
1912 *media_type = MEDIA_TYPE_DATA;
1913 return true;
1914 }
1915
1916 return false;
1917}
1918
1919} // namespace cricket