Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/rtp_transceiver.h" |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 12 | |
| 13 | #include <string> |
| 14 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 15 | #include "absl/algorithm/container.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 16 | #include "pc/rtp_media_utils.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "rtc_base/logging.h" |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 19 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 20 | namespace webrtc { |
| 21 | |
| 22 | RtpTransceiver::RtpTransceiver(cricket::MediaType media_type) |
| 23 | : unified_plan_(false), media_type_(media_type) { |
| 24 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 25 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 26 | } |
| 27 | |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 28 | RtpTransceiver::RtpTransceiver( |
| 29 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 30 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 31 | receiver) |
| 32 | : unified_plan_(true), media_type_(sender->media_type()) { |
| 33 | RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO || |
| 34 | media_type_ == cricket::MEDIA_TYPE_VIDEO); |
| 35 | RTC_DCHECK_EQ(sender->media_type(), receiver->media_type()); |
| 36 | senders_.push_back(sender); |
| 37 | receivers_.push_back(receiver); |
| 38 | } |
| 39 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 40 | RtpTransceiver::~RtpTransceiver() { |
| 41 | Stop(); |
| 42 | } |
| 43 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 44 | void RtpTransceiver::SetChannel(cricket::ChannelInterface* channel) { |
| 45 | // Cannot set a non-null channel on a stopped transceiver. |
| 46 | if (stopped_ && channel) { |
| 47 | return; |
| 48 | } |
| 49 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 50 | if (channel) { |
| 51 | RTC_DCHECK_EQ(media_type(), channel->media_type()); |
| 52 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 53 | |
| 54 | if (channel_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 55 | channel_->SignalFirstPacketReceived().disconnect(this); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 58 | channel_ = channel; |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 59 | |
| 60 | if (channel_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 61 | channel_->SignalFirstPacketReceived().connect( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 62 | this, &RtpTransceiver::OnFirstPacketReceived); |
| 63 | } |
| 64 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 65 | for (const auto& sender : senders_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 66 | sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel() |
| 67 | : nullptr); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 68 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 69 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 70 | for (const auto& receiver : receivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 71 | if (!channel_) { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 72 | receiver->internal()->Stop(); |
| 73 | } |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 74 | |
| 75 | receiver->internal()->SetMediaChannel(channel_ ? channel_->media_channel() |
| 76 | : nullptr); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | void RtpTransceiver::AddSender( |
| 81 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 82 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 83 | RTC_DCHECK(!unified_plan_); |
| 84 | RTC_DCHECK(sender); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 85 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 86 | RTC_DCHECK(!absl::c_linear_search(senders_, sender)); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 87 | senders_.push_back(sender); |
| 88 | } |
| 89 | |
| 90 | bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) { |
| 91 | RTC_DCHECK(!unified_plan_); |
| 92 | if (sender) { |
| 93 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
| 94 | } |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 95 | auto it = absl::c_find(senders_, sender); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 96 | if (it == senders_.end()) { |
| 97 | return false; |
| 98 | } |
| 99 | (*it)->internal()->Stop(); |
| 100 | senders_.erase(it); |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | void RtpTransceiver::AddReceiver( |
| 105 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 106 | receiver) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 107 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 108 | RTC_DCHECK(!unified_plan_); |
| 109 | RTC_DCHECK(receiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 110 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 111 | RTC_DCHECK(!absl::c_linear_search(receivers_, receiver)); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 112 | receivers_.push_back(receiver); |
| 113 | } |
| 114 | |
| 115 | bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) { |
| 116 | RTC_DCHECK(!unified_plan_); |
| 117 | if (receiver) { |
| 118 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
| 119 | } |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 120 | auto it = absl::c_find(receivers_, receiver); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 121 | if (it == receivers_.end()) { |
| 122 | return false; |
| 123 | } |
| 124 | (*it)->internal()->Stop(); |
| 125 | receivers_.erase(it); |
| 126 | return true; |
| 127 | } |
| 128 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 129 | rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const { |
| 130 | RTC_DCHECK(unified_plan_); |
| 131 | RTC_CHECK_EQ(1u, senders_.size()); |
| 132 | return senders_[0]->internal(); |
| 133 | } |
| 134 | |
| 135 | rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal() |
| 136 | const { |
| 137 | RTC_DCHECK(unified_plan_); |
| 138 | RTC_CHECK_EQ(1u, receivers_.size()); |
| 139 | return receivers_[0]->internal(); |
| 140 | } |
| 141 | |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 142 | cricket::MediaType RtpTransceiver::media_type() const { |
| 143 | return media_type_; |
| 144 | } |
| 145 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 146 | absl::optional<std::string> RtpTransceiver::mid() const { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 147 | return mid_; |
| 148 | } |
| 149 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 150 | void RtpTransceiver::OnFirstPacketReceived(cricket::ChannelInterface*) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 151 | for (const auto& receiver : receivers_) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 152 | receiver->internal()->NotifyFirstPacketReceived(); |
| 153 | } |
| 154 | } |
| 155 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 156 | rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const { |
| 157 | RTC_DCHECK(unified_plan_); |
| 158 | RTC_CHECK_EQ(1u, senders_.size()); |
| 159 | return senders_[0]; |
| 160 | } |
| 161 | |
| 162 | rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const { |
| 163 | RTC_DCHECK(unified_plan_); |
| 164 | RTC_CHECK_EQ(1u, receivers_.size()); |
| 165 | return receivers_[0]; |
| 166 | } |
| 167 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 168 | void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 169 | RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>") |
| 170 | << ") current direction from " |
| 171 | << (current_direction_ ? RtpTransceiverDirectionToString( |
| 172 | *current_direction_) |
| 173 | : "<not set>") |
| 174 | << " to " << RtpTransceiverDirectionToString(direction) |
| 175 | << "."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 176 | current_direction_ = direction; |
| 177 | if (RtpTransceiverDirectionHasSend(*current_direction_)) { |
| 178 | has_ever_been_used_to_send_ = true; |
| 179 | } |
| 180 | } |
| 181 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 182 | void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) { |
| 183 | fired_direction_ = direction; |
| 184 | } |
| 185 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 186 | bool RtpTransceiver::stopped() const { |
| 187 | return stopped_; |
| 188 | } |
| 189 | |
| 190 | RtpTransceiverDirection RtpTransceiver::direction() const { |
| 191 | return direction_; |
| 192 | } |
| 193 | |
| 194 | void RtpTransceiver::SetDirection(RtpTransceiverDirection new_direction) { |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 195 | if (stopped()) { |
| 196 | return; |
| 197 | } |
| 198 | if (new_direction == direction_) { |
| 199 | return; |
| 200 | } |
| 201 | direction_ = new_direction; |
| 202 | SignalNegotiationNeeded(); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 203 | } |
| 204 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 205 | absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction() |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 206 | const { |
| 207 | return current_direction_; |
| 208 | } |
| 209 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 210 | absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction() |
| 211 | const { |
| 212 | return fired_direction_; |
| 213 | } |
| 214 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 215 | void RtpTransceiver::Stop() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 216 | for (const auto& sender : senders_) { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 217 | sender->internal()->Stop(); |
| 218 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 219 | for (const auto& receiver : receivers_) { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 220 | receiver->internal()->Stop(); |
| 221 | } |
| 222 | stopped_ = true; |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 223 | current_direction_ = absl::nullopt; |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void RtpTransceiver::SetCodecPreferences( |
| 227 | rtc::ArrayView<RtpCodecCapability> codecs) { |
| 228 | // TODO(steveanton): Implement this. |
| 229 | RTC_NOTREACHED() << "Not implemented"; |
| 230 | } |
| 231 | |
| 232 | } // namespace webrtc |