blob: 53a3702c4002cc9749f8ef39980fff6840e9fcf2 [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"
Harald Alvestrand5761e7b2021-01-29 14:45:08 +000028#include "rtc_base/thread.h"
Steve Antondcc3c022017-12-22 16:02:54 -080029
Steve Anton6e634bf2017-11-13 10:44:53 -080030namespace webrtc {
Johannes Kron3e983682020-03-29 22:17:00 +020031namespace {
32template <class T>
33RTCError VerifyCodecPreferences(const std::vector<RtpCodecCapability>& codecs,
34 const std::vector<T>& send_codecs,
35 const std::vector<T>& recv_codecs) {
36 // If the intersection between codecs and
37 // RTCRtpSender.getCapabilities(kind).codecs or the intersection between
38 // codecs and RTCRtpReceiver.getCapabilities(kind).codecs only contains RTX,
39 // RED or FEC codecs or is an empty set, throw InvalidModificationError.
40 // This ensures that we always have something to offer, regardless of
41 // transceiver.direction.
42
43 if (!absl::c_any_of(codecs, [&recv_codecs](const RtpCodecCapability& codec) {
44 return codec.name != cricket::kRtxCodecName &&
45 codec.name != cricket::kRedCodecName &&
46 codec.name != cricket::kFlexfecCodecName &&
47 absl::c_any_of(recv_codecs, [&codec](const T& recv_codec) {
48 return recv_codec.MatchesCapability(codec);
49 });
50 })) {
51 return RTCError(RTCErrorType::INVALID_MODIFICATION,
52 "Invalid codec preferences: Missing codec from recv "
53 "codec capabilities.");
54 }
55
56 if (!absl::c_any_of(codecs, [&send_codecs](const RtpCodecCapability& codec) {
57 return codec.name != cricket::kRtxCodecName &&
58 codec.name != cricket::kRedCodecName &&
59 codec.name != cricket::kFlexfecCodecName &&
60 absl::c_any_of(send_codecs, [&codec](const T& send_codec) {
61 return send_codec.MatchesCapability(codec);
62 });
63 })) {
64 return RTCError(RTCErrorType::INVALID_MODIFICATION,
65 "Invalid codec preferences: Missing codec from send "
66 "codec capabilities.");
67 }
68
69 // Let codecCapabilities be the union of
70 // RTCRtpSender.getCapabilities(kind).codecs and
71 // RTCRtpReceiver.getCapabilities(kind).codecs. For each codec in codecs, If
72 // codec is not in codecCapabilities, throw InvalidModificationError.
73 for (const auto& codec_preference : codecs) {
74 bool is_recv_codec =
75 absl::c_any_of(recv_codecs, [&codec_preference](const T& codec) {
76 return codec.MatchesCapability(codec_preference);
77 });
78
79 bool is_send_codec =
80 absl::c_any_of(send_codecs, [&codec_preference](const T& codec) {
81 return codec.MatchesCapability(codec_preference);
82 });
83
84 if (!is_recv_codec && !is_send_codec) {
85 return RTCError(
86 RTCErrorType::INVALID_MODIFICATION,
87 std::string("Invalid codec preferences: invalid codec with name \"") +
88 codec_preference.name + "\".");
89 }
90 }
91
92 // Check we have a real codec (not just rtx, red or fec)
93 if (absl::c_all_of(codecs, [](const RtpCodecCapability& codec) {
94 return codec.name == cricket::kRtxCodecName ||
95 codec.name == cricket::kRedCodecName ||
96 codec.name == cricket::kUlpfecCodecName;
97 })) {
98 return RTCError(RTCErrorType::INVALID_MODIFICATION,
99 "Invalid codec preferences: codec list must have a non "
100 "RTX, RED or FEC entry.");
101 }
102
103 return RTCError::OK();
104}
105
Harald Alvestrand6060df52020-08-11 09:54:02 +0200106TaskQueueBase* GetCurrentTaskQueueOrThread() {
107 TaskQueueBase* current = TaskQueueBase::Current();
108 if (!current)
109 current = rtc::ThreadManager::Instance()->CurrentThread();
110 return current;
111}
112
Johannes Kron3e983682020-03-29 22:17:00 +0200113} // namespace
Steve Anton6e634bf2017-11-13 10:44:53 -0800114
115RtpTransceiver::RtpTransceiver(cricket::MediaType media_type)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200116 : thread_(GetCurrentTaskQueueOrThread()),
117 unified_plan_(false),
118 media_type_(media_type) {
Steve Anton6e634bf2017-11-13 10:44:53 -0800119 RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO ||
120 media_type == cricket::MEDIA_TYPE_VIDEO);
121}
122
Steve Anton79e79602017-11-20 10:25:56 -0800123RtpTransceiver::RtpTransceiver(
124 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender,
125 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200126 receiver,
Markus Handell0357b3e2020-03-16 13:40:51 +0100127 cricket::ChannelManager* channel_manager,
Harald Alvestrand280054f2020-11-10 13:12:53 +0000128 std::vector<RtpHeaderExtensionCapability> header_extensions_offered,
129 std::function<void()> on_negotiation_needed)
Harald Alvestrand6060df52020-08-11 09:54:02 +0200130 : thread_(GetCurrentTaskQueueOrThread()),
131 unified_plan_(true),
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200132 media_type_(sender->media_type()),
Markus Handell0357b3e2020-03-16 13:40:51 +0100133 channel_manager_(channel_manager),
Harald Alvestrand280054f2020-11-10 13:12:53 +0000134 header_extensions_to_offer_(std::move(header_extensions_offered)),
135 on_negotiation_needed_(std::move(on_negotiation_needed)) {
Steve Anton79e79602017-11-20 10:25:56 -0800136 RTC_DCHECK(media_type_ == cricket::MEDIA_TYPE_AUDIO ||
137 media_type_ == cricket::MEDIA_TYPE_VIDEO);
138 RTC_DCHECK_EQ(sender->media_type(), receiver->media_type());
139 senders_.push_back(sender);
140 receivers_.push_back(receiver);
141}
142
Steve Anton6e634bf2017-11-13 10:44:53 -0800143RtpTransceiver::~RtpTransceiver() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200144 StopInternal();
Steve Anton6e634bf2017-11-13 10:44:53 -0800145}
146
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800147void RtpTransceiver::SetChannel(cricket::ChannelInterface* channel) {
148 // Cannot set a non-null channel on a stopped transceiver.
149 if (stopped_ && channel) {
150 return;
151 }
152
Tommife041642021-04-07 10:08:28 +0200153 RTC_LOG_THREAD_BLOCK_COUNT();
154
Steve Anton6e634bf2017-11-13 10:44:53 -0800155 if (channel) {
156 RTC_DCHECK_EQ(media_type(), channel->media_type());
157 }
Steve Anton60776752018-01-10 11:51:34 -0800158
159 if (channel_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800160 channel_->SignalFirstPacketReceived().disconnect(this);
Steve Anton60776752018-01-10 11:51:34 -0800161 }
162
Steve Anton6e634bf2017-11-13 10:44:53 -0800163 channel_ = channel;
Steve Anton60776752018-01-10 11:51:34 -0800164
165 if (channel_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800166 channel_->SignalFirstPacketReceived().connect(
Steve Anton60776752018-01-10 11:51:34 -0800167 this, &RtpTransceiver::OnFirstPacketReceived);
168 }
169
Mirko Bonadei739baf02019-01-27 17:29:42 +0100170 for (const auto& sender : senders_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800171 sender->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
172 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800173 }
Steve Anton60776752018-01-10 11:51:34 -0800174
Tommife041642021-04-07 10:08:28 +0200175 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0);
176
Mirko Bonadei739baf02019-01-27 17:29:42 +0100177 for (const auto& receiver : receivers_) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800178 if (!channel_) {
Tommife041642021-04-07 10:08:28 +0200179 // TODO(tommi): This can internally block and hop to the worker thread.
180 // It's likely that SetMediaChannel also does that, so perhaps we should
181 // require SetMediaChannel(nullptr) to also Stop() and skip this call.
Steve Anton6e634bf2017-11-13 10:44:53 -0800182 receiver->internal()->Stop();
183 }
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800184
185 receiver->internal()->SetMediaChannel(channel_ ? channel_->media_channel()
186 : nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800187 }
Tommife041642021-04-07 10:08:28 +0200188
189 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(receivers_.size() * 2);
Steve Anton6e634bf2017-11-13 10:44:53 -0800190}
191
192void RtpTransceiver::AddSender(
193 rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800194 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800195 RTC_DCHECK(!unified_plan_);
196 RTC_DCHECK(sender);
Steve Anton69470252018-02-09 11:43:08 -0800197 RTC_DCHECK_EQ(media_type(), sender->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800198 RTC_DCHECK(!absl::c_linear_search(senders_, sender));
Steve Anton6e634bf2017-11-13 10:44:53 -0800199 senders_.push_back(sender);
200}
201
202bool RtpTransceiver::RemoveSender(RtpSenderInterface* sender) {
203 RTC_DCHECK(!unified_plan_);
204 if (sender) {
205 RTC_DCHECK_EQ(media_type(), sender->media_type());
206 }
Steve Anton64b626b2019-01-28 17:25:26 -0800207 auto it = absl::c_find(senders_, sender);
Steve Anton6e634bf2017-11-13 10:44:53 -0800208 if (it == senders_.end()) {
209 return false;
210 }
211 (*it)->internal()->Stop();
212 senders_.erase(it);
213 return true;
214}
215
216void RtpTransceiver::AddReceiver(
217 rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
218 receiver) {
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800219 RTC_DCHECK(!stopped_);
Steve Anton6e634bf2017-11-13 10:44:53 -0800220 RTC_DCHECK(!unified_plan_);
221 RTC_DCHECK(receiver);
Steve Anton69470252018-02-09 11:43:08 -0800222 RTC_DCHECK_EQ(media_type(), receiver->media_type());
Steve Anton64b626b2019-01-28 17:25:26 -0800223 RTC_DCHECK(!absl::c_linear_search(receivers_, receiver));
Steve Anton6e634bf2017-11-13 10:44:53 -0800224 receivers_.push_back(receiver);
225}
226
227bool RtpTransceiver::RemoveReceiver(RtpReceiverInterface* receiver) {
228 RTC_DCHECK(!unified_plan_);
229 if (receiver) {
230 RTC_DCHECK_EQ(media_type(), receiver->media_type());
231 }
Steve Anton64b626b2019-01-28 17:25:26 -0800232 auto it = absl::c_find(receivers_, receiver);
Steve Anton6e634bf2017-11-13 10:44:53 -0800233 if (it == receivers_.end()) {
234 return false;
235 }
236 (*it)->internal()->Stop();
Markus Handell43e62fc2020-01-07 19:46:15 +0100237 // After the receiver has been removed, there's no guarantee that the
238 // contained media channel isn't deleted shortly after this. To make sure that
239 // the receiver doesn't spontaneously try to use it's (potentially stale)
240 // media channel reference, we clear it out.
241 (*it)->internal()->SetMediaChannel(nullptr);
Steve Anton6e634bf2017-11-13 10:44:53 -0800242 receivers_.erase(it);
243 return true;
244}
245
Steve Antonf9381f02017-12-14 10:23:57 -0800246rtc::scoped_refptr<RtpSenderInternal> RtpTransceiver::sender_internal() const {
247 RTC_DCHECK(unified_plan_);
248 RTC_CHECK_EQ(1u, senders_.size());
249 return senders_[0]->internal();
250}
251
252rtc::scoped_refptr<RtpReceiverInternal> RtpTransceiver::receiver_internal()
253 const {
254 RTC_DCHECK(unified_plan_);
255 RTC_CHECK_EQ(1u, receivers_.size());
256 return receivers_[0]->internal();
257}
258
Steve Anton69470252018-02-09 11:43:08 -0800259cricket::MediaType RtpTransceiver::media_type() const {
260 return media_type_;
261}
262
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200263absl::optional<std::string> RtpTransceiver::mid() const {
Steve Anton6e634bf2017-11-13 10:44:53 -0800264 return mid_;
265}
266
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800267void RtpTransceiver::OnFirstPacketReceived(cricket::ChannelInterface*) {
Mirko Bonadei739baf02019-01-27 17:29:42 +0100268 for (const auto& receiver : receivers_) {
Steve Anton60776752018-01-10 11:51:34 -0800269 receiver->internal()->NotifyFirstPacketReceived();
270 }
271}
272
Steve Anton6e634bf2017-11-13 10:44:53 -0800273rtc::scoped_refptr<RtpSenderInterface> RtpTransceiver::sender() const {
274 RTC_DCHECK(unified_plan_);
275 RTC_CHECK_EQ(1u, senders_.size());
276 return senders_[0];
277}
278
279rtc::scoped_refptr<RtpReceiverInterface> RtpTransceiver::receiver() const {
280 RTC_DCHECK(unified_plan_);
281 RTC_CHECK_EQ(1u, receivers_.size());
282 return receivers_[0];
283}
284
Steve Antondcc3c022017-12-22 16:02:54 -0800285void RtpTransceiver::set_current_direction(RtpTransceiverDirection direction) {
Steve Anton3d954a62018-04-02 11:27:23 -0700286 RTC_LOG(LS_INFO) << "Changing transceiver (MID=" << mid_.value_or("<not set>")
287 << ") current direction from "
288 << (current_direction_ ? RtpTransceiverDirectionToString(
289 *current_direction_)
290 : "<not set>")
291 << " to " << RtpTransceiverDirectionToString(direction)
292 << ".";
Steve Antondcc3c022017-12-22 16:02:54 -0800293 current_direction_ = direction;
294 if (RtpTransceiverDirectionHasSend(*current_direction_)) {
295 has_ever_been_used_to_send_ = true;
296 }
297}
298
Steve Anton0f5400a2018-07-17 14:25:36 -0700299void RtpTransceiver::set_fired_direction(RtpTransceiverDirection direction) {
300 fired_direction_ = direction;
301}
302
Steve Anton6e634bf2017-11-13 10:44:53 -0800303bool RtpTransceiver::stopped() const {
304 return stopped_;
305}
306
Harald Alvestrand6060df52020-08-11 09:54:02 +0200307bool RtpTransceiver::stopping() const {
308 RTC_DCHECK_RUN_ON(thread_);
309 return stopping_;
310}
311
Steve Anton6e634bf2017-11-13 10:44:53 -0800312RtpTransceiverDirection RtpTransceiver::direction() const {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200313 if (unified_plan_ && stopping())
314 return webrtc::RtpTransceiverDirection::kStopped;
315
Steve Anton6e634bf2017-11-13 10:44:53 -0800316 return direction_;
317}
318
Harald Alvestrand6060df52020-08-11 09:54:02 +0200319RTCError RtpTransceiver::SetDirectionWithError(
320 RtpTransceiverDirection new_direction) {
321 if (unified_plan_ && stopping()) {
322 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
323 "Cannot set direction on a stopping transceiver.");
Steve Anton52d86772018-02-20 15:48:12 -0800324 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200325 if (new_direction == direction_)
326 return RTCError::OK();
327
328 if (new_direction == RtpTransceiverDirection::kStopped) {
329 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
330 "The set direction 'stopped' is invalid.");
Steve Anton52d86772018-02-20 15:48:12 -0800331 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200332
Steve Anton52d86772018-02-20 15:48:12 -0800333 direction_ = new_direction;
Harald Alvestrand280054f2020-11-10 13:12:53 +0000334 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200335
336 return RTCError::OK();
Steve Anton6e634bf2017-11-13 10:44:53 -0800337}
338
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200339absl::optional<RtpTransceiverDirection> RtpTransceiver::current_direction()
Steve Anton6e634bf2017-11-13 10:44:53 -0800340 const {
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000341 if (unified_plan_ && stopped())
Harald Alvestrand6060df52020-08-11 09:54:02 +0200342 return webrtc::RtpTransceiverDirection::kStopped;
343
Steve Anton6e634bf2017-11-13 10:44:53 -0800344 return current_direction_;
345}
346
Steve Anton0f5400a2018-07-17 14:25:36 -0700347absl::optional<RtpTransceiverDirection> RtpTransceiver::fired_direction()
348 const {
349 return fired_direction_;
350}
351
Harald Alvestrand6060df52020-08-11 09:54:02 +0200352void RtpTransceiver::StopSendingAndReceiving() {
353 // 1. Let sender be transceiver.[[Sender]].
354 // 2. Let receiver be transceiver.[[Receiver]].
355 //
356 // 3. Stop sending media with sender.
357 //
358 // 4. Send an RTCP BYE for each RTP stream that was being sent by sender, as
359 // specified in [RFC3550].
360 RTC_DCHECK_RUN_ON(thread_);
361 for (const auto& sender : senders_)
Steve Anton6e634bf2017-11-13 10:44:53 -0800362 sender->internal()->Stop();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200363
364 // 5. Stop receiving media with receiver.
365 for (const auto& receiver : receivers_)
Harald Alvestrand1ee33252020-09-24 13:31:15 +0000366 receiver->internal()->StopAndEndTrack();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200367
368 stopping_ = true;
369 direction_ = webrtc::RtpTransceiverDirection::kInactive;
370}
371
372RTCError RtpTransceiver::StopStandard() {
373 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000374 // If we're on Plan B, do what Stop() used to do there.
375 if (!unified_plan_) {
376 StopInternal();
377 return RTCError::OK();
378 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200379 // 1. Let transceiver be the RTCRtpTransceiver object on which the method is
380 // invoked.
381 //
382 // 2. Let connection be the RTCPeerConnection object associated with
383 // transceiver.
384 //
385 // 3. If connection.[[IsClosed]] is true, throw an InvalidStateError.
386 if (is_pc_closed_) {
387 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
388 "PeerConnection is closed.");
Harald Alvestranda88c9772020-08-10 18:06:09 +0000389 }
Harald Alvestrand6060df52020-08-11 09:54:02 +0200390
391 // 4. If transceiver.[[Stopping]] is true, abort these steps.
392 if (stopping_)
393 return RTCError::OK();
394
395 // 5. Stop sending and receiving given transceiver, and update the
396 // negotiation-needed flag for connection.
397 StopSendingAndReceiving();
Harald Alvestrand280054f2020-11-10 13:12:53 +0000398 on_negotiation_needed_();
Harald Alvestrand6060df52020-08-11 09:54:02 +0200399
400 return RTCError::OK();
401}
402
403void RtpTransceiver::StopInternal() {
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000404 StopTransceiverProcedure();
405}
406
407void RtpTransceiver::StopTransceiverProcedure() {
Harald Alvestrand6060df52020-08-11 09:54:02 +0200408 RTC_DCHECK_RUN_ON(thread_);
Harald Alvestrandc75c4282020-08-26 12:17:54 +0000409 // As specified in the "Stop the RTCRtpTransceiver" procedure
Harald Alvestrand6060df52020-08-11 09:54:02 +0200410 // 1. If transceiver.[[Stopping]] is false, stop sending and receiving given
411 // transceiver.
412 if (!stopping_)
413 StopSendingAndReceiving();
414
415 // 2. Set transceiver.[[Stopped]] to true.
Steve Anton6e634bf2017-11-13 10:44:53 -0800416 stopped_ = true;
Harald Alvestrand6060df52020-08-11 09:54:02 +0200417
418 // Signal the updated change to the senders.
419 for (const auto& sender : senders_)
420 sender->internal()->SetTransceiverAsStopped();
421
422 // 3. Set transceiver.[[Receptive]] to false.
423 // 4. Set transceiver.[[CurrentDirection]] to null.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200424 current_direction_ = absl::nullopt;
Steve Anton6e634bf2017-11-13 10:44:53 -0800425}
426
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200427RTCError RtpTransceiver::SetCodecPreferences(
428 rtc::ArrayView<RtpCodecCapability> codec_capabilities) {
429 RTC_DCHECK(unified_plan_);
430
431 // 3. If codecs is an empty list, set transceiver's [[PreferredCodecs]] slot
432 // to codecs and abort these steps.
433 if (codec_capabilities.empty()) {
434 codec_preferences_.clear();
435 return RTCError::OK();
436 }
437
438 // 4. Remove any duplicate values in codecs.
439 std::vector<RtpCodecCapability> codecs;
440 absl::c_remove_copy_if(codec_capabilities, std::back_inserter(codecs),
441 [&codecs](const RtpCodecCapability& codec) {
442 return absl::c_linear_search(codecs, codec);
443 });
444
Johannes Kron3e983682020-03-29 22:17:00 +0200445 // 6. to 8.
446 RTCError result;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200447 if (media_type_ == cricket::MEDIA_TYPE_AUDIO) {
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200448 std::vector<cricket::AudioCodec> recv_codecs, send_codecs;
449 channel_manager_->GetSupportedAudioReceiveCodecs(&recv_codecs);
450 channel_manager_->GetSupportedAudioSendCodecs(&send_codecs);
451
Johannes Kron3e983682020-03-29 22:17:00 +0200452 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200453 } else if (media_type_ == cricket::MEDIA_TYPE_VIDEO) {
Johannes Kron3e983682020-03-29 22:17:00 +0200454 std::vector<cricket::VideoCodec> recv_codecs, send_codecs;
455 channel_manager_->GetSupportedVideoReceiveCodecs(&recv_codecs);
456 channel_manager_->GetSupportedVideoSendCodecs(&send_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200457
Johannes Kron3e983682020-03-29 22:17:00 +0200458 result = VerifyCodecPreferences(codecs, send_codecs, recv_codecs);
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200459 }
460
Johannes Kron3e983682020-03-29 22:17:00 +0200461 if (result.ok()) {
462 codec_preferences_ = codecs;
Florent Castelli2d9d82e2019-04-23 19:25:51 +0200463 }
464
Johannes Kron3e983682020-03-29 22:17:00 +0200465 return result;
Steve Anton6e634bf2017-11-13 10:44:53 -0800466}
467
Markus Handell0357b3e2020-03-16 13:40:51 +0100468std::vector<RtpHeaderExtensionCapability>
469RtpTransceiver::HeaderExtensionsToOffer() const {
Markus Handell755c65d2020-06-24 01:06:10 +0200470 return header_extensions_to_offer_;
471}
472
Markus Handell5932fe12020-12-17 22:19:40 +0100473std::vector<RtpHeaderExtensionCapability>
474RtpTransceiver::HeaderExtensionsNegotiated() const {
475 if (!channel_)
476 return {};
477 std::vector<RtpHeaderExtensionCapability> result;
478 for (const auto& ext : channel_->GetNegotiatedRtpHeaderExtensions()) {
479 result.emplace_back(ext.uri, ext.id, RtpTransceiverDirection::kSendRecv);
480 }
481 return result;
482}
483
Markus Handell755c65d2020-06-24 01:06:10 +0200484RTCError RtpTransceiver::SetOfferedRtpHeaderExtensions(
485 rtc::ArrayView<const RtpHeaderExtensionCapability>
486 header_extensions_to_offer) {
487 for (const auto& entry : header_extensions_to_offer) {
488 // Handle unsupported requests for mandatory extensions as per
489 // https://w3c.github.io/webrtc-extensions/#rtcrtptransceiver-interface.
490 // Note:
491 // - We do not handle setOfferedRtpHeaderExtensions algorithm step 2.1,
492 // this has to be checked on a higher level. We naturally error out
493 // in the handling of Step 2.2 if an unset URI is encountered.
494
495 // Step 2.2.
496 // Handle unknown extensions.
497 auto it = std::find_if(
498 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
499 [&entry](const auto& offered) { return entry.uri == offered.uri; });
500 if (it == header_extensions_to_offer_.end()) {
Markus Handellc17bca72021-01-14 17:08:01 +0100501 return RTCError(RTCErrorType::UNSUPPORTED_PARAMETER,
Markus Handell755c65d2020-06-24 01:06:10 +0200502 "Attempted to modify an unoffered extension.");
503 }
504
505 // Step 2.4-2.5.
506 // - Use of the transceiver interface indicates unified plan is in effect,
507 // hence the MID extension needs to be enabled.
508 // - Also handle the mandatory video orientation extensions.
509 if ((entry.uri == RtpExtension::kMidUri ||
510 entry.uri == RtpExtension::kVideoRotationUri) &&
511 entry.direction != RtpTransceiverDirection::kSendRecv) {
512 return RTCError(RTCErrorType::INVALID_MODIFICATION,
513 "Attempted to stop a mandatory extension.");
514 }
515 }
516
517 // Apply mutation after error checking.
518 for (const auto& entry : header_extensions_to_offer) {
519 auto it = std::find_if(
520 header_extensions_to_offer_.begin(), header_extensions_to_offer_.end(),
521 [&entry](const auto& offered) { return entry.uri == offered.uri; });
522 it->direction = entry.direction;
523 }
524
525 return RTCError::OK();
Markus Handell0357b3e2020-03-16 13:40:51 +0100526}
527
Harald Alvestrand6060df52020-08-11 09:54:02 +0200528void RtpTransceiver::SetPeerConnectionClosed() {
529 is_pc_closed_ = true;
530}
531
Steve Anton6e634bf2017-11-13 10:44:53 -0800532} // namespace webrtc