blob: b267e4e3337d3d6116ab386916ee1fc9e4fb9172 [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
13#include <string>
Markus Handell0357b3e2020-03-16 13:40:51 +010014#include <utility>
Steve Anton6e634bf2017-11-13 10:44:53 -080015
Steve Anton64b626b2019-01-28 17:25:26 -080016#include "absl/algorithm/container.h"
Markus Handell0357b3e2020-03-16 13:40:51 +010017#include "api/rtp_parameters.h"
Florent Castelli2d9d82e2019-04-23 19:25:51 +020018#include "pc/channel_manager.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "pc/rtp_media_utils.h"
Florent Castelli2d9d82e2019-04-23 19:25:51 +020020#include "pc/rtp_parameters_conversion.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
Steve Antondcc3c022017-12-22 16:02:54 -080023
Steve Anton6e634bf2017-11-13 10:44:53 -080024namespace webrtc {
Johannes Kron3e983682020-03-29 22:17:00 +020025namespace {
26template <class T>
27RTCError VerifyCodecPreferences(const std::vector<RtpCodecCapability>& codecs,
28 const std::vector<T>& send_codecs,
29 const std::vector<T>& recv_codecs) {
30 // If the intersection between codecs and
31 // RTCRtpSender.getCapabilities(kind).codecs or the intersection between
32 // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX,
33 // RED or FEC codecs or is an empty set, throw InvalidModificationError.
34 // This ensures that we always have something to offer, regardless of
35 // transceiver.direction.
36
37 if (!absl::c_any_of(codecs, [&recv_codecs](const RtpCodecCapability& codec) {
38 return codec.name != cricket::kRtxCodecName &&
39 codec.name != cricket::kRedCodecName &&
40 codec.name != cricket::kFlexfecCodecName &&
41 absl::c_any_of(recv_codecs, [&codec](const T& recv_codec) {
42 return recv_codec.MatchesCapability(codec);
43 });
44 })) {
45 return RTCError(RTCErrorType::INVALID_MODIFICATION,
46 "Invalid codec preferences: Missing codec from recv "
47 "codec capabilities.");
48 }
49
50 if (!absl::c_any_of(codecs, [&send_codecs](const RtpCodecCapability& codec) {
51 return codec.name != cricket::kRtxCodecName &&
52 codec.name != cricket::kRedCodecName &&
53 codec.name != cricket::kFlexfecCodecName &&
54 absl::c_any_of(send_codecs, [&codec](const T& send_codec) {
55 return send_codec.MatchesCapability(codec);
56 });
57 })) {
58 return RTCError(RTCErrorType::INVALID_MODIFICATION,
59 "Invalid codec preferences: Missing codec from send "
60 "codec capabilities.");
61 }
62
63 // Let codecCapabilities be the union of
64 // RTCRtpSender.getCapabilities(kind).codecs and
65 // RTCRtpReceiver.getCapabilities(kind).codecs. For each codec in codecs, If
66 // codec is not in codecCapabilities, throw InvalidModificationError.
67 for (const auto& codec_preference : codecs) {
68 bool is_recv_codec =
69 absl::c_any_of(recv_codecs, [&codec_preference](const T& codec) {
70 return codec.MatchesCapability(codec_preference);
71 });
72
73 bool is_send_codec =
74 absl::c_any_of(send_codecs, [&codec_preference](const T& codec) {
75 return codec.MatchesCapability(codec_preference);
76 });
77
78 if (!is_recv_codec && !is_send_codec) {
79 return RTCError(
80 RTCErrorType::INVALID_MODIFICATION,
81 std::string("Invalid codec preferences: invalid codec with name \"") +
82 codec_preference.name + "\".");
83 }
84 }
85
86 // Check we have a real codec (not just rtx, red or fec)
87 if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) {
88 return codec.name == cricket::kRtxCodecName ||
89 codec.name == cricket::kRedCodecName ||
90 codec.name == cricket::kUlpfecCodecName;
91 })) {
92 return RTCError(RTCErrorType::INVALID_MODIFICATION,
93 "Invalid codec preferences: codec list must have a non "
94 "RTX, RED or FEC entry.");
95 }
96
97 return RTCError::OK();
98}
99
Harald Alvestrand6060df52020-08-11 09:54:02 +0200100TaskQueueBase* GetCurrentTaskQueueOrThread() {
101 TaskQueueBase* current = TaskQueueBase::Current();
102 if (!current)
103 current = rtc::ThreadManager::Instance()->CurrentThread();
104 return current;
105}
106
Johannes Kron3e983682020-03-29 22:17:00 +0200107} // namespace
Steve Anton6e634bf2017-11-13 10:44:53 -0800108
109RtpTransceiver::RtpTransceiver(cricket::MediaType media_type)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200110 : thread_(GetCurrentTaskQueueOrThread()),
111 unified_plan_(false),
112 media_type_(media_type) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800113 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
114 media_type == cricket::MEDIA_TYPE_VIDEO);
115}
116
Steve Anton79e79602017-11-20 10:25:56 -0800117RtpTransceiver::RtpTransceiver(
118 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
119 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200120 receiver,
Markus Handell0357b3e2020-03-16 13:40:51 +0100121 cricket::ChannelManager* channel_manager,
122 std::vector<RtpHeaderExtensionCapability> header_extensions_offered)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200123 : thread_(GetCurrentTaskQueueOrThread()),
124 unified_plan_(true),
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200125 media_type_(sender->media_type()),
Markus Handell0357b3e2020-03-16 13:40:51 +0100126 channel_manager_(channel_manager),
Markus Handell755c65d2020-06-24 01:06:10 +0200127 header_extensions_to_offer_(std::move(header_extensions_offered)) {
Steve Anton79e79602017-11-20 10:25:56 -0800128 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
129 media_type_ == cricket::MEDIA_TYPE_VIDEO);
130 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
131 senders_.push_back(sender);
132 receivers_.push_back(receiver);
133}
134
Steve Anton6e634bf2017-11-13 10:44:53 -0800135RtpTransceiver::~RtpTransceiver() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200136 StopInternal();
Steve Anton6e634bf2017-11-13 10:44:53 -0800137}
138
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800139void RtpTransceiver::SetChannel(cricket::ChannelInterface* channel) {
140 // Cannot set a non-null channel on a stopped transceiver.
141 if (stopped_ && channel) {
142 return;
143 }
144
Steve Anton6e634bf2017-11-13 10:44:53 -0800145 if (channel) {
146 RTC_DCHECK_EQ(media_type(), channel->media_type());
147 }
Steve Anton60776752018-01-10 11:51:34 -0800148
149 if (channel_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800150 channel_->SignalFirstPacketReceived().disconnect(this);
Steve Anton60776752018-01-10 11:51:34 -0800151 }
152
Steve Anton6e634bf2017-11-13 10:44:53 -0800153 channel_ = channel;
Steve Anton60776752018-01-10 11:51:34 -0800154
155 if (channel_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800156 channel_->SignalFirstPacketReceived().connect(
Steve Anton60776752018-01-10 11:51:34 -0800157 this, &RtpTransceiver::OnFirstPacketReceived);
158 }
159
Mirko Bonadei739baf02019-01-27 17:29:42 +0100160 for (const auto& sender : senders_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800161 sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
162 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800163 }
Steve Anton60776752018-01-10 11:51:34 -0800164
Mirko Bonadei739baf02019-01-27 17:29:42 +0100165 for (const auto& receiver : receivers_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800166 if (!channel_) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800167 receiver->internal()->Stop();
168 }
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800169
170 receiver->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
171 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800172 }
173}
174
175void RtpTransceiver::AddSender(
176 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800177 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800178 RTC_DCHECK(!unified_plan_);
179 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -0800180 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800181 RTC_DCHECK(!absl::c_linear_search(senders_, sender));
Steve Anton6e634bf2017-11-13 10:44:53 -0800182 senders_.push_back(sender);
183}
184
185bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
186 RTC_DCHECK(!unified_plan_);
187 if (sender) {
188 RTC_DCHECK_EQ(media_type(), sender->media_type());
189 }
Steve Anton64b626b2019-01-28 17:25:26 -0800190 auto it = absl::c_find(senders_, sender);
Steve Anton6e634bf2017-11-13 10:44:53 -0800191 if (it == senders_.end()) {
192 return false;
193 }
194 (*it)->internal()->Stop();
195 senders_.erase(it);
196 return true;
197}
198
199void RtpTransceiver::AddReceiver(
200 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
201 receiver) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800202 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800203 RTC_DCHECK(!unified_plan_);
204 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800205 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800206 RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
Steve Anton6e634bf2017-11-13 10:44:53 -0800207 receivers_.push_back(receiver);
208}
209
210bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
211 RTC_DCHECK(!unified_plan_);
212 if (receiver) {
213 RTC_DCHECK_EQ(media_type(), receiver->media_type());
214 }
Steve Anton64b626b2019-01-28 17:25:26 -0800215 auto it = absl::c_find(receivers_, receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -0800216 if (it == receivers_.end()) {
217 return false;
218 }
219 (*it)->internal()->Stop();
Markus Handell43e62fc2020-01-07 19:46:15 +0100220 // After the receiver has been removed, there's no guarantee that the
221 // contained media channel isn't deleted shortly after this. To make sure that
222 // the receiver doesn't spontaneously try to use it's (potentially stale)
223 // media channel reference, we clear it out.
224 (*it)->internal()->SetMediaChannel(nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800225 receivers_.erase(it);
226 return true;
227}
228
Steve Antonf9381f02017-12-14 10:23:57 -0800229rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
230 RTC_DCHECK(unified_plan_);
231 RTC_CHECK_EQ(1u, senders_.size());
232 return senders_[0]->internal();
233}
234
235rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
236 const {
237 RTC_DCHECK(unified_plan_);
238 RTC_CHECK_EQ(1u, receivers_.size());
239 return receivers_[0]->internal();
240}
241
Steve Anton69470252018-02-09 11:43:08 -0800242cricket::MediaType RtpTransceiver::media_type() const {
243 return media_type_;
244}
245
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200246absl::optional<std::string> RtpTransceiver::mid() const {
Steve Anton6e634bf2017-11-13 10:44:53 -0800247 return mid_;
248}
249
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800250void RtpTransceiver::OnFirstPacketReceived(cricket::ChannelInterface*) {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100251 for (const auto& receiver : receivers_) {
Steve Anton60776752018-01-10 11:51:34 -0800252 receiver->internal()->NotifyFirstPacketReceived();
253 }
254}
255
Steve Anton6e634bf2017-11-13 10:44:53 -0800256rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
257 RTC_DCHECK(unified_plan_);
258 RTC_CHECK_EQ(1u, senders_.size());
259 return senders_[0];
260}
261
262rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
263 RTC_DCHECK(unified_plan_);
264 RTC_CHECK_EQ(1u, receivers_.size());
265 return receivers_[0];
266}
267
Steve Antondcc3c022017-12-22 16:02:54 -0800268void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700269 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
270 << ") current direction from "
271 << (current_direction_ ? RtpTransceiverDirectionToString(
272 *current_direction_)
273 : "<not set>")
274 << " to " << RtpTransceiverDirectionToString(direction)
275 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800276 current_direction_ = direction;
277 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
278 has_ever_been_used_to_send_ = true;
279 }
280}
281
Steve Anton0f5400a2018-07-17 14:25:36 -0700282void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) {
283 fired_direction_ = direction;
284}
285
Steve Anton6e634bf2017-11-13 10:44:53 -0800286bool RtpTransceiver::stopped() const {
287 return stopped_;
288}
289
Harald Alvestrand6060df52020-08-11 09:54:02 +0200290bool RtpTransceiver::stopping() const {
291 RTC_DCHECK_RUN_ON(thread_);
292 return stopping_;
293}
294
Steve Anton6e634bf2017-11-13 10:44:53 -0800295RtpTransceiverDirection RtpTransceiver::direction() const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200296 if (unified_plan_ && stopping())
297 return webrtc::RtpTransceiverDirection::kStopped;
298
Steve Anton6e634bf2017-11-13 10:44:53 -0800299 return direction_;
300}
301
Harald Alvestrand6060df52020-08-11 09:54:02 +0200302RTCError RtpTransceiver::SetDirectionWithError(
303 RtpTransceiverDirection new_direction) {
304 if (unified_plan_ && stopping()) {
305 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
306 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800307 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200308 if (new_direction == direction_)
309 return RTCError::OK();
310
311 if (new_direction == RtpTransceiverDirection::kStopped) {
312 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
313 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800314 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200315
Steve Anton52d86772018-02-20 15:48:12 -0800316 direction_ = new_direction;
317 SignalNegotiationNeeded();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200318
319 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800320}
321
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200322absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800323 const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200324 if (unified_plan_ && stopping())
325 return webrtc::RtpTransceiverDirection::kStopped;
326
Steve Anton6e634bf2017-11-13 10:44:53 -0800327 return current_direction_;
328}
329
Steve Anton0f5400a2018-07-17 14:25:36 -0700330absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
331 const {
332 return fired_direction_;
333}
334
Harald Alvestrand6060df52020-08-11 09:54:02 +0200335void RtpTransceiver::StopSendingAndReceiving() {
336 // 1. Let sender be transceiver.[[Sender]].
337 // 2. Let receiver be transceiver.[[Receiver]].
338 //
339 // 3. Stop sending media with sender.
340 //
341 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
342 // specified in [RFC3550].
343 RTC_DCHECK_RUN_ON(thread_);
344 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800345 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200346
347 // 5. Stop receiving media with receiver.
348 for (const auto& receiver : receivers_)
Harald Alvestranda88c9772020-08-10 18:06:09 +0000349 receiver->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200350
351 stopping_ = true;
352 direction_ = webrtc::RtpTransceiverDirection::kInactive;
353}
354
355RTCError RtpTransceiver::StopStandard() {
356 RTC_DCHECK_RUN_ON(thread_);
357 RTC_DCHECK(unified_plan_);
358 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
359 // invoked.
360 //
361 // 2. Let connection be the RTCPeerConnection object associated with
362 // transceiver.
363 //
364 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
365 if (is_pc_closed_) {
366 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
367 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000368 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200369
370 // 4. If transceiver.[[Stopping]] is true, abort these steps.
371 if (stopping_)
372 return RTCError::OK();
373
374 // 5. Stop sending and receiving given transceiver, and update the
375 // negotiation-needed flag for connection.
376 StopSendingAndReceiving();
377 SignalNegotiationNeeded();
378
379 return RTCError::OK();
380}
381
382void RtpTransceiver::StopInternal() {
383 RTC_DCHECK_RUN_ON(thread_);
384 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
385 // transceiver.
386 if (!stopping_)
387 StopSendingAndReceiving();
388
389 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800390 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200391
392 // Signal the updated change to the senders.
393 for (const auto& sender : senders_)
394 sender->internal()->SetTransceiverAsStopped();
395
396 // 3. Set transceiver.[[Receptive]] to false.
397 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200398 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800399}
400
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200401RTCError RtpTransceiver::SetCodecPreferences(
402 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
403 RTC_DCHECK(unified_plan_);
404
405 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
406 // to codecs and abort these steps.
407 if (codec_capabilities.empty()) {
408 codec_preferences_.clear();
409 return RTCError::OK();
410 }
411
412 // 4. Remove any duplicate values in codecs.
413 std::vector<RtpCodecCapability> codecs;
414 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
415 [&codecs](const RtpCodecCapability& codec) {
416 return absl::c_linear_search(codecs, codec);
417 });
418
Johannes Kron3e983682020-03-29 22:17:00 +0200419 // 6. to 8.
420 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200421 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200422 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
423 channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
424 channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
425
Johannes Kron3e983682020-03-29 22:17:00 +0200426 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200427 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200428 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
429 channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs);
430 channel_manager_->GetSupportedVideoSendCodecs(&send_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200431
Johannes Kron3e983682020-03-29 22:17:00 +0200432 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200433 }
434
Johannes Kron3e983682020-03-29 22:17:00 +0200435 if (result.ok()) {
436 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200437 }
438
Johannes Kron3e983682020-03-29 22:17:00 +0200439 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800440}
441
Markus Handell0357b3e2020-03-16 13:40:51 +0100442std::vector<RtpHeaderExtensionCapability>
443RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200444 return header_extensions_to_offer_;
445}
446
447RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
448 rtc::ArrayView<const RtpHeaderExtensionCapability>
449 header_extensions_to_offer) {
450 for (const auto& entry : header_extensions_to_offer) {
451 // Handle unsupported requests for mandatory extensions as per
452 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
453 // Note:
454 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
455 // this has to be checked on a higher level. We naturally error out
456 // in the handling of Step 2.2 if an unset URI is encountered.
457
458 // Step 2.2.
459 // Handle unknown extensions.
460 auto it = std::find_if(
461 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
462 [&entry](const auto& offered) { return entry.uri == offered.uri; });
463 if (it == header_extensions_to_offer_.end()) {
464 return RTCError(RTCErrorType::INVALID_PARAMETER,
465 "Attempted to modify an unoffered extension.");
466 }
467
468 // Step 2.4-2.5.
469 // - Use of the transceiver interface indicates unified plan is in effect,
470 // hence the MID extension needs to be enabled.
471 // - Also handle the mandatory video orientation extensions.
472 if ((entry.uri == RtpExtension::kMidUri ||
473 entry.uri == RtpExtension::kVideoRotationUri) &&
474 entry.direction != RtpTransceiverDirection::kSendRecv) {
475 return RTCError(RTCErrorType::INVALID_MODIFICATION,
476 "Attempted to stop a mandatory extension.");
477 }
478 }
479
480 // Apply mutation after error checking.
481 for (const auto& entry : header_extensions_to_offer) {
482 auto it = std::find_if(
483 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
484 [&entry](const auto& offered) { return entry.uri == offered.uri; });
485 it->direction = entry.direction;
486 }
487
488 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100489}
490
Harald Alvestrand6060df52020-08-11 09:54:02 +0200491void RtpTransceiver::SetPeerConnectionClosed() {
492 is_pc_closed_ = true;
493}
494
Steve Anton6e634bf2017-11-13 10:44:53 -0800495} // namespace webrtc