blob: 0b7de31372f8f476adf1c1dfda6527870367b9ae [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 Alvestrand5761e7b2021-01-29 14:45:08 +000013#include <iterator>
Steve Anton6e634bf2017-11-13 10:44:53 -080014#include <string>
Markus Handell0357b3e2020-03-16 13:40:51 +010015#include <utility>
Markus Handell5932fe12020-12-17 22:19:40 +010016#include <vector>
Steve Anton6e634bf2017-11-13 10:44:53 -080017
Steve Anton64b626b2019-01-28 17:25:26 -080018#include "absl/algorithm/container.h"
Markus Handell0357b3e2020-03-16 13:40:51 +010019#include "api/rtp_parameters.h"
Artem Titovd15a5752021-02-10 14:31:24 +010020#include "api/sequence_checker.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000021#include "media/base/codec.h"
22#include "media/base/media_constants.h"
Florent Castelli2d9d82e2019-04-23 19:25:51 +020023#include "pc/channel_manager.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "pc/rtp_media_utils.h"
Markus Handell5932fe12020-12-17 22:19:40 +010025#include "pc/session_description.h"
Yves Gerey3e707812018-11-28 16:47:49 +010026#include "rtc_base/checks.h"
27#include "rtc_base/logging.h"
Tommi99c8a802021-04-27 15:00:00 +020028#include "rtc_base/task_utils/to_queued_task.h"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000029#include "rtc_base/thread.h"
Steve Antondcc3c022017-12-22 16:02:54 -080030
Steve Anton6e634bf2017-11-13 10:44:53 -080031namespace webrtc {
Johannes Kron3e983682020-03-29 22:17:00 +020032namespace {
33template <class T>
34RTCError VerifyCodecPreferences(const std::vector<RtpCodecCapability>& codecs,
35 const std::vector<T>& send_codecs,
36 const std::vector<T>& recv_codecs) {
37 // If the intersection between codecs and
38 // RTCRtpSender.getCapabilities(kind).codecs or the intersection between
39 // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX,
40 // RED or FEC codecs or is an empty set, throw InvalidModificationError.
41 // This ensures that we always have something to offer, regardless of
42 // transceiver.direction.
43
44 if (!absl::c_any_of(codecs, [&recv_codecs](const RtpCodecCapability& codec) {
45 return codec.name != cricket::kRtxCodecName &&
46 codec.name != cricket::kRedCodecName &&
47 codec.name != cricket::kFlexfecCodecName &&
48 absl::c_any_of(recv_codecs, [&codec](const T& recv_codec) {
49 return recv_codec.MatchesCapability(codec);
50 });
51 })) {
52 return RTCError(RTCErrorType::INVALID_MODIFICATION,
53 "Invalid codec preferences: Missing codec from recv "
54 "codec capabilities.");
55 }
56
57 if (!absl::c_any_of(codecs, [&send_codecs](const RtpCodecCapability& codec) {
58 return codec.name != cricket::kRtxCodecName &&
59 codec.name != cricket::kRedCodecName &&
60 codec.name != cricket::kFlexfecCodecName &&
61 absl::c_any_of(send_codecs, [&codec](const T& send_codec) {
62 return send_codec.MatchesCapability(codec);
63 });
64 })) {
65 return RTCError(RTCErrorType::INVALID_MODIFICATION,
66 "Invalid codec preferences: Missing codec from send "
67 "codec capabilities.");
68 }
69
70 // Let codecCapabilities be the union of
71 // RTCRtpSender.getCapabilities(kind).codecs and
72 // RTCRtpReceiver.getCapabilities(kind).codecs. For each codec in codecs, If
73 // codec is not in codecCapabilities, throw InvalidModificationError.
74 for (const auto& codec_preference : codecs) {
75 bool is_recv_codec =
76 absl::c_any_of(recv_codecs, [&codec_preference](const T& codec) {
77 return codec.MatchesCapability(codec_preference);
78 });
79
80 bool is_send_codec =
81 absl::c_any_of(send_codecs, [&codec_preference](const T& codec) {
82 return codec.MatchesCapability(codec_preference);
83 });
84
85 if (!is_recv_codec && !is_send_codec) {
86 return RTCError(
87 RTCErrorType::INVALID_MODIFICATION,
88 std::string("Invalid codec preferences: invalid codec with name \"") +
89 codec_preference.name + "\".");
90 }
91 }
92
93 // Check we have a real codec (not just rtx, red or fec)
94 if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) {
95 return codec.name == cricket::kRtxCodecName ||
96 codec.name == cricket::kRedCodecName ||
97 codec.name == cricket::kUlpfecCodecName;
98 })) {
99 return RTCError(RTCErrorType::INVALID_MODIFICATION,
100 "Invalid codec preferences: codec list must have a non "
101 "RTX, RED or FEC entry.");
102 }
103
104 return RTCError::OK();
105}
106
Harald Alvestrand6060df52020-08-11 09:54:02 +0200107TaskQueueBase* GetCurrentTaskQueueOrThread() {
108 TaskQueueBase* current = TaskQueueBase::Current();
109 if (!current)
110 current = rtc::ThreadManager::Instance()->CurrentThread();
111 return current;
112}
113
Johannes Kron3e983682020-03-29 22:17:00 +0200114} // namespace
Steve Anton6e634bf2017-11-13 10:44:53 -0800115
Tommi99c8a802021-04-27 15:00:00 +0200116RtpTransceiver::RtpTransceiver(
117 cricket::MediaType media_type,
118 cricket::ChannelManager* channel_manager /* = nullptr*/)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200119 : thread_(GetCurrentTaskQueueOrThread()),
120 unified_plan_(false),
Tommi99c8a802021-04-27 15:00:00 +0200121 media_type_(media_type),
122 channel_manager_(channel_manager) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800123 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
124 media_type == cricket::MEDIA_TYPE_VIDEO);
Tommi99c8a802021-04-27 15:00:00 +0200125 RTC_DCHECK(channel_manager_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800126}
127
Steve Anton79e79602017-11-20 10:25:56 -0800128RtpTransceiver::RtpTransceiver(
129 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
130 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200131 receiver,
Markus Handell0357b3e2020-03-16 13:40:51 +0100132 cricket::ChannelManager* channel_manager,
Harald Alvestrand280054f2020-11-10 13:12:53 +0000133 std::vector<RtpHeaderExtensionCapability> header_extensions_offered,
134 std::function<void()> on_negotiation_needed)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200135 : thread_(GetCurrentTaskQueueOrThread()),
136 unified_plan_(true),
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200137 media_type_(sender->media_type()),
Markus Handell0357b3e2020-03-16 13:40:51 +0100138 channel_manager_(channel_manager),
Harald Alvestrand280054f2020-11-10 13:12:53 +0000139 header_extensions_to_offer_(std::move(header_extensions_offered)),
140 on_negotiation_needed_(std::move(on_negotiation_needed)) {
Steve Anton79e79602017-11-20 10:25:56 -0800141 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
142 media_type_ == cricket::MEDIA_TYPE_VIDEO);
143 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
Tommi99c8a802021-04-27 15:00:00 +0200144 RTC_DCHECK(channel_manager_);
Steve Anton79e79602017-11-20 10:25:56 -0800145 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 }
Steve Anton6e634bf2017-11-13 10:44:53 -0800157}
158
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800159void RtpTransceiver::SetChannel(cricket::ChannelInterface* channel) {
Tommi99c8a802021-04-27 15:00:00 +0200160 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800161 // Cannot set a non-null channel on a stopped transceiver.
162 if (stopped_ && channel) {
163 return;
164 }
165
Tommi99c8a802021-04-27 15:00:00 +0200166 RTC_DCHECK(channel || channel_);
167
Tommife041642021-04-07 10:08:28 +0200168 RTC_LOG_THREAD_BLOCK_COUNT();
169
Tommi99c8a802021-04-27 15:00:00 +0200170 if (channel_) {
171 signaling_thread_safety_->SetNotAlive();
172 signaling_thread_safety_ = nullptr;
173 }
174
Steve Anton6e634bf2017-11-13 10:44:53 -0800175 if (channel) {
176 RTC_DCHECK_EQ(media_type(), channel->media_type());
Tommi99c8a802021-04-27 15:00:00 +0200177 signaling_thread_safety_ = PendingTaskSafetyFlag::Create();
Steve Anton6e634bf2017-11-13 10:44:53 -0800178 }
Steve Anton60776752018-01-10 11:51:34 -0800179
Tommi99c8a802021-04-27 15:00:00 +0200180 // An alternative to this, could be to require SetChannel to be called
181 // on the network thread. The channel object operates for the most part
182 // on the network thread, as part of its initialization being on the network
183 // thread is required, so setting a channel object as part of the construction
184 // (without thread hopping) might be the more efficient thing to do than
185 // how SetChannel works today.
186 // Similarly, if the channel() accessor is limited to the network thread, that
187 // helps with keeping the channel implementation requirements being met and
188 // avoids synchronization for accessing the pointer or network related state.
189 channel_manager_->network_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
190 if (channel_) {
191 channel_->SetFirstPacketReceivedCallback(nullptr);
192 }
Steve Anton60776752018-01-10 11:51:34 -0800193
Tommi99c8a802021-04-27 15:00:00 +0200194 channel_ = channel;
Steve Anton60776752018-01-10 11:51:34 -0800195
Tommi99c8a802021-04-27 15:00:00 +0200196 if (channel_) {
197 channel_->SetFirstPacketReceivedCallback(
198 [thread = thread_, flag = signaling_thread_safety_, this]() mutable {
199 thread->PostTask(ToQueuedTask(
200 std::move(flag), [this]() { OnFirstPacketReceived(); }));
201 });
202 }
203 });
Steve Anton60776752018-01-10 11:51:34 -0800204
Mirko Bonadei739baf02019-01-27 17:29:42 +0100205 for (const auto& sender : senders_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800206 sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
207 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800208 }
Steve Anton60776752018-01-10 11:51:34 -0800209
Tommi99c8a802021-04-27 15:00:00 +0200210 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1);
Tommife041642021-04-07 10:08:28 +0200211
Mirko Bonadei739baf02019-01-27 17:29:42 +0100212 for (const auto& receiver : receivers_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800213 if (!channel_) {
Tommife041642021-04-07 10:08:28 +0200214 // TODO(tommi): This can internally block and hop to the worker thread.
215 // It's likely that SetMediaChannel also does that, so perhaps we should
216 // require SetMediaChannel(nullptr) to also Stop() and skip this call.
Steve Anton6e634bf2017-11-13 10:44:53 -0800217 receiver->internal()->Stop();
218 }
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800219
220 receiver->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
221 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800222 }
223}
224
225void RtpTransceiver::AddSender(
226 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
Tommi99c8a802021-04-27 15:00:00 +0200227 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800228 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800229 RTC_DCHECK(!unified_plan_);
230 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -0800231 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800232 RTC_DCHECK(!absl::c_linear_search(senders_, sender));
Steve Anton6e634bf2017-11-13 10:44:53 -0800233 senders_.push_back(sender);
234}
235
236bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
237 RTC_DCHECK(!unified_plan_);
238 if (sender) {
239 RTC_DCHECK_EQ(media_type(), sender->media_type());
240 }
Steve Anton64b626b2019-01-28 17:25:26 -0800241 auto it = absl::c_find(senders_, sender);
Steve Anton6e634bf2017-11-13 10:44:53 -0800242 if (it == senders_.end()) {
243 return false;
244 }
245 (*it)->internal()->Stop();
246 senders_.erase(it);
247 return true;
248}
249
250void RtpTransceiver::AddReceiver(
251 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
252 receiver) {
Tommi99c8a802021-04-27 15:00:00 +0200253 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800254 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800255 RTC_DCHECK(!unified_plan_);
256 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800257 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800258 RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
Steve Anton6e634bf2017-11-13 10:44:53 -0800259 receivers_.push_back(receiver);
260}
261
262bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
263 RTC_DCHECK(!unified_plan_);
264 if (receiver) {
265 RTC_DCHECK_EQ(media_type(), receiver->media_type());
266 }
Steve Anton64b626b2019-01-28 17:25:26 -0800267 auto it = absl::c_find(receivers_, receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -0800268 if (it == receivers_.end()) {
269 return false;
270 }
271 (*it)->internal()->Stop();
Markus Handell43e62fc2020-01-07 19:46:15 +0100272 // After the receiver has been removed, there's no guarantee that the
273 // contained media channel isn't deleted shortly after this. To make sure that
274 // the receiver doesn't spontaneously try to use it's (potentially stale)
275 // media channel reference, we clear it out.
276 (*it)->internal()->SetMediaChannel(nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800277 receivers_.erase(it);
278 return true;
279}
280
Steve Antonf9381f02017-12-14 10:23:57 -0800281rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
282 RTC_DCHECK(unified_plan_);
283 RTC_CHECK_EQ(1u, senders_.size());
284 return senders_[0]->internal();
285}
286
287rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
288 const {
289 RTC_DCHECK(unified_plan_);
290 RTC_CHECK_EQ(1u, receivers_.size());
291 return receivers_[0]->internal();
292}
293
Steve Anton69470252018-02-09 11:43:08 -0800294cricket::MediaType RtpTransceiver::media_type() const {
295 return media_type_;
296}
297
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200298absl::optional<std::string> RtpTransceiver::mid() const {
Steve Anton6e634bf2017-11-13 10:44:53 -0800299 return mid_;
300}
301
Tommi99c8a802021-04-27 15:00:00 +0200302void RtpTransceiver::OnFirstPacketReceived() {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100303 for (const auto& receiver : receivers_) {
Steve Anton60776752018-01-10 11:51:34 -0800304 receiver->internal()->NotifyFirstPacketReceived();
305 }
306}
307
Steve Anton6e634bf2017-11-13 10:44:53 -0800308rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
309 RTC_DCHECK(unified_plan_);
310 RTC_CHECK_EQ(1u, senders_.size());
311 return senders_[0];
312}
313
314rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
315 RTC_DCHECK(unified_plan_);
316 RTC_CHECK_EQ(1u, receivers_.size());
317 return receivers_[0];
318}
319
Steve Antondcc3c022017-12-22 16:02:54 -0800320void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700321 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
322 << ") current direction from "
323 << (current_direction_ ? RtpTransceiverDirectionToString(
324 *current_direction_)
325 : "<not set>")
326 << " to " << RtpTransceiverDirectionToString(direction)
327 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800328 current_direction_ = direction;
329 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
330 has_ever_been_used_to_send_ = true;
331 }
332}
333
Steve Anton0f5400a2018-07-17 14:25:36 -0700334void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) {
335 fired_direction_ = direction;
336}
337
Steve Anton6e634bf2017-11-13 10:44:53 -0800338bool RtpTransceiver::stopped() const {
Tommi99c8a802021-04-27 15:00:00 +0200339 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800340 return stopped_;
341}
342
Harald Alvestrand6060df52020-08-11 09:54:02 +0200343bool RtpTransceiver::stopping() const {
344 RTC_DCHECK_RUN_ON(thread_);
345 return stopping_;
346}
347
Steve Anton6e634bf2017-11-13 10:44:53 -0800348RtpTransceiverDirection RtpTransceiver::direction() const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200349 if (unified_plan_ && stopping())
350 return webrtc::RtpTransceiverDirection::kStopped;
351
Steve Anton6e634bf2017-11-13 10:44:53 -0800352 return direction_;
353}
354
Harald Alvestrand6060df52020-08-11 09:54:02 +0200355RTCError RtpTransceiver::SetDirectionWithError(
356 RtpTransceiverDirection new_direction) {
357 if (unified_plan_ && stopping()) {
358 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
359 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800360 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200361 if (new_direction == direction_)
362 return RTCError::OK();
363
364 if (new_direction == RtpTransceiverDirection::kStopped) {
365 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
366 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800367 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200368
Steve Anton52d86772018-02-20 15:48:12 -0800369 direction_ = new_direction;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000370 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200371
372 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800373}
374
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200375absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800376 const {
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000377 if (unified_plan_ && stopped())
Harald Alvestrand6060df52020-08-11 09:54:02 +0200378 return webrtc::RtpTransceiverDirection::kStopped;
379
Steve Anton6e634bf2017-11-13 10:44:53 -0800380 return current_direction_;
381}
382
Steve Anton0f5400a2018-07-17 14:25:36 -0700383absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
384 const {
385 return fired_direction_;
386}
387
Harald Alvestrand6060df52020-08-11 09:54:02 +0200388void RtpTransceiver::StopSendingAndReceiving() {
389 // 1. Let sender be transceiver.[[Sender]].
390 // 2. Let receiver be transceiver.[[Receiver]].
391 //
392 // 3. Stop sending media with sender.
393 //
394 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
395 // specified in [RFC3550].
396 RTC_DCHECK_RUN_ON(thread_);
397 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800398 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200399
400 // 5. Stop receiving media with receiver.
401 for (const auto& receiver : receivers_)
Harald Alvestrand1ee33252020-09-24 13:31:15 +0000402 receiver->internal()->StopAndEndTrack();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200403
404 stopping_ = true;
405 direction_ = webrtc::RtpTransceiverDirection::kInactive;
406}
407
408RTCError RtpTransceiver::StopStandard() {
409 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000410 // If we're on Plan B, do what Stop() used to do there.
411 if (!unified_plan_) {
412 StopInternal();
413 return RTCError::OK();
414 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200415 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
416 // invoked.
417 //
418 // 2. Let connection be the RTCPeerConnection object associated with
419 // transceiver.
420 //
421 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
422 if (is_pc_closed_) {
423 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
424 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000425 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200426
427 // 4. If transceiver.[[Stopping]] is true, abort these steps.
428 if (stopping_)
429 return RTCError::OK();
430
431 // 5. Stop sending and receiving given transceiver, and update the
432 // negotiation-needed flag for connection.
433 StopSendingAndReceiving();
Harald Alvestrand280054f2020-11-10 13:12:53 +0000434 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200435
436 return RTCError::OK();
437}
438
439void RtpTransceiver::StopInternal() {
Harald Alvestrand85466662021-04-19 21:21:36 +0000440 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000441 StopTransceiverProcedure();
442}
443
444void RtpTransceiver::StopTransceiverProcedure() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200445 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000446 // As specified in the "Stop the RTCRtpTransceiver" procedure
Harald Alvestrand6060df52020-08-11 09:54:02 +0200447 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
448 // transceiver.
449 if (!stopping_)
450 StopSendingAndReceiving();
451
452 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800453 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200454
455 // Signal the updated change to the senders.
456 for (const auto& sender : senders_)
457 sender->internal()->SetTransceiverAsStopped();
458
459 // 3. Set transceiver.[[Receptive]] to false.
460 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200461 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800462}
463
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200464RTCError RtpTransceiver::SetCodecPreferences(
465 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
466 RTC_DCHECK(unified_plan_);
467
468 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
469 // to codecs and abort these steps.
470 if (codec_capabilities.empty()) {
471 codec_preferences_.clear();
472 return RTCError::OK();
473 }
474
475 // 4. Remove any duplicate values in codecs.
476 std::vector<RtpCodecCapability> codecs;
477 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
478 [&codecs](const RtpCodecCapability& codec) {
479 return absl::c_linear_search(codecs, codec);
480 });
481
Johannes Kron3e983682020-03-29 22:17:00 +0200482 // 6. to 8.
483 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200484 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200485 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
486 channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
487 channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
488
Johannes Kron3e983682020-03-29 22:17:00 +0200489 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200490 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200491 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
492 channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs);
493 channel_manager_->GetSupportedVideoSendCodecs(&send_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200494
Johannes Kron3e983682020-03-29 22:17:00 +0200495 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200496 }
497
Johannes Kron3e983682020-03-29 22:17:00 +0200498 if (result.ok()) {
499 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200500 }
501
Johannes Kron3e983682020-03-29 22:17:00 +0200502 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800503}
504
Markus Handell0357b3e2020-03-16 13:40:51 +0100505std::vector<RtpHeaderExtensionCapability>
506RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200507 return header_extensions_to_offer_;
508}
509
Markus Handell5932fe12020-12-17 22:19:40 +0100510std::vector<RtpHeaderExtensionCapability>
511RtpTransceiver::HeaderExtensionsNegotiated() const {
Tommicc7a3682021-05-04 14:59:38 +0200512 RTC_DCHECK_RUN_ON(thread_);
Markus Handell5932fe12020-12-17 22:19:40 +0100513 std::vector<RtpHeaderExtensionCapability> result;
Tommicc7a3682021-05-04 14:59:38 +0200514 for (const auto& ext : negotiated_header_extensions_) {
Markus Handell5932fe12020-12-17 22:19:40 +0100515 result.emplace_back(ext.uri, ext.id, RtpTransceiverDirection::kSendRecv);
516 }
517 return result;
518}
519
Markus Handell755c65d2020-06-24 01:06:10 +0200520RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
521 rtc::ArrayView<const RtpHeaderExtensionCapability>
522 header_extensions_to_offer) {
523 for (const auto& entry : header_extensions_to_offer) {
524 // Handle unsupported requests for mandatory extensions as per
525 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
526 // Note:
527 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
528 // this has to be checked on a higher level. We naturally error out
529 // in the handling of Step 2.2 if an unset URI is encountered.
530
531 // Step 2.2.
532 // Handle unknown extensions.
533 auto it = std::find_if(
534 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
535 [&entry](const auto& offered) { return entry.uri == offered.uri; });
536 if (it == header_extensions_to_offer_.end()) {
Markus Handellc17bca72021-01-14 17:08:01 +0100537 return RTCError(RTCErrorType::UNSUPPORTED_PARAMETER,
Markus Handell755c65d2020-06-24 01:06:10 +0200538 "Attempted to modify an unoffered extension.");
539 }
540
541 // Step 2.4-2.5.
542 // - Use of the transceiver interface indicates unified plan is in effect,
543 // hence the MID extension needs to be enabled.
544 // - Also handle the mandatory video orientation extensions.
545 if ((entry.uri == RtpExtension::kMidUri ||
546 entry.uri == RtpExtension::kVideoRotationUri) &&
547 entry.direction != RtpTransceiverDirection::kSendRecv) {
548 return RTCError(RTCErrorType::INVALID_MODIFICATION,
549 "Attempted to stop a mandatory extension.");
550 }
551 }
552
553 // Apply mutation after error checking.
554 for (const auto& entry : header_extensions_to_offer) {
555 auto it = std::find_if(
556 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
557 [&entry](const auto& offered) { return entry.uri == offered.uri; });
558 it->direction = entry.direction;
559 }
560
561 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100562}
563
Tommicc7a3682021-05-04 14:59:38 +0200564void RtpTransceiver::OnNegotiationUpdate(
565 SdpType sdp_type,
566 const cricket::MediaContentDescription* content) {
567 RTC_DCHECK_RUN_ON(thread_);
568 RTC_DCHECK(content);
569 if (sdp_type == SdpType::kAnswer)
570 negotiated_header_extensions_ = content->rtp_header_extensions();
571}
572
Harald Alvestrand6060df52020-08-11 09:54:02 +0200573void RtpTransceiver::SetPeerConnectionClosed() {
574 is_pc_closed_ = true;
575}
576
Steve Anton6e634bf2017-11-13 10:44:53 -0800577} // namespace webrtc