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