blob: db49468c07f5996dcacaf903b06ba86ea2b72f72 [file] [log] [blame]
Steve Anton6e634bf2017-11-13 10:44:53 -08001/*
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 Anton10542f22019-01-11 09:11:00 -080011#include "pc/rtp_transceiver.h"
Steve Anton6e634bf2017-11-13 10:44:53 -080012
Harald Alvestrandc24a2182022-02-23 13:44:59 +000013#include <algorithm>
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000014#include <iterator>
Steve Anton6e634bf2017-11-13 10:44:53 -080015#include <string>
Markus Handell0357b3e2020-03-16 13:40:51 +010016#include <utility>
Markus Handell5932fe12020-12-17 22:19:40 +010017#include <vector>
Steve Anton6e634bf2017-11-13 10:44:53 -080018
Steve Anton64b626b2019-01-28 17:25:26 -080019#include "absl/algorithm/container.h"
Harald Alvestrand485457f2022-05-23 08:46:57 +000020#include "absl/memory/memory.h"
Harald Alvestrand8f429922022-05-04 10:32:30 +000021#include "api/peer_connection_interface.h"
Markus Handell0357b3e2020-03-16 13:40:51 +010022#include "api/rtp_parameters.h"
Artem Titovd15a5752021-02-10 14:31:24 +010023#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000024#include "media/base/codec.h"
25#include "media/base/media_constants.h"
Harald Alvestrand8101e7b2022-05-23 14:57:47 +000026#include "media/base/media_engine.h"
Harald Alvestrand8f429922022-05-04 10:32:30 +000027#include "pc/channel.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "pc/rtp_media_utils.h"
Markus Handell5932fe12020-12-17 22:19:40 +010029#include "pc/session_description.h"
Yves Gerey3e707812018-11-28 16:47:49 +010030#include "rtc_base/checks.h"
31#include "rtc_base/logging.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000032#include "rtc_base/thread.h"
Steve Antondcc3c022017-12-22 16:02:54 -080033
Steve Anton6e634bf2017-11-13 10:44:53 -080034namespace webrtc {
Johannes Kron3e983682020-03-29 22:17:00 +020035namespace {
36template <class T>
37RTCError 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
Harald Alvestrand6060df52020-08-11 09:54:02 +0200110TaskQueueBase* GetCurrentTaskQueueOrThread() {
111 TaskQueueBase* current = TaskQueueBase::Current();
112 if (!current)
113 current = rtc::ThreadManager::Instance()->CurrentThread();
114 return current;
115}
116
Johannes Kron3e983682020-03-29 22:17:00 +0200117} // namespace
Steve Anton6e634bf2017-11-13 10:44:53 -0800118
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000119RtpTransceiver::RtpTransceiver(cricket::MediaType media_type,
120 ConnectionContext* context)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200121 : thread_(GetCurrentTaskQueueOrThread()),
122 unified_plan_(false),
Tommi99c8a802021-04-27 15:00:00 +0200123 media_type_(media_type),
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000124 context_(context) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800125 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
126 media_type == cricket::MEDIA_TYPE_VIDEO);
127}
128
Steve Anton79e79602017-11-20 10:25:56 -0800129RtpTransceiver::RtpTransceiver(
130 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
131 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200132 receiver,
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000133 ConnectionContext* context,
Harald Alvestrand280054f2020-11-10 13:12:53 +0000134 std::vector<RtpHeaderExtensionCapability> header_extensions_offered,
135 std::function<void()> on_negotiation_needed)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200136 : thread_(GetCurrentTaskQueueOrThread()),
137 unified_plan_(true),
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200138 media_type_(sender->media_type()),
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000139 context_(context),
Harald Alvestrand280054f2020-11-10 13:12:53 +0000140 header_extensions_to_offer_(std::move(header_extensions_offered)),
141 on_negotiation_needed_(std::move(on_negotiation_needed)) {
Steve Anton79e79602017-11-20 10:25:56 -0800142 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
143 media_type_ == cricket::MEDIA_TYPE_VIDEO);
144 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
145 senders_.push_back(sender);
146 receivers_.push_back(receiver);
147}
148
Steve Anton6e634bf2017-11-13 10:44:53 -0800149RtpTransceiver::~RtpTransceiver() {
Tommi99c8a802021-04-27 15:00:00 +0200150 // TODO(tommi): On Android, when running PeerConnectionClientTest (e.g.
151 // PeerConnectionClientTest#testCameraSwitch), the instance doesn't get
152 // deleted on `thread_`. See if we can fix that.
Harald Alvestrand85466662021-04-19 21:21:36 +0000153 if (!stopped_) {
154 RTC_DCHECK_RUN_ON(thread_);
155 StopInternal();
156 }
Tomas Gunnarsson16de2162022-01-26 10:21:57 +0100157
Harald Alvestrand8f429922022-05-04 10:32:30 +0000158 RTC_CHECK(!channel_) << "Missing call to ClearChannel?";
159}
160
161RTCError RtpTransceiver::CreateChannel(
162 absl::string_view mid,
163 Call* call_ptr,
164 const cricket::MediaConfig& media_config,
165 bool srtp_required,
166 CryptoOptions crypto_options,
167 const cricket::AudioOptions& audio_options,
168 const cricket::VideoOptions& video_options,
169 VideoBitrateAllocatorFactory* video_bitrate_allocator_factory,
170 std::function<RtpTransportInternal*(absl::string_view)> transport_lookup) {
171 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000172 if (!media_engine()) {
Harald Alvestrand8f429922022-05-04 10:32:30 +0000173 // TODO(hta): Must be a better way
174 return RTCError(RTCErrorType::INTERNAL_ERROR,
175 "No media engine for mid=" + std::string(mid));
176 }
177 std::unique_ptr<cricket::ChannelInterface> new_channel;
178 if (media_type() == cricket::MEDIA_TYPE_AUDIO) {
179 // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to
180 // the worker thread. We shouldn't be using the `call_ptr_` hack here but
181 // simply be on the worker thread and use `call_` (update upstream code).
Harald Alvestrand485457f2022-05-23 08:46:57 +0000182 RTC_DCHECK(call_ptr);
183 RTC_DCHECK(media_engine());
184 // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in
185 // PeerConnection and add the expectation that we're already on the right
186 // thread.
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200187 context()->worker_thread()->BlockingCall([&] {
188 RTC_DCHECK_RUN_ON(context()->worker_thread());
Harald Alvestrand8f429922022-05-04 10:32:30 +0000189
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200190 cricket::VoiceMediaChannel* media_channel =
191 media_engine()->voice().CreateMediaChannel(
192 call_ptr, media_config, audio_options, crypto_options);
193 if (!media_channel) {
194 return;
195 }
Harald Alvestrand485457f2022-05-23 08:46:57 +0000196
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200197 new_channel = std::make_unique<cricket::VoiceChannel>(
198 context()->worker_thread(), context()->network_thread(),
199 context()->signaling_thread(), absl::WrapUnique(media_channel), mid,
200 srtp_required, crypto_options, context()->ssrc_generator());
201 });
Harald Alvestrand8f429922022-05-04 10:32:30 +0000202 } else {
203 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, media_type());
204
205 // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to
206 // the worker thread. We shouldn't be using the `call_ptr_` hack here but
207 // simply be on the worker thread and use `call_` (update upstream code).
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200208 context()->worker_thread()->BlockingCall([&] {
209 RTC_DCHECK_RUN_ON(context()->worker_thread());
210 cricket::VideoMediaChannel* media_channel =
211 media_engine()->video().CreateMediaChannel(
212 call_ptr, media_config, video_options, crypto_options,
213 video_bitrate_allocator_factory);
214 if (!media_channel) {
215 return;
216 }
Harald Alvestrand485457f2022-05-23 08:46:57 +0000217
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200218 new_channel = std::make_unique<cricket::VideoChannel>(
219 context()->worker_thread(), context()->network_thread(),
220 context()->signaling_thread(), absl::WrapUnique(media_channel), mid,
221 srtp_required, crypto_options, context()->ssrc_generator());
222 });
Harald Alvestrand8f429922022-05-04 10:32:30 +0000223 }
224 if (!new_channel) {
225 // TODO(hta): Must be a better way
226 return RTCError(RTCErrorType::INTERNAL_ERROR,
227 "Failed to create channel for mid=" + std::string(mid));
228 }
229 SetChannel(std::move(new_channel), transport_lookup);
230 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800231}
232
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100233void RtpTransceiver::SetChannel(
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000234 std::unique_ptr<cricket::ChannelInterface> channel,
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100235 std::function<RtpTransportInternal*(const std::string&)> transport_lookup) {
Tommi99c8a802021-04-27 15:00:00 +0200236 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestranddaee8702022-04-29 11:42:55 +0000237 RTC_DCHECK(channel);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000238 RTC_DCHECK(transport_lookup);
Harald Alvestranddaee8702022-04-29 11:42:55 +0000239 RTC_DCHECK(!channel_);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000240 // Cannot set a channel on a stopped transceiver.
Harald Alvestranddaee8702022-04-29 11:42:55 +0000241 if (stopped_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800242 return;
243 }
244
Tommife041642021-04-07 10:08:28 +0200245 RTC_LOG_THREAD_BLOCK_COUNT();
246
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000247 RTC_DCHECK_EQ(media_type(), channel->media_type());
248 signaling_thread_safety_ = PendingTaskSafetyFlag::Create();
Steve Anton60776752018-01-10 11:51:34 -0800249
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000250 std::unique_ptr<cricket::ChannelInterface> channel_to_delete;
251
Tommi99c8a802021-04-27 15:00:00 +0200252 // An alternative to this, could be to require SetChannel to be called
253 // on the network thread. The channel object operates for the most part
254 // on the network thread, as part of its initialization being on the network
255 // thread is required, so setting a channel object as part of the construction
256 // (without thread hopping) might be the more efficient thing to do than
257 // how SetChannel works today.
258 // Similarly, if the channel() accessor is limited to the network thread, that
259 // helps with keeping the channel implementation requirements being met and
260 // avoids synchronization for accessing the pointer or network related state.
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200261 context()->network_thread()->BlockingCall([&]() {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000262 if (channel_) {
263 channel_->SetFirstPacketReceivedCallback(nullptr);
264 channel_->SetRtpTransport(nullptr);
265 channel_to_delete = std::move(channel_);
266 }
267
268 channel_ = std::move(channel);
Steve Anton60776752018-01-10 11:51:34 -0800269
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000270 channel_->SetRtpTransport(transport_lookup(channel_->mid()));
271 channel_->SetFirstPacketReceivedCallback(
272 [thread = thread_, flag = signaling_thread_safety_, this]() mutable {
Danil Chapovalova30439b2022-07-07 10:08:49 +0200273 thread->PostTask(
274 SafeTask(std::move(flag), [this]() { OnFirstPacketReceived(); }));
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000275 });
Tommi99c8a802021-04-27 15:00:00 +0200276 });
Harald Alvestranddaee8702022-04-29 11:42:55 +0000277 PushNewMediaChannelAndDeleteChannel(nullptr);
Tommi6589def2022-02-17 23:36:47 +0100278
279 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2);
Steve Anton6e634bf2017-11-13 10:44:53 -0800280}
281
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000282void RtpTransceiver::ClearChannel() {
283 RTC_DCHECK_RUN_ON(thread_);
284
285 if (!channel_) {
286 return;
287 }
288
289 RTC_LOG_THREAD_BLOCK_COUNT();
290
291 if (channel_) {
292 signaling_thread_safety_->SetNotAlive();
293 signaling_thread_safety_ = nullptr;
294 }
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000295 std::unique_ptr<cricket::ChannelInterface> channel_to_delete;
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000296
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200297 context()->network_thread()->BlockingCall([&]() {
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000298 if (channel_) {
299 channel_->SetFirstPacketReceivedCallback(nullptr);
300 channel_->SetRtpTransport(nullptr);
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000301 channel_to_delete = std::move(channel_);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000302 }
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000303 });
304
305 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1);
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000306 PushNewMediaChannelAndDeleteChannel(std::move(channel_to_delete));
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000307
308 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2);
309}
310
Harald Alvestranddaee8702022-04-29 11:42:55 +0000311void RtpTransceiver::PushNewMediaChannelAndDeleteChannel(
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000312 std::unique_ptr<cricket::ChannelInterface> channel_to_delete) {
Harald Alvestranddaee8702022-04-29 11:42:55 +0000313 // The clumsy combination of pushing down media channel and deleting
314 // the channel is due to the desire to do both things in one Invoke().
315 if (!channel_to_delete && senders_.empty() && receivers_.empty()) {
316 return;
317 }
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200318 context()->worker_thread()->BlockingCall([&]() {
Harald Alvestranddaee8702022-04-29 11:42:55 +0000319 // Push down the new media_channel, if any, otherwise clear it.
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000320 auto* media_channel = channel_ ? channel_->media_channel() : nullptr;
321 for (const auto& sender : senders_) {
322 sender->internal()->SetMediaChannel(media_channel);
323 }
324
325 for (const auto& receiver : receivers_) {
326 receiver->internal()->SetMediaChannel(media_channel);
327 }
328
329 // Destroy the channel, if we had one, now _after_ updating the receivers
330 // who might have had references to the previous channel.
331 if (channel_to_delete) {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000332 channel_to_delete.reset(nullptr);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000333 }
334 });
335}
336
Steve Anton6e634bf2017-11-13 10:44:53 -0800337void RtpTransceiver::AddSender(
338 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
Tommi99c8a802021-04-27 15:00:00 +0200339 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800340 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800341 RTC_DCHECK(!unified_plan_);
342 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -0800343 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800344 RTC_DCHECK(!absl::c_linear_search(senders_, sender));
Steve Anton6e634bf2017-11-13 10:44:53 -0800345 senders_.push_back(sender);
346}
347
348bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
349 RTC_DCHECK(!unified_plan_);
350 if (sender) {
351 RTC_DCHECK_EQ(media_type(), sender->media_type());
352 }
Steve Anton64b626b2019-01-28 17:25:26 -0800353 auto it = absl::c_find(senders_, sender);
Steve Anton6e634bf2017-11-13 10:44:53 -0800354 if (it == senders_.end()) {
355 return false;
356 }
357 (*it)->internal()->Stop();
358 senders_.erase(it);
359 return true;
360}
361
362void RtpTransceiver::AddReceiver(
363 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
364 receiver) {
Tommi99c8a802021-04-27 15:00:00 +0200365 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800366 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800367 RTC_DCHECK(!unified_plan_);
368 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800369 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800370 RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
Steve Anton6e634bf2017-11-13 10:44:53 -0800371 receivers_.push_back(receiver);
372}
373
374bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
Tommi6589def2022-02-17 23:36:47 +0100375 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800376 RTC_DCHECK(!unified_plan_);
377 if (receiver) {
378 RTC_DCHECK_EQ(media_type(), receiver->media_type());
379 }
Steve Anton64b626b2019-01-28 17:25:26 -0800380 auto it = absl::c_find(receivers_, receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -0800381 if (it == receivers_.end()) {
382 return false;
383 }
Tommi6589def2022-02-17 23:36:47 +0100384
Steve Anton6e634bf2017-11-13 10:44:53 -0800385 (*it)->internal()->Stop();
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200386 context()->worker_thread()->BlockingCall([&]() {
Tommi6589def2022-02-17 23:36:47 +0100387 // `Stop()` will clear the receiver's pointer to the media channel.
388 (*it)->internal()->SetMediaChannel(nullptr);
389 });
390
Steve Anton6e634bf2017-11-13 10:44:53 -0800391 receivers_.erase(it);
392 return true;
393}
394
Steve Antonf9381f02017-12-14 10:23:57 -0800395rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
396 RTC_DCHECK(unified_plan_);
397 RTC_CHECK_EQ(1u, senders_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100398 return rtc::scoped_refptr<RtpSenderInternal>(senders_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800399}
400
401rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
402 const {
403 RTC_DCHECK(unified_plan_);
404 RTC_CHECK_EQ(1u, receivers_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100405 return rtc::scoped_refptr<RtpReceiverInternal>(receivers_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800406}
407
Steve Anton69470252018-02-09 11:43:08 -0800408cricket::MediaType RtpTransceiver::media_type() const {
409 return media_type_;
410}
411
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200412absl::optional<std::string> RtpTransceiver::mid() const {
Steve Anton6e634bf2017-11-13 10:44:53 -0800413 return mid_;
414}
415
Tommi99c8a802021-04-27 15:00:00 +0200416void RtpTransceiver::OnFirstPacketReceived() {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100417 for (const auto& receiver : receivers_) {
Steve Anton60776752018-01-10 11:51:34 -0800418 receiver->internal()->NotifyFirstPacketReceived();
419 }
420}
421
Steve Anton6e634bf2017-11-13 10:44:53 -0800422rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
423 RTC_DCHECK(unified_plan_);
424 RTC_CHECK_EQ(1u, senders_.size());
425 return senders_[0];
426}
427
428rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
429 RTC_DCHECK(unified_plan_);
430 RTC_CHECK_EQ(1u, receivers_.size());
431 return receivers_[0];
432}
433
Steve Antondcc3c022017-12-22 16:02:54 -0800434void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700435 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
436 << ") current direction from "
437 << (current_direction_ ? RtpTransceiverDirectionToString(
438 *current_direction_)
439 : "<not set>")
440 << " to " << RtpTransceiverDirectionToString(direction)
441 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800442 current_direction_ = direction;
443 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
444 has_ever_been_used_to_send_ = true;
445 }
446}
447
Henrik Boström0a162762022-05-02 15:47:52 +0200448void RtpTransceiver::set_fired_direction(
449 absl::optional<RtpTransceiverDirection> direction) {
Steve Anton0f5400a2018-07-17 14:25:36 -0700450 fired_direction_ = direction;
451}
452
Steve Anton6e634bf2017-11-13 10:44:53 -0800453bool RtpTransceiver::stopped() const {
Tommi99c8a802021-04-27 15:00:00 +0200454 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800455 return stopped_;
456}
457
Harald Alvestrand6060df52020-08-11 09:54:02 +0200458bool RtpTransceiver::stopping() const {
459 RTC_DCHECK_RUN_ON(thread_);
460 return stopping_;
461}
462
Steve Anton6e634bf2017-11-13 10:44:53 -0800463RtpTransceiverDirection RtpTransceiver::direction() const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200464 if (unified_plan_ && stopping())
465 return webrtc::RtpTransceiverDirection::kStopped;
466
Steve Anton6e634bf2017-11-13 10:44:53 -0800467 return direction_;
468}
469
Harald Alvestrand6060df52020-08-11 09:54:02 +0200470RTCError RtpTransceiver::SetDirectionWithError(
471 RtpTransceiverDirection new_direction) {
472 if (unified_plan_ && stopping()) {
473 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
474 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800475 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200476 if (new_direction == direction_)
477 return RTCError::OK();
478
479 if (new_direction == RtpTransceiverDirection::kStopped) {
480 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
481 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800482 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200483
Steve Anton52d86772018-02-20 15:48:12 -0800484 direction_ = new_direction;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000485 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200486
487 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800488}
489
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200490absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800491 const {
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000492 if (unified_plan_ && stopped())
Harald Alvestrand6060df52020-08-11 09:54:02 +0200493 return webrtc::RtpTransceiverDirection::kStopped;
494
Steve Anton6e634bf2017-11-13 10:44:53 -0800495 return current_direction_;
496}
497
Steve Anton0f5400a2018-07-17 14:25:36 -0700498absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
499 const {
500 return fired_direction_;
501}
502
Harald Alvestrand6060df52020-08-11 09:54:02 +0200503void RtpTransceiver::StopSendingAndReceiving() {
504 // 1. Let sender be transceiver.[[Sender]].
505 // 2. Let receiver be transceiver.[[Receiver]].
506 //
507 // 3. Stop sending media with sender.
508 //
Tommi6589def2022-02-17 23:36:47 +0100509 RTC_DCHECK_RUN_ON(thread_);
510
Harald Alvestrand6060df52020-08-11 09:54:02 +0200511 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
512 // specified in [RFC3550].
Harald Alvestrand6060df52020-08-11 09:54:02 +0200513 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800514 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200515
Tommi6589def2022-02-17 23:36:47 +0100516 // Signal to receiver sources that we're stopping.
Harald Alvestrand6060df52020-08-11 09:54:02 +0200517 for (const auto& receiver : receivers_)
Tommi6589def2022-02-17 23:36:47 +0100518 receiver->internal()->Stop();
519
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200520 context()->worker_thread()->BlockingCall([&]() {
Tommi6589def2022-02-17 23:36:47 +0100521 // 5 Stop receiving media with receiver.
522 for (const auto& receiver : receivers_)
523 receiver->internal()->SetMediaChannel(nullptr);
524 });
Harald Alvestrand6060df52020-08-11 09:54:02 +0200525
526 stopping_ = true;
527 direction_ = webrtc::RtpTransceiverDirection::kInactive;
528}
529
530RTCError RtpTransceiver::StopStandard() {
531 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000532 // If we're on Plan B, do what Stop() used to do there.
533 if (!unified_plan_) {
534 StopInternal();
535 return RTCError::OK();
536 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200537 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
538 // invoked.
539 //
540 // 2. Let connection be the RTCPeerConnection object associated with
541 // transceiver.
542 //
543 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
544 if (is_pc_closed_) {
545 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
546 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000547 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200548
549 // 4. If transceiver.[[Stopping]] is true, abort these steps.
550 if (stopping_)
551 return RTCError::OK();
552
553 // 5. Stop sending and receiving given transceiver, and update the
554 // negotiation-needed flag for connection.
555 StopSendingAndReceiving();
Harald Alvestrand280054f2020-11-10 13:12:53 +0000556 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200557
558 return RTCError::OK();
559}
560
561void RtpTransceiver::StopInternal() {
Harald Alvestrand85466662021-04-19 21:21:36 +0000562 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000563 StopTransceiverProcedure();
564}
565
566void RtpTransceiver::StopTransceiverProcedure() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200567 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000568 // As specified in the "Stop the RTCRtpTransceiver" procedure
Harald Alvestrand6060df52020-08-11 09:54:02 +0200569 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
570 // transceiver.
571 if (!stopping_)
572 StopSendingAndReceiving();
573
574 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800575 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200576
577 // Signal the updated change to the senders.
578 for (const auto& sender : senders_)
579 sender->internal()->SetTransceiverAsStopped();
580
581 // 3. Set transceiver.[[Receptive]] to false.
582 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200583 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800584}
585
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200586RTCError RtpTransceiver::SetCodecPreferences(
587 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
588 RTC_DCHECK(unified_plan_);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200589 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
590 // to codecs and abort these steps.
591 if (codec_capabilities.empty()) {
592 codec_preferences_.clear();
593 return RTCError::OK();
594 }
595
596 // 4. Remove any duplicate values in codecs.
597 std::vector<RtpCodecCapability> codecs;
598 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
599 [&codecs](const RtpCodecCapability& codec) {
600 return absl::c_linear_search(codecs, codec);
601 });
602
Johannes Kron3e983682020-03-29 22:17:00 +0200603 // 6. to 8.
604 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200605 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200606 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000607 send_codecs = media_engine()->voice().send_codecs();
608 recv_codecs = media_engine()->voice().recv_codecs();
Johannes Kron3e983682020-03-29 22:17:00 +0200609 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200610 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200611 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000612 send_codecs = media_engine()->video().send_codecs(context()->use_rtx());
613 recv_codecs = media_engine()->video().recv_codecs(context()->use_rtx());
Johannes Kron3e983682020-03-29 22:17:00 +0200614 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200615 }
616
Johannes Kron3e983682020-03-29 22:17:00 +0200617 if (result.ok()) {
618 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200619 }
620
Johannes Kron3e983682020-03-29 22:17:00 +0200621 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800622}
623
Markus Handell0357b3e2020-03-16 13:40:51 +0100624std::vector<RtpHeaderExtensionCapability>
625RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200626 return header_extensions_to_offer_;
627}
628
Markus Handell5932fe12020-12-17 22:19:40 +0100629std::vector<RtpHeaderExtensionCapability>
630RtpTransceiver::HeaderExtensionsNegotiated() const {
Tommicc7a3682021-05-04 14:59:38 +0200631 RTC_DCHECK_RUN_ON(thread_);
Markus Handell5932fe12020-12-17 22:19:40 +0100632 std::vector<RtpHeaderExtensionCapability> result;
Tommicc7a3682021-05-04 14:59:38 +0200633 for (const auto& ext : negotiated_header_extensions_) {
Markus Handell5932fe12020-12-17 22:19:40 +0100634 result.emplace_back(ext.uri, ext.id, RtpTransceiverDirection::kSendRecv);
635 }
636 return result;
637}
638
Markus Handell755c65d2020-06-24 01:06:10 +0200639RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
640 rtc::ArrayView<const RtpHeaderExtensionCapability>
641 header_extensions_to_offer) {
642 for (const auto& entry : header_extensions_to_offer) {
643 // Handle unsupported requests for mandatory extensions as per
644 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
645 // Note:
646 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
647 // this has to be checked on a higher level. We naturally error out
648 // in the handling of Step 2.2 if an unset URI is encountered.
649
650 // Step 2.2.
651 // Handle unknown extensions.
652 auto it = std::find_if(
653 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
654 [&entry](const auto& offered) { return entry.uri == offered.uri; });
655 if (it == header_extensions_to_offer_.end()) {
Markus Handellc17bca72021-01-14 17:08:01 +0100656 return RTCError(RTCErrorType::UNSUPPORTED_PARAMETER,
Markus Handell755c65d2020-06-24 01:06:10 +0200657 "Attempted to modify an unoffered extension.");
658 }
659
660 // Step 2.4-2.5.
661 // - Use of the transceiver interface indicates unified plan is in effect,
662 // hence the MID extension needs to be enabled.
663 // - Also handle the mandatory video orientation extensions.
664 if ((entry.uri == RtpExtension::kMidUri ||
665 entry.uri == RtpExtension::kVideoRotationUri) &&
666 entry.direction != RtpTransceiverDirection::kSendRecv) {
667 return RTCError(RTCErrorType::INVALID_MODIFICATION,
668 "Attempted to stop a mandatory extension.");
669 }
670 }
671
672 // Apply mutation after error checking.
673 for (const auto& entry : header_extensions_to_offer) {
674 auto it = std::find_if(
675 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
676 [&entry](const auto& offered) { return entry.uri == offered.uri; });
677 it->direction = entry.direction;
678 }
679
680 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100681}
682
Tommicc7a3682021-05-04 14:59:38 +0200683void RtpTransceiver::OnNegotiationUpdate(
684 SdpType sdp_type,
685 const cricket::MediaContentDescription* content) {
686 RTC_DCHECK_RUN_ON(thread_);
687 RTC_DCHECK(content);
688 if (sdp_type == SdpType::kAnswer)
689 negotiated_header_extensions_ = content->rtp_header_extensions();
690}
691
Harald Alvestrand6060df52020-08-11 09:54:02 +0200692void RtpTransceiver::SetPeerConnectionClosed() {
693 is_pc_closed_ = true;
694}
695
Steve Anton6e634bf2017-11-13 10:44:53 -0800696} // namespace webrtc