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