blob: 701b83ffc22faf6fd046b4db77c9604ed7104c91 [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
302void RtpTransceiver::SetDirection(RtpTransceiverDirection new_direction) {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200303 SetDirectionWithError(new_direction);
304}
305
306RTCError RtpTransceiver::SetDirectionWithError(
307 RtpTransceiverDirection new_direction) {
308 if (unified_plan_ && stopping()) {
309 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
310 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800311 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200312 if (new_direction == direction_)
313 return RTCError::OK();
314
315 if (new_direction == RtpTransceiverDirection::kStopped) {
316 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
317 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800318 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200319
Steve Anton52d86772018-02-20 15:48:12 -0800320 direction_ = new_direction;
321 SignalNegotiationNeeded();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200322
323 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800324}
325
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200326absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800327 const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200328 if (unified_plan_ && stopping())
329 return webrtc::RtpTransceiverDirection::kStopped;
330
Steve Anton6e634bf2017-11-13 10:44:53 -0800331 return current_direction_;
332}
333
Steve Anton0f5400a2018-07-17 14:25:36 -0700334absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
335 const {
336 return fired_direction_;
337}
338
Harald Alvestrand6060df52020-08-11 09:54:02 +0200339void RtpTransceiver::StopSendingAndReceiving() {
340 // 1. Let sender be transceiver.[[Sender]].
341 // 2. Let receiver be transceiver.[[Receiver]].
342 //
343 // 3. Stop sending media with sender.
344 //
345 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
346 // specified in [RFC3550].
347 RTC_DCHECK_RUN_ON(thread_);
348 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800349 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200350
351 // 5. Stop receiving media with receiver.
352 for (const auto& receiver : receivers_)
Harald Alvestranda88c9772020-08-10 18:06:09 +0000353 receiver->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200354
355 stopping_ = true;
356 direction_ = webrtc::RtpTransceiverDirection::kInactive;
357}
358
359RTCError RtpTransceiver::StopStandard() {
360 RTC_DCHECK_RUN_ON(thread_);
361 RTC_DCHECK(unified_plan_);
362 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
363 // invoked.
364 //
365 // 2. Let connection be the RTCPeerConnection object associated with
366 // transceiver.
367 //
368 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
369 if (is_pc_closed_) {
370 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
371 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000372 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200373
374 // 4. If transceiver.[[Stopping]] is true, abort these steps.
375 if (stopping_)
376 return RTCError::OK();
377
378 // 5. Stop sending and receiving given transceiver, and update the
379 // negotiation-needed flag for connection.
380 StopSendingAndReceiving();
381 SignalNegotiationNeeded();
382
383 return RTCError::OK();
384}
385
386void RtpTransceiver::StopInternal() {
387 RTC_DCHECK_RUN_ON(thread_);
388 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
389 // transceiver.
390 if (!stopping_)
391 StopSendingAndReceiving();
392
393 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800394 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200395
396 // Signal the updated change to the senders.
397 for (const auto& sender : senders_)
398 sender->internal()->SetTransceiverAsStopped();
399
400 // 3. Set transceiver.[[Receptive]] to false.
401 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200402 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800403}
404
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200405RTCError RtpTransceiver::SetCodecPreferences(
406 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
407 RTC_DCHECK(unified_plan_);
408
409 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
410 // to codecs and abort these steps.
411 if (codec_capabilities.empty()) {
412 codec_preferences_.clear();
413 return RTCError::OK();
414 }
415
416 // 4. Remove any duplicate values in codecs.
417 std::vector<RtpCodecCapability> codecs;
418 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
419 [&codecs](const RtpCodecCapability& codec) {
420 return absl::c_linear_search(codecs, codec);
421 });
422
Johannes Kron3e983682020-03-29 22:17:00 +0200423 // 6. to 8.
424 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200425 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200426 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
427 channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
428 channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
429
Johannes Kron3e983682020-03-29 22:17:00 +0200430 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200431 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200432 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
433 channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs);
434 channel_manager_->GetSupportedVideoSendCodecs(&send_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200435
Johannes Kron3e983682020-03-29 22:17:00 +0200436 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200437 }
438
Johannes Kron3e983682020-03-29 22:17:00 +0200439 if (result.ok()) {
440 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200441 }
442
Johannes Kron3e983682020-03-29 22:17:00 +0200443 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800444}
445
Markus Handell0357b3e2020-03-16 13:40:51 +0100446std::vector<RtpHeaderExtensionCapability>
447RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200448 return header_extensions_to_offer_;
449}
450
451RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
452 rtc::ArrayView<const RtpHeaderExtensionCapability>
453 header_extensions_to_offer) {
454 for (const auto& entry : header_extensions_to_offer) {
455 // Handle unsupported requests for mandatory extensions as per
456 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
457 // Note:
458 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
459 // this has to be checked on a higher level. We naturally error out
460 // in the handling of Step 2.2 if an unset URI is encountered.
461
462 // Step 2.2.
463 // Handle unknown extensions.
464 auto it = std::find_if(
465 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
466 [&entry](const auto& offered) { return entry.uri == offered.uri; });
467 if (it == header_extensions_to_offer_.end()) {
468 return RTCError(RTCErrorType::INVALID_PARAMETER,
469 "Attempted to modify an unoffered extension.");
470 }
471
472 // Step 2.4-2.5.
473 // - Use of the transceiver interface indicates unified plan is in effect,
474 // hence the MID extension needs to be enabled.
475 // - Also handle the mandatory video orientation extensions.
476 if ((entry.uri == RtpExtension::kMidUri ||
477 entry.uri == RtpExtension::kVideoRotationUri) &&
478 entry.direction != RtpTransceiverDirection::kSendRecv) {
479 return RTCError(RTCErrorType::INVALID_MODIFICATION,
480 "Attempted to stop a mandatory extension.");
481 }
482 }
483
484 // Apply mutation after error checking.
485 for (const auto& entry : header_extensions_to_offer) {
486 auto it = std::find_if(
487 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
488 [&entry](const auto& offered) { return entry.uri == offered.uri; });
489 it->direction = entry.direction;
490 }
491
492 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100493}
494
Harald Alvestrand6060df52020-08-11 09:54:02 +0200495void RtpTransceiver::SetPeerConnectionClosed() {
496 is_pc_closed_ = true;
497}
498
Steve Anton6e634bf2017-11-13 10:44:53 -0800499} // namespace webrtc