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