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