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 | |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 13 | #include <iterator> |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 14 | #include <string> |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 15 | #include <utility> |
Markus Handell | 5932fe1 | 2020-12-17 22:19:40 +0100 | [diff] [blame] | 16 | #include <vector> |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 17 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 18 | #include "absl/algorithm/container.h" |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 19 | #include "api/rtp_parameters.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 21 | #include "media/base/codec.h" |
| 22 | #include "media/base/media_constants.h" |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 23 | #include "pc/channel_manager.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "pc/rtp_media_utils.h" |
Markus Handell | 5932fe1 | 2020-12-17 22:19:40 +0100 | [diff] [blame] | 25 | #include "pc/session_description.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
| 27 | #include "rtc_base/logging.h" |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 28 | #include "rtc_base/task_utils/to_queued_task.h" |
Harald Alvestrand | 5761e7b | 2021-01-29 14:45:08 +0000 | [diff] [blame] | 29 | #include "rtc_base/thread.h" |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 30 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 31 | namespace webrtc { |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 32 | namespace { |
| 33 | template <class T> |
| 34 | RTCError VerifyCodecPreferences(const std::vector<RtpCodecCapability>& codecs, |
| 35 | const std::vector<T>& send_codecs, |
| 36 | const std::vector<T>& recv_codecs) { |
| 37 | // If the intersection between codecs and |
| 38 | // RTCRtpSender.getCapabilities(kind).codecs or the intersection between |
| 39 | // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX, |
| 40 | // RED or FEC codecs or is an empty set, throw InvalidModificationError. |
| 41 | // This ensures that we always have something to offer, regardless of |
| 42 | // transceiver.direction. |
| 43 | |
| 44 | if (!absl::c_any_of(codecs, [&recv_codecs](const RtpCodecCapability& codec) { |
| 45 | return codec.name != cricket::kRtxCodecName && |
| 46 | codec.name != cricket::kRedCodecName && |
| 47 | codec.name != cricket::kFlexfecCodecName && |
| 48 | absl::c_any_of(recv_codecs, [&codec](const T& recv_codec) { |
| 49 | return recv_codec.MatchesCapability(codec); |
| 50 | }); |
| 51 | })) { |
| 52 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 53 | "Invalid codec preferences: Missing codec from recv " |
| 54 | "codec capabilities."); |
| 55 | } |
| 56 | |
| 57 | if (!absl::c_any_of(codecs, [&send_codecs](const RtpCodecCapability& codec) { |
| 58 | return codec.name != cricket::kRtxCodecName && |
| 59 | codec.name != cricket::kRedCodecName && |
| 60 | codec.name != cricket::kFlexfecCodecName && |
| 61 | absl::c_any_of(send_codecs, [&codec](const T& send_codec) { |
| 62 | return send_codec.MatchesCapability(codec); |
| 63 | }); |
| 64 | })) { |
| 65 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 66 | "Invalid codec preferences: Missing codec from send " |
| 67 | "codec capabilities."); |
| 68 | } |
| 69 | |
| 70 | // Let codecCapabilities be the union of |
| 71 | // RTCRtpSender.getCapabilities(kind).codecs and |
| 72 | // RTCRtpReceiver.getCapabilities(kind).codecs. For each codec in codecs, If |
| 73 | // codec is not in codecCapabilities, throw InvalidModificationError. |
| 74 | for (const auto& codec_preference : codecs) { |
| 75 | bool is_recv_codec = |
| 76 | absl::c_any_of(recv_codecs, [&codec_preference](const T& codec) { |
| 77 | return codec.MatchesCapability(codec_preference); |
| 78 | }); |
| 79 | |
| 80 | bool is_send_codec = |
| 81 | absl::c_any_of(send_codecs, [&codec_preference](const T& codec) { |
| 82 | return codec.MatchesCapability(codec_preference); |
| 83 | }); |
| 84 | |
| 85 | if (!is_recv_codec && !is_send_codec) { |
| 86 | return RTCError( |
| 87 | RTCErrorType::INVALID_MODIFICATION, |
| 88 | std::string("Invalid codec preferences: invalid codec with name \"") + |
| 89 | codec_preference.name + "\"."); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Check we have a real codec (not just rtx, red or fec) |
| 94 | if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) { |
| 95 | return codec.name == cricket::kRtxCodecName || |
| 96 | codec.name == cricket::kRedCodecName || |
| 97 | codec.name == cricket::kUlpfecCodecName; |
| 98 | })) { |
| 99 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 100 | "Invalid codec preferences: codec list must have a non " |
| 101 | "RTX, RED or FEC entry."); |
| 102 | } |
| 103 | |
| 104 | return RTCError::OK(); |
| 105 | } |
| 106 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 107 | TaskQueueBase* GetCurrentTaskQueueOrThread() { |
| 108 | TaskQueueBase* current = TaskQueueBase::Current(); |
| 109 | if (!current) |
| 110 | current = rtc::ThreadManager::Instance()->CurrentThread(); |
| 111 | return current; |
| 112 | } |
| 113 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 114 | } // namespace |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 115 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 116 | RtpTransceiver::RtpTransceiver( |
| 117 | cricket::MediaType media_type, |
| 118 | cricket::ChannelManager* channel_manager /* = nullptr*/) |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 119 | : thread_(GetCurrentTaskQueueOrThread()), |
| 120 | unified_plan_(false), |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 121 | media_type_(media_type), |
| 122 | channel_manager_(channel_manager) { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 123 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 124 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 125 | RTC_DCHECK(channel_manager_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 128 | RtpTransceiver::RtpTransceiver( |
| 129 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 130 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 131 | receiver, |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 132 | cricket::ChannelManager* channel_manager, |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 +0000 | [diff] [blame] | 133 | std::vector<RtpHeaderExtensionCapability> header_extensions_offered, |
| 134 | std::function<void()> on_negotiation_needed) |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 135 | : thread_(GetCurrentTaskQueueOrThread()), |
| 136 | unified_plan_(true), |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 137 | media_type_(sender->media_type()), |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 138 | channel_manager_(channel_manager), |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 +0000 | [diff] [blame] | 139 | header_extensions_to_offer_(std::move(header_extensions_offered)), |
| 140 | on_negotiation_needed_(std::move(on_negotiation_needed)) { |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 141 | RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO || |
| 142 | media_type_ == cricket::MEDIA_TYPE_VIDEO); |
| 143 | RTC_DCHECK_EQ(sender->media_type(), receiver->media_type()); |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 144 | RTC_DCHECK(channel_manager_); |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 145 | senders_.push_back(sender); |
| 146 | receivers_.push_back(receiver); |
| 147 | } |
| 148 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 149 | RtpTransceiver::~RtpTransceiver() { |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 150 | // TODO(tommi): On Android, when running PeerConnectionClientTest (e.g. |
| 151 | // PeerConnectionClientTest#testCameraSwitch), the instance doesn't get |
| 152 | // deleted on `thread_`. See if we can fix that. |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 +0000 | [diff] [blame] | 153 | if (!stopped_) { |
| 154 | RTC_DCHECK_RUN_ON(thread_); |
| 155 | StopInternal(); |
| 156 | } |
Tomas Gunnarsson | 16de216 | 2022-01-26 10:21:57 +0100 | [diff] [blame] | 157 | |
| 158 | RTC_CHECK(!channel_) << "Missing call to SetChannel(nullptr)?"; |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Tomas Gunnarsson | 4f8a58c | 2022-01-19 11:36:23 +0100 | [diff] [blame] | 161 | void RtpTransceiver::SetChannel( |
| 162 | cricket::ChannelInterface* channel, |
| 163 | std::function<RtpTransportInternal*(const std::string&)> transport_lookup) { |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 164 | RTC_DCHECK_RUN_ON(thread_); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 165 | // Cannot set a non-null channel on a stopped transceiver. |
Tomas Gunnarsson | 16de216 | 2022-01-26 10:21:57 +0100 | [diff] [blame] | 166 | if ((stopped_ && channel) || channel == channel_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 170 | RTC_DCHECK(channel || channel_); |
Tomas Gunnarsson | 4f8a58c | 2022-01-19 11:36:23 +0100 | [diff] [blame] | 171 | RTC_DCHECK(!channel || transport_lookup) << "lookup function not supplied"; |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 172 | |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 173 | RTC_LOG_THREAD_BLOCK_COUNT(); |
| 174 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 175 | if (channel_) { |
| 176 | signaling_thread_safety_->SetNotAlive(); |
| 177 | signaling_thread_safety_ = nullptr; |
| 178 | } |
| 179 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 180 | if (channel) { |
| 181 | RTC_DCHECK_EQ(media_type(), channel->media_type()); |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 182 | signaling_thread_safety_ = PendingTaskSafetyFlag::Create(); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 183 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 184 | |
Tomas Gunnarsson | 16de216 | 2022-01-26 10:21:57 +0100 | [diff] [blame] | 185 | cricket::ChannelInterface* channel_to_delete = nullptr; |
| 186 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 187 | // An alternative to this, could be to require SetChannel to be called |
| 188 | // on the network thread. The channel object operates for the most part |
| 189 | // on the network thread, as part of its initialization being on the network |
| 190 | // thread is required, so setting a channel object as part of the construction |
| 191 | // (without thread hopping) might be the more efficient thing to do than |
| 192 | // how SetChannel works today. |
| 193 | // Similarly, if the channel() accessor is limited to the network thread, that |
| 194 | // helps with keeping the channel implementation requirements being met and |
| 195 | // avoids synchronization for accessing the pointer or network related state. |
| 196 | channel_manager_->network_thread()->Invoke<void>(RTC_FROM_HERE, [&]() { |
| 197 | if (channel_) { |
| 198 | channel_->SetFirstPacketReceivedCallback(nullptr); |
Tomas Gunnarsson | 4f8a58c | 2022-01-19 11:36:23 +0100 | [diff] [blame] | 199 | channel_->SetRtpTransport(nullptr); |
Tomas Gunnarsson | 16de216 | 2022-01-26 10:21:57 +0100 | [diff] [blame] | 200 | channel_to_delete = channel_; |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 201 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 202 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 203 | channel_ = channel; |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 204 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 205 | if (channel_) { |
Tomas Gunnarsson | 5411b17 | 2022-01-24 08:45:26 +0100 | [diff] [blame] | 206 | channel_->SetRtpTransport(transport_lookup(channel_->mid())); |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 207 | channel_->SetFirstPacketReceivedCallback( |
| 208 | [thread = thread_, flag = signaling_thread_safety_, this]() mutable { |
| 209 | thread->PostTask(ToQueuedTask( |
| 210 | std::move(flag), [this]() { OnFirstPacketReceived(); })); |
| 211 | }); |
| 212 | } |
| 213 | }); |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 214 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 215 | for (const auto& sender : senders_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 216 | sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel() |
| 217 | : nullptr); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 218 | } |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 219 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 220 | RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1); |
Tommi | fe04164 | 2021-04-07 10:08:28 +0200 | [diff] [blame] | 221 | |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 222 | for (const auto& receiver : receivers_) { |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 223 | if (!channel_) { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 224 | receiver->internal()->Stop(); |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame] | 225 | } else { |
| 226 | receiver->internal()->SetMediaChannel(channel_->media_channel()); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 227 | } |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 228 | } |
Tomas Gunnarsson | 16de216 | 2022-01-26 10:21:57 +0100 | [diff] [blame] | 229 | |
| 230 | // Destroy the channel, if we had one, now _after_ updating the receivers who |
| 231 | // might have had references to the previous channel. |
| 232 | if (channel_to_delete) { |
| 233 | channel_manager_->DestroyChannel(channel_to_delete); |
| 234 | } |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void RtpTransceiver::AddSender( |
| 238 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) { |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 239 | RTC_DCHECK_RUN_ON(thread_); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 240 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 241 | RTC_DCHECK(!unified_plan_); |
| 242 | RTC_DCHECK(sender); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 243 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 244 | RTC_DCHECK(!absl::c_linear_search(senders_, sender)); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 245 | senders_.push_back(sender); |
| 246 | } |
| 247 | |
| 248 | bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) { |
| 249 | RTC_DCHECK(!unified_plan_); |
| 250 | if (sender) { |
| 251 | RTC_DCHECK_EQ(media_type(), sender->media_type()); |
| 252 | } |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 253 | auto it = absl::c_find(senders_, sender); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 254 | if (it == senders_.end()) { |
| 255 | return false; |
| 256 | } |
| 257 | (*it)->internal()->Stop(); |
| 258 | senders_.erase(it); |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | void RtpTransceiver::AddReceiver( |
| 263 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 264 | receiver) { |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 265 | RTC_DCHECK_RUN_ON(thread_); |
Amit Hilbuch | dd9390c | 2018-11-13 16:26:05 -0800 | [diff] [blame] | 266 | RTC_DCHECK(!stopped_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 267 | RTC_DCHECK(!unified_plan_); |
| 268 | RTC_DCHECK(receiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 269 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 270 | RTC_DCHECK(!absl::c_linear_search(receivers_, receiver)); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 271 | receivers_.push_back(receiver); |
| 272 | } |
| 273 | |
| 274 | bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) { |
| 275 | RTC_DCHECK(!unified_plan_); |
| 276 | if (receiver) { |
| 277 | RTC_DCHECK_EQ(media_type(), receiver->media_type()); |
| 278 | } |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 279 | auto it = absl::c_find(receivers_, receiver); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 280 | if (it == receivers_.end()) { |
| 281 | return false; |
| 282 | } |
Tommi | 4ccdf93 | 2021-05-17 14:50:10 +0200 | [diff] [blame] | 283 | // `Stop()` will clear the internally cached pointer to the media channel. |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 284 | (*it)->internal()->Stop(); |
| 285 | receivers_.erase(it); |
| 286 | return true; |
| 287 | } |
| 288 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 289 | rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const { |
| 290 | RTC_DCHECK(unified_plan_); |
| 291 | RTC_CHECK_EQ(1u, senders_.size()); |
Niels Möller | e7cc883 | 2022-01-04 15:20:03 +0100 | [diff] [blame] | 292 | return rtc::scoped_refptr<RtpSenderInternal>(senders_[0]->internal()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal() |
| 296 | const { |
| 297 | RTC_DCHECK(unified_plan_); |
| 298 | RTC_CHECK_EQ(1u, receivers_.size()); |
Niels Möller | e7cc883 | 2022-01-04 15:20:03 +0100 | [diff] [blame] | 299 | return rtc::scoped_refptr<RtpReceiverInternal>(receivers_[0]->internal()); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 302 | cricket::MediaType RtpTransceiver::media_type() const { |
| 303 | return media_type_; |
| 304 | } |
| 305 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 306 | absl::optional<std::string> RtpTransceiver::mid() const { |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 307 | return mid_; |
| 308 | } |
| 309 | |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 310 | void RtpTransceiver::OnFirstPacketReceived() { |
Mirko Bonadei | 739baf0 | 2019-01-27 17:29:42 +0100 | [diff] [blame] | 311 | for (const auto& receiver : receivers_) { |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 312 | receiver->internal()->NotifyFirstPacketReceived(); |
| 313 | } |
| 314 | } |
| 315 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 316 | rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const { |
| 317 | RTC_DCHECK(unified_plan_); |
| 318 | RTC_CHECK_EQ(1u, senders_.size()); |
| 319 | return senders_[0]; |
| 320 | } |
| 321 | |
| 322 | rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const { |
| 323 | RTC_DCHECK(unified_plan_); |
| 324 | RTC_CHECK_EQ(1u, receivers_.size()); |
| 325 | return receivers_[0]; |
| 326 | } |
| 327 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 328 | void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) { |
Steve Anton | 3d954a6 | 2018-04-02 11:27:23 -0700 | [diff] [blame] | 329 | RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>") |
| 330 | << ") current direction from " |
| 331 | << (current_direction_ ? RtpTransceiverDirectionToString( |
| 332 | *current_direction_) |
| 333 | : "<not set>") |
| 334 | << " to " << RtpTransceiverDirectionToString(direction) |
| 335 | << "."; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 336 | current_direction_ = direction; |
| 337 | if (RtpTransceiverDirectionHasSend(*current_direction_)) { |
| 338 | has_ever_been_used_to_send_ = true; |
| 339 | } |
| 340 | } |
| 341 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 342 | void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) { |
| 343 | fired_direction_ = direction; |
| 344 | } |
| 345 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 346 | bool RtpTransceiver::stopped() const { |
Tommi | 99c8a80 | 2021-04-27 15:00:00 +0200 | [diff] [blame] | 347 | RTC_DCHECK_RUN_ON(thread_); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 348 | return stopped_; |
| 349 | } |
| 350 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 351 | bool RtpTransceiver::stopping() const { |
| 352 | RTC_DCHECK_RUN_ON(thread_); |
| 353 | return stopping_; |
| 354 | } |
| 355 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 356 | RtpTransceiverDirection RtpTransceiver::direction() const { |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 357 | if (unified_plan_ && stopping()) |
| 358 | return webrtc::RtpTransceiverDirection::kStopped; |
| 359 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 360 | return direction_; |
| 361 | } |
| 362 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 363 | RTCError RtpTransceiver::SetDirectionWithError( |
| 364 | RtpTransceiverDirection new_direction) { |
| 365 | if (unified_plan_ && stopping()) { |
| 366 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 367 | "Cannot set direction on a stopping transceiver."); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 368 | } |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 369 | if (new_direction == direction_) |
| 370 | return RTCError::OK(); |
| 371 | |
| 372 | if (new_direction == RtpTransceiverDirection::kStopped) { |
| 373 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 374 | "The set direction 'stopped' is invalid."); |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 375 | } |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 376 | |
Steve Anton | 52d8677 | 2018-02-20 15:48:12 -0800 | [diff] [blame] | 377 | direction_ = new_direction; |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 +0000 | [diff] [blame] | 378 | on_negotiation_needed_(); |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 379 | |
| 380 | return RTCError::OK(); |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 383 | absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction() |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 384 | const { |
Harald Alvestrand | c75c428 | 2020-08-26 12:17:54 +0000 | [diff] [blame] | 385 | if (unified_plan_ && stopped()) |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 386 | return webrtc::RtpTransceiverDirection::kStopped; |
| 387 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 388 | return current_direction_; |
| 389 | } |
| 390 | |
Steve Anton | 0f5400a | 2018-07-17 14:25:36 -0700 | [diff] [blame] | 391 | absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction() |
| 392 | const { |
| 393 | return fired_direction_; |
| 394 | } |
| 395 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 396 | void RtpTransceiver::StopSendingAndReceiving() { |
| 397 | // 1. Let sender be transceiver.[[Sender]]. |
| 398 | // 2. Let receiver be transceiver.[[Receiver]]. |
| 399 | // |
| 400 | // 3. Stop sending media with sender. |
| 401 | // |
| 402 | // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as |
| 403 | // specified in [RFC3550]. |
| 404 | RTC_DCHECK_RUN_ON(thread_); |
| 405 | for (const auto& sender : senders_) |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 406 | sender->internal()->Stop(); |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 407 | |
| 408 | // 5. Stop receiving media with receiver. |
| 409 | for (const auto& receiver : receivers_) |
Harald Alvestrand | 1ee3325 | 2020-09-24 13:31:15 +0000 | [diff] [blame] | 410 | receiver->internal()->StopAndEndTrack(); |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 411 | |
| 412 | stopping_ = true; |
| 413 | direction_ = webrtc::RtpTransceiverDirection::kInactive; |
| 414 | } |
| 415 | |
| 416 | RTCError RtpTransceiver::StopStandard() { |
| 417 | RTC_DCHECK_RUN_ON(thread_); |
Harald Alvestrand | c75c428 | 2020-08-26 12:17:54 +0000 | [diff] [blame] | 418 | // If we're on Plan B, do what Stop() used to do there. |
| 419 | if (!unified_plan_) { |
| 420 | StopInternal(); |
| 421 | return RTCError::OK(); |
| 422 | } |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 423 | // 1. Let transceiver be the RTCRtpTransceiver object on which the method is |
| 424 | // invoked. |
| 425 | // |
| 426 | // 2. Let connection be the RTCPeerConnection object associated with |
| 427 | // transceiver. |
| 428 | // |
| 429 | // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError. |
| 430 | if (is_pc_closed_) { |
| 431 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 432 | "PeerConnection is closed."); |
Harald Alvestrand | a88c977 | 2020-08-10 18:06:09 +0000 | [diff] [blame] | 433 | } |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 434 | |
| 435 | // 4. If transceiver.[[Stopping]] is true, abort these steps. |
| 436 | if (stopping_) |
| 437 | return RTCError::OK(); |
| 438 | |
| 439 | // 5. Stop sending and receiving given transceiver, and update the |
| 440 | // negotiation-needed flag for connection. |
| 441 | StopSendingAndReceiving(); |
Harald Alvestrand | 280054f | 2020-11-10 13:12:53 +0000 | [diff] [blame] | 442 | on_negotiation_needed_(); |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 443 | |
| 444 | return RTCError::OK(); |
| 445 | } |
| 446 | |
| 447 | void RtpTransceiver::StopInternal() { |
Harald Alvestrand | 8546666 | 2021-04-19 21:21:36 +0000 | [diff] [blame] | 448 | RTC_DCHECK_RUN_ON(thread_); |
Harald Alvestrand | c75c428 | 2020-08-26 12:17:54 +0000 | [diff] [blame] | 449 | StopTransceiverProcedure(); |
| 450 | } |
| 451 | |
| 452 | void RtpTransceiver::StopTransceiverProcedure() { |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 453 | RTC_DCHECK_RUN_ON(thread_); |
Harald Alvestrand | c75c428 | 2020-08-26 12:17:54 +0000 | [diff] [blame] | 454 | // As specified in the "Stop the RTCRtpTransceiver" procedure |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 455 | // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given |
| 456 | // transceiver. |
| 457 | if (!stopping_) |
| 458 | StopSendingAndReceiving(); |
| 459 | |
| 460 | // 2. Set transceiver.[[Stopped]] to true. |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 461 | stopped_ = true; |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 462 | |
| 463 | // Signal the updated change to the senders. |
| 464 | for (const auto& sender : senders_) |
| 465 | sender->internal()->SetTransceiverAsStopped(); |
| 466 | |
| 467 | // 3. Set transceiver.[[Receptive]] to false. |
| 468 | // 4. Set transceiver.[[CurrentDirection]] to null. |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 469 | current_direction_ = absl::nullopt; |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 470 | } |
| 471 | |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 472 | RTCError RtpTransceiver::SetCodecPreferences( |
| 473 | rtc::ArrayView<RtpCodecCapability> codec_capabilities) { |
| 474 | RTC_DCHECK(unified_plan_); |
| 475 | |
| 476 | // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot |
| 477 | // to codecs and abort these steps. |
| 478 | if (codec_capabilities.empty()) { |
| 479 | codec_preferences_.clear(); |
| 480 | return RTCError::OK(); |
| 481 | } |
| 482 | |
| 483 | // 4. Remove any duplicate values in codecs. |
| 484 | std::vector<RtpCodecCapability> codecs; |
| 485 | absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs), |
| 486 | [&codecs](const RtpCodecCapability& codec) { |
| 487 | return absl::c_linear_search(codecs, codec); |
| 488 | }); |
| 489 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 490 | // 6. to 8. |
| 491 | RTCError result; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 492 | if (media_type_ == cricket::MEDIA_TYPE_AUDIO) { |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 493 | std::vector<cricket::AudioCodec> recv_codecs, send_codecs; |
| 494 | channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs); |
| 495 | channel_manager_->GetSupportedAudioSendCodecs(&send_codecs); |
| 496 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 497 | result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 498 | } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) { |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 499 | std::vector<cricket::VideoCodec> recv_codecs, send_codecs; |
| 500 | channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs); |
| 501 | channel_manager_->GetSupportedVideoSendCodecs(&send_codecs); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 502 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 503 | result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs); |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 506 | if (result.ok()) { |
| 507 | codec_preferences_ = codecs; |
Florent Castelli | 2d9d82e | 2019-04-23 19:25:51 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 510 | return result; |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 513 | std::vector<RtpHeaderExtensionCapability> |
| 514 | RtpTransceiver::HeaderExtensionsToOffer() const { |
Markus Handell | 755c65d | 2020-06-24 01:06:10 +0200 | [diff] [blame] | 515 | return header_extensions_to_offer_; |
| 516 | } |
| 517 | |
Markus Handell | 5932fe1 | 2020-12-17 22:19:40 +0100 | [diff] [blame] | 518 | std::vector<RtpHeaderExtensionCapability> |
| 519 | RtpTransceiver::HeaderExtensionsNegotiated() const { |
Tommi | cc7a368 | 2021-05-04 14:59:38 +0200 | [diff] [blame] | 520 | RTC_DCHECK_RUN_ON(thread_); |
Markus Handell | 5932fe1 | 2020-12-17 22:19:40 +0100 | [diff] [blame] | 521 | std::vector<RtpHeaderExtensionCapability> result; |
Tommi | cc7a368 | 2021-05-04 14:59:38 +0200 | [diff] [blame] | 522 | for (const auto& ext : negotiated_header_extensions_) { |
Markus Handell | 5932fe1 | 2020-12-17 22:19:40 +0100 | [diff] [blame] | 523 | result.emplace_back(ext.uri, ext.id, RtpTransceiverDirection::kSendRecv); |
| 524 | } |
| 525 | return result; |
| 526 | } |
| 527 | |
Markus Handell | 755c65d | 2020-06-24 01:06:10 +0200 | [diff] [blame] | 528 | RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions( |
| 529 | rtc::ArrayView<const RtpHeaderExtensionCapability> |
| 530 | header_extensions_to_offer) { |
| 531 | for (const auto& entry : header_extensions_to_offer) { |
| 532 | // Handle unsupported requests for mandatory extensions as per |
| 533 | // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface. |
| 534 | // Note: |
| 535 | // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1, |
| 536 | // this has to be checked on a higher level. We naturally error out |
| 537 | // in the handling of Step 2.2 if an unset URI is encountered. |
| 538 | |
| 539 | // Step 2.2. |
| 540 | // Handle unknown extensions. |
| 541 | auto it = std::find_if( |
| 542 | header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(), |
| 543 | [&entry](const auto& offered) { return entry.uri == offered.uri; }); |
| 544 | if (it == header_extensions_to_offer_.end()) { |
Markus Handell | c17bca7 | 2021-01-14 17:08:01 +0100 | [diff] [blame] | 545 | return RTCError(RTCErrorType::UNSUPPORTED_PARAMETER, |
Markus Handell | 755c65d | 2020-06-24 01:06:10 +0200 | [diff] [blame] | 546 | "Attempted to modify an unoffered extension."); |
| 547 | } |
| 548 | |
| 549 | // Step 2.4-2.5. |
| 550 | // - Use of the transceiver interface indicates unified plan is in effect, |
| 551 | // hence the MID extension needs to be enabled. |
| 552 | // - Also handle the mandatory video orientation extensions. |
| 553 | if ((entry.uri == RtpExtension::kMidUri || |
| 554 | entry.uri == RtpExtension::kVideoRotationUri) && |
| 555 | entry.direction != RtpTransceiverDirection::kSendRecv) { |
| 556 | return RTCError(RTCErrorType::INVALID_MODIFICATION, |
| 557 | "Attempted to stop a mandatory extension."); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // Apply mutation after error checking. |
| 562 | for (const auto& entry : header_extensions_to_offer) { |
| 563 | auto it = std::find_if( |
| 564 | header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(), |
| 565 | [&entry](const auto& offered) { return entry.uri == offered.uri; }); |
| 566 | it->direction = entry.direction; |
| 567 | } |
| 568 | |
| 569 | return RTCError::OK(); |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Tommi | cc7a368 | 2021-05-04 14:59:38 +0200 | [diff] [blame] | 572 | void RtpTransceiver::OnNegotiationUpdate( |
| 573 | SdpType sdp_type, |
| 574 | const cricket::MediaContentDescription* content) { |
| 575 | RTC_DCHECK_RUN_ON(thread_); |
| 576 | RTC_DCHECK(content); |
| 577 | if (sdp_type == SdpType::kAnswer) |
| 578 | negotiated_header_extensions_ = content->rtp_header_extensions(); |
| 579 | } |
| 580 | |
Harald Alvestrand | 6060df5 | 2020-08-11 09:54:02 +0200 | [diff] [blame] | 581 | void RtpTransceiver::SetPeerConnectionClosed() { |
| 582 | is_pc_closed_ = true; |
| 583 | } |
| 584 | |
Steve Anton | 6e634bf | 2017-11-13 10:44:53 -0800 | [diff] [blame] | 585 | } // namespace webrtc |