blob: 929736e8c6f4c41b05183b4018597651bf2e067d [file] [log] [blame]
Harald Alvestrandcdcfab02020-09-28 13:02:071/*
2 * Copyright 2020 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
11#include "pc/sdp_offer_answer.h"
12
Harald Alvestrandc06e3742020-10-01 10:23:3313#include <algorithm>
Harald Alvestrand1f7eab62020-10-18 16:51:4714#include <iterator>
Harald Alvestrand3eaee6b2020-10-19 06:35:5515#include <map>
Harald Alvestrandc06e3742020-10-01 10:23:3316#include <queue>
Harald Alvestrand1f7eab62020-10-18 16:51:4717#include <type_traits>
Harald Alvestrandf01bd6c2020-10-23 13:30:4618#include <utility>
Harald Alvestrandc06e3742020-10-01 10:23:3319
Harald Alvestrand1f7eab62020-10-18 16:51:4720#include "absl/algorithm/container.h"
Harald Alvestrand9cd199d2020-10-27 07:10:4321#include "absl/memory/memory.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4722#include "absl/strings/string_view.h"
23#include "api/array_view.h"
24#include "api/crypto/crypto_options.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4725#include "api/dtls_transport_interface.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4726#include "api/rtp_parameters.h"
27#include "api/rtp_receiver_interface.h"
28#include "api/rtp_sender_interface.h"
Harald Alvestrand763f5a92020-10-22 10:39:4029#include "api/video/builtin_video_bitrate_allocator_factory.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4730#include "media/base/codec.h"
31#include "media/base/media_engine.h"
32#include "media/base/rid_description.h"
Harald Alvestrand85466662021-04-19 21:21:3633#include "p2p/base/ice_transport_internal.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4734#include "p2p/base/p2p_constants.h"
35#include "p2p/base/p2p_transport_channel.h"
36#include "p2p/base/port.h"
37#include "p2p/base/transport_description.h"
38#include "p2p/base/transport_description_factory.h"
39#include "p2p/base/transport_info.h"
40#include "pc/data_channel_utils.h"
Harald Alvestrand85466662021-04-19 21:21:3641#include "pc/dtls_transport.h"
Harald Alvestrandcdcfab02020-09-28 13:02:0742#include "pc/media_stream.h"
Markus Handella1b82012021-05-26 16:56:3043#include "pc/media_stream_proxy.h"
Harald Alvestrandcdcfab02020-09-28 13:02:0744#include "pc/peer_connection.h"
Harald Alvestrand1090e442020-10-05 07:01:0945#include "pc/peer_connection_message_handler.h"
Harald Alvestrandcdcfab02020-09-28 13:02:0746#include "pc/rtp_media_utils.h"
Harald Alvestrand3eaee6b2020-10-19 06:35:5547#include "pc/rtp_sender.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4748#include "pc/rtp_transport_internal.h"
49#include "pc/simulcast_description.h"
Harald Alvestrand3eaee6b2020-10-19 06:35:5550#include "pc/stats_collector.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4751#include "pc/usage_pattern.h"
Harald Alvestrand763f5a92020-10-22 10:39:4052#include "pc/webrtc_session_description_factory.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4753#include "rtc_base/helpers.h"
54#include "rtc_base/location.h"
55#include "rtc_base/logging.h"
56#include "rtc_base/ref_counted_object.h"
Harald Alvestrand763f5a92020-10-22 10:39:4057#include "rtc_base/rtc_certificate.h"
Harald Alvestrand1f7eab62020-10-18 16:51:4758#include "rtc_base/ssl_stream_adapter.h"
59#include "rtc_base/string_encode.h"
60#include "rtc_base/strings/string_builder.h"
61#include "rtc_base/third_party/sigslot/sigslot.h"
Harald Alvestrandcdcfab02020-09-28 13:02:0762#include "rtc_base/trace_event.h"
Henrik Boström4ea80f32021-06-09 08:29:5063#include "system_wrappers/include/field_trial.h"
Harald Alvestrandcdcfab02020-09-28 13:02:0764#include "system_wrappers/include/metrics.h"
65
66using cricket::ContentInfo;
67using cricket::ContentInfos;
68using cricket::MediaContentDescription;
69using cricket::MediaProtocolType;
70using cricket::RidDescription;
71using cricket::RidDirection;
72using cricket::SessionDescription;
73using cricket::SimulcastDescription;
74using cricket::SimulcastLayer;
75using cricket::SimulcastLayerList;
76using cricket::StreamParams;
77using cricket::TransportInfo;
78
79using cricket::LOCAL_PORT_TYPE;
80using cricket::PRFLX_PORT_TYPE;
81using cricket::RELAY_PORT_TYPE;
82using cricket::STUN_PORT_TYPE;
83
84namespace webrtc {
85
86namespace {
87
Harald Alvestrandc06e3742020-10-01 10:23:3388typedef webrtc::PeerConnectionInterface::RTCOfferAnswerOptions
89 RTCOfferAnswerOptions;
90
Henrik Boström4ea80f32021-06-09 08:29:5091constexpr const char* kAlwaysAllowPayloadTypeDemuxingFieldTrialName =
92 "WebRTC-AlwaysAllowPayloadTypeDemuxing";
93
Harald Alvestrandcdcfab02020-09-28 13:02:0794// Error messages
95const char kInvalidSdp[] = "Invalid session description.";
96const char kInvalidCandidates[] = "Description contains invalid candidates.";
97const char kBundleWithoutRtcpMux[] =
98 "rtcp-mux must be enabled when BUNDLE "
99 "is enabled.";
100const char kMlineMismatchInAnswer[] =
101 "The order of m-lines in answer doesn't match order in offer. Rejecting "
102 "answer.";
103const char kMlineMismatchInSubsequentOffer[] =
104 "The order of m-lines in subsequent offer doesn't match order from "
105 "previous offer/answer.";
106const char kSdpWithoutIceUfragPwd[] =
107 "Called with SDP without ice-ufrag and ice-pwd.";
108const char kSdpWithoutDtlsFingerprint[] =
109 "Called with SDP without DTLS fingerprint.";
110const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto.";
111
Harald Alvestranda474fbf2020-10-01 16:47:23112const char kSessionError[] = "Session error code: ";
113const char kSessionErrorDesc[] = "Session error description: ";
114
Harald Alvestrandcdcfab02020-09-28 13:02:07115// UMA metric names.
116const char kSimulcastVersionApplyLocalDescription[] =
117 "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription";
118const char kSimulcastVersionApplyRemoteDescription[] =
119 "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription";
120const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled";
121
Harald Alvestrandbc9ca252020-10-05 13:08:41122// The length of RTCP CNAMEs.
123static const int kRtcpCnameLength = 16;
124
Harald Alvestranda474fbf2020-10-01 16:47:23125const char kDefaultStreamId[] = "default";
126// NOTE: Duplicated in peer_connection.cc:
127static const char kDefaultAudioSenderId[] = "defaulta0";
128static const char kDefaultVideoSenderId[] = "defaultv0";
129
Harald Alvestrandcdcfab02020-09-28 13:02:07130void NoteAddIceCandidateResult(int result) {
131 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result,
132 kAddIceCandidateMax);
133}
134
135void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type,
136 cricket::MediaType media_type) {
137 // Array of structs needed to map {KeyExchangeProtocolType,
138 // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in
139 // order to avoid -Wglobal-constructors and -Wexit-time-destructors.
140 static constexpr struct {
141 KeyExchangeProtocolType protocol_type;
142 cricket::MediaType media_type;
143 KeyExchangeProtocolMedia protocol_media;
144 } kEnumCounterKeyProtocolMediaMap[] = {
145 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO,
146 kEnumCounterKeyProtocolMediaTypeDtlsAudio},
147 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO,
148 kEnumCounterKeyProtocolMediaTypeDtlsVideo},
149 {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA,
150 kEnumCounterKeyProtocolMediaTypeDtlsData},
151 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO,
152 kEnumCounterKeyProtocolMediaTypeSdesAudio},
153 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO,
154 kEnumCounterKeyProtocolMediaTypeSdesVideo},
155 {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA,
156 kEnumCounterKeyProtocolMediaTypeSdesData},
157 };
158
159 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type,
160 kEnumCounterKeyProtocolMax);
161
162 for (const auto& i : kEnumCounterKeyProtocolMediaMap) {
163 if (i.protocol_type == protocol_type && i.media_type == media_type) {
164 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia",
165 i.protocol_media,
166 kEnumCounterKeyProtocolMediaTypeMax);
167 }
168 }
169}
170
Henrik Boströmf8187e02021-04-26 19:04:26171std::map<std::string, const cricket::ContentGroup*> GetBundleGroupsByMid(
172 const SessionDescription* desc) {
173 std::vector<const cricket::ContentGroup*> bundle_groups =
174 desc->GetGroupsByName(cricket::GROUP_TYPE_BUNDLE);
175 std::map<std::string, const cricket::ContentGroup*> bundle_groups_by_mid;
176 for (const cricket::ContentGroup* bundle_group : bundle_groups) {
177 for (const std::string& content_name : bundle_group->content_names()) {
178 bundle_groups_by_mid[content_name] = bundle_group;
179 }
180 }
181 return bundle_groups_by_mid;
182}
183
Harald Alvestrandcdcfab02020-09-28 13:02:07184// Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
185bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
186 const SessionDescriptionInterface* new_desc,
187 const std::string& content_name) {
188 if (!old_desc) {
189 return false;
190 }
191 const SessionDescription* new_sd = new_desc->description();
192 const SessionDescription* old_sd = old_desc->description();
193 const ContentInfo* cinfo = new_sd->GetContentByName(content_name);
194 if (!cinfo || cinfo->rejected) {
195 return false;
196 }
197 // If the content isn't rejected, check if ufrag and password has changed.
198 const cricket::TransportDescription* new_transport_desc =
199 new_sd->GetTransportDescriptionByName(content_name);
200 const cricket::TransportDescription* old_transport_desc =
201 old_sd->GetTransportDescriptionByName(content_name);
202 if (!new_transport_desc || !old_transport_desc) {
203 // No transport description exists. This is not an ICE restart.
204 return false;
205 }
206 if (cricket::IceCredentialsChanged(
207 old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd,
208 new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) {
209 RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name
210 << ".";
211 return true;
212 }
213 return false;
214}
215
216// Generates a string error message for SetLocalDescription/SetRemoteDescription
217// from an RTCError.
218std::string GetSetDescriptionErrorMessage(cricket::ContentSource source,
219 SdpType type,
220 const RTCError& error) {
221 rtc::StringBuilder oss;
222 oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote")
223 << " " << SdpTypeToString(type) << " sdp: " << error.message();
224 return oss.Release();
225}
226
227std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) {
228 std::string output = "streams=[";
229 const char* separator = "";
230 for (const auto& stream_id : stream_ids) {
231 output.append(separator).append(stream_id);
232 separator = ", ";
233 }
234 output.append("]");
235 return output;
236}
237
238void ReportSimulcastApiVersion(const char* name,
239 const SessionDescription& session) {
240 bool has_legacy = false;
241 bool has_spec_compliant = false;
242 for (const ContentInfo& content : session.contents()) {
243 if (!content.media_description()) {
244 continue;
245 }
246 has_spec_compliant |= content.media_description()->HasSimulcast();
247 for (const StreamParams& sp : content.media_description()->streams()) {
248 has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics);
249 }
250 }
251
252 if (has_legacy) {
253 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy,
254 kSimulcastApiVersionMax);
255 }
256 if (has_spec_compliant) {
257 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant,
258 kSimulcastApiVersionMax);
259 }
260 if (!has_legacy && !has_spec_compliant) {
261 RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone,
262 kSimulcastApiVersionMax);
263 }
264}
265
266const ContentInfo* FindTransceiverMSection(
Harald Alvestrand85466662021-04-19 21:21:36267 RtpTransceiver* transceiver,
Harald Alvestrandcdcfab02020-09-28 13:02:07268 const SessionDescriptionInterface* session_description) {
269 return transceiver->mid()
270 ? session_description->description()->GetContentByName(
271 *transceiver->mid())
272 : nullptr;
273}
274
275// If the direction is "recvonly" or "inactive", treat the description
276// as containing no streams.
277// See: https://code.google.com/p/webrtc/issues/detail?id=5054
278std::vector<cricket::StreamParams> GetActiveStreams(
279 const cricket::MediaContentDescription* desc) {
280 return RtpTransceiverDirectionHasSend(desc->direction())
281 ? desc->streams()
282 : std::vector<cricket::StreamParams>();
283}
284
285// Logic to decide if an m= section can be recycled. This means that the new
286// m= section is not rejected, but the old local or remote m= section is
287// rejected. |old_content_one| and |old_content_two| refer to the m= section
288// of the old remote and old local descriptions in no particular order.
289// We need to check both the old local and remote because either
290// could be the most current from the latest negotation.
291bool IsMediaSectionBeingRecycled(SdpType type,
292 const ContentInfo& content,
293 const ContentInfo* old_content_one,
294 const ContentInfo* old_content_two) {
295 return type == SdpType::kOffer && !content.rejected &&
296 ((old_content_one && old_content_one->rejected) ||
297 (old_content_two && old_content_two->rejected));
298}
299
300// Verify that the order of media sections in |new_desc| matches
301// |current_desc|. The number of m= sections in |new_desc| should be no
302// less than |current_desc|. In the case of checking an answer's
303// |new_desc|, the |current_desc| is the last offer that was set as the
304// local or remote. In the case of checking an offer's |new_desc| we
305// check against the local and remote descriptions stored from the last
306// negotiation, because either of these could be the most up to date for
307// possible rejected m sections. These are the |current_desc| and
308// |secondary_current_desc|.
309bool MediaSectionsInSameOrder(const SessionDescription& current_desc,
310 const SessionDescription* secondary_current_desc,
311 const SessionDescription& new_desc,
312 const SdpType type) {
313 if (current_desc.contents().size() > new_desc.contents().size()) {
314 return false;
315 }
316
317 for (size_t i = 0; i < current_desc.contents().size(); ++i) {
318 const cricket::ContentInfo* secondary_content_info = nullptr;
319 if (secondary_current_desc &&
320 i < secondary_current_desc->contents().size()) {
321 secondary_content_info = &secondary_current_desc->contents()[i];
322 }
323 if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i],
324 &current_desc.contents()[i],
325 secondary_content_info)) {
326 // For new offer descriptions, if the media section can be recycled, it's
327 // valid for the MID and media type to change.
328 continue;
329 }
330 if (new_desc.contents()[i].name != current_desc.contents()[i].name) {
331 return false;
332 }
333 const MediaContentDescription* new_desc_mdesc =
334 new_desc.contents()[i].media_description();
335 const MediaContentDescription* current_desc_mdesc =
336 current_desc.contents()[i].media_description();
337 if (new_desc_mdesc->type() != current_desc_mdesc->type()) {
338 return false;
339 }
340 }
341 return true;
342}
343
344bool MediaSectionsHaveSameCount(const SessionDescription& desc1,
345 const SessionDescription& desc2) {
346 return desc1.contents().size() == desc2.contents().size();
347}
348// Checks that each non-rejected content has SDES crypto keys or a DTLS
349// fingerprint, unless it's in a BUNDLE group, in which case only the
350// BUNDLE-tag section (first media section/description in the BUNDLE group)
351// needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint
352// to SDES keys, will be caught in JsepTransport negotiation, and backstopped
353// by Channel's |srtp_required| check.
Henrik Boströmf8187e02021-04-26 19:04:26354RTCError VerifyCrypto(const SessionDescription* desc,
355 bool dtls_enabled,
356 const std::map<std::string, const cricket::ContentGroup*>&
357 bundle_groups_by_mid) {
Harald Alvestrandcdcfab02020-09-28 13:02:07358 for (const cricket::ContentInfo& content_info : desc->contents()) {
359 if (content_info.rejected) {
360 continue;
361 }
362 // Note what media is used with each crypto protocol, for all sections.
363 NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls
364 : webrtc::kEnumCounterKeyProtocolSdes,
365 content_info.media_description()->type());
366 const std::string& mid = content_info.name;
Henrik Boströmf8187e02021-04-26 19:04:26367 auto it = bundle_groups_by_mid.find(mid);
368 const cricket::ContentGroup* bundle =
369 it != bundle_groups_by_mid.end() ? it->second : nullptr;
370 if (bundle && mid != *(bundle->FirstContentName())) {
Harald Alvestrandcdcfab02020-09-28 13:02:07371 // This isn't the first media section in the BUNDLE group, so it's not
372 // required to have crypto attributes, since only the crypto attributes
373 // from the first section actually get used.
374 continue;
375 }
376
377 // If the content isn't rejected or bundled into another m= section, crypto
378 // must be present.
379 const MediaContentDescription* media = content_info.media_description();
380 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
381 if (!media || !tinfo) {
382 // Something is not right.
383 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
384 }
385 if (dtls_enabled) {
386 if (!tinfo->description.identity_fingerprint) {
387 RTC_LOG(LS_WARNING)
388 << "Session description must have DTLS fingerprint if "
389 "DTLS enabled.";
390 return RTCError(RTCErrorType::INVALID_PARAMETER,
391 kSdpWithoutDtlsFingerprint);
392 }
393 } else {
394 if (media->cryptos().empty()) {
395 RTC_LOG(LS_WARNING)
396 << "Session description must have SDES when DTLS disabled.";
397 return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto);
398 }
399 }
400 }
401 return RTCError::OK();
402}
403
404// Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless
405// it's in a BUNDLE group, in which case only the BUNDLE-tag section (first
406// media section/description in the BUNDLE group) needs a ufrag and pwd.
Henrik Boströmf8187e02021-04-26 19:04:26407bool VerifyIceUfragPwdPresent(
408 const SessionDescription* desc,
409 const std::map<std::string, const cricket::ContentGroup*>&
410 bundle_groups_by_mid) {
Harald Alvestrandcdcfab02020-09-28 13:02:07411 for (const cricket::ContentInfo& content_info : desc->contents()) {
412 if (content_info.rejected) {
413 continue;
414 }
415 const std::string& mid = content_info.name;
Henrik Boströmf8187e02021-04-26 19:04:26416 auto it = bundle_groups_by_mid.find(mid);
417 const cricket::ContentGroup* bundle =
418 it != bundle_groups_by_mid.end() ? it->second : nullptr;
419 if (bundle && mid != *(bundle->FirstContentName())) {
Harald Alvestrandcdcfab02020-09-28 13:02:07420 // This isn't the first media section in the BUNDLE group, so it's not
421 // required to have ufrag/password, since only the ufrag/password from
422 // the first section actually get used.
423 continue;
424 }
425
426 // If the content isn't rejected or bundled into another m= section,
427 // ice-ufrag and ice-pwd must be present.
428 const TransportInfo* tinfo = desc->GetTransportInfoByName(mid);
429 if (!tinfo) {
430 // Something is not right.
431 RTC_LOG(LS_ERROR) << kInvalidSdp;
432 return false;
433 }
434 if (tinfo->description.ice_ufrag.empty() ||
435 tinfo->description.ice_pwd.empty()) {
436 RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd.";
437 return false;
438 }
439 }
440 return true;
441}
442
Harald Alvestrand85466662021-04-19 21:21:36443RTCError ValidateMids(const cricket::SessionDescription& description) {
Harald Alvestrandcdcfab02020-09-28 13:02:07444 std::set<std::string> mids;
445 for (const cricket::ContentInfo& content : description.contents()) {
446 if (content.name.empty()) {
447 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
448 "A media section is missing a MID attribute.");
449 }
450 if (!mids.insert(content.name).second) {
451 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
452 "Duplicate a=mid value '" + content.name + "'.");
453 }
454 }
455 return RTCError::OK();
456}
457
458bool IsValidOfferToReceiveMedia(int value) {
459 typedef PeerConnectionInterface::RTCOfferAnswerOptions Options;
460 return (value >= Options::kUndefined) &&
461 (value <= Options::kMaxOfferToReceiveMedia);
462}
463
464bool ValidateOfferAnswerOptions(
465 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) {
466 return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) &&
467 IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video);
468}
469
470// Map internal signaling state name to spec name:
471// https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum
472std::string GetSignalingStateString(
473 PeerConnectionInterface::SignalingState state) {
474 switch (state) {
475 case PeerConnectionInterface::kStable:
476 return "stable";
477 case PeerConnectionInterface::kHaveLocalOffer:
478 return "have-local-offer";
479 case PeerConnectionInterface::kHaveLocalPrAnswer:
480 return "have-local-pranswer";
481 case PeerConnectionInterface::kHaveRemoteOffer:
482 return "have-remote-offer";
483 case PeerConnectionInterface::kHaveRemotePrAnswer:
484 return "have-remote-pranswer";
485 case PeerConnectionInterface::kClosed:
486 return "closed";
487 }
488 RTC_NOTREACHED();
489 return "";
490}
491
492// This method will extract any send encodings that were sent by the remote
493// connection. This is currently only relevant for Simulcast scenario (where
494// the number of layers may be communicated by the server).
Harald Alvestrand85466662021-04-19 21:21:36495std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription(
Harald Alvestrandcdcfab02020-09-28 13:02:07496 const MediaContentDescription& desc) {
497 if (!desc.HasSimulcast()) {
498 return {};
499 }
500 std::vector<RtpEncodingParameters> result;
501 const SimulcastDescription& simulcast = desc.simulcast_description();
502
503 // This is a remote description, the parameters we are after should appear
504 // as receive streams.
505 for (const auto& alternatives : simulcast.receive_layers()) {
506 RTC_DCHECK(!alternatives.empty());
507 // There is currently no way to specify or choose from alternatives.
508 // We will always use the first alternative, which is the most preferred.
509 const SimulcastLayer& layer = alternatives[0];
510 RtpEncodingParameters parameters;
511 parameters.rid = layer.rid;
512 parameters.active = !layer.is_paused;
513 result.push_back(parameters);
514 }
515
516 return result;
517}
518
Harald Alvestrand85466662021-04-19 21:21:36519RTCError UpdateSimulcastLayerStatusInSender(
Harald Alvestrandcdcfab02020-09-28 13:02:07520 const std::vector<SimulcastLayer>& layers,
521 rtc::scoped_refptr<RtpSenderInternal> sender) {
522 RTC_DCHECK(sender);
523 RtpParameters parameters = sender->GetParametersInternal();
524 std::vector<std::string> disabled_layers;
525
526 // The simulcast envelope cannot be changed, only the status of the streams.
527 // So we will iterate over the send encodings rather than the layers.
528 for (RtpEncodingParameters& encoding : parameters.encodings) {
529 auto iter = std::find_if(layers.begin(), layers.end(),
530 [&encoding](const SimulcastLayer& layer) {
531 return layer.rid == encoding.rid;
532 });
533 // A layer that cannot be found may have been removed by the remote party.
534 if (iter == layers.end()) {
535 disabled_layers.push_back(encoding.rid);
536 continue;
537 }
538
539 encoding.active = !iter->is_paused;
540 }
541
542 RTCError result = sender->SetParametersInternal(parameters);
543 if (result.ok()) {
544 result = sender->DisableEncodingLayers(disabled_layers);
545 }
546
547 return result;
548}
549
Harald Alvestrand85466662021-04-19 21:21:36550bool SimulcastIsRejected(const ContentInfo* local_content,
Lennart Grahl0d0ed762021-05-17 14:06:37551 const MediaContentDescription& answer_media_desc,
552 bool enable_encrypted_rtp_header_extensions) {
Harald Alvestrandcdcfab02020-09-28 13:02:07553 bool simulcast_offered = local_content &&
554 local_content->media_description() &&
555 local_content->media_description()->HasSimulcast();
556 bool simulcast_answered = answer_media_desc.HasSimulcast();
557 bool rids_supported = RtpExtension::FindHeaderExtensionByUri(
Lennart Grahl0d0ed762021-05-17 14:06:37558 answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri,
559 enable_encrypted_rtp_header_extensions
560 ? RtpExtension::Filter::kPreferEncryptedExtension
561 : RtpExtension::Filter::kDiscardEncryptedExtension);
Harald Alvestrandcdcfab02020-09-28 13:02:07562 return simulcast_offered && (!simulcast_answered || !rids_supported);
563}
564
Harald Alvestrand85466662021-04-19 21:21:36565RTCError DisableSimulcastInSender(
Harald Alvestrandcdcfab02020-09-28 13:02:07566 rtc::scoped_refptr<RtpSenderInternal> sender) {
567 RTC_DCHECK(sender);
568 RtpParameters parameters = sender->GetParametersInternal();
569 if (parameters.encodings.size() <= 1) {
570 return RTCError::OK();
571 }
572
573 std::vector<std::string> disabled_layers;
574 std::transform(
575 parameters.encodings.begin() + 1, parameters.encodings.end(),
576 std::back_inserter(disabled_layers),
577 [](const RtpEncodingParameters& encoding) { return encoding.rid; });
578 return sender->DisableEncodingLayers(disabled_layers);
579}
580
Harald Alvestrandc06e3742020-10-01 10:23:33581// The SDP parser used to populate these values by default for the 'content
582// name' if an a=mid line was absent.
Harald Alvestrand85466662021-04-19 21:21:36583absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) {
Harald Alvestrandc06e3742020-10-01 10:23:33584 switch (media_type) {
585 case cricket::MEDIA_TYPE_AUDIO:
586 return cricket::CN_AUDIO;
587 case cricket::MEDIA_TYPE_VIDEO:
588 return cricket::CN_VIDEO;
589 case cricket::MEDIA_TYPE_DATA:
590 return cricket::CN_DATA;
Philipp Hancke4e8c1152020-10-13 10:43:15591 case cricket::MEDIA_TYPE_UNSUPPORTED:
592 return "not supported";
Harald Alvestrandc06e3742020-10-01 10:23:33593 }
594 RTC_NOTREACHED();
595 return "";
596}
597
598// Add options to |[audio/video]_media_description_options| from |senders|.
599void AddPlanBRtpSenderOptions(
600 const std::vector<rtc::scoped_refptr<
601 RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders,
602 cricket::MediaDescriptionOptions* audio_media_description_options,
603 cricket::MediaDescriptionOptions* video_media_description_options,
604 int num_sim_layers) {
605 for (const auto& sender : senders) {
606 if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
607 if (audio_media_description_options) {
608 audio_media_description_options->AddAudioSender(
609 sender->id(), sender->internal()->stream_ids());
610 }
611 } else {
612 RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO);
613 if (video_media_description_options) {
614 video_media_description_options->AddVideoSender(
615 sender->id(), sender->internal()->stream_ids(), {},
616 SimulcastLayerList(), num_sim_layers);
617 }
618 }
619 }
620}
621
Harald Alvestrand85466662021-04-19 21:21:36622cricket::MediaDescriptionOptions GetMediaDescriptionOptionsForTransceiver(
623 RtpTransceiver* transceiver,
Harald Alvestrandc06e3742020-10-01 10:23:33624 const std::string& mid,
625 bool is_create_offer) {
626 // NOTE: a stopping transceiver should be treated as a stopped one in
627 // createOffer as specified in
628 // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer.
629 bool stopped =
630 is_create_offer ? transceiver->stopping() : transceiver->stopped();
631 cricket::MediaDescriptionOptions media_description_options(
632 transceiver->media_type(), mid, transceiver->direction(), stopped);
633 media_description_options.codec_preferences =
634 transceiver->codec_preferences();
635 media_description_options.header_extensions =
636 transceiver->HeaderExtensionsToOffer();
637 // This behavior is specified in JSEP. The gist is that:
638 // 1. The MSID is included if the RtpTransceiver's direction is sendonly or
639 // sendrecv.
640 // 2. If the MSID is included, then it must be included in any subsequent
641 // offer/answer exactly the same until the RtpTransceiver is stopped.
642 if (stopped || (!RtpTransceiverDirectionHasSend(transceiver->direction()) &&
Harald Alvestrand85466662021-04-19 21:21:36643 !transceiver->has_ever_been_used_to_send())) {
Harald Alvestrandc06e3742020-10-01 10:23:33644 return media_description_options;
645 }
646
647 cricket::SenderOptions sender_options;
648 sender_options.track_id = transceiver->sender()->id();
649 sender_options.stream_ids = transceiver->sender()->stream_ids();
650
651 // The following sets up RIDs and Simulcast.
652 // RIDs are included if Simulcast is requested or if any RID was specified.
653 RtpParameters send_parameters =
Harald Alvestrand85466662021-04-19 21:21:36654 transceiver->sender_internal()->GetParametersInternal();
Harald Alvestrandc06e3742020-10-01 10:23:33655 bool has_rids = std::any_of(send_parameters.encodings.begin(),
656 send_parameters.encodings.end(),
657 [](const RtpEncodingParameters& encoding) {
658 return !encoding.rid.empty();
659 });
660
661 std::vector<RidDescription> send_rids;
662 SimulcastLayerList send_layers;
663 for (const RtpEncodingParameters& encoding : send_parameters.encodings) {
664 if (encoding.rid.empty()) {
665 continue;
666 }
667 send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend));
668 send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active));
669 }
670
671 if (has_rids) {
672 sender_options.rids = send_rids;
673 }
674
675 sender_options.simulcast_layers = send_layers;
676 // When RIDs are configured, we must set num_sim_layers to 0 to.
677 // Otherwise, num_sim_layers must be 1 because either there is no
678 // simulcast, or simulcast is acheived by munging the SDP.
679 sender_options.num_sim_layers = has_rids ? 0 : 1;
680 media_description_options.sender_options.push_back(sender_options);
681
682 return media_description_options;
683}
684
685// Returns the ContentInfo at mline index |i|, or null if none exists.
Harald Alvestrand85466662021-04-19 21:21:36686const ContentInfo* GetContentByIndex(const SessionDescriptionInterface* sdesc,
687 size_t i) {
Harald Alvestrandc06e3742020-10-01 10:23:33688 if (!sdesc) {
689 return nullptr;
690 }
691 const ContentInfos& contents = sdesc->description()->contents();
692 return (i < contents.size() ? &contents[i] : nullptr);
693}
694
695// From |rtc_options|, fill parts of |session_options| shared by all generated
696// m= sectionss (in other words, nothing that involves a map/array).
697void ExtractSharedMediaSessionOptions(
698 const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options,
699 cricket::MediaSessionOptions* session_options) {
700 session_options->vad_enabled = rtc_options.voice_activity_detection;
701 session_options->bundle_enabled = rtc_options.use_rtp_mux;
702 session_options->raw_packetization_for_video =
703 rtc_options.raw_packetization_for_video;
704}
705
Harald Alvestrandbc9ca252020-10-05 13:08:41706// Generate a RTCP CNAME when a PeerConnection is created.
707std::string GenerateRtcpCname() {
708 std::string cname;
709 if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) {
710 RTC_LOG(LS_ERROR) << "Failed to generate CNAME.";
711 RTC_NOTREACHED();
712 }
713 return cname;
714}
715
Harald Alvestrand6f04b6532020-10-09 11:42:17716// Check if we can send |new_stream| on a PeerConnection.
717bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams,
718 webrtc::MediaStreamInterface* new_stream) {
719 if (!new_stream || !current_streams) {
720 return false;
721 }
722 if (current_streams->find(new_stream->id()) != nullptr) {
723 RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id()
724 << " is already added.";
725 return false;
726 }
727 return true;
728}
729
Tomas Gunnarsson92eebef2021-02-10 12:05:44730rtc::scoped_refptr<webrtc::DtlsTransport> LookupDtlsTransportByMid(
731 rtc::Thread* network_thread,
732 JsepTransportController* controller,
733 const std::string& mid) {
734 // TODO(tommi): Can we post this (and associated operations where this
735 // function is called) to the network thread and avoid this Invoke?
736 // We might be able to simplify a few things if we set the transport on
737 // the network thread and then update the implementation to check that
738 // the set_ and relevant get methods are always called on the network
739 // thread (we'll need to update proxy maps).
740 return network_thread->Invoke<rtc::scoped_refptr<webrtc::DtlsTransport>>(
741 RTC_FROM_HERE,
742 [controller, &mid] { return controller->LookupDtlsTransportByMid(mid); });
743}
744
Henrik Boström4ea80f32021-06-09 08:29:50745bool ContentHasHeaderExtension(const cricket::ContentInfo& content_info,
746 absl::string_view header_extension_uri) {
747 for (const RtpExtension& rtp_header_extension :
748 content_info.media_description()->rtp_header_extensions()) {
749 if (rtp_header_extension.uri == header_extension_uri) {
750 return true;
751 }
752 }
753 return false;
754}
755
Harald Alvestrandcdcfab02020-09-28 13:02:07756} // namespace
757
758// Used by parameterless SetLocalDescription() to create an offer or answer.
759// Upon completion of creating the session description, SetLocalDescription() is
760// invoked with the result.
761class SdpOfferAnswerHandler::ImplicitCreateSessionDescriptionObserver
762 : public CreateSessionDescriptionObserver {
763 public:
764 ImplicitCreateSessionDescriptionObserver(
765 rtc::WeakPtr<SdpOfferAnswerHandler> sdp_handler,
766 rtc::scoped_refptr<SetLocalDescriptionObserverInterface>
767 set_local_description_observer)
768 : sdp_handler_(std::move(sdp_handler)),
769 set_local_description_observer_(
770 std::move(set_local_description_observer)) {}
771 ~ImplicitCreateSessionDescriptionObserver() override {
772 RTC_DCHECK(was_called_);
773 }
774
775 void SetOperationCompleteCallback(
776 std::function<void()> operation_complete_callback) {
777 operation_complete_callback_ = std::move(operation_complete_callback);
778 }
779
780 bool was_called() const { return was_called_; }
781
782 void OnSuccess(SessionDescriptionInterface* desc_ptr) override {
783 RTC_DCHECK(!was_called_);
784 std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr);
785 was_called_ = true;
786
787 // Abort early if |pc_| is no longer valid.
788 if (!sdp_handler_) {
789 operation_complete_callback_();
790 return;
791 }
792 // DoSetLocalDescription() is a synchronous operation that invokes
793 // |set_local_description_observer_| with the result.
794 sdp_handler_->DoSetLocalDescription(
795 std::move(desc), std::move(set_local_description_observer_));
796 operation_complete_callback_();
797 }
798
799 void OnFailure(RTCError error) override {
800 RTC_DCHECK(!was_called_);
801 was_called_ = true;
802 set_local_description_observer_->OnSetLocalDescriptionComplete(RTCError(
803 error.type(), std::string("SetLocalDescription failed to create "
804 "session description - ") +
805 error.message()));
806 operation_complete_callback_();
807 }
808
809 private:
810 bool was_called_ = false;
811 rtc::WeakPtr<SdpOfferAnswerHandler> sdp_handler_;
812 rtc::scoped_refptr<SetLocalDescriptionObserverInterface>
813 set_local_description_observer_;
814 std::function<void()> operation_complete_callback_;
815};
816
817// Wraps a CreateSessionDescriptionObserver and an OperationsChain operation
818// complete callback. When the observer is invoked, the wrapped observer is
819// invoked followed by invoking the completion callback.
820class CreateSessionDescriptionObserverOperationWrapper
821 : public CreateSessionDescriptionObserver {
822 public:
823 CreateSessionDescriptionObserverOperationWrapper(
824 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer,
825 std::function<void()> operation_complete_callback)
826 : observer_(std::move(observer)),
827 operation_complete_callback_(std::move(operation_complete_callback)) {
828 RTC_DCHECK(observer_);
829 }
830 ~CreateSessionDescriptionObserverOperationWrapper() override {
Tomas Gunnarsson36992362020-10-05 19:41:36831#if RTC_DCHECK_IS_ON
Harald Alvestrandcdcfab02020-09-28 13:02:07832 RTC_DCHECK(was_called_);
Tomas Gunnarsson36992362020-10-05 19:41:36833#endif
Harald Alvestrandcdcfab02020-09-28 13:02:07834 }
835
836 void OnSuccess(SessionDescriptionInterface* desc) override {
Tomas Gunnarsson36992362020-10-05 19:41:36837#if RTC_DCHECK_IS_ON
Harald Alvestrandcdcfab02020-09-28 13:02:07838 RTC_DCHECK(!was_called_);
Harald Alvestrandcdcfab02020-09-28 13:02:07839 was_called_ = true;
840#endif // RTC_DCHECK_IS_ON
841 // Completing the operation before invoking the observer allows the observer
842 // to execute SetLocalDescription() without delay.
843 operation_complete_callback_();
844 observer_->OnSuccess(desc);
845 }
846
847 void OnFailure(RTCError error) override {
Tomas Gunnarsson36992362020-10-05 19:41:36848#if RTC_DCHECK_IS_ON
Harald Alvestrandcdcfab02020-09-28 13:02:07849 RTC_DCHECK(!was_called_);
Harald Alvestrandcdcfab02020-09-28 13:02:07850 was_called_ = true;
851#endif // RTC_DCHECK_IS_ON
852 operation_complete_callback_();
853 observer_->OnFailure(std::move(error));
854 }
855
856 private:
Tomas Gunnarsson36992362020-10-05 19:41:36857#if RTC_DCHECK_IS_ON
Harald Alvestrandcdcfab02020-09-28 13:02:07858 bool was_called_ = false;
859#endif // RTC_DCHECK_IS_ON
860 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_;
861 std::function<void()> operation_complete_callback_;
862};
863
864// Wrapper for SetSessionDescriptionObserver that invokes the success or failure
865// callback in a posted message handled by the peer connection. This introduces
866// a delay that prevents recursive API calls by the observer, but this also
867// means that the PeerConnection can be modified before the observer sees the
868// result of the operation. This is ill-advised for synchronizing states.
869//
870// Implements both the SetLocalDescriptionObserverInterface and the
871// SetRemoteDescriptionObserverInterface.
872class SdpOfferAnswerHandler::SetSessionDescriptionObserverAdapter
873 : public SetLocalDescriptionObserverInterface,
874 public SetRemoteDescriptionObserverInterface {
875 public:
876 SetSessionDescriptionObserverAdapter(
877 rtc::WeakPtr<SdpOfferAnswerHandler> handler,
878 rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer)
879 : handler_(std::move(handler)),
880 inner_observer_(std::move(inner_observer)) {}
881
882 // SetLocalDescriptionObserverInterface implementation.
883 void OnSetLocalDescriptionComplete(RTCError error) override {
884 OnSetDescriptionComplete(std::move(error));
885 }
886 // SetRemoteDescriptionObserverInterface implementation.
887 void OnSetRemoteDescriptionComplete(RTCError error) override {
888 OnSetDescriptionComplete(std::move(error));
889 }
890
891 private:
892 void OnSetDescriptionComplete(RTCError error) {
893 if (!handler_)
894 return;
895 if (error.ok()) {
Harald Alvestrand1090e442020-10-05 07:01:09896 handler_->pc_->message_handler()->PostSetSessionDescriptionSuccess(
897 inner_observer_);
Harald Alvestrandcdcfab02020-09-28 13:02:07898 } else {
Harald Alvestrand1090e442020-10-05 07:01:09899 handler_->pc_->message_handler()->PostSetSessionDescriptionFailure(
900 inner_observer_, std::move(error));
Harald Alvestrandcdcfab02020-09-28 13:02:07901 }
902 }
903
904 rtc::WeakPtr<SdpOfferAnswerHandler> handler_;
905 rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer_;
906};
907
908class SdpOfferAnswerHandler::LocalIceCredentialsToReplace {
909 public:
910 // Sets the ICE credentials that need restarting to the ICE credentials of
911 // the current and pending descriptions.
912 void SetIceCredentialsFromLocalDescriptions(
913 const SessionDescriptionInterface* current_local_description,
914 const SessionDescriptionInterface* pending_local_description) {
915 ice_credentials_.clear();
916 if (current_local_description) {
917 AppendIceCredentialsFromSessionDescription(*current_local_description);
918 }
919 if (pending_local_description) {
920 AppendIceCredentialsFromSessionDescription(*pending_local_description);
921 }
922 }
923
924 void ClearIceCredentials() { ice_credentials_.clear(); }
925
926 // Returns true if we have ICE credentials that need restarting.
927 bool HasIceCredentials() const { return !ice_credentials_.empty(); }
928
929 // Returns true if |local_description| shares no ICE credentials with the
930 // ICE credentials that need restarting.
931 bool SatisfiesIceRestart(
932 const SessionDescriptionInterface& local_description) const {
933 for (const auto& transport_info :
934 local_description.description()->transport_infos()) {
935 if (ice_credentials_.find(std::make_pair(
936 transport_info.description.ice_ufrag,
937 transport_info.description.ice_pwd)) != ice_credentials_.end()) {
938 return false;
939 }
940 }
941 return true;
942 }
943
944 private:
945 void AppendIceCredentialsFromSessionDescription(
946 const SessionDescriptionInterface& desc) {
947 for (const auto& transport_info : desc.description()->transport_infos()) {
948 ice_credentials_.insert(
949 std::make_pair(transport_info.description.ice_ufrag,
950 transport_info.description.ice_pwd));
951 }
952 }
953
954 std::set<std::pair<std::string, std::string>> ice_credentials_;
955};
956
957SdpOfferAnswerHandler::SdpOfferAnswerHandler(PeerConnection* pc)
958 : pc_(pc),
Harald Alvestrand6f04b6532020-10-09 11:42:17959 local_streams_(StreamCollection::Create()),
960 remote_streams_(StreamCollection::Create()),
Harald Alvestrandcdcfab02020-09-28 13:02:07961 operations_chain_(rtc::OperationsChain::Create()),
Harald Alvestrandbc9ca252020-10-05 13:08:41962 rtcp_cname_(GenerateRtcpCname()),
Harald Alvestrandcdcfab02020-09-28 13:02:07963 local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()),
964 weak_ptr_factory_(this) {
965 operations_chain_->SetOnChainEmptyCallback(
966 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr()]() {
967 if (!this_weak_ptr)
968 return;
969 this_weak_ptr->OnOperationsChainEmpty();
970 });
971}
972
973SdpOfferAnswerHandler::~SdpOfferAnswerHandler() {}
974
Harald Alvestrand9cd199d2020-10-27 07:10:43975// Static
976std::unique_ptr<SdpOfferAnswerHandler> SdpOfferAnswerHandler::Create(
977 PeerConnection* pc,
978 const PeerConnectionInterface::RTCConfiguration& configuration,
979 PeerConnectionDependencies& dependencies) {
980 auto handler = absl::WrapUnique(new SdpOfferAnswerHandler(pc));
981 handler->Initialize(configuration, dependencies);
982 return handler;
983}
984
Harald Alvestrand763f5a92020-10-22 10:39:40985void SdpOfferAnswerHandler::Initialize(
986 const PeerConnectionInterface::RTCConfiguration& configuration,
Harald Alvestrand9cd199d2020-10-27 07:10:43987 PeerConnectionDependencies& dependencies) {
Harald Alvestrand763f5a92020-10-22 10:39:40988 RTC_DCHECK_RUN_ON(signaling_thread());
989 video_options_.screencast_min_bitrate_kbps =
990 configuration.screencast_min_bitrate;
991 audio_options_.combined_audio_video_bwe =
992 configuration.combined_audio_video_bwe;
993
994 audio_options_.audio_jitter_buffer_max_packets =
995 configuration.audio_jitter_buffer_max_packets;
996
997 audio_options_.audio_jitter_buffer_fast_accelerate =
998 configuration.audio_jitter_buffer_fast_accelerate;
999
1000 audio_options_.audio_jitter_buffer_min_delay_ms =
1001 configuration.audio_jitter_buffer_min_delay_ms;
1002
1003 audio_options_.audio_jitter_buffer_enable_rtx_handling =
1004 configuration.audio_jitter_buffer_enable_rtx_handling;
1005
1006 // Obtain a certificate from RTCConfiguration if any were provided (optional).
1007 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
1008 if (!configuration.certificates.empty()) {
1009 // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of
1010 // just picking the first one. The decision should be made based on the DTLS
1011 // handshake. The DTLS negotiations need to know about all certificates.
1012 certificate = configuration.certificates[0];
1013 }
1014
1015 webrtc_session_desc_factory_ =
1016 std::make_unique<WebRtcSessionDescriptionFactory>(
1017 signaling_thread(), channel_manager(), this, pc_->session_id(),
Harald Alvestrand9cd199d2020-10-27 07:10:431018 pc_->dtls_enabled(), std::move(dependencies.cert_generator),
Harald Alvestranda0947872020-11-09 14:15:001019 certificate, &ssrc_generator_,
1020 [this](const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
1021 transport_controller()->SetLocalCertificate(certificate);
1022 });
Harald Alvestrand763f5a92020-10-22 10:39:401023
Harald Alvestrand4da4a872020-11-04 10:34:211024 if (pc_->options()->disable_encryption) {
Harald Alvestrand763f5a92020-10-22 10:39:401025 webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
1026 }
1027
1028 webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions(
1029 pc_->GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions);
1030 webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan());
1031
Harald Alvestrand9cd199d2020-10-27 07:10:431032 if (dependencies.video_bitrate_allocator_factory) {
Harald Alvestrand763f5a92020-10-22 10:39:401033 video_bitrate_allocator_factory_ =
Harald Alvestrand9cd199d2020-10-27 07:10:431034 std::move(dependencies.video_bitrate_allocator_factory);
Harald Alvestrand763f5a92020-10-22 10:39:401035 } else {
1036 video_bitrate_allocator_factory_ =
1037 CreateBuiltinVideoBitrateAllocatorFactory();
1038 }
1039}
1040
Harald Alvestrandbc9ca252020-10-05 13:08:411041// ==================================================================
1042// Access to pc_ variables
1043cricket::ChannelManager* SdpOfferAnswerHandler::channel_manager() const {
1044 return pc_->channel_manager();
1045}
Harald Alvestrande15fb152020-10-19 13:28:051046TransceiverList* SdpOfferAnswerHandler::transceivers() {
1047 if (!pc_->rtp_manager()) {
1048 return nullptr;
1049 }
1050 return pc_->rtp_manager()->transceivers();
Harald Alvestrandbc9ca252020-10-05 13:08:411051}
Harald Alvestrande15fb152020-10-19 13:28:051052const TransceiverList* SdpOfferAnswerHandler::transceivers() const {
1053 if (!pc_->rtp_manager()) {
1054 return nullptr;
1055 }
1056 return pc_->rtp_manager()->transceivers();
Harald Alvestrandbc9ca252020-10-05 13:08:411057}
1058JsepTransportController* SdpOfferAnswerHandler::transport_controller() {
Harald Alvestrand653429c2020-10-19 16:05:201059 return pc_->transport_controller();
Harald Alvestrandbc9ca252020-10-05 13:08:411060}
Harald Alvestrandf01bd6c2020-10-23 13:30:461061const JsepTransportController* SdpOfferAnswerHandler::transport_controller()
1062 const {
1063 return pc_->transport_controller();
1064}
Harald Alvestrandbc9ca252020-10-05 13:08:411065DataChannelController* SdpOfferAnswerHandler::data_channel_controller() {
Harald Alvestrand653429c2020-10-19 16:05:201066 return pc_->data_channel_controller();
Harald Alvestrandbc9ca252020-10-05 13:08:411067}
1068const DataChannelController* SdpOfferAnswerHandler::data_channel_controller()
1069 const {
Harald Alvestrand653429c2020-10-19 16:05:201070 return pc_->data_channel_controller();
Harald Alvestrandbc9ca252020-10-05 13:08:411071}
1072cricket::PortAllocator* SdpOfferAnswerHandler::port_allocator() {
Harald Alvestrand653429c2020-10-19 16:05:201073 return pc_->port_allocator();
Harald Alvestrandbc9ca252020-10-05 13:08:411074}
1075const cricket::PortAllocator* SdpOfferAnswerHandler::port_allocator() const {
Harald Alvestrand653429c2020-10-19 16:05:201076 return pc_->port_allocator();
Harald Alvestrandbc9ca252020-10-05 13:08:411077}
Harald Alvestrande15fb152020-10-19 13:28:051078RtpTransmissionManager* SdpOfferAnswerHandler::rtp_manager() {
1079 return pc_->rtp_manager();
1080}
1081const RtpTransmissionManager* SdpOfferAnswerHandler::rtp_manager() const {
1082 return pc_->rtp_manager();
1083}
Harald Alvestrandbc9ca252020-10-05 13:08:411084
1085// ===================================================================
1086
Harald Alvestrandcdcfab02020-09-28 13:02:071087void SdpOfferAnswerHandler::PrepareForShutdown() {
1088 RTC_DCHECK_RUN_ON(signaling_thread());
1089 weak_ptr_factory_.InvalidateWeakPtrs();
1090}
1091
1092void SdpOfferAnswerHandler::Close() {
1093 ChangeSignalingState(PeerConnectionInterface::kClosed);
1094}
1095
1096void SdpOfferAnswerHandler::RestartIce() {
1097 RTC_DCHECK_RUN_ON(signaling_thread());
1098 local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions(
1099 current_local_description(), pending_local_description());
1100 UpdateNegotiationNeeded();
1101}
1102
1103rtc::Thread* SdpOfferAnswerHandler::signaling_thread() const {
1104 return pc_->signaling_thread();
1105}
1106
1107void SdpOfferAnswerHandler::CreateOffer(
1108 CreateSessionDescriptionObserver* observer,
1109 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
1110 RTC_DCHECK_RUN_ON(signaling_thread());
1111 // Chain this operation. If asynchronous operations are pending on the chain,
1112 // this operation will be queued to be invoked, otherwise the contents of the
1113 // lambda will execute immediately.
1114 operations_chain_->ChainOperation(
1115 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1116 observer_refptr =
1117 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
1118 options](std::function<void()> operations_chain_callback) {
1119 // Abort early if |this_weak_ptr| is no longer valid.
1120 if (!this_weak_ptr) {
1121 observer_refptr->OnFailure(
1122 RTCError(RTCErrorType::INTERNAL_ERROR,
1123 "CreateOffer failed because the session was shut down"));
1124 operations_chain_callback();
1125 return;
1126 }
1127 // The operation completes asynchronously when the wrapper is invoked.
1128 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
1129 observer_wrapper(new rtc::RefCountedObject<
1130 CreateSessionDescriptionObserverOperationWrapper>(
1131 std::move(observer_refptr),
1132 std::move(operations_chain_callback)));
1133 this_weak_ptr->DoCreateOffer(options, observer_wrapper);
1134 });
1135}
1136
1137void SdpOfferAnswerHandler::SetLocalDescription(
1138 SetSessionDescriptionObserver* observer,
1139 SessionDescriptionInterface* desc_ptr) {
1140 RTC_DCHECK_RUN_ON(signaling_thread());
1141 // Chain this operation. If asynchronous operations are pending on the chain,
1142 // this operation will be queued to be invoked, otherwise the contents of the
1143 // lambda will execute immediately.
1144 operations_chain_->ChainOperation(
1145 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1146 observer_refptr =
1147 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
1148 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
1149 std::function<void()> operations_chain_callback) mutable {
1150 // Abort early if |this_weak_ptr| is no longer valid.
1151 if (!this_weak_ptr) {
1152 // For consistency with SetSessionDescriptionObserverAdapter whose
1153 // posted messages doesn't get processed when the PC is destroyed, we
1154 // do not inform |observer_refptr| that the operation failed.
1155 operations_chain_callback();
1156 return;
1157 }
1158 // SetSessionDescriptionObserverAdapter takes care of making sure the
1159 // |observer_refptr| is invoked in a posted message.
1160 this_weak_ptr->DoSetLocalDescription(
1161 std::move(desc),
1162 rtc::scoped_refptr<SetLocalDescriptionObserverInterface>(
1163 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
1164 this_weak_ptr, observer_refptr)));
1165 // For backwards-compatability reasons, we declare the operation as
1166 // completed here (rather than in a post), so that the operation chain
1167 // is not blocked by this operation when the observer is invoked. This
1168 // allows the observer to trigger subsequent offer/answer operations
1169 // synchronously if the operation chain is now empty.
1170 operations_chain_callback();
1171 });
1172}
1173
1174void SdpOfferAnswerHandler::SetLocalDescription(
1175 std::unique_ptr<SessionDescriptionInterface> desc,
1176 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
1177 RTC_DCHECK_RUN_ON(signaling_thread());
1178 // Chain this operation. If asynchronous operations are pending on the chain,
1179 // this operation will be queued to be invoked, otherwise the contents of the
1180 // lambda will execute immediately.
1181 operations_chain_->ChainOperation(
1182 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
1183 desc = std::move(desc)](
1184 std::function<void()> operations_chain_callback) mutable {
1185 // Abort early if |this_weak_ptr| is no longer valid.
1186 if (!this_weak_ptr) {
1187 observer->OnSetLocalDescriptionComplete(RTCError(
1188 RTCErrorType::INTERNAL_ERROR,
1189 "SetLocalDescription failed because the session was shut down"));
1190 operations_chain_callback();
1191 return;
1192 }
1193 this_weak_ptr->DoSetLocalDescription(std::move(desc), observer);
1194 // DoSetLocalDescription() is implemented as a synchronous operation.
1195 // The |observer| will already have been informed that it completed, and
1196 // we can mark this operation as complete without any loose ends.
1197 operations_chain_callback();
1198 });
1199}
1200
1201void SdpOfferAnswerHandler::SetLocalDescription(
1202 SetSessionDescriptionObserver* observer) {
1203 RTC_DCHECK_RUN_ON(signaling_thread());
1204 SetLocalDescription(
1205 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
1206 weak_ptr_factory_.GetWeakPtr(), observer));
1207}
1208
1209void SdpOfferAnswerHandler::SetLocalDescription(
1210 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
1211 RTC_DCHECK_RUN_ON(signaling_thread());
1212 // The |create_sdp_observer| handles performing DoSetLocalDescription() with
1213 // the resulting description as well as completing the operation.
1214 rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver>
1215 create_sdp_observer(
1216 new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>(
1217 weak_ptr_factory_.GetWeakPtr(), observer));
1218 // Chain this operation. If asynchronous operations are pending on the chain,
1219 // this operation will be queued to be invoked, otherwise the contents of the
1220 // lambda will execute immediately.
1221 operations_chain_->ChainOperation(
1222 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1223 create_sdp_observer](std::function<void()> operations_chain_callback) {
1224 // The |create_sdp_observer| is responsible for completing the
1225 // operation.
1226 create_sdp_observer->SetOperationCompleteCallback(
1227 std::move(operations_chain_callback));
1228 // Abort early if |this_weak_ptr| is no longer valid. This triggers the
1229 // same code path as if DoCreateOffer() or DoCreateAnswer() failed.
1230 if (!this_weak_ptr) {
1231 create_sdp_observer->OnFailure(RTCError(
1232 RTCErrorType::INTERNAL_ERROR,
1233 "SetLocalDescription failed because the session was shut down"));
1234 return;
1235 }
1236 switch (this_weak_ptr->signaling_state()) {
1237 case PeerConnectionInterface::kStable:
1238 case PeerConnectionInterface::kHaveLocalOffer:
1239 case PeerConnectionInterface::kHaveRemotePrAnswer:
1240 // TODO(hbos): If [LastCreatedOffer] exists and still represents the
1241 // current state of the system, use that instead of creating another
1242 // offer.
1243 this_weak_ptr->DoCreateOffer(
1244 PeerConnectionInterface::RTCOfferAnswerOptions(),
1245 create_sdp_observer);
1246 break;
1247 case PeerConnectionInterface::kHaveLocalPrAnswer:
1248 case PeerConnectionInterface::kHaveRemoteOffer:
1249 // TODO(hbos): If [LastCreatedAnswer] exists and still represents
1250 // the current state of the system, use that instead of creating
1251 // another answer.
1252 this_weak_ptr->DoCreateAnswer(
1253 PeerConnectionInterface::RTCOfferAnswerOptions(),
1254 create_sdp_observer);
1255 break;
1256 case PeerConnectionInterface::kClosed:
1257 create_sdp_observer->OnFailure(RTCError(
1258 RTCErrorType::INVALID_STATE,
1259 "SetLocalDescription called when PeerConnection is closed."));
1260 break;
1261 }
1262 });
1263}
1264
1265RTCError SdpOfferAnswerHandler::ApplyLocalDescription(
Henrik Boströmf8187e02021-04-26 19:04:261266 std::unique_ptr<SessionDescriptionInterface> desc,
1267 const std::map<std::string, const cricket::ContentGroup*>&
1268 bundle_groups_by_mid) {
Markus Handell518669d2021-06-07 11:30:461269 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::ApplyLocalDescription");
Harald Alvestrandcdcfab02020-09-28 13:02:071270 RTC_DCHECK_RUN_ON(signaling_thread());
1271 RTC_DCHECK(desc);
1272
1273 // Update stats here so that we have the most recent stats for tracks and
1274 // streams that might be removed by updating the session description.
1275 pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
1276
1277 // Take a reference to the old local description since it's used below to
1278 // compare against the new local description. When setting the new local
1279 // description, grab ownership of the replaced session description in case it
1280 // is the same as |old_local_description|, to keep it alive for the duration
1281 // of the method.
1282 const SessionDescriptionInterface* old_local_description =
1283 local_description();
1284 std::unique_ptr<SessionDescriptionInterface> replaced_local_description;
1285 SdpType type = desc->GetType();
1286 if (type == SdpType::kAnswer) {
1287 replaced_local_description = pending_local_description_
1288 ? std::move(pending_local_description_)
1289 : std::move(current_local_description_);
1290 current_local_description_ = std::move(desc);
1291 pending_local_description_ = nullptr;
1292 current_remote_description_ = std::move(pending_remote_description_);
1293 } else {
1294 replaced_local_description = std::move(pending_local_description_);
1295 pending_local_description_ = std::move(desc);
1296 }
1297 // The session description to apply now must be accessed by
1298 // |local_description()|.
1299 RTC_DCHECK(local_description());
1300
1301 // Report statistics about any use of simulcast.
1302 ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription,
1303 *local_description()->description());
1304
1305 if (!is_caller_) {
1306 if (remote_description()) {
1307 // Remote description was applied first, so this PC is the callee.
1308 is_caller_ = false;
1309 } else {
1310 // Local description is applied first, so this PC is the caller.
1311 is_caller_ = true;
1312 }
1313 }
1314
Harald Alvestranda474fbf2020-10-01 16:47:231315 RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type);
Harald Alvestrandcdcfab02020-09-28 13:02:071316 if (!error.ok()) {
1317 return error;
1318 }
1319
1320 if (IsUnifiedPlan()) {
1321 RTCError error = UpdateTransceiversAndDataChannels(
1322 cricket::CS_LOCAL, *local_description(), old_local_description,
Henrik Boströmf8187e02021-04-26 19:04:261323 remote_description(), bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:071324 if (!error.ok()) {
1325 return error;
1326 }
1327 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
1328 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
Harald Alvestrand85466662021-04-19 21:21:361329 for (const auto& transceiver_ext : transceivers()->List()) {
1330 auto transceiver = transceiver_ext->internal();
Harald Alvestrandcdcfab02020-09-28 13:02:071331 if (transceiver->stopped()) {
1332 continue;
1333 }
1334
1335 // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots.
1336 // Note that code paths that don't set MID won't be able to use
1337 // information about DTLS transports.
1338 if (transceiver->mid()) {
Tomas Gunnarsson92eebef2021-02-10 12:05:441339 auto dtls_transport = LookupDtlsTransportByMid(
1340 pc_->network_thread(), transport_controller(), *transceiver->mid());
Harald Alvestrand85466662021-04-19 21:21:361341 transceiver->sender_internal()->set_transport(dtls_transport);
1342 transceiver->receiver_internal()->set_transport(dtls_transport);
Harald Alvestrandcdcfab02020-09-28 13:02:071343 }
1344
1345 const ContentInfo* content =
Harald Alvestrandc06e3742020-10-01 10:23:331346 FindMediaSectionForTransceiver(transceiver, local_description());
Harald Alvestrandcdcfab02020-09-28 13:02:071347 if (!content) {
1348 continue;
1349 }
1350 const MediaContentDescription* media_desc = content->media_description();
1351 // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run
1352 // the following steps:
1353 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
1354 // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and
1355 // transceiver's [[FiredDirection]] slot is either "sendrecv" or
1356 // "recvonly", process the removal of a remote track for the media
1357 // description, given transceiver, removeList, and muteTracks.
1358 if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
Harald Alvestrand85466662021-04-19 21:21:361359 (transceiver->fired_direction() &&
1360 RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) {
1361 ProcessRemovalOfRemoteTrack(transceiver_ext, &remove_list,
Harald Alvestranda474fbf2020-10-01 16:47:231362 &removed_streams);
Harald Alvestrandcdcfab02020-09-28 13:02:071363 }
1364 // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and
1365 // [[FiredDirection]] slots to direction.
Harald Alvestrand85466662021-04-19 21:21:361366 transceiver->set_current_direction(media_desc->direction());
1367 transceiver->set_fired_direction(media_desc->direction());
Harald Alvestrandcdcfab02020-09-28 13:02:071368 }
1369 }
1370 auto observer = pc_->Observer();
1371 for (const auto& transceiver : remove_list) {
1372 observer->OnRemoveTrack(transceiver->receiver());
1373 }
1374 for (const auto& stream : removed_streams) {
1375 observer->OnRemoveStream(stream);
1376 }
1377 } else {
1378 // Media channels will be created only when offer is set. These may use new
1379 // transports just created by PushdownTransportDescription.
1380 if (type == SdpType::kOffer) {
1381 // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local
1382 // description is applied. Restore back to old description.
Harald Alvestrandbc9ca252020-10-05 13:08:411383 RTCError error = CreateChannels(*local_description()->description());
Harald Alvestrandcdcfab02020-09-28 13:02:071384 if (!error.ok()) {
1385 return error;
1386 }
1387 }
1388 // Remove unused channels if MediaContentDescription is rejected.
Harald Alvestranda474fbf2020-10-01 16:47:231389 RemoveUnusedChannels(local_description()->description());
Harald Alvestrandcdcfab02020-09-28 13:02:071390 }
1391
1392 error = UpdateSessionState(type, cricket::CS_LOCAL,
Henrik Boströmf8187e02021-04-26 19:04:261393 local_description()->description(),
1394 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:071395 if (!error.ok()) {
1396 return error;
1397 }
1398
1399 if (remote_description()) {
1400 // Now that we have a local description, we can push down remote candidates.
Harald Alvestrandbc9ca252020-10-05 13:08:411401 UseCandidatesInSessionDescription(remote_description());
Harald Alvestrandcdcfab02020-09-28 13:02:071402 }
1403
1404 pending_ice_restarts_.clear();
Harald Alvestranda474fbf2020-10-01 16:47:231405 if (session_error() != SessionError::kNone) {
1406 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
Harald Alvestrandcdcfab02020-09-28 13:02:071407 }
1408
1409 // If setting the description decided our SSL role, allocate any necessary
1410 // SCTP sids.
1411 rtc::SSLRole role;
Florent Castelli516e2842021-04-19 13:29:501412 if (pc_->GetSctpSslRole(&role)) {
Harald Alvestrandbc9ca252020-10-05 13:08:411413 data_channel_controller()->AllocateSctpSids(role);
Harald Alvestrandcdcfab02020-09-28 13:02:071414 }
1415
1416 if (IsUnifiedPlan()) {
Harald Alvestrand85466662021-04-19 21:21:361417 // We must use List and not ListInternal here because
1418 // transceivers()->StableState() is indexed by the non-internal refptr.
1419 for (const auto& transceiver_ext : transceivers()->List()) {
1420 auto transceiver = transceiver_ext->internal();
Harald Alvestrandcdcfab02020-09-28 13:02:071421 if (transceiver->stopped()) {
1422 continue;
1423 }
1424 const ContentInfo* content =
Harald Alvestrandc06e3742020-10-01 10:23:331425 FindMediaSectionForTransceiver(transceiver, local_description());
Harald Alvestrandcdcfab02020-09-28 13:02:071426 if (!content) {
1427 continue;
1428 }
Harald Alvestrand85466662021-04-19 21:21:361429 cricket::ChannelInterface* channel = transceiver->channel();
Harald Alvestrandcdcfab02020-09-28 13:02:071430 if (content->rejected || !channel || channel->local_streams().empty()) {
1431 // 0 is a special value meaning "this sender has no associated send
1432 // stream". Need to call this so the sender won't attempt to configure
1433 // a no longer existing stream and run into DCHECKs in the lower
1434 // layers.
Harald Alvestrand85466662021-04-19 21:21:361435 transceiver->sender_internal()->SetSsrc(0);
Harald Alvestrandcdcfab02020-09-28 13:02:071436 } else {
1437 // Get the StreamParams from the channel which could generate SSRCs.
1438 const std::vector<StreamParams>& streams = channel->local_streams();
Harald Alvestrand85466662021-04-19 21:21:361439 transceiver->sender_internal()->set_stream_ids(streams[0].stream_ids());
1440 auto encodings = transceiver->sender_internal()->init_send_encodings();
1441 transceiver->sender_internal()->SetSsrc(streams[0].first_ssrc());
Eldar Rello950d6b92021-04-06 19:38:001442 if (!encodings.empty()) {
1443 transceivers()
Harald Alvestrand85466662021-04-19 21:21:361444 ->StableState(transceiver_ext)
Eldar Rello950d6b92021-04-06 19:38:001445 ->SetInitSendEncodings(encodings);
1446 }
Harald Alvestrandcdcfab02020-09-28 13:02:071447 }
1448 }
1449 } else {
1450 // Plan B semantics.
1451
1452 // Update state and SSRC of local MediaStreams and DataChannels based on the
1453 // local session description.
1454 const cricket::ContentInfo* audio_content =
1455 GetFirstAudioContent(local_description()->description());
1456 if (audio_content) {
1457 if (audio_content->rejected) {
Harald Alvestranda474fbf2020-10-01 16:47:231458 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
Harald Alvestrandcdcfab02020-09-28 13:02:071459 } else {
1460 const cricket::AudioContentDescription* audio_desc =
1461 audio_content->media_description()->as_audio();
Harald Alvestranda474fbf2020-10-01 16:47:231462 UpdateLocalSenders(audio_desc->streams(), audio_desc->type());
Harald Alvestrandcdcfab02020-09-28 13:02:071463 }
1464 }
1465
1466 const cricket::ContentInfo* video_content =
1467 GetFirstVideoContent(local_description()->description());
1468 if (video_content) {
1469 if (video_content->rejected) {
Harald Alvestranda474fbf2020-10-01 16:47:231470 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrandcdcfab02020-09-28 13:02:071471 } else {
1472 const cricket::VideoContentDescription* video_desc =
1473 video_content->media_description()->as_video();
Harald Alvestranda474fbf2020-10-01 16:47:231474 UpdateLocalSenders(video_desc->streams(), video_desc->type());
Harald Alvestrandcdcfab02020-09-28 13:02:071475 }
1476 }
1477 }
1478
Harald Alvestrand7af57c62021-04-16 11:12:141479 // This function does nothing with data content.
Harald Alvestrandcdcfab02020-09-28 13:02:071480
1481 if (type == SdpType::kAnswer &&
1482 local_ice_credentials_to_replace_->SatisfiesIceRestart(
1483 *current_local_description_)) {
1484 local_ice_credentials_to_replace_->ClearIceCredentials();
1485 }
1486
1487 return RTCError::OK();
1488}
1489
1490void SdpOfferAnswerHandler::SetRemoteDescription(
1491 SetSessionDescriptionObserver* observer,
1492 SessionDescriptionInterface* desc_ptr) {
1493 RTC_DCHECK_RUN_ON(signaling_thread());
1494 // Chain this operation. If asynchronous operations are pending on the chain,
1495 // this operation will be queued to be invoked, otherwise the contents of the
1496 // lambda will execute immediately.
1497 operations_chain_->ChainOperation(
1498 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
1499 observer_refptr =
1500 rtc::scoped_refptr<SetSessionDescriptionObserver>(observer),
1501 desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)](
1502 std::function<void()> operations_chain_callback) mutable {
1503 // Abort early if |this_weak_ptr| is no longer valid.
1504 if (!this_weak_ptr) {
1505 // For consistency with SetSessionDescriptionObserverAdapter whose
1506 // posted messages doesn't get processed when the PC is destroyed, we
1507 // do not inform |observer_refptr| that the operation failed.
1508 operations_chain_callback();
1509 return;
1510 }
1511 // SetSessionDescriptionObserverAdapter takes care of making sure the
1512 // |observer_refptr| is invoked in a posted message.
1513 this_weak_ptr->DoSetRemoteDescription(
1514 std::move(desc),
1515 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>(
1516 new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>(
1517 this_weak_ptr, observer_refptr)));
1518 // For backwards-compatability reasons, we declare the operation as
1519 // completed here (rather than in a post), so that the operation chain
1520 // is not blocked by this operation when the observer is invoked. This
1521 // allows the observer to trigger subsequent offer/answer operations
1522 // synchronously if the operation chain is now empty.
1523 operations_chain_callback();
1524 });
1525}
1526
1527void SdpOfferAnswerHandler::SetRemoteDescription(
1528 std::unique_ptr<SessionDescriptionInterface> desc,
1529 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
1530 RTC_DCHECK_RUN_ON(signaling_thread());
1531 // Chain this operation. If asynchronous operations are pending on the chain,
1532 // this operation will be queued to be invoked, otherwise the contents of the
1533 // lambda will execute immediately.
1534 operations_chain_->ChainOperation(
1535 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer,
1536 desc = std::move(desc)](
1537 std::function<void()> operations_chain_callback) mutable {
1538 // Abort early if |this_weak_ptr| is no longer valid.
1539 if (!this_weak_ptr) {
1540 observer->OnSetRemoteDescriptionComplete(RTCError(
1541 RTCErrorType::INTERNAL_ERROR,
1542 "SetRemoteDescription failed because the session was shut down"));
1543 operations_chain_callback();
1544 return;
1545 }
1546 this_weak_ptr->DoSetRemoteDescription(std::move(desc),
1547 std::move(observer));
1548 // DoSetRemoteDescription() is implemented as a synchronous operation.
1549 // The |observer| will already have been informed that it completed, and
1550 // we can mark this operation as complete without any loose ends.
1551 operations_chain_callback();
1552 });
1553}
1554
1555RTCError SdpOfferAnswerHandler::ApplyRemoteDescription(
Henrik Boströmf8187e02021-04-26 19:04:261556 std::unique_ptr<SessionDescriptionInterface> desc,
1557 const std::map<std::string, const cricket::ContentGroup*>&
1558 bundle_groups_by_mid) {
Markus Handell518669d2021-06-07 11:30:461559 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::ApplyRemoteDescription");
Harald Alvestrandcdcfab02020-09-28 13:02:071560 RTC_DCHECK_RUN_ON(signaling_thread());
1561 RTC_DCHECK(desc);
1562
1563 // Update stats here so that we have the most recent stats for tracks and
1564 // streams that might be removed by updating the session description.
1565 pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
1566
1567 // Take a reference to the old remote description since it's used below to
1568 // compare against the new remote description. When setting the new remote
1569 // description, grab ownership of the replaced session description in case it
1570 // is the same as |old_remote_description|, to keep it alive for the duration
1571 // of the method.
1572 const SessionDescriptionInterface* old_remote_description =
1573 remote_description();
1574 std::unique_ptr<SessionDescriptionInterface> replaced_remote_description;
1575 SdpType type = desc->GetType();
1576 if (type == SdpType::kAnswer) {
1577 replaced_remote_description = pending_remote_description_
1578 ? std::move(pending_remote_description_)
1579 : std::move(current_remote_description_);
1580 current_remote_description_ = std::move(desc);
1581 pending_remote_description_ = nullptr;
1582 current_local_description_ = std::move(pending_local_description_);
1583 } else {
1584 replaced_remote_description = std::move(pending_remote_description_);
1585 pending_remote_description_ = std::move(desc);
1586 }
1587 // The session description to apply now must be accessed by
1588 // |remote_description()|.
1589 RTC_DCHECK(remote_description());
1590
1591 // Report statistics about any use of simulcast.
1592 ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription,
1593 *remote_description()->description());
1594
Harald Alvestranda474fbf2020-10-01 16:47:231595 RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type);
Harald Alvestrandcdcfab02020-09-28 13:02:071596 if (!error.ok()) {
1597 return error;
1598 }
1599 // Transport and Media channels will be created only when offer is set.
1600 if (IsUnifiedPlan()) {
1601 RTCError error = UpdateTransceiversAndDataChannels(
1602 cricket::CS_REMOTE, *remote_description(), local_description(),
Henrik Boströmf8187e02021-04-26 19:04:261603 old_remote_description, bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:071604 if (!error.ok()) {
1605 return error;
1606 }
1607 } else {
1608 // Media channels will be created only when offer is set. These may use new
1609 // transports just created by PushdownTransportDescription.
1610 if (type == SdpType::kOffer) {
1611 // TODO(mallinath) - Handle CreateChannel failure, as new local
1612 // description is applied. Restore back to old description.
Harald Alvestrandbc9ca252020-10-05 13:08:411613 RTCError error = CreateChannels(*remote_description()->description());
Harald Alvestrandcdcfab02020-09-28 13:02:071614 if (!error.ok()) {
1615 return error;
1616 }
1617 }
1618 // Remove unused channels if MediaContentDescription is rejected.
Harald Alvestranda474fbf2020-10-01 16:47:231619 RemoveUnusedChannels(remote_description()->description());
Harald Alvestrandcdcfab02020-09-28 13:02:071620 }
1621
1622 // NOTE: Candidates allocation will be initiated only when
1623 // SetLocalDescription is called.
1624 error = UpdateSessionState(type, cricket::CS_REMOTE,
Henrik Boströmf8187e02021-04-26 19:04:261625 remote_description()->description(),
1626 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:071627 if (!error.ok()) {
1628 return error;
1629 }
1630
1631 if (local_description() &&
Harald Alvestrandbc9ca252020-10-05 13:08:411632 !UseCandidatesInSessionDescription(remote_description())) {
Harald Alvestrandcdcfab02020-09-28 13:02:071633 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates);
1634 }
1635
1636 if (old_remote_description) {
1637 for (const cricket::ContentInfo& content :
1638 old_remote_description->description()->contents()) {
1639 // Check if this new SessionDescription contains new ICE ufrag and
1640 // password that indicates the remote peer requests an ICE restart.
1641 // TODO(deadbeef): When we start storing both the current and pending
1642 // remote description, this should reset pending_ice_restarts and compare
1643 // against the current description.
1644 if (CheckForRemoteIceRestart(old_remote_description, remote_description(),
1645 content.name)) {
1646 if (type == SdpType::kOffer) {
1647 pending_ice_restarts_.insert(content.name);
1648 }
1649 } else {
1650 // We retain all received candidates only if ICE is not restarted.
1651 // When ICE is restarted, all previous candidates belong to an old
1652 // generation and should not be kept.
1653 // TODO(deadbeef): This goes against the W3C spec which says the remote
1654 // description should only contain candidates from the last set remote
1655 // description plus any candidates added since then. We should remove
1656 // this once we're sure it won't break anything.
1657 WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
1658 old_remote_description, content.name, mutable_remote_description());
1659 }
1660 }
1661 }
1662
Harald Alvestranda474fbf2020-10-01 16:47:231663 if (session_error() != SessionError::kNone) {
1664 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
Harald Alvestrandcdcfab02020-09-28 13:02:071665 }
1666
1667 // Set the the ICE connection state to connecting since the connection may
1668 // become writable with peer reflexive candidates before any remote candidate
1669 // is signaled.
1670 // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix
1671 // is to have a new signal the indicates a change in checking state from the
1672 // transport and expose a new checking() member from transport that can be
1673 // read to determine the current checking state. The existing SignalConnecting
1674 // actually means "gathering candidates", so cannot be be used here.
1675 if (remote_description()->GetType() != SdpType::kOffer &&
1676 remote_description()->number_of_mediasections() > 0u &&
1677 pc_->ice_connection_state() ==
1678 PeerConnectionInterface::kIceConnectionNew) {
1679 pc_->SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking);
1680 }
1681
1682 // If setting the description decided our SSL role, allocate any necessary
1683 // SCTP sids.
1684 rtc::SSLRole role;
Florent Castelli516e2842021-04-19 13:29:501685 if (pc_->GetSctpSslRole(&role)) {
Harald Alvestrandbc9ca252020-10-05 13:08:411686 data_channel_controller()->AllocateSctpSids(role);
Harald Alvestrandcdcfab02020-09-28 13:02:071687 }
1688
1689 if (IsUnifiedPlan()) {
1690 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>
1691 now_receiving_transceivers;
1692 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list;
1693 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
1694 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
Harald Alvestrand85466662021-04-19 21:21:361695 for (const auto& transceiver_ext : transceivers()->List()) {
1696 const auto transceiver = transceiver_ext->internal();
Harald Alvestrandc06e3742020-10-01 10:23:331697 const ContentInfo* content =
1698 FindMediaSectionForTransceiver(transceiver, remote_description());
Harald Alvestrandcdcfab02020-09-28 13:02:071699 if (!content) {
1700 continue;
1701 }
1702 const MediaContentDescription* media_desc = content->media_description();
1703 RtpTransceiverDirection local_direction =
1704 RtpTransceiverDirectionReversed(media_desc->direction());
1705 // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the
1706 // RTCSessionDescription: Set the associated remote streams given
1707 // transceiver.[[Receiver]], msids, addList, and removeList".
1708 // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription
1709 if (RtpTransceiverDirectionHasRecv(local_direction)) {
1710 std::vector<std::string> stream_ids;
1711 if (!media_desc->streams().empty()) {
1712 // The remote description has signaled the stream IDs.
1713 stream_ids = media_desc->streams()[0].stream_ids();
1714 }
Harald Alvestrandbc9ca252020-10-05 13:08:411715 transceivers()
Harald Alvestrand85466662021-04-19 21:21:361716 ->StableState(transceiver_ext)
Harald Alvestrand38b768c2020-09-29 11:54:051717 ->SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids());
Harald Alvestrandcdcfab02020-09-28 13:02:071718
1719 RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name
1720 << " (" << GetStreamIdsString(stream_ids) << ").";
Harald Alvestrand85466662021-04-19 21:21:361721 SetAssociatedRemoteStreams(transceiver->receiver_internal(), stream_ids,
1722 &added_streams, &removed_streams);
Harald Alvestrandcdcfab02020-09-28 13:02:071723 // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6
1724 // "Set the RTCSessionDescription: If direction is sendrecv or recvonly,
1725 // and transceiver's current direction is neither sendrecv nor recvonly,
1726 // process the addition of a remote track for the media description.
1727 if (!transceiver->fired_direction() ||
1728 !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) {
1729 RTC_LOG(LS_INFO)
1730 << "Processing the addition of a remote track for MID="
1731 << content->name << ".";
1732 now_receiving_transceivers.push_back(transceiver);
1733 }
1734 }
1735 // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's
1736 // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the
1737 // removal of a remote track for the media description, given transceiver,
1738 // removeList, and muteTracks.
1739 if (!RtpTransceiverDirectionHasRecv(local_direction) &&
1740 (transceiver->fired_direction() &&
1741 RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) {
Harald Alvestrand85466662021-04-19 21:21:361742 ProcessRemovalOfRemoteTrack(transceiver_ext, &remove_list,
Harald Alvestranda474fbf2020-10-01 16:47:231743 &removed_streams);
Harald Alvestrandcdcfab02020-09-28 13:02:071744 }
1745 // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction.
Harald Alvestrand85466662021-04-19 21:21:361746 transceiver->set_fired_direction(local_direction);
Harald Alvestrandcdcfab02020-09-28 13:02:071747 // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run
1748 // the following steps:
1749 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
1750 // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to
1751 // direction.
Harald Alvestrand85466662021-04-19 21:21:361752 transceiver->set_current_direction(local_direction);
Harald Alvestrandcdcfab02020-09-28 13:02:071753 // 2.2.8.1.11.[3-6]: Set the transport internal slots.
1754 if (transceiver->mid()) {
Tomas Gunnarsson92eebef2021-02-10 12:05:441755 auto dtls_transport = LookupDtlsTransportByMid(pc_->network_thread(),
1756 transport_controller(),
1757 *transceiver->mid());
Harald Alvestrand85466662021-04-19 21:21:361758 transceiver->sender_internal()->set_transport(dtls_transport);
1759 transceiver->receiver_internal()->set_transport(dtls_transport);
Harald Alvestrandcdcfab02020-09-28 13:02:071760 }
1761 }
1762 // 2.2.8.1.12: If the media description is rejected, and transceiver is
1763 // not already stopped, stop the RTCRtpTransceiver transceiver.
1764 if (content->rejected && !transceiver->stopped()) {
1765 RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name
1766 << " since the media section was rejected.";
Harald Alvestrand85466662021-04-19 21:21:361767 transceiver->StopTransceiverProcedure();
Harald Alvestrandcdcfab02020-09-28 13:02:071768 }
1769 if (!content->rejected &&
1770 RtpTransceiverDirectionHasRecv(local_direction)) {
1771 if (!media_desc->streams().empty() &&
1772 media_desc->streams()[0].has_ssrcs()) {
1773 uint32_t ssrc = media_desc->streams()[0].first_ssrc();
Harald Alvestrand85466662021-04-19 21:21:361774 transceiver->receiver_internal()->SetupMediaChannel(ssrc);
Harald Alvestrandcdcfab02020-09-28 13:02:071775 } else {
Harald Alvestrand85466662021-04-19 21:21:361776 transceiver->receiver_internal()->SetupUnsignaledMediaChannel();
Harald Alvestrandcdcfab02020-09-28 13:02:071777 }
1778 }
1779 }
1780 // Once all processing has finished, fire off callbacks.
1781 auto observer = pc_->Observer();
1782 for (const auto& transceiver : now_receiving_transceivers) {
1783 pc_->stats()->AddTrack(transceiver->receiver()->track());
1784 observer->OnTrack(transceiver);
1785 observer->OnAddTrack(transceiver->receiver(),
1786 transceiver->receiver()->streams());
1787 }
1788 for (const auto& stream : added_streams) {
1789 observer->OnAddStream(stream);
1790 }
1791 for (const auto& transceiver : remove_list) {
1792 observer->OnRemoveTrack(transceiver->receiver());
1793 }
1794 for (const auto& stream : removed_streams) {
1795 observer->OnRemoveStream(stream);
1796 }
1797 }
1798
1799 const cricket::ContentInfo* audio_content =
1800 GetFirstAudioContent(remote_description()->description());
1801 const cricket::ContentInfo* video_content =
1802 GetFirstVideoContent(remote_description()->description());
1803 const cricket::AudioContentDescription* audio_desc =
1804 GetFirstAudioContentDescription(remote_description()->description());
1805 const cricket::VideoContentDescription* video_desc =
1806 GetFirstVideoContentDescription(remote_description()->description());
Harald Alvestrandcdcfab02020-09-28 13:02:071807
1808 // Check if the descriptions include streams, just in case the peer supports
1809 // MSID, but doesn't indicate so with "a=msid-semantic".
1810 if (remote_description()->description()->msid_supported() ||
1811 (audio_desc && !audio_desc->streams().empty()) ||
1812 (video_desc && !video_desc->streams().empty())) {
1813 remote_peer_supports_msid_ = true;
1814 }
1815
1816 // We wait to signal new streams until we finish processing the description,
1817 // since only at that point will new streams have all their tracks.
1818 rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create());
1819
1820 if (!IsUnifiedPlan()) {
1821 // TODO(steveanton): When removing RTP senders/receivers in response to a
1822 // rejected media section, there is some cleanup logic that expects the
1823 // voice/ video channel to still be set. But in this method the voice/video
1824 // channel would have been destroyed by the SetRemoteDescription caller
1825 // above so the cleanup that relies on them fails to run. The RemoveSenders
1826 // calls should be moved to right before the DestroyChannel calls to fix
1827 // this.
1828
1829 // Find all audio rtp streams and create corresponding remote AudioTracks
1830 // and MediaStreams.
1831 if (audio_content) {
1832 if (audio_content->rejected) {
Harald Alvestranda474fbf2020-10-01 16:47:231833 RemoveSenders(cricket::MEDIA_TYPE_AUDIO);
Harald Alvestrandcdcfab02020-09-28 13:02:071834 } else {
1835 bool default_audio_track_needed =
1836 !remote_peer_supports_msid_ &&
1837 RtpTransceiverDirectionHasSend(audio_desc->direction());
Harald Alvestranda474fbf2020-10-01 16:47:231838 UpdateRemoteSendersList(GetActiveStreams(audio_desc),
1839 default_audio_track_needed, audio_desc->type(),
1840 new_streams);
Harald Alvestrandcdcfab02020-09-28 13:02:071841 }
1842 }
1843
1844 // Find all video rtp streams and create corresponding remote VideoTracks
1845 // and MediaStreams.
1846 if (video_content) {
1847 if (video_content->rejected) {
Harald Alvestranda474fbf2020-10-01 16:47:231848 RemoveSenders(cricket::MEDIA_TYPE_VIDEO);
Harald Alvestrandcdcfab02020-09-28 13:02:071849 } else {
1850 bool default_video_track_needed =
1851 !remote_peer_supports_msid_ &&
1852 RtpTransceiverDirectionHasSend(video_desc->direction());
Harald Alvestranda474fbf2020-10-01 16:47:231853 UpdateRemoteSendersList(GetActiveStreams(video_desc),
1854 default_video_track_needed, video_desc->type(),
1855 new_streams);
Harald Alvestrandcdcfab02020-09-28 13:02:071856 }
1857 }
1858
Harald Alvestrandcdcfab02020-09-28 13:02:071859 // Iterate new_streams and notify the observer about new MediaStreams.
1860 auto observer = pc_->Observer();
1861 for (size_t i = 0; i < new_streams->count(); ++i) {
1862 MediaStreamInterface* new_stream = new_streams->at(i);
1863 pc_->stats()->AddStream(new_stream);
1864 observer->OnAddStream(
1865 rtc::scoped_refptr<MediaStreamInterface>(new_stream));
1866 }
1867
Harald Alvestrandbc9ca252020-10-05 13:08:411868 UpdateEndedRemoteMediaStreams();
Harald Alvestrandcdcfab02020-09-28 13:02:071869 }
1870
1871 if (type == SdpType::kAnswer &&
1872 local_ice_credentials_to_replace_->SatisfiesIceRestart(
1873 *current_local_description_)) {
1874 local_ice_credentials_to_replace_->ClearIceCredentials();
1875 }
1876
1877 return RTCError::OK();
1878}
1879
1880void SdpOfferAnswerHandler::DoSetLocalDescription(
1881 std::unique_ptr<SessionDescriptionInterface> desc,
1882 rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) {
1883 RTC_DCHECK_RUN_ON(signaling_thread());
1884 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoSetLocalDescription");
1885
1886 if (!observer) {
1887 RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL.";
1888 return;
1889 }
1890
1891 if (!desc) {
1892 observer->OnSetLocalDescriptionComplete(
1893 RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL."));
1894 return;
1895 }
1896
1897 // If a session error has occurred the PeerConnection is in a possibly
1898 // inconsistent state so fail right away.
Harald Alvestranda474fbf2020-10-01 16:47:231899 if (session_error() != SessionError::kNone) {
1900 std::string error_message = GetSessionErrorMsg();
Harald Alvestrandcdcfab02020-09-28 13:02:071901 RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message;
1902 observer->OnSetLocalDescriptionComplete(
1903 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
1904 return;
1905 }
1906
1907 // For SLD we support only explicit rollback.
1908 if (desc->GetType() == SdpType::kRollback) {
1909 if (IsUnifiedPlan()) {
1910 observer->OnSetLocalDescriptionComplete(Rollback(desc->GetType()));
1911 } else {
1912 observer->OnSetLocalDescriptionComplete(
1913 RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
1914 "Rollback not supported in Plan B"));
1915 }
1916 return;
1917 }
1918
Henrik Boströmf8187e02021-04-26 19:04:261919 std::map<std::string, const cricket::ContentGroup*> bundle_groups_by_mid =
1920 GetBundleGroupsByMid(desc->description());
1921 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL,
1922 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:071923 if (!error.ok()) {
1924 std::string error_message = GetSetDescriptionErrorMessage(
1925 cricket::CS_LOCAL, desc->GetType(), error);
1926 RTC_LOG(LS_ERROR) << error_message;
1927 observer->OnSetLocalDescriptionComplete(
1928 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
1929 return;
1930 }
1931
1932 // Grab the description type before moving ownership to ApplyLocalDescription,
1933 // which may destroy it before returning.
1934 const SdpType type = desc->GetType();
1935
Henrik Boströmf8187e02021-04-26 19:04:261936 error = ApplyLocalDescription(std::move(desc), bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:071937 // |desc| may be destroyed at this point.
1938
1939 if (!error.ok()) {
1940 // If ApplyLocalDescription fails, the PeerConnection could be in an
1941 // inconsistent state, so act conservatively here and set the session error
1942 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
Harald Alvestranda474fbf2020-10-01 16:47:231943 SetSessionError(SessionError::kContent, error.message());
Harald Alvestrandcdcfab02020-09-28 13:02:071944 std::string error_message =
1945 GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error);
1946 RTC_LOG(LS_ERROR) << error_message;
1947 observer->OnSetLocalDescriptionComplete(
1948 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
1949 return;
1950 }
1951 RTC_DCHECK(local_description());
1952
1953 if (local_description()->GetType() == SdpType::kAnswer) {
Harald Alvestranda474fbf2020-10-01 16:47:231954 RemoveStoppedTransceivers();
Harald Alvestrandcdcfab02020-09-28 13:02:071955
1956 // TODO(deadbeef): We already had to hop to the network thread for
1957 // MaybeStartGathering...
1958 pc_->network_thread()->Invoke<void>(
Niels Möller4bab23f2021-01-18 08:24:331959 RTC_FROM_HERE, [this] { port_allocator()->DiscardCandidatePool(); });
Harald Alvestrandcdcfab02020-09-28 13:02:071960 // Make UMA notes about what was agreed to.
Harald Alvestranda474fbf2020-10-01 16:47:231961 ReportNegotiatedSdpSemantics(*local_description());
Harald Alvestrandcdcfab02020-09-28 13:02:071962 }
1963
1964 observer->OnSetLocalDescriptionComplete(RTCError::OK());
Harald Alvestrand44d0dff2020-10-09 05:43:531965 pc_->NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED);
Harald Alvestrandcdcfab02020-09-28 13:02:071966
1967 // Check if negotiation is needed. We must do this after informing the
1968 // observer that SetLocalDescription() has completed to ensure negotiation is
1969 // not needed prior to the promise resolving.
1970 if (IsUnifiedPlan()) {
1971 bool was_negotiation_needed = is_negotiation_needed_;
1972 UpdateNegotiationNeeded();
1973 if (signaling_state() == PeerConnectionInterface::kStable &&
1974 was_negotiation_needed && is_negotiation_needed_) {
1975 // Legacy version.
1976 pc_->Observer()->OnRenegotiationNeeded();
1977 // Spec-compliant version; the event may get invalidated before firing.
1978 GenerateNegotiationNeededEvent();
1979 }
1980 }
1981
1982 // MaybeStartGathering needs to be called after informing the observer so that
1983 // we don't signal any candidates before signaling that SetLocalDescription
1984 // completed.
Harald Alvestrandbc9ca252020-10-05 13:08:411985 transport_controller()->MaybeStartGathering();
Harald Alvestrandcdcfab02020-09-28 13:02:071986}
1987
1988void SdpOfferAnswerHandler::DoCreateOffer(
1989 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
1990 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
1991 RTC_DCHECK_RUN_ON(signaling_thread());
1992 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoCreateOffer");
1993
1994 if (!observer) {
1995 RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL.";
1996 return;
1997 }
1998
1999 if (pc_->IsClosed()) {
2000 std::string error = "CreateOffer called when PeerConnection is closed.";
2001 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand1090e442020-10-05 07:01:092002 pc_->message_handler()->PostCreateSessionDescriptionFailure(
Harald Alvestrandcdcfab02020-09-28 13:02:072003 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
2004 return;
2005 }
2006
2007 // If a session error has occurred the PeerConnection is in a possibly
2008 // inconsistent state so fail right away.
Harald Alvestranda474fbf2020-10-01 16:47:232009 if (session_error() != SessionError::kNone) {
2010 std::string error_message = GetSessionErrorMsg();
Harald Alvestrandcdcfab02020-09-28 13:02:072011 RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message;
Harald Alvestrand1090e442020-10-05 07:01:092012 pc_->message_handler()->PostCreateSessionDescriptionFailure(
Harald Alvestrandcdcfab02020-09-28 13:02:072013 observer,
2014 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2015 return;
2016 }
2017
2018 if (!ValidateOfferAnswerOptions(options)) {
2019 std::string error = "CreateOffer called with invalid options.";
2020 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand1090e442020-10-05 07:01:092021 pc_->message_handler()->PostCreateSessionDescriptionFailure(
Harald Alvestrandcdcfab02020-09-28 13:02:072022 observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error)));
2023 return;
2024 }
2025
2026 // Legacy handling for offer_to_receive_audio and offer_to_receive_video.
2027 // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions".
2028 if (IsUnifiedPlan()) {
Harald Alvestranda474fbf2020-10-01 16:47:232029 RTCError error = HandleLegacyOfferOptions(options);
Harald Alvestrandcdcfab02020-09-28 13:02:072030 if (!error.ok()) {
Harald Alvestrand1090e442020-10-05 07:01:092031 pc_->message_handler()->PostCreateSessionDescriptionFailure(
2032 observer, std::move(error));
Harald Alvestrandcdcfab02020-09-28 13:02:072033 return;
2034 }
2035 }
2036
2037 cricket::MediaSessionOptions session_options;
Harald Alvestrandc06e3742020-10-01 10:23:332038 GetOptionsForOffer(options, &session_options);
Harald Alvestrandcdcfab02020-09-28 13:02:072039 webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
2040}
2041
2042void SdpOfferAnswerHandler::CreateAnswer(
2043 CreateSessionDescriptionObserver* observer,
2044 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
Markus Handell518669d2021-06-07 11:30:462045 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::CreateAnswer");
Harald Alvestrandcdcfab02020-09-28 13:02:072046 RTC_DCHECK_RUN_ON(signaling_thread());
2047 // Chain this operation. If asynchronous operations are pending on the chain,
2048 // this operation will be queued to be invoked, otherwise the contents of the
2049 // lambda will execute immediately.
2050 operations_chain_->ChainOperation(
2051 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2052 observer_refptr =
2053 rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer),
2054 options](std::function<void()> operations_chain_callback) {
2055 // Abort early if |this_weak_ptr| is no longer valid.
2056 if (!this_weak_ptr) {
2057 observer_refptr->OnFailure(RTCError(
2058 RTCErrorType::INTERNAL_ERROR,
2059 "CreateAnswer failed because the session was shut down"));
2060 operations_chain_callback();
2061 return;
2062 }
2063 // The operation completes asynchronously when the wrapper is invoked.
2064 rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper>
2065 observer_wrapper(new rtc::RefCountedObject<
2066 CreateSessionDescriptionObserverOperationWrapper>(
2067 std::move(observer_refptr),
2068 std::move(operations_chain_callback)));
2069 this_weak_ptr->DoCreateAnswer(options, observer_wrapper);
2070 });
2071}
2072
2073void SdpOfferAnswerHandler::DoCreateAnswer(
2074 const PeerConnectionInterface::RTCOfferAnswerOptions& options,
2075 rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) {
2076 RTC_DCHECK_RUN_ON(signaling_thread());
2077 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoCreateAnswer");
2078 if (!observer) {
2079 RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL.";
2080 return;
2081 }
2082
2083 // If a session error has occurred the PeerConnection is in a possibly
2084 // inconsistent state so fail right away.
Harald Alvestranda474fbf2020-10-01 16:47:232085 if (session_error() != SessionError::kNone) {
2086 std::string error_message = GetSessionErrorMsg();
Harald Alvestrandcdcfab02020-09-28 13:02:072087 RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message;
Harald Alvestrand1090e442020-10-05 07:01:092088 pc_->message_handler()->PostCreateSessionDescriptionFailure(
Harald Alvestrandcdcfab02020-09-28 13:02:072089 observer,
2090 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2091 return;
2092 }
2093
2094 if (!(signaling_state_ == PeerConnectionInterface::kHaveRemoteOffer ||
2095 signaling_state_ == PeerConnectionInterface::kHaveLocalPrAnswer)) {
2096 std::string error =
2097 "PeerConnection cannot create an answer in a state other than "
2098 "have-remote-offer or have-local-pranswer.";
2099 RTC_LOG(LS_ERROR) << error;
Harald Alvestrand1090e442020-10-05 07:01:092100 pc_->message_handler()->PostCreateSessionDescriptionFailure(
Harald Alvestrandcdcfab02020-09-28 13:02:072101 observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
2102 return;
2103 }
2104
2105 // The remote description should be set if we're in the right state.
2106 RTC_DCHECK(remote_description());
2107
2108 if (IsUnifiedPlan()) {
2109 if (options.offer_to_receive_audio !=
2110 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) {
2111 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not "
2112 "supported with Unified Plan semantics. Use the "
2113 "RtpTransceiver API instead.";
2114 }
2115 if (options.offer_to_receive_video !=
2116 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) {
2117 RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not "
2118 "supported with Unified Plan semantics. Use the "
2119 "RtpTransceiver API instead.";
2120 }
2121 }
2122
2123 cricket::MediaSessionOptions session_options;
Harald Alvestrandc06e3742020-10-01 10:23:332124 GetOptionsForAnswer(options, &session_options);
Harald Alvestrandcdcfab02020-09-28 13:02:072125 webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
2126}
2127
2128void SdpOfferAnswerHandler::DoSetRemoteDescription(
2129 std::unique_ptr<SessionDescriptionInterface> desc,
2130 rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) {
2131 RTC_DCHECK_RUN_ON(signaling_thread());
2132 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoSetRemoteDescription");
2133
2134 if (!observer) {
2135 RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL.";
2136 return;
2137 }
2138
2139 if (!desc) {
2140 observer->OnSetRemoteDescriptionComplete(RTCError(
2141 RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL."));
2142 return;
2143 }
2144
2145 // If a session error has occurred the PeerConnection is in a possibly
2146 // inconsistent state so fail right away.
Harald Alvestranda474fbf2020-10-01 16:47:232147 if (session_error() != SessionError::kNone) {
2148 std::string error_message = GetSessionErrorMsg();
Harald Alvestrandcdcfab02020-09-28 13:02:072149 RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message;
2150 observer->OnSetRemoteDescriptionComplete(
2151 RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
2152 return;
2153 }
2154 if (IsUnifiedPlan()) {
2155 if (pc_->configuration()->enable_implicit_rollback) {
2156 if (desc->GetType() == SdpType::kOffer &&
2157 signaling_state() == PeerConnectionInterface::kHaveLocalOffer) {
2158 Rollback(desc->GetType());
2159 }
2160 }
2161 // Explicit rollback.
2162 if (desc->GetType() == SdpType::kRollback) {
2163 observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType()));
2164 return;
2165 }
2166 } else if (desc->GetType() == SdpType::kRollback) {
2167 observer->OnSetRemoteDescriptionComplete(
2168 RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2169 "Rollback not supported in Plan B"));
2170 return;
2171 }
Philipp Hanckeb8ca2a12020-10-07 10:47:102172 if (desc->GetType() == SdpType::kOffer ||
2173 desc->GetType() == SdpType::kAnswer) {
2174 // Report to UMA the format of the received offer or answer.
Harald Alvestrandcdcfab02020-09-28 13:02:072175 pc_->ReportSdpFormatReceived(*desc);
Philipp Hancke54b925c2021-01-28 08:56:392176 pc_->ReportSdpBundleUsage(*desc);
Harald Alvestrandcdcfab02020-09-28 13:02:072177 }
2178
2179 // Handle remote descriptions missing a=mid lines for interop with legacy end
2180 // points.
Harald Alvestrandc06e3742020-10-01 10:23:332181 FillInMissingRemoteMids(desc->description());
Harald Alvestrandcdcfab02020-09-28 13:02:072182
Henrik Boströmf8187e02021-04-26 19:04:262183 std::map<std::string, const cricket::ContentGroup*> bundle_groups_by_mid =
2184 GetBundleGroupsByMid(desc->description());
2185 RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE,
2186 bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:072187 if (!error.ok()) {
2188 std::string error_message = GetSetDescriptionErrorMessage(
2189 cricket::CS_REMOTE, desc->GetType(), error);
2190 RTC_LOG(LS_ERROR) << error_message;
2191 observer->OnSetRemoteDescriptionComplete(
2192 RTCError(error.type(), std::move(error_message)));
2193 return;
2194 }
2195
2196 // Grab the description type before moving ownership to
2197 // ApplyRemoteDescription, which may destroy it before returning.
2198 const SdpType type = desc->GetType();
2199
Henrik Boströmf8187e02021-04-26 19:04:262200 error = ApplyRemoteDescription(std::move(desc), bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:072201 // |desc| may be destroyed at this point.
2202
2203 if (!error.ok()) {
2204 // If ApplyRemoteDescription fails, the PeerConnection could be in an
2205 // inconsistent state, so act conservatively here and set the session error
2206 // so that future calls to SetLocalDescription/SetRemoteDescription fail.
Harald Alvestranda474fbf2020-10-01 16:47:232207 SetSessionError(SessionError::kContent, error.message());
Harald Alvestrandcdcfab02020-09-28 13:02:072208 std::string error_message =
2209 GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error);
2210 RTC_LOG(LS_ERROR) << error_message;
2211 observer->OnSetRemoteDescriptionComplete(
2212 RTCError(error.type(), std::move(error_message)));
2213 return;
2214 }
2215 RTC_DCHECK(remote_description());
2216
2217 if (type == SdpType::kAnswer) {
Harald Alvestranda474fbf2020-10-01 16:47:232218 RemoveStoppedTransceivers();
Harald Alvestrandcdcfab02020-09-28 13:02:072219 // TODO(deadbeef): We already had to hop to the network thread for
2220 // MaybeStartGathering...
2221 pc_->network_thread()->Invoke<void>(
Niels Möller4bab23f2021-01-18 08:24:332222 RTC_FROM_HERE, [this] { port_allocator()->DiscardCandidatePool(); });
Harald Alvestrandcdcfab02020-09-28 13:02:072223 // Make UMA notes about what was agreed to.
Harald Alvestranda474fbf2020-10-01 16:47:232224 ReportNegotiatedSdpSemantics(*remote_description());
Harald Alvestrandcdcfab02020-09-28 13:02:072225 }
2226
2227 observer->OnSetRemoteDescriptionComplete(RTCError::OK());
Harald Alvestrand44d0dff2020-10-09 05:43:532228 pc_->NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED);
Harald Alvestrandcdcfab02020-09-28 13:02:072229
2230 // Check if negotiation is needed. We must do this after informing the
2231 // observer that SetRemoteDescription() has completed to ensure negotiation is
2232 // not needed prior to the promise resolving.
2233 if (IsUnifiedPlan()) {
2234 bool was_negotiation_needed = is_negotiation_needed_;
2235 UpdateNegotiationNeeded();
2236 if (signaling_state() == PeerConnectionInterface::kStable &&
2237 was_negotiation_needed && is_negotiation_needed_) {
2238 // Legacy version.
2239 pc_->Observer()->OnRenegotiationNeeded();
2240 // Spec-compliant version; the event may get invalidated before firing.
2241 GenerateNegotiationNeededEvent();
2242 }
2243 }
2244}
2245
2246void SdpOfferAnswerHandler::SetAssociatedRemoteStreams(
2247 rtc::scoped_refptr<RtpReceiverInternal> receiver,
2248 const std::vector<std::string>& stream_ids,
2249 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams,
2250 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
2251 RTC_DCHECK_RUN_ON(signaling_thread());
2252 std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams;
2253 for (const std::string& stream_id : stream_ids) {
2254 rtc::scoped_refptr<MediaStreamInterface> stream =
Harald Alvestrand6f04b6532020-10-09 11:42:172255 remote_streams_->find(stream_id);
Harald Alvestrandcdcfab02020-09-28 13:02:072256 if (!stream) {
2257 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
2258 MediaStream::Create(stream_id));
Harald Alvestrand6f04b6532020-10-09 11:42:172259 remote_streams_->AddStream(stream);
Harald Alvestrandcdcfab02020-09-28 13:02:072260 added_streams->push_back(stream);
2261 }
2262 media_streams.push_back(stream);
2263 }
2264 // Special case: "a=msid" missing, use random stream ID.
2265 if (media_streams.empty() &&
2266 !(remote_description()->description()->msid_signaling() &
2267 cricket::kMsidSignalingMediaSection)) {
2268 if (!missing_msid_default_stream_) {
2269 missing_msid_default_stream_ = MediaStreamProxy::Create(
2270 rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid()));
2271 added_streams->push_back(missing_msid_default_stream_);
2272 }
2273 media_streams.push_back(missing_msid_default_stream_);
2274 }
2275 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
2276 receiver->streams();
2277 // SetStreams() will add/remove the receiver's track to/from the streams. This
2278 // differs from the spec - the spec uses an "addList" and "removeList" to
2279 // update the stream-track relationships in a later step. We do this earlier,
2280 // changing the order of things, but the end-result is the same.
2281 // TODO(hbos): When we remove remote_streams(), use set_stream_ids()
2282 // instead. https://crbug.com/webrtc/9480
2283 receiver->SetStreams(media_streams);
Harald Alvestranda474fbf2020-10-01 16:47:232284 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
Harald Alvestrandcdcfab02020-09-28 13:02:072285}
2286
2287bool SdpOfferAnswerHandler::AddIceCandidate(
2288 const IceCandidateInterface* ice_candidate) {
Tomas Gunnarsson2efb8a52021-04-01 14:26:572289 const AddIceCandidateResult result = AddIceCandidateInternal(ice_candidate);
2290 NoteAddIceCandidateResult(result);
2291 // If the return value is kAddIceCandidateFailNotReady, the candidate has been
2292 // added, although not 'ready', but that's a success.
2293 return result == kAddIceCandidateSuccess ||
2294 result == kAddIceCandidateFailNotReady;
2295}
2296
2297AddIceCandidateResult SdpOfferAnswerHandler::AddIceCandidateInternal(
2298 const IceCandidateInterface* ice_candidate) {
Harald Alvestrandcdcfab02020-09-28 13:02:072299 RTC_DCHECK_RUN_ON(signaling_thread());
2300 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::AddIceCandidate");
2301 if (pc_->IsClosed()) {
2302 RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed.";
Tomas Gunnarsson2efb8a52021-04-01 14:26:572303 return kAddIceCandidateFailClosed;
Harald Alvestrandcdcfab02020-09-28 13:02:072304 }
2305
2306 if (!remote_description()) {
2307 RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added "
2308 "without any remote session description.";
Tomas Gunnarsson2efb8a52021-04-01 14:26:572309 return kAddIceCandidateFailNoRemoteDescription;
Harald Alvestrandcdcfab02020-09-28 13:02:072310 }
2311
2312 if (!ice_candidate) {
2313 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null.";
Tomas Gunnarsson2efb8a52021-04-01 14:26:572314 return kAddIceCandidateFailNullCandidate;
Harald Alvestrandcdcfab02020-09-28 13:02:072315 }
2316
2317 bool valid = false;
Harald Alvestrandbc9ca252020-10-05 13:08:412318 bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid);
Harald Alvestrandcdcfab02020-09-28 13:02:072319 if (!valid) {
Tomas Gunnarsson2efb8a52021-04-01 14:26:572320 return kAddIceCandidateFailNotValid;
Harald Alvestrandcdcfab02020-09-28 13:02:072321 }
2322
2323 // Add this candidate to the remote session description.
2324 if (!mutable_remote_description()->AddCandidate(ice_candidate)) {
2325 RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used.";
Tomas Gunnarsson2efb8a52021-04-01 14:26:572326 return kAddIceCandidateFailInAddition;
Harald Alvestrandcdcfab02020-09-28 13:02:072327 }
2328
Tomas Gunnarsson2efb8a52021-04-01 14:26:572329 if (!ready) {
Harald Alvestrandcdcfab02020-09-28 13:02:072330 RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate.";
Tomas Gunnarsson2efb8a52021-04-01 14:26:572331 return kAddIceCandidateFailNotReady;
Harald Alvestrandcdcfab02020-09-28 13:02:072332 }
Tomas Gunnarsson2efb8a52021-04-01 14:26:572333
2334 if (!UseCandidate(ice_candidate)) {
2335 return kAddIceCandidateFailNotUsable;
2336 }
2337
2338 pc_->NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED);
2339
2340 return kAddIceCandidateSuccess;
Harald Alvestrandcdcfab02020-09-28 13:02:072341}
2342
2343void SdpOfferAnswerHandler::AddIceCandidate(
2344 std::unique_ptr<IceCandidateInterface> candidate,
2345 std::function<void(RTCError)> callback) {
Markus Handell518669d2021-06-07 11:30:462346 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::AddIceCandidate");
Harald Alvestrandcdcfab02020-09-28 13:02:072347 RTC_DCHECK_RUN_ON(signaling_thread());
2348 // Chain this operation. If asynchronous operations are pending on the chain,
2349 // this operation will be queued to be invoked, otherwise the contents of the
2350 // lambda will execute immediately.
2351 operations_chain_->ChainOperation(
2352 [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(),
2353 candidate = std::move(candidate), callback = std::move(callback)](
2354 std::function<void()> operations_chain_callback) {
Tomas Gunnarsson2efb8a52021-04-01 14:26:572355 auto result =
2356 this_weak_ptr
2357 ? this_weak_ptr->AddIceCandidateInternal(candidate.get())
2358 : kAddIceCandidateFailClosed;
2359 NoteAddIceCandidateResult(result);
2360 operations_chain_callback();
2361 if (result == kAddIceCandidateFailClosed) {
Harald Alvestrandcdcfab02020-09-28 13:02:072362 callback(RTCError(
2363 RTCErrorType::INVALID_STATE,
2364 "AddIceCandidate failed because the session was shut down"));
Tomas Gunnarsson2efb8a52021-04-01 14:26:572365 } else if (result != kAddIceCandidateSuccess &&
2366 result != kAddIceCandidateFailNotReady) {
Harald Alvestrandcdcfab02020-09-28 13:02:072367 // Fail with an error type and message consistent with Chromium.
2368 // TODO(hbos): Fail with error types according to spec.
2369 callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
2370 "Error processing ICE candidate"));
Tomas Gunnarsson2efb8a52021-04-01 14:26:572371 } else {
2372 callback(RTCError::OK());
Harald Alvestrandcdcfab02020-09-28 13:02:072373 }
Harald Alvestrandcdcfab02020-09-28 13:02:072374 });
2375}
2376
2377bool SdpOfferAnswerHandler::RemoveIceCandidates(
2378 const std::vector<cricket::Candidate>& candidates) {
2379 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::RemoveIceCandidates");
2380 RTC_DCHECK_RUN_ON(signaling_thread());
2381 if (pc_->IsClosed()) {
2382 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed.";
2383 return false;
2384 }
2385
2386 if (!remote_description()) {
2387 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed "
2388 "without any remote session description.";
2389 return false;
2390 }
2391
2392 if (candidates.empty()) {
2393 RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty.";
2394 return false;
2395 }
2396
2397 size_t number_removed =
2398 mutable_remote_description()->RemoveCandidates(candidates);
2399 if (number_removed != candidates.size()) {
2400 RTC_LOG(LS_ERROR)
2401 << "RemoveIceCandidates: Failed to remove candidates. Requested "
2402 << candidates.size() << " but only " << number_removed
2403 << " are removed.";
2404 }
2405
2406 // Remove the candidates from the transport controller.
Harald Alvestrandbc9ca252020-10-05 13:08:412407 RTCError error = transport_controller()->RemoveRemoteCandidates(candidates);
Harald Alvestrandcdcfab02020-09-28 13:02:072408 if (!error.ok()) {
2409 RTC_LOG(LS_ERROR)
2410 << "RemoveIceCandidates: Error when removing remote candidates: "
2411 << error.message();
2412 }
2413 return true;
2414}
2415
2416void SdpOfferAnswerHandler::AddLocalIceCandidate(
2417 const JsepIceCandidate* candidate) {
2418 RTC_DCHECK_RUN_ON(signaling_thread());
2419 if (local_description()) {
2420 mutable_local_description()->AddCandidate(candidate);
2421 }
2422}
2423
2424void SdpOfferAnswerHandler::RemoveLocalIceCandidates(
2425 const std::vector<cricket::Candidate>& candidates) {
2426 RTC_DCHECK_RUN_ON(signaling_thread());
2427 if (local_description()) {
2428 mutable_local_description()->RemoveCandidates(candidates);
2429 }
2430}
2431
2432const SessionDescriptionInterface* SdpOfferAnswerHandler::local_description()
2433 const {
2434 RTC_DCHECK_RUN_ON(signaling_thread());
2435 return pending_local_description_ ? pending_local_description_.get()
2436 : current_local_description_.get();
2437}
2438
2439const SessionDescriptionInterface* SdpOfferAnswerHandler::remote_description()
2440 const {
2441 RTC_DCHECK_RUN_ON(signaling_thread());
2442 return pending_remote_description_ ? pending_remote_description_.get()
2443 : current_remote_description_.get();
2444}
2445
2446const SessionDescriptionInterface*
2447SdpOfferAnswerHandler::current_local_description() const {
2448 RTC_DCHECK_RUN_ON(signaling_thread());
2449 return current_local_description_.get();
2450}
2451
2452const SessionDescriptionInterface*
2453SdpOfferAnswerHandler::current_remote_description() const {
2454 RTC_DCHECK_RUN_ON(signaling_thread());
2455 return current_remote_description_.get();
2456}
2457
2458const SessionDescriptionInterface*
2459SdpOfferAnswerHandler::pending_local_description() const {
2460 RTC_DCHECK_RUN_ON(signaling_thread());
2461 return pending_local_description_.get();
2462}
2463
2464const SessionDescriptionInterface*
2465SdpOfferAnswerHandler::pending_remote_description() const {
2466 RTC_DCHECK_RUN_ON(signaling_thread());
2467 return pending_remote_description_.get();
2468}
2469
2470PeerConnectionInterface::SignalingState SdpOfferAnswerHandler::signaling_state()
2471 const {
2472 RTC_DCHECK_RUN_ON(signaling_thread());
2473 return signaling_state_;
2474}
2475
2476void SdpOfferAnswerHandler::ChangeSignalingState(
2477 PeerConnectionInterface::SignalingState signaling_state) {
Markus Handell518669d2021-06-07 11:30:462478 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::ChangeSignalingState");
Harald Alvestrandcdcfab02020-09-28 13:02:072479 RTC_DCHECK_RUN_ON(signaling_thread());
2480 if (signaling_state_ == signaling_state) {
2481 return;
2482 }
2483 RTC_LOG(LS_INFO) << "Session: " << pc_->session_id() << " Old state: "
2484 << GetSignalingStateString(signaling_state_)
2485 << " New state: "
2486 << GetSignalingStateString(signaling_state);
2487 signaling_state_ = signaling_state;
2488 pc_->Observer()->OnSignalingChange(signaling_state_);
2489}
2490
2491RTCError SdpOfferAnswerHandler::UpdateSessionState(
2492 SdpType type,
2493 cricket::ContentSource source,
Henrik Boströmf8187e02021-04-26 19:04:262494 const cricket::SessionDescription* description,
2495 const std::map<std::string, const cricket::ContentGroup*>&
2496 bundle_groups_by_mid) {
Harald Alvestrandcdcfab02020-09-28 13:02:072497 RTC_DCHECK_RUN_ON(signaling_thread());
2498
2499 // If there's already a pending error then no state transition should happen.
2500 // But all call-sites should be verifying this before calling us!
Harald Alvestranda474fbf2020-10-01 16:47:232501 RTC_DCHECK(session_error() == SessionError::kNone);
Harald Alvestrandcdcfab02020-09-28 13:02:072502
Taylor Brandstetterd0acbd82021-01-25 21:44:552503 // If this is answer-ish we're ready to let media flow.
2504 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
2505 EnableSending();
2506 }
2507
Harald Alvestrandcdcfab02020-09-28 13:02:072508 // Update the signaling state according to the specified state machine (see
2509 // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum).
2510 if (type == SdpType::kOffer) {
2511 ChangeSignalingState(source == cricket::CS_LOCAL
2512 ? PeerConnectionInterface::kHaveLocalOffer
2513 : PeerConnectionInterface::kHaveRemoteOffer);
2514 } else if (type == SdpType::kPrAnswer) {
2515 ChangeSignalingState(source == cricket::CS_LOCAL
2516 ? PeerConnectionInterface::kHaveLocalPrAnswer
2517 : PeerConnectionInterface::kHaveRemotePrAnswer);
2518 } else {
Harald Alvestrandcdcfab02020-09-28 13:02:072519 RTC_DCHECK(type == SdpType::kAnswer);
2520 ChangeSignalingState(PeerConnectionInterface::kStable);
Harald Alvestrande15fb152020-10-19 13:28:052521 transceivers()->DiscardStableStates();
Harald Alvestrandcdcfab02020-09-28 13:02:072522 }
2523
2524 // Update internal objects according to the session description's media
2525 // descriptions.
Tommicc7a3682021-05-04 12:59:382526 return PushdownMediaDescription(type, source, bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:072527}
2528
2529bool SdpOfferAnswerHandler::ShouldFireNegotiationNeededEvent(
2530 uint32_t event_id) {
2531 RTC_DCHECK_RUN_ON(signaling_thread());
2532 // Plan B? Always fire to conform with useless legacy behavior.
2533 if (!IsUnifiedPlan()) {
2534 return true;
2535 }
2536 // The event ID has been invalidated. Either negotiation is no longer needed
2537 // or a newer negotiation needed event has been generated.
2538 if (event_id != negotiation_needed_event_id_) {
2539 return false;
2540 }
2541 // The chain is no longer empty, update negotiation needed when it becomes
2542 // empty. This should generate a newer negotiation needed event, making this
2543 // one obsolete.
2544 if (!operations_chain_->IsEmpty()) {
2545 // Since we just suppressed an event that would have been fired, if
2546 // negotiation is still needed by the time the chain becomes empty again, we
2547 // must make sure to generate another event if negotiation is needed then.
2548 // This happens when |is_negotiation_needed_| goes from false to true, so we
2549 // set it to false until UpdateNegotiationNeeded() is called.
2550 is_negotiation_needed_ = false;
2551 update_negotiation_needed_on_empty_chain_ = true;
2552 return false;
2553 }
2554 // We must not fire if the signaling state is no longer "stable". If
2555 // negotiation is still needed when we return to "stable", a new negotiation
2556 // needed event will be generated, so this one can safely be suppressed.
2557 if (signaling_state_ != PeerConnectionInterface::kStable) {
2558 return false;
2559 }
2560 // All checks have passed - please fire "negotiationneeded" now!
2561 return true;
2562}
2563
Harald Alvestrand6f04b6532020-10-09 11:42:172564rtc::scoped_refptr<StreamCollectionInterface>
2565SdpOfferAnswerHandler::local_streams() {
2566 RTC_DCHECK_RUN_ON(signaling_thread());
2567 RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified "
2568 "Plan SdpSemantics. Please use GetSenders "
2569 "instead.";
2570 return local_streams_;
2571}
2572
2573rtc::scoped_refptr<StreamCollectionInterface>
2574SdpOfferAnswerHandler::remote_streams() {
2575 RTC_DCHECK_RUN_ON(signaling_thread());
2576 RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified "
2577 "Plan SdpSemantics. Please use GetReceivers "
2578 "instead.";
2579 return remote_streams_;
2580}
2581
2582bool SdpOfferAnswerHandler::AddStream(MediaStreamInterface* local_stream) {
2583 RTC_DCHECK_RUN_ON(signaling_thread());
2584 RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan "
2585 "SdpSemantics. Please use AddTrack instead.";
2586 if (pc_->IsClosed()) {
2587 return false;
2588 }
2589 if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
2590 return false;
2591 }
2592
2593 local_streams_->AddStream(local_stream);
2594 MediaStreamObserver* observer = new MediaStreamObserver(local_stream);
Harald Alvestrand763f5a92020-10-22 10:39:402595 observer->SignalAudioTrackAdded.connect(
2596 this, &SdpOfferAnswerHandler::OnAudioTrackAdded);
Harald Alvestrand6f04b6532020-10-09 11:42:172597 observer->SignalAudioTrackRemoved.connect(
Harald Alvestrand763f5a92020-10-22 10:39:402598 this, &SdpOfferAnswerHandler::OnAudioTrackRemoved);
2599 observer->SignalVideoTrackAdded.connect(
2600 this, &SdpOfferAnswerHandler::OnVideoTrackAdded);
Harald Alvestrand6f04b6532020-10-09 11:42:172601 observer->SignalVideoTrackRemoved.connect(
Harald Alvestrand763f5a92020-10-22 10:39:402602 this, &SdpOfferAnswerHandler::OnVideoTrackRemoved);
Harald Alvestrand6f04b6532020-10-09 11:42:172603 stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer));
2604
2605 for (const auto& track : local_stream->GetAudioTracks()) {
Harald Alvestrande15fb152020-10-19 13:28:052606 rtp_manager()->AddAudioTrack(track.get(), local_stream);
Harald Alvestrand6f04b6532020-10-09 11:42:172607 }
2608 for (const auto& track : local_stream->GetVideoTracks()) {
Harald Alvestrande15fb152020-10-19 13:28:052609 rtp_manager()->AddVideoTrack(track.get(), local_stream);
Harald Alvestrand6f04b6532020-10-09 11:42:172610 }
2611
2612 pc_->stats()->AddStream(local_stream);
2613 UpdateNegotiationNeeded();
2614 return true;
2615}
2616
2617void SdpOfferAnswerHandler::RemoveStream(MediaStreamInterface* local_stream) {
2618 RTC_DCHECK_RUN_ON(signaling_thread());
2619 RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified "
2620 "Plan SdpSemantics. Please use RemoveTrack "
2621 "instead.";
2622 TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream");
2623 if (!pc_->IsClosed()) {
2624 for (const auto& track : local_stream->GetAudioTracks()) {
Harald Alvestrande15fb152020-10-19 13:28:052625 rtp_manager()->RemoveAudioTrack(track.get(), local_stream);
Harald Alvestrand6f04b6532020-10-09 11:42:172626 }
2627 for (const auto& track : local_stream->GetVideoTracks()) {
Harald Alvestrande15fb152020-10-19 13:28:052628 rtp_manager()->RemoveVideoTrack(track.get(), local_stream);
Harald Alvestrand6f04b6532020-10-09 11:42:172629 }
2630 }
2631 local_streams_->RemoveStream(local_stream);
2632 stream_observers_.erase(
2633 std::remove_if(
2634 stream_observers_.begin(), stream_observers_.end(),
2635 [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) {
2636 return observer->stream()->id().compare(local_stream->id()) == 0;
2637 }),
2638 stream_observers_.end());
2639
2640 if (pc_->IsClosed()) {
2641 return;
2642 }
2643 UpdateNegotiationNeeded();
2644}
2645
Harald Alvestrand763f5a92020-10-22 10:39:402646void SdpOfferAnswerHandler::OnAudioTrackAdded(AudioTrackInterface* track,
2647 MediaStreamInterface* stream) {
2648 if (pc_->IsClosed()) {
2649 return;
2650 }
2651 rtp_manager()->AddAudioTrack(track, stream);
2652 UpdateNegotiationNeeded();
2653}
2654
2655void SdpOfferAnswerHandler::OnAudioTrackRemoved(AudioTrackInterface* track,
2656 MediaStreamInterface* stream) {
2657 if (pc_->IsClosed()) {
2658 return;
2659 }
2660 rtp_manager()->RemoveAudioTrack(track, stream);
2661 UpdateNegotiationNeeded();
2662}
2663
2664void SdpOfferAnswerHandler::OnVideoTrackAdded(VideoTrackInterface* track,
2665 MediaStreamInterface* stream) {
2666 if (pc_->IsClosed()) {
2667 return;
2668 }
2669 rtp_manager()->AddVideoTrack(track, stream);
2670 UpdateNegotiationNeeded();
2671}
2672
2673void SdpOfferAnswerHandler::OnVideoTrackRemoved(VideoTrackInterface* track,
2674 MediaStreamInterface* stream) {
2675 if (pc_->IsClosed()) {
2676 return;
2677 }
2678 rtp_manager()->RemoveVideoTrack(track, stream);
2679 UpdateNegotiationNeeded();
2680}
2681
Harald Alvestrandcdcfab02020-09-28 13:02:072682RTCError SdpOfferAnswerHandler::Rollback(SdpType desc_type) {
Markus Handell518669d2021-06-07 11:30:462683 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::Rollback");
Harald Alvestrandcdcfab02020-09-28 13:02:072684 auto state = signaling_state();
2685 if (state != PeerConnectionInterface::kHaveLocalOffer &&
2686 state != PeerConnectionInterface::kHaveRemoteOffer) {
2687 return RTCError(RTCErrorType::INVALID_STATE,
2688 "Called in wrong signalingState: " +
2689 GetSignalingStateString(signaling_state()));
2690 }
2691 RTC_DCHECK_RUN_ON(signaling_thread());
2692 RTC_DCHECK(IsUnifiedPlan());
2693 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams;
2694 std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams;
2695 std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers;
2696
Harald Alvestrande15fb152020-10-19 13:28:052697 for (auto&& transceivers_stable_state_pair : transceivers()->StableStates()) {
Harald Alvestrandcdcfab02020-09-28 13:02:072698 auto transceiver = transceivers_stable_state_pair.first;
2699 auto state = transceivers_stable_state_pair.second;
2700
2701 if (state.remote_stream_ids()) {
2702 std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams;
2703 std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams;
2704 SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(),
2705 state.remote_stream_ids().value(),
2706 &added_streams, &removed_streams);
2707 all_added_streams.insert(all_added_streams.end(), added_streams.begin(),
2708 added_streams.end());
2709 all_removed_streams.insert(all_removed_streams.end(),
2710 removed_streams.begin(),
2711 removed_streams.end());
2712 if (!state.has_m_section() && !state.newly_created()) {
2713 continue;
2714 }
2715 }
2716
2717 RTC_DCHECK(transceiver->internal()->mid().has_value());
Harald Alvestrandbc9ca252020-10-05 13:08:412718 DestroyTransceiverChannel(transceiver);
Harald Alvestrandcdcfab02020-09-28 13:02:072719
2720 if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer &&
2721 transceiver->receiver()) {
2722 removed_receivers.push_back(transceiver->receiver());
2723 }
2724 if (state.newly_created()) {
2725 if (transceiver->internal()->reused_for_addtrack()) {
2726 transceiver->internal()->set_created_by_addtrack(true);
2727 } else {
Harald Alvestrande15fb152020-10-19 13:28:052728 transceivers()->Remove(transceiver);
Harald Alvestrandcdcfab02020-09-28 13:02:072729 }
2730 }
Eldar Rello950d6b92021-04-06 19:38:002731 if (state.init_send_encodings()) {
2732 transceiver->internal()->sender_internal()->set_init_send_encodings(
2733 state.init_send_encodings().value());
2734 }
Harald Alvestrandcdcfab02020-09-28 13:02:072735 transceiver->internal()->sender_internal()->set_transport(nullptr);
2736 transceiver->internal()->receiver_internal()->set_transport(nullptr);
2737 transceiver->internal()->set_mid(state.mid());
2738 transceiver->internal()->set_mline_index(state.mline_index());
2739 }
Taylor Brandstetterd2b885f2021-07-23 00:41:552740 RTCError e = transport_controller()->RollbackTransports();
2741 if (!e.ok()) {
2742 return e;
2743 }
Harald Alvestrande15fb152020-10-19 13:28:052744 transceivers()->DiscardStableStates();
Harald Alvestrandcdcfab02020-09-28 13:02:072745 pending_local_description_.reset();
2746 pending_remote_description_.reset();
2747 ChangeSignalingState(PeerConnectionInterface::kStable);
2748
2749 // Once all processing has finished, fire off callbacks.
2750 for (const auto& receiver : removed_receivers) {
2751 pc_->Observer()->OnRemoveTrack(receiver);
2752 }
2753 for (const auto& stream : all_added_streams) {
2754 pc_->Observer()->OnAddStream(stream);
2755 }
2756 for (const auto& stream : all_removed_streams) {
2757 pc_->Observer()->OnRemoveStream(stream);
2758 }
2759
2760 // The assumption is that in case of implicit rollback UpdateNegotiationNeeded
2761 // gets called in SetRemoteDescription.
2762 if (desc_type == SdpType::kRollback) {
2763 UpdateNegotiationNeeded();
2764 if (is_negotiation_needed_) {
2765 // Legacy version.
2766 pc_->Observer()->OnRenegotiationNeeded();
2767 // Spec-compliant version; the event may get invalidated before firing.
2768 GenerateNegotiationNeededEvent();
2769 }
2770 }
2771 return RTCError::OK();
2772}
2773
2774bool SdpOfferAnswerHandler::IsUnifiedPlan() const {
Harald Alvestrandcdcfab02020-09-28 13:02:072775 return pc_->IsUnifiedPlan();
2776}
2777
2778void SdpOfferAnswerHandler::OnOperationsChainEmpty() {
2779 RTC_DCHECK_RUN_ON(signaling_thread());
2780 if (pc_->IsClosed() || !update_negotiation_needed_on_empty_chain_)
2781 return;
2782 update_negotiation_needed_on_empty_chain_ = false;
2783 // Firing when chain is empty is only supported in Unified Plan to avoid Plan
2784 // B regressions. (In Plan B, onnegotiationneeded is already broken anyway, so
2785 // firing it even more might just be confusing.)
2786 if (IsUnifiedPlan()) {
2787 UpdateNegotiationNeeded();
2788 }
2789}
2790
2791absl::optional<bool> SdpOfferAnswerHandler::is_caller() {
2792 RTC_DCHECK_RUN_ON(signaling_thread());
2793 return is_caller_;
2794}
2795
2796bool SdpOfferAnswerHandler::HasNewIceCredentials() {
2797 RTC_DCHECK_RUN_ON(signaling_thread());
2798 return local_ice_credentials_to_replace_->HasIceCredentials();
2799}
2800
2801bool SdpOfferAnswerHandler::IceRestartPending(
2802 const std::string& content_name) const {
2803 RTC_DCHECK_RUN_ON(signaling_thread());
2804 return pending_ice_restarts_.find(content_name) !=
2805 pending_ice_restarts_.end();
2806}
2807
Harald Alvestrandf01bd6c2020-10-23 13:30:462808bool SdpOfferAnswerHandler::NeedsIceRestart(
2809 const std::string& content_name) const {
Tommic3257d02021-02-10 17:40:082810 return pc_->NeedsIceRestart(content_name);
Harald Alvestrandf01bd6c2020-10-23 13:30:462811}
2812
2813absl::optional<rtc::SSLRole> SdpOfferAnswerHandler::GetDtlsRole(
2814 const std::string& mid) const {
2815 return transport_controller()->GetDtlsRole(mid);
2816}
2817
Harald Alvestrandcdcfab02020-09-28 13:02:072818void SdpOfferAnswerHandler::UpdateNegotiationNeeded() {
2819 RTC_DCHECK_RUN_ON(signaling_thread());
2820 if (!IsUnifiedPlan()) {
2821 pc_->Observer()->OnRenegotiationNeeded();
2822 GenerateNegotiationNeededEvent();
2823 return;
2824 }
2825
2826 // In the spec, a task is queued here to run the following steps - this is
2827 // meant to ensure we do not fire onnegotiationneeded prematurely if multiple
2828 // changes are being made at once. In order to support Chromium's
2829 // implementation where the JavaScript representation of the PeerConnection
2830 // lives on a separate thread though, the queuing of a task is instead
2831 // performed by the PeerConnectionObserver posting from the signaling thread
2832 // to the JavaScript main thread that negotiation is needed. And because the
2833 // Operations Chain lives on the WebRTC signaling thread,
2834 // ShouldFireNegotiationNeededEvent() must be called before firing the event
2835 // to ensure the Operations Chain is still empty and the event has not been
2836 // invalidated.
2837
2838 // If connection's [[IsClosed]] slot is true, abort these steps.
2839 if (pc_->IsClosed())
2840 return;
2841
2842 // If connection's signaling state is not "stable", abort these steps.
2843 if (signaling_state() != PeerConnectionInterface::kStable)
2844 return;
2845
2846 // NOTE
2847 // The negotiation-needed flag will be updated once the state transitions to
2848 // "stable", as part of the steps for setting an RTCSessionDescription.
2849
2850 // If the result of checking if negotiation is needed is false, clear the
2851 // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot
2852 // to false, and abort these steps.
2853 bool is_negotiation_needed = CheckIfNegotiationIsNeeded();
2854 if (!is_negotiation_needed) {
2855 is_negotiation_needed_ = false;
2856 // Invalidate any negotiation needed event that may previosuly have been
2857 // generated.
2858 ++negotiation_needed_event_id_;
2859 return;
2860 }
2861
2862 // If connection's [[NegotiationNeeded]] slot is already true, abort these
2863 // steps.
2864 if (is_negotiation_needed_)
2865 return;
2866
2867 // Set connection's [[NegotiationNeeded]] slot to true.
2868 is_negotiation_needed_ = true;
2869
2870 // Queue a task that runs the following steps:
2871 // If connection's [[IsClosed]] slot is true, abort these steps.
2872 // If connection's [[NegotiationNeeded]] slot is false, abort these steps.
2873 // Fire an event named negotiationneeded at connection.
2874 pc_->Observer()->OnRenegotiationNeeded();
2875 // Fire the spec-compliant version; when ShouldFireNegotiationNeededEvent() is
2876 // used in the task queued by the observer, this event will only fire when the
2877 // chain is empty.
2878 GenerateNegotiationNeededEvent();
2879}
2880
2881bool SdpOfferAnswerHandler::CheckIfNegotiationIsNeeded() {
2882 RTC_DCHECK_RUN_ON(signaling_thread());
2883 // 1. If any implementation-specific negotiation is required, as described at
2884 // the start of this section, return true.
2885
2886 // 2. If connection.[[LocalIceCredentialsToReplace]] is not empty, return
2887 // true.
2888 if (local_ice_credentials_to_replace_->HasIceCredentials()) {
2889 return true;
2890 }
2891
2892 // 3. Let description be connection.[[CurrentLocalDescription]].
2893 const SessionDescriptionInterface* description = current_local_description();
2894 if (!description)
2895 return true;
2896
2897 // 4. If connection has created any RTCDataChannels, and no m= section in
2898 // description has been negotiated yet for data, return true.
Harald Alvestrandbc9ca252020-10-05 13:08:412899 if (data_channel_controller()->HasSctpDataChannels()) {
Harald Alvestrandcdcfab02020-09-28 13:02:072900 if (!cricket::GetFirstDataContent(description->description()->contents()))
2901 return true;
2902 }
2903
2904 // 5. For each transceiver in connection's set of transceivers, perform the
2905 // following checks:
Harald Alvestrand85466662021-04-19 21:21:362906 for (const auto& transceiver : transceivers()->ListInternal()) {
Harald Alvestrandcdcfab02020-09-28 13:02:072907 const ContentInfo* current_local_msection =
Harald Alvestrand85466662021-04-19 21:21:362908 FindTransceiverMSection(transceiver, description);
Harald Alvestrandcdcfab02020-09-28 13:02:072909
Harald Alvestrand85466662021-04-19 21:21:362910 const ContentInfo* current_remote_msection =
2911 FindTransceiverMSection(transceiver, current_remote_description());
Harald Alvestrandcdcfab02020-09-28 13:02:072912
2913 // 5.4 If transceiver is stopped and is associated with an m= section,
2914 // but the associated m= section is not yet rejected in
2915 // connection.[[CurrentLocalDescription]] or
2916 // connection.[[CurrentRemoteDescription]], return true.
2917 if (transceiver->stopped()) {
2918 RTC_DCHECK(transceiver->stopping());
2919 if (current_local_msection && !current_local_msection->rejected &&
2920 ((current_remote_msection && !current_remote_msection->rejected) ||
2921 !current_remote_msection)) {
2922 return true;
2923 }
2924 continue;
2925 }
2926
2927 // 5.1 If transceiver.[[Stopping]] is true and transceiver.[[Stopped]] is
2928 // false, return true.
2929 if (transceiver->stopping() && !transceiver->stopped())
2930 return true;
2931
2932 // 5.2 If transceiver isn't stopped and isn't yet associated with an m=
2933 // section in description, return true.
2934 if (!current_local_msection)
2935 return true;
2936
2937 const MediaContentDescription* current_local_media_description =
2938 current_local_msection->media_description();
2939 // 5.3 If transceiver isn't stopped and is associated with an m= section
2940 // in description then perform the following checks:
2941
2942 // 5.3.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the
2943 // associated m= section in description either doesn't contain a single
2944 // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this
2945 // m= section, or the MSID values themselves, differ from what is in
2946 // transceiver.sender.[[AssociatedMediaStreamIds]], return true.
2947 if (RtpTransceiverDirectionHasSend(transceiver->direction())) {
2948 if (current_local_media_description->streams().size() == 0)
2949 return true;
2950
2951 std::vector<std::string> msection_msids;
2952 for (const auto& stream : current_local_media_description->streams()) {
2953 for (const std::string& msid : stream.stream_ids())
2954 msection_msids.push_back(msid);
2955 }
2956
2957 std::vector<std::string> transceiver_msids =
2958 transceiver->sender()->stream_ids();
2959 if (msection_msids.size() != transceiver_msids.size())
2960 return true;
2961
2962 absl::c_sort(transceiver_msids);
2963 absl::c_sort(msection_msids);
2964 if (transceiver_msids != msection_msids)
2965 return true;
2966 }
2967
2968 // 5.3.2 If description is of type "offer", and the direction of the
2969 // associated m= section in neither connection.[[CurrentLocalDescription]]
2970 // nor connection.[[CurrentRemoteDescription]] matches
2971 // transceiver.[[Direction]], return true.
2972 if (description->GetType() == SdpType::kOffer) {
2973 if (!current_remote_description())
2974 return true;
2975
2976 if (!current_remote_msection)
2977 return true;
2978
2979 RtpTransceiverDirection current_local_direction =
2980 current_local_media_description->direction();
2981 RtpTransceiverDirection current_remote_direction =
2982 current_remote_msection->media_description()->direction();
2983 if (transceiver->direction() != current_local_direction &&
2984 transceiver->direction() !=
2985 RtpTransceiverDirectionReversed(current_remote_direction)) {
2986 return true;
2987 }
2988 }
2989
2990 // 5.3.3 If description is of type "answer", and the direction of the
2991 // associated m= section in the description does not match
2992 // transceiver.[[Direction]] intersected with the offered direction (as
2993 // described in [JSEP] (section 5.3.1.)), return true.
2994 if (description->GetType() == SdpType::kAnswer) {
2995 if (!remote_description())
2996 return true;
2997
2998 const ContentInfo* offered_remote_msection =
Harald Alvestrand85466662021-04-19 21:21:362999 FindTransceiverMSection(transceiver, remote_description());
Harald Alvestrandcdcfab02020-09-28 13:02:073000
3001 RtpTransceiverDirection offered_direction =
3002 offered_remote_msection
3003 ? offered_remote_msection->media_description()->direction()
3004 : RtpTransceiverDirection::kInactive;
3005
3006 if (current_local_media_description->direction() !=
3007 (RtpTransceiverDirectionIntersection(
3008 transceiver->direction(),
3009 RtpTransceiverDirectionReversed(offered_direction)))) {
3010 return true;
3011 }
3012 }
3013 }
3014
3015 // If all the preceding checks were performed and true was not returned,
3016 // nothing remains to be negotiated; return false.
3017 return false;
3018}
3019
3020void SdpOfferAnswerHandler::GenerateNegotiationNeededEvent() {
3021 RTC_DCHECK_RUN_ON(signaling_thread());
3022 ++negotiation_needed_event_id_;
3023 pc_->Observer()->OnNegotiationNeededEvent(negotiation_needed_event_id_);
3024}
3025
3026RTCError SdpOfferAnswerHandler::ValidateSessionDescription(
3027 const SessionDescriptionInterface* sdesc,
Henrik Boströmf8187e02021-04-26 19:04:263028 cricket::ContentSource source,
3029 const std::map<std::string, const cricket::ContentGroup*>&
3030 bundle_groups_by_mid) {
Harald Alvestranda474fbf2020-10-01 16:47:233031 if (session_error() != SessionError::kNone) {
3032 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg());
Harald Alvestrandcdcfab02020-09-28 13:02:073033 }
3034
3035 if (!sdesc || !sdesc->description()) {
3036 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp);
3037 }
3038
3039 SdpType type = sdesc->GetType();
Harald Alvestrandc06e3742020-10-01 10:23:333040 if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) ||
3041 (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) {
Harald Alvestrandcdcfab02020-09-28 13:02:073042 LOG_AND_RETURN_ERROR(
3043 RTCErrorType::INVALID_STATE,
3044 "Called in wrong state: " + GetSignalingStateString(signaling_state()));
3045 }
3046
3047 RTCError error = ValidateMids(*sdesc->description());
3048 if (!error.ok()) {
3049 return error;
3050 }
3051
3052 // Verify crypto settings.
3053 std::string crypto_error;
3054 if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED ||
3055 pc_->dtls_enabled()) {
Henrik Boströmf8187e02021-04-26 19:04:263056 RTCError crypto_error = VerifyCrypto(
3057 sdesc->description(), pc_->dtls_enabled(), bundle_groups_by_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:073058 if (!crypto_error.ok()) {
3059 return crypto_error;
3060 }
3061 }
3062
3063 // Verify ice-ufrag and ice-pwd.
Henrik Boströmf8187e02021-04-26 19:04:263064 if (!VerifyIceUfragPwdPresent(sdesc->description(), bundle_groups_by_mid)) {
Harald Alvestrandcdcfab02020-09-28 13:02:073065 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3066 kSdpWithoutIceUfragPwd);
3067 }
3068
Henrik Boströmf8187e02021-04-26 19:04:263069 if (!pc_->ValidateBundleSettings(sdesc->description(),
3070 bundle_groups_by_mid)) {
Harald Alvestrandcdcfab02020-09-28 13:02:073071 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3072 kBundleWithoutRtcpMux);
3073 }
3074
3075 // TODO(skvlad): When the local rtcp-mux policy is Require, reject any
3076 // m-lines that do not rtcp-mux enabled.
3077
3078 // Verify m-lines in Answer when compared against Offer.
3079 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
3080 // With an answer we want to compare the new answer session description with
3081 // the offer's session description from the current negotiation.
3082 const cricket::SessionDescription* offer_desc =
3083 (source == cricket::CS_LOCAL) ? remote_description()->description()
3084 : local_description()->description();
3085 if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) ||
3086 !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(),
3087 type)) {
3088 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3089 kMlineMismatchInAnswer);
3090 }
3091 } else {
3092 // The re-offers should respect the order of m= sections in current
3093 // description. See RFC3264 Section 8 paragraph 4 for more details.
3094 // With a re-offer, either the current local or current remote descriptions
3095 // could be the most up to date, so we would like to check against both of
3096 // them if they exist. It could be the case that one of them has a 0 port
3097 // for a media section, but the other does not. This is important to check
3098 // against in the case that we are recycling an m= section.
3099 const cricket::SessionDescription* current_desc = nullptr;
3100 const cricket::SessionDescription* secondary_current_desc = nullptr;
3101 if (local_description()) {
3102 current_desc = local_description()->description();
3103 if (remote_description()) {
3104 secondary_current_desc = remote_description()->description();
3105 }
3106 } else if (remote_description()) {
3107 current_desc = remote_description()->description();
3108 }
3109 if (current_desc &&
3110 !MediaSectionsInSameOrder(*current_desc, secondary_current_desc,
3111 *sdesc->description(), type)) {
3112 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3113 kMlineMismatchInSubsequentOffer);
3114 }
3115 }
3116
3117 if (IsUnifiedPlan()) {
3118 // Ensure that each audio and video media section has at most one
3119 // "StreamParams". This will return an error if receiving a session
3120 // description from a "Plan B" endpoint which adds multiple tracks of the
3121 // same type. With Unified Plan, there can only be at most one track per
3122 // media section.
3123 for (const ContentInfo& content : sdesc->description()->contents()) {
3124 const MediaContentDescription& desc = *content.media_description();
3125 if ((desc.type() == cricket::MEDIA_TYPE_AUDIO ||
3126 desc.type() == cricket::MEDIA_TYPE_VIDEO) &&
3127 desc.streams().size() > 1u) {
3128 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3129 "Media section has more than one track specified "
3130 "with a=ssrc lines which is not supported with "
3131 "Unified Plan.");
3132 }
3133 }
3134 }
3135
3136 return RTCError::OK();
3137}
3138
3139RTCError SdpOfferAnswerHandler::UpdateTransceiversAndDataChannels(
3140 cricket::ContentSource source,
3141 const SessionDescriptionInterface& new_session,
3142 const SessionDescriptionInterface* old_local_description,
Henrik Boströmf8187e02021-04-26 19:04:263143 const SessionDescriptionInterface* old_remote_description,
3144 const std::map<std::string, const cricket::ContentGroup*>&
3145 bundle_groups_by_mid) {
Markus Handell518669d2021-06-07 11:30:463146 TRACE_EVENT0("webrtc",
3147 "SdpOfferAnswerHandler::UpdateTransceiversAndDataChannels");
Harald Alvestrandcdcfab02020-09-28 13:02:073148 RTC_DCHECK_RUN_ON(signaling_thread());
3149 RTC_DCHECK(IsUnifiedPlan());
3150
Harald Alvestrandcdcfab02020-09-28 13:02:073151 if (new_session.GetType() == SdpType::kOffer) {
Henrik Boströmf8187e02021-04-26 19:04:263152 // If the BUNDLE policy is max-bundle, then we know for sure that all
3153 // transports will be bundled from the start. Return an error if max-bundle
3154 // is specified but the session description does not have a BUNDLE group.
3155 if (pc_->configuration()->bundle_policy ==
3156 PeerConnectionInterface::kBundlePolicyMaxBundle &&
3157 bundle_groups_by_mid.empty()) {
3158 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
3159 "max-bundle configured but session description "
3160 "has no BUNDLE group");
Harald Alvestrandcdcfab02020-09-28 13:02:073161 }
Harald Alvestrandcdcfab02020-09-28 13:02:073162 }
3163
3164 const ContentInfos& new_contents = new_session.description()->contents();
3165 for (size_t i = 0; i < new_contents.size(); ++i) {
3166 const cricket::ContentInfo& new_content = new_contents[i];
3167 cricket::MediaType media_type = new_content.media_description()->type();
Harald Alvestrandbc9ca252020-10-05 13:08:413168 mid_generator_.AddKnownId(new_content.name);
Henrik Boströmf8187e02021-04-26 19:04:263169 auto it = bundle_groups_by_mid.find(new_content.name);
3170 const cricket::ContentGroup* bundle_group =
3171 it != bundle_groups_by_mid.end() ? it->second : nullptr;
Harald Alvestrandcdcfab02020-09-28 13:02:073172 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3173 media_type == cricket::MEDIA_TYPE_VIDEO) {
3174 const cricket::ContentInfo* old_local_content = nullptr;
3175 if (old_local_description &&
3176 i < old_local_description->description()->contents().size()) {
3177 old_local_content =
3178 &old_local_description->description()->contents()[i];
3179 }
3180 const cricket::ContentInfo* old_remote_content = nullptr;
3181 if (old_remote_description &&
3182 i < old_remote_description->description()->contents().size()) {
3183 old_remote_content =
3184 &old_remote_description->description()->contents()[i];
3185 }
Harald Alvestrandcdcfab02020-09-28 13:02:073186 auto transceiver_or_error =
3187 AssociateTransceiver(source, new_session.GetType(), i, new_content,
3188 old_local_content, old_remote_content);
3189 if (!transceiver_or_error.ok()) {
Harald Alvestrand09bd9ba2020-10-09 08:13:303190 // In the case where a transceiver is rejected locally, we don't
3191 // expect to find a transceiver, but might find it in the case
3192 // where state is still "stopping", not "stopped".
3193 if (new_content.rejected) {
3194 continue;
3195 }
Harald Alvestrandcdcfab02020-09-28 13:02:073196 return transceiver_or_error.MoveError();
3197 }
3198 auto transceiver = transceiver_or_error.MoveValue();
3199 RTCError error =
3200 UpdateTransceiverChannel(transceiver, new_content, bundle_group);
3201 if (!error.ok()) {
3202 return error;
3203 }
3204 } else if (media_type == cricket::MEDIA_TYPE_DATA) {
3205 if (pc_->GetDataMid() && new_content.name != *(pc_->GetDataMid())) {
3206 // Ignore all but the first data section.
3207 RTC_LOG(LS_INFO) << "Ignoring data media section with MID="
3208 << new_content.name;
3209 continue;
3210 }
3211 RTCError error = UpdateDataChannel(source, new_content, bundle_group);
3212 if (!error.ok()) {
3213 return error;
3214 }
Philipp Hancke4e8c1152020-10-13 10:43:153215 } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) {
3216 RTC_LOG(LS_INFO) << "Ignoring unsupported media type";
Harald Alvestrandcdcfab02020-09-28 13:02:073217 } else {
3218 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3219 "Unknown section type.");
3220 }
3221 }
3222
3223 return RTCError::OK();
3224}
3225
3226RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
3227SdpOfferAnswerHandler::AssociateTransceiver(
3228 cricket::ContentSource source,
3229 SdpType type,
3230 size_t mline_index,
3231 const ContentInfo& content,
3232 const ContentInfo* old_local_content,
3233 const ContentInfo* old_remote_content) {
Markus Handell518669d2021-06-07 11:30:463234 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::AssociateTransceiver");
Harald Alvestrandcdcfab02020-09-28 13:02:073235 RTC_DCHECK(IsUnifiedPlan());
Tomas Gunnarsson36992362020-10-05 19:41:363236#if RTC_DCHECK_IS_ON
Harald Alvestrandcdcfab02020-09-28 13:02:073237 // If this is an offer then the m= section might be recycled. If the m=
3238 // section is being recycled (defined as: rejected in the current local or
3239 // remote description and not rejected in new description), the transceiver
Harald Alvestrande15fb152020-10-19 13:28:053240 // should have been removed by RemoveStoppedtransceivers()->
Harald Alvestrandcdcfab02020-09-28 13:02:073241 if (IsMediaSectionBeingRecycled(type, content, old_local_content,
3242 old_remote_content)) {
3243 const std::string& old_mid =
3244 (old_local_content && old_local_content->rejected)
3245 ? old_local_content->name
3246 : old_remote_content->name;
Harald Alvestrande15fb152020-10-19 13:28:053247 auto old_transceiver = transceivers()->FindByMid(old_mid);
Harald Alvestrandcdcfab02020-09-28 13:02:073248 // The transceiver should be disassociated in RemoveStoppedTransceivers()
3249 RTC_DCHECK(!old_transceiver);
3250 }
Tomas Gunnarsson36992362020-10-05 19:41:363251#endif
3252
Harald Alvestrandcdcfab02020-09-28 13:02:073253 const MediaContentDescription* media_desc = content.media_description();
Harald Alvestrande15fb152020-10-19 13:28:053254 auto transceiver = transceivers()->FindByMid(content.name);
Harald Alvestrandcdcfab02020-09-28 13:02:073255 if (source == cricket::CS_LOCAL) {
3256 // Find the RtpTransceiver that corresponds to this m= section, using the
3257 // mapping between transceivers and m= section indices established when
3258 // creating the offer.
3259 if (!transceiver) {
Harald Alvestrande15fb152020-10-19 13:28:053260 transceiver = transceivers()->FindByMLineIndex(mline_index);
Harald Alvestrandcdcfab02020-09-28 13:02:073261 }
3262 if (!transceiver) {
Harald Alvestrand09bd9ba2020-10-09 08:13:303263 // This may happen normally when media sections are rejected.
Harald Alvestrandcdcfab02020-09-28 13:02:073264 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER,
Harald Alvestrand09bd9ba2020-10-09 08:13:303265 "Transceiver not found based on m-line index");
Harald Alvestrandcdcfab02020-09-28 13:02:073266 }
3267 } else {
3268 RTC_DCHECK_EQ(source, cricket::CS_REMOTE);
3269 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers
3270 // of the same type...
3271 // When simulcast is requested, a transceiver cannot be associated because
3272 // AddTrack cannot be called to initialize it.
3273 if (!transceiver &&
3274 RtpTransceiverDirectionHasRecv(media_desc->direction()) &&
3275 !media_desc->HasSimulcast()) {
Harald Alvestrandc06e3742020-10-01 10:23:333276 transceiver = FindAvailableTransceiverToReceive(media_desc->type());
Harald Alvestrandcdcfab02020-09-28 13:02:073277 }
3278 // If no RtpTransceiver was found in the previous step, create one with a
3279 // recvonly direction.
3280 if (!transceiver) {
3281 RTC_LOG(LS_INFO) << "Adding "
3282 << cricket::MediaTypeToString(media_desc->type())
3283 << " transceiver for MID=" << content.name
3284 << " at i=" << mline_index
3285 << " in response to the remote description.";
3286 std::string sender_id = rtc::CreateRandomUuid();
3287 std::vector<RtpEncodingParameters> send_encodings =
3288 GetSendEncodingsFromRemoteDescription(*media_desc);
Harald Alvestrande15fb152020-10-19 13:28:053289 auto sender = rtp_manager()->CreateSender(media_desc->type(), sender_id,
3290 nullptr, {}, send_encodings);
Harald Alvestrandcdcfab02020-09-28 13:02:073291 std::string receiver_id;
3292 if (!media_desc->streams().empty()) {
3293 receiver_id = media_desc->streams()[0].id;
3294 } else {
3295 receiver_id = rtc::CreateRandomUuid();
3296 }
Harald Alvestrande15fb152020-10-19 13:28:053297 auto receiver =
3298 rtp_manager()->CreateReceiver(media_desc->type(), receiver_id);
3299 transceiver = rtp_manager()->CreateAndAddTransceiver(sender, receiver);
Harald Alvestrandcdcfab02020-09-28 13:02:073300 transceiver->internal()->set_direction(
3301 RtpTransceiverDirection::kRecvOnly);
3302 if (type == SdpType::kOffer) {
Harald Alvestrande15fb152020-10-19 13:28:053303 transceivers()->StableState(transceiver)->set_newly_created();
Harald Alvestrandcdcfab02020-09-28 13:02:073304 }
3305 }
Tomas Gunnarsson36992362020-10-05 19:41:363306
3307 RTC_DCHECK(transceiver);
3308
Harald Alvestrandcdcfab02020-09-28 13:02:073309 // Check if the offer indicated simulcast but the answer rejected it.
3310 // This can happen when simulcast is not supported on the remote party.
Lennart Grahl0d0ed762021-05-17 14:06:373311 if (SimulcastIsRejected(old_local_content, *media_desc,
3312 pc_->GetCryptoOptions()
3313 .srtp.enable_encrypted_rtp_header_extensions)) {
Harald Alvestrandcdcfab02020-09-28 13:02:073314 RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true);
3315 RTCError error =
3316 DisableSimulcastInSender(transceiver->internal()->sender_internal());
3317 if (!error.ok()) {
3318 RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast.";
3319 return std::move(error);
3320 }
3321 }
3322 }
Tomas Gunnarsson36992362020-10-05 19:41:363323
Harald Alvestrandcdcfab02020-09-28 13:02:073324 if (transceiver->media_type() != media_desc->type()) {
3325 LOG_AND_RETURN_ERROR(
3326 RTCErrorType::INVALID_PARAMETER,
3327 "Transceiver type does not match media description type.");
3328 }
Tomas Gunnarsson36992362020-10-05 19:41:363329
Harald Alvestrandcdcfab02020-09-28 13:02:073330 if (media_desc->HasSimulcast()) {
3331 std::vector<SimulcastLayer> layers =
3332 source == cricket::CS_LOCAL
3333 ? media_desc->simulcast_description().send_layers().GetAllLayers()
3334 : media_desc->simulcast_description()
3335 .receive_layers()
3336 .GetAllLayers();
3337 RTCError error = UpdateSimulcastLayerStatusInSender(
3338 layers, transceiver->internal()->sender_internal());
3339 if (!error.ok()) {
3340 RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers.";
3341 return std::move(error);
3342 }
3343 }
3344 if (type == SdpType::kOffer) {
3345 bool state_changes = transceiver->internal()->mid() != content.name ||
3346 transceiver->internal()->mline_index() != mline_index;
3347 if (state_changes) {
Harald Alvestrandbc9ca252020-10-05 13:08:413348 transceivers()
Harald Alvestrande15fb152020-10-19 13:28:053349 ->StableState(transceiver)
Harald Alvestrand38b768c2020-09-29 11:54:053350 ->SetMSectionIfUnset(transceiver->internal()->mid(),
3351 transceiver->internal()->mline_index());
Harald Alvestrandcdcfab02020-09-28 13:02:073352 }
3353 }
3354 // Associate the found or created RtpTransceiver with the m= section by
3355 // setting the value of the RtpTransceiver's mid property to the MID of the m=
3356 // section, and establish a mapping between the transceiver and the index of
3357 // the m= section.
3358 transceiver->internal()->set_mid(content.name);
3359 transceiver->internal()->set_mline_index(mline_index);
3360 return std::move(transceiver);
3361}
3362
Harald Alvestrandcdcfab02020-09-28 13:02:073363RTCError SdpOfferAnswerHandler::UpdateTransceiverChannel(
3364 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3365 transceiver,
3366 const cricket::ContentInfo& content,
3367 const cricket::ContentGroup* bundle_group) {
Markus Handell518669d2021-06-07 11:30:463368 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::UpdateTransceiverChannel");
Harald Alvestrandcdcfab02020-09-28 13:02:073369 RTC_DCHECK(IsUnifiedPlan());
3370 RTC_DCHECK(transceiver);
3371 cricket::ChannelInterface* channel = transceiver->internal()->channel();
3372 if (content.rejected) {
3373 if (channel) {
3374 transceiver->internal()->SetChannel(nullptr);
Harald Alvestrandbc9ca252020-10-05 13:08:413375 DestroyChannelInterface(channel);
Harald Alvestrandcdcfab02020-09-28 13:02:073376 }
3377 } else {
3378 if (!channel) {
3379 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
Harald Alvestrandbc9ca252020-10-05 13:08:413380 channel = CreateVoiceChannel(content.name);
Harald Alvestrandcdcfab02020-09-28 13:02:073381 } else {
3382 RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type());
Harald Alvestrandbc9ca252020-10-05 13:08:413383 channel = CreateVideoChannel(content.name);
Harald Alvestrandcdcfab02020-09-28 13:02:073384 }
3385 if (!channel) {
3386 LOG_AND_RETURN_ERROR(
3387 RTCErrorType::INTERNAL_ERROR,
3388 "Failed to create channel for mid=" + content.name);
3389 }
3390 transceiver->internal()->SetChannel(channel);
3391 }
3392 }
3393 return RTCError::OK();
3394}
3395
3396RTCError SdpOfferAnswerHandler::UpdateDataChannel(
3397 cricket::ContentSource source,
3398 const cricket::ContentInfo& content,
3399 const cricket::ContentGroup* bundle_group) {
Harald Alvestrandcdcfab02020-09-28 13:02:073400 if (content.rejected) {
Florent Castellidcb9ffc2021-06-29 12:58:233401 RTC_LOG(LS_INFO) << "Rejected data channel transport with mid="
3402 << content.mid();
3403
3404 rtc::StringBuilder sb;
3405 sb << "Rejected data channel transport with mid=" << content.mid();
3406 RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, sb.Release());
3407 error.set_error_detail(RTCErrorDetailType::DATA_CHANNEL_FAILURE);
3408 DestroyDataChannelTransport(error);
Harald Alvestrandcdcfab02020-09-28 13:02:073409 } else {
Harald Alvestrand7af57c62021-04-16 11:12:143410 if (!data_channel_controller()->data_channel_transport()) {
Harald Alvestrandcdcfab02020-09-28 13:02:073411 RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid();
Harald Alvestrandbc9ca252020-10-05 13:08:413412 if (!CreateDataChannel(content.name)) {
Harald Alvestrandcdcfab02020-09-28 13:02:073413 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
3414 "Failed to create data channel.");
3415 }
3416 }
Harald Alvestrandcdcfab02020-09-28 13:02:073417 }
3418 return RTCError::OK();
3419}
3420
Harald Alvestrandc06e3742020-10-01 10:23:333421bool SdpOfferAnswerHandler::ExpectSetLocalDescription(SdpType type) {
3422 PeerConnectionInterface::SignalingState state = signaling_state();
3423 if (type == SdpType::kOffer) {
3424 return (state == PeerConnectionInterface::kStable) ||
3425 (state == PeerConnectionInterface::kHaveLocalOffer);
3426 } else {
3427 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
3428 return (state == PeerConnectionInterface::kHaveRemoteOffer) ||
3429 (state == PeerConnectionInterface::kHaveLocalPrAnswer);
3430 }
3431}
3432
3433bool SdpOfferAnswerHandler::ExpectSetRemoteDescription(SdpType type) {
3434 PeerConnectionInterface::SignalingState state = signaling_state();
3435 if (type == SdpType::kOffer) {
3436 return (state == PeerConnectionInterface::kStable) ||
3437 (state == PeerConnectionInterface::kHaveRemoteOffer);
3438 } else {
3439 RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer);
3440 return (state == PeerConnectionInterface::kHaveLocalOffer) ||
3441 (state == PeerConnectionInterface::kHaveRemotePrAnswer);
3442 }
3443}
3444
3445void SdpOfferAnswerHandler::FillInMissingRemoteMids(
3446 cricket::SessionDescription* new_remote_description) {
3447 RTC_DCHECK_RUN_ON(signaling_thread());
3448 RTC_DCHECK(new_remote_description);
3449 const cricket::ContentInfos no_infos;
3450 const cricket::ContentInfos& local_contents =
3451 (local_description() ? local_description()->description()->contents()
3452 : no_infos);
3453 const cricket::ContentInfos& remote_contents =
3454 (remote_description() ? remote_description()->description()->contents()
3455 : no_infos);
3456 for (size_t i = 0; i < new_remote_description->contents().size(); ++i) {
3457 cricket::ContentInfo& content = new_remote_description->contents()[i];
3458 if (!content.name.empty()) {
3459 continue;
3460 }
3461 std::string new_mid;
3462 absl::string_view source_explanation;
3463 if (IsUnifiedPlan()) {
3464 if (i < local_contents.size()) {
3465 new_mid = local_contents[i].name;
3466 source_explanation = "from the matching local media section";
3467 } else if (i < remote_contents.size()) {
3468 new_mid = remote_contents[i].name;
3469 source_explanation = "from the matching previous remote media section";
3470 } else {
Harald Alvestrandbc9ca252020-10-05 13:08:413471 new_mid = mid_generator_.GenerateString();
Harald Alvestrandc06e3742020-10-01 10:23:333472 source_explanation = "generated just now";
3473 }
3474 } else {
3475 new_mid = std::string(
3476 GetDefaultMidForPlanB(content.media_description()->type()));
3477 source_explanation = "to match pre-existing behavior";
3478 }
3479 RTC_DCHECK(!new_mid.empty());
3480 content.name = new_mid;
3481 new_remote_description->transport_infos()[i].content_name = new_mid;
3482 RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i
3483 << " is missing an a=mid line. Filling in the value '"
3484 << new_mid << "' " << source_explanation << ".";
3485 }
3486}
3487
3488rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
3489SdpOfferAnswerHandler::FindAvailableTransceiverToReceive(
3490 cricket::MediaType media_type) const {
3491 RTC_DCHECK_RUN_ON(signaling_thread());
3492 RTC_DCHECK(IsUnifiedPlan());
3493 // From JSEP section 5.10 (Applying a Remote Description):
3494 // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of
3495 // the same type that were added to the PeerConnection by addTrack and are not
3496 // associated with any m= section and are not stopped, find the first such
3497 // RtpTransceiver.
Harald Alvestrande15fb152020-10-19 13:28:053498 for (auto transceiver : transceivers()->List()) {
Harald Alvestrandc06e3742020-10-01 10:23:333499 if (transceiver->media_type() == media_type &&
3500 transceiver->internal()->created_by_addtrack() && !transceiver->mid() &&
3501 !transceiver->stopped()) {
3502 return transceiver;
3503 }
3504 }
3505 return nullptr;
3506}
3507
3508const cricket::ContentInfo*
3509SdpOfferAnswerHandler::FindMediaSectionForTransceiver(
Harald Alvestrand85466662021-04-19 21:21:363510 const RtpTransceiver* transceiver,
Harald Alvestrandc06e3742020-10-01 10:23:333511 const SessionDescriptionInterface* sdesc) const {
3512 RTC_DCHECK_RUN_ON(signaling_thread());
3513 RTC_DCHECK(transceiver);
3514 RTC_DCHECK(sdesc);
3515 if (IsUnifiedPlan()) {
Harald Alvestrand85466662021-04-19 21:21:363516 if (!transceiver->mid()) {
Harald Alvestrandc06e3742020-10-01 10:23:333517 // This transceiver is not associated with a media section yet.
3518 return nullptr;
3519 }
Harald Alvestrand85466662021-04-19 21:21:363520 return sdesc->description()->GetContentByName(*transceiver->mid());
Harald Alvestrandc06e3742020-10-01 10:23:333521 } else {
3522 // Plan B only allows at most one audio and one video section, so use the
3523 // first media section of that type.
3524 return cricket::GetFirstMediaContent(sdesc->description()->contents(),
3525 transceiver->media_type());
3526 }
3527}
3528
3529void SdpOfferAnswerHandler::GetOptionsForOffer(
3530 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3531 cricket::MediaSessionOptions* session_options) {
3532 RTC_DCHECK_RUN_ON(signaling_thread());
3533 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
3534
3535 if (IsUnifiedPlan()) {
3536 GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options);
3537 } else {
3538 GetOptionsForPlanBOffer(offer_answer_options, session_options);
3539 }
3540
Harald Alvestrandc06e3742020-10-01 10:23:333541 // Apply ICE restart flag and renomination flag.
3542 bool ice_restart = offer_answer_options.ice_restart || HasNewIceCredentials();
3543 for (auto& options : session_options->media_description_options) {
3544 options.transport_options.ice_restart = ice_restart;
3545 options.transport_options.enable_ice_renomination =
3546 pc_->configuration()->enable_ice_renomination;
3547 }
3548
Harald Alvestrandbc9ca252020-10-05 13:08:413549 session_options->rtcp_cname = rtcp_cname_;
Harald Alvestrandc06e3742020-10-01 10:23:333550 session_options->crypto_options = pc_->GetCryptoOptions();
3551 session_options->pooled_ice_credentials =
3552 pc_->network_thread()->Invoke<std::vector<cricket::IceParameters>>(
3553 RTC_FROM_HERE,
Niels Möller4bab23f2021-01-18 08:24:333554 [this] { return port_allocator()->GetPooledIceCredentials(); });
Harald Alvestrandc06e3742020-10-01 10:23:333555 session_options->offer_extmap_allow_mixed =
3556 pc_->configuration()->offer_extmap_allow_mixed;
3557
3558 // Allow fallback for using obsolete SCTP syntax.
3559 // Note that the default in |session_options| is true, while
3560 // the default in |options| is false.
3561 session_options->use_obsolete_sctp_sdp =
3562 offer_answer_options.use_obsolete_sctp_sdp;
3563}
3564
3565void SdpOfferAnswerHandler::GetOptionsForPlanBOffer(
3566 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3567 cricket::MediaSessionOptions* session_options) {
3568 // Figure out transceiver directional preferences.
Harald Alvestrande15fb152020-10-19 13:28:053569 bool send_audio =
3570 !rtp_manager()->GetAudioTransceiver()->internal()->senders().empty();
3571 bool send_video =
3572 !rtp_manager()->GetVideoTransceiver()->internal()->senders().empty();
Harald Alvestrandc06e3742020-10-01 10:23:333573
3574 // By default, generate sendrecv/recvonly m= sections.
3575 bool recv_audio = true;
3576 bool recv_video = true;
3577
3578 // By default, only offer a new m= section if we have media to send with it.
3579 bool offer_new_audio_description = send_audio;
3580 bool offer_new_video_description = send_video;
3581 bool offer_new_data_description =
Harald Alvestrandbc9ca252020-10-05 13:08:413582 data_channel_controller()->HasDataChannels();
Harald Alvestrandc06e3742020-10-01 10:23:333583
3584 // The "offer_to_receive_X" options allow those defaults to be overridden.
3585 if (offer_answer_options.offer_to_receive_audio !=
3586 PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) {
3587 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
3588 offer_new_audio_description =
3589 offer_new_audio_description ||
3590 (offer_answer_options.offer_to_receive_audio > 0);
3591 }
3592 if (offer_answer_options.offer_to_receive_video !=
3593 RTCOfferAnswerOptions::kUndefined) {
3594 recv_video = (offer_answer_options.offer_to_receive_video > 0);
3595 offer_new_video_description =
3596 offer_new_video_description ||
3597 (offer_answer_options.offer_to_receive_video > 0);
3598 }
3599
3600 absl::optional<size_t> audio_index;
3601 absl::optional<size_t> video_index;
3602 absl::optional<size_t> data_index;
3603 // If a current description exists, generate m= sections in the same order,
3604 // using the first audio/video/data section that appears and rejecting
3605 // extraneous ones.
3606 if (local_description()) {
Harald Alvestrandbc9ca252020-10-05 13:08:413607 GenerateMediaDescriptionOptions(
Harald Alvestrandc06e3742020-10-01 10:23:333608 local_description(),
3609 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
3610 RtpTransceiverDirectionFromSendRecv(send_video, recv_video),
3611 &audio_index, &video_index, &data_index, session_options);
3612 }
3613
3614 // Add audio/video/data m= sections to the end if needed.
3615 if (!audio_index && offer_new_audio_description) {
3616 cricket::MediaDescriptionOptions options(
3617 cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO,
3618 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), false);
3619 options.header_extensions =
Harald Alvestrandbc9ca252020-10-05 13:08:413620 channel_manager()->GetSupportedAudioRtpHeaderExtensions();
Harald Alvestrandc06e3742020-10-01 10:23:333621 session_options->media_description_options.push_back(options);
3622 audio_index = session_options->media_description_options.size() - 1;
3623 }
3624 if (!video_index && offer_new_video_description) {
3625 cricket::MediaDescriptionOptions options(
3626 cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO,
3627 RtpTransceiverDirectionFromSendRecv(send_video, recv_video), false);
3628 options.header_extensions =
Harald Alvestrandbc9ca252020-10-05 13:08:413629 channel_manager()->GetSupportedVideoRtpHeaderExtensions();
Harald Alvestrandc06e3742020-10-01 10:23:333630 session_options->media_description_options.push_back(options);
3631 video_index = session_options->media_description_options.size() - 1;
3632 }
3633 if (!data_index && offer_new_data_description) {
3634 session_options->media_description_options.push_back(
Harald Alvestrandbc9ca252020-10-05 13:08:413635 GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA));
Harald Alvestrandc06e3742020-10-01 10:23:333636 data_index = session_options->media_description_options.size() - 1;
3637 }
3638
3639 cricket::MediaDescriptionOptions* audio_media_description_options =
3640 !audio_index ? nullptr
3641 : &session_options->media_description_options[*audio_index];
3642 cricket::MediaDescriptionOptions* video_media_description_options =
3643 !video_index ? nullptr
3644 : &session_options->media_description_options[*video_index];
3645
Harald Alvestrande15fb152020-10-19 13:28:053646 AddPlanBRtpSenderOptions(rtp_manager()->GetSendersInternal(),
Harald Alvestrandc06e3742020-10-01 10:23:333647 audio_media_description_options,
3648 video_media_description_options,
3649 offer_answer_options.num_simulcast_layers);
3650}
3651
3652void SdpOfferAnswerHandler::GetOptionsForUnifiedPlanOffer(
3653 const RTCOfferAnswerOptions& offer_answer_options,
3654 cricket::MediaSessionOptions* session_options) {
3655 // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial
3656 // Offers) and 5.2.2 (Subsequent Offers).
3657 RTC_DCHECK_EQ(session_options->media_description_options.size(), 0);
3658 const ContentInfos no_infos;
3659 const ContentInfos& local_contents =
3660 (local_description() ? local_description()->description()->contents()
3661 : no_infos);
3662 const ContentInfos& remote_contents =
3663 (remote_description() ? remote_description()->description()->contents()
3664 : no_infos);
3665 // The mline indices that can be recycled. New transceivers should reuse these
3666 // slots first.
3667 std::queue<size_t> recycleable_mline_indices;
3668 // First, go through each media section that exists in either the local or
3669 // remote description and generate a media section in this offer for the
3670 // associated transceiver. If a media section can be recycled, generate a
3671 // default, rejected media section here that can be later overwritten.
3672 for (size_t i = 0;
3673 i < std::max(local_contents.size(), remote_contents.size()); ++i) {
3674 // Either |local_content| or |remote_content| is non-null.
3675 const ContentInfo* local_content =
3676 (i < local_contents.size() ? &local_contents[i] : nullptr);
3677 const ContentInfo* current_local_content =
3678 GetContentByIndex(current_local_description(), i);
3679 const ContentInfo* remote_content =
3680 (i < remote_contents.size() ? &remote_contents[i] : nullptr);
3681 const ContentInfo* current_remote_content =
3682 GetContentByIndex(current_remote_description(), i);
3683 bool had_been_rejected =
3684 (current_local_content && current_local_content->rejected) ||
3685 (current_remote_content && current_remote_content->rejected);
3686 const std::string& mid =
3687 (local_content ? local_content->name : remote_content->name);
3688 cricket::MediaType media_type =
3689 (local_content ? local_content->media_description()->type()
3690 : remote_content->media_description()->type());
3691 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3692 media_type == cricket::MEDIA_TYPE_VIDEO) {
3693 // A media section is considered eligible for recycling if it is marked as
3694 // rejected in either the current local or current remote description.
Harald Alvestrande15fb152020-10-19 13:28:053695 auto transceiver = transceivers()->FindByMid(mid);
Harald Alvestrandc06e3742020-10-01 10:23:333696 if (!transceiver) {
3697 // No associated transceiver. The media section has been stopped.
3698 recycleable_mline_indices.push(i);
3699 session_options->media_description_options.push_back(
3700 cricket::MediaDescriptionOptions(media_type, mid,
3701 RtpTransceiverDirection::kInactive,
3702 /*stopped=*/true));
3703 } else {
3704 // NOTE: a stopping transceiver should be treated as a stopped one in
3705 // createOffer as specified in
3706 // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer.
3707 if (had_been_rejected && transceiver->stopping()) {
3708 session_options->media_description_options.push_back(
3709 cricket::MediaDescriptionOptions(
3710 transceiver->media_type(), mid,
3711 RtpTransceiverDirection::kInactive,
3712 /*stopped=*/true));
3713 recycleable_mline_indices.push(i);
3714 } else {
3715 session_options->media_description_options.push_back(
3716 GetMediaDescriptionOptionsForTransceiver(
Harald Alvestrand85466662021-04-19 21:21:363717 transceiver->internal(), mid,
Harald Alvestrandc06e3742020-10-01 10:23:333718 /*is_create_offer=*/true));
3719 // CreateOffer shouldn't really cause any state changes in
3720 // PeerConnection, but we need a way to match new transceivers to new
3721 // media sections in SetLocalDescription and JSEP specifies this is
3722 // done by recording the index of the media section generated for the
3723 // transceiver in the offer.
3724 transceiver->internal()->set_mline_index(i);
3725 }
3726 }
Philipp Hancke4e8c1152020-10-13 10:43:153727 } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) {
3728 RTC_DCHECK(local_content->rejected);
3729 session_options->media_description_options.push_back(
3730 cricket::MediaDescriptionOptions(media_type, mid,
3731 RtpTransceiverDirection::kInactive,
3732 /*stopped=*/true));
Harald Alvestrandc06e3742020-10-01 10:23:333733 } else {
3734 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
3735 if (had_been_rejected) {
3736 session_options->media_description_options.push_back(
Harald Alvestrandbc9ca252020-10-05 13:08:413737 GetMediaDescriptionOptionsForRejectedData(mid));
Harald Alvestrandc06e3742020-10-01 10:23:333738 } else {
3739 RTC_CHECK(pc_->GetDataMid());
3740 if (mid == *(pc_->GetDataMid())) {
3741 session_options->media_description_options.push_back(
Harald Alvestrandbc9ca252020-10-05 13:08:413742 GetMediaDescriptionOptionsForActiveData(mid));
Harald Alvestrandc06e3742020-10-01 10:23:333743 } else {
3744 session_options->media_description_options.push_back(
Harald Alvestrandbc9ca252020-10-05 13:08:413745 GetMediaDescriptionOptionsForRejectedData(mid));
Harald Alvestrandc06e3742020-10-01 10:23:333746 }
3747 }
3748 }
3749 }
3750
3751 // Next, look for transceivers that are newly added (that is, are not stopped
3752 // and not associated). Reuse media sections marked as recyclable first,
3753 // otherwise append to the end of the offer. New media sections should be
3754 // added in the order they were added to the PeerConnection.
Harald Alvestrand85466662021-04-19 21:21:363755 for (const auto& transceiver : transceivers()->ListInternal()) {
Harald Alvestrandc06e3742020-10-01 10:23:333756 if (transceiver->mid() || transceiver->stopping()) {
3757 continue;
3758 }
3759 size_t mline_index;
3760 if (!recycleable_mline_indices.empty()) {
3761 mline_index = recycleable_mline_indices.front();
3762 recycleable_mline_indices.pop();
3763 session_options->media_description_options[mline_index] =
3764 GetMediaDescriptionOptionsForTransceiver(
Harald Alvestrandbc9ca252020-10-05 13:08:413765 transceiver, mid_generator_.GenerateString(),
Harald Alvestrandc06e3742020-10-01 10:23:333766 /*is_create_offer=*/true);
3767 } else {
3768 mline_index = session_options->media_description_options.size();
3769 session_options->media_description_options.push_back(
3770 GetMediaDescriptionOptionsForTransceiver(
Harald Alvestrandbc9ca252020-10-05 13:08:413771 transceiver, mid_generator_.GenerateString(),
Harald Alvestrandc06e3742020-10-01 10:23:333772 /*is_create_offer=*/true));
3773 }
3774 // See comment above for why CreateOffer changes the transceiver's state.
Harald Alvestrand85466662021-04-19 21:21:363775 transceiver->set_mline_index(mline_index);
Harald Alvestrandc06e3742020-10-01 10:23:333776 }
3777 // Lastly, add a m-section if we have local data channels and an m section
3778 // does not already exist.
Harald Alvestrandbc9ca252020-10-05 13:08:413779 if (!pc_->GetDataMid() && data_channel_controller()->HasDataChannels()) {
Harald Alvestrandc06e3742020-10-01 10:23:333780 session_options->media_description_options.push_back(
Harald Alvestrandbc9ca252020-10-05 13:08:413781 GetMediaDescriptionOptionsForActiveData(
3782 mid_generator_.GenerateString()));
Harald Alvestrandc06e3742020-10-01 10:23:333783 }
3784}
3785
3786void SdpOfferAnswerHandler::GetOptionsForAnswer(
3787 const RTCOfferAnswerOptions& offer_answer_options,
3788 cricket::MediaSessionOptions* session_options) {
3789 RTC_DCHECK_RUN_ON(signaling_thread());
3790 ExtractSharedMediaSessionOptions(offer_answer_options, session_options);
3791
3792 if (IsUnifiedPlan()) {
3793 GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options);
3794 } else {
3795 GetOptionsForPlanBAnswer(offer_answer_options, session_options);
3796 }
3797
Harald Alvestrandc06e3742020-10-01 10:23:333798 // Apply ICE renomination flag.
3799 for (auto& options : session_options->media_description_options) {
3800 options.transport_options.enable_ice_renomination =
3801 pc_->configuration()->enable_ice_renomination;
3802 }
3803
Harald Alvestrandbc9ca252020-10-05 13:08:413804 session_options->rtcp_cname = rtcp_cname_;
Harald Alvestrandc06e3742020-10-01 10:23:333805 session_options->crypto_options = pc_->GetCryptoOptions();
3806 session_options->pooled_ice_credentials =
3807 pc_->network_thread()->Invoke<std::vector<cricket::IceParameters>>(
3808 RTC_FROM_HERE,
Niels Möller4bab23f2021-01-18 08:24:333809 [this] { return port_allocator()->GetPooledIceCredentials(); });
Harald Alvestrandc06e3742020-10-01 10:23:333810}
3811
3812void SdpOfferAnswerHandler::GetOptionsForPlanBAnswer(
3813 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3814 cricket::MediaSessionOptions* session_options) {
3815 // Figure out transceiver directional preferences.
Harald Alvestrande15fb152020-10-19 13:28:053816 bool send_audio =
3817 !rtp_manager()->GetAudioTransceiver()->internal()->senders().empty();
3818 bool send_video =
3819 !rtp_manager()->GetVideoTransceiver()->internal()->senders().empty();
Harald Alvestrandc06e3742020-10-01 10:23:333820
3821 // By default, generate sendrecv/recvonly m= sections. The direction is also
3822 // restricted by the direction in the offer.
3823 bool recv_audio = true;
3824 bool recv_video = true;
3825
3826 // The "offer_to_receive_X" options allow those defaults to be overridden.
3827 if (offer_answer_options.offer_to_receive_audio !=
3828 RTCOfferAnswerOptions::kUndefined) {
3829 recv_audio = (offer_answer_options.offer_to_receive_audio > 0);
3830 }
3831 if (offer_answer_options.offer_to_receive_video !=
3832 RTCOfferAnswerOptions::kUndefined) {
3833 recv_video = (offer_answer_options.offer_to_receive_video > 0);
3834 }
3835
3836 absl::optional<size_t> audio_index;
3837 absl::optional<size_t> video_index;
3838 absl::optional<size_t> data_index;
3839
3840 // Generate m= sections that match those in the offer.
3841 // Note that mediasession.cc will handle intersection our preferred
3842 // direction with the offered direction.
Harald Alvestrandbc9ca252020-10-05 13:08:413843 GenerateMediaDescriptionOptions(
Harald Alvestrandc06e3742020-10-01 10:23:333844 remote_description(),
3845 RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio),
3846 RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index,
3847 &video_index, &data_index, session_options);
3848
3849 cricket::MediaDescriptionOptions* audio_media_description_options =
3850 !audio_index ? nullptr
3851 : &session_options->media_description_options[*audio_index];
3852 cricket::MediaDescriptionOptions* video_media_description_options =
3853 !video_index ? nullptr
3854 : &session_options->media_description_options[*video_index];
3855
Harald Alvestrande15fb152020-10-19 13:28:053856 AddPlanBRtpSenderOptions(rtp_manager()->GetSendersInternal(),
Harald Alvestrandc06e3742020-10-01 10:23:333857 audio_media_description_options,
3858 video_media_description_options,
3859 offer_answer_options.num_simulcast_layers);
3860}
3861
3862void SdpOfferAnswerHandler::GetOptionsForUnifiedPlanAnswer(
3863 const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options,
3864 cricket::MediaSessionOptions* session_options) {
3865 // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial
3866 // Answers) and 5.3.2 (Subsequent Answers).
3867 RTC_DCHECK(remote_description());
3868 RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer);
3869 for (const ContentInfo& content :
3870 remote_description()->description()->contents()) {
3871 cricket::MediaType media_type = content.media_description()->type();
3872 if (media_type == cricket::MEDIA_TYPE_AUDIO ||
3873 media_type == cricket::MEDIA_TYPE_VIDEO) {
Harald Alvestrande15fb152020-10-19 13:28:053874 auto transceiver = transceivers()->FindByMid(content.name);
Harald Alvestrandc06e3742020-10-01 10:23:333875 if (transceiver) {
3876 session_options->media_description_options.push_back(
3877 GetMediaDescriptionOptionsForTransceiver(
Harald Alvestrand85466662021-04-19 21:21:363878 transceiver->internal(), content.name,
Harald Alvestrandc06e3742020-10-01 10:23:333879 /*is_create_offer=*/false));
3880 } else {
3881 // This should only happen with rejected transceivers.
3882 RTC_DCHECK(content.rejected);
3883 session_options->media_description_options.push_back(
3884 cricket::MediaDescriptionOptions(media_type, content.name,
3885 RtpTransceiverDirection::kInactive,
3886 /*stopped=*/true));
3887 }
Philipp Hancke4e8c1152020-10-13 10:43:153888 } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) {
3889 RTC_DCHECK(content.rejected);
3890 session_options->media_description_options.push_back(
3891 cricket::MediaDescriptionOptions(media_type, content.name,
3892 RtpTransceiverDirection::kInactive,
3893 /*stopped=*/true));
Harald Alvestrandc06e3742020-10-01 10:23:333894 } else {
3895 RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type);
3896 // Reject all data sections if data channels are disabled.
3897 // Reject a data section if it has already been rejected.
3898 // Reject all data sections except for the first one.
Florent Castelli516e2842021-04-19 13:29:503899 if (content.rejected || content.name != *(pc_->GetDataMid())) {
Harald Alvestrandc06e3742020-10-01 10:23:333900 session_options->media_description_options.push_back(
Harald Alvestrandbc9ca252020-10-05 13:08:413901 GetMediaDescriptionOptionsForRejectedData(content.name));
Harald Alvestrandc06e3742020-10-01 10:23:333902 } else {
3903 session_options->media_description_options.push_back(
Harald Alvestrandbc9ca252020-10-05 13:08:413904 GetMediaDescriptionOptionsForActiveData(content.name));
Harald Alvestrandc06e3742020-10-01 10:23:333905 }
3906 }
3907 }
3908}
3909
Harald Alvestranda474fbf2020-10-01 16:47:233910const char* SdpOfferAnswerHandler::SessionErrorToString(
3911 SessionError error) const {
3912 switch (error) {
3913 case SessionError::kNone:
3914 return "ERROR_NONE";
3915 case SessionError::kContent:
3916 return "ERROR_CONTENT";
3917 case SessionError::kTransport:
3918 return "ERROR_TRANSPORT";
3919 }
3920 RTC_NOTREACHED();
3921 return "";
3922}
3923
3924std::string SdpOfferAnswerHandler::GetSessionErrorMsg() {
3925 RTC_DCHECK_RUN_ON(signaling_thread());
3926 rtc::StringBuilder desc;
3927 desc << kSessionError << SessionErrorToString(session_error()) << ". ";
3928 desc << kSessionErrorDesc << session_error_desc() << ".";
3929 return desc.Release();
3930}
3931
3932void SdpOfferAnswerHandler::SetSessionError(SessionError error,
3933 const std::string& error_desc) {
3934 RTC_DCHECK_RUN_ON(signaling_thread());
3935 if (error != session_error_) {
3936 session_error_ = error;
3937 session_error_desc_ = error_desc;
3938 }
3939}
3940
3941RTCError SdpOfferAnswerHandler::HandleLegacyOfferOptions(
3942 const PeerConnectionInterface::RTCOfferAnswerOptions& options) {
3943 RTC_DCHECK_RUN_ON(signaling_thread());
3944 RTC_DCHECK(IsUnifiedPlan());
3945
3946 if (options.offer_to_receive_audio == 0) {
3947 RemoveRecvDirectionFromReceivingTransceiversOfType(
3948 cricket::MEDIA_TYPE_AUDIO);
3949 } else if (options.offer_to_receive_audio == 1) {
3950 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO);
3951 } else if (options.offer_to_receive_audio > 1) {
3952 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
3953 "offer_to_receive_audio > 1 is not supported.");
3954 }
3955
3956 if (options.offer_to_receive_video == 0) {
3957 RemoveRecvDirectionFromReceivingTransceiversOfType(
3958 cricket::MEDIA_TYPE_VIDEO);
3959 } else if (options.offer_to_receive_video == 1) {
3960 AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO);
3961 } else if (options.offer_to_receive_video > 1) {
3962 LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER,
3963 "offer_to_receive_video > 1 is not supported.");
3964 }
3965
3966 return RTCError::OK();
3967}
3968
3969void SdpOfferAnswerHandler::RemoveRecvDirectionFromReceivingTransceiversOfType(
3970 cricket::MediaType media_type) {
3971 for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) {
3972 RtpTransceiverDirection new_direction =
3973 RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false);
3974 if (new_direction != transceiver->direction()) {
3975 RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type)
3976 << " transceiver (MID="
3977 << transceiver->mid().value_or("<not set>") << ") from "
3978 << RtpTransceiverDirectionToString(
3979 transceiver->direction())
3980 << " to "
3981 << RtpTransceiverDirectionToString(new_direction)
3982 << " since CreateOffer specified offer_to_receive=0";
3983 transceiver->internal()->set_direction(new_direction);
3984 }
3985 }
3986}
3987
3988void SdpOfferAnswerHandler::AddUpToOneReceivingTransceiverOfType(
3989 cricket::MediaType media_type) {
3990 RTC_DCHECK_RUN_ON(signaling_thread());
3991 if (GetReceivingTransceiversOfType(media_type).empty()) {
3992 RTC_LOG(LS_INFO)
3993 << "Adding one recvonly " << cricket::MediaTypeToString(media_type)
3994 << " transceiver since CreateOffer specified offer_to_receive=1";
3995 RtpTransceiverInit init;
3996 init.direction = RtpTransceiverDirection::kRecvOnly;
3997 pc_->AddTransceiver(media_type, nullptr, init,
3998 /*update_negotiation_needed=*/false);
3999 }
4000}
4001
4002std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
4003SdpOfferAnswerHandler::GetReceivingTransceiversOfType(
4004 cricket::MediaType media_type) {
4005 std::vector<
4006 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>>
4007 receiving_transceivers;
Harald Alvestrande15fb152020-10-19 13:28:054008 for (const auto& transceiver : transceivers()->List()) {
Harald Alvestranda474fbf2020-10-01 16:47:234009 if (!transceiver->stopped() && transceiver->media_type() == media_type &&
4010 RtpTransceiverDirectionHasRecv(transceiver->direction())) {
4011 receiving_transceivers.push_back(transceiver);
4012 }
4013 }
4014 return receiving_transceivers;
4015}
4016
4017void SdpOfferAnswerHandler::ProcessRemovalOfRemoteTrack(
4018 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4019 transceiver,
4020 std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list,
4021 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
4022 RTC_DCHECK(transceiver->mid());
4023 RTC_LOG(LS_INFO) << "Processing the removal of a track for MID="
4024 << *transceiver->mid();
4025 std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams =
4026 transceiver->internal()->receiver_internal()->streams();
4027 // This will remove the remote track from the streams.
4028 transceiver->internal()->receiver_internal()->set_stream_ids({});
4029 remove_list->push_back(transceiver);
4030 RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams);
4031}
4032
4033void SdpOfferAnswerHandler::RemoveRemoteStreamsIfEmpty(
4034 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams,
4035 std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) {
4036 RTC_DCHECK_RUN_ON(signaling_thread());
4037 // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of
4038 // streams, see if the stream was removed by checking if this was the last
4039 // receiver with that stream ID.
4040 for (const auto& remote_stream : remote_streams) {
4041 if (remote_stream->GetAudioTracks().empty() &&
4042 remote_stream->GetVideoTracks().empty()) {
Harald Alvestrand6f04b6532020-10-09 11:42:174043 remote_streams_->RemoveStream(remote_stream);
Harald Alvestranda474fbf2020-10-01 16:47:234044 removed_streams->push_back(remote_stream);
4045 }
4046 }
4047}
4048
4049void SdpOfferAnswerHandler::RemoveSenders(cricket::MediaType media_type) {
4050 RTC_DCHECK_RUN_ON(signaling_thread());
4051 UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type);
4052 UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false,
4053 media_type, nullptr);
4054}
4055
4056void SdpOfferAnswerHandler::UpdateLocalSenders(
4057 const std::vector<cricket::StreamParams>& streams,
4058 cricket::MediaType media_type) {
Markus Handell518669d2021-06-07 11:30:464059 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::UpdateLocalSenders");
Harald Alvestranda474fbf2020-10-01 16:47:234060 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrande15fb152020-10-19 13:28:054061 std::vector<RtpSenderInfo>* current_senders =
4062 rtp_manager()->GetLocalSenderInfos(media_type);
Harald Alvestranda474fbf2020-10-01 16:47:234063
4064 // Find removed tracks. I.e., tracks where the track id, stream id or ssrc
4065 // don't match the new StreamParam.
4066 for (auto sender_it = current_senders->begin();
4067 sender_it != current_senders->end();
4068 /* incremented manually */) {
Harald Alvestrande15fb152020-10-19 13:28:054069 const RtpSenderInfo& info = *sender_it;
Harald Alvestranda474fbf2020-10-01 16:47:234070 const cricket::StreamParams* params =
4071 cricket::GetStreamBySsrc(streams, info.first_ssrc);
4072 if (!params || params->id != info.sender_id ||
4073 params->first_stream_id() != info.stream_id) {
Harald Alvestrande15fb152020-10-19 13:28:054074 rtp_manager()->OnLocalSenderRemoved(info, media_type);
Harald Alvestranda474fbf2020-10-01 16:47:234075 sender_it = current_senders->erase(sender_it);
4076 } else {
4077 ++sender_it;
4078 }
4079 }
4080
4081 // Find new and active senders.
4082 for (const cricket::StreamParams& params : streams) {
4083 // The sync_label is the MediaStream label and the |stream.id| is the
4084 // sender id.
4085 const std::string& stream_id = params.first_stream_id();
4086 const std::string& sender_id = params.id;
4087 uint32_t ssrc = params.first_ssrc();
Harald Alvestrande15fb152020-10-19 13:28:054088 const RtpSenderInfo* sender_info =
4089 rtp_manager()->FindSenderInfo(*current_senders, stream_id, sender_id);
Harald Alvestranda474fbf2020-10-01 16:47:234090 if (!sender_info) {
Harald Alvestrande15fb152020-10-19 13:28:054091 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
4092 rtp_manager()->OnLocalSenderAdded(current_senders->back(), media_type);
Harald Alvestranda474fbf2020-10-01 16:47:234093 }
4094 }
4095}
4096
4097void SdpOfferAnswerHandler::UpdateRemoteSendersList(
4098 const cricket::StreamParamsVec& streams,
4099 bool default_sender_needed,
4100 cricket::MediaType media_type,
4101 StreamCollection* new_streams) {
Markus Handell518669d2021-06-07 11:30:464102 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::UpdateRemoteSendersList");
Harald Alvestranda474fbf2020-10-01 16:47:234103 RTC_DCHECK_RUN_ON(signaling_thread());
4104 RTC_DCHECK(!IsUnifiedPlan());
4105
Harald Alvestrande15fb152020-10-19 13:28:054106 std::vector<RtpSenderInfo>* current_senders =
4107 rtp_manager()->GetRemoteSenderInfos(media_type);
Harald Alvestranda474fbf2020-10-01 16:47:234108
4109 // Find removed senders. I.e., senders where the sender id or ssrc don't match
4110 // the new StreamParam.
4111 for (auto sender_it = current_senders->begin();
4112 sender_it != current_senders->end();
4113 /* incremented manually */) {
Harald Alvestrande15fb152020-10-19 13:28:054114 const RtpSenderInfo& info = *sender_it;
Harald Alvestranda474fbf2020-10-01 16:47:234115 const cricket::StreamParams* params =
4116 cricket::GetStreamBySsrc(streams, info.first_ssrc);
4117 std::string params_stream_id;
4118 if (params) {
4119 params_stream_id =
4120 (!params->first_stream_id().empty() ? params->first_stream_id()
4121 : kDefaultStreamId);
4122 }
4123 bool sender_exists = params && params->id == info.sender_id &&
4124 params_stream_id == info.stream_id;
4125 // If this is a default track, and we still need it, don't remove it.
4126 if ((info.stream_id == kDefaultStreamId && default_sender_needed) ||
4127 sender_exists) {
4128 ++sender_it;
4129 } else {
Harald Alvestrande15fb152020-10-19 13:28:054130 rtp_manager()->OnRemoteSenderRemoved(
4131 info, remote_streams_->find(info.stream_id), media_type);
Harald Alvestranda474fbf2020-10-01 16:47:234132 sender_it = current_senders->erase(sender_it);
4133 }
4134 }
4135
4136 // Find new and active senders.
4137 for (const cricket::StreamParams& params : streams) {
4138 if (!params.has_ssrcs()) {
4139 // The remote endpoint has streams, but didn't signal ssrcs. For an active
4140 // sender, this means it is coming from a Unified Plan endpoint,so we just
4141 // create a default.
4142 default_sender_needed = true;
4143 break;
4144 }
4145
4146 // |params.id| is the sender id and the stream id uses the first of
4147 // |params.stream_ids|. The remote description could come from a Unified
4148 // Plan endpoint, with multiple or no stream_ids() signaled. Since this is
4149 // not supported in Plan B, we just take the first here and create the
4150 // default stream ID if none is specified.
4151 const std::string& stream_id =
4152 (!params.first_stream_id().empty() ? params.first_stream_id()
4153 : kDefaultStreamId);
4154 const std::string& sender_id = params.id;
4155 uint32_t ssrc = params.first_ssrc();
4156
4157 rtc::scoped_refptr<MediaStreamInterface> stream =
Harald Alvestrand6f04b6532020-10-09 11:42:174158 remote_streams_->find(stream_id);
Harald Alvestranda474fbf2020-10-01 16:47:234159 if (!stream) {
4160 // This is a new MediaStream. Create a new remote MediaStream.
4161 stream = MediaStreamProxy::Create(rtc::Thread::Current(),
4162 MediaStream::Create(stream_id));
Harald Alvestrand6f04b6532020-10-09 11:42:174163 remote_streams_->AddStream(stream);
Harald Alvestranda474fbf2020-10-01 16:47:234164 new_streams->AddStream(stream);
4165 }
4166
Harald Alvestrande15fb152020-10-19 13:28:054167 const RtpSenderInfo* sender_info =
4168 rtp_manager()->FindSenderInfo(*current_senders, stream_id, sender_id);
Harald Alvestranda474fbf2020-10-01 16:47:234169 if (!sender_info) {
Harald Alvestrande15fb152020-10-19 13:28:054170 current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
4171 rtp_manager()->OnRemoteSenderAdded(current_senders->back(), stream,
4172 media_type);
Harald Alvestranda474fbf2020-10-01 16:47:234173 }
4174 }
4175
4176 // Add default sender if necessary.
4177 if (default_sender_needed) {
4178 rtc::scoped_refptr<MediaStreamInterface> default_stream =
Harald Alvestrand6f04b6532020-10-09 11:42:174179 remote_streams_->find(kDefaultStreamId);
Harald Alvestranda474fbf2020-10-01 16:47:234180 if (!default_stream) {
4181 // Create the new default MediaStream.
4182 default_stream = MediaStreamProxy::Create(
4183 rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId));
Harald Alvestrand6f04b6532020-10-09 11:42:174184 remote_streams_->AddStream(default_stream);
Harald Alvestranda474fbf2020-10-01 16:47:234185 new_streams->AddStream(default_stream);
4186 }
4187 std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO)
4188 ? kDefaultAudioSenderId
4189 : kDefaultVideoSenderId;
Harald Alvestrande15fb152020-10-19 13:28:054190 const RtpSenderInfo* default_sender_info = rtp_manager()->FindSenderInfo(
4191 *current_senders, kDefaultStreamId, default_sender_id);
Harald Alvestranda474fbf2020-10-01 16:47:234192 if (!default_sender_info) {
Harald Alvestrande15fb152020-10-19 13:28:054193 current_senders->push_back(
4194 RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0));
4195 rtp_manager()->OnRemoteSenderAdded(current_senders->back(),
4196 default_stream, media_type);
Harald Alvestranda474fbf2020-10-01 16:47:234197 }
4198 }
4199}
4200
Taylor Brandstetterd0acbd82021-01-25 21:44:554201void SdpOfferAnswerHandler::EnableSending() {
Markus Handell518669d2021-06-07 11:30:464202 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::EnableSending");
Taylor Brandstetterd0acbd82021-01-25 21:44:554203 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrand85466662021-04-19 21:21:364204 for (const auto& transceiver : transceivers()->ListInternal()) {
4205 cricket::ChannelInterface* channel = transceiver->channel();
Tommi1959f8f2021-04-26 08:20:194206 if (channel) {
Taylor Brandstetterd0acbd82021-01-25 21:44:554207 channel->Enable(true);
4208 }
4209 }
Taylor Brandstetterd0acbd82021-01-25 21:44:554210}
4211
Harald Alvestranda474fbf2020-10-01 16:47:234212RTCError SdpOfferAnswerHandler::PushdownMediaDescription(
4213 SdpType type,
Henrik Boströmf8187e02021-04-26 19:04:264214 cricket::ContentSource source,
4215 const std::map<std::string, const cricket::ContentGroup*>&
4216 bundle_groups_by_mid) {
Markus Handell518669d2021-06-07 11:30:464217 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::PushdownMediaDescription");
Harald Alvestranda474fbf2020-10-01 16:47:234218 const SessionDescriptionInterface* sdesc =
4219 (source == cricket::CS_LOCAL ? local_description()
4220 : remote_description());
4221 RTC_DCHECK_RUN_ON(signaling_thread());
4222 RTC_DCHECK(sdesc);
4223
Henrik Boströmf8187e02021-04-26 19:04:264224 if (!UpdatePayloadTypeDemuxingState(source, bundle_groups_by_mid)) {
Taylor Brandstetterd0acbd82021-01-25 21:44:554225 // Note that this is never expected to fail, since RtpDemuxer doesn't return
4226 // an error when changing payload type demux criteria, which is all this
4227 // does.
4228 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4229 "Failed to update payload type demuxing state.");
4230 }
Harald Alvestranda474fbf2020-10-01 16:47:234231
Taylor Brandstetterd0acbd82021-01-25 21:44:554232 // Push down the new SDP media section for each audio/video transceiver.
Tommicc7a3682021-05-04 12:59:384233 auto rtp_transceivers = transceivers()->ListInternal();
4234 std::vector<
4235 std::pair<cricket::ChannelInterface*, const MediaContentDescription*>>
4236 channels;
4237 for (const auto& transceiver : rtp_transceivers) {
Harald Alvestranda474fbf2020-10-01 16:47:234238 const ContentInfo* content_info =
4239 FindMediaSectionForTransceiver(transceiver, sdesc);
Harald Alvestrand85466662021-04-19 21:21:364240 cricket::ChannelInterface* channel = transceiver->channel();
Harald Alvestranda474fbf2020-10-01 16:47:234241 if (!channel || !content_info || content_info->rejected) {
4242 continue;
4243 }
4244 const MediaContentDescription* content_desc =
4245 content_info->media_description();
Taylor Brandstetterd0acbd82021-01-25 21:44:554246 if (!content_desc) {
4247 continue;
4248 }
Tommicc7a3682021-05-04 12:59:384249
4250 transceiver->OnNegotiationUpdate(type, content_desc);
4251 channels.push_back(std::make_pair(channel, content_desc));
4252 }
4253
Markus Handellbd933ee2021-06-02 14:17:354254 // This for-loop of invokes helps audio impairment during re-negotiations.
4255 // One of the causes is that downstairs decoder creation is synchronous at the
4256 // moment, and that a decoder is created for each codec listed in the SDP.
4257 //
4258 // TODO(bugs.webrtc.org/12840): consider merging the invokes again after
4259 // these projects have shipped:
4260 // - bugs.webrtc.org/12462
4261 // - crbug.com/1157227
4262 // - crbug.com/1187289
4263 for (const auto& entry : channels) {
Tommicc7a3682021-05-04 12:59:384264 RTCError error =
4265 pc_->worker_thread()->Invoke<RTCError>(RTC_FROM_HERE, [&]() {
4266 std::string error;
Markus Handellbd933ee2021-06-02 14:17:354267 bool success =
4268 (source == cricket::CS_LOCAL)
4269 ? entry.first->SetLocalContent(entry.second, type, &error)
4270 : entry.first->SetRemoteContent(entry.second, type, &error);
4271 if (!success) {
4272 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error);
Tommicc7a3682021-05-04 12:59:384273 }
4274 return RTCError::OK();
4275 });
4276 if (!error.ok()) {
4277 return error;
Harald Alvestranda474fbf2020-10-01 16:47:234278 }
4279 }
4280
Harald Alvestranda474fbf2020-10-01 16:47:234281 // Need complete offer/answer with an SCTP m= section before starting SCTP,
4282 // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19
4283 if (pc_->sctp_mid() && local_description() && remote_description()) {
Harald Alvestranda474fbf2020-10-01 16:47:234284 auto local_sctp_description = cricket::GetFirstSctpDataContentDescription(
4285 local_description()->description());
4286 auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription(
4287 remote_description()->description());
Tomas Gunnarsson92eebef2021-02-10 12:05:444288 if (local_sctp_description && remote_sctp_description) {
Harald Alvestranda474fbf2020-10-01 16:47:234289 int max_message_size;
4290 // A remote max message size of zero means "any size supported".
4291 // We configure the connection with our own max message size.
4292 if (remote_sctp_description->max_message_size() == 0) {
4293 max_message_size = local_sctp_description->max_message_size();
4294 } else {
4295 max_message_size =
4296 std::min(local_sctp_description->max_message_size(),
4297 remote_sctp_description->max_message_size());
4298 }
Tomas Gunnarsson92eebef2021-02-10 12:05:444299 pc_->StartSctpTransport(local_sctp_description->port(),
4300 remote_sctp_description->port(),
4301 max_message_size);
Harald Alvestranda474fbf2020-10-01 16:47:234302 }
4303 }
4304
4305 return RTCError::OK();
4306}
4307
4308RTCError SdpOfferAnswerHandler::PushdownTransportDescription(
4309 cricket::ContentSource source,
4310 SdpType type) {
Markus Handell518669d2021-06-07 11:30:464311 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::PushdownTransportDescription");
Harald Alvestranda474fbf2020-10-01 16:47:234312 RTC_DCHECK_RUN_ON(signaling_thread());
4313
4314 if (source == cricket::CS_LOCAL) {
4315 const SessionDescriptionInterface* sdesc = local_description();
4316 RTC_DCHECK(sdesc);
Harald Alvestrandbc9ca252020-10-05 13:08:414317 return transport_controller()->SetLocalDescription(type,
4318 sdesc->description());
Harald Alvestranda474fbf2020-10-01 16:47:234319 } else {
4320 const SessionDescriptionInterface* sdesc = remote_description();
4321 RTC_DCHECK(sdesc);
Harald Alvestrandbc9ca252020-10-05 13:08:414322 return transport_controller()->SetRemoteDescription(type,
4323 sdesc->description());
Harald Alvestranda474fbf2020-10-01 16:47:234324 }
4325}
4326
4327void SdpOfferAnswerHandler::RemoveStoppedTransceivers() {
Markus Handell518669d2021-06-07 11:30:464328 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::RemoveStoppedTransceivers");
Harald Alvestranda474fbf2020-10-01 16:47:234329 RTC_DCHECK_RUN_ON(signaling_thread());
4330 // 3.2.10.1: For each transceiver in the connection's set of transceivers
4331 // run the following steps:
4332 if (!IsUnifiedPlan())
4333 return;
4334 // Traverse a copy of the transceiver list.
Harald Alvestrande15fb152020-10-19 13:28:054335 auto transceiver_list = transceivers()->List();
Harald Alvestranda474fbf2020-10-01 16:47:234336 for (auto transceiver : transceiver_list) {
4337 // 3.2.10.1.1: If transceiver is stopped, associated with an m= section
4338 // and the associated m= section is rejected in
4339 // connection.[[CurrentLocalDescription]] or
4340 // connection.[[CurrentRemoteDescription]], remove the
4341 // transceiver from the connection's set of transceivers.
4342 if (!transceiver->stopped()) {
4343 continue;
4344 }
Harald Alvestrand85466662021-04-19 21:21:364345 const ContentInfo* local_content = FindMediaSectionForTransceiver(
4346 transceiver->internal(), local_description());
4347 const ContentInfo* remote_content = FindMediaSectionForTransceiver(
4348 transceiver->internal(), remote_description());
Harald Alvestranda474fbf2020-10-01 16:47:234349 if ((local_content && local_content->rejected) ||
4350 (remote_content && remote_content->rejected)) {
4351 RTC_LOG(LS_INFO) << "Dissociating transceiver"
Tomas Gunnarsson7fa8d462021-04-16 12:28:264352 " since the media section is being recycled.";
Harald Alvestranda474fbf2020-10-01 16:47:234353 transceiver->internal()->set_mid(absl::nullopt);
4354 transceiver->internal()->set_mline_index(absl::nullopt);
Tomas Gunnarsson7fa8d462021-04-16 12:28:264355 } else if (!local_content && !remote_content) {
Harald Alvestranda474fbf2020-10-01 16:47:234356 // TODO(bugs.webrtc.org/11973): Consider if this should be removed already
4357 // See https://github.com/w3c/webrtc-pc/issues/2576
4358 RTC_LOG(LS_INFO)
4359 << "Dropping stopped transceiver that was never associated";
Harald Alvestranda474fbf2020-10-01 16:47:234360 }
Tomas Gunnarsson7fa8d462021-04-16 12:28:264361 transceivers()->Remove(transceiver);
Harald Alvestranda474fbf2020-10-01 16:47:234362 }
4363}
4364
4365void SdpOfferAnswerHandler::RemoveUnusedChannels(
4366 const SessionDescription* desc) {
4367 RTC_DCHECK_RUN_ON(signaling_thread());
4368 // Destroy video channel first since it may have a pointer to the
4369 // voice channel.
4370 const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc);
4371 if (!video_info || video_info->rejected) {
Harald Alvestrande15fb152020-10-19 13:28:054372 DestroyTransceiverChannel(rtp_manager()->GetVideoTransceiver());
Harald Alvestranda474fbf2020-10-01 16:47:234373 }
4374
4375 const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc);
4376 if (!audio_info || audio_info->rejected) {
Harald Alvestrande15fb152020-10-19 13:28:054377 DestroyTransceiverChannel(rtp_manager()->GetAudioTransceiver());
Harald Alvestranda474fbf2020-10-01 16:47:234378 }
4379
4380 const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc);
Florent Castellidcb9ffc2021-06-29 12:58:234381 if (!data_info) {
4382 RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA,
4383 "No data channel section in the description.");
4384 error.set_error_detail(RTCErrorDetailType::DATA_CHANNEL_FAILURE);
4385 DestroyDataChannelTransport(error);
4386 } else if (data_info->rejected) {
4387 rtc::StringBuilder sb;
4388 sb << "Rejected data channel with mid=" << data_info->name << ".";
4389
4390 RTCError error(RTCErrorType::OPERATION_ERROR_WITH_DATA, sb.Release());
4391 error.set_error_detail(RTCErrorDetailType::DATA_CHANNEL_FAILURE);
4392 DestroyDataChannelTransport(error);
Harald Alvestranda474fbf2020-10-01 16:47:234393 }
4394}
4395
4396void SdpOfferAnswerHandler::ReportNegotiatedSdpSemantics(
4397 const SessionDescriptionInterface& answer) {
4398 SdpSemanticNegotiated semantics_negotiated;
4399 switch (answer.description()->msid_signaling()) {
4400 case 0:
4401 semantics_negotiated = kSdpSemanticNegotiatedNone;
4402 break;
4403 case cricket::kMsidSignalingMediaSection:
4404 semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan;
4405 break;
4406 case cricket::kMsidSignalingSsrcAttribute:
4407 semantics_negotiated = kSdpSemanticNegotiatedPlanB;
4408 break;
4409 case cricket::kMsidSignalingMediaSection |
4410 cricket::kMsidSignalingSsrcAttribute:
4411 semantics_negotiated = kSdpSemanticNegotiatedMixed;
4412 break;
4413 default:
4414 RTC_NOTREACHED();
4415 }
4416 RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated",
4417 semantics_negotiated, kSdpSemanticNegotiatedMax);
4418}
4419
Harald Alvestrandbc9ca252020-10-05 13:08:414420void SdpOfferAnswerHandler::UpdateEndedRemoteMediaStreams() {
4421 RTC_DCHECK_RUN_ON(signaling_thread());
4422 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove;
Harald Alvestrand6f04b6532020-10-09 11:42:174423 for (size_t i = 0; i < remote_streams_->count(); ++i) {
4424 MediaStreamInterface* stream = remote_streams_->at(i);
Harald Alvestrandbc9ca252020-10-05 13:08:414425 if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) {
4426 streams_to_remove.push_back(stream);
4427 }
4428 }
4429
4430 for (auto& stream : streams_to_remove) {
Harald Alvestrand6f04b6532020-10-09 11:42:174431 remote_streams_->RemoveStream(stream);
Harald Alvestrandbc9ca252020-10-05 13:08:414432 pc_->Observer()->OnRemoveStream(std::move(stream));
4433 }
4434}
4435
4436bool SdpOfferAnswerHandler::UseCandidatesInSessionDescription(
4437 const SessionDescriptionInterface* remote_desc) {
4438 RTC_DCHECK_RUN_ON(signaling_thread());
4439 if (!remote_desc) {
4440 return true;
4441 }
4442 bool ret = true;
4443
4444 for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) {
4445 const IceCandidateCollection* candidates = remote_desc->candidates(m);
4446 for (size_t n = 0; n < candidates->count(); ++n) {
4447 const IceCandidateInterface* candidate = candidates->at(n);
4448 bool valid = false;
4449 if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) {
4450 if (valid) {
4451 RTC_LOG(LS_INFO)
4452 << "UseCandidatesInSessionDescription: Not ready to use "
4453 "candidate.";
4454 }
4455 continue;
4456 }
4457 ret = UseCandidate(candidate);
4458 if (!ret) {
4459 break;
4460 }
4461 }
4462 }
4463 return ret;
4464}
4465
4466bool SdpOfferAnswerHandler::UseCandidate(
4467 const IceCandidateInterface* candidate) {
4468 RTC_DCHECK_RUN_ON(signaling_thread());
Tomas Gunnarsson8cb97062021-02-08 17:57:044469
4470 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
4471
Harald Alvestrandbc9ca252020-10-05 13:08:414472 RTCErrorOr<const cricket::ContentInfo*> result =
4473 FindContentInfo(remote_description(), candidate);
Tomas Gunnarsson8cb97062021-02-08 17:57:044474 if (!result.ok())
Harald Alvestrandbc9ca252020-10-05 13:08:414475 return false;
Tomas Gunnarsson8cb97062021-02-08 17:57:044476
4477 const cricket::Candidate& c = candidate->candidate();
4478 RTCError error = cricket::VerifyCandidate(c);
Tomas Gunnarsson27bc6e22021-02-12 12:16:264479 if (!error.ok()) {
4480 RTC_LOG(LS_WARNING) << "Invalid candidate: " << c.ToString();
4481 return true;
4482 }
Tomas Gunnarsson8cb97062021-02-08 17:57:044483
4484 pc_->AddRemoteCandidate(result.value()->name, c);
4485
Harald Alvestrandbc9ca252020-10-05 13:08:414486 return true;
4487}
4488
4489// We need to check the local/remote description for the Transport instead of
4490// the session, because a new Transport added during renegotiation may have
4491// them unset while the session has them set from the previous negotiation.
4492// Not doing so may trigger the auto generation of transport description and
4493// mess up DTLS identity information, ICE credential, etc.
4494bool SdpOfferAnswerHandler::ReadyToUseRemoteCandidate(
4495 const IceCandidateInterface* candidate,
4496 const SessionDescriptionInterface* remote_desc,
4497 bool* valid) {
4498 RTC_DCHECK_RUN_ON(signaling_thread());
4499 *valid = true;
4500
4501 const SessionDescriptionInterface* current_remote_desc =
4502 remote_desc ? remote_desc : remote_description();
4503
4504 if (!current_remote_desc) {
4505 return false;
4506 }
4507
4508 RTCErrorOr<const cricket::ContentInfo*> result =
4509 FindContentInfo(current_remote_desc, candidate);
4510 if (!result.ok()) {
4511 RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. "
4512 << result.error().message();
4513
4514 *valid = false;
4515 return false;
4516 }
4517
Tomas Gunnarsson45de8b32021-04-02 10:33:394518 return true;
Harald Alvestrandbc9ca252020-10-05 13:08:414519}
4520
Harald Alvestrandbc9ca252020-10-05 13:08:414521RTCErrorOr<const cricket::ContentInfo*> SdpOfferAnswerHandler::FindContentInfo(
4522 const SessionDescriptionInterface* description,
4523 const IceCandidateInterface* candidate) {
Philipp Hancke31e06cb2021-02-26 08:23:534524 if (!candidate->sdp_mid().empty()) {
Harald Alvestrandbc9ca252020-10-05 13:08:414525 auto& contents = description->description()->contents();
4526 auto it = absl::c_find_if(
4527 contents, [candidate](const cricket::ContentInfo& content_info) {
4528 return content_info.mid() == candidate->sdp_mid();
4529 });
4530 if (it == contents.end()) {
4531 return RTCError(
4532 RTCErrorType::INVALID_PARAMETER,
4533 "Mid " + candidate->sdp_mid() +
4534 " specified but no media section with that mid found.");
4535 } else {
4536 return &*it;
4537 }
Philipp Hancke31e06cb2021-02-26 08:23:534538 } else if (candidate->sdp_mline_index() >= 0) {
4539 size_t mediacontent_index =
4540 static_cast<size_t>(candidate->sdp_mline_index());
4541 size_t content_size = description->description()->contents().size();
4542 if (mediacontent_index < content_size) {
4543 return &description->description()->contents()[mediacontent_index];
4544 } else {
4545 return RTCError(RTCErrorType::INVALID_RANGE,
4546 "Media line index (" +
4547 rtc::ToString(candidate->sdp_mline_index()) +
4548 ") out of range (number of mlines: " +
4549 rtc::ToString(content_size) + ").");
4550 }
Harald Alvestrandbc9ca252020-10-05 13:08:414551 }
4552
4553 return RTCError(RTCErrorType::INVALID_PARAMETER,
4554 "Neither sdp_mline_index nor sdp_mid specified.");
4555}
4556
4557RTCError SdpOfferAnswerHandler::CreateChannels(const SessionDescription& desc) {
Markus Handell518669d2021-06-07 11:30:464558 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::CreateChannels");
Harald Alvestrandbc9ca252020-10-05 13:08:414559 // Creating the media channels. Transports should already have been created
4560 // at this point.
4561 RTC_DCHECK_RUN_ON(signaling_thread());
4562 const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc);
4563 if (voice && !voice->rejected &&
Harald Alvestrande15fb152020-10-19 13:28:054564 !rtp_manager()->GetAudioTransceiver()->internal()->channel()) {
Harald Alvestrandbc9ca252020-10-05 13:08:414565 cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name);
4566 if (!voice_channel) {
4567 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4568 "Failed to create voice channel.");
4569 }
Harald Alvestrande15fb152020-10-19 13:28:054570 rtp_manager()->GetAudioTransceiver()->internal()->SetChannel(voice_channel);
Harald Alvestrandbc9ca252020-10-05 13:08:414571 }
4572
4573 const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc);
4574 if (video && !video->rejected &&
Harald Alvestrande15fb152020-10-19 13:28:054575 !rtp_manager()->GetVideoTransceiver()->internal()->channel()) {
Harald Alvestrandbc9ca252020-10-05 13:08:414576 cricket::VideoChannel* video_channel = CreateVideoChannel(video->name);
4577 if (!video_channel) {
4578 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4579 "Failed to create video channel.");
4580 }
Harald Alvestrande15fb152020-10-19 13:28:054581 rtp_manager()->GetVideoTransceiver()->internal()->SetChannel(video_channel);
Harald Alvestrandbc9ca252020-10-05 13:08:414582 }
4583
4584 const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc);
Florent Castelli516e2842021-04-19 13:29:504585 if (data && !data->rejected &&
4586 !data_channel_controller()->data_channel_transport()) {
Harald Alvestrandbc9ca252020-10-05 13:08:414587 if (!CreateDataChannel(data->name)) {
4588 LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR,
4589 "Failed to create data channel.");
4590 }
4591 }
4592
4593 return RTCError::OK();
4594}
4595
4596// TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
4597cricket::VoiceChannel* SdpOfferAnswerHandler::CreateVoiceChannel(
4598 const std::string& mid) {
Markus Handell518669d2021-06-07 11:30:464599 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::CreateVoiceChannel");
Harald Alvestrandbc9ca252020-10-05 13:08:414600 RTC_DCHECK_RUN_ON(signaling_thread());
Tomas Gunnarsson00f4fd92021-04-08 12:39:474601 if (!channel_manager()->media_engine())
4602 return nullptr;
4603
Harald Alvestrandbc9ca252020-10-05 13:08:414604 RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid);
4605
4606 // TODO(bugs.webrtc.org/11992): CreateVoiceChannel internally switches to the
4607 // worker thread. We shouldn't be using the |call_ptr_| hack here but simply
4608 // be on the worker thread and use |call_| (update upstream code).
Tomas Gunnarssoneb9c3f22021-04-19 10:53:094609 return channel_manager()->CreateVoiceChannel(
Tomas Gunnarsson3278a712021-03-30 15:23:424610 pc_->call_ptr(), pc_->configuration()->media_config, rtp_transport,
4611 signaling_thread(), mid, pc_->SrtpRequired(), pc_->GetCryptoOptions(),
4612 &ssrc_generator_, audio_options());
Harald Alvestrandbc9ca252020-10-05 13:08:414613}
4614
4615// TODO(steveanton): Perhaps this should be managed by the RtpTransceiver.
4616cricket::VideoChannel* SdpOfferAnswerHandler::CreateVideoChannel(
4617 const std::string& mid) {
Markus Handell518669d2021-06-07 11:30:464618 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::CreateVideoChannel");
Harald Alvestrandbc9ca252020-10-05 13:08:414619 RTC_DCHECK_RUN_ON(signaling_thread());
Tomas Gunnarsson00f4fd92021-04-08 12:39:474620 if (!channel_manager()->media_engine())
4621 return nullptr;
4622
Tomas Gunnarsson92eebef2021-02-10 12:05:444623 // NOTE: This involves a non-ideal hop (Invoke) over to the network thread.
Harald Alvestrandbc9ca252020-10-05 13:08:414624 RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid);
4625
4626 // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to the
4627 // worker thread. We shouldn't be using the |call_ptr_| hack here but simply
4628 // be on the worker thread and use |call_| (update upstream code).
Tomas Gunnarssoneb9c3f22021-04-19 10:53:094629 return channel_manager()->CreateVideoChannel(
Tomas Gunnarsson3278a712021-03-30 15:23:424630 pc_->call_ptr(), pc_->configuration()->media_config, rtp_transport,
4631 signaling_thread(), mid, pc_->SrtpRequired(), pc_->GetCryptoOptions(),
4632 &ssrc_generator_, video_options(),
4633 video_bitrate_allocator_factory_.get());
Harald Alvestrandbc9ca252020-10-05 13:08:414634}
4635
4636bool SdpOfferAnswerHandler::CreateDataChannel(const std::string& mid) {
4637 RTC_DCHECK_RUN_ON(signaling_thread());
Florent Castelli516e2842021-04-19 13:29:504638 if (!pc_->network_thread()->Invoke<bool>(RTC_FROM_HERE, [this, &mid] {
4639 RTC_DCHECK_RUN_ON(pc_->network_thread());
4640 return pc_->SetupDataChannelTransport_n(mid);
4641 })) {
4642 return false;
Harald Alvestrandbc9ca252020-10-05 13:08:414643 }
Florent Castelli516e2842021-04-19 13:29:504644 // TODO(tommi): Is this necessary? SetupDataChannelTransport_n() above
4645 // will have queued up updating the transport name on the signaling thread
4646 // and could update the mid at the same time. This here is synchronous
4647 // though, but it changes the state of PeerConnection and makes it be
4648 // out of sync (transport name not set while the mid is set).
4649 pc_->SetSctpDataMid(mid);
Tomas Gunnarsson00f4fd92021-04-08 12:39:474650 return true;
Harald Alvestrandbc9ca252020-10-05 13:08:414651}
4652
4653void SdpOfferAnswerHandler::DestroyTransceiverChannel(
4654 rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>
4655 transceiver) {
Markus Handell518669d2021-06-07 11:30:464656 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DestroyTransceiverChannel");
Harald Alvestrandbc9ca252020-10-05 13:08:414657 RTC_DCHECK(transceiver);
Tommife041642021-04-07 08:08:284658 RTC_LOG_THREAD_BLOCK_COUNT();
4659
4660 // TODO(tommi): We're currently on the signaling thread.
4661 // There are multiple hops to the worker ahead.
4662 // Consider if we can make the call to SetChannel() on the worker thread
4663 // (and require that to be the context it's always called in) and also
4664 // call DestroyChannelInterface there, since it also needs to hop to the
4665 // worker.
Harald Alvestrandbc9ca252020-10-05 13:08:414666
4667 cricket::ChannelInterface* channel = transceiver->internal()->channel();
Tommife041642021-04-07 08:08:284668 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0);
Harald Alvestrandbc9ca252020-10-05 13:08:414669 if (channel) {
Tommife041642021-04-07 08:08:284670 // TODO(tommi): VideoRtpReceiver::SetMediaChannel blocks and jumps to the
Tommi99c8a802021-04-27 13:00:004671 // worker thread. When being set to nullptr, there are additional
Harald Alvestrand7af57c62021-04-16 11:12:144672 // blocking calls to e.g. ClearRecordableEncodedFrameCallback which triggers
4673 // another blocking call or Stop() for video channels.
Tommi99c8a802021-04-27 13:00:004674 // The channel object also needs to be de-initialized on the network thread
4675 // so if ownership of the channel object lies with the transceiver, we could
4676 // un-set the channel pointer and uninitialize/destruct the channel object
4677 // at the same time, rather than in separate steps.
Harald Alvestrandbc9ca252020-10-05 13:08:414678 transceiver->internal()->SetChannel(nullptr);
Tommife041642021-04-07 08:08:284679 // TODO(tommi): All channel objects end up getting deleted on the
Tommi99c8a802021-04-27 13:00:004680 // worker thread (ideally should be on the network thread but the
4681 // MediaChannel objects are tied to the worker. Can the teardown be done
4682 // asynchronously across the threads rather than blocking?
Harald Alvestrandbc9ca252020-10-05 13:08:414683 DestroyChannelInterface(channel);
4684 }
4685}
4686
Florent Castellidcb9ffc2021-06-29 12:58:234687void SdpOfferAnswerHandler::DestroyDataChannelTransport(RTCError error) {
Harald Alvestrandbc9ca252020-10-05 13:08:414688 RTC_DCHECK_RUN_ON(signaling_thread());
Tomas Gunnarssond69e0702021-04-07 13:14:434689 const bool has_sctp = pc_->sctp_mid().has_value();
Harald Alvestrandbc9ca252020-10-05 13:08:414690
Harald Alvestrand7af57c62021-04-16 11:12:144691 if (has_sctp)
Florent Castellidcb9ffc2021-06-29 12:58:234692 data_channel_controller()->OnTransportChannelClosed(error);
Tomas Gunnarssond69e0702021-04-07 13:14:434693
4694 pc_->network_thread()->Invoke<void>(RTC_FROM_HERE, [this] {
4695 RTC_DCHECK_RUN_ON(pc_->network_thread());
4696 pc_->TeardownDataChannelTransport_n();
4697 });
4698
4699 if (has_sctp)
Harald Alvestrand653429c2020-10-19 16:05:204700 pc_->ResetSctpDataMid();
Harald Alvestrandbc9ca252020-10-05 13:08:414701}
4702
4703void SdpOfferAnswerHandler::DestroyChannelInterface(
4704 cricket::ChannelInterface* channel) {
Markus Handell518669d2021-06-07 11:30:464705 TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DestroyChannelInterface");
Tomas Gunnarsson00f4fd92021-04-08 12:39:474706 RTC_DCHECK_RUN_ON(signaling_thread());
4707 RTC_DCHECK(channel_manager()->media_engine());
4708 RTC_DCHECK(channel);
4709
Harald Alvestrandbc9ca252020-10-05 13:08:414710 // TODO(bugs.webrtc.org/11992): All the below methods should be called on the
4711 // worker thread. (they switch internally anyway). Change
4712 // DestroyChannelInterface to either be called on the worker thread, or do
4713 // this asynchronously on the worker.
Tommife041642021-04-07 08:08:284714 RTC_LOG_THREAD_BLOCK_COUNT();
4715
Harald Alvestrandbc9ca252020-10-05 13:08:414716 switch (channel->media_type()) {
4717 case cricket::MEDIA_TYPE_AUDIO:
4718 channel_manager()->DestroyVoiceChannel(
4719 static_cast<cricket::VoiceChannel*>(channel));
4720 break;
4721 case cricket::MEDIA_TYPE_VIDEO:
4722 channel_manager()->DestroyVideoChannel(
4723 static_cast<cricket::VideoChannel*>(channel));
4724 break;
4725 case cricket::MEDIA_TYPE_DATA:
Harald Alvestrand7af57c62021-04-16 11:12:144726 RTC_NOTREACHED()
4727 << "Trying to destroy datachannel through DestroyChannelInterface";
Harald Alvestrandbc9ca252020-10-05 13:08:414728 break;
4729 default:
4730 RTC_NOTREACHED() << "Unknown media type: " << channel->media_type();
4731 break;
4732 }
Tommife041642021-04-07 08:08:284733
4734 // TODO(tommi): Figure out why we can get 2 blocking calls when running
4735 // PeerConnectionCryptoTest.CreateAnswerWithDifferentSslRoles.
Tomas Gunnarsson60e67482021-04-07 17:12:134736 // and 3 when running
4737 // PeerConnectionCryptoTest.CreateAnswerWithDifferentSslRoles
4738 // RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(1);
Harald Alvestrandbc9ca252020-10-05 13:08:414739}
4740
4741void SdpOfferAnswerHandler::DestroyAllChannels() {
4742 RTC_DCHECK_RUN_ON(signaling_thread());
Harald Alvestrande15fb152020-10-19 13:28:054743 if (!transceivers()) {
4744 return;
4745 }
Tommife041642021-04-07 08:08:284746
4747 RTC_LOG_THREAD_BLOCK_COUNT();
4748
Harald Alvestrandbc9ca252020-10-05 13:08:414749 // Destroy video channels first since they may have a pointer to a voice
4750 // channel.
Tommife041642021-04-07 08:08:284751 auto list = transceivers()->List();
4752 RTC_DCHECK_BLOCK_COUNT_NO_MORE_THAN(0);
4753
4754 for (const auto& transceiver : list) {
Harald Alvestrandbc9ca252020-10-05 13:08:414755 if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) {
4756 DestroyTransceiverChannel(transceiver);
4757 }
4758 }
Tommife041642021-04-07 08:08:284759 for (const auto& transceiver : list) {
Harald Alvestrandbc9ca252020-10-05 13:08:414760 if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
4761 DestroyTransceiverChannel(transceiver);
4762 }
4763 }
Tommife041642021-04-07 08:08:284764
Florent Castellidcb9ffc2021-06-29 12:58:234765 DestroyDataChannelTransport({});
Harald Alvestrandbc9ca252020-10-05 13:08:414766}
4767
4768void SdpOfferAnswerHandler::GenerateMediaDescriptionOptions(
4769 const SessionDescriptionInterface* session_desc,
4770 RtpTransceiverDirection audio_direction,
4771 RtpTransceiverDirection video_direction,
4772 absl::optional<size_t>* audio_index,
4773 absl::optional<size_t>* video_index,
4774 absl::optional<size_t>* data_index,
4775 cricket::MediaSessionOptions* session_options) {
4776 RTC_DCHECK_RUN_ON(signaling_thread());
4777 for (const cricket::ContentInfo& content :
4778 session_desc->description()->contents()) {
4779 if (IsAudioContent(&content)) {
4780 // If we already have an audio m= section, reject this extra one.
4781 if (*audio_index) {
4782 session_options->media_description_options.push_back(
4783 cricket::MediaDescriptionOptions(
4784 cricket::MEDIA_TYPE_AUDIO, content.name,
4785 RtpTransceiverDirection::kInactive, /*stopped=*/true));
4786 } else {
4787 bool stopped = (audio_direction == RtpTransceiverDirection::kInactive);
4788 session_options->media_description_options.push_back(
4789 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO,
4790 content.name, audio_direction,
4791 stopped));
4792 *audio_index = session_options->media_description_options.size() - 1;
4793 }
4794 session_options->media_description_options.back().header_extensions =
4795 channel_manager()->GetSupportedAudioRtpHeaderExtensions();
4796 } else if (IsVideoContent(&content)) {
4797 // If we already have an video m= section, reject this extra one.
4798 if (*video_index) {
4799 session_options->media_description_options.push_back(
4800 cricket::MediaDescriptionOptions(
4801 cricket::MEDIA_TYPE_VIDEO, content.name,
4802 RtpTransceiverDirection::kInactive, /*stopped=*/true));
4803 } else {
4804 bool stopped = (video_direction == RtpTransceiverDirection::kInactive);
4805 session_options->media_description_options.push_back(
4806 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO,
4807 content.name, video_direction,
4808 stopped));
4809 *video_index = session_options->media_description_options.size() - 1;
4810 }
4811 session_options->media_description_options.back().header_extensions =
4812 channel_manager()->GetSupportedVideoRtpHeaderExtensions();
Philipp Hancke4e8c1152020-10-13 10:43:154813 } else if (IsUnsupportedContent(&content)) {
4814 session_options->media_description_options.push_back(
4815 cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_UNSUPPORTED,
4816 content.name,
4817 RtpTransceiverDirection::kInactive,
4818 /*stopped=*/true));
Harald Alvestrandbc9ca252020-10-05 13:08:414819 } else {
4820 RTC_DCHECK(IsDataContent(&content));
4821 // If we already have an data m= section, reject this extra one.
4822 if (*data_index) {
4823 session_options->media_description_options.push_back(
4824 GetMediaDescriptionOptionsForRejectedData(content.name));
4825 } else {
4826 session_options->media_description_options.push_back(
4827 GetMediaDescriptionOptionsForActiveData(content.name));
4828 *data_index = session_options->media_description_options.size() - 1;
4829 }
4830 }
4831 }
4832}
4833
4834cricket::MediaDescriptionOptions
4835SdpOfferAnswerHandler::GetMediaDescriptionOptionsForActiveData(
4836 const std::string& mid) const {
4837 RTC_DCHECK_RUN_ON(signaling_thread());
4838 // Direction for data sections is meaningless, but legacy endpoints might
4839 // expect sendrecv.
4840 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
4841 RtpTransceiverDirection::kSendRecv,
4842 /*stopped=*/false);
Harald Alvestrandbc9ca252020-10-05 13:08:414843 return options;
4844}
4845
4846cricket::MediaDescriptionOptions
4847SdpOfferAnswerHandler::GetMediaDescriptionOptionsForRejectedData(
4848 const std::string& mid) const {
4849 RTC_DCHECK_RUN_ON(signaling_thread());
4850 cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid,
4851 RtpTransceiverDirection::kInactive,
4852 /*stopped=*/true);
Harald Alvestrandbc9ca252020-10-05 13:08:414853 return options;
4854}
4855
Taylor Brandstetterd0acbd82021-01-25 21:44:554856bool SdpOfferAnswerHandler::UpdatePayloadTypeDemuxingState(
Henrik Boströmf8187e02021-04-26 19:04:264857 cricket::ContentSource source,
4858 const std::map<std::string, const cricket::ContentGroup*>&
4859 bundle_groups_by_mid) {
Markus Handell518669d2021-06-07 11:30:464860 TRACE_EVENT0("webrtc",
4861 "SdpOfferAnswerHandler::UpdatePayloadTypeDemuxingState");
Harald Alvestrandbc9ca252020-10-05 13:08:414862 RTC_DCHECK_RUN_ON(signaling_thread());
4863 // We may need to delete any created default streams and disable creation of
4864 // new ones on the basis of payload type. This is needed to avoid SSRC
4865 // collisions in Call's RtpDemuxer, in the case that a transceiver has
4866 // created a default stream, and then some other channel gets the SSRC
Taylor Brandstetterd3ef4992020-10-16 01:22:574867 // signaled in the corresponding Unified Plan "m=" section. Specifically, we
4868 // need to disable payload type based demuxing when two bundled "m=" sections
4869 // are using the same payload type(s). For more context
Harald Alvestrandbc9ca252020-10-05 13:08:414870 // see https://bugs.chromium.org/p/webrtc/issues/detail?id=11477
4871 const SessionDescriptionInterface* sdesc =
4872 (source == cricket::CS_LOCAL ? local_description()
4873 : remote_description());
Henrik Boströmf8187e02021-04-26 19:04:264874 struct PayloadTypes {
4875 std::set<int> audio_payload_types;
4876 std::set<int> video_payload_types;
Henrik Boström4ea80f32021-06-09 08:29:504877 bool pt_demuxing_possible_audio = true;
4878 bool pt_demuxing_possible_video = true;
Henrik Boströmf8187e02021-04-26 19:04:264879 };
4880 std::map<const cricket::ContentGroup*, PayloadTypes> payload_types_by_bundle;
Henrik Boström4ea80f32021-06-09 08:29:504881 // If the MID is missing from *any* receiving m= section, this is set to true.
4882 bool mid_header_extension_missing_audio = false;
4883 bool mid_header_extension_missing_video = false;
Harald Alvestrandbc9ca252020-10-05 13:08:414884 for (auto& content_info : sdesc->description()->contents()) {
Henrik Boströmf8187e02021-04-26 19:04:264885 auto it = bundle_groups_by_mid.find(content_info.name);
4886 const cricket::ContentGroup* bundle_group =
4887 it != bundle_groups_by_mid.end() ? it->second : nullptr;
Taylor Brandstetterd3ef4992020-10-16 01:22:574888 // If this m= section isn't bundled, it's safe to demux by payload type
4889 // since other m= sections using the same payload type will also be using
4890 // different transports.
Henrik Boströmf8187e02021-04-26 19:04:264891 if (!bundle_group) {
Taylor Brandstetterd3ef4992020-10-16 01:22:574892 continue;
4893 }
Henrik Boströmf8187e02021-04-26 19:04:264894 PayloadTypes* payload_types = &payload_types_by_bundle[bundle_group];
Harald Alvestrandbc9ca252020-10-05 13:08:414895 if (content_info.rejected ||
4896 (source == cricket::ContentSource::CS_LOCAL &&
4897 !RtpTransceiverDirectionHasRecv(
4898 content_info.media_description()->direction())) ||
4899 (source == cricket::ContentSource::CS_REMOTE &&
4900 !RtpTransceiverDirectionHasSend(
4901 content_info.media_description()->direction()))) {
4902 // Ignore transceivers that are not receiving.
4903 continue;
4904 }
4905 switch (content_info.media_description()->type()) {
Taylor Brandstetterd3ef4992020-10-16 01:22:574906 case cricket::MediaType::MEDIA_TYPE_AUDIO: {
Henrik Boström4ea80f32021-06-09 08:29:504907 if (!mid_header_extension_missing_audio) {
4908 mid_header_extension_missing_audio =
4909 !ContentHasHeaderExtension(content_info, RtpExtension::kMidUri);
4910 }
Taylor Brandstetterd3ef4992020-10-16 01:22:574911 const cricket::AudioContentDescription* audio_desc =
4912 content_info.media_description()->as_audio();
4913 for (const cricket::AudioCodec& audio : audio_desc->codecs()) {
Henrik Boströmf8187e02021-04-26 19:04:264914 if (payload_types->audio_payload_types.count(audio.id)) {
Taylor Brandstetterd3ef4992020-10-16 01:22:574915 // Two m= sections are using the same payload type, thus demuxing
4916 // by payload type is not possible.
Henrik Boström4ea80f32021-06-09 08:29:504917 payload_types->pt_demuxing_possible_audio = false;
Taylor Brandstetterd3ef4992020-10-16 01:22:574918 }
Henrik Boströmf8187e02021-04-26 19:04:264919 payload_types->audio_payload_types.insert(audio.id);
Taylor Brandstetterd3ef4992020-10-16 01:22:574920 }
Harald Alvestrandbc9ca252020-10-05 13:08:414921 break;
Taylor Brandstetterd3ef4992020-10-16 01:22:574922 }
4923 case cricket::MediaType::MEDIA_TYPE_VIDEO: {
Henrik Boström4ea80f32021-06-09 08:29:504924 if (!mid_header_extension_missing_video) {
4925 mid_header_extension_missing_video =
4926 !ContentHasHeaderExtension(content_info, RtpExtension::kMidUri);
4927 }
Taylor Brandstetterd3ef4992020-10-16 01:22:574928 const cricket::VideoContentDescription* video_desc =
4929 content_info.media_description()->as_video();
4930 for (const cricket::VideoCodec& video : video_desc->codecs()) {
Henrik Boströmf8187e02021-04-26 19:04:264931 if (payload_types->video_payload_types.count(video.id)) {
Taylor Brandstetterd3ef4992020-10-16 01:22:574932 // Two m= sections are using the same payload type, thus demuxing
4933 // by payload type is not possible.
Henrik Boström4ea80f32021-06-09 08:29:504934 payload_types->pt_demuxing_possible_video = false;
Taylor Brandstetterd3ef4992020-10-16 01:22:574935 }
Henrik Boströmf8187e02021-04-26 19:04:264936 payload_types->video_payload_types.insert(video.id);
Taylor Brandstetterd3ef4992020-10-16 01:22:574937 }
Harald Alvestrandbc9ca252020-10-05 13:08:414938 break;
Taylor Brandstetterd3ef4992020-10-16 01:22:574939 }
Harald Alvestrandbc9ca252020-10-05 13:08:414940 default:
4941 // Ignore data channels.
4942 continue;
4943 }
4944 }
Harald Alvestrandbc9ca252020-10-05 13:08:414945
4946 // Gather all updates ahead of time so that all channels can be updated in a
4947 // single Invoke; necessary due to thread guards.
Taylor Brandstetterd0acbd82021-01-25 21:44:554948 std::vector<std::pair<RtpTransceiverDirection, cricket::ChannelInterface*>>
4949 channels_to_update;
Harald Alvestrand85466662021-04-19 21:21:364950 for (const auto& transceiver : transceivers()->ListInternal()) {
4951 cricket::ChannelInterface* channel = transceiver->channel();
Harald Alvestrandbc9ca252020-10-05 13:08:414952 const ContentInfo* content =
4953 FindMediaSectionForTransceiver(transceiver, sdesc);
4954 if (!channel || !content) {
4955 continue;
4956 }
4957 RtpTransceiverDirection local_direction =
4958 content->media_description()->direction();
4959 if (source == cricket::CS_REMOTE) {
4960 local_direction = RtpTransceiverDirectionReversed(local_direction);
4961 }
Harald Alvestrand85466662021-04-19 21:21:364962 channels_to_update.emplace_back(local_direction, transceiver->channel());
Harald Alvestrandbc9ca252020-10-05 13:08:414963 }
Taylor Brandstetterd0acbd82021-01-25 21:44:554964
4965 if (channels_to_update.empty()) {
4966 return true;
4967 }
Henrik Boström4ea80f32021-06-09 08:29:504968
4969 // In Unified Plan, payload type demuxing is useful for legacy endpoints that
4970 // don't support the MID header extension, but it can also cause incorrrect
4971 // forwarding of packets when going from one m= section to multiple m=
4972 // sections in the same BUNDLE. This only happens if media arrives prior to
4973 // negotiation, but this can cause missing video and unsignalled ssrc bugs
4974 // severe enough to warrant disabling PT demuxing in such cases. Therefore, if
4975 // a MID header extension is present on all m= sections for a given kind
4976 // (audio/video) then we use that as an OK to disable payload type demuxing in
4977 // BUNDLEs of that kind. However if PT demuxing was ever turned on (e.g. MID
4978 // was ever removed on ANY m= section of that kind) then we continue to allow
4979 // PT demuxing in order to prevent disabling it in follow-up O/A exchanges and
4980 // allowing early media by PT.
4981 bool bundled_pt_demux_allowed_audio = !IsUnifiedPlan() ||
4982 mid_header_extension_missing_audio ||
4983 pt_demuxing_has_been_used_audio_;
4984 bool bundled_pt_demux_allowed_video = !IsUnifiedPlan() ||
4985 mid_header_extension_missing_video ||
4986 pt_demuxing_has_been_used_video_;
4987 // Kill switch for the above change.
4988 if (field_trial::IsEnabled(kAlwaysAllowPayloadTypeDemuxingFieldTrialName)) {
4989 // TODO(https://crbug.com/webrtc/12814): If disabling PT-based demux does
4990 // not trigger regressions, remove this kill switch.
4991 bundled_pt_demux_allowed_audio = true;
4992 bundled_pt_demux_allowed_video = true;
4993 }
4994
Taylor Brandstetterd0acbd82021-01-25 21:44:554995 return pc_->worker_thread()->Invoke<bool>(
Henrik Boströmf8187e02021-04-26 19:04:264996 RTC_FROM_HERE,
Henrik Boström4ea80f32021-06-09 08:29:504997 [&channels_to_update, &bundle_groups_by_mid, &payload_types_by_bundle,
4998 bundled_pt_demux_allowed_audio, bundled_pt_demux_allowed_video,
4999 pt_demuxing_has_been_used_audio = &pt_demuxing_has_been_used_audio_,
5000 pt_demuxing_has_been_used_video = &pt_demuxing_has_been_used_video_]() {
Taylor Brandstetterd0acbd82021-01-25 21:44:555001 for (const auto& it : channels_to_update) {
5002 RtpTransceiverDirection local_direction = it.first;
5003 cricket::ChannelInterface* channel = it.second;
5004 cricket::MediaType media_type = channel->media_type();
Henrik Boströmf8187e02021-04-26 19:04:265005 auto bundle_it = bundle_groups_by_mid.find(channel->content_name());
5006 const cricket::ContentGroup* bundle_group =
5007 bundle_it != bundle_groups_by_mid.end() ? bundle_it->second
5008 : nullptr;
Taylor Brandstetterd0acbd82021-01-25 21:44:555009 if (media_type == cricket::MediaType::MEDIA_TYPE_AUDIO) {
Henrik Boström4ea80f32021-06-09 08:29:505010 bool pt_demux_enabled =
5011 RtpTransceiverDirectionHasRecv(local_direction) &&
5012 (!bundle_group || (bundled_pt_demux_allowed_audio &&
5013 payload_types_by_bundle[bundle_group]
5014 .pt_demuxing_possible_audio));
5015 if (pt_demux_enabled) {
5016 *pt_demuxing_has_been_used_audio = true;
5017 }
5018 if (!channel->SetPayloadTypeDemuxingEnabled(pt_demux_enabled)) {
Taylor Brandstetterd0acbd82021-01-25 21:44:555019 return false;
5020 }
5021 } else if (media_type == cricket::MediaType::MEDIA_TYPE_VIDEO) {
Henrik Boström4ea80f32021-06-09 08:29:505022 bool pt_demux_enabled =
5023 RtpTransceiverDirectionHasRecv(local_direction) &&
5024 (!bundle_group || (bundled_pt_demux_allowed_video &&
5025 payload_types_by_bundle[bundle_group]
5026 .pt_demuxing_possible_video));
5027 if (pt_demux_enabled) {
5028 *pt_demuxing_has_been_used_video = true;
5029 }
5030 if (!channel->SetPayloadTypeDemuxingEnabled(pt_demux_enabled)) {
Taylor Brandstetterd0acbd82021-01-25 21:44:555031 return false;
5032 }
5033 }
5034 }
5035 return true;
5036 });
Harald Alvestrandbc9ca252020-10-05 13:08:415037}
5038
Harald Alvestrandcdcfab02020-09-28 13:02:075039} // namespace webrtc