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