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> |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 14 | #include <utility> |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 15 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 16 | #include "absl/algorithm/container.h" |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 17 | #include "api/rtp_parameters.h" |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 18 | #include "pc/channel_manager.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "pc/rtp_media_utils.h" |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 20 | #include "pc/rtp_parameters_conversion.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/logging.h" |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 23 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 24 | namespace webrtc { |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 25 | namespace { |
| 26 | template <class T> |
| 27 | RTCError VerifyCodecPreferences(const std::vector<RtpCodecCapability>& codecs, |
| 28 | const std::vector<T>& send_codecs, |
| 29 | const std::vector<T>& recv_codecs) { |
| 30 | // If the intersection between codecs and |
| 31 | // RTCRtpSender.getCapabilities(kind).codecs or the intersection between |
| 32 | // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX, |
| 33 | // RED or FEC codecs or is an empty set, throw InvalidModificationError. |
| 34 | // This ensures that we always have something to offer, regardless of |
| 35 | // transceiver.direction. |
| 36 | |
| 37 | if (!absl::c_any_of(codecs, [&recv_codecs](const RtpCodecCapability& codec) { |
| 38 | return codec.name != cricket::kRtxCodecName && |
| 39 | codec.name != cricket::kRedCodecName && |
| 40 | codec.name != cricket::kFlexfecCodecName && |
| 41 | absl::c_any_of(recv_codecs, [&codec](const T& recv_codec) { |
| 42 | return recv_codec.MatchesCapability(codec); |
| 43 | }); |
| 44 | })) { |
| 45 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 46 | "Invalid codec preferences: Missing codec from recv " |
| 47 | "codec capabilities."); |
| 48 | } |
| 49 | |
| 50 | if (!absl::c_any_of(codecs, [&send_codecs](const RtpCodecCapability& codec) { |
| 51 | return codec.name != cricket::kRtxCodecName && |
| 52 | codec.name != cricket::kRedCodecName && |
| 53 | codec.name != cricket::kFlexfecCodecName && |
| 54 | absl::c_any_of(send_codecs, [&codec](const T& send_codec) { |
| 55 | return send_codec.MatchesCapability(codec); |
| 56 | }); |
| 57 | })) { |
| 58 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 59 | "Invalid codec preferences: Missing codec from send " |
| 60 | "codec capabilities."); |
| 61 | } |
| 62 | |
| 63 | // Let codecCapabilities be the union of |
| 64 | // RTCRtpSender.getCapabilities(kind).codecs and |
| 65 | // RTCRtpReceiver.getCapabilities(kind).codecs. For each codec in codecs, If |
| 66 | // codec is not in codecCapabilities, throw InvalidModificationError. |
| 67 | for (const auto& codec_preference : codecs) { |
| 68 | bool is_recv_codec = |
| 69 | absl::c_any_of(recv_codecs, [&codec_preference](const T& codec) { |
| 70 | return codec.MatchesCapability(codec_preference); |
| 71 | }); |
| 72 | |
| 73 | bool is_send_codec = |
| 74 | absl::c_any_of(send_codecs, [&codec_preference](const T& codec) { |
| 75 | return codec.MatchesCapability(codec_preference); |
| 76 | }); |
| 77 | |
| 78 | if (!is_recv_codec && !is_send_codec) { |
| 79 | return RTCError( |
| 80 | RTCErrorType::INVALID_MODIFICATION, |
| 81 | std::string("Invalid codec preferences: invalid codec with name \"") + |
| 82 | codec_preference.name + "\"."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Check we have a real codec (not just rtx, red or fec) |
| 87 | if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) { |
| 88 | return codec.name == cricket::kRtxCodecName || |
| 89 | codec.name == cricket::kRedCodecName || |
| 90 | codec.name == cricket::kUlpfecCodecName; |
| 91 | })) { |
| 92 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 93 | "Invalid codec preferences: codec list must have a non " |
| 94 | "RTX, RED or FEC entry."); |
| 95 | } |
| 96 | |
| 97 | return RTCError::OK(); |
| 98 | } |
| 99 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 100 | TaskQueueBase* GetCurrentTaskQueueOrThread() { |
| 101 | TaskQueueBase* current = TaskQueueBase::Current(); |
| 102 | if (!current) |
| 103 | current = rtc::ThreadManager::Instance()->CurrentThread(); |
| 104 | return current; |
| 105 | } |
| 106 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 107 | } // namespace |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 108 | |
| 109 | RtpTransceiver::RtpTransceiver(cricket::MediaType media_type) |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 110 | : thread_(GetCurrentTaskQueueOrThread()), |
| 111 | unified_plan_(false), |
| 112 | media_type_(media_type) { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 113 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 114 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 115 | } |
| 116 | |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 117 | RtpTransceiver::RtpTransceiver( |
| 118 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 119 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 120 | receiver, |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 121 | cricket::ChannelManager* channel_manager, |
| 122 | std::vector<RtpHeaderExtensionCapability> header_extensions_offered) |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 123 | : thread_(GetCurrentTaskQueueOrThread()), |
| 124 | unified_plan_(true), |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 125 | media_type_(sender->media_type()), |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 126 | channel_manager_(channel_manager), |
Markus Handell | 755c65d | 2020-06-24 01:06:10 +0200 | [diff] [blame] | 127 | header_extensions_to_offer_(std::move(header_extensions_offered)) { |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 128 | RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO || |
| 129 | media_type_ == cricket::MEDIA_TYPE_VIDEO); |
| 130 | RTC_DCHECK_EQ(sender->media_type(), receiver->media_type()); |
| 131 | senders_.push_back(sender); |
| 132 | receivers_.push_back(receiver); |
| 133 | } |
| 134 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 135 | RtpTransceiver::~RtpTransceiver() { |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 136 | StopInternal(); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 139 | void RtpTransceiver::SetChannel(cricket::ChannelInterface* channel) { |
| 140 | // Cannot set a non-null channel on a stopped transceiver. |
| 141 | if (stopped_ && channel) { |
| 142 | return; |
| 143 | } |
| 144 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 145 | if (channel) { |
| 146 | RTC_DCHECK_EQ(media_type(), channel->media_type()); |
| 147 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 148 | |
| 149 | if (channel_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 150 | channel_->SignalFirstPacketReceived().disconnect(this); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 153 | channel_ = channel; |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 154 | |
| 155 | if (channel_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 156 | channel_->SignalFirstPacketReceived().connect( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 157 | this, &RtpTransceiver::OnFirstPacketReceived); |
| 158 | } |
| 159 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 160 | for (const auto& sender : senders_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 161 | sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel() |
| 162 | : nullptr); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 163 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 164 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 165 | for (const auto& receiver : receivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 166 | if (!channel_) { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 167 | receiver->internal()->Stop(); |
| 168 | } |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 169 | |
| 170 | receiver->internal()->SetMediaChannel(channel_ ? channel_->media_channel() |
| 171 | : nullptr); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | void RtpTransceiver::AddSender( |
| 176 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 177 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 178 | RTC_DCHECK(!unified_plan_); |
| 179 | RTC_DCHECK(sender); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 180 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 181 | RTC_DCHECK(!absl::c_linear_search(senders_, sender)); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 182 | senders_.push_back(sender); |
| 183 | } |
| 184 | |
| 185 | bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) { |
| 186 | RTC_DCHECK(!unified_plan_); |
| 187 | if (sender) { |
| 188 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
| 189 | } |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 190 | auto it = absl::c_find(senders_, sender); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 191 | if (it == senders_.end()) { |
| 192 | return false; |
| 193 | } |
| 194 | (*it)->internal()->Stop(); |
| 195 | senders_.erase(it); |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | void RtpTransceiver::AddReceiver( |
| 200 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 201 | receiver) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 202 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 203 | RTC_DCHECK(!unified_plan_); |
| 204 | RTC_DCHECK(receiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 205 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 206 | RTC_DCHECK(!absl::c_linear_search(receivers_, receiver)); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 207 | receivers_.push_back(receiver); |
| 208 | } |
| 209 | |
| 210 | bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) { |
| 211 | RTC_DCHECK(!unified_plan_); |
| 212 | if (receiver) { |
| 213 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
| 214 | } |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 215 | auto it = absl::c_find(receivers_, receiver); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 216 | if (it == receivers_.end()) { |
| 217 | return false; |
| 218 | } |
| 219 | (*it)->internal()->Stop(); |
Markus Handell | 43e62fc | 2020-01-07 19:46:15 +0100 | [diff] [blame] | 220 | // After the receiver has been removed, there's no guarantee that the |
| 221 | // contained media channel isn't deleted shortly after this. To make sure that |
| 222 | // the receiver doesn't spontaneously try to use it's (potentially stale) |
| 223 | // media channel reference, we clear it out. |
| 224 | (*it)->internal()->SetMediaChannel(nullptr); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 225 | receivers_.erase(it); |
| 226 | return true; |
| 227 | } |
| 228 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 229 | rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const { |
| 230 | RTC_DCHECK(unified_plan_); |
| 231 | RTC_CHECK_EQ(1u, senders_.size()); |
| 232 | return senders_[0]->internal(); |
| 233 | } |
| 234 | |
| 235 | rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal() |
| 236 | const { |
| 237 | RTC_DCHECK(unified_plan_); |
| 238 | RTC_CHECK_EQ(1u, receivers_.size()); |
| 239 | return receivers_[0]->internal(); |
| 240 | } |
| 241 | |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 242 | cricket::MediaType RtpTransceiver::media_type() const { |
| 243 | return media_type_; |
| 244 | } |
| 245 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 246 | absl::optional<std::string> RtpTransceiver::mid() const { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 247 | return mid_; |
| 248 | } |
| 249 | |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 250 | void RtpTransceiver::OnFirstPacketReceived(cricket::ChannelInterface*) { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 251 | for (const auto& receiver : receivers_) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 252 | receiver->internal()->NotifyFirstPacketReceived(); |
| 253 | } |
| 254 | } |
| 255 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 256 | rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const { |
| 257 | RTC_DCHECK(unified_plan_); |
| 258 | RTC_CHECK_EQ(1u, senders_.size()); |
| 259 | return senders_[0]; |
| 260 | } |
| 261 | |
| 262 | rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const { |
| 263 | RTC_DCHECK(unified_plan_); |
| 264 | RTC_CHECK_EQ(1u, receivers_.size()); |
| 265 | return receivers_[0]; |
| 266 | } |
| 267 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 268 | void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 269 | RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>") |
| 270 | << ") current direction from " |
| 271 | << (current_direction_ ? RtpTransceiverDirectionToString( |
| 272 | *current_direction_) |
| 273 | : "<not set>") |
| 274 | << " to " << RtpTransceiverDirectionToString(direction) |
| 275 | << "."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 276 | current_direction_ = direction; |
| 277 | if (RtpTransceiverDirectionHasSend(*current_direction_)) { |
| 278 | has_ever_been_used_to_send_ = true; |
| 279 | } |
| 280 | } |
| 281 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 282 | void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) { |
| 283 | fired_direction_ = direction; |
| 284 | } |
| 285 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 286 | bool RtpTransceiver::stopped() const { |
| 287 | return stopped_; |
| 288 | } |
| 289 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 290 | bool RtpTransceiver::stopping() const { |
| 291 | RTC_DCHECK_RUN_ON(thread_); |
| 292 | return stopping_; |
| 293 | } |
| 294 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 295 | RtpTransceiverDirection RtpTransceiver::direction() const { |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 296 | if (unified_plan_ && stopping()) |
| 297 | return webrtc::RtpTransceiverDirection::kStopped; |
| 298 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 299 | return direction_; |
| 300 | } |
| 301 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 302 | RTCError RtpTransceiver::SetDirectionWithError( |
| 303 | RtpTransceiverDirection new_direction) { |
| 304 | if (unified_plan_ && stopping()) { |
| 305 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 306 | "Cannot set direction on a stopping transceiver."); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 307 | } |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 308 | if (new_direction == direction_) |
| 309 | return RTCError::OK(); |
| 310 | |
| 311 | if (new_direction == RtpTransceiverDirection::kStopped) { |
| 312 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 313 | "The set direction 'stopped' is invalid."); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 314 | } |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 315 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 316 | direction_ = new_direction; |
| 317 | SignalNegotiationNeeded(); |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 318 | |
| 319 | return RTCError::OK(); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 322 | absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction() |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 323 | const { |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 324 | if (unified_plan_ && stopping()) |
| 325 | return webrtc::RtpTransceiverDirection::kStopped; |
| 326 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 327 | return current_direction_; |
| 328 | } |
| 329 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 330 | absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction() |
| 331 | const { |
| 332 | return fired_direction_; |
| 333 | } |
| 334 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 335 | void RtpTransceiver::StopSendingAndReceiving() { |
| 336 | // 1. Let sender be transceiver.[[Sender]]. |
| 337 | // 2. Let receiver be transceiver.[[Receiver]]. |
| 338 | // |
| 339 | // 3. Stop sending media with sender. |
| 340 | // |
| 341 | // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as |
| 342 | // specified in [RFC3550]. |
| 343 | RTC_DCHECK_RUN_ON(thread_); |
| 344 | for (const auto& sender : senders_) |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 345 | sender->internal()->Stop(); |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 346 | |
| 347 | // 5. Stop receiving media with receiver. |
| 348 | for (const auto& receiver : receivers_) |
Harald Alvestrand | a88c977 | 2020-08-10 18:06:09 +0000 | [diff] [blame] | 349 | receiver->internal()->Stop(); |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 350 | |
| 351 | stopping_ = true; |
| 352 | direction_ = webrtc::RtpTransceiverDirection::kInactive; |
| 353 | } |
| 354 | |
| 355 | RTCError RtpTransceiver::StopStandard() { |
| 356 | RTC_DCHECK_RUN_ON(thread_); |
| 357 | RTC_DCHECK(unified_plan_); |
| 358 | // 1. Let transceiver be the RTCRtpTransceiver object on which the method is |
| 359 | // invoked. |
| 360 | // |
| 361 | // 2. Let connection be the RTCPeerConnection object associated with |
| 362 | // transceiver. |
| 363 | // |
| 364 | // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError. |
| 365 | if (is_pc_closed_) { |
| 366 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 367 | "PeerConnection is closed."); |
Harald Alvestrand | a88c977 | 2020-08-10 18:06:09 +0000 | [diff] [blame] | 368 | } |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 369 | |
| 370 | // 4. If transceiver.[[Stopping]] is true, abort these steps. |
| 371 | if (stopping_) |
| 372 | return RTCError::OK(); |
| 373 | |
| 374 | // 5. Stop sending and receiving given transceiver, and update the |
| 375 | // negotiation-needed flag for connection. |
| 376 | StopSendingAndReceiving(); |
| 377 | SignalNegotiationNeeded(); |
| 378 | |
| 379 | return RTCError::OK(); |
| 380 | } |
| 381 | |
| 382 | void RtpTransceiver::StopInternal() { |
| 383 | RTC_DCHECK_RUN_ON(thread_); |
| 384 | // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given |
| 385 | // transceiver. |
| 386 | if (!stopping_) |
| 387 | StopSendingAndReceiving(); |
| 388 | |
| 389 | // 2. Set transceiver.[[Stopped]] to true. |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 390 | stopped_ = true; |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 391 | |
| 392 | // Signal the updated change to the senders. |
| 393 | for (const auto& sender : senders_) |
| 394 | sender->internal()->SetTransceiverAsStopped(); |
| 395 | |
| 396 | // 3. Set transceiver.[[Receptive]] to false. |
| 397 | // 4. Set transceiver.[[CurrentDirection]] to null. |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 398 | current_direction_ = absl::nullopt; |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 401 | RTCError RtpTransceiver::SetCodecPreferences( |
| 402 | rtc::ArrayView<RtpCodecCapability> codec_capabilities) { |
| 403 | RTC_DCHECK(unified_plan_); |
| 404 | |
| 405 | // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot |
| 406 | // to codecs and abort these steps. |
| 407 | if (codec_capabilities.empty()) { |
| 408 | codec_preferences_.clear(); |
| 409 | return RTCError::OK(); |
| 410 | } |
| 411 | |
| 412 | // 4. Remove any duplicate values in codecs. |
| 413 | std::vector<RtpCodecCapability> codecs; |
| 414 | absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs), |
| 415 | [&codecs](const RtpCodecCapability& codec) { |
| 416 | return absl::c_linear_search(codecs, codec); |
| 417 | }); |
| 418 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 419 | // 6. to 8. |
| 420 | RTCError result; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 421 | if (media_type_ == cricket::MEDIA_TYPE_AUDIO) { |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 422 | std::vector<cricket::AudioCodec> recv_codecs, send_codecs; |
| 423 | channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs); |
| 424 | channel_manager_->GetSupportedAudioSendCodecs(&send_codecs); |
| 425 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 426 | result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 427 | } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) { |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 428 | std::vector<cricket::VideoCodec> recv_codecs, send_codecs; |
| 429 | channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs); |
| 430 | channel_manager_->GetSupportedVideoSendCodecs(&send_codecs); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 431 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 432 | result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 435 | if (result.ok()) { |
| 436 | codec_preferences_ = codecs; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 439 | return result; |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 442 | std::vector<RtpHeaderExtensionCapability> |
| 443 | RtpTransceiver::HeaderExtensionsToOffer() const { |
Markus Handell | 755c65d | 2020-06-24 01:06:10 +0200 | [diff] [blame] | 444 | return header_extensions_to_offer_; |
| 445 | } |
| 446 | |
| 447 | RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions( |
| 448 | rtc::ArrayView<const RtpHeaderExtensionCapability> |
| 449 | header_extensions_to_offer) { |
| 450 | for (const auto& entry : header_extensions_to_offer) { |
| 451 | // Handle unsupported requests for mandatory extensions as per |
| 452 | // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface. |
| 453 | // Note: |
| 454 | // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1, |
| 455 | // this has to be checked on a higher level. We naturally error out |
| 456 | // in the handling of Step 2.2 if an unset URI is encountered. |
| 457 | |
| 458 | // Step 2.2. |
| 459 | // Handle unknown extensions. |
| 460 | auto it = std::find_if( |
| 461 | header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(), |
| 462 | [&entry](const auto& offered) { return entry.uri == offered.uri; }); |
| 463 | if (it == header_extensions_to_offer_.end()) { |
| 464 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 465 | "Attempted to modify an unoffered extension."); |
| 466 | } |
| 467 | |
| 468 | // Step 2.4-2.5. |
| 469 | // - Use of the transceiver interface indicates unified plan is in effect, |
| 470 | // hence the MID extension needs to be enabled. |
| 471 | // - Also handle the mandatory video orientation extensions. |
| 472 | if ((entry.uri == RtpExtension::kMidUri || |
| 473 | entry.uri == RtpExtension::kVideoRotationUri) && |
| 474 | entry.direction != RtpTransceiverDirection::kSendRecv) { |
| 475 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 476 | "Attempted to stop a mandatory extension."); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // Apply mutation after error checking. |
| 481 | for (const auto& entry : header_extensions_to_offer) { |
| 482 | auto it = std::find_if( |
| 483 | header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(), |
| 484 | [&entry](const auto& offered) { return entry.uri == offered.uri; }); |
| 485 | it->direction = entry.direction; |
| 486 | } |
| 487 | |
| 488 | return RTCError::OK(); |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 489 | } |
| 490 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 491 | void RtpTransceiver::SetPeerConnectionClosed() { |
| 492 | is_pc_closed_ = true; |
| 493 | } |
| 494 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 495 | } // namespace webrtc |