You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2809 lines
109 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "pc/media_session.h"
  11. #include <algorithm>
  12. #include <functional>
  13. #include <map>
  14. #include <memory>
  15. #include <set>
  16. #include <unordered_map>
  17. #include <utility>
  18. #include "absl/algorithm/container.h"
  19. #include "absl/strings/match.h"
  20. #include "absl/types/optional.h"
  21. #include "api/crypto_params.h"
  22. #include "media/base/h264_profile_level_id.h"
  23. #include "media/base/media_constants.h"
  24. #include "media/sctp/sctp_transport_internal.h"
  25. #include "p2p/base/p2p_constants.h"
  26. #include "pc/channel_manager.h"
  27. #include "pc/media_protocol_names.h"
  28. #include "pc/rtp_media_utils.h"
  29. #include "pc/srtp_filter.h"
  30. #include "pc/used_ids.h"
  31. #include "rtc_base/checks.h"
  32. #include "rtc_base/helpers.h"
  33. #include "rtc_base/logging.h"
  34. #include "rtc_base/third_party/base64/base64.h"
  35. #include "rtc_base/unique_id_generator.h"
  36. namespace {
  37. using rtc::UniqueRandomIdGenerator;
  38. using webrtc::RtpTransceiverDirection;
  39. const char kInline[] = "inline:";
  40. void GetSupportedSdesCryptoSuiteNames(
  41. void (*func)(const webrtc::CryptoOptions&, std::vector<int>*),
  42. const webrtc::CryptoOptions& crypto_options,
  43. std::vector<std::string>* names) {
  44. std::vector<int> crypto_suites;
  45. func(crypto_options, &crypto_suites);
  46. for (const auto crypto : crypto_suites) {
  47. names->push_back(rtc::SrtpCryptoSuiteToName(crypto));
  48. }
  49. }
  50. } // namespace
  51. namespace cricket {
  52. // RTP Profile names
  53. // http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xml
  54. // RFC4585
  55. const char kMediaProtocolAvpf[] = "RTP/AVPF";
  56. // RFC5124
  57. const char kMediaProtocolDtlsSavpf[] = "UDP/TLS/RTP/SAVPF";
  58. // We always generate offers with "UDP/TLS/RTP/SAVPF" when using DTLS-SRTP,
  59. // but we tolerate "RTP/SAVPF" in offers we receive, for compatibility.
  60. const char kMediaProtocolSavpf[] = "RTP/SAVPF";
  61. // Note that the below functions support some protocol strings purely for
  62. // legacy compatibility, as required by JSEP in Section 5.1.2, Profile Names
  63. // and Interoperability.
  64. static bool IsDtlsRtp(const std::string& protocol) {
  65. // Most-likely values first.
  66. return protocol == "UDP/TLS/RTP/SAVPF" || protocol == "TCP/TLS/RTP/SAVPF" ||
  67. protocol == "UDP/TLS/RTP/SAVP" || protocol == "TCP/TLS/RTP/SAVP";
  68. }
  69. static bool IsPlainRtp(const std::string& protocol) {
  70. // Most-likely values first.
  71. return protocol == "RTP/SAVPF" || protocol == "RTP/AVPF" ||
  72. protocol == "RTP/SAVP" || protocol == "RTP/AVP";
  73. }
  74. static RtpTransceiverDirection NegotiateRtpTransceiverDirection(
  75. RtpTransceiverDirection offer,
  76. RtpTransceiverDirection wants) {
  77. bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer);
  78. bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer);
  79. bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants);
  80. bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants);
  81. return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send,
  82. offer_send && wants_recv);
  83. }
  84. static bool IsMediaContentOfType(const ContentInfo* content,
  85. MediaType media_type) {
  86. if (!content || !content->media_description()) {
  87. return false;
  88. }
  89. return content->media_description()->type() == media_type;
  90. }
  91. static bool CreateCryptoParams(int tag,
  92. const std::string& cipher,
  93. CryptoParams* crypto_out) {
  94. int key_len;
  95. int salt_len;
  96. if (!rtc::GetSrtpKeyAndSaltLengths(rtc::SrtpCryptoSuiteFromName(cipher),
  97. &key_len, &salt_len)) {
  98. return false;
  99. }
  100. int master_key_len = key_len + salt_len;
  101. std::string master_key;
  102. if (!rtc::CreateRandomData(master_key_len, &master_key)) {
  103. return false;
  104. }
  105. RTC_CHECK_EQ(master_key_len, master_key.size());
  106. std::string key = rtc::Base64::Encode(master_key);
  107. crypto_out->tag = tag;
  108. crypto_out->cipher_suite = cipher;
  109. crypto_out->key_params = kInline;
  110. crypto_out->key_params += key;
  111. return true;
  112. }
  113. static bool AddCryptoParams(const std::string& cipher_suite,
  114. CryptoParamsVec* cryptos_out) {
  115. int size = static_cast<int>(cryptos_out->size());
  116. cryptos_out->resize(size + 1);
  117. return CreateCryptoParams(size, cipher_suite, &cryptos_out->at(size));
  118. }
  119. void AddMediaCryptos(const CryptoParamsVec& cryptos,
  120. MediaContentDescription* media) {
  121. for (const CryptoParams& crypto : cryptos) {
  122. media->AddCrypto(crypto);
  123. }
  124. }
  125. bool CreateMediaCryptos(const std::vector<std::string>& crypto_suites,
  126. MediaContentDescription* media) {
  127. CryptoParamsVec cryptos;
  128. for (const std::string& crypto_suite : crypto_suites) {
  129. if (!AddCryptoParams(crypto_suite, &cryptos)) {
  130. return false;
  131. }
  132. }
  133. AddMediaCryptos(cryptos, media);
  134. return true;
  135. }
  136. const CryptoParamsVec* GetCryptos(const ContentInfo* content) {
  137. if (!content || !content->media_description()) {
  138. return nullptr;
  139. }
  140. return &content->media_description()->cryptos();
  141. }
  142. bool FindMatchingCrypto(const CryptoParamsVec& cryptos,
  143. const CryptoParams& crypto,
  144. CryptoParams* crypto_out) {
  145. auto it = absl::c_find_if(
  146. cryptos, [&crypto](const CryptoParams& c) { return crypto.Matches(c); });
  147. if (it == cryptos.end()) {
  148. return false;
  149. }
  150. *crypto_out = *it;
  151. return true;
  152. }
  153. // For audio, HMAC 32 (if enabled) is prefered over HMAC 80 because of the
  154. // low overhead.
  155. void GetSupportedAudioSdesCryptoSuites(
  156. const webrtc::CryptoOptions& crypto_options,
  157. std::vector<int>* crypto_suites) {
  158. if (crypto_options.srtp.enable_gcm_crypto_suites) {
  159. crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
  160. crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
  161. }
  162. if (crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher) {
  163. crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_32);
  164. }
  165. crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
  166. }
  167. void GetSupportedAudioSdesCryptoSuiteNames(
  168. const webrtc::CryptoOptions& crypto_options,
  169. std::vector<std::string>* crypto_suite_names) {
  170. GetSupportedSdesCryptoSuiteNames(GetSupportedAudioSdesCryptoSuites,
  171. crypto_options, crypto_suite_names);
  172. }
  173. void GetSupportedVideoSdesCryptoSuites(
  174. const webrtc::CryptoOptions& crypto_options,
  175. std::vector<int>* crypto_suites) {
  176. if (crypto_options.srtp.enable_gcm_crypto_suites) {
  177. crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
  178. crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
  179. }
  180. crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
  181. }
  182. void GetSupportedVideoSdesCryptoSuiteNames(
  183. const webrtc::CryptoOptions& crypto_options,
  184. std::vector<std::string>* crypto_suite_names) {
  185. GetSupportedSdesCryptoSuiteNames(GetSupportedVideoSdesCryptoSuites,
  186. crypto_options, crypto_suite_names);
  187. }
  188. void GetSupportedDataSdesCryptoSuites(
  189. const webrtc::CryptoOptions& crypto_options,
  190. std::vector<int>* crypto_suites) {
  191. if (crypto_options.srtp.enable_gcm_crypto_suites) {
  192. crypto_suites->push_back(rtc::SRTP_AEAD_AES_256_GCM);
  193. crypto_suites->push_back(rtc::SRTP_AEAD_AES_128_GCM);
  194. }
  195. crypto_suites->push_back(rtc::SRTP_AES128_CM_SHA1_80);
  196. }
  197. void GetSupportedDataSdesCryptoSuiteNames(
  198. const webrtc::CryptoOptions& crypto_options,
  199. std::vector<std::string>* crypto_suite_names) {
  200. GetSupportedSdesCryptoSuiteNames(GetSupportedDataSdesCryptoSuites,
  201. crypto_options, crypto_suite_names);
  202. }
  203. // Support any GCM cipher (if enabled through options). For video support only
  204. // 80-bit SHA1 HMAC. For audio 32-bit HMAC is tolerated (if enabled) unless
  205. // bundle is enabled because it is low overhead.
  206. // Pick the crypto in the list that is supported.
  207. static bool SelectCrypto(const MediaContentDescription* offer,
  208. bool bundle,
  209. const webrtc::CryptoOptions& crypto_options,
  210. CryptoParams* crypto_out) {
  211. bool audio = offer->type() == MEDIA_TYPE_AUDIO;
  212. const CryptoParamsVec& cryptos = offer->cryptos();
  213. for (const CryptoParams& crypto : cryptos) {
  214. if ((crypto_options.srtp.enable_gcm_crypto_suites &&
  215. rtc::IsGcmCryptoSuiteName(crypto.cipher_suite)) ||
  216. rtc::CS_AES_CM_128_HMAC_SHA1_80 == crypto.cipher_suite ||
  217. (rtc::CS_AES_CM_128_HMAC_SHA1_32 == crypto.cipher_suite && audio &&
  218. !bundle && crypto_options.srtp.enable_aes128_sha1_32_crypto_cipher)) {
  219. return CreateCryptoParams(crypto.tag, crypto.cipher_suite, crypto_out);
  220. }
  221. }
  222. return false;
  223. }
  224. // Finds all StreamParams of all media types and attach them to stream_params.
  225. static StreamParamsVec GetCurrentStreamParams(
  226. const std::vector<const ContentInfo*>& active_local_contents) {
  227. StreamParamsVec stream_params;
  228. for (const ContentInfo* content : active_local_contents) {
  229. for (const StreamParams& params : content->media_description()->streams()) {
  230. stream_params.push_back(params);
  231. }
  232. }
  233. return stream_params;
  234. }
  235. // Filters the data codecs for the data channel type.
  236. void FilterDataCodecs(std::vector<DataCodec>* codecs, bool sctp) {
  237. // Filter RTP codec for SCTP and vice versa.
  238. const char* codec_name =
  239. sctp ? kGoogleRtpDataCodecName : kGoogleSctpDataCodecName;
  240. codecs->erase(std::remove_if(codecs->begin(), codecs->end(),
  241. [&codec_name](const DataCodec& codec) {
  242. return absl::EqualsIgnoreCase(codec.name,
  243. codec_name);
  244. }),
  245. codecs->end());
  246. }
  247. static StreamParams CreateStreamParamsForNewSenderWithSsrcs(
  248. const SenderOptions& sender,
  249. const std::string& rtcp_cname,
  250. bool include_rtx_streams,
  251. bool include_flexfec_stream,
  252. UniqueRandomIdGenerator* ssrc_generator) {
  253. StreamParams result;
  254. result.id = sender.track_id;
  255. // TODO(brandtr): Update when we support multistream protection.
  256. if (include_flexfec_stream && sender.num_sim_layers > 1) {
  257. include_flexfec_stream = false;
  258. RTC_LOG(LS_WARNING)
  259. << "Our FlexFEC implementation only supports protecting "
  260. "a single media streams. This session has multiple "
  261. "media streams however, so no FlexFEC SSRC will be generated.";
  262. }
  263. result.GenerateSsrcs(sender.num_sim_layers, include_rtx_streams,
  264. include_flexfec_stream, ssrc_generator);
  265. result.cname = rtcp_cname;
  266. result.set_stream_ids(sender.stream_ids);
  267. return result;
  268. }
  269. static bool ValidateSimulcastLayers(
  270. const std::vector<RidDescription>& rids,
  271. const SimulcastLayerList& simulcast_layers) {
  272. return absl::c_all_of(
  273. simulcast_layers.GetAllLayers(), [&rids](const SimulcastLayer& layer) {
  274. return absl::c_any_of(rids, [&layer](const RidDescription& rid) {
  275. return rid.rid == layer.rid;
  276. });
  277. });
  278. }
  279. static StreamParams CreateStreamParamsForNewSenderWithRids(
  280. const SenderOptions& sender,
  281. const std::string& rtcp_cname) {
  282. RTC_DCHECK(!sender.rids.empty());
  283. RTC_DCHECK_EQ(sender.num_sim_layers, 0)
  284. << "RIDs are the compliant way to indicate simulcast.";
  285. RTC_DCHECK(ValidateSimulcastLayers(sender.rids, sender.simulcast_layers));
  286. StreamParams result;
  287. result.id = sender.track_id;
  288. result.cname = rtcp_cname;
  289. result.set_stream_ids(sender.stream_ids);
  290. // More than one rid should be signaled.
  291. if (sender.rids.size() > 1) {
  292. result.set_rids(sender.rids);
  293. }
  294. return result;
  295. }
  296. // Adds SimulcastDescription if indicated by the media description options.
  297. // MediaContentDescription should already be set up with the send rids.
  298. static void AddSimulcastToMediaDescription(
  299. const MediaDescriptionOptions& media_description_options,
  300. MediaContentDescription* description) {
  301. RTC_DCHECK(description);
  302. // Check if we are using RIDs in this scenario.
  303. if (absl::c_all_of(description->streams(), [](const StreamParams& params) {
  304. return !params.has_rids();
  305. })) {
  306. return;
  307. }
  308. RTC_DCHECK_EQ(1, description->streams().size())
  309. << "RIDs are only supported in Unified Plan semantics.";
  310. RTC_DCHECK_EQ(1, media_description_options.sender_options.size());
  311. RTC_DCHECK(description->type() == MediaType::MEDIA_TYPE_AUDIO ||
  312. description->type() == MediaType::MEDIA_TYPE_VIDEO);
  313. // One RID or less indicates that simulcast is not needed.
  314. if (description->streams()[0].rids().size() <= 1) {
  315. return;
  316. }
  317. // Only negotiate the send layers.
  318. SimulcastDescription simulcast;
  319. simulcast.send_layers() =
  320. media_description_options.sender_options[0].simulcast_layers;
  321. description->set_simulcast_description(simulcast);
  322. }
  323. // Adds a StreamParams for each SenderOptions in |sender_options| to
  324. // content_description.
  325. // |current_params| - All currently known StreamParams of any media type.
  326. template <class C>
  327. static bool AddStreamParams(
  328. const std::vector<SenderOptions>& sender_options,
  329. const std::string& rtcp_cname,
  330. UniqueRandomIdGenerator* ssrc_generator,
  331. StreamParamsVec* current_streams,
  332. MediaContentDescriptionImpl<C>* content_description) {
  333. // SCTP streams are not negotiated using SDP/ContentDescriptions.
  334. if (IsSctpProtocol(content_description->protocol())) {
  335. return true;
  336. }
  337. const bool include_rtx_streams =
  338. ContainsRtxCodec(content_description->codecs());
  339. const bool include_flexfec_stream =
  340. ContainsFlexfecCodec(content_description->codecs());
  341. for (const SenderOptions& sender : sender_options) {
  342. // groupid is empty for StreamParams generated using
  343. // MediaSessionDescriptionFactory.
  344. StreamParams* param =
  345. GetStreamByIds(*current_streams, "" /*group_id*/, sender.track_id);
  346. if (!param) {
  347. // This is a new sender.
  348. StreamParams stream_param =
  349. sender.rids.empty()
  350. ?
  351. // Signal SSRCs and legacy simulcast (if requested).
  352. CreateStreamParamsForNewSenderWithSsrcs(
  353. sender, rtcp_cname, include_rtx_streams,
  354. include_flexfec_stream, ssrc_generator)
  355. :
  356. // Signal RIDs and spec-compliant simulcast (if requested).
  357. CreateStreamParamsForNewSenderWithRids(sender, rtcp_cname);
  358. content_description->AddStream(stream_param);
  359. // Store the new StreamParams in current_streams.
  360. // This is necessary so that we can use the CNAME for other media types.
  361. current_streams->push_back(stream_param);
  362. } else {
  363. // Use existing generated SSRCs/groups, but update the sync_label if
  364. // necessary. This may be needed if a MediaStreamTrack was moved from one
  365. // MediaStream to another.
  366. param->set_stream_ids(sender.stream_ids);
  367. content_description->AddStream(*param);
  368. }
  369. }
  370. return true;
  371. }
  372. // Updates the transport infos of the |sdesc| according to the given
  373. // |bundle_group|. The transport infos of the content names within the
  374. // |bundle_group| should be updated to use the ufrag, pwd and DTLS role of the
  375. // first content within the |bundle_group|.
  376. static bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group,
  377. SessionDescription* sdesc) {
  378. // The bundle should not be empty.
  379. if (!sdesc || !bundle_group.FirstContentName()) {
  380. return false;
  381. }
  382. // We should definitely have a transport for the first content.
  383. const std::string& selected_content_name = *bundle_group.FirstContentName();
  384. const TransportInfo* selected_transport_info =
  385. sdesc->GetTransportInfoByName(selected_content_name);
  386. if (!selected_transport_info) {
  387. return false;
  388. }
  389. // Set the other contents to use the same ICE credentials.
  390. const std::string& selected_ufrag =
  391. selected_transport_info->description.ice_ufrag;
  392. const std::string& selected_pwd =
  393. selected_transport_info->description.ice_pwd;
  394. ConnectionRole selected_connection_role =
  395. selected_transport_info->description.connection_role;
  396. const absl::optional<OpaqueTransportParameters>& selected_opaque_parameters =
  397. selected_transport_info->description.opaque_parameters;
  398. for (TransportInfo& transport_info : sdesc->transport_infos()) {
  399. if (bundle_group.HasContentName(transport_info.content_name) &&
  400. transport_info.content_name != selected_content_name) {
  401. transport_info.description.ice_ufrag = selected_ufrag;
  402. transport_info.description.ice_pwd = selected_pwd;
  403. transport_info.description.connection_role = selected_connection_role;
  404. transport_info.description.opaque_parameters = selected_opaque_parameters;
  405. }
  406. }
  407. return true;
  408. }
  409. // Gets the CryptoParamsVec of the given |content_name| from |sdesc|, and
  410. // sets it to |cryptos|.
  411. static bool GetCryptosByName(const SessionDescription* sdesc,
  412. const std::string& content_name,
  413. CryptoParamsVec* cryptos) {
  414. if (!sdesc || !cryptos) {
  415. return false;
  416. }
  417. const ContentInfo* content = sdesc->GetContentByName(content_name);
  418. if (!content || !content->media_description()) {
  419. return false;
  420. }
  421. *cryptos = content->media_description()->cryptos();
  422. return true;
  423. }
  424. // Prunes the |target_cryptos| by removing the crypto params (cipher_suite)
  425. // which are not available in |filter|.
  426. static void PruneCryptos(const CryptoParamsVec& filter,
  427. CryptoParamsVec* target_cryptos) {
  428. if (!target_cryptos) {
  429. return;
  430. }
  431. target_cryptos->erase(
  432. std::remove_if(target_cryptos->begin(), target_cryptos->end(),
  433. // Returns true if the |crypto|'s cipher_suite is not
  434. // found in |filter|.
  435. [&filter](const CryptoParams& crypto) {
  436. for (const CryptoParams& entry : filter) {
  437. if (entry.cipher_suite == crypto.cipher_suite)
  438. return false;
  439. }
  440. return true;
  441. }),
  442. target_cryptos->end());
  443. }
  444. static bool IsRtpContent(SessionDescription* sdesc,
  445. const std::string& content_name) {
  446. bool is_rtp = false;
  447. ContentInfo* content = sdesc->GetContentByName(content_name);
  448. if (content && content->media_description()) {
  449. is_rtp = IsRtpProtocol(content->media_description()->protocol());
  450. }
  451. return is_rtp;
  452. }
  453. // Updates the crypto parameters of the |sdesc| according to the given
  454. // |bundle_group|. The crypto parameters of all the contents within the
  455. // |bundle_group| should be updated to use the common subset of the
  456. // available cryptos.
  457. static bool UpdateCryptoParamsForBundle(const ContentGroup& bundle_group,
  458. SessionDescription* sdesc) {
  459. // The bundle should not be empty.
  460. if (!sdesc || !bundle_group.FirstContentName()) {
  461. return false;
  462. }
  463. bool common_cryptos_needed = false;
  464. // Get the common cryptos.
  465. const ContentNames& content_names = bundle_group.content_names();
  466. CryptoParamsVec common_cryptos;
  467. bool first = true;
  468. for (const std::string& content_name : content_names) {
  469. if (!IsRtpContent(sdesc, content_name)) {
  470. continue;
  471. }
  472. // The common cryptos are needed if any of the content does not have DTLS
  473. // enabled.
  474. if (!sdesc->GetTransportInfoByName(content_name)->description.secure()) {
  475. common_cryptos_needed = true;
  476. }
  477. if (first) {
  478. first = false;
  479. // Initial the common_cryptos with the first content in the bundle group.
  480. if (!GetCryptosByName(sdesc, content_name, &common_cryptos)) {
  481. return false;
  482. }
  483. if (common_cryptos.empty()) {
  484. // If there's no crypto params, we should just return.
  485. return true;
  486. }
  487. } else {
  488. CryptoParamsVec cryptos;
  489. if (!GetCryptosByName(sdesc, content_name, &cryptos)) {
  490. return false;
  491. }
  492. PruneCryptos(cryptos, &common_cryptos);
  493. }
  494. }
  495. if (common_cryptos.empty() && common_cryptos_needed) {
  496. return false;
  497. }
  498. // Update to use the common cryptos.
  499. for (const std::string& content_name : content_names) {
  500. if (!IsRtpContent(sdesc, content_name)) {
  501. continue;
  502. }
  503. ContentInfo* content = sdesc->GetContentByName(content_name);
  504. if (IsMediaContent(content)) {
  505. MediaContentDescription* media_desc = content->media_description();
  506. if (!media_desc) {
  507. return false;
  508. }
  509. media_desc->set_cryptos(common_cryptos);
  510. }
  511. }
  512. return true;
  513. }
  514. static std::vector<const ContentInfo*> GetActiveContents(
  515. const SessionDescription& description,
  516. const MediaSessionOptions& session_options) {
  517. std::vector<const ContentInfo*> active_contents;
  518. for (size_t i = 0; i < description.contents().size(); ++i) {
  519. RTC_DCHECK_LT(i, session_options.media_description_options.size());
  520. const ContentInfo& content = description.contents()[i];
  521. const MediaDescriptionOptions& media_options =
  522. session_options.media_description_options[i];
  523. if (!content.rejected && !media_options.stopped &&
  524. content.name == media_options.mid) {
  525. active_contents.push_back(&content);
  526. }
  527. }
  528. return active_contents;
  529. }
  530. template <class C>
  531. static bool ContainsRtxCodec(const std::vector<C>& codecs) {
  532. for (const auto& codec : codecs) {
  533. if (IsRtxCodec(codec)) {
  534. return true;
  535. }
  536. }
  537. return false;
  538. }
  539. template <class C>
  540. static bool IsRtxCodec(const C& codec) {
  541. return absl::EqualsIgnoreCase(codec.name, kRtxCodecName);
  542. }
  543. template <class C>
  544. static bool ContainsFlexfecCodec(const std::vector<C>& codecs) {
  545. for (const auto& codec : codecs) {
  546. if (IsFlexfecCodec(codec)) {
  547. return true;
  548. }
  549. }
  550. return false;
  551. }
  552. template <class C>
  553. static bool IsFlexfecCodec(const C& codec) {
  554. return absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName);
  555. }
  556. // Create a media content to be offered for the given |sender_options|,
  557. // according to the given options.rtcp_mux, session_options.is_muc, codecs,
  558. // secure_transport, crypto, and current_streams. If we don't currently have
  559. // crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is
  560. // created (according to crypto_suites). The created content is added to the
  561. // offer.
  562. static bool CreateContentOffer(
  563. const MediaDescriptionOptions& media_description_options,
  564. const MediaSessionOptions& session_options,
  565. const SecurePolicy& secure_policy,
  566. const CryptoParamsVec* current_cryptos,
  567. const std::vector<std::string>& crypto_suites,
  568. const RtpHeaderExtensions& rtp_extensions,
  569. UniqueRandomIdGenerator* ssrc_generator,
  570. StreamParamsVec* current_streams,
  571. MediaContentDescription* offer) {
  572. offer->set_rtcp_mux(session_options.rtcp_mux_enabled);
  573. if (offer->type() == cricket::MEDIA_TYPE_VIDEO) {
  574. offer->set_rtcp_reduced_size(true);
  575. }
  576. offer->set_rtp_header_extensions(rtp_extensions);
  577. AddSimulcastToMediaDescription(media_description_options, offer);
  578. if (secure_policy != SEC_DISABLED) {
  579. if (current_cryptos) {
  580. AddMediaCryptos(*current_cryptos, offer);
  581. }
  582. if (offer->cryptos().empty()) {
  583. if (!CreateMediaCryptos(crypto_suites, offer)) {
  584. return false;
  585. }
  586. }
  587. }
  588. offer->set_alt_protocol(media_description_options.alt_protocol);
  589. if (secure_policy == SEC_REQUIRED && offer->cryptos().empty()) {
  590. return false;
  591. }
  592. return true;
  593. }
  594. template <class C>
  595. static bool CreateMediaContentOffer(
  596. const MediaDescriptionOptions& media_description_options,
  597. const MediaSessionOptions& session_options,
  598. const std::vector<C>& codecs,
  599. const SecurePolicy& secure_policy,
  600. const CryptoParamsVec* current_cryptos,
  601. const std::vector<std::string>& crypto_suites,
  602. const RtpHeaderExtensions& rtp_extensions,
  603. UniqueRandomIdGenerator* ssrc_generator,
  604. StreamParamsVec* current_streams,
  605. MediaContentDescriptionImpl<C>* offer) {
  606. offer->AddCodecs(codecs);
  607. if (!AddStreamParams(media_description_options.sender_options,
  608. session_options.rtcp_cname, ssrc_generator,
  609. current_streams, offer)) {
  610. return false;
  611. }
  612. return CreateContentOffer(media_description_options, session_options,
  613. secure_policy, current_cryptos, crypto_suites,
  614. rtp_extensions, ssrc_generator, current_streams,
  615. offer);
  616. }
  617. template <class C>
  618. static bool ReferencedCodecsMatch(const std::vector<C>& codecs1,
  619. const int codec1_id,
  620. const std::vector<C>& codecs2,
  621. const int codec2_id) {
  622. const C* codec1 = FindCodecById(codecs1, codec1_id);
  623. const C* codec2 = FindCodecById(codecs2, codec2_id);
  624. return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2);
  625. }
  626. template <class C>
  627. static void NegotiatePacketization(const C& local_codec,
  628. const C& remote_codec,
  629. C* negotiated_codec) {}
  630. template <>
  631. void NegotiatePacketization(const VideoCodec& local_codec,
  632. const VideoCodec& remote_codec,
  633. VideoCodec* negotiated_codec) {
  634. negotiated_codec->packetization =
  635. VideoCodec::IntersectPacketization(local_codec, remote_codec);
  636. }
  637. template <class C>
  638. static void NegotiateCodecs(const std::vector<C>& local_codecs,
  639. const std::vector<C>& offered_codecs,
  640. std::vector<C>* negotiated_codecs,
  641. bool keep_offer_order) {
  642. for (const C& ours : local_codecs) {
  643. C theirs;
  644. // Note that we intentionally only find one matching codec for each of our
  645. // local codecs, in case the remote offer contains duplicate codecs.
  646. if (FindMatchingCodec(local_codecs, offered_codecs, ours, &theirs)) {
  647. C negotiated = ours;
  648. NegotiatePacketization(ours, theirs, &negotiated);
  649. negotiated.IntersectFeedbackParams(theirs);
  650. if (IsRtxCodec(negotiated)) {
  651. const auto apt_it =
  652. theirs.params.find(kCodecParamAssociatedPayloadType);
  653. // FindMatchingCodec shouldn't return something with no apt value.
  654. RTC_DCHECK(apt_it != theirs.params.end());
  655. negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second);
  656. }
  657. if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) {
  658. webrtc::H264::GenerateProfileLevelIdForAnswer(
  659. ours.params, theirs.params, &negotiated.params);
  660. }
  661. negotiated.id = theirs.id;
  662. negotiated.name = theirs.name;
  663. negotiated_codecs->push_back(std::move(negotiated));
  664. }
  665. }
  666. if (keep_offer_order) {
  667. // RFC3264: Although the answerer MAY list the formats in their desired
  668. // order of preference, it is RECOMMENDED that unless there is a
  669. // specific reason, the answerer list formats in the same relative order
  670. // they were present in the offer.
  671. // This can be skipped when the transceiver has any codec preferences.
  672. std::unordered_map<int, int> payload_type_preferences;
  673. int preference = static_cast<int>(offered_codecs.size() + 1);
  674. for (const C& codec : offered_codecs) {
  675. payload_type_preferences[codec.id] = preference--;
  676. }
  677. absl::c_sort(*negotiated_codecs, [&payload_type_preferences](const C& a,
  678. const C& b) {
  679. return payload_type_preferences[a.id] > payload_type_preferences[b.id];
  680. });
  681. }
  682. }
  683. // Finds a codec in |codecs2| that matches |codec_to_match|, which is
  684. // a member of |codecs1|. If |codec_to_match| is an RTX codec, both
  685. // the codecs themselves and their associated codecs must match.
  686. template <class C>
  687. static bool FindMatchingCodec(const std::vector<C>& codecs1,
  688. const std::vector<C>& codecs2,
  689. const C& codec_to_match,
  690. C* found_codec) {
  691. // |codec_to_match| should be a member of |codecs1|, in order to look up RTX
  692. // codecs' associated codecs correctly. If not, that's a programming error.
  693. RTC_DCHECK(absl::c_any_of(codecs1, [&codec_to_match](const C& codec) {
  694. return &codec == &codec_to_match;
  695. }));
  696. for (const C& potential_match : codecs2) {
  697. if (potential_match.Matches(codec_to_match)) {
  698. if (IsRtxCodec(codec_to_match)) {
  699. int apt_value_1 = 0;
  700. int apt_value_2 = 0;
  701. if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType,
  702. &apt_value_1) ||
  703. !potential_match.GetParam(kCodecParamAssociatedPayloadType,
  704. &apt_value_2)) {
  705. RTC_LOG(LS_WARNING) << "RTX missing associated payload type.";
  706. continue;
  707. }
  708. if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2,
  709. apt_value_2)) {
  710. continue;
  711. }
  712. }
  713. if (found_codec) {
  714. *found_codec = potential_match;
  715. }
  716. return true;
  717. }
  718. }
  719. return false;
  720. }
  721. // Find the codec in |codec_list| that |rtx_codec| is associated with.
  722. template <class C>
  723. static const C* GetAssociatedCodec(const std::vector<C>& codec_list,
  724. const C& rtx_codec) {
  725. std::string associated_pt_str;
  726. if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
  727. &associated_pt_str)) {
  728. RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.name
  729. << " is missing an associated payload type.";
  730. return nullptr;
  731. }
  732. int associated_pt;
  733. if (!rtc::FromString(associated_pt_str, &associated_pt)) {
  734. RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str
  735. << " of RTX codec " << rtx_codec.name
  736. << " to an integer.";
  737. return nullptr;
  738. }
  739. // Find the associated reference codec for the reference RTX codec.
  740. const C* associated_codec = FindCodecById(codec_list, associated_pt);
  741. if (!associated_codec) {
  742. RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type "
  743. << associated_pt << " for RTX codec " << rtx_codec.name
  744. << ".";
  745. }
  746. return associated_codec;
  747. }
  748. // Adds all codecs from |reference_codecs| to |offered_codecs| that don't
  749. // already exist in |offered_codecs| and ensure the payload types don't
  750. // collide.
  751. template <class C>
  752. static void MergeCodecs(const std::vector<C>& reference_codecs,
  753. std::vector<C>* offered_codecs,
  754. UsedPayloadTypes* used_pltypes) {
  755. // Add all new codecs that are not RTX codecs.
  756. for (const C& reference_codec : reference_codecs) {
  757. if (!IsRtxCodec(reference_codec) &&
  758. !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
  759. reference_codec, nullptr)) {
  760. C codec = reference_codec;
  761. used_pltypes->FindAndSetIdUsed(&codec);
  762. offered_codecs->push_back(codec);
  763. }
  764. }
  765. // Add all new RTX codecs.
  766. for (const C& reference_codec : reference_codecs) {
  767. if (IsRtxCodec(reference_codec) &&
  768. !FindMatchingCodec<C>(reference_codecs, *offered_codecs,
  769. reference_codec, nullptr)) {
  770. C rtx_codec = reference_codec;
  771. const C* associated_codec =
  772. GetAssociatedCodec(reference_codecs, rtx_codec);
  773. if (!associated_codec) {
  774. continue;
  775. }
  776. // Find a codec in the offered list that matches the reference codec.
  777. // Its payload type may be different than the reference codec.
  778. C matching_codec;
  779. if (!FindMatchingCodec<C>(reference_codecs, *offered_codecs,
  780. *associated_codec, &matching_codec)) {
  781. RTC_LOG(LS_WARNING)
  782. << "Couldn't find matching " << associated_codec->name << " codec.";
  783. continue;
  784. }
  785. rtx_codec.params[kCodecParamAssociatedPayloadType] =
  786. rtc::ToString(matching_codec.id);
  787. used_pltypes->FindAndSetIdUsed(&rtx_codec);
  788. offered_codecs->push_back(rtx_codec);
  789. }
  790. }
  791. }
  792. template <typename Codecs>
  793. static Codecs MatchCodecPreference(
  794. const std::vector<webrtc::RtpCodecCapability>& codec_preferences,
  795. const Codecs& codecs) {
  796. Codecs filtered_codecs;
  797. std::set<std::string> kept_codecs_ids;
  798. bool want_rtx = false;
  799. for (const auto& codec_preference : codec_preferences) {
  800. auto found_codec = absl::c_find_if(
  801. codecs, [&codec_preference](const typename Codecs::value_type& codec) {
  802. webrtc::RtpCodecParameters codec_parameters =
  803. codec.ToCodecParameters();
  804. return codec_parameters.name == codec_preference.name &&
  805. codec_parameters.kind == codec_preference.kind &&
  806. codec_parameters.num_channels ==
  807. codec_preference.num_channels &&
  808. codec_parameters.clock_rate == codec_preference.clock_rate &&
  809. codec_parameters.parameters == codec_preference.parameters;
  810. });
  811. if (found_codec != codecs.end()) {
  812. filtered_codecs.push_back(*found_codec);
  813. kept_codecs_ids.insert(std::to_string(found_codec->id));
  814. } else if (IsRtxCodec(codec_preference)) {
  815. want_rtx = true;
  816. }
  817. }
  818. if (want_rtx) {
  819. for (const auto& codec : codecs) {
  820. if (IsRtxCodec(codec)) {
  821. const auto apt =
  822. codec.params.find(cricket::kCodecParamAssociatedPayloadType);
  823. if (apt != codec.params.end() &&
  824. kept_codecs_ids.count(apt->second) > 0) {
  825. filtered_codecs.push_back(codec);
  826. }
  827. }
  828. }
  829. }
  830. return filtered_codecs;
  831. }
  832. static bool FindByUriAndEncryption(const RtpHeaderExtensions& extensions,
  833. const webrtc::RtpExtension& ext_to_match,
  834. webrtc::RtpExtension* found_extension) {
  835. auto it = absl::c_find_if(
  836. extensions, [&ext_to_match](const webrtc::RtpExtension& extension) {
  837. // We assume that all URIs are given in a canonical
  838. // format.
  839. return extension.uri == ext_to_match.uri &&
  840. extension.encrypt == ext_to_match.encrypt;
  841. });
  842. if (it == extensions.end()) {
  843. return false;
  844. }
  845. if (found_extension) {
  846. *found_extension = *it;
  847. }
  848. return true;
  849. }
  850. static bool FindByUri(const RtpHeaderExtensions& extensions,
  851. const webrtc::RtpExtension& ext_to_match,
  852. webrtc::RtpExtension* found_extension) {
  853. // We assume that all URIs are given in a canonical format.
  854. const webrtc::RtpExtension* found =
  855. webrtc::RtpExtension::FindHeaderExtensionByUri(extensions,
  856. ext_to_match.uri);
  857. if (!found) {
  858. return false;
  859. }
  860. if (found_extension) {
  861. *found_extension = *found;
  862. }
  863. return true;
  864. }
  865. static bool FindByUriWithEncryptionPreference(
  866. const RtpHeaderExtensions& extensions,
  867. const webrtc::RtpExtension& ext_to_match,
  868. bool encryption_preference,
  869. webrtc::RtpExtension* found_extension) {
  870. const webrtc::RtpExtension* unencrypted_extension = nullptr;
  871. for (const webrtc::RtpExtension& extension : extensions) {
  872. // We assume that all URIs are given in a canonical format.
  873. if (extension.uri == ext_to_match.uri) {
  874. if (!encryption_preference || extension.encrypt) {
  875. if (found_extension) {
  876. *found_extension = extension;
  877. }
  878. return true;
  879. }
  880. unencrypted_extension = &extension;
  881. }
  882. }
  883. if (unencrypted_extension) {
  884. if (found_extension) {
  885. *found_extension = *unencrypted_extension;
  886. }
  887. return true;
  888. }
  889. return false;
  890. }
  891. // Adds all extensions from |reference_extensions| to |offered_extensions| that
  892. // don't already exist in |offered_extensions| and ensure the IDs don't
  893. // collide. If an extension is added, it's also added to |regular_extensions| or
  894. // |encrypted_extensions|, and if the extension is in |regular_extensions| or
  895. // |encrypted_extensions|, its ID is marked as used in |used_ids|.
  896. // |offered_extensions| is for either audio or video while |regular_extensions|
  897. // and |encrypted_extensions| are used for both audio and video. There could be
  898. // overlap between audio extensions and video extensions.
  899. static void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions,
  900. RtpHeaderExtensions* offered_extensions,
  901. RtpHeaderExtensions* regular_extensions,
  902. RtpHeaderExtensions* encrypted_extensions,
  903. UsedRtpHeaderExtensionIds* used_ids) {
  904. for (auto reference_extension : reference_extensions) {
  905. if (!FindByUriAndEncryption(*offered_extensions, reference_extension,
  906. nullptr)) {
  907. webrtc::RtpExtension existing;
  908. if (reference_extension.encrypt) {
  909. if (FindByUriAndEncryption(*encrypted_extensions, reference_extension,
  910. &existing)) {
  911. offered_extensions->push_back(existing);
  912. } else {
  913. used_ids->FindAndSetIdUsed(&reference_extension);
  914. encrypted_extensions->push_back(reference_extension);
  915. offered_extensions->push_back(reference_extension);
  916. }
  917. } else {
  918. if (FindByUriAndEncryption(*regular_extensions, reference_extension,
  919. &existing)) {
  920. offered_extensions->push_back(existing);
  921. } else {
  922. used_ids->FindAndSetIdUsed(&reference_extension);
  923. regular_extensions->push_back(reference_extension);
  924. offered_extensions->push_back(reference_extension);
  925. }
  926. }
  927. }
  928. }
  929. }
  930. static void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* extensions,
  931. RtpHeaderExtensions* all_extensions,
  932. UsedRtpHeaderExtensionIds* used_ids) {
  933. RtpHeaderExtensions encrypted_extensions;
  934. for (const webrtc::RtpExtension& extension : *extensions) {
  935. webrtc::RtpExtension existing;
  936. // Don't add encrypted extensions again that were already included in a
  937. // previous offer or regular extensions that are also included as encrypted
  938. // extensions.
  939. if (extension.encrypt ||
  940. !webrtc::RtpExtension::IsEncryptionSupported(extension.uri) ||
  941. (FindByUriWithEncryptionPreference(*extensions, extension, true,
  942. &existing) &&
  943. existing.encrypt)) {
  944. continue;
  945. }
  946. if (FindByUri(*all_extensions, extension, &existing)) {
  947. encrypted_extensions.push_back(existing);
  948. } else {
  949. webrtc::RtpExtension encrypted(extension);
  950. encrypted.encrypt = true;
  951. used_ids->FindAndSetIdUsed(&encrypted);
  952. all_extensions->push_back(encrypted);
  953. encrypted_extensions.push_back(encrypted);
  954. }
  955. }
  956. extensions->insert(extensions->end(), encrypted_extensions.begin(),
  957. encrypted_extensions.end());
  958. }
  959. static void NegotiateRtpHeaderExtensions(
  960. const RtpHeaderExtensions& local_extensions,
  961. const RtpHeaderExtensions& offered_extensions,
  962. bool enable_encrypted_rtp_header_extensions,
  963. RtpHeaderExtensions* negotiated_extensions) {
  964. // TransportSequenceNumberV2 is not offered by default. The special logic for
  965. // the TransportSequenceNumber extensions works as follows:
  966. // Offer Answer
  967. // V1 V1 if in local_extensions.
  968. // V1 and V2 V2 regardless of local_extensions.
  969. // V2 V2 regardless of local_extensions.
  970. const webrtc::RtpExtension* transport_sequence_number_v2_offer =
  971. webrtc::RtpExtension::FindHeaderExtensionByUri(
  972. offered_extensions,
  973. webrtc::RtpExtension::kTransportSequenceNumberV2Uri);
  974. for (const webrtc::RtpExtension& ours : local_extensions) {
  975. webrtc::RtpExtension theirs;
  976. if (FindByUriWithEncryptionPreference(
  977. offered_extensions, ours, enable_encrypted_rtp_header_extensions,
  978. &theirs)) {
  979. if (transport_sequence_number_v2_offer &&
  980. ours.uri == webrtc::RtpExtension::kTransportSequenceNumberUri) {
  981. // Don't respond to
  982. // http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
  983. // if we get an offer including
  984. // http://www.webrtc.org/experiments/rtp-hdrext/transport-wide-cc-02
  985. continue;
  986. } else {
  987. // We respond with their RTP header extension id.
  988. negotiated_extensions->push_back(theirs);
  989. }
  990. }
  991. }
  992. if (transport_sequence_number_v2_offer) {
  993. // Respond that we support kTransportSequenceNumberV2Uri.
  994. negotiated_extensions->push_back(*transport_sequence_number_v2_offer);
  995. }
  996. }
  997. static void StripCNCodecs(AudioCodecs* audio_codecs) {
  998. audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(),
  999. [](const AudioCodec& codec) {
  1000. return absl::EqualsIgnoreCase(
  1001. codec.name, kComfortNoiseCodecName);
  1002. }),
  1003. audio_codecs->end());
  1004. }
  1005. template <class C>
  1006. static bool SetCodecsInAnswer(
  1007. const MediaContentDescriptionImpl<C>* offer,
  1008. const std::vector<C>& local_codecs,
  1009. const MediaDescriptionOptions& media_description_options,
  1010. const MediaSessionOptions& session_options,
  1011. UniqueRandomIdGenerator* ssrc_generator,
  1012. StreamParamsVec* current_streams,
  1013. MediaContentDescriptionImpl<C>* answer) {
  1014. std::vector<C> negotiated_codecs;
  1015. NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs,
  1016. media_description_options.codec_preferences.empty());
  1017. answer->AddCodecs(negotiated_codecs);
  1018. answer->set_protocol(offer->protocol());
  1019. if (!AddStreamParams(media_description_options.sender_options,
  1020. session_options.rtcp_cname, ssrc_generator,
  1021. current_streams, answer)) {
  1022. return false; // Something went seriously wrong.
  1023. }
  1024. return true;
  1025. }
  1026. // Create a media content to be answered for the given |sender_options|
  1027. // according to the given session_options.rtcp_mux, session_options.streams,
  1028. // codecs, crypto, and current_streams. If we don't currently have crypto (in
  1029. // current_cryptos) and it is enabled (in secure_policy), crypto is created
  1030. // (according to crypto_suites). The codecs, rtcp_mux, and crypto are all
  1031. // negotiated with the offer. If the negotiation fails, this method returns
  1032. // false. The created content is added to the offer.
  1033. static bool CreateMediaContentAnswer(
  1034. const MediaContentDescription* offer,
  1035. const MediaDescriptionOptions& media_description_options,
  1036. const MediaSessionOptions& session_options,
  1037. const SecurePolicy& sdes_policy,
  1038. const CryptoParamsVec* current_cryptos,
  1039. const RtpHeaderExtensions& local_rtp_extenstions,
  1040. UniqueRandomIdGenerator* ssrc_generator,
  1041. bool enable_encrypted_rtp_header_extensions,
  1042. StreamParamsVec* current_streams,
  1043. bool bundle_enabled,
  1044. MediaContentDescription* answer) {
  1045. answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum());
  1046. RtpHeaderExtensions negotiated_rtp_extensions;
  1047. NegotiateRtpHeaderExtensions(
  1048. local_rtp_extenstions, offer->rtp_header_extensions(),
  1049. enable_encrypted_rtp_header_extensions, &negotiated_rtp_extensions);
  1050. answer->set_rtp_header_extensions(negotiated_rtp_extensions);
  1051. answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux());
  1052. if (answer->type() == cricket::MEDIA_TYPE_VIDEO) {
  1053. answer->set_rtcp_reduced_size(offer->rtcp_reduced_size());
  1054. }
  1055. answer->set_remote_estimate(offer->remote_estimate());
  1056. if (sdes_policy != SEC_DISABLED) {
  1057. CryptoParams crypto;
  1058. if (SelectCrypto(offer, bundle_enabled, session_options.crypto_options,
  1059. &crypto)) {
  1060. if (current_cryptos) {
  1061. FindMatchingCrypto(*current_cryptos, crypto, &crypto);
  1062. }
  1063. answer->AddCrypto(crypto);
  1064. }
  1065. }
  1066. if (answer->cryptos().empty() && sdes_policy == SEC_REQUIRED) {
  1067. return false;
  1068. }
  1069. AddSimulcastToMediaDescription(media_description_options, answer);
  1070. answer->set_direction(NegotiateRtpTransceiverDirection(
  1071. offer->direction(), media_description_options.direction));
  1072. if (offer->alt_protocol() == media_description_options.alt_protocol) {
  1073. answer->set_alt_protocol(media_description_options.alt_protocol);
  1074. }
  1075. return true;
  1076. }
  1077. static bool IsMediaProtocolSupported(MediaType type,
  1078. const std::string& protocol,
  1079. bool secure_transport) {
  1080. // Since not all applications serialize and deserialize the media protocol,
  1081. // we will have to accept |protocol| to be empty.
  1082. if (protocol.empty()) {
  1083. return true;
  1084. }
  1085. if (type == MEDIA_TYPE_DATA) {
  1086. // Check for SCTP, but also for RTP for RTP-based data channels.
  1087. // TODO(pthatcher): Remove RTP once RTP-based data channels are gone.
  1088. if (secure_transport) {
  1089. // Most likely scenarios first.
  1090. return IsDtlsSctp(protocol) || IsDtlsRtp(protocol) ||
  1091. IsPlainRtp(protocol);
  1092. } else {
  1093. return IsPlainSctp(protocol) || IsPlainRtp(protocol);
  1094. }
  1095. }
  1096. // Allow for non-DTLS RTP protocol even when using DTLS because that's what
  1097. // JSEP specifies.
  1098. if (secure_transport) {
  1099. // Most likely scenarios first.
  1100. return IsDtlsRtp(protocol) || IsPlainRtp(protocol);
  1101. } else {
  1102. return IsPlainRtp(protocol);
  1103. }
  1104. }
  1105. static void SetMediaProtocol(bool secure_transport,
  1106. MediaContentDescription* desc) {
  1107. if (!desc->cryptos().empty())
  1108. desc->set_protocol(kMediaProtocolSavpf);
  1109. else if (secure_transport)
  1110. desc->set_protocol(kMediaProtocolDtlsSavpf);
  1111. else
  1112. desc->set_protocol(kMediaProtocolAvpf);
  1113. }
  1114. // Gets the TransportInfo of the given |content_name| from the
  1115. // |current_description|. If doesn't exist, returns a new one.
  1116. static const TransportDescription* GetTransportDescription(
  1117. const std::string& content_name,
  1118. const SessionDescription* current_description) {
  1119. const TransportDescription* desc = NULL;
  1120. if (current_description) {
  1121. const TransportInfo* info =
  1122. current_description->GetTransportInfoByName(content_name);
  1123. if (info) {
  1124. desc = &info->description;
  1125. }
  1126. }
  1127. return desc;
  1128. }
  1129. // Gets the current DTLS state from the transport description.
  1130. static bool IsDtlsActive(const ContentInfo* content,
  1131. const SessionDescription* current_description) {
  1132. if (!content) {
  1133. return false;
  1134. }
  1135. size_t msection_index = content - &current_description->contents()[0];
  1136. if (current_description->transport_infos().size() <= msection_index) {
  1137. return false;
  1138. }
  1139. return current_description->transport_infos()[msection_index]
  1140. .description.secure();
  1141. }
  1142. void MediaDescriptionOptions::AddAudioSender(
  1143. const std::string& track_id,
  1144. const std::vector<std::string>& stream_ids) {
  1145. RTC_DCHECK(type == MEDIA_TYPE_AUDIO);
  1146. AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1);
  1147. }
  1148. void MediaDescriptionOptions::AddVideoSender(
  1149. const std::string& track_id,
  1150. const std::vector<std::string>& stream_ids,
  1151. const std::vector<RidDescription>& rids,
  1152. const SimulcastLayerList& simulcast_layers,
  1153. int num_sim_layers) {
  1154. RTC_DCHECK(type == MEDIA_TYPE_VIDEO);
  1155. RTC_DCHECK(rids.empty() || num_sim_layers == 0)
  1156. << "RIDs are the compliant way to indicate simulcast.";
  1157. RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers));
  1158. AddSenderInternal(track_id, stream_ids, rids, simulcast_layers,
  1159. num_sim_layers);
  1160. }
  1161. void MediaDescriptionOptions::AddRtpDataChannel(const std::string& track_id,
  1162. const std::string& stream_id) {
  1163. RTC_DCHECK(type == MEDIA_TYPE_DATA);
  1164. // TODO(steveanton): Is it the case that RtpDataChannel will never have more
  1165. // than one stream?
  1166. AddSenderInternal(track_id, {stream_id}, {}, SimulcastLayerList(), 1);
  1167. }
  1168. void MediaDescriptionOptions::AddSenderInternal(
  1169. const std::string& track_id,
  1170. const std::vector<std::string>& stream_ids,
  1171. const std::vector<RidDescription>& rids,
  1172. const SimulcastLayerList& simulcast_layers,
  1173. int num_sim_layers) {
  1174. // TODO(steveanton): Support any number of stream ids.
  1175. RTC_CHECK(stream_ids.size() == 1U);
  1176. SenderOptions options;
  1177. options.track_id = track_id;
  1178. options.stream_ids = stream_ids;
  1179. options.simulcast_layers = simulcast_layers;
  1180. options.rids = rids;
  1181. options.num_sim_layers = num_sim_layers;
  1182. sender_options.push_back(options);
  1183. }
  1184. bool MediaSessionOptions::HasMediaDescription(MediaType type) const {
  1185. return absl::c_any_of(
  1186. media_description_options,
  1187. [type](const MediaDescriptionOptions& t) { return t.type == type; });
  1188. }
  1189. MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
  1190. const TransportDescriptionFactory* transport_desc_factory,
  1191. rtc::UniqueRandomIdGenerator* ssrc_generator)
  1192. : ssrc_generator_(ssrc_generator),
  1193. transport_desc_factory_(transport_desc_factory) {
  1194. RTC_DCHECK(ssrc_generator_);
  1195. }
  1196. MediaSessionDescriptionFactory::MediaSessionDescriptionFactory(
  1197. ChannelManager* channel_manager,
  1198. const TransportDescriptionFactory* transport_desc_factory,
  1199. rtc::UniqueRandomIdGenerator* ssrc_generator)
  1200. : MediaSessionDescriptionFactory(transport_desc_factory, ssrc_generator) {
  1201. channel_manager->GetSupportedAudioSendCodecs(&audio_send_codecs_);
  1202. channel_manager->GetSupportedAudioReceiveCodecs(&audio_recv_codecs_);
  1203. channel_manager->GetSupportedAudioRtpHeaderExtensions(&audio_rtp_extensions_);
  1204. channel_manager->GetSupportedVideoCodecs(&video_codecs_);
  1205. channel_manager->GetSupportedVideoRtpHeaderExtensions(&video_rtp_extensions_);
  1206. channel_manager->GetSupportedDataCodecs(&rtp_data_codecs_);
  1207. ComputeAudioCodecsIntersectionAndUnion();
  1208. }
  1209. const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs()
  1210. const {
  1211. return audio_sendrecv_codecs_;
  1212. }
  1213. const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const {
  1214. return audio_send_codecs_;
  1215. }
  1216. const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const {
  1217. return audio_recv_codecs_;
  1218. }
  1219. void MediaSessionDescriptionFactory::set_audio_codecs(
  1220. const AudioCodecs& send_codecs,
  1221. const AudioCodecs& recv_codecs) {
  1222. audio_send_codecs_ = send_codecs;
  1223. audio_recv_codecs_ = recv_codecs;
  1224. ComputeAudioCodecsIntersectionAndUnion();
  1225. }
  1226. static void RemoveUnifiedPlanExtensions(RtpHeaderExtensions* extensions) {
  1227. RTC_DCHECK(extensions);
  1228. extensions->erase(
  1229. std::remove_if(extensions->begin(), extensions->end(),
  1230. [](auto extension) {
  1231. return extension.uri == webrtc::RtpExtension::kMidUri ||
  1232. extension.uri == webrtc::RtpExtension::kRidUri ||
  1233. extension.uri ==
  1234. webrtc::RtpExtension::kRepairedRidUri;
  1235. }),
  1236. extensions->end());
  1237. }
  1238. RtpHeaderExtensions
  1239. MediaSessionDescriptionFactory::audio_rtp_header_extensions() const {
  1240. RtpHeaderExtensions extensions = audio_rtp_extensions_;
  1241. if (!is_unified_plan_) {
  1242. RemoveUnifiedPlanExtensions(&extensions);
  1243. }
  1244. return extensions;
  1245. }
  1246. RtpHeaderExtensions
  1247. MediaSessionDescriptionFactory::video_rtp_header_extensions() const {
  1248. RtpHeaderExtensions extensions = video_rtp_extensions_;
  1249. if (!is_unified_plan_) {
  1250. RemoveUnifiedPlanExtensions(&extensions);
  1251. }
  1252. return extensions;
  1253. }
  1254. std::unique_ptr<SessionDescription> MediaSessionDescriptionFactory::CreateOffer(
  1255. const MediaSessionOptions& session_options,
  1256. const SessionDescription* current_description) const {
  1257. // Must have options for each existing section.
  1258. if (current_description) {
  1259. RTC_DCHECK_LE(current_description->contents().size(),
  1260. session_options.media_description_options.size());
  1261. }
  1262. IceCredentialsIterator ice_credentials(
  1263. session_options.pooled_ice_credentials);
  1264. std::vector<const ContentInfo*> current_active_contents;
  1265. if (current_description) {
  1266. current_active_contents =
  1267. GetActiveContents(*current_description, session_options);
  1268. }
  1269. StreamParamsVec current_streams =
  1270. GetCurrentStreamParams(current_active_contents);
  1271. AudioCodecs offer_audio_codecs;
  1272. VideoCodecs offer_video_codecs;
  1273. RtpDataCodecs offer_rtp_data_codecs;
  1274. GetCodecsForOffer(current_active_contents, &offer_audio_codecs,
  1275. &offer_video_codecs, &offer_rtp_data_codecs);
  1276. if (!session_options.vad_enabled) {
  1277. // If application doesn't want CN codecs in offer.
  1278. StripCNCodecs(&offer_audio_codecs);
  1279. }
  1280. FilterDataCodecs(&offer_rtp_data_codecs,
  1281. session_options.data_channel_type == DCT_SCTP);
  1282. RtpHeaderExtensions audio_rtp_extensions;
  1283. RtpHeaderExtensions video_rtp_extensions;
  1284. GetRtpHdrExtsToOffer(current_active_contents,
  1285. session_options.offer_extmap_allow_mixed,
  1286. &audio_rtp_extensions, &video_rtp_extensions);
  1287. auto offer = std::make_unique<SessionDescription>();
  1288. // Iterate through the media description options, matching with existing media
  1289. // descriptions in |current_description|.
  1290. size_t msection_index = 0;
  1291. for (const MediaDescriptionOptions& media_description_options :
  1292. session_options.media_description_options) {
  1293. const ContentInfo* current_content = nullptr;
  1294. if (current_description &&
  1295. msection_index < current_description->contents().size()) {
  1296. current_content = &current_description->contents()[msection_index];
  1297. // Media type must match unless this media section is being recycled.
  1298. RTC_DCHECK(current_content->name != media_description_options.mid ||
  1299. IsMediaContentOfType(current_content,
  1300. media_description_options.type));
  1301. }
  1302. switch (media_description_options.type) {
  1303. case MEDIA_TYPE_AUDIO:
  1304. if (!AddAudioContentForOffer(
  1305. media_description_options, session_options, current_content,
  1306. current_description, audio_rtp_extensions, offer_audio_codecs,
  1307. &current_streams, offer.get(), &ice_credentials)) {
  1308. return nullptr;
  1309. }
  1310. break;
  1311. case MEDIA_TYPE_VIDEO:
  1312. if (!AddVideoContentForOffer(
  1313. media_description_options, session_options, current_content,
  1314. current_description, video_rtp_extensions, offer_video_codecs,
  1315. &current_streams, offer.get(), &ice_credentials)) {
  1316. return nullptr;
  1317. }
  1318. break;
  1319. case MEDIA_TYPE_DATA:
  1320. if (!AddDataContentForOffer(media_description_options, session_options,
  1321. current_content, current_description,
  1322. offer_rtp_data_codecs, &current_streams,
  1323. offer.get(), &ice_credentials)) {
  1324. return nullptr;
  1325. }
  1326. break;
  1327. default:
  1328. RTC_NOTREACHED();
  1329. }
  1330. ++msection_index;
  1331. }
  1332. // Bundle the contents together, if we've been asked to do so, and update any
  1333. // parameters that need to be tweaked for BUNDLE.
  1334. if (session_options.bundle_enabled) {
  1335. ContentGroup offer_bundle(GROUP_TYPE_BUNDLE);
  1336. for (const ContentInfo& content : offer->contents()) {
  1337. if (content.rejected) {
  1338. continue;
  1339. }
  1340. // TODO(deadbeef): There are conditions that make bundling two media
  1341. // descriptions together illegal. For example, they use the same payload
  1342. // type to represent different codecs, or same IDs for different header
  1343. // extensions. We need to detect this and not try to bundle those media
  1344. // descriptions together.
  1345. offer_bundle.AddContentName(content.name);
  1346. }
  1347. if (!offer_bundle.content_names().empty()) {
  1348. offer->AddGroup(offer_bundle);
  1349. if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) {
  1350. RTC_LOG(LS_ERROR)
  1351. << "CreateOffer failed to UpdateTransportInfoForBundle.";
  1352. return nullptr;
  1353. }
  1354. if (!UpdateCryptoParamsForBundle(offer_bundle, offer.get())) {
  1355. RTC_LOG(LS_ERROR)
  1356. << "CreateOffer failed to UpdateCryptoParamsForBundle.";
  1357. return nullptr;
  1358. }
  1359. }
  1360. }
  1361. // The following determines how to signal MSIDs to ensure compatibility with
  1362. // older endpoints (in particular, older Plan B endpoints).
  1363. if (is_unified_plan_) {
  1364. // Be conservative and signal using both a=msid and a=ssrc lines. Unified
  1365. // Plan answerers will look at a=msid and Plan B answerers will look at the
  1366. // a=ssrc MSID line.
  1367. offer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
  1368. cricket::kMsidSignalingSsrcAttribute);
  1369. } else {
  1370. // Plan B always signals MSID using a=ssrc lines.
  1371. offer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
  1372. }
  1373. offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed);
  1374. if (session_options.media_transport_settings.has_value()) {
  1375. offer->AddMediaTransportSetting(
  1376. session_options.media_transport_settings->transport_name,
  1377. session_options.media_transport_settings->transport_setting);
  1378. }
  1379. return offer;
  1380. }
  1381. std::unique_ptr<SessionDescription>
  1382. MediaSessionDescriptionFactory::CreateAnswer(
  1383. const SessionDescription* offer,
  1384. const MediaSessionOptions& session_options,
  1385. const SessionDescription* current_description) const {
  1386. if (!offer) {
  1387. return nullptr;
  1388. }
  1389. // Must have options for exactly as many sections as in the offer.
  1390. RTC_DCHECK_EQ(offer->contents().size(),
  1391. session_options.media_description_options.size());
  1392. IceCredentialsIterator ice_credentials(
  1393. session_options.pooled_ice_credentials);
  1394. std::vector<const ContentInfo*> current_active_contents;
  1395. if (current_description) {
  1396. current_active_contents =
  1397. GetActiveContents(*current_description, session_options);
  1398. }
  1399. StreamParamsVec current_streams =
  1400. GetCurrentStreamParams(current_active_contents);
  1401. // Get list of all possible codecs that respects existing payload type
  1402. // mappings and uses a single payload type space.
  1403. //
  1404. // Note that these lists may be further filtered for each m= section; this
  1405. // step is done just to establish the payload type mappings shared by all
  1406. // sections.
  1407. AudioCodecs answer_audio_codecs;
  1408. VideoCodecs answer_video_codecs;
  1409. RtpDataCodecs answer_rtp_data_codecs;
  1410. GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs,
  1411. &answer_video_codecs, &answer_rtp_data_codecs);
  1412. if (!session_options.vad_enabled) {
  1413. // If application doesn't want CN codecs in answer.
  1414. StripCNCodecs(&answer_audio_codecs);
  1415. }
  1416. FilterDataCodecs(&answer_rtp_data_codecs,
  1417. session_options.data_channel_type == DCT_SCTP);
  1418. auto answer = std::make_unique<SessionDescription>();
  1419. // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE
  1420. // group in the answer with the appropriate content names.
  1421. const ContentGroup* offer_bundle = offer->GetGroupByName(GROUP_TYPE_BUNDLE);
  1422. ContentGroup answer_bundle(GROUP_TYPE_BUNDLE);
  1423. // Transport info shared by the bundle group.
  1424. std::unique_ptr<TransportInfo> bundle_transport;
  1425. answer->set_extmap_allow_mixed(offer->extmap_allow_mixed());
  1426. // Iterate through the media description options, matching with existing
  1427. // media descriptions in |current_description|.
  1428. size_t msection_index = 0;
  1429. for (const MediaDescriptionOptions& media_description_options :
  1430. session_options.media_description_options) {
  1431. const ContentInfo* offer_content = &offer->contents()[msection_index];
  1432. // Media types and MIDs must match between the remote offer and the
  1433. // MediaDescriptionOptions.
  1434. RTC_DCHECK(
  1435. IsMediaContentOfType(offer_content, media_description_options.type));
  1436. RTC_DCHECK(media_description_options.mid == offer_content->name);
  1437. const ContentInfo* current_content = nullptr;
  1438. if (current_description &&
  1439. msection_index < current_description->contents().size()) {
  1440. current_content = &current_description->contents()[msection_index];
  1441. }
  1442. switch (media_description_options.type) {
  1443. case MEDIA_TYPE_AUDIO:
  1444. if (!AddAudioContentForAnswer(
  1445. media_description_options, session_options, offer_content,
  1446. offer, current_content, current_description,
  1447. bundle_transport.get(), answer_audio_codecs, &current_streams,
  1448. answer.get(), &ice_credentials)) {
  1449. return nullptr;
  1450. }
  1451. break;
  1452. case MEDIA_TYPE_VIDEO:
  1453. if (!AddVideoContentForAnswer(
  1454. media_description_options, session_options, offer_content,
  1455. offer, current_content, current_description,
  1456. bundle_transport.get(), answer_video_codecs, &current_streams,
  1457. answer.get(), &ice_credentials)) {
  1458. return nullptr;
  1459. }
  1460. break;
  1461. case MEDIA_TYPE_DATA:
  1462. if (!AddDataContentForAnswer(
  1463. media_description_options, session_options, offer_content,
  1464. offer, current_content, current_description,
  1465. bundle_transport.get(), answer_rtp_data_codecs,
  1466. &current_streams, answer.get(), &ice_credentials)) {
  1467. return nullptr;
  1468. }
  1469. break;
  1470. default:
  1471. RTC_NOTREACHED();
  1472. }
  1473. ++msection_index;
  1474. // See if we can add the newly generated m= section to the BUNDLE group in
  1475. // the answer.
  1476. ContentInfo& added = answer->contents().back();
  1477. if (!added.rejected && session_options.bundle_enabled && offer_bundle &&
  1478. offer_bundle->HasContentName(added.name)) {
  1479. answer_bundle.AddContentName(added.name);
  1480. bundle_transport.reset(
  1481. new TransportInfo(*answer->GetTransportInfoByName(added.name)));
  1482. }
  1483. }
  1484. // If a BUNDLE group was offered, put a BUNDLE group in the answer even if
  1485. // it's empty. RFC5888 says:
  1486. //
  1487. // A SIP entity that receives an offer that contains an "a=group" line
  1488. // with semantics that are understood MUST return an answer that
  1489. // contains an "a=group" line with the same semantics.
  1490. if (offer_bundle) {
  1491. answer->AddGroup(answer_bundle);
  1492. }
  1493. if (answer_bundle.FirstContentName()) {
  1494. // Share the same ICE credentials and crypto params across all contents,
  1495. // as BUNDLE requires.
  1496. if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) {
  1497. RTC_LOG(LS_ERROR)
  1498. << "CreateAnswer failed to UpdateTransportInfoForBundle.";
  1499. return NULL;
  1500. }
  1501. if (!UpdateCryptoParamsForBundle(answer_bundle, answer.get())) {
  1502. RTC_LOG(LS_ERROR)
  1503. << "CreateAnswer failed to UpdateCryptoParamsForBundle.";
  1504. return NULL;
  1505. }
  1506. }
  1507. // The following determines how to signal MSIDs to ensure compatibility with
  1508. // older endpoints (in particular, older Plan B endpoints).
  1509. if (is_unified_plan_) {
  1510. // Unified Plan needs to look at what the offer included to find the most
  1511. // compatible answer.
  1512. if (offer->msid_signaling() == 0) {
  1513. // We end up here in one of three cases:
  1514. // 1. An empty offer. We'll reply with an empty answer so it doesn't
  1515. // matter what we pick here.
  1516. // 2. A data channel only offer. We won't add any MSIDs to the answer so
  1517. // it also doesn't matter what we pick here.
  1518. // 3. Media that's either sendonly or inactive from the remote endpoint.
  1519. // We don't have any information to say whether the endpoint is Plan B
  1520. // or Unified Plan, so be conservative and send both.
  1521. answer->set_msid_signaling(cricket::kMsidSignalingMediaSection |
  1522. cricket::kMsidSignalingSsrcAttribute);
  1523. } else if (offer->msid_signaling() ==
  1524. (cricket::kMsidSignalingMediaSection |
  1525. cricket::kMsidSignalingSsrcAttribute)) {
  1526. // If both a=msid and a=ssrc MSID signaling methods were used, we're
  1527. // probably talking to a Unified Plan endpoint so respond with just
  1528. // a=msid.
  1529. answer->set_msid_signaling(cricket::kMsidSignalingMediaSection);
  1530. } else {
  1531. // Otherwise, it's clear which method the offerer is using so repeat that
  1532. // back to them.
  1533. answer->set_msid_signaling(offer->msid_signaling());
  1534. }
  1535. } else {
  1536. // Plan B always signals MSID using a=ssrc lines.
  1537. answer->set_msid_signaling(cricket::kMsidSignalingSsrcAttribute);
  1538. }
  1539. return answer;
  1540. }
  1541. const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer(
  1542. const RtpTransceiverDirection& direction) const {
  1543. switch (direction) {
  1544. // If stream is inactive - generate list as if sendrecv.
  1545. case RtpTransceiverDirection::kSendRecv:
  1546. case RtpTransceiverDirection::kInactive:
  1547. return audio_sendrecv_codecs_;
  1548. case RtpTransceiverDirection::kSendOnly:
  1549. return audio_send_codecs_;
  1550. case RtpTransceiverDirection::kRecvOnly:
  1551. return audio_recv_codecs_;
  1552. }
  1553. RTC_NOTREACHED();
  1554. return audio_sendrecv_codecs_;
  1555. }
  1556. const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer(
  1557. const RtpTransceiverDirection& offer,
  1558. const RtpTransceiverDirection& answer) const {
  1559. switch (answer) {
  1560. // For inactive and sendrecv answers, generate lists as if we were to accept
  1561. // the offer's direction. See RFC 3264 Section 6.1.
  1562. case RtpTransceiverDirection::kSendRecv:
  1563. case RtpTransceiverDirection::kInactive:
  1564. return GetAudioCodecsForOffer(
  1565. webrtc::RtpTransceiverDirectionReversed(offer));
  1566. case RtpTransceiverDirection::kSendOnly:
  1567. return audio_send_codecs_;
  1568. case RtpTransceiverDirection::kRecvOnly:
  1569. return audio_recv_codecs_;
  1570. }
  1571. RTC_NOTREACHED();
  1572. return audio_sendrecv_codecs_;
  1573. }
  1574. void MergeCodecsFromDescription(
  1575. const std::vector<const ContentInfo*>& current_active_contents,
  1576. AudioCodecs* audio_codecs,
  1577. VideoCodecs* video_codecs,
  1578. RtpDataCodecs* rtp_data_codecs,
  1579. UsedPayloadTypes* used_pltypes) {
  1580. for (const ContentInfo* content : current_active_contents) {
  1581. if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
  1582. const AudioContentDescription* audio =
  1583. content->media_description()->as_audio();
  1584. MergeCodecs<AudioCodec>(audio->codecs(), audio_codecs, used_pltypes);
  1585. } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
  1586. const VideoContentDescription* video =
  1587. content->media_description()->as_video();
  1588. MergeCodecs<VideoCodec>(video->codecs(), video_codecs, used_pltypes);
  1589. } else if (IsMediaContentOfType(content, MEDIA_TYPE_DATA)) {
  1590. const RtpDataContentDescription* data =
  1591. content->media_description()->as_rtp_data();
  1592. if (data) {
  1593. // Only relevant for RTP datachannels
  1594. MergeCodecs<RtpDataCodec>(data->codecs(), rtp_data_codecs,
  1595. used_pltypes);
  1596. }
  1597. }
  1598. }
  1599. }
  1600. // Getting codecs for an offer involves these steps:
  1601. //
  1602. // 1. Construct payload type -> codec mappings for current description.
  1603. // 2. Add any reference codecs that weren't already present
  1604. // 3. For each individual media description (m= section), filter codecs based
  1605. // on the directional attribute (happens in another method).
  1606. void MediaSessionDescriptionFactory::GetCodecsForOffer(
  1607. const std::vector<const ContentInfo*>& current_active_contents,
  1608. AudioCodecs* audio_codecs,
  1609. VideoCodecs* video_codecs,
  1610. RtpDataCodecs* rtp_data_codecs) const {
  1611. // First - get all codecs from the current description if the media type
  1612. // is used. Add them to |used_pltypes| so the payload type is not reused if a
  1613. // new media type is added.
  1614. UsedPayloadTypes used_pltypes;
  1615. MergeCodecsFromDescription(current_active_contents, audio_codecs,
  1616. video_codecs, rtp_data_codecs, &used_pltypes);
  1617. // Add our codecs that are not in the current description.
  1618. MergeCodecs<AudioCodec>(all_audio_codecs_, audio_codecs, &used_pltypes);
  1619. MergeCodecs<VideoCodec>(video_codecs_, video_codecs, &used_pltypes);
  1620. MergeCodecs<DataCodec>(rtp_data_codecs_, rtp_data_codecs, &used_pltypes);
  1621. }
  1622. // Getting codecs for an answer involves these steps:
  1623. //
  1624. // 1. Construct payload type -> codec mappings for current description.
  1625. // 2. Add any codecs from the offer that weren't already present.
  1626. // 3. Add any remaining codecs that weren't already present.
  1627. // 4. For each individual media description (m= section), filter codecs based
  1628. // on the directional attribute (happens in another method).
  1629. void MediaSessionDescriptionFactory::GetCodecsForAnswer(
  1630. const std::vector<const ContentInfo*>& current_active_contents,
  1631. const SessionDescription& remote_offer,
  1632. AudioCodecs* audio_codecs,
  1633. VideoCodecs* video_codecs,
  1634. RtpDataCodecs* rtp_data_codecs) const {
  1635. // First - get all codecs from the current description if the media type
  1636. // is used. Add them to |used_pltypes| so the payload type is not reused if a
  1637. // new media type is added.
  1638. UsedPayloadTypes used_pltypes;
  1639. MergeCodecsFromDescription(current_active_contents, audio_codecs,
  1640. video_codecs, rtp_data_codecs, &used_pltypes);
  1641. // Second - filter out codecs that we don't support at all and should ignore.
  1642. AudioCodecs filtered_offered_audio_codecs;
  1643. VideoCodecs filtered_offered_video_codecs;
  1644. RtpDataCodecs filtered_offered_rtp_data_codecs;
  1645. for (const ContentInfo& content : remote_offer.contents()) {
  1646. if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) {
  1647. const AudioContentDescription* audio =
  1648. content.media_description()->as_audio();
  1649. for (const AudioCodec& offered_audio_codec : audio->codecs()) {
  1650. if (!FindMatchingCodec<AudioCodec>(audio->codecs(),
  1651. filtered_offered_audio_codecs,
  1652. offered_audio_codec, nullptr) &&
  1653. FindMatchingCodec<AudioCodec>(audio->codecs(), all_audio_codecs_,
  1654. offered_audio_codec, nullptr)) {
  1655. filtered_offered_audio_codecs.push_back(offered_audio_codec);
  1656. }
  1657. }
  1658. } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) {
  1659. const VideoContentDescription* video =
  1660. content.media_description()->as_video();
  1661. for (const VideoCodec& offered_video_codec : video->codecs()) {
  1662. if (!FindMatchingCodec<VideoCodec>(video->codecs(),
  1663. filtered_offered_video_codecs,
  1664. offered_video_codec, nullptr) &&
  1665. FindMatchingCodec<VideoCodec>(video->codecs(), video_codecs_,
  1666. offered_video_codec, nullptr)) {
  1667. filtered_offered_video_codecs.push_back(offered_video_codec);
  1668. }
  1669. }
  1670. } else if (IsMediaContentOfType(&content, MEDIA_TYPE_DATA)) {
  1671. const RtpDataContentDescription* data =
  1672. content.media_description()->as_rtp_data();
  1673. if (data) {
  1674. // RTP data. This part is inactive for SCTP data.
  1675. for (const RtpDataCodec& offered_rtp_data_codec : data->codecs()) {
  1676. if (!FindMatchingCodec<RtpDataCodec>(
  1677. data->codecs(), filtered_offered_rtp_data_codecs,
  1678. offered_rtp_data_codec, nullptr) &&
  1679. FindMatchingCodec<RtpDataCodec>(data->codecs(), rtp_data_codecs_,
  1680. offered_rtp_data_codec,
  1681. nullptr)) {
  1682. filtered_offered_rtp_data_codecs.push_back(offered_rtp_data_codec);
  1683. }
  1684. }
  1685. }
  1686. }
  1687. }
  1688. // Add codecs that are not in the current description but were in
  1689. // |remote_offer|.
  1690. MergeCodecs<AudioCodec>(filtered_offered_audio_codecs, audio_codecs,
  1691. &used_pltypes);
  1692. MergeCodecs<VideoCodec>(filtered_offered_video_codecs, video_codecs,
  1693. &used_pltypes);
  1694. MergeCodecs<DataCodec>(filtered_offered_rtp_data_codecs, rtp_data_codecs,
  1695. &used_pltypes);
  1696. }
  1697. void MediaSessionDescriptionFactory::GetRtpHdrExtsToOffer(
  1698. const std::vector<const ContentInfo*>& current_active_contents,
  1699. bool extmap_allow_mixed,
  1700. RtpHeaderExtensions* offer_audio_extensions,
  1701. RtpHeaderExtensions* offer_video_extensions) const {
  1702. // All header extensions allocated from the same range to avoid potential
  1703. // issues when using BUNDLE.
  1704. // Strictly speaking the SDP attribute extmap_allow_mixed signals that the
  1705. // receiver supports an RTP stream where one- and two-byte RTP header
  1706. // extensions are mixed. For backwards compatibility reasons it's used in
  1707. // WebRTC to signal that two-byte RTP header extensions are supported.
  1708. UsedRtpHeaderExtensionIds used_ids(
  1709. extmap_allow_mixed ? UsedRtpHeaderExtensionIds::IdDomain::kTwoByteAllowed
  1710. : UsedRtpHeaderExtensionIds::IdDomain::kOneByteOnly);
  1711. RtpHeaderExtensions all_regular_extensions;
  1712. RtpHeaderExtensions all_encrypted_extensions;
  1713. // First - get all extensions from the current description if the media type
  1714. // is used.
  1715. // Add them to |used_ids| so the local ids are not reused if a new media
  1716. // type is added.
  1717. for (const ContentInfo* content : current_active_contents) {
  1718. if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) {
  1719. const AudioContentDescription* audio =
  1720. content->media_description()->as_audio();
  1721. MergeRtpHdrExts(audio->rtp_header_extensions(), offer_audio_extensions,
  1722. &all_regular_extensions, &all_encrypted_extensions,
  1723. &used_ids);
  1724. } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) {
  1725. const VideoContentDescription* video =
  1726. content->media_description()->as_video();
  1727. MergeRtpHdrExts(video->rtp_header_extensions(), offer_video_extensions,
  1728. &all_regular_extensions, &all_encrypted_extensions,
  1729. &used_ids);
  1730. }
  1731. }
  1732. // Add our default RTP header extensions that are not in the current
  1733. // description.
  1734. MergeRtpHdrExts(audio_rtp_header_extensions(), offer_audio_extensions,
  1735. &all_regular_extensions, &all_encrypted_extensions,
  1736. &used_ids);
  1737. MergeRtpHdrExts(video_rtp_header_extensions(), offer_video_extensions,
  1738. &all_regular_extensions, &all_encrypted_extensions,
  1739. &used_ids);
  1740. // TODO(jbauch): Support adding encrypted header extensions to existing
  1741. // sessions.
  1742. if (enable_encrypted_rtp_header_extensions_ &&
  1743. current_active_contents.empty()) {
  1744. AddEncryptedVersionsOfHdrExts(offer_audio_extensions,
  1745. &all_encrypted_extensions, &used_ids);
  1746. AddEncryptedVersionsOfHdrExts(offer_video_extensions,
  1747. &all_encrypted_extensions, &used_ids);
  1748. }
  1749. }
  1750. bool MediaSessionDescriptionFactory::AddTransportOffer(
  1751. const std::string& content_name,
  1752. const TransportOptions& transport_options,
  1753. const SessionDescription* current_desc,
  1754. SessionDescription* offer_desc,
  1755. IceCredentialsIterator* ice_credentials) const {
  1756. if (!transport_desc_factory_)
  1757. return false;
  1758. const TransportDescription* current_tdesc =
  1759. GetTransportDescription(content_name, current_desc);
  1760. std::unique_ptr<TransportDescription> new_tdesc(
  1761. transport_desc_factory_->CreateOffer(transport_options, current_tdesc,
  1762. ice_credentials));
  1763. if (!new_tdesc) {
  1764. RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name="
  1765. << content_name;
  1766. }
  1767. offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc));
  1768. return true;
  1769. }
  1770. std::unique_ptr<TransportDescription>
  1771. MediaSessionDescriptionFactory::CreateTransportAnswer(
  1772. const std::string& content_name,
  1773. const SessionDescription* offer_desc,
  1774. const TransportOptions& transport_options,
  1775. const SessionDescription* current_desc,
  1776. bool require_transport_attributes,
  1777. IceCredentialsIterator* ice_credentials) const {
  1778. if (!transport_desc_factory_)
  1779. return NULL;
  1780. const TransportDescription* offer_tdesc =
  1781. GetTransportDescription(content_name, offer_desc);
  1782. const TransportDescription* current_tdesc =
  1783. GetTransportDescription(content_name, current_desc);
  1784. return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options,
  1785. require_transport_attributes,
  1786. current_tdesc, ice_credentials);
  1787. }
  1788. bool MediaSessionDescriptionFactory::AddTransportAnswer(
  1789. const std::string& content_name,
  1790. const TransportDescription& transport_desc,
  1791. SessionDescription* answer_desc) const {
  1792. answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc));
  1793. return true;
  1794. }
  1795. // |audio_codecs| = set of all possible codecs that can be used, with correct
  1796. // payload type mappings
  1797. //
  1798. // |supported_audio_codecs| = set of codecs that are supported for the direction
  1799. // of this m= section
  1800. //
  1801. // acd->codecs() = set of previously negotiated codecs for this m= section
  1802. //
  1803. // The payload types should come from audio_codecs, but the order should come
  1804. // from acd->codecs() and then supported_codecs, to ensure that re-offers don't
  1805. // change existing codec priority, and that new codecs are added with the right
  1806. // priority.
  1807. bool MediaSessionDescriptionFactory::AddAudioContentForOffer(
  1808. const MediaDescriptionOptions& media_description_options,
  1809. const MediaSessionOptions& session_options,
  1810. const ContentInfo* current_content,
  1811. const SessionDescription* current_description,
  1812. const RtpHeaderExtensions& audio_rtp_extensions,
  1813. const AudioCodecs& audio_codecs,
  1814. StreamParamsVec* current_streams,
  1815. SessionDescription* desc,
  1816. IceCredentialsIterator* ice_credentials) const {
  1817. // Filter audio_codecs (which includes all codecs, with correctly remapped
  1818. // payload types) based on transceiver direction.
  1819. const AudioCodecs& supported_audio_codecs =
  1820. GetAudioCodecsForOffer(media_description_options.direction);
  1821. AudioCodecs filtered_codecs;
  1822. if (!media_description_options.codec_preferences.empty()) {
  1823. // Add the codecs from the current transceiver's codec preferences.
  1824. // They override any existing codecs from previous negotiations.
  1825. filtered_codecs = MatchCodecPreference(
  1826. media_description_options.codec_preferences, supported_audio_codecs);
  1827. } else {
  1828. // Add the codecs from current content if it exists and is not rejected nor
  1829. // recycled.
  1830. if (current_content && !current_content->rejected &&
  1831. current_content->name == media_description_options.mid) {
  1832. RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
  1833. const AudioContentDescription* acd =
  1834. current_content->media_description()->as_audio();
  1835. for (const AudioCodec& codec : acd->codecs()) {
  1836. if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
  1837. nullptr)) {
  1838. filtered_codecs.push_back(codec);
  1839. }
  1840. }
  1841. }
  1842. // Add other supported audio codecs.
  1843. AudioCodec found_codec;
  1844. for (const AudioCodec& codec : supported_audio_codecs) {
  1845. if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
  1846. codec, &found_codec) &&
  1847. !FindMatchingCodec<AudioCodec>(supported_audio_codecs,
  1848. filtered_codecs, codec, nullptr)) {
  1849. // Use the |found_codec| from |audio_codecs| because it has the
  1850. // correctly mapped payload type.
  1851. filtered_codecs.push_back(found_codec);
  1852. }
  1853. }
  1854. }
  1855. cricket::SecurePolicy sdes_policy =
  1856. IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
  1857. : secure();
  1858. std::unique_ptr<AudioContentDescription> audio(new AudioContentDescription());
  1859. std::vector<std::string> crypto_suites;
  1860. GetSupportedAudioSdesCryptoSuiteNames(session_options.crypto_options,
  1861. &crypto_suites);
  1862. if (!CreateMediaContentOffer(media_description_options, session_options,
  1863. filtered_codecs, sdes_policy,
  1864. GetCryptos(current_content), crypto_suites,
  1865. audio_rtp_extensions, ssrc_generator_,
  1866. current_streams, audio.get())) {
  1867. return false;
  1868. }
  1869. bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
  1870. SetMediaProtocol(secure_transport, audio.get());
  1871. audio->set_direction(media_description_options.direction);
  1872. desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
  1873. media_description_options.stopped, std::move(audio));
  1874. if (!AddTransportOffer(media_description_options.mid,
  1875. media_description_options.transport_options,
  1876. current_description, desc, ice_credentials)) {
  1877. return false;
  1878. }
  1879. return true;
  1880. }
  1881. bool MediaSessionDescriptionFactory::AddVideoContentForOffer(
  1882. const MediaDescriptionOptions& media_description_options,
  1883. const MediaSessionOptions& session_options,
  1884. const ContentInfo* current_content,
  1885. const SessionDescription* current_description,
  1886. const RtpHeaderExtensions& video_rtp_extensions,
  1887. const VideoCodecs& video_codecs,
  1888. StreamParamsVec* current_streams,
  1889. SessionDescription* desc,
  1890. IceCredentialsIterator* ice_credentials) const {
  1891. cricket::SecurePolicy sdes_policy =
  1892. IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
  1893. : secure();
  1894. std::unique_ptr<VideoContentDescription> video(new VideoContentDescription());
  1895. std::vector<std::string> crypto_suites;
  1896. GetSupportedVideoSdesCryptoSuiteNames(session_options.crypto_options,
  1897. &crypto_suites);
  1898. VideoCodecs filtered_codecs;
  1899. if (!media_description_options.codec_preferences.empty()) {
  1900. // Add the codecs from the current transceiver's codec preferences.
  1901. // They override any existing codecs from previous negotiations.
  1902. filtered_codecs = MatchCodecPreference(
  1903. media_description_options.codec_preferences, video_codecs_);
  1904. } else {
  1905. // Add the codecs from current content if it exists and is not rejected nor
  1906. // recycled.
  1907. if (current_content && !current_content->rejected &&
  1908. current_content->name == media_description_options.mid) {
  1909. RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
  1910. const VideoContentDescription* vcd =
  1911. current_content->media_description()->as_video();
  1912. for (const VideoCodec& codec : vcd->codecs()) {
  1913. if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
  1914. nullptr)) {
  1915. filtered_codecs.push_back(codec);
  1916. }
  1917. }
  1918. }
  1919. // Add other supported video codecs.
  1920. VideoCodec found_codec;
  1921. for (const VideoCodec& codec : video_codecs_) {
  1922. if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
  1923. &found_codec) &&
  1924. !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
  1925. nullptr)) {
  1926. // Use the |found_codec| from |video_codecs| because it has the
  1927. // correctly mapped payload type.
  1928. filtered_codecs.push_back(found_codec);
  1929. }
  1930. }
  1931. }
  1932. if (session_options.raw_packetization_for_video) {
  1933. for (VideoCodec& codec : filtered_codecs) {
  1934. if (codec.GetCodecType() == VideoCodec::CODEC_VIDEO) {
  1935. codec.packetization = kPacketizationParamRaw;
  1936. }
  1937. }
  1938. }
  1939. if (!CreateMediaContentOffer(media_description_options, session_options,
  1940. filtered_codecs, sdes_policy,
  1941. GetCryptos(current_content), crypto_suites,
  1942. video_rtp_extensions, ssrc_generator_,
  1943. current_streams, video.get())) {
  1944. return false;
  1945. }
  1946. video->set_bandwidth(kAutoBandwidth);
  1947. bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
  1948. SetMediaProtocol(secure_transport, video.get());
  1949. video->set_direction(media_description_options.direction);
  1950. desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
  1951. media_description_options.stopped, std::move(video));
  1952. if (!AddTransportOffer(media_description_options.mid,
  1953. media_description_options.transport_options,
  1954. current_description, desc, ice_credentials)) {
  1955. return false;
  1956. }
  1957. return true;
  1958. }
  1959. bool MediaSessionDescriptionFactory::AddSctpDataContentForOffer(
  1960. const MediaDescriptionOptions& media_description_options,
  1961. const MediaSessionOptions& session_options,
  1962. const ContentInfo* current_content,
  1963. const SessionDescription* current_description,
  1964. StreamParamsVec* current_streams,
  1965. SessionDescription* desc,
  1966. IceCredentialsIterator* ice_credentials) const {
  1967. std::unique_ptr<SctpDataContentDescription> data(
  1968. new SctpDataContentDescription());
  1969. bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
  1970. cricket::SecurePolicy sdes_policy =
  1971. IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
  1972. : secure();
  1973. std::vector<std::string> crypto_suites;
  1974. // SDES doesn't make sense for SCTP, so we disable it, and we only
  1975. // get SDES crypto suites for RTP-based data channels.
  1976. sdes_policy = cricket::SEC_DISABLED;
  1977. // Unlike SetMediaProtocol below, we need to set the protocol
  1978. // before we call CreateMediaContentOffer. Otherwise,
  1979. // CreateMediaContentOffer won't know this is SCTP and will
  1980. // generate SSRCs rather than SIDs.
  1981. data->set_protocol(secure_transport ? kMediaProtocolUdpDtlsSctp
  1982. : kMediaProtocolSctp);
  1983. data->set_use_sctpmap(session_options.use_obsolete_sctp_sdp);
  1984. data->set_max_message_size(kSctpSendBufferSize);
  1985. if (!CreateContentOffer(media_description_options, session_options,
  1986. sdes_policy, GetCryptos(current_content),
  1987. crypto_suites, RtpHeaderExtensions(), ssrc_generator_,
  1988. current_streams, data.get())) {
  1989. return false;
  1990. }
  1991. desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp,
  1992. std::move(data));
  1993. if (!AddTransportOffer(media_description_options.mid,
  1994. media_description_options.transport_options,
  1995. current_description, desc, ice_credentials)) {
  1996. return false;
  1997. }
  1998. return true;
  1999. }
  2000. bool MediaSessionDescriptionFactory::AddRtpDataContentForOffer(
  2001. const MediaDescriptionOptions& media_description_options,
  2002. const MediaSessionOptions& session_options,
  2003. const ContentInfo* current_content,
  2004. const SessionDescription* current_description,
  2005. const RtpDataCodecs& rtp_data_codecs,
  2006. StreamParamsVec* current_streams,
  2007. SessionDescription* desc,
  2008. IceCredentialsIterator* ice_credentials) const {
  2009. std::unique_ptr<RtpDataContentDescription> data(
  2010. new RtpDataContentDescription());
  2011. bool secure_transport = (transport_desc_factory_->secure() != SEC_DISABLED);
  2012. cricket::SecurePolicy sdes_policy =
  2013. IsDtlsActive(current_content, current_description) ? cricket::SEC_DISABLED
  2014. : secure();
  2015. std::vector<std::string> crypto_suites;
  2016. GetSupportedDataSdesCryptoSuiteNames(session_options.crypto_options,
  2017. &crypto_suites);
  2018. if (!CreateMediaContentOffer(media_description_options, session_options,
  2019. rtp_data_codecs, sdes_policy,
  2020. GetCryptos(current_content), crypto_suites,
  2021. RtpHeaderExtensions(), ssrc_generator_,
  2022. current_streams, data.get())) {
  2023. return false;
  2024. }
  2025. data->set_bandwidth(kDataMaxBandwidth);
  2026. SetMediaProtocol(secure_transport, data.get());
  2027. desc->AddContent(media_description_options.mid, MediaProtocolType::kRtp,
  2028. media_description_options.stopped, std::move(data));
  2029. if (!AddTransportOffer(media_description_options.mid,
  2030. media_description_options.transport_options,
  2031. current_description, desc, ice_credentials)) {
  2032. return false;
  2033. }
  2034. return true;
  2035. }
  2036. bool MediaSessionDescriptionFactory::AddDataContentForOffer(
  2037. const MediaDescriptionOptions& media_description_options,
  2038. const MediaSessionOptions& session_options,
  2039. const ContentInfo* current_content,
  2040. const SessionDescription* current_description,
  2041. const RtpDataCodecs& rtp_data_codecs,
  2042. StreamParamsVec* current_streams,
  2043. SessionDescription* desc,
  2044. IceCredentialsIterator* ice_credentials) const {
  2045. bool is_sctp =
  2046. (session_options.data_channel_type == DCT_SCTP ||
  2047. session_options.data_channel_type == DCT_DATA_CHANNEL_TRANSPORT_SCTP);
  2048. // If the DataChannel type is not specified, use the DataChannel type in
  2049. // the current description.
  2050. if (session_options.data_channel_type == DCT_NONE && current_content) {
  2051. RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_DATA));
  2052. is_sctp = (current_content->media_description()->protocol() ==
  2053. kMediaProtocolSctp);
  2054. }
  2055. if (is_sctp) {
  2056. return AddSctpDataContentForOffer(
  2057. media_description_options, session_options, current_content,
  2058. current_description, current_streams, desc, ice_credentials);
  2059. } else {
  2060. return AddRtpDataContentForOffer(media_description_options, session_options,
  2061. current_content, current_description,
  2062. rtp_data_codecs, current_streams, desc,
  2063. ice_credentials);
  2064. }
  2065. }
  2066. // |audio_codecs| = set of all possible codecs that can be used, with correct
  2067. // payload type mappings
  2068. //
  2069. // |supported_audio_codecs| = set of codecs that are supported for the direction
  2070. // of this m= section
  2071. //
  2072. // acd->codecs() = set of previously negotiated codecs for this m= section
  2073. //
  2074. // The payload types should come from audio_codecs, but the order should come
  2075. // from acd->codecs() and then supported_codecs, to ensure that re-offers don't
  2076. // change existing codec priority, and that new codecs are added with the right
  2077. // priority.
  2078. bool MediaSessionDescriptionFactory::AddAudioContentForAnswer(
  2079. const MediaDescriptionOptions& media_description_options,
  2080. const MediaSessionOptions& session_options,
  2081. const ContentInfo* offer_content,
  2082. const SessionDescription* offer_description,
  2083. const ContentInfo* current_content,
  2084. const SessionDescription* current_description,
  2085. const TransportInfo* bundle_transport,
  2086. const AudioCodecs& audio_codecs,
  2087. StreamParamsVec* current_streams,
  2088. SessionDescription* answer,
  2089. IceCredentialsIterator* ice_credentials) const {
  2090. RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_AUDIO));
  2091. const AudioContentDescription* offer_audio_description =
  2092. offer_content->media_description()->as_audio();
  2093. std::unique_ptr<TransportDescription> audio_transport = CreateTransportAnswer(
  2094. media_description_options.mid, offer_description,
  2095. media_description_options.transport_options, current_description,
  2096. bundle_transport != nullptr, ice_credentials);
  2097. if (!audio_transport) {
  2098. return false;
  2099. }
  2100. // Pick codecs based on the requested communications direction in the offer
  2101. // and the selected direction in the answer.
  2102. // Note these will be filtered one final time in CreateMediaContentAnswer.
  2103. auto wants_rtd = media_description_options.direction;
  2104. auto offer_rtd = offer_audio_description->direction();
  2105. auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd);
  2106. AudioCodecs supported_audio_codecs =
  2107. GetAudioCodecsForAnswer(offer_rtd, answer_rtd);
  2108. AudioCodecs filtered_codecs;
  2109. if (!media_description_options.codec_preferences.empty()) {
  2110. filtered_codecs = MatchCodecPreference(
  2111. media_description_options.codec_preferences, supported_audio_codecs);
  2112. } else {
  2113. // Add the codecs from current content if it exists and is not rejected nor
  2114. // recycled.
  2115. if (current_content && !current_content->rejected &&
  2116. current_content->name == media_description_options.mid) {
  2117. RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_AUDIO));
  2118. const AudioContentDescription* acd =
  2119. current_content->media_description()->as_audio();
  2120. for (const AudioCodec& codec : acd->codecs()) {
  2121. if (FindMatchingCodec<AudioCodec>(acd->codecs(), audio_codecs, codec,
  2122. nullptr)) {
  2123. filtered_codecs.push_back(codec);
  2124. }
  2125. }
  2126. }
  2127. // Add other supported audio codecs.
  2128. for (const AudioCodec& codec : supported_audio_codecs) {
  2129. if (FindMatchingCodec<AudioCodec>(supported_audio_codecs, audio_codecs,
  2130. codec, nullptr) &&
  2131. !FindMatchingCodec<AudioCodec>(supported_audio_codecs,
  2132. filtered_codecs, codec, nullptr)) {
  2133. // We should use the local codec with local parameters and the codec id
  2134. // would be correctly mapped in |NegotiateCodecs|.
  2135. filtered_codecs.push_back(codec);
  2136. }
  2137. }
  2138. }
  2139. bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
  2140. session_options.bundle_enabled;
  2141. std::unique_ptr<AudioContentDescription> audio_answer(
  2142. new AudioContentDescription());
  2143. // Do not require or create SDES cryptos if DTLS is used.
  2144. cricket::SecurePolicy sdes_policy =
  2145. audio_transport->secure() ? cricket::SEC_DISABLED : secure();
  2146. if (!SetCodecsInAnswer(offer_audio_description, filtered_codecs,
  2147. media_description_options, session_options,
  2148. ssrc_generator_, current_streams,
  2149. audio_answer.get())) {
  2150. return false;
  2151. }
  2152. if (!CreateMediaContentAnswer(
  2153. offer_audio_description, media_description_options, session_options,
  2154. sdes_policy, GetCryptos(current_content),
  2155. audio_rtp_header_extensions(), ssrc_generator_,
  2156. enable_encrypted_rtp_header_extensions_, current_streams,
  2157. bundle_enabled, audio_answer.get())) {
  2158. return false; // Fails the session setup.
  2159. }
  2160. bool secure = bundle_transport ? bundle_transport->description.secure()
  2161. : audio_transport->secure();
  2162. bool rejected = media_description_options.stopped ||
  2163. offer_content->rejected ||
  2164. !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO,
  2165. audio_answer->protocol(), secure);
  2166. if (!AddTransportAnswer(media_description_options.mid,
  2167. *(audio_transport.get()), answer)) {
  2168. return false;
  2169. }
  2170. if (rejected) {
  2171. RTC_LOG(LS_INFO) << "Audio m= section '" << media_description_options.mid
  2172. << "' being rejected in answer.";
  2173. }
  2174. answer->AddContent(media_description_options.mid, offer_content->type,
  2175. rejected, std::move(audio_answer));
  2176. return true;
  2177. }
  2178. bool MediaSessionDescriptionFactory::AddVideoContentForAnswer(
  2179. const MediaDescriptionOptions& media_description_options,
  2180. const MediaSessionOptions& session_options,
  2181. const ContentInfo* offer_content,
  2182. const SessionDescription* offer_description,
  2183. const ContentInfo* current_content,
  2184. const SessionDescription* current_description,
  2185. const TransportInfo* bundle_transport,
  2186. const VideoCodecs& video_codecs,
  2187. StreamParamsVec* current_streams,
  2188. SessionDescription* answer,
  2189. IceCredentialsIterator* ice_credentials) const {
  2190. RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_VIDEO));
  2191. const VideoContentDescription* offer_video_description =
  2192. offer_content->media_description()->as_video();
  2193. std::unique_ptr<TransportDescription> video_transport = CreateTransportAnswer(
  2194. media_description_options.mid, offer_description,
  2195. media_description_options.transport_options, current_description,
  2196. bundle_transport != nullptr, ice_credentials);
  2197. if (!video_transport) {
  2198. return false;
  2199. }
  2200. VideoCodecs filtered_codecs;
  2201. if (!media_description_options.codec_preferences.empty()) {
  2202. filtered_codecs = MatchCodecPreference(
  2203. media_description_options.codec_preferences, video_codecs_);
  2204. } else {
  2205. // Add the codecs from current content if it exists and is not rejected nor
  2206. // recycled.
  2207. if (current_content && !current_content->rejected &&
  2208. current_content->name == media_description_options.mid) {
  2209. RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_VIDEO));
  2210. const VideoContentDescription* vcd =
  2211. current_content->media_description()->as_video();
  2212. for (const VideoCodec& codec : vcd->codecs()) {
  2213. if (FindMatchingCodec<VideoCodec>(vcd->codecs(), video_codecs, codec,
  2214. nullptr)) {
  2215. filtered_codecs.push_back(codec);
  2216. }
  2217. }
  2218. }
  2219. // Add other supported video codecs.
  2220. for (const VideoCodec& codec : video_codecs_) {
  2221. if (FindMatchingCodec<VideoCodec>(video_codecs_, video_codecs, codec,
  2222. nullptr) &&
  2223. !FindMatchingCodec<VideoCodec>(video_codecs_, filtered_codecs, codec,
  2224. nullptr)) {
  2225. // We should use the local codec with local parameters and the codec id
  2226. // would be correctly mapped in |NegotiateCodecs|.
  2227. filtered_codecs.push_back(codec);
  2228. }
  2229. }
  2230. }
  2231. if (session_options.raw_packetization_for_video) {
  2232. for (VideoCodec& codec : filtered_codecs) {
  2233. if (codec.GetCodecType() == VideoCodec::CODEC_VIDEO) {
  2234. codec.packetization = kPacketizationParamRaw;
  2235. }
  2236. }
  2237. }
  2238. bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
  2239. session_options.bundle_enabled;
  2240. std::unique_ptr<VideoContentDescription> video_answer(
  2241. new VideoContentDescription());
  2242. // Do not require or create SDES cryptos if DTLS is used.
  2243. cricket::SecurePolicy sdes_policy =
  2244. video_transport->secure() ? cricket::SEC_DISABLED : secure();
  2245. if (!SetCodecsInAnswer(offer_video_description, filtered_codecs,
  2246. media_description_options, session_options,
  2247. ssrc_generator_, current_streams,
  2248. video_answer.get())) {
  2249. return false;
  2250. }
  2251. if (!CreateMediaContentAnswer(
  2252. offer_video_description, media_description_options, session_options,
  2253. sdes_policy, GetCryptos(current_content),
  2254. video_rtp_header_extensions(), ssrc_generator_,
  2255. enable_encrypted_rtp_header_extensions_, current_streams,
  2256. bundle_enabled, video_answer.get())) {
  2257. return false; // Failed the sessin setup.
  2258. }
  2259. bool secure = bundle_transport ? bundle_transport->description.secure()
  2260. : video_transport->secure();
  2261. bool rejected = media_description_options.stopped ||
  2262. offer_content->rejected ||
  2263. !IsMediaProtocolSupported(MEDIA_TYPE_VIDEO,
  2264. video_answer->protocol(), secure);
  2265. if (!AddTransportAnswer(media_description_options.mid,
  2266. *(video_transport.get()), answer)) {
  2267. return false;
  2268. }
  2269. if (!rejected) {
  2270. video_answer->set_bandwidth(kAutoBandwidth);
  2271. } else {
  2272. RTC_LOG(LS_INFO) << "Video m= section '" << media_description_options.mid
  2273. << "' being rejected in answer.";
  2274. }
  2275. answer->AddContent(media_description_options.mid, offer_content->type,
  2276. rejected, std::move(video_answer));
  2277. return true;
  2278. }
  2279. bool MediaSessionDescriptionFactory::AddDataContentForAnswer(
  2280. const MediaDescriptionOptions& media_description_options,
  2281. const MediaSessionOptions& session_options,
  2282. const ContentInfo* offer_content,
  2283. const SessionDescription* offer_description,
  2284. const ContentInfo* current_content,
  2285. const SessionDescription* current_description,
  2286. const TransportInfo* bundle_transport,
  2287. const RtpDataCodecs& rtp_data_codecs,
  2288. StreamParamsVec* current_streams,
  2289. SessionDescription* answer,
  2290. IceCredentialsIterator* ice_credentials) const {
  2291. std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer(
  2292. media_description_options.mid, offer_description,
  2293. media_description_options.transport_options, current_description,
  2294. bundle_transport != nullptr, ice_credentials);
  2295. if (!data_transport) {
  2296. return false;
  2297. }
  2298. // Do not require or create SDES cryptos if DTLS is used.
  2299. cricket::SecurePolicy sdes_policy =
  2300. data_transport->secure() ? cricket::SEC_DISABLED : secure();
  2301. bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) &&
  2302. session_options.bundle_enabled;
  2303. RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA));
  2304. std::unique_ptr<MediaContentDescription> data_answer;
  2305. if (offer_content->media_description()->as_sctp()) {
  2306. // SCTP data content
  2307. data_answer = std::make_unique<SctpDataContentDescription>();
  2308. const SctpDataContentDescription* offer_data_description =
  2309. offer_content->media_description()->as_sctp();
  2310. // Respond with the offerer's proto, whatever it is.
  2311. data_answer->as_sctp()->set_protocol(offer_data_description->protocol());
  2312. // Respond with our max message size or the remote max messsage size,
  2313. // whichever is smaller.
  2314. // 0 is treated specially - it means "I can accept any size". Since
  2315. // we do not implement infinite size messages, reply with
  2316. // kSctpSendBufferSize.
  2317. if (offer_data_description->max_message_size() == 0) {
  2318. data_answer->as_sctp()->set_max_message_size(kSctpSendBufferSize);
  2319. } else {
  2320. data_answer->as_sctp()->set_max_message_size(std::min(
  2321. offer_data_description->max_message_size(), kSctpSendBufferSize));
  2322. }
  2323. if (!CreateMediaContentAnswer(
  2324. offer_data_description, media_description_options, session_options,
  2325. sdes_policy, GetCryptos(current_content), RtpHeaderExtensions(),
  2326. ssrc_generator_, enable_encrypted_rtp_header_extensions_,
  2327. current_streams, bundle_enabled, data_answer.get())) {
  2328. return false; // Fails the session setup.
  2329. }
  2330. // Respond with sctpmap if the offer uses sctpmap.
  2331. bool offer_uses_sctpmap = offer_data_description->use_sctpmap();
  2332. data_answer->as_sctp()->set_use_sctpmap(offer_uses_sctpmap);
  2333. } else {
  2334. // RTP offer
  2335. data_answer = std::make_unique<RtpDataContentDescription>();
  2336. const RtpDataContentDescription* offer_data_description =
  2337. offer_content->media_description()->as_rtp_data();
  2338. RTC_CHECK(offer_data_description);
  2339. if (!SetCodecsInAnswer(offer_data_description, rtp_data_codecs,
  2340. media_description_options, session_options,
  2341. ssrc_generator_, current_streams,
  2342. data_answer->as_rtp_data())) {
  2343. return false;
  2344. }
  2345. if (!CreateMediaContentAnswer(
  2346. offer_data_description, media_description_options, session_options,
  2347. sdes_policy, GetCryptos(current_content), RtpHeaderExtensions(),
  2348. ssrc_generator_, enable_encrypted_rtp_header_extensions_,
  2349. current_streams, bundle_enabled, data_answer.get())) {
  2350. return false; // Fails the session setup.
  2351. }
  2352. }
  2353. bool secure = bundle_transport ? bundle_transport->description.secure()
  2354. : data_transport->secure();
  2355. bool rejected = session_options.data_channel_type == DCT_NONE ||
  2356. media_description_options.stopped ||
  2357. offer_content->rejected ||
  2358. !IsMediaProtocolSupported(MEDIA_TYPE_DATA,
  2359. data_answer->protocol(), secure);
  2360. if (!AddTransportAnswer(media_description_options.mid,
  2361. *(data_transport.get()), answer)) {
  2362. return false;
  2363. }
  2364. if (!rejected) {
  2365. data_answer->set_bandwidth(kDataMaxBandwidth);
  2366. } else {
  2367. // RFC 3264
  2368. // The answer MUST contain the same number of m-lines as the offer.
  2369. RTC_LOG(LS_INFO) << "Data is not supported in the answer.";
  2370. }
  2371. answer->AddContent(media_description_options.mid, offer_content->type,
  2372. rejected, std::move(data_answer));
  2373. return true;
  2374. }
  2375. void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() {
  2376. audio_sendrecv_codecs_.clear();
  2377. all_audio_codecs_.clear();
  2378. // Compute the audio codecs union.
  2379. for (const AudioCodec& send : audio_send_codecs_) {
  2380. all_audio_codecs_.push_back(send);
  2381. if (!FindMatchingCodec<AudioCodec>(audio_send_codecs_, audio_recv_codecs_,
  2382. send, nullptr)) {
  2383. // It doesn't make sense to have an RTX codec we support sending but not
  2384. // receiving.
  2385. RTC_DCHECK(!IsRtxCodec(send));
  2386. }
  2387. }
  2388. for (const AudioCodec& recv : audio_recv_codecs_) {
  2389. if (!FindMatchingCodec<AudioCodec>(audio_recv_codecs_, audio_send_codecs_,
  2390. recv, nullptr)) {
  2391. all_audio_codecs_.push_back(recv);
  2392. }
  2393. }
  2394. // Use NegotiateCodecs to merge our codec lists, since the operation is
  2395. // essentially the same. Put send_codecs as the offered_codecs, which is the
  2396. // order we'd like to follow. The reasoning is that encoding is usually more
  2397. // expensive than decoding, and prioritizing a codec in the send list probably
  2398. // means it's a codec we can handle efficiently.
  2399. NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_,
  2400. &audio_sendrecv_codecs_, true);
  2401. }
  2402. bool IsMediaContent(const ContentInfo* content) {
  2403. return (content && (content->type == MediaProtocolType::kRtp ||
  2404. content->type == MediaProtocolType::kSctp));
  2405. }
  2406. bool IsAudioContent(const ContentInfo* content) {
  2407. return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO);
  2408. }
  2409. bool IsVideoContent(const ContentInfo* content) {
  2410. return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO);
  2411. }
  2412. bool IsDataContent(const ContentInfo* content) {
  2413. return IsMediaContentOfType(content, MEDIA_TYPE_DATA);
  2414. }
  2415. const ContentInfo* GetFirstMediaContent(const ContentInfos& contents,
  2416. MediaType media_type) {
  2417. for (const ContentInfo& content : contents) {
  2418. if (IsMediaContentOfType(&content, media_type)) {
  2419. return &content;
  2420. }
  2421. }
  2422. return nullptr;
  2423. }
  2424. const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) {
  2425. return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
  2426. }
  2427. const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) {
  2428. return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
  2429. }
  2430. const ContentInfo* GetFirstDataContent(const ContentInfos& contents) {
  2431. return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
  2432. }
  2433. const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc,
  2434. MediaType media_type) {
  2435. if (sdesc == nullptr) {
  2436. return nullptr;
  2437. }
  2438. return GetFirstMediaContent(sdesc->contents(), media_type);
  2439. }
  2440. const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) {
  2441. return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
  2442. }
  2443. const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) {
  2444. return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
  2445. }
  2446. const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) {
  2447. return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
  2448. }
  2449. const MediaContentDescription* GetFirstMediaContentDescription(
  2450. const SessionDescription* sdesc,
  2451. MediaType media_type) {
  2452. const ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
  2453. return (content ? content->media_description() : nullptr);
  2454. }
  2455. const AudioContentDescription* GetFirstAudioContentDescription(
  2456. const SessionDescription* sdesc) {
  2457. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO);
  2458. return desc ? desc->as_audio() : nullptr;
  2459. }
  2460. const VideoContentDescription* GetFirstVideoContentDescription(
  2461. const SessionDescription* sdesc) {
  2462. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO);
  2463. return desc ? desc->as_video() : nullptr;
  2464. }
  2465. const RtpDataContentDescription* GetFirstRtpDataContentDescription(
  2466. const SessionDescription* sdesc) {
  2467. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
  2468. return desc ? desc->as_rtp_data() : nullptr;
  2469. }
  2470. const SctpDataContentDescription* GetFirstSctpDataContentDescription(
  2471. const SessionDescription* sdesc) {
  2472. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
  2473. return desc ? desc->as_sctp() : nullptr;
  2474. }
  2475. //
  2476. // Non-const versions of the above functions.
  2477. //
  2478. ContentInfo* GetFirstMediaContent(ContentInfos* contents,
  2479. MediaType media_type) {
  2480. for (ContentInfo& content : *contents) {
  2481. if (IsMediaContentOfType(&content, media_type)) {
  2482. return &content;
  2483. }
  2484. }
  2485. return nullptr;
  2486. }
  2487. ContentInfo* GetFirstAudioContent(ContentInfos* contents) {
  2488. return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO);
  2489. }
  2490. ContentInfo* GetFirstVideoContent(ContentInfos* contents) {
  2491. return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO);
  2492. }
  2493. ContentInfo* GetFirstDataContent(ContentInfos* contents) {
  2494. return GetFirstMediaContent(contents, MEDIA_TYPE_DATA);
  2495. }
  2496. ContentInfo* GetFirstMediaContent(SessionDescription* sdesc,
  2497. MediaType media_type) {
  2498. if (sdesc == nullptr) {
  2499. return nullptr;
  2500. }
  2501. return GetFirstMediaContent(&sdesc->contents(), media_type);
  2502. }
  2503. ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) {
  2504. return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO);
  2505. }
  2506. ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) {
  2507. return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO);
  2508. }
  2509. ContentInfo* GetFirstDataContent(SessionDescription* sdesc) {
  2510. return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA);
  2511. }
  2512. MediaContentDescription* GetFirstMediaContentDescription(
  2513. SessionDescription* sdesc,
  2514. MediaType media_type) {
  2515. ContentInfo* content = GetFirstMediaContent(sdesc, media_type);
  2516. return (content ? content->media_description() : nullptr);
  2517. }
  2518. AudioContentDescription* GetFirstAudioContentDescription(
  2519. SessionDescription* sdesc) {
  2520. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO);
  2521. return desc ? desc->as_audio() : nullptr;
  2522. }
  2523. VideoContentDescription* GetFirstVideoContentDescription(
  2524. SessionDescription* sdesc) {
  2525. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO);
  2526. return desc ? desc->as_video() : nullptr;
  2527. }
  2528. RtpDataContentDescription* GetFirstRtpDataContentDescription(
  2529. SessionDescription* sdesc) {
  2530. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
  2531. return desc ? desc->as_rtp_data() : nullptr;
  2532. }
  2533. SctpDataContentDescription* GetFirstSctpDataContentDescription(
  2534. SessionDescription* sdesc) {
  2535. auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA);
  2536. return desc ? desc->as_sctp() : nullptr;
  2537. }
  2538. } // namespace cricket