henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 35 | #include "talk/base/helpers.h" |
| 36 | #include "talk/base/logging.h" |
| 37 | #include "talk/base/scoped_ptr.h" |
| 38 | #include "talk/base/stringutils.h" |
| 39 | #include "talk/media/base/constants.h" |
| 40 | #include "talk/media/base/cryptoparams.h" |
| 41 | #include "talk/p2p/base/constants.h" |
| 42 | #include "talk/session/media/channelmanager.h" |
| 43 | #include "talk/session/media/srtpfilter.h" |
| 44 | #include "talk/xmpp/constants.h" |
| 45 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame^] | 46 | #ifdef HAVE_SCTP |
| 47 | #include "talk/media/sctp/sctpdataengine.h" |
| 48 | #else |
| 49 | static const uint32 kMaxSctpSid = USHRT_MAX; |
| 50 | #endif |
| 51 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | namespace { |
| 53 | const char kInline[] = "inline:"; |
| 54 | } |
| 55 | |
| 56 | namespace cricket { |
| 57 | |
| 58 | using talk_base::scoped_ptr; |
| 59 | |
| 60 | // RTP Profile names |
| 61 | // http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml |
| 62 | // RFC4585 |
| 63 | const char kMediaProtocolAvpf[] = "RTP/AVPF"; |
| 64 | // RFC5124 |
| 65 | const char kMediaProtocolSavpf[] = "RTP/SAVPF"; |
| 66 | |
| 67 | const char kMediaProtocolRtpPrefix[] = "RTP/"; |
| 68 | |
| 69 | const char kMediaProtocolSctp[] = "SCTP"; |
| 70 | const char kMediaProtocolDtlsSctp[] = "DTLS/SCTP"; |
| 71 | |
| 72 | static bool IsMediaContentOfType(const ContentInfo* content, |
| 73 | MediaType media_type) { |
| 74 | if (!IsMediaContent(content)) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | const MediaContentDescription* mdesc = |
| 79 | static_cast<const MediaContentDescription*>(content->description); |
| 80 | return mdesc && mdesc->type() == media_type; |
| 81 | } |
| 82 | |
| 83 | static bool CreateCryptoParams(int tag, const std::string& cipher, |
| 84 | CryptoParams *out) { |
| 85 | std::string key; |
| 86 | key.reserve(SRTP_MASTER_KEY_BASE64_LEN); |
| 87 | |
| 88 | if (!talk_base::CreateRandomString(SRTP_MASTER_KEY_BASE64_LEN, &key)) { |
| 89 | return false; |
| 90 | } |
| 91 | out->tag = tag; |
| 92 | out->cipher_suite = cipher; |
| 93 | out->key_params = kInline; |
| 94 | out->key_params += key; |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | #ifdef HAVE_SRTP |
| 99 | static bool AddCryptoParams(const std::string& cipher_suite, |
| 100 | CryptoParamsVec *out) { |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 101 | int size = static_cast<int>(out->size()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 102 | |
| 103 | out->resize(size + 1); |
| 104 | return CreateCryptoParams(size, cipher_suite, &out->at(size)); |
| 105 | } |
| 106 | |
| 107 | void AddMediaCryptos(const CryptoParamsVec& cryptos, |
| 108 | MediaContentDescription* media) { |
| 109 | for (CryptoParamsVec::const_iterator crypto = cryptos.begin(); |
| 110 | crypto != cryptos.end(); ++crypto) { |
| 111 | media->AddCrypto(*crypto); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites, |
| 116 | MediaContentDescription* media) { |
| 117 | CryptoParamsVec cryptos; |
| 118 | for (std::vector<std::string>::const_iterator it = crypto_suites.begin(); |
| 119 | it != crypto_suites.end(); ++it) { |
| 120 | if (!AddCryptoParams(*it, &cryptos)) { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | AddMediaCryptos(cryptos, media); |
| 125 | return true; |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | const CryptoParamsVec* GetCryptos(const MediaContentDescription* media) { |
| 130 | if (!media) { |
| 131 | return NULL; |
| 132 | } |
| 133 | return &media->cryptos(); |
| 134 | } |
| 135 | |
| 136 | bool FindMatchingCrypto(const CryptoParamsVec& cryptos, |
| 137 | const CryptoParams& crypto, |
| 138 | CryptoParams* out) { |
| 139 | for (CryptoParamsVec::const_iterator it = cryptos.begin(); |
| 140 | it != cryptos.end(); ++it) { |
| 141 | if (crypto.Matches(*it)) { |
| 142 | *out = *it; |
| 143 | return true; |
| 144 | } |
| 145 | } |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // For audio, HMAC 32 is prefered because of the low overhead. |
| 150 | void GetSupportedAudioCryptoSuites( |
| 151 | std::vector<std::string>* crypto_suites) { |
| 152 | #ifdef HAVE_SRTP |
| 153 | crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_32); |
| 154 | crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_80); |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | void GetSupportedVideoCryptoSuites( |
| 159 | std::vector<std::string>* crypto_suites) { |
| 160 | GetSupportedDefaultCryptoSuites(crypto_suites); |
| 161 | } |
| 162 | |
| 163 | void GetSupportedDataCryptoSuites( |
| 164 | std::vector<std::string>* crypto_suites) { |
| 165 | GetSupportedDefaultCryptoSuites(crypto_suites); |
| 166 | } |
| 167 | |
| 168 | void GetSupportedDefaultCryptoSuites( |
| 169 | std::vector<std::string>* crypto_suites) { |
| 170 | #ifdef HAVE_SRTP |
| 171 | crypto_suites->push_back(CS_AES_CM_128_HMAC_SHA1_80); |
| 172 | #endif |
| 173 | } |
| 174 | |
| 175 | // For video support only 80-bit SHA1 HMAC. For audio 32-bit HMAC is |
| 176 | // tolerated unless bundle is enabled because it is low overhead. Pick the |
| 177 | // crypto in the list that is supported. |
| 178 | static bool SelectCrypto(const MediaContentDescription* offer, |
| 179 | bool bundle, |
| 180 | CryptoParams *crypto) { |
| 181 | bool audio = offer->type() == MEDIA_TYPE_AUDIO; |
| 182 | const CryptoParamsVec& cryptos = offer->cryptos(); |
| 183 | |
| 184 | for (CryptoParamsVec::const_iterator i = cryptos.begin(); |
| 185 | i != cryptos.end(); ++i) { |
| 186 | if (CS_AES_CM_128_HMAC_SHA1_80 == i->cipher_suite || |
| 187 | (CS_AES_CM_128_HMAC_SHA1_32 == i->cipher_suite && audio && !bundle)) { |
| 188 | return CreateCryptoParams(i->tag, i->cipher_suite, crypto); |
| 189 | } |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | static const StreamParams* FindFirstStreamParamsByCname( |
| 195 | const StreamParamsVec& params_vec, |
| 196 | const std::string& cname) { |
| 197 | for (StreamParamsVec::const_iterator it = params_vec.begin(); |
| 198 | it != params_vec.end(); ++it) { |
| 199 | if (cname == it->cname) |
| 200 | return &*it; |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | // Generates a new CNAME or the CNAME of an already existing StreamParams |
| 206 | // if a StreamParams exist for another Stream in streams with sync_label |
| 207 | // sync_label. |
| 208 | static bool GenerateCname(const StreamParamsVec& params_vec, |
| 209 | const MediaSessionOptions::Streams& streams, |
| 210 | const std::string& synch_label, |
| 211 | std::string* cname) { |
| 212 | ASSERT(cname != NULL); |
| 213 | if (!cname) |
| 214 | return false; |
| 215 | |
| 216 | // Check if a CNAME exist for any of the other synched streams. |
| 217 | for (MediaSessionOptions::Streams::const_iterator stream_it = streams.begin(); |
| 218 | stream_it != streams.end() ; ++stream_it) { |
| 219 | if (synch_label != stream_it->sync_label) |
| 220 | continue; |
| 221 | |
| 222 | StreamParams param; |
| 223 | // groupid is empty for StreamParams generated using |
| 224 | // MediaSessionDescriptionFactory. |
| 225 | if (GetStreamByIds(params_vec, "", stream_it->id, |
| 226 | ¶m)) { |
| 227 | *cname = param.cname; |
| 228 | return true; |
| 229 | } |
| 230 | } |
| 231 | // No other stream seems to exist that we should sync with. |
| 232 | // Generate a random string for the RTCP CNAME, as stated in RFC 6222. |
| 233 | // This string is only used for synchronization, and therefore is opaque. |
| 234 | do { |
| 235 | if (!talk_base::CreateRandomString(16, cname)) { |
| 236 | ASSERT(false); |
| 237 | return false; |
| 238 | } |
| 239 | } while (FindFirstStreamParamsByCname(params_vec, *cname)); |
| 240 | |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | // Generate random SSRC values that are not already present in |params_vec|. |
| 245 | // Either 2 or 1 ssrcs will be generated based on |include_rtx_stream| being |
| 246 | // true or false. The generated values are added to |ssrcs|. |
| 247 | static void GenerateSsrcs(const StreamParamsVec& params_vec, |
| 248 | bool include_rtx_stream, |
| 249 | std::vector<uint32>* ssrcs) { |
| 250 | unsigned int num_ssrcs = include_rtx_stream ? 2 : 1; |
| 251 | for (unsigned int i = 0; i < num_ssrcs; i++) { |
| 252 | uint32 candidate; |
| 253 | do { |
| 254 | candidate = talk_base::CreateRandomNonZeroId(); |
| 255 | } while (GetStreamBySsrc(params_vec, candidate, NULL) || |
| 256 | std::count(ssrcs->begin(), ssrcs->end(), candidate) > 0); |
| 257 | ssrcs->push_back(candidate); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Returns false if we exhaust the range of SIDs. |
| 262 | static bool GenerateSctpSid(const StreamParamsVec& params_vec, |
| 263 | uint32* sid) { |
| 264 | if (params_vec.size() > kMaxSctpSid) { |
| 265 | LOG(LS_WARNING) << |
| 266 | "Could not generate an SCTP SID: too many SCTP streams."; |
| 267 | return false; |
| 268 | } |
| 269 | while (true) { |
| 270 | uint32 candidate = talk_base::CreateRandomNonZeroId() % kMaxSctpSid; |
| 271 | if (!GetStreamBySsrc(params_vec, candidate, NULL)) { |
| 272 | *sid = candidate; |
| 273 | return true; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | static bool GenerateSctpSids(const StreamParamsVec& params_vec, |
| 279 | std::vector<uint32>* sids) { |
| 280 | uint32 sid; |
| 281 | if (!GenerateSctpSid(params_vec, &sid)) { |
| 282 | LOG(LS_WARNING) << "Could not generated an SCTP SID."; |
| 283 | return false; |
| 284 | } |
| 285 | sids->push_back(sid); |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | // Finds all StreamParams of all media types and attach them to stream_params. |
| 290 | static void GetCurrentStreamParams(const SessionDescription* sdesc, |
| 291 | StreamParamsVec* stream_params) { |
| 292 | if (!sdesc) |
| 293 | return; |
| 294 | |
| 295 | const ContentInfos& contents = sdesc->contents(); |
| 296 | for (ContentInfos::const_iterator content = contents.begin(); |
| 297 | content != contents.end(); ++content) { |
| 298 | if (!IsMediaContent(&*content)) { |
| 299 | continue; |
| 300 | } |
| 301 | const MediaContentDescription* media = |
| 302 | static_cast<const MediaContentDescription*>( |
| 303 | content->description); |
| 304 | const StreamParamsVec& streams = media->streams(); |
| 305 | for (StreamParamsVec::const_iterator it = streams.begin(); |
| 306 | it != streams.end(); ++it) { |
| 307 | stream_params->push_back(*it); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | template <typename IdStruct> |
| 313 | class UsedIds { |
| 314 | public: |
| 315 | UsedIds(int min_allowed_id, int max_allowed_id) |
| 316 | : min_allowed_id_(min_allowed_id), |
| 317 | max_allowed_id_(max_allowed_id), |
| 318 | next_id_(max_allowed_id) { |
| 319 | } |
| 320 | |
| 321 | // Loops through all Id in |ids| and changes its id if it is |
| 322 | // already in use by another IdStruct. Call this methods with all Id |
| 323 | // in a session description to make sure no duplicate ids exists. |
| 324 | // Note that typename Id must be a type of IdStruct. |
| 325 | template <typename Id> |
| 326 | void FindAndSetIdUsed(std::vector<Id>* ids) { |
| 327 | for (typename std::vector<Id>::iterator it = ids->begin(); |
| 328 | it != ids->end(); ++it) { |
| 329 | FindAndSetIdUsed(&*it); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // Finds and sets an unused id if the |idstruct| id is already in use. |
| 334 | void FindAndSetIdUsed(IdStruct* idstruct) { |
| 335 | const int original_id = idstruct->id; |
| 336 | int new_id = idstruct->id; |
| 337 | |
| 338 | if (original_id > max_allowed_id_ || original_id < min_allowed_id_) { |
| 339 | // If the original id is not in range - this is an id that can't be |
| 340 | // dynamically changed. |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if (IsIdUsed(original_id)) { |
| 345 | new_id = FindUnusedId(); |
| 346 | LOG(LS_WARNING) << "Duplicate id found. Reassigning from " << original_id |
| 347 | << " to " << new_id; |
| 348 | idstruct->id = new_id; |
| 349 | } |
| 350 | SetIdUsed(new_id); |
| 351 | } |
| 352 | |
| 353 | private: |
| 354 | // Returns the first unused id in reverse order. |
| 355 | // This hopefully reduce the risk of more collisions. We want to change the |
| 356 | // default ids as little as possible. |
| 357 | int FindUnusedId() { |
| 358 | while (IsIdUsed(next_id_) && next_id_ >= min_allowed_id_) { |
| 359 | --next_id_; |
| 360 | } |
| 361 | ASSERT(next_id_ >= min_allowed_id_); |
| 362 | return next_id_; |
| 363 | } |
| 364 | |
| 365 | bool IsIdUsed(int new_id) { |
| 366 | return id_set_.find(new_id) != id_set_.end(); |
| 367 | } |
| 368 | |
| 369 | void SetIdUsed(int new_id) { |
| 370 | id_set_.insert(new_id); |
| 371 | } |
| 372 | |
| 373 | const int min_allowed_id_; |
| 374 | const int max_allowed_id_; |
| 375 | int next_id_; |
| 376 | std::set<int> id_set_; |
| 377 | }; |
| 378 | |
| 379 | // Helper class used for finding duplicate RTP payload types among audio, video |
| 380 | // and data codecs. When bundle is used the payload types may not collide. |
| 381 | class UsedPayloadTypes : public UsedIds<Codec> { |
| 382 | public: |
| 383 | UsedPayloadTypes() |
| 384 | : UsedIds<Codec>(kDynamicPayloadTypeMin, kDynamicPayloadTypeMax) { |
| 385 | } |
| 386 | |
| 387 | |
| 388 | private: |
| 389 | static const int kDynamicPayloadTypeMin = 96; |
| 390 | static const int kDynamicPayloadTypeMax = 127; |
| 391 | }; |
| 392 | |
| 393 | // Helper class used for finding duplicate RTP Header extension ids among |
| 394 | // audio and video extensions. |
| 395 | class UsedRtpHeaderExtensionIds : public UsedIds<RtpHeaderExtension> { |
| 396 | public: |
| 397 | UsedRtpHeaderExtensionIds() |
| 398 | : UsedIds<RtpHeaderExtension>(kLocalIdMin, kLocalIdMax) { |
| 399 | } |
| 400 | |
| 401 | private: |
| 402 | // Min and Max local identifier as specified by RFC5285. |
| 403 | static const int kLocalIdMin = 1; |
| 404 | static const int kLocalIdMax = 255; |
| 405 | }; |
| 406 | |
| 407 | static bool IsSctp(const MediaContentDescription* desc) { |
| 408 | return ((desc->protocol() == kMediaProtocolSctp) || |
| 409 | (desc->protocol() == kMediaProtocolDtlsSctp)); |
| 410 | } |
| 411 | |
| 412 | // Adds a StreamParams for each Stream in Streams with media type |
| 413 | // media_type to content_description. |
| 414 | // |current_params| - All currently known StreamParams of any media type. |
| 415 | template <class C> |
| 416 | static bool AddStreamParams( |
| 417 | MediaType media_type, |
| 418 | const MediaSessionOptions::Streams& streams, |
| 419 | StreamParamsVec* current_streams, |
| 420 | MediaContentDescriptionImpl<C>* content_description, |
| 421 | const bool add_legacy_stream) { |
| 422 | const bool include_rtx_stream = |
| 423 | ContainsRtxCodec(content_description->codecs()); |
| 424 | |
| 425 | if (streams.empty() && add_legacy_stream) { |
| 426 | // TODO(perkj): Remove this legacy stream when all apps use StreamParams. |
| 427 | std::vector<uint32> ssrcs; |
| 428 | if (IsSctp(content_description)) { |
| 429 | GenerateSctpSids(*current_streams, &ssrcs); |
| 430 | } else { |
| 431 | GenerateSsrcs(*current_streams, include_rtx_stream, &ssrcs); |
| 432 | } |
| 433 | if (include_rtx_stream) { |
| 434 | content_description->AddLegacyStream(ssrcs[0], ssrcs[1]); |
| 435 | content_description->set_multistream(true); |
| 436 | } else { |
| 437 | content_description->AddLegacyStream(ssrcs[0]); |
| 438 | } |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | MediaSessionOptions::Streams::const_iterator stream_it; |
| 443 | for (stream_it = streams.begin(); |
| 444 | stream_it != streams.end(); ++stream_it) { |
| 445 | if (stream_it->type != media_type) |
| 446 | continue; // Wrong media type. |
| 447 | |
| 448 | StreamParams param; |
| 449 | // groupid is empty for StreamParams generated using |
| 450 | // MediaSessionDescriptionFactory. |
| 451 | if (!GetStreamByIds(*current_streams, "", stream_it->id, |
| 452 | ¶m)) { |
| 453 | // This is a new stream. |
| 454 | // Get a CNAME. Either new or same as one of the other synched streams. |
| 455 | std::string cname; |
| 456 | if (!GenerateCname(*current_streams, streams, stream_it->sync_label, |
| 457 | &cname)) { |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | std::vector<uint32> ssrcs; |
| 462 | if (IsSctp(content_description)) { |
| 463 | GenerateSctpSids(*current_streams, &ssrcs); |
| 464 | } else { |
| 465 | GenerateSsrcs(*current_streams, include_rtx_stream, &ssrcs); |
| 466 | } |
| 467 | StreamParams stream_param; |
| 468 | stream_param.id = stream_it->id; |
| 469 | stream_param.ssrcs.push_back(ssrcs[0]); |
| 470 | if (include_rtx_stream) { |
| 471 | stream_param.AddFidSsrc(ssrcs[0], ssrcs[1]); |
| 472 | content_description->set_multistream(true); |
| 473 | } |
| 474 | stream_param.cname = cname; |
| 475 | stream_param.sync_label = stream_it->sync_label; |
| 476 | content_description->AddStream(stream_param); |
| 477 | |
| 478 | // Store the new StreamParams in current_streams. |
| 479 | // This is necessary so that we can use the CNAME for other media types. |
| 480 | current_streams->push_back(stream_param); |
| 481 | } else { |
| 482 | content_description->AddStream(param); |
| 483 | } |
| 484 | } |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | // Updates the transport infos of the |sdesc| according to the given |
| 489 | // |bundle_group|. The transport infos of the content names within the |
| 490 | // |bundle_group| should be updated to use the ufrag and pwd of the first |
| 491 | // content within the |bundle_group|. |
| 492 | static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group, |
| 493 | SessionDescription* sdesc) { |
| 494 | // The bundle should not be empty. |
| 495 | if (!sdesc || !bundle_group.FirstContentName()) { |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | // We should definitely have a transport for the first content. |
| 500 | std::string selected_content_name = *bundle_group.FirstContentName(); |
| 501 | const TransportInfo* selected_transport_info = |
| 502 | sdesc->GetTransportInfoByName(selected_content_name); |
| 503 | if (!selected_transport_info) { |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | // Set the other contents to use the same ICE credentials. |
| 508 | const std::string selected_ufrag = |
| 509 | selected_transport_info->description.ice_ufrag; |
| 510 | const std::string selected_pwd = |
| 511 | selected_transport_info->description.ice_pwd; |
| 512 | for (TransportInfos::iterator it = |
| 513 | sdesc->transport_infos().begin(); |
| 514 | it != sdesc->transport_infos().end(); ++it) { |
| 515 | if (bundle_group.HasContentName(it->content_name) && |
| 516 | it->content_name != selected_content_name) { |
| 517 | it->description.ice_ufrag = selected_ufrag; |
| 518 | it->description.ice_pwd = selected_pwd; |
| 519 | } |
| 520 | } |
| 521 | return true; |
| 522 | } |
| 523 | |
| 524 | // Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and |
| 525 | // sets it to |cryptos|. |
| 526 | static bool GetCryptosByName(const SessionDescription* sdesc, |
| 527 | const std::string& content_name, |
| 528 | CryptoParamsVec* cryptos) { |
| 529 | if (!sdesc || !cryptos) { |
| 530 | return false; |
| 531 | } |
| 532 | |
| 533 | const ContentInfo* content = sdesc->GetContentByName(content_name); |
| 534 | if (!IsMediaContent(content) || !content->description) { |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | const MediaContentDescription* media_desc = |
| 539 | static_cast<const MediaContentDescription*>(content->description); |
| 540 | *cryptos = media_desc->cryptos(); |
| 541 | return true; |
| 542 | } |
| 543 | |
| 544 | // Predicate function used by the remove_if. |
| 545 | // Returns true if the |crypto|'s cipher_suite is not found in |filter|. |
| 546 | static bool CryptoNotFound(const CryptoParams crypto, |
| 547 | const CryptoParamsVec* filter) { |
| 548 | if (filter == NULL) { |
| 549 | return true; |
| 550 | } |
| 551 | for (CryptoParamsVec::const_iterator it = filter->begin(); |
| 552 | it != filter->end(); ++it) { |
| 553 | if (it->cipher_suite == crypto.cipher_suite) { |
| 554 | return false; |
| 555 | } |
| 556 | } |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | // Prunes the |target_cryptos| by removing the crypto params (cipher_suite) |
| 561 | // which are not available in |filter|. |
| 562 | static void PruneCryptos(const CryptoParamsVec& filter, |
| 563 | CryptoParamsVec* target_cryptos) { |
| 564 | if (!target_cryptos) { |
| 565 | return; |
| 566 | } |
| 567 | target_cryptos->erase(std::remove_if(target_cryptos->begin(), |
| 568 | target_cryptos->end(), |
| 569 | bind2nd(ptr_fun(CryptoNotFound), |
| 570 | &filter)), |
| 571 | target_cryptos->end()); |
| 572 | } |
| 573 | |
| 574 | static bool IsRtpContent(SessionDescription* sdesc, |
| 575 | const std::string& content_name) { |
| 576 | bool is_rtp = false; |
| 577 | ContentInfo* content = sdesc->GetContentByName(content_name); |
| 578 | if (IsMediaContent(content)) { |
| 579 | MediaContentDescription* media_desc = |
| 580 | static_cast<MediaContentDescription*>(content->description); |
| 581 | if (!media_desc) { |
| 582 | return false; |
| 583 | } |
| 584 | is_rtp = media_desc->protocol().empty() || |
| 585 | talk_base::starts_with(media_desc->protocol().data(), |
| 586 | kMediaProtocolRtpPrefix); |
| 587 | } |
| 588 | return is_rtp; |
| 589 | } |
| 590 | |
| 591 | // Updates the crypto parameters of the |sdesc| according to the given |
| 592 | // |bundle_group|. The crypto parameters of all the contents within the |
| 593 | // |bundle_group| should be updated to use the common subset of the |
| 594 | // available cryptos. |
| 595 | static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group, |
| 596 | SessionDescription* sdesc) { |
| 597 | // The bundle should not be empty. |
| 598 | if (!sdesc || !bundle_group.FirstContentName()) { |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | // Get the common cryptos. |
| 603 | const ContentNames& content_names = bundle_group.content_names(); |
| 604 | CryptoParamsVec common_cryptos; |
| 605 | for (ContentNames::const_iterator it = content_names.begin(); |
| 606 | it != content_names.end(); ++it) { |
| 607 | if (!IsRtpContent(sdesc, *it)) { |
| 608 | continue; |
| 609 | } |
| 610 | if (it == content_names.begin()) { |
| 611 | // Initial the common_cryptos with the first content in the bundle group. |
| 612 | if (!GetCryptosByName(sdesc, *it, &common_cryptos)) { |
| 613 | return false; |
| 614 | } |
| 615 | if (common_cryptos.empty()) { |
| 616 | // If there's no crypto params, we should just return. |
| 617 | return true; |
| 618 | } |
| 619 | } else { |
| 620 | CryptoParamsVec cryptos; |
| 621 | if (!GetCryptosByName(sdesc, *it, &cryptos)) { |
| 622 | return false; |
| 623 | } |
| 624 | PruneCryptos(cryptos, &common_cryptos); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | if (common_cryptos.empty()) { |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | // Update to use the common cryptos. |
| 633 | for (ContentNames::const_iterator it = content_names.begin(); |
| 634 | it != content_names.end(); ++it) { |
| 635 | if (!IsRtpContent(sdesc, *it)) { |
| 636 | continue; |
| 637 | } |
| 638 | ContentInfo* content = sdesc->GetContentByName(*it); |
| 639 | if (IsMediaContent(content)) { |
| 640 | MediaContentDescription* media_desc = |
| 641 | static_cast<MediaContentDescription*>(content->description); |
| 642 | if (!media_desc) { |
| 643 | return false; |
| 644 | } |
| 645 | media_desc->set_cryptos(common_cryptos); |
| 646 | } |
| 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
| 651 | template <class C> |
| 652 | static bool ContainsRtxCodec(const std::vector<C>& codecs) { |
| 653 | typename std::vector<C>::const_iterator it; |
| 654 | for (it = codecs.begin(); it != codecs.end(); ++it) { |
| 655 | if (IsRtxCodec(*it)) { |
| 656 | return true; |
| 657 | } |
| 658 | } |
| 659 | return false; |
| 660 | } |
| 661 | |
| 662 | template <class C> |
| 663 | static bool IsRtxCodec(const C& codec) { |
| 664 | return stricmp(codec.name.c_str(), kRtxCodecName) == 0; |
| 665 | } |
| 666 | |
| 667 | // Create a media content to be offered in a session-initiate, |
| 668 | // according to the given options.rtcp_mux, options.is_muc, |
| 669 | // options.streams, codecs, secure_transport, crypto, and streams. If we don't |
| 670 | // currently have crypto (in current_cryptos) and it is enabled (in |
| 671 | // secure_policy), crypto is created (according to crypto_suites). If |
| 672 | // add_legacy_stream is true, and current_streams is empty, a legacy |
| 673 | // stream is created. The created content is added to the offer. |
| 674 | template <class C> |
| 675 | static bool CreateMediaContentOffer( |
| 676 | const MediaSessionOptions& options, |
| 677 | const std::vector<C>& codecs, |
| 678 | const SecureMediaPolicy& secure_policy, |
| 679 | const CryptoParamsVec* current_cryptos, |
| 680 | const std::vector<std::string>& crypto_suites, |
| 681 | const RtpHeaderExtensions& rtp_extensions, |
| 682 | bool add_legacy_stream, |
| 683 | StreamParamsVec* current_streams, |
| 684 | MediaContentDescriptionImpl<C>* offer) { |
| 685 | offer->AddCodecs(codecs); |
| 686 | offer->SortCodecs(); |
| 687 | |
| 688 | offer->set_crypto_required(secure_policy == SEC_REQUIRED); |
| 689 | offer->set_rtcp_mux(options.rtcp_mux_enabled); |
| 690 | offer->set_multistream(options.is_muc); |
| 691 | offer->set_rtp_header_extensions(rtp_extensions); |
| 692 | |
| 693 | if (!AddStreamParams( |
| 694 | offer->type(), options.streams, current_streams, |
| 695 | offer, add_legacy_stream)) { |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | #ifdef HAVE_SRTP |
| 700 | if (secure_policy != SEC_DISABLED) { |
| 701 | if (current_cryptos) { |
| 702 | AddMediaCryptos(*current_cryptos, offer); |
| 703 | } |
| 704 | if (offer->cryptos().empty()) { |
| 705 | if (!CreateMediaCryptos(crypto_suites, offer)) { |
| 706 | return false; |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | #endif |
| 711 | |
| 712 | if (offer->crypto_required() && offer->cryptos().empty()) { |
| 713 | return false; |
| 714 | } |
| 715 | return true; |
| 716 | } |
| 717 | |
| 718 | template <class C> |
| 719 | static void NegotiateCodecs(const std::vector<C>& local_codecs, |
| 720 | const std::vector<C>& offered_codecs, |
| 721 | std::vector<C>* negotiated_codecs) { |
| 722 | typename std::vector<C>::const_iterator ours; |
| 723 | for (ours = local_codecs.begin(); |
| 724 | ours != local_codecs.end(); ++ours) { |
| 725 | typename std::vector<C>::const_iterator theirs; |
| 726 | for (theirs = offered_codecs.begin(); |
| 727 | theirs != offered_codecs.end(); ++theirs) { |
| 728 | if (ours->Matches(*theirs)) { |
| 729 | C negotiated = *ours; |
| 730 | negotiated.IntersectFeedbackParams(*theirs); |
| 731 | if (IsRtxCodec(negotiated)) { |
| 732 | // Only negotiate RTX if kCodecParamAssociatedPayloadType has been |
| 733 | // set. |
| 734 | std::string apt_value; |
| 735 | if (!theirs->GetParam(kCodecParamAssociatedPayloadType, &apt_value)) { |
| 736 | LOG(LS_WARNING) << "RTX missing associated payload type."; |
| 737 | continue; |
| 738 | } |
| 739 | negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_value); |
| 740 | } |
| 741 | negotiated.id = theirs->id; |
| 742 | negotiated_codecs->push_back(negotiated); |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | template <class C> |
| 749 | static bool FindMatchingCodec(const std::vector<C>& codecs, |
| 750 | const C& codec_to_match, |
| 751 | C* found_codec) { |
| 752 | for (typename std::vector<C>::const_iterator it = codecs.begin(); |
| 753 | it != codecs.end(); ++it) { |
| 754 | if (it->Matches(codec_to_match)) { |
| 755 | if (found_codec != NULL) { |
| 756 | *found_codec= *it; |
| 757 | } |
| 758 | return true; |
| 759 | } |
| 760 | } |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | // Adds all codecs from |reference_codecs| to |offered_codecs| that dont' |
| 765 | // already exist in |offered_codecs| and ensure the payload types don't |
| 766 | // collide. |
| 767 | template <class C> |
| 768 | static void FindCodecsToOffer( |
| 769 | const std::vector<C>& reference_codecs, |
| 770 | std::vector<C>* offered_codecs, |
| 771 | UsedPayloadTypes* used_pltypes) { |
| 772 | |
| 773 | typedef std::map<int, C> RtxCodecReferences; |
| 774 | RtxCodecReferences new_rtx_codecs; |
| 775 | |
| 776 | // Find all new RTX codecs. |
| 777 | for (typename std::vector<C>::const_iterator it = reference_codecs.begin(); |
| 778 | it != reference_codecs.end(); ++it) { |
| 779 | if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && IsRtxCodec(*it)) { |
| 780 | C rtx_codec = *it; |
| 781 | int referenced_pl_type = |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 782 | talk_base::FromString<int>(0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 783 | rtx_codec.params[kCodecParamAssociatedPayloadType]); |
| 784 | new_rtx_codecs.insert(std::pair<int, C>(referenced_pl_type, |
| 785 | rtx_codec)); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | // Add all new codecs that are not RTX codecs. |
| 790 | for (typename std::vector<C>::const_iterator it = reference_codecs.begin(); |
| 791 | it != reference_codecs.end(); ++it) { |
| 792 | if (!FindMatchingCodec<C>(*offered_codecs, *it, NULL) && !IsRtxCodec(*it)) { |
| 793 | C codec = *it; |
| 794 | int original_payload_id = codec.id; |
| 795 | used_pltypes->FindAndSetIdUsed(&codec); |
| 796 | offered_codecs->push_back(codec); |
| 797 | |
| 798 | // If this codec is referenced by a new RTX codec, update the reference |
| 799 | // in the RTX codec with the new payload type. |
| 800 | typename RtxCodecReferences::iterator rtx_it = |
| 801 | new_rtx_codecs.find(original_payload_id); |
| 802 | if (rtx_it != new_rtx_codecs.end()) { |
| 803 | C& rtx_codec = rtx_it->second; |
| 804 | rtx_codec.params[kCodecParamAssociatedPayloadType] = |
| 805 | talk_base::ToString(codec.id); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | // Add all new RTX codecs. |
| 811 | for (typename RtxCodecReferences::iterator it = new_rtx_codecs.begin(); |
| 812 | it != new_rtx_codecs.end(); ++it) { |
| 813 | C& rtx_codec = it->second; |
| 814 | used_pltypes->FindAndSetIdUsed(&rtx_codec); |
| 815 | offered_codecs->push_back(rtx_codec); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | |
| 820 | static bool FindByUri(const RtpHeaderExtensions& extensions, |
| 821 | const RtpHeaderExtension& ext_to_match, |
| 822 | RtpHeaderExtension* found_extension) { |
| 823 | for (RtpHeaderExtensions::const_iterator it = extensions.begin(); |
| 824 | it != extensions.end(); ++it) { |
| 825 | // We assume that all URIs are given in a canonical format. |
| 826 | if (it->uri == ext_to_match.uri) { |
| 827 | if (found_extension != NULL) { |
| 828 | *found_extension= *it; |
| 829 | } |
| 830 | return true; |
| 831 | } |
| 832 | } |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | static void FindAndSetRtpHdrExtUsed( |
| 837 | const RtpHeaderExtensions& reference_extensions, |
| 838 | RtpHeaderExtensions* offered_extensions, |
| 839 | UsedRtpHeaderExtensionIds* used_extensions) { |
| 840 | for (RtpHeaderExtensions::const_iterator it = reference_extensions.begin(); |
| 841 | it != reference_extensions.end(); ++it) { |
| 842 | if (!FindByUri(*offered_extensions, *it, NULL)) { |
| 843 | RtpHeaderExtension ext = *it; |
| 844 | used_extensions->FindAndSetIdUsed(&ext); |
| 845 | offered_extensions->push_back(ext); |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | static void NegotiateRtpHeaderExtensions( |
| 851 | const RtpHeaderExtensions& local_extensions, |
| 852 | const RtpHeaderExtensions& offered_extensions, |
| 853 | RtpHeaderExtensions* negotiated_extenstions) { |
| 854 | RtpHeaderExtensions::const_iterator ours; |
| 855 | for (ours = local_extensions.begin(); |
| 856 | ours != local_extensions.end(); ++ours) { |
| 857 | RtpHeaderExtension theirs; |
| 858 | if (FindByUri(offered_extensions, *ours, &theirs)) { |
| 859 | // We respond with their RTP header extension id. |
| 860 | negotiated_extenstions->push_back(theirs); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | static void StripCNCodecs(AudioCodecs* audio_codecs) { |
| 866 | AudioCodecs::iterator iter = audio_codecs->begin(); |
| 867 | while (iter != audio_codecs->end()) { |
| 868 | if (stricmp(iter->name.c_str(), kComfortNoiseCodecName) == 0) { |
| 869 | iter = audio_codecs->erase(iter); |
| 870 | } else { |
| 871 | ++iter; |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | // Create a media content to be answered in a session-accept, |
| 877 | // according to the given options.rtcp_mux, options.streams, codecs, |
| 878 | // crypto, and streams. If we don't currently have crypto (in |
| 879 | // current_cryptos) and it is enabled (in secure_policy), crypto is |
| 880 | // created (according to crypto_suites). If add_legacy_stream is |
| 881 | // true, and current_streams is empty, a legacy stream is created. |
| 882 | // The codecs, rtcp_mux, and crypto are all negotiated with the offer |
| 883 | // from the incoming session-initiate. If the negotiation fails, this |
| 884 | // method returns false. The created content is added to the offer. |
| 885 | template <class C> |
| 886 | static bool CreateMediaContentAnswer( |
| 887 | const MediaContentDescriptionImpl<C>* offer, |
| 888 | const MediaSessionOptions& options, |
| 889 | const std::vector<C>& local_codecs, |
| 890 | const SecureMediaPolicy& sdes_policy, |
| 891 | const CryptoParamsVec* current_cryptos, |
| 892 | const RtpHeaderExtensions& local_rtp_extenstions, |
| 893 | StreamParamsVec* current_streams, |
| 894 | bool add_legacy_stream, |
| 895 | bool bundle_enabled, |
| 896 | MediaContentDescriptionImpl<C>* answer) { |
| 897 | std::vector<C> negotiated_codecs; |
| 898 | NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs); |
| 899 | answer->AddCodecs(negotiated_codecs); |
| 900 | answer->SortCodecs(); |
| 901 | answer->set_protocol(offer->protocol()); |
| 902 | RtpHeaderExtensions negotiated_rtp_extensions; |
| 903 | NegotiateRtpHeaderExtensions(local_rtp_extenstions, |
| 904 | offer->rtp_header_extensions(), |
| 905 | &negotiated_rtp_extensions); |
| 906 | answer->set_rtp_header_extensions(negotiated_rtp_extensions); |
| 907 | |
| 908 | answer->set_rtcp_mux(options.rtcp_mux_enabled && offer->rtcp_mux()); |
| 909 | |
| 910 | if (sdes_policy != SEC_DISABLED) { |
| 911 | CryptoParams crypto; |
| 912 | if (SelectCrypto(offer, bundle_enabled, &crypto)) { |
| 913 | if (current_cryptos) { |
| 914 | FindMatchingCrypto(*current_cryptos, crypto, &crypto); |
| 915 | } |
| 916 | answer->AddCrypto(crypto); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | if (answer->cryptos().empty() && |
| 921 | (offer->crypto_required() || sdes_policy == SEC_REQUIRED)) { |
| 922 | return false; |
| 923 | } |
| 924 | |
| 925 | if (!AddStreamParams( |
| 926 | answer->type(), options.streams, current_streams, |
| 927 | answer, add_legacy_stream)) { |
| 928 | return false; // Something went seriously wrong. |
| 929 | } |
| 930 | |
| 931 | // Make sure the answer media content direction is per default set as |
| 932 | // described in RFC3264 section 6.1. |
| 933 | switch (offer->direction()) { |
| 934 | case MD_INACTIVE: |
| 935 | answer->set_direction(MD_INACTIVE); |
| 936 | break; |
| 937 | case MD_SENDONLY: |
| 938 | answer->set_direction(MD_RECVONLY); |
| 939 | break; |
| 940 | case MD_RECVONLY: |
| 941 | answer->set_direction(MD_SENDONLY); |
| 942 | break; |
| 943 | case MD_SENDRECV: |
| 944 | answer->set_direction(MD_SENDRECV); |
| 945 | break; |
| 946 | default: |
| 947 | break; |
| 948 | } |
| 949 | |
| 950 | return true; |
| 951 | } |
| 952 | |
| 953 | static bool IsMediaProtocolSupported(MediaType type, |
| 954 | const std::string& protocol) { |
| 955 | // Data channels can have a protocol of SCTP or SCTP/DTLS. |
| 956 | if (type == MEDIA_TYPE_DATA && |
| 957 | (protocol == kMediaProtocolSctp || |
| 958 | protocol == kMediaProtocolDtlsSctp)) { |
| 959 | return true; |
| 960 | } |
| 961 | // Since not all applications serialize and deserialize the media protocol, |
| 962 | // we will have to accept |protocol| to be empty. |
| 963 | return protocol == kMediaProtocolAvpf || protocol == kMediaProtocolSavpf || |
| 964 | protocol.empty(); |
| 965 | } |
| 966 | |
| 967 | static void SetMediaProtocol(bool secure_transport, |
| 968 | MediaContentDescription* desc) { |
| 969 | if (!desc->cryptos().empty() || secure_transport) |
| 970 | desc->set_protocol(kMediaProtocolSavpf); |
| 971 | else |
| 972 | desc->set_protocol(kMediaProtocolAvpf); |
| 973 | } |
| 974 | |
| 975 | void MediaSessionOptions::AddStream(MediaType type, |
| 976 | const std::string& id, |
| 977 | const std::string& sync_label) { |
| 978 | streams.push_back(Stream(type, id, sync_label)); |
| 979 | |
| 980 | if (type == MEDIA_TYPE_VIDEO) |
| 981 | has_video = true; |
| 982 | else if (type == MEDIA_TYPE_AUDIO) |
| 983 | has_audio = true; |
| 984 | // If we haven't already set the data_channel_type, and we add a |
| 985 | // stream, we assume it's an RTP data stream. |
| 986 | else if (type == MEDIA_TYPE_DATA && data_channel_type == DCT_NONE) |
| 987 | data_channel_type = DCT_RTP; |
| 988 | } |
| 989 | |
| 990 | void MediaSessionOptions::RemoveStream(MediaType type, |
| 991 | const std::string& id) { |
| 992 | Streams::iterator stream_it = streams.begin(); |
| 993 | for (; stream_it != streams.end(); ++stream_it) { |
| 994 | if (stream_it->type == type && stream_it->id == id) { |
| 995 | streams.erase(stream_it); |
| 996 | return; |
| 997 | } |
| 998 | } |
| 999 | ASSERT(false); |
| 1000 | } |
| 1001 | |
| 1002 | MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( |
| 1003 | const TransportDescriptionFactory* transport_desc_factory) |
| 1004 | : secure_(SEC_DISABLED), |
| 1005 | add_legacy_(true), |
| 1006 | transport_desc_factory_(transport_desc_factory) { |
| 1007 | } |
| 1008 | |
| 1009 | MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( |
| 1010 | ChannelManager* channel_manager, |
| 1011 | const TransportDescriptionFactory* transport_desc_factory) |
| 1012 | : secure_(SEC_DISABLED), |
| 1013 | add_legacy_(true), |
| 1014 | transport_desc_factory_(transport_desc_factory) { |
| 1015 | channel_manager->GetSupportedAudioCodecs(&audio_codecs_); |
| 1016 | channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_); |
| 1017 | channel_manager->GetSupportedVideoCodecs(&video_codecs_); |
| 1018 | channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_); |
| 1019 | channel_manager->GetSupportedDataCodecs(&data_codecs_); |
| 1020 | } |
| 1021 | |
| 1022 | SessionDescription* MediaSessionDescriptionFactory::CreateOffer( |
| 1023 | const MediaSessionOptions& options, |
| 1024 | const SessionDescription* current_description) const { |
| 1025 | bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED); |
| 1026 | |
| 1027 | scoped_ptr<SessionDescription> offer(new SessionDescription()); |
| 1028 | |
| 1029 | StreamParamsVec current_streams; |
| 1030 | GetCurrentStreamParams(current_description, ¤t_streams); |
| 1031 | |
| 1032 | AudioCodecs audio_codecs; |
| 1033 | VideoCodecs video_codecs; |
| 1034 | DataCodecs data_codecs; |
| 1035 | GetCodecsToOffer(current_description, &audio_codecs, &video_codecs, |
| 1036 | &data_codecs); |
| 1037 | |
| 1038 | if (!options.vad_enabled) { |
| 1039 | // If application doesn't want CN codecs in offer. |
| 1040 | StripCNCodecs(&audio_codecs); |
| 1041 | } |
| 1042 | |
| 1043 | RtpHeaderExtensions audio_rtp_extensions; |
| 1044 | RtpHeaderExtensions video_rtp_extensions; |
| 1045 | GetRtpHdrExtsToOffer(current_description, &audio_rtp_extensions, |
| 1046 | &video_rtp_extensions); |
| 1047 | |
| 1048 | // Handle m=audio. |
| 1049 | if (options.has_audio) { |
| 1050 | scoped_ptr<AudioContentDescription> audio(new AudioContentDescription()); |
| 1051 | std::vector<std::string> crypto_suites; |
| 1052 | GetSupportedAudioCryptoSuites(&crypto_suites); |
| 1053 | if (!CreateMediaContentOffer( |
| 1054 | options, |
| 1055 | audio_codecs, |
| 1056 | secure(), |
| 1057 | GetCryptos(GetFirstAudioContentDescription(current_description)), |
| 1058 | crypto_suites, |
| 1059 | audio_rtp_extensions, |
| 1060 | add_legacy_, |
| 1061 | ¤t_streams, |
| 1062 | audio.get())) { |
| 1063 | return NULL; |
| 1064 | } |
| 1065 | |
| 1066 | audio->set_lang(lang_); |
| 1067 | SetMediaProtocol(secure_transport, audio.get()); |
| 1068 | offer->AddContent(CN_AUDIO, NS_JINGLE_RTP, audio.release()); |
| 1069 | if (!AddTransportOffer(CN_AUDIO, options.transport_options, |
| 1070 | current_description, offer.get())) { |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | // Handle m=video. |
| 1076 | if (options.has_video) { |
| 1077 | scoped_ptr<VideoContentDescription> video(new VideoContentDescription()); |
| 1078 | std::vector<std::string> crypto_suites; |
| 1079 | GetSupportedVideoCryptoSuites(&crypto_suites); |
| 1080 | if (!CreateMediaContentOffer( |
| 1081 | options, |
| 1082 | video_codecs, |
| 1083 | secure(), |
| 1084 | GetCryptos(GetFirstVideoContentDescription(current_description)), |
| 1085 | crypto_suites, |
| 1086 | video_rtp_extensions, |
| 1087 | add_legacy_, |
| 1088 | ¤t_streams, |
| 1089 | video.get())) { |
| 1090 | return NULL; |
| 1091 | } |
| 1092 | |
| 1093 | video->set_bandwidth(options.video_bandwidth); |
| 1094 | SetMediaProtocol(secure_transport, video.get()); |
| 1095 | offer->AddContent(CN_VIDEO, NS_JINGLE_RTP, video.release()); |
| 1096 | if (!AddTransportOffer(CN_VIDEO, options.transport_options, |
| 1097 | current_description, offer.get())) { |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // Handle m=data. |
| 1103 | if (options.has_data()) { |
| 1104 | scoped_ptr<DataContentDescription> data(new DataContentDescription()); |
| 1105 | bool is_sctp = (options.data_channel_type == DCT_SCTP); |
| 1106 | |
| 1107 | std::vector<std::string> crypto_suites; |
| 1108 | cricket::SecurePolicy sdes_policy = secure(); |
| 1109 | if (is_sctp) { |
| 1110 | // SDES doesn't make sense for SCTP, so we disable it, and we only |
| 1111 | // get SDES crypto suites for RTP-based data channels. |
| 1112 | sdes_policy = cricket::SEC_DISABLED; |
| 1113 | // Unlike SetMediaProtocol below, we need to set the protocol |
| 1114 | // before we call CreateMediaContentOffer. Otherwise, |
| 1115 | // CreateMediaContentOffer won't know this is SCTP and will |
| 1116 | // generate SSRCs rather than SIDs. |
| 1117 | data->set_protocol( |
| 1118 | secure_transport ? kMediaProtocolDtlsSctp : kMediaProtocolSctp); |
| 1119 | } else { |
| 1120 | GetSupportedDataCryptoSuites(&crypto_suites); |
| 1121 | } |
| 1122 | |
| 1123 | if (!CreateMediaContentOffer( |
| 1124 | options, |
| 1125 | data_codecs, |
| 1126 | sdes_policy, |
| 1127 | GetCryptos(GetFirstDataContentDescription(current_description)), |
| 1128 | crypto_suites, |
| 1129 | RtpHeaderExtensions(), |
| 1130 | add_legacy_, |
| 1131 | ¤t_streams, |
| 1132 | data.get())) { |
| 1133 | return NULL; |
| 1134 | } |
| 1135 | |
| 1136 | if (is_sctp) { |
| 1137 | offer->AddContent(CN_DATA, NS_JINGLE_DRAFT_SCTP, data.release()); |
| 1138 | } else { |
| 1139 | data->set_bandwidth(options.data_bandwidth); |
| 1140 | SetMediaProtocol(secure_transport, data.get()); |
| 1141 | offer->AddContent(CN_DATA, NS_JINGLE_RTP, data.release()); |
| 1142 | } |
| 1143 | if (!AddTransportOffer(CN_DATA, options.transport_options, |
| 1144 | current_description, offer.get())) { |
| 1145 | return NULL; |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | // Bundle the contents together, if we've been asked to do so, and update any |
| 1150 | // parameters that need to be tweaked for BUNDLE. |
| 1151 | if (options.bundle_enabled) { |
| 1152 | ContentGroup offer_bundle(GROUP_TYPE_BUNDLE); |
| 1153 | for (ContentInfos::const_iterator content = offer->contents().begin(); |
| 1154 | content != offer->contents().end(); ++content) { |
| 1155 | offer_bundle.AddContentName(content->name); |
| 1156 | } |
| 1157 | offer->AddGroup(offer_bundle); |
| 1158 | if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) { |
| 1159 | LOG(LS_ERROR) << "CreateOffer failed to UpdateTransportInfoForBundle."; |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) { |
| 1163 | LOG(LS_ERROR) << "CreateOffer failed to UpdateCryptoParamsForBundle."; |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return offer.release(); |
| 1169 | } |
| 1170 | |
| 1171 | SessionDescription* MediaSessionDescriptionFactory::CreateAnswer( |
| 1172 | const SessionDescription* offer, const MediaSessionOptions& options, |
| 1173 | const SessionDescription* current_description) const { |
| 1174 | // The answer contains the intersection of the codecs in the offer with the |
| 1175 | // codecs we support, ordered by our local preference. As indicated by |
| 1176 | // XEP-0167, we retain the same payload ids from the offer in the answer. |
| 1177 | scoped_ptr<SessionDescription> answer(new SessionDescription()); |
| 1178 | |
| 1179 | StreamParamsVec current_streams; |
| 1180 | GetCurrentStreamParams(current_description, ¤t_streams); |
| 1181 | |
| 1182 | bool bundle_enabled = |
| 1183 | offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled; |
| 1184 | |
| 1185 | // Handle m=audio. |
| 1186 | const ContentInfo* audio_content = GetFirstAudioContent(offer); |
| 1187 | if (audio_content) { |
| 1188 | scoped_ptr<TransportDescription> audio_transport( |
| 1189 | CreateTransportAnswer(audio_content->name, offer, |
| 1190 | options.transport_options, |
| 1191 | current_description)); |
| 1192 | if (!audio_transport) { |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | |
| 1196 | AudioCodecs audio_codecs = audio_codecs_; |
| 1197 | if (!options.vad_enabled) { |
| 1198 | StripCNCodecs(&audio_codecs); |
| 1199 | } |
| 1200 | |
| 1201 | scoped_ptr<AudioContentDescription> audio_answer( |
| 1202 | new AudioContentDescription()); |
| 1203 | // Do not require or create SDES cryptos if DTLS is used. |
| 1204 | cricket::SecurePolicy sdes_policy = |
| 1205 | audio_transport->secure() ? cricket::SEC_DISABLED : secure(); |
| 1206 | if (!CreateMediaContentAnswer( |
| 1207 | static_cast<const AudioContentDescription*>( |
| 1208 | audio_content->description), |
| 1209 | options, |
| 1210 | audio_codecs, |
| 1211 | sdes_policy, |
| 1212 | GetCryptos(GetFirstAudioContentDescription(current_description)), |
| 1213 | audio_rtp_extensions_, |
| 1214 | ¤t_streams, |
| 1215 | add_legacy_, |
| 1216 | bundle_enabled, |
| 1217 | audio_answer.get())) { |
| 1218 | return NULL; // Fails the session setup. |
| 1219 | } |
| 1220 | |
| 1221 | bool rejected = !options.has_audio || audio_content->rejected || |
| 1222 | !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO, |
| 1223 | audio_answer->protocol()); |
| 1224 | if (!rejected) { |
| 1225 | AddTransportAnswer(audio_content->name, *(audio_transport.get()), |
| 1226 | answer.get()); |
| 1227 | } else { |
| 1228 | // RFC 3264 |
| 1229 | // The answer MUST contain the same number of m-lines as the offer. |
| 1230 | LOG(LS_INFO) << "Audio is not supported in the answer."; |
| 1231 | } |
| 1232 | |
| 1233 | answer->AddContent(audio_content->name, audio_content->type, rejected, |
| 1234 | audio_answer.release()); |
| 1235 | } else { |
| 1236 | LOG(LS_INFO) << "Audio is not available in the offer."; |
| 1237 | } |
| 1238 | |
| 1239 | // Handle m=video. |
| 1240 | const ContentInfo* video_content = GetFirstVideoContent(offer); |
| 1241 | if (video_content) { |
| 1242 | scoped_ptr<TransportDescription> video_transport( |
| 1243 | CreateTransportAnswer(video_content->name, offer, |
| 1244 | options.transport_options, |
| 1245 | current_description)); |
| 1246 | if (!video_transport) { |
| 1247 | return NULL; |
| 1248 | } |
| 1249 | |
| 1250 | scoped_ptr<VideoContentDescription> video_answer( |
| 1251 | new VideoContentDescription()); |
| 1252 | // Do not require or create SDES cryptos if DTLS is used. |
| 1253 | cricket::SecurePolicy sdes_policy = |
| 1254 | video_transport->secure() ? cricket::SEC_DISABLED : secure(); |
| 1255 | if (!CreateMediaContentAnswer( |
| 1256 | static_cast<const VideoContentDescription*>( |
| 1257 | video_content->description), |
| 1258 | options, |
| 1259 | video_codecs_, |
| 1260 | sdes_policy, |
| 1261 | GetCryptos(GetFirstVideoContentDescription(current_description)), |
| 1262 | video_rtp_extensions_, |
| 1263 | ¤t_streams, |
| 1264 | add_legacy_, |
| 1265 | bundle_enabled, |
| 1266 | video_answer.get())) { |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | bool rejected = !options.has_video || video_content->rejected || |
| 1270 | !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO, video_answer->protocol()); |
| 1271 | if (!rejected) { |
| 1272 | if (!AddTransportAnswer(video_content->name, *(video_transport.get()), |
| 1273 | answer.get())) { |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | video_answer->set_bandwidth(options.video_bandwidth); |
| 1277 | } else { |
| 1278 | // RFC 3264 |
| 1279 | // The answer MUST contain the same number of m-lines as the offer. |
| 1280 | LOG(LS_INFO) << "Video is not supported in the answer."; |
| 1281 | } |
| 1282 | answer->AddContent(video_content->name, video_content->type, rejected, |
| 1283 | video_answer.release()); |
| 1284 | } else { |
| 1285 | LOG(LS_INFO) << "Video is not available in the offer."; |
| 1286 | } |
| 1287 | |
| 1288 | // Handle m=data. |
| 1289 | const ContentInfo* data_content = GetFirstDataContent(offer); |
| 1290 | if (data_content) { |
| 1291 | scoped_ptr<TransportDescription> data_transport( |
| 1292 | CreateTransportAnswer(data_content->name, offer, |
| 1293 | options.transport_options, |
| 1294 | current_description)); |
| 1295 | if (!data_transport) { |
| 1296 | return NULL; |
| 1297 | } |
| 1298 | scoped_ptr<DataContentDescription> data_answer( |
| 1299 | new DataContentDescription()); |
| 1300 | // Do not require or create SDES cryptos if DTLS is used. |
| 1301 | cricket::SecurePolicy sdes_policy = |
| 1302 | data_transport->secure() ? cricket::SEC_DISABLED : secure(); |
| 1303 | if (!CreateMediaContentAnswer( |
| 1304 | static_cast<const DataContentDescription*>( |
| 1305 | data_content->description), |
| 1306 | options, |
| 1307 | data_codecs_, |
| 1308 | sdes_policy, |
| 1309 | GetCryptos(GetFirstDataContentDescription(current_description)), |
| 1310 | RtpHeaderExtensions(), |
| 1311 | ¤t_streams, |
| 1312 | add_legacy_, |
| 1313 | bundle_enabled, |
| 1314 | data_answer.get())) { |
| 1315 | return NULL; // Fails the session setup. |
| 1316 | } |
| 1317 | |
| 1318 | bool rejected = !options.has_data() || data_content->rejected || |
| 1319 | !IsMediaProtocolSupported(MEDIA_TYPE_DATA, data_answer->protocol()); |
| 1320 | if (!rejected) { |
| 1321 | data_answer->set_bandwidth(options.data_bandwidth); |
| 1322 | if (!AddTransportAnswer(data_content->name, *(data_transport.get()), |
| 1323 | answer.get())) { |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | } else { |
| 1327 | // RFC 3264 |
| 1328 | // The answer MUST contain the same number of m-lines as the offer. |
| 1329 | LOG(LS_INFO) << "Data is not supported in the answer."; |
| 1330 | } |
| 1331 | answer->AddContent(data_content->name, data_content->type, rejected, |
| 1332 | data_answer.release()); |
| 1333 | } else { |
| 1334 | LOG(LS_INFO) << "Data is not available in the offer."; |
| 1335 | } |
| 1336 | |
| 1337 | // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE |
| 1338 | // group in the answer with the appropriate content names. |
| 1339 | if (offer->HasGroup(GROUP_TYPE_BUNDLE) && options.bundle_enabled) { |
| 1340 | const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE); |
| 1341 | ContentGroup answer_bundle(GROUP_TYPE_BUNDLE); |
| 1342 | for (ContentInfos::const_iterator content = answer->contents().begin(); |
| 1343 | content != answer->contents().end(); ++content) { |
| 1344 | if (!content->rejected && offer_bundle->HasContentName(content->name)) { |
| 1345 | answer_bundle.AddContentName(content->name); |
| 1346 | } |
| 1347 | } |
| 1348 | if (answer_bundle.FirstContentName()) { |
| 1349 | answer->AddGroup(answer_bundle); |
| 1350 | |
| 1351 | // Share the same ICE credentials and crypto params across all contents, |
| 1352 | // as BUNDLE requires. |
| 1353 | if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) { |
| 1354 | LOG(LS_ERROR) << "CreateAnswer failed to UpdateTransportInfoForBundle."; |
| 1355 | return NULL; |
| 1356 | } |
| 1357 | |
| 1358 | if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) { |
| 1359 | LOG(LS_ERROR) << "CreateAnswer failed to UpdateCryptoParamsForBundle."; |
| 1360 | return NULL; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | return answer.release(); |
| 1366 | } |
| 1367 | |
| 1368 | // Gets the TransportInfo of the given |content_name| from the |
| 1369 | // |current_description|. If doesn't exist, returns a new one. |
| 1370 | static const TransportDescription* GetTransportDescription( |
| 1371 | const std::string& content_name, |
| 1372 | const SessionDescription* current_description) { |
| 1373 | const TransportDescription* desc = NULL; |
| 1374 | if (current_description) { |
| 1375 | const TransportInfo* info = |
| 1376 | current_description->GetTransportInfoByName(content_name); |
| 1377 | if (info) { |
| 1378 | desc = &info->description; |
| 1379 | } |
| 1380 | } |
| 1381 | return desc; |
| 1382 | } |
| 1383 | |
| 1384 | void MediaSessionDescriptionFactory::GetCodecsToOffer( |
| 1385 | const SessionDescription* current_description, |
| 1386 | AudioCodecs* audio_codecs, |
| 1387 | VideoCodecs* video_codecs, |
| 1388 | DataCodecs* data_codecs) const { |
| 1389 | UsedPayloadTypes used_pltypes; |
| 1390 | audio_codecs->clear(); |
| 1391 | video_codecs->clear(); |
| 1392 | data_codecs->clear(); |
| 1393 | |
| 1394 | |
| 1395 | // First - get all codecs from the current description if the media type |
| 1396 | // is used. |
| 1397 | // Add them to |used_pltypes| so the payloadtype is not reused if a new media |
| 1398 | // type is added. |
| 1399 | if (current_description) { |
| 1400 | const AudioContentDescription* audio = |
| 1401 | GetFirstAudioContentDescription(current_description); |
| 1402 | if (audio) { |
| 1403 | *audio_codecs = audio->codecs(); |
| 1404 | used_pltypes.FindAndSetIdUsed<AudioCodec>(audio_codecs); |
| 1405 | } |
| 1406 | const VideoContentDescription* video = |
| 1407 | GetFirstVideoContentDescription(current_description); |
| 1408 | if (video) { |
| 1409 | *video_codecs = video->codecs(); |
| 1410 | used_pltypes.FindAndSetIdUsed<VideoCodec>(video_codecs); |
| 1411 | } |
| 1412 | const DataContentDescription* data = |
| 1413 | GetFirstDataContentDescription(current_description); |
| 1414 | if (data) { |
| 1415 | *data_codecs = data->codecs(); |
| 1416 | used_pltypes.FindAndSetIdUsed<DataCodec>(data_codecs); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | // Add our codecs that are not in |current_description|. |
| 1421 | FindCodecsToOffer<AudioCodec>(audio_codecs_, audio_codecs, &used_pltypes); |
| 1422 | FindCodecsToOffer<VideoCodec>(video_codecs_, video_codecs, &used_pltypes); |
| 1423 | FindCodecsToOffer<DataCodec>(data_codecs_, data_codecs, &used_pltypes); |
| 1424 | } |
| 1425 | |
| 1426 | void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer( |
| 1427 | const SessionDescription* current_description, |
| 1428 | RtpHeaderExtensions* audio_extensions, |
| 1429 | RtpHeaderExtensions* video_extensions) const { |
| 1430 | UsedRtpHeaderExtensionIds used_ids; |
| 1431 | audio_extensions->clear(); |
| 1432 | video_extensions->clear(); |
| 1433 | |
| 1434 | // First - get all extensions from the current description if the media type |
| 1435 | // is used. |
| 1436 | // Add them to |used_ids| so the local ids are not reused if a new media |
| 1437 | // type is added. |
| 1438 | if (current_description) { |
| 1439 | const AudioContentDescription* audio = |
| 1440 | GetFirstAudioContentDescription(current_description); |
| 1441 | if (audio) { |
| 1442 | *audio_extensions = audio->rtp_header_extensions(); |
| 1443 | used_ids.FindAndSetIdUsed(audio_extensions); |
| 1444 | } |
| 1445 | const VideoContentDescription* video = |
| 1446 | GetFirstVideoContentDescription(current_description); |
| 1447 | if (video) { |
| 1448 | *video_extensions = video->rtp_header_extensions(); |
| 1449 | used_ids.FindAndSetIdUsed(video_extensions); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | // Add our default RTP header extensions that are not in |
| 1454 | // |current_description|. |
| 1455 | FindAndSetRtpHdrExtUsed(audio_rtp_header_extensions(), audio_extensions, |
| 1456 | &used_ids); |
| 1457 | FindAndSetRtpHdrExtUsed(video_rtp_header_extensions(), video_extensions, |
| 1458 | &used_ids); |
| 1459 | } |
| 1460 | |
| 1461 | bool MediaSessionDescriptionFactory::AddTransportOffer( |
| 1462 | const std::string& content_name, |
| 1463 | const TransportOptions& transport_options, |
| 1464 | const SessionDescription* current_desc, |
| 1465 | SessionDescription* offer_desc) const { |
| 1466 | if (!transport_desc_factory_) |
| 1467 | return false; |
| 1468 | const TransportDescription* current_tdesc = |
| 1469 | GetTransportDescription(content_name, current_desc); |
| 1470 | talk_base::scoped_ptr<TransportDescription> new_tdesc( |
| 1471 | transport_desc_factory_->CreateOffer(transport_options, current_tdesc)); |
| 1472 | bool ret = (new_tdesc.get() != NULL && |
| 1473 | offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc))); |
| 1474 | if (!ret) { |
| 1475 | LOG(LS_ERROR) |
| 1476 | << "Failed to AddTransportOffer, content name=" << content_name; |
| 1477 | } |
| 1478 | return ret; |
| 1479 | } |
| 1480 | |
| 1481 | TransportDescription* MediaSessionDescriptionFactory::CreateTransportAnswer( |
| 1482 | const std::string& content_name, |
| 1483 | const SessionDescription* offer_desc, |
| 1484 | const TransportOptions& transport_options, |
| 1485 | const SessionDescription* current_desc) const { |
| 1486 | if (!transport_desc_factory_) |
| 1487 | return NULL; |
| 1488 | const TransportDescription* offer_tdesc = |
| 1489 | GetTransportDescription(content_name, offer_desc); |
| 1490 | const TransportDescription* current_tdesc = |
| 1491 | GetTransportDescription(content_name, current_desc); |
| 1492 | return |
| 1493 | transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options, |
| 1494 | current_tdesc); |
| 1495 | } |
| 1496 | |
| 1497 | bool MediaSessionDescriptionFactory::AddTransportAnswer( |
| 1498 | const std::string& content_name, |
| 1499 | const TransportDescription& transport_desc, |
| 1500 | SessionDescription* answer_desc) const { |
| 1501 | if (!answer_desc->AddTransportInfo(TransportInfo(content_name, |
| 1502 | transport_desc))) { |
| 1503 | LOG(LS_ERROR) |
| 1504 | << "Failed to AddTransportAnswer, content name=" << content_name; |
| 1505 | return false; |
| 1506 | } |
| 1507 | return true; |
| 1508 | } |
| 1509 | |
| 1510 | bool IsMediaContent(const ContentInfo* content) { |
| 1511 | return (content && |
| 1512 | (content->type == NS_JINGLE_RTP || |
| 1513 | content->type == NS_JINGLE_DRAFT_SCTP)); |
| 1514 | } |
| 1515 | |
| 1516 | bool IsAudioContent(const ContentInfo* content) { |
| 1517 | return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO); |
| 1518 | } |
| 1519 | |
| 1520 | bool IsVideoContent(const ContentInfo* content) { |
| 1521 | return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO); |
| 1522 | } |
| 1523 | |
| 1524 | bool IsDataContent(const ContentInfo* content) { |
| 1525 | return IsMediaContentOfType(content, MEDIA_TYPE_DATA); |
| 1526 | } |
| 1527 | |
| 1528 | static const ContentInfo* GetFirstMediaContent(const ContentInfos& contents, |
| 1529 | MediaType media_type) { |
| 1530 | for (ContentInfos::const_iterator content = contents.begin(); |
| 1531 | content != contents.end(); content++) { |
| 1532 | if (IsMediaContentOfType(&*content, media_type)) { |
| 1533 | return &*content; |
| 1534 | } |
| 1535 | } |
| 1536 | return NULL; |
| 1537 | } |
| 1538 | |
| 1539 | const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) { |
| 1540 | return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO); |
| 1541 | } |
| 1542 | |
| 1543 | const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) { |
| 1544 | return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO); |
| 1545 | } |
| 1546 | |
| 1547 | const ContentInfo* GetFirstDataContent(const ContentInfos& contents) { |
| 1548 | return GetFirstMediaContent(contents, MEDIA_TYPE_DATA); |
| 1549 | } |
| 1550 | |
| 1551 | static const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc, |
| 1552 | MediaType media_type) { |
| 1553 | if (sdesc == NULL) |
| 1554 | return NULL; |
| 1555 | |
| 1556 | return GetFirstMediaContent(sdesc->contents(), media_type); |
| 1557 | } |
| 1558 | |
| 1559 | const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) { |
| 1560 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO); |
| 1561 | } |
| 1562 | |
| 1563 | const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) { |
| 1564 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO); |
| 1565 | } |
| 1566 | |
| 1567 | const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) { |
| 1568 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA); |
| 1569 | } |
| 1570 | |
| 1571 | const MediaContentDescription* GetFirstMediaContentDescription( |
| 1572 | const SessionDescription* sdesc, MediaType media_type) { |
| 1573 | const ContentInfo* content = GetFirstMediaContent(sdesc, media_type); |
| 1574 | const ContentDescription* description = content ? content->description : NULL; |
| 1575 | return static_cast<const MediaContentDescription*>(description); |
| 1576 | } |
| 1577 | |
| 1578 | const AudioContentDescription* GetFirstAudioContentDescription( |
| 1579 | const SessionDescription* sdesc) { |
| 1580 | return static_cast<const AudioContentDescription*>( |
| 1581 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO)); |
| 1582 | } |
| 1583 | |
| 1584 | const VideoContentDescription* GetFirstVideoContentDescription( |
| 1585 | const SessionDescription* sdesc) { |
| 1586 | return static_cast<const VideoContentDescription*>( |
| 1587 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO)); |
| 1588 | } |
| 1589 | |
| 1590 | const DataContentDescription* GetFirstDataContentDescription( |
| 1591 | const SessionDescription* sdesc) { |
| 1592 | return static_cast<const DataContentDescription*>( |
| 1593 | GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA)); |
| 1594 | } |
| 1595 | |
| 1596 | bool GetMediaChannelNameFromComponent( |
| 1597 | int component, MediaType media_type, std::string* channel_name) { |
| 1598 | if (media_type == MEDIA_TYPE_AUDIO) { |
| 1599 | if (component == ICE_CANDIDATE_COMPONENT_RTP) { |
| 1600 | *channel_name = GICE_CHANNEL_NAME_RTP; |
| 1601 | return true; |
| 1602 | } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) { |
| 1603 | *channel_name = GICE_CHANNEL_NAME_RTCP; |
| 1604 | return true; |
| 1605 | } |
| 1606 | } else if (media_type == MEDIA_TYPE_VIDEO) { |
| 1607 | if (component == ICE_CANDIDATE_COMPONENT_RTP) { |
| 1608 | *channel_name = GICE_CHANNEL_NAME_VIDEO_RTP; |
| 1609 | return true; |
| 1610 | } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) { |
| 1611 | *channel_name = GICE_CHANNEL_NAME_VIDEO_RTCP; |
| 1612 | return true; |
| 1613 | } |
| 1614 | } else if (media_type == MEDIA_TYPE_DATA) { |
| 1615 | if (component == ICE_CANDIDATE_COMPONENT_RTP) { |
| 1616 | *channel_name = GICE_CHANNEL_NAME_DATA_RTP; |
| 1617 | return true; |
| 1618 | } else if (component == ICE_CANDIDATE_COMPONENT_RTCP) { |
| 1619 | *channel_name = GICE_CHANNEL_NAME_DATA_RTCP; |
| 1620 | return true; |
| 1621 | } |
| 1622 | } |
| 1623 | |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
| 1627 | bool GetMediaComponentFromChannelName( |
| 1628 | const std::string& channel_name, int* component) { |
| 1629 | if (channel_name == GICE_CHANNEL_NAME_RTP || |
| 1630 | channel_name == GICE_CHANNEL_NAME_VIDEO_RTP || |
| 1631 | channel_name == GICE_CHANNEL_NAME_DATA_RTP) { |
| 1632 | *component = ICE_CANDIDATE_COMPONENT_RTP; |
| 1633 | return true; |
| 1634 | } else if (channel_name == GICE_CHANNEL_NAME_RTCP || |
| 1635 | channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP || |
| 1636 | channel_name == GICE_CHANNEL_NAME_DATA_RTP) { |
| 1637 | *component = ICE_CANDIDATE_COMPONENT_RTCP; |
| 1638 | return true; |
| 1639 | } |
| 1640 | |
| 1641 | return false; |
| 1642 | } |
| 1643 | |
| 1644 | bool GetMediaTypeFromChannelName( |
| 1645 | const std::string& channel_name, MediaType* media_type) { |
| 1646 | if (channel_name == GICE_CHANNEL_NAME_RTP || |
| 1647 | channel_name == GICE_CHANNEL_NAME_RTCP) { |
| 1648 | *media_type = MEDIA_TYPE_AUDIO; |
| 1649 | return true; |
| 1650 | } else if (channel_name == GICE_CHANNEL_NAME_VIDEO_RTP || |
| 1651 | channel_name == GICE_CHANNEL_NAME_VIDEO_RTCP) { |
| 1652 | *media_type = MEDIA_TYPE_VIDEO; |
| 1653 | return true; |
| 1654 | } else if (channel_name == GICE_CHANNEL_NAME_DATA_RTP || |
| 1655 | channel_name == GICE_CHANNEL_NAME_DATA_RTCP) { |
| 1656 | *media_type = MEDIA_TYPE_DATA; |
| 1657 | return true; |
| 1658 | } |
| 1659 | |
| 1660 | return false; |
| 1661 | } |
| 1662 | |
| 1663 | } // namespace cricket |