blob: 8d14e46b4b828a011210921432224dd43c309a9c [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 Alvestrand11dc6572020-08-10 14:41:03 +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 Alvestrand11dc6572020-08-10 14:41:03 +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 Alvestrand11dc6572020-08-10 14:41:03 +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 Alvestrand11dc6572020-08-10 14:41:03 +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 Alvestrand11dc6572020-08-10 14:41:03 +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 Alvestrand11dc6572020-08-10 14:41:03 +0200296 if (unified_plan_ && stopping())
297 return webrtc::RtpTransceiverDirection::kStopped;
298
Steve Anton6e634bf2017-11-13 10:44:53 -0800299 return direction_;
300}
301
302void RtpTransceiver::SetDirection(RtpTransceiverDirection new_direction) {
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200303 // Catch internal usage of SetDirection without error.
304 // We cannot deprecate this function while it is being proxied, so
305 // settle for causing a runtime error instead.
306 RTC_NOTREACHED();
307 SetDirectionWithError(new_direction);
308}
309
310RTCError RtpTransceiver::SetDirectionWithError(
311 RtpTransceiverDirection new_direction) {
312 if (unified_plan_ && stopping()) {
313 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
314 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800315 }
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200316 if (new_direction == direction_)
317 return RTCError::OK();
318
319 if (new_direction == RtpTransceiverDirection::kStopped) {
320 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
321 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800322 }
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200323
Steve Anton52d86772018-02-20 15:48:12 -0800324 direction_ = new_direction;
325 SignalNegotiationNeeded();
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200326
327 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800328}
329
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200330absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800331 const {
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200332 if (unified_plan_ && stopping())
333 return webrtc::RtpTransceiverDirection::kStopped;
334
Steve Anton6e634bf2017-11-13 10:44:53 -0800335 return current_direction_;
336}
337
Steve Anton0f5400a2018-07-17 14:25:36 -0700338absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
339 const {
340 return fired_direction_;
341}
342
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200343void RtpTransceiver::StopSendingAndReceiving() {
344 // 1. Let sender be transceiver.[[Sender]].
345 // 2. Let receiver be transceiver.[[Receiver]].
346 //
347 // 3. Stop sending media with sender.
348 //
349 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
350 // specified in [RFC3550].
351 RTC_DCHECK_RUN_ON(thread_);
352 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800353 sender->internal()->Stop();
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200354
355 // 5. Stop receiving media with receiver.
356 for (const auto& receiver : receivers_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800357 receiver->internal()->Stop();
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200358
359 stopping_ = true;
360 direction_ = webrtc::RtpTransceiverDirection::kInactive;
361}
362
363RTCError RtpTransceiver::StopStandard() {
364 RTC_DCHECK_RUN_ON(thread_);
365 RTC_DCHECK(unified_plan_);
366 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
367 // invoked.
368 //
369 // 2. Let connection be the RTCPeerConnection object associated with
370 // transceiver.
371 //
372 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
373 if (is_pc_closed_) {
374 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
375 "PeerConnection is closed.");
Steve Anton6e634bf2017-11-13 10:44:53 -0800376 }
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200377
378 // 4. If transceiver.[[Stopping]] is true, abort these steps.
379 if (stopping_)
380 return RTCError::OK();
381
382 // 5. Stop sending and receiving given transceiver, and update the
383 // negotiation-needed flag for connection.
384 StopSendingAndReceiving();
385 SignalNegotiationNeeded();
386
387 return RTCError::OK();
388}
389
390void RtpTransceiver::StopInternal() {
391 RTC_DCHECK_RUN_ON(thread_);
392 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
393 // transceiver.
394 if (!stopping_)
395 StopSendingAndReceiving();
396
397 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800398 stopped_ = true;
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200399
400 // Signal the updated change to the senders.
401 for (const auto& sender : senders_)
402 sender->internal()->SetTransceiverAsStopped();
403
404 // 3. Set transceiver.[[Receptive]] to false.
405 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200406 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800407}
408
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200409RTCError RtpTransceiver::SetCodecPreferences(
410 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
411 RTC_DCHECK(unified_plan_);
412
413 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
414 // to codecs and abort these steps.
415 if (codec_capabilities.empty()) {
416 codec_preferences_.clear();
417 return RTCError::OK();
418 }
419
420 // 4. Remove any duplicate values in codecs.
421 std::vector<RtpCodecCapability> codecs;
422 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
423 [&codecs](const RtpCodecCapability& codec) {
424 return absl::c_linear_search(codecs, codec);
425 });
426
Johannes Kron3e983682020-03-29 22:17:00 +0200427 // 6. to 8.
428 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200429 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200430 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
431 channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
432 channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
433
Johannes Kron3e983682020-03-29 22:17:00 +0200434 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200435 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200436 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
437 channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs);
438 channel_manager_->GetSupportedVideoSendCodecs(&send_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200439
Johannes Kron3e983682020-03-29 22:17:00 +0200440 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200441 }
442
Johannes Kron3e983682020-03-29 22:17:00 +0200443 if (result.ok()) {
444 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200445 }
446
Johannes Kron3e983682020-03-29 22:17:00 +0200447 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800448}
449
Markus Handell0357b3e2020-03-16 13:40:51 +0100450std::vector<RtpHeaderExtensionCapability>
451RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200452 return header_extensions_to_offer_;
453}
454
455RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
456 rtc::ArrayView<const RtpHeaderExtensionCapability>
457 header_extensions_to_offer) {
458 for (const auto& entry : header_extensions_to_offer) {
459 // Handle unsupported requests for mandatory extensions as per
460 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
461 // Note:
462 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
463 // this has to be checked on a higher level. We naturally error out
464 // in the handling of Step 2.2 if an unset URI is encountered.
465
466 // Step 2.2.
467 // Handle unknown extensions.
468 auto it = std::find_if(
469 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
470 [&entry](const auto& offered) { return entry.uri == offered.uri; });
471 if (it == header_extensions_to_offer_.end()) {
472 return RTCError(RTCErrorType::INVALID_PARAMETER,
473 "Attempted to modify an unoffered extension.");
474 }
475
476 // Step 2.4-2.5.
477 // - Use of the transceiver interface indicates unified plan is in effect,
478 // hence the MID extension needs to be enabled.
479 // - Also handle the mandatory video orientation extensions.
480 if ((entry.uri == RtpExtension::kMidUri ||
481 entry.uri == RtpExtension::kVideoRotationUri) &&
482 entry.direction != RtpTransceiverDirection::kSendRecv) {
483 return RTCError(RTCErrorType::INVALID_MODIFICATION,
484 "Attempted to stop a mandatory extension.");
485 }
486 }
487
488 // Apply mutation after error checking.
489 for (const auto& entry : header_extensions_to_offer) {
490 auto it = std::find_if(
491 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
492 [&entry](const auto& offered) { return entry.uri == offered.uri; });
493 it->direction = entry.direction;
494 }
495
496 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100497}
498
Harald Alvestrand11dc6572020-08-10 14:41:03 +0200499void RtpTransceiver::SetPeerConnectionClosed() {
500 is_pc_closed_ = true;
501}
502
Steve Anton6e634bf2017-11-13 10:44:53 -0800503} // namespace webrtc