blob: 44a96d4c618b0c2879bbdfcf1eea587639bb961e [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"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000031#include "rtc_base/location.h"
Yves Gerey3e707812018-11-28 16:47:49 +010032#include "rtc_base/logging.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000033#include "rtc_base/thread.h"
Steve Antondcc3c022017-12-22 16:02:54 -080034
Steve Anton6e634bf2017-11-13 10:44:53 -080035namespace webrtc {
Johannes Kron3e983682020-03-29 22:17:00 +020036namespace {
37template <class T>
38RTCError 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 Alvestrand6060df52020-08-11 09:54:02 +0200111TaskQueueBase* GetCurrentTaskQueueOrThread() {
112 TaskQueueBase* current = TaskQueueBase::Current();
113 if (!current)
114 current = rtc::ThreadManager::Instance()->CurrentThread();
115 return current;
116}
117
Johannes Kron3e983682020-03-29 22:17:00 +0200118} // namespace
Steve Anton6e634bf2017-11-13 10:44:53 -0800119
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000120RtpTransceiver::RtpTransceiver(cricket::MediaType media_type,
121 ConnectionContext* context)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200122 : thread_(GetCurrentTaskQueueOrThread()),
123 unified_plan_(false),
Tommi99c8a802021-04-27 15:00:00 +0200124 media_type_(media_type),
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000125 context_(context) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800126 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
127 media_type == cricket::MEDIA_TYPE_VIDEO);
128}
129
Steve Anton79e79602017-11-20 10:25:56 -0800130RtpTransceiver::RtpTransceiver(
131 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
132 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200133 receiver,
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000134 ConnectionContext* context,
Harald Alvestrand280054f2020-11-10 13:12:53 +0000135 std::vector<RtpHeaderExtensionCapability> header_extensions_offered,
136 std::function<void()> on_negotiation_needed)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200137 : thread_(GetCurrentTaskQueueOrThread()),
138 unified_plan_(true),
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200139 media_type_(sender->media_type()),
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000140 context_(context),
Harald Alvestrand280054f2020-11-10 13:12:53 +0000141 header_extensions_to_offer_(std::move(header_extensions_offered)),
142 on_negotiation_needed_(std::move(on_negotiation_needed)) {
Steve Anton79e79602017-11-20 10:25:56 -0800143 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
144 media_type_ == cricket::MEDIA_TYPE_VIDEO);
145 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
146 senders_.push_back(sender);
147 receivers_.push_back(receiver);
148}
149
Steve Anton6e634bf2017-11-13 10:44:53 -0800150RtpTransceiver::~RtpTransceiver() {
Tommi99c8a802021-04-27 15:00:00 +0200151 // TODO(tommi): On Android, when running PeerConnectionClientTest (e.g.
152 // PeerConnectionClientTest#testCameraSwitch), the instance doesn't get
153 // deleted on `thread_`. See if we can fix that.
Harald Alvestrand85466662021-04-19 21:21:36 +0000154 if (!stopped_) {
155 RTC_DCHECK_RUN_ON(thread_);
156 StopInternal();
157 }
Tomas Gunnarsson16de2162022-01-26 10:21:57 +0100158
Harald Alvestrand8f429922022-05-04 10:32:30 +0000159 RTC_CHECK(!channel_) << "Missing call to ClearChannel?";
160}
161
162RTCError RtpTransceiver::CreateChannel(
163 absl::string_view mid,
164 Call* call_ptr,
165 const cricket::MediaConfig& media_config,
166 bool srtp_required,
167 CryptoOptions crypto_options,
168 const cricket::AudioOptions& audio_options,
169 const cricket::VideoOptions& video_options,
170 VideoBitrateAllocatorFactory* video_bitrate_allocator_factory,
171 std::function<RtpTransportInternal*(absl::string_view)> transport_lookup) {
172 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000173 if (!media_engine()) {
Harald Alvestrand8f429922022-05-04 10:32:30 +0000174 // TODO(hta): Must be a better way
175 return RTCError(RTCErrorType::INTERNAL_ERROR,
176 "No media engine for mid=" + std::string(mid));
177 }
178 std::unique_ptr<cricket::ChannelInterface> new_channel;
179 if (media_type() == cricket::MEDIA_TYPE_AUDIO) {
180 // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to
181 // the worker thread. We shouldn't be using the `call_ptr_` hack here but
182 // simply be on the worker thread and use `call_` (update upstream code).
Harald Alvestrand485457f2022-05-23 08:46:57 +0000183 RTC_DCHECK(call_ptr);
184 RTC_DCHECK(media_engine());
185 // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in
186 // PeerConnection and add the expectation that we're already on the right
187 // thread.
188 new_channel =
189 context()
190 ->worker_thread()
191 ->Invoke<std::unique_ptr<cricket::VoiceChannel>>(
192 RTC_FROM_HERE, [&]() -> std::unique_ptr<cricket::VoiceChannel> {
193 RTC_DCHECK_RUN_ON(context()->worker_thread());
Harald Alvestrand8f429922022-05-04 10:32:30 +0000194
Harald Alvestrand485457f2022-05-23 08:46:57 +0000195 cricket::VoiceMediaChannel* media_channel =
196 media_engine()->voice().CreateMediaChannel(
197 call_ptr, media_config, audio_options,
198 crypto_options);
199 if (!media_channel) {
200 return nullptr;
201 }
202
203 auto voice_channel = std::make_unique<cricket::VoiceChannel>(
204 context()->worker_thread(), context()->network_thread(),
205 context()->signaling_thread(),
206 absl::WrapUnique(media_channel), mid, srtp_required,
207 crypto_options, context()->ssrc_generator());
208
209 return voice_channel;
210 });
Harald Alvestrand8f429922022-05-04 10:32:30 +0000211 } else {
212 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, media_type());
213
214 // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to
215 // the worker thread. We shouldn't be using the `call_ptr_` hack here but
216 // simply be on the worker thread and use `call_` (update upstream code).
Harald Alvestrand485457f2022-05-23 08:46:57 +0000217 new_channel =
218 context()
219 ->worker_thread()
220 ->Invoke<std::unique_ptr<cricket::VideoChannel>>(
221 RTC_FROM_HERE, [&]() -> std::unique_ptr<cricket::VideoChannel> {
222 RTC_DCHECK_RUN_ON(context()->worker_thread());
223 cricket::VideoMediaChannel* media_channel =
224 media_engine()->video().CreateMediaChannel(
225 call_ptr, media_config, video_options, crypto_options,
226 video_bitrate_allocator_factory);
227 if (!media_channel) {
228 return nullptr;
229 }
230
231 auto video_channel = std::make_unique<cricket::VideoChannel>(
232 context()->worker_thread(), context()->network_thread(),
233 context()->signaling_thread(),
234 absl::WrapUnique(media_channel), mid, srtp_required,
235 crypto_options, context()->ssrc_generator());
236
237 return video_channel;
238 });
Harald Alvestrand8f429922022-05-04 10:32:30 +0000239 }
240 if (!new_channel) {
241 // TODO(hta): Must be a better way
242 return RTCError(RTCErrorType::INTERNAL_ERROR,
243 "Failed to create channel for mid=" + std::string(mid));
244 }
245 SetChannel(std::move(new_channel), transport_lookup);
246 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800247}
248
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100249void RtpTransceiver::SetChannel(
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000250 std::unique_ptr<cricket::ChannelInterface> channel,
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100251 std::function<RtpTransportInternal*(const std::string&)> transport_lookup) {
Tommi99c8a802021-04-27 15:00:00 +0200252 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestranddaee8702022-04-29 11:42:55 +0000253 RTC_DCHECK(channel);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000254 RTC_DCHECK(transport_lookup);
Harald Alvestranddaee8702022-04-29 11:42:55 +0000255 RTC_DCHECK(!channel_);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000256 // Cannot set a channel on a stopped transceiver.
Harald Alvestranddaee8702022-04-29 11:42:55 +0000257 if (stopped_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800258 return;
259 }
260
Tommife041642021-04-07 10:08:28 +0200261 RTC_LOG_THREAD_BLOCK_COUNT();
262
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000263 RTC_DCHECK_EQ(media_type(), channel->media_type());
264 signaling_thread_safety_ = PendingTaskSafetyFlag::Create();
Steve Anton60776752018-01-10 11:51:34 -0800265
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000266 std::unique_ptr<cricket::ChannelInterface> channel_to_delete;
267
Tommi99c8a802021-04-27 15:00:00 +0200268 // An alternative to this, could be to require SetChannel to be called
269 // on the network thread. The channel object operates for the most part
270 // on the network thread, as part of its initialization being on the network
271 // thread is required, so setting a channel object as part of the construction
272 // (without thread hopping) might be the more efficient thing to do than
273 // how SetChannel works today.
274 // Similarly, if the channel() accessor is limited to the network thread, that
275 // helps with keeping the channel implementation requirements being met and
276 // avoids synchronization for accessing the pointer or network related state.
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000277 context()->network_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000278 if (channel_) {
279 channel_->SetFirstPacketReceivedCallback(nullptr);
280 channel_->SetRtpTransport(nullptr);
281 channel_to_delete = std::move(channel_);
282 }
283
284 channel_ = std::move(channel);
Steve Anton60776752018-01-10 11:51:34 -0800285
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000286 channel_->SetRtpTransport(transport_lookup(channel_->mid()));
287 channel_->SetFirstPacketReceivedCallback(
288 [thread = thread_, flag = signaling_thread_safety_, this]() mutable {
Danil Chapovalova30439b2022-07-07 10:08:49 +0200289 thread->PostTask(
290 SafeTask(std::move(flag), [this]() { OnFirstPacketReceived(); }));
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000291 });
Tommi99c8a802021-04-27 15:00:00 +0200292 });
Harald Alvestranddaee8702022-04-29 11:42:55 +0000293 PushNewMediaChannelAndDeleteChannel(nullptr);
Tommi6589def2022-02-17 23:36:47 +0100294
295 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2);
Steve Anton6e634bf2017-11-13 10:44:53 -0800296}
297
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000298void RtpTransceiver::ClearChannel() {
299 RTC_DCHECK_RUN_ON(thread_);
300
301 if (!channel_) {
302 return;
303 }
304
305 RTC_LOG_THREAD_BLOCK_COUNT();
306
307 if (channel_) {
308 signaling_thread_safety_->SetNotAlive();
309 signaling_thread_safety_ = nullptr;
310 }
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000311 std::unique_ptr<cricket::ChannelInterface> channel_to_delete;
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000312
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000313 context()->network_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000314 if (channel_) {
315 channel_->SetFirstPacketReceivedCallback(nullptr);
316 channel_->SetRtpTransport(nullptr);
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000317 channel_to_delete = std::move(channel_);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000318 }
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000319 });
320
321 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1);
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000322 PushNewMediaChannelAndDeleteChannel(std::move(channel_to_delete));
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000323
324 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2);
325}
326
Harald Alvestranddaee8702022-04-29 11:42:55 +0000327void RtpTransceiver::PushNewMediaChannelAndDeleteChannel(
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000328 std::unique_ptr<cricket::ChannelInterface> channel_to_delete) {
Harald Alvestranddaee8702022-04-29 11:42:55 +0000329 // The clumsy combination of pushing down media channel and deleting
330 // the channel is due to the desire to do both things in one Invoke().
331 if (!channel_to_delete && senders_.empty() && receivers_.empty()) {
332 return;
333 }
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000334 context()->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
Harald Alvestranddaee8702022-04-29 11:42:55 +0000335 // Push down the new media_channel, if any, otherwise clear it.
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000336 auto* media_channel = channel_ ? channel_->media_channel() : nullptr;
337 for (const auto& sender : senders_) {
338 sender->internal()->SetMediaChannel(media_channel);
339 }
340
341 for (const auto& receiver : receivers_) {
342 receiver->internal()->SetMediaChannel(media_channel);
343 }
344
345 // Destroy the channel, if we had one, now _after_ updating the receivers
346 // who might have had references to the previous channel.
347 if (channel_to_delete) {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000348 channel_to_delete.reset(nullptr);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000349 }
350 });
351}
352
Steve Anton6e634bf2017-11-13 10:44:53 -0800353void RtpTransceiver::AddSender(
354 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
Tommi99c8a802021-04-27 15:00:00 +0200355 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800356 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800357 RTC_DCHECK(!unified_plan_);
358 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -0800359 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800360 RTC_DCHECK(!absl::c_linear_search(senders_, sender));
Steve Anton6e634bf2017-11-13 10:44:53 -0800361 senders_.push_back(sender);
362}
363
364bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
365 RTC_DCHECK(!unified_plan_);
366 if (sender) {
367 RTC_DCHECK_EQ(media_type(), sender->media_type());
368 }
Steve Anton64b626b2019-01-28 17:25:26 -0800369 auto it = absl::c_find(senders_, sender);
Steve Anton6e634bf2017-11-13 10:44:53 -0800370 if (it == senders_.end()) {
371 return false;
372 }
373 (*it)->internal()->Stop();
374 senders_.erase(it);
375 return true;
376}
377
378void RtpTransceiver::AddReceiver(
379 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
380 receiver) {
Tommi99c8a802021-04-27 15:00:00 +0200381 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800382 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800383 RTC_DCHECK(!unified_plan_);
384 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800385 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800386 RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
Steve Anton6e634bf2017-11-13 10:44:53 -0800387 receivers_.push_back(receiver);
388}
389
390bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
Tommi6589def2022-02-17 23:36:47 +0100391 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800392 RTC_DCHECK(!unified_plan_);
393 if (receiver) {
394 RTC_DCHECK_EQ(media_type(), receiver->media_type());
395 }
Steve Anton64b626b2019-01-28 17:25:26 -0800396 auto it = absl::c_find(receivers_, receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -0800397 if (it == receivers_.end()) {
398 return false;
399 }
Tommi6589def2022-02-17 23:36:47 +0100400
Steve Anton6e634bf2017-11-13 10:44:53 -0800401 (*it)->internal()->Stop();
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000402 context()->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
Tommi6589def2022-02-17 23:36:47 +0100403 // `Stop()` will clear the receiver's pointer to the media channel.
404 (*it)->internal()->SetMediaChannel(nullptr);
405 });
406
Steve Anton6e634bf2017-11-13 10:44:53 -0800407 receivers_.erase(it);
408 return true;
409}
410
Steve Antonf9381f02017-12-14 10:23:57 -0800411rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
412 RTC_DCHECK(unified_plan_);
413 RTC_CHECK_EQ(1u, senders_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100414 return rtc::scoped_refptr<RtpSenderInternal>(senders_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800415}
416
417rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
418 const {
419 RTC_DCHECK(unified_plan_);
420 RTC_CHECK_EQ(1u, receivers_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100421 return rtc::scoped_refptr<RtpReceiverInternal>(receivers_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800422}
423
Steve Anton69470252018-02-09 11:43:08 -0800424cricket::MediaType RtpTransceiver::media_type() const {
425 return media_type_;
426}
427
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200428absl::optional<std::string> RtpTransceiver::mid() const {
Steve Anton6e634bf2017-11-13 10:44:53 -0800429 return mid_;
430}
431
Tommi99c8a802021-04-27 15:00:00 +0200432void RtpTransceiver::OnFirstPacketReceived() {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100433 for (const auto& receiver : receivers_) {
Steve Anton60776752018-01-10 11:51:34 -0800434 receiver->internal()->NotifyFirstPacketReceived();
435 }
436}
437
Steve Anton6e634bf2017-11-13 10:44:53 -0800438rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
439 RTC_DCHECK(unified_plan_);
440 RTC_CHECK_EQ(1u, senders_.size());
441 return senders_[0];
442}
443
444rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
445 RTC_DCHECK(unified_plan_);
446 RTC_CHECK_EQ(1u, receivers_.size());
447 return receivers_[0];
448}
449
Steve Antondcc3c022017-12-22 16:02:54 -0800450void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700451 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
452 << ") current direction from "
453 << (current_direction_ ? RtpTransceiverDirectionToString(
454 *current_direction_)
455 : "<not set>")
456 << " to " << RtpTransceiverDirectionToString(direction)
457 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800458 current_direction_ = direction;
459 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
460 has_ever_been_used_to_send_ = true;
461 }
462}
463
Henrik Boström0a162762022-05-02 15:47:52 +0200464void RtpTransceiver::set_fired_direction(
465 absl::optional<RtpTransceiverDirection> direction) {
Steve Anton0f5400a2018-07-17 14:25:36 -0700466 fired_direction_ = direction;
467}
468
Steve Anton6e634bf2017-11-13 10:44:53 -0800469bool RtpTransceiver::stopped() const {
Tommi99c8a802021-04-27 15:00:00 +0200470 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800471 return stopped_;
472}
473
Harald Alvestrand6060df52020-08-11 09:54:02 +0200474bool RtpTransceiver::stopping() const {
475 RTC_DCHECK_RUN_ON(thread_);
476 return stopping_;
477}
478
Steve Anton6e634bf2017-11-13 10:44:53 -0800479RtpTransceiverDirection RtpTransceiver::direction() const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200480 if (unified_plan_ && stopping())
481 return webrtc::RtpTransceiverDirection::kStopped;
482
Steve Anton6e634bf2017-11-13 10:44:53 -0800483 return direction_;
484}
485
Harald Alvestrand6060df52020-08-11 09:54:02 +0200486RTCError RtpTransceiver::SetDirectionWithError(
487 RtpTransceiverDirection new_direction) {
488 if (unified_plan_ && stopping()) {
489 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
490 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800491 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200492 if (new_direction == direction_)
493 return RTCError::OK();
494
495 if (new_direction == RtpTransceiverDirection::kStopped) {
496 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
497 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800498 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200499
Steve Anton52d86772018-02-20 15:48:12 -0800500 direction_ = new_direction;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000501 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200502
503 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800504}
505
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200506absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800507 const {
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000508 if (unified_plan_ && stopped())
Harald Alvestrand6060df52020-08-11 09:54:02 +0200509 return webrtc::RtpTransceiverDirection::kStopped;
510
Steve Anton6e634bf2017-11-13 10:44:53 -0800511 return current_direction_;
512}
513
Steve Anton0f5400a2018-07-17 14:25:36 -0700514absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
515 const {
516 return fired_direction_;
517}
518
Harald Alvestrand6060df52020-08-11 09:54:02 +0200519void RtpTransceiver::StopSendingAndReceiving() {
520 // 1. Let sender be transceiver.[[Sender]].
521 // 2. Let receiver be transceiver.[[Receiver]].
522 //
523 // 3. Stop sending media with sender.
524 //
Tommi6589def2022-02-17 23:36:47 +0100525 RTC_DCHECK_RUN_ON(thread_);
526
Harald Alvestrand6060df52020-08-11 09:54:02 +0200527 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
528 // specified in [RFC3550].
Harald Alvestrand6060df52020-08-11 09:54:02 +0200529 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800530 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200531
Tommi6589def2022-02-17 23:36:47 +0100532 // Signal to receiver sources that we're stopping.
Harald Alvestrand6060df52020-08-11 09:54:02 +0200533 for (const auto& receiver : receivers_)
Tommi6589def2022-02-17 23:36:47 +0100534 receiver->internal()->Stop();
535
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000536 context()->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
Tommi6589def2022-02-17 23:36:47 +0100537 // 5 Stop receiving media with receiver.
538 for (const auto& receiver : receivers_)
539 receiver->internal()->SetMediaChannel(nullptr);
540 });
Harald Alvestrand6060df52020-08-11 09:54:02 +0200541
542 stopping_ = true;
543 direction_ = webrtc::RtpTransceiverDirection::kInactive;
544}
545
546RTCError RtpTransceiver::StopStandard() {
547 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000548 // If we're on Plan B, do what Stop() used to do there.
549 if (!unified_plan_) {
550 StopInternal();
551 return RTCError::OK();
552 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200553 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
554 // invoked.
555 //
556 // 2. Let connection be the RTCPeerConnection object associated with
557 // transceiver.
558 //
559 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
560 if (is_pc_closed_) {
561 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
562 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000563 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200564
565 // 4. If transceiver.[[Stopping]] is true, abort these steps.
566 if (stopping_)
567 return RTCError::OK();
568
569 // 5. Stop sending and receiving given transceiver, and update the
570 // negotiation-needed flag for connection.
571 StopSendingAndReceiving();
Harald Alvestrand280054f2020-11-10 13:12:53 +0000572 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200573
574 return RTCError::OK();
575}
576
577void RtpTransceiver::StopInternal() {
Harald Alvestrand85466662021-04-19 21:21:36 +0000578 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000579 StopTransceiverProcedure();
580}
581
582void RtpTransceiver::StopTransceiverProcedure() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200583 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000584 // As specified in the "Stop the RTCRtpTransceiver" procedure
Harald Alvestrand6060df52020-08-11 09:54:02 +0200585 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
586 // transceiver.
587 if (!stopping_)
588 StopSendingAndReceiving();
589
590 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800591 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200592
593 // Signal the updated change to the senders.
594 for (const auto& sender : senders_)
595 sender->internal()->SetTransceiverAsStopped();
596
597 // 3. Set transceiver.[[Receptive]] to false.
598 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200599 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800600}
601
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200602RTCError RtpTransceiver::SetCodecPreferences(
603 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
604 RTC_DCHECK(unified_plan_);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200605 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
606 // to codecs and abort these steps.
607 if (codec_capabilities.empty()) {
608 codec_preferences_.clear();
609 return RTCError::OK();
610 }
611
612 // 4. Remove any duplicate values in codecs.
613 std::vector<RtpCodecCapability> codecs;
614 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
615 [&codecs](const RtpCodecCapability& codec) {
616 return absl::c_linear_search(codecs, codec);
617 });
618
Johannes Kron3e983682020-03-29 22:17:00 +0200619 // 6. to 8.
620 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200621 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200622 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000623 send_codecs = media_engine()->voice().send_codecs();
624 recv_codecs = media_engine()->voice().recv_codecs();
Johannes Kron3e983682020-03-29 22:17:00 +0200625 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200626 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200627 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
Harald Alvestrandc3fa7c32022-05-22 10:57:01 +0000628 send_codecs = media_engine()->video().send_codecs(context()->use_rtx());
629 recv_codecs = media_engine()->video().recv_codecs(context()->use_rtx());
Johannes Kron3e983682020-03-29 22:17:00 +0200630 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200631 }
632
Johannes Kron3e983682020-03-29 22:17:00 +0200633 if (result.ok()) {
634 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200635 }
636
Johannes Kron3e983682020-03-29 22:17:00 +0200637 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800638}
639
Markus Handell0357b3e2020-03-16 13:40:51 +0100640std::vector<RtpHeaderExtensionCapability>
641RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200642 return header_extensions_to_offer_;
643}
644
Markus Handell5932fe12020-12-17 22:19:40 +0100645std::vector<RtpHeaderExtensionCapability>
646RtpTransceiver::HeaderExtensionsNegotiated() const {
Tommicc7a3682021-05-04 14:59:38 +0200647 RTC_DCHECK_RUN_ON(thread_);
Markus Handell5932fe12020-12-17 22:19:40 +0100648 std::vector<RtpHeaderExtensionCapability> result;
Tommicc7a3682021-05-04 14:59:38 +0200649 for (const auto& ext : negotiated_header_extensions_) {
Markus Handell5932fe12020-12-17 22:19:40 +0100650 result.emplace_back(ext.uri, ext.id, RtpTransceiverDirection::kSendRecv);
651 }
652 return result;
653}
654
Markus Handell755c65d2020-06-24 01:06:10 +0200655RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
656 rtc::ArrayView<const RtpHeaderExtensionCapability>
657 header_extensions_to_offer) {
658 for (const auto& entry : header_extensions_to_offer) {
659 // Handle unsupported requests for mandatory extensions as per
660 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
661 // Note:
662 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
663 // this has to be checked on a higher level. We naturally error out
664 // in the handling of Step 2.2 if an unset URI is encountered.
665
666 // Step 2.2.
667 // Handle unknown extensions.
668 auto it = std::find_if(
669 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
670 [&entry](const auto& offered) { return entry.uri == offered.uri; });
671 if (it == header_extensions_to_offer_.end()) {
Markus Handellc17bca72021-01-14 17:08:01 +0100672 return RTCError(RTCErrorType::UNSUPPORTED_PARAMETER,
Markus Handell755c65d2020-06-24 01:06:10 +0200673 "Attempted to modify an unoffered extension.");
674 }
675
676 // Step 2.4-2.5.
677 // - Use of the transceiver interface indicates unified plan is in effect,
678 // hence the MID extension needs to be enabled.
679 // - Also handle the mandatory video orientation extensions.
680 if ((entry.uri == RtpExtension::kMidUri ||
681 entry.uri == RtpExtension::kVideoRotationUri) &&
682 entry.direction != RtpTransceiverDirection::kSendRecv) {
683 return RTCError(RTCErrorType::INVALID_MODIFICATION,
684 "Attempted to stop a mandatory extension.");
685 }
686 }
687
688 // Apply mutation after error checking.
689 for (const auto& entry : header_extensions_to_offer) {
690 auto it = std::find_if(
691 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
692 [&entry](const auto& offered) { return entry.uri == offered.uri; });
693 it->direction = entry.direction;
694 }
695
696 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100697}
698
Tommicc7a3682021-05-04 14:59:38 +0200699void RtpTransceiver::OnNegotiationUpdate(
700 SdpType sdp_type,
701 const cricket::MediaContentDescription* content) {
702 RTC_DCHECK_RUN_ON(thread_);
703 RTC_DCHECK(content);
704 if (sdp_type == SdpType::kAnswer)
705 negotiated_header_extensions_ = content->rtp_header_extensions();
706}
707
Harald Alvestrand6060df52020-08-11 09:54:02 +0200708void RtpTransceiver::SetPeerConnectionClosed() {
709 is_pc_closed_ = true;
710}
711
Steve Anton6e634bf2017-11-13 10:44:53 -0800712} // namespace webrtc