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