blob: e152d76bf5fa394962fa689c9a75682a70b77441 [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
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100159void RtpTransceiver::SetChannel(
160 cricket::ChannelInterface* channel,
161 std::function<RtpTransportInternal*(const std::string&)> transport_lookup) {
Tommi99c8a802021-04-27 15:00:00 +0200162 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800163 // Cannot set a non-null channel on a stopped transceiver.
164 if (stopped_ && channel) {
165 return;
166 }
167
Tommi99c8a802021-04-27 15:00:00 +0200168 RTC_DCHECK(channel || channel_);
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100169 RTC_DCHECK(!channel || transport_lookup) << "lookup function not supplied";
Tommi99c8a802021-04-27 15:00:00 +0200170
Tommife041642021-04-07 10:08:28 +0200171 RTC_LOG_THREAD_BLOCK_COUNT();
172
Tommi99c8a802021-04-27 15:00:00 +0200173 if (channel_) {
174 signaling_thread_safety_->SetNotAlive();
175 signaling_thread_safety_ = nullptr;
176 }
177
Steve Anton6e634bf2017-11-13 10:44:53 -0800178 if (channel) {
179 RTC_DCHECK_EQ(media_type(), channel->media_type());
Tommi99c8a802021-04-27 15:00:00 +0200180 signaling_thread_safety_ = PendingTaskSafetyFlag::Create();
Steve Anton6e634bf2017-11-13 10:44:53 -0800181 }
Steve Anton60776752018-01-10 11:51:34 -0800182
Tommi99c8a802021-04-27 15:00:00 +0200183 // An alternative to this, could be to require SetChannel to be called
184 // on the network thread. The channel object operates for the most part
185 // on the network thread, as part of its initialization being on the network
186 // thread is required, so setting a channel object as part of the construction
187 // (without thread hopping) might be the more efficient thing to do than
188 // how SetChannel works today.
189 // Similarly, if the channel() accessor is limited to the network thread, that
190 // helps with keeping the channel implementation requirements being met and
191 // avoids synchronization for accessing the pointer or network related state.
192 channel_manager_->network_thread()->Invoke<void>(RTC_FROM_HERE, [&]() {
193 if (channel_) {
194 channel_->SetFirstPacketReceivedCallback(nullptr);
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100195 channel_->SetRtpTransport(nullptr);
Tommi99c8a802021-04-27 15:00:00 +0200196 }
Steve Anton60776752018-01-10 11:51:34 -0800197
Tommi99c8a802021-04-27 15:00:00 +0200198 channel_ = channel;
Steve Anton60776752018-01-10 11:51:34 -0800199
Tommi99c8a802021-04-27 15:00:00 +0200200 if (channel_) {
Tomas Gunnarsson4f8a58c2022-01-19 11:36:23 +0100201 channel_->SetRtpTransport(transport_lookup(channel_->content_name()));
Tommi99c8a802021-04-27 15:00:00 +0200202 channel_->SetFirstPacketReceivedCallback(
203 [thread = thread_, flag = signaling_thread_safety_, this]() mutable {
204 thread->PostTask(ToQueuedTask(
205 std::move(flag), [this]() { OnFirstPacketReceived(); }));
206 });
207 }
208 });
Steve Anton60776752018-01-10 11:51:34 -0800209
Mirko Bonadei739baf02019-01-27 17:29:42 +0100210 for (const auto& sender : senders_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800211 sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
212 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800213 }
Steve Anton60776752018-01-10 11:51:34 -0800214
Tommi99c8a802021-04-27 15:00:00 +0200215 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1);
Tommife041642021-04-07 10:08:28 +0200216
Mirko Bonadei739baf02019-01-27 17:29:42 +0100217 for (const auto& receiver : receivers_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800218 if (!channel_) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800219 receiver->internal()->Stop();
Tommi4ccdf932021-05-17 14:50:10 +0200220 } else {
221 receiver->internal()->SetMediaChannel(channel_->media_channel());
Steve Anton6e634bf2017-11-13 10:44:53 -0800222 }
Steve Anton6e634bf2017-11-13 10:44:53 -0800223 }
224}
225
226void RtpTransceiver::AddSender(
227 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
Tommi99c8a802021-04-27 15:00:00 +0200228 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800229 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800230 RTC_DCHECK(!unified_plan_);
231 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -0800232 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800233 RTC_DCHECK(!absl::c_linear_search(senders_, sender));
Steve Anton6e634bf2017-11-13 10:44:53 -0800234 senders_.push_back(sender);
235}
236
237bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
238 RTC_DCHECK(!unified_plan_);
239 if (sender) {
240 RTC_DCHECK_EQ(media_type(), sender->media_type());
241 }
Steve Anton64b626b2019-01-28 17:25:26 -0800242 auto it = absl::c_find(senders_, sender);
Steve Anton6e634bf2017-11-13 10:44:53 -0800243 if (it == senders_.end()) {
244 return false;
245 }
246 (*it)->internal()->Stop();
247 senders_.erase(it);
248 return true;
249}
250
251void RtpTransceiver::AddReceiver(
252 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
253 receiver) {
Tommi99c8a802021-04-27 15:00:00 +0200254 RTC_DCHECK_RUN_ON(thread_);
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800255 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800256 RTC_DCHECK(!unified_plan_);
257 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800258 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800259 RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
Steve Anton6e634bf2017-11-13 10:44:53 -0800260 receivers_.push_back(receiver);
261}
262
263bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
264 RTC_DCHECK(!unified_plan_);
265 if (receiver) {
266 RTC_DCHECK_EQ(media_type(), receiver->media_type());
267 }
Steve Anton64b626b2019-01-28 17:25:26 -0800268 auto it = absl::c_find(receivers_, receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -0800269 if (it == receivers_.end()) {
270 return false;
271 }
Tommi4ccdf932021-05-17 14:50:10 +0200272 // `Stop()` will clear the internally cached pointer to the media channel.
Steve Anton6e634bf2017-11-13 10:44:53 -0800273 (*it)->internal()->Stop();
274 receivers_.erase(it);
275 return true;
276}
277
Steve Antonf9381f02017-12-14 10:23:57 -0800278rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
279 RTC_DCHECK(unified_plan_);
280 RTC_CHECK_EQ(1u, senders_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100281 return rtc::scoped_refptr<RtpSenderInternal>(senders_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800282}
283
284rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
285 const {
286 RTC_DCHECK(unified_plan_);
287 RTC_CHECK_EQ(1u, receivers_.size());
Niels Möllere7cc8832022-01-04 15:20:03 +0100288 return rtc::scoped_refptr<RtpReceiverInternal>(receivers_[0]->internal());
Steve Antonf9381f02017-12-14 10:23:57 -0800289}
290
Steve Anton69470252018-02-09 11:43:08 -0800291cricket::MediaType RtpTransceiver::media_type() const {
292 return media_type_;
293}
294
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200295absl::optional<std::string> RtpTransceiver::mid() const {
Steve Anton6e634bf2017-11-13 10:44:53 -0800296 return mid_;
297}
298
Tommi99c8a802021-04-27 15:00:00 +0200299void RtpTransceiver::OnFirstPacketReceived() {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100300 for (const auto& receiver : receivers_) {
Steve Anton60776752018-01-10 11:51:34 -0800301 receiver->internal()->NotifyFirstPacketReceived();
302 }
303}
304
Steve Anton6e634bf2017-11-13 10:44:53 -0800305rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
306 RTC_DCHECK(unified_plan_);
307 RTC_CHECK_EQ(1u, senders_.size());
308 return senders_[0];
309}
310
311rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
312 RTC_DCHECK(unified_plan_);
313 RTC_CHECK_EQ(1u, receivers_.size());
314 return receivers_[0];
315}
316
Steve Antondcc3c022017-12-22 16:02:54 -0800317void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700318 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
319 << ") current direction from "
320 << (current_direction_ ? RtpTransceiverDirectionToString(
321 *current_direction_)
322 : "<not set>")
323 << " to " << RtpTransceiverDirectionToString(direction)
324 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800325 current_direction_ = direction;
326 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
327 has_ever_been_used_to_send_ = true;
328 }
329}
330
Steve Anton0f5400a2018-07-17 14:25:36 -0700331void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) {
332 fired_direction_ = direction;
333}
334
Steve Anton6e634bf2017-11-13 10:44:53 -0800335bool RtpTransceiver::stopped() const {
Tommi99c8a802021-04-27 15:00:00 +0200336 RTC_DCHECK_RUN_ON(thread_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800337 return stopped_;
338}
339
Harald Alvestrand6060df52020-08-11 09:54:02 +0200340bool RtpTransceiver::stopping() const {
341 RTC_DCHECK_RUN_ON(thread_);
342 return stopping_;
343}
344
Steve Anton6e634bf2017-11-13 10:44:53 -0800345RtpTransceiverDirection RtpTransceiver::direction() const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200346 if (unified_plan_ && stopping())
347 return webrtc::RtpTransceiverDirection::kStopped;
348
Steve Anton6e634bf2017-11-13 10:44:53 -0800349 return direction_;
350}
351
Harald Alvestrand6060df52020-08-11 09:54:02 +0200352RTCError RtpTransceiver::SetDirectionWithError(
353 RtpTransceiverDirection new_direction) {
354 if (unified_plan_ && stopping()) {
355 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
356 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800357 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200358 if (new_direction == direction_)
359 return RTCError::OK();
360
361 if (new_direction == RtpTransceiverDirection::kStopped) {
362 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
363 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800364 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200365
Steve Anton52d86772018-02-20 15:48:12 -0800366 direction_ = new_direction;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000367 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200368
369 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800370}
371
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200372absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800373 const {
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000374 if (unified_plan_ && stopped())
Harald Alvestrand6060df52020-08-11 09:54:02 +0200375 return webrtc::RtpTransceiverDirection::kStopped;
376
Steve Anton6e634bf2017-11-13 10:44:53 -0800377 return current_direction_;
378}
379
Steve Anton0f5400a2018-07-17 14:25:36 -0700380absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
381 const {
382 return fired_direction_;
383}
384
Harald Alvestrand6060df52020-08-11 09:54:02 +0200385void RtpTransceiver::StopSendingAndReceiving() {
386 // 1. Let sender be transceiver.[[Sender]].
387 // 2. Let receiver be transceiver.[[Receiver]].
388 //
389 // 3. Stop sending media with sender.
390 //
391 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
392 // specified in [RFC3550].
393 RTC_DCHECK_RUN_ON(thread_);
394 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800395 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200396
397 // 5. Stop receiving media with receiver.
398 for (const auto& receiver : receivers_)
Harald Alvestrand1ee33252020-09-24 13:31:15 +0000399 receiver->internal()->StopAndEndTrack();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200400
401 stopping_ = true;
402 direction_ = webrtc::RtpTransceiverDirection::kInactive;
403}
404
405RTCError RtpTransceiver::StopStandard() {
406 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000407 // If we're on Plan B, do what Stop() used to do there.
408 if (!unified_plan_) {
409 StopInternal();
410 return RTCError::OK();
411 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200412 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
413 // invoked.
414 //
415 // 2. Let connection be the RTCPeerConnection object associated with
416 // transceiver.
417 //
418 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
419 if (is_pc_closed_) {
420 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
421 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000422 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200423
424 // 4. If transceiver.[[Stopping]] is true, abort these steps.
425 if (stopping_)
426 return RTCError::OK();
427
428 // 5. Stop sending and receiving given transceiver, and update the
429 // negotiation-needed flag for connection.
430 StopSendingAndReceiving();
Harald Alvestrand280054f2020-11-10 13:12:53 +0000431 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200432
433 return RTCError::OK();
434}
435
436void RtpTransceiver::StopInternal() {
Harald Alvestrand85466662021-04-19 21:21:36 +0000437 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000438 StopTransceiverProcedure();
439}
440
441void RtpTransceiver::StopTransceiverProcedure() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200442 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000443 // As specified in the "Stop the RTCRtpTransceiver" procedure
Harald Alvestrand6060df52020-08-11 09:54:02 +0200444 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
445 // transceiver.
446 if (!stopping_)
447 StopSendingAndReceiving();
448
449 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800450 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200451
452 // Signal the updated change to the senders.
453 for (const auto& sender : senders_)
454 sender->internal()->SetTransceiverAsStopped();
455
456 // 3. Set transceiver.[[Receptive]] to false.
457 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200458 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800459}
460
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200461RTCError RtpTransceiver::SetCodecPreferences(
462 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
463 RTC_DCHECK(unified_plan_);
464
465 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
466 // to codecs and abort these steps.
467 if (codec_capabilities.empty()) {
468 codec_preferences_.clear();
469 return RTCError::OK();
470 }
471
472 // 4. Remove any duplicate values in codecs.
473 std::vector<RtpCodecCapability> codecs;
474 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
475 [&codecs](const RtpCodecCapability& codec) {
476 return absl::c_linear_search(codecs, codec);
477 });
478
Johannes Kron3e983682020-03-29 22:17:00 +0200479 // 6. to 8.
480 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200481 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200482 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
483 channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
484 channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
485
Johannes Kron3e983682020-03-29 22:17:00 +0200486 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200487 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200488 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
489 channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs);
490 channel_manager_->GetSupportedVideoSendCodecs(&send_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200491
Johannes Kron3e983682020-03-29 22:17:00 +0200492 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200493 }
494
Johannes Kron3e983682020-03-29 22:17:00 +0200495 if (result.ok()) {
496 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200497 }
498
Johannes Kron3e983682020-03-29 22:17:00 +0200499 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800500}
501
Markus Handell0357b3e2020-03-16 13:40:51 +0100502std::vector<RtpHeaderExtensionCapability>
503RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200504 return header_extensions_to_offer_;
505}
506
Markus Handell5932fe12020-12-17 22:19:40 +0100507std::vector<RtpHeaderExtensionCapability>
508RtpTransceiver::HeaderExtensionsNegotiated() const {
Tommicc7a3682021-05-04 14:59:38 +0200509 RTC_DCHECK_RUN_ON(thread_);
Markus Handell5932fe12020-12-17 22:19:40 +0100510 std::vector<RtpHeaderExtensionCapability> result;
Tommicc7a3682021-05-04 14:59:38 +0200511 for (const auto& ext : negotiated_header_extensions_) {
Markus Handell5932fe12020-12-17 22:19:40 +0100512 result.emplace_back(ext.uri, ext.id, RtpTransceiverDirection::kSendRecv);
513 }
514 return result;
515}
516
Markus Handell755c65d2020-06-24 01:06:10 +0200517RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
518 rtc::ArrayView<const RtpHeaderExtensionCapability>
519 header_extensions_to_offer) {
520 for (const auto& entry : header_extensions_to_offer) {
521 // Handle unsupported requests for mandatory extensions as per
522 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
523 // Note:
524 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
525 // this has to be checked on a higher level. We naturally error out
526 // in the handling of Step 2.2 if an unset URI is encountered.
527
528 // Step 2.2.
529 // Handle unknown extensions.
530 auto it = std::find_if(
531 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
532 [&entry](const auto& offered) { return entry.uri == offered.uri; });
533 if (it == header_extensions_to_offer_.end()) {
Markus Handellc17bca72021-01-14 17:08:01 +0100534 return RTCError(RTCErrorType::UNSUPPORTED_PARAMETER,
Markus Handell755c65d2020-06-24 01:06:10 +0200535 "Attempted to modify an unoffered extension.");
536 }
537
538 // Step 2.4-2.5.
539 // - Use of the transceiver interface indicates unified plan is in effect,
540 // hence the MID extension needs to be enabled.
541 // - Also handle the mandatory video orientation extensions.
542 if ((entry.uri == RtpExtension::kMidUri ||
543 entry.uri == RtpExtension::kVideoRotationUri) &&
544 entry.direction != RtpTransceiverDirection::kSendRecv) {
545 return RTCError(RTCErrorType::INVALID_MODIFICATION,
546 "Attempted to stop a mandatory extension.");
547 }
548 }
549
550 // Apply mutation after error checking.
551 for (const auto& entry : header_extensions_to_offer) {
552 auto it = std::find_if(
553 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
554 [&entry](const auto& offered) { return entry.uri == offered.uri; });
555 it->direction = entry.direction;
556 }
557
558 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100559}
560
Tommicc7a3682021-05-04 14:59:38 +0200561void RtpTransceiver::OnNegotiationUpdate(
562 SdpType sdp_type,
563 const cricket::MediaContentDescription* content) {
564 RTC_DCHECK_RUN_ON(thread_);
565 RTC_DCHECK(content);
566 if (sdp_type == SdpType::kAnswer)
567 negotiated_header_extensions_ = content->rtp_header_extensions();
568}
569
Harald Alvestrand6060df52020-08-11 09:54:02 +0200570void RtpTransceiver::SetPeerConnectionClosed() {
571 is_pc_closed_ = true;
572}
573
Steve Anton6e634bf2017-11-13 10:44:53 -0800574} // namespace webrtc