blob: caa9ba5d6486b2a11ac527b816b07a8e074e4f9b [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 Alvestrand8f429922022-05-04 10:32:30 +000020#include "api/peer_connection_interface.h"
Markus Handell0357b3e2020-03-16 13:40:51 +010021#include "api/rtp_parameters.h"
Artem Titovd15a5752021-02-10 14:31:24 +010022#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000023#include "media/base/codec.h"
24#include "media/base/media_constants.h"
Harald Alvestrand8f429922022-05-04 10:32:30 +000025#include "pc/channel.h"
Florent Castelli2d9d82e2019-04-23 19:25:51 +020026#include "pc/channel_manager.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "pc/rtp_media_utils.h"
Markus Handell5932fe12020-12-17 22:19:40 +010028#include "pc/session_description.h"
Yves Gerey3e707812018-11-28 16:47:49 +010029#include "rtc_base/checks.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000030#include "rtc_base/location.h"
Yves Gerey3e707812018-11-28 16:47:49 +010031#include "rtc_base/logging.h"
Tommi99c8a802021-04-27 15:00:00 +020032#include "rtc_base/task_utils/to_queued_task.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
Tommi99c8a802021-04-27 15:00:00 +0200120RtpTransceiver::RtpTransceiver(
121 cricket::MediaType media_type,
122 cricket::ChannelManager* channel_manager /* = nullptr*/)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200123 : thread_(GetCurrentTaskQueueOrThread()),
124 unified_plan_(false),
Tommi99c8a802021-04-27 15:00:00 +0200125 media_type_(media_type),
126 channel_manager_(channel_manager) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800127 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
128 media_type == cricket::MEDIA_TYPE_VIDEO);
Tommi99c8a802021-04-27 15:00:00 +0200129 RTC_DCHECK(channel_manager_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800130}
131
Steve Anton79e79602017-11-20 10:25:56 -0800132RtpTransceiver::RtpTransceiver(
133 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
134 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200135 receiver,
Markus Handell0357b3e2020-03-16 13:40:51 +0100136 cricket::ChannelManager* channel_manager,
Harald Alvestrand280054f2020-11-10 13:12:53 +0000137 std::vector<RtpHeaderExtensionCapability> header_extensions_offered,
138 std::function<void()> on_negotiation_needed)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200139 : thread_(GetCurrentTaskQueueOrThread()),
140 unified_plan_(true),
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200141 media_type_(sender->media_type()),
Markus Handell0357b3e2020-03-16 13:40:51 +0100142 channel_manager_(channel_manager),
Harald Alvestrand280054f2020-11-10 13:12:53 +0000143 header_extensions_to_offer_(std::move(header_extensions_offered)),
144 on_negotiation_needed_(std::move(on_negotiation_needed)) {
Steve Anton79e79602017-11-20 10:25:56 -0800145 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
146 media_type_ == cricket::MEDIA_TYPE_VIDEO);
147 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
Tommi99c8a802021-04-27 15:00:00 +0200148 RTC_DCHECK(channel_manager_);
Steve Anton79e79602017-11-20 10:25:56 -0800149 senders_.push_back(sender);
150 receivers_.push_back(receiver);
151}
152
Steve Anton6e634bf2017-11-13 10:44:53 -0800153RtpTransceiver::~RtpTransceiver() {
Tommi99c8a802021-04-27 15:00:00 +0200154 // TODO(tommi): On Android, when running PeerConnectionClientTest (e.g.
155 // PeerConnectionClientTest#testCameraSwitch), the instance doesn't get
156 // deleted on `thread_`. See if we can fix that.
Harald Alvestrand85466662021-04-19 21:21:36 +0000157 if (!stopped_) {
158 RTC_DCHECK_RUN_ON(thread_);
159 StopInternal();
160 }
Tomas Gunnarsson16de2162022-01-26 10:21:57 +0100161
Harald Alvestrand8f429922022-05-04 10:32:30 +0000162 RTC_CHECK(!channel_) << "Missing call to ClearChannel?";
163}
164
165RTCError RtpTransceiver::CreateChannel(
166 absl::string_view mid,
167 Call* call_ptr,
168 const cricket::MediaConfig& media_config,
169 bool srtp_required,
170 CryptoOptions crypto_options,
171 const cricket::AudioOptions& audio_options,
172 const cricket::VideoOptions& video_options,
173 VideoBitrateAllocatorFactory* video_bitrate_allocator_factory,
174 std::function<RtpTransportInternal*(absl::string_view)> transport_lookup) {
175 RTC_DCHECK_RUN_ON(thread_);
176 if (!channel_manager_->media_engine()) {
177 // TODO(hta): Must be a better way
178 return RTCError(RTCErrorType::INTERNAL_ERROR,
179 "No media engine for mid=" + std::string(mid));
180 }
181 std::unique_ptr<cricket::ChannelInterface> new_channel;
182 if (media_type() == cricket::MEDIA_TYPE_AUDIO) {
183 // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to
184 // the worker thread. We shouldn't be using the `call_ptr_` hack here but
185 // simply be on the worker thread and use `call_` (update upstream code).
186 new_channel = channel_manager_->CreateVoiceChannel(
187 call_ptr, media_config, mid, srtp_required, crypto_options,
188 audio_options);
189
190 } else {
191 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, media_type());
192
193 // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to
194 // the worker thread. We shouldn't be using the `call_ptr_` hack here but
195 // simply be on the worker thread and use `call_` (update upstream code).
196 new_channel = channel_manager_->CreateVideoChannel(
197 call_ptr, media_config, mid, srtp_required, crypto_options,
198 video_options, video_bitrate_allocator_factory);
199 }
200 if (!new_channel) {
201 // TODO(hta): Must be a better way
202 return RTCError(RTCErrorType::INTERNAL_ERROR,
203 "Failed to create channel for mid=" + std::string(mid));
204 }
205 SetChannel(std::move(new_channel), transport_lookup);
206 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800207}
208
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100209void RtpTransceiver::SetChannel(
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000210 std::unique_ptr<cricket::ChannelInterface> channel,
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100211 std::function<RtpTransportInternal*(const std::string&)> transport_lookup) {
Tommi99c8a802021-04-27 15:00:00 +0200212 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestranddaee8702022-04-29 11:42:55 +0000213 RTC_DCHECK(channel);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000214 RTC_DCHECK(transport_lookup);
Harald Alvestranddaee8702022-04-29 11:42:55 +0000215 RTC_DCHECK(!channel_);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000216 // Cannot set a channel on a stopped transceiver.
Harald Alvestranddaee8702022-04-29 11:42:55 +0000217 if (stopped_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800218 return;
219 }
220
Tommife041642021-04-07 10:08:28 +0200221 RTC_LOG_THREAD_BLOCK_COUNT();
222
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000223 RTC_DCHECK_EQ(media_type(), channel->media_type());
224 signaling_thread_safety_ = PendingTaskSafetyFlag::Create();
Steve Anton60776752018-01-10 11:51:34 -0800225
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000226 std::unique_ptr<cricket::ChannelInterface> channel_to_delete;
227
Tommi99c8a802021-04-27 15:00:00 +0200228 // An alternative to this, could be to require SetChannel to be called
229 // on the network thread. The channel object operates for the most part
230 // on the network thread, as part of its initialization being on the network
231 // thread is required, so setting a channel object as part of the construction
232 // (without thread hopping) might be the more efficient thing to do than
233 // how SetChannel works today.
234 // Similarly, if the channel() accessor is limited to the network thread, that
235 // helps with keeping the channel implementation requirements being met and
236 // avoids synchronization for accessing the pointer or network related state.
237 channel_manager_->network_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000238 if (channel_) {
239 channel_->SetFirstPacketReceivedCallback(nullptr);
240 channel_->SetRtpTransport(nullptr);
241 channel_to_delete = std::move(channel_);
242 }
243
244 channel_ = std::move(channel);
Steve Anton60776752018-01-10 11:51:34 -0800245
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000246 channel_->SetRtpTransport(transport_lookup(channel_->mid()));
247 channel_->SetFirstPacketReceivedCallback(
248 [thread = thread_, flag = signaling_thread_safety_, this]() mutable {
249 thread->PostTask(ToQueuedTask(std::move(flag),
250 [this]() { OnFirstPacketReceived(); }));
251 });
Tommi99c8a802021-04-27 15:00:00 +0200252 });
Harald Alvestranddaee8702022-04-29 11:42:55 +0000253 PushNewMediaChannelAndDeleteChannel(nullptr);
Tommi6589def2022-02-17 23:36:47 +0100254
255 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2);
Steve Anton6e634bf2017-11-13 10:44:53 -0800256}
257
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000258void RtpTransceiver::ClearChannel() {
259 RTC_DCHECK_RUN_ON(thread_);
260
261 if (!channel_) {
262 return;
263 }
264
265 RTC_LOG_THREAD_BLOCK_COUNT();
266
267 if (channel_) {
268 signaling_thread_safety_->SetNotAlive();
269 signaling_thread_safety_ = nullptr;
270 }
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000271 std::unique_ptr<cricket::ChannelInterface> channel_to_delete;
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000272
273 channel_manager_->network_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
274 if (channel_) {
275 channel_->SetFirstPacketReceivedCallback(nullptr);
276 channel_->SetRtpTransport(nullptr);
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000277 channel_to_delete = std::move(channel_);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000278 }
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000279 });
280
281 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1);
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000282 PushNewMediaChannelAndDeleteChannel(std::move(channel_to_delete));
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000283
284 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(2);
285}
286
Harald Alvestranddaee8702022-04-29 11:42:55 +0000287void RtpTransceiver::PushNewMediaChannelAndDeleteChannel(
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000288 std::unique_ptr<cricket::ChannelInterface> channel_to_delete) {
Harald Alvestranddaee8702022-04-29 11:42:55 +0000289 // The clumsy combination of pushing down media channel and deleting
290 // the channel is due to the desire to do both things in one Invoke().
291 if (!channel_to_delete && senders_.empty() && receivers_.empty()) {
292 return;
293 }
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000294 channel_manager_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
Harald Alvestranddaee8702022-04-29 11:42:55 +0000295 // Push down the new media_channel, if any, otherwise clear it.
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000296 auto* media_channel = channel_ ? channel_->media_channel() : nullptr;
297 for (const auto& sender : senders_) {
298 sender->internal()->SetMediaChannel(media_channel);
299 }
300
301 for (const auto& receiver : receivers_) {
302 receiver->internal()->SetMediaChannel(media_channel);
303 }
304
305 // Destroy the channel, if we had one, now _after_ updating the receivers
306 // who might have had references to the previous channel.
307 if (channel_to_delete) {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000308 channel_to_delete.reset(nullptr);
Harald Alvestrand19ebabc2022-04-28 13:31:17 +0000309 }
310 });
311}
312
Steve Anton6e634bf2017-11-13 10:44:53 -0800313void RtpTransceiver::AddSender(
314 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
Tommi99c8a802021-04-27 15:00:00 +0200315 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800316 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800317 RTC_DCHECK(!unified_plan_);
318 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -0800319 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800320 RTC_DCHECK(!absl::c_linear_search(senders_, sender));
Steve Anton6e634bf2017-11-13 10:44:53 -0800321 senders_.push_back(sender);
322}
323
324bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
325 RTC_DCHECK(!unified_plan_);
326 if (sender) {
327 RTC_DCHECK_EQ(media_type(), sender->media_type());
328 }
Steve Anton64b626b2019-01-28 17:25:26 -0800329 auto it = absl::c_find(senders_, sender);
Steve Anton6e634bf2017-11-13 10:44:53 -0800330 if (it == senders_.end()) {
331 return false;
332 }
333 (*it)->internal()->Stop();
334 senders_.erase(it);
335 return true;
336}
337
338void RtpTransceiver::AddReceiver(
339 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
340 receiver) {
Tommi99c8a802021-04-27 15:00:00 +0200341 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800342 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800343 RTC_DCHECK(!unified_plan_);
344 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800345 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800346 RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
Steve Anton6e634bf2017-11-13 10:44:53 -0800347 receivers_.push_back(receiver);
348}
349
350bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
Tommi6589def2022-02-17 23:36:47 +0100351 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800352 RTC_DCHECK(!unified_plan_);
353 if (receiver) {
354 RTC_DCHECK_EQ(media_type(), receiver->media_type());
355 }
Steve Anton64b626b2019-01-28 17:25:26 -0800356 auto it = absl::c_find(receivers_, receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -0800357 if (it == receivers_.end()) {
358 return false;
359 }
Tommi6589def2022-02-17 23:36:47 +0100360
Steve Anton6e634bf2017-11-13 10:44:53 -0800361 (*it)->internal()->Stop();
Tommi6589def2022-02-17 23:36:47 +0100362 channel_manager_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
363 // `Stop()` will clear the receiver's pointer to the media channel.
364 (*it)->internal()->SetMediaChannel(nullptr);
365 });
366
Steve Anton6e634bf2017-11-13 10:44:53 -0800367 receivers_.erase(it);
368 return true;
369}
370
Steve Antonf9381f02017-12-14 10:23:57 -0800371rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
372 RTC_DCHECK(unified_plan_);
373 RTC_CHECK_EQ(1u, senders_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100374 return rtc::scoped_refptr<RtpSenderInternal>(senders_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800375}
376
377rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
378 const {
379 RTC_DCHECK(unified_plan_);
380 RTC_CHECK_EQ(1u, receivers_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100381 return rtc::scoped_refptr<RtpReceiverInternal>(receivers_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800382}
383
Steve Anton69470252018-02-09 11:43:08 -0800384cricket::MediaType RtpTransceiver::media_type() const {
385 return media_type_;
386}
387
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200388absl::optional<std::string> RtpTransceiver::mid() const {
Steve Anton6e634bf2017-11-13 10:44:53 -0800389 return mid_;
390}
391
Tommi99c8a802021-04-27 15:00:00 +0200392void RtpTransceiver::OnFirstPacketReceived() {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100393 for (const auto& receiver : receivers_) {
Steve Anton60776752018-01-10 11:51:34 -0800394 receiver->internal()->NotifyFirstPacketReceived();
395 }
396}
397
Steve Anton6e634bf2017-11-13 10:44:53 -0800398rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
399 RTC_DCHECK(unified_plan_);
400 RTC_CHECK_EQ(1u, senders_.size());
401 return senders_[0];
402}
403
404rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
405 RTC_DCHECK(unified_plan_);
406 RTC_CHECK_EQ(1u, receivers_.size());
407 return receivers_[0];
408}
409
Steve Antondcc3c022017-12-22 16:02:54 -0800410void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700411 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
412 << ") current direction from "
413 << (current_direction_ ? RtpTransceiverDirectionToString(
414 *current_direction_)
415 : "<not set>")
416 << " to " << RtpTransceiverDirectionToString(direction)
417 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800418 current_direction_ = direction;
419 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
420 has_ever_been_used_to_send_ = true;
421 }
422}
423
Henrik Boström0a162762022-05-02 15:47:52 +0200424void RtpTransceiver::set_fired_direction(
425 absl::optional<RtpTransceiverDirection> direction) {
Steve Anton0f5400a2018-07-17 14:25:36 -0700426 fired_direction_ = direction;
427}
428
Steve Anton6e634bf2017-11-13 10:44:53 -0800429bool RtpTransceiver::stopped() const {
Tommi99c8a802021-04-27 15:00:00 +0200430 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800431 return stopped_;
432}
433
Harald Alvestrand6060df52020-08-11 09:54:02 +0200434bool RtpTransceiver::stopping() const {
435 RTC_DCHECK_RUN_ON(thread_);
436 return stopping_;
437}
438
Steve Anton6e634bf2017-11-13 10:44:53 -0800439RtpTransceiverDirection RtpTransceiver::direction() const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200440 if (unified_plan_ && stopping())
441 return webrtc::RtpTransceiverDirection::kStopped;
442
Steve Anton6e634bf2017-11-13 10:44:53 -0800443 return direction_;
444}
445
Harald Alvestrand6060df52020-08-11 09:54:02 +0200446RTCError RtpTransceiver::SetDirectionWithError(
447 RtpTransceiverDirection new_direction) {
448 if (unified_plan_ && stopping()) {
449 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
450 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800451 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200452 if (new_direction == direction_)
453 return RTCError::OK();
454
455 if (new_direction == RtpTransceiverDirection::kStopped) {
456 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
457 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800458 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200459
Steve Anton52d86772018-02-20 15:48:12 -0800460 direction_ = new_direction;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000461 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200462
463 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800464}
465
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200466absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800467 const {
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000468 if (unified_plan_ && stopped())
Harald Alvestrand6060df52020-08-11 09:54:02 +0200469 return webrtc::RtpTransceiverDirection::kStopped;
470
Steve Anton6e634bf2017-11-13 10:44:53 -0800471 return current_direction_;
472}
473
Steve Anton0f5400a2018-07-17 14:25:36 -0700474absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
475 const {
476 return fired_direction_;
477}
478
Harald Alvestrand6060df52020-08-11 09:54:02 +0200479void RtpTransceiver::StopSendingAndReceiving() {
480 // 1. Let sender be transceiver.[[Sender]].
481 // 2. Let receiver be transceiver.[[Receiver]].
482 //
483 // 3. Stop sending media with sender.
484 //
Tommi6589def2022-02-17 23:36:47 +0100485 RTC_DCHECK_RUN_ON(thread_);
486
Harald Alvestrand6060df52020-08-11 09:54:02 +0200487 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
488 // specified in [RFC3550].
Harald Alvestrand6060df52020-08-11 09:54:02 +0200489 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800490 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200491
Tommi6589def2022-02-17 23:36:47 +0100492 // Signal to receiver sources that we're stopping.
Harald Alvestrand6060df52020-08-11 09:54:02 +0200493 for (const auto& receiver : receivers_)
Tommi6589def2022-02-17 23:36:47 +0100494 receiver->internal()->Stop();
495
496 channel_manager_->worker_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
497 // 5 Stop receiving media with receiver.
498 for (const auto& receiver : receivers_)
499 receiver->internal()->SetMediaChannel(nullptr);
500 });
Harald Alvestrand6060df52020-08-11 09:54:02 +0200501
502 stopping_ = true;
503 direction_ = webrtc::RtpTransceiverDirection::kInactive;
504}
505
506RTCError RtpTransceiver::StopStandard() {
507 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000508 // If we're on Plan B, do what Stop() used to do there.
509 if (!unified_plan_) {
510 StopInternal();
511 return RTCError::OK();
512 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200513 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
514 // invoked.
515 //
516 // 2. Let connection be the RTCPeerConnection object associated with
517 // transceiver.
518 //
519 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
520 if (is_pc_closed_) {
521 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
522 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000523 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200524
525 // 4. If transceiver.[[Stopping]] is true, abort these steps.
526 if (stopping_)
527 return RTCError::OK();
528
529 // 5. Stop sending and receiving given transceiver, and update the
530 // negotiation-needed flag for connection.
531 StopSendingAndReceiving();
Harald Alvestrand280054f2020-11-10 13:12:53 +0000532 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200533
534 return RTCError::OK();
535}
536
537void RtpTransceiver::StopInternal() {
Harald Alvestrand85466662021-04-19 21:21:36 +0000538 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000539 StopTransceiverProcedure();
540}
541
542void RtpTransceiver::StopTransceiverProcedure() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200543 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000544 // As specified in the "Stop the RTCRtpTransceiver" procedure
Harald Alvestrand6060df52020-08-11 09:54:02 +0200545 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
546 // transceiver.
547 if (!stopping_)
548 StopSendingAndReceiving();
549
550 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800551 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200552
553 // Signal the updated change to the senders.
554 for (const auto& sender : senders_)
555 sender->internal()->SetTransceiverAsStopped();
556
557 // 3. Set transceiver.[[Receptive]] to false.
558 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200559 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800560}
561
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200562RTCError RtpTransceiver::SetCodecPreferences(
563 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
564 RTC_DCHECK(unified_plan_);
Artem Titovc6c02ef2022-05-09 08:30:09 +0000565
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200566 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
567 // to codecs and abort these steps.
568 if (codec_capabilities.empty()) {
569 codec_preferences_.clear();
570 return RTCError::OK();
571 }
572
573 // 4. Remove any duplicate values in codecs.
574 std::vector<RtpCodecCapability> codecs;
575 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
576 [&codecs](const RtpCodecCapability& codec) {
577 return absl::c_linear_search(codecs, codec);
578 });
579
Johannes Kron3e983682020-03-29 22:17:00 +0200580 // 6. to 8.
581 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200582 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200583 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
Artem Titovc6c02ef2022-05-09 08:30:09 +0000584 channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
585 channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200586
Johannes Kron3e983682020-03-29 22:17:00 +0200587 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200588 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200589 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
590 channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs);
591 channel_manager_->GetSupportedVideoSendCodecs(&send_codecs);
Artem Titovc6c02ef2022-05-09 08:30:09 +0000592
Johannes Kron3e983682020-03-29 22:17:00 +0200593 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200594 }
595
Johannes Kron3e983682020-03-29 22:17:00 +0200596 if (result.ok()) {
597 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200598 }
599
Johannes Kron3e983682020-03-29 22:17:00 +0200600 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800601}
602
Markus Handell0357b3e2020-03-16 13:40:51 +0100603std::vector<RtpHeaderExtensionCapability>
604RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200605 return header_extensions_to_offer_;
606}
607
Markus Handell5932fe12020-12-17 22:19:40 +0100608std::vector<RtpHeaderExtensionCapability>
609RtpTransceiver::HeaderExtensionsNegotiated() const {
Tommicc7a3682021-05-04 14:59:38 +0200610 RTC_DCHECK_RUN_ON(thread_);
Markus Handell5932fe12020-12-17 22:19:40 +0100611 std::vector<RtpHeaderExtensionCapability> result;
Tommicc7a3682021-05-04 14:59:38 +0200612 for (const auto& ext : negotiated_header_extensions_) {
Markus Handell5932fe12020-12-17 22:19:40 +0100613 result.emplace_back(ext.uri, ext.id, RtpTransceiverDirection::kSendRecv);
614 }
615 return result;
616}
617
Markus Handell755c65d2020-06-24 01:06:10 +0200618RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
619 rtc::ArrayView<const RtpHeaderExtensionCapability>
620 header_extensions_to_offer) {
621 for (const auto& entry : header_extensions_to_offer) {
622 // Handle unsupported requests for mandatory extensions as per
623 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
624 // Note:
625 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
626 // this has to be checked on a higher level. We naturally error out
627 // in the handling of Step 2.2 if an unset URI is encountered.
628
629 // Step 2.2.
630 // Handle unknown extensions.
631 auto it = std::find_if(
632 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
633 [&entry](const auto& offered) { return entry.uri == offered.uri; });
634 if (it == header_extensions_to_offer_.end()) {
Markus Handellc17bca72021-01-14 17:08:01 +0100635 return RTCError(RTCErrorType::UNSUPPORTED_PARAMETER,
Markus Handell755c65d2020-06-24 01:06:10 +0200636 "Attempted to modify an unoffered extension.");
637 }
638
639 // Step 2.4-2.5.
640 // - Use of the transceiver interface indicates unified plan is in effect,
641 // hence the MID extension needs to be enabled.
642 // - Also handle the mandatory video orientation extensions.
643 if ((entry.uri == RtpExtension::kMidUri ||
644 entry.uri == RtpExtension::kVideoRotationUri) &&
645 entry.direction != RtpTransceiverDirection::kSendRecv) {
646 return RTCError(RTCErrorType::INVALID_MODIFICATION,
647 "Attempted to stop a mandatory extension.");
648 }
649 }
650
651 // Apply mutation after error checking.
652 for (const auto& entry : header_extensions_to_offer) {
653 auto it = std::find_if(
654 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
655 [&entry](const auto& offered) { return entry.uri == offered.uri; });
656 it->direction = entry.direction;
657 }
658
659 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100660}
661
Tommicc7a3682021-05-04 14:59:38 +0200662void RtpTransceiver::OnNegotiationUpdate(
663 SdpType sdp_type,
664 const cricket::MediaContentDescription* content) {
665 RTC_DCHECK_RUN_ON(thread_);
666 RTC_DCHECK(content);
667 if (sdp_type == SdpType::kAnswer)
668 negotiated_header_extensions_ = content->rtp_header_extensions();
669}
670
Harald Alvestrand6060df52020-08-11 09:54:02 +0200671void RtpTransceiver::SetPeerConnectionClosed() {
672 is_pc_closed_ = true;
673}
674
Steve Anton6e634bf2017-11-13 10:44:53 -0800675} // namespace webrtc