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.

356 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2017 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/rtp_transceiver.h"
  11. #include <string>
  12. #include "absl/algorithm/container.h"
  13. #include "pc/channel_manager.h"
  14. #include "pc/rtp_media_utils.h"
  15. #include "pc/rtp_parameters_conversion.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/logging.h"
  18. namespace webrtc {
  19. RtpTransceiver::RtpTransceiver(cricket::MediaType media_type)
  20. : unified_plan_(false), media_type_(media_type) {
  21. RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
  22. media_type == cricket::MEDIA_TYPE_VIDEO);
  23. }
  24. RtpTransceiver::RtpTransceiver(
  25. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
  26. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  27. receiver,
  28. cricket::ChannelManager* channel_manager)
  29. : unified_plan_(true),
  30. media_type_(sender->media_type()),
  31. channel_manager_(channel_manager) {
  32. RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
  33. media_type_ == cricket::MEDIA_TYPE_VIDEO);
  34. RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
  35. senders_.push_back(sender);
  36. receivers_.push_back(receiver);
  37. }
  38. RtpTransceiver::~RtpTransceiver() {
  39. Stop();
  40. }
  41. void RtpTransceiver::SetChannel(cricket::ChannelInterface* channel) {
  42. // Cannot set a non-null channel on a stopped transceiver.
  43. if (stopped_ && channel) {
  44. return;
  45. }
  46. if (channel) {
  47. RTC_DCHECK_EQ(media_type(), channel->media_type());
  48. }
  49. if (channel_) {
  50. channel_->SignalFirstPacketReceived().disconnect(this);
  51. }
  52. channel_ = channel;
  53. if (channel_) {
  54. channel_->SignalFirstPacketReceived().connect(
  55. this, &RtpTransceiver::OnFirstPacketReceived);
  56. }
  57. for (const auto& sender : senders_) {
  58. sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
  59. : nullptr);
  60. }
  61. for (const auto& receiver : receivers_) {
  62. if (!channel_) {
  63. receiver->internal()->Stop();
  64. }
  65. receiver->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
  66. : nullptr);
  67. }
  68. }
  69. void RtpTransceiver::AddSender(
  70. rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
  71. RTC_DCHECK(!stopped_);
  72. RTC_DCHECK(!unified_plan_);
  73. RTC_DCHECK(sender);
  74. RTC_DCHECK_EQ(media_type(), sender->media_type());
  75. RTC_DCHECK(!absl::c_linear_search(senders_, sender));
  76. senders_.push_back(sender);
  77. }
  78. bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
  79. RTC_DCHECK(!unified_plan_);
  80. if (sender) {
  81. RTC_DCHECK_EQ(media_type(), sender->media_type());
  82. }
  83. auto it = absl::c_find(senders_, sender);
  84. if (it == senders_.end()) {
  85. return false;
  86. }
  87. (*it)->internal()->Stop();
  88. senders_.erase(it);
  89. return true;
  90. }
  91. void RtpTransceiver::AddReceiver(
  92. rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
  93. receiver) {
  94. RTC_DCHECK(!stopped_);
  95. RTC_DCHECK(!unified_plan_);
  96. RTC_DCHECK(receiver);
  97. RTC_DCHECK_EQ(media_type(), receiver->media_type());
  98. RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
  99. receivers_.push_back(receiver);
  100. }
  101. bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
  102. RTC_DCHECK(!unified_plan_);
  103. if (receiver) {
  104. RTC_DCHECK_EQ(media_type(), receiver->media_type());
  105. }
  106. auto it = absl::c_find(receivers_, receiver);
  107. if (it == receivers_.end()) {
  108. return false;
  109. }
  110. (*it)->internal()->Stop();
  111. receivers_.erase(it);
  112. return true;
  113. }
  114. rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
  115. RTC_DCHECK(unified_plan_);
  116. RTC_CHECK_EQ(1u, senders_.size());
  117. return senders_[0]->internal();
  118. }
  119. rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
  120. const {
  121. RTC_DCHECK(unified_plan_);
  122. RTC_CHECK_EQ(1u, receivers_.size());
  123. return receivers_[0]->internal();
  124. }
  125. cricket::MediaType RtpTransceiver::media_type() const {
  126. return media_type_;
  127. }
  128. absl::optional<std::string> RtpTransceiver::mid() const {
  129. return mid_;
  130. }
  131. void RtpTransceiver::OnFirstPacketReceived(cricket::ChannelInterface*) {
  132. for (const auto& receiver : receivers_) {
  133. receiver->internal()->NotifyFirstPacketReceived();
  134. }
  135. }
  136. rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
  137. RTC_DCHECK(unified_plan_);
  138. RTC_CHECK_EQ(1u, senders_.size());
  139. return senders_[0];
  140. }
  141. rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
  142. RTC_DCHECK(unified_plan_);
  143. RTC_CHECK_EQ(1u, receivers_.size());
  144. return receivers_[0];
  145. }
  146. void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
  147. RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
  148. << ") current direction from "
  149. << (current_direction_ ? RtpTransceiverDirectionToString(
  150. *current_direction_)
  151. : "<not set>")
  152. << " to " << RtpTransceiverDirectionToString(direction)
  153. << ".";
  154. current_direction_ = direction;
  155. if (RtpTransceiverDirectionHasSend(*current_direction_)) {
  156. has_ever_been_used_to_send_ = true;
  157. }
  158. }
  159. void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) {
  160. fired_direction_ = direction;
  161. }
  162. bool RtpTransceiver::stopped() const {
  163. return stopped_;
  164. }
  165. RtpTransceiverDirection RtpTransceiver::direction() const {
  166. return direction_;
  167. }
  168. void RtpTransceiver::SetDirection(RtpTransceiverDirection new_direction) {
  169. if (stopped()) {
  170. return;
  171. }
  172. if (new_direction == direction_) {
  173. return;
  174. }
  175. direction_ = new_direction;
  176. SignalNegotiationNeeded();
  177. }
  178. absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
  179. const {
  180. return current_direction_;
  181. }
  182. absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
  183. const {
  184. return fired_direction_;
  185. }
  186. void RtpTransceiver::Stop() {
  187. for (const auto& sender : senders_) {
  188. sender->internal()->Stop();
  189. }
  190. for (const auto& receiver : receivers_) {
  191. receiver->internal()->Stop();
  192. }
  193. stopped_ = true;
  194. current_direction_ = absl::nullopt;
  195. }
  196. RTCError RtpTransceiver::SetCodecPreferences(
  197. rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
  198. RTC_DCHECK(unified_plan_);
  199. // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
  200. // to codecs and abort these steps.
  201. if (codec_capabilities.empty()) {
  202. codec_preferences_.clear();
  203. return RTCError::OK();
  204. }
  205. // 4. Remove any duplicate values in codecs.
  206. std::vector<RtpCodecCapability> codecs;
  207. absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
  208. [&codecs](const RtpCodecCapability& codec) {
  209. return absl::c_linear_search(codecs, codec);
  210. });
  211. if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
  212. std::vector<cricket::AudioCodec> audio_codecs;
  213. std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
  214. channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
  215. channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
  216. // 6. If the intersection between codecs and
  217. // RTCRtpSender.getCapabilities(kind).codecs or the intersection between
  218. // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX,
  219. // RED or FEC codecs or is an empty set, throw InvalidModificationError.
  220. // This ensures that we always have something to offer, regardless of
  221. // transceiver.direction.
  222. if (!absl::c_any_of(
  223. codecs, [&recv_codecs](const RtpCodecCapability& codec) {
  224. return codec.name != cricket::kRtxCodecName &&
  225. codec.name != cricket::kRedCodecName &&
  226. codec.name != cricket::kFlexfecCodecName &&
  227. absl::c_any_of(
  228. recv_codecs,
  229. [&codec](const cricket::AudioCodec& recv_codec) {
  230. return recv_codec.MatchesCapability(codec);
  231. });
  232. })) {
  233. return RTCError(RTCErrorType::INVALID_MODIFICATION,
  234. "Invalid codec preferences: Missing codec from recv "
  235. "codec capabilities.");
  236. }
  237. if (!absl::c_any_of(
  238. codecs, [&send_codecs](const RtpCodecCapability& codec) {
  239. return codec.name != cricket::kRtxCodecName &&
  240. codec.name != cricket::kRedCodecName &&
  241. codec.name != cricket::kFlexfecCodecName &&
  242. absl::c_any_of(
  243. send_codecs,
  244. [&codec](const cricket::AudioCodec& send_codec) {
  245. return send_codec.MatchesCapability(codec);
  246. });
  247. })) {
  248. return RTCError(RTCErrorType::INVALID_MODIFICATION,
  249. "Invalid codec preferences: Missing codec from send "
  250. "codec capabilities.");
  251. }
  252. // 7. Let codecCapabilities be the union of
  253. // RTCRtpSender.getCapabilities(kind).codecs and
  254. // RTCRtpReceiver.getCapabilities(kind).codecs. 8.1 For each codec in
  255. // codecs, If codec is not in codecCapabilities, throw
  256. // InvalidModificationError.
  257. for (const auto& codec_preference : codecs) {
  258. bool is_recv_codec = absl::c_any_of(
  259. recv_codecs, [&codec_preference](const cricket::AudioCodec& codec) {
  260. return codec.MatchesCapability(codec_preference);
  261. });
  262. bool is_send_codec = absl::c_any_of(
  263. send_codecs, [&codec_preference](const cricket::AudioCodec& codec) {
  264. return codec.MatchesCapability(codec_preference);
  265. });
  266. if (!is_recv_codec && !is_send_codec) {
  267. return RTCError(
  268. RTCErrorType::INVALID_MODIFICATION,
  269. std::string(
  270. "Invalid codec preferences: invalid codec with name \"") +
  271. codec_preference.name + "\".");
  272. }
  273. }
  274. } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
  275. std::vector<cricket::VideoCodec> video_codecs;
  276. // Video codecs are both for the receive and send side, so the checks are
  277. // simpler than the audio ones.
  278. channel_manager_->GetSupportedVideoCodecs(&video_codecs);
  279. // Validate codecs
  280. for (const auto& codec_preference : codecs) {
  281. if (!absl::c_any_of(video_codecs, [&codec_preference](
  282. const cricket::VideoCodec& codec) {
  283. return codec.MatchesCapability(codec_preference);
  284. })) {
  285. return RTCError(
  286. RTCErrorType::INVALID_MODIFICATION,
  287. std::string(
  288. "Invalid codec preferences: invalid codec with name \"") +
  289. codec_preference.name + "\".");
  290. }
  291. }
  292. }
  293. // Check we have a real codec (not just rtx, red or fec)
  294. if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) {
  295. return codec.name == cricket::kRtxCodecName ||
  296. codec.name == cricket::kRedCodecName ||
  297. codec.name == cricket::kUlpfecCodecName;
  298. })) {
  299. return RTCError(RTCErrorType::INVALID_MODIFICATION,
  300. "Invalid codec preferences: codec list must have a non "
  301. "RTX, RED or FEC entry.");
  302. }
  303. codec_preferences_ = codecs;
  304. return RTCError::OK();
  305. }
  306. } // namespace webrtc