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