Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [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 17:11:00 | [diff] [blame] | 11 | #include "pc/rtp_transceiver.h" |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 12 | |
| 13 | #include <string> |
| 14 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 15 | #include "absl/algorithm/container.h" |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 16 | #include "pc/channel_manager.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 17 | #include "pc/rtp_media_utils.h" |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 18 | #include "pc/rtp_parameters_conversion.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/logging.h" |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 21 | |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | |
| 24 | RtpTransceiver::RtpTransceiver(cricket::MediaType media_type) |
| 25 | : unified_plan_(false), media_type_(media_type) { |
| 26 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 27 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 28 | } |
| 29 | |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 30 | RtpTransceiver::RtpTransceiver( |
| 31 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 32 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 33 | receiver, |
| 34 | cricket::ChannelManager* channel_manager) |
| 35 | : unified_plan_(true), |
| 36 | media_type_(sender->media_type()), |
| 37 | channel_manager_(channel_manager) { |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 38 | RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO || |
| 39 | media_type_ == cricket::MEDIA_TYPE_VIDEO); |
| 40 | RTC_DCHECK_EQ(sender->media_type(), receiver->media_type()); |
| 41 | senders_.push_back(sender); |
| 42 | receivers_.push_back(receiver); |
| 43 | } |
| 44 | |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 45 | RtpTransceiver::~RtpTransceiver() { |
| 46 | Stop(); |
| 47 | } |
| 48 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 49 | void RtpTransceiver::SetChannel(cricket::ChannelInterface* channel) { |
| 50 | // Cannot set a non-null channel on a stopped transceiver. |
| 51 | if (stopped_ && channel) { |
| 52 | return; |
| 53 | } |
| 54 | |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 55 | if (channel) { |
| 56 | RTC_DCHECK_EQ(media_type(), channel->media_type()); |
| 57 | } |
Steve Anton | 6077675 | 2018-01-10 19:51:34 | [diff] [blame] | 58 | |
| 59 | if (channel_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 60 | channel_->SignalFirstPacketReceived().disconnect(this); |
Steve Anton | 6077675 | 2018-01-10 19:51:34 | [diff] [blame] | 61 | } |
| 62 | |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 63 | channel_ = channel; |
Steve Anton | 6077675 | 2018-01-10 19:51:34 | [diff] [blame] | 64 | |
| 65 | if (channel_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 66 | channel_->SignalFirstPacketReceived().connect( |
Steve Anton | 6077675 | 2018-01-10 19:51:34 | [diff] [blame] | 67 | this, &RtpTransceiver::OnFirstPacketReceived); |
| 68 | } |
| 69 | |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 70 | for (const auto& sender : senders_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 71 | sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel() |
| 72 | : nullptr); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 73 | } |
Steve Anton | 6077675 | 2018-01-10 19:51:34 | [diff] [blame] | 74 | |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 75 | for (const auto& receiver : receivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 76 | if (!channel_) { |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 77 | receiver->internal()->Stop(); |
| 78 | } |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 79 | |
| 80 | receiver->internal()->SetMediaChannel(channel_ ? channel_->media_channel() |
| 81 | : nullptr); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | void RtpTransceiver::AddSender( |
| 86 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 87 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 88 | RTC_DCHECK(!unified_plan_); |
| 89 | RTC_DCHECK(sender); |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 90 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 91 | RTC_DCHECK(!absl::c_linear_search(senders_, sender)); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 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 | } |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 100 | auto it = absl::c_find(senders_, sender); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 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) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 112 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 113 | RTC_DCHECK(!unified_plan_); |
| 114 | RTC_DCHECK(receiver); |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 115 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 116 | RTC_DCHECK(!absl::c_linear_search(receivers_, receiver)); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 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 | } |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 125 | auto it = absl::c_find(receivers_, receiver); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 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 18:23:57 | [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 19:43:08 | [diff] [blame] | 147 | cricket::MediaType RtpTransceiver::media_type() const { |
| 148 | return media_type_; |
| 149 | } |
| 150 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 151 | absl::optional<std::string> RtpTransceiver::mid() const { |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 152 | return mid_; |
| 153 | } |
| 154 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 155 | void RtpTransceiver::OnFirstPacketReceived(cricket::ChannelInterface*) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 156 | for (const auto& receiver : receivers_) { |
Steve Anton | 6077675 | 2018-01-10 19:51:34 | [diff] [blame] | 157 | receiver->internal()->NotifyFirstPacketReceived(); |
| 158 | } |
| 159 | } |
| 160 | |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [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-23 00:02:54 | [diff] [blame] | 173 | void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [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-23 00:02:54 | [diff] [blame] | 181 | current_direction_ = direction; |
| 182 | if (RtpTransceiverDirectionHasSend(*current_direction_)) { |
| 183 | has_ever_been_used_to_send_ = true; |
| 184 | } |
| 185 | } |
| 186 | |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 187 | void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) { |
| 188 | fired_direction_ = direction; |
| 189 | } |
| 190 | |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 191 | bool RtpTransceiver::stopped() const { |
| 192 | return stopped_; |
| 193 | } |
| 194 | |
| 195 | RtpTransceiverDirection RtpTransceiver::direction() const { |
| 196 | return direction_; |
| 197 | } |
| 198 | |
| 199 | void RtpTransceiver::SetDirection(RtpTransceiverDirection new_direction) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 200 | if (stopped()) { |
| 201 | return; |
| 202 | } |
| 203 | if (new_direction == direction_) { |
| 204 | return; |
| 205 | } |
| 206 | direction_ = new_direction; |
| 207 | SignalNegotiationNeeded(); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 208 | } |
| 209 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 210 | absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction() |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 211 | const { |
| 212 | return current_direction_; |
| 213 | } |
| 214 | |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 215 | absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction() |
| 216 | const { |
| 217 | return fired_direction_; |
| 218 | } |
| 219 | |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 220 | void RtpTransceiver::Stop() { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 221 | for (const auto& sender : senders_) { |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 222 | sender->internal()->Stop(); |
| 223 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 224 | for (const auto& receiver : receivers_) { |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 225 | receiver->internal()->Stop(); |
| 226 | } |
| 227 | stopped_ = true; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 228 | current_direction_ = absl::nullopt; |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 229 | } |
| 230 | |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 231 | RTCError RtpTransceiver::SetCodecPreferences( |
| 232 | rtc::ArrayView<RtpCodecCapability> codec_capabilities) { |
| 233 | RTC_DCHECK(unified_plan_); |
| 234 | |
| 235 | // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot |
| 236 | // to codecs and abort these steps. |
| 237 | if (codec_capabilities.empty()) { |
| 238 | codec_preferences_.clear(); |
| 239 | return RTCError::OK(); |
| 240 | } |
| 241 | |
| 242 | // 4. Remove any duplicate values in codecs. |
| 243 | std::vector<RtpCodecCapability> codecs; |
| 244 | absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs), |
| 245 | [&codecs](const RtpCodecCapability& codec) { |
| 246 | return absl::c_linear_search(codecs, codec); |
| 247 | }); |
| 248 | |
| 249 | if (media_type_ == cricket::MEDIA_TYPE_AUDIO) { |
| 250 | std::vector<cricket::AudioCodec> audio_codecs; |
| 251 | |
| 252 | std::vector<cricket::AudioCodec> recv_codecs, send_codecs; |
| 253 | channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs); |
| 254 | channel_manager_->GetSupportedAudioSendCodecs(&send_codecs); |
| 255 | |
| 256 | // 6. If the intersection between codecs and |
| 257 | // RTCRtpSender.getCapabilities(kind).codecs or the intersection between |
| 258 | // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX, |
| 259 | // RED or FEC codecs or is an empty set, throw InvalidModificationError. |
| 260 | // This ensures that we always have something to offer, regardless of |
| 261 | // transceiver.direction. |
| 262 | |
| 263 | if (!absl::c_any_of( |
| 264 | codecs, [&recv_codecs](const RtpCodecCapability& codec) { |
| 265 | return codec.name != cricket::kRtxCodecName && |
| 266 | codec.name != cricket::kRedCodecName && |
| 267 | codec.name != cricket::kFlexfecCodecName && |
| 268 | absl::c_any_of( |
| 269 | recv_codecs, |
| 270 | [&codec](const cricket::AudioCodec& recv_codec) { |
| 271 | return recv_codec.MatchesCapability(codec); |
| 272 | }); |
| 273 | })) { |
| 274 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 275 | "Invalid codec preferences: Missing codec from recv " |
| 276 | "codec capabilities."); |
| 277 | } |
| 278 | |
| 279 | if (!absl::c_any_of( |
| 280 | codecs, [&send_codecs](const RtpCodecCapability& codec) { |
| 281 | return codec.name != cricket::kRtxCodecName && |
| 282 | codec.name != cricket::kRedCodecName && |
| 283 | codec.name != cricket::kFlexfecCodecName && |
| 284 | absl::c_any_of( |
| 285 | send_codecs, |
| 286 | [&codec](const cricket::AudioCodec& send_codec) { |
| 287 | return send_codec.MatchesCapability(codec); |
| 288 | }); |
| 289 | })) { |
| 290 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 291 | "Invalid codec preferences: Missing codec from send " |
| 292 | "codec capabilities."); |
| 293 | } |
| 294 | |
| 295 | // 7. Let codecCapabilities be the union of |
| 296 | // RTCRtpSender.getCapabilities(kind).codecs and |
| 297 | // RTCRtpReceiver.getCapabilities(kind).codecs. 8.1 For each codec in |
| 298 | // codecs, If codec is not in codecCapabilities, throw |
| 299 | // InvalidModificationError. |
| 300 | for (const auto& codec_preference : codecs) { |
| 301 | bool is_recv_codec = absl::c_any_of( |
| 302 | recv_codecs, [&codec_preference](const cricket::AudioCodec& codec) { |
| 303 | return codec.MatchesCapability(codec_preference); |
| 304 | }); |
| 305 | |
| 306 | bool is_send_codec = absl::c_any_of( |
| 307 | send_codecs, [&codec_preference](const cricket::AudioCodec& codec) { |
| 308 | return codec.MatchesCapability(codec_preference); |
| 309 | }); |
| 310 | |
| 311 | if (!is_recv_codec && !is_send_codec) { |
| 312 | return RTCError( |
| 313 | RTCErrorType::INVALID_MODIFICATION, |
| 314 | std::string( |
| 315 | "Invalid codec preferences: invalid codec with name \"") + |
| 316 | codec_preference.name + "\"."); |
| 317 | } |
| 318 | } |
| 319 | } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) { |
| 320 | std::vector<cricket::VideoCodec> video_codecs; |
| 321 | // Video codecs are both for the receive and send side, so the checks are |
| 322 | // simpler than the audio ones. |
| 323 | channel_manager_->GetSupportedVideoCodecs(&video_codecs); |
| 324 | |
| 325 | // Validate codecs |
| 326 | for (const auto& codec_preference : codecs) { |
| 327 | if (!absl::c_any_of(video_codecs, [&codec_preference]( |
| 328 | const cricket::VideoCodec& codec) { |
| 329 | return codec.MatchesCapability(codec_preference); |
| 330 | })) { |
| 331 | return RTCError( |
| 332 | RTCErrorType::INVALID_MODIFICATION, |
| 333 | std::string( |
| 334 | "Invalid codec preferences: invalid codec with name \"") + |
| 335 | codec_preference.name + "\"."); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Check we have a real codec (not just rtx, red or fec) |
| 341 | if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) { |
| 342 | return codec.name == cricket::kRtxCodecName || |
| 343 | codec.name == cricket::kRedCodecName || |
| 344 | codec.name == cricket::kUlpfecCodecName; |
| 345 | })) { |
| 346 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 347 | "Invalid codec preferences: codec list must have a non " |
| 348 | "RTX, RED or FEC entry."); |
| 349 | } |
| 350 | |
| 351 | codec_preferences_ = codecs; |
| 352 | |
| 353 | return RTCError::OK(); |
Steve Anton | 6e634bf | 2017-11-13 18:44:53 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | } // namespace webrtc |